2013-08-03 05:34:46 +00:00
|
|
|
// +build linux darwin
|
|
|
|
|
2011-03-20 17:25:17 +00:00
|
|
|
/*
|
2018-01-04 00:52:49 +00:00
|
|
|
Copyright 2011 The Perkeep Authors
|
2011-03-20 17:25:17 +00:00
|
|
|
|
|
|
|
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 (
|
2018-01-16 23:03:16 +00:00
|
|
|
"context"
|
2011-03-20 17:25:17 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2013-12-17 04:57:43 +00:00
|
|
|
"io/ioutil"
|
2012-03-19 06:54:20 +00:00
|
|
|
"log"
|
2011-03-20 17:25:17 +00:00
|
|
|
"os"
|
2013-07-10 11:27:54 +00:00
|
|
|
"os/exec"
|
2012-12-26 18:02:22 +00:00
|
|
|
"os/signal"
|
2014-11-30 03:06:54 +00:00
|
|
|
"path/filepath"
|
2013-07-10 09:37:31 +00:00
|
|
|
"runtime"
|
2013-02-27 19:17:30 +00:00
|
|
|
"strings"
|
2012-12-26 18:02:22 +00:00
|
|
|
"syscall"
|
2013-07-10 11:27:54 +00:00
|
|
|
"time"
|
2011-03-20 17:25:17 +00:00
|
|
|
|
2018-01-03 05:03:30 +00:00
|
|
|
"perkeep.org/internal/osutil"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/blob"
|
|
|
|
"perkeep.org/pkg/cacher"
|
|
|
|
"perkeep.org/pkg/client"
|
|
|
|
"perkeep.org/pkg/cmdmain"
|
|
|
|
"perkeep.org/pkg/fs"
|
|
|
|
"perkeep.org/pkg/search"
|
2016-04-07 19:19:05 +00:00
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2011-03-20 17:25:17 +00:00
|
|
|
)
|
|
|
|
|
2013-07-10 11:27:54 +00:00
|
|
|
var (
|
2013-07-15 11:29:00 +00:00
|
|
|
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.")
|
2014-01-31 11:20:50 +00:00
|
|
|
term = flag.Bool("term", false, "Open a terminal window. Doesn't shut down when exited. Mostly for demos.")
|
2013-12-17 04:57:43 +00:00
|
|
|
open = flag.Bool("open", false, "Open a GUI window")
|
2013-07-10 11:27:54 +00:00
|
|
|
)
|
|
|
|
|
2013-02-27 19:17:30 +00:00
|
|
|
func usage() {
|
2018-01-08 03:57:49 +00:00
|
|
|
fmt.Fprint(os.Stderr, "usage: pk-mount [opts] [<mountpoint> [<root-blobref>|<share URL>|<root-name>]]\n")
|
2013-02-27 19:17:30 +00:00
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2016-11-17 23:01:43 +00:00
|
|
|
func init() {
|
|
|
|
// So we can simply use log.Printf and log.Fatalf.
|
|
|
|
// For logging that depends on verbosity (cmdmain.FlagVerbose), use cmdmain.Logf/Printf.
|
|
|
|
log.SetOutput(cmdmain.Stderr)
|
|
|
|
}
|
|
|
|
|
2018-01-16 23:03:16 +00:00
|
|
|
var ctxbg = context.Background()
|
|
|
|
|
2011-03-20 17:25:17 +00:00
|
|
|
func main() {
|
2013-07-15 11:29:00 +00:00
|
|
|
var conn *fuse.Conn
|
|
|
|
|
2011-03-20 17:25:17 +00:00
|
|
|
// Scans the arg list and sets up flags
|
2011-10-11 00:38:18 +00:00
|
|
|
client.AddFlags()
|
2014-01-17 03:52:11 +00:00
|
|
|
flag.Usage = usage
|
2011-03-20 17:25:17 +00:00
|
|
|
flag.Parse()
|
2011-03-25 04:07:49 +00:00
|
|
|
|
2015-12-16 16:14:51 +00:00
|
|
|
if *cmdmain.FlagLegal {
|
|
|
|
cmdmain.PrintLicenses()
|
2014-07-03 20:03:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-08 20:09:00 +00:00
|
|
|
if *cmdmain.FlagHelp {
|
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:27:54 +00:00
|
|
|
narg := flag.NArg()
|
2013-12-17 04:57:43 +00:00
|
|
|
if narg > 2 {
|
2013-02-27 19:17:30 +00:00
|
|
|
usage()
|
2011-03-20 17:25:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-17 04:57:43 +00:00
|
|
|
var mountPoint string
|
|
|
|
var err error
|
|
|
|
if narg > 0 {
|
|
|
|
mountPoint = flag.Arg(0)
|
|
|
|
} else {
|
2018-04-25 22:28:27 +00:00
|
|
|
if fi, err := os.Stat("/pk"); err == nil && fi.IsDir() {
|
|
|
|
log.Printf("no mount point given; using /pk")
|
|
|
|
mountPoint = "/pk"
|
|
|
|
} else {
|
|
|
|
mountPoint, err = ioutil.TempDir("", "pk-mount")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Printf("no mount point given and recommended directory /pk doesn't exist; using temp directory %s", mountPoint)
|
|
|
|
defer os.Remove(mountPoint)
|
2013-12-17 04:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-10 11:27:54 +00:00
|
|
|
|
|
|
|
errorf := func(msg string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, msg, args...)
|
|
|
|
fmt.Fprint(os.Stderr, "\n")
|
2013-02-27 19:17:30 +00:00
|
|
|
usage()
|
2011-03-25 04:07:49 +00:00
|
|
|
}
|
|
|
|
|
2013-02-27 19:17:30 +00:00
|
|
|
var (
|
|
|
|
cl *client.Client
|
2013-08-04 02:54:30 +00:00
|
|
|
root blob.Ref // nil if only one arg
|
2013-02-27 19:17:30 +00:00
|
|
|
camfs *fs.CamliFileSystem
|
|
|
|
)
|
2013-07-10 11:27:54 +00:00
|
|
|
if narg == 2 {
|
2013-02-27 19:17:30 +00:00
|
|
|
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
|
2018-01-16 23:03:16 +00:00
|
|
|
cl, root, err = client.NewFromShareRoot(ctxbg, rootArg)
|
2013-02-27 19:17:30 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cl = client.NewOrFail() // automatic from flags
|
2014-01-09 19:32:45 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
var ok bool
|
|
|
|
root, ok = blob.Parse(rootArg)
|
2014-01-09 19:32:45 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
if !ok {
|
2014-01-09 19:32:45 +00:00
|
|
|
// not a blobref, check for root name instead
|
|
|
|
req := &search.WithAttrRequest{N: 1, Attr: "camliRoot", Value: rootArg}
|
2018-01-16 23:03:16 +00:00
|
|
|
wres, err := cl.GetPermanodesWithAttr(ctxbg, req)
|
2014-01-09 19:32:45 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-02-27 19:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-03 20:50:06 +00:00
|
|
|
} else {
|
|
|
|
cl = client.NewOrFail() // automatic from flags
|
2013-02-27 19:17:30 +00:00
|
|
|
}
|
2012-04-29 04:29:51 +00:00
|
|
|
|
2013-02-27 19:17:30 +00:00
|
|
|
diskCacheFetcher, err := cacher.NewDiskCache(cl)
|
2011-03-25 04:07:49 +00:00
|
|
|
if err != nil {
|
2013-02-27 19:17:30 +00:00
|
|
|
log.Fatalf("Error setting up local disk cache: %v", err)
|
2011-03-25 04:07:49 +00:00
|
|
|
}
|
2013-01-20 17:52:03 +00:00
|
|
|
defer diskCacheFetcher.Clean()
|
2013-08-04 02:54:30 +00:00
|
|
|
if root.Valid() {
|
2012-04-29 04:29:51 +00:00
|
|
|
var err error
|
2014-01-09 19:32:45 +00:00
|
|
|
camfs, err = fs.NewRootedCamliFileSystem(cl, diskCacheFetcher, root)
|
2012-04-29 04:29:51 +00:00
|
|
|
if err != nil {
|
2013-02-27 19:17:30 +00:00
|
|
|
log.Fatalf("Error creating root with %v: %v", root, err)
|
2012-04-29 04:29:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-01-09 19:32:45 +00:00
|
|
|
camfs = fs.NewDefaultCamliFileSystem(cl, diskCacheFetcher)
|
2012-04-29 04:29:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-19 06:54:20 +00:00
|
|
|
if *debug {
|
2014-01-14 17:04:36 +00:00
|
|
|
fuse.Debug = func(msg interface{}) { log.Print(msg) }
|
2012-03-19 06:54:20 +00:00
|
|
|
// TODO: set fs's logger
|
|
|
|
}
|
2011-03-20 17:25:17 +00:00
|
|
|
|
2012-12-26 18:02:22 +00:00
|
|
|
// This doesn't appear to work on OS X:
|
|
|
|
sigc := make(chan os.Signal, 1)
|
|
|
|
|
2014-11-30 03:06:54 +00:00
|
|
|
conn, err = fuse.Mount(mountPoint, fuse.VolumeName(filepath.Base(mountPoint)))
|
2011-03-20 17:25:17 +00:00
|
|
|
if err != nil {
|
2016-04-07 19:19:05 +00:00
|
|
|
if err == fuse.ErrOSXFUSENotFound {
|
2013-07-10 09:37:31 +00:00
|
|
|
log.Fatal("FUSE not available; install from http://osxfuse.github.io/")
|
|
|
|
}
|
2012-03-19 06:54:20 +00:00
|
|
|
log.Fatalf("Mount: %v", err)
|
2011-03-20 17:25:17 +00:00
|
|
|
}
|
2013-07-15 11:29:00 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}()
|
2013-07-20 17:23:39 +00:00
|
|
|
defer cmd.Process.Kill()
|
2013-07-15 11:29:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-17 04:57:43 +00:00
|
|
|
if *open {
|
|
|
|
if runtime.GOOS == "darwin" {
|
2014-01-31 11:20:50 +00:00
|
|
|
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.")
|
|
|
|
}
|
2013-12-17 04:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-15 11:29:00 +00:00
|
|
|
|
2013-10-15 13:29:22 +00:00
|
|
|
signal.Notify(sigc, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
|
2013-07-15 11:29:00 +00:00
|
|
|
|
|
|
|
doneServe := make(chan error, 1)
|
|
|
|
go func() {
|
2014-01-14 17:04:36 +00:00
|
|
|
doneServe <- fusefs.Serve(conn, camfs)
|
2013-07-15 11:29:00 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
quitKey := make(chan bool, 1)
|
|
|
|
go awaitQuitKey(quitKey)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-doneServe:
|
|
|
|
log.Printf("conn.Serve returned %v", err)
|
2014-12-27 00:52:24 +00:00
|
|
|
|
|
|
|
// check if the mount process has an error to report
|
|
|
|
<-conn.Ready
|
|
|
|
if err := conn.MountError; err != nil {
|
|
|
|
log.Printf("conn.MountError: %v", err)
|
|
|
|
}
|
2013-07-15 11:29:00 +00:00
|
|
|
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...")
|
2013-07-21 06:26:56 +00:00
|
|
|
err = fs.Unmount(mountPoint)
|
2013-07-15 11:29:00 +00:00
|
|
|
log.Printf("Unmount = %v", err)
|
|
|
|
|
2018-01-08 03:57:49 +00:00
|
|
|
log.Printf("pk-mount FUSE process ending.")
|
2013-07-15 11:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func awaitQuitKey(done chan<- bool) {
|
|
|
|
var buf [1]byte
|
|
|
|
for {
|
|
|
|
_, err := os.Stdin.Read(buf[:])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if buf[0] == 'q' {
|
2018-04-27 18:24:04 +00:00
|
|
|
if *debug {
|
|
|
|
stacks := make([]byte, 1<<20)
|
|
|
|
stacks = stacks[:runtime.Stack(stacks, true)]
|
|
|
|
os.Stderr.Write(stacks)
|
|
|
|
}
|
2013-07-15 11:29:00 +00:00
|
|
|
done <- true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|