github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/pkg/archive/archive_unix.go (about) 1 // +build !windows 2 3 package archive 4 5 import ( 6 "archive/tar" 7 "errors" 8 "os" 9 "path/filepath" 10 "syscall" 11 12 "github.com/docker/docker/pkg/system" 13 rsystem "github.com/opencontainers/runc/libcontainer/system" 14 ) 15 16 // fixVolumePathPrefix does platform specific processing to ensure that if 17 // the path being passed in is not in a volume path format, convert it to one. 18 func fixVolumePathPrefix(srcPath string) string { 19 return srcPath 20 } 21 22 // getWalkRoot calculates the root path when performing a TarWithOptions. 23 // We use a separate function as this is platform specific. On Linux, we 24 // can't use filepath.Join(srcPath,include) because this will clean away 25 // a trailing "." or "/" which may be important. 26 func getWalkRoot(srcPath string, include string) string { 27 return srcPath + string(filepath.Separator) + include 28 } 29 30 // CanonicalTarNameForPath returns platform-specific filepath 31 // to canonical posix-style path for tar archival. p is relative 32 // path. 33 func CanonicalTarNameForPath(p string) (string, error) { 34 return p, nil // already unix-style 35 } 36 37 // chmodTarEntry is used to adjust the file permissions used in tar header based 38 // on the platform the archival is done. 39 40 func chmodTarEntry(perm os.FileMode) os.FileMode { 41 return perm // noop for unix as golang APIs provide perm bits correctly 42 } 43 44 func setHeaderForSpecialDevice(hdr *tar.Header, ta *tarAppender, name string, stat interface{}) (inode uint64, err error) { 45 s, ok := stat.(*syscall.Stat_t) 46 47 if !ok { 48 err = errors.New("cannot convert stat value to syscall.Stat_t") 49 return 50 } 51 52 inode = uint64(s.Ino) 53 54 // Currently go does not fill in the major/minors 55 if s.Mode&syscall.S_IFBLK != 0 || 56 s.Mode&syscall.S_IFCHR != 0 { 57 hdr.Devmajor = int64(major(uint64(s.Rdev))) 58 hdr.Devminor = int64(minor(uint64(s.Rdev))) 59 } 60 61 return 62 } 63 64 func getFileUIDGID(stat interface{}) (int, int, error) { 65 s, ok := stat.(*syscall.Stat_t) 66 67 if !ok { 68 return -1, -1, errors.New("cannot convert stat value to syscall.Stat_t") 69 } 70 return int(s.Uid), int(s.Gid), nil 71 } 72 73 func major(device uint64) uint64 { 74 return (device >> 8) & 0xfff 75 } 76 77 func minor(device uint64) uint64 { 78 return (device & 0xff) | ((device >> 12) & 0xfff00) 79 } 80 81 // handleTarTypeBlockCharFifo is an OS-specific helper function used by 82 // createTarFile to handle the following types of header: Block; Char; Fifo 83 func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error { 84 if rsystem.RunningInUserNS() { 85 // cannot create a device if running in user namespace 86 return nil 87 } 88 89 mode := uint32(hdr.Mode & 07777) 90 switch hdr.Typeflag { 91 case tar.TypeBlock: 92 mode |= syscall.S_IFBLK 93 case tar.TypeChar: 94 mode |= syscall.S_IFCHR 95 case tar.TypeFifo: 96 mode |= syscall.S_IFIFO 97 } 98 99 if err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor))); err != nil { 100 return err 101 } 102 return nil 103 } 104 105 func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error { 106 if hdr.Typeflag == tar.TypeLink { 107 if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) { 108 if err := os.Chmod(path, hdrInfo.Mode()); err != nil { 109 return err 110 } 111 } 112 } else if hdr.Typeflag != tar.TypeSymlink { 113 if err := os.Chmod(path, hdrInfo.Mode()); err != nil { 114 return err 115 } 116 } 117 return nil 118 }