/// <summary> /// Executes <see cref="RegisterFileForBuildManifestCommand"/>. Checks if local file exists and computes it's ContentHash. /// Else checks if Cache contains SHA-256 Hash for given <see cref="RegisterFileForBuildManifestCommand.Hash"/>. /// Returns true if SHA-256 ContentHash exists. /// Else the file is materialized using <see cref="ExecuteMaterializeFileAsync"/>, the build manifest hash is computed and stored into cache. /// </summary> private async Task <IIpcResult> ExecuteGetBuildManifestHashAsync(RegisterFileForBuildManifestCommand cmd) { Contract.Requires(cmd != null); if (m_inMemoryBuildManifestStore.TryGetValue(cmd.Hash, out var buildManifestHash)) { RecordFileForBuildManifestInXLG(cmd.DropName, cmd.RelativePath, cmd.Hash, buildManifestHash); return(IpcResult.Success(cmd.RenderResult(true))); } ContentHash?sha256Hash = await TryGetBuildManifestHashFromLocalFileAsync(cmd.FullFilePath) ?? await TryGetBuildManifestHashAsync(cmd.Hash); if (sha256Hash.HasValue) { m_inMemoryBuildManifestStore.TryAdd(cmd.Hash, sha256Hash.Value); RecordFileForBuildManifestInXLG(cmd.DropName, cmd.RelativePath, cmd.Hash, sha256Hash.Value); return(IpcResult.Success(cmd.RenderResult(true))); } var computeHashResult = await ComputeBuildManifestHashFromCacheAsync(cmd); if (computeHashResult.Succeeded) { m_inMemoryBuildManifestStore.TryAdd(cmd.Hash, computeHashResult.Result); RecordFileForBuildManifestInXLG(cmd.DropName, cmd.RelativePath, cmd.Hash, computeHashResult.Result); await StoreBuildManifestHashAsync(cmd.Hash, computeHashResult.Result); return(IpcResult.Success(cmd.RenderResult(true))); } Tracing.Logger.Log.ErrorApiServerGetBuildManifestHashFromCacheFailed(m_loggingContext, cmd.Hash.Serialize(), computeHashResult.Failure.DescribeIncludingInnerFailures()); return(IpcResult.Success(cmd.RenderResult(false))); }
/// <summary> /// Compute the SHA-256 hash for file stored in Cache. Required for Build Manifets generation. /// </summary> /// <remarks> /// Returns null when unable to find content in cache. /// </remarks> private async Task <Possible <ContentHash> > ComputeBuildManifestHashFromCacheAsync(RegisterFileForBuildManifestCommand cmd) { // Ensure file is materialized locally MaterializeFileCommand materializeCommand = new MaterializeFileCommand(cmd.File, cmd.FullFilePath); var materializeResult = await ExecuteMaterializeFileAsync(materializeCommand); if (!materializeResult.Succeeded) { return(new Failure <string>("Unable to materialize file = " + cmd.File.Path.ToString(m_context.PathTable) + " with hash: " + cmd.Hash.Serialize())); } // Open content stream for locally available file var loadFileContentResult = await m_engineCache.ArtifactContentCache.TryOpenContentStreamAsync(cmd.Hash); if (loadFileContentResult.Succeeded) { using (var streamWithLength = loadFileContentResult.Result) { try { return(await ContentHashingUtilities.HashContentStreamAsync(streamWithLength, ContentHashingUtilities.BuildManifestHashType)); } catch (BuildXLException ex) { return(new Failure <string>(ex.Message)); } } } return(new Failure <string>(loadFileContentResult.Failure.DescribeIncludingInnerFailures())); }