github.com/devdivbcp/moby@v17.12.0-ce-rc1.0.20200726071732-2d4bfdc789ad+incompatible/daemon/graphdriver/copy/copy.go (about) 1 // +build linux 2 3 package copy // import "github.com/docker/docker/daemon/graphdriver/copy" 4 5 import ( 6 "container/list" 7 "fmt" 8 "io" 9 "os" 10 "path/filepath" 11 "syscall" 12 "time" 13 14 "github.com/docker/docker/pkg/pools" 15 "github.com/docker/docker/pkg/system" 16 rsystem "github.com/opencontainers/runc/libcontainer/system" 17 "golang.org/x/sys/unix" 18 ) 19 20 // Mode indicates whether to use hardlink or copy content 21 type Mode int 22 23 const ( 24 // Content creates a new file, and copies the content of the file 25 Content Mode = iota 26 // Hardlink creates a new hardlink to the existing file 27 Hardlink 28 ) 29 30 func copyRegular(srcPath, dstPath string, fileinfo os.FileInfo, copyWithFileRange, copyWithFileClone *bool) error { 31 srcFile, err := os.Open(srcPath) 32 if err != nil { 33 return err 34 } 35 defer srcFile.Close() 36 37 // If the destination file already exists, we shouldn't blow it away 38 dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, fileinfo.Mode()) 39 if err != nil { 40 return err 41 } 42 defer dstFile.Close() 43 44 if *copyWithFileClone { 45 err = fiClone(srcFile, dstFile) 46 if err == nil { 47 return nil 48 } 49 50 *copyWithFileClone = false 51 if err == unix.EXDEV { 52 *copyWithFileRange = false 53 } 54 } 55 if *copyWithFileRange { 56 err = doCopyWithFileRange(srcFile, dstFile, fileinfo) 57 // Trying the file_clone may not have caught the exdev case 58 // as the ioctl may not have been available (therefore EINVAL) 59 if err == unix.EXDEV || err == unix.ENOSYS { 60 *copyWithFileRange = false 61 } else { 62 return err 63 } 64 } 65 return legacyCopy(srcFile, dstFile) 66 } 67 68 func doCopyWithFileRange(srcFile, dstFile *os.File, fileinfo os.FileInfo) error { 69 amountLeftToCopy := fileinfo.Size() 70 71 for amountLeftToCopy > 0 { 72 n, err := unix.CopyFileRange(int(srcFile.Fd()), nil, int(dstFile.Fd()), nil, int(amountLeftToCopy), 0) 73 if err != nil { 74 return err 75 } 76 77 amountLeftToCopy = amountLeftToCopy - int64(n) 78 } 79 80 return nil 81 } 82 83 func legacyCopy(srcFile io.Reader, dstFile io.Writer) error { 84 _, err := pools.Copy(dstFile, srcFile) 85 86 return err 87 } 88 89 func copyXattr(srcPath, dstPath, attr string) error { 90 data, err := system.Lgetxattr(srcPath, attr) 91 if err != nil { 92 return err 93 } 94 if data != nil { 95 if err := system.Lsetxattr(dstPath, attr, data, 0); err != nil { 96 return err 97 } 98 } 99 return nil 100 } 101 102 type fileID struct { 103 dev uint64 104 ino uint64 105 } 106 107 type dirMtimeInfo struct { 108 dstPath *string 109 stat *syscall.Stat_t 110 } 111 112 // DirCopy copies or hardlinks the contents of one directory to another, 113 // properly handling xattrs, and soft links 114 // 115 // Copying xattrs can be opted out of by passing false for copyXattrs. 116 func DirCopy(srcDir, dstDir string, copyMode Mode, copyXattrs bool) error { 117 copyWithFileRange := true 118 copyWithFileClone := true 119 120 // This is a map of source file inodes to dst file paths 121 copiedFiles := make(map[fileID]string) 122 123 dirsToSetMtimes := list.New() 124 err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error { 125 if err != nil { 126 return err 127 } 128 129 // Rebase path 130 relPath, err := filepath.Rel(srcDir, srcPath) 131 if err != nil { 132 return err 133 } 134 135 dstPath := filepath.Join(dstDir, relPath) 136 if err != nil { 137 return err 138 } 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, ©WithFileRange, ©WithFileClone); 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 rsystem.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 copyXattrs { 213 if err := doCopyXattrs(srcPath, dstPath); err != nil { 214 return err 215 } 216 } 217 218 isSymlink := f.Mode()&os.ModeSymlink != 0 219 220 // There is no LChmod, so ignore mode for symlink. Also, this 221 // must happen after chown, as that can modify the file mode 222 if !isSymlink { 223 if err := os.Chmod(dstPath, f.Mode()); err != nil { 224 return err 225 } 226 } 227 228 // system.Chtimes doesn't support a NOFOLLOW flag atm 229 // nolint: unconvert 230 if f.IsDir() { 231 dirsToSetMtimes.PushFront(&dirMtimeInfo{dstPath: &dstPath, stat: stat}) 232 } else if !isSymlink { 233 aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) 234 mTime := time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec)) 235 if err := system.Chtimes(dstPath, aTime, mTime); err != nil { 236 return err 237 } 238 } else { 239 ts := []syscall.Timespec{stat.Atim, stat.Mtim} 240 if err := system.LUtimesNano(dstPath, ts); err != nil { 241 return err 242 } 243 } 244 return nil 245 }) 246 if err != nil { 247 return err 248 } 249 for e := dirsToSetMtimes.Front(); e != nil; e = e.Next() { 250 mtimeInfo := e.Value.(*dirMtimeInfo) 251 ts := []syscall.Timespec{mtimeInfo.stat.Atim, mtimeInfo.stat.Mtim} 252 if err := system.LUtimesNano(*mtimeInfo.dstPath, ts); err != nil { 253 return err 254 } 255 } 256 257 return nil 258 } 259 260 func doCopyXattrs(srcPath, dstPath string) error { 261 if err := copyXattr(srcPath, dstPath, "security.capability"); err != nil { 262 return err 263 } 264 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 }