copy_linux.go 3.7 KB

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