2013-08-03 05:34:46 +00:00
|
|
|
// +build linux darwin
|
|
|
|
|
2011-03-20 17:25:17 +00:00
|
|
|
/*
|
|
|
|
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"
|
2013-12-17 04:57:43 +00:00
|
|
|
"io/ioutil"
|
2012-03-19 06:54:20 +00:00
|
|
|
"log"
|
2013-06-26 13:19:14 +00:00
|
|
|
"net/http"
|
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"
|
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
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/cacher"
|
|
|
|
"camlistore.org/pkg/client"
|
|
|
|
"camlistore.org/pkg/fs"
|
2014-01-09 19:32:45 +00:00
|
|
|
"camlistore.org/pkg/search"
|
2012-03-19 06:54:20 +00:00
|
|
|
"camlistore.org/third_party/code.google.com/p/rsc/fuse"
|
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.")
|
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() {
|
2014-01-09 19:32:45 +00:00
|
|
|
fmt.Fprint(os.Stderr, "usage: cammount [opts] [<mountpoint> [<root-blobref>|<share URL>|<root-name>]]\n")
|
2013-02-27 19:17:30 +00:00
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
mountPoint, err = ioutil.TempDir("", "cammount")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-01-17 03:52:11 +00:00
|
|
|
log.Printf("No mount point given. Using: %s", mountPoint)
|
2013-12-17 04:57:43 +00:00
|
|
|
defer os.Remove(mountPoint)
|
|
|
|
}
|
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
|
|
|
|
cl, root, err = client.NewFromShareRoot(rootArg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cl = client.NewOrFail() // automatic from flags
|
2014-01-09 19:32:45 +00:00
|
|
|
cl.SetHTTPClient(&http.Client{Transport: cl.TransportForConfig(nil)})
|
|
|
|
|
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}
|
|
|
|
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)
|
|
|
|
}
|
2013-02-27 19:17:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-03 20:50:06 +00:00
|
|
|
} else {
|
|
|
|
cl = client.NewOrFail() // automatic from flags
|
2013-06-26 13:19:14 +00:00
|
|
|
cl.SetHTTPClient(&http.Client{Transport: cl.TransportForConfig(nil)})
|
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 {
|
2013-07-20 17:18:18 +00:00
|
|
|
fuse.Debugf = log.Printf
|
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)
|
|
|
|
|
2013-07-15 11:29:00 +00:00
|
|
|
conn, err = fuse.Mount(mountPoint)
|
2011-03-20 17:25:17 +00:00
|
|
|
if err != nil {
|
2013-07-10 09:37:31 +00:00
|
|
|
if err.Error() == "cannot find load_fusefs" && runtime.GOOS == "darwin" {
|
|
|
|
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" {
|
|
|
|
cmd := exec.Command("open", mountPoint)
|
|
|
|
go cmd.Run()
|
|
|
|
}
|
|
|
|
}
|
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() {
|
|
|
|
doneServe <- conn.Serve(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...")
|
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)
|
|
|
|
|
2013-07-21 19:26:05 +00:00
|
|
|
log.Printf("cammount 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' {
|
|
|
|
done <- true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|