2011-03-23 03:08:53 +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-08-07 01:46:37 +00:00
|
|
|
package fs
|
2011-03-23 03:08:53 +00:00
|
|
|
|
|
|
|
import (
|
2011-03-24 22:33:15 +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
|
|
|
"encoding/json"
|
2011-03-23 03:08:53 +00:00
|
|
|
"fmt"
|
2011-03-24 22:33:15 +00:00
|
|
|
"io"
|
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
|
|
|
"log"
|
2011-03-23 03:08:53 +00:00
|
|
|
"os"
|
2012-03-19 06:14:34 +00:00
|
|
|
"syscall"
|
2012-04-27 08:46:31 +00:00
|
|
|
"time"
|
2011-03-23 03:08:53 +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/lru"
|
|
|
|
"camlistore.org/pkg/schema"
|
2012-03-19 06:14:34 +00:00
|
|
|
|
|
|
|
"camlistore.org/third_party/code.google.com/p/rsc/fuse"
|
2011-03-23 03:08:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = fmt.Println
|
|
|
|
var _ = log.Println
|
2012-03-19 06:54:20 +00:00
|
|
|
var _ = bytes.NewReader
|
2011-03-23 03:08:53 +00:00
|
|
|
|
2012-03-19 06:14:34 +00:00
|
|
|
var errNotDir = fuse.Errno(syscall.ENOTDIR)
|
2011-03-24 02:14:04 +00:00
|
|
|
|
2012-03-19 06:14:34 +00:00
|
|
|
type CamliFileSystem struct {
|
2011-06-04 15:56:03 +00:00
|
|
|
fetcher blobref.SeekFetcher
|
2011-03-25 02:20:22 +00:00
|
|
|
root *blobref.BlobRef
|
2011-03-23 05:11:27 +00:00
|
|
|
|
2011-03-26 04:21:24 +00:00
|
|
|
blobToSchema *lru.Cache // ~map[blobstring]*schema.Superset
|
|
|
|
nameToBlob *lru.Cache // ~map[string]*blobref.BlobRef
|
2011-03-26 04:36:52 +00:00
|
|
|
nameToAttr *lru.Cache // ~map[string]*fuse.Attr
|
2011-03-23 03:08:53 +00:00
|
|
|
}
|
|
|
|
|
2012-03-19 06:54:20 +00:00
|
|
|
type CamliFile struct {
|
|
|
|
fs *CamliFileSystem
|
|
|
|
blob *blobref.BlobRef
|
|
|
|
ss *schema.Superset
|
|
|
|
|
|
|
|
size uint64 // memoized
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-19 06:14:34 +00:00
|
|
|
var _ fuse.FS = (*CamliFileSystem)(nil)
|
|
|
|
|
2011-06-04 15:56:03 +00:00
|
|
|
func NewCamliFileSystem(fetcher blobref.SeekFetcher, root *blobref.BlobRef) *CamliFileSystem {
|
2011-03-23 05:11:27 +00:00
|
|
|
return &CamliFileSystem{
|
2011-03-26 04:21:24 +00:00
|
|
|
fetcher: fetcher,
|
|
|
|
blobToSchema: lru.New(1024), // arbitrary; TODO: tunable/smarter?
|
|
|
|
root: root,
|
|
|
|
nameToBlob: lru.New(1024), // arbitrary: TODO: tunable/smarter?
|
2011-03-26 04:36:52 +00:00
|
|
|
nameToAttr: lru.New(1024), // arbitrary: TODO: tunable/smarter?
|
2011-03-23 05:11:27 +00:00
|
|
|
}
|
2011-03-23 03:08:53 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 02:44:04 +00:00
|
|
|
type node struct {
|
|
|
|
fs *CamliFileSystem
|
|
|
|
blobref *blobref.BlobRef
|
2012-04-27 08:46:31 +00:00
|
|
|
attr fuse.Attr
|
2012-03-19 06:14:34 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 02:44:04 +00:00
|
|
|
func (n *node) Attr() (attr fuse.Attr) {
|
2012-04-27 08:46:31 +00:00
|
|
|
return n.attr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) populateAttr(ss *schema.Superset) error {
|
|
|
|
// TODO: common stuff:
|
|
|
|
// Uid uint32
|
|
|
|
// Gid uint32
|
|
|
|
// Mtime time.Time
|
|
|
|
// inode?
|
|
|
|
|
|
|
|
switch ss.Type {
|
|
|
|
case "directory":
|
|
|
|
n.attr.Mode = os.ModeDir
|
|
|
|
case "file":
|
|
|
|
n.attr.Size = ss.SumPartsSize()
|
|
|
|
n.attr.Blocks = 0 // TODO: set?
|
|
|
|
n.attr.Mtime = time.Time{} // TODO: set, for sure.
|
|
|
|
n.attr.Mode = 0
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("unknown attr type %q in populateAttr", ss.Type)
|
|
|
|
log.Print(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2012-04-27 02:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *CamliFileSystem) Root() (fuse.Node, fuse.Error) {
|
2012-04-27 08:46:31 +00:00
|
|
|
ss, err := fs.fetchSchemaSuperset(fs.root)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: map these to fuse.Error better
|
|
|
|
log.Printf("Error fetching root: %v", err)
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
n := &node{fs: fs, blobref: fs.root}
|
|
|
|
n.populateAttr(ss)
|
|
|
|
return n, nil
|
2011-03-23 05:11:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 05:04:50 +00:00
|
|
|
// Errors returned are:
|
2012-03-19 06:14:34 +00:00
|
|
|
// os.ErrNotExist -- blob not found
|
|
|
|
// os.ErrInvalid -- not JSON or a camli schema 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
|
|
|
func (fs *CamliFileSystem) fetchSchemaSuperset(br *blobref.BlobRef) (*schema.Superset, error) {
|
2011-03-26 04:21:24 +00:00
|
|
|
blobStr := br.String()
|
|
|
|
if ss, ok := fs.blobToSchema.Get(blobStr); ok {
|
|
|
|
return ss.(*schema.Superset), nil
|
|
|
|
}
|
|
|
|
|
2011-03-23 05:11:27 +00:00
|
|
|
rsc, _, err := fs.fetcher.Fetch(br)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-03-23 03:35:04 +00:00
|
|
|
}
|
2011-03-23 05:11:27 +00:00
|
|
|
defer rsc.Close()
|
|
|
|
jd := json.NewDecoder(rsc)
|
|
|
|
ss := new(schema.Superset)
|
2011-03-25 02:20:22 +00:00
|
|
|
err = jd.Decode(ss)
|
|
|
|
if err != nil {
|
2011-03-23 05:11:27 +00:00
|
|
|
log.Printf("Error parsing %s as schema blob: %v", br, err)
|
2012-03-19 06:14:34 +00:00
|
|
|
return nil, os.ErrInvalid
|
2011-03-25 02:20:22 +00:00
|
|
|
}
|
2011-03-24 05:04:50 +00:00
|
|
|
if ss.Type == "" {
|
|
|
|
log.Printf("blob %s is JSON but lacks camliType", br)
|
2012-03-19 06:14:34 +00:00
|
|
|
return nil, os.ErrInvalid
|
2011-03-24 05:04:50 +00:00
|
|
|
}
|
|
|
|
ss.BlobRef = br
|
2011-03-26 04:21:24 +00:00
|
|
|
fs.blobToSchema.Add(blobStr, ss)
|
2011-03-23 05:11:27 +00:00
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
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 (file *CamliFile) GetReader() (io.ReadCloser, error) {
|
2011-08-07 01:46:37 +00:00
|
|
|
return file.ss.NewFileReader(file.fs.fetcher)
|
|
|
|
}
|