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.

129 lines
2.3 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
  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. return nil
  81. })
  82. return e
  83. }
  84. func PickFiles(rootPath string) error {
  85. var (
  86. rootStat os.FileInfo
  87. e error
  88. )
  89. rootStat, e = os.Stat(rootPath)
  90. if e != nil {
  91. return e
  92. }
  93. if !rootStat.IsDir() {
  94. return pickFilesSingle(rootPath)
  95. }
  96. return pickFilesRecursive(rootPath)
  97. }
  98. func ResetAllPickedFiles() error {
  99. var (
  100. e error
  101. )
  102. e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
  103. return tx.DeleteBucket(Variables.FsHashPicksBucket)
  104. })
  105. return e
  106. }