dump.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2015 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 main
  7. import (
  8. "encoding/binary"
  9. "fmt"
  10. "log"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/db"
  13. "github.com/syncthing/syncthing/lib/db/backend"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. )
  16. func dump(ldb backend.Backend) {
  17. it, err := ldb.NewPrefixIterator(nil)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. for it.Next() {
  22. key := it.Key()
  23. switch key[0] {
  24. case db.KeyTypeDevice:
  25. folder := binary.BigEndian.Uint32(key[1:])
  26. device := binary.BigEndian.Uint32(key[1+4:])
  27. name := nulString(key[1+4+4:])
  28. fmt.Printf("[device] F:%d D:%d N:%q", folder, device, name)
  29. var f protocol.FileInfo
  30. err := f.Unmarshal(it.Value())
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. fmt.Printf(" V:%v\n", f)
  35. case db.KeyTypeGlobal:
  36. folder := binary.BigEndian.Uint32(key[1:])
  37. name := nulString(key[1+4:])
  38. var flv db.VersionList
  39. flv.Unmarshal(it.Value())
  40. fmt.Printf("[global] F:%d N:%q V:%s\n", folder, name, flv)
  41. case db.KeyTypeBlock:
  42. folder := binary.BigEndian.Uint32(key[1:])
  43. hash := key[1+4 : 1+4+32]
  44. name := nulString(key[1+4+32:])
  45. fmt.Printf("[block] F:%d H:%x N:%q I:%d\n", folder, hash, name, binary.BigEndian.Uint32(it.Value()))
  46. case db.KeyTypeDeviceStatistic:
  47. fmt.Printf("[dstat] K:%x V:%x\n", it.Key(), it.Value())
  48. case db.KeyTypeFolderStatistic:
  49. fmt.Printf("[fstat] K:%x V:%x\n", it.Key(), it.Value())
  50. case db.KeyTypeVirtualMtime:
  51. folder := binary.BigEndian.Uint32(key[1:])
  52. name := nulString(key[1+4:])
  53. val := it.Value()
  54. var real, virt time.Time
  55. real.UnmarshalBinary(val[:len(val)/2])
  56. virt.UnmarshalBinary(val[len(val)/2:])
  57. fmt.Printf("[mtime] F:%d N:%q R:%v V:%v\n", folder, name, real, virt)
  58. case db.KeyTypeFolderIdx:
  59. key := binary.BigEndian.Uint32(it.Key()[1:])
  60. fmt.Printf("[folderidx] K:%d V:%q\n", key, it.Value())
  61. case db.KeyTypeDeviceIdx:
  62. key := binary.BigEndian.Uint32(it.Key()[1:])
  63. val := it.Value()
  64. if len(val) == 0 {
  65. fmt.Printf("[deviceidx] K:%d V:<nil>\n", key)
  66. } else {
  67. dev := protocol.DeviceIDFromBytes(val)
  68. fmt.Printf("[deviceidx] K:%d V:%s\n", key, dev)
  69. }
  70. case db.KeyTypeIndexID:
  71. device := binary.BigEndian.Uint32(it.Key()[1:])
  72. folder := binary.BigEndian.Uint32(it.Key()[5:])
  73. fmt.Printf("[indexid] D:%d F:%d I:%x\n", device, folder, it.Value())
  74. case db.KeyTypeFolderMeta:
  75. folder := binary.BigEndian.Uint32(it.Key()[1:])
  76. fmt.Printf("[foldermeta] F:%d V:%x\n", folder, it.Value())
  77. case db.KeyTypeMiscData:
  78. fmt.Printf("[miscdata] K:%q V:%q\n", it.Key()[1:], it.Value())
  79. case db.KeyTypeSequence:
  80. folder := binary.BigEndian.Uint32(it.Key()[1:])
  81. seq := binary.BigEndian.Uint64(it.Key()[5:])
  82. fmt.Printf("[sequence] F:%d S:%d V:%q\n", folder, seq, it.Value())
  83. case db.KeyTypeNeed:
  84. folder := binary.BigEndian.Uint32(it.Key()[1:])
  85. file := string(it.Key()[5:])
  86. fmt.Printf("[need] F:%d V:%q\n", folder, file)
  87. case db.KeyTypeBlockList:
  88. fmt.Printf("[blocklist] H:%x\n", it.Key()[1:])
  89. default:
  90. fmt.Printf("[???]\n %x\n %x\n", it.Key(), it.Value())
  91. }
  92. }
  93. }