2019-03-31 06:08:29 +00:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-03-23 07:01:34 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2019-03-31 06:08:29 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-03-23 07:01:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2019-03-31 06:08:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (fs *FilerServer) AtomicRenameEntry(ctx context.Context, req *filer_pb.AtomicRenameEntryRequest) (*filer_pb.AtomicRenameEntryResponse, error) {
|
|
|
|
|
2019-05-03 21:12:51 +00:00
|
|
|
glog.V(1).Infof("AtomicRenameEntry %v", req)
|
|
|
|
|
2020-12-11 07:50:32 +00:00
|
|
|
oldParent := util.FullPath(filepath.ToSlash(req.OldDirectory))
|
|
|
|
newParent := util.FullPath(filepath.ToSlash(req.NewDirectory))
|
|
|
|
|
|
|
|
if err := fs.filer.CanRename(oldParent, newParent); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-31 06:08:29 +00:00
|
|
|
ctx, err := fs.filer.BeginTransaction(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
oldEntry, err := fs.filer.FindEntry(ctx, oldParent.Child(req.OldName))
|
|
|
|
if err != nil {
|
|
|
|
fs.filer.RollbackTransaction(ctx)
|
|
|
|
return nil, fmt.Errorf("%s/%s not found: %v", req.OldDirectory, req.OldName, err)
|
|
|
|
}
|
|
|
|
|
2019-03-31 15:10:47 +00:00
|
|
|
var events MoveEvents
|
2020-12-11 07:50:32 +00:00
|
|
|
moveErr := fs.moveEntry(ctx, oldParent, oldEntry, newParent, req.NewName, &events)
|
2019-03-31 06:08:29 +00:00
|
|
|
if moveErr != nil {
|
|
|
|
fs.filer.RollbackTransaction(ctx)
|
2020-06-10 17:59:25 +00:00
|
|
|
return nil, fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, moveErr)
|
2019-03-31 06:08:29 +00:00
|
|
|
} else {
|
|
|
|
if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
|
|
|
|
fs.filer.RollbackTransaction(ctx)
|
2020-06-10 17:59:25 +00:00
|
|
|
return nil, fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, commitError)
|
2019-03-31 06:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &filer_pb.AtomicRenameEntryResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (fs *FilerServer) moveEntry(ctx context.Context, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, events *MoveEvents) error {
|
2020-04-10 08:37:03 +00:00
|
|
|
|
|
|
|
if err := fs.moveSelfEntry(ctx, oldParent, entry, newParent, newName, events, func() error {
|
|
|
|
if entry.IsDirectory() {
|
|
|
|
if err := fs.moveFolderSubEntries(ctx, oldParent, entry, newParent, newName, events); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-31 06:08:29 +00:00
|
|
|
}
|
2020-04-10 08:37:03 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("fail to move %s => %s: %v", oldParent.Child(entry.Name()), newParent.Child(newName), err)
|
2019-03-31 06:08:29 +00:00
|
|
|
}
|
2020-04-10 08:37:03 +00:00
|
|
|
|
|
|
|
return nil
|
2019-03-31 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (fs *FilerServer) moveFolderSubEntries(ctx context.Context, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, events *MoveEvents) error {
|
2019-03-31 06:08:29 +00:00
|
|
|
|
|
|
|
currentDirPath := oldParent.Child(entry.Name())
|
|
|
|
newDirPath := newParent.Child(newName)
|
|
|
|
|
|
|
|
glog.V(1).Infof("moving folder %s => %s", currentDirPath, newDirPath)
|
|
|
|
|
|
|
|
lastFileName := ""
|
|
|
|
includeLastFile := false
|
|
|
|
for {
|
|
|
|
|
2021-01-02 04:23:23 +00:00
|
|
|
entries, err := fs.filer.ListDirectoryEntries(ctx, currentDirPath, lastFileName, includeLastFile, 1024, "", "")
|
2019-03-31 06:08:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-03 16:22:56 +00:00
|
|
|
// println("found", len(entries), "entries under", currentDirPath)
|
2019-03-31 06:08:29 +00:00
|
|
|
|
|
|
|
for _, item := range entries {
|
|
|
|
lastFileName = item.Name()
|
2019-09-03 16:22:56 +00:00
|
|
|
// println("processing", lastFileName)
|
2019-03-31 15:10:47 +00:00
|
|
|
err := fs.moveEntry(ctx, currentDirPath, item, newDirPath, item.Name(), events)
|
2019-03-31 06:08:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(entries) < 1024 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
func (fs *FilerServer) moveSelfEntry(ctx context.Context, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, events *MoveEvents,
|
2020-04-10 08:37:03 +00:00
|
|
|
moveFolderSubEntries func() error) error {
|
2019-03-31 06:08:29 +00:00
|
|
|
|
|
|
|
oldPath, newPath := oldParent.Child(entry.Name()), newParent.Child(newName)
|
|
|
|
|
|
|
|
glog.V(1).Infof("moving entry %s => %s", oldPath, newPath)
|
|
|
|
|
2019-07-11 16:29:04 +00:00
|
|
|
if oldPath == newPath {
|
|
|
|
glog.V(1).Infof("skip moving entry %s => %s", oldPath, newPath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-31 06:08:29 +00:00
|
|
|
// add to new directory
|
2020-09-01 07:21:19 +00:00
|
|
|
newEntry := &filer.Entry{
|
2019-03-31 06:08:29 +00:00
|
|
|
FullPath: newPath,
|
|
|
|
Attr: entry.Attr,
|
|
|
|
Chunks: entry.Chunks,
|
2020-09-21 18:08:34 +00:00
|
|
|
Extended: entry.Extended,
|
2020-11-30 12:34:04 +00:00
|
|
|
Content: entry.Content,
|
2019-03-31 15:10:47 +00:00
|
|
|
}
|
2020-08-29 06:48:48 +00:00
|
|
|
createErr := fs.filer.CreateEntry(ctx, newEntry, false, false, nil)
|
2019-03-31 06:08:29 +00:00
|
|
|
if createErr != nil {
|
|
|
|
return createErr
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:37:03 +00:00
|
|
|
events.newEntries = append(events.newEntries, newEntry)
|
|
|
|
|
2020-04-13 04:00:55 +00:00
|
|
|
if moveFolderSubEntries != nil {
|
2020-04-10 08:37:03 +00:00
|
|
|
if moveChildrenErr := moveFolderSubEntries(); moveChildrenErr != nil {
|
|
|
|
return moveChildrenErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 06:08:29 +00:00
|
|
|
// delete old entry
|
2020-08-29 06:48:48 +00:00
|
|
|
deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false, false, false, nil)
|
2019-03-31 06:08:29 +00:00
|
|
|
if deleteErr != nil {
|
|
|
|
return deleteErr
|
|
|
|
}
|
2019-03-31 15:10:47 +00:00
|
|
|
|
|
|
|
events.oldEntries = append(events.oldEntries, entry)
|
2020-04-10 08:37:03 +00:00
|
|
|
|
2019-03-31 06:08:29 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2019-03-31 15:10:47 +00:00
|
|
|
|
|
|
|
type MoveEvents struct {
|
2020-09-01 07:21:19 +00:00
|
|
|
oldEntries []*filer.Entry
|
|
|
|
newEntries []*filer.Entry
|
2019-03-31 15:10:47 +00:00
|
|
|
}
|