copy.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // +build linux
  2. package copy
  3. /*
  4. #include <linux/fs.h>
  5. #ifndef FICLONE
  6. #define FICLONE _IOW(0x94, 9, int)
  7. #endif
  8. */
  9. import "C"
  10. import (
  11. "container/list"
  12. "fmt"
  13. "io"
  14. "os"
  15. "path/filepath"
  16. "syscall"
  17. "time"
  18. "github.com/docker/docker/pkg/pools"
  19. "github.com/docker/docker/pkg/system"
  20. rsystem "github.com/opencontainers/runc/libcontainer/system"
  21. "golang.org/x/sys/unix"
  22. )
  23. // Mode indicates whether to use hardlink or copy content
  24. type Mode int
  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. 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. // 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. if *copyWithFileClone {
  44. _, _, err = unix.Syscall(unix.SYS_IOCTL, dstFile.Fd(), C.FICLONE, srcFile.Fd())
  45. if err == nil {
  46. return nil
  47. }
  48. *copyWithFileClone = false
  49. if err == unix.EXDEV {
  50. *copyWithFileRange = false
  51. }
  52. }
  53. if *copyWithFileRange {
  54. err = doCopyWithFileRange(srcFile, dstFile, fileinfo)
  55. // Trying the file_clone may not have caught the exdev case
  56. // as the ioctl may not have been available (therefore EINVAL)
  57. if err == unix.EXDEV || err == unix.ENOSYS {
  58. *copyWithFileRange = false
  59. } else {
  60. return err
  61. }
  62. }
  63. return legacyCopy(srcFile, dstFile)
  64. }
  65. func doCopyWithFileRange(srcFile, dstFile *os.File, fileinfo os.FileInfo) error {
  66. amountLeftToCopy := fileinfo.Size()
  67. for amountLeftToCopy > 0 {
  68. n, err := unix.CopyFileRange(int(srcFile.Fd()), nil, int(dstFile.Fd()), nil, int(amountLeftToCopy), 0)
  69. if err != nil {
  70. return err
  71. }
  72. amountLeftToCopy = amountLeftToCopy - int64(n)
  73. }
  74. return nil
  75. }
  76. func legacyCopy(srcFile io.Reader, dstFile io.Writer) error {
  77. _, err := pools.Copy(dstFile, srcFile)
  78. return err
  79. }
  80. func copyXattr(srcPath, dstPath, attr string) error {
  81. data, err := system.Lgetxattr(srcPath, attr)
  82. if err != nil {
  83. return err
  84. }
  85. if data != nil {
  86. if err := system.Lsetxattr(dstPath, attr, data, 0); err != nil {
  87. return err
  88. }
  89. }
  90. return nil
  91. }
  92. type fileID struct {
  93. dev uint64
  94. ino uint64
  95. }
  96. type dirMtimeInfo struct {
  97. dstPath *string
  98. stat *syscall.Stat_t
  99. }
  100. // DirCopy copies or hardlinks the contents of one directory to another,
  101. // properly handling xattrs, and soft links
  102. //
  103. // Copying xattrs can be opted out of by passing false for copyXattrs.
  104. func DirCopy(srcDir, dstDir string, copyMode Mode, copyXattrs bool) error {
  105. copyWithFileRange := true
  106. copyWithFileClone := true
  107. // This is a map of source file inodes to dst file paths
  108. copiedFiles := make(map[fileID]string)
  109. dirsToSetMtimes := list.New()
  110. err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
  111. if err != nil {
  112. return err
  113. }
  114. // Rebase path
  115. relPath, err := filepath.Rel(srcDir, srcPath)
  116. if err != nil {
  117. return err
  118. }
  119. dstPath := filepath.Join(dstDir, relPath)
  120. if err != nil {
  121. return err
  122. }
  123. stat, ok := f.Sys().(*syscall.Stat_t)
  124. if !ok {
  125. return fmt.Errorf("Unable to get raw syscall.Stat_t data for %s", srcPath)
  126. }
  127. isHardlink := false
  128. switch f.Mode() & os.ModeType {
  129. case 0: // Regular file
  130. id := fileID{dev: stat.Dev, ino: stat.Ino}
  131. if copyMode == Hardlink {
  132. isHardlink = true
  133. if err2 := os.Link(srcPath, dstPath); err2 != nil {
  134. return err2
  135. }
  136. } else if hardLinkDstPath, ok := copiedFiles[id]; ok {
  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 os.ModeDir:
  147. if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
  148. return err
  149. }
  150. case os.ModeSymlink:
  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 os.ModeNamedPipe:
  159. fallthrough
  160. case os.ModeSocket:
  161. if rsystem.RunningInUserNS() {
  162. // cannot create a device if running in user namespace
  163. return nil
  164. }
  165. if err := unix.Mkfifo(dstPath, stat.Mode); err != nil {
  166. return err
  167. }
  168. case os.ModeDevice:
  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 for %s", 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 copyXattrs {
  184. if err := doCopyXattrs(srcPath, dstPath); err != nil {
  185. return err
  186. }
  187. }
  188. isSymlink := f.Mode()&os.ModeSymlink != 0
  189. // There is no LChmod, so ignore mode for symlink. Also, this
  190. // must happen after chown, as that can modify the file mode
  191. if !isSymlink {
  192. if err := os.Chmod(dstPath, f.Mode()); err != nil {
  193. return err
  194. }
  195. }
  196. // system.Chtimes doesn't support a NOFOLLOW flag atm
  197. // nolint: unconvert
  198. if f.IsDir() {
  199. dirsToSetMtimes.PushFront(&dirMtimeInfo{dstPath: &dstPath, stat: stat})
  200. } else if !isSymlink {
  201. aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
  202. mTime := time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec))
  203. if err := system.Chtimes(dstPath, aTime, mTime); err != nil {
  204. return err
  205. }
  206. } else {
  207. ts := []syscall.Timespec{stat.Atim, stat.Mtim}
  208. if err := system.LUtimesNano(dstPath, ts); err != nil {
  209. return err
  210. }
  211. }
  212. return nil
  213. })
  214. if err != nil {
  215. return err
  216. }
  217. for e := dirsToSetMtimes.Front(); e != nil; e = e.Next() {
  218. mtimeInfo := e.Value.(*dirMtimeInfo)
  219. ts := []syscall.Timespec{mtimeInfo.stat.Atim, mtimeInfo.stat.Mtim}
  220. if err := system.LUtimesNano(*mtimeInfo.dstPath, ts); err != nil {
  221. return err
  222. }
  223. }
  224. return nil
  225. }
  226. func doCopyXattrs(srcPath, dstPath string) error {
  227. if err := copyXattr(srcPath, dstPath, "security.capability"); err != nil {
  228. return err
  229. }
  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. }