/** * Adds a {@link TarArchiveEntry} if its extraction path does not exist yet. Also adds all of * the parent directories on the extraction path, if the parent does not exist. Parent will have * modified time to set to {@link LayerConfiguration#DEFAULT_MODIFIED_TIME}. * * @param tarArchiveEntry the {@link TarArchiveEntry} */ public void Add(TarEntry tarArchiveEntry) { if (names.Contains(tarArchiveEntry.Name)) { return; } // Adds all directories along extraction paths to explicitly set permissions for those // directories. SystemPath namePath = Paths.Get(tarArchiveEntry.Name); if (namePath.GetParent() != namePath.GetRoot()) { TarEntry dir = TarEntry.CreateTarEntry(namePath.GetParent().ToString().Replace(Path.DirectorySeparatorChar, '/')); dir.Name += "/"; dir.ModTime = DateTimeOffset.FromUnixTimeMilliseconds(LayerConfiguration.DefaultModifiedTime.ToUnixTimeMilliseconds()).DateTime; dir.TarHeader.Mode &= ~(int)PosixFilePermissions.All; dir.TarHeader.Mode |= (int)( PosixFilePermissions.OwnerAll | PosixFilePermissions.GroupReadExecute | PosixFilePermissions.OthersReadExecute); dir.TarHeader.TypeFlag = TarHeader.LF_DIR; Add(dir); } entries.Add(tarArchiveEntry); names.Add(tarArchiveEntry.Name); }
public void TestSelect_invalidLayerDigest() { CacheStorageFiles cacheStorageFiles = new CacheStorageFiles(temporaryFolder.NewFolder().ToPath()); CacheStorageReader cacheStorageReader = new CacheStorageReader(cacheStorageFiles); DescriptorDigest selector = layerDigest1; SystemPath selectorFile = cacheStorageFiles.GetSelectorFile(selector); Files.CreateDirectories(selectorFile.GetParent()); Files.Write(selectorFile, Encoding.UTF8.GetBytes("not a valid layer digest")); try { cacheStorageReader.Select(selector); Assert.Fail("Should have thrown CacheCorruptedException"); } catch (CacheCorruptedException ex) { Assert.That( ex.Message, Does.StartWith( "Expected valid layer digest as contents of selector file `" + selectorFile + "` for selector `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`, but got: not a valid layer digest")); } }
public async Task <BuildResult> CallAsync() { await pullAndCacheBaseImageLayersStep.GetFuture().ConfigureAwait(false); await buildAndCacheApplicationLayersStep.GetFuture().ConfigureAwait(false); await buildImageStep.GetFuture().ConfigureAwait(false); string description = string.Format( CultureInfo.CurrentCulture, Resources.WriteTarFileStepDescriptionFormat, outputPath.GetFileName()); buildConfiguration.GetEventHandlers().Dispatch(LogEvent.Progress(description)); using (progressEventDispatcherFactory.Create(description, this.Index)) { Image image = await buildImageStep.GetFuture().ConfigureAwait(false); // Builds the image to a tarball. Files.CreateDirectories(outputPath.GetParent()); using (Stream outputStream = new BufferedStream(FileOperations.NewLockingOutputStream(outputPath))) { await new ImageTarball(image, buildConfiguration.GetTargetImageConfiguration().GetImage()) .WriteToAsync(outputStream).ConfigureAwait(false); } return(await BuildResult.FromImageAsync(image, buildConfiguration.GetTargetFormat()).ConfigureAwait(false)); } }
/** * Writes a json template to the destination path by writing to a temporary file then moving the * file. * * @param jsonTemplate the json template * @param destination the destination path * @throws IOException if an I/O exception occurs */ public static async Task WriteMetadataAsync(object jsonTemplate, SystemPath destination) { destination = destination ?? throw new ArgumentNullException(nameof(destination)); using (TemporaryFile temporaryFile = Files.CreateTempFile(destination.GetParent())) { using (Stream outputStream = Files.NewOutputStream(temporaryFile.Path)) { await JsonTemplateMapper.WriteToAsync(jsonTemplate, outputStream).ConfigureAwait(false); } Files.Move( temporaryFile.Path, destination, StandardCopyOption.REPLACE_EXISTING); } }
public void TestSelect() { CacheStorageFiles cacheStorageFiles = new CacheStorageFiles(temporaryFolder.NewFolder().ToPath()); CacheStorageReader cacheStorageReader = new CacheStorageReader(cacheStorageFiles); DescriptorDigest selector = layerDigest1; SystemPath selectorFile = cacheStorageFiles.GetSelectorFile(selector); Files.CreateDirectories(selectorFile.GetParent()); Files.Write(selectorFile, Encoding.UTF8.GetBytes(layerDigest2.GetHash())); Maybe <DescriptorDigest> selectedLayerDigest = cacheStorageReader.Select(selector); Assert.IsTrue(selectedLayerDigest.IsPresent()); Assert.AreEqual(layerDigest2, selectedLayerDigest.Get()); }
/** * Writes the {@code selector} to a file in the selectors directory, with contents {@code * layerDigest}. * * @param selector the selector * @param layerDigest the layer digest it selects * @throws IOException if an I/O exception occurs */ private void WriteSelector(DescriptorDigest selector, DescriptorDigest layerDigest) { SystemPath selectorFile = cacheStorageFiles.GetSelectorFile(selector); // Creates the selectors directory if it doesn't exist. Files.CreateDirectories(selectorFile.GetParent()); // Writes the selector to a temporary file and then moves the file to the intended location. using (TemporaryFile temporarySelectorFile = Files.CreateTempFile()) { using (Stream fileOut = FileOperations.NewLockingOutputStream(temporarySelectorFile.Path)) { byte[] bytes = Encoding.UTF8.GetBytes(layerDigest.GetHash()); fileOut.Write(bytes, 0, bytes.Length); } // Attempts an atomic move first, and falls back to non-atomic if the file system does not // support atomic moves. Files.Move( temporarySelectorFile.Path, selectorFile, StandardCopyOption.REPLACE_EXISTING); } }