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