github.com/akerouanton/docker@v1.11.0-rc3/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/system"
    13  )
    14  
    15  // fixVolumePathPrefix does platform specific processing to ensure that if
    16  // the path being passed in is not in a volume path format, convert it to one.
    17  func fixVolumePathPrefix(srcPath string) string {
    18  	return srcPath
    19  }
    20  
    21  // getWalkRoot calculates the root path when performing a TarWithOptions.
    22  // We use a separate function as this is platform specific. On Linux, we
    23  // can't use filepath.Join(srcPath,include) because this will clean away
    24  // a trailing "." or "/" which may be important.
    25  func getWalkRoot(srcPath string, include string) string {
    26  	return srcPath + string(filepath.Separator) + include
    27  }
    28  
    29  // CanonicalTarNameForPath returns platform-specific filepath
    30  // to canonical posix-style path for tar archival. p is relative
    31  // path.
    32  func CanonicalTarNameForPath(p string) (string, error) {
    33  	return p, nil // already unix-style
    34  }
    35  
    36  // chmodTarEntry is used to adjust the file permissions used in tar header based
    37  // on the platform the archival is done.
    38  
    39  func chmodTarEntry(perm os.FileMode) os.FileMode {
    40  	return perm // noop for unix as golang APIs provide perm bits correctly
    41  }
    42  
    43  func setHeaderForSpecialDevice(hdr *tar.Header, ta *tarAppender, name string, stat interface{}) (inode uint64, err error) {
    44  	s, ok := stat.(*syscall.Stat_t)
    45  
    46  	if !ok {
    47  		err = errors.New("cannot convert stat value to syscall.Stat_t")
    48  		return
    49  	}
    50  
    51  	inode = uint64(s.Ino)
    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 getFileUIDGID(stat interface{}) (int, int, error) {
    64  	s, ok := stat.(*syscall.Stat_t)
    65  
    66  	if !ok {
    67  		return -1, -1, errors.New("cannot convert stat value to syscall.Stat_t")
    68  	}
    69  	return int(s.Uid), int(s.Gid), nil
    70  }
    71  
    72  func major(device uint64) uint64 {
    73  	return (device >> 8) & 0xfff
    74  }
    75  
    76  func minor(device uint64) uint64 {
    77  	return (device & 0xff) | ((device >> 12) & 0xfff00)
    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  	mode := uint32(hdr.Mode & 07777)
    84  	switch hdr.Typeflag {
    85  	case tar.TypeBlock:
    86  		mode |= syscall.S_IFBLK
    87  	case tar.TypeChar:
    88  		mode |= syscall.S_IFCHR
    89  	case tar.TypeFifo:
    90  		mode |= syscall.S_IFIFO
    91  	}
    92  
    93  	if err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
    94  		return err
    95  	}
    96  	return nil
    97  }
    98  
    99  func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
   100  	if hdr.Typeflag == tar.TypeLink {
   101  		if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
   102  			if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
   103  				return err
   104  			}
   105  		}
   106  	} else if hdr.Typeflag != tar.TypeSymlink {
   107  		if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
   108  			return err
   109  		}
   110  	}
   111  	return nil
   112  }