db.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. func dbExport(ctx *cli.Context) {
  29. args := ctx.Args()
  30. if len(args) != 3 {
  31. 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")
  32. }
  33. store, err := openLDBStore(args[0], common.Hex2Bytes(args[2]))
  34. if err != nil {
  35. utils.Fatalf("error opening local chunk database: %s", err)
  36. }
  37. defer store.Close()
  38. var out io.Writer
  39. if args[1] == "-" {
  40. out = os.Stdout
  41. } else {
  42. f, err := os.Create(args[1])
  43. if err != nil {
  44. utils.Fatalf("error opening output file: %s", err)
  45. }
  46. defer f.Close()
  47. out = f
  48. }
  49. count, err := store.Export(out)
  50. if err != nil {
  51. utils.Fatalf("error exporting local chunk database: %s", err)
  52. }
  53. log.Info(fmt.Sprintf("successfully exported %d chunks", count))
  54. }
  55. func dbImport(ctx *cli.Context) {
  56. args := ctx.Args()
  57. if len(args) != 3 {
  58. 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")
  59. }
  60. store, err := openLDBStore(args[0], common.Hex2Bytes(args[2]))
  61. if err != nil {
  62. utils.Fatalf("error opening local chunk database: %s", err)
  63. }
  64. defer store.Close()
  65. var in io.Reader
  66. if args[1] == "-" {
  67. in = os.Stdin
  68. } else {
  69. f, err := os.Open(args[1])
  70. if err != nil {
  71. utils.Fatalf("error opening input file: %s", err)
  72. }
  73. defer f.Close()
  74. in = f
  75. }
  76. count, err := store.Import(in)
  77. if err != nil {
  78. utils.Fatalf("error importing local chunk database: %s", err)
  79. }
  80. log.Info(fmt.Sprintf("successfully imported %d chunks", count))
  81. }
  82. func dbClean(ctx *cli.Context) {
  83. args := ctx.Args()
  84. if len(args) != 2 {
  85. utils.Fatalf("invalid arguments, please specify <chunkdb> (path to a local chunk database) and the base key")
  86. }
  87. store, err := openLDBStore(args[0], common.Hex2Bytes(args[1]))
  88. if err != nil {
  89. utils.Fatalf("error opening local chunk database: %s", err)
  90. }
  91. defer store.Close()
  92. store.Cleanup()
  93. }
  94. func openLDBStore(path string, basekey []byte) (*storage.LDBStore, error) {
  95. if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil {
  96. return nil, fmt.Errorf("invalid chunkdb path: %s", err)
  97. }
  98. storeparams := storage.NewDefaultStoreParams()
  99. ldbparams := storage.NewLDBStoreParams(storeparams, path)
  100. ldbparams.BaseKey = basekey
  101. return storage.NewLDBStore(ldbparams)
  102. }