This commit is contained in:
Alex Yatskov 2015-06-16 17:18:00 +09:00
parent 0506c89aa9
commit f81ad172f7

44
dir.go
View File

@ -25,6 +25,7 @@ package main
import ( import (
"bazil.org/fuse" "bazil.org/fuse"
"bazil.org/fuse/fs" "bazil.org/fuse/fs"
"errors"
"golang.org/x/net/context" "golang.org/x/net/context"
"os" "os"
"path" "path"
@ -77,8 +78,11 @@ func (this *versionedDir) version() error {
} }
func (this *versionedDir) createDir(name string) (*versionedDir, error) { func (this *versionedDir) createDir(name string) (*versionedDir, error) {
childPath := path.Join(this.node.path, name) if err := this.version(); err != nil {
return nil, err
}
childPath := path.Join(this.node.path, name)
if err := os.Mkdir(this.node.ver.rebasePath(childPath), 0755); err != nil { if err := os.Mkdir(this.node.ver.rebasePath(childPath), 0755); err != nil {
return nil, err return nil, err
} }
@ -95,8 +99,11 @@ func (this *versionedDir) createDir(name string) (*versionedDir, error) {
} }
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) if err := this.version(); err != nil {
return nil, err
}
childPath := path.Join(this.node.path, name)
handle, err := os.OpenFile(this.node.ver.rebasePath(childPath), flags, 0644) handle, err := os.OpenFile(this.node.ver.rebasePath(childPath), flags, 0644)
if err != nil { if err != nil {
return nil, err return nil, err
@ -130,36 +137,31 @@ func (this *versionedDir) Getattr(ctx context.Context, req *fuse.GetattrRequest,
} }
func (this *versionedDir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error { func (this *versionedDir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
this.version()
return this.node.setAttr(req, resp) return this.node.setAttr(req, resp)
} }
func (this *versionedDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) { func (this *versionedDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (node fs.Node, handle fs.Handle, err error) {
if err := this.version(); err != nil {
return nil, nil, err
}
if req.Mode.IsDir() { if req.Mode.IsDir() {
dir, err := this.createDir(req.Name) var dir *versionedDir
if err != nil { if dir, err = this.createDir(req.Name); err == nil {
return nil, nil, err node = dir
handle = dir
}
} else if req.Mode.IsRegular() {
var file *versionedFile
if file, err = this.createFile(req.Name, int(req.Flags)); err == nil {
node = file
handle = file
} }
return dir, dir, nil
} else { } else {
file, err := this.createFile(req.Name, int(req.Flags)) err = errors.New("unsupported filetype")
if err != nil {
return nil, nil, err
} }
return file, file, nil return
}
} }
func (this *versionedDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) { func (this *versionedDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
if err := this.version(); err != nil {
return nil, err
}
return this.createDir(req.Name) return this.createDir(req.Name)
} }