copy.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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"
  17. "os"
  18. "path/filepath"
  19. "github.com/sirupsen/logrus"
  20. )
  21. // XAttrErrorHandler transform a non-nil xattr error.
  22. // Return nil to ignore an error.
  23. // xattrKey can be empty for listxattr operation.
  24. type XAttrErrorHandler func(dst, src, xattrKey string, err error) error
  25. type copyDirOpts struct {
  26. xeh XAttrErrorHandler
  27. // xex contains a set of xattrs to exclude when copying
  28. xex map[string]struct{}
  29. }
  30. type CopyDirOpt func(*copyDirOpts) error
  31. // WithXAttrErrorHandler allows specifying XAttrErrorHandler
  32. // If nil XAttrErrorHandler is specified (default), CopyDir stops
  33. // on a non-nil xattr error.
  34. func WithXAttrErrorHandler(xeh XAttrErrorHandler) CopyDirOpt {
  35. return func(o *copyDirOpts) error {
  36. o.xeh = xeh
  37. return nil
  38. }
  39. }
  40. // WithAllowXAttrErrors allows ignoring xattr errors.
  41. func WithAllowXAttrErrors() CopyDirOpt {
  42. xeh := func(dst, src, xattrKey string, err error) error {
  43. return nil
  44. }
  45. return WithXAttrErrorHandler(xeh)
  46. }
  47. // WithXAttrExclude allows for exclusion of specified xattr during CopyDir operation.
  48. func WithXAttrExclude(keys ...string) CopyDirOpt {
  49. return func(o *copyDirOpts) error {
  50. if o.xex == nil {
  51. o.xex = make(map[string]struct{}, len(keys))
  52. }
  53. for _, key := range keys {
  54. o.xex[key] = struct{}{}
  55. }
  56. return nil
  57. }
  58. }
  59. // CopyDir copies the directory from src to dst.
  60. // Most efficient copy of files is attempted.
  61. func CopyDir(dst, src string, opts ...CopyDirOpt) error {
  62. var o copyDirOpts
  63. for _, opt := range opts {
  64. if err := opt(&o); err != nil {
  65. return err
  66. }
  67. }
  68. inodes := map[uint64]string{}
  69. return copyDirectory(dst, src, inodes, &o)
  70. }
  71. func copyDirectory(dst, src string, inodes map[uint64]string, o *copyDirOpts) error {
  72. stat, err := os.Stat(src)
  73. if err != nil {
  74. return fmt.Errorf("failed to stat %s: %w", src, err)
  75. }
  76. if !stat.IsDir() {
  77. return fmt.Errorf("source %s is not directory", src)
  78. }
  79. if st, err := os.Stat(dst); err != nil {
  80. if err := os.Mkdir(dst, stat.Mode()); err != nil {
  81. return fmt.Errorf("failed to mkdir %s: %w", dst, err)
  82. }
  83. } else if !st.IsDir() {
  84. return fmt.Errorf("cannot copy to non-directory: %s", dst)
  85. } else {
  86. if err := os.Chmod(dst, stat.Mode()); err != nil {
  87. return fmt.Errorf("failed to chmod on %s: %w", dst, err)
  88. }
  89. }
  90. entries, err := os.ReadDir(src)
  91. if err != nil {
  92. return fmt.Errorf("failed to read %s: %w", src, err)
  93. }
  94. if err := copyFileInfo(stat, src, dst); err != nil {
  95. return fmt.Errorf("failed to copy file info for %s: %w", dst, err)
  96. }
  97. if err := copyXAttrs(dst, src, o.xex, o.xeh); err != nil {
  98. return fmt.Errorf("failed to copy xattrs: %w", err)
  99. }
  100. for _, entry := range entries {
  101. source := filepath.Join(src, entry.Name())
  102. target := filepath.Join(dst, entry.Name())
  103. fileInfo, err := entry.Info()
  104. if err != nil {
  105. return fmt.Errorf("failed to get file info for %s: %w", entry.Name(), err)
  106. }
  107. switch {
  108. case entry.IsDir():
  109. if err := copyDirectory(target, source, inodes, o); err != nil {
  110. return err
  111. }
  112. continue
  113. case (fileInfo.Mode() & os.ModeType) == 0:
  114. link, err := getLinkSource(target, fileInfo, inodes)
  115. if err != nil {
  116. return fmt.Errorf("failed to get hardlink: %w", err)
  117. }
  118. if link != "" {
  119. if err := os.Link(link, target); err != nil {
  120. return fmt.Errorf("failed to create hard link: %w", err)
  121. }
  122. } else if err := CopyFile(target, source); err != nil {
  123. return fmt.Errorf("failed to copy files: %w", err)
  124. }
  125. case (fileInfo.Mode() & os.ModeSymlink) == os.ModeSymlink:
  126. link, err := os.Readlink(source)
  127. if err != nil {
  128. return fmt.Errorf("failed to read link: %s: %w", source, err)
  129. }
  130. if err := os.Symlink(link, target); err != nil {
  131. return fmt.Errorf("failed to create symlink: %s: %w", target, err)
  132. }
  133. case (fileInfo.Mode() & os.ModeDevice) == os.ModeDevice,
  134. (fileInfo.Mode() & os.ModeNamedPipe) == os.ModeNamedPipe,
  135. (fileInfo.Mode() & os.ModeSocket) == os.ModeSocket:
  136. if err := copyIrregular(target, fileInfo); err != nil {
  137. return fmt.Errorf("failed to create irregular file: %w", err)
  138. }
  139. default:
  140. logrus.Warnf("unsupported mode: %s: %s", source, fileInfo.Mode())
  141. continue
  142. }
  143. if err := copyFileInfo(fileInfo, source, target); err != nil {
  144. return fmt.Errorf("failed to copy file info: %w", err)
  145. }
  146. if err := copyXAttrs(target, source, o.xex, o.xeh); err != nil {
  147. return fmt.Errorf("failed to copy xattrs: %w", err)
  148. }
  149. }
  150. return nil
  151. }
  152. // CopyFile copies the source file to the target.
  153. // The most efficient means of copying is used for the platform.
  154. func CopyFile(target, source string) error {
  155. return copyFile(target, source)
  156. }
  157. func openAndCopyFile(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. _, err = io.Copy(tgt, src)
  169. return err
  170. }