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.

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