hash.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package hash
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "path/filepath"
  6. "time"
  7. )
  8. // Fdhashes struct for holding all informations about one folder.
  9. type Fdhashes struct {
  10. Path string
  11. Hashes map[string]string
  12. Times map[string]time.Time
  13. Dirty bool
  14. }
  15. func LoadHashfile(fileStr string) (*Fdhashes, error) {
  16. dir, _ := filepath.Split(fileStr)
  17. dir = filepath.ToSlash(filepath.Clean(dir))
  18. var data Fdhashes
  19. file, err := ioutil.ReadFile(fileStr)
  20. if err != nil {
  21. return nil, err
  22. }
  23. err = json.Unmarshal([]byte(file), &data)
  24. if err != nil {
  25. return nil, err
  26. }
  27. if data.Path != dir {
  28. data.Path = dir
  29. data.Dirty = true
  30. }
  31. return &data, nil
  32. }
  33. /*
  34. func LoadHashfile(fileStr string) Fdhashes {
  35. dir, _ := filepath.Split(fileStr)
  36. dir = filepath.ToSlash(filepath.Clean(dir))
  37. data := Fdhashes{Path: dir, Hashes: make(map[string]string), Times: make(map[string]time.Time), Dirty: false}
  38. if !rewrite {
  39. file, err := ioutil.ReadFile(fileStr)
  40. if err != nil {
  41. panic(err)
  42. }
  43. err = json.Unmarshal([]byte(file), &data)
  44. if err != nil {
  45. log.Printf("can't read file %s", fileStr)
  46. }
  47. }
  48. if data.Path != dir {
  49. data.Path = dir
  50. data.Dirty = true
  51. }
  52. return data
  53. }
  54. */