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.

134 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. e error
  16. )
  17. e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
  18. indexBucket = tx.Bucket(Variables.FsHashIndexBucket)
  19. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  20. e = AddFileToBucket(picksBucket, filePath)
  21. if e != nil {
  22. return e
  23. }
  24. return RemoveFileFromBucket(indexBucket, filePath)
  25. })
  26. return e
  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. e error
  37. )
  38. fsStatus, e = GetFilesystemDiff(rootPath)
  39. if e != nil {
  40. return e
  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. e = 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. e = AddFileToBucket(picksBucket, f)
  54. if e != nil {
  55. return e
  56. }
  57. }
  58. }
  59. if len(fsStatus.ModifiedFiles) > 0 {
  60. for _, f = range fsStatus.ModifiedFiles {
  61. bar.Increment()
  62. e = AddFileToBucket(picksBucket, f)
  63. if e != nil {
  64. return e
  65. }
  66. }
  67. }
  68. if len(fsStatus.MissingFiles) > 0 {
  69. for _, f = range fsStatus.MissingFiles {
  70. bar.Increment()
  71. e = RemoveFileFromBucket(indexBucket, f)
  72. if e != nil {
  73. return e
  74. }
  75. e = RemoveFileFromBucket(picksBucket, f)
  76. if e != nil {
  77. return e
  78. }
  79. }
  80. }
  81. return nil
  82. })
  83. return e
  84. }
  85. func PickFiles(rootPath string) error {
  86. var (
  87. realRootPath string
  88. rootStat os.FileInfo
  89. e error
  90. )
  91. realRootPath = filepath.Join(Variables.RootDir, rootPath)
  92. rootStat, e = os.Stat(realRootPath)
  93. if e != nil {
  94. return e
  95. }
  96. if !rootStat.IsDir() {
  97. return pickFilesSingle(realRootPath)
  98. }
  99. return pickFilesRecursive(realRootPath)
  100. }
  101. func ResetAllPickedFiles() error {
  102. var (
  103. e error
  104. )
  105. e = Database.FsDB.Batch(func(tx *bolt.Tx) error {
  106. return tx.DeleteBucket(Variables.FsHashPicksBucket)
  107. })
  108. return e
  109. }