github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/graphdriver/copy/copy.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package copy // import "github.com/docker/docker/daemon/graphdriver/copy"
     5  
     6  import (
     7  	"container/list"
     8  	"fmt"
     9  	"io"
    10  	"os"
    11  	"path/filepath"
    12  	"syscall"
    13  	"time"
    14  
    15  	"github.com/containerd/containerd/pkg/userns"
    16  	"github.com/docker/docker/pkg/pools"
    17  	"github.com/docker/docker/pkg/system"
    18  	"golang.org/x/sys/unix"
    19  )
    20  
    21  // Mode indicates whether to use hardlink or copy content
    22  type Mode int
    23  
    24  const (
    25  	// Content creates a new file, and copies the content of the file
    26  	Content Mode = iota
    27  	// Hardlink creates a new hardlink to the existing file
    28  	Hardlink
    29  )
    30  
    31  func copyRegular(srcPath, dstPath string, fileinfo os.FileInfo, copyWithFileRange, copyWithFileClone *bool) error {
    32  	srcFile, err := os.Open(srcPath)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	defer srcFile.Close()
    37  
    38  	// If the destination file already exists, we shouldn't blow it away
    39  	dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, fileinfo.Mode())
    40  	if err != nil {
    41  		return err
    42  	}
    43  	defer dstFile.Close()
    44  
    45  	if *copyWithFileClone {
    46  		err = unix.IoctlFileClone(int(dstFile.Fd()), int(srcFile.Fd()))
    47  		if err == nil {
    48  			return nil
    49  		}
    50  
    51  		*copyWithFileClone = false
    52  		if err == unix.EXDEV {
    53  			*copyWithFileRange = false
    54  		}
    55  	}
    56  	if *copyWithFileRange {
    57  		err = doCopyWithFileRange(srcFile, dstFile, fileinfo)
    58  		// Trying the file_clone may not have caught the exdev case
    59  		// as the ioctl may not have been available (therefore EINVAL)
    60  		if err == unix.EXDEV || err == unix.ENOSYS {
    61  			*copyWithFileRange = false
    62  		} else {
    63  			return err
    64  		}
    65  	}
    66  	return legacyCopy(srcFile, dstFile)
    67  }
    68  
    69  func doCopyWithFileRange(srcFile, dstFile *os.File, fileinfo os.FileInfo) error {
    70  	amountLeftToCopy := fileinfo.Size()
    71  
    72  	for amountLeftToCopy > 0 {
    73  		n, err := unix.CopyFileRange(int(srcFile.Fd()), nil, int(dstFile.Fd()), nil, int(amountLeftToCopy), 0)
    74  		if err != nil {
    75  			return err
    76  		}
    77  
    78  		amountLeftToCopy = amountLeftToCopy - int64(n)
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  func legacyCopy(srcFile io.Reader, dstFile io.Writer) error {
    85  	_, err := pools.Copy(dstFile, srcFile)
    86  
    87  	return err
    88  }
    89  
    90  func copyXattr(srcPath, dstPath, attr string) error {
    91  	data, err := system.Lgetxattr(srcPath, attr)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	if data != nil {
    96  		if err := system.Lsetxattr(dstPath, attr, data, 0); err != nil {
    97  			return err
    98  		}
    99  	}
   100  	return nil
   101  }
   102  
   103  type fileID struct {
   104  	dev uint64
   105  	ino uint64
   106  }
   107  
   108  type dirMtimeInfo struct {
   109  	dstPath *string
   110  	stat    *syscall.Stat_t
   111  }
   112  
   113  // DirCopy copies or hardlinks the contents of one directory to another, properly
   114  // handling soft links, "security.capability" and (optionally) "trusted.overlay.opaque"
   115  // xattrs.
   116  //
   117  // The copyOpaqueXattrs controls if "trusted.overlay.opaque" xattrs are copied.
   118  // Passing false disables copying "trusted.overlay.opaque" xattrs.
   119  func DirCopy(srcDir, dstDir string, copyMode Mode, copyOpaqueXattrs bool) error {
   120  	copyWithFileRange := true
   121  	copyWithFileClone := true
   122  
   123  	// This is a map of source file inodes to dst file paths
   124  	copiedFiles := make(map[fileID]string)
   125  
   126  	dirsToSetMtimes := list.New()
   127  	err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
   128  		if err != nil {
   129  			return err
   130  		}
   131  
   132  		// Rebase path
   133  		relPath, err := filepath.Rel(srcDir, srcPath)
   134  		if err != nil {
   135  			return err
   136  		}
   137  
   138  		dstPath := filepath.Join(dstDir, relPath)
   139  
   140  		stat, ok := f.Sys().(*syscall.Stat_t)
   141  		if !ok {
   142  			return fmt.Errorf("Unable to get raw syscall.Stat_t data for %s", srcPath)
   143  		}
   144  
   145  		isHardlink := false
   146  
   147  		switch mode := f.Mode(); {
   148  		case mode.IsRegular():
   149  			// the type is 32bit on mips
   150  			id := fileID{dev: uint64(stat.Dev), ino: stat.Ino} //nolint: unconvert
   151  			if copyMode == Hardlink {
   152  				isHardlink = true
   153  				if err2 := os.Link(srcPath, dstPath); err2 != nil {
   154  					return err2
   155  				}
   156  			} else if hardLinkDstPath, ok := copiedFiles[id]; ok {
   157  				if err2 := os.Link(hardLinkDstPath, dstPath); err2 != nil {
   158  					return err2
   159  				}
   160  			} else {
   161  				if err2 := copyRegular(srcPath, dstPath, f, &copyWithFileRange, &copyWithFileClone); err2 != nil {
   162  					return err2
   163  				}
   164  				copiedFiles[id] = dstPath
   165  			}
   166  
   167  		case mode.IsDir():
   168  			if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
   169  				return err
   170  			}
   171  
   172  		case mode&os.ModeSymlink != 0:
   173  			link, err := os.Readlink(srcPath)
   174  			if err != nil {
   175  				return err
   176  			}
   177  
   178  			if err := os.Symlink(link, dstPath); err != nil {
   179  				return err
   180  			}
   181  
   182  		case mode&os.ModeNamedPipe != 0:
   183  			fallthrough
   184  		case mode&os.ModeSocket != 0:
   185  			if err := unix.Mkfifo(dstPath, stat.Mode); err != nil {
   186  				return err
   187  			}
   188  
   189  		case mode&os.ModeDevice != 0:
   190  			if userns.RunningInUserNS() {
   191  				// cannot create a device if running in user namespace
   192  				return nil
   193  			}
   194  			if err := unix.Mknod(dstPath, stat.Mode, int(stat.Rdev)); err != nil {
   195  				return err
   196  			}
   197  
   198  		default:
   199  			return fmt.Errorf("unknown file type (%d / %s) for %s", f.Mode(), f.Mode().String(), srcPath)
   200  		}
   201  
   202  		// Everything below is copying metadata from src to dst. All this metadata
   203  		// already shares an inode for hardlinks.
   204  		if isHardlink {
   205  			return nil
   206  		}
   207  
   208  		if err := os.Lchown(dstPath, int(stat.Uid), int(stat.Gid)); err != nil {
   209  			return err
   210  		}
   211  
   212  		if err := copyXattr(srcPath, dstPath, "security.capability"); err != nil {
   213  			return err
   214  		}
   215  
   216  		if copyOpaqueXattrs {
   217  			if err := doCopyXattrs(srcPath, dstPath); err != nil {
   218  				return err
   219  			}
   220  		}
   221  
   222  		isSymlink := f.Mode()&os.ModeSymlink != 0
   223  
   224  		// There is no LChmod, so ignore mode for symlink. Also, this
   225  		// must happen after chown, as that can modify the file mode
   226  		if !isSymlink {
   227  			if err := os.Chmod(dstPath, f.Mode()); err != nil {
   228  				return err
   229  			}
   230  		}
   231  
   232  		// system.Chtimes doesn't support a NOFOLLOW flag atm
   233  		//nolint: unconvert
   234  		if f.IsDir() {
   235  			dirsToSetMtimes.PushFront(&dirMtimeInfo{dstPath: &dstPath, stat: stat})
   236  		} else if !isSymlink {
   237  			aTime := time.Unix(stat.Atim.Unix())
   238  			mTime := time.Unix(stat.Mtim.Unix())
   239  			if err := system.Chtimes(dstPath, aTime, mTime); err != nil {
   240  				return err
   241  			}
   242  		} else {
   243  			ts := []syscall.Timespec{stat.Atim, stat.Mtim}
   244  			if err := system.LUtimesNano(dstPath, ts); err != nil {
   245  				return err
   246  			}
   247  		}
   248  		return nil
   249  	})
   250  	if err != nil {
   251  		return err
   252  	}
   253  	for e := dirsToSetMtimes.Front(); e != nil; e = e.Next() {
   254  		mtimeInfo := e.Value.(*dirMtimeInfo)
   255  		ts := []syscall.Timespec{mtimeInfo.stat.Atim, mtimeInfo.stat.Mtim}
   256  		if err := system.LUtimesNano(*mtimeInfo.dstPath, ts); err != nil {
   257  			return err
   258  		}
   259  	}
   260  
   261  	return nil
   262  }
   263  
   264  func doCopyXattrs(srcPath, dstPath string) error {
   265  	// We need to copy this attribute if it appears in an overlay upper layer, as
   266  	// this function is used to copy those. It is set by overlay if a directory
   267  	// is removed and then re-created and should not inherit anything from the
   268  	// same dir in the lower dir.
   269  	return copyXattr(srcPath, dstPath, "trusted.overlay.opaque")
   270  }