/// <nodoc /> private async Task <EvaluationResult> ValidateAndStoreIncrementalDownloadStateAsync(DownloadData downloadData) { // If the hash is given in the download setting, use the corresponding hashType(hash algorithm) to get the content hash of the downloaded file. var downloadedHash = await GetContentHashAsync(downloadData.DownloadedFilePath, HashTypeParser(downloadData.Settings.Hash)); if (downloadData.ContentHash.HasValue) { // Validate downloaded hash if specified if (downloadData.ContentHash != downloadedHash) { m_logger.DownloadMismatchedHash( m_context.LoggingContext, downloadData.Settings.ModuleName, downloadData.Settings.Url, downloadData.Settings.Hash, downloadedHash.ToString()); return(EvaluationResult.Error); } } else { try { var incrementalState = new DownloadIncrementalState(downloadData, downloadedHash); await incrementalState.SaveAsync(m_context); } catch (BuildXLException e) { m_logger.ErrorStoringIncrementality(m_context.LoggingContext, downloadData.Settings.ModuleName, e.Message); return(EvaluationResult.Error); } } return(new EvaluationResult(FileArtifact.CreateSourceFile(downloadData.DownloadedFilePath))); }
/// <nodoc /> public static async Task <DownloadIncrementalState> TryLoadAsync(Logger logger, FrontEndContext context, DownloadData downloadData) { var manifestFilePath = downloadData.DownloadManifestFile.ToString(context.PathTable); DownloadIncrementalState result = null; if (!FileUtilities.Exists(manifestFilePath)) { return(null); } using (var reader = new StreamReader(manifestFilePath)) { var versionLine = await reader.ReadLineAsync(); if (!string.Equals(versionLine, ManifestVersion, StringComparison.Ordinal)) { logger.DownloadManifestDoesNotMatch(context.LoggingContext, downloadData.Settings.ModuleName, downloadData.Settings.Url, "versionLine", ManifestVersion, versionLine); return(null); } var urlLine = await reader.ReadLineAsync(); if (!string.Equals(urlLine, downloadData.Settings.Url, StringComparison.Ordinal)) { logger.DownloadManifestDoesNotMatch(context.LoggingContext, downloadData.Settings.ModuleName, downloadData.Settings.Url, "url", downloadData.Settings.Url, urlLine); return(null); } var fileNameLine = await reader.ReadLineAsync(); if (!string.Equals(fileNameLine, downloadData.Settings.FileName, StringComparison.Ordinal)) { logger.DownloadManifestDoesNotMatch(context.LoggingContext, downloadData.Settings.ModuleName, downloadData.Settings.Url, "fileName", downloadData.Settings.FileName, fileNameLine); return(null); } var hashLine = await reader.ReadLineAsync(); if (hashLine == null || !ContentHash.TryParse(hashLine, out var expectedHash)) { return(null); } result = new DownloadIncrementalState(downloadData, expectedHash); } return(result); }
/// <summary> /// Checks if we actually need to download of if the disk is up to date. /// </summary> /// <returns>Returns EvaluationResult.Continue if we still need to download, else the result will be what should be returned</returns> private async Task <EvaluationResult> CheckIfDownloadIsNeededAsync(DownloadData downloadData) { try { var downloadFilePath = downloadData.DownloadedFilePath.ToString(m_context.PathTable); // Check if the file already exists and matches the expected hash. if (File.Exists(downloadFilePath)) { var expectedHashType = downloadData.ContentHash.HasValue ? downloadData.ContentHash.Value.HashType : HashTypeParser(downloadData.Settings.Hash); // We don't record the file until we know it is the one used in this build. var recordFileAccess = false; var actualHash = await GetContentHashAsync(downloadData.DownloadedFilePath, expectedHashType, recordFileAccess); // Compare actual hash to compare if we need to download again. // Compare against the static hash value. if (downloadData.ContentHash.HasValue && actualHash == downloadData.ContentHash.Value) { // Record the file with the build's default hasher. m_frontEndHost.Engine.RecordFrontEndFile(downloadData.DownloadedFilePath, Name); return(new EvaluationResult(FileArtifact.CreateSourceFile(downloadData.DownloadedFilePath))); } var incrementalState = await DownloadIncrementalState.TryLoadAsync(m_logger, m_context, downloadData); if (incrementalState != null && incrementalState.ContentHash == actualHash) { // Record the file with the build's default hasher. m_frontEndHost.Engine.RecordFrontEndFile(downloadData.DownloadedFilePath, Name); return(new EvaluationResult(FileArtifact.CreateSourceFile(downloadData.DownloadedFilePath))); } } } catch (IOException e) { m_logger.ErrorCheckingIncrementality(m_context.LoggingContext, downloadData.Settings.ModuleName, e.Message); return(EvaluationResult.Error); } catch (UnauthorizedAccessException e) { m_logger.ErrorCheckingIncrementality(m_context.LoggingContext, downloadData.Settings.ModuleName, e.Message); return(EvaluationResult.Error); } // Download is needed return(EvaluationResult.Continue); }
/// <summary> /// Checks if we actually need to download of if the disk is up to date. /// </summary> /// <returns>Returns EvaluationResult.Continue if we still need to download, else the result will be what should be returned</returns> private async Task <EvaluationResult> CheckIfDownloadIsNeededAsync(DownloadData downloadData) { try { var downloadFilePath = downloadData.DownloadedFilePath.ToString(m_context.PathTable); // Check if the file already exists and matches the exected hash. if (File.Exists(downloadFilePath)) { var expectedHashType = downloadData.ContentHash.HasValue ? downloadData.ContentHash.Value.HashType : HashType.Unknown; // Compare actual hash to compare if we need to download again. var actualHash = await GetContentHashAsync(downloadData.DownloadedFilePath, expectedHashType); // Compare against the static hash value. if (downloadData.ContentHash.HasValue && actualHash == downloadData.ContentHash.Value) { return(new EvaluationResult(FileArtifact.CreateSourceFile(downloadData.DownloadedFilePath))); } var incrementalState = await DownloadIncrementalState.TryLoadAsync(m_logger, m_context, downloadData); if (incrementalState != null && incrementalState.ContentHash == actualHash) { return(new EvaluationResult(FileArtifact.CreateSourceFile(downloadData.DownloadedFilePath))); } } } catch (IOException e) { m_logger.ErrorCheckingIncrementality(m_context.LoggingContext, downloadData.Settings.ModuleName, e.Message); return(EvaluationResult.Error); } catch (UnauthorizedAccessException e) { m_logger.ErrorCheckingIncrementality(m_context.LoggingContext, downloadData.Settings.ModuleName, e.Message); return(EvaluationResult.Error); } // Download is needed return(EvaluationResult.Continue); }
/// <nodoc /> private async Task <EvaluationResult> ValidateAndStoreIncrementalDownloadStateAsync(DownloadData downloadData) { // If the hash is given in the download setting, use the corresponding hashType(hash algorithm) to get the content hash of the downloaded file. // We don't record the file until we know it is the correct one and will be used in this build. var recordFileAccess = false; var downloadedHash = await GetContentHashAsync(downloadData.DownloadedFilePath, HashTypeParser(downloadData.Settings.Hash), recordFileAccess); if (downloadData.ContentHash.HasValue) { // Validate downloaded hash if specified if (downloadData.ContentHash != downloadedHash) { m_logger.DownloadMismatchedHash( m_context.LoggingContext, downloadData.Settings.ModuleName, downloadData.Settings.Url, downloadData.Settings.Hash, downloadedHash.ToString()); return(EvaluationResult.Error); } } else { try { var incrementalState = new DownloadIncrementalState(downloadData, downloadedHash); await incrementalState.SaveAsync(m_context); } catch (BuildXLException e) { m_logger.ErrorStoringIncrementality(m_context.LoggingContext, downloadData.Settings.ModuleName, e.Message); return(EvaluationResult.Error); } } // Record the file with the build's default hasher. m_frontEndHost.Engine.RecordFrontEndFile(downloadData.DownloadedFilePath, Name); return(new EvaluationResult(FileArtifact.CreateSourceFile(downloadData.DownloadedFilePath))); }