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.

234 lines
4.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
  1. package Filesystem
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "sync"
  9. "github.com/vbauerster/mpb"
  10. bolt "go.etcd.io/bbolt"
  11. "golang.org/x/sync/semaphore"
  12. "PackageManager/Client/Database"
  13. "PackageManager/Client/ProgressBar"
  14. "PackageManager/Color"
  15. "PackageManager/Variables"
  16. )
  17. var (
  18. fsStatusWG sync.WaitGroup
  19. FsWalkWG sync.WaitGroup
  20. )
  21. type FilesystemStatus struct {
  22. NewFiles []string
  23. PickedFiles []string
  24. ModifiedFiles []string
  25. MissingFiles []string
  26. }
  27. func ShowFilesystemDiff(root string) error {
  28. var (
  29. fsStatus FilesystemStatus
  30. picksBucket *bolt.Bucket
  31. pickedFiles []string
  32. e error
  33. )
  34. fsStatus, e = GetFilesystemDiff(root)
  35. if e != nil {
  36. return e
  37. }
  38. e = Database.FsDB.View(func(tx *bolt.Tx) error {
  39. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  40. return picksBucket.ForEach(func(key, _ []byte) error {
  41. pickedFiles = append(pickedFiles, string(key))
  42. return nil
  43. })
  44. })
  45. fmt.Println("New files:")
  46. PrintFilesOrLength(fsStatus.NewFiles, Color.Green)
  47. fmt.Println("Added files:")
  48. PrintFilesOrLength(pickedFiles, Color.Green)
  49. fmt.Println("Modified files:")
  50. PrintFilesOrLength(fsStatus.ModifiedFiles, Color.Warning)
  51. fmt.Println("Deleted files:")
  52. PrintFilesOrLength(fsStatus.MissingFiles, Color.Fatal)
  53. return nil
  54. }
  55. func GetFilesystemLength(root string) (int, error) {
  56. var (
  57. rootStat os.FileInfo
  58. fsCount int = 0
  59. e error
  60. )
  61. rootStat, e = os.Stat(root)
  62. if e != nil {
  63. return fsCount, e
  64. }
  65. if rootStat.IsDir() && root[len(root)-1:] != "/" {
  66. root = root + "/"
  67. }
  68. filepath.Walk(root, func(p string, i os.FileInfo, _ error) error {
  69. // Ignore path in Variables.PruneRegexPaths
  70. if i.IsDir() && matchAny(p, PruneRegex) {
  71. return filepath.SkipDir
  72. }
  73. // Ignore path in Variables.IgnoreRegexPaths
  74. if matchAny(p, IgnoreRegex) {
  75. return nil
  76. }
  77. if !i.Mode().IsRegular() && (i.Mode()&os.ModeSymlink == 0) {
  78. return nil
  79. }
  80. fsCount++
  81. return nil
  82. })
  83. return fsCount, e
  84. }
  85. func (fsStatus *FilesystemStatus) parseFile(indexBucket, picksBucket *bolt.Bucket, p string, bar *mpb.Bar) {
  86. var (
  87. newFileObject FileObject
  88. knownFileObject FileObject
  89. pick, known []byte
  90. e error
  91. )
  92. defer func() {
  93. bar.Increment()
  94. fsStatusWG.Done()
  95. }()
  96. fsStatusWG.Wait()
  97. fsStatusWG.Add(1)
  98. pick = picksBucket.Get([]byte(p))
  99. known = indexBucket.Get([]byte(p))
  100. if pick != nil {
  101. fsStatus.PickedFiles = append(fsStatus.PickedFiles, p)
  102. return
  103. }
  104. if known != nil {
  105. newFileObject, e = CreateFileObject(p)
  106. if e != nil {
  107. return
  108. }
  109. knownFileObject, e = FromBytes(known)
  110. if e != nil {
  111. return
  112. }
  113. e = newFileObject.IsDifferent(knownFileObject)
  114. if e != nil {
  115. fsStatus.ModifiedFiles = append(fsStatus.ModifiedFiles, p)
  116. }
  117. return
  118. }
  119. fsStatus.NewFiles = append(fsStatus.NewFiles, p)
  120. return
  121. }
  122. func GetFilesystemDiff(root string) (FilesystemStatus, error) {
  123. var (
  124. fsStatus FilesystemStatus = FilesystemStatus{}
  125. sem *semaphore.Weighted
  126. picksBucket *bolt.Bucket
  127. indexBucket *bolt.Bucket
  128. rootStat os.FileInfo
  129. bar *mpb.Bar
  130. fsCount int
  131. poolSize int
  132. e error
  133. )
  134. poolSize = runtime.NumCPU()
  135. sem = semaphore.NewWeighted(int64(poolSize))
  136. rootStat, e = os.Stat(root)
  137. if e != nil {
  138. return fsStatus, e
  139. }
  140. if rootStat.IsDir() && root[len(root)-1:] != "/" {
  141. root = root + "/"
  142. }
  143. fsCount, e = GetFilesystemLength(root)
  144. if e != nil {
  145. return fsStatus, e
  146. }
  147. bar = ProgressBar.InitBar("Scanning...", fsCount)
  148. e = Database.FsDB.View(func(tx *bolt.Tx) error {
  149. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  150. indexBucket = tx.Bucket(Variables.FsHashIndexBucket)
  151. filepath.Walk(root, func(p string, i os.FileInfo, _ error) error {
  152. // Ignore path in Variables.PruneRegexPaths
  153. if i.IsDir() && matchAny(p, PruneRegex) {
  154. return filepath.SkipDir
  155. }
  156. // Ignore path in Variables.IgnoreRegexPaths
  157. if matchAny(p, IgnoreRegex) {
  158. return nil
  159. }
  160. if !i.Mode().IsRegular() && (i.Mode()&os.ModeSymlink == 0) {
  161. return nil
  162. }
  163. FsWalkWG.Add(1)
  164. sem.Acquire(context.Background(), 1)
  165. go func() {
  166. fsStatus.parseFile(indexBucket, picksBucket, p, bar)
  167. sem.Release(1)
  168. FsWalkWG.Done()
  169. }()
  170. FsWalkWG.Wait()
  171. return nil
  172. })
  173. indexBucket.ForEach(func(k, v []byte) error {
  174. _, e = os.Lstat(string(k))
  175. if os.IsNotExist(e) {
  176. fsStatus.MissingFiles = append(fsStatus.MissingFiles, string(k))
  177. }
  178. return nil
  179. })
  180. return nil
  181. })
  182. return fsStatus, e
  183. }