/** * Builds and returns the layer {@link Blob}. * * @return the new layer */ public IBlob Build() { UniqueTarArchiveEntries uniqueTarArchiveEntries = new UniqueTarArchiveEntries(); // Adds all the layer entries as tar entries. foreach (LayerEntry layerEntry in layerEntries) { // Adds the entries to uniqueTarArchiveEntries, which makes sure all entries are unique and // adds parent directories for each extraction path. TarEntry entry = TarEntry.CreateEntryFromFile(layerEntry.SourceFile); entry.Name = layerEntry.ExtractionPath.ToString().TrimStart('/'); if (Directory.Exists(layerEntry.SourceFile)) { entry.Name += '/'; entry.TarHeader.TypeFlag = TarHeader.LF_DIR; } // Sets the entry's permissions by masking out the permission bits from the entry's mode (the // lowest 9 bits) then using a bitwise OR to set them to the layerEntry's permissions. entry.SetMode((entry.GetMode() & ~PosixFilePermissions.All) | layerEntry.Permissions.GetPermissionBits()); entry.ModTime = DateTimeOffset.FromUnixTimeMilliseconds(layerEntry.LastModifiedTime.ToUnixTimeMilliseconds()).DateTime; uniqueTarArchiveEntries.Add(entry); } // Gets the entries sorted by extraction path. IList <TarEntry> sortedFilesystemEntries = uniqueTarArchiveEntries.GetSortedEntries(); HashSet <string> names = new HashSet <string>(); // Adds all the files to a tar stream. TarStreamBuilder tarStreamBuilder = new TarStreamBuilder(); foreach (TarEntry entry in sortedFilesystemEntries) { // Strips out all non-reproducible elements from tar archive entries. // Modified time is configured per entry entry.TarHeader.GroupId = 0; entry.TarHeader.UserId = 0; entry.TarHeader.UserName = ""; entry.TarHeader.GroupName = ""; Debug.Assert(!names.Contains(entry.Name)); names.Add(entry.Name); tarStreamBuilder.AddTarArchiveEntry(entry); } return(Blobs.From(tarStreamBuilder.WriteAsTarArchiveToAsync, -1)); }
/** Creates a TarStreamBuilder using TarArchiveEntries. */ private void SetUpWithTarEntries() { // Prepares a test TarStreamBuilder. testTarStreamBuilder.AddTarArchiveEntry(TarStreamBuilder.CreateEntryFromFile(fileA.ToFile(), "some/path/to/resourceFileA")); testTarStreamBuilder.AddTarArchiveEntry(TarStreamBuilder.CreateEntryFromFile(fileB.ToFile(), "crepecake")); testTarStreamBuilder.AddTarArchiveEntry( TarStreamBuilder.CreateEntryFromFile(directoryA.ToFile(), "some/path/to/")); testTarStreamBuilder.AddTarArchiveEntry( TarStreamBuilder.CreateEntryFromFile( fileA.ToFile(), "some/really/long/path/that/exceeds/100/characters/abcdefghijklmnopqrstuvwxyz0123456789012345678901234567890")); }