copy.go 3.8 KB

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