copy_linux.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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{unix.Timespec(StatAtime(st)), unix.Timespec(StatMtime(st))}
  46. if err := unix.UtimesNanoAt(unix.AT_FDCWD, name, timespec, unix.AT_SYMLINK_NOFOLLOW); err != nil {
  47. return errors.Wrapf(err, "failed to utime %s", name)
  48. }
  49. return nil
  50. }
  51. const maxSSizeT = int64(^uint(0) >> 1)
  52. func copyFileContent(dst, src *os.File) error {
  53. st, err := src.Stat()
  54. if err != nil {
  55. return errors.Wrap(err, "unable to stat source")
  56. }
  57. size := st.Size()
  58. first := true
  59. srcFd := int(src.Fd())
  60. dstFd := int(dst.Fd())
  61. for size > 0 {
  62. // Ensure that we are never trying to copy more than SSIZE_MAX at a
  63. // time and at the same time avoids overflows when the file is larger
  64. // than 4GB on 32-bit systems.
  65. var copySize int
  66. if size > maxSSizeT {
  67. copySize = int(maxSSizeT)
  68. } else {
  69. copySize = int(size)
  70. }
  71. n, err := unix.CopyFileRange(srcFd, nil, dstFd, nil, copySize, 0)
  72. if err != nil {
  73. if (err != unix.ENOSYS && err != unix.EXDEV) || !first {
  74. return errors.Wrap(err, "copy file range failed")
  75. }
  76. buf := bufferPool.Get().(*[]byte)
  77. _, err = io.CopyBuffer(dst, src, *buf)
  78. bufferPool.Put(buf)
  79. return errors.Wrap(err, "userspace copy failed")
  80. }
  81. first = false
  82. size -= int64(n)
  83. }
  84. return nil
  85. }
  86. func copyXAttrs(dst, src string, xeh XAttrErrorHandler) error {
  87. xattrKeys, err := sysx.LListxattr(src)
  88. if err != nil {
  89. e := errors.Wrapf(err, "failed to list xattrs on %s", src)
  90. if xeh != nil {
  91. e = xeh(dst, src, "", e)
  92. }
  93. return e
  94. }
  95. for _, xattr := range xattrKeys {
  96. data, err := sysx.LGetxattr(src, xattr)
  97. if err != nil {
  98. e := errors.Wrapf(err, "failed to get xattr %q on %s", xattr, src)
  99. if xeh != nil {
  100. if e = xeh(dst, src, xattr, e); e == nil {
  101. continue
  102. }
  103. }
  104. return e
  105. }
  106. if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
  107. e := errors.Wrapf(err, "failed to set xattr %q on %s", xattr, dst)
  108. if xeh != nil {
  109. if e = xeh(dst, src, xattr, e); e == nil {
  110. continue
  111. }
  112. }
  113. return e
  114. }
  115. }
  116. return nil
  117. }
  118. func copyDevice(dst string, fi os.FileInfo) error {
  119. st, ok := fi.Sys().(*syscall.Stat_t)
  120. if !ok {
  121. return errors.New("unsupported stat type")
  122. }
  123. return unix.Mknod(dst, uint32(fi.Mode()), int(st.Rdev))
  124. }