copy.go 3.3 KB

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