2013-07-05 17:41:07 +00:00
|
|
|
// +build linux darwin
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2013-07-08 01:52:14 +00:00
|
|
|
// Package fs implements a FUSE filesystem for Camlistore and is
|
|
|
|
// used by the cammount binary.
|
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
|
|
|
package fs // import "perkeep.org/pkg/fs"
|
2011-03-23 03:08:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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-04-28 07:55:46 +00:00
|
|
|
"sync"
|
2012-04-30 09:43:06 +00:00
|
|
|
"time"
|
2011-03-23 03:08:53 +00:00
|
|
|
|
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/internal/lru"
|
|
|
|
"perkeep.org/pkg/blob"
|
|
|
|
"perkeep.org/pkg/client"
|
|
|
|
"perkeep.org/pkg/schema"
|
2012-03-19 06:14:34 +00:00
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
|
|
|
"golang.org/x/net/context"
|
2011-03-23 03:08:53 +00:00
|
|
|
)
|
|
|
|
|
2012-04-30 09:43:06 +00:00
|
|
|
var serverStart = time.Now()
|
|
|
|
|
2012-03-19 06:14:34 +00:00
|
|
|
type CamliFileSystem struct {
|
2014-03-14 19:11:08 +00:00
|
|
|
fetcher blob.Fetcher
|
2013-02-07 05:47:01 +00:00
|
|
|
client *client.Client // or nil, if not doing search queries
|
2014-01-14 17:04:36 +00:00
|
|
|
root fusefs.Node
|
2011-03-23 05:11:27 +00:00
|
|
|
|
2012-04-29 00:03:07 +00:00
|
|
|
// IgnoreOwners, if true, collapses all file ownership to the
|
|
|
|
// uid/gid running the fuse filesystem, and sets all the
|
|
|
|
// permissions to 0600/0700.
|
|
|
|
IgnoreOwners bool
|
|
|
|
|
2013-01-22 18:20:34 +00:00
|
|
|
blobToSchema *lru.Cache // ~map[blobstring]*schema.Blob
|
2013-08-04 02:54:30 +00:00
|
|
|
nameToBlob *lru.Cache // ~map[string]blob.Ref
|
2011-03-26 04:36:52 +00:00
|
|
|
nameToAttr *lru.Cache // ~map[string]*fuse.Attr
|
2011-03-23 03:08:53 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
var _ fusefs.FS = (*CamliFileSystem)(nil)
|
2012-03-19 06:14:34 +00:00
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func newCamliFileSystem(fetcher blob.Fetcher) *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?
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-09 19:32:45 +00:00
|
|
|
// NewDefaultCamliFileSystem returns a filesystem with a generic base, from which
|
|
|
|
// users can navigate by blobref, tag, date, etc.
|
2014-03-14 19:11:08 +00:00
|
|
|
func NewDefaultCamliFileSystem(client *client.Client, fetcher blob.Fetcher) *CamliFileSystem {
|
2013-02-07 05:47:01 +00:00
|
|
|
if client == nil || fetcher == nil {
|
|
|
|
panic("nil argument")
|
|
|
|
}
|
2012-04-29 04:29:51 +00:00
|
|
|
fs := newCamliFileSystem(fetcher)
|
|
|
|
fs.root = &root{fs: fs} // root.go
|
2013-02-07 05:47:01 +00:00
|
|
|
fs.client = client
|
2012-04-29 04:29:51 +00:00
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
2014-01-09 19:32:45 +00:00
|
|
|
// NewRootedCamliFileSystem returns a CamliFileSystem with a node based on a blobref
|
|
|
|
// as its base.
|
2014-03-14 19:11:08 +00:00
|
|
|
func NewRootedCamliFileSystem(cli *client.Client, fetcher blob.Fetcher, root blob.Ref) (*CamliFileSystem, error) {
|
2012-04-29 04:29:51 +00:00
|
|
|
fs := newCamliFileSystem(fetcher)
|
2014-01-09 19:32:45 +00:00
|
|
|
fs.client = cli
|
|
|
|
|
|
|
|
n, err := fs.newNodeFromBlobRef(root)
|
2012-04-29 04:29:51 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-01-09 19:32:45 +00:00
|
|
|
|
2012-04-29 04:29:51 +00:00
|
|
|
fs.root = n
|
2014-01-09 19:32:45 +00:00
|
|
|
|
2012-04-29 04:29:51 +00:00
|
|
|
return fs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// node implements fuse.Node with a read-only Camli "file" or
|
|
|
|
// "directory" blob.
|
2012-04-27 02:44:04 +00:00
|
|
|
type node struct {
|
|
|
|
fs *CamliFileSystem
|
2013-08-04 02:54:30 +00:00
|
|
|
blobref blob.Ref
|
2012-04-28 07:55:46 +00:00
|
|
|
|
2013-02-18 19:02:26 +00:00
|
|
|
pnodeModTime time.Time // optionally set by recent.go; modtime of permanode
|
|
|
|
|
2012-04-28 11:15:45 +00:00
|
|
|
dmu sync.Mutex // guards dirents. acquire before mu.
|
|
|
|
dirents []fuse.Dirent // nil until populated once
|
|
|
|
|
2012-04-28 07:55:46 +00:00
|
|
|
mu sync.Mutex // guards rest
|
2012-04-28 01:57:24 +00:00
|
|
|
attr fuse.Attr
|
2013-01-22 18:20:34 +00:00
|
|
|
meta *schema.Blob
|
2013-08-04 02:54:30 +00:00
|
|
|
lookMap map[string]blob.Ref
|
2012-03-19 06:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.Node = (*node)(nil)
|
|
|
|
|
|
|
|
func (n *node) Attr(ctx context.Context, a *fuse.Attr) error {
|
|
|
|
if _, err := n.schema(); err != nil {
|
|
|
|
return err
|
2012-04-28 07:55:46 +00:00
|
|
|
}
|
2016-04-07 19:19:05 +00:00
|
|
|
*a = n.attr
|
|
|
|
return nil
|
2012-04-27 08:46:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (n *node) addLookupEntry(name string, ref blob.Ref) {
|
2012-04-28 07:55:46 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
if n.lookMap == nil {
|
2013-08-04 02:54:30 +00:00
|
|
|
n.lookMap = make(map[string]blob.Ref)
|
2012-04-28 07:55:46 +00:00
|
|
|
}
|
|
|
|
n.lookMap[name] = ref
|
|
|
|
}
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.NodeStringLookuper = (*node)(nil)
|
|
|
|
|
|
|
|
func (n *node) Lookup(ctx context.Context, name string) (fusefs.Node, error) {
|
2012-04-28 01:57:24 +00:00
|
|
|
if name == ".quitquitquit" {
|
|
|
|
// TODO: only in dev mode
|
|
|
|
log.Fatalf("Shutting down due to .quitquitquit lookup.")
|
|
|
|
}
|
2012-04-28 07:55:46 +00:00
|
|
|
|
2012-04-28 11:15:45 +00:00
|
|
|
// If we haven't done Readdir yet (dirents isn't set), then force a Readdir
|
|
|
|
// call to populate lookMap.
|
|
|
|
n.dmu.Lock()
|
|
|
|
loaded := n.dirents != nil
|
|
|
|
n.dmu.Unlock()
|
|
|
|
if !loaded {
|
2016-04-07 19:19:05 +00:00
|
|
|
n.ReadDirAll(nil)
|
2012-04-28 11:15:45 +00:00
|
|
|
}
|
|
|
|
|
2012-04-28 07:55:46 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
ref, ok := n.lookMap[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
|
|
|
return &node{fs: n.fs, blobref: ref}, nil
|
|
|
|
}
|
|
|
|
|
2013-01-22 18:20:34 +00:00
|
|
|
func (n *node) schema() (*schema.Blob, error) {
|
2012-04-28 07:55:46 +00:00
|
|
|
// TODO: use singleflight library here instead of a lock?
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2013-01-22 18:20:34 +00:00
|
|
|
if n.meta != nil {
|
|
|
|
return n.meta, nil
|
2012-04-28 07:55:46 +00:00
|
|
|
}
|
2013-01-22 18:20:34 +00:00
|
|
|
blob, err := n.fs.fetchSchemaMeta(n.blobref)
|
2012-04-28 07:55:46 +00:00
|
|
|
if err == nil {
|
2013-01-22 18:20:34 +00:00
|
|
|
n.meta = blob
|
2012-04-28 07:55:46 +00:00
|
|
|
n.populateAttr()
|
|
|
|
}
|
2013-01-22 18:20:34 +00:00
|
|
|
return blob, err
|
2012-04-28 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func isWriteFlags(flags fuse.OpenFlags) bool {
|
|
|
|
// TODO read/writeness are not flags, use O_ACCMODE
|
|
|
|
return flags&fuse.OpenFlags(os.O_WRONLY|os.O_RDWR|os.O_APPEND|os.O_CREATE) != 0
|
2013-12-26 21:55:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.NodeOpener = (*node)(nil)
|
|
|
|
|
|
|
|
func (n *node) Open(ctx context.Context, req *fuse.OpenRequest, res *fuse.OpenResponse) (fusefs.Handle, error) {
|
2012-04-28 11:15:45 +00:00
|
|
|
log.Printf("CAMLI Open on %v: %#v", n.blobref, req)
|
2013-12-26 21:55:27 +00:00
|
|
|
if isWriteFlags(req.Flags) {
|
2013-12-22 21:06:39 +00:00
|
|
|
return nil, fuse.EPERM
|
|
|
|
}
|
2012-04-28 11:47:02 +00:00
|
|
|
ss, err := n.schema()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("open of %v: %v", n.blobref, err)
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
2013-01-22 18:20:34 +00:00
|
|
|
if ss.Type() == "directory" {
|
2012-04-28 11:47:02 +00:00
|
|
|
return n, nil
|
|
|
|
}
|
2012-12-26 18:02:22 +00:00
|
|
|
fr, err := ss.NewFileReader(n.fs.fetcher)
|
|
|
|
if err != nil {
|
|
|
|
// Will only happen if ss.Type != "file" or "bytes"
|
|
|
|
log.Printf("NewFileReader(%s) = %v", n.blobref, err)
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
return &nodeReader{n: n, fr: fr}, nil
|
2012-04-28 11:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type nodeReader struct {
|
2012-04-28 11:47:02 +00:00
|
|
|
n *node
|
2012-12-26 18:02:22 +00:00
|
|
|
fr *schema.FileReader
|
2012-04-28 11:15:45 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.HandleReader = (*nodeReader)(nil)
|
|
|
|
|
|
|
|
func (nr *nodeReader) Read(ctx context.Context, req *fuse.ReadRequest, res *fuse.ReadResponse) error {
|
2012-04-28 11:47:02 +00:00
|
|
|
log.Printf("CAMLI nodeReader READ on %v: %#v", nr.n.blobref, req)
|
2012-12-26 18:02:22 +00:00
|
|
|
if req.Offset >= nr.fr.Size() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
size := req.Size
|
2013-02-07 05:57:07 +00:00
|
|
|
if int64(size)+req.Offset >= nr.fr.Size() {
|
2012-12-26 18:02:22 +00:00
|
|
|
size -= int((int64(size) + req.Offset) - nr.fr.Size())
|
2012-04-28 11:47:02 +00:00
|
|
|
}
|
2012-12-26 18:02:22 +00:00
|
|
|
buf := make([]byte, size)
|
|
|
|
n, err := nr.fr.ReadAt(buf, req.Offset)
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2012-04-28 11:47:02 +00:00
|
|
|
log.Printf("camli read on %v at %d: %v", nr.n.blobref, req.Offset, err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
res.Data = buf[:n]
|
|
|
|
return nil
|
2012-04-28 11:15:45 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.HandleReleaser = (*nodeReader)(nil)
|
|
|
|
|
|
|
|
func (nr *nodeReader) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
|
2012-12-26 18:02:22 +00:00
|
|
|
log.Printf("CAMLI nodeReader RELEASE on %v", nr.n.blobref)
|
|
|
|
nr.fr.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.HandleReadDirAller = (*node)(nil)
|
|
|
|
|
|
|
|
func (n *node) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
|
|
|
|
log.Printf("CAMLI ReadDirAll on %v", n.blobref)
|
2012-04-28 11:15:45 +00:00
|
|
|
n.dmu.Lock()
|
|
|
|
defer n.dmu.Unlock()
|
|
|
|
if n.dirents != nil {
|
|
|
|
return n.dirents, nil
|
|
|
|
}
|
2012-04-28 07:55:46 +00:00
|
|
|
|
|
|
|
ss, err := n.schema()
|
|
|
|
if err != nil {
|
2016-04-07 19:19:05 +00:00
|
|
|
log.Printf("camli.ReadDirAll error on %v: %v", n.blobref, err)
|
2012-04-28 07:55:46 +00:00
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
2013-02-18 18:17:38 +00:00
|
|
|
dr, err := schema.NewDirReader(n.fs.fetcher, ss.BlobRef())
|
2012-04-28 07:55:46 +00:00
|
|
|
if err != nil {
|
2016-04-07 19:19:05 +00:00
|
|
|
log.Printf("camli.ReadDirAll error on %v: %v", n.blobref, err)
|
2012-04-28 07:55:46 +00:00
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
2013-02-18 18:17:38 +00:00
|
|
|
schemaEnts, err := dr.Readdir(-1)
|
|
|
|
if err != nil {
|
2016-04-07 19:19:05 +00:00
|
|
|
log.Printf("camli.ReadDirAll error on %v: %v", n.blobref, err)
|
2012-04-28 07:55:46 +00:00
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
2012-04-28 11:15:45 +00:00
|
|
|
n.dirents = make([]fuse.Dirent, 0)
|
2013-02-18 18:17:38 +00:00
|
|
|
for _, sent := range schemaEnts {
|
|
|
|
if name := sent.FileName(); name != "" {
|
|
|
|
n.addLookupEntry(name, sent.BlobRef())
|
|
|
|
n.dirents = append(n.dirents, fuse.Dirent{Name: name})
|
2012-04-28 07:55:46 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-28 11:15:45 +00:00
|
|
|
return n.dirents, nil
|
2012-04-28 01:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-04-28 07:55:46 +00:00
|
|
|
// populateAttr should only be called once n.ss is known to be set and
|
|
|
|
// non-nil
|
|
|
|
func (n *node) populateAttr() error {
|
2013-01-22 18:20:34 +00:00
|
|
|
meta := n.meta
|
2012-04-27 08:46:31 +00:00
|
|
|
|
2013-01-22 18:20:34 +00:00
|
|
|
n.attr.Mode = meta.FileMode()
|
2012-04-29 00:03:07 +00:00
|
|
|
|
|
|
|
if n.fs.IgnoreOwners {
|
|
|
|
n.attr.Uid = uint32(os.Getuid())
|
|
|
|
n.attr.Gid = uint32(os.Getgid())
|
|
|
|
executeBit := n.attr.Mode & 0100
|
2014-11-09 05:53:23 +00:00
|
|
|
n.attr.Mode = (n.attr.Mode ^ n.attr.Mode.Perm()) | 0400 | executeBit
|
2012-04-29 00:03:07 +00:00
|
|
|
} else {
|
2013-01-22 18:20:34 +00:00
|
|
|
n.attr.Uid = uint32(meta.MapUid())
|
|
|
|
n.attr.Gid = uint32(meta.MapGid())
|
2012-04-29 00:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: inode?
|
|
|
|
|
2013-02-18 19:02:26 +00:00
|
|
|
if mt := meta.ModTime(); !mt.IsZero() {
|
|
|
|
n.attr.Mtime = mt
|
|
|
|
} else {
|
|
|
|
n.attr.Mtime = n.pnodeModTime
|
|
|
|
}
|
2012-04-28 08:35:45 +00:00
|
|
|
|
2013-01-22 18:20:34 +00:00
|
|
|
switch meta.Type() {
|
2012-04-27 08:46:31 +00:00
|
|
|
case "file":
|
2013-01-22 18:20:34 +00:00
|
|
|
n.attr.Size = uint64(meta.PartsSize())
|
2012-04-28 08:35:45 +00:00
|
|
|
n.attr.Blocks = 0 // TODO: set?
|
2013-02-18 19:02:26 +00:00
|
|
|
n.attr.Mode |= 0400
|
2012-04-29 00:03:07 +00:00
|
|
|
case "directory":
|
2013-02-18 19:02:26 +00:00
|
|
|
n.attr.Mode |= 0500
|
2012-04-28 08:35:45 +00:00
|
|
|
case "symlink":
|
2013-02-18 19:02:26 +00:00
|
|
|
n.attr.Mode |= 0400
|
2012-04-27 08:46:31 +00:00
|
|
|
default:
|
2013-01-22 18:20:34 +00:00
|
|
|
log.Printf("unknown attr ss.Type %q in populateAttr", meta.Type())
|
2012-04-27 08:46:31 +00:00
|
|
|
}
|
|
|
|
return nil
|
2012-04-27 02:44:04 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
func (fs *CamliFileSystem) Root() (fusefs.Node, error) {
|
2012-04-29 04:29:51 +00:00
|
|
|
return fs.root, nil
|
2011-03-23 05:11:27 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.FSStatfser = (*CamliFileSystem)(nil)
|
|
|
|
|
|
|
|
func (fs *CamliFileSystem) Statfs(ctx context.Context, req *fuse.StatfsRequest, res *fuse.StatfsResponse) error {
|
2012-04-28 01:57:24 +00:00
|
|
|
// Make some stuff up, just to see if it makes "lsof" happy.
|
|
|
|
res.Blocks = 1 << 35
|
|
|
|
res.Bfree = 1 << 34
|
2013-07-16 03:43:05 +00:00
|
|
|
res.Bavail = 1 << 34
|
2012-04-28 01:57:24 +00:00
|
|
|
res.Files = 1 << 29
|
|
|
|
res.Ffree = 1 << 28
|
|
|
|
res.Namelen = 2048
|
|
|
|
res.Bsize = 1024
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
2013-08-04 02:54:30 +00:00
|
|
|
func (fs *CamliFileSystem) fetchSchemaMeta(br blob.Ref) (*schema.Blob, error) {
|
2011-03-26 04:21:24 +00:00
|
|
|
blobStr := br.String()
|
2013-01-22 18:20:34 +00:00
|
|
|
if blob, ok := fs.blobToSchema.Get(blobStr); ok {
|
|
|
|
return blob.(*schema.Blob), nil
|
2011-03-26 04:21:24 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
rc, _, err := fs.fetcher.Fetch(br)
|
2011-03-23 05:11:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-03-23 03:35:04 +00:00
|
|
|
}
|
2014-03-14 19:11:08 +00:00
|
|
|
defer rc.Close()
|
|
|
|
blob, err := schema.BlobFromReader(br, rc)
|
2011-03-25 02:20:22 +00:00
|
|
|
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
|
|
|
}
|
2013-01-22 18:20:34 +00:00
|
|
|
if blob.Type() == "" {
|
2011-03-24 05:04:50 +00:00
|
|
|
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
|
|
|
}
|
2013-01-22 18:20:34 +00:00
|
|
|
fs.blobToSchema.Add(blobStr, blob)
|
|
|
|
return blob, nil
|
2011-03-23 05:11:27 +00:00
|
|
|
}
|
2013-02-07 05:57:07 +00:00
|
|
|
|
2014-01-09 19:32:45 +00:00
|
|
|
// consolated logic for determining a node to mount based on an arbitrary blobref
|
2014-01-14 17:04:36 +00:00
|
|
|
func (fs *CamliFileSystem) newNodeFromBlobRef(root blob.Ref) (fusefs.Node, error) {
|
2014-01-09 19:32:45 +00:00
|
|
|
blob, err := fs.fetchSchemaMeta(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch blob.Type() {
|
|
|
|
case "directory":
|
|
|
|
n := &node{fs: fs, blobref: root, meta: blob}
|
|
|
|
n.populateAttr()
|
|
|
|
return n, nil
|
|
|
|
|
|
|
|
case "permanode":
|
|
|
|
// other mutDirs listed in the default fileystem have names and are displayed
|
|
|
|
return &mutDir{fs: fs, permanode: root, name: "-"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Blobref must be of a directory or permanode got a %v", blob.Type())
|
|
|
|
}
|
|
|
|
|
2016-05-03 00:52:27 +00:00
|
|
|
type notImplementDirNode struct{}
|
2013-02-07 05:57:07 +00:00
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.Node = (*notImplementDirNode)(nil)
|
|
|
|
|
|
|
|
func (notImplementDirNode) Attr(ctx context.Context, a *fuse.Attr) error {
|
|
|
|
a.Mode = os.ModeDir | 0000
|
|
|
|
a.Uid = uint32(os.Getuid())
|
|
|
|
a.Gid = uint32(os.Getgid())
|
|
|
|
return nil
|
2013-02-07 05:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type staticFileNode string
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.Node = (*notImplementDirNode)(nil)
|
|
|
|
|
|
|
|
func (s staticFileNode) Attr(ctx context.Context, a *fuse.Attr) error {
|
|
|
|
a.Mode = 0400
|
|
|
|
a.Uid = uint32(os.Getuid())
|
|
|
|
a.Gid = uint32(os.Getgid())
|
|
|
|
a.Size = uint64(len(s))
|
|
|
|
a.Mtime = serverStart
|
|
|
|
a.Ctime = serverStart
|
|
|
|
a.Crtime = serverStart
|
|
|
|
return nil
|
2013-02-07 05:57:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 19:19:05 +00:00
|
|
|
var _ fusefs.HandleReader = (*staticFileNode)(nil)
|
|
|
|
|
|
|
|
func (s staticFileNode) Read(ctx context.Context, req *fuse.ReadRequest, res *fuse.ReadResponse) error {
|
2013-02-07 05:57:07 +00:00
|
|
|
if req.Offset > int64(len(s)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s = s[req.Offset:]
|
|
|
|
size := req.Size
|
|
|
|
if size > len(s) {
|
|
|
|
size = len(s)
|
|
|
|
}
|
|
|
|
res.Data = make([]byte, size)
|
|
|
|
copy(res.Data, s)
|
|
|
|
return nil
|
|
|
|
}
|