copy_linux.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "fmt"
  16. "os"
  17. "syscall"
  18. "github.com/containerd/continuity/sysx"
  19. "golang.org/x/sys/unix"
  20. )
  21. func copyFileInfo(fi os.FileInfo, src, name string) error {
  22. st := fi.Sys().(*syscall.Stat_t)
  23. if err := os.Lchown(name, int(st.Uid), int(st.Gid)); err != nil {
  24. if os.IsPermission(err) {
  25. // Normally if uid/gid are the same this would be a no-op, but some
  26. // filesystems may still return EPERM... for instance NFS does this.
  27. // In such a case, this is not an error.
  28. if dstStat, err2 := os.Lstat(name); err2 == nil {
  29. st2 := dstStat.Sys().(*syscall.Stat_t)
  30. if st.Uid == st2.Uid && st.Gid == st2.Gid {
  31. err = nil
  32. }
  33. }
  34. }
  35. if err != nil {
  36. return fmt.Errorf("failed to chown %s: %w", name, err)
  37. }
  38. }
  39. if (fi.Mode() & os.ModeSymlink) != os.ModeSymlink {
  40. if err := os.Chmod(name, fi.Mode()); err != nil {
  41. return fmt.Errorf("failed to chmod %s: %w", name, err)
  42. }
  43. }
  44. timespec := []unix.Timespec{
  45. unix.NsecToTimespec(syscall.TimespecToNsec(StatAtime(st))),
  46. unix.NsecToTimespec(syscall.TimespecToNsec(StatMtime(st))),
  47. }
  48. if err := unix.UtimesNanoAt(unix.AT_FDCWD, name, timespec, unix.AT_SYMLINK_NOFOLLOW); err != nil {
  49. return fmt.Errorf("failed to utime %s: %w", name, err)
  50. }
  51. return nil
  52. }
  53. func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
  54. xattrKeys, err := sysx.LListxattr(src)
  55. if err != nil {
  56. e := fmt.Errorf("failed to list xattrs on %s: %w", src, err)
  57. if errorHandler != nil {
  58. e = errorHandler(dst, src, "", e)
  59. }
  60. return e
  61. }
  62. for _, xattr := range xattrKeys {
  63. if _, exclude := excludes[xattr]; exclude {
  64. continue
  65. }
  66. data, err := sysx.LGetxattr(src, xattr)
  67. if err != nil {
  68. e := fmt.Errorf("failed to get xattr %q on %s: %w", xattr, src, err)
  69. if errorHandler != nil {
  70. if e = errorHandler(dst, src, xattr, e); e == nil {
  71. continue
  72. }
  73. }
  74. return e
  75. }
  76. if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
  77. e := fmt.Errorf("failed to set xattr %q on %s: %w", xattr, dst, err)
  78. if errorHandler != nil {
  79. if e = errorHandler(dst, src, xattr, e); e == nil {
  80. continue
  81. }
  82. }
  83. return e
  84. }
  85. }
  86. return nil
  87. }