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.

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