backup_zip.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package backup
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "io"
  8. "os"
  9. "path/filepath"
  10. "github.com/uozi-tech/cosy"
  11. )
  12. // createZipArchive creates a zip archive from a directory
  13. func createZipArchive(zipPath, srcDir string) error {
  14. // Create a new zip file
  15. zipFile, err := os.Create(zipPath)
  16. if err != nil {
  17. return cosy.WrapErrorWithParams(ErrCreateZipFile, err.Error())
  18. }
  19. defer zipFile.Close()
  20. // Create a new zip writer
  21. zipWriter := zip.NewWriter(zipFile)
  22. defer zipWriter.Close()
  23. // Walk through all files in the source directory
  24. err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
  25. if err != nil {
  26. return err
  27. }
  28. // Get relative path
  29. relPath, err := filepath.Rel(srcDir, path)
  30. if err != nil {
  31. return err
  32. }
  33. // Skip if it's the source directory itself
  34. if relPath == "." {
  35. return nil
  36. }
  37. // Check if it's a symlink
  38. if info.Mode()&os.ModeSymlink != 0 {
  39. // Get target of symlink
  40. linkTarget, err := os.Readlink(path)
  41. if err != nil {
  42. return cosy.WrapErrorWithParams(ErrReadSymlink, err.Error())
  43. }
  44. // Create symlink entry in zip
  45. header := &zip.FileHeader{
  46. Name: relPath,
  47. Method: zip.Deflate,
  48. }
  49. header.SetMode(info.Mode())
  50. writer, err := zipWriter.CreateHeader(header)
  51. if err != nil {
  52. return cosy.WrapErrorWithParams(ErrCreateZipEntry, err.Error())
  53. }
  54. // Write link target as content (common way to store symlinks in zip)
  55. _, err = writer.Write([]byte(linkTarget))
  56. if err != nil {
  57. return cosy.WrapErrorWithParams(ErrCopyContent, relPath)
  58. }
  59. return nil
  60. }
  61. // Create zip header
  62. header, err := zip.FileInfoHeader(info)
  63. if err != nil {
  64. return cosy.WrapErrorWithParams(ErrCreateZipHeader, err.Error())
  65. }
  66. // Set relative path as name
  67. header.Name = relPath
  68. if info.IsDir() {
  69. header.Name += "/"
  70. }
  71. // Set compression method
  72. header.Method = zip.Deflate
  73. // Create zip entry writer
  74. writer, err := zipWriter.CreateHeader(header)
  75. if err != nil {
  76. return cosy.WrapErrorWithParams(ErrCreateZipEntry, err.Error())
  77. }
  78. // Skip if it's a directory
  79. if info.IsDir() {
  80. return nil
  81. }
  82. // Open source file
  83. source, err := os.Open(path)
  84. if err != nil {
  85. return cosy.WrapErrorWithParams(ErrOpenSourceFile, err.Error())
  86. }
  87. defer source.Close()
  88. // Copy to zip
  89. _, err = io.Copy(writer, source)
  90. return err
  91. })
  92. return err
  93. }
  94. // calculateFileHash calculates the SHA-256 hash of a file
  95. func calculateFileHash(filePath string) (string, error) {
  96. // Open file
  97. file, err := os.Open(filePath)
  98. if err != nil {
  99. return "", cosy.WrapErrorWithParams(ErrReadFile, filePath)
  100. }
  101. defer file.Close()
  102. // Create hash
  103. hash := sha256.New()
  104. if _, err := io.Copy(hash, file); err != nil {
  105. return "", cosy.WrapErrorWithParams(ErrCalculateHash, err.Error())
  106. }
  107. // Return hex hash
  108. return hex.EncodeToString(hash.Sum(nil)), nil
  109. }
  110. // createZipArchiveToBuffer creates a zip archive of files in the specified directory
  111. // and writes the zip content to the provided buffer
  112. func createZipArchiveToBuffer(buffer *bytes.Buffer, sourceDir string) error {
  113. // Create a zip writer that writes to the buffer
  114. zipWriter := zip.NewWriter(buffer)
  115. defer zipWriter.Close()
  116. // Walk through all files in the source directory
  117. err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error {
  118. if err != nil {
  119. return err
  120. }
  121. // Skip the source directory itself
  122. if path == sourceDir {
  123. return nil
  124. }
  125. // Get the relative path to the source directory
  126. relPath, err := filepath.Rel(sourceDir, path)
  127. if err != nil {
  128. return err
  129. }
  130. // Check if it's a symlink
  131. if info.Mode()&os.ModeSymlink != 0 {
  132. // Get target of symlink
  133. linkTarget, err := os.Readlink(path)
  134. if err != nil {
  135. return cosy.WrapErrorWithParams(ErrReadSymlink, err.Error())
  136. }
  137. // Create symlink entry in zip
  138. header := &zip.FileHeader{
  139. Name: relPath,
  140. Method: zip.Deflate,
  141. }
  142. header.SetMode(info.Mode())
  143. writer, err := zipWriter.CreateHeader(header)
  144. if err != nil {
  145. return cosy.WrapErrorWithParams(ErrCreateZipEntry, err.Error())
  146. }
  147. // Write link target as content
  148. _, err = writer.Write([]byte(linkTarget))
  149. if err != nil {
  150. return cosy.WrapErrorWithParams(ErrCopyContent, relPath)
  151. }
  152. return nil
  153. }
  154. // Create a zip header from the file info
  155. header, err := zip.FileInfoHeader(info)
  156. if err != nil {
  157. return cosy.WrapErrorWithParams(ErrCreateZipHeader, err.Error())
  158. }
  159. // Set the name to be relative to the source directory
  160. header.Name = relPath
  161. // Set the compression method
  162. if !info.IsDir() {
  163. header.Method = zip.Deflate
  164. }
  165. // Create the entry in the zip file
  166. writer, err := zipWriter.CreateHeader(header)
  167. if err != nil {
  168. return cosy.WrapErrorWithParams(ErrCreateZipEntry, err.Error())
  169. }
  170. // If it's a directory, we're done
  171. if info.IsDir() {
  172. return nil
  173. }
  174. // Open the source file
  175. file, err := os.Open(path)
  176. if err != nil {
  177. return cosy.WrapErrorWithParams(ErrOpenSourceFile, err.Error())
  178. }
  179. defer file.Close()
  180. // Copy the file contents to the zip entry
  181. _, err = io.Copy(writer, file)
  182. if err != nil {
  183. return cosy.WrapErrorWithParams(ErrCopyContent, relPath)
  184. }
  185. return nil
  186. })
  187. if err != nil {
  188. return err
  189. }
  190. // Close the zip writer to ensure all data is written
  191. return zipWriter.Close()
  192. }