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.

170 lines
2.9 KiB

  1. package Filesystem
  2. import (
  3. "PackageManager/Client/Database"
  4. "PackageManager/Variables"
  5. "crypto/md5"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. )
  13. func HashFile(path string) (string, error) {
  14. var (
  15. Md5Hash string
  16. hashBytes [16]byte
  17. file *os.File
  18. e error
  19. )
  20. file, e = os.Open(path)
  21. if e != nil {
  22. panic(e)
  23. }
  24. defer file.Close()
  25. body, e := ioutil.ReadAll(file)
  26. if e != nil {
  27. panic(e)
  28. }
  29. //Get the 16 bytes hash
  30. hashBytes = md5.Sum(body)
  31. //Convert the bytes to a string
  32. Md5Hash = fmt.Sprintf("%x", hashBytes)
  33. return Md5Hash, nil
  34. }
  35. func UpdateFilesystemHash() error {
  36. var (
  37. fileHash string
  38. rows []Database.FilesystemHashRow
  39. dir string
  40. e error
  41. )
  42. for _, dir = range Variables.InstallDirs {
  43. _, e = os.Stat(dir)
  44. if os.IsNotExist(e) {
  45. continue
  46. }
  47. e = filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
  48. if e != nil {
  49. return e
  50. }
  51. // Ignore hidden files
  52. if strings.HasPrefix(info.Name(), ".") || strings.HasPrefix(path, ".") {
  53. return nil
  54. }
  55. // Ignore directories
  56. if info.IsDir() {
  57. return nil
  58. }
  59. fileHash, e = HashFile(path)
  60. if e != nil {
  61. return e
  62. }
  63. rows = append(rows, Database.FilesystemHashRow{
  64. Path: path,
  65. Hash: fileHash,
  66. UpdatedAt: info.ModTime(),
  67. })
  68. // TODO: If len(rows) > x, update the db and clear out rows, and continue
  69. return nil
  70. })
  71. if e != nil {
  72. panic(e)
  73. }
  74. }
  75. return Database.FindOrCreateFileHash(rows)
  76. }
  77. func GetFilesystemDiff() (map[int]string, map[int]string, error) {
  78. var (
  79. fileHash string
  80. hashes []string = []string{}
  81. lastUpdatedAt time.Time
  82. dirtyFiles map[int]string
  83. newFiles map[int]string = make(map[int]string)
  84. newFilesTmp map[string]string = make(map[string]string)
  85. counter int
  86. ok bool
  87. e error
  88. )
  89. lastUpdatedAt, e = Database.GetMostRecentTimestamp()
  90. if e != nil {
  91. return dirtyFiles, newFiles, e
  92. }
  93. e = filepath.Walk(".", func(path string, info os.FileInfo, e error) error {
  94. if e != nil {
  95. return e
  96. }
  97. // Ignore hidden files
  98. if strings.HasPrefix(info.Name(), ".") || strings.HasPrefix(path, ".") {
  99. return nil
  100. }
  101. // Ignore directories
  102. if info.IsDir() {
  103. return nil
  104. }
  105. fileHash, e = HashFile(path)
  106. if e != nil {
  107. return e
  108. }
  109. hashes = append(hashes, fileHash)
  110. if info.ModTime().After(lastUpdatedAt) {
  111. newFilesTmp[path] = path
  112. }
  113. // TODO: If len(rows) > x, update the db and clear out rows, and continue
  114. return nil
  115. })
  116. if e != nil {
  117. return dirtyFiles, newFiles, e
  118. }
  119. dirtyFiles, e = Database.FindModifiedFiles(hashes)
  120. if e != nil {
  121. return dirtyFiles, newFiles, e
  122. }
  123. for _, file := range dirtyFiles {
  124. _, ok = newFilesTmp[file]
  125. if ok {
  126. delete(newFilesTmp, file)
  127. }
  128. }
  129. counter = len(dirtyFiles)
  130. for _, file := range newFilesTmp {
  131. newFiles[counter] = file
  132. counter++
  133. }
  134. return dirtyFiles, newFiles, e
  135. }