main.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 http://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "encoding/binary"
  9. "flag"
  10. "fmt"
  11. "log"
  12. "os"
  13. "github.com/syncthing/protocol"
  14. "github.com/syncthing/syncthing/lib/db"
  15. "github.com/syndtr/goleveldb/leveldb"
  16. "github.com/syndtr/goleveldb/leveldb/opt"
  17. )
  18. func main() {
  19. log.SetFlags(0)
  20. log.SetOutput(os.Stdout)
  21. flag.Parse()
  22. ldb, err := leveldb.OpenFile(flag.Arg(0), &opt.Options{
  23. ErrorIfMissing: true,
  24. Strict: opt.StrictAll,
  25. OpenFilesCacheCapacity: 100,
  26. })
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. it := ldb.NewIterator(nil, nil)
  31. var dev protocol.DeviceID
  32. for it.Next() {
  33. key := it.Key()
  34. switch key[0] {
  35. case db.KeyTypeDevice:
  36. folder := nulString(key[1 : 1+64])
  37. devBytes := key[1+64 : 1+64+32]
  38. name := nulString(key[1+64+32:])
  39. copy(dev[:], devBytes)
  40. fmt.Printf("[device] F:%q N:%q D:%v\n", folder, name, dev)
  41. var f protocol.FileInfo
  42. err := f.UnmarshalXDR(it.Value())
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. fmt.Printf(" N:%q\n F:%#o\n M:%d\n V:%v\n S:%d\n B:%d\n", f.Name, f.Flags, f.Modified, f.Version, f.Size(), len(f.Blocks))
  47. case db.KeyTypeGlobal:
  48. folder := nulString(key[1 : 1+64])
  49. name := nulString(key[1+64:])
  50. fmt.Printf("[global] F:%q N:%q V:%x\n", folder, name, it.Value())
  51. case db.KeyTypeBlock:
  52. folder := nulString(key[1 : 1+64])
  53. hash := key[1+64 : 1+64+32]
  54. name := nulString(key[1+64+32:])
  55. fmt.Printf("[block] F:%q H:%x N:%q I:%d\n", folder, hash, name, binary.BigEndian.Uint32(it.Value()))
  56. case db.KeyTypeDeviceStatistic:
  57. fmt.Printf("[dstat]\n %x\n %x\n", it.Key(), it.Value())
  58. case db.KeyTypeFolderStatistic:
  59. fmt.Printf("[fstat]\n %x\n %x\n", it.Key(), it.Value())
  60. default:
  61. fmt.Printf("[???]\n %x\n %x\n", it.Key(), it.Value())
  62. }
  63. }
  64. }
  65. func nulString(bs []byte) string {
  66. for i := range bs {
  67. if bs[i] == 0 {
  68. return string(bs[:i])
  69. }
  70. }
  71. return string(bs)
  72. }