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