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