From c156800411736eca18b00bdfbed2b16e2c4254a2 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 18 May 2015 11:58:49 +0900 Subject: [PATCH] Some limited support for writing files --- database.go | 4 ++-- file.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/database.go b/database.go index a81b613..841703f 100644 --- a/database.go +++ b/database.go @@ -26,6 +26,7 @@ import ( "bazil.org/fuse/fs" "io/ioutil" "path/filepath" + "sync/atomic" ) type database struct { @@ -119,6 +120,5 @@ func (this *database) Root() (fs.Node, error) { } func (this *database) AllocInode() uint64 { - this.inodeCnt++ - return this.inodeCnt + return atomic.AddUint64(&this.inodeCnt, 1) } diff --git a/file.go b/file.go index 532e1f5..702b502 100644 --- a/file.go +++ b/file.go @@ -26,6 +26,7 @@ import ( "bazil.org/fuse" "golang.org/x/net/context" "io/ioutil" + "os" ) type versionedFile struct { @@ -46,6 +47,41 @@ func (this versionedFile) Attr(attr *fuse.Attr) { attr.Inode = this.inode } +func (this versionedFile) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error { + file, err := os.OpenFile(this.node.rebasedPath(), os.O_WRONLY, 0666) + if err != nil { + return err + } + defer file.Close() + + size, err := file.WriteAt(req.Data, req.Offset) + if err != nil { + return err + } + + resp.Size = size + return nil +} + +func (this versionedFile) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error { + info, err := os.Stat(this.node.rebasedPath()) + if err != nil { + return err + } + + this.node.info = info + this.Attr(&resp.Attr) + return nil +} + +func (this versionedFile) Fsync(ctx context.Context, req *fuse.FsyncRequest) error { + return nil +} + +// func (this versionedFile) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { +// return nil +// } + func (this versionedFile) ReadAll(ctx context.Context) ([]byte, error) { bytes, err := ioutil.ReadFile(this.node.rebasedPath()) if err != nil {