2018-06-07 14:39:30 +08:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
2019-01-17 09:17:19 +08:00
|
|
|
"context"
|
2018-12-30 02:31:36 +08:00
|
|
|
"github.com/seaweedfs/fuse"
|
|
|
|
"github.com/seaweedfs/fuse/fs"
|
2018-12-23 03:11:57 +08:00
|
|
|
"math"
|
|
|
|
"path/filepath"
|
|
|
|
|
2018-06-07 14:39:30 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error {
|
|
|
|
|
|
|
|
newDir := newDirectory.(*Dir)
|
|
|
|
|
2019-03-16 08:20:24 +08:00
|
|
|
return dir.wfs.withFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
|
2018-06-07 14:39:30 +08:00
|
|
|
|
|
|
|
// 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 {
|
2018-12-29 07:10:23 +08:00
|
|
|
glog.V(3).Infof("renaming find %s/%s: %v", dir.Path, req.OldName, err)
|
2018-06-07 14:39:30 +08:00
|
|
|
return fuse.ENOENT
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := resp.Entry
|
|
|
|
|
|
|
|
glog.V(4).Infof("found existing directory entry resp: %+v", resp)
|
|
|
|
|
|
|
|
return moveEntry(ctx, client, dir.Path, entry, newDir.Path, req.NewName)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveEntry(ctx context.Context, client filer_pb.SeaweedFilerClient, oldParent string, entry *filer_pb.Entry, newParent, newName string) error {
|
|
|
|
if entry.IsDirectory {
|
2019-03-05 05:00:08 +08:00
|
|
|
currentDirPath := filepath.ToSlash(filepath.Join(oldParent, entry.Name))
|
2018-06-07 14:39:30 +08:00
|
|
|
|
2018-12-17 15:20:08 +08:00
|
|
|
lastFileName := ""
|
|
|
|
includeLastFile := false
|
|
|
|
limit := math.MaxInt32
|
|
|
|
for limit > 0 {
|
|
|
|
request := &filer_pb.ListEntriesRequest{
|
|
|
|
Directory: currentDirPath,
|
|
|
|
StartFromFileName: lastFileName,
|
|
|
|
InclusiveStartFrom: includeLastFile,
|
|
|
|
Limit: 1024,
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("read directory: %v", request)
|
|
|
|
resp, err := client.ListEntries(ctx, request)
|
2018-06-07 14:39:30 +08:00
|
|
|
if err != nil {
|
2018-12-17 15:20:08 +08:00
|
|
|
glog.V(0).Infof("list %s: %v", oldParent, err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
if len(resp.Entries) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range resp.Entries {
|
|
|
|
lastFileName = item.Name
|
2019-03-05 05:00:08 +08:00
|
|
|
err := moveEntry(ctx, client, currentDirPath, item, filepath.ToSlash(filepath.Join(newParent, newName)), item.Name)
|
2018-12-17 15:20:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
limit--
|
|
|
|
}
|
|
|
|
if len(resp.Entries) < 1024 {
|
|
|
|
break
|
2018-06-07 14:39:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// add to new directory
|
|
|
|
{
|
|
|
|
request := &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: newParent,
|
|
|
|
Entry: &filer_pb.Entry{
|
|
|
|
Name: newName,
|
|
|
|
IsDirectory: entry.IsDirectory,
|
|
|
|
Attributes: entry.Attributes,
|
|
|
|
Chunks: entry.Chunks,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("create new entry: %v", request)
|
|
|
|
if _, err := client.CreateEntry(ctx, request); err != nil {
|
|
|
|
glog.V(0).Infof("renaming create %s/%s: %v", newParent, newName, err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete old entry
|
|
|
|
{
|
|
|
|
request := &filer_pb.DeleteEntryRequest{
|
|
|
|
Directory: oldParent,
|
|
|
|
Name: entry.Name,
|
|
|
|
IsDeleteData: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("remove old entry: %v", request)
|
|
|
|
_, err := client.DeleteEntry(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("renaming delete %s/%s: %v", oldParent, entry.Name, err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|