12 "github.com/docker/docker/pkg/idtools"
13 "github.com/docker/docker/pkg/system"
14 rsystem "github.com/opencontainers/runc/libcontainer/system"
17 // fixVolumePathPrefix does platform specific processing to ensure that if
18 // the path being passed in is not in a volume path format, convert it to one.
19 func fixVolumePathPrefix(srcPath string) string {
23 // getWalkRoot calculates the root path when performing a TarWithOptions.
24 // We use a separate function as this is platform specific. On Linux, we
25 // can't use filepath.Join(srcPath,include) because this will clean away
26 // a trailing "." or "/" which may be important.
27 func getWalkRoot(srcPath string, include string) string {
28 return srcPath + string(filepath.Separator) + include
31 // CanonicalTarNameForPath returns platform-specific filepath
32 // to canonical posix-style path for tar archival. p is relative
34 func CanonicalTarNameForPath(p string) (string, error) {
35 return p, nil // already unix-style
38 // chmodTarEntry is used to adjust the file permissions used in tar header based
39 // on the platform the archival is done.
41 func chmodTarEntry(perm os.FileMode) os.FileMode {
42 return perm // noop for unix as golang APIs provide perm bits correctly
45 func setHeaderForSpecialDevice(hdr *tar.Header, name string, stat interface{}) (err error) {
46 s, ok := stat.(*syscall.Stat_t)
49 // Currently go does not fill in the major/minors
50 if s.Mode&syscall.S_IFBLK != 0 ||
51 s.Mode&syscall.S_IFCHR != 0 {
52 hdr.Devmajor = int64(major(uint64(s.Rdev)))
53 hdr.Devminor = int64(minor(uint64(s.Rdev)))
60 func getInodeFromStat(stat interface{}) (inode uint64, err error) {
61 s, ok := stat.(*syscall.Stat_t)
70 func getFileUIDGID(stat interface{}) (idtools.IDPair, error) {
71 s, ok := stat.(*syscall.Stat_t)
74 return idtools.IDPair{}, errors.New("cannot convert stat value to syscall.Stat_t")
76 return idtools.IDPair{UID: int(s.Uid), GID: int(s.Gid)}, nil
79 func major(device uint64) uint64 {
80 return (device >> 8) & 0xfff
83 func minor(device uint64) uint64 {
84 return (device & 0xff) | ((device >> 12) & 0xfff00)
87 // handleTarTypeBlockCharFifo is an OS-specific helper function used by
88 // createTarFile to handle the following types of header: Block; Char; Fifo
89 func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
90 if rsystem.RunningInUserNS() {
91 // cannot create a device if running in user namespace
95 mode := uint32(hdr.Mode & 07777)
98 mode |= syscall.S_IFBLK
100 mode |= syscall.S_IFCHR
102 mode |= syscall.S_IFIFO
105 return system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
108 func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
109 if hdr.Typeflag == tar.TypeLink {
110 if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
111 if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
115 } else if hdr.Typeflag != tar.TypeSymlink {
116 if err := os.Chmod(path, hdrInfo.Mode()); err != nil {