forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cammount.go
239 lines (208 loc) · 5.67 KB
/
cammount.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// +build linux darwin
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/cacher"
"camlistore.org/pkg/client"
"camlistore.org/pkg/fs"
"camlistore.org/pkg/osutil"
"camlistore.org/pkg/search"
"camlistore.org/third_party/bazil.org/fuse"
fusefs "camlistore.org/third_party/bazil.org/fuse/fs"
)
var (
debug = flag.Bool("debug", false, "print debugging messages.")
xterm = flag.Bool("xterm", false, "Run an xterm in the mounted directory. Shut down when xterm ends.")
term = flag.Bool("term", false, "Open a terminal window. Doesn't shut down when exited. Mostly for demos.")
open = flag.Bool("open", false, "Open a GUI window")
)
func usage() {
fmt.Fprint(os.Stderr, "usage: cammount [opts] [<mountpoint> [<root-blobref>|<share URL>|<root-name>]]\n")
flag.PrintDefaults()
os.Exit(2)
}
func main() {
var conn *fuse.Conn
// Scans the arg list and sets up flags
client.AddFlags()
flag.Usage = usage
flag.Parse()
narg := flag.NArg()
if narg > 2 {
usage()
}
var mountPoint string
var err error
if narg > 0 {
mountPoint = flag.Arg(0)
} else {
mountPoint, err = ioutil.TempDir("", "cammount")
if err != nil {
log.Fatal(err)
}
log.Printf("No mount point given. Using: %s", mountPoint)
defer os.Remove(mountPoint)
}
errorf := func(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg, args...)
fmt.Fprint(os.Stderr, "\n")
usage()
}
var (
cl *client.Client
root blob.Ref // nil if only one arg
camfs *fs.CamliFileSystem
)
if narg == 2 {
rootArg := flag.Arg(1)
// not trying very hard since NewFromShareRoot will do it better with a regex
if strings.HasPrefix(rootArg, "http://") ||
strings.HasPrefix(rootArg, "https://") {
if client.ExplicitServer() != "" {
errorf("Can't use an explicit blobserver with a share URL; the blobserver is implicit from the share URL.")
}
var err error
cl, root, err = client.NewFromShareRoot(rootArg)
if err != nil {
log.Fatal(err)
}
} else {
cl = client.NewOrFail() // automatic from flags
cl.SetHTTPClient(&http.Client{Transport: cl.TransportForConfig(nil)})
var ok bool
root, ok = blob.Parse(rootArg)
if !ok {
// not a blobref, check for root name instead
req := &search.WithAttrRequest{N: 1, Attr: "camliRoot", Value: rootArg}
wres, err := cl.GetPermanodesWithAttr(req)
if err != nil {
log.Fatal("could not query search")
}
if wres.WithAttr != nil {
root = wres.WithAttr[0].Permanode
} else {
log.Fatalf("root specified is not a blobref or name of a root: %q\n", rootArg)
}
}
}
} else {
cl = client.NewOrFail() // automatic from flags
cl.SetHTTPClient(&http.Client{Transport: cl.TransportForConfig(nil)})
}
diskCacheFetcher, err := cacher.NewDiskCache(cl)
if err != nil {
log.Fatalf("Error setting up local disk cache: %v", err)
}
defer diskCacheFetcher.Clean()
if root.Valid() {
var err error
camfs, err = fs.NewRootedCamliFileSystem(cl, diskCacheFetcher, root)
if err != nil {
log.Fatalf("Error creating root with %v: %v", root, err)
}
} else {
camfs = fs.NewDefaultCamliFileSystem(cl, diskCacheFetcher)
}
if *debug {
fuse.Debug = func(msg interface{}) { log.Print(msg) }
// TODO: set fs's logger
}
// This doesn't appear to work on OS X:
sigc := make(chan os.Signal, 1)
conn, err = fuse.Mount(mountPoint)
if err != nil {
if err.Error() == "cannot find load_fusefs" && runtime.GOOS == "darwin" {
log.Fatal("FUSE not available; install from http://osxfuse.github.io/")
}
log.Fatalf("Mount: %v", err)
}
xtermDone := make(chan bool, 1)
if *xterm {
cmd := exec.Command("xterm")
cmd.Dir = mountPoint
if err := cmd.Start(); err != nil {
log.Printf("Error starting xterm: %v", err)
} else {
go func() {
cmd.Wait()
xtermDone <- true
}()
defer cmd.Process.Kill()
}
}
if *open {
if runtime.GOOS == "darwin" {
go exec.Command("open", mountPoint).Run()
}
}
if *term {
if runtime.GOOS == "darwin" {
if osutil.DirExists("/Applications/iTerm.app/") {
go exec.Command("open", "-a", "iTerm", mountPoint).Run()
} else {
log.Printf("TODO: iTerm not installed. Figure out how to open with Terminal.app instead.")
}
}
}
signal.Notify(sigc, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
doneServe := make(chan error, 1)
go func() {
doneServe <- fusefs.Serve(conn, camfs)
}()
quitKey := make(chan bool, 1)
go awaitQuitKey(quitKey)
select {
case err := <-doneServe:
log.Printf("conn.Serve returned %v", err)
case sig := <-sigc:
log.Printf("Signal %s received, shutting down.", sig)
case <-quitKey:
log.Printf("Quit key pressed. Shutting down.")
case <-xtermDone:
log.Printf("xterm done")
}
time.AfterFunc(2*time.Second, func() {
os.Exit(1)
})
log.Printf("Unmounting...")
err = fs.Unmount(mountPoint)
log.Printf("Unmount = %v", err)
log.Printf("cammount FUSE process ending.")
}
func awaitQuitKey(done chan<- bool) {
var buf [1]byte
for {
_, err := os.Stdin.Read(buf[:])
if err != nil {
return
}
if buf[0] == 'q' {
done <- true
return
}
}
}