/** * Writes an uncompressed {@code layerBlob} to the {@code layerDirectory}. * * @param uncompressedLayerBlob the uncompressed layer {@link Blob} * @param layerDirectory the directory for the layer * @return a {@link WrittenLayer} with the written layer information * @throws IOException if an I/O exception occurs */ private async Task <WrittenLayer> WriteUncompressedLayerBlobToDirectoryAsync( IBlob uncompressedLayerBlob, SystemPath layerDirectory) { using (TemporaryFile temporaryLayerFile = CacheStorageFiles.GetTemporaryLayerFile(layerDirectory)) { DescriptorDigest layerDiffId; BlobDescriptor blobDescriptor; // Writes the layer with GZIP compression. The original bytes are captured as the layer's // diff ID and the bytes outputted from the GZIP compression are captured as the layer's // content descriptor. using (CountingDigestOutputStream compressedDigestOutputStream = new CountingDigestOutputStream( Files.NewOutputStream(temporaryLayerFile.Path))) { using (GZipStream compressorStream = new GZipStream(compressedDigestOutputStream, CompressionMode.Compress, true)) { BlobDescriptor descriptor = await uncompressedLayerBlob.WriteToAsync(compressorStream).ConfigureAwait(false); layerDiffId = descriptor.GetDigest(); } // The GZIPOutputStream must be closed in order to write out the remaining compressed data. blobDescriptor = compressedDigestOutputStream.ComputeDigest(); } DescriptorDigest layerDigest = blobDescriptor.GetDigest(); long layerSize = blobDescriptor.GetSize(); // Renames the temporary layer file to the correct filename. SystemPath layerFile = layerDirectory.Resolve(cacheStorageFiles.GetLayerFilename(layerDiffId)); temporaryLayerFile.MoveIfDoesNotExist(layerFile); return(new WrittenLayer(layerDigest, layerDiffId, layerSize)); } }
/** * Gets the size of {@code blob}. * * @param blob the {@link Blob} * @return the size (in bytes) of {@code blob} * @throws IOException if an I/O exception occurs */ private static async Task <long> SizeOfAsync(IBlob blob) { CountingDigestOutputStream countingOutputStream = new CountingDigestOutputStream(Stream.Null); await blob.WriteToAsync(countingOutputStream).ConfigureAwait(false); return(countingOutputStream.GetCount()); }
/** * Decompresses the file to obtain the diff ID. * * @param compressedFile the file containing the compressed contents * @return the digest of the decompressed file * @throws IOException if an I/O exception occurs */ private static async Task <DescriptorDigest> GetDiffIdByDecompressingFileAsync(SystemPath compressedFile) { using (CountingDigestOutputStream diffIdCaptureOutputStream = new CountingDigestOutputStream(Stream.Null)) { using (GZipStream decompressorStream = new GZipStream(Files.NewInputStream(compressedFile), CompressionMode.Decompress)) { await ByteStreams.CopyAsync(decompressorStream, diffIdCaptureOutputStream).ConfigureAwait(false); } return(diffIdCaptureOutputStream.ComputeDigest().GetDigest()); } }
public void SetUpFakes() { layerContentOutputStream = new MemoryStream(); layerOutputStream = new CountingDigestOutputStream(layerContentOutputStream); fakeDigest = DescriptorDigest.FromHash( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); testBlobPuller = new BlobPuller( fakeRegistryEndpointRequestProperties, fakeDigest, layerOutputStream, _ => { }, _ => { }); }
public async Task Test_smokeTestAsync() { foreach (KeyValuePair <string, string> knownHash in KNOWN_SHA256_HASHES) { string toHash = knownHash.Key; string expectedHash = knownHash.Value; byte[] bytesToHash = Encoding.UTF8.GetBytes(toHash); Stream underlyingOutputStream = new MemoryStream(); using (CountingDigestOutputStream countingDigestOutputStream = new CountingDigestOutputStream(underlyingOutputStream)) using (Stream toHashInputStream = new MemoryStream(bytesToHash)) { await toHashInputStream.CopyToAsync(countingDigestOutputStream).ConfigureAwait(false); BlobDescriptor blobDescriptor = countingDigestOutputStream.ComputeDigest(); Assert.AreEqual(DescriptorDigest.FromHash(expectedHash), blobDescriptor.GetDigest()); Assert.AreEqual(bytesToHash.Length, blobDescriptor.GetSize()); } } }