github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/pkg/archive/archive_unix.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package archive // import "github.com/docker/docker/pkg/archive"
     5  
     6  import (
     7  	"archive/tar"
     8  	"errors"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"syscall"
    13  
    14  	"github.com/containerd/containerd/pkg/userns"
    15  	"github.com/docker/docker/pkg/idtools"
    16  	"github.com/docker/docker/pkg/system"
    17  	"golang.org/x/sys/unix"
    18  )
    19  
    20  func init() {
    21  	sysStat = statUnix
    22  }
    23  
    24  // fixVolumePathPrefix does platform specific processing to ensure that if
    25  // the path being passed in is not in a volume path format, convert it to one.
    26  func fixVolumePathPrefix(srcPath string) string {
    27  	return srcPath
    28  }
    29  
    30  // getWalkRoot calculates the root path when performing a TarWithOptions.
    31  // We use a separate function as this is platform specific. On Linux, we
    32  // can't use filepath.Join(srcPath,include) because this will clean away
    33  // a trailing "." or "/" which may be important.
    34  func getWalkRoot(srcPath string, include string) string {
    35  	return strings.TrimSuffix(srcPath, string(filepath.Separator)) + string(filepath.Separator) + include
    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  func chmodTarEntry(perm os.FileMode) os.FileMode {
    41  	return perm // noop for unix as golang APIs provide perm bits correctly
    42  }
    43  
    44  // statUnix populates hdr from system-dependent fields of fi without performing
    45  // any OS lookups.
    46  func statUnix(fi os.FileInfo, hdr *tar.Header) error {
    47  	s, ok := fi.Sys().(*syscall.Stat_t)
    48  	if !ok {
    49  		return nil
    50  	}
    51  
    52  	hdr.Uid = int(s.Uid)
    53  	hdr.Gid = int(s.Gid)
    54  
    55  	if s.Mode&unix.S_IFBLK != 0 ||
    56  		s.Mode&unix.S_IFCHR != 0 {
    57  		hdr.Devmajor = int64(unix.Major(uint64(s.Rdev))) //nolint: unconvert
    58  		hdr.Devminor = int64(unix.Minor(uint64(s.Rdev))) //nolint: unconvert
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func getInodeFromStat(stat interface{}) (inode uint64, err error) {
    65  	s, ok := stat.(*syscall.Stat_t)
    66  
    67  	if ok {
    68  		inode = s.Ino
    69  	}
    70  
    71  	return
    72  }
    73  
    74  func getFileUIDGID(stat interface{}) (idtools.Identity, error) {
    75  	s, ok := stat.(*syscall.Stat_t)
    76  
    77  	if !ok {
    78  		return idtools.Identity{}, errors.New("cannot convert stat value to syscall.Stat_t")
    79  	}
    80  	return idtools.Identity{UID: int(s.Uid), GID: int(s.Gid)}, nil
    81  }
    82  
    83  // handleTarTypeBlockCharFifo is an OS-specific helper function used by
    84  // createTarFile to handle the following types of header: Block; Char; Fifo
    85  func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
    86  	mode := uint32(hdr.Mode & 07777)
    87  	switch hdr.Typeflag {
    88  	case tar.TypeBlock:
    89  		mode |= unix.S_IFBLK
    90  	case tar.TypeChar:
    91  		mode |= unix.S_IFCHR
    92  	case tar.TypeFifo:
    93  		mode |= unix.S_IFIFO
    94  	}
    95  
    96  	err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
    97  	if errors.Is(err, syscall.EPERM) && userns.RunningInUserNS() {
    98  		// In most cases, cannot create a device if running in user namespace
    99  		err = nil
   100  	}
   101  	return err
   102  }
   103  
   104  func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
   105  	if hdr.Typeflag == tar.TypeLink {
   106  		if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
   107  			if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
   108  				return err
   109  			}
   110  		}
   111  	} else if hdr.Typeflag != tar.TypeSymlink {
   112  		if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
   113  			return err
   114  		}
   115  	}
   116  	return nil
   117  }