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