copy.go 6.8 KB

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