2014-11-13 12:36:05 -08:00
|
|
|
// +build !windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package archive // import "github.com/docker/docker/pkg/archive"
|
2014-11-13 12:36:05 -08:00
|
|
|
|
|
|
|
import (
|
2015-05-01 16:01:10 -06:00
|
|
|
"archive/tar"
|
2014-11-13 12:36:05 -08:00
|
|
|
"errors"
|
2015-03-03 18:40:16 -08:00
|
|
|
"os"
|
2015-08-10 15:21:30 -07:00
|
|
|
"path/filepath"
|
2014-11-13 12:36:05 -08:00
|
|
|
"syscall"
|
2015-05-07 15:14:11 -07:00
|
|
|
|
2017-05-31 17:18:04 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-05-07 15:14:11 -07:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-02-12 16:05:50 -08:00
|
|
|
rsystem "github.com/opencontainers/runc/libcontainer/system"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2014-11-13 12:36:05 -08:00
|
|
|
)
|
|
|
|
|
2015-08-10 15:21:30 -07: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 18:00:39 +02:00
|
|
|
// We use a separate function as this is platform specific. On Linux, we
|
2015-08-10 15:21:30 -07: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 12:51:27 +03:00
|
|
|
// CanonicalTarNameForPath returns platform-specific filepath
|
2015-02-17 12:27:07 -08:00
|
|
|
// to canonical posix-style path for tar archival. p is relative
|
|
|
|
// path.
|
2018-06-27 12:07:17 -07:00
|
|
|
func CanonicalTarNameForPath(p string) string {
|
|
|
|
return p // already unix-style
|
2015-02-17 12:27:07 -08:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:40:16 -08: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
|
|
|
|
}
|
|
|
|
|
2017-03-15 15:28:06 -07:00
|
|
|
func setHeaderForSpecialDevice(hdr *tar.Header, name string, stat interface{}) (err error) {
|
2014-11-13 12:36:05 -08:00
|
|
|
s, ok := stat.(*syscall.Stat_t)
|
|
|
|
|
2017-05-15 14:54:27 -07:00
|
|
|
if ok {
|
|
|
|
// Currently go does not fill in the major/minors
|
2017-05-23 10:22:32 -04:00
|
|
|
if s.Mode&unix.S_IFBLK != 0 ||
|
|
|
|
s.Mode&unix.S_IFCHR != 0 {
|
2017-10-18 10:43:31 +02:00
|
|
|
hdr.Devmajor = int64(unix.Major(uint64(s.Rdev))) // nolint: unconvert
|
|
|
|
hdr.Devminor = int64(unix.Minor(uint64(s.Rdev))) // nolint: unconvert
|
2017-05-15 14:54:27 -07:00
|
|
|
}
|
2014-11-13 12:36:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-15 15:28:06 -07:00
|
|
|
func getInodeFromStat(stat interface{}) (inode uint64, err error) {
|
|
|
|
s, ok := stat.(*syscall.Stat_t)
|
|
|
|
|
2017-05-15 14:54:27 -07:00
|
|
|
if ok {
|
2017-08-24 13:11:44 -04:00
|
|
|
inode = s.Ino
|
2017-03-15 15:28:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-31 17:18:04 -04:00
|
|
|
func getFileUIDGID(stat interface{}) (idtools.IDPair, error) {
|
2015-10-08 11:51:41 -04:00
|
|
|
s, ok := stat.(*syscall.Stat_t)
|
|
|
|
|
|
|
|
if !ok {
|
2017-05-31 17:18:04 -04:00
|
|
|
return idtools.IDPair{}, errors.New("cannot convert stat value to syscall.Stat_t")
|
2015-10-08 11:51:41 -04:00
|
|
|
}
|
2017-05-31 17:18:04 -04:00
|
|
|
return idtools.IDPair{UID: int(s.Uid), GID: int(s.Gid)}, nil
|
2015-10-08 11:51:41 -04:00
|
|
|
}
|
|
|
|
|
2015-05-07 15:14:11 -07: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 16:05:50 -08:00
|
|
|
if rsystem.RunningInUserNS() {
|
|
|
|
// cannot create a device if running in user namespace
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-07 15:14:11 -07:00
|
|
|
mode := uint32(hdr.Mode & 07777)
|
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeBlock:
|
2017-05-23 10:22:32 -04:00
|
|
|
mode |= unix.S_IFBLK
|
2015-05-07 15:14:11 -07:00
|
|
|
case tar.TypeChar:
|
2017-05-23 10:22:32 -04:00
|
|
|
mode |= unix.S_IFCHR
|
2015-05-07 15:14:11 -07:00
|
|
|
case tar.TypeFifo:
|
2017-05-23 10:22:32 -04:00
|
|
|
mode |= unix.S_IFIFO
|
2015-05-07 15:14:11 -07:00
|
|
|
}
|
|
|
|
|
2017-03-30 12:04:42 +03:00
|
|
|
return system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
|
2015-05-07 15:14:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|