Skip to content

*: use path.join instead of manually concatenating paths #553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/backup_impl_restore_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"fmt"
"path"
"strings"
"time"

Expand Down Expand Up @@ -76,10 +77,10 @@ func (b *BackupContext) RestoreBackup(ctx context.Context, request *backuppb.Res
var backupPath string
if request.GetBucketName() == "" || request.GetPath() == "" {
backupBucketName = b.backupBucketName
backupPath = b.backupRootPath + meta.SEPERATOR + request.GetBackupName()
backupPath = path.Join(b.backupRootPath, request.GetBackupName())
} else {
backupBucketName = request.GetBucketName()
backupPath = request.GetPath() + meta.SEPERATOR + request.GetBackupName()
backupPath = path.Join(request.GetPath(), request.GetBackupName())
}

if getResp.GetCode() != backuppb.ResponseCode_Success {
Expand Down
2 changes: 1 addition & 1 deletion core/restore/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,9 @@ func (ct *CollectionTask) copyFiles(ctx context.Context, paths []string) ([]stri
return nil, fmt.Errorf("restore_collection: failed to copy backup data: %w", err)
}
tempPaths = append(tempPaths, tempPath)
ct.tearDownFns = append(ct.tearDownFns, ct.cleanTempFiles(tempPath))
}

ct.tearDownFns = append(ct.tearDownFns, ct.cleanTempFiles(tempDir))
return tempPaths, nil
}

Expand Down
53 changes: 9 additions & 44 deletions core/storage/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,65 +124,31 @@ type CopyPathInput struct {
DestBucket string
DestKeyFn func(attr ObjectAttr) string

// optional
CopySuffix string

// OnSuccess when an object copy success, this func will be call
// May be executed concurrently, please pay attention to thread safety
OnSuccess func(attr ObjectAttr)
}

// getAttrs get all attrs under bucket/prefix
func (c *Copier) getAttrs(ctx context.Context, bucket, prefix string, copySuffix string) ([]ObjectAttr, error) {
var attrs []ObjectAttr

paths, sizes, err := c.src.ListWithPrefix(ctx, bucket, prefix, true)
func (c *Copier) getAttrs(ctx context.Context, bucket, prefix string) ([]ObjectAttr, error) {
keys, sizes, err := c.src.ListWithPrefix(ctx, bucket, prefix, true)
if err != nil {
return nil, err
return nil, fmt.Errorf("storage: copier list %w", err)
}

for i, path := range paths {
attrs = append(attrs, ObjectAttr{Key: path, Length: sizes[i]})
attrs := make([]ObjectAttr, 0, len(keys))
for i, key := range keys {
attrs = append(attrs, ObjectAttr{Key: key, Length: sizes[i]})
c.totalSize.Add(uint64(sizes[i]))
c.cnt.Add(1)
}

return attrs, nil
}

func (c *Copier) getAttrs2(ctx context.Context, bucket, prefix string, copySuffix string) ([]ObjectAttr, error) {
var attrs []ObjectAttr

p, err := c.src.ListObjectsPage(ctx, bucket, prefix)
if err != nil {
return nil, err
}
for p.HasMorePages() {
page, err := p.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("storage: copier list objects %w", err)
}
for _, attr := range page.Contents {
if attr.IsEmpty() {
continue
}

if copySuffix != "" && !strings.HasSuffix(attr.Key, copySuffix) {
continue
}

attrs = append(attrs, attr)
c.totalSize.Add(uint64(attr.Length))
c.cnt.Add(1)
}
}

return attrs, nil
}

// CopyPrefix Copy all files under src path
func (c *Copier) CopyPrefix(ctx context.Context, i CopyPathInput) error {
srcAttrs, err := c.getAttrs(ctx, i.SrcBucket, i.SrcPrefix, i.CopySuffix)
srcAttrs, err := c.getAttrs(ctx, i.SrcBucket, i.SrcPrefix)
if err != nil {
return fmt.Errorf("storage: copier get src attrs %w", err)
}
Expand Down Expand Up @@ -233,14 +199,13 @@ func (c *Copier) CopyPrefix(ctx context.Context, i CopyPathInput) error {

func (c *Copier) Copy(ctx context.Context, srcPrefix, destPrefix, srcBucket, destBucket string) error {
fn := c.selectCopyFn()
srcAttrs, err := c.getAttrs(ctx, srcBucket, srcPrefix, "")
srcAttrs, err := c.getAttrs(ctx, srcBucket, srcPrefix)
if err != nil {
return fmt.Errorf("storage: copier get src attrs %w", err)
}
for _, srcAttr := range srcAttrs {
destKey := strings.Replace(srcAttr.Key, srcPrefix, destPrefix, 1)
err := fn(ctx, srcAttr, destKey, srcBucket, destBucket)
if err != nil {
if err := fn(ctx, srcAttr, destKey, srcBucket, destBucket); err != nil {
return err
}
}
Expand Down