github.com/hauerwu/docker@v1.8.0-rc1/pkg/chrootarchive/archive.go (about) 1 package chrootarchive 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "path/filepath" 8 9 "github.com/docker/docker/pkg/archive" 10 "github.com/docker/docker/pkg/system" 11 ) 12 13 var chrootArchiver = &archive.Archiver{Untar: Untar} 14 15 // Untar reads a stream of bytes from `archive`, parses it as a tar archive, 16 // and unpacks it into the directory at `dest`. 17 // The archive may be compressed with one of the following algorithms: 18 // identity (uncompressed), gzip, bzip2, xz. 19 func Untar(tarArchive io.Reader, dest string, options *archive.TarOptions) error { 20 21 if tarArchive == nil { 22 return fmt.Errorf("Empty archive") 23 } 24 if options == nil { 25 options = &archive.TarOptions{} 26 } 27 if options.ExcludePatterns == nil { 28 options.ExcludePatterns = []string{} 29 } 30 31 dest = filepath.Clean(dest) 32 if _, err := os.Stat(dest); os.IsNotExist(err) { 33 if err := system.MkdirAll(dest, 0777); err != nil { 34 return err 35 } 36 } 37 38 decompressedArchive, err := archive.DecompressStream(tarArchive) 39 if err != nil { 40 return err 41 } 42 defer decompressedArchive.Close() 43 44 return invokeUnpack(decompressedArchive, dest, options) 45 } 46 47 // TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other. 48 // If either Tar or Untar fails, TarUntar aborts and returns the error. 49 func TarUntar(src, dst string) error { 50 return chrootArchiver.TarUntar(src, dst) 51 } 52 53 // CopyWithTar creates a tar archive of filesystem path `src`, and 54 // unpacks it at filesystem path `dst`. 55 // The archive is streamed directly with fixed buffering and no 56 // intermediary disk IO. 57 func CopyWithTar(src, dst string) error { 58 return chrootArchiver.CopyWithTar(src, dst) 59 } 60 61 // CopyFileWithTar emulates the behavior of the 'cp' command-line 62 // for a single file. It copies a regular file from path `src` to 63 // path `dst`, and preserves all its metadata. 64 // 65 // If `dst` ends with a trailing slash '/' ('\' on Windows), the final 66 // destination path will be `dst/base(src)` or `dst\base(src)` 67 func CopyFileWithTar(src, dst string) (err error) { 68 return chrootArchiver.CopyFileWithTar(src, dst) 69 } 70 71 // UntarPath is a convenience function which looks for an archive 72 // at filesystem path `src`, and unpacks it at `dst`. 73 func UntarPath(src, dst string) error { 74 return chrootArchiver.UntarPath(src, dst) 75 }