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