copy.go 3.6 KB

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