-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (125 loc) · 2.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
)
var progName = filepath.Base(os.Args[0])
type Globals struct {
db DB
Debug bool
AllDomains bool
ListDomains bool
LowerCase bool
Domain string
Root string
FSRoot NodeEntry
}
var global Globals = Globals{}
func usage() {
fmt.Fprintf(os.Stderr, "%s: invalid parameters\n", progName)
}
type DB struct {
*sql.DB
}
func init() {
flag.BoolVar(&global.AllDomains, "A", false, "Show all backup file domains.")
flag.BoolVar(&global.ListDomains, "L", false, "List all domains in backup.")
flag.BoolVar(&global.LowerCase, "l", false, "Convert all filenames to lowercase.")
flag.BoolVar(&global.Debug, "v", false, "Verbose logging.")
flag.StringVar(&global.Domain, "d", "CameraRollDomain", "Select domain to mount.")
}
func getBackupDir() (root string) {
if flag.NArg() >= 0 {
root = flag.Arg(0)
}
if root == "" {
root = os.Getenv("ROOT")
}
return
}
func getMountPoint() (mount string) {
if flag.NArg() >= 1 {
mount = flag.Arg(1)
}
if mount == "" {
mount = os.Getenv("MOUNT")
}
return
}
func openDB() error {
global.Root = getBackupDir()
debug("Opening database in %s", global.Root)
err := global.db.OpenDB(global.Root)
if err != nil {
log.Fatalf("%s: %v", global.Root, err)
}
return err
}
func debug(fmt string, args ...any) {
if global.Debug {
log.Printf(fmt, args...)
}
}
func main() {
var err error
log.SetFlags(0)
log.SetPrefix(progName + ": ")
flag.Parse()
if flag.NArg() > 2 {
usage()
os.Exit(2)
}
switch {
case global.ListDomains:
err = openDB()
if err != nil {
log.Fatalf("%s: %v", global.Root, err)
}
domains, err := global.db.GetDomains()
if err != nil {
log.Fatal(err)
}
for d := range domains {
fmt.Printf("%s\n", domains[d])
}
default:
mountpoint := getMountPoint()
if mountpoint == "" {
log.Fatalf("mount point required.")
}
debug("Using mountpoint: %s\n", mountpoint)
err = openDB()
if err != nil {
log.Fatalf("%s: %v", global.Root, err)
}
debug("Database opened successfully")
global.FSRoot, err = global.db.ReadListing()
if err != nil {
log.Fatalf("%s: %v\n", global.Root, err)
}
if err = mount(mountpoint); err != nil {
log.Fatal(err)
}
debug("Completed.")
}
}
func HandleSignals(unmount func()) {
ch := make(chan os.Signal, 1)
go func() {
for range ch {
debug("Received signal..")
fmt.Fprintf(os.Stderr, "\rCtrl-C detected. Unmounting.\n")
unmount()
signal.Stop(ch)
return
}
}()
debug("Enabling signal handlers")
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
}