-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (152 loc) · 4.11 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
152
153
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
_ "net/http/pprof"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/docopt/docopt-go"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var (
db *gorm.DB
uid uint32
gid uint32
storagePath string
mountpoint string
)
func parseUserAndSet(uidgid []string) {
uidParsed, err := strconv.ParseUint(uidgid[0], 10, 32)
if err != nil {
log.Fatal("Error parsing uid:", err)
}
gidParsed, err := strconv.ParseUint(uidgid[1], 10, 32)
if err != nil {
log.Fatal("Error parsing gid:", err)
}
uid = uint32(uidParsed)
gid = uint32(gidParsed)
}
func setUIDGID(uidgid string) {
if uidgid == "" {
uid = uint32(syscall.Getuid())
gid = uint32(syscall.Getgid())
} else {
split := strings.Split(uidgid, ":")
if len(split) != 2 {
log.Fatal("Invalid uid:gid parameter")
}
parseUserAndSet(split)
}
}
const usage = `Usage:
memetagfs [-v] [-s storage] [-d database.db] [-u uid:gid] [-p] [--logcache] [--logfuse string] <mountpoint>
memetagfs [-d database.db] [-s storage] -i -t tags.sql -c data.sql -r storage
memetagfs -d database.db -s storage --fsck [-f] [-p] [-v] <mountpoint>
memetagfs -h
Options:
-s --storage dir Storage directory [default: storage]
-d --database database Path to the database [default: fs.db]
-p --prof Run a webserver to profile the binary
-u uid:gid Use this uid and gid for files instead of current user
-i Import H2 database from jtagsfs
-t tags.sql tags.sql file from jtagsfs
-c data.sql data.sql file from jtagsfs
-r storage storage from jtagsfs
--fsck Check the database and storage for errors and try to fix them
-f Fix the errors in the database and storage
-v --verbose Verbose logging
--logcache Display internal cache events and effectiveness
--logfuse string Shows filesystem access for lines that contain 'string'
-h --help Show this help.
`
func main() {
opts, err := docopt.ParseDoc(usage)
if err != nil {
log.Fatalln("Error parsing options:", err)
}
mountpoint, _ = opts.String("<mountpoint>")
if uidgid, err := opts.String("-u"); err == nil {
setUIDGID(uidgid)
} else {
setUIDGID("")
}
logCache = opts["--logcache"].(bool)
dbPath, _ := opts.String("--database")
db, err = gorm.Open("sqlite3", dbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
if v, _ := opts.Bool("--verbose"); v {
db.LogMode(true)
}
storagePath, _ = opts.String("--storage")
if logfuse, err := opts.String("--logfuse"); err == nil {
fuse.Debug = func(msg interface{}) {
if strings.Contains(msg.(fmt.Stringer).String(), logfuse) {
log.Println(msg)
}
}
}
db.AutoMigrate(item{})
if p, _ := opts.Bool("--prof"); p {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
if i, _ := opts.Bool("-i"); i {
if err := importH2(opts["-t"].(string), opts["-c"].(string), opts["-r"].(string)); err != nil {
log.Fatal(err)
}
return
}
upgradeStorage()
c, err := fuse.Mount(mountpoint)
if err != nil {
log.Fatal(err)
}
defer c.Close()
cc := make(chan os.Signal)
signal.Notify(cc, os.Interrupt)
signal.Notify(cc, syscall.SIGTERM)
go func() {
for range cc {
fuse.Unmount(mountpoint)
}
}()
if fsckOpt, _ := opts.Bool("--fsck"); fsckOpt {
fix, _ := opts.Bool("-f")
go func() {
defer func() {
log.Println("Wait for a second for file operations to finish before unmounting...")
time.Sleep(time.Second * 1)
for i := 0; i < 5; i++ {
if err := fuse.Unmount(mountpoint); err != nil {
log.Println("Error while unmounting", err)
time.Sleep(time.Second * 3)
} else {
break
}
}
log.Println("Couldn't unmount the filesystem after fsck, please do it manually.")
}()
if err := fsck(fix); err != nil {
log.Println("Check complete,", err)
return
}
log.Println("Check complete, no errors found.")
}()
}
if err = fs.Serve(c, filesystem{}); err != nil {
log.Fatal(err)
}
}