// Contents are embedded in SARIF. Create a file from these contents.
        internal string CreateFileFromContents(string fileName)
        {
            var fileData = FileDetails[fileName];

            string finalPath = 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 we have to create a location under the temp path.
            else
            {
                // 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);
                }

                // 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 (!_fileSystem.FileExists(finalPath))
            {
                string contents = fileData.GetContents();
                _fileSystem.WriteAllText(finalPath, contents);
                // File should be readonly, because it is embedded.
                _fileSystem.SetAttributes(finalPath, FileAttributes.ReadOnly);
            }

            if (!FileDetails.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.
                FileDetails.Add(finalPath, fileData);
            }

            return(finalPath);
        }
Exemplo n.º 2
0
        public bool TryRebaselineCurrentSarifError(string uriBaseId, string originalFilename)
        {
            if (CurrentSarifError == null)
            {
                return(false);
            }

            string rebaselinedFile;

            if (FileDetails.ContainsKey(originalFilename))
            {
                // File contents embedded in SARIF.
                rebaselinedFile = CreateFileFromContents(originalFilename);
            }
            else
            {
                if (Uri.IsWellFormedUriString(originalFilename, UriKind.Absolute))
                {
                    // File needs to be downloaded.
                    rebaselinedFile = DownloadFile(originalFilename);
                }
                else
                {
                    // User needs to locate file.
                    rebaselinedFile = GetRebaselinedFileName(uriBaseId, originalFilename);
                }

                if (String.IsNullOrEmpty(rebaselinedFile) || originalFilename.Equals(rebaselinedFile, StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }
            }

            CurrentSarifError.RemapFilePath(originalFilename, rebaselinedFile);
            return(true);
        }
        public bool TryRebaselineAllSarifErrors(string uriBaseId, string originalFilename)
        {
            if (CurrentSarifResult == null)
            {
                return(false);
            }

            string rebaselinedFile = null;

            if (FileDetails.ContainsKey(originalFilename))
            {
                // File contents embedded in SARIF.
                rebaselinedFile = CreateFileFromContents(originalFilename);
            }
            else
            {
                Uri uri = null;

                if (Uri.TryCreate(originalFilename, UriKind.Absolute, out uri) && uri.IsHttpScheme())
                {
                    bool allow = _allowedDownloadHosts.Contains(uri.Host);

                    // File needs to be downloaded, prompt for confirmation if host is not already allowed
                    if (!allow)
                    {
                        bool alwaysAllow;
                        MessageDialogCommand result = MessageDialog.Show(Resources.ConfirmDownloadDialog_Title,
                                                                         string.Format(Resources.ConfirmDownloadDialog_Message, uri),
                                                                         MessageDialogCommandSet.YesNo,
                                                                         string.Format(Resources.ConfirmDownloadDialog_CheckboxLabel, uri.Host),
                                                                         out alwaysAllow);

                        if (result != MessageDialogCommand.No)
                        {
                            allow = true;

                            if (alwaysAllow)
                            {
                                AddAllowedDownloadHost(uri.Host);
                            }
                        }
                    }

                    if (allow)
                    {
                        rebaselinedFile = DownloadFile(originalFilename);
                    }
                }
                else
                {
                    // User needs to locate file.
                    rebaselinedFile = GetRebaselinedFileName(uriBaseId, originalFilename);
                }

                if (String.IsNullOrEmpty(rebaselinedFile) || originalFilename.Equals(rebaselinedFile, StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }
            }

            // Update all the paths in this result set
            RemapFileNames(originalFilename, rebaselinedFile);
            return(true);
        }