copy_linux.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "errors"
  16. "fmt"
  17. "io"
  18. "os"
  19. "syscall"
  20. "github.com/containerd/continuity/sysx"
  21. "golang.org/x/sys/unix"
  22. )
  23. func copyFileInfo(fi os.FileInfo, src, name string) error {
  24. st := fi.Sys().(*syscall.Stat_t)
  25. if err := os.Lchown(name, int(st.Uid), int(st.Gid)); err != nil {
  26. if os.IsPermission(err) {
  27. // Normally if uid/gid are the same this would be a no-op, but some
  28. // filesystems may still return EPERM... for instance NFS does this.
  29. // In such a case, this is not an error.
  30. if dstStat, err2 := os.Lstat(name); err2 == nil {
  31. st2 := dstStat.Sys().(*syscall.Stat_t)
  32. if st.Uid == st2.Uid && st.Gid == st2.Gid {
  33. err = nil
  34. }
  35. }
  36. }
  37. if err != nil {
  38. return fmt.Errorf("failed to chown %s: %w", name, err)
  39. }
  40. }
  41. if (fi.Mode() & os.ModeSymlink) != os.ModeSymlink {
  42. if err := os.Chmod(name, fi.Mode()); err != nil {
  43. return fmt.Errorf("failed to chmod %s: %w", name, err)
  44. }
  45. }
  46. timespec := []unix.Timespec{
  47. unix.NsecToTimespec(syscall.TimespecToNsec(StatAtime(st))),
  48. unix.NsecToTimespec(syscall.TimespecToNsec(StatMtime(st))),
  49. }
  50. if err := unix.UtimesNanoAt(unix.AT_FDCWD, name, timespec, unix.AT_SYMLINK_NOFOLLOW); err != nil {
  51. return fmt.Errorf("failed to utime %s: %w", name, err)
  52. }
  53. return nil
  54. }
  55. const maxSSizeT = int64(^uint(0) >> 1)
  56. func copyFileContent(dst, src *os.File) error {
  57. st, err := src.Stat()
  58. if err != nil {
  59. return fmt.Errorf("unable to stat source: %w", err)
  60. }
  61. size := st.Size()
  62. first := true
  63. srcFd := int(src.Fd())
  64. dstFd := int(dst.Fd())
  65. for size > 0 {
  66. // Ensure that we are never trying to copy more than SSIZE_MAX at a
  67. // time and at the same time avoids overflows when the file is larger
  68. // than 4GB on 32-bit systems.
  69. var copySize int
  70. if size > maxSSizeT {
  71. copySize = int(maxSSizeT)
  72. } else {
  73. copySize = int(size)
  74. }
  75. n, err := unix.CopyFileRange(srcFd, nil, dstFd, nil, copySize, 0)
  76. if err != nil {
  77. if (err != unix.ENOSYS && err != unix.EXDEV) || !first {
  78. return fmt.Errorf("copy file range failed: %w", err)
  79. }
  80. buf := bufferPool.Get().(*[]byte)
  81. _, err = io.CopyBuffer(dst, src, *buf)
  82. bufferPool.Put(buf)
  83. if err != nil {
  84. return fmt.Errorf("userspace copy failed: %w", err)
  85. }
  86. return nil
  87. }
  88. first = false
  89. size -= int64(n)
  90. }
  91. return nil
  92. }
  93. func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
  94. xattrKeys, err := sysx.LListxattr(src)
  95. if err != nil {
  96. e := fmt.Errorf("failed to list xattrs on %s: %w", src, err)
  97. if errorHandler != nil {
  98. e = errorHandler(dst, src, "", e)
  99. }
  100. return e
  101. }
  102. for _, xattr := range xattrKeys {
  103. if _, exclude := excludes[xattr]; exclude {
  104. continue
  105. }
  106. data, err := sysx.LGetxattr(src, xattr)
  107. if err != nil {
  108. e := fmt.Errorf("failed to get xattr %q on %s: %w", xattr, src, err)
  109. if errorHandler != nil {
  110. if e = errorHandler(dst, src, xattr, e); e == nil {
  111. continue
  112. }
  113. }
  114. return e
  115. }
  116. if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
  117. e := fmt.Errorf("failed to set xattr %q on %s: %w", xattr, dst, err)
  118. if errorHandler != nil {
  119. if e = errorHandler(dst, src, xattr, e); e == nil {
  120. continue
  121. }
  122. }
  123. return e
  124. }
  125. }
  126. return nil
  127. }
  128. func copyDevice(dst string, fi os.FileInfo) error {
  129. st, ok := fi.Sys().(*syscall.Stat_t)
  130. if !ok {
  131. return errors.New("unsupported stat type")
  132. }
  133. return unix.Mknod(dst, uint32(fi.Mode()), int(st.Rdev))
  134. }