copy_unix.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //go:build darwin || freebsd || openbsd || netbsd || dragonfly || solaris
  2. /*
  3. Copyright The containerd Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package fs
  15. import (
  16. "errors"
  17. "fmt"
  18. "os"
  19. "runtime"
  20. "syscall"
  21. "github.com/containerd/continuity/sysx"
  22. "golang.org/x/sys/unix"
  23. )
  24. func copyFileInfo(fi os.FileInfo, src, name string) error {
  25. st := fi.Sys().(*syscall.Stat_t)
  26. if err := os.Lchown(name, int(st.Uid), int(st.Gid)); err != nil {
  27. if os.IsPermission(err) {
  28. // Normally if uid/gid are the same this would be a no-op, but some
  29. // filesystems may still return EPERM... for instance NFS does this.
  30. // In such a case, this is not an error.
  31. if dstStat, err2 := os.Lstat(name); err2 == nil {
  32. st2 := dstStat.Sys().(*syscall.Stat_t)
  33. if st.Uid == st2.Uid && st.Gid == st2.Gid {
  34. err = nil
  35. }
  36. }
  37. }
  38. if err != nil {
  39. return fmt.Errorf("failed to chown %s: %w", name, err)
  40. }
  41. }
  42. if (fi.Mode() & os.ModeSymlink) != os.ModeSymlink {
  43. if err := os.Chmod(name, fi.Mode()); err != nil {
  44. return fmt.Errorf("failed to chmod %s: %w", name, err)
  45. }
  46. }
  47. if err := utimesNano(name, StatAtime(st), StatMtime(st)); err != nil {
  48. return fmt.Errorf("failed to utime %s: %w", name, err)
  49. }
  50. return nil
  51. }
  52. func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
  53. xattrKeys, err := sysx.LListxattr(src)
  54. if err != nil {
  55. if os.IsPermission(err) && runtime.GOOS == "darwin" {
  56. // On darwin, character devices do not permit listing xattrs
  57. return nil
  58. }
  59. if errors.Is(err, unix.ENOTSUP) {
  60. return nil
  61. }
  62. e := fmt.Errorf("failed to list xattrs on %s: %w", src, err)
  63. if errorHandler != nil {
  64. e = errorHandler(dst, src, "", e)
  65. }
  66. return e
  67. }
  68. for _, xattr := range xattrKeys {
  69. if _, exclude := excludes[xattr]; exclude {
  70. continue
  71. }
  72. data, err := sysx.LGetxattr(src, xattr)
  73. if err != nil {
  74. e := fmt.Errorf("failed to get xattr %q on %s: %w", xattr, src, err)
  75. if errorHandler != nil {
  76. if e = errorHandler(dst, src, xattr, e); e == nil {
  77. continue
  78. }
  79. }
  80. return e
  81. }
  82. if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
  83. e := fmt.Errorf("failed to set xattr %q on %s: %w", xattr, dst, err)
  84. if errorHandler != nil {
  85. if e = errorHandler(dst, src, xattr, e); e == nil {
  86. continue
  87. }
  88. }
  89. return e
  90. }
  91. }
  92. return nil
  93. }