copy_unix.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // +build solaris darwin freebsd
  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. "golang.org/x/sys/unix"
  22. )
  23. func copyFileInfo(fi os.FileInfo, name string) error {
  24. st := fi.Sys().(*syscall.Stat_t)
  25. if err := os.Lchown(name, int(st.Uid), int(st.Gid)); err != nil {
  26. if os.IsPermission(err) {
  27. // Normally if uid/gid are the same this would be a no-op, but some
  28. // filesystems may still return EPERM... for instance NFS does this.
  29. // In such a case, this is not an error.
  30. if dstStat, err2 := os.Lstat(name); err2 == nil {
  31. st2 := dstStat.Sys().(*syscall.Stat_t)
  32. if st.Uid == st2.Uid && st.Gid == st2.Gid {
  33. err = nil
  34. }
  35. }
  36. }
  37. if err != nil {
  38. return errors.Wrapf(err, "failed to chown %s", name)
  39. }
  40. }
  41. if (fi.Mode() & os.ModeSymlink) != os.ModeSymlink {
  42. if err := os.Chmod(name, fi.Mode()); err != nil {
  43. return errors.Wrapf(err, "failed to chmod %s", name)
  44. }
  45. }
  46. timespec := []syscall.Timespec{StatAtime(st), StatMtime(st)}
  47. if err := syscall.UtimesNano(name, timespec); err != nil {
  48. return errors.Wrapf(err, "failed to utime %s", name)
  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) error {
  59. xattrKeys, err := sysx.LListxattr(src)
  60. if err != nil {
  61. return errors.Wrapf(err, "failed to list xattrs on %s", src)
  62. }
  63. for _, xattr := range xattrKeys {
  64. data, err := sysx.LGetxattr(src, xattr)
  65. if err != nil {
  66. return errors.Wrapf(err, "failed to get xattr %q on %s", xattr, src)
  67. }
  68. if err := sysx.LSetxattr(dst, xattr, data, 0); err != nil {
  69. return errors.Wrapf(err, "failed to set xattr %q on %s", xattr, dst)
  70. }
  71. }
  72. return nil
  73. }
  74. func copyDevice(dst string, fi os.FileInfo) error {
  75. st, ok := fi.Sys().(*syscall.Stat_t)
  76. if !ok {
  77. return errors.New("unsupported stat type")
  78. }
  79. return unix.Mknod(dst, uint32(fi.Mode()), int(st.Rdev))
  80. }