protondrive: keep folders with unseen content, saved sessions on config edits and a usable Fs after shutdown
- rmdir asks the server whether a folder has any child, so it never trashes a folder holding Proton Docs, unreadable files or children added by another client since it was listed - rclone config asks before replacing a saved session (default no), and signs the old session out after signing in again - Shutdown only drops caches; after Disconnect the remote reports that it isn't signed in - update, move and copy refuse a file another client has trashed - ranged reads are computed on the decrypted size whatever original_file_size says Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0ec7dce0e5
commit
c00162ed20
7 files changed
+456
-47
No files matched your search
@@ -123,12 +123,21 @@ type node struct {
|
||||
// original is set and the extended attributes have it, else the
|
||||
// encrypted size.
|
||||
func (n *node) size(original bool) int64 {
|
||||
if original && n.X != nil && (n.X.SHA1 != "" || n.X.Size > 0 || len(n.X.BlockSizes) > 0) {
|
||||
return n.X.Size
|
||||
if size, ok := n.plaintextSize(); original && ok {
|
||||
return size
|
||||
}
|
||||
return n.EncryptedSize
|
||||
}
|
||||
|
||||
// plaintextSize returns the size of the file's content, and whether
|
||||
// the extended attributes record it.
|
||||
func (n *node) plaintextSize() (int64, bool) {
|
||||
if n.X != nil && (n.X.SHA1 != "" || n.X.Size > 0 || len(n.X.BlockSizes) > 0) {
|
||||
return n.X.Size, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// modTime returns the modification time from the extended attributes,
|
||||
// else the server's.
|
||||
func (n *node) modTime() time.Time {
|
||||
@@ -243,7 +252,7 @@ func (c *client) shouldRetry(ctx context.Context, resp *http.Response, err error
|
||||
return true, err
|
||||
case errors.As(err, &apiErr) && apiErr.Status >= 400 && apiErr.Status < 500:
|
||||
return false, err
|
||||
case errors.Is(err, errSessionExpired), errors.Is(err, errCaptcha):
|
||||
case errors.Is(err, errSessionExpired), errors.Is(err, errCaptcha), errors.Is(err, errNotSignedIn):
|
||||
return false, err
|
||||
}
|
||||
return fserrors.ShouldRetry(err), err
|
||||
@@ -604,6 +613,12 @@ func (c *client) getNode(ctx context.Context, id string) (*node, error) {
|
||||
if n := c.cache.get(id); n != nil {
|
||||
return n, nil
|
||||
}
|
||||
return c.fetchNode(ctx, id)
|
||||
}
|
||||
|
||||
// fetchNode reads node id, of any state, from the server whatever the
|
||||
// cache holds, and caches it. It wraps errNotFound if it doesn't exist.
|
||||
func (c *client) fetchNode(ctx context.Context, id string) (*node, error) {
|
||||
links, err := c.fetchLinks(ctx, []string{id})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get node %s: %w", id, err)
|
||||
@@ -632,6 +647,36 @@ func (c *client) getNode(ctx context.Context, id string) (*node, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// activeNode reads node id from the server whatever the cache holds,
|
||||
// and returns it if it is active. Otherwise it forgets the node and
|
||||
// the listing of its parent and returns errNotFound.
|
||||
func (c *client) activeNode(ctx context.Context, id string) (*node, error) {
|
||||
if id == c.root.ID {
|
||||
return c.root, nil
|
||||
}
|
||||
n, err := c.fetchNode(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if n.State != api.LinkStateActive {
|
||||
c.forget(id, n.ParentID)
|
||||
return nil, fmt.Errorf("get node %s: %w", id, errNotFound)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// hasChildren reports whether the server lists any child of folder id,
|
||||
// whatever its type and whether or not it can be read. It asks the
|
||||
// server, not the cache.
|
||||
func (c *client) hasChildren(ctx context.Context, id string) (bool, error) {
|
||||
var page api.ChildrenResponse
|
||||
opts := &rest.Opts{Method: "GET", Path: c.volumePath("/drive/v2/volumes", "folders", id, "children")}
|
||||
if err := c.apiCall(ctx, opts, nil, &page); err != nil {
|
||||
return false, fmt.Errorf("list %s: %w", id, err)
|
||||
}
|
||||
return len(page.LinkIDs) > 0 || page.More, nil
|
||||
}
|
||||
|
||||
// listChildren calls fn with batches of the active children of folder
|
||||
// parentID, one call at a time, until all are listed or fn returns an
|
||||
// error.
|
||||
|
||||
@@ -1027,3 +1027,55 @@ func TestMutationsInvalidateCache(t *testing.T) {
|
||||
assert.ElementsMatch(t, []string{"a"}, names(c.rootID()))
|
||||
requireDump(t, u)
|
||||
}
|
||||
|
||||
func TestHasChildren(t *testing.T) {
|
||||
t.Parallel()
|
||||
c, _, u := newTestClient(t)
|
||||
ctx := context.Background()
|
||||
empty := u.SeedFolder(u.RootID(), "empty")
|
||||
docs := u.SeedFolder(u.RootID(), "docs")
|
||||
u.SeedDoc(docs, "notes")
|
||||
unreadable := u.SeedFolder(u.RootID(), "unreadable")
|
||||
u.Corrupt(u.SeedFile(unreadable, "x", []byte("x"), time.Now()), "name")
|
||||
later := u.SeedFolder(u.RootID(), "later")
|
||||
|
||||
for id, want := range map[string]bool{empty: false, docs: true, unreadable: true, later: false} {
|
||||
got, err := c.hasChildren(ctx, id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, want, got, id)
|
||||
}
|
||||
|
||||
// A listing cached as empty doesn't hide a child added since.
|
||||
assert.Empty(t, listAll(t, c, later))
|
||||
u.SeedFile(later, "new", []byte("x"), time.Now())
|
||||
got, err := c.hasChildren(ctx, later)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, got)
|
||||
|
||||
_, err = c.hasChildren(ctx, "missing")
|
||||
assert.Equal(t, api.CodeNotExists, apiErrorCode(err))
|
||||
}
|
||||
|
||||
func TestActiveNode(t *testing.T) {
|
||||
t.Parallel()
|
||||
c, srv, u := newTestClient(t)
|
||||
ctx := context.Background()
|
||||
id := u.SeedFile(u.RootID(), "x", []byte("x"), time.Now())
|
||||
n, err := c.activeNode(ctx, id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "x", n.Name)
|
||||
require.Len(t, listAll(t, c, c.rootID()), 1)
|
||||
|
||||
// Trashed by another client while this one has it cached.
|
||||
other := newTestClientFor(t, srv, u, clientOptions{})
|
||||
require.NoError(t, other.trash(ctx, id))
|
||||
cached, err := c.getNode(ctx, id)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, api.LinkStateActive, cached.State, "the cache is stale")
|
||||
|
||||
_, err = c.activeNode(ctx, id)
|
||||
assert.ErrorIs(t, err, errNotFound)
|
||||
assert.Empty(t, listAll(t, c, c.rootID()), "the stale listing is dropped")
|
||||
_, err = c.activeNode(ctx, "missing")
|
||||
assert.ErrorIs(t, err, errNotFound)
|
||||
}
|
||||
@@ -254,13 +254,12 @@ type Options struct {
|
||||
|
||||
// Fs represents a remote proton drive
|
||||
type Fs struct {
|
||||
name string // name of this remote
|
||||
root string // the path we are working on
|
||||
opt Options // parsed config options
|
||||
features *fs.Features // optional features
|
||||
c *client // Proton Drive API client
|
||||
dirCache *dircache.DirCache // map of directory path to folder ID
|
||||
closeOnce *sync.Once // closes c once
|
||||
name string // name of this remote
|
||||
root string // the path we are working on
|
||||
opt Options // parsed config options
|
||||
features *fs.Features // optional features
|
||||
c *client // Proton Drive API client
|
||||
dirCache *dircache.DirCache // map of directory path to folder ID
|
||||
}
|
||||
|
||||
// Object describes a file
|
||||
@@ -449,6 +448,11 @@ func parseOptions(m configmap.Mapper) (*Options, error) {
|
||||
|
||||
// newConfigSession returns a session keeping its credentials in m.
|
||||
func newConfigSession(ctx context.Context, m configmap.Mapper, opt *Options) *session {
|
||||
return newStoreSession(ctx, opt, &configStore{m: m})
|
||||
}
|
||||
|
||||
// newStoreSession returns a session keeping its credentials in store.
|
||||
func newStoreSession(ctx context.Context, opt *Options, store credentialStore) *session {
|
||||
endpoint := opt.Endpoint
|
||||
if endpoint == "" {
|
||||
endpoint = defaultEndpoint
|
||||
@@ -457,7 +461,18 @@ func newConfigSession(ctx context.Context, m configmap.Mapper, opt *Options) *se
|
||||
if appVersion == "" {
|
||||
appVersion = appVersionFromRcloneVersion(fs.Version)
|
||||
}
|
||||
return newSession(fshttp.NewClient(ctx), strings.TrimSuffix(endpoint, "/"), appVersion, &configStore{m: m})
|
||||
return newSession(fshttp.NewClient(ctx), strings.TrimSuffix(endpoint, "/"), appVersion, store)
|
||||
}
|
||||
|
||||
// signOut ends the session old, if it has one, on the server. It is
|
||||
// best effort: a failure is only logged.
|
||||
func signOut(ctx context.Context, opt *Options, old credentials) {
|
||||
if old.UID == "" {
|
||||
return
|
||||
}
|
||||
if err := newStoreSession(ctx, opt, &memoryStore{creds: old}).logout(ctx); err != nil {
|
||||
fs.Debugf(nil, "protondrive: signing the previous session out failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// passwordSignIn signs s in with the username and password of opt,
|
||||
@@ -554,8 +569,19 @@ func configure(ctx context.Context, name string, m configmap.Mapper, in fs.Confi
|
||||
return nil, err
|
||||
}
|
||||
hasPassword := opt.Username != "" && opt.Password != ""
|
||||
store := &configStore{m: m}
|
||||
switch in.State {
|
||||
case "":
|
||||
if saved := store.Load(); saved.UID != "" && saved.AccessToken != "" && saved.RefreshToken != "" && saved.KeyPassword != "" {
|
||||
return fs.ConfigConfirm("reauth", false, "config_refresh_token", "Already signed in - sign in again?")
|
||||
}
|
||||
return fs.ConfigGoto("method")
|
||||
case "reauth":
|
||||
if in.Result != "true" {
|
||||
return nil, nil
|
||||
}
|
||||
return fs.ConfigGoto("method")
|
||||
case "method":
|
||||
return fs.ConfigConfirm("choose", !hasPassword, "config_browser_sign_in", `Sign in with a web browser?
|
||||
|
||||
`+thirdPartyNotice+`
|
||||
@@ -569,7 +595,8 @@ password set above.`)
|
||||
}
|
||||
return fs.ConfigGoto("password")
|
||||
case "browser":
|
||||
s := newConfigSession(ctx, m, opt)
|
||||
old := store.Load()
|
||||
s := newStoreSession(ctx, opt, store)
|
||||
fork, err := s.forkStart(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -578,6 +605,7 @@ password set above.`)
|
||||
if err := s.forkPoll(ctx, fork, signIn.pollInterval, signIn.pollTimeout); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
signOut(ctx, opt, old)
|
||||
return nil, nil
|
||||
case "password", "password_2fa":
|
||||
if !hasPassword {
|
||||
@@ -587,7 +615,8 @@ password set above.`)
|
||||
if in.State == "password_2fa" {
|
||||
twoFA = strings.TrimSpace(in.Result)
|
||||
}
|
||||
s := newConfigSession(ctx, m, opt)
|
||||
old := store.Load()
|
||||
s := newStoreSession(ctx, opt, store)
|
||||
err := passwordSignIn(ctx, s, opt, twoFA)
|
||||
if errors.Is(err, errNeedTwoFA) {
|
||||
return fs.ConfigInput("password_2fa", "config_2fa", "Two-factor authentication code")
|
||||
@@ -595,6 +624,7 @@ password set above.`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
signOut(ctx, opt, old)
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unknown state %q", in.State)
|
||||
@@ -624,11 +654,10 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
|
||||
|
||||
root = strings.Trim(root, "/")
|
||||
f := &Fs{
|
||||
name: name,
|
||||
root: root,
|
||||
opt: *opt,
|
||||
c: c,
|
||||
closeOnce: new(sync.Once),
|
||||
name: name,
|
||||
root: root,
|
||||
opt: *opt,
|
||||
c: c,
|
||||
}
|
||||
f.features = (&fs.Features{
|
||||
ReadMimeType: true,
|
||||
@@ -639,7 +668,7 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
|
||||
err = f.dirCache.FindRoot(ctx, false)
|
||||
if err != nil {
|
||||
if err != fs.ErrorDirNotFound {
|
||||
_ = f.Shutdown(ctx)
|
||||
c.close()
|
||||
return nil, fmt.Errorf("couldn't find the root: %w", err)
|
||||
}
|
||||
// Assume it is a file
|
||||
@@ -659,11 +688,11 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
|
||||
// File doesn't exist so return old f
|
||||
return f, nil
|
||||
}
|
||||
_ = f.Shutdown(ctx)
|
||||
c.close()
|
||||
return nil, err
|
||||
}
|
||||
f.features.Fill(ctx, &tempF)
|
||||
// XXX: update the old f here instead of returning tempF, since
|
||||
// Update the old f here instead of returning tempF, since
|
||||
// `features` were already filled with functions having *f as a receiver.
|
||||
// See https://github.com/rclone/rclone/issues/2182
|
||||
f.dirCache = tempF.dirCache
|
||||
@@ -831,18 +860,15 @@ func (f *Fs) Rmdir(ctx context.Context, dir string) error {
|
||||
if directoryID == f.c.rootID() {
|
||||
return errCanNotRemoveRootDirectory
|
||||
}
|
||||
err = f.c.listChildren(ctx, directoryID, func(nodes []*node) error {
|
||||
if len(nodes) > 0 {
|
||||
return fs.ErrorDirectoryNotEmpty
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if errors.Is(err, fs.ErrorDirectoryNotEmpty) {
|
||||
return fs.ErrorDirectoryNotEmpty
|
||||
}
|
||||
// Asked of the server, as a listing leaves out children rclone
|
||||
// can't read, which trashing the folder would trash too.
|
||||
nonEmpty, err := f.c.hasChildren(ctx, directoryID)
|
||||
if err != nil {
|
||||
return translateError(err, opDir)
|
||||
}
|
||||
if nonEmpty {
|
||||
return fs.ErrorDirectoryNotEmpty
|
||||
}
|
||||
if err := f.c.trash(ctx, directoryID); err != nil {
|
||||
return translateError(err, opDir)
|
||||
}
|
||||
@@ -870,7 +896,7 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (fs.Object,
|
||||
fs.Debugf(src, "Can't copy - not same remote type")
|
||||
return nil, fs.ErrorCantCopy
|
||||
}
|
||||
n, err := f.c.getNode(ctx, srcObj.n.ID)
|
||||
n, err := f.c.activeNode(ctx, srcObj.n.ID)
|
||||
if err != nil {
|
||||
return nil, translateError(err, opObject)
|
||||
}
|
||||
@@ -927,7 +953,7 @@ func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object,
|
||||
fs.Debugf(src, "Can't move - not same remote type")
|
||||
return nil, fs.ErrorCantMove
|
||||
}
|
||||
n, err := f.c.getNode(ctx, srcObj.n.ID)
|
||||
n, err := f.c.activeNode(ctx, srcObj.n.ID)
|
||||
if err != nil {
|
||||
return nil, translateError(err, opObject)
|
||||
}
|
||||
@@ -999,7 +1025,7 @@ func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := f.c.getNode(ctx, srcID)
|
||||
n, err := f.c.activeNode(ctx, srcID)
|
||||
if err != nil {
|
||||
return translateError(err, opDirMove)
|
||||
}
|
||||
@@ -1011,9 +1037,12 @@ func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string
|
||||
return nil
|
||||
}
|
||||
|
||||
// Disconnect signs the session out and removes it from the config
|
||||
// Disconnect signs the session out and removes it from the config.
|
||||
// Afterwards the Fs returns a "not signed in" error.
|
||||
func (f *Fs) Disconnect(ctx context.Context) error {
|
||||
return f.c.s.logout(ctx)
|
||||
err := f.c.s.logout(ctx)
|
||||
f.c.cache.clear()
|
||||
return err
|
||||
}
|
||||
|
||||
// DirCacheFlush resets the directory cache - used in testing as an
|
||||
@@ -1023,10 +1052,10 @@ func (f *Fs) DirCacheFlush() {
|
||||
f.c.cache.clear()
|
||||
}
|
||||
|
||||
// Shutdown releases the keys and caches of the Fs, which must not be
|
||||
// used afterwards
|
||||
// Shutdown drops the caches of the Fs. The Fs stays usable, as rclone
|
||||
// may still be using it.
|
||||
func (f *Fs) Shutdown(ctx context.Context) error {
|
||||
f.closeOnce.Do(f.c.close)
|
||||
f.c.cache.clear()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1099,8 +1128,19 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadClo
|
||||
if o.n == nil {
|
||||
return nil, fs.ErrorObjectNotFound
|
||||
}
|
||||
size := o.Size()
|
||||
fs.FixRangeOption(options, size)
|
||||
// Ranges are of the content the reader serves, whatever size the
|
||||
// object reports.
|
||||
size, known := o.n.plaintextSize()
|
||||
if !known && hasSuffixRange(options) {
|
||||
var err error
|
||||
if size, err = o.countContent(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
known = true
|
||||
}
|
||||
if known {
|
||||
fs.FixRangeOption(options, size)
|
||||
}
|
||||
var offset, limit int64 = 0, -1
|
||||
for _, option := range options {
|
||||
switch x := option.(type) {
|
||||
@@ -1121,6 +1161,36 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadClo
|
||||
return rc, nil
|
||||
}
|
||||
|
||||
// hasSuffixRange reports whether options ask for the end of the
|
||||
// content, which needs its size.
|
||||
func hasSuffixRange(options []fs.OpenOption) bool {
|
||||
for _, option := range options {
|
||||
if r, ok := option.(*fs.RangeOption); ok && r.Start < 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// countContent returns the size of the content of a file whose
|
||||
// extended attributes don't record it, by reading all of it.
|
||||
func (o *Object) countContent(ctx context.Context) (int64, error) {
|
||||
fs.Debugf(o, "reading the whole file to find its size")
|
||||
rc, err := o.fs.c.open(ctx, o.n, 0, -1)
|
||||
if err != nil {
|
||||
return 0, translateError(err, opObject)
|
||||
}
|
||||
n, err := io.Copy(io.Discard, rc)
|
||||
closeErr := rc.Close()
|
||||
if err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
if err != nil {
|
||||
return 0, translateError(err, opObject)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Update the object with the contents of the io.Reader, modTime and size
|
||||
//
|
||||
// If the object exists, its content becomes a new revision of it.
|
||||
@@ -1135,7 +1205,7 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
|
||||
r.SHA1, _ = src.Hash(ctx, hash.SHA1)
|
||||
}
|
||||
if o.n != nil {
|
||||
n, err := o.fs.c.getNode(ctx, o.n.ID)
|
||||
n, err := o.fs.c.activeNode(ctx, o.n.ID)
|
||||
if err != nil {
|
||||
return translateError(err, opObject)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package protondrive
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"maps"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -16,6 +19,7 @@ import (
|
||||
"github.com/rclone/rclone/fs/config/configmap"
|
||||
"github.com/rclone/rclone/fs/config/obscure"
|
||||
"github.com/rclone/rclone/fs/fserrors"
|
||||
"github.com/rclone/rclone/fs/object"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -230,6 +234,8 @@ func TestTwoRemotesIsolated(t *testing.T) {
|
||||
require.NoError(t, f2.Mkdir(ctx, "two"))
|
||||
|
||||
// Make both sessions refresh, saving new tokens.
|
||||
access1, access2 := m1["client_access_token"], m2["client_access_token"]
|
||||
srv.ResetRequests()
|
||||
srv.ExpireAccessToken(uid1)
|
||||
srv.ExpireAccessToken(uid2)
|
||||
f1.DirCacheFlush()
|
||||
@@ -243,6 +249,9 @@ func TestTwoRemotesIsolated(t *testing.T) {
|
||||
assert.Equal(t, "one", entries1[0].Remote())
|
||||
require.Len(t, entries2, 1)
|
||||
assert.Equal(t, "two", entries2[0].Remote())
|
||||
assert.Equal(t, 2, srv.Requests()["POST /auth/v4/refresh"])
|
||||
assert.NotEqual(t, access1, m1["client_access_token"], "remote 1 saved its refreshed token")
|
||||
assert.NotEqual(t, access2, m2["client_access_token"], "remote 2 saved its refreshed token")
|
||||
assert.Equal(t, uid1, m1["client_uid"])
|
||||
assert.Equal(t, uid2, m2["client_uid"])
|
||||
for _, k := range []string{"client_uid", "client_access_token", "client_refresh_token", "client_salted_key_pass", "draft_client_uid"} {
|
||||
@@ -364,3 +373,228 @@ func TestConfigStore(t *testing.T) {
|
||||
assert.Equal(t, creds, s.Load(), "saved credentials must not be shadowed by spent ones")
|
||||
})
|
||||
}
|
||||
|
||||
// putFile uploads content to f at remote and returns the object.
|
||||
func putFile(t *testing.T, f fs.Fs, remote string, content []byte) fs.Object {
|
||||
t.Helper()
|
||||
src := object.NewStaticObjectInfo(remote, time.Now(), int64(len(content)), true, nil, nil)
|
||||
o, err := f.Put(context.Background(), bytes.NewReader(content), src)
|
||||
require.NoError(t, err)
|
||||
return o
|
||||
}
|
||||
|
||||
// readObject returns the content of o read with options.
|
||||
func readObject(t *testing.T, o fs.Object, options ...fs.OpenOption) ([]byte, error) {
|
||||
t.Helper()
|
||||
rc, err := o.Open(context.Background(), options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { require.NoError(t, rc.Close()) }()
|
||||
return io.ReadAll(rc)
|
||||
}
|
||||
|
||||
func TestRmdirRefusesHiddenChildren(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUser("user", "pass")
|
||||
f := newTestFs(t, sessionConfig(srv, u))
|
||||
|
||||
require.NoError(t, f.Mkdir(ctx, "docs"))
|
||||
docsID, err := f.dirCache.FindDir(ctx, "docs", false)
|
||||
require.NoError(t, err)
|
||||
u.SeedDoc(docsID, "notes")
|
||||
assert.Equal(t, fs.ErrorDirectoryNotEmpty, f.Rmdir(ctx, "docs"))
|
||||
|
||||
// Filled by another client after an empty listing was cached.
|
||||
require.NoError(t, f.Mkdir(ctx, "later"))
|
||||
entries, err := f.List(ctx, "later")
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, entries)
|
||||
laterID, err := f.dirCache.FindDir(ctx, "later", false)
|
||||
require.NoError(t, err)
|
||||
u.SeedFile(laterID, "new.txt", []byte("x"), time.Now())
|
||||
assert.Equal(t, fs.ErrorDirectoryNotEmpty, f.Rmdir(ctx, "later"))
|
||||
|
||||
require.NoError(t, f.Mkdir(ctx, "empty"))
|
||||
require.NoError(t, f.Rmdir(ctx, "empty"))
|
||||
paths := map[string]bool{}
|
||||
for _, d := range u.Dump() {
|
||||
paths[d.Path] = d.State == api.LinkStateActive
|
||||
}
|
||||
assert.True(t, paths["docs/notes"])
|
||||
assert.True(t, paths["later/new.txt"])
|
||||
}
|
||||
|
||||
func TestConfigKeepsSession(t *testing.T) {
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUser("user", "pass")
|
||||
ri, err := fs.Find("protondrive")
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("default", func(t *testing.T) {
|
||||
m := sessionConfig(srv, u)
|
||||
want := maps.Clone(m)
|
||||
srv.ResetRequests()
|
||||
asked := configAnswers(t, m, signInOptions{}, map[string]string{"reauth": "false"})
|
||||
require.Len(t, asked, 1)
|
||||
assert.Equal(t, false, asked[0].Option.Default)
|
||||
assert.Equal(t, "config_refresh_token", asked[0].Option.Name)
|
||||
assert.Empty(t, srv.Requests())
|
||||
assert.Equal(t, want, m)
|
||||
})
|
||||
t.Run("config update", func(t *testing.T) {
|
||||
// As "rclone config update remote enable_caching=false" runs it.
|
||||
m := sessionConfig(srv, u)
|
||||
srv.ResetRequests()
|
||||
out, err := fs.BackendConfig(context.Background(), "test", m, ri, configmap.Simple{"config_refresh_token": "false"}, fs.ConfigIn{})
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, out)
|
||||
assert.Empty(t, srv.Requests())
|
||||
})
|
||||
}
|
||||
|
||||
func TestConfigSignInAgain(t *testing.T) {
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUser("user", "pass")
|
||||
m := sessionConfig(srv, u)
|
||||
m["username"] = "user"
|
||||
m["password"] = obscure.MustObscure("pass")
|
||||
old := maps.Clone(m)
|
||||
|
||||
configAnswers(t, m, signInOptions{}, map[string]string{"reauth": "true", "choose": "false"})
|
||||
assert.NotEqual(t, old["client_uid"], m["client_uid"])
|
||||
assert.Equal(t, 1, srv.Requests()["DELETE /core/v4/auth"], "the old session is signed out")
|
||||
requireSession(t, srv, m, u)
|
||||
|
||||
// The old session no longer works.
|
||||
old["username"], old["password"] = "", ""
|
||||
_, err := NewFs(context.Background(), "test", "", old)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestShutdownKeepsFsUsable(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUser("user", "pass")
|
||||
f := newTestFs(t, sessionConfig(srv, u))
|
||||
o := putFile(t, f, "dir/a.txt", []byte("hello"))
|
||||
|
||||
require.NoError(t, f.Shutdown(ctx))
|
||||
require.NoError(t, f.Shutdown(ctx))
|
||||
got, err := readObject(t, o)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "hello", string(got))
|
||||
putFile(t, f, "dir/b.txt", []byte("world"))
|
||||
entries, err := f.List(ctx, "dir")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, entries, 2)
|
||||
o, err = f.NewObject(ctx, "dir/b.txt")
|
||||
require.NoError(t, err)
|
||||
got, err = readObject(t, o)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "world", string(got))
|
||||
}
|
||||
|
||||
func TestDisconnect(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUser("user", "pass")
|
||||
m := sessionConfig(srv, u)
|
||||
f := newTestFs(t, m)
|
||||
o := putFile(t, f, "a.txt", []byte("hello"))
|
||||
|
||||
require.NoError(t, f.Disconnect(ctx))
|
||||
assert.Empty(t, m["client_access_token"])
|
||||
assert.Empty(t, m["client_refresh_token"])
|
||||
|
||||
_, err := f.List(ctx, "")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "rclone config reconnect")
|
||||
_, err = readObject(t, o)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "rclone config reconnect")
|
||||
_, err = f.Put(ctx, bytes.NewReader([]byte("x")), object.NewStaticObjectInfo("b.txt", time.Now(), 1, true, nil, nil))
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "rclone config reconnect")
|
||||
}
|
||||
|
||||
func TestTrashedBehindTheFsBack(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUser("user", "pass")
|
||||
f := newTestFs(t, sessionConfig(srv, u))
|
||||
other := newTestFs(t, sessionConfig(srv, u))
|
||||
|
||||
o := putFile(t, f, "a.txt", []byte("hello"))
|
||||
require.NoError(t, f.Mkdir(ctx, "dir"))
|
||||
_, err := f.List(ctx, "")
|
||||
require.NoError(t, err)
|
||||
trash := func(remote string) {
|
||||
otherObj, err := other.NewObject(ctx, remote)
|
||||
if err == nil {
|
||||
require.NoError(t, otherObj.Remove(ctx))
|
||||
return
|
||||
}
|
||||
require.NoError(t, other.Purge(ctx, remote))
|
||||
}
|
||||
trash("a.txt")
|
||||
trash("dir")
|
||||
|
||||
src := object.NewStaticObjectInfo("a.txt", time.Now(), 1, true, nil, nil)
|
||||
assert.Equal(t, fs.ErrorObjectNotFound, o.Update(ctx, bytes.NewReader([]byte("x")), src))
|
||||
_, err = f.Move(ctx, o, "b.txt")
|
||||
assert.Equal(t, fs.ErrorObjectNotFound, err)
|
||||
_, err = f.Copy(ctx, o, "c.txt")
|
||||
assert.Equal(t, fs.ErrorObjectNotFound, err)
|
||||
assert.Equal(t, fs.ErrorDirNotFound, f.DirMove(ctx, f, "dir", "dir2"))
|
||||
for _, d := range u.Dump() {
|
||||
assert.NotContains(t, []string{"b.txt", "c.txt", "dir2"}, d.Path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenRange(t *testing.T) {
|
||||
srv := protontest.New(t)
|
||||
u := srv.AddUser("user", "pass")
|
||||
content := []byte("0123456789abcdefghijklmnopqrstuvwxyz")
|
||||
|
||||
// A file without extended attributes, whose plaintext size only
|
||||
// its content tells.
|
||||
seeded := u.SeedFile(u.RootID(), "tmp", content, time.Now())
|
||||
stored := u.StoredFile(seeded)
|
||||
stored.Revision.XAttr = ""
|
||||
m := sessionConfig(srv, u)
|
||||
cleanup := newTestFs(t, m)
|
||||
o, err := cleanup.NewObject(context.Background(), "tmp")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, o.Remove(context.Background()))
|
||||
u.SeedStoredFile(u.RootID(), stored)
|
||||
|
||||
for _, original := range []bool{true, false} {
|
||||
t.Run(fmt.Sprint("original_file_size=", original), func(t *testing.T) {
|
||||
m := sessionConfig(srv, u)
|
||||
m["original_file_size"] = fmt.Sprint(original)
|
||||
f := newTestFs(t, m)
|
||||
put := putFile(t, f, fmt.Sprintf("put-%v.txt", original), content)
|
||||
bare, err := f.NewObject(context.Background(), "tmp")
|
||||
require.NoError(t, err)
|
||||
for _, o := range []fs.Object{put, bare} {
|
||||
for _, test := range []struct {
|
||||
opt fs.OpenOption
|
||||
want string
|
||||
}{
|
||||
{&fs.RangeOption{Start: -1, End: 5}, "vwxyz"},
|
||||
{&fs.RangeOption{Start: 30, End: -1}, "uvwxyz"},
|
||||
{&fs.RangeOption{Start: 30, End: 1000}, "uvwxyz"},
|
||||
{&fs.RangeOption{Start: 2, End: 4}, "234"},
|
||||
{&fs.SeekOption{Offset: 33}, "xyz"},
|
||||
{&fs.RangeOption{Start: -1, End: 1000}, string(content)},
|
||||
} {
|
||||
got, err := readObject(t, o, test.opt)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, test.want, string(got), "%s %v", o.Remote(), test.opt)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,10 @@ import (
|
||||
// refreshed and the user must sign in again.
|
||||
var errSessionExpired = errors.New(`session expired: run "rclone config reconnect remote:"`)
|
||||
|
||||
// errNotSignedIn is returned by an authenticated call of a session
|
||||
// which has no credentials, as after signing out.
|
||||
var errNotSignedIn = errors.New(`not signed in: run "rclone config reconnect remote:"`)
|
||||
|
||||
// errCaptcha is returned by password sign-in when Proton asks for human
|
||||
// verification, which only its sign-in page can do.
|
||||
var errCaptcha = errors.New(`password sign-in needs a CAPTCHA solved: run "rclone config reconnect remote:" and choose browser sign-in`)
|
||||
@@ -191,12 +195,16 @@ func (s *session) serverTime() time.Time {
|
||||
// semantics.
|
||||
//
|
||||
// An unsuccessful HTTP status or a response Code outside 1000-1999 is
|
||||
// returned as an *api.Error. On HTTP 401 the session is refreshed and
|
||||
// returned as an *api.Error. Without credentials it returns
|
||||
// errNotSignedIn and sends nothing. On HTTP 401 the session is refreshed and
|
||||
// the call retried once, so opts.Body, if set, must be an io.Seeker to
|
||||
// be retried. If the session can't be refreshed the error wraps
|
||||
// errSessionExpired.
|
||||
func (s *session) call(ctx context.Context, opts *rest.Opts, request, response any) (*http.Response, error) {
|
||||
creds := s.current()
|
||||
if creds.UID == "" {
|
||||
return nil, fserrors.FatalError(errNotSignedIn)
|
||||
}
|
||||
resp, err := s.do(ctx, opts, request, response, &creds)
|
||||
var apiErr *api.Error
|
||||
if !errors.As(err, &apiErr) || apiErr.Status != http.StatusUnauthorized {
|
||||
|
||||
@@ -515,7 +515,11 @@ func TestLogout(t *testing.T) {
|
||||
|
||||
require.NoError(t, s.logout(context.Background()))
|
||||
assert.Equal(t, credentials{}, store.Load())
|
||||
assert.ErrorIs(t, authCall(context.Background(), s), errSessionExpired)
|
||||
srv.ResetRequests()
|
||||
err := authCall(context.Background(), s)
|
||||
assert.ErrorIs(t, err, errNotSignedIn)
|
||||
assert.True(t, fserrors.IsFatalError(err))
|
||||
assert.Empty(t, srv.Requests(), "no request is sent without a session")
|
||||
|
||||
// The server ended the session too.
|
||||
old := newSession(srv.Client(), srv.URL(), testAppVersion, &memoryStore{creds: before})
|
||||
|
||||
@@ -59,10 +59,6 @@ Small, specific, independently actionable. Remove only when actually done.
|
||||
bad verifier token, a missing block and a stale `CurrentRevisionID`
|
||||
(the fake uses 422 / 2000, which the spec maps to "upgrade rclone").
|
||||
|
||||
- [ ] protondrive: `Rmdir` checks emptiness with `listChildren`, which
|
||||
leaves out Proton Docs/Sheets, drafts and unreadable nodes, so a folder
|
||||
holding only those is trashed (recoverable from the trash). Needs a raw
|
||||
child count from the client.
|
||||
- [ ] protondrive: several `Fs` of one account in one process each have
|
||||
their own node cache; only Move/DirMove sources are invalidated across
|
||||
them. Events (spec 6.2) fix this.
|
||||
|
||||
Reference in new issue
Block a user