Debian Codegraph @

cmd/lmdb_copy/main.go

raw symbols SCIP
1 /*
2 Command lmdb_copy is a clone of mdb_copy that copies an LMDB environment. A
3 consistent copy is made even if the source database is in use.
4
5 Command line flags mirror the flags for the original program. For information
6 about, run lmdb_copy with the -h flag.
7
8 lmdb_copy -h
9 */
10 package main
11
12 import (
13 "flag"
14 "log"
15 "os"
16
17 "github.com/bmatsuo/lmdb-go/internal/lmdbcmd"
18 "github.com/bmatsuo/lmdb-go/lmdb"
19 )
20
21 func main() {
22 opt := &Options{}
23 flag.BoolVar(&opt.Compact, "c", false, "Compact while copying.")
24 flag.Parse()
25
26 lmdbcmd.PrintVersion()
27
28 if flag.NArg() > 2 {
29 log.Fatalf("too many arguments specified")
30 }
31 if flag.NArg() == 0 {
32 log.Fatalf("at least one argument must be specified")
33 }
34
35 var srcpath, dstpath string
36 srcpath = flag.Arg(0)
37 if flag.NArg() > 1 {
38 dstpath = flag.Arg(1)
39 }
40
41 copyEnv(srcpath, dstpath, opt)
42 }
43
44 // Options contain the command line options for an lmdb_copy command.
45 type Options struct {
46 Compact bool
47 }
48
49 func copyEnv(srcpath, dstpath string, opt *Options) error {
50 env, err := lmdb.NewEnv()
51 if err != nil {
52 return err
53 }
54 err = env.Open(srcpath, lmdbcmd.OpenFlag(), 0644)
55 if err != nil {
56 return err
57 }
58 var flags uint
59 if opt != nil && opt.Compact {
60 flags |= lmdb.CopyCompact
61 }
62 if dstpath != "" {
63 return env.CopyFlag(dstpath, flags)
64 }
65 fd := os.Stdout.Fd()
66 return env.CopyFDFlag(fd, flags)
67 }