copy_linux.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. func copyFileContent(dst, src *os.File) error {
  52. st, err := src.Stat()
  53. if err != nil {
  54. return errors.Wrap(err, "unable to stat source")
  55. }
  56. size := st.Size()
  57. first := true
  58. srcFd := int(src.Fd())
  59. dstFd := int(dst.Fd())
  60. for size > 0 {
  61. n, err := unix.CopyFileRange(srcFd, nil, dstFd, nil, int(size), 0)
  62. if err != nil {
  63. if (err != unix.ENOSYS && err != unix.EXDEV) || !first {
  64. return errors.Wrap(err, "copy file range failed")
  65. }
  66. buf := bufferPool.Get().(*[]byte)
  67. _, err = io.CopyBuffer(dst, src, *buf)
  68. bufferPool.Put(buf)
  69. return errors.Wrap(err, "userspace copy failed")
  70. }
  71. first = false
  72. size -= int64(n)
  73. }
  74. return nil
  75. }
  76. func copyXAttrs(dst, src string) error {
  77. xattrKeys, err := sysx.LListxattr(src)
  78. if err != nil {
  79. return errors.Wrapf(err, "failed to list xattrs on %s", src)
  80. }
  81. for _, xattr := range xattrKeys {
  82. data, err := sysx.LGetxattr(src, xattr)
  83. if err != nil {
  84. return errors.Wrapf(err, "failed to get xattr %q on %s", xattr, src)
  85. }
  86. if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
  87. return errors.Wrapf(err, "failed to set xattr %q on %s", xattr, dst)
  88. }
  89. }
  90. return nil
  91. }
  92. func copyDevice(dst string, fi os.FileInfo) error {
  93. st, ok := fi.Sys().(*syscall.Stat_t)
  94. if !ok {
  95. return errors.New("unsupported stat type")
  96. }
  97. return unix.Mknod(dst, uint32(fi.Mode()), int(st.Rdev))
  98. }