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.

185 lines
3.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
  1. package Filesystem
  2. import (
  3. "PackageManager/Client/Database"
  4. "PackageManager/Color"
  5. "PackageManager/Variables"
  6. "fmt"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. bolt "go.etcd.io/bbolt"
  11. )
  12. type FilesystemStatus struct {
  13. NewFiles []string
  14. PickedFiles []string
  15. ModifiedFiles []string
  16. MissingFiles []string
  17. }
  18. func ShowFilesystemDiff(root string) error {
  19. var (
  20. fsStatus FilesystemStatus
  21. //f string
  22. e error
  23. )
  24. fsStatus, e = GetFilesystemDiff(root)
  25. if e != nil {
  26. return e
  27. }
  28. fmt.Println("New files:")
  29. PrintFilesOrLength(fsStatus.NewFiles, Color.Green)
  30. fmt.Println("Added files:")
  31. PrintFilesOrLength(fsStatus.PickedFiles, Color.Green)
  32. fmt.Println("Modified files:")
  33. PrintFilesOrLength(fsStatus.ModifiedFiles, Color.Warning)
  34. fmt.Println("Deleted files:")
  35. PrintFilesOrLength(fsStatus.MissingFiles, Color.Fatal)
  36. return nil
  37. }
  38. func GetFilesystemLength(root string) (int, error) {
  39. var (
  40. rootStat os.FileInfo
  41. fsCount int = 0
  42. e error
  43. )
  44. rootStat, e = os.Stat(root)
  45. if e != nil {
  46. return fsCount, e
  47. }
  48. if rootStat.IsDir() && root[len(root)-1:] != "/" {
  49. root = root + "/"
  50. }
  51. filepath.Walk(root, func(p string, i os.FileInfo, _ error) error {
  52. // Ignore path in Variables.PruneRegexPaths
  53. if i.IsDir() && matchAny(p, PruneRegex) {
  54. log.Println("Prune", p)
  55. return filepath.SkipDir
  56. }
  57. // Ignore path in Variables.IgnoreRegexPaths
  58. if matchAny(p, IgnoreRegex) {
  59. return nil
  60. }
  61. if !i.Mode().IsRegular() && (i.Mode()&os.ModeSymlink == 0) {
  62. return nil
  63. }
  64. fsCount++
  65. return nil
  66. })
  67. return fsCount, e
  68. }
  69. func GetFilesystemDiff(root string) (FilesystemStatus, error) {
  70. var (
  71. fsStatus FilesystemStatus = FilesystemStatus{}
  72. rootStat os.FileInfo
  73. picksBucket *bolt.Bucket
  74. indexBucket *bolt.Bucket
  75. pick, known []byte
  76. newFileObject FileObject
  77. knownFileObject FileObject
  78. e error
  79. )
  80. rootStat, e = os.Stat(root)
  81. if e != nil {
  82. return fsStatus, e
  83. }
  84. if rootStat.IsDir() && root[len(root)-1:] != "/" {
  85. root = root + "/"
  86. }
  87. e = Database.FsDB.View(func(tx *bolt.Tx) error {
  88. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  89. indexBucket = tx.Bucket(Variables.FsHashIndexBucket)
  90. filepath.Walk(root, func(p string, i os.FileInfo, _ error) error {
  91. // Ignore path in Variables.PruneRegexPaths
  92. if i.IsDir() && matchAny(p, PruneRegex) {
  93. log.Println("Prune", p)
  94. return filepath.SkipDir
  95. }
  96. // Ignore path in Variables.IgnoreRegexPaths
  97. if matchAny(p, IgnoreRegex) {
  98. return nil
  99. }
  100. if !i.Mode().IsRegular() && (i.Mode()&os.ModeSymlink == 0) {
  101. return nil
  102. }
  103. pick = picksBucket.Get([]byte(p))
  104. known = indexBucket.Get([]byte(p))
  105. if pick != nil {
  106. fsStatus.PickedFiles = append(fsStatus.PickedFiles, p)
  107. return nil
  108. }
  109. if known != nil {
  110. newFileObject, e = CreateFileObject(p)
  111. if os.IsNotExist(e) {
  112. return nil
  113. }
  114. if e != nil {
  115. return e
  116. }
  117. knownFileObject, e = FromBytes(known)
  118. if e != nil {
  119. return e
  120. }
  121. e = newFileObject.IsDifferent(knownFileObject)
  122. if e != nil {
  123. fsStatus.ModifiedFiles = append(fsStatus.ModifiedFiles, p)
  124. }
  125. return nil
  126. }
  127. fsStatus.NewFiles = append(fsStatus.NewFiles, p)
  128. return nil
  129. })
  130. indexBucket.ForEach(func(k, v []byte) error {
  131. _, e = os.Lstat(string(k))
  132. if os.IsNotExist(e) {
  133. fsStatus.MissingFiles = append(fsStatus.MissingFiles, string(k))
  134. }
  135. return nil
  136. })
  137. return nil
  138. })
  139. return fsStatus, e
  140. }