示例#1
0
        /// <exception cref="IOException">
        /// Thrown if a network error occurs or the SHA-1 hash of the downloaded file
        /// does not match the expected value in the update manifest.
        /// </exception>
        private void DownloadUpdateSync(Update update)
        {
            _cancellationTokenSource = new CancellationTokenSource();

            var path       = Path.Combine(Path.GetTempPath(), update.FileName);
            var downloader = new FileDownloader
            {
                Uri  = update.Uri,
                Path = path,
                CancellationToken = _cancellationTokenSource.Token
            };

            downloader.BeforeRequest   += NotifyBeforeRequest;
            downloader.ProgressChanged += DownloaderOnProgressChanged;

            downloader.DownloadSync();

            if (downloader.State != FileDownloadState.Success)
            {
                return;
            }

            var hash = new SHA1Algorithm().ComputeFile(path);

            if (!String.Equals(hash, update.SHA1, StringComparison.OrdinalIgnoreCase))
            {
                _logger.ErrorFormat(
                    "Unable to verify integrity of \"{0}\" via SHA-1 hash: expected {1}, but found {2}",
                    path, update.SHA1, hash);
                throw new IOException("Update file is corrupt or has been tampered with; SHA-1 hash is incorrect");
            }

            _latestInstallerPath = path;
        }
示例#2
0
        public string CreateTempDirectory(Assembly assembly = null, params string[] subdirectoryNames)
        {
            var pathHash    = new SHA1Algorithm().ComputeText(AssemblyUtils.GetInstallDir());
            var folderNames = new List <string>
            {
                RootTempDirectory,
                pathHash.Substring(0, 7),
                AssemblyUtils.GetAssemblyName(assembly),
                Process.GetCurrentProcess().Id.ToString("0")
            };

            folderNames.AddRange(subdirectoryNames.Where(s => !string.IsNullOrWhiteSpace(s)));
            var path = Combine(folderNames.ToArray());

            Directory.CreateDirectory(path);
            RegisterTempDirectories(path);
            return(path);
        }