gfile_copy.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gfile
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/errors/gcode"
  10. "github.com/gogf/gf/errors/gerror"
  11. "io"
  12. "io/ioutil"
  13. "os"
  14. "path/filepath"
  15. )
  16. // Copy file/directory from <src> to <dst>.
  17. //
  18. // If <src> is file, it calls CopyFile to implements copy feature,
  19. // or else it calls CopyDir.
  20. func Copy(src string, dst string) error {
  21. if src == "" {
  22. return gerror.NewCode(gcode.CodeInvalidParameter, "source path cannot be empty")
  23. }
  24. if dst == "" {
  25. return gerror.NewCode(gcode.CodeInvalidParameter, "destination path cannot be empty")
  26. }
  27. if IsFile(src) {
  28. return CopyFile(src, dst)
  29. }
  30. return CopyDir(src, dst)
  31. }
  32. // CopyFile copies the contents of the file named <src> to the file named
  33. // by <dst>. The file will be created if it does not exist. If the
  34. // destination file exists, all it's contents will be replaced by the contents
  35. // of the source file. The file mode will be copied from the source and
  36. // the copied data is synced/flushed to stable storage.
  37. // Thanks: https://gist.github.com/r0l1/92462b38df26839a3ca324697c8cba04
  38. func CopyFile(src, dst string) (err error) {
  39. if src == "" {
  40. return gerror.NewCode(gcode.CodeInvalidParameter, "source file cannot be empty")
  41. }
  42. if dst == "" {
  43. return gerror.NewCode(gcode.CodeInvalidParameter, "destination file cannot be empty")
  44. }
  45. // If src and dst are the same path, it does nothing.
  46. if src == dst {
  47. return nil
  48. }
  49. in, err := os.Open(src)
  50. if err != nil {
  51. return
  52. }
  53. defer func() {
  54. if e := in.Close(); e != nil {
  55. err = e
  56. }
  57. }()
  58. out, err := os.Create(dst)
  59. if err != nil {
  60. return
  61. }
  62. defer func() {
  63. if e := out.Close(); e != nil {
  64. err = e
  65. }
  66. }()
  67. _, err = io.Copy(out, in)
  68. if err != nil {
  69. return
  70. }
  71. err = out.Sync()
  72. if err != nil {
  73. return
  74. }
  75. err = os.Chmod(dst, DefaultPermCopy)
  76. if err != nil {
  77. return
  78. }
  79. return
  80. }
  81. // CopyDir recursively copies a directory tree, attempting to preserve permissions.
  82. //
  83. // Note that, the Source directory must exist and symlinks are ignored and skipped.
  84. func CopyDir(src string, dst string) (err error) {
  85. if src == "" {
  86. return gerror.NewCode(gcode.CodeInvalidParameter, "source directory cannot be empty")
  87. }
  88. if dst == "" {
  89. return gerror.NewCode(gcode.CodeInvalidParameter, "destination directory cannot be empty")
  90. }
  91. // If src and dst are the same path, it does nothing.
  92. if src == dst {
  93. return nil
  94. }
  95. src = filepath.Clean(src)
  96. dst = filepath.Clean(dst)
  97. si, err := os.Stat(src)
  98. if err != nil {
  99. return err
  100. }
  101. if !si.IsDir() {
  102. return fmt.Errorf("source is not a directory")
  103. }
  104. if !Exists(dst) {
  105. err = os.MkdirAll(dst, DefaultPermCopy)
  106. if err != nil {
  107. return
  108. }
  109. }
  110. entries, err := ioutil.ReadDir(src)
  111. if err != nil {
  112. return
  113. }
  114. for _, entry := range entries {
  115. srcPath := filepath.Join(src, entry.Name())
  116. dstPath := filepath.Join(dst, entry.Name())
  117. if entry.IsDir() {
  118. err = CopyDir(srcPath, dstPath)
  119. if err != nil {
  120. return
  121. }
  122. } else {
  123. // Skip symlinks.
  124. if entry.Mode()&os.ModeSymlink != 0 {
  125. continue
  126. }
  127. err = CopyFile(srcPath, dstPath)
  128. if err != nil {
  129. return
  130. }
  131. }
  132. }
  133. return
  134. }