fileutils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package fileutils
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "syscall"
  8. )
  9. // CopyFile copies the file at source to dest
  10. func CopyFile(source string, dest string) error {
  11. si, err := os.Lstat(source)
  12. if err != nil {
  13. return err
  14. }
  15. st, ok := si.Sys().(*syscall.Stat_t)
  16. if !ok {
  17. return fmt.Errorf("could not convert to syscall.Stat_t")
  18. }
  19. uid := int(st.Uid)
  20. gid := int(st.Gid)
  21. // Handle symlinks
  22. if si.Mode()&os.ModeSymlink != 0 {
  23. target, err := os.Readlink(source)
  24. if err != nil {
  25. return err
  26. }
  27. if err := os.Symlink(target, dest); err != nil {
  28. return err
  29. }
  30. }
  31. // Handle device files
  32. if st.Mode&syscall.S_IFMT == syscall.S_IFBLK || st.Mode&syscall.S_IFMT == syscall.S_IFCHR {
  33. devMajor := int64(major(uint64(st.Rdev)))
  34. devMinor := int64(minor(uint64(st.Rdev)))
  35. mode := uint32(si.Mode() & 07777)
  36. if st.Mode&syscall.S_IFMT == syscall.S_IFBLK {
  37. mode |= syscall.S_IFBLK
  38. }
  39. if st.Mode&syscall.S_IFMT == syscall.S_IFCHR {
  40. mode |= syscall.S_IFCHR
  41. }
  42. if err := syscall.Mknod(dest, mode, int(mkdev(devMajor, devMinor))); err != nil {
  43. return err
  44. }
  45. }
  46. // Handle regular files
  47. if si.Mode().IsRegular() {
  48. sf, err := os.Open(source)
  49. if err != nil {
  50. return err
  51. }
  52. defer sf.Close()
  53. df, err := os.Create(dest)
  54. if err != nil {
  55. return err
  56. }
  57. defer df.Close()
  58. _, err = io.Copy(df, sf)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. // Chown the file
  64. if err := os.Lchown(dest, uid, gid); err != nil {
  65. return err
  66. }
  67. // Chmod the file
  68. if !(si.Mode()&os.ModeSymlink == os.ModeSymlink) {
  69. if err := os.Chmod(dest, si.Mode()); err != nil {
  70. return err
  71. }
  72. }
  73. return nil
  74. }
  75. // CopyDirectory copies the files under the source directory
  76. // to dest directory. The dest directory is created if it
  77. // does not exist.
  78. func CopyDirectory(source string, dest string) error {
  79. fi, err := os.Stat(source)
  80. if err != nil {
  81. return err
  82. }
  83. // Get owner.
  84. st, ok := fi.Sys().(*syscall.Stat_t)
  85. if !ok {
  86. return fmt.Errorf("could not convert to syscall.Stat_t")
  87. }
  88. // We have to pick an owner here anyway.
  89. if err := MkdirAllNewAs(dest, fi.Mode(), int(st.Uid), int(st.Gid)); err != nil {
  90. return err
  91. }
  92. return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
  93. if err != nil {
  94. return err
  95. }
  96. // Get the relative path
  97. relPath, err := filepath.Rel(source, path)
  98. if err != nil {
  99. return nil
  100. }
  101. if info.IsDir() {
  102. // Skip the source directory.
  103. if path != source {
  104. // Get the owner.
  105. st, ok := info.Sys().(*syscall.Stat_t)
  106. if !ok {
  107. return fmt.Errorf("could not convert to syscall.Stat_t")
  108. }
  109. uid := int(st.Uid)
  110. gid := int(st.Gid)
  111. if err := os.Mkdir(filepath.Join(dest, relPath), info.Mode()); err != nil {
  112. return err
  113. }
  114. if err := os.Lchown(filepath.Join(dest, relPath), uid, gid); err != nil {
  115. return err
  116. }
  117. }
  118. return nil
  119. }
  120. // Copy the file.
  121. if err := CopyFile(path, filepath.Join(dest, relPath)); err != nil {
  122. return err
  123. }
  124. return nil
  125. })
  126. }
  127. func major(device uint64) uint64 {
  128. return (device >> 8) & 0xfff
  129. }
  130. func minor(device uint64) uint64 {
  131. return (device & 0xff) | ((device >> 12) & 0xfff00)
  132. }
  133. func mkdev(major int64, minor int64) uint32 {
  134. return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
  135. }