2014-11-13 15:36:05 -05:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
2015-05-01 18:01:10 -04:00
|
|
|
"archive/tar"
|
2014-11-13 15:36:05 -05:00
|
|
|
"errors"
|
2015-03-03 21:40:16 -05:00
|
|
|
"os"
|
2015-08-10 18:21:30 -04:00
|
|
|
"path/filepath"
|
2014-11-13 15:36:05 -05:00
|
|
|
"syscall"
|
2015-05-07 18:14:11 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-02-12 19:05:50 -05:00
|
|
|
rsystem "github.com/opencontainers/runc/libcontainer/system"
|
2014-11-13 15:36:05 -05:00
|
|
|
)
|
|
|
|
|
2015-08-10 18:21:30 -04:00
|
|
|
// fixVolumePathPrefix does platform specific processing to ensure that if
|
|
|
|
// the path being passed in is not in a volume path format, convert it to one.
|
|
|
|
func fixVolumePathPrefix(srcPath string) string {
|
|
|
|
return srcPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// getWalkRoot calculates the root path when performing a TarWithOptions.
|
2015-12-13 11:00:39 -05:00
|
|
|
// We use a separate function as this is platform specific. On Linux, we
|
2015-08-10 18:21:30 -04:00
|
|
|
// can't use filepath.Join(srcPath,include) because this will clean away
|
|
|
|
// a trailing "." or "/" which may be important.
|
|
|
|
func getWalkRoot(srcPath string, include string) string {
|
|
|
|
return srcPath + string(filepath.Separator) + include
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:51:27 -04:00
|
|
|
// CanonicalTarNameForPath returns platform-specific filepath
|
2015-02-17 15:27:07 -05:00
|
|
|
// to canonical posix-style path for tar archival. p is relative
|
|
|
|
// path.
|
2015-02-24 22:43:29 -05:00
|
|
|
func CanonicalTarNameForPath(p string) (string, error) {
|
2015-02-17 15:27:07 -05:00
|
|
|
return p, nil // already unix-style
|
|
|
|
}
|
|
|
|
|
2015-03-03 21:40:16 -05:00
|
|
|
// chmodTarEntry is used to adjust the file permissions used in tar header based
|
|
|
|
// on the platform the archival is done.
|
|
|
|
|
|
|
|
func chmodTarEntry(perm os.FileMode) os.FileMode {
|
|
|
|
return perm // noop for unix as golang APIs provide perm bits correctly
|
|
|
|
}
|
|
|
|
|
2015-10-12 15:11:22 -04:00
|
|
|
func setHeaderForSpecialDevice(hdr *tar.Header, ta *tarAppender, name string, stat interface{}) (inode uint64, err error) {
|
2014-11-13 15:36:05 -05:00
|
|
|
s, ok := stat.(*syscall.Stat_t)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("cannot convert stat value to syscall.Stat_t")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inode = uint64(s.Ino)
|
|
|
|
|
2015-08-19 20:44:02 -04:00
|
|
|
// Currently go does not fill in the major/minors
|
2015-03-24 21:09:25 -04:00
|
|
|
if s.Mode&syscall.S_IFBLK != 0 ||
|
|
|
|
s.Mode&syscall.S_IFCHR != 0 {
|
2014-11-13 15:36:05 -05:00
|
|
|
hdr.Devmajor = int64(major(uint64(s.Rdev)))
|
|
|
|
hdr.Devminor = int64(minor(uint64(s.Rdev)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
func getFileUIDGID(stat interface{}) (int, int, error) {
|
|
|
|
s, ok := stat.(*syscall.Stat_t)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return -1, -1, errors.New("cannot convert stat value to syscall.Stat_t")
|
|
|
|
}
|
|
|
|
return int(s.Uid), int(s.Gid), nil
|
|
|
|
}
|
|
|
|
|
2014-11-13 15:36:05 -05:00
|
|
|
func major(device uint64) uint64 {
|
|
|
|
return (device >> 8) & 0xfff
|
|
|
|
}
|
|
|
|
|
|
|
|
func minor(device uint64) uint64 {
|
|
|
|
return (device & 0xff) | ((device >> 12) & 0xfff00)
|
|
|
|
}
|
2015-05-07 18:14:11 -04:00
|
|
|
|
|
|
|
// handleTarTypeBlockCharFifo is an OS-specific helper function used by
|
|
|
|
// createTarFile to handle the following types of header: Block; Char; Fifo
|
|
|
|
func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
|
2016-02-12 19:05:50 -05:00
|
|
|
if rsystem.RunningInUserNS() {
|
|
|
|
// cannot create a device if running in user namespace
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:14:11 -04:00
|
|
|
mode := uint32(hdr.Mode & 07777)
|
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeBlock:
|
|
|
|
mode |= syscall.S_IFBLK
|
|
|
|
case tar.TypeChar:
|
|
|
|
mode |= syscall.S_IFCHR
|
|
|
|
case tar.TypeFifo:
|
|
|
|
mode |= syscall.S_IFIFO
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
|
|
|
|
if hdr.Typeflag == tar.TypeLink {
|
|
|
|
if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
|
|
|
|
if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if hdr.Typeflag != tar.TypeSymlink {
|
|
|
|
if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|