copy.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package fs
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "sync"
  20. "github.com/sirupsen/logrus"
  21. )
  22. var bufferPool = &sync.Pool{
  23. New: func() interface{} {
  24. buffer := make([]byte, 32*1024)
  25. return &buffer
  26. },
  27. }
  28. // XAttrErrorHandler transform a non-nil xattr error.
  29. // Return nil to ignore an error.
  30. // xattrKey can be empty for listxattr operation.
  31. type XAttrErrorHandler func(dst, src, xattrKey string, err error) error
  32. type copyDirOpts struct {
  33. xeh XAttrErrorHandler
  34. // xex contains a set of xattrs to exclude when copying
  35. xex map[string]struct{}
  36. }
  37. type CopyDirOpt func(*copyDirOpts) error
  38. // WithXAttrErrorHandler allows specifying XAttrErrorHandler
  39. // If nil XAttrErrorHandler is specified (default), CopyDir stops
  40. // on a non-nil xattr error.
  41. func WithXAttrErrorHandler(xeh XAttrErrorHandler) CopyDirOpt {
  42. return func(o *copyDirOpts) error {
  43. o.xeh = xeh
  44. return nil
  45. }
  46. }
  47. // WithAllowXAttrErrors allows ignoring xattr errors.
  48. func WithAllowXAttrErrors() CopyDirOpt {
  49. xeh := func(dst, src, xattrKey string, err error) error {
  50. return nil
  51. }
  52. return WithXAttrErrorHandler(xeh)
  53. }
  54. // WithXAttrExclude allows for exclusion of specified xattr during CopyDir operation.
  55. func WithXAttrExclude(keys ...string) CopyDirOpt {
  56. return func(o *copyDirOpts) error {
  57. if o.xex == nil {
  58. o.xex = make(map[string]struct{}, len(keys))
  59. }
  60. for _, key := range keys {
  61. o.xex[key] = struct{}{}
  62. }
  63. return nil
  64. }
  65. }
  66. // CopyDir copies the directory from src to dst.
  67. // Most efficient copy of files is attempted.
  68. func CopyDir(dst, src string, opts ...CopyDirOpt) error {
  69. var o copyDirOpts
  70. for _, opt := range opts {
  71. if err := opt(&o); err != nil {
  72. return err
  73. }
  74. }
  75. inodes := map[uint64]string{}
  76. return copyDirectory(dst, src, inodes, &o)
  77. }
  78. func copyDirectory(dst, src string, inodes map[uint64]string, o *copyDirOpts) error {
  79. stat, err := os.Stat(src)
  80. if err != nil {
  81. return fmt.Errorf("failed to stat %s: %w", src, err)
  82. }
  83. if !stat.IsDir() {
  84. return fmt.Errorf("source %s is not directory", src)
  85. }
  86. if st, err := os.Stat(dst); err != nil {
  87. if err := os.Mkdir(dst, stat.Mode()); err != nil {
  88. return fmt.Errorf("failed to mkdir %s: %w", dst, err)
  89. }
  90. } else if !st.IsDir() {
  91. return fmt.Errorf("cannot copy to non-directory: %s", dst)
  92. } else {
  93. if err := os.Chmod(dst, stat.Mode()); err != nil {
  94. return fmt.Errorf("failed to chmod on %s: %w", dst, err)
  95. }
  96. }
  97. fis, err := ioutil.ReadDir(src)
  98. if err != nil {
  99. return fmt.Errorf("failed to read %s: %w", src, err)
  100. }
  101. if err := copyFileInfo(stat, src, dst); err != nil {
  102. return fmt.Errorf("failed to copy file info for %s: %w", dst, err)
  103. }
  104. if err := copyXAttrs(dst, src, o.xex, o.xeh); err != nil {
  105. return fmt.Errorf("failed to copy xattrs: %w", err)
  106. }
  107. for _, fi := range fis {
  108. source := filepath.Join(src, fi.Name())
  109. target := filepath.Join(dst, fi.Name())
  110. switch {
  111. case fi.IsDir():
  112. if err := copyDirectory(target, source, inodes, o); err != nil {
  113. return err
  114. }
  115. continue
  116. case (fi.Mode() & os.ModeType) == 0:
  117. link, err := getLinkSource(target, fi, inodes)
  118. if err != nil {
  119. return fmt.Errorf("failed to get hardlink: %w", err)
  120. }
  121. if link != "" {
  122. if err := os.Link(link, target); err != nil {
  123. return fmt.Errorf("failed to create hard link: %w", err)
  124. }
  125. } else if err := CopyFile(target, source); err != nil {
  126. return fmt.Errorf("failed to copy files: %w", err)
  127. }
  128. case (fi.Mode() & os.ModeSymlink) == os.ModeSymlink:
  129. link, err := os.Readlink(source)
  130. if err != nil {
  131. return fmt.Errorf("failed to read link: %s: %w", source, err)
  132. }
  133. if err := os.Symlink(link, target); err != nil {
  134. return fmt.Errorf("failed to create symlink: %s: %w", target, err)
  135. }
  136. case (fi.Mode() & os.ModeDevice) == os.ModeDevice,
  137. (fi.Mode() & os.ModeNamedPipe) == os.ModeNamedPipe,
  138. (fi.Mode() & os.ModeSocket) == os.ModeSocket:
  139. if err := copyIrregular(target, fi); err != nil {
  140. return fmt.Errorf("failed to create irregular file: %w", err)
  141. }
  142. default:
  143. logrus.Warnf("unsupported mode: %s: %s", source, fi.Mode())
  144. continue
  145. }
  146. if err := copyFileInfo(fi, source, target); err != nil {
  147. return fmt.Errorf("failed to copy file info: %w", err)
  148. }
  149. if err := copyXAttrs(target, source, o.xex, o.xeh); err != nil {
  150. return fmt.Errorf("failed to copy xattrs: %w", err)
  151. }
  152. }
  153. return nil
  154. }
  155. // CopyFile copies the source file to the target.
  156. // The most efficient means of copying is used for the platform.
  157. func CopyFile(target, source string) error {
  158. src, err := os.Open(source)
  159. if err != nil {
  160. return fmt.Errorf("failed to open source %s: %w", source, err)
  161. }
  162. defer src.Close()
  163. tgt, err := os.Create(target)
  164. if err != nil {
  165. return fmt.Errorf("failed to open target %s: %w", target, err)
  166. }
  167. defer tgt.Close()
  168. return copyFileContent(tgt, src)
  169. }