copy_linux.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "os"
  18. "syscall"
  19. "github.com/containerd/continuity/sysx"
  20. "golang.org/x/sys/unix"
  21. )
  22. func copyFileInfo(fi os.FileInfo, src, 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 fmt.Errorf("failed to chown %s: %w", name, err)
  38. }
  39. }
  40. if (fi.Mode() & os.ModeSymlink) != os.ModeSymlink {
  41. if err := os.Chmod(name, fi.Mode()); err != nil {
  42. return fmt.Errorf("failed to chmod %s: %w", name, err)
  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 fmt.Errorf("failed to utime %s: %w", name, err)
  51. }
  52. return nil
  53. }
  54. func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
  55. xattrKeys, err := sysx.LListxattr(src)
  56. if err != nil {
  57. if errors.Is(err, unix.ENOTSUP) {
  58. return nil
  59. }
  60. e := fmt.Errorf("failed to list xattrs on %s: %w", src, err)
  61. if errorHandler != nil {
  62. e = errorHandler(dst, src, "", e)
  63. }
  64. return e
  65. }
  66. for _, xattr := range xattrKeys {
  67. if _, exclude := excludes[xattr]; exclude {
  68. continue
  69. }
  70. data, err := sysx.LGetxattr(src, xattr)
  71. if err != nil {
  72. e := fmt.Errorf("failed to get xattr %q on %s: %w", xattr, src, err)
  73. if errorHandler != nil {
  74. if e = errorHandler(dst, src, xattr, e); e == nil {
  75. continue
  76. }
  77. }
  78. return e
  79. }
  80. if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
  81. e := fmt.Errorf("failed to set xattr %q on %s: %w", xattr, dst, err)
  82. if errorHandler != nil {
  83. if e = errorHandler(dst, src, xattr, e); e == nil {
  84. continue
  85. }
  86. }
  87. return e
  88. }
  89. }
  90. return nil
  91. }