Proper handling of files and directories

This commit is contained in:
Alex Yatskov 2015-05-25 13:45:16 +09:00
parent 5597bf4cfc
commit 48d8aa99df
2 changed files with 20 additions and 22 deletions

11
dir.go
View File

@ -61,23 +61,24 @@ func (this *versionedDir) createDir(name string) (*versionedDir, error) {
dir := newVersionedDir(node, this)
this.dirs[name] = dir
return dir, nil
}
func (this *versionedDir) createFile(name string, flags int) (*versionedFile, error) {
childPath := path.Join(this.node.path, name)
handle, err := os.OpenFile(this.node.ver.rebasePath(childPath), flags, 0644)
if err != nil {
return nil, err
}
node, err := newVersionedNode(childPath, this.node.ver)
if err != nil {
return nil, err
}
file := newVersionedFile(node, this)
if err := file.create(name, flags); err != nil {
return nil, err
}
file.handle = handle
this.files[name] = file
return file, nil
}

31
file.go
View File

@ -24,6 +24,7 @@ package main
import (
"bazil.org/fuse"
"bazil.org/fuse/fs"
"errors"
"golang.org/x/net/context"
"os"
@ -43,24 +44,18 @@ func newVersionedFile(node *versionedNode, parent *versionedDir) *versionedFile
parent: parent}
}
func (this *versionedFile) create(path string, flags int) error {
handle, err := os.OpenFile(this.node.ver.rebasePath(path), flags, 0644)
func (this *versionedFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
if this.handle != nil {
return nil, errors.New("attempted to open already opened file")
}
handle, err := os.OpenFile(this.node.rebasedPath(), int(req.Flags), 0644)
if err != nil {
return err
return nil, err
}
this.handle = handle
return nil
}
func (this *versionedFile) release() bool {
if this.handle == nil {
return false
}
this.handle.Close()
this.handle = nil
return true
return this, nil
}
func (this *versionedFile) Attr(attr *fuse.Attr) {
@ -94,11 +89,13 @@ func (this *versionedFile) Setattr(ctx context.Context, req *fuse.SetattrRequest
}
func (this *versionedFile) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
if this.release() {
return nil
if this.handle == nil {
return errors.New("attempted to release unopened file")
}
return errors.New("attempted to release unopened file")
this.handle.Close()
this.handle = nil
return nil
}
func (this *versionedFile) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {