Documentation ¶
Overview ¶
Package archive provides helper functions for dealing with archive files.
Index ¶
- Constants
- Variables
- func ApplyLayer(dest string, layer io.Reader) (int64, error)
- func ApplyUncompressedLayer(dest string, layer io.Reader, options *TarOptions) (int64, error)
- func CanonicalTarNameForPath(relativePath string) string
- func ChangesSize(newDir string, changes []Change) int64
- func CheckSystemDriveAndRemoveDriveLetter(path string) (string, error)
- func CompressStream(dest io.Writer, compression Compression) (io.WriteCloser, error)
- func CopyResource(srcPath, dstPath string, followLink bool) error
- func CopyTo(content io.Reader, srcInfo CopyInfo, dstPath string) error
- func DecompressStream(archive io.Reader) (io.ReadCloser, error)
- func ExportChanges(dir string, changes []Change, idMap idtools.IdentityMapping) (io.ReadCloser, error)
- func FileInfoHeader(name string, fi os.FileInfo, link string) (*tar.Header, error)
- func FileInfoHeaderNoLookups(fi os.FileInfo, link string) (*tar.Header, error)
- func Generate(input ...string) (io.Reader, error)
- func GetRebaseName(path, resolvedPath string) (string, string)
- func IsArchivePath(path string) bool
- func PrepareArchiveCopy(srcContent io.Reader, srcInfo, dstInfo CopyInfo) (dstDir string, content io.ReadCloser, err error)
- func PreserveTrailingDotOrSeparator(cleanedPath string, originalPath string) string
- func ReadSecurityXattrToTarHeader(path string, hdr *tar.Header) error
- func RebaseArchiveEntries(srcContent io.Reader, oldBase, newBase string) io.ReadCloser
- func ReplaceFileTarWrapper(inputTarStream io.ReadCloser, mods map[string]TarModifierFunc) io.ReadCloser
- func ResolveHostSourcePath(path string, followLink bool) (resolvedPath, rebaseName string, err error)
- func SplitPathDirEntry(path string) (dir, base string)
- func Tar(path string, compression Compression) (io.ReadCloser, error)
- func TarResource(sourceInfo CopyInfo) (content io.ReadCloser, err error)
- func TarResourceRebase(sourcePath, rebaseName string) (content io.ReadCloser, err error)
- func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error)
- func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error
- func UnpackLayer(dest string, layer io.Reader, options *TarOptions) (size int64, err error)
- func Untar(tarArchive io.Reader, dest string, options *TarOptions) error
- func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) error
- type Archiver
- func (archiver *Archiver) CopyFileWithTar(src, dst string) (err error)
- func (archiver *Archiver) CopyWithTar(src, dst string) error
- func (archiver *Archiver) IdentityMapping() idtools.IdentityMapping
- func (archiver *Archiver) TarUntar(src, dst string) error
- func (archiver *Archiver) UntarPath(src, dst string) error
- type Change
- type ChangeType
- type Compression
- type CopyInfo
- type FileInfo
- type TarModifierFunc
- type TarOptions
- type Tarballer
- type TempArchive
- type WhiteoutFormat
Constants ¶
const ( // ChangeModify represents the modify operation. ChangeModify = iota // ChangeAdd represents the add operation. ChangeAdd // ChangeDelete represents the delete operation. ChangeDelete )
const ImpliedDirectoryMode = 0o755
ImpliedDirectoryMode represents the mode (Unix permissions) applied to directories that are implied by files in a tar, but that do not have their own header entry.
The permissions mask is stored in a constant instead of locally to ensure that magic numbers do not proliferate in the codebase. The default value 0755 has been selected based on the default umask of 0022, and a convention of mkdir(1) calling mkdir(2) with permissions of 0777, resulting in a final value of 0755.
This value is currently implementation-defined, and not captured in any cross-runtime specification. Thus, it is subject to change in Moby at any time -- image authors who require consistent or known directory permissions should explicitly control them by ensuring that header entries exist for any applicable path.
const WhiteoutLinkDir = WhiteoutMetaPrefix + "plnk"
WhiteoutLinkDir is a directory AUFS uses for storing hardlink links to other layers. Normally these should not go into exported archives and all changed hardlinks should be copied to the top layer.
const WhiteoutMetaPrefix = WhiteoutPrefix + WhiteoutPrefix
WhiteoutMetaPrefix prefix means whiteout has a special meaning and is not for removing an actual file. Normally these files are excluded from exported archives.
const WhiteoutOpaqueDir = WhiteoutMetaPrefix + ".opq"
WhiteoutOpaqueDir file means directory has been made opaque - meaning readdir calls to this directory do not follow to lower layers.
const WhiteoutPrefix = ".wh."
WhiteoutPrefix prefix means file is a whiteout. If this is followed by a filename this means that file has been removed from the base layer.
Variables ¶
var ( ErrNotDirectory = errors.New("not a directory") ErrDirNotExists = errors.New("no such directory") ErrCannotCopyDir = errors.New("cannot copy directory") ErrInvalidCopySource = errors.New("invalid copy source content") )
Errors used or returned by this file.
Functions ¶
func ApplyLayer ¶
ApplyLayer parses a diff in the standard layer format from `layer`, and applies it to the directory `dest`. The stream `layer` can be compressed or uncompressed. Returns the size in bytes of the contents of the layer.
func ApplyUncompressedLayer ¶
ApplyUncompressedLayer parses a diff in the standard layer format from `layer`, and applies it to the directory `dest`. The stream `layer` can only be uncompressed. Returns the size in bytes of the contents of the layer.
func CanonicalTarNameForPath ¶
CanonicalTarNameForPath canonicalizes relativePath to a POSIX-style path using forward slashes. It is an alias for filepath.ToSlash, which is a no-op on Linux and Unix.
func ChangesSize ¶
ChangesSize calculates the size in bytes of the provided changes, based on newDir.
func CheckSystemDriveAndRemoveDriveLetter ¶
CheckSystemDriveAndRemoveDriveLetter verifies that a path, if it includes a drive letter, is the system drive. On Linux: this is a no-op. On Windows: this does the following> CheckSystemDriveAndRemoveDriveLetter verifies and manipulates a Windows path. This is used, for example, when validating a user provided path in docker cp. If a drive letter is supplied, it must be the system drive. The drive letter is always removed. Also, it translates it to OS semantics (IOW / to \). We need the path in this syntax so that it can ultimately be concatenated with a Windows long-path which doesn't support drive-letters. Examples: C: --> Fail C:\ --> \ a --> a /a --> \a d:\ --> Fail
func CompressStream ¶
func CompressStream(dest io.Writer, compression Compression) (io.WriteCloser, error)
CompressStream compresses the dest with specified compression algorithm.
func CopyResource ¶
CopyResource performs an archive copy from the given source path to the given destination path. The source path MUST exist and the destination path's parent directory must exist.
func CopyTo ¶
CopyTo handles extracting the given content whose entries should be sourced from srcInfo to dstPath.
func DecompressStream ¶
func DecompressStream(archive io.Reader) (io.ReadCloser, error)
DecompressStream decompresses the archive and returns a ReaderCloser with the decompressed archive.
func ExportChanges ¶
func ExportChanges(dir string, changes []Change, idMap idtools.IdentityMapping) (io.ReadCloser, error)
ExportChanges produces an Archive from the provided changes, relative to dir.
func FileInfoHeader ¶
FileInfoHeader creates a populated Header from fi.
Compared to the archive/tar package, this function fills in less information but is safe to call from a chrooted process. The AccessTime and ChangeTime fields are not set in the returned header, ModTime is truncated to one-second precision, and the Uname and Gname fields are only set when fi is a FileInfo value returned from tar.Header.FileInfo().
func FileInfoHeaderNoLookups ¶
FileInfoHeaderNoLookups creates a partially-populated tar.Header from fi.
Compared to the archive/tar.FileInfoHeader function, this function is safe to call from a chrooted process as it does not populate fields which would require operating system lookups. It behaves identically to tar.FileInfoHeader when fi is a FileInfo value returned from tar.Header.FileInfo().
When fi is a FileInfo for a native file, such as returned from os.Stat() and os.Lstat(), the returned Header value differs from one returned from tar.FileInfoHeader in the following ways. The Uname and Gname fields are not set as OS lookups would be required to populate them. The AccessTime and ChangeTime fields are not currently set (not yet implemented) although that is subject to change. Callers which require the AccessTime or ChangeTime fields to be zeroed should explicitly zero them out in the returned Header value to avoid any compatibility issues in the future.
func Generate ¶
Generate generates a new archive from the content provided as input.
`files` is a sequence of path/content pairs. A new file is added to the archive for each pair. If the last pair is incomplete, the file is created with an empty content. For example:
Generate("foo.txt", "hello world", "emptyfile")
The above call will return an archive with 2 files:
- ./foo.txt with content "hello world"
- ./empty with empty content
FIXME: stream content instead of buffering FIXME: specify permissions and other archive metadata
func GetRebaseName ¶
GetRebaseName normalizes and compares path and resolvedPath, return completed resolved path and rebased file name
func IsArchivePath ¶
IsArchivePath checks if the (possibly compressed) file at the given path starts with a tar file header.
func PrepareArchiveCopy ¶
func PrepareArchiveCopy(srcContent io.Reader, srcInfo, dstInfo CopyInfo) (dstDir string, content io.ReadCloser, err error)
PrepareArchiveCopy prepares the given srcContent archive, which should contain the archived resource described by srcInfo, to the destination described by dstInfo. Returns the possibly modified content archive along with the path to the destination directory which it should be extracted to.
func PreserveTrailingDotOrSeparator ¶
PreserveTrailingDotOrSeparator returns the given cleaned path (after processing using any utility functions from the path or filepath stdlib packages) and appends a trailing `/.` or `/` if its corresponding original path (from before being processed by utility functions from the path or filepath stdlib packages) ends with a trailing `/.` or `/`. If the cleaned path already ends in a `.` path segment, then another is not added. If the clean path already ends in a path separator, then another is not added.
func ReadSecurityXattrToTarHeader ¶
ReadSecurityXattrToTarHeader reads security.capability xattr from filesystem to a tar header
func RebaseArchiveEntries ¶
func RebaseArchiveEntries(srcContent io.Reader, oldBase, newBase string) io.ReadCloser
RebaseArchiveEntries rewrites the given srcContent archive replacing an occurrence of oldBase with newBase at the beginning of entry names.
func ReplaceFileTarWrapper ¶
func ReplaceFileTarWrapper(inputTarStream io.ReadCloser, mods map[string]TarModifierFunc) io.ReadCloser
ReplaceFileTarWrapper converts inputTarStream to a new tar stream. Files in the tar stream are modified if they match any of the keys in mods.
func ResolveHostSourcePath ¶
func ResolveHostSourcePath(path string, followLink bool) (resolvedPath, rebaseName string, err error)
ResolveHostSourcePath decides real path need to be copied with parameters such as whether to follow symbol link or not, if followLink is true, resolvedPath will return link target of any symbol link file, else it will only resolve symlink of directory but return symbol link file itself without resolving.
func SplitPathDirEntry ¶
SplitPathDirEntry splits the given path between its directory name and its basename by first cleaning the path but preserves a trailing "." if the original path specified the current directory.
func Tar ¶
func Tar(path string, compression Compression) (io.ReadCloser, error)
Tar creates an archive from the directory at `path`, and returns it as a stream of bytes.
func TarResource ¶
func TarResource(sourceInfo CopyInfo) (content io.ReadCloser, err error)
TarResource archives the resource described by the given CopyInfo to a Tar archive. A non-nil error is returned if sourcePath does not exist or is asserted to be a directory but exists as another type of file.
This function acts as a convenient wrapper around TarWithOptions, which requires a directory as the source path. TarResource accepts either a directory or a file path and correctly sets the Tar options.
func TarResourceRebase ¶
func TarResourceRebase(sourcePath, rebaseName string) (content io.ReadCloser, err error)
TarResourceRebase is like TarResource but renames the first path element of items in the resulting tar archive to match the given rebaseName if not "".
func TarWithOptions ¶
func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error)
TarWithOptions creates an archive from the directory at `path`, only including files whose relative paths are included in `options.IncludeFiles` (if non-nil) or not in `options.ExcludePatterns`.
func Unpack ¶
func Unpack(decompressedArchive io.Reader, dest string, options *TarOptions) error
Unpack unpacks the decompressedArchive to dest with options.
func UnpackLayer ¶
UnpackLayer unpack `layer` to a `dest`. The stream `layer` can be compressed or uncompressed. Returns the size in bytes of the contents of the layer.
func Untar ¶
func Untar(tarArchive io.Reader, dest string, options *TarOptions) error
Untar reads a stream of bytes from `archive`, parses it as a tar archive, and unpacks it into the directory at `dest`. The archive may be compressed with one of the following algorithms: identity (uncompressed), gzip, bzip2, xz.
FIXME: specify behavior when target path exists vs. doesn't exist.
func UntarUncompressed ¶
func UntarUncompressed(tarArchive io.Reader, dest string, options *TarOptions) error
UntarUncompressed reads a stream of bytes from `archive`, parses it as a tar archive, and unpacks it into the directory at `dest`. The archive must be an uncompressed stream.
Types ¶
type Archiver ¶
type Archiver struct { Untar func(io.Reader, string, *TarOptions) error IDMapping idtools.IdentityMapping }
Archiver implements the Archiver interface and allows the reuse of most utility functions of this package with a pluggable Untar function. Also, to facilitate the passing of specific id mappings for untar, an Archiver can be created with maps which will then be passed to Untar operations.
func NewDefaultArchiver ¶
func NewDefaultArchiver() *Archiver
NewDefaultArchiver returns a new Archiver without any IdentityMapping
func (*Archiver) CopyFileWithTar ¶
CopyFileWithTar emulates the behavior of the 'cp' command-line for a single file. It copies a regular file from path `src` to path `dst`, and preserves all its metadata.
func (*Archiver) CopyWithTar ¶
CopyWithTar creates a tar archive of filesystem path `src`, and unpacks it at filesystem path `dst`. The archive is streamed directly with fixed buffering and no intermediary disk IO.
func (*Archiver) IdentityMapping ¶
func (archiver *Archiver) IdentityMapping() idtools.IdentityMapping
IdentityMapping returns the IdentityMapping of the archiver.
type Change ¶
type Change struct { Path string Kind ChangeType }
Change represents a change, it wraps the change type and path. It describes changes of the files in the path respect to the parent layers. The change could be modify, add, delete. This is used for layer diff.
func Changes ¶
Changes walks the path rw and determines changes for the files in the path, with respect to the parent layers
func ChangesDirs ¶
ChangesDirs compares two directories and generates an array of Change objects describing the changes. If oldDir is "", then all files in newDir will be Add-Changes.
type ChangeType ¶
type ChangeType int
ChangeType represents the change type.
func (ChangeType) String ¶
func (c ChangeType) String() string
type Compression ¶
type Compression int
Compression is the state represents if compressed or not.
const ( // Uncompressed represents the uncompressed. Uncompressed Compression = iota // Bzip2 is bzip2 compression algorithm. Bzip2 // Gzip is gzip compression algorithm. Gzip // Xz is xz compression algorithm. Xz // Zstd is zstd compression algorithm. Zstd )
func DetectCompression ¶
func DetectCompression(source []byte) Compression
DetectCompression detects the compression algorithm of the source.
func (*Compression) Extension ¶
func (compression *Compression) Extension() string
Extension returns the extension of a file that uses the specified compression algorithm.
type CopyInfo ¶
CopyInfo holds basic info about the source or destination path of a copy operation.
func CopyInfoDestinationPath ¶
CopyInfoDestinationPath stats the given path to create a CopyInfo struct representing that resource for the destination of an archive copy operation. The given path should be an absolute local path.
func CopyInfoSourcePath ¶
CopyInfoSourcePath stats the given path to create a CopyInfo struct representing that resource for the source of an archive copy operation. The given path should be an absolute local path. A source path has all symlinks evaluated that appear before the last path separator ("/" on Unix). As it is to be a copy source, the path must exist.
type FileInfo ¶
type FileInfo struct {
// contains filtered or unexported fields
}
FileInfo describes the information of a file.
type TarModifierFunc ¶
type TarModifierFunc func(path string, header *tar.Header, content io.Reader) (*tar.Header, []byte, error)
TarModifierFunc is a function that can be passed to ReplaceFileTarWrapper to modify the contents or header of an entry in the archive. If the file already exists in the archive the TarModifierFunc will be called with the Header and a reader which will return the files content. If the file does not exist both header and content will be nil.
type TarOptions ¶
type TarOptions struct { IncludeFiles []string ExcludePatterns []string Compression Compression NoLchown bool IDMap idtools.IdentityMapping ChownOpts *idtools.Identity IncludeSourceDir bool // WhiteoutFormat is the expected on disk format for whiteout files. // This format will be converted to the standard format on pack // and from the standard format on unpack. WhiteoutFormat WhiteoutFormat // When unpacking, specifies whether overwriting a directory with a // non-directory is allowed and vice versa. NoOverwriteDirNonDir bool // For each include when creating an archive, the included name will be // replaced with the matching name from this map. RebaseNames map[string]string InUserNS bool // Allow unpacking to succeed in spite of failures to set extended // attributes on the unpacked files due to the destination filesystem // not supporting them or a lack of permissions. Extended attributes // were probably in the archive for a reason, so set this option at // your own peril. BestEffortXattrs bool }
TarOptions wraps the tar options.
func TarResourceRebaseOpts ¶
func TarResourceRebaseOpts(sourceBase string, rebaseName string) *TarOptions
TarResourceRebaseOpts does not preform the Tar, but instead just creates the rebase parameters to be sent to TarWithOptions (the TarOptions struct)
type Tarballer ¶
type Tarballer struct {
// contains filtered or unexported fields
}
Tarballer is a lower-level interface to TarWithOptions which gives the caller control over which goroutine the archiving operation executes on.
func NewTarballer ¶
func NewTarballer(srcPath string, options *TarOptions) (*Tarballer, error)
NewTarballer constructs a new tarballer. The arguments are the same as for TarWithOptions.
func (*Tarballer) Do ¶
func (t *Tarballer) Do()
Do performs the archiving operation in the background. The resulting archive can be read from t.Reader(). Do should only be called once on each Tarballer instance.
func (*Tarballer) Reader ¶
func (t *Tarballer) Reader() io.ReadCloser
Reader returns the reader for the created archive.
type TempArchive ¶
type TempArchive struct { *os.File Size int64 // Pre-computed from Stat().Size() as a convenience // contains filtered or unexported fields }
TempArchive is a temporary archive. The archive can only be read once - as soon as reading completes, the file will be deleted.
func NewTempArchive ¶
func NewTempArchive(src io.Reader, dir string) (*TempArchive, error)
NewTempArchive reads the content of src into a temporary file, and returns the contents of that file as an archive. The archive can only be read once - as soon as reading completes, the file will be deleted.
func (*TempArchive) Close ¶
func (archive *TempArchive) Close() error
Close closes the underlying file if it's still open, or does a no-op to allow callers to try to close the TempArchive multiple times safely.
type WhiteoutFormat ¶
type WhiteoutFormat int
WhiteoutFormat is the format of whiteouts unpacked
const ( // AUFSWhiteoutFormat is the default format for whiteouts AUFSWhiteoutFormat WhiteoutFormat = iota // OverlayWhiteoutFormat formats whiteout according to the overlay // standard. OverlayWhiteoutFormat )