copy.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // +build linux
  2. package overlay
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "syscall"
  8. "time"
  9. "github.com/docker/docker/pkg/pools"
  10. "github.com/docker/docker/pkg/system"
  11. rsystem "github.com/opencontainers/runc/libcontainer/system"
  12. "golang.org/x/sys/unix"
  13. )
  14. type copyFlags int
  15. const (
  16. copyHardlink copyFlags = 1 << iota
  17. )
  18. func copyRegular(srcPath, dstPath string, mode os.FileMode) error {
  19. srcFile, err := os.Open(srcPath)
  20. if err != nil {
  21. return err
  22. }
  23. defer srcFile.Close()
  24. dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE, mode)
  25. if err != nil {
  26. return err
  27. }
  28. defer dstFile.Close()
  29. _, err = pools.Copy(dstFile, srcFile)
  30. return err
  31. }
  32. func copyXattr(srcPath, dstPath, attr string) error {
  33. data, err := system.Lgetxattr(srcPath, attr)
  34. if err != nil {
  35. return err
  36. }
  37. if data != nil {
  38. if err := system.Lsetxattr(dstPath, attr, data, 0); err != nil {
  39. return err
  40. }
  41. }
  42. return nil
  43. }
  44. func copyDir(srcDir, dstDir string, flags copyFlags) error {
  45. err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
  46. if err != nil {
  47. return err
  48. }
  49. // Rebase path
  50. relPath, err := filepath.Rel(srcDir, srcPath)
  51. if err != nil {
  52. return err
  53. }
  54. dstPath := filepath.Join(dstDir, relPath)
  55. if err != nil {
  56. return err
  57. }
  58. stat, ok := f.Sys().(*syscall.Stat_t)
  59. if !ok {
  60. return fmt.Errorf("Unable to get raw syscall.Stat_t data for %s", srcPath)
  61. }
  62. isHardlink := false
  63. switch f.Mode() & os.ModeType {
  64. case 0: // Regular file
  65. if flags&copyHardlink != 0 {
  66. isHardlink = true
  67. if err := os.Link(srcPath, dstPath); err != nil {
  68. return err
  69. }
  70. } else {
  71. if err := copyRegular(srcPath, dstPath, f.Mode()); err != nil {
  72. return err
  73. }
  74. }
  75. case os.ModeDir:
  76. if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
  77. return err
  78. }
  79. case os.ModeSymlink:
  80. link, err := os.Readlink(srcPath)
  81. if err != nil {
  82. return err
  83. }
  84. if err := os.Symlink(link, dstPath); err != nil {
  85. return err
  86. }
  87. case os.ModeNamedPipe:
  88. fallthrough
  89. case os.ModeSocket:
  90. if rsystem.RunningInUserNS() {
  91. // cannot create a device if running in user namespace
  92. return nil
  93. }
  94. if err := unix.Mkfifo(dstPath, stat.Mode); err != nil {
  95. return err
  96. }
  97. case os.ModeDevice:
  98. if err := unix.Mknod(dstPath, stat.Mode, int(stat.Rdev)); err != nil {
  99. return err
  100. }
  101. default:
  102. return fmt.Errorf("unknown file type for %s", srcPath)
  103. }
  104. // Everything below is copying metadata from src to dst. All this metadata
  105. // already shares an inode for hardlinks.
  106. if isHardlink {
  107. return nil
  108. }
  109. if err := os.Lchown(dstPath, int(stat.Uid), int(stat.Gid)); err != nil {
  110. return err
  111. }
  112. if err := copyXattr(srcPath, dstPath, "security.capability"); err != nil {
  113. return err
  114. }
  115. // We need to copy this attribute if it appears in an overlay upper layer, as
  116. // this function is used to copy those. It is set by overlay if a directory
  117. // is removed and then re-created and should not inherit anything from the
  118. // same dir in the lower dir.
  119. if err := copyXattr(srcPath, dstPath, "trusted.overlay.opaque"); err != nil {
  120. return err
  121. }
  122. isSymlink := f.Mode()&os.ModeSymlink != 0
  123. // There is no LChmod, so ignore mode for symlink. Also, this
  124. // must happen after chown, as that can modify the file mode
  125. if !isSymlink {
  126. if err := os.Chmod(dstPath, f.Mode()); err != nil {
  127. return err
  128. }
  129. }
  130. // system.Chtimes doesn't support a NOFOLLOW flag atm
  131. // nolint: unconvert
  132. if !isSymlink {
  133. aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
  134. mTime := time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec))
  135. if err := system.Chtimes(dstPath, aTime, mTime); err != nil {
  136. return err
  137. }
  138. } else {
  139. ts := []syscall.Timespec{stat.Atim, stat.Mtim}
  140. if err := system.LUtimesNano(dstPath, ts); err != nil {
  141. return err
  142. }
  143. }
  144. return nil
  145. })
  146. return err
  147. }