set_anal.go 873 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
  2. // Use of this source code is governed by an MIT-style license that can be
  3. // found in the LICENSE file.
  4. //+build anal
  5. package files
  6. import (
  7. "crypto/md5"
  8. "github.com/calmh/syncthing/scanner"
  9. )
  10. type key struct {
  11. Name string
  12. Version uint64
  13. Modified int64
  14. Hash [md5.Size]byte
  15. }
  16. func keyFor(f scanner.File) key {
  17. h := md5.New()
  18. for _, b := range f.Blocks {
  19. h.Write(b.Hash)
  20. }
  21. return key{
  22. Name: f.Name,
  23. Version: f.Version,
  24. Modified: f.Modified,
  25. Hash: md5.Sum(nil),
  26. }
  27. }
  28. func (a key) newerThan(b key) bool {
  29. if a.Version != b.Version {
  30. return a.Version > b.Version
  31. }
  32. if a.Modified != b.Modified {
  33. return a.Modified > b.Modified
  34. }
  35. for i := 0; i < md5.Size; i++ {
  36. if a.Hash[i] != b.Hash[i] {
  37. return a.Hash[i] > b.Hash[i]
  38. }
  39. }
  40. return false
  41. }