github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/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  )
    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  		err = errors.New("cannot convert stat value to syscall.Stat_t")
    50  		return
    51  	}
    52  
    53  	// Currently go does not fill in the major/minors
    54  	if s.Mode&syscall.S_IFBLK != 0 ||
    55  		s.Mode&syscall.S_IFCHR != 0 {
    56  		hdr.Devmajor = int64(major(uint64(s.Rdev)))
    57  		hdr.Devminor = int64(minor(uint64(s.Rdev)))
    58  	}
    59  
    60  	return
    61  }
    62  
    63  func getInodeFromStat(stat interface{}) (inode uint64, err error) {
    64  	s, ok := stat.(*syscall.Stat_t)
    65  
    66  	if !ok {
    67  		err = errors.New("cannot convert stat value to syscall.Stat_t")
    68  		return
    69  	}
    70  
    71  	inode = uint64(s.Ino)
    72  
    73  	return
    74  }
    75  
    76  func getFileUIDGID(stat interface{}) (idtools.IDPair, error) {
    77  	s, ok := stat.(*syscall.Stat_t)
    78  
    79  	if !ok {
    80  		return idtools.IDPair{}, errors.New("cannot convert stat value to syscall.Stat_t")
    81  	}
    82  	return idtools.IDPair{UID: int(s.Uid), GID: int(s.Gid)}, nil
    83  }
    84  
    85  func major(device uint64) uint64 {
    86  	return (device >> 8) & 0xfff
    87  }
    88  
    89  func minor(device uint64) uint64 {
    90  	return (device & 0xff) | ((device >> 12) & 0xfff00)
    91  }
    92  
    93  // handleTarTypeBlockCharFifo is an OS-specific helper function used by
    94  // createTarFile to handle the following types of header: Block; Char; Fifo
    95  func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
    96  	if rsystem.RunningInUserNS() {
    97  		// cannot create a device if running in user namespace
    98  		return nil
    99  	}
   100  
   101  	mode := uint32(hdr.Mode & 07777)
   102  	switch hdr.Typeflag {
   103  	case tar.TypeBlock:
   104  		mode |= syscall.S_IFBLK
   105  	case tar.TypeChar:
   106  		mode |= syscall.S_IFCHR
   107  	case tar.TypeFifo:
   108  		mode |= syscall.S_IFIFO
   109  	}
   110  
   111  	return system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
   112  }
   113  
   114  func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
   115  	if hdr.Typeflag == tar.TypeLink {
   116  		if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
   117  			if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
   118  				return err
   119  			}
   120  		}
   121  	} else if hdr.Typeflag != tar.TypeSymlink {
   122  		if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
   123  			return err
   124  		}
   125  	}
   126  	return nil
   127  }