2011-01-28 07:07:18 +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.
|
|
|
|
*/
|
|
|
|
|
2011-01-02 22:36:03 +00:00
|
|
|
// Usage:
|
|
|
|
//
|
|
|
|
// Writes to stdout by default:
|
|
|
|
// camget BLOBREF
|
|
|
|
//
|
|
|
|
// Like curl, lets you set output file/directory with -o:
|
|
|
|
// camget -o dir BLOBREF (if dir exists and is directory, BLOBREF must be a directory, and -f to overwrite any files)
|
|
|
|
// camget -o file BLOBREF
|
|
|
|
//
|
|
|
|
// Should be possible to get a directory JSON blob without recursively
|
|
|
|
// fetching an entire directory. Likewise with files. But default
|
|
|
|
// should be sensitive on the type of the listed blob. Maybe --blob
|
|
|
|
// just to get the blob? Seems consistent.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-01-02 04:23:39 +00:00
|
|
|
"bytes"
|
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
|
|
|
"errors"
|
2011-01-02 22:36:03 +00:00
|
|
|
"flag"
|
2012-01-02 04:23:39 +00:00
|
|
|
"fmt"
|
2011-01-15 01:25:37 +00:00
|
|
|
"io"
|
2011-01-02 22:36:03 +00:00
|
|
|
"log"
|
2011-01-15 01:25:37 +00:00
|
|
|
"os"
|
2012-01-02 05:07:58 +00:00
|
|
|
"path/filepath"
|
2011-01-26 05:33:19 +00:00
|
|
|
"strings"
|
2012-01-02 05:07:58 +00:00
|
|
|
"time"
|
2011-01-02 22:36:03 +00:00
|
|
|
|
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/blobref"
|
|
|
|
"camlistore.org/pkg/client"
|
|
|
|
"camlistore.org/pkg/index"
|
|
|
|
"camlistore.org/pkg/schema"
|
2011-10-11 00:38:18 +00:00
|
|
|
)
|
2011-01-02 22:36:03 +00:00
|
|
|
|
2011-10-11 00:38:18 +00:00
|
|
|
var (
|
|
|
|
flagVerbose = flag.Bool("verbose", false, "be verbose")
|
|
|
|
flagCheck = flag.Bool("check", false, "just check for the existence of listed blobs; returning 0 if all our present")
|
|
|
|
flagOutput = flag.String("o", "-", "Output file/directory to create. Use -f to overwrite.")
|
|
|
|
flagVia = flag.String("via", "", "Fetch the blob via the given comma-separated sharerefs (dev only).")
|
|
|
|
)
|
2011-01-02 22:36:03 +00:00
|
|
|
|
2012-01-02 04:23:39 +00:00
|
|
|
var viaRefs []*blobref.BlobRef
|
|
|
|
|
2011-01-02 22:36:03 +00:00
|
|
|
func main() {
|
2011-10-11 00:38:18 +00:00
|
|
|
client.AddFlags()
|
2011-01-02 22:36:03 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2012-01-02 04:23:39 +00:00
|
|
|
if len(*flagVia) > 0 {
|
|
|
|
vs := strings.Split(*flagVia, ",")
|
|
|
|
viaRefs = make([]*blobref.BlobRef, len(vs))
|
|
|
|
for i, sbr := range vs {
|
|
|
|
viaRefs[i] = blobref.Parse(sbr)
|
|
|
|
if viaRefs[i] == nil {
|
|
|
|
log.Fatalf("Invalid -via blobref: %q", sbr)
|
|
|
|
}
|
|
|
|
if *flagVerbose {
|
|
|
|
log.Printf("via: %s", sbr)
|
|
|
|
}
|
|
|
|
}
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 04:23:39 +00:00
|
|
|
cl := client.NewOrFail()
|
2011-01-15 01:25:37 +00:00
|
|
|
|
2011-01-02 22:36:03 +00:00
|
|
|
for n := 0; n < flag.NArg(); n++ {
|
|
|
|
arg := flag.Arg(n)
|
2011-01-26 05:33:19 +00:00
|
|
|
br := blobref.Parse(arg)
|
|
|
|
if br == nil {
|
2012-01-02 04:23:39 +00:00
|
|
|
log.Fatalf("Failed to parse argument %q as a blobref.", arg)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
2012-01-02 04:23:39 +00:00
|
|
|
if *flagCheck {
|
|
|
|
// TODO: do HEAD requests checking if the blobs exists.
|
|
|
|
log.Fatal("not implemented")
|
|
|
|
return
|
2011-01-26 05:33:19 +00:00
|
|
|
}
|
2012-01-02 04:23:39 +00:00
|
|
|
if *flagOutput == "-" {
|
|
|
|
rc, err := fetch(cl, br)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
if _, err := io.Copy(os.Stdout, rc); err != nil {
|
|
|
|
log.Fatalf("Failed reading %q: %v", br, err)
|
2011-01-26 05:33:19 +00:00
|
|
|
}
|
2012-01-02 04:23:39 +00:00
|
|
|
return
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
2012-01-02 04:23:39 +00:00
|
|
|
if err := smartFetch(cl, *flagOutput, br); err != nil {
|
|
|
|
log.Fatal(err)
|
2011-01-15 01:25:37 +00:00
|
|
|
}
|
2012-01-02 04:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func fetch(cl *client.Client, br *blobref.BlobRef) (r io.ReadCloser, err error) {
|
2012-01-02 04:23:39 +00:00
|
|
|
if *flagVerbose {
|
|
|
|
log.Printf("Fetching %s", br.String())
|
|
|
|
}
|
|
|
|
if len(viaRefs) > 0 {
|
|
|
|
r, _, err = cl.FetchVia(br, viaRefs)
|
|
|
|
} else {
|
|
|
|
r, _, err = cl.FetchStreaming(br)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to fetch %q: %s", br, err)
|
|
|
|
}
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
2012-01-02 05:07:58 +00:00
|
|
|
// A little less than the sniffer will take, so we don't truncate.
|
|
|
|
const sniffSize = 900 * 1024
|
2012-01-02 04:23:39 +00:00
|
|
|
|
|
|
|
// smartFetch the things that blobs point to, not just blobs. (wow)
|
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
|
|
|
func smartFetch(cl *client.Client, targ string, br *blobref.BlobRef) error {
|
2012-01-02 05:07:58 +00:00
|
|
|
if *flagVerbose {
|
|
|
|
log.Printf("Fetching %v into %q", br, targ)
|
|
|
|
}
|
|
|
|
|
2012-01-02 04:23:39 +00:00
|
|
|
rc, err := fetch(cl, br)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
|
|
|
|
sniffer := new(index.BlobSniffer)
|
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
|
|
|
_, err = io.CopyN(sniffer, rc, sniffSize)
|
|
|
|
if err != nil && err != io.EOF {
|
2012-01-02 04:23:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sniffer.Parse()
|
2012-01-02 05:07:58 +00:00
|
|
|
sc, ok := sniffer.Superset()
|
2012-01-02 04:23:39 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// opaque data - put it in a file
|
2012-01-02 05:07:58 +00:00
|
|
|
f, err := os.Create(targ)
|
2011-01-15 01:25:37 +00:00
|
|
|
if err != nil {
|
2012-01-02 05:07:58 +00:00
|
|
|
return fmt.Errorf("opaque: %v", err)
|
2011-01-15 01:25:37 +00:00
|
|
|
}
|
2012-01-02 04:23:39 +00:00
|
|
|
defer f.Close()
|
|
|
|
body, _ := sniffer.Body()
|
|
|
|
r := io.MultiReader(bytes.NewBuffer(body), rc)
|
|
|
|
_, err = io.Copy(f, r)
|
|
|
|
return err
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 05:07:58 +00:00
|
|
|
sc.BlobRef = br
|
|
|
|
|
|
|
|
switch sc.Type {
|
|
|
|
case "directory":
|
|
|
|
dir := filepath.Join(targ, sc.FileName)
|
2012-03-05 16:04:42 +00:00
|
|
|
if err := os.MkdirAll(dir, sc.FileMode()); err != nil {
|
2012-01-02 05:07:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := setFileMeta(dir, sc); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
entries := blobref.Parse(sc.Entries)
|
|
|
|
if entries == nil {
|
|
|
|
return fmt.Errorf("bad entries blobref: %v", sc.Entries)
|
|
|
|
}
|
|
|
|
return smartFetch(cl, dir, entries)
|
|
|
|
case "static-set":
|
|
|
|
// directory entries
|
|
|
|
for _, m := range sc.Members {
|
|
|
|
dref := blobref.Parse(m)
|
|
|
|
if dref == nil {
|
|
|
|
return fmt.Errorf("bad member blobref: %v", m)
|
|
|
|
}
|
|
|
|
if err := smartFetch(cl, targ, dref); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case "file":
|
|
|
|
name := filepath.Join(targ, sc.FileName)
|
|
|
|
f, err := os.Create(name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("file type: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
for _, p := range sc.Parts {
|
|
|
|
if p.BytesRef != nil {
|
|
|
|
panic("don't know how to handle BytesRef")
|
|
|
|
}
|
|
|
|
rc, err := fetch(cl, p.BlobRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(f, rc)
|
|
|
|
rc.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := setFileMeta(name, sc); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
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
|
|
|
return errors.New("unknown blob type: " + sc.Type)
|
2012-01-02 05:07:58 +00:00
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
2012-01-02 04:23:39 +00:00
|
|
|
|
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
|
|
|
func setFileMeta(name string, sc *schema.Superset) error {
|
2012-03-05 16:04:42 +00:00
|
|
|
if err := os.Chmod(name, sc.FileMode()); err != nil {
|
2012-01-02 05:07:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Chown(name, sc.UnixOwnerId, sc.UnixGroupId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t, err := time.Parse(time.RFC3339, sc.UnixMtime)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2012-03-05 16:04:42 +00:00
|
|
|
return os.Chtimes(name, t, t)
|
2011-01-02 22:36:03 +00:00
|
|
|
}
|