github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/cmd/swarm/db.go (about) 1 // Copyright 2017 The Spectrum Authors 2 // This file is part of Spectrum. 3 // 4 // Spectrum 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 // Spectrum 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 Spectrum. 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/SmartMeshFoundation/Spectrum/cmd/utils" 26 "github.com/SmartMeshFoundation/Spectrum/log" 27 "github.com/SmartMeshFoundation/Spectrum/swarm/storage" 28 "gopkg.in/urfave/cli.v1" 29 ) 30 31 func dbExport(ctx *cli.Context) { 32 args := ctx.Args() 33 if len(args) != 2 { 34 utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database) and <file> (path to write the tar archive to, - for stdout)") 35 } 36 37 store, err := openDbStore(args[0]) 38 if err != nil { 39 utils.Fatalf("error opening local chunk database: %s", err) 40 } 41 defer store.Close() 42 43 var out io.Writer 44 if args[1] == "-" { 45 out = os.Stdout 46 } else { 47 f, err := os.Create(args[1]) 48 if err != nil { 49 utils.Fatalf("error opening output file: %s", err) 50 } 51 defer f.Close() 52 out = f 53 } 54 55 count, err := store.Export(out) 56 if err != nil { 57 utils.Fatalf("error exporting local chunk database: %s", err) 58 } 59 60 log.Info(fmt.Sprintf("successfully exported %d chunks", count)) 61 } 62 63 func dbImport(ctx *cli.Context) { 64 args := ctx.Args() 65 if len(args) != 2 { 66 utils.Fatalf("invalid arguments, please specify both <chunkdb> (path to a local chunk database) and <file> (path to read the tar archive from, - for stdin)") 67 } 68 69 store, err := openDbStore(args[0]) 70 if err != nil { 71 utils.Fatalf("error opening local chunk database: %s", err) 72 } 73 defer store.Close() 74 75 var in io.Reader 76 if args[1] == "-" { 77 in = os.Stdin 78 } else { 79 f, err := os.Open(args[1]) 80 if err != nil { 81 utils.Fatalf("error opening input file: %s", err) 82 } 83 defer f.Close() 84 in = f 85 } 86 87 count, err := store.Import(in) 88 if err != nil { 89 utils.Fatalf("error importing local chunk database: %s", err) 90 } 91 92 log.Info(fmt.Sprintf("successfully imported %d chunks", count)) 93 } 94 95 func dbClean(ctx *cli.Context) { 96 args := ctx.Args() 97 if len(args) != 1 { 98 utils.Fatalf("invalid arguments, please specify <chunkdb> (path to a local chunk database)") 99 } 100 101 store, err := openDbStore(args[0]) 102 if err != nil { 103 utils.Fatalf("error opening local chunk database: %s", err) 104 } 105 defer store.Close() 106 107 store.Cleanup() 108 } 109 110 func openDbStore(path string) (*storage.DbStore, error) { 111 if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil { 112 return nil, fmt.Errorf("invalid chunkdb path: %s", err) 113 } 114 hash := storage.MakeHashFunc("SHA3") 115 return storage.NewDbStore(path, hash, 10000000, 0) 116 }