copy_unix.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // +build darwin freebsd openbsd 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. "io"
  17. "os"
  18. "syscall"
  19. "github.com/containerd/continuity/sysx"
  20. "github.com/pkg/errors"
  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. if err := utimesNano(name, StatAtime(st), StatMtime(st)); err != nil {
  46. return errors.Wrapf(err, "failed to utime %s", name)
  47. }
  48. return nil
  49. }
  50. func copyFileContent(dst, src *os.File) error {
  51. buf := bufferPool.Get().(*[]byte)
  52. _, err := io.CopyBuffer(dst, src, *buf)
  53. bufferPool.Put(buf)
  54. return err
  55. }
  56. func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
  57. xattrKeys, err := sysx.LListxattr(src)
  58. if err != nil {
  59. e := errors.Wrapf(err, "failed to list xattrs on %s", src)
  60. if errorHandler != nil {
  61. e = errorHandler(dst, src, "", e)
  62. }
  63. return e
  64. }
  65. for _, xattr := range xattrKeys {
  66. if _, exclude := excludes[xattr]; exclude {
  67. continue
  68. }
  69. data, err := sysx.LGetxattr(src, xattr)
  70. if err != nil {
  71. e := errors.Wrapf(err, "failed to get xattr %q on %s", xattr, src)
  72. if errorHandler != nil {
  73. if e = errorHandler(dst, src, xattr, e); e == nil {
  74. continue
  75. }
  76. }
  77. return e
  78. }
  79. if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
  80. e := errors.Wrapf(err, "failed to set xattr %q on %s", xattr, dst)
  81. if errorHandler != nil {
  82. if e = errorHandler(dst, src, xattr, e); e == nil {
  83. continue
  84. }
  85. }
  86. return e
  87. }
  88. }
  89. return nil
  90. }