Commit c52a4ac4 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 1df9b3eb
...@@ -2,8 +2,10 @@ package main ...@@ -2,8 +2,10 @@ package main
import ( import (
"flag" "flag"
"fmt"
"log" "log"
"os" "os"
"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/nodefs"
...@@ -21,11 +23,14 @@ type file struct { ...@@ -21,11 +23,14 @@ type file struct {
// fileHandle represents opened file. // fileHandle represents opened file.
type fileHandle struct { type fileHandle struct {
nodefs.File
content []byte
} }
func (d *dir) Lookup(out *fuse.Attr, name string, ctx *fuse.Context) (*nodefs.Inode, fuse.Status) { func (d *dir) Lookup(out *fuse.Attr, name string, _ *fuse.Context) (*nodefs.Inode, fuse.Status) {
ientry := d.Inode().GetChild(name) ientry := d.Inode().GetChild(name)
if ientry == nil { if ientry == nil {
return nil, fuse.ENOENT return nil, fuse.ENOENT
...@@ -34,6 +39,39 @@ func (d *dir) Lookup(out *fuse.Attr, name string, ctx *fuse.Context) (*nodefs.In ...@@ -34,6 +39,39 @@ func (d *dir) Lookup(out *fuse.Attr, name string, ctx *fuse.Context) (*nodefs.In
return ientry, fuse.OK return ientry, fuse.OK
} }
var nopen = 0
func (f *file) Open(flags uint32, _ *fuse.Context) (nodefs.File, fuse.Status) {
_, name := f.Inode().Parent()
nopen++ // XXX -> atomic
data := fmt.Sprintf("%04d %s\n", nopen, name)
h := &fileHandle{File: nodefs.NewDefaultFile(), content: []byte(data)}
// force direct-io to disable pagecache: we alway return different data
// and st_size=0 (like in /proc).
return &nodefs.WithFlags{
File: h,
FuseFlags: fuse.FOPEN_DIRECT_IO,
}, fuse.OK
}
func (fh *fileHandle) Read(dest []byte, off int64) (fuse.ReadResult, fuse.Status) {
l := int64(len(dest))
// XXX demonstrate we can indeed serve different content to different openings.
if l >= 1 {
l = 1
time.Sleep(1*time.Second)
}
end := off + l
if ldata := int64(len(fh.content)); end > ldata {
end = ldata
}
res := fh.content[off:end]
fmt.Printf("read [%d:%d] -> %q\n", off, end, res)
return fuse.ReadResultData(res), fuse.OK
}
func (d *dir) mkdir(name string) *dir { func (d *dir) mkdir(name string) *dir {
...@@ -57,9 +95,6 @@ func main() { ...@@ -57,9 +95,6 @@ func main() {
} }
mntpt := flag.Args()[0] mntpt := flag.Args()[0]
root := &dir{Node: nodefs.NewDefaultNode()} root := &dir{Node: nodefs.NewDefaultNode()}
//nodefs.NewInode(true, root)
//println(root.Inode())
opts := nodefs.NewOptions() opts := nodefs.NewOptions()
if *debug { if *debug {
opts.Debug = true opts.Debug = true
...@@ -69,6 +104,8 @@ func main() { ...@@ -69,6 +104,8 @@ func main() {
log.Fatal(err) // XXX err ctx? log.Fatal(err) // XXX err ctx?
} }
// NOTE cannot make entries before mount because Inode.AddChild does
// not work before that (panics on nil deref to mountRootXXX)
root.mkdir("aaa") root.mkdir("aaa")
root.mkfile("hello.txt") root.mkfile("hello.txt")
......
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