github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/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  // CanonicalTarNameForPath returns platform-specific filepath
    39  // to canonical posix-style path for tar archival. p is relative
    40  // path.
    41  func CanonicalTarNameForPath(p string) string {
    42  	return p // already unix-style
    43  }
    44  
    45  // chmodTarEntry is used to adjust the file permissions used in tar header based
    46  // on the platform the archival is done.
    47  
    48  func chmodTarEntry(perm os.FileMode) os.FileMode {
    49  	return perm // noop for unix as golang APIs provide perm bits correctly
    50  }
    51  
    52  // statUnix populates hdr from system-dependent fields of fi without performing
    53  // any OS lookups.
    54  func statUnix(fi os.FileInfo, hdr *tar.Header) error {
    55  	s, ok := fi.Sys().(*syscall.Stat_t)
    56  	if !ok {
    57  		return nil
    58  	}
    59  
    60  	hdr.Uid = int(s.Uid)
    61  	hdr.Gid = int(s.Gid)
    62  
    63  	if s.Mode&unix.S_IFBLK != 0 ||
    64  		s.Mode&unix.S_IFCHR != 0 {
    65  		hdr.Devmajor = int64(unix.Major(uint64(s.Rdev))) //nolint: unconvert
    66  		hdr.Devminor = int64(unix.Minor(uint64(s.Rdev))) //nolint: unconvert
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  func getInodeFromStat(stat interface{}) (inode uint64, err error) {
    73  	s, ok := stat.(*syscall.Stat_t)
    74  
    75  	if ok {
    76  		inode = s.Ino
    77  	}
    78  
    79  	return
    80  }
    81  
    82  func getFileUIDGID(stat interface{}) (idtools.Identity, error) {
    83  	s, ok := stat.(*syscall.Stat_t)
    84  
    85  	if !ok {
    86  		return idtools.Identity{}, errors.New("cannot convert stat value to syscall.Stat_t")
    87  	}
    88  	return idtools.Identity{UID: int(s.Uid), GID: int(s.Gid)}, nil
    89  }
    90  
    91  // handleTarTypeBlockCharFifo is an OS-specific helper function used by
    92  // createTarFile to handle the following types of header: Block; Char; Fifo
    93  func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
    94  	mode := uint32(hdr.Mode & 07777)
    95  	switch hdr.Typeflag {
    96  	case tar.TypeBlock:
    97  		mode |= unix.S_IFBLK
    98  	case tar.TypeChar:
    99  		mode |= unix.S_IFCHR
   100  	case tar.TypeFifo:
   101  		mode |= unix.S_IFIFO
   102  	}
   103  
   104  	err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
   105  	if errors.Is(err, syscall.EPERM) && userns.RunningInUserNS() {
   106  		// In most cases, cannot create a device if running in user namespace
   107  		err = nil
   108  	}
   109  	return err
   110  }
   111  
   112  func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
   113  	if hdr.Typeflag == tar.TypeLink {
   114  		if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
   115  			if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
   116  				return err
   117  			}
   118  		}
   119  	} else if hdr.Typeflag != tar.TypeSymlink {
   120  		if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
   121  			return err
   122  		}
   123  	}
   124  	return nil
   125  }