in src/Sarif.Viewer.VisualStudio.Core/CodeAnalysisResultManager.cs [292:369]
internal string CreateFileFromContents(IDictionary<string, ArtifactDetailsModel> fileDetailsDictionary, string fileName)
{
if (!fileDetailsDictionary.TryGetValue(fileName, out ArtifactDetailsModel fileData))
{
return null;
}
if (!fileData.HasContent)
{
return null;
}
string contents = fileData.GetContents();
if (string.IsNullOrEmpty(contents))
{
// artifact doesn't have embedded contents
return null;
}
string finalPath = this.temporaryFilePath;
// If the file path already starts with the temporary location,
// that means we've already built the temporary file, so we can
// just open it.
if (fileName.StartsWith(finalPath))
{
finalPath = fileName;
}
else
{
// Else we have to create a location under the temp path.
// Strip off the leading drive letter and backslash (e.g., "C:\"), if present.
if (Path.IsPathRooted(fileName))
{
string pathRoot = Path.GetPathRoot(fileName);
fileName = fileName.Substring(pathRoot.Length);
}
if (fileName.StartsWith("/") || fileName.StartsWith("\\"))
{
fileName = fileName.Substring(1);
}
// In this code path, we are working with a non-file system URI
// where the log file contains embedded SARIF content
if (Uri.TryCreate(fileName, UriKind.RelativeOrAbsolute, out Uri uri) &&
uri.IsAbsoluteUri &&
!uri.IsFile)
{
fileName = Guid.NewGuid().ToString();
}
// Combine all paths into the final.
// Sha256Hash is guaranteed to exist. When SARIF file is read, only files
// with Sha256 hashes are added to the FileDetails dictionary.
finalPath = Path.Combine(finalPath, fileData.Sha256Hash, fileName);
}
string directory = Path.GetDirectoryName(finalPath);
Directory.CreateDirectory(directory);
if (!this._fileSystem.FileExists(finalPath))
{
this._fileSystem.FileWriteAllText(finalPath, contents);
// File should be readonly, because it is embedded.
this._fileSystem.FileSetAttributes(finalPath, FileAttributes.ReadOnly);
}
if (!fileDetailsDictionary.ContainsKey(finalPath))
{
// Add another key to our file data object, so that we can
// find it if the user closes the window and reopens it.
fileDetailsDictionary.Add(finalPath, fileData);
}
return finalPath;
}