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.

236 lines
4.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
  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. }()
  95. pick = picksBucket.Get([]byte(p))
  96. known = indexBucket.Get([]byte(p))
  97. if pick != nil {
  98. fsStatusWG.Wait()
  99. fsStatusWG.Add(1)
  100. fsStatus.PickedFiles = append(fsStatus.PickedFiles, p)
  101. fsStatusWG.Done()
  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. fsStatusWG.Wait()
  116. fsStatusWG.Add(1)
  117. fsStatus.ModifiedFiles = append(fsStatus.ModifiedFiles, p)
  118. fsStatusWG.Done()
  119. }
  120. return
  121. }
  122. fsStatus.NewFiles = append(fsStatus.NewFiles, p)
  123. return
  124. }
  125. func GetFilesystemDiff(root string) (FilesystemStatus, error) {
  126. var (
  127. fsStatus FilesystemStatus = FilesystemStatus{}
  128. sem *semaphore.Weighted
  129. picksBucket *bolt.Bucket
  130. indexBucket *bolt.Bucket
  131. rootStat os.FileInfo
  132. bar *mpb.Bar
  133. fsCount int
  134. poolSize int
  135. e error
  136. )
  137. poolSize = runtime.NumCPU()
  138. sem = semaphore.NewWeighted(int64(poolSize))
  139. rootStat, e = os.Stat(root)
  140. if e != nil {
  141. return fsStatus, e
  142. }
  143. if rootStat.IsDir() && root[len(root)-1:] != "/" {
  144. root = root + "/"
  145. }
  146. fsCount, e = GetFilesystemLength(root)
  147. if e != nil {
  148. return fsStatus, e
  149. }
  150. bar = ProgressBar.InitBar("Scanning...", fsCount)
  151. e = Database.FsDB.View(func(tx *bolt.Tx) error {
  152. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  153. indexBucket = tx.Bucket(Variables.FsHashIndexBucket)
  154. filepath.Walk(root, func(p string, i os.FileInfo, _ error) error {
  155. // Ignore path in Variables.PruneRegexPaths
  156. if i.IsDir() && matchAny(p, PruneRegex) {
  157. return filepath.SkipDir
  158. }
  159. // Ignore path in Variables.IgnoreRegexPaths
  160. if matchAny(p, IgnoreRegex) {
  161. return nil
  162. }
  163. if !i.Mode().IsRegular() && (i.Mode()&os.ModeSymlink == 0) {
  164. return nil
  165. }
  166. FsWalkWG.Add(1)
  167. sem.Acquire(context.Background(), 1)
  168. go func() {
  169. fsStatus.parseFile(indexBucket, picksBucket, p, bar)
  170. sem.Release(1)
  171. FsWalkWG.Done()
  172. }()
  173. FsWalkWG.Wait()
  174. return nil
  175. })
  176. indexBucket.ForEach(func(k, v []byte) error {
  177. _, e = os.Lstat(string(k))
  178. if os.IsNotExist(e) {
  179. fsStatus.MissingFiles = append(fsStatus.MissingFiles, string(k))
  180. }
  181. return nil
  182. })
  183. return nil
  184. })
  185. return fsStatus, e
  186. }