Some limited support for writing files

This commit is contained in:
Alex Yatskov 2015-05-18 11:58:49 +09:00
parent da03558a45
commit c156800411
2 changed files with 38 additions and 2 deletions

View File

@ -26,6 +26,7 @@ import (
"bazil.org/fuse/fs" "bazil.org/fuse/fs"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"sync/atomic"
) )
type database struct { type database struct {
@ -119,6 +120,5 @@ func (this *database) Root() (fs.Node, error) {
} }
func (this *database) AllocInode() uint64 { func (this *database) AllocInode() uint64 {
this.inodeCnt++ return atomic.AddUint64(&this.inodeCnt, 1)
return this.inodeCnt
} }

36
file.go
View File

@ -26,6 +26,7 @@ import (
"bazil.org/fuse" "bazil.org/fuse"
"golang.org/x/net/context" "golang.org/x/net/context"
"io/ioutil" "io/ioutil"
"os"
) )
type versionedFile struct { type versionedFile struct {
@ -46,6 +47,41 @@ func (this versionedFile) Attr(attr *fuse.Attr) {
attr.Inode = this.inode 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) { func (this versionedFile) ReadAll(ctx context.Context) ([]byte, error) {
bytes, err := ioutil.ReadFile(this.node.rebasedPath()) bytes, err := ioutil.ReadFile(this.node.rebasedPath())
if err != nil { if err != nil {