Tizen_4.0 base
[platform/upstream/docker-engine.git] / pkg / archive / archive_unix.go
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/idtools"
13         "github.com/docker/docker/pkg/system"
14         rsystem "github.com/opencontainers/runc/libcontainer/system"
15 )
16
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 {
20         return srcPath
21 }
22
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
29 }
30
31 // CanonicalTarNameForPath returns platform-specific filepath
32 // to canonical posix-style path for tar archival. p is relative
33 // path.
34 func CanonicalTarNameForPath(p string) (string, error) {
35         return p, nil // already unix-style
36 }
37
38 // chmodTarEntry is used to adjust the file permissions used in tar header based
39 // on the platform the archival is done.
40
41 func chmodTarEntry(perm os.FileMode) os.FileMode {
42         return perm // noop for unix as golang APIs provide perm bits correctly
43 }
44
45 func setHeaderForSpecialDevice(hdr *tar.Header, name string, stat interface{}) (err error) {
46         s, ok := stat.(*syscall.Stat_t)
47
48         if ok {
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)))
54                 }
55         }
56
57         return
58 }
59
60 func getInodeFromStat(stat interface{}) (inode uint64, err error) {
61         s, ok := stat.(*syscall.Stat_t)
62
63         if ok {
64                 inode = uint64(s.Ino)
65         }
66
67         return
68 }
69
70 func getFileUIDGID(stat interface{}) (idtools.IDPair, error) {
71         s, ok := stat.(*syscall.Stat_t)
72
73         if !ok {
74                 return idtools.IDPair{}, errors.New("cannot convert stat value to syscall.Stat_t")
75         }
76         return idtools.IDPair{UID: int(s.Uid), GID: int(s.Gid)}, nil
77 }
78
79 func major(device uint64) uint64 {
80         return (device >> 8) & 0xfff
81 }
82
83 func minor(device uint64) uint64 {
84         return (device & 0xff) | ((device >> 12) & 0xfff00)
85 }
86
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
92                 return nil
93         }
94
95         mode := uint32(hdr.Mode & 07777)
96         switch hdr.Typeflag {
97         case tar.TypeBlock:
98                 mode |= syscall.S_IFBLK
99         case tar.TypeChar:
100                 mode |= syscall.S_IFCHR
101         case tar.TypeFifo:
102                 mode |= syscall.S_IFIFO
103         }
104
105         return system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
106 }
107
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 {
112                                 return err
113                         }
114                 }
115         } else if hdr.Typeflag != tar.TypeSymlink {
116                 if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
117                         return err
118                 }
119         }
120         return nil
121 }