Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

box: fix server-side copying a file over existing dst - fixes #3511

nielash3c7ad8d961af
diff --git a/backend/box/box.go b/backend/box/box.go
index 4f38955eb..7183f35e5 100644
--- a/backend/box/box.go
+++ b/backend/box/box.go
@@ -43,6 +43,7 @@ import (
"github.com/rclone/rclone/lib/jwtutil"
"github.com/rclone/rclone/lib/oauthutil"
"github.com/rclone/rclone/lib/pacer"
+ "github.com/rclone/rclone/lib/random"
"github.com/rclone/rclone/lib/rest"
"github.com/youmark/pkcs8"
"golang.org/x/oauth2"
@@ -256,7 +257,6 @@ func getQueryParams(boxConfig *api.ConfigJSON) map[string]string {
}
func getDecryptedPrivateKey(boxConfig *api.ConfigJSON) (key *rsa.PrivateKey, err error) {
-
block, rest := pem.Decode([]byte(boxConfig.BoxAppSettings.AppAuth.PrivateKey))
if len(rest) > 0 {
return nil, fmt.Errorf("box: extra data included in private key: %w", err)
@@ -619,7 +619,7 @@ func (f *Fs) CreateDir(ctx context.Context, pathID, leaf string) (newID string,
return shouldRetry(ctx, resp, err)
})
if err != nil {
- //fmt.Printf("...Error %v\n", err)
+ // fmt.Printf("...Error %v\n", err)
return "", err
}
// fmt.Printf("...Id %q\n", *info.Id)
@@ -966,6 +966,26 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object,
return nil, err
}
+ // check if dest already exists
+ item, err := f.preUploadCheck(ctx, leaf, directoryID, src.Size())
+ if err != nil {
+ return nil, err
+ }
+ if item != nil { // dest already exists, need to copy to temp name and then move
+ tempSuffix := "-rclone-copy-" + random.String(8)
+ fs.Debugf(remote, "dst already exists, copying to temp name %v", remote+tempSuffix)
+ tempObj, err := f.Copy(ctx, src, remote+tempSuffix)
+ if err != nil {
+ return nil, err
+ }
+ fs.Debugf(remote+tempSuffix, "moving to real name %v", remote)
+ err = f.deleteObject(ctx, item.ID)
+ if err != nil {
+ return nil, err
+ }
+ return f.Move(ctx, tempObj, remote)
+ }
+
// Copy the object
opts := rest.Opts{
Method: "POST",