2019-03-31 14:08:29 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-03-23 15:01:34 +08:00
|
|
|
"path/filepath"
|
2021-10-17 19:22:42 +08:00
|
|
|
"time"
|
2020-03-23 15:01:34 +08:00
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2019-03-31 14:08:29 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-03-23 15:01:34 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2019-03-31 14:08:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-12-11 15:50:32 +08: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 14:08:29 +08: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)
|
|
|
|
}
|
|
|
|
|
2021-10-17 19:22:42 +08:00
|
|
|
moveErr := fs.moveEntry(ctx, nil, oldParent, oldEntry, newParent, req.NewName, req.Signatures)
|
2019-03-31 14:08:29 +08:00
|
|
|
if moveErr != nil {
|
|
|
|
fs.filer.RollbackTransaction(ctx)
|
2020-06-11 01:59:25 +08:00
|
|
|
return nil, fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, moveErr)
|
2019-03-31 14:08:29 +08:00
|
|
|
} else {
|
|
|
|
if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
|
|
|
|
fs.filer.RollbackTransaction(ctx)
|
2020-06-11 01:59:25 +08:00
|
|
|
return nil, fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, commitError)
|
2019-03-31 14:08:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &filer_pb.AtomicRenameEntryResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-17 19:22:42 +08:00
|
|
|
func (fs *FilerServer) StreamRenameEntry(req *filer_pb.StreamRenameEntryRequest, stream filer_pb.SeaweedFiler_StreamRenameEntryServer) (err error) {
|
2020-04-10 16:37:03 +08:00
|
|
|
|
2021-10-17 19:22:42 +08:00
|
|
|
glog.V(1).Infof("StreamRenameEntry %v", req)
|
|
|
|
|
|
|
|
oldParent := util.FullPath(filepath.ToSlash(req.OldDirectory))
|
|
|
|
newParent := util.FullPath(filepath.ToSlash(req.NewDirectory))
|
|
|
|
|
|
|
|
if err := fs.filer.CanRename(oldParent, newParent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
ctx, err = fs.filer.BeginTransaction(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
oldEntry, err := fs.filer.FindEntry(ctx, oldParent.Child(req.OldName))
|
|
|
|
if err != nil {
|
|
|
|
fs.filer.RollbackTransaction(ctx)
|
|
|
|
return fmt.Errorf("%s/%s not found: %v", req.OldDirectory, req.OldName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
moveErr := fs.moveEntry(ctx, stream, oldParent, oldEntry, newParent, req.NewName, req.Signatures)
|
|
|
|
if moveErr != nil {
|
|
|
|
fs.filer.RollbackTransaction(ctx)
|
|
|
|
return fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, moveErr)
|
|
|
|
} else {
|
|
|
|
if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
|
|
|
|
fs.filer.RollbackTransaction(ctx)
|
|
|
|
return fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, commitError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FilerServer) moveEntry(ctx context.Context, stream filer_pb.SeaweedFiler_StreamRenameEntryServer, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, signatures []int32) error {
|
|
|
|
|
|
|
|
if err := fs.moveSelfEntry(ctx, stream, oldParent, entry, newParent, newName, func() error {
|
2020-04-10 16:37:03 +08:00
|
|
|
if entry.IsDirectory() {
|
2021-10-17 19:22:42 +08:00
|
|
|
if err := fs.moveFolderSubEntries(ctx, stream, oldParent, entry, newParent, newName, signatures); err != nil {
|
2020-04-10 16:37:03 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-03-31 14:08:29 +08:00
|
|
|
}
|
2020-04-10 16:37:03 +08:00
|
|
|
return nil
|
2021-07-01 16:19:28 +08:00
|
|
|
}, signatures); err != nil {
|
2020-04-10 16:37:03 +08:00
|
|
|
return fmt.Errorf("fail to move %s => %s: %v", oldParent.Child(entry.Name()), newParent.Child(newName), err)
|
2019-03-31 14:08:29 +08:00
|
|
|
}
|
2020-04-10 16:37:03 +08:00
|
|
|
|
|
|
|
return nil
|
2019-03-31 14:08:29 +08:00
|
|
|
}
|
|
|
|
|
2021-10-17 19:22:42 +08:00
|
|
|
func (fs *FilerServer) moveFolderSubEntries(ctx context.Context, stream filer_pb.SeaweedFiler_StreamRenameEntryServer, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, signatures []int32) 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 {
|
|
|
|
|
2021-04-25 02:49:03 +08:00
|
|
|
entries, hasMore, 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)
|
2021-10-17 19:22:42 +08:00
|
|
|
err := fs.moveEntry(ctx, stream, currentDirPath, item, newDirPath, item.Name(), signatures)
|
2019-03-31 14:08:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-01-15 15:10:37 +08:00
|
|
|
if !hasMore {
|
2019-03-31 14:08:29 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-17 19:22:42 +08:00
|
|
|
func (fs *FilerServer) moveSelfEntry(ctx context.Context, stream filer_pb.SeaweedFiler_StreamRenameEntryServer, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, moveFolderSubEntries func() error, signatures []int32) 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
|
2020-09-01 15:21:19 +08:00
|
|
|
newEntry := &filer.Entry{
|
2019-03-31 14:08:29 +08:00
|
|
|
FullPath: newPath,
|
|
|
|
Attr: entry.Attr,
|
|
|
|
Chunks: entry.Chunks,
|
2020-09-22 02:08:34 +08:00
|
|
|
Extended: entry.Extended,
|
2020-11-30 20:34:04 +08:00
|
|
|
Content: entry.Content,
|
2019-03-31 23:10:47 +08:00
|
|
|
}
|
2021-07-01 16:19:28 +08:00
|
|
|
if createErr := fs.filer.CreateEntry(ctx, newEntry, false, false, signatures); createErr != nil {
|
2019-03-31 14:08:29 +08:00
|
|
|
return createErr
|
|
|
|
}
|
2021-10-17 19:22:42 +08:00
|
|
|
if stream != nil {
|
|
|
|
if err := stream.Send(&filer_pb.StreamRenameEntryResponse{
|
|
|
|
Directory: string(newParent),
|
|
|
|
EventNotification: &filer_pb.EventNotification{
|
2021-11-03 14:39:16 +08:00
|
|
|
OldEntry: &filer_pb.Entry{
|
2021-10-17 19:22:42 +08:00
|
|
|
Name: entry.Name(),
|
|
|
|
},
|
|
|
|
NewEntry: newEntry.ToProtoEntry(),
|
|
|
|
DeleteChunks: false,
|
|
|
|
NewParentPath: string(newParent),
|
|
|
|
IsFromOtherCluster: false,
|
|
|
|
Signatures: nil,
|
|
|
|
},
|
|
|
|
TsNs: time.Now().UnixNano(),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 14:08:29 +08:00
|
|
|
|
2020-04-13 12:00:55 +08:00
|
|
|
if moveFolderSubEntries != nil {
|
2020-04-10 16:37:03 +08:00
|
|
|
if moveChildrenErr := moveFolderSubEntries(); moveChildrenErr != nil {
|
|
|
|
return moveChildrenErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 14:08:29 +08:00
|
|
|
// delete old entry
|
2021-07-01 16:19:28 +08:00
|
|
|
deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false, false, false, signatures)
|
2019-03-31 14:08:29 +08:00
|
|
|
if deleteErr != nil {
|
|
|
|
return deleteErr
|
|
|
|
}
|
2021-10-17 19:22:42 +08:00
|
|
|
if stream != nil {
|
|
|
|
if err := stream.Send(&filer_pb.StreamRenameEntryResponse{
|
|
|
|
Directory: string(oldParent),
|
|
|
|
EventNotification: &filer_pb.EventNotification{
|
|
|
|
OldEntry: &filer_pb.Entry{
|
|
|
|
Name: entry.Name(),
|
|
|
|
},
|
|
|
|
NewEntry: nil,
|
|
|
|
DeleteChunks: false,
|
|
|
|
NewParentPath: "",
|
|
|
|
IsFromOtherCluster: false,
|
|
|
|
Signatures: nil,
|
|
|
|
},
|
|
|
|
TsNs: time.Now().UnixNano(),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-03-31 23:10:47 +08:00
|
|
|
|
2019-03-31 14:08:29 +08:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|