github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/graphdriver/copy/copy.go (about) 1 // +build linux 2 3 package copy // import "github.com/demonoid81/moby/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/demonoid81/moby/pkg/pools" 15 "github.com/demonoid81/moby/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 137 stat, ok := f.Sys().(*syscall.Stat_t) 138 if !ok { 139 return fmt.Errorf("Unable to get raw syscall.Stat_t data for %s", srcPath) 140 } 141 142 isHardlink := false 143 144 switch mode := f.Mode(); { 145 case mode.IsRegular(): 146 // the type is 32bit on mips 147 id := fileID{dev: uint64(stat.Dev), ino: stat.Ino} // nolint: unconvert 148 if copyMode == Hardlink { 149 isHardlink = true 150 if err2 := os.Link(srcPath, dstPath); err2 != nil { 151 return err2 152 } 153 } else if hardLinkDstPath, ok := copiedFiles[id]; ok { 154 if err2 := os.Link(hardLinkDstPath, dstPath); err2 != nil { 155 return err2 156 } 157 } else { 158 if err2 := copyRegular(srcPath, dstPath, f, ©WithFileRange, ©WithFileClone); err2 != nil { 159 return err2 160 } 161 copiedFiles[id] = dstPath 162 } 163 164 case mode.IsDir(): 165 if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) { 166 return err 167 } 168 169 case mode&os.ModeSymlink != 0: 170 link, err := os.Readlink(srcPath) 171 if err != nil { 172 return err 173 } 174 175 if err := os.Symlink(link, dstPath); err != nil { 176 return err 177 } 178 179 case mode&os.ModeNamedPipe != 0: 180 fallthrough 181 case mode&os.ModeSocket != 0: 182 if err := unix.Mkfifo(dstPath, stat.Mode); err != nil { 183 return err 184 } 185 186 case mode&os.ModeDevice != 0: 187 if rsystem.RunningInUserNS() { 188 // cannot create a device if running in user namespace 189 return nil 190 } 191 if err := unix.Mknod(dstPath, stat.Mode, int(stat.Rdev)); err != nil { 192 return err 193 } 194 195 default: 196 return fmt.Errorf("unknown file type (%d / %s) for %s", f.Mode(), f.Mode().String(), srcPath) 197 } 198 199 // Everything below is copying metadata from src to dst. All this metadata 200 // already shares an inode for hardlinks. 201 if isHardlink { 202 return nil 203 } 204 205 if err := os.Lchown(dstPath, int(stat.Uid), int(stat.Gid)); err != nil { 206 return err 207 } 208 209 if copyXattrs { 210 if err := doCopyXattrs(srcPath, dstPath); err != nil { 211 return err 212 } 213 } 214 215 isSymlink := f.Mode()&os.ModeSymlink != 0 216 217 // There is no LChmod, so ignore mode for symlink. Also, this 218 // must happen after chown, as that can modify the file mode 219 if !isSymlink { 220 if err := os.Chmod(dstPath, f.Mode()); err != nil { 221 return err 222 } 223 } 224 225 // system.Chtimes doesn't support a NOFOLLOW flag atm 226 // nolint: unconvert 227 if f.IsDir() { 228 dirsToSetMtimes.PushFront(&dirMtimeInfo{dstPath: &dstPath, stat: stat}) 229 } else if !isSymlink { 230 aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec)) 231 mTime := time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec)) 232 if err := system.Chtimes(dstPath, aTime, mTime); err != nil { 233 return err 234 } 235 } else { 236 ts := []syscall.Timespec{stat.Atim, stat.Mtim} 237 if err := system.LUtimesNano(dstPath, ts); err != nil { 238 return err 239 } 240 } 241 return nil 242 }) 243 if err != nil { 244 return err 245 } 246 for e := dirsToSetMtimes.Front(); e != nil; e = e.Next() { 247 mtimeInfo := e.Value.(*dirMtimeInfo) 248 ts := []syscall.Timespec{mtimeInfo.stat.Atim, mtimeInfo.stat.Mtim} 249 if err := system.LUtimesNano(*mtimeInfo.dstPath, ts); err != nil { 250 return err 251 } 252 } 253 254 return nil 255 } 256 257 func doCopyXattrs(srcPath, dstPath string) error { 258 if err := copyXattr(srcPath, dstPath, "security.capability"); err != nil { 259 return err 260 } 261 262 // We need to copy this attribute if it appears in an overlay upper layer, as 263 // this function is used to copy those. It is set by overlay if a directory 264 // is removed and then re-created and should not inherit anything from the 265 // same dir in the lower dir. 266 return copyXattr(srcPath, dstPath, "trusted.overlay.opaque") 267 }