copy_unix.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //go:build darwin || freebsd || openbsd || netbsd || solaris
  2. // +build darwin freebsd openbsd netbsd solaris
  3. /*
  4. Copyright The containerd Authors.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package fs
  16. import (
  17. "fmt"
  18. "io"
  19. "os"
  20. "runtime"
  21. "syscall"
  22. "github.com/containerd/continuity/sysx"
  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 copyFileContent(dst, src *os.File) error {
  53. buf := bufferPool.Get().(*[]byte)
  54. _, err := io.CopyBuffer(dst, src, *buf)
  55. bufferPool.Put(buf)
  56. return err
  57. }
  58. func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
  59. xattrKeys, err := sysx.LListxattr(src)
  60. if err != nil {
  61. if os.IsPermission(err) && runtime.GOOS == "darwin" {
  62. // On darwin, character devices do not permit listing xattrs
  63. return nil
  64. }
  65. e := fmt.Errorf("failed to list xattrs on %s: %w", src, err)
  66. if errorHandler != nil {
  67. e = errorHandler(dst, src, "", e)
  68. }
  69. return e
  70. }
  71. for _, xattr := range xattrKeys {
  72. if _, exclude := excludes[xattr]; exclude {
  73. continue
  74. }
  75. data, err := sysx.LGetxattr(src, xattr)
  76. if err != nil {
  77. e := fmt.Errorf("failed to get xattr %q on %s: %w", xattr, src, err)
  78. if errorHandler != nil {
  79. if e = errorHandler(dst, src, xattr, e); e == nil {
  80. continue
  81. }
  82. }
  83. return e
  84. }
  85. if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
  86. e := fmt.Errorf("failed to set xattr %q on %s: %w", xattr, dst, err)
  87. if errorHandler != nil {
  88. if e = errorHandler(dst, src, xattr, e); e == nil {
  89. continue
  90. }
  91. }
  92. return e
  93. }
  94. }
  95. return nil
  96. }