Some work on deleting

This commit is contained in:
Alex Yatskov 2015-06-16 18:08:30 +09:00
parent c141246e7c
commit 9258b1d6fc
8 changed files with 54 additions and 38 deletions

View File

@ -73,6 +73,12 @@ func (this *database) load(dir string) error {
} }
func (this *database) save() error { func (this *database) save() error {
for _, ver := range this.vers {
if err := ver.finalize(); err != nil {
return err
}
}
return nil return nil
} }

33
dir.go
View File

@ -44,16 +44,14 @@ type versionedDir struct {
} }
func newVersionedDir(node *versionedNode, parent *versionedDir) *versionedDir { func newVersionedDir(node *versionedNode, parent *versionedDir) *versionedDir {
return &versionedDir{ dirs := make(map[string]*versionedDir)
dirs: make(map[string]*versionedDir), files := make(map[string]*versionedFile)
files: make(map[string]*versionedFile),
node: node, return &versionedDir{dirs, files, node, allocInode(), parent}
inode: allocInode(),
parent: parent}
} }
func (this *versionedDir) version() error { func (this *versionedDir) version() error {
if this.node.flags&NodeModified == NodeModified { if this.node.versioned {
return nil return nil
} }
@ -69,7 +67,7 @@ func (this *versionedDir) version() error {
return err return err
} }
node.flags |= NodeModified node.versioned = true
this.node = node this.node = node
return nil return nil
@ -91,8 +89,8 @@ 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
} }
@ -114,8 +112,8 @@ func (this *versionedDir) createFile(name string, flags int) (*versionedFile, er
file := newVersionedFile(node, this) file := newVersionedFile(node, this)
file.handle = handle file.handle = handle
this.files[name] = file this.files[name] = file
return file, nil return file, nil
} }
@ -163,17 +161,18 @@ func (this *versionedDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs
return this.createDir(req.Name) return this.createDir(req.Name)
} }
func (this *versionedDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { func (this *versionedDir) Remove(ctx context.Context, req *fuse.RemoveRequest) (err error) {
var fullPath string
if req.Dir { if req.Dir {
fullPath = this.dirs[req.Name].node.rebasedPath() if err = this.dirs[req.Name].node.remove(); err == nil {
delete(this.dirs, req.Name) delete(this.dirs, req.Name)
}
} else { } else {
fullPath = this.files[req.Name].node.rebasedPath() if err = this.files[req.Name].node.remove(); err == nil {
delete(this.files, req.Name) delete(this.files, req.Name)
}
} }
return os.Remove(fullPath) return
} }
func (this *versionedDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { func (this *versionedDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {

View File

@ -42,10 +42,7 @@ type versionedFile struct {
} }
func newVersionedFile(node *versionedNode, parent *versionedDir) *versionedFile { func newVersionedFile(node *versionedNode, parent *versionedDir) *versionedFile {
return &versionedFile{ return &versionedFile{node, allocInode(), parent, nil}
node: node,
inode: allocInode(),
parent: parent}
} }
func (this *versionedFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) { func (this *versionedFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {

View File

@ -1,3 +1 @@
{ {"deleted":["/test_file_2"]}
"deleted": ["/test_file_2"]
}

View File

@ -1,3 +1 @@
{ {"deleted":["/test_dira"]}
"deleted": ["/test_dira"]
}

28
node.go
View File

@ -33,17 +33,12 @@ import (
// versionedNode // versionedNode
// //
const (
NodeModified = 1 << iota
NodeDeleted
)
type versionedNode struct { type versionedNode struct {
path string path string
info os.FileInfo info os.FileInfo
ver *version ver *version
parent *versionedNode parent *versionedNode
flags uint versioned bool
} }
type versionedNodeMap map[string]*versionedNode type versionedNodeMap map[string]*versionedNode
@ -58,7 +53,18 @@ func newVersionedNode(path string, ver *version, parent *versionedNode) (*versio
} }
func newVersionedNodeStat(path string, ver *version, parent *versionedNode, info os.FileInfo) *versionedNode { func newVersionedNodeStat(path string, ver *version, parent *versionedNode, info os.FileInfo) *versionedNode {
return &versionedNode{path, info, ver, parent, 0} return &versionedNode{path, info, ver, parent, false}
}
func (this *versionedNode) remove() error {
if this.versioned {
if err := os.Remove(this.rebasedPath()); err != nil {
return err
}
}
this.ver.removePath(this.path)
return nil
} }
func (this *versionedNode) setAttr(req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error { func (this *versionedNode) setAttr(req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {

View File

@ -143,6 +143,14 @@ func (this *version) rebasePath(paths ...string) string {
return filepath.Join(combined...) return filepath.Join(combined...)
} }
func (this *version) removePath(path string) {
this.meta.Deleted = append(this.meta.Deleted, path)
}
func (this *version) finalize() error {
return this.meta.save()
}
func (this *version) dump(root *versionedDir, depth int) { func (this *version) dump(root *versionedDir, depth int) {
indent := strings.Repeat("\t", depth) indent := strings.Repeat("\t", depth)
for name, dir := range root.dirs { for name, dir := range root.dirs {

4
vfs.go
View File

@ -70,4 +70,8 @@ func main() {
if err := conn.MountError; err != nil { if err := conn.MountError; err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := db.save(); err != nil {
log.Fatal(err)
}
} }