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 {
for _, ver := range this.vers {
if err := ver.finalize(); err != nil {
return err
}
}
return nil
}

29
dir.go
View File

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

20
node.go
View File

@ -33,17 +33,12 @@ import (
// versionedNode
//
const (
NodeModified = 1 << iota
NodeDeleted
)
type versionedNode struct {
path string
info os.FileInfo
ver *version
parent *versionedNode
flags uint
versioned bool
}
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 {
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 {

View File

@ -143,6 +143,14 @@ func (this *version) rebasePath(paths ...string) string {
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) {
indent := strings.Repeat("\t", depth)
for name, dir := range root.dirs {

4
vfs.go
View File

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