bep_index_updates.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import "github.com/syncthing/syncthing/internal/gen/bep"
  8. type Index struct {
  9. Folder string
  10. Files []FileInfo
  11. LastSequence int64
  12. }
  13. func (i *Index) toWire() *bep.Index {
  14. files := make([]*bep.FileInfo, len(i.Files))
  15. for j, f := range i.Files {
  16. files[j] = f.ToWire(false)
  17. }
  18. return &bep.Index{
  19. Folder: i.Folder,
  20. Files: files,
  21. LastSequence: i.LastSequence,
  22. }
  23. }
  24. func indexFromWire(w *bep.Index) *Index {
  25. if w == nil {
  26. return nil
  27. }
  28. i := &Index{
  29. Folder: w.Folder,
  30. LastSequence: w.LastSequence,
  31. }
  32. i.Files = make([]FileInfo, len(w.Files))
  33. for j, f := range w.Files {
  34. i.Files[j] = FileInfoFromWire(f)
  35. }
  36. return i
  37. }
  38. type IndexUpdate struct {
  39. Folder string
  40. Files []FileInfo
  41. LastSequence int64
  42. PrevSequence int64
  43. }
  44. func (i *IndexUpdate) toWire() *bep.IndexUpdate {
  45. files := make([]*bep.FileInfo, len(i.Files))
  46. for j, f := range i.Files {
  47. files[j] = f.ToWire(false)
  48. }
  49. return &bep.IndexUpdate{
  50. Folder: i.Folder,
  51. Files: files,
  52. LastSequence: i.LastSequence,
  53. PrevSequence: i.PrevSequence,
  54. }
  55. }
  56. func indexUpdateFromWire(w *bep.IndexUpdate) *IndexUpdate {
  57. if w == nil {
  58. return nil
  59. }
  60. i := &IndexUpdate{
  61. Folder: w.Folder,
  62. LastSequence: w.LastSequence,
  63. PrevSequence: w.PrevSequence,
  64. }
  65. i.Files = make([]FileInfo, len(w.Files))
  66. for j, f := range w.Files {
  67. i.Files[j] = FileInfoFromWire(f)
  68. }
  69. return i
  70. }