|
| 1 | +// Package archive provides helper functions for dealing with archive files. |
| 2 | +package archive |
| 3 | + |
| 4 | +import ( |
| 5 | + "archive/tar" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/docker/docker/pkg/idtools" |
| 10 | + "github.com/moby/go-archive" |
| 11 | +) |
| 12 | + |
| 13 | +// ImpliedDirectoryMode represents the mode (Unix permissions) applied to directories that are implied by files in a |
| 14 | +// tar, but that do not have their own header entry. |
| 15 | +// |
| 16 | +// Deprecated: use [archive.ImpliedDirectoryMode] instead. |
| 17 | +const ImpliedDirectoryMode = archive.ImpliedDirectoryMode |
| 18 | + |
| 19 | +type ( |
| 20 | + // Compression is the state represents if compressed or not. |
| 21 | + // |
| 22 | + // Deprecated: use [archive.Compression] instead. |
| 23 | + Compression = archive.Compression |
| 24 | + // WhiteoutFormat is the format of whiteouts unpacked |
| 25 | + // |
| 26 | + // Deprecated: use [archive.WhiteoutFormat] instead. |
| 27 | + WhiteoutFormat = archive.WhiteoutFormat |
| 28 | + |
| 29 | + // TarOptions wraps the tar options. |
| 30 | + // |
| 31 | + // Deprecated: use [archive.TarOptions] instead. |
| 32 | + TarOptions struct { |
| 33 | + IncludeFiles []string |
| 34 | + ExcludePatterns []string |
| 35 | + Compression archive.Compression |
| 36 | + NoLchown bool |
| 37 | + IDMap idtools.IdentityMapping |
| 38 | + ChownOpts *idtools.Identity |
| 39 | + IncludeSourceDir bool |
| 40 | + // WhiteoutFormat is the expected on disk format for whiteout files. |
| 41 | + // This format will be converted to the standard format on pack |
| 42 | + // and from the standard format on unpack. |
| 43 | + WhiteoutFormat archive.WhiteoutFormat |
| 44 | + // When unpacking, specifies whether overwriting a directory with a |
| 45 | + // non-directory is allowed and vice versa. |
| 46 | + NoOverwriteDirNonDir bool |
| 47 | + // For each include when creating an archive, the included name will be |
| 48 | + // replaced with the matching name from this map. |
| 49 | + RebaseNames map[string]string |
| 50 | + InUserNS bool |
| 51 | + // Allow unpacking to succeed in spite of failures to set extended |
| 52 | + // attributes on the unpacked files due to the destination filesystem |
| 53 | + // not supporting them or a lack of permissions. Extended attributes |
| 54 | + // were probably in the archive for a reason, so set this option at |
| 55 | + // your own peril. |
| 56 | + BestEffortXattrs bool |
| 57 | + } |
| 58 | +) |
| 59 | + |
| 60 | +// Archiver implements the Archiver interface and allows the reuse of most utility functions of |
| 61 | +// this package with a pluggable Untar function. Also, to facilitate the passing of specific id |
| 62 | +// mappings for untar, an Archiver can be created with maps which will then be passed to Untar operations. |
| 63 | +// |
| 64 | +// Deprecated: use [archive.Archiver] instead. |
| 65 | +type Archiver struct { |
| 66 | + Untar func(io.Reader, string, *TarOptions) error |
| 67 | + IDMapping idtools.IdentityMapping |
| 68 | +} |
| 69 | + |
| 70 | +// NewDefaultArchiver returns a new Archiver without any IdentityMapping |
| 71 | +// |
| 72 | +// Deprecated: use [archive.NewDefaultArchiver] instead. |
| 73 | +func NewDefaultArchiver() *Archiver { |
| 74 | + return &Archiver{Untar: Untar} |
| 75 | +} |
| 76 | + |
| 77 | +const ( |
| 78 | + Uncompressed = archive.Uncompressed // Deprecated: use [archive.Uncompressed] instead. |
| 79 | + Bzip2 = archive.Bzip2 // Deprecated: use [archive.Bzip2] instead. |
| 80 | + Gzip = archive.Gzip // Deprecated: use [archive.Gzip] instead. |
| 81 | + Xz = archive.Xz // Deprecated: use [archive.Xz] instead. |
| 82 | + Zstd = archive.Zstd // Deprecated: use [archive.Zstd] instead. |
| 83 | +) |
| 84 | + |
| 85 | +const ( |
| 86 | + AUFSWhiteoutFormat = archive.AUFSWhiteoutFormat // Deprecated: use [archive.AUFSWhiteoutFormat] instead. |
| 87 | + OverlayWhiteoutFormat = archive.OverlayWhiteoutFormat // Deprecated: use [archive.OverlayWhiteoutFormat] instead. |
| 88 | +) |
| 89 | + |
| 90 | +// IsArchivePath checks if the (possibly compressed) file at the given path |
| 91 | +// starts with a tar file header. |
| 92 | +// |
| 93 | +// Deprecated: use [archive.IsArchivePath] instead. |
| 94 | +func IsArchivePath(path string) bool { |
| 95 | + return archive.IsArchivePath(path) |
| 96 | +} |
| 97 | + |
| 98 | +// DetectCompression detects the compression algorithm of the source. |
| 99 | +// |
| 100 | +// Deprecated: use [archive.DetectCompression] instead. |
| 101 | +func DetectCompression(source []byte) archive.Compression { |
| 102 | + return archive.DetectCompression(source) |
| 103 | +} |
| 104 | + |
| 105 | +// DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive. |
| 106 | +// |
| 107 | +// Deprecated: use [archive.DecompressStream] instead. |
| 108 | +func DecompressStream(arch io.Reader) (io.ReadCloser, error) { |
| 109 | + return archive.DecompressStream(arch) |
| 110 | +} |
| 111 | + |
| 112 | +// CompressStream compresses the dest with specified compression algorithm. |
| 113 | +// |
| 114 | +// Deprecated: use [archive.CompressStream] instead. |
| 115 | +func CompressStream(dest io.Writer, compression archive.Compression) (io.WriteCloser, error) { |
| 116 | + return archive.CompressStream(dest, compression) |
| 117 | +} |
| 118 | + |
| 119 | +// TarModifierFunc is a function that can be passed to ReplaceFileTarWrapper. |
| 120 | +// |
| 121 | +// Deprecated: use [archive.TarModifierFunc] instead. |
| 122 | +type TarModifierFunc = archive.TarModifierFunc |
| 123 | + |
| 124 | +// ReplaceFileTarWrapper converts inputTarStream to a new tar stream. |
| 125 | +// |
| 126 | +// Deprecated: use [archive.ReplaceFileTarWrapper] instead. |
| 127 | +func ReplaceFileTarWrapper(inputTarStream io.ReadCloser, mods map[string]archive.TarModifierFunc) io.ReadCloser { |
| 128 | + return archive.ReplaceFileTarWrapper(inputTarStream, mods) |
| 129 | +} |
| 130 | + |
| 131 | +// FileInfoHeaderNoLookups creates a partially-populated tar.Header from fi. |
| 132 | +// |
| 133 | +// Deprecated: use [archive.FileInfoHeaderNoLookups] instead. |
| 134 | +func FileInfoHeaderNoLookups(fi os.FileInfo, link string) (*tar.Header, error) { |
| 135 | + return archive.FileInfoHeaderNoLookups(fi, link) |
| 136 | +} |
| 137 | + |
| 138 | +// FileInfoHeader creates a populated Header from fi. |
| 139 | +// |
| 140 | +// Deprecated: use [archive.FileInfoHeader] instead. |
| 141 | +func FileInfoHeader(name string, fi os.FileInfo, link string) (*tar.Header, error) { |
| 142 | + return archive.FileInfoHeader(name, fi, link) |
| 143 | +} |
| 144 | + |
| 145 | +// ReadSecurityXattrToTarHeader reads security.capability xattr from filesystem |
| 146 | +// to a tar header |
| 147 | +// |
| 148 | +// Deprecated: use [archive.ReadSecurityXattrToTarHeader] instead. |
| 149 | +func ReadSecurityXattrToTarHeader(path string, hdr *tar.Header) error { |
| 150 | + return archive.ReadSecurityXattrToTarHeader(path, hdr) |
| 151 | +} |
| 152 | + |
| 153 | +// Tar creates an archive from the directory at `path`, and returns it as a |
| 154 | +// stream of bytes. |
| 155 | +// |
| 156 | +// Deprecated: use [archive.Tar] instead. |
| 157 | +func Tar(path string, compression archive.Compression) (io.ReadCloser, error) { |
| 158 | + return archive.TarWithOptions(path, &archive.TarOptions{Compression: compression}) |
| 159 | +} |
| 160 | + |
| 161 | +// TarWithOptions creates an archive with the given options. |
| 162 | +// |
| 163 | +// Deprecated: use [archive.TarWithOptions] instead. |
| 164 | +func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) { |
| 165 | + return archive.TarWithOptions(srcPath, toArchiveOpt(options)) |
| 166 | +} |
| 167 | + |
| 168 | +// Tarballer is a lower-level interface to TarWithOptions. |
| 169 | +// |
| 170 | +// Deprecated: use [archive.Tarballer] instead. |
| 171 | +type Tarballer = archive.Tarballer |
| 172 | + |
| 173 | +// NewTarballer constructs a new tarballer using TarWithOptions. |
| 174 | +// |
| 175 | +// Deprecated: use [archive.Tarballer] instead. |
| 176 | +func NewTarballer(srcPath string, options *TarOptions) (*archive.Tarballer, error) { |
| 177 | + return archive.NewTarballer(srcPath, toArchiveOpt(options)) |
| 178 | +} |
| 179 | + |
| 180 | +// Unpack unpacks the decompressedArchive to dest with options. |
| 181 | +// |
| 182 | +// Deprecated: use [archive.Unpack] instead. |
| 183 | +func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error { |
| 184 | + return archive.Unpack(decompressedArchive, dest, toArchiveOpt(options)) |
| 185 | +} |
| 186 | + |
| 187 | +// Untar reads a stream of bytes from `archive`, parses it as a tar archive, |
| 188 | +// and unpacks it into the directory at `dest`. |
| 189 | +// |
| 190 | +// Deprecated: use [archive.Untar] instead. |
| 191 | +func Untar(tarArchive io.Reader, dest string, options *TarOptions) error { |
| 192 | + return archive.Untar(tarArchive, dest, toArchiveOpt(options)) |
| 193 | +} |
| 194 | + |
| 195 | +// UntarUncompressed reads a stream of bytes from `tarArchive`, parses it as a tar archive, |
| 196 | +// and unpacks it into the directory at `dest`. |
| 197 | +// The archive must be an uncompressed stream. |
| 198 | +// |
| 199 | +// Deprecated: use [archive.UntarUncompressed] instead. |
| 200 | +func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) error { |
| 201 | + return archive.UntarUncompressed(tarArchive, dest, toArchiveOpt(options)) |
| 202 | +} |
| 203 | + |
| 204 | +// TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other. |
| 205 | +// If either Tar or Untar fails, TarUntar aborts and returns the error. |
| 206 | +func (archiver *Archiver) TarUntar(src, dst string) error { |
| 207 | + return (&archive.Archiver{ |
| 208 | + Untar: func(reader io.Reader, s string, options *archive.TarOptions) error { |
| 209 | + return archiver.Untar(reader, s, &TarOptions{ |
| 210 | + IDMap: archiver.IDMapping, |
| 211 | + }) |
| 212 | + }, |
| 213 | + IDMapping: idtools.ToUserIdentityMapping(archiver.IDMapping), |
| 214 | + }).TarUntar(src, dst) |
| 215 | +} |
| 216 | + |
| 217 | +// UntarPath untar a file from path to a destination, src is the source tar file path. |
| 218 | +func (archiver *Archiver) UntarPath(src, dst string) error { |
| 219 | + return (&archive.Archiver{ |
| 220 | + Untar: func(reader io.Reader, s string, options *archive.TarOptions) error { |
| 221 | + return archiver.Untar(reader, s, &TarOptions{ |
| 222 | + IDMap: archiver.IDMapping, |
| 223 | + }) |
| 224 | + }, |
| 225 | + IDMapping: idtools.ToUserIdentityMapping(archiver.IDMapping), |
| 226 | + }).UntarPath(src, dst) |
| 227 | +} |
| 228 | + |
| 229 | +// CopyWithTar creates a tar archive of filesystem path `src`, and |
| 230 | +// unpacks it at filesystem path `dst`. |
| 231 | +// The archive is streamed directly with fixed buffering and no |
| 232 | +// intermediary disk IO. |
| 233 | +func (archiver *Archiver) CopyWithTar(src, dst string) error { |
| 234 | + return (&archive.Archiver{ |
| 235 | + Untar: func(reader io.Reader, s string, options *archive.TarOptions) error { |
| 236 | + return archiver.Untar(reader, s, nil) |
| 237 | + }, |
| 238 | + IDMapping: idtools.ToUserIdentityMapping(archiver.IDMapping), |
| 239 | + }).CopyWithTar(src, dst) |
| 240 | +} |
| 241 | + |
| 242 | +// CopyFileWithTar emulates the behavior of the 'cp' command-line |
| 243 | +// for a single file. It copies a regular file from path `src` to |
| 244 | +// path `dst`, and preserves all its metadata. |
| 245 | +func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error) { |
| 246 | + return (&archive.Archiver{ |
| 247 | + Untar: func(reader io.Reader, s string, options *archive.TarOptions) error { |
| 248 | + return archiver.Untar(reader, s, nil) |
| 249 | + }, |
| 250 | + IDMapping: idtools.ToUserIdentityMapping(archiver.IDMapping), |
| 251 | + }).CopyFileWithTar(src, dst) |
| 252 | +} |
| 253 | + |
| 254 | +// IdentityMapping returns the IdentityMapping of the archiver. |
| 255 | +func (archiver *Archiver) IdentityMapping() idtools.IdentityMapping { |
| 256 | + return archiver.IDMapping |
| 257 | +} |
0 commit comments