github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/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 major(device uint64) uint64 {
    65  	return (device >> 8) & 0xfff
    66  }
    67  
    68  func minor(device uint64) uint64 {
    69  	return (device & 0xff) | ((device >> 12) & 0xfff00)
    70  }
    71  
    72  // handleTarTypeBlockCharFifo is an OS-specific helper function used by
    73  // createTarFile to handle the following types of header: Block; Char; Fifo
    74  func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
    75  	mode := uint32(hdr.Mode & 07777)
    76  	switch hdr.Typeflag {
    77  	case tar.TypeBlock:
    78  		mode |= syscall.S_IFBLK
    79  	case tar.TypeChar:
    80  		mode |= syscall.S_IFCHR
    81  	case tar.TypeFifo:
    82  		mode |= syscall.S_IFIFO
    83  	}
    84  
    85  	if err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
    86  		return err
    87  	}
    88  	return nil
    89  }
    90  
    91  func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
    92  	if hdr.Typeflag == tar.TypeLink {
    93  		if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
    94  			if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
    95  				return err
    96  			}
    97  		}
    98  	} else if hdr.Typeflag != tar.TypeSymlink {
    99  		if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
   100  			return err
   101  		}
   102  	}
   103  	return nil
   104  }