util.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. package model
  5. import (
  6. "fmt"
  7. "path/filepath"
  8. "github.com/calmh/syncthing/protocol"
  9. "github.com/calmh/syncthing/scanner"
  10. )
  11. func fileFromFileInfo(f protocol.FileInfo) scanner.File {
  12. var blocks = make([]scanner.Block, len(f.Blocks))
  13. var offset int64
  14. for i, b := range f.Blocks {
  15. blocks[i] = scanner.Block{
  16. Offset: offset,
  17. Size: b.Size,
  18. Hash: b.Hash,
  19. }
  20. offset += int64(b.Size)
  21. }
  22. return scanner.File{
  23. // Name is with native separator and normalization
  24. Name: filepath.FromSlash(f.Name),
  25. Size: offset,
  26. Flags: f.Flags &^ protocol.FlagInvalid,
  27. Modified: f.Modified,
  28. Version: f.Version,
  29. Blocks: blocks,
  30. Suppressed: f.Flags&protocol.FlagInvalid != 0,
  31. }
  32. }
  33. func fileInfoFromFile(f scanner.File) protocol.FileInfo {
  34. var blocks = make([]protocol.BlockInfo, len(f.Blocks))
  35. for i, b := range f.Blocks {
  36. blocks[i] = protocol.BlockInfo{
  37. Size: b.Size,
  38. Hash: b.Hash,
  39. }
  40. }
  41. pf := protocol.FileInfo{
  42. Name: filepath.ToSlash(f.Name),
  43. Flags: f.Flags,
  44. Modified: f.Modified,
  45. Version: f.Version,
  46. Blocks: blocks,
  47. }
  48. if f.Suppressed {
  49. pf.Flags |= protocol.FlagInvalid
  50. }
  51. return pf
  52. }
  53. func cmMap(cm protocol.ClusterConfigMessage) map[string]map[string]uint32 {
  54. m := make(map[string]map[string]uint32)
  55. for _, repo := range cm.Repositories {
  56. m[repo.ID] = make(map[string]uint32)
  57. for _, node := range repo.Nodes {
  58. m[repo.ID][node.ID] = node.Flags
  59. }
  60. }
  61. return m
  62. }
  63. type ClusterConfigMismatch error
  64. // compareClusterConfig returns nil for two equivalent configurations,
  65. // otherwise a decriptive error
  66. func compareClusterConfig(local, remote protocol.ClusterConfigMessage) error {
  67. lm := cmMap(local)
  68. rm := cmMap(remote)
  69. for repo, lnodes := range lm {
  70. _ = lnodes
  71. if rnodes, ok := rm[repo]; ok {
  72. for node, lflags := range lnodes {
  73. if rflags, ok := rnodes[node]; ok {
  74. if lflags&protocol.FlagShareBits != rflags&protocol.FlagShareBits {
  75. return ClusterConfigMismatch(fmt.Errorf("remote has different sharing flags for node %q in repository %q", node, repo))
  76. }
  77. }
  78. }
  79. }
  80. }
  81. return nil
  82. }