Commit 0a5037fa authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

example/hello: convert hello to new nodefs API

parent f5e4c658
...@@ -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()
} }
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