Commit 56dcd64b authored by Kirill Smelkov's avatar Kirill Smelkov

Merge remote-tracking branch 'origin/master' into t

* origin/master: (28 commits)
  nodefs: use offset for MemRegularFile reads. Test it.
  zipfs: fix and test tarfs
  nodefs: don't tweak MemRegularFile mode
  nodefs: fix builds on darwin
  example/hello: convert hello to new nodefs API
  nodefs: add MemRegularFile.Flush
  example/statfs: use new nodefs library
  example/loopback: use new nodefs library
  nodefs: document Access
  example/zipfs: use new nodefs
  nodefs: use bridge.getattr for Access calculation
  example/multizip: use new nodefs package.
  nodefs: comment ExampleNewPersistentInode
  nodefs: add Options.DefaultPermissions
  zipfs: rewrite using new nodefs API.
  nodefs: use testMount in tests
  nodefs: don't pass root into Options.OnAdd
  nodefs: add Inode.RmAllChildren
  nodefs: various fixes
  nodefs: check that default Create returns EROFS
  ...
parents a510f37f 928afa1c
...@@ -13,32 +13,27 @@ import ( ...@@ -13,32 +13,27 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort" "sort"
"syscall"
"testing" "testing"
"time" "time"
"github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/pathfs"
"github.com/hanwen/go-fuse/internal/testutil" "github.com/hanwen/go-fuse/internal/testutil"
"github.com/hanwen/go-fuse/nodefs"
) )
func setupFs(fs pathfs.FileSystem, N int) (string, func()) { func setupFs(fs nodefs.InodeEmbedder, N int) (string, func()) {
opts := &nodefs.Options{ opts := &nodefs.Options{}
EntryTimeout: 0.0, opts.Debug = testutil.VerboseTest()
AttrTimeout: 0.0,
NegativeTimeout: 0.0,
}
mountPoint := testutil.TempDir() mountPoint := testutil.TempDir()
nfs := pathfs.NewPathNodeFs(fs, nil) server, err := nodefs.Mount(mountPoint, fs, opts)
state, _, err := nodefs.MountRoot(mountPoint, nfs.Root(), opts)
if err != nil { if err != nil {
panic(fmt.Sprintf("cannot mount %v", err)) // ugh - benchmark has no error methods. log.Panicf("cannot mount %v", err)
} }
lmap := NewLatencyMap() lmap := NewLatencyMap()
if testutil.VerboseTest() { if testutil.VerboseTest() {
state.RecordLatencies(lmap) server.RecordLatencies(lmap)
} }
go state.Serve()
return mountPoint, func() { return mountPoint, func() {
if testutil.VerboseTest() { if testutil.VerboseTest() {
var total time.Duration var total time.Duration
...@@ -55,7 +50,7 @@ func setupFs(fs pathfs.FileSystem, N int) (string, func()) { ...@@ -55,7 +50,7 @@ func setupFs(fs pathfs.FileSystem, N int) (string, func()) {
log.Printf("total %v, %v/bench op", total, total/time.Duration(N)) log.Printf("total %v, %v/bench op", total, total/time.Duration(N))
} }
err := state.Unmount() err := server.Unmount()
if err != nil { if err != nil {
log.Println("error during unmount", err) log.Println("error during unmount", err)
} else { } else {
...@@ -65,11 +60,11 @@ func setupFs(fs pathfs.FileSystem, N int) (string, func()) { ...@@ -65,11 +60,11 @@ func setupFs(fs pathfs.FileSystem, N int) (string, func()) {
} }
func TestNewStatFs(t *testing.T) { func TestNewStatFs(t *testing.T) {
fs := NewStatFS() fs := &StatFS{}
for _, n := range []string{ for _, n := range []string{
"file.txt", "sub/dir/foo.txt", "file.txt", "sub/dir/foo.txt",
"sub/dir/bar.txt", "sub/marine.txt"} { "sub/dir/bar.txt", "sub/marine.txt"} {
fs.AddFile(n) fs.AddFile(n, fuse.Attr{Mode: syscall.S_IFREG})
} }
wd, clean := setupFs(fs, 1) wd, clean := setupFs(fs, 1)
...@@ -116,13 +111,13 @@ func TestNewStatFs(t *testing.T) { ...@@ -116,13 +111,13 @@ func TestNewStatFs(t *testing.T) {
func BenchmarkGoFuseStat(b *testing.B) { func BenchmarkGoFuseStat(b *testing.B) {
b.StopTimer() b.StopTimer()
fs := NewStatFS() fs := &StatFS{}
wd, _ := os.Getwd() wd, _ := os.Getwd()
fileList := wd + "/testpaths.txt" fileList := wd + "/testpaths.txt"
files := ReadLines(fileList) files := ReadLines(fileList)
for _, fn := range files { for _, fn := range files {
fs.AddFile(fn) fs.AddFile(fn, fuse.Attr{Mode: syscall.S_IFREG})
} }
wd, clean := setupFs(fs, b.N) wd, clean := setupFs(fs, b.N)
...@@ -151,13 +146,13 @@ func readdir(d string) error { ...@@ -151,13 +146,13 @@ func readdir(d string) error {
func BenchmarkGoFuseReaddir(b *testing.B) { func BenchmarkGoFuseReaddir(b *testing.B) {
b.StopTimer() b.StopTimer()
fs := NewStatFS() fs := &StatFS{}
wd, _ := os.Getwd() wd, _ := os.Getwd()
dirSet := map[string]struct{}{} dirSet := map[string]struct{}{}
for _, fn := range ReadLines(wd + "/testpaths.txt") { for _, fn := range ReadLines(wd + "/testpaths.txt") {
fs.AddFile(fn) fs.AddFile(fn, fuse.Attr{Mode: syscall.S_IFREG})
dirSet[filepath.Dir(fn)] = struct{}{} dirSet[filepath.Dir(fn)] = struct{}{}
} }
......
...@@ -5,65 +5,66 @@ ...@@ -5,65 +5,66 @@
package benchmark package benchmark
import ( import (
"context"
"path/filepath" "path/filepath"
"strings" "strings"
"syscall"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/pathfs" "github.com/hanwen/go-fuse/nodefs"
) )
type StatFS struct { type StatFS struct {
pathfs.FileSystem nodefs.Inode
entries map[string]*fuse.Attr
dirs map[string][]fuse.DirEntry files map[string]fuse.Attr
} }
func (fs *StatFS) Add(name string, a *fuse.Attr) { var _ = (nodefs.OnAdder)((*StatFS)(nil))
name = strings.TrimRight(name, "/")
_, ok := fs.entries[name] func (r *StatFS) OnAdd(ctx context.Context) {
if ok { for nm, a := range r.files {
return r.addFile(nm, a)
} }
r.files = nil
}
fs.entries[name] = a func (r *StatFS) AddFile(name string, a fuse.Attr) {
if name == "/" || name == "" { if r.files == nil {
return r.files = map[string]fuse.Attr{}
} }
dir, base := filepath.Split(name) r.files[name] = a
dir = strings.TrimRight(dir, "/")
fs.dirs[dir] = append(fs.dirs[dir], fuse.DirEntry{Name: base, Mode: a.Mode})
fs.Add(dir, &fuse.Attr{Mode: fuse.S_IFDIR | 0755})
} }
func (fs *StatFS) AddFile(name string) { func (r *StatFS) addFile(name string, a fuse.Attr) {
fs.Add(name, &fuse.Attr{Mode: fuse.S_IFREG | 0644}) dir, base := filepath.Split(name)
}
func (fs *StatFS) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) { p := r.EmbeddedInode()
if d := fs.dirs[name]; d != nil {
return &fuse.Attr{Mode: 0755 | fuse.S_IFDIR}, fuse.OK
}
e := fs.entries[name]
if e == nil {
return nil, fuse.ENOENT
}
return e, fuse.OK // Add directories leading up to the file.
} for _, component := range strings.Split(dir, "/") {
if len(component) == 0 {
continue
}
ch := p.GetChild(component)
if ch == nil {
// Create a directory
ch = p.NewPersistentInode(context.Background(), &nodefs.Inode{},
nodefs.NodeAttr{Mode: syscall.S_IFDIR})
// Add it
p.AddChild(component, ch, true)
}
func (fs *StatFS) OpenDir(name string, context *fuse.Context) (stream []fuse.DirEntry, status fuse.Status) { p = ch
entries := fs.dirs[name]
if entries == nil {
return nil, fuse.ENOENT
} }
return entries, fuse.OK
}
func NewStatFS() *StatFS { // Create the file
return &StatFS{ child := p.NewPersistentInode(context.Background(), &nodefs.MemRegularFile{
FileSystem: pathfs.NewDefaultFileSystem(), Data: make([]byte, a.Size),
entries: make(map[string]*fuse.Attr), Attr: a,
dirs: make(map[string][]fuse.DirEntry), }, nodefs.NodeAttr{})
}
// And add it
p.AddChild(base, child, true)
} }
...@@ -7,59 +7,49 @@ ...@@ -7,59 +7,49 @@
package main package main
import ( import (
"context"
"flag" "flag"
"log" "log"
"syscall"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
) )
type HelloFs struct { type HelloRoot struct {
pathfs.FileSystem nodefs.Inode
} }
func (me *HelloFs) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) { func (r *HelloRoot) OnAdd(ctx context.Context) {
switch name { ch := r.NewPersistentInode(
case "file.txt": ctx, &nodefs.MemRegularFile{
return &fuse.Attr{ Data: []byte("file.txt"),
Mode: fuse.S_IFREG | 0644, Size: uint64(len(name)), Attr: fuse.Attr{
}, fuse.OK Mode: 0644,
case "": },
return &fuse.Attr{ }, nodefs.NodeAttr{Ino: 2})
Mode: fuse.S_IFDIR | 0755, r.AddChild("file.txt", ch, false)
}, fuse.OK
}
return nil, fuse.ENOENT
} }
func (me *HelloFs) OpenDir(name string, context *fuse.Context) (c []fuse.DirEntry, code fuse.Status) { func (r *HelloRoot) Getattr(ctx context.Context, fh nodefs.FileHandle, out *fuse.AttrOut) syscall.Errno {
if name == "" { out.Mode = 0755
c = []fuse.DirEntry{{Name: "file.txt", Mode: fuse.S_IFREG}} return 0
return c, fuse.OK
}
return nil, fuse.ENOENT
} }
func (me *HelloFs) Open(name string, flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) { var _ = (nodefs.Getattrer)((*HelloRoot)(nil))
if name != "file.txt" { var _ = (nodefs.OnAdder)((*HelloRoot)(nil))
return nil, fuse.ENOENT
}
if flags&fuse.O_ANYWRITE != 0 {
return nil, fuse.EPERM
}
return nodefs.NewDataFile([]byte(name)), fuse.OK
}
func main() { func main() {
debug := flag.Bool("debug", false, "print debug data")
flag.Parse() flag.Parse()
if len(flag.Args()) < 1 { if len(flag.Args()) < 1 {
log.Fatal("Usage:\n hello MOUNTPOINT") log.Fatal("Usage:\n hello MOUNTPOINT")
} }
nfs := pathfs.NewPathNodeFs(&HelloFs{FileSystem: pathfs.NewDefaultFileSystem()}, nil) opts := &nodefs.Options{}
server, _, err := nodefs.MountRoot(flag.Arg(0), nfs.Root(), nil) opts.Debug = *debug
server, err := nodefs.Mount(flag.Arg(0), &HelloRoot{}, opts)
if err != nil { if err != nil {
log.Fatalf("Mount fail: %v\n", err) log.Fatalf("Mount fail: %v\n", err)
} }
server.Serve() server.Wait()
} }
...@@ -14,14 +14,11 @@ import ( ...@@ -14,14 +14,11 @@ import (
"os" "os"
"os/signal" "os/signal"
"path" "path"
"path/filepath"
"runtime/pprof" "runtime/pprof"
"syscall" "syscall"
"time" "time"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
) )
func writeMemProfile(fn string, sigs <-chan os.Signal) { func writeMemProfile(fn string, sigs <-chan os.Signal) {
...@@ -48,7 +45,6 @@ func main() { ...@@ -48,7 +45,6 @@ func main() {
// Scans the arg list and sets up flags // Scans the arg list and sets up flags
debug := flag.Bool("debug", false, "print debugging messages.") debug := flag.Bool("debug", false, "print debugging messages.")
other := flag.Bool("allow-other", false, "mount with -o allowother.") other := flag.Bool("allow-other", false, "mount with -o allowother.")
enableLinks := flag.Bool("l", false, "Enable hard link support")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to this file") cpuprofile := flag.String("cpuprofile", "", "write cpu profile to this file")
memprofile := flag.String("memprofile", "", "write memory profile to this file") memprofile := flag.String("memprofile", "", "write memory profile to this file")
flag.Parse() flag.Parse()
...@@ -78,36 +74,26 @@ func main() { ...@@ -78,36 +74,26 @@ func main() {
fmt.Printf("Note: You must unmount gracefully, otherwise the profile file(s) will stay empty!\n") fmt.Printf("Note: You must unmount gracefully, otherwise the profile file(s) will stay empty!\n")
} }
var finalFs pathfs.FileSystem
orig := flag.Arg(1) orig := flag.Arg(1)
loopbackfs := pathfs.NewLoopbackFileSystem(orig) loopbackRoot, err := nodefs.NewLoopbackRoot(orig)
finalFs = loopbackfs if err != nil {
log.Fatalf("NewLoopbackRoot(%s): %v\n", orig, err)
}
sec := time.Second
opts := &nodefs.Options{ opts := &nodefs.Options{
// These options are to be compatible with libfuse defaults, // These options are to be compatible with libfuse defaults,
// making benchmarking easier. // making benchmarking easier.
NegativeTimeout: time.Second, AttrTimeout: &sec,
AttrTimeout: time.Second, EntryTimeout: &sec,
EntryTimeout: time.Second,
}
// Enable ClientInodes so hard links work
pathFsOpts := &pathfs.PathNodeFsOptions{ClientInodes: *enableLinks}
pathFs := pathfs.NewPathNodeFs(finalFs, pathFsOpts)
conn := nodefs.NewFileSystemConnector(pathFs.Root(), opts)
mountPoint := flag.Arg(0)
origAbs, _ := filepath.Abs(orig)
mOpts := &fuse.MountOptions{
AllowOther: *other,
Name: "loopbackfs",
FsName: origAbs,
Debug: *debug,
} }
state, err := fuse.NewServer(conn.RawFS(), mountPoint, mOpts) opts.Debug = *debug
opts.AllowOther = *other
server, err := nodefs.Mount(flag.Arg(0), loopbackRoot, opts)
if err != nil { if err != nil {
fmt.Printf("Mount fail: %v\n", err) log.Fatalf("Mount fail: %v\n", err)
os.Exit(1)
} }
fmt.Println("Mounted!") fmt.Println("Mounted!")
state.Serve() server.Wait()
} }
...@@ -9,9 +9,9 @@ import ( ...@@ -9,9 +9,9 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
"github.com/hanwen/go-fuse/zipfs" "github.com/hanwen/go-fuse/zipfs"
) )
...@@ -25,15 +25,19 @@ func main() { ...@@ -25,15 +25,19 @@ func main() {
os.Exit(2) os.Exit(2)
} }
fs := zipfs.NewMultiZipFs() fs := &zipfs.MultiZipFs{}
nfs := pathfs.NewPathNodeFs(fs, nil) sec := time.Second
opts := nodefs.NewOptions() opts := nodefs.Options{
EntryTimeout: &sec,
AttrTimeout: &sec,
DefaultPermissions: true,
}
opts.Debug = *debug opts.Debug = *debug
state, _, err := nodefs.MountRoot(flag.Arg(0), nfs.Root(), opts) server, err := nodefs.Mount(flag.Arg(0), fs, &opts)
if err != nil { if err != nil {
fmt.Printf("Mount fail: %v\n", err) fmt.Printf("Mount fail: %v\n", err)
os.Exit(1) os.Exit(1)
} }
state.Serve() server.Serve()
} }
...@@ -14,11 +14,12 @@ import ( ...@@ -14,11 +14,12 @@ import (
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
"syscall"
"time" "time"
"github.com/hanwen/go-fuse/benchmark" "github.com/hanwen/go-fuse/benchmark"
"github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/pathfs" "github.com/hanwen/go-fuse/nodefs"
) )
func main() { func main() {
...@@ -27,7 +28,7 @@ func main() { ...@@ -27,7 +28,7 @@ func main() {
profile := flag.String("profile", "", "record cpu profile.") profile := flag.String("profile", "", "record cpu profile.")
mem_profile := flag.String("mem-profile", "", "record memory profile.") mem_profile := flag.String("mem-profile", "", "record memory profile.")
command := flag.String("run", "", "run this command after mounting.") command := flag.String("run", "", "run this command after mounting.")
ttl := flag.Float64("ttl", 1.0, "attribute/entry cache TTL.") ttl := flag.Duration("ttl", time.Second, "attribute/entry cache TTL.")
flag.Parse() flag.Parse()
if flag.NArg() < 2 { if flag.NArg() < 2 {
fmt.Fprintf(os.Stderr, "usage: %s MOUNTPOINT FILENAMES-FILE\n", os.Args[0]) fmt.Fprintf(os.Stderr, "usage: %s MOUNTPOINT FILENAMES-FILE\n", os.Args[0])
...@@ -48,21 +49,21 @@ func main() { ...@@ -48,21 +49,21 @@ func main() {
log.Fatalf("os.Create: %v", err) log.Fatalf("os.Create: %v", err)
} }
} }
fs := benchmark.NewStatFS()
fs := &benchmark.StatFS{}
lines := benchmark.ReadLines(flag.Arg(1)) lines := benchmark.ReadLines(flag.Arg(1))
for _, l := range lines { for _, l := range lines {
fs.AddFile(l) fs.AddFile(strings.TrimSpace(l),
fuse.Attr{Mode: syscall.S_IFREG})
} }
nfs := pathfs.NewPathNodeFs(fs, nil)
opts := &nodefs.Options{ opts := &nodefs.Options{
AttrTimeout: time.Duration(*ttl * float64(time.Second)), AttrTimeout: ttl,
EntryTimeout: time.Duration(*ttl * float64(time.Second)), EntryTimeout: ttl,
Debug: *debug,
} }
state, _, err := nodefs.MountRoot(flag.Arg(0), nfs.Root(), opts) opts.Debug = *debug
server, err := nodefs.Mount(flag.Arg(0), fs, opts)
if err != nil { if err != nil {
fmt.Printf("Mount fail: %v\n", err) log.Fatalf("Mount fail: %v\n", err)
os.Exit(1)
} }
runtime.GC() runtime.GC()
...@@ -78,7 +79,7 @@ func main() { ...@@ -78,7 +79,7 @@ func main() {
cmd.Start() cmd.Start()
} }
state.Serve() server.Wait()
if memProfFile != nil { if memProfFile != nil {
pprof.WriteHeapProfile(memProfFile) pprof.WriteHeapProfile(memProfFile)
} }
......
...@@ -16,7 +16,7 @@ import ( ...@@ -16,7 +16,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/nodefs"
"github.com/hanwen/go-fuse/zipfs" "github.com/hanwen/go-fuse/zipfs"
) )
...@@ -26,7 +26,7 @@ func main() { ...@@ -26,7 +26,7 @@ func main() {
profile := flag.String("profile", "", "record cpu profile.") profile := flag.String("profile", "", "record cpu profile.")
mem_profile := flag.String("mem-profile", "", "record memory profile.") mem_profile := flag.String("mem-profile", "", "record memory profile.")
command := flag.String("run", "", "run this command after mounting.") command := flag.String("run", "", "run this command after mounting.")
ttl := flag.Float64("ttl", 1.0, "attribute/entry cache TTL.") ttl := flag.Duration("ttl", time.Second, "attribute/entry cache TTL.")
flag.Parse() flag.Parse()
if flag.NArg() < 2 { if flag.NArg() < 2 {
fmt.Fprintf(os.Stderr, "usage: %s MOUNTPOINT ZIP-FILE\n", os.Args[0]) fmt.Fprintf(os.Stderr, "usage: %s MOUNTPOINT ZIP-FILE\n", os.Args[0])
...@@ -55,11 +55,12 @@ func main() { ...@@ -55,11 +55,12 @@ func main() {
} }
opts := &nodefs.Options{ opts := &nodefs.Options{
AttrTimeout: time.Duration(*ttl * float64(time.Second)), AttrTimeout: ttl,
EntryTimeout: time.Duration(*ttl * float64(time.Second)), EntryTimeout: ttl,
Debug: *debug, DefaultPermissions: true,
} }
state, _, err := nodefs.MountRoot(flag.Arg(0), root, opts) opts.Debug = *debug
server, err := nodefs.Mount(flag.Arg(0), root, opts)
if err != nil { if err != nil {
fmt.Printf("Mount fail: %v\n", err) fmt.Printf("Mount fail: %v\n", err)
os.Exit(1) os.Exit(1)
...@@ -78,7 +79,7 @@ func main() { ...@@ -78,7 +79,7 @@ func main() {
cmd.Start() cmd.Start()
} }
state.Serve() server.Wait()
if memProfFile != nil { if memProfFile != nil {
pprof.WriteHeapProfile(memProfFile) pprof.WriteHeapProfile(memProfFile)
} }
......
...@@ -15,60 +15,60 @@ import ( ...@@ -15,60 +15,60 @@ import (
) )
const ( const (
_OP_LOOKUP = int32(1) _OP_LOOKUP = uint32(1)
_OP_FORGET = int32(2) _OP_FORGET = uint32(2)
_OP_GETATTR = int32(3) _OP_GETATTR = uint32(3)
_OP_SETATTR = int32(4) _OP_SETATTR = uint32(4)
_OP_READLINK = int32(5) _OP_READLINK = uint32(5)
_OP_SYMLINK = int32(6) _OP_SYMLINK = uint32(6)
_OP_MKNOD = int32(8) _OP_MKNOD = uint32(8)
_OP_MKDIR = int32(9) _OP_MKDIR = uint32(9)
_OP_UNLINK = int32(10) _OP_UNLINK = uint32(10)
_OP_RMDIR = int32(11) _OP_RMDIR = uint32(11)
_OP_RENAME = int32(12) _OP_RENAME = uint32(12)
_OP_LINK = int32(13) _OP_LINK = uint32(13)
_OP_OPEN = int32(14) _OP_OPEN = uint32(14)
_OP_READ = int32(15) _OP_READ = uint32(15)
_OP_WRITE = int32(16) _OP_WRITE = uint32(16)
_OP_STATFS = int32(17) _OP_STATFS = uint32(17)
_OP_RELEASE = int32(18) _OP_RELEASE = uint32(18)
_OP_FSYNC = int32(20) _OP_FSYNC = uint32(20)
_OP_SETXATTR = int32(21) _OP_SETXATTR = uint32(21)
_OP_GETXATTR = int32(22) _OP_GETXATTR = uint32(22)
_OP_LISTXATTR = int32(23) _OP_LISTXATTR = uint32(23)
_OP_REMOVEXATTR = int32(24) _OP_REMOVEXATTR = uint32(24)
_OP_FLUSH = int32(25) _OP_FLUSH = uint32(25)
_OP_INIT = int32(26) _OP_INIT = uint32(26)
_OP_OPENDIR = int32(27) _OP_OPENDIR = uint32(27)
_OP_READDIR = int32(28) _OP_READDIR = uint32(28)
_OP_RELEASEDIR = int32(29) _OP_RELEASEDIR = uint32(29)
_OP_FSYNCDIR = int32(30) _OP_FSYNCDIR = uint32(30)
_OP_GETLK = int32(31) _OP_GETLK = uint32(31)
_OP_SETLK = int32(32) _OP_SETLK = uint32(32)
_OP_SETLKW = int32(33) _OP_SETLKW = uint32(33)
_OP_ACCESS = int32(34) _OP_ACCESS = uint32(34)
_OP_CREATE = int32(35) _OP_CREATE = uint32(35)
_OP_INTERRUPT = int32(36) _OP_INTERRUPT = uint32(36)
_OP_BMAP = int32(37) _OP_BMAP = uint32(37)
_OP_DESTROY = int32(38) _OP_DESTROY = uint32(38)
_OP_IOCTL = int32(39) _OP_IOCTL = uint32(39)
_OP_POLL = int32(40) _OP_POLL = uint32(40)
_OP_NOTIFY_REPLY = int32(41) _OP_NOTIFY_REPLY = uint32(41)
_OP_BATCH_FORGET = int32(42) _OP_BATCH_FORGET = uint32(42)
_OP_FALLOCATE = int32(43) // protocol version 19. _OP_FALLOCATE = uint32(43) // protocol version 19.
_OP_READDIRPLUS = int32(44) // protocol version 21. _OP_READDIRPLUS = uint32(44) // protocol version 21.
_OP_RENAME2 = int32(45) // protocol version 23. _OP_RENAME2 = uint32(45) // protocol version 23.
_OP_LSEEK = int32(46) _OP_LSEEK = uint32(46)
_OP_COPY_FILE_RANGE = int32(47) _OP_COPY_FILE_RANGE = uint32(47)
// The following entries don't have to be compatible across Go-FUSE versions. // The following entries don't have to be compatible across Go-FUSE versions.
_OP_NOTIFY_INVAL_ENTRY = int32(100) _OP_NOTIFY_INVAL_ENTRY = uint32(100)
_OP_NOTIFY_INVAL_INODE = int32(101) _OP_NOTIFY_INVAL_INODE = uint32(101)
_OP_NOTIFY_STORE_CACHE = int32(102) _OP_NOTIFY_STORE_CACHE = uint32(102)
_OP_NOTIFY_RETRIEVE_CACHE = int32(103) _OP_NOTIFY_RETRIEVE_CACHE = uint32(103)
_OP_NOTIFY_DELETE = int32(104) // protocol version 18 _OP_NOTIFY_DELETE = uint32(104) // protocol version 18
_OPCODE_COUNT = int32(105) _OPCODE_COUNT = uint32(105)
) )
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
...@@ -510,7 +510,7 @@ type operationHandler struct { ...@@ -510,7 +510,7 @@ type operationHandler struct {
var operationHandlers []*operationHandler var operationHandlers []*operationHandler
func operationName(op int32) string { func operationName(op uint32) string {
h := getHandler(op) h := getHandler(op)
if h == nil { if h == nil {
return "unknown" return "unknown"
...@@ -518,7 +518,7 @@ func operationName(op int32) string { ...@@ -518,7 +518,7 @@ func operationName(op int32) string {
return h.Name return h.Name
} }
func getHandler(o int32) *operationHandler { func getHandler(o uint32) *operationHandler {
if o >= _OPCODE_COUNT { if o >= _OPCODE_COUNT {
return nil return nil
} }
...@@ -531,12 +531,12 @@ func init() { ...@@ -531,12 +531,12 @@ func init() {
operationHandlers[i] = &operationHandler{Name: fmt.Sprintf("OPCODE-%d", i)} operationHandlers[i] = &operationHandler{Name: fmt.Sprintf("OPCODE-%d", i)}
} }
fileOps := []int32{_OP_READLINK, _OP_NOTIFY_INVAL_ENTRY, _OP_NOTIFY_DELETE} fileOps := []uint32{_OP_READLINK, _OP_NOTIFY_INVAL_ENTRY, _OP_NOTIFY_DELETE}
for _, op := range fileOps { for _, op := range fileOps {
operationHandlers[op].FileNameOut = true operationHandlers[op].FileNameOut = true
} }
for op, sz := range map[int32]uintptr{ for op, sz := range map[uint32]uintptr{
_OP_FORGET: unsafe.Sizeof(ForgetIn{}), _OP_FORGET: unsafe.Sizeof(ForgetIn{}),
_OP_BATCH_FORGET: unsafe.Sizeof(_BatchForgetIn{}), _OP_BATCH_FORGET: unsafe.Sizeof(_BatchForgetIn{}),
_OP_GETATTR: unsafe.Sizeof(GetAttrIn{}), _OP_GETATTR: unsafe.Sizeof(GetAttrIn{}),
...@@ -578,7 +578,7 @@ func init() { ...@@ -578,7 +578,7 @@ func init() {
operationHandlers[op].InputSize = sz operationHandlers[op].InputSize = sz
} }
for op, sz := range map[int32]uintptr{ for op, sz := range map[uint32]uintptr{
_OP_LOOKUP: unsafe.Sizeof(EntryOut{}), _OP_LOOKUP: unsafe.Sizeof(EntryOut{}),
_OP_GETATTR: unsafe.Sizeof(AttrOut{}), _OP_GETATTR: unsafe.Sizeof(AttrOut{}),
_OP_SETATTR: unsafe.Sizeof(AttrOut{}), _OP_SETATTR: unsafe.Sizeof(AttrOut{}),
...@@ -609,7 +609,7 @@ func init() { ...@@ -609,7 +609,7 @@ func init() {
operationHandlers[op].OutputSize = sz operationHandlers[op].OutputSize = sz
} }
for op, v := range map[int32]string{ for op, v := range map[uint32]string{
_OP_LOOKUP: "LOOKUP", _OP_LOOKUP: "LOOKUP",
_OP_FORGET: "FORGET", _OP_FORGET: "FORGET",
_OP_BATCH_FORGET: "BATCH_FORGET", _OP_BATCH_FORGET: "BATCH_FORGET",
...@@ -664,7 +664,7 @@ func init() { ...@@ -664,7 +664,7 @@ func init() {
operationHandlers[op].Name = v operationHandlers[op].Name = v
} }
for op, v := range map[int32]operationFunc{ for op, v := range map[uint32]operationFunc{
_OP_OPEN: doOpen, _OP_OPEN: doOpen,
_OP_READDIR: doReadDir, _OP_READDIR: doReadDir,
_OP_WRITE: doWrite, _OP_WRITE: doWrite,
...@@ -713,7 +713,7 @@ func init() { ...@@ -713,7 +713,7 @@ func init() {
} }
// Outputs. // Outputs.
for op, f := range map[int32]castPointerFunc{ for op, f := range map[uint32]castPointerFunc{
_OP_LOOKUP: func(ptr unsafe.Pointer) interface{} { return (*EntryOut)(ptr) }, _OP_LOOKUP: func(ptr unsafe.Pointer) interface{} { return (*EntryOut)(ptr) },
_OP_OPEN: func(ptr unsafe.Pointer) interface{} { return (*OpenOut)(ptr) }, _OP_OPEN: func(ptr unsafe.Pointer) interface{} { return (*OpenOut)(ptr) },
_OP_OPENDIR: func(ptr unsafe.Pointer) interface{} { return (*OpenOut)(ptr) }, _OP_OPENDIR: func(ptr unsafe.Pointer) interface{} { return (*OpenOut)(ptr) },
...@@ -738,7 +738,7 @@ func init() { ...@@ -738,7 +738,7 @@ func init() {
} }
// Inputs. // Inputs.
for op, f := range map[int32]castPointerFunc{ for op, f := range map[uint32]castPointerFunc{
_OP_FLUSH: func(ptr unsafe.Pointer) interface{} { return (*FlushIn)(ptr) }, _OP_FLUSH: func(ptr unsafe.Pointer) interface{} { return (*FlushIn)(ptr) },
_OP_GETATTR: func(ptr unsafe.Pointer) interface{} { return (*GetAttrIn)(ptr) }, _OP_GETATTR: func(ptr unsafe.Pointer) interface{} { return (*GetAttrIn)(ptr) },
_OP_SETXATTR: func(ptr unsafe.Pointer) interface{} { return (*SetXAttrIn)(ptr) }, _OP_SETXATTR: func(ptr unsafe.Pointer) interface{} { return (*SetXAttrIn)(ptr) },
...@@ -775,7 +775,7 @@ func init() { ...@@ -775,7 +775,7 @@ func init() {
} }
// File name args. // File name args.
for op, count := range map[int32]int{ for op, count := range map[uint32]int{
_OP_CREATE: 1, _OP_CREATE: 1,
_OP_SETXATTR: 1, _OP_SETXATTR: 1,
_OP_GETXATTR: 1, _OP_GETXATTR: 1,
......
...@@ -81,7 +81,7 @@ const debugDataDumpMax = 8 // maximum #bytes of request/response data to dump ...@@ -81,7 +81,7 @@ const debugDataDumpMax = 8 // maximum #bytes of request/response data to dump
func (r *request) InputDebug() string { func (r *request) InputDebug() string {
val := "" val := ""
if r.handler.DecodeIn != nil { if r.handler != nil && r.handler.DecodeIn != nil {
val = fmt.Sprintf("%v ", Print(r.handler.DecodeIn(r.inData))) val = fmt.Sprintf("%v ", Print(r.handler.DecodeIn(r.inData)))
} }
...@@ -107,7 +107,7 @@ func (r *request) InputDebug() string { ...@@ -107,7 +107,7 @@ func (r *request) InputDebug() string {
func (r *request) OutputDebug() string { func (r *request) OutputDebug() string {
var dataStr string var dataStr string
if r.handler.DecodeOut != nil && r.handler.OutputSize > 0 { if r.handler != nil && r.handler.DecodeOut != nil && r.handler.OutputSize > 0 {
dataStr = Print(r.handler.DecodeOut(r.outData())) dataStr = Print(r.handler.DecodeOut(r.outData()))
} }
...@@ -118,7 +118,7 @@ func (r *request) OutputDebug() string { ...@@ -118,7 +118,7 @@ func (r *request) OutputDebug() string {
flatStr := "" flatStr := ""
if r.flatDataSize() > 0 { if r.flatDataSize() > 0 {
if r.handler.FileNameOut { if r.handler != nil && r.handler.FileNameOut {
s := strings.TrimRight(string(r.flatData), "\x00") s := strings.TrimRight(string(r.flatData), "\x00")
flatStr = fmt.Sprintf(" %q", s) flatStr = fmt.Sprintf(" %q", s)
} else { } else {
......
...@@ -603,7 +603,7 @@ type Caller struct { ...@@ -603,7 +603,7 @@ type Caller struct {
type InHeader struct { type InHeader struct {
Length uint32 Length uint32
Opcode int32 Opcode uint32
Unique uint64 Unique uint64
NodeId uint64 NodeId uint64
Caller Caller
......
...@@ -60,3 +60,5 @@ To do/To decide ...@@ -60,3 +60,5 @@ To do/To decide
* decide on a final package name * decide on a final package name
* handle less open/create. * handle less open/create.
* Symlink []byte vs string.
This diff is collapsed.
This diff is collapsed.
...@@ -9,17 +9,15 @@ import ( ...@@ -9,17 +9,15 @@ import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"sync" "sync"
"syscall" "syscall"
"testing" "testing"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/internal/testutil"
) )
type keepCacheFile struct { type keepCacheFile struct {
OperationStubs Inode
keepCache bool keepCache bool
mu sync.Mutex mu sync.Mutex
...@@ -27,6 +25,10 @@ type keepCacheFile struct { ...@@ -27,6 +25,10 @@ type keepCacheFile struct {
count int count int
} }
var _ = (Reader)((*keepCacheFile)(nil))
var _ = (Opener)((*keepCacheFile)(nil))
var _ = (Getattrer)((*keepCacheFile)(nil))
func (f *keepCacheFile) setContent(delta int) { func (f *keepCacheFile) setContent(delta int) {
f.mu.Lock() f.mu.Lock()
defer f.mu.Unlock() defer f.mu.Unlock()
...@@ -44,7 +46,7 @@ func (f *keepCacheFile) Open(ctx context.Context, flags uint32) (FileHandle, uin ...@@ -44,7 +46,7 @@ func (f *keepCacheFile) Open(ctx context.Context, flags uint32) (FileHandle, uin
return nil, fl, OK return nil, fl, OK
} }
func (f *keepCacheFile) Getattr(ctx context.Context, out *fuse.AttrOut) syscall.Errno { func (f *keepCacheFile) Getattr(ctx context.Context, fh FileHandle, out *fuse.AttrOut) syscall.Errno {
f.mu.Lock() f.mu.Lock()
defer f.mu.Unlock() defer f.mu.Unlock()
out.Size = uint64(len(f.content)) out.Size = uint64(len(f.content))
...@@ -62,13 +64,15 @@ func (f *keepCacheFile) Read(ctx context.Context, fh FileHandle, dest []byte, of ...@@ -62,13 +64,15 @@ func (f *keepCacheFile) Read(ctx context.Context, fh FileHandle, dest []byte, of
} }
type keepCacheRoot struct { type keepCacheRoot struct {
OperationStubs Inode
keep, nokeep *keepCacheFile keep, nokeep *keepCacheFile
} }
var _ = (OnAdder)((*keepCacheRoot)(nil))
func (r *keepCacheRoot) OnAdd(ctx context.Context) { func (r *keepCacheRoot) OnAdd(ctx context.Context) {
i := r.Inode() i := &r.Inode
r.keep = &keepCacheFile{ r.keep = &keepCacheFile{
keepCache: true, keepCache: true,
...@@ -87,18 +91,9 @@ func (r *keepCacheRoot) OnAdd(ctx context.Context) { ...@@ -87,18 +91,9 @@ func (r *keepCacheRoot) OnAdd(ctx context.Context) {
// invalidation triggers if mtime or file size is changed, so only // invalidation triggers if mtime or file size is changed, so only
// change content but no metadata. // change content but no metadata.
func TestKeepCache(t *testing.T) { func TestKeepCache(t *testing.T) {
mntDir := testutil.TempDir()
defer os.RemoveAll(mntDir)
root := &keepCacheRoot{} root := &keepCacheRoot{}
server, err := Mount(mntDir, root, &Options{ mntDir, clean := testMount(t, root, nil)
MountOptions: fuse.MountOptions{ defer clean()
Debug: testutil.VerboseTest(),
},
FirstAutomaticIno: 1,
// no caching.
})
defer server.Unmount()
c1, err := ioutil.ReadFile(mntDir + "/keep") c1, err := ioutil.ReadFile(mntDir + "/keep")
if err != nil { if err != nil {
t.Fatalf("read keep 1: %v", err) t.Fatalf("read keep 1: %v", err)
...@@ -113,7 +108,7 @@ func TestKeepCache(t *testing.T) { ...@@ -113,7 +108,7 @@ func TestKeepCache(t *testing.T) {
t.Errorf("keep read 2 got %q want read 1 %q", c2, c1) t.Errorf("keep read 2 got %q want read 1 %q", c2, c1)
} }
if s := root.keep.Inode().NotifyContent(0, 100); s != OK { if s := root.keep.Inode.NotifyContent(0, 100); s != OK {
t.Errorf("NotifyContent: %v", s) t.Errorf("NotifyContent: %v", s)
} }
......
This diff is collapsed.
...@@ -13,23 +13,24 @@ import ( ...@@ -13,23 +13,24 @@ import (
"testing" "testing"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/internal/testutil"
) )
type dioRoot struct { type dioRoot struct {
OperationStubs Inode
} }
func (r *dioRoot) OnAdd(ctx context.Context) { func (r *dioRoot) OnAdd(ctx context.Context) {
r.Inode().AddChild("file", r.Inode().NewInode(ctx, &dioFile{}, NodeAttr{}), false) r.Inode.AddChild("file", r.Inode.NewInode(ctx, &dioFile{}, NodeAttr{}), false)
} }
// A file handle that pretends that every hole/data starts at // A file handle that pretends that every hole/data starts at
// multiples of 1024 // multiples of 1024
type dioFH struct { type dioFH struct {
FileHandleStubs
} }
var _ = (FileLseeker)((*dioFH)(nil))
var _ = (FileReader)((*dioFH)(nil))
func (f *dioFH) Lseek(ctx context.Context, off uint64, whence uint32) (uint64, syscall.Errno) { func (f *dioFH) Lseek(ctx context.Context, off uint64, whence uint32) (uint64, syscall.Errno) {
next := (off + 1023) & (^uint64(1023)) next := (off + 1023) & (^uint64(1023))
return next, OK return next, OK
...@@ -42,27 +43,19 @@ func (fh *dioFH) Read(ctx context.Context, data []byte, off int64) (fuse.ReadRes ...@@ -42,27 +43,19 @@ func (fh *dioFH) Read(ctx context.Context, data []byte, off int64) (fuse.ReadRes
// overrides Open so it can return a dioFH file handle // overrides Open so it can return a dioFH file handle
type dioFile struct { type dioFile struct {
OperationStubs Inode
} }
var _ = (Opener)((*dioFile)(nil))
func (f *dioFile) Open(ctx context.Context, flags uint32) (fh FileHandle, fuseFlags uint32, errno syscall.Errno) { func (f *dioFile) Open(ctx context.Context, flags uint32) (fh FileHandle, fuseFlags uint32, errno syscall.Errno) {
return &dioFH{}, fuse.FOPEN_DIRECT_IO, OK return &dioFH{}, fuse.FOPEN_DIRECT_IO, OK
} }
func TestDirectIO(t *testing.T) { func TestDirectIO(t *testing.T) {
root := &dioRoot{} root := &dioRoot{}
mntDir := testutil.TempDir() mntDir, clean := testMount(t, root, nil)
defer clean()
defer os.RemoveAll(mntDir)
server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{
Debug: testutil.VerboseTest(),
},
FirstAutomaticIno: 1,
// no caching.
})
defer server.Unmount()
f, err := os.Open(mntDir + "/file") f, err := os.Open(mntDir + "/file")
if err != nil { if err != nil {
......
...@@ -24,6 +24,20 @@ type loopbackFile struct { ...@@ -24,6 +24,20 @@ type loopbackFile struct {
fd int fd int
} }
var _ = (FileHandle)((*loopbackFile)(nil))
var _ = (FileReleaser)((*loopbackFile)(nil))
var _ = (FileGetattrer)((*loopbackFile)(nil))
var _ = (FileReader)((*loopbackFile)(nil))
var _ = (FileWriter)((*loopbackFile)(nil))
var _ = (FileGetlker)((*loopbackFile)(nil))
var _ = (FileSetlker)((*loopbackFile)(nil))
var _ = (FileSetlkwer)((*loopbackFile)(nil))
var _ = (FileLseeker)((*loopbackFile)(nil))
var _ = (FileFlusher)((*loopbackFile)(nil))
var _ = (FileFsyncer)((*loopbackFile)(nil))
var _ = (FileSetattrer)((*loopbackFile)(nil))
var _ = (FileAllocater)((*loopbackFile)(nil))
func (f *loopbackFile) Read(ctx context.Context, buf []byte, off int64) (res fuse.ReadResult, errno syscall.Errno) { func (f *loopbackFile) Read(ctx context.Context, buf []byte, off int64) (res fuse.ReadResult, errno syscall.Errno) {
r := fuse.ReadResultFd(uintptr(f.fd), off, len(buf)) r := fuse.ReadResultFd(uintptr(f.fd), off, len(buf))
return r, OK return r, OK
......
...@@ -13,6 +13,8 @@ import ( ...@@ -13,6 +13,8 @@ import (
"sync" "sync"
"syscall" "syscall"
"unsafe" "unsafe"
"github.com/hanwen/go-fuse/fuse"
) )
type parentData struct { type parentData struct {
...@@ -50,10 +52,14 @@ func (i *NodeAttr) Reserved() bool { ...@@ -50,10 +52,14 @@ func (i *NodeAttr) Reserved() bool {
// Operations instances, which is the extension interface for file // Operations instances, which is the extension interface for file
// systems. One can create fully-formed trees of Inodes ahead of time // systems. One can create fully-formed trees of Inodes ahead of time
// by creating "persistent" Inodes. // by creating "persistent" Inodes.
//
// The Inode struct contains a lock, so it should not be
// copied. Inodes should be obtained by calling Inode.NewInode() or
// Inode.NewPersistentInode().
type Inode struct { type Inode struct {
nodeAttr NodeAttr nodeAttr NodeAttr
ops Operations ops InodeEmbedder
bridge *rawBridge bridge *rawBridge
// Following data is mutable. // Following data is mutable.
...@@ -90,16 +96,30 @@ type Inode struct { ...@@ -90,16 +96,30 @@ type Inode struct {
parents map[parentData]struct{} parents map[parentData]struct{}
} }
func (n *Inode) dirOps() DirOperations { func (n *Inode) embed() *Inode {
return n.ops.(DirOperations) return n
} }
func (n *Inode) fileOps() FileOperations { func (n *Inode) EmbeddedInode() *Inode {
return n.ops.(FileOperations) return n
}
func initInode(n *Inode, ops InodeEmbedder, attr NodeAttr, bridge *rawBridge, persistent bool) {
n.ops = ops
n.nodeAttr = attr
n.bridge = bridge
n.persistent = persistent
n.parents = make(map[parentData]struct{})
if attr.Mode == fuse.S_IFDIR {
n.children = make(map[string]*Inode)
}
} }
func (n *Inode) linkOps() SymlinkOperations { // Set node ID and mode in EntryOut
return n.ops.(SymlinkOperations) func (n *Inode) setEntryOut(out *fuse.EntryOut) {
out.NodeId = n.nodeAttr.Ino
out.Ino = n.nodeAttr.Ino
out.Mode = (out.Attr.Mode & 07777) | n.nodeAttr.Mode
} }
// NodeAttr returns the (Ino, Gen) tuple for this node. // NodeAttr returns the (Ino, Gen) tuple for this node.
...@@ -117,14 +137,28 @@ func (n *Inode) Root() *Inode { ...@@ -117,14 +137,28 @@ func (n *Inode) Root() *Inode {
return n.bridge.root return n.bridge.root
} }
func modeStr(m uint32) string {
return map[uint32]string{
syscall.S_IFREG: "reg",
syscall.S_IFLNK: "lnk",
syscall.S_IFDIR: "dir",
syscall.S_IFSOCK: "soc",
syscall.S_IFIFO: "pip",
syscall.S_IFCHR: "chr",
syscall.S_IFBLK: "blk",
}[m]
}
// debugString is used for debugging. Racy. // debugString is used for debugging. Racy.
func (n *Inode) debugString() string { func (n *Inode) String() string {
n.mu.Lock()
defer n.mu.Unlock()
var ss []string var ss []string
for nm, ch := range n.children { for nm, ch := range n.children {
ss = append(ss, fmt.Sprintf("%q=%d", nm, ch.nodeAttr.Ino)) ss = append(ss, fmt.Sprintf("%q=%d[%s]", nm, ch.nodeAttr.Ino, modeStr(ch.nodeAttr.Mode)))
} }
return fmt.Sprintf("%d: %s", n.nodeAttr, strings.Join(ss, ",")) return fmt.Sprintf("%d[%s]: %s", n.nodeAttr.Ino, modeStr(n.nodeAttr.Mode), strings.Join(ss, ","))
} }
// sortNodes rearranges inode group in consistent order. // sortNodes rearranges inode group in consistent order.
...@@ -214,7 +248,7 @@ func (n *Inode) Forgotten() bool { ...@@ -214,7 +248,7 @@ func (n *Inode) Forgotten() bool {
// Operations returns the object implementing the file system // Operations returns the object implementing the file system
// operations. // operations.
func (n *Inode) Operations() Operations { func (n *Inode) Operations() InodeEmbedder {
return n.ops return n.ops
} }
...@@ -273,7 +307,7 @@ func (iparent *Inode) setEntry(name string, ichild *Inode) { ...@@ -273,7 +307,7 @@ func (iparent *Inode) setEntry(name string, ichild *Inode) {
// NewPersistentInode returns an Inode whose lifetime is not in // NewPersistentInode returns an Inode whose lifetime is not in
// control of the kernel. // control of the kernel.
func (n *Inode) NewPersistentInode(ctx context.Context, node Operations, id NodeAttr) *Inode { func (n *Inode) NewPersistentInode(ctx context.Context, node InodeEmbedder, id NodeAttr) *Inode {
return n.newInode(ctx, node, id, true) return n.newInode(ctx, node, id, true)
} }
...@@ -284,16 +318,16 @@ func (n *Inode) ForgetPersistent() { ...@@ -284,16 +318,16 @@ func (n *Inode) ForgetPersistent() {
n.removeRef(0, true) n.removeRef(0, true)
} }
// NewInode returns an inode for the given Operations. The mode should // NewInode returns an inode for the given InodeEmbedder. The mode
// be standard mode argument (eg. S_IFDIR). The inode number in id.Ino // should be standard mode argument (eg. S_IFDIR). The inode number in
// argument is used to implement hard-links. If it is given, and // id.Ino argument is used to implement hard-links. If it is given,
// another node with the same ID is known, that will node will be // and another node with the same ID is known, that will node will be
// returned, and the passed-in `node` is ignored. // returned, and the passed-in `node` is ignored.
func (n *Inode) NewInode(ctx context.Context, ops Operations, id NodeAttr) *Inode { func (n *Inode) NewInode(ctx context.Context, node InodeEmbedder, id NodeAttr) *Inode {
return n.newInode(ctx, ops, id, false) return n.newInode(ctx, node, id, false)
} }
func (n *Inode) newInode(ctx context.Context, ops Operations, id NodeAttr, persistent bool) *Inode { func (n *Inode) newInode(ctx context.Context, ops InodeEmbedder, id NodeAttr, persistent bool) *Inode {
return n.bridge.newInode(ctx, ops, id, persistent) return n.bridge.newInode(ctx, ops, id, persistent)
} }
...@@ -454,6 +488,22 @@ func (n *Inode) Parent() (string, *Inode) { ...@@ -454,6 +488,22 @@ func (n *Inode) Parent() (string, *Inode) {
return "", nil return "", nil
} }
// RmAllChildren recursively drops a tree, forgetting all persistent
// nodes.
func (n *Inode) RmAllChildren() {
for {
chs := n.Children()
if len(chs) == 0 {
break
}
for nm, ch := range chs {
ch.RmAllChildren()
n.RmChild(nm)
}
}
n.removeRef(0, true)
}
// RmChild removes multiple children. Returns whether the removal // RmChild removes multiple children. Returns whether the removal
// succeeded and whether the node is still live afterward. The removal // succeeded and whether the node is still live afterward. The removal
// is transactional: it only succeeds if all names are children, and // is transactional: it only succeeds if all names are children, and
......
...@@ -6,37 +6,39 @@ package nodefs ...@@ -6,37 +6,39 @@ package nodefs
import ( import (
"context" "context"
"os"
"os/exec" "os/exec"
"syscall" "syscall"
"testing" "testing"
"time" "time"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/internal/testutil"
) )
type interruptRoot struct { type interruptRoot struct {
OperationStubs Inode
child interruptOps child interruptOps
} }
type interruptOps struct { var _ = (Lookuper)((*interruptRoot)(nil))
OperationStubs
interrupted bool
}
func (r *interruptRoot) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*Inode, syscall.Errno) { func (r *interruptRoot) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*Inode, syscall.Errno) {
if name != "file" { if name != "file" {
return nil, syscall.ENOENT return nil, syscall.ENOENT
} }
ch := r.Inode().NewInode(ctx, &r.child, NodeAttr{ ch := r.Inode.NewInode(ctx, &r.child, NodeAttr{
Ino: 2, Ino: 2,
Gen: 1}) Gen: 1})
return ch, OK return ch, OK
} }
type interruptOps struct {
Inode
interrupted bool
}
var _ = (Opener)((*interruptOps)(nil))
func (o *interruptOps) Open(ctx context.Context, flags uint32) (FileHandle, uint32, syscall.Errno) { func (o *interruptOps) Open(ctx context.Context, flags uint32) (FileHandle, uint32, syscall.Errno) {
select { select {
case <-time.After(100 * time.Millisecond): case <-time.After(100 * time.Millisecond):
...@@ -50,22 +52,18 @@ func (o *interruptOps) Open(ctx context.Context, flags uint32) (FileHandle, uint ...@@ -50,22 +52,18 @@ func (o *interruptOps) Open(ctx context.Context, flags uint32) (FileHandle, uint
// This currently doesn't test functionality, but is useful to investigate how // This currently doesn't test functionality, but is useful to investigate how
// INTERRUPT opcodes are handled. // INTERRUPT opcodes are handled.
func TestInterrupt(t *testing.T) { func TestInterrupt(t *testing.T) {
mntDir := testutil.TempDir()
defer os.Remove(mntDir)
root := &interruptRoot{} root := &interruptRoot{}
oneSec := time.Second oneSec := time.Second
server, err := Mount(mntDir, root, &Options{ mntDir, clean := testMount(t, root, &Options{
MountOptions: fuse.MountOptions{
Debug: testutil.VerboseTest(),
},
EntryTimeout: &oneSec, EntryTimeout: &oneSec,
AttrTimeout: &oneSec, AttrTimeout: &oneSec,
}) })
if err != nil { defer func() {
t.Fatal(err) if clean != nil {
} clean()
defer server.Unmount() }
}()
cmd := exec.Command("cat", mntDir+"/file") cmd := exec.Command("cat", mntDir+"/file")
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
...@@ -77,7 +75,9 @@ func TestInterrupt(t *testing.T) { ...@@ -77,7 +75,9 @@ func TestInterrupt(t *testing.T) {
t.Errorf("Kill: %v", err) t.Errorf("Kill: %v", err)
} }
server.Unmount() clean()
clean = nil
if !root.child.interrupted { if !root.child.interrupted {
t.Errorf("open request was not interrupted") t.Errorf("open request was not interrupted")
} }
......
...@@ -6,7 +6,6 @@ package nodefs ...@@ -6,7 +6,6 @@ package nodefs
import ( import (
"context" "context"
"log"
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
...@@ -21,6 +20,32 @@ type loopbackRoot struct { ...@@ -21,6 +20,32 @@ type loopbackRoot struct {
rootDev uint64 rootDev uint64
} }
type loopbackNode struct {
Inode
}
var _ = (Statfser)((*loopbackNode)(nil))
var _ = (Statfser)((*loopbackNode)(nil))
var _ = (Getattrer)((*loopbackNode)(nil))
var _ = (Getxattrer)((*loopbackNode)(nil))
var _ = (Setxattrer)((*loopbackNode)(nil))
var _ = (Removexattrer)((*loopbackNode)(nil))
var _ = (Listxattrer)((*loopbackNode)(nil))
var _ = (Readlinker)((*loopbackNode)(nil))
var _ = (Opener)((*loopbackNode)(nil))
var _ = (CopyFileRanger)((*loopbackNode)(nil))
var _ = (Lookuper)((*loopbackNode)(nil))
var _ = (Opendirer)((*loopbackNode)(nil))
var _ = (Readdirer)((*loopbackNode)(nil))
var _ = (Mkdirer)((*loopbackNode)(nil))
var _ = (Mknoder)((*loopbackNode)(nil))
var _ = (Linker)((*loopbackNode)(nil))
var _ = (Symlinker)((*loopbackNode)(nil))
var _ = (Creater)((*loopbackNode)(nil))
var _ = (Unlinker)((*loopbackNode)(nil))
var _ = (Rmdirer)((*loopbackNode)(nil))
var _ = (Renamer)((*loopbackNode)(nil))
func (n *loopbackNode) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.Errno { func (n *loopbackNode) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.Errno {
s := syscall.Statfs_t{} s := syscall.Statfs_t{}
err := syscall.Statfs(n.path(), &s) err := syscall.Statfs(n.path(), &s)
...@@ -31,8 +56,8 @@ func (n *loopbackNode) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall. ...@@ -31,8 +56,8 @@ func (n *loopbackNode) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.
return OK return OK
} }
func (n *loopbackRoot) Getattr(ctx context.Context, out *fuse.AttrOut) syscall.Errno { func (n *loopbackRoot) Getattr(ctx context.Context, f FileHandle, out *fuse.AttrOut) syscall.Errno {
log.Println("getattr")
st := syscall.Stat_t{} st := syscall.Stat_t{}
err := syscall.Stat(n.rootPath, &st) err := syscall.Stat(n.rootPath, &st)
if err != nil { if err != nil {
...@@ -42,16 +67,12 @@ func (n *loopbackRoot) Getattr(ctx context.Context, out *fuse.AttrOut) syscall.E ...@@ -42,16 +67,12 @@ func (n *loopbackRoot) Getattr(ctx context.Context, out *fuse.AttrOut) syscall.E
return OK return OK
} }
type loopbackNode struct {
OperationStubs
}
func (n *loopbackNode) root() *loopbackRoot { func (n *loopbackNode) root() *loopbackRoot {
return n.Inode().Root().Operations().(*loopbackRoot) return n.Root().Operations().(*loopbackRoot)
} }
func (n *loopbackNode) path() string { func (n *loopbackNode) path() string {
path := n.Inode().Path(nil) path := n.Path(nil)
return filepath.Join(n.root().rootPath, path) return filepath.Join(n.root().rootPath, path)
} }
...@@ -66,7 +87,7 @@ func (n *loopbackNode) Lookup(ctx context.Context, name string, out *fuse.EntryO ...@@ -66,7 +87,7 @@ func (n *loopbackNode) Lookup(ctx context.Context, name string, out *fuse.EntryO
out.Attr.FromStat(&st) out.Attr.FromStat(&st)
node := &loopbackNode{} node := &loopbackNode{}
ch := n.inode().NewInode(ctx, node, n.root().idFromStat(&st)) ch := n.NewInode(ctx, node, n.root().idFromStat(&st))
return ch, 0 return ch, 0
} }
...@@ -85,7 +106,7 @@ func (n *loopbackNode) Mknod(ctx context.Context, name string, mode, rdev uint32 ...@@ -85,7 +106,7 @@ func (n *loopbackNode) Mknod(ctx context.Context, name string, mode, rdev uint32
out.Attr.FromStat(&st) out.Attr.FromStat(&st)
node := &loopbackNode{} node := &loopbackNode{}
ch := n.inode().NewInode(ctx, node, n.root().idFromStat(&st)) ch := n.NewInode(ctx, node, n.root().idFromStat(&st))
return ch, 0 return ch, 0
} }
...@@ -105,7 +126,7 @@ func (n *loopbackNode) Mkdir(ctx context.Context, name string, mode uint32, out ...@@ -105,7 +126,7 @@ func (n *loopbackNode) Mkdir(ctx context.Context, name string, mode uint32, out
out.Attr.FromStat(&st) out.Attr.FromStat(&st)
node := &loopbackNode{} node := &loopbackNode{}
ch := n.inode().NewInode(ctx, node, n.root().idFromStat(&st)) ch := n.NewInode(ctx, node, n.root().idFromStat(&st))
return ch, 0 return ch, 0
} }
...@@ -122,14 +143,14 @@ func (n *loopbackNode) Unlink(ctx context.Context, name string) syscall.Errno { ...@@ -122,14 +143,14 @@ func (n *loopbackNode) Unlink(ctx context.Context, name string) syscall.Errno {
return ToErrno(err) return ToErrno(err)
} }
func toLoopbackNode(op Operations) *loopbackNode { func toLoopbackNode(op InodeEmbedder) *loopbackNode {
if r, ok := op.(*loopbackRoot); ok { if r, ok := op.(*loopbackRoot); ok {
return &r.loopbackNode return &r.loopbackNode
} }
return op.(*loopbackNode) return op.(*loopbackNode)
} }
func (n *loopbackNode) Rename(ctx context.Context, name string, newParent Operations, newName string, flags uint32) syscall.Errno { func (n *loopbackNode) Rename(ctx context.Context, name string, newParent InodeEmbedder, newName string, flags uint32) syscall.Errno {
newParentLoopback := toLoopbackNode(newParent) newParentLoopback := toLoopbackNode(newParent)
if flags&RENAME_EXCHANGE != 0 { if flags&RENAME_EXCHANGE != 0 {
return n.renameExchange(name, newParentLoopback, newName) return n.renameExchange(name, newParentLoopback, newName)
...@@ -176,7 +197,7 @@ func (n *loopbackNode) Create(ctx context.Context, name string, flags uint32, mo ...@@ -176,7 +197,7 @@ func (n *loopbackNode) Create(ctx context.Context, name string, flags uint32, mo
} }
node := &loopbackNode{} node := &loopbackNode{}
ch := n.inode().NewInode(ctx, node, n.root().idFromStat(&st)) ch := n.NewInode(ctx, node, n.root().idFromStat(&st))
lf := NewLoopbackFile(fd) lf := NewLoopbackFile(fd)
return ch, lf, 0, 0 return ch, lf, 0, 0
} }
...@@ -193,13 +214,13 @@ func (n *loopbackNode) Symlink(ctx context.Context, target, name string, out *fu ...@@ -193,13 +214,13 @@ func (n *loopbackNode) Symlink(ctx context.Context, target, name string, out *fu
return nil, ToErrno(err) return nil, ToErrno(err)
} }
node := &loopbackNode{} node := &loopbackNode{}
ch := n.inode().NewInode(ctx, node, n.root().idFromStat(&st)) ch := n.NewInode(ctx, node, n.root().idFromStat(&st))
out.Attr.FromStat(&st) out.Attr.FromStat(&st)
return ch, 0 return ch, 0
} }
func (n *loopbackNode) Link(ctx context.Context, target Operations, name string, out *fuse.EntryOut) (*Inode, syscall.Errno) { func (n *loopbackNode) Link(ctx context.Context, target InodeEmbedder, name string, out *fuse.EntryOut) (*Inode, syscall.Errno) {
p := filepath.Join(n.path(), name) p := filepath.Join(n.path(), name)
targetNode := toLoopbackNode(target) targetNode := toLoopbackNode(target)
...@@ -213,7 +234,7 @@ func (n *loopbackNode) Link(ctx context.Context, target Operations, name string, ...@@ -213,7 +234,7 @@ func (n *loopbackNode) Link(ctx context.Context, target Operations, name string,
return nil, ToErrno(err) return nil, ToErrno(err)
} }
node := &loopbackNode{} node := &loopbackNode{}
ch := n.inode().NewInode(ctx, node, n.root().idFromStat(&st)) ch := n.NewInode(ctx, node, n.root().idFromStat(&st))
out.Attr.FromStat(&st) out.Attr.FromStat(&st)
return ch, 0 return ch, 0
...@@ -258,11 +279,10 @@ func (n *loopbackNode) Readdir(ctx context.Context) (DirStream, syscall.Errno) { ...@@ -258,11 +279,10 @@ func (n *loopbackNode) Readdir(ctx context.Context) (DirStream, syscall.Errno) {
return NewLoopbackDirStream(n.path()) return NewLoopbackDirStream(n.path())
} }
func (n *loopbackNode) Fgetattr(ctx context.Context, f FileHandle, out *fuse.AttrOut) syscall.Errno { func (n *loopbackNode) Getattr(ctx context.Context, f FileHandle, out *fuse.AttrOut) syscall.Errno {
if f != nil { if f != nil {
return f.Getattr(ctx, out) return f.(FileGetattrer).Getattr(ctx, out)
} }
p := n.path() p := n.path()
var err error = nil var err error = nil
...@@ -277,7 +297,7 @@ func (n *loopbackNode) Fgetattr(ctx context.Context, f FileHandle, out *fuse.Att ...@@ -277,7 +297,7 @@ func (n *loopbackNode) Fgetattr(ctx context.Context, f FileHandle, out *fuse.Att
// NewLoopback returns a root node for a loopback file system whose // NewLoopback returns a root node for a loopback file system whose
// root is at the given root. // root is at the given root.
func NewLoopbackRoot(root string) (DirOperations, error) { func NewLoopbackRoot(root string) (InodeEmbedder, error) {
var st syscall.Stat_t var st syscall.Stat_t
err := syscall.Stat(root, &st) err := syscall.Stat(root, &st)
if err != nil { if err != nil {
......
// +build darwin
// Copyright 2019 the Go-FUSE Authors. All rights reserved. // Copyright 2019 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
...@@ -14,19 +16,19 @@ import ( ...@@ -14,19 +16,19 @@ import (
"github.com/hanwen/go-fuse/internal/utimens" "github.com/hanwen/go-fuse/internal/utimens"
) )
func (n *loopbackNode) GetXAttr(ctx context.Context, attr string, dest []byte) (uint32, syscall.Errno) { func (n *loopbackNode) Getxattr(ctx context.Context, attr string, dest []byte) (uint32, syscall.Errno) {
return 0, syscall.ENOSYS return 0, syscall.ENOSYS
} }
func (n *loopbackNode) SetXAttr(ctx context.Context, attr string, data []byte, flags uint32) syscall.Errno { func (n *loopbackNode) Setxattr(ctx context.Context, attr string, data []byte, flags uint32) syscall.Errno {
return syscall.ENOSYS return syscall.ENOSYS
} }
func (n *loopbackNode) RemoveXAttr(ctx context.Context, attr string) syscall.Errno { func (n *loopbackNode) Removexattr(ctx context.Context, attr string) syscall.Errno {
return syscall.ENOSYS return syscall.ENOSYS
} }
func (n *loopbackNode) ListXAttr(ctx context.Context, dest []byte) (uint32, syscall.Errno) { func (n *loopbackNode) Listxattr(ctx context.Context, dest []byte) (uint32, syscall.Errno) {
return 0, syscall.ENOSYS return 0, syscall.ENOSYS
} }
...@@ -95,11 +97,11 @@ func timeToTimeval(t *time.Time) syscall.Timeval { ...@@ -95,11 +97,11 @@ func timeToTimeval(t *time.Time) syscall.Timeval {
} }
// MacOS before High Sierra lacks utimensat() and UTIME_OMIT. // MacOS before High Sierra lacks utimensat() and UTIME_OMIT.
// We emulate using utimes() and extra GetAttr() calls. // We emulate using utimes() and extra Getattr() calls.
func (f *loopbackFile) utimens(a *time.Time, m *time.Time) syscall.Errno { func (f *loopbackFile) utimens(a *time.Time, m *time.Time) syscall.Errno {
var attr fuse.AttrOut var attr fuse.AttrOut
if a == nil || m == nil { if a == nil || m == nil {
errno := f.GetAttr(context.Background(), &attr) errno := f.Getattr(context.Background(), &attr)
if errno != 0 { if errno != 0 {
return errno return errno
} }
...@@ -108,3 +110,9 @@ func (f *loopbackFile) utimens(a *time.Time, m *time.Time) syscall.Errno { ...@@ -108,3 +110,9 @@ func (f *loopbackFile) utimens(a *time.Time, m *time.Time) syscall.Errno {
err := syscall.Futimes(int(f.fd), tv) err := syscall.Futimes(int(f.fd), tv)
return ToErrno(err) return ToErrno(err)
} }
func (n *loopbackNode) CopyFileRange(ctx context.Context, fhIn FileHandle,
offIn uint64, out *Inode, fhOut FileHandle, offOut uint64,
len uint64, flags uint64) (uint32, syscall.Errno) {
return 0, syscall.ENOSYS
}
// +build linux
// Copyright 2019 the Go-FUSE Authors. All rights reserved. // Copyright 2019 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
...@@ -49,7 +51,7 @@ func (n *loopbackNode) renameExchange(name string, newparent *loopbackNode, newN ...@@ -49,7 +51,7 @@ func (n *loopbackNode) renameExchange(name string, newparent *loopbackNode, newN
} }
// Double check that nodes didn't change from under us. // Double check that nodes didn't change from under us.
inode := n.Inode() inode := &n.Inode
if inode.Root() != inode && inode.NodeAttr().Ino != n.root().idFromStat(&st).Ino { if inode.Root() != inode && inode.NodeAttr().Ino != n.root().idFromStat(&st).Ino {
return syscall.EBUSY return syscall.EBUSY
} }
...@@ -57,7 +59,7 @@ func (n *loopbackNode) renameExchange(name string, newparent *loopbackNode, newN ...@@ -57,7 +59,7 @@ func (n *loopbackNode) renameExchange(name string, newparent *loopbackNode, newN
return ToErrno(err) return ToErrno(err)
} }
newinode := newparent.Inode() newinode := &newparent.Inode
if newinode.Root() != newinode && newinode.NodeAttr().Ino != n.root().idFromStat(&st).Ino { if newinode.Root() != newinode && newinode.NodeAttr().Ino != n.root().idFromStat(&st).Ino {
return syscall.EBUSY return syscall.EBUSY
} }
......
// Copyright 2019 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nodefs
import (
"context"
"syscall"
"github.com/hanwen/go-fuse/fuse"
)
// MemRegularFile is a filesystem node that holds a read-only data
// slice in memory.
type MemRegularFile struct {
Inode
Data []byte
Attr fuse.Attr
}
var _ = (Opener)((*MemRegularFile)(nil))
var _ = (Getattrer)((*MemRegularFile)(nil))
var _ = (Reader)((*MemRegularFile)(nil))
var _ = (Flusher)((*MemRegularFile)(nil))
func (f *MemRegularFile) Open(ctx context.Context, flags uint32) (fh FileHandle, fuseFlags uint32, errno syscall.Errno) {
if flags&(syscall.O_RDWR) != 0 || flags&syscall.O_WRONLY != 0 {
return nil, 0, syscall.EPERM
}
return nil, fuse.FOPEN_KEEP_CACHE, OK
}
func (f *MemRegularFile) Getattr(ctx context.Context, fh FileHandle, out *fuse.AttrOut) syscall.Errno {
out.Attr = f.Attr
out.Attr.Size = uint64(len(f.Data))
return OK
}
func (f *MemRegularFile) Flush(ctx context.Context, fh FileHandle) syscall.Errno {
return 0
}
func (f *MemRegularFile) Read(ctx context.Context, fh FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
end := int(off) + len(dest)
if end > len(f.Data) {
end = len(f.Data)
}
return fuse.ReadResultData(f.Data[off:end]), OK
}
// MemSymlink is an inode holding a symlink in memory.
type MemSymlink struct {
Inode
Data []byte
}
var _ = (Readlinker)((*MemSymlink)(nil))
func (l *MemSymlink) Readlink(ctx context.Context) ([]byte, syscall.Errno) {
return l.Data, OK
}
// Copyright 2019 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nodefs
import (
"bytes"
"context"
"io/ioutil"
"math/rand"
"os"
"syscall"
"testing"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/internal/testutil"
)
func testMount(t *testing.T, root InodeEmbedder, opts *Options) (string, func()) {
t.Helper()
mntDir := testutil.TempDir()
if opts == nil {
opts = &Options{
FirstAutomaticIno: 1,
}
}
opts.Debug = testutil.VerboseTest()
server, err := Mount(mntDir, root, opts)
if err != nil {
t.Fatal(err)
}
return mntDir, func() {
server.Unmount()
os.Remove(mntDir)
}
}
func TestDataFile(t *testing.T) {
want := "hello"
root := &Inode{}
mntDir, clean := testMount(t, root, &Options{
FirstAutomaticIno: 1,
OnAdd: func(ctx context.Context) {
n := root.EmbeddedInode()
ch := n.NewPersistentInode(
ctx,
&MemRegularFile{
Data: []byte(want),
Attr: fuse.Attr{
Mode: 0464,
},
},
NodeAttr{})
n.AddChild("file", ch, false)
},
})
defer clean()
var st syscall.Stat_t
if err := syscall.Lstat(mntDir+"/file", &st); err != nil {
t.Fatalf("Lstat: %v", err)
}
if want := uint32(syscall.S_IFREG | 0464); st.Mode != want {
t.Errorf("got mode %o, want %o", st.Mode, want)
}
fd, err := syscall.Open(mntDir+"/file", syscall.O_RDONLY, 0)
if err != nil {
t.Fatalf("Open: %v", err)
}
var buf [1024]byte
n, err := syscall.Read(fd, buf[:])
if err != nil {
t.Errorf("Read: %v", err)
}
if err := syscall.Close(fd); err != nil {
t.Errorf("Close: %v", err)
}
got := string(buf[:n])
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
func TestDataFileLargeRead(t *testing.T) {
root := &Inode{}
data := make([]byte, 256*1024)
rand.Read(data[:])
mntDir, clean := testMount(t, root, &Options{
FirstAutomaticIno: 1,
OnAdd: func(ctx context.Context) {
n := root.EmbeddedInode()
ch := n.NewPersistentInode(
ctx,
&MemRegularFile{
Data: data,
Attr: fuse.Attr{
Mode: 0464,
},
},
NodeAttr{})
n.AddChild("file", ch, false)
},
})
defer clean()
got, err := ioutil.ReadFile(mntDir + "/file")
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
if !bytes.Equal(got, data) {
t.Errorf("roundtrip read had change")
}
}
type SymlinkerRoot struct {
Inode
}
func (s *SymlinkerRoot) Symlink(ctx context.Context, target, name string, out *fuse.EntryOut) (*Inode, syscall.Errno) {
l := &MemSymlink{
Data: []byte(target),
}
ch := s.NewPersistentInode(ctx, l, NodeAttr{Mode: syscall.S_IFLNK})
return ch, 0
}
func TestDataSymlink(t *testing.T) {
root := &SymlinkerRoot{}
mntDir, clean := testMount(t, root, nil)
defer clean()
if err := syscall.Symlink("target", mntDir+"/link"); err != nil {
t.Fatalf("Symlink: %v", err)
}
if got, err := os.Readlink(mntDir + "/link"); err != nil {
t.Fatalf("Readlink: %v", err)
} else if want := "target"; got != want {
t.Errorf("Readlink: got %q want %q", got, want)
}
}
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
// requests. This is a convenience wrapper around NewNodeFS and // requests. This is a convenience wrapper around NewNodeFS and
// fuse.NewServer. If nil is given as options, default settings are // fuse.NewServer. If nil is given as options, default settings are
// applied, which are 1 second entry and attribute timeout. // applied, which are 1 second entry and attribute timeout.
func Mount(dir string, root DirOperations, options *Options) (*fuse.Server, error) { func Mount(dir string, root InodeEmbedder, options *Options) (*fuse.Server, error) {
if options == nil { if options == nil {
oneSec := time.Second oneSec := time.Second
options = &Options{ options = &Options{
......
// Copyright 2019 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nodefs
import (
"context"
"path/filepath"
"syscall"
"testing"
"github.com/hanwen/go-fuse/fuse"
)
func TestReadonlyCreate(t *testing.T) {
root := &Inode{}
mntDir, clean := testMount(t, root, nil)
defer clean()
_, err := syscall.Creat(mntDir+"/test", 0644)
if want := syscall.EROFS; want != err {
t.Fatalf("got err %v, want %v", err, want)
}
}
func TestDefaultPermissions(t *testing.T) {
root := &Inode{}
mntDir, clean := testMount(t, root, &Options{
DefaultPermissions: true,
OnAdd: func(ctx context.Context) {
dir := root.NewPersistentInode(ctx, &Inode{}, NodeAttr{Mode: syscall.S_IFDIR})
file := root.NewPersistentInode(ctx, &Inode{}, NodeAttr{Mode: syscall.S_IFREG})
root.AddChild("dir", dir, false)
root.AddChild("file", file, false)
},
})
defer clean()
for k, v := range map[string]uint32{
"dir": fuse.S_IFDIR | 0755,
"file": fuse.S_IFREG | 0644,
} {
var st syscall.Stat_t
if err := syscall.Lstat(filepath.Join(mntDir, k), &st); err != nil {
t.Error("Lstat", err)
} else if st.Mode != v {
t.Errorf("got %o want %o", st.Mode, v)
}
}
}
...@@ -28,7 +28,7 @@ type testCase struct { ...@@ -28,7 +28,7 @@ type testCase struct {
origDir string origDir string
mntDir string mntDir string
loopback DirOperations loopback InodeEmbedder
rawFS fuse.RawFileSystem rawFS fuse.RawFileSystem
server *fuse.Server server *fuse.Server
} }
...@@ -437,7 +437,7 @@ func TestNotifyEntry(t *testing.T) { ...@@ -437,7 +437,7 @@ func TestNotifyEntry(t *testing.T) {
t.Fatalf("got after %#v, want %#v", after, st) t.Fatalf("got after %#v, want %#v", after, st)
} }
if errno := tc.loopback.Inode().NotifyEntry("file"); errno != 0 { if errno := tc.loopback.EmbeddedInode().NotifyEntry("file"); errno != 0 {
t.Errorf("notify failed: %v", errno) t.Errorf("notify failed: %v", errno)
} }
......
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"bytes" "bytes"
"context" "context"
"io/ioutil" "io/ioutil"
"os" "log"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
...@@ -18,7 +18,6 @@ import ( ...@@ -18,7 +18,6 @@ import (
"testing" "testing"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/internal/testutil"
) )
var testData = map[string]string{ var testData = map[string]string{
...@@ -62,20 +61,10 @@ func TestZipFS(t *testing.T) { ...@@ -62,20 +61,10 @@ func TestZipFS(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
root := &zipRoot{r: r} root := &zipRoot{zr: r}
mntDir, clean := testMount(t, root, nil)
defer clean()
mntDir := testutil.TempDir()
defer os.Remove(mntDir)
server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{
Debug: testutil.VerboseTest(),
},
FirstAutomaticIno: 1,
})
if err != nil {
t.Fatal(err)
}
defer server.Unmount()
for k, v := range testData { for k, v := range testData {
c, err := ioutil.ReadFile(filepath.Join(mntDir, k)) c, err := ioutil.ReadFile(filepath.Join(mntDir, k))
if err != nil { if err != nil {
...@@ -104,20 +93,48 @@ func TestZipFS(t *testing.T) { ...@@ -104,20 +93,48 @@ func TestZipFS(t *testing.T) {
} }
} }
func TestZipFSOnAdd(t *testing.T) {
zipBytes := createZip(testData)
r, err := zip.NewReader(&byteReaderAt{zipBytes}, int64(len(zipBytes)))
if err != nil {
t.Fatal(err)
}
zr := &zipRoot{zr: r}
root := &Inode{}
mnt, clean := testMount(t, root, &Options{
OnAdd: func(ctx context.Context) {
root.AddChild("sub",
root.NewPersistentInode(ctx, zr, NodeAttr{Mode: syscall.S_IFDIR}), false)
},
})
defer clean()
c, err := ioutil.ReadFile(mnt + "/sub/dir/subdir/subfile")
if err != nil {
t.Fatal("ReadFile", err)
}
if got, want := string(c), "content3"; got != want {
t.Errorf("got %q, want %q", got, want)
}
}
// zipFile is a file read from a zip archive. // zipFile is a file read from a zip archive.
type zipFile struct { type zipFile struct {
OperationStubs Inode
file *zip.File file *zip.File
mu sync.Mutex mu sync.Mutex
data []byte data []byte
} }
var _ = (FileOperations)((*zipFile)(nil)) var _ = (Opener)((*zipFile)(nil))
var _ = (Getattrer)((*zipFile)(nil))
// Getattr sets the minimum, which is the size. A more full-featured // Getattr sets the minimum, which is the size. A more full-featured
// FS would also set timestamps and permissions. // FS would also set timestamps and permissions.
func (zf *zipFile) Getattr(ctx context.Context, out *fuse.AttrOut) syscall.Errno { func (zf *zipFile) Getattr(ctx context.Context, f FileHandle, out *fuse.AttrOut) syscall.Errno {
out.Size = zf.file.UncompressedSize64 out.Size = zf.file.UncompressedSize64
return OK return OK
} }
...@@ -157,27 +174,29 @@ func (zf *zipFile) Read(ctx context.Context, f FileHandle, dest []byte, off int6 ...@@ -157,27 +174,29 @@ func (zf *zipFile) Read(ctx context.Context, f FileHandle, dest []byte, off int6
// zipRoot is the root of the Zip filesystem. Its only functionality // zipRoot is the root of the Zip filesystem. Its only functionality
// is populating the filesystem. // is populating the filesystem.
type zipRoot struct { type zipRoot struct {
OperationStubs Inode
r *zip.Reader zr *zip.Reader
} }
var _ = (OnAdder)((*zipRoot)(nil))
func (zr *zipRoot) OnAdd(ctx context.Context) { func (zr *zipRoot) OnAdd(ctx context.Context) {
// OnAdd is called once we are attached to an Inode. We can // OnAdd is called once we are attached to an Inode. We can
// then construct a tree. We construct the entire tree, and // then construct a tree. We construct the entire tree, and
// we don't want parts of the tree to disappear when the // we don't want parts of the tree to disappear when the
// kernel is short on memory, so we use persistent inodes. // kernel is short on memory, so we use persistent inodes.
for _, f := range zr.r.File { for _, f := range zr.zr.File {
dir, base := filepath.Split(f.Name) dir, base := filepath.Split(f.Name)
p := zr.Inode() p := &zr.Inode
for _, component := range strings.Split(dir, "/") { for _, component := range strings.Split(dir, "/") {
if len(component) == 0 { if len(component) == 0 {
continue continue
} }
ch := p.GetChild(component) ch := p.GetChild(component)
if ch == nil { if ch == nil {
ch = p.NewPersistentInode(ctx, &OperationStubs{}, ch = p.NewPersistentInode(ctx, &Inode{},
NodeAttr{Mode: fuse.S_IFDIR}) NodeAttr{Mode: fuse.S_IFDIR})
p.AddChild(component, ch, true) p.AddChild(component, ch, true)
} }
...@@ -188,3 +207,65 @@ func (zr *zipRoot) OnAdd(ctx context.Context) { ...@@ -188,3 +207,65 @@ func (zr *zipRoot) OnAdd(ctx context.Context) {
p.AddChild(base, ch, true) p.AddChild(base, ch, true)
} }
} }
// Persistent inodes can be used to create an in-memory
// prefabricated file system tree.
func ExampleInode_NewPersistentInode() {
// This is where we'll mount the FS
mntDir, _ := ioutil.TempDir("", "")
files := map[string]string{
"file": "content",
"subdir/other-file": "other-content",
}
root := &Inode{}
populate := func(ctx context.Context) {
for name, content := range files {
dir, base := filepath.Split(name)
p := root
// Add directories leading up to the file.
for _, component := range strings.Split(dir, "/") {
if len(component) == 0 {
continue
}
ch := p.GetChild(component)
if ch == nil {
// Create a directory
ch = p.NewPersistentInode(ctx, &Inode{},
NodeAttr{Mode: syscall.S_IFDIR})
// Add it
p.AddChild(component, ch, true)
}
p = ch
}
// Create the file
child := p.NewPersistentInode(ctx, &MemRegularFile{
Data: []byte(content),
}, NodeAttr{})
// And add it
p.AddChild(base, child, true)
}
}
server, err := Mount(mntDir, root, &Options{
MountOptions: fuse.MountOptions{Debug: true},
// This adds read permissions to the files and
// directories, which is necessary for doing a chdir
// into the mount.
DefaultPermissions: true,
OnAdd: populate,
})
if err != nil {
log.Panic(err)
}
log.Printf("Mounted on %s", mntDir)
log.Printf("Unmount by calling 'fusermount -u %s'", mntDir)
server.Wait()
}
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zipfs
import (
"fmt"
"strings"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
)
type MemFile interface {
Stat(out *fuse.Attr)
Data() []byte
}
type memNode struct {
nodefs.Node
file MemFile
fs *MemTreeFs
}
// memTreeFs creates a tree of internal Inodes. Since the tree is
// loaded in memory completely at startup, it does not need inode
// discovery through Lookup() at serve time.
type MemTreeFs struct {
root *memNode
files map[string]MemFile
Name string
}
func NewMemTreeFs(files map[string]MemFile) *MemTreeFs {
fs := &MemTreeFs{
root: &memNode{Node: nodefs.NewDefaultNode()},
files: files,
}
fs.root.fs = fs
return fs
}
func (fs *MemTreeFs) String() string {
return fs.Name
}
func (fs *MemTreeFs) Root() nodefs.Node {
return fs.root
}
func (fs *MemTreeFs) onMount() {
for k, v := range fs.files {
fs.addFile(k, v)
}
fs.files = nil
}
func (n *memNode) OnMount(c *nodefs.FileSystemConnector) {
n.fs.onMount()
}
func (n *memNode) Print(indent int) {
s := ""
for i := 0; i < indent; i++ {
s = s + " "
}
children := n.Inode().Children()
for k, v := range children {
if v.IsDir() {
fmt.Println(s + k + ":")
mn, ok := v.Node().(*memNode)
if ok {
mn.Print(indent + 2)
}
} else {
fmt.Println(s + k)
}
}
}
func (n *memNode) OpenDir(context *fuse.Context) (stream []fuse.DirEntry, code fuse.Status) {
children := n.Inode().Children()
stream = make([]fuse.DirEntry, 0, len(children))
for k, v := range children {
mode := fuse.S_IFREG | 0666
if v.IsDir() {
mode = fuse.S_IFDIR | 0777
}
stream = append(stream, fuse.DirEntry{
Name: k,
Mode: uint32(mode),
})
}
return stream, fuse.OK
}
func (n *memNode) Open(flags uint32, context *fuse.Context) (fuseFile nodefs.File, code fuse.Status) {
if flags&fuse.O_ANYWRITE != 0 {
return nil, fuse.EPERM
}
return nodefs.NewDataFile(n.file.Data()), fuse.OK
}
func (n *memNode) Deletable() bool {
return false
}
func (n *memNode) GetAttr(out *fuse.Attr, file nodefs.File, context *fuse.Context) fuse.Status {
if n.Inode().IsDir() {
out.Mode = fuse.S_IFDIR | 0777
return fuse.OK
}
n.file.Stat(out)
out.Blocks = (out.Size + 511) / 512
return fuse.OK
}
func (n *MemTreeFs) addFile(name string, f MemFile) {
comps := strings.Split(name, "/")
node := n.root.Inode()
for i, c := range comps {
child := node.GetChild(c)
if child == nil {
fsnode := &memNode{
Node: nodefs.NewDefaultNode(),
fs: n,
}
if i == len(comps)-1 {
fsnode.file = f
}
child = node.NewChild(c, fsnode.file == nil, fsnode)
}
node = child
}
}
...@@ -15,191 +15,69 @@ symlinking path/to/zipfile to /config/zipmount ...@@ -15,191 +15,69 @@ symlinking path/to/zipfile to /config/zipmount
*/ */
import ( import (
"context"
"log" "log"
"path/filepath" "syscall"
"sync"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
) )
const ( // MultiZipFs is a filesystem that mounts zipfiles.
CONFIG_PREFIX = "config/"
)
////////////////////////////////////////////////////////////////
// MultiZipFs is a path filesystem that mounts zipfiles.
type MultiZipFs struct { type MultiZipFs struct {
lock sync.RWMutex nodefs.Inode
zips map[string]nodefs.Node
dirZipFileMap map[string]string
// zip files that we are in the process of unmounting.
zombie map[string]bool
nodeFs *pathfs.PathNodeFs
pathfs.FileSystem
} }
func NewMultiZipFs() *MultiZipFs { func (fs *MultiZipFs) OnAdd(ctx context.Context) {
m := &MultiZipFs{ n := fs.NewPersistentInode(ctx, &configRoot{}, nodefs.NodeAttr{Mode: syscall.S_IFDIR})
zips: make(map[string]nodefs.Node),
zombie: make(map[string]bool),
dirZipFileMap: make(map[string]string),
FileSystem: pathfs.NewDefaultFileSystem(),
}
return m
}
func (fs *MultiZipFs) String() string { fs.AddChild("config", n, false)
return "MultiZipFs"
} }
func (fs *MultiZipFs) OnMount(nodeFs *pathfs.PathNodeFs) { type configRoot struct {
fs.nodeFs = nodeFs nodefs.Inode
} }
func (fs *MultiZipFs) OpenDir(name string, context *fuse.Context) (stream []fuse.DirEntry, code fuse.Status) { var _ = (nodefs.Unlinker)((*configRoot)(nil))
fs.lock.RLock() var _ = (nodefs.Symlinker)((*configRoot)(nil))
defer fs.lock.RUnlock()
stream = make([]fuse.DirEntry, 0, len(fs.zips)+2) func (r *configRoot) Unlink(ctx context.Context, basename string) syscall.Errno {
if name == "" { if r.GetChild(basename) == nil {
var d fuse.DirEntry return syscall.ENOENT
d.Name = "config"
d.Mode = fuse.S_IFDIR | 0700
stream = append(stream, fuse.DirEntry(d))
} }
if name == "config" { // XXX RmChild should return Inode?
for k := range fs.zips {
var d fuse.DirEntry
d.Name = k
d.Mode = fuse.S_IFLNK
stream = append(stream, fuse.DirEntry(d))
}
}
return stream, fuse.OK _, parent := r.Parent()
} ch := parent.GetChild(basename)
if ch == nil {
func (fs *MultiZipFs) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) { return syscall.ENOENT
a := &fuse.Attr{}
a.Owner = *fuse.CurrentOwner()
if name == "" {
// Should not write in top dir.
a.Mode = fuse.S_IFDIR | 0500
return a, fuse.OK
}
if name == "config" {
a.Mode = fuse.S_IFDIR | 0700
return a, fuse.OK
}
dir, base := filepath.Split(name)
if dir != "" && dir != CONFIG_PREFIX {
return nil, fuse.ENOENT
}
submode := uint32(fuse.S_IFDIR | 0700)
if dir == CONFIG_PREFIX {
submode = fuse.S_IFLNK | 0600
} }
success, _ := parent.RmChild(basename)
fs.lock.RLock() if !success {
defer fs.lock.RUnlock() return syscall.EIO
a.Mode = submode
_, hasDir := fs.zips[base]
if hasDir {
return a, fuse.OK
} }
return nil, fuse.ENOENT ch.RmAllChildren()
parent.RmChild(basename)
parent.NotifyEntry(basename)
return 0
} }
func (fs *MultiZipFs) Unlink(name string, context *fuse.Context) (code fuse.Status) { func (r *configRoot) Symlink(ctx context.Context, target string, base string, out *fuse.EntryOut) (*nodefs.Inode, syscall.Errno) {
dir, basename := filepath.Split(name) root, err := NewArchiveFileSystem(target)
if dir == CONFIG_PREFIX {
fs.lock.Lock()
defer fs.lock.Unlock()
if fs.zombie[basename] {
return fuse.ENOENT
}
root, ok := fs.zips[basename]
if !ok {
return fuse.ENOENT
}
name := fs.dirZipFileMap[basename]
fs.zombie[basename] = true
delete(fs.zips, basename)
delete(fs.dirZipFileMap, basename)
// Drop the lock to ensure that notify doesn't cause a deadlock.
fs.lock.Unlock()
code = fs.nodeFs.UnmountNode(root.Inode())
fs.lock.Lock()
delete(fs.zombie, basename)
if !code.Ok() {
// Failed: reinstate
fs.zips[basename] = root
fs.dirZipFileMap[basename] = name
}
return code
}
return fuse.EPERM
}
func (fs *MultiZipFs) Readlink(path string, context *fuse.Context) (val string, code fuse.Status) {
dir, base := filepath.Split(path)
if dir != CONFIG_PREFIX {
return "", fuse.ENOENT
}
fs.lock.Lock()
defer fs.lock.Unlock()
if fs.zombie[base] {
return "", fuse.ENOENT
}
zipfile, ok := fs.dirZipFileMap[base]
if !ok {
return "", fuse.ENOENT
}
return zipfile, fuse.OK
}
func (fs *MultiZipFs) Symlink(value string, linkName string, context *fuse.Context) (code fuse.Status) {
dir, base := filepath.Split(linkName)
if dir != CONFIG_PREFIX {
return fuse.EPERM
}
fs.lock.Lock()
defer fs.lock.Unlock()
if fs.zombie[base] {
return fuse.EBUSY
}
_, ok := fs.dirZipFileMap[base]
if ok {
return fuse.EBUSY
}
root, err := NewArchiveFileSystem(value)
if err != nil { if err != nil {
log.Println("NewZipArchiveFileSystem failed.", err) log.Println("NewZipArchiveFileSystem failed.", err)
return fuse.EINVAL return nil, syscall.EINVAL
} }
code = fs.nodeFs.Mount(base, root, nil) _, parent := r.Parent()
if !code.Ok() { ch := r.NewPersistentInode(ctx, root, nodefs.NodeAttr{Mode: syscall.S_IFDIR})
return code parent.AddChild(base, ch, false)
}
fs.dirZipFileMap[base] = value link := r.NewPersistentInode(ctx, &nodefs.MemSymlink{
fs.zips[base] = root Data: []byte(target),
return fuse.OK }, nodefs.NodeAttr{Mode: syscall.S_IFLNK})
r.AddChild(base, link, false)
return link, 0
} }
...@@ -11,31 +11,29 @@ import ( ...@@ -11,31 +11,29 @@ import (
"time" "time"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
"github.com/hanwen/go-fuse/internal/testutil" "github.com/hanwen/go-fuse/internal/testutil"
"github.com/hanwen/go-fuse/nodefs"
) )
const testTtl = 100 * time.Millisecond const testTtl = 100 * time.Millisecond
func setupMzfs(t *testing.T) (mountPoint string, state *fuse.Server, cleanup func()) { func setupMzfs(t *testing.T) (mountPoint string, state *fuse.Server, cleanup func()) {
fs := NewMultiZipFs() root := &MultiZipFs{}
mountPoint = testutil.TempDir() mountPoint = testutil.TempDir()
nfs := pathfs.NewPathNodeFs(fs, nil)
state, _, err := nodefs.MountRoot(mountPoint, nfs.Root(), &nodefs.Options{ dt := testTtl
EntryTimeout: testTtl, opts := &nodefs.Options{
AttrTimeout: testTtl, EntryTimeout: &dt,
NegativeTimeout: 0.0, AttrTimeout: &dt,
Debug: testutil.VerboseTest(), }
}) opts.Debug = testutil.VerboseTest()
server, err := nodefs.Mount(mountPoint, root, opts)
if err != nil { if err != nil {
t.Fatalf("MountNodeFileSystem failed: %v", err) t.Fatalf("MountNodeFileSystem failed: %v", err)
} }
go state.Serve() return mountPoint, server, func() {
state.WaitMount() server.Unmount()
return mountPoint, state, func() {
state.Unmount()
os.RemoveAll(mountPoint) os.RemoveAll(mountPoint)
} }
} }
......
...@@ -9,11 +9,16 @@ import ( ...@@ -9,11 +9,16 @@ import (
"bytes" "bytes"
"compress/bzip2" "compress/bzip2"
"compress/gzip" "compress/gzip"
"github.com/hanwen/go-fuse/fuse" "context"
"io" "io"
"log"
"os" "os"
"path/filepath"
"strings" "strings"
"syscall" "syscall"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/nodefs"
) )
// TODO - handle symlinks. // TODO - handle symlinks.
...@@ -26,23 +31,14 @@ func HeaderToFileInfo(out *fuse.Attr, h *tar.Header) { ...@@ -26,23 +31,14 @@ func HeaderToFileInfo(out *fuse.Attr, h *tar.Header) {
out.SetTimes(&h.AccessTime, &h.ModTime, &h.ChangeTime) out.SetTimes(&h.AccessTime, &h.ModTime, &h.ChangeTime)
} }
type TarFile struct { type tarRoot struct {
data []byte nodefs.Inode
tar.Header rc io.ReadCloser
}
func (f *TarFile) Stat(out *fuse.Attr) {
HeaderToFileInfo(out, &f.Header)
out.Mode |= syscall.S_IFREG
}
func (f *TarFile) Data() []byte {
return f.data
} }
func NewTarTree(r io.Reader) map[string]MemFile { func (r *tarRoot) OnAdd(ctx context.Context) {
files := map[string]MemFile{} tr := tar.NewReader(r.rc)
tr := tar.NewReader(r) defer r.rc.Close()
var longName *string var longName *string
for { for {
...@@ -52,7 +48,9 @@ func NewTarTree(r io.Reader) map[string]MemFile { ...@@ -52,7 +48,9 @@ func NewTarTree(r io.Reader) map[string]MemFile {
break break
} }
if err != nil { if err != nil {
// handle error log.Printf("Add: %v", err)
// XXX handle error
break
} }
if hdr.Typeflag == 'L' { if hdr.Typeflag == 'L' {
...@@ -74,35 +72,70 @@ func NewTarTree(r io.Reader) map[string]MemFile { ...@@ -74,35 +72,70 @@ func NewTarTree(r io.Reader) map[string]MemFile {
buf := bytes.NewBuffer(make([]byte, 0, hdr.Size)) buf := bytes.NewBuffer(make([]byte, 0, hdr.Size))
io.Copy(buf, tr) io.Copy(buf, tr)
dir, base := filepath.Split(filepath.Clean(hdr.Name))
p := r.EmbeddedInode()
for _, comp := range strings.Split(dir, "/") {
if len(comp) == 0 {
continue
}
ch := p.GetChild(comp)
if ch == nil {
ch = p.NewPersistentInode(ctx,
&nodefs.Inode{},
nodefs.NodeAttr{Mode: syscall.S_IFDIR})
p.AddChild(comp, ch, false)
}
p = ch
}
files[hdr.Name] = &TarFile{ if hdr.Typeflag == tar.TypeSymlink {
Header: *hdr, p.AddChild(base, r.NewPersistentInode(ctx, &nodefs.MemSymlink{
data: buf.Bytes(), Data: []byte(hdr.Linkname),
}, nodefs.NodeAttr{Mode: syscall.S_IFLNK}), false)
} else {
df := &nodefs.MemRegularFile{
Data: buf.Bytes(),
}
HeaderToFileInfo(&df.Attr, hdr)
p.AddChild(base, r.NewPersistentInode(ctx, df, nodefs.NodeAttr{}), false)
} }
} }
return files
} }
func NewTarCompressedTree(name string, format string) (map[string]MemFile, error) { type readCloser struct {
io.Reader
close func() error
}
func (rc *readCloser) Close() error {
return rc.close()
}
func NewTarCompressedTree(name string, format string) (nodefs.InodeEmbedder, error) {
f, err := os.Open(name) f, err := os.Open(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close()
var stream io.Reader var stream io.ReadCloser
switch format { switch format {
case "gz": case "gz":
unzip, err := gzip.NewReader(f) unzip, err := gzip.NewReader(f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer unzip.Close() stream = &readCloser{
stream = unzip unzip,
f.Close,
}
case "bz2": case "bz2":
unzip := bzip2.NewReader(f) unzip := bzip2.NewReader(f)
stream = unzip stream = &readCloser{
unzip,
f.Close,
}
} }
return NewTarTree(stream), nil return &tarRoot{rc: stream}, nil
} }
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zipfs
import (
"archive/tar"
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/hanwen/go-fuse/internal/testutil"
"github.com/hanwen/go-fuse/nodefs"
)
var tarContents = map[string]string{
"file.txt": "content",
"dir/subfile.txt": "other content",
}
type addClose struct {
io.Reader
}
func (c *addClose) Close() error {
return nil
}
func TestTar(t *testing.T) {
buf := &bytes.Buffer{}
w := tar.NewWriter(buf)
now := time.Now()
for k, v := range tarContents {
h := &tar.Header{
Name: k,
Size: int64(len(v)),
Mode: 0464,
Uid: 42,
Gid: 42,
ModTime: now,
}
isLink := filepath.Base(k) == "link"
if isLink {
h.Typeflag = tar.TypeSymlink
h.Linkname = v
}
w.WriteHeader(h)
if !isLink {
w.Write([]byte(v))
}
}
w.Close()
root := &tarRoot{rc: &addClose{buf}}
mnt := testutil.TempDir()
defer os.Remove(mnt)
opts := &nodefs.Options{}
opts.Debug = true
s, err := nodefs.Mount(mnt, root, opts)
if err != nil {
t.Errorf("Mount: %v", err)
}
defer s.Unmount()
for k, want := range tarContents {
p := filepath.Join(mnt, k)
var st syscall.Stat_t
if err := syscall.Lstat(p, &st); err != nil {
t.Fatalf("Stat %q: %v", p, err)
}
if filepath.Base(k) == "link" {
got, err := os.Readlink(p)
if err != nil {
t.Fatalf("Readlink: %v", err)
}
if got != want {
t.Errorf("Readlink: got %q want %q", got, want)
}
} else {
if got, want := st.Mode, uint32(syscall.S_IFREG|0464); got != want {
t.Errorf("got mode %o, want %o", got, want)
}
c, err := ioutil.ReadFile(p)
if err != nil {
t.Errorf("read %q: %v", k, err)
got := string(c)
if got != want {
t.Errorf("file %q: got %q, want %q", k, got, want)
}
}
}
}
}
...@@ -7,14 +7,18 @@ package zipfs ...@@ -7,14 +7,18 @@ package zipfs
import ( import (
"archive/zip" "archive/zip"
"bytes" "bytes"
"context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"syscall"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs" "github.com/hanwen/go-fuse/nodefs"
) )
type ZipFile struct { type ZipFile struct {
...@@ -22,11 +26,6 @@ type ZipFile struct { ...@@ -22,11 +26,6 @@ type ZipFile struct {
} }
func (f *ZipFile) Stat(out *fuse.Attr) { func (f *ZipFile) Stat(out *fuse.Attr) {
out.Mode = fuse.S_IFREG | uint32(f.File.Mode())
out.Size = uint64(f.File.UncompressedSize)
out.Mtime = uint64(f.File.ModTime().Unix())
out.Atime = out.Mtime
out.Ctime = out.Mtime
} }
func (f *ZipFile) Data() []byte { func (f *ZipFile) Data() []byte {
...@@ -44,41 +43,123 @@ func (f *ZipFile) Data() []byte { ...@@ -44,41 +43,123 @@ func (f *ZipFile) Data() []byte {
return dest.Bytes() return dest.Bytes()
} }
type zipRoot struct {
nodefs.Inode
zr *zip.ReadCloser
}
var _ = (nodefs.OnAdder)((*zipRoot)(nil))
func (zr *zipRoot) OnAdd(ctx context.Context) {
for _, f := range zr.zr.File {
if f.FileInfo().IsDir() {
continue
}
dir, base := filepath.Split(filepath.Clean(f.Name))
p := &zr.Inode
for _, component := range strings.Split(dir, "/") {
if len(component) == 0 {
continue
}
ch := p.GetChild(component)
if ch == nil {
ch = p.NewPersistentInode(ctx, &nodefs.Inode{},
nodefs.NodeAttr{Mode: fuse.S_IFDIR})
p.AddChild(component, ch, true)
}
p = ch
}
ch := p.NewPersistentInode(ctx, &zipFile{file: f}, nodefs.NodeAttr{})
p.AddChild(base, ch, true)
}
}
// NewZipTree creates a new file-system for the zip file named name. // NewZipTree creates a new file-system for the zip file named name.
func NewZipTree(name string) (map[string]MemFile, error) { func NewZipTree(name string) (nodefs.InodeEmbedder, error) {
r, err := zip.OpenReader(name) r, err := zip.OpenReader(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
out := map[string]MemFile{} return &zipRoot{zr: r}, nil
for _, f := range r.File { }
if strings.HasSuffix(f.Name, "/") {
continue // zipFile is a file read from a zip archive.
type zipFile struct {
nodefs.Inode
file *zip.File
mu sync.Mutex
data []byte
}
var _ = (nodefs.Opener)((*zipFile)(nil))
var _ = (nodefs.Getattrer)((*zipFile)(nil))
// Getattr sets the minimum, which is the size. A more full-featured
// FS would also set timestamps and permissions.
func (zf *zipFile) Getattr(ctx context.Context, f nodefs.FileHandle, out *fuse.AttrOut) syscall.Errno {
out.Mode = uint32(zf.file.Mode()) & 07777
out.Nlink = 1
out.Mtime = uint64(zf.file.ModTime().Unix())
out.Atime = out.Mtime
out.Ctime = out.Mtime
out.Size = zf.file.UncompressedSize64
out.Blocks = (out.Size + 511) / 512
return 0
}
// Open lazily unpacks zip data
func (zf *zipFile) Open(ctx context.Context, flags uint32) (nodefs.FileHandle, uint32, syscall.Errno) {
zf.mu.Lock()
defer zf.mu.Unlock()
if zf.data == nil {
rc, err := zf.file.Open()
if err != nil {
return nil, 0, syscall.EIO
}
content, err := ioutil.ReadAll(rc)
if err != nil {
return nil, 0, syscall.EIO
} }
n := filepath.Clean(f.Name)
zf := &ZipFile{f} zf.data = content
out[n] = zf }
// We don't return a filehandle since we don't really need
// one. The file content is immutable, so hint the kernel to
// cache the data.
return nil, fuse.FOPEN_KEEP_CACHE, 0
}
// Read simply returns the data that was already unpacked in the Open call
func (zf *zipFile) Read(ctx context.Context, f nodefs.FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
end := int(off) + len(dest)
if end > len(zf.data) {
end = len(zf.data)
} }
return out, nil return fuse.ReadResultData(zf.data[off:end]), 0
} }
func NewArchiveFileSystem(name string) (root nodefs.Node, err error) { var _ = (nodefs.OnAdder)((*zipRoot)(nil))
var files map[string]MemFile
func NewArchiveFileSystem(name string) (root nodefs.InodeEmbedder, err error) {
switch { switch {
case strings.HasSuffix(name, ".zip"): case strings.HasSuffix(name, ".zip"):
files, err = NewZipTree(name) root, err = NewZipTree(name)
case strings.HasSuffix(name, ".tar.gz"): case strings.HasSuffix(name, ".tar.gz"):
files, err = NewTarCompressedTree(name, "gz") root, err = NewTarCompressedTree(name, "gz")
case strings.HasSuffix(name, ".tar.bz2"): case strings.HasSuffix(name, ".tar.bz2"):
files, err = NewTarCompressedTree(name, "bz2") root, err = NewTarCompressedTree(name, "bz2")
case strings.HasSuffix(name, ".tar"): case strings.HasSuffix(name, ".tar"):
f, err := os.Open(name) f, err := os.Open(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
files = NewTarTree(f) root = &tarRoot{rc: f}
default: default:
return nil, fmt.Errorf("unknown archive format %q", name) return nil, fmt.Errorf("unknown archive format %q", name)
} }
...@@ -87,7 +168,5 @@ func NewArchiveFileSystem(name string) (root nodefs.Node, err error) { ...@@ -87,7 +168,5 @@ func NewArchiveFileSystem(name string) (root nodefs.Node, err error) {
return nil, err return nil, err
} }
mfs := NewMemTreeFs(files) return root, nil
mfs.Name = fmt.Sprintf("fs(%s)", name)
return mfs.Root(), nil
} }
...@@ -14,8 +14,8 @@ import ( ...@@ -14,8 +14,8 @@ import (
"time" "time"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/internal/testutil" "github.com/hanwen/go-fuse/internal/testutil"
"github.com/hanwen/go-fuse/nodefs"
) )
func testZipFile() string { func testZipFile() string {
...@@ -34,15 +34,12 @@ func setupZipfs(t *testing.T) (mountPoint string, cleanup func()) { ...@@ -34,15 +34,12 @@ func setupZipfs(t *testing.T) (mountPoint string, cleanup func()) {
} }
mountPoint = testutil.TempDir() mountPoint = testutil.TempDir()
state, _, err := nodefs.MountRoot(mountPoint, root, &nodefs.Options{ opts := &nodefs.Options{}
Debug: testutil.VerboseTest(), opts.Debug = testutil.VerboseTest()
}) server, err := nodefs.Mount(mountPoint, root, opts)
go state.Serve()
state.WaitMount()
return mountPoint, func() { return mountPoint, func() {
state.Unmount() server.Unmount()
os.RemoveAll(mountPoint) os.RemoveAll(mountPoint)
} }
} }
...@@ -70,19 +67,17 @@ func TestZipFs(t *testing.T) { ...@@ -70,19 +67,17 @@ func TestZipFs(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Stat failed: %v", err) t.Fatalf("Stat failed: %v", err)
} }
if fi.Mode() != 0664 { if got, want := fi.Mode(), 0664; int(got) != want {
t.Fatalf("File mode 0%o != 0664", fi.Mode()) t.Fatalf("File mode: got 0%o want 0%o", got, want)
} }
if st := fi.Sys().(*syscall.Stat_t); st.Blocks != 1 { if st := fi.Sys().(*syscall.Stat_t); st.Blocks != 1 {
t.Errorf("got block count %d, want 1", st.Blocks) t.Errorf("got block count %d, want 1", st.Blocks)
} }
mtime, err := time.Parse(time.RFC3339, "2011-02-22T12:56:12Z") if want, err := time.Parse(time.RFC3339, "2011-02-22T12:56:12Z"); err != nil {
if err != nil {
panic(err) panic(err)
} } else if !fi.ModTime().Equal(want) {
if !fi.ModTime().Equal(mtime) { t.Fatalf("File mtime got %v, want %v", fi.ModTime(), want)
t.Fatalf("File mtime %v != %v", fi.ModTime(), mtime)
} }
if fi.IsDir() { if fi.IsDir() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment