github.com/XinFinOrg/xdcchain@v1.1.0/cmd/swarm/db.go (about) 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 17 package main 18 19 import ( 20 "fmt" 21 "io" 22 "os" 23 "path/filepath" 24 25 "github.com/ethereum/go-ethereum/cmd/utils" 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/log" 28 "github.com/ethereum/go-ethereum/swarm/storage" 29 "gopkg.in/urfave/cli.v1" 30 ) 31 32 var dbCommand = cli.Command{ 33 Name: "db", 34 CustomHelpTemplate: helpTemplate, 35 Usage: "manage the local chunk database", 36 ArgsUsage: "db COMMAND", 37 Description: "Manage the local chunk database", 38 Subcommands: []cli.Command{ 39 { 40 Action: dbExport, 41 CustomHelpTemplate: helpTemplate, 42 Name: "export", 43 Usage: "export a local chunk database as a tar archive (use - to send to stdout)", 44 ArgsUsage: "<chunkdb> <file>", 45 Description: ` 46 Export a local chunk database as a tar archive (use - to send to stdout). 47 48 swarm db export ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar 49 50 The export may be quite large, consider piping the output through the Unix 51 pv(1) tool to get a progress bar: 52 53 swarm db export ~/.ethereum/swarm/bzz-KEY/chunks - | pv > chunks.tar 54 `, 55 }, 56 { 57 Action: dbImport, 58 CustomHelpTemplate: helpTemplate, 59 Name: "import", 60 Usage: "import chunks from a tar archive into a local chunk database (use - to read from stdin)", 61 ArgsUsage: "<chunkdb> <file>", 62 Description: `Import chunks from a tar archive into a local chunk database (use - to read from stdin). 63 64 swarm db import ~/.ethereum/swarm/bzz-KEY/chunks chunks.tar 65 66 The import may be quite large, consider piping the input through the Unix 67 pv(1) tool to get a progress bar: 68 69 pv chunks.tar | swarm db import ~/.ethereum/swarm/bzz-KEY/chunks -`, 70 }, 71 }, 72 } 73 74 func dbExport(ctx *cli.Context) { 75 args := ctx.Args() 76 if len(args) != 3 { 77 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") 78 } 79 80 store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) 81 if err != nil { 82 utils.Fatalf("error opening local chunk database: %s", err) 83 } 84 defer store.Close() 85 86 var out io.Writer 87 if args[1] == "-" { 88 out = os.Stdout 89 } else { 90 f, err := os.Create(args[1]) 91 if err != nil { 92 utils.Fatalf("error opening output file: %s", err) 93 } 94 defer f.Close() 95 out = f 96 } 97 98 count, err := store.Export(out) 99 if err != nil { 100 utils.Fatalf("error exporting local chunk database: %s", err) 101 } 102 103 log.Info(fmt.Sprintf("successfully exported %d chunks", count)) 104 } 105 106 func dbImport(ctx *cli.Context) { 107 args := ctx.Args() 108 if len(args) != 3 { 109 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") 110 } 111 112 store, err := openLDBStore(args[0], common.Hex2Bytes(args[2])) 113 if err != nil { 114 utils.Fatalf("error opening local chunk database: %s", err) 115 } 116 defer store.Close() 117 118 var in io.Reader 119 if args[1] == "-" { 120 in = os.Stdin 121 } else { 122 f, err := os.Open(args[1]) 123 if err != nil { 124 utils.Fatalf("error opening input file: %s", err) 125 } 126 defer f.Close() 127 in = f 128 } 129 130 count, err := store.Import(in) 131 if err != nil { 132 utils.Fatalf("error importing local chunk database: %s", err) 133 } 134 135 log.Info(fmt.Sprintf("successfully imported %d chunks", count)) 136 } 137 138 func openLDBStore(path string, basekey []byte) (*storage.LDBStore, error) { 139 if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil { 140 return nil, fmt.Errorf("invalid chunkdb path: %s", err) 141 } 142 143 storeparams := storage.NewDefaultStoreParams() 144 ldbparams := storage.NewLDBStoreParams(storeparams, path) 145 ldbparams.BaseKey = basekey 146 return storage.NewLDBStore(ldbparams) 147 }