private static async Task FindBinaryFromServerAsync()

in src/Microsoft.Diagnostics.Runtime/src/Implementation/SymbolServerLocator.cs [163:241]


        private static async Task<string?> FindBinaryFromServerAsync(string server, string cache, string indexPath)
        {
            // There are three ways symbol files can be indexed.  Start looking for each one.
            string fullDestPath = Path.Combine(cache, indexPath);

            // First, check for the compressed location.  This is the one we really want to download.
            string compressedFilePath = indexPath.Substring(0, indexPath.Length - 1) + "_";
            string compressedFileTarget = Path.Combine(cache, compressedFilePath);

            TryDeleteFile(compressedFileTarget);
            Task<string?> compressedFilePathDownload = GetPhysicalFileFromServerAsync(server, compressedFilePath, compressedFileTarget);

            // Second, check if the raw file itself is indexed, uncompressed.
            Task<string?> rawFileDownload = GetPhysicalFileFromServerAsync(server, indexPath, fullDestPath);

            // Last, check for a redirection link.
            string filePtrSigPath = Path.Combine(Path.GetDirectoryName(indexPath)!, "file.ptr");
            Task<string?> filePtrDownload = GetPhysicalFileFromServerAsync(server, filePtrSigPath, fullDestPath, true);

            // Handle compressed download.
            string? result = await compressedFilePathDownload.ConfigureAwait(false);
            if (result != null)
            {
                try
                {
                    // Decompress it
                    Command.Run("Expand " + Command.Quote(result) + " " + Command.Quote(fullDestPath));
                    Trace($"Found '{Path.GetFileName(indexPath)}' on server '{server}'.  Copied to '{fullDestPath}'.");
                    return fullDestPath;
                }
                catch (Exception e)
                {
                    Trace($"Exception encountered while expanding file '{result}': {e.Message}");
                }
                finally
                {
                    if (File.Exists(result))
                        File.Delete(result);
                }
            }

            // Handle uncompressed download.
            result = await rawFileDownload.ConfigureAwait(false);
            if (result != null)
            {
                Trace($"Found '{Path.GetFileName(indexPath)}' on server '{server}'.  Copied to '{result}'.");
                return result;
            }

            // Handle redirection case.
            string filePtrData = (await filePtrDownload.ConfigureAwait(false))?.Trim() ?? string.Empty;
            if (filePtrData.StartsWith("PATH:"))
                filePtrData = filePtrData.Substring(5);

            if (!filePtrData.StartsWith("MSG:") && File.Exists(filePtrData))
            {
                try
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(fullDestPath)!);
                    using (FileStream input = File.OpenRead(filePtrData))
                    using (FileStream output = File.OpenWrite(fullDestPath))
                        await input.CopyToAsync(output).ConfigureAwait(false);

                    Trace($"Found '{Path.GetFileName(indexPath)}' on server '{server}'.  Copied to '{fullDestPath}'.");
                    return fullDestPath;
                }
                catch (Exception)
                {
                    Trace($"Error copying from file.ptr: content '{filePtrData}' from '{filePtrSigPath}' to '{fullDestPath}'.");
                }
            }
            else if (!string.IsNullOrWhiteSpace(filePtrData))
            {
                Trace($"Error resolving file.ptr: content '{filePtrData}' from '{filePtrSigPath}'.");
            }

            Trace($"No file matching '{Path.GetFileName(indexPath)}' found on server '{server}'.");
            return null;
        }