fileutils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. modeType := si.Mode() & os.ModeType
  22. // Handle symlinks
  23. if modeType == os.ModeSymlink {
  24. target, err := os.Readlink(source)
  25. if err != nil {
  26. return err
  27. }
  28. if err := os.Symlink(target, dest); err != nil {
  29. return err
  30. }
  31. }
  32. // Handle device files
  33. if modeType == os.ModeDevice {
  34. devMajor := int64(major(uint64(st.Rdev)))
  35. devMinor := int64(minor(uint64(st.Rdev)))
  36. mode := uint32(si.Mode() & os.ModePerm)
  37. if si.Mode()&os.ModeCharDevice != 0 {
  38. mode |= syscall.S_IFCHR
  39. } else {
  40. mode |= syscall.S_IFBLK
  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. err = copyInternal(source, dest)
  49. if err != nil {
  50. return err
  51. }
  52. }
  53. // Chown the file
  54. if err := os.Lchown(dest, uid, gid); err != nil {
  55. return err
  56. }
  57. // Chmod the file
  58. if !(modeType == os.ModeSymlink) {
  59. if err := os.Chmod(dest, si.Mode()); err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. func copyInternal(source, dest string) (retErr error) {
  66. sf, err := os.Open(source)
  67. if err != nil {
  68. return err
  69. }
  70. defer sf.Close()
  71. df, err := os.Create(dest)
  72. if err != nil {
  73. return err
  74. }
  75. defer func() {
  76. err := df.Close()
  77. if retErr == nil {
  78. retErr = err
  79. }
  80. }()
  81. _, err = io.Copy(df, sf)
  82. return err
  83. }
  84. // CopyDirectory copies the files under the source directory
  85. // to dest directory. The dest directory is created if it
  86. // does not exist.
  87. func CopyDirectory(source string, dest string) error {
  88. fi, err := os.Stat(source)
  89. if err != nil {
  90. return err
  91. }
  92. // Get owner.
  93. st, ok := fi.Sys().(*syscall.Stat_t)
  94. if !ok {
  95. return fmt.Errorf("could not convert to syscall.Stat_t")
  96. }
  97. // We have to pick an owner here anyway.
  98. if err := MkdirAllNewAs(dest, fi.Mode(), int(st.Uid), int(st.Gid)); err != nil {
  99. return err
  100. }
  101. return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
  102. if err != nil {
  103. return err
  104. }
  105. // Get the relative path
  106. relPath, err := filepath.Rel(source, path)
  107. if err != nil {
  108. return nil
  109. }
  110. if info.IsDir() {
  111. // Skip the source directory.
  112. if path != source {
  113. // Get the owner.
  114. st, ok := info.Sys().(*syscall.Stat_t)
  115. if !ok {
  116. return fmt.Errorf("could not convert to syscall.Stat_t")
  117. }
  118. uid := int(st.Uid)
  119. gid := int(st.Gid)
  120. if err := os.Mkdir(filepath.Join(dest, relPath), info.Mode()); err != nil {
  121. return err
  122. }
  123. if err := os.Lchown(filepath.Join(dest, relPath), uid, gid); err != nil {
  124. return err
  125. }
  126. }
  127. return nil
  128. }
  129. return CopyFile(path, filepath.Join(dest, relPath))
  130. })
  131. }
  132. // Gives a number indicating the device driver to be used to access the passed device
  133. func major(device uint64) uint64 {
  134. return (device >> 8) & 0xfff
  135. }
  136. // Gives a number that serves as a flag to the device driver for the passed device
  137. func minor(device uint64) uint64 {
  138. return (device & 0xff) | ((device >> 12) & 0xfff00)
  139. }
  140. func mkdev(major int64, minor int64) uint32 {
  141. return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
  142. }