diff --git a/backend/protondrive/protondrive.go b/backend/protondrive/protondrive.go index 9e708c61f..060faf46a 100644 --- a/backend/protondrive/protondrive.go +++ b/backend/protondrive/protondrive.go @@ -649,17 +649,22 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e fs.Logf(nil, "protondrive: upload_concurrency %d is more than Proton allows: using %d", opt.UploadConcurrency, maxUploadConcurrency) opt.UploadConcurrency = maxUploadConcurrency } - if opt.DraftClientUID == "" { - // Saved before any upload, so drafts this remote leaves can be - // told apart from other clients' by later runs. - opt.DraftClientUID = random.String(32) - m.Set(draftClientUIDKey, opt.DraftClientUID) - } - c, err := connect(ctx, name, newConfigSession(ctx, m, opt), opt) if err != nil { return nil, err } + if opt.DraftClientUID == "" { + // Saved before any upload, so drafts this remote leaves can be + // told apart from other clients' by later runs. A config from + // the environment or a connection string saves nothing, so + // then the value is derived, the same on every run. + uid := random.String(32) + m.Set(draftClientUIDKey, uid) + if saved, ok := m.Get(draftClientUIDKey); !ok || saved != uid { + uid = c.derivedDraftClientUID(name) + } + opt.DraftClientUID, c.opt.DraftClientUID = uid, uid + } root = strings.Trim(root, "/") f := &Fs{ diff --git a/backend/protondrive/protondrive_internal_test.go b/backend/protondrive/protondrive_internal_test.go index 7a50edd38..014f527ca 100644 --- a/backend/protondrive/protondrive_internal_test.go +++ b/backend/protondrive/protondrive_internal_test.go @@ -160,6 +160,31 @@ func TestOptionsBackwardsCompatible(t *testing.T) { assert.True(t, f.opt.EnableCaching) } +// unsavedMap is a config which saves nothing, as one from environment +// variables or a connection string. +type unsavedMap struct{ configmap.Simple } + +// Set does nothing. +func (unsavedMap) Set(key, value string) {} + +func TestDraftClientUIDStableWhenUnsaved(t *testing.T) { + ctx := context.Background() + srv := protontest.New(t) + u := srv.AddUser("user", "pass") + uid := func(name string) string { + m := sessionConfig(srv, u) + f, err := NewFs(ctx, name, "", unsavedMap{m}) + require.NoError(t, err) + t.Cleanup(func() { _ = f.(*Fs).Shutdown(ctx) }) + got := f.(*Fs).c.opt.DraftClientUID + assert.Len(t, got, 32) + return got + } + first := uid("env") + assert.Equal(t, first, uid("env"), "a later run of the same remote has another draft client ID") + assert.NotEqual(t, first, uid("other"), "another remote of the account has the same draft client ID") +} + func TestNewFsPasswordFallback(t *testing.T) { srv := protontest.New(t) u := srv.AddUser("user", "pass") diff --git a/backend/protondrive/upload.go b/backend/protondrive/upload.go index a69fb3667..9370134fa 100644 --- a/backend/protondrive/upload.go +++ b/backend/protondrive/upload.go @@ -143,6 +143,14 @@ func (c *client) draftClientUID() *string { return &uid } +// derivedDraftClientUID returns a draft client ID for the remote name +// derived from the name and the account's volume, so it is the same on +// every run. +func (c *client) derivedDraftClientUID(name string) string { + sum := sha256.Sum256([]byte("rclone draft client\x00" + c.volumeID + "\x00" + name)) + return hex.EncodeToString(sum[:16]) +} + // uploadConcurrency returns the most blocks of one file to upload at // once. func (c *client) uploadConcurrency() int { diff --git a/docs/content/protondrive.md b/docs/content/protondrive.md index dc4a82996..a0530e4ce 100644 --- a/docs/content/protondrive.md +++ b/docs/content/protondrive.md @@ -298,6 +298,10 @@ An upload which is cancelled or fails can leave a draft behind. A draft left by the same remote is replaced on the next upload of that file. A draft left by another client, which may still be uploading, makes the upload fail unless the `replace_existing_draft` advanced option is set. +A remote set up with environment variables or a connection string, +which can't save settings, is told apart by its name and account, so two +machines using such a remote of the same name on the same account count +as the same remote. If the final step of an upload fails in a way which leaves it unknown whether the file was saved, rclone reports "upload outcome unknown: it