PackageManager just because
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
2.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package Filesystem
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "github.com/vbauerster/mpb"
  7. bolt "go.etcd.io/bbolt"
  8. "PackageManager/Client/Database"
  9. "PackageManager/Client/ProgressBar"
  10. "PackageManager/Variables"
  11. )
  12. func pickFilesSingle(fileKey, filePath string) error {
  13. var (
  14. indexBucket *bolt.Bucket
  15. picksBucket *bolt.Bucket
  16. e error
  17. )
  18. e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
  19. indexBucket = tx.Bucket(Variables.FsHashIndexBucket)
  20. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  21. e = AddFileToBucket(picksBucket, fileKey, filePath)
  22. if e != nil {
  23. return e
  24. }
  25. return RemoveFileFromBucket(indexBucket, fileKey)
  26. })
  27. return e
  28. }
  29. func pickFilesRecursive(rootPath string) error {
  30. var (
  31. fsStatus FilesystemStatus
  32. indexBucket *bolt.Bucket
  33. picksBucket *bolt.Bucket
  34. bar *mpb.Bar
  35. totalLen int
  36. trimF string
  37. f string
  38. e error
  39. )
  40. fsStatus, e = GetFilesystemDiff(rootPath)
  41. if e != nil {
  42. return e
  43. }
  44. totalLen = len(fsStatus.NewFiles) + len(fsStatus.ModifiedFiles) + len(fsStatus.MissingFiles)
  45. if totalLen == 0 {
  46. return nil
  47. }
  48. bar = ProgressBar.InitBar("Adding...", totalLen)
  49. e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
  50. indexBucket = tx.Bucket(Variables.FsHashIndexBucket)
  51. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  52. if len(fsStatus.NewFiles) > 0 {
  53. for _, f = range fsStatus.NewFiles {
  54. bar.Increment()
  55. trimF = strings.TrimPrefix(f, Variables.RootDir)
  56. e = AddFileToBucket(picksBucket, trimF, f)
  57. if e != nil {
  58. return e
  59. }
  60. }
  61. }
  62. if len(fsStatus.ModifiedFiles) > 0 {
  63. for _, f = range fsStatus.ModifiedFiles {
  64. bar.Increment()
  65. trimF = strings.TrimPrefix(f, Variables.RootDir)
  66. e = AddFileToBucket(picksBucket, trimF, f)
  67. if e != nil {
  68. return e
  69. }
  70. }
  71. }
  72. if len(fsStatus.MissingFiles) > 0 {
  73. for _, f = range fsStatus.MissingFiles {
  74. bar.Increment()
  75. e = RemoveFileFromBucket(indexBucket, f)
  76. if e != nil {
  77. return e
  78. }
  79. e = RemoveFileFromBucket(picksBucket, f)
  80. if e != nil {
  81. return e
  82. }
  83. }
  84. }
  85. return nil
  86. })
  87. return e
  88. }
  89. func PickFiles(rootPath string) error {
  90. var (
  91. realRootPath string
  92. rootStat os.FileInfo
  93. e error
  94. )
  95. realRootPath = filepath.Join(Variables.RootDir, rootPath)
  96. rootStat, e = os.Stat(realRootPath)
  97. if e != nil {
  98. return e
  99. }
  100. if !rootStat.IsDir() {
  101. return pickFilesSingle(rootPath, realRootPath)
  102. }
  103. return pickFilesRecursive(realRootPath)
  104. }
  105. func ResetAllPickedFiles() error {
  106. var (
  107. e error
  108. )
  109. e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
  110. return tx.DeleteBucket(Variables.FsHashPicksBucket)
  111. })
  112. return e
  113. }