protondrive: keep the draft client ID stable for remotes configured from the environment
A remote set up with environment variables or a connection string can't save its draft client ID, so it got a new random one on every run, and a draft left by an earlier run then blocked uploading that name as if another client were uploading it. When the ID can't be saved it is now derived from the remote name and the account's volume, so it is the same on every run. A remote in the config file still gets a random ID, saved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
94eedc942f
commit
674b538ca5
4 files changed
+49
-7
No files matched your search
@@ -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{
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user