github.com/rentongzhang/docker@v1.8.2-rc1/pkg/archive/archive_unix.go (about)

     1  // +build !windows
     2  
     3  package archive
     4  
     5  import (
     6  	"archive/tar"
     7  	"errors"
     8  	"os"
     9  	"syscall"
    10  
    11  	"github.com/docker/docker/pkg/system"
    12  )
    13  
    14  // CanonicalTarNameForPath returns platform-specific filepath
    15  // to canonical posix-style path for tar archival. p is relative
    16  // path.
    17  func CanonicalTarNameForPath(p string) (string, error) {
    18  	return p, nil // already unix-style
    19  }
    20  
    21  // chmodTarEntry is used to adjust the file permissions used in tar header based
    22  // on the platform the archival is done.
    23  
    24  func chmodTarEntry(perm os.FileMode) os.FileMode {
    25  	return perm // noop for unix as golang APIs provide perm bits correctly
    26  }
    27  
    28  func setHeaderForSpecialDevice(hdr *tar.Header, ta *tarAppender, name string, stat interface{}) (nlink uint32, inode uint64, err error) {
    29  	s, ok := stat.(*syscall.Stat_t)
    30  
    31  	if !ok {
    32  		err = errors.New("cannot convert stat value to syscall.Stat_t")
    33  		return
    34  	}
    35  
    36  	nlink = uint32(s.Nlink)
    37  	inode = uint64(s.Ino)
    38  
    39  	// Currently go does not fil in the major/minors
    40  	if s.Mode&syscall.S_IFBLK != 0 ||
    41  		s.Mode&syscall.S_IFCHR != 0 {
    42  		hdr.Devmajor = int64(major(uint64(s.Rdev)))
    43  		hdr.Devminor = int64(minor(uint64(s.Rdev)))
    44  	}
    45  
    46  	return
    47  }
    48  
    49  func major(device uint64) uint64 {
    50  	return (device >> 8) & 0xfff
    51  }
    52  
    53  func minor(device uint64) uint64 {
    54  	return (device & 0xff) | ((device >> 12) & 0xfff00)
    55  }
    56  
    57  // handleTarTypeBlockCharFifo is an OS-specific helper function used by
    58  // createTarFile to handle the following types of header: Block; Char; Fifo
    59  func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
    60  	mode := uint32(hdr.Mode & 07777)
    61  	switch hdr.Typeflag {
    62  	case tar.TypeBlock:
    63  		mode |= syscall.S_IFBLK
    64  	case tar.TypeChar:
    65  		mode |= syscall.S_IFCHR
    66  	case tar.TypeFifo:
    67  		mode |= syscall.S_IFIFO
    68  	}
    69  
    70  	if err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
    71  		return err
    72  	}
    73  	return nil
    74  }
    75  
    76  func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
    77  	if hdr.Typeflag == tar.TypeLink {
    78  		if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
    79  			if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
    80  				return err
    81  			}
    82  		}
    83  	} else if hdr.Typeflag != tar.TypeSymlink {
    84  		if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
    85  			return err
    86  		}
    87  	}
    88  	return nil
    89  }