diff options
| author | Chris Lu <chris.lu@gmail.com> | 2018-06-06 22:11:01 -0700 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2018-06-06 22:11:01 -0700 |
| commit | 5c25d29272b6a4d48df628ac640466d473d5d4aa (patch) | |
| tree | de8df08f7124c3994fbecc4b09d54daf0da8adeb /weed/filesys/dir.go | |
| parent | b3447f437565b5c3232f2bbc2109663459ffd8bd (diff) | |
| download | seaweedfs-5c25d29272b6a4d48df628ac640466d473d5d4aa.tar.xz seaweedfs-5c25d29272b6a4d48df628ac640466d473d5d4aa.zip | |
support renaming files
Diffstat (limited to 'weed/filesys/dir.go')
| -rw-r--r-- | weed/filesys/dir.go | 85 |
1 files changed, 82 insertions, 3 deletions
diff --git a/weed/filesys/dir.go b/weed/filesys/dir.go index f8fdba2c5..c463fde9c 100644 --- a/weed/filesys/dir.go +++ b/weed/filesys/dir.go @@ -28,6 +28,7 @@ var _ = fs.NodeMkdirer(&Dir{}) var _ = fs.NodeStringLookuper(&Dir{}) var _ = fs.HandleReadDirAller(&Dir{}) var _ = fs.NodeRemover(&Dir{}) +var _ = fs.NodeRenamer(&Dir{}) func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error { @@ -251,9 +252,10 @@ func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { return dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error { request := &filer_pb.DeleteEntryRequest{ - Directory: dir.Path, - Name: req.Name, - IsDirectory: req.Dir, + Directory: dir.Path, + Name: req.Name, + IsDirectory: req.Dir, + IsDeleteData: true, } glog.V(1).Infof("remove directory entry: %v", request) @@ -268,3 +270,80 @@ func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { }) } + +func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error { + + dir.NodeMapLock.Lock() + defer dir.NodeMapLock.Unlock() + + newDir := newDirectory.(*Dir) + + var entry *filer_pb.Entry + err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error { + + // find existing entry + { + request := &filer_pb.LookupDirectoryEntryRequest{ + Directory: dir.Path, + Name: req.OldName, + } + + glog.V(4).Infof("find existing directory entry: %v", request) + resp, err := client.LookupDirectoryEntry(ctx, request) + if err != nil { + return err + } + + entry = resp.Entry + + if entry.IsDirectory { + // do not support moving directory + return fuse.ENOTSUP + } + + glog.V(4).Infof("found existing directory entry resp: %+v", resp) + + } + + // add to new directory + { + request := &filer_pb.CreateEntryRequest{ + Directory: newDir.Path, + Entry: &filer_pb.Entry{ + Name: req.NewName, + IsDirectory: false, + Attributes: entry.Attributes, + Chunks: entry.Chunks, + }, + } + + glog.V(1).Infof("create new entry: %v", request) + if _, err := client.CreateEntry(ctx, request); err != nil { + return fmt.Errorf("create new entry: %v", err) + } + } + + // delete old entry + { + request := &filer_pb.DeleteEntryRequest{ + Directory: dir.Path, + Name: req.OldName, + IsDirectory: false, + IsDeleteData: false, + } + + glog.V(1).Infof("remove old entry: %v", request) + _, err := client.DeleteEntry(ctx, request) + if err != nil { + return err + } + + delete(dir.NodeMap, req.OldName) + + } + + return nil + }) + + return err +} |
