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.

162 lines
2.7 KiB

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