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