db.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "io"
  20. "os"
  21. "path/filepath"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/swarm/storage"
  26. "gopkg.in/urfave/cli.v1"
  27. )
  28. var dbCommand = cli.Command{
  29. Name: "db",
  30. CustomHelpTemplate: helpTemplate,
  31. Usage: "manage the local chunk database",
  32. ArgsUsage: "db COMMAND",
  33. Description: "Manage the local chunk database",
  34. Subcommands: []cli.Command{
  35. {
  36. Action: dbExport,
  37. CustomHelpTemplate: helpTemplate,
  38. Name: "export",
  39. Usage: "export a local chunk database as a tar archive (use - to send to stdout)",
  40. ArgsUsage: "<chunkdb> <file>",
  41. Description: `
  42. Export a local chunk database as a tar archive (use - to send to stdout).
  43. swarm db export ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar
  44. The export may be quite large, consider piping the output through the Unix
  45. pv(1) tool to get a progress bar:
  46. swarm db export ~/.ethereum/swarm/bzz-KEY/chunks - | pv > chunks.tar
  47. `,
  48. },
  49. {
  50. Action: dbImport,
  51. CustomHelpTemplate: helpTemplate,
  52. Name: "import",
  53. Usage: "import chunks from a tar archive into a local chunk database (use - to read from stdin)",
  54. ArgsUsage: "<chunkdb> <file>",
  55. Description: `Import chunks from a tar archive into a local chunk database (use - to read from stdin).
  56. swarm db import ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar
  57. The import may be quite large, consider piping the input through the Unix
  58. pv(1) tool to get a progress bar:
  59. pv chunks.tar | swarm db import ~/.ethereum/swarm/bzz-KEY/chunks -`,
  60. },
  61. },
  62. }
  63. func dbExport(ctx *cli.Context) {
  64. args := ctx.Args()
  65. if len(args) != 3 {
  66. utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database), <file> (path to write the tar archive to, - for stdout) and the base key")
  67. }
  68. store, err := openLDBStore(args[0], common.Hex2Bytes(args[2]))
  69. if err != nil {
  70. utils.Fatalf("error opening local chunk database: %s", err)
  71. }
  72. defer store.Close()
  73. var out io.Writer
  74. if args[1] == "-" {
  75. out = os.Stdout
  76. } else {
  77. f, err := os.Create(args[1])
  78. if err != nil {
  79. utils.Fatalf("error opening output file: %s", err)
  80. }
  81. defer f.Close()
  82. out = f
  83. }
  84. count, err := store.Export(out)
  85. if err != nil {
  86. utils.Fatalf("error exporting local chunk database: %s", err)
  87. }
  88. log.Info(fmt.Sprintf("successfully exported %d chunks", count))
  89. }
  90. func dbImport(ctx *cli.Context) {
  91. args := ctx.Args()
  92. if len(args) != 3 {
  93. utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database), <file> (path to read the tar archive from, - for stdin) and the base key")
  94. }
  95. store, err := openLDBStore(args[0], common.Hex2Bytes(args[2]))
  96. if err != nil {
  97. utils.Fatalf("error opening local chunk database: %s", err)
  98. }
  99. defer store.Close()
  100. var in io.Reader
  101. if args[1] == "-" {
  102. in = os.Stdin
  103. } else {
  104. f, err := os.Open(args[1])
  105. if err != nil {
  106. utils.Fatalf("error opening input file: %s", err)
  107. }
  108. defer f.Close()
  109. in = f
  110. }
  111. count, err := store.Import(in)
  112. if err != nil {
  113. utils.Fatalf("error importing local chunk database: %s", err)
  114. }
  115. log.Info(fmt.Sprintf("successfully imported %d chunks", count))
  116. }
  117. func openLDBStore(path string, basekey []byte) (*storage.LDBStore, error) {
  118. if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil {
  119. return nil, fmt.Errorf("invalid chunkdb path: %s", err)
  120. }
  121. storeparams := storage.NewDefaultStoreParams()
  122. ldbparams := storage.NewLDBStoreParams(storeparams, path)
  123. ldbparams.BaseKey = basekey
  124. return storage.NewLDBStore(ldbparams)
  125. }