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.

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