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
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 nil
  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 nil
  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 nil
  73. }
  74. e = RemoveFileFromBucket(picksBucket, f)
  75. if e != nil {
  76. return nil
  77. }
  78. }
  79. }
  80. return nil
  81. })
  82. ProgressBar.CloseBar(bar)
  83. return e
  84. }
  85. func PickFiles(rootPath string) error {
  86. var (
  87. rootStat os.FileInfo
  88. e error
  89. )
  90. rootStat, e = os.Stat(rootPath)
  91. if e != nil {
  92. return e
  93. }
  94. if !rootStat.IsDir() {
  95. return pickFilesSingle(rootPath)
  96. }
  97. return pickFilesRecursive(rootPath)
  98. }
  99. func ResetAllPickedFiles() error {
  100. var (
  101. e error
  102. )
  103. e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
  104. return tx.DeleteBucket(Variables.FsHashPicksBucket)
  105. })
  106. return e
  107. }