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) dir := newVersionedDir(node, this)
this.dirs[name] = dir this.dirs[name] = dir
return dir, nil return dir, nil
} }
func (this *versionedDir) createFile(name string, flags int) (*versionedFile, error) { func (this *versionedDir) createFile(name string, flags int) (*versionedFile, error) {
childPath := path.Join(this.node.path, name) 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) node, err := newVersionedNode(childPath, this.node.ver)
if err != nil { if err != nil {
return nil, err return nil, err
} }
file := newVersionedFile(node, this) file := newVersionedFile(node, this)
if err := file.create(name, flags); err != nil { file.handle = handle
return nil, err
}
this.files[name] = file this.files[name] = file
return file, nil return file, nil
} }

31
file.go
View File

@ -24,6 +24,7 @@ package main
import ( import (
"bazil.org/fuse" "bazil.org/fuse"
"bazil.org/fuse/fs"
"errors" "errors"
"golang.org/x/net/context" "golang.org/x/net/context"
"os" "os"
@ -43,24 +44,18 @@ func newVersionedFile(node *versionedNode, parent *versionedDir) *versionedFile
parent: parent} parent: parent}
} }
func (this *versionedFile) create(path string, flags int) error { func (this *versionedFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
handle, err := os.OpenFile(this.node.ver.rebasePath(path), flags, 0644) 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 { if err != nil {
return err return nil, err
} }
this.handle = handle this.handle = handle
return nil return this, nil
}
func (this *versionedFile) release() bool {
if this.handle == nil {
return false
}
this.handle.Close()
this.handle = nil
return true
} }
func (this *versionedFile) Attr(attr *fuse.Attr) { 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 { func (this *versionedFile) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
if this.release() { if this.handle == nil {
return 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 { func (this *versionedFile) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {