2013-07-10 11:10:48 +00:00
|
|
|
// +build linux darwin
|
|
|
|
|
|
|
|
/*
|
2013-08-02 19:57:16 +00:00
|
|
|
Copyright 2013 Google Inc.
|
2013-07-10 11:10:48 +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 fs
|
|
|
|
|
2013-07-11 00:56:09 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2014-01-06 22:49:25 +00:00
|
|
|
"fmt"
|
2013-07-11 06:49:27 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-07-11 00:56:09 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2013-07-21 19:26:05 +00:00
|
|
|
"path/filepath"
|
2013-07-11 00:56:09 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2013-07-16 03:44:29 +00:00
|
|
|
"time"
|
2013-07-11 00:56:09 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-07-16 03:44:29 +00:00
|
|
|
"camlistore.org/pkg/readerutil"
|
2013-07-11 06:49:27 +00:00
|
|
|
"camlistore.org/pkg/schema"
|
2013-07-11 00:56:09 +00:00
|
|
|
"camlistore.org/pkg/search"
|
2013-10-05 07:43:15 +00:00
|
|
|
"camlistore.org/pkg/syncutil"
|
2013-07-11 00:56:09 +00:00
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
"camlistore.org/third_party/bazil.org/fuse"
|
|
|
|
"camlistore.org/third_party/bazil.org/fuse/fs"
|
2013-07-11 00:56:09 +00:00
|
|
|
)
|
|
|
|
|
2013-07-16 03:44:29 +00:00
|
|
|
// How often to refresh directory nodes by reading from the blobstore.
|
|
|
|
const populateInterval = 30 * time.Second
|
|
|
|
|
2014-01-07 22:48:58 +00:00
|
|
|
// How long an item that was created locally will be present
|
|
|
|
// regardless of its presence in the indexing server.
|
|
|
|
const deletionRefreshWindow = time.Minute
|
|
|
|
|
2013-07-28 19:59:56 +00:00
|
|
|
type nodeType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
fileType nodeType = iota
|
|
|
|
dirType
|
|
|
|
symlinkType
|
|
|
|
)
|
|
|
|
|
2013-07-11 00:56:09 +00:00
|
|
|
// mutDir is a mutable directory.
|
|
|
|
// Its br is the permanode with camliPath:entname attributes.
|
|
|
|
type mutDir struct {
|
2013-07-11 06:49:27 +00:00
|
|
|
fs *CamliFileSystem
|
2013-08-04 02:54:30 +00:00
|
|
|
permanode blob.Ref
|
2013-07-21 19:26:05 +00:00
|
|
|
parent *mutDir // or nil, if the root within its roots.go root.
|
|
|
|
name string // ent name (base name within parent)
|
2013-07-11 00:56:09 +00:00
|
|
|
|
2014-01-07 22:48:58 +00:00
|
|
|
localCreateTime time.Time // time this node was created locally (iff it was)
|
|
|
|
|
2013-07-11 06:49:27 +00:00
|
|
|
mu sync.Mutex
|
2013-07-16 03:44:29 +00:00
|
|
|
lastPop time.Time
|
2013-07-28 05:54:55 +00:00
|
|
|
children map[string]mutFileOrDir
|
2013-12-29 08:27:50 +00:00
|
|
|
xattrs map[string][]byte
|
2014-01-07 07:16:37 +00:00
|
|
|
deleted bool
|
2013-07-11 00:56:09 +00:00
|
|
|
}
|
|
|
|
|
2014-01-06 22:49:25 +00:00
|
|
|
func (m *mutDir) String() string {
|
|
|
|
return fmt.Sprintf("&mutDir{%p name=%q perm:%v}", m, m.fullPath(), m.permanode)
|
|
|
|
}
|
|
|
|
|
2013-07-21 19:26:05 +00:00
|
|
|
// for debugging
|
|
|
|
func (n *mutDir) fullPath() string {
|
|
|
|
if n == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return filepath.Join(n.parent.fullPath(), n.name)
|
|
|
|
}
|
|
|
|
|
2013-07-11 00:56:09 +00:00
|
|
|
func (n *mutDir) Attr() fuse.Attr {
|
|
|
|
return fuse.Attr{
|
2013-08-04 02:54:30 +00:00
|
|
|
Inode: n.permanode.Sum64(),
|
2013-07-22 16:53:17 +00:00
|
|
|
Mode: os.ModeDir | 0700,
|
|
|
|
Uid: uint32(os.Getuid()),
|
|
|
|
Gid: uint32(os.Getgid()),
|
2013-07-11 00:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Access(req *fuse.AccessRequest, intr fs.Intr) fuse.Error {
|
2014-01-07 07:16:37 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
if n.deleted {
|
|
|
|
return fuse.ENOENT
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) Access(req *fuse.AccessRequest, intr fs.Intr) fuse.Error {
|
2014-01-07 07:16:37 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
if n.deleted {
|
|
|
|
return fuse.ENOENT
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-07-16 03:44:29 +00:00
|
|
|
// populate hits the blobstore to populate map of child nodes.
|
2013-07-11 00:56:09 +00:00
|
|
|
func (n *mutDir) populate() error {
|
2013-07-16 03:44:29 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
|
|
|
|
// Only re-populate if we haven't done so recently.
|
|
|
|
now := time.Now()
|
|
|
|
if n.lastPop.Add(populateInterval).After(now) {
|
2013-07-11 06:49:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2013-07-16 03:44:29 +00:00
|
|
|
n.lastPop = now
|
2013-07-11 00:56:09 +00:00
|
|
|
|
|
|
|
res, err := n.fs.client.Describe(&search.DescribeRequest{
|
2013-07-11 06:49:27 +00:00
|
|
|
BlobRef: n.permanode,
|
2013-07-11 00:56:09 +00:00
|
|
|
Depth: 3,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println("mutDir.paths:", err)
|
|
|
|
return nil
|
|
|
|
}
|
2013-07-11 06:49:27 +00:00
|
|
|
db := res.Meta[n.permanode.String()]
|
2013-07-11 00:56:09 +00:00
|
|
|
if db == nil {
|
|
|
|
return errors.New("dir blobref not described")
|
|
|
|
}
|
2013-07-11 06:49:27 +00:00
|
|
|
|
|
|
|
// Find all child permanodes and stick them in n.children
|
|
|
|
if n.children == nil {
|
2013-07-28 05:54:55 +00:00
|
|
|
n.children = make(map[string]mutFileOrDir)
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
2014-01-07 22:48:58 +00:00
|
|
|
currentChildren := map[string]bool{}
|
2013-07-11 00:56:09 +00:00
|
|
|
for k, v := range db.Permanode.Attr {
|
|
|
|
const p = "camliPath:"
|
|
|
|
if !strings.HasPrefix(k, p) || len(v) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := k[len(p):]
|
|
|
|
childRef := v[0]
|
|
|
|
child := res.Meta[childRef]
|
|
|
|
if child == nil {
|
2013-07-16 03:44:29 +00:00
|
|
|
log.Printf("child not described: %v", childRef)
|
2013-07-11 00:56:09 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-05-08 22:16:45 +00:00
|
|
|
if child.Permanode == nil {
|
|
|
|
log.Printf("invalid child, not a permanode: %v", childRef)
|
|
|
|
continue
|
|
|
|
}
|
2013-07-28 19:59:56 +00:00
|
|
|
if target := child.Permanode.Attr.Get("camliSymlinkTarget"); target != "" {
|
|
|
|
// This is a symlink.
|
2014-01-07 22:48:58 +00:00
|
|
|
n.maybeAddChild(name, child.Permanode, &mutFile{
|
2013-07-28 19:59:56 +00:00
|
|
|
fs: n.fs,
|
2013-08-04 02:54:30 +00:00
|
|
|
permanode: blob.ParseOrZero(childRef),
|
2013-07-28 19:59:56 +00:00
|
|
|
parent: n,
|
|
|
|
name: name,
|
|
|
|
symLink: true,
|
|
|
|
target: target,
|
2014-01-07 22:48:58 +00:00
|
|
|
})
|
2013-12-29 08:27:50 +00:00
|
|
|
} else if isDir(child.Permanode) {
|
|
|
|
// This is a directory.
|
2014-01-07 22:48:58 +00:00
|
|
|
n.maybeAddChild(name, child.Permanode, &mutDir{
|
2013-12-29 08:27:50 +00:00
|
|
|
fs: n.fs,
|
|
|
|
permanode: blob.ParseOrZero(childRef),
|
|
|
|
parent: n,
|
|
|
|
name: name,
|
2014-01-07 22:48:58 +00:00
|
|
|
})
|
2013-12-29 08:27:50 +00:00
|
|
|
} else if contentRef := child.Permanode.Attr.Get("camliContent"); contentRef != "" {
|
2013-07-11 00:56:09 +00:00
|
|
|
// This is a file.
|
|
|
|
content := res.Meta[contentRef]
|
|
|
|
if content == nil {
|
2013-07-16 03:44:29 +00:00
|
|
|
log.Printf("child content not described: %v", childRef)
|
2013-07-11 00:56:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if content.CamliType != "file" {
|
2013-07-16 03:44:29 +00:00
|
|
|
log.Printf("child not a file: %v", childRef)
|
2013-07-11 00:56:09 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-03-11 20:24:48 +00:00
|
|
|
if content.File == nil {
|
|
|
|
log.Printf("camlitype \"file\" child %v has no described File member", childRef)
|
|
|
|
continue
|
|
|
|
}
|
2014-01-07 22:48:58 +00:00
|
|
|
n.maybeAddChild(name, child.Permanode, &mutFile{
|
2013-07-11 06:49:27 +00:00
|
|
|
fs: n.fs,
|
2013-08-04 02:54:30 +00:00
|
|
|
permanode: blob.ParseOrZero(childRef),
|
2013-07-11 06:49:27 +00:00
|
|
|
parent: n,
|
2013-07-11 00:56:09 +00:00
|
|
|
name: name,
|
2013-08-04 02:54:30 +00:00
|
|
|
content: blob.ParseOrZero(contentRef),
|
2013-07-11 06:49:27 +00:00
|
|
|
size: content.File.Size,
|
2014-01-07 22:48:58 +00:00
|
|
|
})
|
2013-12-29 08:27:50 +00:00
|
|
|
} else {
|
|
|
|
// unhandled type...
|
2013-07-11 00:56:09 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-01-07 22:48:58 +00:00
|
|
|
currentChildren[name] = true
|
|
|
|
}
|
|
|
|
// Remove unreferenced children
|
|
|
|
for name, oldchild := range n.children {
|
|
|
|
if _, ok := currentChildren[name]; !ok {
|
|
|
|
if oldchild.eligibleToDelete() {
|
|
|
|
delete(n.children, name)
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 00:56:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-07 22:48:58 +00:00
|
|
|
// maybeAddChild adds a child directory to this mutable directory
|
|
|
|
// unless it already has one with this name and permanode.
|
|
|
|
func (m *mutDir) maybeAddChild(name string, permanode *search.DescribedPermanode,
|
|
|
|
child mutFileOrDir) {
|
|
|
|
if current, ok := m.children[name]; !ok ||
|
|
|
|
current.permanodeString() != child.permanodeString() {
|
|
|
|
|
|
|
|
child.xattr().load(permanode)
|
|
|
|
m.children[name] = child
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
fs: only return a permanode as a directory if we're sure it is
If a well-timed lookup occurs while a file is being created, after the
permanode is available, but before any attribute claims declaring file
content, the file will be assumed to be a directory.
Ideally, FUSE would only hold on to this mistake for about a minute, or
the duration of the attribute cache, but it's much longer in practice.
Even with cammount effectively disabling all caches and returning fresh
dirents with random inodes, FUSE seemingly never sent in another Lookup
request for the broken entry.
While I wouldn't consider this a fix for the condition at large, I do
believe being explicit about what's a file, symlink, or directory is the
correct thing to do.
See also: http://camlistore.org/issue/293
Change-Id: I64d4afc8dfdb87b9b121d161936dd613139f00e2
2013-12-27 23:23:53 +00:00
|
|
|
func isDir(d *search.DescribedPermanode) bool {
|
|
|
|
// Explicit
|
|
|
|
if d.Attr.Get("camliNodeType") == "directory" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Implied
|
|
|
|
for k := range d.Attr {
|
|
|
|
if strings.HasPrefix(k, "camliPath:") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
|
2013-07-11 00:56:09 +00:00
|
|
|
if err := n.populate(); err != nil {
|
|
|
|
log.Println("populate:", err)
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
var ents []fuse.Dirent
|
2013-07-21 19:26:05 +00:00
|
|
|
for name, childNode := range n.children {
|
|
|
|
var ino uint64
|
|
|
|
switch v := childNode.(type) {
|
|
|
|
case *mutDir:
|
2013-08-04 02:54:30 +00:00
|
|
|
ino = v.permanode.Sum64()
|
2013-07-21 19:26:05 +00:00
|
|
|
case *mutFile:
|
2013-08-04 02:54:30 +00:00
|
|
|
ino = v.permanode.Sum64()
|
2013-07-21 19:26:05 +00:00
|
|
|
default:
|
|
|
|
log.Printf("mutDir.ReadDir: unknown child type %T", childNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: figure out what Dirent.Type means.
|
|
|
|
// fuse.go says "Type uint32 // ?"
|
|
|
|
dirent := fuse.Dirent{
|
2013-07-22 02:01:22 +00:00
|
|
|
Name: name,
|
2013-07-21 19:26:05 +00:00
|
|
|
Inode: ino,
|
|
|
|
}
|
|
|
|
log.Printf("mutDir(%q) appending inode %x, %+v", n.fullPath(), dirent.Inode, dirent)
|
|
|
|
ents = append(ents, dirent)
|
2013-07-11 00:56:09 +00:00
|
|
|
}
|
|
|
|
return ents, nil
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Lookup(name string, intr fs.Intr) (ret fs.Node, err fuse.Error) {
|
2013-07-21 19:26:05 +00:00
|
|
|
defer func() {
|
2014-01-06 22:49:25 +00:00
|
|
|
log.Printf("mutDir(%q).Lookup(%q) = %v, %v", n.fullPath(), name, ret, err)
|
2013-07-21 19:26:05 +00:00
|
|
|
}()
|
2013-07-11 00:56:09 +00:00
|
|
|
if err := n.populate(); err != nil {
|
|
|
|
log.Println("populate:", err)
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2013-07-11 06:49:27 +00:00
|
|
|
if n2 := n.children[name]; n2 != nil {
|
|
|
|
return n2, nil
|
2013-07-11 00:56:09 +00:00
|
|
|
}
|
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
|
|
|
|
2013-07-21 19:26:05 +00:00
|
|
|
// Create of regular file. (not a dir)
|
|
|
|
//
|
2013-07-27 23:07:25 +00:00
|
|
|
// Flags are always 514: O_CREAT is 0x200 | O_RDWR is 0x2.
|
|
|
|
// From fuse_vnops.c:
|
|
|
|
// /* XXX: We /always/ creat() like this. Wish we were on Linux. */
|
|
|
|
// foi->flags = O_CREAT | O_RDWR;
|
2013-07-21 19:26:05 +00:00
|
|
|
//
|
|
|
|
// 2013/07/21 05:26:35 <- &{Create [ID=0x3 Node=0x8 Uid=61652 Gid=5000 Pid=13115] "x" fl=514 mode=-rw-r--r-- fuse.Intr}
|
|
|
|
// 2013/07/21 05:26:36 -> 0x3 Create {LookupResponse:{Node:23 Generation:0 EntryValid:1m0s AttrValid:1m0s Attr:{Inode:15976986887557313215 Size:0 Blocks:0 Atime:2013-07-21 05:23:51.537251251 +1200 NZST Mtime:2013-07-21 05:23:51.537251251 +1200 NZST Ctime:2013-07-21 05:23:51.537251251 +1200 NZST Crtime:2013-07-21 05:23:51.537251251 +1200 NZST Mode:-rw------- Nlink:1 Uid:61652 Gid:5000 Rdev:0 Flags:0}} OpenResponse:{Handle:1 Flags:OpenDirectIO}}
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Create(req *fuse.CreateRequest, res *fuse.CreateResponse, intr fs.Intr) (fs.Node, fs.Handle, fuse.Error) {
|
2013-07-28 19:59:56 +00:00
|
|
|
child, err := n.creat(req.Name, fileType)
|
2013-07-11 06:49:27 +00:00
|
|
|
if err != nil {
|
2013-07-21 19:26:05 +00:00
|
|
|
log.Printf("mutDir.Create(%q): %v", req.Name, err)
|
2013-07-11 06:49:27 +00:00
|
|
|
return nil, nil, fuse.EIO
|
|
|
|
}
|
|
|
|
|
2013-07-16 03:44:29 +00:00
|
|
|
// Create and return a file handle.
|
|
|
|
h, ferr := child.(*mutFile).newHandle(nil)
|
|
|
|
if ferr != nil {
|
|
|
|
return nil, nil, ferr
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
2013-07-27 23:07:25 +00:00
|
|
|
|
|
|
|
// This isn't required (or even ever been shown to make a
|
|
|
|
// difference), but we do it to match OpenRequest below, where
|
|
|
|
// it causes test failures without:
|
|
|
|
res.OpenResponse.Flags &= ^fuse.OpenDirectIO
|
|
|
|
|
2013-07-16 03:44:29 +00:00
|
|
|
return child, h, nil
|
|
|
|
}
|
2013-07-11 06:49:27 +00:00
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Mkdir(req *fuse.MkdirRequest, intr fs.Intr) (fs.Node, fuse.Error) {
|
2013-07-28 19:59:56 +00:00
|
|
|
child, err := n.creat(req.Name, dirType)
|
2013-07-11 06:49:27 +00:00
|
|
|
if err != nil {
|
2013-07-16 03:44:29 +00:00
|
|
|
log.Printf("mutDir.Mkdir(%q): %v", req.Name, err)
|
|
|
|
return nil, fuse.EIO
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
2013-07-16 03:44:29 +00:00
|
|
|
return child, nil
|
|
|
|
}
|
2013-07-11 06:49:27 +00:00
|
|
|
|
2013-07-28 19:59:56 +00:00
|
|
|
// &fuse.SymlinkRequest{Header:fuse.Header{Conn:(*fuse.Conn)(0xc210047180), ID:0x4, Node:0x8, Uid:0xf0d4, Gid:0x1388, Pid:0x7e88}, NewName:"some-link", Target:"../../some-target"}
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Symlink(req *fuse.SymlinkRequest, intr fs.Intr) (fs.Node, fuse.Error) {
|
2013-07-28 19:59:56 +00:00
|
|
|
node, err := n.creat(req.NewName, symlinkType)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("mutDir.Symlink(%q): %v", req.NewName, err)
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
mf := node.(*mutFile)
|
|
|
|
mf.symLink = true
|
|
|
|
mf.target = req.Target
|
|
|
|
|
|
|
|
claim := schema.NewSetAttributeClaim(mf.permanode, "camliSymlinkTarget", req.Target)
|
|
|
|
_, err = n.fs.client.UploadAndSignBlob(claim)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("mutDir.Symlink(%q) upload error: %v", req.NewName, err)
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) creat(name string, typ nodeType) (fs.Node, error) {
|
2013-07-16 03:44:29 +00:00
|
|
|
// Create a Permanode for the file/directory.
|
|
|
|
pr, err := n.fs.client.UploadNewPermanode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-13 09:27:48 +00:00
|
|
|
var grp syncutil.Group
|
|
|
|
grp.Go(func() (err error) {
|
|
|
|
// Add a camliPath:name attribute to the directory permanode.
|
|
|
|
claim := schema.NewSetAttributeClaim(n.permanode, "camliPath:"+name, pr.BlobRef.String())
|
|
|
|
_, err = n.fs.client.UploadAndSignBlob(claim)
|
|
|
|
return
|
|
|
|
})
|
2013-12-29 08:27:50 +00:00
|
|
|
|
|
|
|
// Hide OS X Finder .DS_Store junk. This is distinct from
|
|
|
|
// extended attributes.
|
|
|
|
if name == ".DS_Store" {
|
fs: only return a permanode as a directory if we're sure it is
If a well-timed lookup occurs while a file is being created, after the
permanode is available, but before any attribute claims declaring file
content, the file will be assumed to be a directory.
Ideally, FUSE would only hold on to this mistake for about a minute, or
the duration of the attribute cache, but it's much longer in practice.
Even with cammount effectively disabling all caches and returning fresh
dirents with random inodes, FUSE seemingly never sent in another Lookup
request for the broken entry.
While I wouldn't consider this a fix for the condition at large, I do
believe being explicit about what's a file, symlink, or directory is the
correct thing to do.
See also: http://camlistore.org/issue/293
Change-Id: I64d4afc8dfdb87b9b121d161936dd613139f00e2
2013-12-27 23:23:53 +00:00
|
|
|
grp.Go(func() (err error) {
|
2013-12-29 08:27:50 +00:00
|
|
|
claim := schema.NewSetAttributeClaim(pr.BlobRef, "camliDefVis", "hide")
|
fs: only return a permanode as a directory if we're sure it is
If a well-timed lookup occurs while a file is being created, after the
permanode is available, but before any attribute claims declaring file
content, the file will be assumed to be a directory.
Ideally, FUSE would only hold on to this mistake for about a minute, or
the duration of the attribute cache, but it's much longer in practice.
Even with cammount effectively disabling all caches and returning fresh
dirents with random inodes, FUSE seemingly never sent in another Lookup
request for the broken entry.
While I wouldn't consider this a fix for the condition at large, I do
believe being explicit about what's a file, symlink, or directory is the
correct thing to do.
See also: http://camlistore.org/issue/293
Change-Id: I64d4afc8dfdb87b9b121d161936dd613139f00e2
2013-12-27 23:23:53 +00:00
|
|
|
_, err = n.fs.client.UploadAndSignBlob(claim)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
2013-12-29 08:27:50 +00:00
|
|
|
|
|
|
|
if typ == dirType {
|
2013-12-13 09:27:48 +00:00
|
|
|
grp.Go(func() (err error) {
|
2013-12-29 08:27:50 +00:00
|
|
|
// Set a directory type on the permanode
|
|
|
|
claim := schema.NewSetAttributeClaim(pr.BlobRef, "camliNodeType", "directory")
|
2013-12-13 09:27:48 +00:00
|
|
|
_, err = n.fs.client.UploadAndSignBlob(claim)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := grp.Err(); err != nil {
|
2013-07-16 03:44:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a child node to this node.
|
2013-07-28 05:54:55 +00:00
|
|
|
var child mutFileOrDir
|
2013-07-28 19:59:56 +00:00
|
|
|
switch typ {
|
|
|
|
case dirType:
|
2013-07-16 03:44:29 +00:00
|
|
|
child = &mutDir{
|
2014-01-07 22:48:58 +00:00
|
|
|
fs: n.fs,
|
|
|
|
permanode: pr.BlobRef,
|
|
|
|
parent: n,
|
|
|
|
name: name,
|
|
|
|
xattrs: map[string][]byte{},
|
|
|
|
localCreateTime: time.Now(),
|
2013-07-16 03:44:29 +00:00
|
|
|
}
|
2013-07-28 19:59:56 +00:00
|
|
|
case fileType, symlinkType:
|
2013-07-16 03:44:29 +00:00
|
|
|
child = &mutFile{
|
2014-01-07 22:48:58 +00:00
|
|
|
fs: n.fs,
|
|
|
|
permanode: pr.BlobRef,
|
|
|
|
parent: n,
|
|
|
|
name: name,
|
|
|
|
xattrs: map[string][]byte{},
|
|
|
|
localCreateTime: time.Now(),
|
2013-07-16 03:44:29 +00:00
|
|
|
}
|
2013-07-28 19:59:56 +00:00
|
|
|
default:
|
|
|
|
panic("bogus creat type")
|
2013-07-16 03:44:29 +00:00
|
|
|
}
|
2013-07-11 06:49:27 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
if n.children == nil {
|
2013-07-28 05:54:55 +00:00
|
|
|
n.children = make(map[string]mutFileOrDir)
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
2013-07-16 03:44:29 +00:00
|
|
|
n.children[name] = child
|
2013-07-11 06:49:27 +00:00
|
|
|
n.mu.Unlock()
|
|
|
|
|
2014-01-06 22:49:25 +00:00
|
|
|
log.Printf("Created %v in %p", child, n)
|
|
|
|
|
2013-07-16 03:44:29 +00:00
|
|
|
return child, nil
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Remove(req *fuse.RemoveRequest, intr fs.Intr) fuse.Error {
|
2013-07-16 03:44:29 +00:00
|
|
|
// Remove the camliPath:name attribute from the directory permanode.
|
2013-09-23 12:30:05 +00:00
|
|
|
claim := schema.NewDelAttributeClaim(n.permanode, "camliPath:"+req.Name, "")
|
2013-07-16 03:44:29 +00:00
|
|
|
_, err := n.fs.client.UploadAndSignBlob(claim)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("mutDir.Create:", err)
|
|
|
|
return fuse.EIO
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
2013-07-16 03:44:29 +00:00
|
|
|
// Remove child from map.
|
|
|
|
n.mu.Lock()
|
|
|
|
if n.children != nil {
|
2014-01-07 07:16:37 +00:00
|
|
|
if removed, ok := n.children[req.Name]; ok {
|
|
|
|
removed.invalidate()
|
|
|
|
delete(n.children, req.Name)
|
|
|
|
log.Printf("Removed %v from %p", removed, n)
|
|
|
|
}
|
2013-07-16 03:44:29 +00:00
|
|
|
}
|
|
|
|
n.mu.Unlock()
|
|
|
|
return nil
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
|
|
|
|
2013-07-28 05:54:55 +00:00
|
|
|
// &RenameRequest{Header:fuse.Header{Conn:(*fuse.Conn)(0xc210048180), ID:0x2, Node:0x8, Uid:0xf0d4, Gid:0x1388, Pid:0x5edb}, NewDir:0x8, OldName:"1", NewName:"2"}
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Rename(req *fuse.RenameRequest, newDir fs.Node, intr fs.Intr) fuse.Error {
|
2013-07-28 05:54:55 +00:00
|
|
|
n2, ok := newDir.(*mutDir)
|
|
|
|
if !ok {
|
|
|
|
log.Printf("*mutDir newDir node isn't a *mutDir; is a %T; can't handle. returning EIO.", newDir)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
2013-10-05 07:43:15 +00:00
|
|
|
var wg syncutil.Group
|
|
|
|
wg.Go(n.populate)
|
|
|
|
wg.Go(n2.populate)
|
|
|
|
if err := wg.Err(); err != nil {
|
2013-07-28 05:54:55 +00:00
|
|
|
log.Printf("*mutDir.Rename src dir populate = %v", err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
n.mu.Lock()
|
|
|
|
target, ok := n.children[req.OldName]
|
|
|
|
n.mu.Unlock()
|
|
|
|
if !ok {
|
|
|
|
log.Printf("*mutDir.Rename src name %q isn't known", req.OldName)
|
|
|
|
return fuse.ENOENT
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
// Add a camliPath:name attribute to the dest permanode before unlinking it from
|
|
|
|
// the source.
|
|
|
|
claim := schema.NewSetAttributeClaim(n2.permanode, "camliPath:"+req.NewName, target.permanodeString())
|
|
|
|
claim.SetClaimDate(now)
|
|
|
|
_, err := n.fs.client.UploadAndSignBlob(claim)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Upload rename link error: %v", err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
2013-09-23 12:30:05 +00:00
|
|
|
delClaim := schema.NewDelAttributeClaim(n.permanode, "camliPath:"+req.OldName, "")
|
2013-07-28 05:54:55 +00:00
|
|
|
delClaim.SetClaimDate(now)
|
|
|
|
_, err = n.fs.client.UploadAndSignBlob(delClaim)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Upload rename src unlink error: %v", err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bradfitz): this locking would be racy, if the kernel
|
|
|
|
// doesn't do it properly. (It should) Let's just trust the
|
|
|
|
// kernel for now. Later we can verify and remove this
|
|
|
|
// comment.
|
|
|
|
n.mu.Lock()
|
|
|
|
if n.children[req.OldName] != target {
|
|
|
|
panic("Race.")
|
|
|
|
}
|
|
|
|
delete(n.children, req.OldName)
|
|
|
|
n.mu.Unlock()
|
|
|
|
n2.mu.Lock()
|
|
|
|
n2.children[req.NewName] = target
|
|
|
|
n2.mu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
2013-07-28 01:29:50 +00:00
|
|
|
}
|
|
|
|
|
2013-07-28 19:59:56 +00:00
|
|
|
// mutFile is a mutable file, or symlink.
|
2013-07-11 00:56:09 +00:00
|
|
|
type mutFile struct {
|
2013-07-11 06:49:27 +00:00
|
|
|
fs *CamliFileSystem
|
2013-08-04 02:54:30 +00:00
|
|
|
permanode blob.Ref
|
2013-07-11 06:49:27 +00:00
|
|
|
parent *mutDir
|
2013-07-16 03:44:29 +00:00
|
|
|
name string // ent name (base name within parent)
|
2013-07-11 06:49:27 +00:00
|
|
|
|
2014-01-07 22:48:58 +00:00
|
|
|
localCreateTime time.Time // time this node was created locally (iff it was)
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
mu sync.Mutex // protects all following fields
|
|
|
|
symLink bool // if true, is a symlink
|
|
|
|
target string // if a symlink
|
|
|
|
content blob.Ref // if a regular file
|
2013-07-21 19:26:05 +00:00
|
|
|
size int64
|
|
|
|
mtime, atime time.Time // if zero, use serverStart
|
2013-12-29 08:27:50 +00:00
|
|
|
xattrs map[string][]byte
|
2014-01-07 07:16:37 +00:00
|
|
|
deleted bool
|
2013-07-21 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2014-01-06 22:49:25 +00:00
|
|
|
func (m *mutFile) String() string {
|
|
|
|
return fmt.Sprintf("&mutFile{%p name=%q perm:%v}", m, m.fullPath(), m.permanode)
|
|
|
|
}
|
|
|
|
|
2013-07-21 19:26:05 +00:00
|
|
|
// for debugging
|
|
|
|
func (n *mutFile) fullPath() string {
|
|
|
|
if n == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return filepath.Join(n.parent.fullPath(), n.name)
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-29 08:27:50 +00:00
|
|
|
func (n *mutFile) xattr() *xattr {
|
|
|
|
return &xattr{"mutFile", n.fs, n.permanode, &n.mu, &n.xattrs}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *mutDir) xattr() *xattr {
|
|
|
|
return &xattr{"mutDir", n.fs, n.permanode, &n.mu, &n.xattrs}
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Removexattr(req *fuse.RemovexattrRequest, intr fs.Intr) fuse.Error {
|
2013-12-29 08:27:50 +00:00
|
|
|
return n.xattr().remove(req)
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Setxattr(req *fuse.SetxattrRequest, intr fs.Intr) fuse.Error {
|
2013-12-29 08:27:50 +00:00
|
|
|
return n.xattr().set(req)
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Getxattr(req *fuse.GetxattrRequest, res *fuse.GetxattrResponse, intr fs.Intr) fuse.Error {
|
2013-12-29 08:27:50 +00:00
|
|
|
return n.xattr().get(req, res)
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutDir) Listxattr(req *fuse.ListxattrRequest, res *fuse.ListxattrResponse, intr fs.Intr) fuse.Error {
|
2013-12-29 08:27:50 +00:00
|
|
|
return n.xattr().list(req, res)
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) Getxattr(req *fuse.GetxattrRequest, res *fuse.GetxattrResponse, intr fs.Intr) fuse.Error {
|
2013-12-29 08:27:50 +00:00
|
|
|
return n.xattr().get(req, res)
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) Listxattr(req *fuse.ListxattrRequest, res *fuse.ListxattrResponse, intr fs.Intr) fuse.Error {
|
2013-12-29 08:27:50 +00:00
|
|
|
return n.xattr().list(req, res)
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) Removexattr(req *fuse.RemovexattrRequest, intr fs.Intr) fuse.Error {
|
2013-12-29 08:27:50 +00:00
|
|
|
return n.xattr().remove(req)
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) Setxattr(req *fuse.SetxattrRequest, intr fs.Intr) fuse.Error {
|
2013-12-29 08:27:50 +00:00
|
|
|
return n.xattr().set(req)
|
|
|
|
}
|
|
|
|
|
2013-07-11 06:49:27 +00:00
|
|
|
func (n *mutFile) Attr() fuse.Attr {
|
2013-07-21 19:26:05 +00:00
|
|
|
// TODO: don't grab n.mu three+ times in here.
|
2013-07-28 19:59:56 +00:00
|
|
|
var mode os.FileMode = 0600 // writable
|
2013-07-21 19:26:05 +00:00
|
|
|
|
|
|
|
n.mu.Lock()
|
|
|
|
size := n.size
|
|
|
|
var blocks uint64
|
|
|
|
if size > 0 {
|
|
|
|
blocks = uint64(size)/512 + 1
|
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
inode := n.permanode.Sum64()
|
2013-07-28 19:59:56 +00:00
|
|
|
if n.symLink {
|
|
|
|
mode |= os.ModeSymlink
|
|
|
|
}
|
2013-07-21 19:26:05 +00:00
|
|
|
n.mu.Unlock()
|
|
|
|
|
2013-07-11 06:49:27 +00:00
|
|
|
return fuse.Attr{
|
2013-07-21 19:26:05 +00:00
|
|
|
Inode: inode,
|
2013-07-28 19:59:56 +00:00
|
|
|
Mode: mode,
|
2013-07-21 19:26:05 +00:00
|
|
|
Uid: uint32(os.Getuid()),
|
|
|
|
Gid: uint32(os.Getgid()),
|
|
|
|
Size: uint64(size),
|
|
|
|
Blocks: blocks,
|
|
|
|
Mtime: n.modTime(),
|
|
|
|
Atime: n.accessTime(),
|
2013-07-11 06:49:27 +00:00
|
|
|
Ctime: serverStart,
|
|
|
|
Crtime: serverStart,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 19:26:05 +00:00
|
|
|
func (n *mutFile) accessTime() time.Time {
|
|
|
|
n.mu.Lock()
|
|
|
|
if !n.atime.IsZero() {
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
return n.atime
|
|
|
|
}
|
|
|
|
n.mu.Unlock()
|
|
|
|
return n.modTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *mutFile) modTime() time.Time {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
if !n.mtime.IsZero() {
|
|
|
|
return n.mtime
|
|
|
|
}
|
|
|
|
return serverStart
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (n *mutFile) setContent(br blob.Ref, size int64) error {
|
2013-07-11 06:49:27 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
n.content = br
|
|
|
|
n.size = size
|
|
|
|
claim := schema.NewSetAttributeClaim(n.permanode, "camliContent", br.String())
|
|
|
|
_, err := n.fs.client.UploadAndSignBlob(claim)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-22 16:52:48 +00:00
|
|
|
func (n *mutFile) setSizeAtLeast(size int64) {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
log.Printf("mutFile.setSizeAtLeast(%d). old size = %d", size, n.size)
|
|
|
|
if size > n.size {
|
|
|
|
n.size = size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 19:26:05 +00:00
|
|
|
// Empirically:
|
|
|
|
// open for read: req.Flags == 0
|
|
|
|
// open for append: req.Flags == 1
|
|
|
|
// open for write: req.Flags == 1
|
|
|
|
// open for read/write (+<) == 2 (bitmask? of?)
|
2013-07-27 23:07:25 +00:00
|
|
|
//
|
|
|
|
// open flags are O_WRONLY (1), O_RDONLY (0), or O_RDWR (2). and also
|
|
|
|
// bitmaks of O_SYMLINK (0x200000) maybe. (from
|
|
|
|
// fuse_filehandle_xlate_to_oflags in macosx/kext/fuse_file.h)
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) Open(req *fuse.OpenRequest, res *fuse.OpenResponse, intr fs.Intr) (fs.Handle, fuse.Error) {
|
2013-07-22 04:13:16 +00:00
|
|
|
mutFileOpen.Incr()
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
log.Printf("mutFile.Open: %v: content: %v dir=%v flags=%v", n.permanode, n.content, req.Dir, req.Flags)
|
2013-07-11 06:49:27 +00:00
|
|
|
r, err := schema.NewFileReader(n.fs.fetcher, n.content)
|
|
|
|
if err != nil {
|
2013-07-22 04:13:16 +00:00
|
|
|
mutFileOpenError.Incr()
|
2013-07-11 06:49:27 +00:00
|
|
|
log.Printf("mutFile.Open: %v", err)
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
2013-07-21 19:26:05 +00:00
|
|
|
|
2013-07-27 23:07:25 +00:00
|
|
|
// Turn off the OpenDirectIO bit (on by default in rsc fuse server.go),
|
|
|
|
// else append operations don't work for some reason.
|
|
|
|
res.Flags &= ^fuse.OpenDirectIO
|
|
|
|
|
2013-07-21 19:26:05 +00:00
|
|
|
// Read-only.
|
2013-12-26 22:11:20 +00:00
|
|
|
if !isWriteFlags(req.Flags) {
|
2013-07-22 04:13:16 +00:00
|
|
|
mutFileOpenRO.Incr()
|
2013-07-21 19:26:05 +00:00
|
|
|
log.Printf("mutFile.Open returning read-only file")
|
|
|
|
n := &node{
|
|
|
|
fs: n.fs,
|
|
|
|
blobref: n.content,
|
|
|
|
}
|
|
|
|
return &nodeReader{n: n, fr: r}, nil
|
|
|
|
}
|
|
|
|
|
2013-07-22 04:13:16 +00:00
|
|
|
mutFileOpenRW.Incr()
|
2013-07-21 19:26:05 +00:00
|
|
|
log.Printf("mutFile.Open returning read-write filehandle")
|
|
|
|
|
2013-07-11 06:49:27 +00:00
|
|
|
defer r.Close()
|
|
|
|
return n.newHandle(r)
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) Fsync(r *fuse.FsyncRequest, intr fs.Intr) fuse.Error {
|
2013-07-16 03:44:29 +00:00
|
|
|
// TODO(adg): in the fuse package, plumb through fsync to mutFileHandle
|
|
|
|
// in the same way we did Truncate.
|
2013-07-21 19:26:05 +00:00
|
|
|
log.Printf("mutFile.Fsync: TODO")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) Readlink(req *fuse.ReadlinkRequest, intr fs.Intr) (string, fuse.Error) {
|
2013-07-28 19:59:56 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
if !n.symLink {
|
|
|
|
log.Printf("mutFile.Readlink on node that's not a symlink?")
|
|
|
|
return "", fuse.EIO
|
|
|
|
}
|
|
|
|
return n.target, nil
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) Setattr(req *fuse.SetattrRequest, res *fuse.SetattrResponse, intr fs.Intr) fuse.Error {
|
2013-07-21 19:26:05 +00:00
|
|
|
log.Printf("mutFile.Setattr on %q: %#v", n.fullPath(), req)
|
|
|
|
// 2013/07/17 19:43:41 mutFile.Setattr on "foo": &fuse.SetattrRequest{Header:fuse.Header{Conn:(*fuse.Conn)(0xc210047180), ID:0x3, Node:0x3d, Uid:0xf0d4, Gid:0x1388, Pid:0x75e8}, Valid:0x30, Handle:0x0, Size:0x0, Atime:time.Time{sec:63509651021, nsec:0x4aec6b8, loc:(*time.Location)(0x47f7600)}, Mtime:time.Time{sec:63509651021, nsec:0x4aec6b8, loc:(*time.Location)(0x47f7600)}, Mode:0x4000000, Uid:0x0, Gid:0x0, Bkuptime:time.Time{sec:62135596800, nsec:0x0, loc:(*time.Location)(0x47f7600)}, Chgtime:time.Time{sec:62135596800, nsec:0x0, loc:(*time.Location)(0x47f7600)}, Crtime:time.Time{sec:0, nsec:0x0, loc:(*time.Location)(nil)}, Flags:0x0}
|
|
|
|
|
|
|
|
n.mu.Lock()
|
|
|
|
if req.Valid&fuse.SetattrMtime != 0 {
|
|
|
|
n.mtime = req.Mtime
|
|
|
|
}
|
|
|
|
if req.Valid&fuse.SetattrAtime != 0 {
|
|
|
|
n.atime = req.Atime
|
|
|
|
}
|
|
|
|
if req.Valid&fuse.SetattrSize != 0 {
|
|
|
|
// TODO(bradfitz): truncate?
|
|
|
|
n.size = int64(req.Size)
|
|
|
|
}
|
|
|
|
n.mu.Unlock()
|
|
|
|
|
|
|
|
res.AttrValid = 1 * time.Minute
|
|
|
|
res.Attr = n.Attr()
|
2013-07-16 03:44:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (n *mutFile) newHandle(body io.Reader) (fs.Handle, fuse.Error) {
|
2013-07-11 06:49:27 +00:00
|
|
|
tmp, err := ioutil.TempFile("", "camli-")
|
|
|
|
if err == nil && body != nil {
|
|
|
|
_, err = io.Copy(tmp, body)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("mutFile.newHandle: %v", err)
|
|
|
|
if tmp != nil {
|
|
|
|
tmp.Close()
|
|
|
|
os.Remove(tmp.Name())
|
|
|
|
}
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
return &mutFileHandle{f: n, tmp: tmp}, nil
|
|
|
|
}
|
|
|
|
|
2013-07-16 03:44:29 +00:00
|
|
|
// mutFileHandle represents an open mutable file.
|
|
|
|
// It stores the file contents in a temporary file, and
|
|
|
|
// delegates reads and writes directly to the temporary file.
|
|
|
|
// When the handle is released, it writes the contents of the
|
|
|
|
// temporary file to the blobstore, and instructs the parent
|
|
|
|
// mutFile to update the file permanode.
|
2013-07-11 06:49:27 +00:00
|
|
|
type mutFileHandle struct {
|
2013-07-22 02:01:22 +00:00
|
|
|
f *mutFile
|
|
|
|
tmp *os.File
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
2013-07-11 00:56:09 +00:00
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (h *mutFileHandle) Read(req *fuse.ReadRequest, res *fuse.ReadResponse, intr fs.Intr) fuse.Error {
|
2013-07-21 19:26:05 +00:00
|
|
|
if h.tmp == nil {
|
|
|
|
log.Printf("Read called on camli mutFileHandle without a tempfile set")
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
2013-07-11 06:49:27 +00:00
|
|
|
buf := make([]byte, req.Size)
|
|
|
|
n, err := h.tmp.ReadAt(buf, req.Offset)
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("mutFileHandle.Read: %v", err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
res.Data = buf[:n]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (h *mutFileHandle) Write(req *fuse.WriteRequest, res *fuse.WriteResponse, intr fs.Intr) fuse.Error {
|
2013-07-21 19:26:05 +00:00
|
|
|
if h.tmp == nil {
|
|
|
|
log.Printf("Write called on camli mutFileHandle without a tempfile set")
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
2013-07-11 06:49:27 +00:00
|
|
|
n, err := h.tmp.WriteAt(req.Data, req.Offset)
|
2013-12-30 20:54:50 +00:00
|
|
|
log.Printf("mutFileHandle.Write(%q, %d bytes at %d, flags %v) = %d, %v",
|
|
|
|
h.f.fullPath(), len(req.Data), req.Offset, req.Flags, n, err)
|
2013-07-11 06:49:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("mutFileHandle.Write:", err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
res.Size = n
|
2013-07-22 16:52:48 +00:00
|
|
|
h.f.setSizeAtLeast(req.Offset + int64(n))
|
2013-07-11 06:49:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-27 05:46:26 +00:00
|
|
|
// Flush is called to let the file system clean up any data buffers
|
|
|
|
// and to pass any errors in the process of closing a file to the user
|
|
|
|
// application.
|
|
|
|
//
|
|
|
|
// Flush *may* be called more than once in the case where a file is
|
|
|
|
// opened more than once, but it's not possible to detect from the
|
|
|
|
// call itself whether this is a final flush.
|
|
|
|
//
|
|
|
|
// This is generally the last opportunity to finalize data and the
|
|
|
|
// return value sets the return value of the Close that led to the
|
|
|
|
// calling of Flush.
|
|
|
|
//
|
|
|
|
// Note that this is distinct from Fsync -- which is a user-requested
|
|
|
|
// flush (fsync, etc...)
|
2014-01-14 17:04:36 +00:00
|
|
|
func (h *mutFileHandle) Flush(*fuse.FlushRequest, fs.Intr) fuse.Error {
|
2013-07-21 19:26:05 +00:00
|
|
|
if h.tmp == nil {
|
2013-12-27 05:46:26 +00:00
|
|
|
log.Printf("Flush called on camli mutFileHandle without a tempfile set")
|
2013-07-21 19:26:05 +00:00
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
_, err := h.tmp.Seek(0, 0)
|
|
|
|
if err != nil {
|
2013-12-27 05:46:26 +00:00
|
|
|
log.Println("mutFileHandle.Flush:", err)
|
2013-07-21 19:26:05 +00:00
|
|
|
return fuse.EIO
|
2013-07-11 06:49:27 +00:00
|
|
|
}
|
2013-07-21 19:26:05 +00:00
|
|
|
var n int64
|
|
|
|
br, err := schema.WriteFileFromReader(h.f.fs.client, h.f.name, readerutil.CountingReader{Reader: h.tmp, N: &n})
|
|
|
|
if err != nil {
|
2013-12-27 05:46:26 +00:00
|
|
|
log.Println("mutFileHandle.Flush:", err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
err = h.f.setContent(br, n)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("mutFileHandle.Flush: %v", err)
|
2013-07-21 19:26:05 +00:00
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
2013-12-27 05:46:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release is called when a file handle is no longer needed. This is
|
|
|
|
// called asynchronously after the last handle to a file is closed.
|
2014-01-14 17:04:36 +00:00
|
|
|
func (h *mutFileHandle) Release(req *fuse.ReleaseRequest, intr fs.Intr) fuse.Error {
|
2013-07-11 06:49:27 +00:00
|
|
|
h.tmp.Close()
|
|
|
|
os.Remove(h.tmp.Name())
|
2013-07-21 19:26:05 +00:00
|
|
|
h.tmp = nil
|
|
|
|
|
2013-07-11 06:49:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-14 17:04:36 +00:00
|
|
|
func (h *mutFileHandle) Truncate(size uint64, intr fs.Intr) fuse.Error {
|
2013-07-21 19:26:05 +00:00
|
|
|
if h.tmp == nil {
|
|
|
|
log.Printf("Truncate called on camli mutFileHandle without a tempfile set")
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("mutFileHandle.Truncate(%q) to size %d", h.f.fullPath(), size)
|
2013-07-11 06:49:27 +00:00
|
|
|
if err := h.tmp.Truncate(int64(size)); err != nil {
|
|
|
|
log.Println("mutFileHandle.Truncate:", err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
return nil
|
2013-07-11 00:56:09 +00:00
|
|
|
}
|
2013-07-28 05:54:55 +00:00
|
|
|
|
|
|
|
// mutFileOrDir is a *mutFile or *mutDir
|
|
|
|
type mutFileOrDir interface {
|
2014-01-14 17:04:36 +00:00
|
|
|
fs.Node
|
2014-01-07 07:16:37 +00:00
|
|
|
invalidate()
|
2013-07-28 05:54:55 +00:00
|
|
|
permanodeString() string
|
2013-12-29 08:27:50 +00:00
|
|
|
xattr() *xattr
|
2014-01-07 22:48:58 +00:00
|
|
|
eligibleToDelete() bool
|
2013-07-28 05:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *mutFile) permanodeString() string {
|
|
|
|
return n.permanode.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *mutDir) permanodeString() string {
|
|
|
|
return n.permanode.String()
|
|
|
|
}
|
2014-01-07 07:16:37 +00:00
|
|
|
|
|
|
|
func (n *mutFile) invalidate() {
|
|
|
|
n.mu.Lock()
|
|
|
|
n.deleted = true
|
|
|
|
n.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *mutDir) invalidate() {
|
|
|
|
n.mu.Lock()
|
|
|
|
n.deleted = true
|
|
|
|
n.mu.Unlock()
|
|
|
|
}
|
2014-01-07 22:48:58 +00:00
|
|
|
|
|
|
|
func (n *mutFile) eligibleToDelete() bool {
|
|
|
|
return n.localCreateTime.Before(time.Now().Add(-deletionRefreshWindow))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *mutDir) eligibleToDelete() bool {
|
|
|
|
return n.localCreateTime.Before(time.Now().Add(-deletionRefreshWindow))
|
|
|
|
}
|