github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/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,
   114  // properly handling xattrs, and soft links
   115  //
   116  // Copying xattrs can be opted out of by passing false for copyXattrs.
   117  func DirCopy(srcDir, dstDir string, copyMode Mode, copyXattrs bool) error {
   118  	copyWithFileRange := true
   119  	copyWithFileClone := true
   120  
   121  	// This is a map of source file inodes to dst file paths
   122  	copiedFiles := make(map[fileID]string)
   123  
   124  	dirsToSetMtimes := list.New()
   125  	err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
   126  		if err != nil {
   127  			return err
   128  		}
   129  
   130  		// Rebase path
   131  		relPath, err := filepath.Rel(srcDir, srcPath)
   132  		if err != nil {
   133  			return err
   134  		}
   135  
   136  		dstPath := filepath.Join(dstDir, relPath)
   137  
   138  		stat, ok := f.Sys().(*syscall.Stat_t)
   139  		if !ok {
   140  			return fmt.Errorf("Unable to get raw syscall.Stat_t data for %s", srcPath)
   141  		}
   142  
   143  		isHardlink := false
   144  
   145  		switch mode := f.Mode(); {
   146  		case mode.IsRegular():
   147  			// the type is 32bit on mips
   148  			id := fileID{dev: uint64(stat.Dev), ino: stat.Ino} //nolint: unconvert
   149  			if copyMode == Hardlink {
   150  				isHardlink = true
   151  				if err2 := os.Link(srcPath, dstPath); err2 != nil {
   152  					return err2
   153  				}
   154  			} else if hardLinkDstPath, ok := copiedFiles[id]; ok {
   155  				if err2 := os.Link(hardLinkDstPath, dstPath); err2 != nil {
   156  					return err2
   157  				}
   158  			} else {
   159  				if err2 := copyRegular(srcPath, dstPath, f, &copyWithFileRange, &copyWithFileClone); err2 != nil {
   160  					return err2
   161  				}
   162  				copiedFiles[id] = dstPath
   163  			}
   164  
   165  		case mode.IsDir():
   166  			if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
   167  				return err
   168  			}
   169  
   170  		case mode&os.ModeSymlink != 0:
   171  			link, err := os.Readlink(srcPath)
   172  			if err != nil {
   173  				return err
   174  			}
   175  
   176  			if err := os.Symlink(link, dstPath); err != nil {
   177  				return err
   178  			}
   179  
   180  		case mode&os.ModeNamedPipe != 0:
   181  			fallthrough
   182  		case mode&os.ModeSocket != 0:
   183  			if err := unix.Mkfifo(dstPath, stat.Mode); err != nil {
   184  				return err
   185  			}
   186  
   187  		case mode&os.ModeDevice != 0:
   188  			if userns.RunningInUserNS() {
   189  				// cannot create a device if running in user namespace
   190  				return nil
   191  			}
   192  			if err := unix.Mknod(dstPath, stat.Mode, int(stat.Rdev)); err != nil {
   193  				return err
   194  			}
   195  
   196  		default:
   197  			return fmt.Errorf("unknown file type (%d / %s) for %s", f.Mode(), f.Mode().String(), srcPath)
   198  		}
   199  
   200  		// Everything below is copying metadata from src to dst. All this metadata
   201  		// already shares an inode for hardlinks.
   202  		if isHardlink {
   203  			return nil
   204  		}
   205  
   206  		if err := os.Lchown(dstPath, int(stat.Uid), int(stat.Gid)); err != nil {
   207  			return err
   208  		}
   209  
   210  		if copyXattrs {
   211  			if err := doCopyXattrs(srcPath, dstPath); err != nil {
   212  				return err
   213  			}
   214  		}
   215  
   216  		isSymlink := f.Mode()&os.ModeSymlink != 0
   217  
   218  		// There is no LChmod, so ignore mode for symlink. Also, this
   219  		// must happen after chown, as that can modify the file mode
   220  		if !isSymlink {
   221  			if err := os.Chmod(dstPath, f.Mode()); err != nil {
   222  				return err
   223  			}
   224  		}
   225  
   226  		// system.Chtimes doesn't support a NOFOLLOW flag atm
   227  		//nolint: unconvert
   228  		if f.IsDir() {
   229  			dirsToSetMtimes.PushFront(&dirMtimeInfo{dstPath: &dstPath, stat: stat})
   230  		} else if !isSymlink {
   231  			aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
   232  			mTime := time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec))
   233  			if err := system.Chtimes(dstPath, aTime, mTime); err != nil {
   234  				return err
   235  			}
   236  		} else {
   237  			ts := []syscall.Timespec{stat.Atim, stat.Mtim}
   238  			if err := system.LUtimesNano(dstPath, ts); err != nil {
   239  				return err
   240  			}
   241  		}
   242  		return nil
   243  	})
   244  	if err != nil {
   245  		return err
   246  	}
   247  	for e := dirsToSetMtimes.Front(); e != nil; e = e.Next() {
   248  		mtimeInfo := e.Value.(*dirMtimeInfo)
   249  		ts := []syscall.Timespec{mtimeInfo.stat.Atim, mtimeInfo.stat.Mtim}
   250  		if err := system.LUtimesNano(*mtimeInfo.dstPath, ts); err != nil {
   251  			return err
   252  		}
   253  	}
   254  
   255  	return nil
   256  }
   257  
   258  func doCopyXattrs(srcPath, dstPath string) error {
   259  	if err := copyXattr(srcPath, dstPath, "security.capability"); err != nil {
   260  		return err
   261  	}
   262  
   263  	// We need to copy this attribute if it appears in an overlay upper layer, as
   264  	// this function is used to copy those. It is set by overlay if a directory
   265  	// is removed and then re-created and should not inherit anything from the
   266  	// same dir in the lower dir.
   267  	return copyXattr(srcPath, dstPath, "trusted.overlay.opaque")
   268  }