Compare commits

..
166 Commits
Author SHA1 Message Date
Nick Craig-Wood be8d6044ea Version v1.75.2 2026-10-09 10:34:46 +01:00
Nick Craig-Wood bd07f481e2 build: fix lint errors after golang.org/x/net update
golang.org/x/net v0.60.0 deprecates its HTTP/2 server and transport in
favour of net/http.

Make the h2c test use http.Transport with unencrypted HTTP/2. serve
restic --stdio still needs http2.Server to serve HTTP/2 on a single
connection as http.Server has no way of doing that.

(cherry picked from commit d3db33c6b7)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 56059c47c8 build: update golang.org/x/net to v0.60.0 to fix multiple CVEs
Upgrade to v0.60.0 of golang.org/x/net in order to address:

- CVE-2026-78659: net/http: HTTP/2 server memory exhaustion due to Trailer headers
- CVE-2026-78669: net/http: excessive CPU consumption from repeated initial window changes
- CVE-2026-78663: net/http: double flow control refund on HTTP/2 server streams
- CVE-2026-97032: net/http: HTTP/2 server crash due to HPACK encoder race

This also updates golang.org/x/crypto, sync, sys, term and text to the
versions golang.org/x/net v0.60.0 requires.

(cherry picked from commit e4a3adb94c)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 35ab21109b rest: fix TestStripHeadersOnCrossHostRedirectFn failing on go1.26
Before go1.27 net/http compares hosts case sensitively when deciding
whether to keep Authorization on a redirect, so it dropped the header
itself on the localhost -> LOCALHOST redirect in the
SameHostDifferentCase test, before StripHeadersOnCrossHostRedirectFn
was consulted. The other headers still show the function treats the
hosts as the same.

(cherry picked from commit f440573c3e)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood fc7a57b447 serve s3: apply --metadata-max-objects to each user separately
The metadata of the objects of all users was kept in one store, so with
--auth-proxy or several --auth-key credentials one user storing the
metadata of many objects could make the server forget that of every
other user.

Each user (each access key) now has a store of their own.

(cherry picked from commit c2ce9a0bfb)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 028f2b4568 serve s3: apply the multipart upload limits to each user separately
--multipart-max-uploads and --multipart-streaming-buffer-total limited
the uploads of all users together, so with --auth-proxy or several
--auth-key credentials one user could use up the limits and stop every
other user uploading.

They now limit the uploads of each user (each access key) separately.

(cherry picked from commit b1657092d3)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 62e770eb54 serve s3: fix multipart upload corruption if the backend write of an early part fails
A multipart part which arrives before the parts ahead of it is buffered
and acknowledged to the client, then written to the backend when its
turn comes. If that write failed the part was dropped, possibly with
some of it already written, but the upload carried on as if nothing
had happened, so it could be completed with the part missing, or with
a resend of the part appended to the partial data.

Such an upload is now failed, so the client starts it again.

(cherry picked from commit 5379bd5409)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 652bbb0908 serve s3: fix presigned URL being turned into a copy of any object GHSA-w3h7-jxxq-vfrp CVE-PENDING
The SigV4 verification in gofakes3 only checked the headers the
client listed as signed and never looked at the other headers which
arrived. A presigned PUT URL signs only the host header, so a holder
of one could add an unsigned x-amz-copy-source header and have the
server copy any object it can reach - in any bucket, and in any
remote when serving a cloud backend - into the object the URL names,
then read it back with the same URL. This affected both --auth-key
and --auth-proxy. AWS refuses such a request with AccessDenied, as
it requires every x-amz-* header to be signed.

This updates gofakes3 to a version which refuses requests carrying
an x-amz-* header not in their signed headers list with AccessDenied,
and adds tests that a presigned PUT URL can't be turned into a copy
in either auth mode. Real S3 clients always sign x-amz-* headers so
they are unaffected.

(cherry picked from commit c3b582ccdf)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 4df376e2db serve s3: test multipart buffer limits can't be bypassed by overflowing part sizes GHSA-qjw8-8hmp-c3m6
In v1.75 the multipart reorder buffer admitted the next part the stream
needed however big, adding its client-declared length to the running
total unchecked. Two concurrent requests for the next part declaring
huge lengths overflowed the total negative, after which out-of-order
parts were admitted past --multipart-streaming-buffer-limit.

The next part is now streamed straight to the backend without being
charged to the buffer, and a buffered part is only admitted if it fits
in what is left of the limits, so this can no longer happen. Add a
test of the attack against both the per-upload and server-wide limits.

(cherry picked from commit d120b5fe0b)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 68e43e5b88 serve s3: document memory use and bring the docs up to date
Add a section on the memory serve s3 uses and how to limit it, and
update the docs for the recent changes to multipart uploads, listings
and metadata.

(cherry picked from commit da71a52714)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 1dcc246e03 serve s3: fix stale metadata being returned for objects changed elsewhere
serve s3 keeps the metadata of objects uploaded through it in memory.
If the object was then changed some other way - by another --auth-proxy
user sharing the backend, another server or directly on the backend -
serve s3 went on returning the old metadata, including the old
modification time, for the new object.

The size and modification time of the object are now stored with its
metadata, which is ignored if they no longer match.

(cherry picked from commit 221398e2d0)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 4cc69c2a62 serve s3: fix uploads failing when the auth proxy expires the user's backend
With --auth-proxy the backend of each user is shut down once it has
been unused for 5 minutes. A request didn't count as using it, so a
long upload or download could have its backend shut down under it and
fail. A multipart upload used the backend it started on throughout, so
failed if the client paused for more than 5 minutes between parts, or
the auth proxy changed the user's secret.

Each request and multipart upload now holds the backend it uses until
it is finished with it. In-progress multipart uploads are aborted when
the server is shut down so their backends are released.

(cherry picked from commit 0edd92134e)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 8d97a50c4a serve s3: stop auth proxy users listing or using each other's multipart uploads GHSA-35g6-9fjv-rmx5 CVE-PENDING
With --auth-proxy each access key ID may be mapped to a different
backend, but gofakes3 kept a single record of multipart uploads for
everyone. Any user could list the uploads of every other user with
ListMultipartUploads (including their upload IDs) and the parts of
them with ListParts.

With --disable-multipart-streaming the uploads are held by gofakes3,
whose upload IDs are sequential so easy to guess. Any user could
upload parts to, abort, or complete another user's upload - which
wrote it to the backend of the user completing it.

This updates gofakes3 to a version which can keep each user's uploads
private and uses the access key ID as the owner.

(cherry picked from commit 32b08f0237)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 7c2405c035 serve s3: stop auth proxy users using each other's multipart uploads and metadata GHSA-35g6-9fjv-rmx5 CVE-PENDING
With --auth-proxy each access key ID may be mapped to a different
backend, but serve s3 found a streaming multipart upload by its upload
ID alone and stored object metadata by path alone.

A user who learnt another user's upload ID (for example from
ListMultipartUploads on a bucket of the same name) could upload parts
to, complete or abort that upload on the other user's backend. A user
with an object at the same bucket and key as another user was returned
the other user's metadata by HeadObject and GetObject and could
overwrite it.

Keep the uploads and metadata of each access key ID apart.

(cherry picked from commit a16a901de7)
2026-10-09 00:01:36 +01:00
Nick Craig-Wood 10335538d1 serve s3: keep the state remembered for each user in one place
Refactor the state serve s3 keeps between requests - in-flight
multipart uploads and object metadata - so that it is reached through
a tenant struct for the user making the request. This makes it
possible to keep users' state apart when they are serving different
backends.

There is only one tenant for now, so there is no change in behaviour.

(cherry picked from commit 2385e27cfd)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood a7b1599204 serve s3: add --metadata-max-objects to limit the metadata kept in memory
serve s3 keeps the metadata of the objects uploaded to it in memory,
with no limit on the number of objects, so memory use grew with every
object uploaded, and a client could use a lot by uploading many empty
objects with large metadata.

The new --metadata-max-objects (default 100000, 0 for no limit) limits
the number of objects whose metadata is kept, forgetting the metadata of
the least recently used first. As the metadata is only kept in memory
it is already lost when the server restarts; the modification time is
stored on the object itself so is never lost.

(cherry picked from commit 77ed639805)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 518f43dee9 serve s3: forget expired multipart uploads completely
When an abandoned multipart upload expired after --multipart-expiry,
gofakes3's record of it was left behind, so it stayed in memory and in
ListMultipartUploads until the server was restarted.

That record is now removed too.

(cherry picked from commit 0ce74ffaac)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 43d3374465 serve s3: update gofakes3 to limit the memory requests can use
This picks up these fixes to serve s3 from gofakes3:

- XML request bodies (CompleteMultipartUpload, DeleteObjects) bigger
  than 10 MiB are rejected rather than read into memory.
- DeleteObjects requests with more than 1000 keys are rejected.
- CompleteMultipartUpload checks the upload exists before reading the
  request body.
- With --disable-multipart-streaming, parts are no longer allocated at
  their declared size before the data arrives.
- Browser POST uploads hold at most 1 MiB in memory and are limited to
  5 GB.

It also brings a SlowDown error, so the SlowDown replies serve s3 sends
when a part waits too long for buffer space, or too many multipart
uploads are in progress, now have status 503 as S3 does rather than
500.

(cherry picked from commit f4c1268f33)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 10875eec70 serve s3: add --multipart-max-uploads to limit uploads in progress
There was no limit on the number of multipart uploads in progress, and
each one holds server resources until it completes or expires - once it
has data, a VFS upload and the buffers of the remote's own upload - so a
client could use unbounded memory by starting many uploads.

The new --multipart-max-uploads (default 1000, 0 for no limit) limits
the streamed multipart uploads in progress. Starting another fails with
SlowDown, which S3 clients retry, before anything is created on the
remote.

With --multipart-expiry 0 nothing ever cleans up abandoned uploads, so
they go on counting towards the limit until no new upload can be
started. A warning is logged at startup when both are set.

(cherry picked from commit d24568e3e6)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 40eaa114c1 serve s3: start the upload to the backend on the first multipart part
Starting a multipart upload opened its VFS upload straight away, which
allocates a 100 KiB buffer (--streaming-upload-cutoff) and starts a
goroutine and a transfer, all held until the upload finished or was
expired after --multipart-expiry (default 24h). A client could make the
server hold about 100 KiB per tiny CreateMultipartUpload request.

The VFS upload is now only opened when the first part is written to it,
so an upload with no parts holds none of these.

(cherry picked from commit 7d2c16269d)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood a34b606abf serve s3: add --multipart-streaming-buffer-total to limit memory across uploads
--multipart-streaming-buffer-limit only limits the memory buffered for
parts arriving out of order in each upload, so a client could use as
much memory as it liked by starting more uploads.

The new --multipart-streaming-buffer-total (default 1G, 0 for no limit)
limits the memory buffered by all uploads together. A part which would
take either over its limit waits as before. Since the part the stream
needs next is never buffered, every upload can still make progress.

(cherry picked from commit 87d81fcb2e)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 37aa0cb629 serve s3: stream in-order multipart parts without buffering them
Every multipart part was buffered in memory in full before being written
to the backend, even the part the stream needed next. There is no limit
on part size, and several concurrent requests for the current part were
each admitted, so a client could make the server hold as much memory as
it sent, whatever --multipart-streaming-buffer-limit was set to.

The part the stream needs next is now written straight through to the
backend as it arrives, so only parts which arrive ahead of their turn
are buffered, and those always within the limit - a part bigger than
the limit waits until it is the next part rather than being admitted
when the buffer is empty. Further requests for the part being streamed
wait, holding no memory, and are then checked against it.

Since what reaches the backend can't be taken back, a part whose body
fails part way records how far it got, and the client's retry of the
part, which must have the same content, carries on from there. Such an
upload can't be completed without the rest of the part.

The body is read to its end, so that an error it only reports there -
as the Content-MD5 check does - fails the part rather than being
missed, and a body longer than declared is rejected. A part rejected
once it has been streamed leaves data which can be neither completed
nor replaced, so the whole upload is failed and cleaned up and the
client starts again.

(cherry picked from commit 7773bb6487)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood a7f0259e55 serve s3: time out multipart parts waiting for buffer space
A part waiting for room in the out-of-order buffer waited forever,
ignoring the request being cancelled, and a waiting part stopped its
upload being expired, so a client could hold requests, goroutines and
uploads open indefinitely.

The wait now ends when the request is cancelled, and after a minute the
part fails with SlowDown so the client retries it later.

(cherry picked from commit 603e6238eb)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 323b0b4559 serve s3: don't buffer re-uploads of multipart parts already streamed
A part uploaded again after it had been streamed to the backend (a
client retrying after a timeout) was buffered in memory in full just to
compare it with the original, and the buffer limit didn't apply to it.

Such a re-upload is now hashed as it is received and compared with the
original without being buffered.

(cherry picked from commit 122b4e8df2)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 26175553a7 serve s3: count buffered multipart parts in whole memory pages
The out-of-order multipart buffer was charged the declared length of
each part, but a buffered part occupies whole 1 MiB pool pages however
small it is. A client could therefore send up to 10000 one-byte parts
ahead of the part the stream needed and pin about 10 GiB of memory in a
single upload while --multipart-streaming-buffer-limit saw only 10 KB.

Each buffered part is now charged the whole pages it occupies.

(cherry picked from commit 4aa3c41aa1)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 4a944dff0f webdav: fix headers option leaking to other hosts on redirect GHSA-3rqf-7h69-gg5v CVE-PENDING
The headers set with the `headers` option were sent to any host the
server redirected to, as net/http only strips Authorization and Cookie
on a cross domain redirect and copies every other header onto each
hop. A server which could be made to redirect to another host would
therefore receive any credential carried in a custom header such as
X-Auth-Token.

Strip the configured headers from a redirect hop once the chain has
left the configured host (a different hostname, subdomain or port)
using rest.StripHeadersOnCrossHostRedirectFn. This logs at debug level
when it happens so a 401 from the redirect target can be diagnosed.
The `auth_redirect` option is unchanged as it is an explicit opt-in to
keep credentials across redirects.

The Authorization header set from `user`, `pass` and `bearer_token` is
deliberately left to net/http's policy, which strips it on a change of
domain but keeps it for a subdomain or a different port, as servers
which redirect to a subdomain rely on that today.

(cherry picked from commit 224a515d68)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood e9b9b48a68 docs: fix release verification instructions trusting unsigned hashes
The manual release verification steps checked the signature with gpg
--verify and then checked the binaries against the whole SHA256SUMS
file. gpg only verifies the clearsigned part, so an unsigned hash line
added outside it would be used by sha256sum -c or by rclone hashsum -C.

The instructions now extract the signed hashes with gpg --decrypt and
check the binaries against those only.

See: GHSA-w33h-3qgq-8mv8
(cherry picked from commit 204adfeedc)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 3a14789eb4 selfupdate: fix hash check trusting unsigned data in the signed SHA256SUMS GHSA-w33h-3qgq-8mv8 CVE-PENDING
rclone selfupdate checked the PGP signature on the clearsigned block
of SHA256SUMS but then looked up the archive hash in the whole
downloaded file. Unsigned data can appear in that file without
invalidating the signature. A hash line placed before or after the
signed text was accepted, allowing whoever controlled the download to
install an arbitrary unsigned archive.

The hash is now only looked up in the signed text.

Affects stable (non --beta) selfupdate since v1.55.0. --beta updates
are not signature checked.

(cherry picked from commit 672bbf75ec)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 47027efd12 fshttp: drop --header values over HTTP after HTTPS downgrade GHSA-rrwf-2qr6-p832 CVE-PENDING
The transport strips the headers set with --header once a redirect
chain leaves the originally requested host, but the host comparison
ignores the URL scheme. A server which redirects https://host:8443/
to http://host:8443/ keeps the same host and port, so the headers
were added to the plaintext request and could be read by anyone on
the network path.

The redirect chain walk now also treats a hop which downgrades the
original HTTPS request to HTTP as leaving the host, so the headers are
removed from that hop and every one after it.

The transport also logs at debug level when it removes the headers,
for either reason, so that a resulting 401 from the redirect target
can be diagnosed.

(cherry picked from commit bc1050bc8a)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 2f43b99abe vfs: fix serve nfs path traversal by refusing unsafe file names GHSA-44pm-2q5p-fm63 CVE-PENDING
Dir.Create, Dir.Mkdir and Dir.Rename joined whatever name they were
given onto the directory's path. A name of "..", "." or one containing
a "/" isn't a child of the directory, and at the VFS root
path.Join("", "..") preserves the ".." so the new node's path pointed
outside the root. Backends which join the Fs root with the remote then
resolved it to a location above the directory the VFS was created on.

serve nfs was exploitable through this. The go-nfs library refuses a
name of exactly "." or ".." in MKDIR only, doesn't check the name in
CREATE, RENAME or SYMLINK, and never refuses a name containing a "/".

serve webdav was exploitable in the same way and has its own fix. The
other serve front-ends rely on their own or their protocol library's
path cleaning to keep such names out of the VFS.

Reject such names with EINVAL in the VFS itself, so that every
front-end and mount is protected regardless of what the layer above it
checks. This is the fix for serve nfs and defence in depth for the
others. The serve webdav check is kept as the front-end is the trust
boundary and gives the client a clear error. The source name of a
rename is only used to look the node up so it isn't checked.

VFS.Mkdir of the root, which previously went through Dir.Mkdir with an
empty name and added a nameless child node to the root, now makes the
root directory on the backend directly, still honouring --read-only.
serve webdav and serve ftp made directories through Dir.Mkdir rather
than VFS.Mkdir, so they are switched to VFS.Mkdir to keep "MKCOL /" and
"MKD /" working.

(cherry picked from commit 3739ad9c35)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 133ced103a serve webdav: fix path traversal above the served directory GHSA-44pm-2q5p-fm63 CVE-PENDING
A request path with a ".." element, such as "MKCOL /..", was passed
straight through to the VFS. At the VFS root path.Join("", "..")
preserves the "..", so a directory node with the path ".." was added
under the root and every path built beneath it started "../". Backends
which join the Fs root with the remote before encoding it - such as
webdav, ftp, sftp, http and memory - resolved that "../" away, letting
an unauthenticated client create, overwrite, copy, move and delete
objects and directories outside the directory rclone was told to serve.
Moving a directory or file from outside the served tree into it also
made it readable.

Reject any request with a "." or ".." element in the request path, or
in the Destination header of COPY and MOVE, with 400 Bad Request. The
check is made once in ServeHTTP rather than in each webdav.FileSystem
method so that the client gets a consistent error. Rejected requests
are logged at INFO level like other requests.

The webdav handler strips --baseurl from the Destination as a string
prefix, so "/base../x" reaches the FileSystem as "../x". The Destination
is therefore checked after stripping the prefix in the same way.

(cherry picked from commit c266a63ced)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 317c885857 netstorage: stop a listing resume token moving signed requests to another host GHSA-xqmq-v4wj-rw5m CVE-PENDING
The resume start value returned by the NetStorage list API is joined
onto the endpoint URL as a URL reference to build the next listing
request. A value beginning with `//` is a network-path reference, so a
server (or an on-path attacker when `protocol = http`) which returns
`//other.example/path` could make rclone send the next request, signed
with the account secret, to a host other than the configured one.

Use rest.URLJoinRoot so the resume token can only change the path.

(cherry picked from commit c8dfd98b0c)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood a34f1f153a netstorage: stop a path starting with // moving the remote to another host GHSA-xqmq-v4wj-rw5m CVE-PENDING
The netstorage backend joins the path after `remote:` onto the
configured host URL according to the URL standard. A path beginning
with `//` is a network-path reference under that standard, so a path
such as `//other.example/dir/` replaced the configured host (and port
and userinfo) rather than being treated as a path. Every request,
including the initial stat, was then signed with the account secret and
sent to the substituted host. The signature covers the request path and
action but not the host, so the substituted host received signatures
valid for replay against the real NetStorage endpoint for the duration
of their validity.

Use rest.URLJoinRoot, which refuses any root that changes the scheme,
host or userinfo of the configured URL.

(cherry picked from commit 4f28f4492c)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood e7d4d425bc http: stop a path starting with // moving the remote to another host GHSA-xqmq-v4wj-rw5m CVE-PENDING
The http backend resolves the path after `remote:` against the
configured url according to the URL standard. A path beginning with
`//` is a network-path reference under that standard, so a path such as
`//other.example/dir/` replaced the configured host (and port and
userinfo) rather than being treated as a path. The configured headers
were then sent to the substituted host on the initial HEAD request and
every request after it.

Use rest.URLJoinRoot, which refuses any root carrying a scheme, host
or userinfo of its own, to join the path onto the configured url.

(cherry picked from commit ecd821243f)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 01a9d67e06 fstests: check a root starting with // can't move the remote to another host GHSA-xqmq-v4wj-rw5m CVE-PENDING
Add an integration test which starts a listener on localhost, makes
the remote with roots naming that listener and checks that nothing
connects to it. The listener replies with junk so a backend which does
connect fails at once rather than retrying a closed connection.

(cherry picked from commit f33fa3872f)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood aece53963a rest: add URLJoinRoot to join a path onto a URL without changing its host GHSA-xqmq-v4wj-rw5m CVE-PENDING
rest.URLJoin resolves its path as a URL reference according to the URL
standard, so a path beginning with `//` is a network-path reference
which replaces the host (and port and userinfo) of the base URL. That
is often the wrong behaviour.

Add rest.URLJoinRoot which joins an unescaped root onto a base URL and
refuses any root which parses with a scheme, host or userinfo of its own.

(cherry picked from commit 28562c8a79)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 291016ea75 lib/http: require authentication for OPTIONS requests GHSA-gj73-fh6v-92fj CVE-PENDING
Before this change the basic, htpasswd and auth proxy authentication
middlewares let every OPTIONS request through without checking
credentials so that browser CORS preflights, which can't carry
credentials, would succeed.

However in serve webdav the OPTIONS request is answered by the WebDAV
handler whose Allow header depends on whether the path is an existing
file, an existing directory or doesn't exist, so an unauthenticated
client could discover the existence and type of any path on the served
remote even with --user/--pass or --htpasswd set.

After this change he authentication middlewares no longer special-case
OPTIONS. Instead the CORS middleware, which runs before authentication
and owns the --allow-origin setting, answers genuine CORS preflights
itself with an empty 200 carrying the CORS headers only when
--allow-origin is set.

All other OPTIONS requests need credentials like any other method, so
clients which probe with OPTIONS before authenticating will get a 401
challenge and must retry with credentials.

(cherry picked from commit 25707f2dc6)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 7e57142de1 pcloud: only accept pCloud hosts from the hostname in the OAuth callback
When pCloud redirects the browser back to rclone's local OAuth
callback server it adds a `hostname` parameter saying which regional
API host (api.pcloud.com or eapi.pcloud.com) the account lives on.
rclone used this for the token exchange and saved it in the config as
the host for all of the remote's later traffic, without checking it.

This only accepts a well formed hostname within pCloud's own domain,
lower cased, failing the config with an error otherwise, and adds a
unit test for the check. This is defence in depth for GHSA-hvpr-p4pv-4f46

(cherry picked from commit 3760671b36)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 722c14d806 pcloud: fix OAuth config accepting callbacks without state GHSA-hvpr-p4pv-4f46 CVE-PENDING
The local web server which receives the OAuth callback during
`rclone config` and `rclone authorize` checks that the `state`
parameter matches the random value rclone generated when starting the
flow. The pcloud backend opted out of this check with the
`StateBlankOK` option in lib/oauthutil, which made the local server
accept a callback with no state at all.

The callback URL (http://127.0.0.1:53682/) is fixed and well known,
so while a pcloud OAuth flow was in progress any web page open in the
user's browser could complete it with a single cross-site request
carrying an attacker's authorization code. This is a CSRF
(CWE-352) on the auth callback.

This commit removes the `StateBlankOK` option so that every callback
must carry the correct state and adds a test that the local auth
server rejects callbacks with a blank, missing or wrong state.

The option was added in May 2020 (#4210) because pCloud briefly
omitted the state parameter from its redirect. The callbacks recorded
five weeks later when EU region support was added show both
api.pcloud.com and eapi.pcloud.com returning `state=` again, so the
exemption has been unnecessary since then.

See: GHSA-hvpr-p4pv-4f46
(cherry picked from commit a53fdac5ec)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 89b8a59e8b vfs: stop --links objects called "...rclonelink" appearing as ".." in directory listings
When reading a directory with --links the VFS skipped entries called "."
and ".." before removing the ".rclonelink" suffix, so an object called
"...rclonelink" (or "..rclonelink" or ".rclonelink") produced a directory
entry called ".." (or "." or "") whose path resolved to the parent
directory. The same happened for names added back from the VFS cache.

The check is now done after the suffix is removed.

See GHSA-55qp-jrwr-x827

(cherry picked from commit 93ba2d40f0)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 1b500594d3 local: fix .rclonelink names escaping the root with --links GHSA-55qp-jrwr-x827 CVE-PENDING
In --links mode the local backend checked that a source name stayed inside
the destination root and only then removed the ".rclonelink" suffix from the
resulting OS path. A source entry called "...rclonelink" passed the check and
was then trimmed to "..", so the object addressed the parent of the
destination the user chose.

Writing the content of such an entry was already refused, as it is done
through an os.Root, but metadata was not: with --metadata rclone rewrote the
timestamps, ownership and extended attributes of the directory above the
destination from values supplied by the source.

The suffix is now removed from the remote name before it is joined to the
root, so the containment check applies to the name that is actually used. A
translated link must also leave a real native file name once the suffix is
removed.

A regular file whose name is exactly ".rclonelink" (or "..rclonelink" or
"...rclonelink") is not a valid translated link and is now skipped in
directory listings.

(cherry picked from commit 4d1bd4c71d)
2026-10-09 00:01:35 +01:00
Dhevenddra 9b495f2717 test: skip the symlink tests when the platform won't allow symlinks
Nine tests fail on an ordinary Windows machine, eight in backend/local and
TestEnvironmentVariables in cmdtest, all with

    symlink file.txt \?\C:\Users\...\symlink.txt: A required privilege is not
    held by the client.

Windows grants SeCreateSymbolicLinkPrivilege only to an elevated process or one
running with Developer Mode enabled, and a default install gives an ordinary
user neither. CI does not see this because the windows-latest runner is
elevated, so the failures only show up on a contributor's own machine, where
AGENTS.md asks for make quicktest to pass before opening a pull request.

cmdtest already recognised the situation and attached a note to the failure
saying the test could safely be ignored. If it is safe to ignore then the test
knows it cannot run, so skip it and say why instead.

backend/local gains a helper that tries a symlink in t.TempDir() and skips if it
cannot make one, called from the six tests that need the privilege. Where a
platform can create symlinks the probe succeeds and nothing is skipped, so other
platforms are unchanged.

TestMetadata is skipped whole because it creates its symlink before anything
else and the object built from it is used throughout.
TestSymlinkEscapeConcurrent is left alone: it goes through putLink and ignores
the error, so it never needed the privilege.

(cherry picked from commit 5fc1cc3ca1)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood f6290108df local: fix directories named *.rclonelink being treated as links with --links
In --links mode the local backend removed the ".rclonelink" suffix from
directory names as well as file names when building the OS path. Only files
are ever translated into symlinks, so a directory called "sub.rclonelink"
addressed the sibling "sub" instead: listing it logged "Failed to read link
size", its metadata was read from the wrong place, and setting metadata on
it with --metadata failed or landed on the sibling.

Directories now use their name as is, whatever suffix it has, in the same
way DirSetModTime already does.

(cherry picked from commit a7ef4c9064)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood ed9f1d836e b2: fix account token and SSE-C key leaking on redirects from --b2-download-url GHSA-gpw7-77cq-9f72 CVE-PENDING
Every file download sends the live account authorization token and,
when configured, the SSE-C customer key headers to the download host.
With --b2-download-url that host is a CDN or proxy the user does not
operate, and the http client followed its redirects with Go's default
policy:

- Go only strips Authorization when the redirect target is not the same
  host or a subdomain, and never looks at the scheme, so a same-host
  HTTPS to HTTP redirect replayed the full-capability B2 token in
  cleartext.
- Go has no idea that the X-Bz-Server-Side-Encryption-Customer-* headers
  carry the customer encryption key, so it forwarded them to any
  redirect target including a different host.

The B2 client now uses rest.StripHeadersOnCrossHostRedirectFn, so it
refuses to follow a redirect which downgrades HTTPS to HTTP and strips
the Authorization and SSE-C headers from any redirect which leaves the
host and port of the original request, matching the protection already
given to the S3, WebDAV and HTTP backends.

Fixes GHSA-gpw7-77cq-9f72

(cherry picked from commit 30009599e4)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 59ea591bcb rest: add StripHeadersOnCrossHostRedirectFn and use it in the s3 and http backends
The s3 and http backends each carried their own copy of the same
CheckRedirect policy. This moves it into lib/rest as
StripHeadersOnCrossHostRedirectFn, so that any backend which sends
credentials in headers to a host it does not control can use the same
policy. It logs at debug level when it strips headers so that a
resulting 401 from the redirect target can be diagnosed. A header
starting with "*" is matched without canonicalising, as that is how
SetHeader and Opts.ExtraHeaders send it.

This changes the s3 policy in two edge cases:

- It compared the raw URL host string, so a redirect to the same host
  in different case or with an explicit default port stripped the
  credentials. It now uses SameHost like the http backend, which treats
  those as the same host.
- The downgrade check compared each hop with the previous one. It now
  compares with the original request like the other backends, so a
  chain which starts on plaintext HTTP may return to it.

The behaviour tests move to lib/rest. The backend tests which check the
policy is wired into each client are kept.

(cherry picked from commit cae10713e8)
2026-10-09 00:01:35 +01:00
Nick Craig-Wood 2cc9088374 build: fix multiple CVEs by upgrading to go1.26.9
- CVE-2026-97032: net/http: HTTP/2 server crash due to HPACK encoder race
- CVE-2026-78659: net/http: HTTP/2 server memory exhaustion due to Trailer headers
- CVE-2026-97031: crypto/tls: reject malformed ECH outer extension references
- CVE-2026-94444: cmd/go: checksum bypass for golang.org/fips140
- CVE-2026-94447: cmd/go: checksum database bypass for golang.org/toolchain
- CVE-2026-94448: html/template: reset context tracking on consecutive template expressions
- CVE-2026-97030: html/template: recognize |yield| as regexp preceder keyword
- CVE-2026-94440: net/textproto, mime/multipart: memory limit bypass when parsing MIME headers
- CVE-2026-56866: net/http: HTTP/1 client connection desynchronization after CONNECT rejection
- CVE-2026-94439: net/http: HTTP/1 server connection desynchronization after 2xx CONNECT response
- CVE-2026-78669: net/http: excessive CPU consumption from repeated initial window changes
- CVE-2026-78660: net/http: HTTP/2 transport accepts malformed framing-related headers
- CVE-2026-56857: os: Root.Mkdir(All) can follow junctions out of the root on Windows
- CVE-2026-78667: net/http: lack of limit on size of parsed Range headers
- CVE-2026-78663: net/http: double flow control refund on HTTP/2 server streams

This also updates the go1.26 test job to go1.26.9.
2026-10-09 00:01:35 +01:00
Nick Craig-Wood d8dbeb9de4 operations: remove --copy-dest --immutable test for single file copy
TestCopyFileCopyDestImmutable relies on --immutable being enforced for
copyto, moveto and single file copies, which v1.75 doesn't do, so it
failed wherever the remote supports server-side copy, such as the
local backend on macOS.

The --copy-dest --immutable fix it was picked with still applies to
sync and copy and is tested by TestSyncCopyDestImmutable.
2026-10-09 00:01:30 +01:00
James Truitt 9cf1af177a docs: fix formatting error in Google Drive docs
(cherry picked from commit 2debe0ae6a)
2026-10-08 17:17:50 +01:00
kaii9 bb42b076af serve restic: fix repositories named data - fixes #6086
(cherry picked from commit 9823661bb8)
2026-10-08 17:17:50 +01:00
kaii9 0992024791 accounting: keep speed averaging active when transfers overlap
(cherry picked from commit aa308e3d7c)
2026-10-08 17:17:50 +01:00
somaz 48ac01c2b8 mount: fix --daemon failing when --rc is enabled - fixes #9196
Before this change the parent process kept the remote control and
metrics servers it started in initConfig listening, so the daemon child
could not bind the same addresses and exited.

After this change the parent shuts those servers down just before
daemonizing, and the child starts them as usual.

(cherry picked from commit 4532a88838)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood b928e8ddd0 serve: fix --auth-proxy backends never being shut down after simultaneous logins
With --auth-proxy, when several logins with the same credentials
arrived together and none was in the cache yet, as when an FTP client
opens several connections at once, each one ran the auth proxy program
and took its own reference to the backend. Only one of them was
remembered, so the others were never given back and the backend was
never shut down.

Logins which arrive together now share one run of the auth proxy
program and one reference to the backend.

(cherry picked from commit 2e9c06c84d)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood 9ac7855da9 serve: fix --auth-proxy stopping VFS cache uploads after 5 minutes
With --auth-proxy the backend of each user is shut down once it has
been unused for 5 minutes. With --vfs-cache-mode writes or full, files
are uploaded to the backend after the transfer which wrote them has
finished, which didn't count as a use. So an upload which hadn't
finished 5 minutes after the user's last command (or disconnection for
serve sftp) was stopped, leaving the file in the VFS cache but not on
the backend.

The backend is now kept until it has no files open for write and
nothing in the VFS cache waiting to be uploaded.

This was introduced in v1.75.1 by

f425f8d46 serve: refactor VFS and proxy handling into Provider

(cherry picked from commit 0caf24af50)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood 5d20bd2832 vfs: add Busy to tell if a VFS still has data to write
Busy returns true if the VFS has files open for write or files in the
VFS cache which are open or waiting to be uploaded. It is for code
which wants to shut a VFS down only once that wouldn't lose any work.

(cherry picked from commit 4fa2a5b02c)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood 2f84013eec lib/cache: add SetCanExpire to keep entries which are still busy
An entry is expired when it hasn't been used for a while, but the user
of the cache may know it is still doing something in the background.
SetCanExpire sets a function which is asked before expiring an entry
and can keep it in the cache until it is ready to go.

The function is called without the cache locked as it may take a
while, so an entry which is used in the meantime isn't expired.

(cherry picked from commit 49d8566ec5)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood 675f9c3684 serve webdav: fix transfers failing after 5 minutes with --auth-proxy
With --auth-proxy the backend of each user is shut down once it has
been unused for 5 minutes. Only the start of each request counted as a
use, so an upload or download lasting longer than that had its backend
shut down under it and failed.

Each request now holds the backend it uses until it has been served.

This was introduced in v1.75.1 by

f425f8d46 serve: refactor VFS and proxy handling into Provider

(cherry picked from commit 7e6d6fa838)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood b8a697888c serve http: fix downloads failing after 5 minutes with --auth-proxy
With --auth-proxy the backend of each user is shut down once it has
been unused for 5 minutes. Only the start of each request counted as a
use, so a download lasting longer than that had its backend shut down
under it and failed.

Each request now holds the backend it uses until it has been served.

This was introduced in v1.75.1 by

f425f8d46 serve: refactor VFS and proxy handling into Provider

(cherry picked from commit 8457da3c8a)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood dddf808b47 serve ftp: fix transfers failing after 5 minutes with --auth-proxy
With --auth-proxy the backend of each user is shut down once it has
been unused for 5 minutes. Only the start of each FTP command counted
as a use, so an upload or download lasting longer than that had its
backend shut down under it and failed with "context canceled".

Each FTP command now holds the backend it uses until it has finished,
which for a download is when the file is closed.

This was introduced in v1.75.1 by

f425f8d46 serve: refactor VFS and proxy handling into Provider

(cherry picked from commit ee0a70f875)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood 451346f01c sftp: fix failed uploads leaving disk space in use on the server - fixes #10033
When an upload failed during the transfer, the partial file was
removed but its handle was never closed. The server kept the deleted
file open, so its space stayed allocated until the pooled connection
was closed, which in rcd, mount or serve could be until rclone exited.

This closes the handle before removing the partial file.

(cherry picked from commit d545615c16)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood 6fadaa46aa filescom: document that names with / or \ in can't be stored and ignore their tests
Files.com now treats the replacement characters rclone uses for / and
\ in file names (/ and \) as if they were the originals, and the
names . and .. as if they were . and .., and rejects them all with
"Invalid path". There is no other way of encoding these names, so
document the restriction and ignore the integration tests for them.

(cherry picked from commit ed92e4446e)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood 563919b0c0 webdav: fix "XML syntax error" on paths which don't exist with ownCloud 10.16
ownCloud 10.16 answers a PROPFIND for a path which doesn't exist with
a 207 Multi-Status response, rather than a 404, with a body which
starts a multistatus document then appends a Sabre NotFound error to
it. This isn't valid XML so rclone failed with

    read metadata failed: XML syntax error on line 2: expected attribute name in element

whenever it was pointed at a directory which didn't exist yet.

Detect the NotFound error in the response and treat it as a 404.

(cherry picked from commit f217c8a2aa)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood 1ecdeb4466 chunker: fix panic listing a directory after an interrupted upload with meta_format none
With meta_format = none, listing a directory which contained the
temporary chunks of an interrupted upload, but no completed chunks for
that file, panicked with "invalid chunked object".

The listing made a placeholder object for the file on seeing a
temporary chunk, which then had no chunks in it when it was validated.
Only make the placeholder on seeing a data chunk, so files which have
nothing but temporary chunks are ignored as they are when metadata is
in use.

(cherry picked from commit 475997b6de)
2026-10-08 17:17:50 +01:00
Nick Craig-Wood b85a681721 azureblob: use the released Azure SDK for Apache Arrow listing
The Blob Listing with Apache Arrow feature is now public and shipped
in azblob v1.8.1, so the temporary backend/azureblob/arrowlist package
which implemented it on top of the previous SDK has been removed and
the backend now uses the SDK's own listing pager with ResponseFormat
set to Arrow.

As the SDK client handles every credential type this also makes Arrow
and parallel listing work with connection_string auth, and the
use_arrow_list and list_parallelism options are no longer hidden.

(cherry picked from commit 3b9f0a05b5)
2026-10-08 17:17:49 +01:00
GhostCoder6969 61b8a90db8 docs: fix typo in speed test help text
Co-authored-by: bounty-agent <bounty@example.com>
(cherry picked from commit 7ebef790b4)
2026-10-08 17:17:12 +01:00
Nick Craig-Wood 419c5f9844 archive: fix a squashfs path to a single file listing its whole directory
Pointing the archive backend at a file inside a squashfs image, for
example `rclone cat :archive:image.sqfs/dir/file.txt`, listed every
file in the directory containing it instead of just that file. The zip
archiver already behaved correctly.

The squashfs archiver now remembers the file the root points at and
only exposes that one, as zip does. Its Root() includes the file so
that the fs cache does not hand back the Fs for the whole directory in
its place.

(cherry picked from commit 67afe55a83)
2026-10-08 17:17:12 +01:00
Neil Cawse 50d2430b4a vfs: stop setting a file's modtime from discarding its cached data
Setting the modtime of a closed file refreshed the cache item's
fingerprint before the new modtime had been applied to the remote
object. The next open then saw a fingerprint mismatch, logged "removed
cache file as stale (remote is different)" and downloaded the whole
file again, even though only the modtime had changed.

Refresh the cache fingerprint again once the remote modtime has been
set.

(cherry picked from commit 9380daed0d)
2026-10-08 17:17:12 +01:00
Neil Cawse cd6941ed93 vfs: fix reads returning zeros after a file changes during handle caching
When a file was reopened within the --vfs-handle-caching grace period
(default 5s) after its remote fingerprint changed - for example after a
touch or any other modtime change - _checkObject removed the stale
cache file and open recreated an empty one, but nothing sized it. The
next GetSize stat'd the empty file and set the item size to 0, so
ReadAt's _ensure clamped the request to nothing and skipped the
download, then the size check zero-extended the file and returned the
zeros. Reads through a mount returned the correct length but zeros for
the first read (128 KiB through FUSE).

This is easy to hit with git on a --vfs-cache-mode full mount: git
freshens pack files with utime, and a concurrent reader then sees
"not a GIT packfile".

Size the recreated cache file from the object before opening it, as a
normal open does.

(cherry picked from commit e0fb01084d)
2026-10-08 17:17:11 +01:00
TastyHeadphones 8290a478c8 oauthutil: fix Renew.Shutdown nil timer panic and race
Shutdown was calling expiryTimer.Stop without holding ts.mu, so it could
panic if OnExpiry had not created the timer yet, and raced the write in
OnExpiry. Hold the lock and skip Stop when the timer is still nil.

Fixes #9980

(cherry picked from commit 80e462d48c)
2026-10-08 17:17:11 +01:00
Rohit Behera d89f30a136 docs: fix the expanded --bwlimit timetable example
It was missing a space between two entries, so it didn't parse.

(cherry picked from commit e497464b60)
2026-10-08 17:17:11 +01:00
Rohit Behera f8899ca982 fs: fix --bwlimit timetables written out of order using the wrong limit
Until the first time slot of the week, the timetable used its last entry
as the limit carried over from the week before. That is only the latest
slot of the week when the entries are in order, so a timetable written
weekend first, like "Sat-00:00,off Mon-00:00,1M", limited Sunday to 1M
instead of leaving it unlimited.

Carry over the latest time slot of the week instead.

(cherry picked from commit 124f3bd830)
2026-10-08 17:17:11 +01:00
Dirk Petersen d77c1bf5c6 s3: fix storage class missing from --s3-versions listings
When listing with --s3-versions or --s3-version-at the storage class
of each object was dropped, so it read as STANDARD unless the object's
metadata was fetched separately. This meant backend restore skipped
objects in GLACIER or DEEP_ARCHIVE with "Not GLACIER or DEEP_ARCHIVE
or INTELLIGENT_TIERING storage class", and lsf --format T showed the
wrong tier.

This happened because ObjectVersion.StorageClass has a different type
from Object.StorageClass so the generated setFrom helper does not copy
it. Convert it explicitly after the setFrom call.

(cherry picked from commit e5eaf414f0)
2026-10-08 17:17:11 +01:00
hsdfat 19bf14aea4 docker: fix files uploaded with the wrong mime type - fixes #6384
The Docker image had no /etc/mime.types, so MIME types came only from
Go's built-in table plus rclone's small extra list, and many
extensions uploaded as application/octet-stream.

This installs Alpine's mailcap package to fix the problem.

(cherry picked from commit 3cdf855547)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 353167fd38 selfupdate: fix --version X.Y depending on ./ in the download site links
To find the latest patch release of a minor version, for example
`rclone selfupdate --version 1.75`, selfupdate searched the listing of
downloads.rclone.org for href="./vX.Y.Z/". The leading ./ is a detail
of how Caddy's file server writes its links. A listing which linked to
the same directories as "vX.Y.Z/", which is an equally valid relative
URL, made selfupdate fail with "could not find the minor release".

This makes the ./ optional in the pattern, so selfupdate no longer
depends on which program wrote the listing, and takes the version from
a capture group rather than from fixed offsets into the match.

The listings written by rclone index now include the ./ for the
benefit of rclone versions without this fix, so this is to remove the
dependency for the future.

(cherry picked from commit 8cd43746a3)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 09e1967c73 serve s3: fix TestEtagHashAuto on Windows
The temporary directory contains a drive letter colon so it needs
quoting in the crypt connection string, otherwise the password
parameter is dropped.

(cherry picked from commit 9dc8b71ae9)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 1d1eb94c34 build: fix lint errors from golangci-lint v2.14.0
The newer revive flags an exported function returning an unexported
type and a redundant type in a var declaration.

(cherry picked from commit 881cedd348)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood fd3b9bd383 build: fix TestS3Minio by switching to pgsty/minio as quay.io image is gone
MinIO no longer publishes images on quay.io (nor Docker Hub or ghcr.io)
so pulling quay.io/minio/minio returns "unauthorized". pgsty/minio is a
maintained community fork with the same entrypoint layout.

(cherry picked from commit 220c65f81d)
2026-10-08 17:17:11 +01:00
nielash 6676f2673e fs: retry "network is unreachable" and "network is down" errors
ENETUNREACH and ENETDOWN were not in the list of retriable errors on
non-Windows platforms, so a single failed connection aborted the
transfer instead of being retried as a low level retry. The Windows
list already includes the equivalent WSAENETUNREACH and WSAENETDOWN.

This is easy to hit on a host with IPv6 enabled but no IPv6 route: if
the A lookup fails transiently while the AAAA lookup succeeds, the only
address to dial is IPv6 and the connect fails with ENETUNREACH. A retry
resolves again and normally succeeds.

(cherry picked from commit 94e319d2fa)
2026-10-08 17:17:11 +01:00
Roland f6af51420f fs: fix bandwidth limits below 1 KiB being multiplied by 1024 - fixes #9958
BwPair.String printed a sub-KiB bandwidth as a bare number, and Set
reads a bare number as KiB, so a rate under 1 KiB grew by a factor of
1024 whenever it went through a string. rc core/bwlimit reports the
current rate in that format and accepts it back, so reading the limit
and setting it again raised it.

63c4fef27 fixed the same corruption for config values by suffixing bare
numbers with B inside Option.String. Move that into a SizeSuffix method
and use it from BwPair.String as well.

(cherry picked from commit 60bdc6d454)
2026-10-08 17:17:11 +01:00
Harsh Raj Singhania a755455609 serve webdav: escape filename in zip download Content-Disposition header
Use mime.FormatMediaType instead of unescaped string concatenation so a
directory name containing a double quote cannot inject extra disposition
parameters.

Fixes #9962

(cherry picked from commit 14a1359c96)
2026-10-08 17:17:11 +01:00
Harsh Raj Singhania 0e6ed13131 serve http: escape filename in zip download Content-Disposition header
Use mime.FormatMediaType instead of unescaped string concatenation so a
directory name containing a double quote cannot inject extra disposition
parameters.

Fixes #9962

(cherry picked from commit 661484f36c)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 96d2633836 serve s3: fix CopyObject of a missing object not returning NoSuchKey
Copying an object onto itself, as clients do to replace its metadata,
reported success when the object didn't exist, and copying a missing
object to a different key failed with an internal error. Both now fail
with NoSuchKey as S3 does.

(cherry picked from commit ce5351c52e)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood f625ac98a6 serve s3: fix --etag-hash auto crashing or using the wrong hash with --auth-proxy
With --etag-hash auto the hash for the ETags was chosen once from the
remote serve s3 was started with. With --auth-proxy there is no such
remote so serve s3 crashed on startup, and when started by the rc it
was the wrong remote, so users whose backends lacked that hash got no
ETags.

The hash is now chosen from the backend of the user making each
request.

(cherry picked from commit 746eac73ef)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 19068b6608 serve s3: forget the metadata of deleted objects
The metadata of every object uploaded was kept in memory for as long as
the server ran, even after the object was deleted, so it grew without
limit and the metadata of a deleted object reappeared on any object
later created at the same key other than through serve s3.

Deleting an object now forgets its metadata.

(cherry picked from commit c350f8ebbf)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood ee40507d38 serve s3: list objects lazily so paging a deep hierarchy is fast - fixes #9855
Listing a prefix with no delimiter walked the entire subtree below it into
memory and only then sliced out the requested page, so every page of a
listing cost a full traversal of the tree.

Walk the tree lazily instead, stopping as soon as the page is full and
skipping the subtrees an earlier page already returned. A page now costs a
number of directory reads proportional to the keys it returns rather than to
the size of the subtree, and ETags are computed only for the objects that
are actually returned.

Entries are emitted in the order their keys have in a flat keyspace, with a
directory sorting as if it carried its trailing slash, so that "a.txt" comes
before "a/b" as it does in a real S3 bucket. Without this a resumed listing
would silently skip keys across a page boundary.

(cherry picked from commit 04697cc02a)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood b4cb0426a3 vfs: fix a VFS being reused while it is being shut down
When the last user of a VFS shut it down at the same time as a new
user of the same remote asked for one, the new user could be given the
VFS being shut down, with its cache and background tasks stopped.

Only a VFS which is still in use is now reused, otherwise a new one is
made.

(cherry picked from commit c62aa2adc9)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 5977137b3b vfs: add Hold to keep a VFS from being shut down while in use
A VFS is shut down when the last of the references to it from New is
given back with Shutdown. Hold takes another reference, for code using
a VFS it didn't create itself, which may otherwise be shut down under
it.

(cherry picked from commit 1dab0f3abd)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 512f6193a2 operations: fix TestMultithreadCopy leaving files behind when multipart upload is skipped
When a backend rejected the multipart upload as too small, the subtest
skipped without removing its local source file, so the following
upload subtests failed their local listing checks.

(cherry picked from commit ee9c228012)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 46cf167c22 internxt: fix server-side directory move failing after a gateway timeout
Moving a directory can take longer than the API gateway allows, so the
request fails with a 520 or 502 error even though the move completes.
The retry then failed with "Folder ... was already moved to that
location (status 409)".

Treat that error as success.

(cherry picked from commit 0c52b183d0)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 2d24951658 internxt: fix "directory not empty" and stale directories after moves and deletes
The Internxt API serves listings from read replicas which lag behind
writes, so for a short while after files or directories are moved or
deleted they can still be listed in their old location.

This caused removing a directory which had just been emptied to fail
with "directory not empty" (eg when moving a directory without server
side directory moves or purging a directory) and a directory which had
just been moved to be found in its old location.

Remember the directories this process has moved or deleted and ignore
directory and file entries which contradict that when listing, finding
directories and checking a directory is empty before removing it.

(cherry picked from commit 7ff5da8517)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood ad9ad335f4 internxt: fix server-side moves failing with "Not Found" or "already exists"
Moving a file into a directory which had just been created failed with
"Not Found (status 404)", and moving a file over one which had just been
deleted (as sync does with --backup-dir and --suffix) failed with "A file
with the same name already exists in destination folder (status 409)".

The API checks moves against read replicas which lag behind writes, so
retry these errors until the replicas catch up.

(cherry picked from commit ee26daecd8)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 7d3e148212 internxt: fix sync with --backup-dir or --suffix deleting the backed up file
The Internxt API serves lookups and listings from read replicas which
lag behind writes, so for a short while after a file is moved it can
still be returned from its old location.

When sync moved a file into the backup location and then uploaded its
replacement, the upload could find the moved file under its old name
and overwrite it. Overwriting renames the existing file by UUID and
deletes it once the upload succeeds, so the file that had just been
moved into the backup location was deleted.

Remember the files this process has moved or deleted for a minute and
ignore lookups and listing entries which contradict that.

(cherry picked from commit f068abd607)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 2ff47e8274 drime: fix deleted files and directories still being listed with hard_delete
With hard_delete set, deleted files and directories carried on being
listed for about a second afterwards because the Drime server doesn't
invalidate its cache of the parent folder listing when entries are
deleted forever. This made removing a directory straight after
emptying it fail with "directory not empty".

Moving an entry to the trash does invalidate the cache, so with
hard_delete set rclone now moves the entry to the trash first and then
deletes it forever.

(cherry picked from commit a2e2b73725)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood f3bec5b1aa drime: fix server-side copy over an existing file leaving a "name (1)" copy
When server-side copying to a destination which already existed, the
Drime server gave the copy the name "name (1)" and rclone only renamed
it if the source and destination leaf names differed. The existing file
was then deleted leaving the copy under the wrong name.

The server refuses to rename an entry to a name which is already in
use, so this removes the existing file straight after the copy, then
renames the copy whenever its name differs from the destination name.

(cherry picked from commit eb10c48a17)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood d418a5a882 premiumizeme: fix uploading files with ";" in their names
The premiumize.me upload server has started truncating the multipart
file name at the first ";", so uploading "a;b.txt" created a file
called "a" and the upload then failed with "object not found".

Directory creation and renames still accept ";", so files whose names
contain ";" are now uploaded under a temporary name and renamed into
place. The encoding is left unchanged so existing files and
directories containing ";" remain accessible.

(cherry picked from commit 7c18e1eb86)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood db96e30c5e vfs: fix TestDirMetadataExtension on remotes which can't upload empty files
(cherry picked from commit 034ac6275f)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 111fda1287 onedrive: document that shared with me shortcuts may fail to list on personal
Shortcuts to shared items (and Personal Vault) are stored as remote
items pointing at another drive and listing them can fail with
"The provided drive id appears to be malformed". This aborts a
--fast-list listing of the whole drive.

(cherry picked from commit 8c38d3068a)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 10d999e3ee rest: limit the size of HTTP response bodies read into memory
rest.ReadBody read the whole response body into memory with no limit.
It is used by the default error handler and by many backends' error
handlers and small API calls, so a server which answered with an error
status and then streamed an endless body could make rclone allocate
memory until it was killed.

ReadBody now reads at most 10 MiB (the same limit drainAndClose
already uses to discard unread bodies) and returns an error if the
body is bigger than that. Every caller reads small API responses -
error bodies, status documents and upload tokens - so no legitimate
response is affected.

Reported by @manus-pi

(cherry picked from commit 90e67915c8)
2026-10-08 17:17:11 +01:00
jxj 6a7dcf12c3 s3: fix version-at listings with URL encoded keys
When S3 returns URL-encoded keys from ListObjectVersions, URL encoding
can change their lexical order. This could make mergeDeleteMarkers
place a delete marker after older versions of the same key, so
--s3-version-at reported deleted objects as live.

Compare decoded keys while merging, while preserving the encoded keys
for the existing listing decode path.

Fixes #9948

(cherry picked from commit cfb90e3ebe)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood fecd1d578a onedrive: update docs for versions, links and time precision on personal accounts
Testing against OneDrive personal (free) and OneDrive for Business shows

- personal accounts now create versions on setting the modification
  time and can delete them, so --onedrive-no-versions and rclone
  cleanup work there
- --onedrive-link-password works on OneDrive for Business
- personal free accounts can't set a link password or --expire
- --onedrive-link-type embed only works on OneDrive personal
- personal accounts store times with 1s precision, not mS

See #9917

(cherry picked from commit ff02636fe4)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood af92ea943d vfs: fix crash reading the metadata of a file which is being written
With --vfs-metadata-extension set, looking up the metadata file of a
file which was open for write and not yet uploaded caused a nil pointer
panic. Such a file has no object to read the modification time from.

Use the modification time of the VFS node instead, which is valid
whether or not the file has been uploaded.

(cherry picked from commit 7a2d7c766d)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood b46bb7647b operations: fix TestDeleteFatalError, TestDirMoveMoveError and TestDirMoveContext on remotes
TestDeleteFatalError set --max-delete before writing its files, so on
chunker, which deletes while uploading, the setup failed.

TestDirMoveMoveError and TestDirMoveContext test the core DirMove logic
with a wrapping Fs. This fails on remotes without Move (eg s3, memory)
and on those whose objects don't belong to the wrapped Fs (eg archive),
so they now only run on local.

(cherry picked from commit ff958c999f)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 3f6b38c9d9 iclouddrive: fix potential crash looking up items
findItem read the response status code when the lookup failed, so a
failure with no HTTP response panicked.

Thanks to @manus-pi for finding this problem.

(cherry picked from commit 1e92520076)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood a1fba2e8c1 shade: fix potential crash in directory move
DirMove discarded the error from the destination check and read the
response status code, so a failure with no HTTP response panicked.
Other errors were reported as the destination existing; they are now
returned.

Thanks to @manus-pi for finding this problem.

(cherry picked from commit 35abcadfc7)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood dca3902fef imagekit: fix potential crash in rmdir and purge
Rmdir and Purge read the response status code before checking for an
error, so a failed DeleteFolder call with no HTTP response (a network
error, or purging the root which fails validation) panicked.

Thanks to @manus-pi for finding this problem.

(cherry picked from commit e2cd9a5dfb)
2026-10-08 17:17:11 +01:00
phatlc 06387f878a serve dlna: log unescaped paths - fixes #7370
The request log printed the escaped URL, so non-ASCII file names showed
up as long runs of %XX escapes. Log the unescaped URL path instead, as
the other serve commands do.

(cherry picked from commit 556940ea44)
2026-10-08 17:17:11 +01:00
phatlc 13ae25b561 vfs: fix AddVirtual ignoring isDir
VFS.AddVirtual always added a file entry to the directory cache, even
when called with isDir set, so a virtual directory showed up as a file
and nothing could be added inside it.

See #9310

(cherry picked from commit ebb1cc1221)
2026-10-08 17:17:11 +01:00
phatlc 5ea1851d52 smb: save the user name in the config even if it matches the current user - fixes #9356
The default for --smb-user was the name of the user running rclone
config, and rclone does not write defaults to the config file, so
entering your own user name left it out. A remote made that way then
logged in as whoever ran it later, e.g. root under systemd.

Make the default blank and look up the current user when the remote is
used, as the ftp backend does. Existing configs work as before.

(cherry picked from commit 3c51b96774)
2026-10-08 17:17:11 +01:00
phatlc 2b65691945 operations: stop --copy-dest replacing files with --immutable
When the source matched a file in --copy-dest, copyDest server-side
copied it over an existing destination which differed, so sync, copy
and copyto with --copy-dest could modify a file --immutable should have
protected.

Leave such a destination for the caller, which rejects it with
ErrorImmutableModified as for any other modified file.

(cherry picked from commit dc98c216f2)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 5de8a01717 rc: reject out of range integer parameters instead of truncating them
This was spotted by CodeQL after this change was merged:

976d05e1d rc: fix rc API accepting an out of range number and overflowing 64 bits

(cherry picked from commit 7723091be9)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 4d50f6d878 docs: remove broken SecureBuild links
(cherry picked from commit 1dc2516f08)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 34ad19a1eb box, serve s3: fix log messages with bad format strings
The box backend logged an int64 with %q which printed
%!q(int64=123) instead of the sequence ID.

serve s3 passed the message from gofakes3 as the format string, so
any % in it was interpreted as a formatting directive and the message
was mangled.

(cherry picked from commit 1c3e432c3f)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 5bc26e57b4 rc: make job/status and job/list require authentication
job/status returns the complete output of the job so it is as
sensitive as the call which started the job, e.g. config/dump started
with _async returns the config file including any secrets. It was
possible to read this without authentication where an authenticated
and an unauthenticated rc server share the same process, e.g. rclone
gui --rc.

An unauthenticated client can't start jobs on a server which needs
authentication, other than with the calls which don't need it, so
this shouldn't affect existing users.

(cherry picked from commit 939f82d908)
2026-10-08 17:17:11 +01:00
Nick Craig-Wood 8d15b2bb75 log: fix race when adding a log output while logging
Handle read the list of extra outputs without holding the mutex, so
calling AddOutput while logging caused a data race.

This reads the extra outputs once under the mutex.

(cherry picked from commit 4cd6359ae9)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood c062ec1b12 jobs: fix tests failing and racing when run with -count > 1
TestRcJobList listed the jobs left running in the global job list by
the previous run of the tests, so reset the global job list first.

(cherry picked from commit dd07bb33ba)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood 7b647c729b operations: don't sleep for a Retry-After error when the transfer is cancelled
The wait to obey a Retry-After error from the server used
time.Sleep() so it carried on sleeping when the context was
cancelled, e.g. by job/stop or --max-duration, and it also slept
after the last try when there was nothing left to retry.

Copying a file also logged the low level retry number counting from
0 rather than 1 as everything else does.

(cherry picked from commit 88bd49fab8)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood 4347404119 operations: fix hang and lost context when moving a directory file by file
When the backend has no DirMove method, operations.DirMove moves the
objects one by one. If one of the moves failed the movers stopped
without receiving the rest of the objects, so DirMove blocked forever
once the channel filled up. This could be seen renaming a directory on
a mount of a backend without DirMove, such as s3.

The moves were also done with context.Background() instead of the
caller's context, so they didn't have the caller's config, couldn't
be cancelled and weren't counted in the caller's stats.

(cherry picked from commit b82a024de0)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood 366b188a5a operations: fix hang when deleting files and a fatal error occurs
When a fatal error such as --max-delete being reached stopped the
deletions, the deleters returned without receiving the rest of the
objects, so whatever was sending them blocked forever once the
channel filled up.

For example "rclone delete --max-delete 1" on a directory with more
files than --checkers hung instead of returning the error.

The deleters now keep receiving objects without deleting them after a
fatal error.

(cherry picked from commit caa341878f)
2026-10-08 17:17:10 +01:00
Jeremy Schoemaker 0dd95c2840 rc: fix large numeric parameters being rejected on 32 bit builds
Params.GetInt64 parsed string parameters with strconv.ParseInt(x, 10, 0).
A bitSize of 0 means int, which is 32 bits on 386, arm, mips and mipsle,
so any value outside the int32 range was rejected as out of range even
though it fits in the int64 the method returns.

Parameters reach the rc API as strings on the paths that matter here: the
rc server puts every URL query and form value into Params as a string, and
rclone rc turns each key=value argument into a string. So on a 32 bit build
operations/getfile could not be given an offset, count, head or tail beyond
2 GiB, and debug/set-soft-memory-limit could not be given a limit beyond
2 GiB, while the same commands worked on a 64 bit build.

Parse with a bitSize of 64 to match the declared return type. This is the
string half of the range checking that was added to the float64 branch of
the same function in 976d05e1.

(cherry picked from commit b5f83bdbf2)
2026-10-08 17:17:10 +01:00
Wang Chencheng efba10c065 operations: fix ignored error in rcat probe reads
Return the first non-EOF input error encountered while Rcat probes whether an
upload is small. Previously that error was ignored and the full probe buffer,
including bytes that were never read, could be passed to Put or PutStream.

(cherry picked from commit b93b73bcb0)
2026-10-08 17:17:10 +01:00
Vasek Sraier cc53a4cdac serve restic: prevent concurrent append-only overwrites
- Reject uploads when object lookup fails for reasons other than "not found".
- Serialize uploads to the same object to prevent races between the
  existence check and the write.

(cherry picked from commit d632f8bb51)
2026-10-08 17:17:10 +01:00
Acts1631 89c6cb0911 compress: fix crash on ranged reads when gzip metadata is corrupted
Gzip metadata is read from the wrapped remote and could contain an
invalid block size or incomplete block index. A range read could then
panic in the seekable gzip reader.

Validate the gzip sidecar invariants before constructing a reader so
malformed remote metadata returns an error instead of crashing rclone.

(cherry picked from commit c8d60a67fc)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood a5f9653807 fstest: remove the TestS3MinioEdge test server
The minio/minio:edge image it ran has gone from Docker Hub and there
is no equivalent tag on quay.io, so this test remote can no longer be
started.

(cherry picked from commit a401763cf6)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood 620ce60d00 fstest: make test server start fail fast when docker run fails
The start function was run inside an if condition, where bash ignores
errexit, so a failed docker run was not noticed. The script then
printed its connection details anyway and the test spent 100 seconds
trying to connect before failing with a message that hid the real
error.

Run start in a subshell with errexit on and check its status
explicitly so the failure is reported immediately with docker's
error message.

(cherry picked from commit 4cf1da0d4f)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood fa75e2b178 build: fix TestS3Minio by pulling minio from quay.io as it has gone from Docker Hub
The minio/minio repository has been removed from Docker Hub so the
TestS3Minio test server could not be started and every Linux CI run
failed. quay.io/minio/minio still serves the final release so use
that instead.

(cherry picked from commit e0d846f29e)
2026-10-08 17:17:10 +01:00
KBS b5055efecf rc: fix rc API accepting an out of range number and overflowing 64 bits
float64(math.MaxInt64) rounds up to 2^63, so x > math.MaxInt64 in
GetInt64 lets 2^63 through to int64(x), which is out of range.

(cherry picked from commit 976d05e1dd)
2026-10-08 17:17:10 +01:00
foecmke ab8a00e159 docs: update --onedrive-hard-delete to mention personal account support
(cherry picked from commit b1a10fe17e)
2026-10-08 17:17:10 +01:00
Kunpeng Xie 70700de356 accounting: stop averaging when the last check finishes
Release the averaging goroutine when checks outlast transfers, with a regression
test for the completion order.

Assisted-by: OpenAI Codex
(cherry picked from commit 4f0148db0c)
2026-10-08 17:17:10 +01:00
tomaszni 277d3ea146 oracleobjectstorage: upload empty streams without multipart
OCI rejects a multipart completion request with no parts. Probe unknown-size
streams through a buffered reader and use a regular upload when the stream is
empty. Peek preserves a non-empty stream for the selected upload path.

(cherry picked from commit e85836d4c7)
2026-10-08 17:17:10 +01:00
Eugene fc65a1b9cb cmount: ignore com.apple.* xattrs on the macOS FSKit backend
(cherry picked from commit 0cb6ddada9)
2026-10-08 17:17:10 +01:00
jzunigax2 b1677718cd internxt: fix lookups of files starting with a dot and dropped uploads
Internxt stores a file as a (plainName, type) pair and never derives the
split itself, so it is a convention shared between clients. This backend
split at the final dot, storing and looking up ".bashrc" as an empty name
of type "bashrc", where the web, desktop, Linux and macOS clients all keep
the leading dot in plainName. List rebuilt the full name so such files
appeared, but NewObject looked them up by the split and missed them.

(cherry picked from commit 77ec281072)
2026-10-08 17:17:10 +01:00
jzunigax2 4dde74364b refactor: streamline file existence checks and metadata retrieval
- Replaced the preUploadCheck function with findFile for better clarity and efficiency in checking file existence.
- Introduced splitNameExt to encapsulate name and extension parsing logic.
- Updated NewObject and Update methods to utilize findFile for improved file metadata handling.
- Enhanced error handling and reduced redundant code in file checks.

(cherry picked from commit e441773d24)
2026-10-08 17:17:10 +01:00
n4n5 bb7b312b19 docs: extend librclone instructions for gomobile
(cherry picked from commit 8fafc08dd1)
2026-10-08 17:17:10 +01:00
Vladimir Babin 43162d20e0 archive: fix crash when creating archive to stdout - fixes #9910
When rclone archive create is run without a destination the archive
should be written to stdout, but ArchiveCreate called
CheckValidDestination on the nil dst and panicked with a nil pointer
dereference. Skip the destination check when there is no destination.

Also remove a leftover debug Printf that wrote to stdout before the
archive data.

Signed-off-by: Vladimir Babin <vovababin@gmail.com>
(cherry picked from commit 3342e34f58)
2026-10-08 17:17:10 +01:00
maximilize ba3a6c17ce docs: clarify --password-command quoting for a path with spaces
(cherry picked from commit 0e19ed565f)
2026-10-08 17:17:10 +01:00
enkvadrat 7c1a79e071 docs: add padding to footer card
This is mostly visible in dark mode, as a side effect off adding the class,
the background of the card also changed to be dark-gray.

(cherry picked from commit c2a5884ad9)
2026-10-08 17:17:10 +01:00
ZRHann 0d582bfffe webdav: fix duplicated listing entries after retried PROPFIND
(cherry picked from commit 812e693fd7)
2026-10-08 17:17:10 +01:00
shaurya 459501873b docs: fix broken --check-filename self-link in bisync docs
The link pointed at a bare relative path (--check-filename) instead of
the in-page anchor for the ### --check-filename heading further down the
page, so it 404s on the rendered docs site. Point it at #check-filename
(Hugo strips leading dashes when generating header anchors).

---------

Co-authored-by: no-hup <19599684+no-hup@users.noreply.github.com>
(cherry picked from commit c3ba184611)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood f1590144a4 docs: update sponsors
(cherry picked from commit 7b3a4e5144)
2026-10-08 17:17:10 +01:00
subomi 9d25ce63a3 docs: lshelp: explain that a directory on a bucket-based remote is a key prefix
Listing a bucket without -R shows each prefix as one directory entry, so the
objects under it are not in the output. This is easy to read as a truncated
listing rather than as one level of a hierarchy.

Fixes #9797

(cherry picked from commit 8c32435bec)
2026-10-08 17:17:10 +01:00
dependabot[bot] 18c39dc283 build: update google.golang.org/grpc to 1.85.0-dev.0.20260825072537-93e31b48545e to fix CVE-2026-84445
A vulnerability exists in gRPC-Go servers configured with
xds.NewGRPCServer() where a crafted request missing both :authority
and Host headers can cause a server panic, resulting in a Denial of
Service (DoS).

This update fixes the problem.

(cherry picked from commit ef6968730f)
2026-10-08 17:17:10 +01:00
youdie006 79ccf3d26b fs: fix about showing a negative total when a quota reaches the int64 maximum
NewUsageValue exists to clip an oversized quota to the maximum value of an
int64, which is what dc95f36bc added it for when Box raised the Enterprise
space_amount to 1e+18 and started returning it as a float.

For the float64 instantiation the guard misses its own boundary.
float64(math.MaxInt64) is not 2**63-1, it rounds up to 2**63, so a quota of
exactly 2**63 fails the comparison and falls through to the int64 conversion,
which the spec leaves implementation dependent for an unrepresentable value.
On linux/amd64 it wraps:

    Before: rclone about -> Total=-9223372036854775808
    After:  rclone about -> Total=9223372036854775807

A negative total is not just a wrong number. vfs.Statfs documents -1 as "not
known", vfs.fillInMissingSizes branches on total < 0, and serve sftp only
computes its usage percentage when total > 0, so the value is read back as a
missing quota.

The int64 and uint64 instantiations are unaffected, since for them
T(int64(math.MaxInt64)) is exact and clipping MaxInt64 to MaxInt64 is a no-op.

(cherry picked from commit 52ac7e0e18)
2026-10-08 17:17:10 +01:00
youdie006 d659d10afd fs: make BwTimetable.Set replace the timetable instead of appending to it
Set built the timetable with *x = append(*x, ts), so setting a bandwidth
timetable on a value that already held one kept both schedules. The single-value
branch of the same function has always done *x = BwTimetable{ts}, and the other
multi-token Set methods in this package build into a local and assign at the end.

The visible effect is through the rc API. The "main" options block registered in
fs.RegisterGlobalOptions is the live globalConfig, and options/set reshapes JSON
straight into it, so

  rclone rc options/set --json '{"main": {"BwLimit": "Mon-10:00,1Mi"}}'

added to the running daemon's timetable rather than replacing it, and the older
slot kept winning: LimitAt for a Sunday returned the previous 10Mi. The same
applies to a _config override on a single call, since AddConfig shallow-copies
the global.

Building into a local also stops a failed parse from leaving the previous
timetable partly overwritten, which the existing error cases already expect.

(cherry picked from commit 5bbc5d5545)
2026-10-08 17:17:10 +01:00
Aditya b8431d4f50 s3: disable signing Accept-Encoding for Ceph and Linode - fixes #8206
Ceph RGW (and Linode Object Storage, which is Ceph-backed) can break
SigV4 when Accept-Encoding is included in the signature, especially
when a reverse proxy rewrites that header. GCS already sets this quirk;
apply the same default for Ceph and Linode as suggested in #8206.

(cherry picked from commit ea589de941)
2026-10-08 17:17:10 +01:00
tomaszni 6c566ca014 oracleobjectstorage: fix SSE-C server-side copies
Set the OCI source SSE-C request headers when using a customer key.
Server-side copies need these headers to decrypt the source object, in
addition to the existing headers that encrypt the destination.

(cherry picked from commit 3eee2c0dd2)
2026-10-08 17:17:10 +01:00
Murat Topcu cd7cef72db selfupdate: fix TestInstallOnLinux panicking when the build is the latest beta
The test asks InstallUpdate to install the latest beta into an
unwritable file and expects an error. When the binary under test
reports exactly the latest beta version (as make quicktest does right
after a beta is published from the same commit), InstallUpdate
correctly decides there is nothing to do and returns nil, and the test
then dereferences the nil error and panics.

Pin fs.Version to a fixed old value for the duration of the test so an
update is always attempted, and use require.Error so a missing error
fails the test instead of crashing it.

(cherry picked from commit 89fc14059e)
2026-10-08 17:17:10 +01:00
phatlc 33ee8c0ef8 serve docker: fix volume path being lost when the plugin restarts
applyOptions consumes the "path" option into vol.Path rather than leaving
it in vol.Options, but restoreState rebuilt the options with only fs and
type. The explicit path was therefore dropped when the plugin restarted,
and since fsString is rebuilt from those options the volume was remounted
at the root of the remote instead of at its subpath.

Before this change a volume created with type + path lost its path
completely, and one created with remote + path silently fell back to the
path of the connection string. With a backend whose credentials are
scoped to the subpath the restored mount then failed every operation
rather than serving the wrong directory.

Feed the persisted path back like fs and type, so applyOptions applies
the same precedence on restore that it applies when the volume is
first created.

Fixes #9853

(cherry picked from commit 9ac29e3b35)
2026-10-08 17:17:10 +01:00
phatlc bb0633ee43 dropbox: match shared-folder and received-file names case-insensitively - fixes #9706
The Dropbox backend advertises CaseInsensitive: true, but the two
shared-mode lookup helpers compared names with an exact, case-sensitive
==, so a shared folder or received file named "Project" could not be
found when requested as "project". Use strings.EqualFold in both
findSharedFolder and findSharedFile to honour the advertised
case-insensitivity.

Fixes #9706

(cherry picked from commit b549554c31)
2026-10-08 17:17:10 +01:00
phatlc 347738843f dropbox: fix shared folder mount for roots nested more than one level deep
In shared_folders mode NewFs derived the shared folder name with
path.Dir(f.root), which returns the parent path rather than the first
path component. For a root like "SharedFolder/subdir/deeper" this yielded
"SharedFolder/subdir", which findSharedFolder cannot match, so NewFs
failed with ErrorDirNotFound. Use the first path component of the root,
as the shared_folders option documents, so deeply nested roots mount.

Fixes #9705

(cherry picked from commit ac7cfcc848)
2026-10-08 17:17:10 +01:00
ferrumclaudepilgrim 4d547c4b7b vfs/vfscache: fix hang when the cache cleaner is disabled
KickCleaner sets the out of space flag, kicks the cleaner and then waits for
that flag to clear. Only the cleaner clears it, and the cleaner returns
immediately when the cache poll interval is not positive, so when it is
disabled nothing ever reads the kick or clears the flag and the caller waits
forever.

It now returns straight away in that case, under the same condition the
cleaner itself uses to decide it is disabled. Callers already retry a bounded
number of times and then report the error, which is the right outcome when
nothing is going to free space.

(cherry picked from commit e724790620)
2026-10-08 17:17:10 +01:00
ferrumclaudepilgrim ab95b42328 fserrors: fix out of space detection on Windows - fixes #8011
IsErrNoSpace compared against syscall.ENOSPC. Go defines that constant on
Windows as a value in its application reserved range which no Windows API
returns, so the comparison could never be true there. A full disk on Windows
reports ERROR_DISK_FULL or ERROR_HANDLE_DISK_FULL instead.

Preallocation failures were still caught, because those return a separate
sentinel, but a disk that is already full fails at the directory creation or
at the open long before preallocation is reached. That is the case reported.

The errors are now held in a list which platform specific files add to in
their init, which is the shape retriable_errors already uses in this package,
and the comparison itself is unchanged. Windows appends the two codes that
lib/file already recognises when preallocation fails. Every other platform
keeps exactly the behaviour it had.

This also reaches the VFS cache, which uses the same helper and has no
preallocation path of its own, so its out of space handling has been inert
on Windows.

(cherry picked from commit ca41db095b)
2026-10-08 17:17:10 +01:00
Sanjay Kanth A 409ba3b1f4 docs: drive: document Branding step needed to publish own client_id
Google now requires an app homepage URL and privacy policy URL to be
set on the OAuth consent screen's "Branding" page before the "PUBLISH
APP" button becomes clickable, even for a personal single-user app.
The existing instructions jumped straight to publishing in step 9
without mentioning this, leaving the button greyed out with no
explanation of why.

Fixes #9854

(cherry picked from commit c875d89033)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood 274342326c dedupe: fix rename mode giving up after 100 names and make it faster - fixes #9860
Before this change `rclone dedupe --dedupe-mode rename` probed the
backend for each candidate `name-N.ext` in turn and gave up when it
had tried 100 names for a given object. With daily runs against the
same duplicated filename this ceiling was eventually reached and
rclone logged "Could not find an available new name". Each probe was
also a backend lookup, so a run against 99 existing names took
minutes on Google Drive.

The rename now uses the listing dedupe has already made to skip names
known to be taken without asking the backend, and only confirms the
final candidate with NewObject (the listing may be incomplete because
of filters). The suffix counter is shared between the objects being
renamed so no name is checked twice. The safety limit is raised to
10000 which, thanks to the listing, no longer costs a lookup per name.

(cherry picked from commit bc4a208e7e)
2026-10-08 17:17:10 +01:00
Nick Craig-Wood 97eee1fecf serve ftp: fix VFS leak when the server fails to start
The deferred cleanup in the constructor checked a local error variable
rather than the error being returned, so failures after the VFS was
created (such as an invalid --passive-port) never shut it down. Name
the error return so the cleanup sees the returned error.

(cherry picked from commit 9b9fd3f493)
2026-10-08 17:17:09 +01:00
Hakan İSMAİL 98274d294a serve, mountlib: test VFS release on shutdown and mount failure
(cherry picked from commit 30e79a017b - without the cmd/mountlib test as the mountlib fix isn't in this branch)
2026-10-08 17:17:09 +01:00
Nick Craig-Wood 34aeb94efa serve webdav,http: fix crash when the server fails to start - fixes #9882
When the HTTP server failed to initialise, for example because the
listen address was already in use, rclone panicked with a nil pointer
dereference instead of reporting the error.

The deferred cleanup in the constructor read the provider from the
named return value, but `return nil, err` sets that to nil before the
deferred function runs. Use a local variable for the server instead.

(cherry picked from commit f2a390b2d4)
2026-10-08 17:17:09 +01:00
Dhevenddra K G de09fecf1c docs: fix duplicated words in vfs and backend documentation
(cherry picked from commit 3d7b101c7f)
2026-10-08 17:17:09 +01:00
Nick Craig-Wood b7b89b0708 serve docker: fix tests leaving unkillable processes and stale FUSE mounts
Writing to the mount with os.WriteFile made the Go runtime register
the file with its poller so the kernel then polled the file from
epoll_ctl and epoll_wait, sending POLL requests to the FUSE server
running in this same process. A thread waiting inside epoll cannot be
preempted by the runtime, so a garbage collection starting while such
a POLL was outstanding stopped the world for good - the test binary
could not be killed even with SIGKILL and the mount was left behind,
wedging anything that touched it.

Now we write through the mount with a descriptor straight from
open(2), which os.NewFile keeps out of the poller, check that it
really is out of the poller with SetDeadline, and check at the end of
the test that the mountpoint is unmounted.

In this commit we fixed the same problem for mount by running in a
subprocess however changing one write file routine here was much
easier than re-arranging the tests.

4a382c09ec mount: run tests in a subprocess to fix deadlock - #3259

Note that go-fuse (and hence mount2) works around this problem it by
forcing an early POLL it can answer with ENOSYS.

See: https://github.com/golang/go/issues/21014
(cherry picked from commit e855d2ed36)
2026-10-08 17:17:09 +01:00
Nick Craig-Wood 4079b6f493 archive: fix listing entries with a leading slash as if they were in the root
The check that an entry returned by an archiver is a direct child of
the directory being listed normalised a parent of "/" to the root, so
an entry named "/x" passed as a child of the root while "x/" and
"dir//x" were rejected.

Decide by stripping the directory prefix and checking what is left
with sanitize.Leaf, which rejects an empty name, ".", ".." and any
name containing a "/". This also covers the leading slash case.

(cherry picked from commit b88e237e8c)
2026-10-08 17:17:09 +01:00
Nick Craig-Wood bc482e79ff archive: fix zip file entries named for a directory causing confusion
A file entry in a zip whose name refers to a directory, such as ".",
"/", "" or "sub/.", was only skipped when it named the root of an
archive which was itself the root of the remote. When the archive was
found by listing its parent directory the entry appeared as a file
with the same name as the archive alongside the directory for it, and
copying the archive tried to write both. When the entry named a
subdirectory it appeared as a file alongside that directory, and with
that subdirectory mounted as the archive root the entry was taken to
be the single file the root points at, hiding every real entry.

Skip any file entry whose last path component is "", "." or "..",
checked on the raw name before it is cleaned or joined on the prefix.

(cherry picked from commit da352a2a1b)
2026-10-08 17:17:09 +01:00
Nick Craig-Wood 7ef04886b9 archive: fix corrupt listings when listing a zip directory more than once
The zip archiver handed out its cached directory tree directly. Any
caller which filters a listing in place (as the core listing code
does) altered the cache, so later listings of the same directory could
be corrupted.

Return a copy of the cached listing instead.

(cherry picked from commit 68eab60564)
2026-10-08 17:17:09 +01:00
Nick Craig-Wood b70ebaba7b seafile: fix corrupted uploads after a retried upload error
When an upload failed with a 500 error the upload was retried with a
new upload link but the same input stream. The stream had already been
consumed by the first attempt so the retry uploaded an empty file.

This fixes it by returning a RetryError instead so the caller retries
the upload with a fresh stream, which will fetch a new upload link.

(cherry picked from commit 6cdd0ea761)
2026-10-08 17:17:09 +01:00
Nick Craig-Wood b03a9f8ea3 filescom: fix corrupted uploads after a retried upload error
When an upload failed with a retryable error the pacer retried the
whole upload with the same input stream. The stream had already been
consumed by the first attempt so the retry uploaded an empty file.

This fixes it by using CallNoRetry for the upload, as the other
backends do, so retryable errors are returned wrapped in a RetryError
for the caller to retry the upload with a fresh stream.

It also makes 5xx errors from the upload storage servers retryable.
These come back from the SDK as a different error type to API errors
so were not being retried at all.

(cherry picked from commit fa43f10af2)
2026-10-08 17:17:09 +01:00
Nick Craig-Wood 8a2bed2f71 pixeldrain: fix corrupted uploads after a retried upload error
When an upload failed with a retryable error the pacer retried the
whole PUT with the same input stream. The stream had already been
consumed by the first attempt so the retry uploaded an empty file.

This fixes it by using CallNoRetry for the upload, as the other
backends do, so retryable errors are returned wrapped in a RetryError
for the caller to retry the upload with a fresh stream.

(cherry picked from commit f4cd80a535)
2026-10-08 17:17:09 +01:00
Shane McCarron df2a2d1127 fs/fshttp: fix TestCertificates leaking client cert/key onto global config
This was fixed in this commit in an inelegant way

399bc6a6a6 fshttp: don't send --header values to other hosts on redirect

The current commit fixes it properly with AddConfig.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
(cherry picked from commit 1b4dea8dac)
2026-10-08 17:17:09 +01:00
PSR94 2e18447255 docs: update Huawei Drive client ID setup
(cherry picked from commit b4c598dbe4)
2026-10-08 17:17:09 +01:00
Nick Craig-Wood ef6bae24c5 Start v1.75.2-DEV development 2026-09-04 18:21:52 +01:00
230 changed files with 13472 additions and 5216 deletions

No files matched your search

+9 -9
View File
@@ -37,7 +37,7 @@ jobs:
include:
- job_name: linux
os: ubuntu-latest
go: '~1.26.6'
go: '~1.26.9'
gotags: cmount
build_flags: '-include "^linux/"'
check: true
@@ -48,14 +48,14 @@ jobs:
- job_name: linux_386
os: ubuntu-latest
go: '~1.26.6'
go: '~1.26.9'
goarch: 386
gotags: cmount
quicktest: true
- job_name: mac_amd64
os: macos-latest
go: '~1.26.6'
go: '~1.26.9'
gotags: 'cmount'
build_flags: '-include "^darwin/amd64" -cgo'
quicktest: true
@@ -64,14 +64,14 @@ jobs:
- job_name: mac_arm64
os: macos-latest
go: '~1.26.6'
go: '~1.26.9'
gotags: 'cmount'
build_flags: '-include "^darwin/arm64" -cgo -macos-arch arm64 -cgo-cflags=-I/usr/local/include -cgo-ldflags=-L/usr/local/lib'
deploy: true
- job_name: windows
os: windows-latest
go: '~1.26.6'
go: '~1.26.9'
gotags: cmount
cgo: '0'
build_flags: '-include "^windows/"'
@@ -81,14 +81,14 @@ jobs:
- job_name: other_os
os: ubuntu-latest
go: '~1.26.6'
go: '~1.26.9'
build_flags: '-exclude "^(windows/|darwin/|linux/)"'
compile_all: true
deploy: true
- job_name: go1.26
os: ubuntu-latest
go: '~1.26.6'
go: '~1.26.9'
quicktest: true
racequicktest: true
@@ -284,7 +284,7 @@ jobs:
id: setup-go
uses: actions/setup-go@v7
with:
go-version: '~1.26.6'
go-version: '~1.26.9'
check-latest: true
cache: false
@@ -399,7 +399,7 @@ jobs:
id: setup-go
uses: actions/setup-go@v7
with:
go-version: '~1.26.6'
go-version: '~1.26.9'
# Caching is handled explicitly below to share the module cache
# with the other jobs - see the build job for the rationale.
cache: false
+1
View File
@@ -39,6 +39,7 @@ RUN echo "**** Install Dependencies ****" && \
apk add --no-cache \
ca-certificates \
fuse3 \
mailcap \
tzdata && \
echo "Enable user_allow_other in fuse" && \
echo "user_allow_other" >> /etc/fuse.conf
Generated
+1813 -1041
View File
File diff suppressed because it is too large. Load diff
Generated
+655 -124
View File
File diff suppressed because it is too large. Load diff
Generated
+795 -150
View File
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -1 +1 @@
v1.75.1
v1.75.2
+8 -7
View File
@@ -36,6 +36,7 @@ import (
"github.com/rclone/rclone/fs/config/configstruct"
"github.com/rclone/rclone/fs/fspath"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/lib/sanitize"
)
// Register with Fs
@@ -529,14 +530,14 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
// isDirectChild reports whether remote names an entry directly in dir
// ("" being the root), with no ".." or other components in between.
func isDirectChild(dir, remote string) bool {
if remote == "" || remote == "." || remote == ".." || strings.HasSuffix(remote, "/") {
return false
if dir != "" {
var ok bool
remote, ok = strings.CutPrefix(remote, dir+"/")
if !ok {
return false
}
}
parent := path.Dir(remote)
if parent == "." || parent == "/" {
parent = ""
}
return parent == dir && path.Clean(remote) == remote
return sanitize.Leaf(remote) == nil
}
// NewObject creates a new remote archive file object
+36
View File
@@ -175,6 +175,18 @@ func testArchive(t *testing.T, archiveName string, archiveFn func(t *testing.T,
checkTree(ctx, "SubDir", t, ":archive:"+zipFile+"/"+subDir, filepath.Join(input, subDir), 0)
// Now check a single file
//
// The filter below applies to both sides, so check first that the
// archive exposes only that file without it
Fsingle, err := cache.Get(ctx, ":archive:"+zipFile+"/"+aFile)
require.NoError(t, err)
entries, err := Fsingle.List(ctx, "")
require.NoError(t, err)
require.Len(t, entries, 1)
_, err = Fsingle.List(ctx, subDir)
assert.Equal(t, fs.ErrorDirNotFound, err)
_, err = Fsingle.NewObject(ctx, subDir)
assert.Equal(t, fs.ErrorObjectNotFound, err)
fiCtx, fi := filter.AddConfig(ctx)
require.NoError(t, fi.AddRule("+ "+aFile))
require.NoError(t, fi.AddRule("- *"))
@@ -379,3 +391,27 @@ func TestArchiveEscapingArchiver(t *testing.T) {
_, err = f.NewObject(ctx, "test.escaping/file.txt")
assert.ErrorIs(t, err, fs.ErrorObjectNotFound)
}
// TestIsDirectChild checks the guard which decides whether an entry
// returned by an archiver belongs directly in the directory listed.
func TestIsDirectChild(t *testing.T) {
for _, test := range []struct {
dir, remote string
want bool
}{
{"", "a.txt", true},
{"", "/a.txt", false},
{"", "a.txt/", false},
{"", "../a.txt", false},
{"", "sub/a.txt", false},
{"d", "d/a.txt", true},
{"d", "d", false},
{"d", "d/", false},
{"d", "d//a.txt", false},
{"d", "d/../a.txt", false},
{"d", "dd/a.txt", false},
{"d", "a.txt", false},
} {
assert.Equal(t, test.want, isDirectChild(test.dir, test.remote), "dir=%q remote=%q", test.dir, test.remote)
}
}
+17 -1
View File
@@ -42,6 +42,7 @@ type Fs struct {
prefix string // position for objects
prefixSlash string // position for objects with a slash on
root string // position to read from within the archive
file string // remote of the single file root points at, if any
}
// recoverParsePanic converts a panic raised while parsing a squashfs image
@@ -115,10 +116,13 @@ func New(ctx context.Context, wrappedFs fs.Fs, remote, prefix, root string) (_ f
_, err := f.newObjectNative(native)
if err == nil {
// If it pointed to a file, find the directory above
// and remember the file so that only it is listed
f.root = path.Dir(f.root)
if f.root == "." || f.root == "/" {
f.root = ""
}
f.file = f.prefixSlash + path.Base(native)
singleObject = true
}
}
}
@@ -150,7 +154,9 @@ func (f *Fs) Name() string {
// Root of the remote (as passed into NewFs)
func (f *Fs) Root() string {
return f.root
// Include the file so the fs cache can tell this Fs apart from
// the one for the directory containing it
return path.Join(f.root, f.file)
}
// Features returns the optional features of this Fs
@@ -227,6 +233,10 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
defer log.Trace(f, "dir=%q", dir)("entries=%v, err=%v", &entries, &err)
defer recoverParsePanic(&err)
if f.file != "" && dir != "" {
return nil, fs.ErrorDirNotFound
}
nativeDir, err := f.toNative(dir)
if err != nil {
return nil, err
@@ -265,6 +275,9 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
fs.Debugf(item.Name(), "FIXME Not regular file - skipping")
continue
}
if f.file != "" && entry.Remote() != f.file {
continue
}
entries = append(entries, entry)
}
@@ -316,6 +329,9 @@ func (f *Fs) newObjectNative(nativePath string) (o fs.Object, err error) {
func (f *Fs) NewObject(ctx context.Context, remote string) (o fs.Object, err error) {
defer log.Trace(f, "remote=%q", remote)("obj=%v, err=%v", &o, &err)
if f.file != "" && remote != f.file {
return nil, fs.ErrorObjectNotFound
}
nativePath, err := f.toNative(remote)
if err != nil {
return nil, err
+12 -10
View File
@@ -9,6 +9,7 @@ import (
"io"
"os"
"path"
"slices"
"strings"
"time"
@@ -144,6 +145,13 @@ func (f *Fs) readZip() (singleObject bool, err error) {
skipped++
continue
}
isDir := strings.HasSuffix(file.Name, "/")
// A file entry whose last component is "", "." or ".." names
// a directory rather than a file, so skip
if !isDir && sanitize.Leaf(path.Base(file.Name)) != nil {
skipped++
continue
}
remote = path.Join(f.prefix, remote)
if f.root != "" {
// Ignore all files outside the root, requiring a path
@@ -158,19 +166,12 @@ func (f *Fs) readZip() (singleObject bool, err error) {
remote = strings.TrimPrefix(remote, f.root+"/")
}
}
if strings.HasSuffix(file.Name, "/") {
if isDir {
dir := fs.NewDir(remote, file.Modified)
dt.AddDir(dir)
} else {
if remote == "" {
// A file at the root itself can only be the
// archive member f.root points at - with no root
// it is a crafted name for the archive's own
// directory, which can't be a file
if f.root == "" {
skipped++
continue
}
// A file at the root is the archive member f.root points at
remote = path.Base(f.root)
singleObject = true
dt = dirtree.New()
@@ -217,7 +218,8 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
return nil, fs.ErrorDirNotFound
}
fs.Debugf(f, "dir=%q, entries=%v", dir, entries)
return entries, nil
// Return a copy as callers may filter the listing in place
return slices.Clone(entries), nil
}
// NewObject finds the Object at remote.
+48 -6
View File
@@ -4,6 +4,7 @@ import (
"archive/zip"
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"sort"
@@ -105,10 +106,12 @@ func TestReadZipRootBoundary(t *testing.T) {
assert.Equal(t, []string{"a.txt"}, remotes)
}
// A file entry whose name refers to the archive's own root (".", "/",
// "./" or "") must be skipped, not turn the whole archive into a single
// file which hides every other entry.
func TestReadZipRootNamedEntry(t *testing.T) {
// A file entry whose name refers to a directory (".", "/", "./", "" or
// "sub/.") must be skipped, not turn the archive or the directory
// mounted as its root into a single file which hides every other
// entry, or become an object named for the archive itself when the
// archive is found by listing its directory.
func TestReadZipDirNamedFileEntry(t *testing.T) {
ctx := context.Background()
dir := t.TempDir()
name := writeZip(t, dir, "dot.zip",
@@ -117,14 +120,53 @@ func TestReadZipRootNamedEntry(t *testing.T) {
"./",
"",
"good.txt",
"sub/",
"sub/.",
"sub/good.txt",
)
localFs, err := cache.Get(ctx, dir)
require.NoError(t, err)
for _, test := range []struct {
prefix, root string
want []string
}{
{"", "", []string{"good.txt", "sub/good.txt"}},
{"sub/dot.zip", "", []string{"sub/dot.zip/good.txt", "sub/dot.zip/sub/good.txt"}},
{"", "sub", []string{"good.txt"}},
} {
t.Run(fmt.Sprintf("prefix=%q,root=%q", test.prefix, test.root), func(t *testing.T) {
f, err := New(ctx, localFs, name, test.prefix, test.root)
require.NoError(t, err)
assert.Equal(t, test.want, allRemotes(t, f))
})
}
}
// Listings are served from a cache which must survive callers
// filtering the returned slice in place, as fs/list does.
func TestListCacheNotAliased(t *testing.T) {
ctx := context.Background()
dir := t.TempDir()
name := writeZip(t, dir, "test.zip", "a.txt", "b.txt", "c.txt")
localFs, err := cache.Get(ctx, dir)
require.NoError(t, err)
f, err := New(ctx, localFs, name, "", "")
require.NoError(t, err)
remotes := allRemotes(t, f)
assert.Equal(t, []string{"good.txt"}, remotes)
entries, err := f.List(ctx, "")
require.NoError(t, err)
require.Len(t, entries, 3)
// Compact in place, dropping the first entry
copy(entries, entries[1:])
entries, err = f.List(ctx, "")
require.NoError(t, err)
var remotes []string
for _, entry := range entries {
remotes = append(remotes, entry.Remote())
}
assert.Equal(t, []string{"a.txt", "b.txt", "c.txt"}, remotes)
}
+11 -18
View File
@@ -18,24 +18,20 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
"github.com/rclone/rclone/backend/azureblob/arrowlist"
"github.com/rclone/rclone/fs"
"golang.org/x/sync/errgroup"
)
// arrowContentType is the Content-Type of an Apache Arrow IPC stream listing
// response, used to tell whether the service honoured an Arrow request.
const arrowContentType = "application/vnd.apache.arrow.stream"
// listArrowParallel lists (containerName, directory) by splitting the blob name
// keyspace into shards and listing them concurrently. opts must already request
// the Arrow format; it is cloned per shard with StartFrom/EndBefore set. It
// returns the number of raw items seen. directory/prefix are already normalised
// by the caller.
func (f *Fs) listArrowParallel(ctx context.Context, containerName, directory, prefix string, addContainer bool, opts *arrowlist.ListBlobsHierarchyOptions, delimiter string, fn listFn) (int, error) {
// Parallel listing needs the arrowlist client (server-side endBefore is
// only honoured on the Arrow path) - without it fall back to sequential.
if _, err := f.arrowCntSVC(containerName); err != nil {
fs.Debugf(f, "Not using parallel Arrow listing: %v", err)
return f.listBlobsPager(ctx, containerName, directory, prefix, addContainer, opts, delimiter, fn)
}
func (f *Fs) listArrowParallel(ctx context.Context, containerName, directory, prefix string, addContainer bool, opts *container.ListBlobsHierarchyOptions, delimiter string, fn listFn) (int, error) {
// Split the keyspace by a single-case character ladder (see
// arrowLadderBoundaries for why single-case matters).
points := arrowLadderBoundaries(directory, arrowShardTarget(f.opt.ListParallelism))
@@ -115,16 +111,13 @@ func (f *Fs) listArrowParallel(ctx context.Context, containerName, directory, pr
// isEndBeforeUnsupported reports whether err means the service can't honour
// the endBefore listing bound, so sharded parallel listing must not be used.
//
// This happens two ways: the arrowlist pager's ErrEndBeforeXMLFallback (the
// server answered XML with endBefore set), or the service rejecting the
// endBefore parameter with 400 OperationNotSupportedWithFeatureMissing "The
// requested operation is not allowed as EndBefore parameter support is
// missing on this account" - endBefore is only supported on the Arrow
// listing path of accounts with Blob Listing with Apache Arrow enabled.
// The service rejects the endBefore parameter with 400
// OperationNotSupportedWithFeatureMissing "The requested operation is not
// allowed as EndBefore parameter support is missing on this account" -
// endBefore is only supported on the Arrow listing path of accounts with
// Blob Listing with Apache Arrow enabled. (The SDK pager would silently
// accept an XML page with endBefore set, but the service never sends one.)
func isEndBeforeUnsupported(err error) bool {
if errors.Is(err, arrowlist.ErrEndBeforeXMLFallback) {
return true
}
var respErr *azcore.ResponseError
return errors.As(err, &respErr) && respErr.ErrorCode == "OperationNotSupportedWithFeatureMissing"
}
+1 -3
View File
@@ -10,7 +10,6 @@ import (
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/rclone/rclone/backend/azureblob/arrowlist"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fstest"
"github.com/rclone/rclone/fstest/fstests"
@@ -212,9 +211,8 @@ func TestArrowLadderBoundaries(t *testing.T) {
}
func TestIsEndBeforeUnsupported(t *testing.T) {
assert.True(t, isEndBeforeUnsupported(arrowlist.ErrEndBeforeXMLFallback))
assert.True(t, isEndBeforeUnsupported(fmt.Errorf("wrapped: %w", arrowlist.ErrEndBeforeXMLFallback)))
assert.True(t, isEndBeforeUnsupported(&azcore.ResponseError{ErrorCode: "OperationNotSupportedWithFeatureMissing"}))
assert.True(t, isEndBeforeUnsupported(fmt.Errorf("wrapped: %w", &azcore.ResponseError{ErrorCode: "OperationNotSupportedWithFeatureMissing"})))
assert.False(t, isEndBeforeUnsupported(&azcore.ResponseError{ErrorCode: "ContainerNotFound"}))
assert.False(t, isEndBeforeUnsupported(errors.New("some other error")))
assert.False(t, isEndBeforeUnsupported(nil))
-421
View File
@@ -1,421 +0,0 @@
//go:build !plan9 && !solaris && !js
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//
// Adapted from internal/arrow/arrow.go in the Azure SDK for Go
// github.com/Azure/azure-sdk-for-go/sdk/storage/azblob at commit
// c6fa341ca22b (branch feature/storage/bifrost), retyped onto the SDK's
// public type aliases so it compiles against the released azblob module.
package arrowlist
import (
"encoding/base64"
"fmt"
"io"
"net/http"
"strconv"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/lease"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/ipc"
"github.com/apache/arrow-go/v18/arrow/memory"
)
const (
// ArrowContentType is the Content-Type for Apache Arrow IPC stream responses.
ArrowContentType = "application/vnd.apache.arrow.stream"
// ArrowAcceptHeader is the Accept header value to request Arrow format.
ArrowAcceptHeader = ArrowContentType
// resourceTypeBlobPrefix is the ResourceType value that identifies a virtual directory prefix
// in hierarchy listing responses. In XML, prefixes are separate <BlobPrefix> elements;
// in Arrow, all rows share the same schema and are distinguished by this field value.
resourceTypeBlobPrefix = "blobprefix"
)
// HandleFlatListResponse parses an Arrow IPC stream response into a ContainerClientListBlobFlatSegmentResponse.
func HandleFlatListResponse(resp *http.Response) (container.ListBlobsFlatResponse, error) {
result := container.ListBlobsFlatResponse{}
// Extract response headers
extractResponseHeaders(resp, &result.ClientRequestID, &result.ContentType, &result.Date, &result.RequestID, &result.Version)
// Parse Arrow IPC stream
items, nextMarker, err := parseArrowStream(resp.Body)
if err != nil {
return result, fmt.Errorf("failed to parse Arrow IPC response: %w", err)
}
result.ListBlobsFlatSegmentResponse = container.ListBlobsFlatSegmentResponse{
Segment: &container.BlobFlatListSegment{
BlobItems: items,
},
NextMarker: nextMarker,
}
return result, nil
}
// HandleHierarchyListResponse parses an Arrow IPC stream response into a ContainerClientListBlobHierarchySegmentResponse.
func HandleHierarchyListResponse(resp *http.Response) (container.ListBlobsHierarchyResponse, error) {
result := container.ListBlobsHierarchyResponse{}
extractResponseHeaders(resp, &result.ClientRequestID, &result.ContentType, &result.Date, &result.RequestID, &result.Version)
items, nextMarker, err := parseArrowStream(resp.Body)
if err != nil {
return result, fmt.Errorf("failed to parse Arrow IPC response: %w", err)
}
// Separate blob items from blob prefixes based on ResourceType field.
var blobItems []*container.BlobItem
var blobPrefixes []*container.BlobPrefix
for _, item := range items {
if item.Properties != nil && item.Properties.ResourceType != nil && *item.Properties.ResourceType == resourceTypeBlobPrefix {
blobPrefixes = append(blobPrefixes, &container.BlobPrefix{
Name: item.Name,
Properties: item.Properties,
})
} else {
blobItems = append(blobItems, item)
}
}
result.ListBlobsHierarchySegmentResponse = container.ListBlobsHierarchySegmentResponse{
Segment: &container.BlobHierarchyListSegment{
BlobItems: blobItems,
BlobPrefixes: blobPrefixes,
},
NextMarker: nextMarker,
}
return result, nil
}
func extractResponseHeaders(resp *http.Response, clientRequestID, contentType **string, date **time.Time, requestID, version **string) {
if val := resp.Header.Get("x-ms-client-request-id"); val != "" {
*clientRequestID = &val
}
if val := resp.Header.Get("Content-Type"); val != "" {
*contentType = &val
}
if val := resp.Header.Get("Date"); val != "" {
if t, err := time.Parse(time.RFC1123, val); err == nil {
*date = &t
}
}
if val := resp.Header.Get("x-ms-request-id"); val != "" {
*requestID = &val
}
if val := resp.Header.Get("x-ms-version"); val != "" {
*version = &val
}
}
// parseArrowStream reads an Arrow IPC stream and converts it to BlobItems.
func parseArrowStream(body io.Reader) ([]*container.BlobItem, *string, error) {
reader, err := ipc.NewReader(body, ipc.WithAllocator(memory.DefaultAllocator))
if err != nil {
return nil, nil, fmt.Errorf("failed to create Arrow IPC reader: %w", err)
}
defer reader.Release()
// Extract NextMarker from schema metadata
var nextMarker *string
var numRecords int
if md := reader.Schema().Metadata(); md.Len() > 0 {
if idx := md.FindKey("NextMarker"); idx >= 0 {
val := md.Values()[idx]
if val != "" {
nextMarker = &val
}
}
if idx := md.FindKey("NumberOfRecords"); idx >= 0 {
if n, err := strconv.Atoi(md.Values()[idx]); err == nil {
numRecords = n
}
}
}
// Build column index for the schema
schema := reader.Schema()
colIndex := buildColumnIndex(schema)
// Pre-allocate if we know the count
items := make([]*container.BlobItem, 0, numRecords)
// Iterate over record batches
for reader.Next() {
rec := reader.RecordBatch()
rows := int(rec.NumRows())
for row := 0; row < rows; row++ {
item := &container.BlobItem{
Properties: &container.BlobProperties{},
}
populateBlobItem(item, rec, row, colIndex)
items = append(items, item)
}
}
if err := reader.Err(); err != nil {
return nil, nil, fmt.Errorf("error reading Arrow record batches: %w", err)
}
return items, nextMarker, nil
}
// columnIndex maps column names to their indices in the record batch for O(1) lookup.
type columnIndex map[string]int
func buildColumnIndex(schema *arrow.Schema) columnIndex {
idx := make(columnIndex, len(schema.Fields()))
for i, f := range schema.Fields() {
idx[f.Name] = i
}
return idx
}
// populateBlobItem fills a BlobItem from a single row across all columns.
func populateBlobItem(item *container.BlobItem, rec arrow.RecordBatch, row int, colIdx columnIndex) {
// Top-level BlobItem fields
item.Name = getStringField(rec, row, colIdx, "Name")
item.Deleted = getBoolField(rec, row, colIdx, "Deleted")
item.Snapshot = getStringField(rec, row, colIdx, "Snapshot")
item.VersionID = getStringField(rec, row, colIdx, "VersionId")
item.IsCurrentVersion = getBoolField(rec, row, colIdx, "IsCurrentVersion")
item.HasVersionsOnly = getBoolField(rec, row, colIdx, "HasVersionsOnly")
// Metadata maps
item.Metadata = getMapField(rec, row, colIdx, "Metadata")
item.OrMetadata = getMapField(rec, row, colIdx, "OrMetadata")
// Tags
if tags := getMapField(rec, row, colIdx, "Tags"); tags != nil {
var blobTags []*container.BlobTag
for k, v := range tags {
key := k
blobTags = append(blobTags, &container.BlobTag{Key: &key, Value: v})
}
item.BlobTags = &container.BlobTags{BlobTagSet: blobTags}
}
// BlobProperties fields
p := item.Properties
p.CreationTime = getTimestampField(rec, row, colIdx, "Creation-Time")
p.LastModified = getTimestampField(rec, row, colIdx, "Last-Modified")
p.ETag = getETagField(rec, row, colIdx, "Etag")
p.ContentLength = getInt64Field(rec, row, colIdx, "Content-Length")
p.ContentType = getStringField(rec, row, colIdx, "Content-Type")
p.ContentEncoding = getStringField(rec, row, colIdx, "Content-Encoding")
p.ContentLanguage = getStringField(rec, row, colIdx, "Content-Language")
p.ContentDisposition = getStringField(rec, row, colIdx, "Content-Disposition")
p.CacheControl = getStringField(rec, row, colIdx, "Cache-Control")
p.ContentMD5 = getBase64BytesField(rec, row, colIdx, "Content-MD5")
p.BlobType = getEnumField[container.BlobType](rec, row, colIdx, "BlobType")
p.AccessTier = getEnumField[container.AccessTier](rec, row, colIdx, "AccessTier")
p.AccessTierInferred = getBoolField(rec, row, colIdx, "AccessTierInferred")
p.AccessTierChangeTime = getTimestampField(rec, row, colIdx, "AccessTierChangeTime")
p.LeaseState = getEnumField[lease.StateType](rec, row, colIdx, "LeaseState")
p.LeaseStatus = getEnumField[lease.StatusType](rec, row, colIdx, "LeaseStatus")
p.LeaseDuration = getEnumField[lease.DurationType](rec, row, colIdx, "LeaseDuration")
p.ServerEncrypted = getBoolField(rec, row, colIdx, "ServerEncrypted")
p.CustomerProvidedKeySHA256 = getStringField(rec, row, colIdx, "CustomerProvidedKeySha256")
p.EncryptionScope = getStringField(rec, row, colIdx, "EncryptionScope")
p.IncrementalCopy = getBoolField(rec, row, colIdx, "IncrementalCopy")
p.IsSealed = getBoolField(rec, row, colIdx, "Sealed")
p.ArchiveStatus = getEnumField[container.ArchiveStatus](rec, row, colIdx, "ArchiveStatus")
p.RehydratePriority = getEnumField[blob.RehydratePriority](rec, row, colIdx, "RehydratePriority")
p.CopyID = getStringField(rec, row, colIdx, "CopyId")
p.CopyStatus = getEnumField[blob.CopyStatusType](rec, row, colIdx, "CopyStatus")
p.CopySource = getStringField(rec, row, colIdx, "CopySource")
p.CopyProgress = getStringField(rec, row, colIdx, "CopyProgress")
p.CopyCompletionTime = getTimestampField(rec, row, colIdx, "CopyCompletionTime")
p.CopyStatusDescription = getStringField(rec, row, colIdx, "CopyStatusDescription")
p.DestinationSnapshot = getStringField(rec, row, colIdx, "CopyDestinationSnapshot")
p.ImmutabilityPolicyExpiresOn = getTimestampField(rec, row, colIdx, "ImmutabilityPolicyUntilDate")
p.ImmutabilityPolicyMode = getEnumField[container.ImmutabilityPolicyMode](rec, row, colIdx, "ImmutabilityPolicyMode")
p.LegalHold = getBoolField(rec, row, colIdx, "LegalHold")
p.DeletedTime = getTimestampField(rec, row, colIdx, "DeletedTime")
p.RemainingRetentionDays = getInt32Field(rec, row, colIdx, "RemainingRetentionDays")
p.LastAccessedOn = getTimestampField(rec, row, colIdx, "LastAccessTime")
p.TagCount = getInt32Field(rec, row, colIdx, "TagCount")
p.BlobSequenceNumber = getInt64Field(rec, row, colIdx, "x-ms-blob-sequence-number")
p.ResourceType = getStringField(rec, row, colIdx, "ResourceType")
// Content-CRC64 is a string field in Arrow but not directly mapped to BlobProperties.
// The field exists in the Arrow schema but has no corresponding field in the Go SDK BlobProperties.
// It is silently ignored for forward compatibility.
}
// Field accessor helpers that safely handle missing columns and null values.
func getStringField(rec arrow.RecordBatch, row int, colIdx columnIndex, name string) *string {
idx, ok := colIdx[name]
if !ok {
return nil
}
col := rec.Column(idx)
if col.IsNull(row) {
return nil
}
if arr, ok := col.(*array.String); ok {
val := arr.Value(row)
return &val
}
return nil
}
func getBoolField(rec arrow.RecordBatch, row int, colIdx columnIndex, name string) *bool {
idx, ok := colIdx[name]
if !ok {
return nil
}
col := rec.Column(idx)
if col.IsNull(row) {
return nil
}
if arr, ok := col.(*array.Boolean); ok {
val := arr.Value(row)
return &val
}
return nil
}
func getTimestampField(rec arrow.RecordBatch, row int, colIdx columnIndex, name string) *time.Time {
idx, ok := colIdx[name]
if !ok {
return nil
}
col := rec.Column(idx)
if col.IsNull(row) {
return nil
}
if arr, ok := col.(*array.Timestamp); ok {
val := arr.Value(row)
// Arrow timestamps are stored as int64 with a unit; convert to time.Time
dt := val.ToTime(arr.DataType().(*arrow.TimestampType).Unit)
return &dt
}
return nil
}
func getInt64Field(rec arrow.RecordBatch, row int, colIdx columnIndex, name string) *int64 {
idx, ok := colIdx[name]
if !ok {
return nil
}
col := rec.Column(idx)
if col.IsNull(row) {
return nil
}
if arr, ok := col.(*array.Uint64); ok {
val := int64(arr.Value(row))
return &val
}
if arr, ok := col.(*array.Int64); ok {
val := arr.Value(row)
return &val
}
return nil
}
func getInt32Field(rec arrow.RecordBatch, row int, colIdx columnIndex, name string) *int32 {
idx, ok := colIdx[name]
if !ok {
return nil
}
col := rec.Column(idx)
if col.IsNull(row) {
return nil
}
if arr, ok := col.(*array.Uint64); ok {
val := int32(arr.Value(row))
return &val
}
if arr, ok := col.(*array.Int32); ok {
val := arr.Value(row)
return &val
}
return nil
}
func getETagField(rec arrow.RecordBatch, row int, colIdx columnIndex, name string) *azcore.ETag {
s := getStringField(rec, row, colIdx, name)
if s == nil {
return nil
}
etag := azcore.ETag(*s)
return &etag
}
func getBase64BytesField(rec arrow.RecordBatch, row int, colIdx columnIndex, name string) []byte {
s := getStringField(rec, row, colIdx, name)
if s == nil {
return nil
}
decoded, err := base64.StdEncoding.DecodeString(*s)
if err != nil {
return nil
}
return decoded
}
// getEnumField returns a pointer to a string-typed enum value.
type stringEnum interface {
~string
}
func getEnumField[T stringEnum](rec arrow.RecordBatch, row int, colIdx columnIndex, name string) *T {
s := getStringField(rec, row, colIdx, name)
if s == nil {
return nil
}
val := T(*s)
return &val
}
func getMapField(rec arrow.RecordBatch, row int, colIdx columnIndex, name string) map[string]*string {
idx, ok := colIdx[name]
if !ok {
return nil
}
col := rec.Column(idx)
if col.IsNull(row) {
return nil
}
mapArr, ok := col.(*array.Map)
if !ok {
return nil
}
// Get the offsets for this row's map entries
start := int(mapArr.Offsets()[row])
end := int(mapArr.Offsets()[row+1])
if start == end {
return nil
}
keys := mapArr.Keys().(*array.String)
values := mapArr.Items().(*array.String)
result := make(map[string]*string, end-start)
for i := start; i < end; i++ {
k := keys.Value(i)
if values.IsNull(i) {
result[k] = nil
} else {
v := values.Value(i)
result[k] = &v
}
}
return result
}
-374
View File
@@ -1,374 +0,0 @@
//go:build !plan9 && !solaris && !js
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//
// Adapted from internal/arrow/arrow_test.go in the Azure SDK for Go
// github.com/Azure/azure-sdk-for-go/sdk/storage/azblob at commit
// c6fa341ca22b (branch feature/storage/bifrost).
package arrowlist
import (
"bytes"
"io"
"net/http"
"testing"
"github.com/apache/arrow-go/v18/arrow"
arrowArray "github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/ipc"
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/stretchr/testify/require"
)
// coreSchema returns a basic Arrow schema for testing with common blob fields.
func coreSchema(md *arrow.Metadata) *arrow.Schema {
fields := []arrow.Field{
{Name: "Name", Type: arrow.BinaryTypes.String, Nullable: false},
{Name: "Creation-Time", Type: &arrow.TimestampType{Unit: arrow.Microsecond}, Nullable: true},
{Name: "Last-Modified", Type: &arrow.TimestampType{Unit: arrow.Microsecond}, Nullable: true},
{Name: "BlobType", Type: arrow.BinaryTypes.String, Nullable: true},
{Name: "Etag", Type: arrow.BinaryTypes.String, Nullable: true},
{Name: "Content-Length", Type: arrow.PrimitiveTypes.Uint64, Nullable: true},
{Name: "Content-Type", Type: arrow.BinaryTypes.String, Nullable: true},
{Name: "ServerEncrypted", Type: arrow.FixedWidthTypes.Boolean, Nullable: true},
{Name: "AccessTier", Type: arrow.BinaryTypes.String, Nullable: true},
{Name: "LeaseState", Type: arrow.BinaryTypes.String, Nullable: true},
{Name: "LeaseStatus", Type: arrow.BinaryTypes.String, Nullable: true},
}
return arrow.NewSchema(fields, md)
}
// buildArrowStream constructs a synthetic Arrow IPC stream from a schema and a populate function.
func buildArrowStream(t *testing.T, schema *arrow.Schema, populate func(builder *arrowArray.RecordBuilder)) []byte {
t.Helper()
alloc := memory.DefaultAllocator
var buf bytes.Buffer
w := ipc.NewWriter(&buf, ipc.WithSchema(schema), ipc.WithAllocator(alloc))
builder := arrowArray.NewRecordBuilder(alloc, schema)
defer builder.Release()
populate(builder)
rec := builder.NewRecordBatch()
defer rec.Release()
require.NoError(t, w.Write(rec))
require.NoError(t, w.Close())
return buf.Bytes()
}
func makeHTTPResponse(data []byte, headers map[string]string) *http.Response {
h := http.Header{}
h.Set("Content-Type", ArrowContentType)
for k, v := range headers {
h.Set(k, v)
}
return &http.Response{
StatusCode: http.StatusOK,
Header: h,
Body: io.NopCloser(bytes.NewReader(data)),
}
}
func TestHandleFlatListResponse_BasicParsing(t *testing.T) {
md := arrow.NewMetadata([]string{"NextMarker", "NumberOfRecords"}, []string{"marker123", "2"})
schema := coreSchema(&md)
data := buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
b.Field(0).(*arrowArray.StringBuilder).Append("blob1.txt")
b.Field(1).(*arrowArray.TimestampBuilder).Append(arrow.Timestamp(1700000000000000))
b.Field(2).(*arrowArray.TimestampBuilder).Append(arrow.Timestamp(1700000000000000))
b.Field(3).(*arrowArray.StringBuilder).Append("BlockBlob")
b.Field(4).(*arrowArray.StringBuilder).Append("0x1234")
b.Field(5).(*arrowArray.Uint64Builder).Append(1024)
b.Field(6).(*arrowArray.StringBuilder).Append("application/octet-stream")
b.Field(7).(*arrowArray.BooleanBuilder).Append(true)
b.Field(8).(*arrowArray.StringBuilder).Append("Hot")
b.Field(9).(*arrowArray.StringBuilder).Append("available")
b.Field(10).(*arrowArray.StringBuilder).Append("unlocked")
b.Field(0).(*arrowArray.StringBuilder).Append("blob2.txt")
b.Field(1).(*arrowArray.TimestampBuilder).Append(arrow.Timestamp(1700000001000000))
b.Field(2).(*arrowArray.TimestampBuilder).Append(arrow.Timestamp(1700000001000000))
b.Field(3).(*arrowArray.StringBuilder).Append("BlockBlob")
b.Field(4).(*arrowArray.StringBuilder).Append("0x5678")
b.Field(5).(*arrowArray.Uint64Builder).Append(2048)
b.Field(6).(*arrowArray.StringBuilder).Append("text/plain")
b.Field(7).(*arrowArray.BooleanBuilder).Append(false)
b.Field(8).(*arrowArray.StringBuilder).Append("Cool")
b.Field(9).(*arrowArray.StringBuilder).Append("available")
b.Field(10).(*arrowArray.StringBuilder).Append("unlocked")
})
resp := makeHTTPResponse(data, nil)
result, err := HandleFlatListResponse(resp)
require.NoError(t, err)
require.NotNil(t, result.NextMarker)
require.Equal(t, "marker123", *result.NextMarker)
require.Len(t, result.Segment.BlobItems, 2)
item := result.Segment.BlobItems[0]
require.Equal(t, "blob1.txt", *item.Name)
require.Equal(t, "BlockBlob", string(*item.Properties.BlobType))
require.Equal(t, int64(1024), *item.Properties.ContentLength)
require.Equal(t, "Hot", string(*item.Properties.AccessTier))
}
func TestHandleFlatListResponse_EmptyRecordBatch(t *testing.T) {
md := arrow.NewMetadata([]string{"NextMarker", "NumberOfRecords"}, []string{"", "0"})
schema := coreSchema(&md)
data := buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
// No rows added
})
resp := makeHTTPResponse(data, nil)
result, err := HandleFlatListResponse(resp)
require.NoError(t, err)
require.Nil(t, result.NextMarker)
require.Empty(t, result.Segment.BlobItems)
}
func TestHandleFlatListResponse_NullFields(t *testing.T) {
md := arrow.NewMetadata([]string{"NextMarker"}, []string{""})
schema := coreSchema(&md)
data := buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
b.Field(0).(*arrowArray.StringBuilder).Append("blob.txt")
b.Field(1).(*arrowArray.TimestampBuilder).AppendNull()
b.Field(2).(*arrowArray.TimestampBuilder).AppendNull()
b.Field(3).(*arrowArray.StringBuilder).AppendNull()
b.Field(4).(*arrowArray.StringBuilder).AppendNull()
b.Field(5).(*arrowArray.Uint64Builder).AppendNull()
b.Field(6).(*arrowArray.StringBuilder).AppendNull()
b.Field(7).(*arrowArray.BooleanBuilder).AppendNull()
b.Field(8).(*arrowArray.StringBuilder).AppendNull()
b.Field(9).(*arrowArray.StringBuilder).AppendNull()
b.Field(10).(*arrowArray.StringBuilder).AppendNull()
})
resp := makeHTTPResponse(data, nil)
result, err := HandleFlatListResponse(resp)
require.NoError(t, err)
require.Len(t, result.Segment.BlobItems, 1)
item := result.Segment.BlobItems[0]
require.Equal(t, "blob.txt", *item.Name)
require.Nil(t, item.Properties.CreationTime)
require.Nil(t, item.Properties.BlobType)
require.Nil(t, item.Properties.ETag)
require.Nil(t, item.Properties.ContentLength)
require.Nil(t, item.Properties.ContentType)
require.Nil(t, item.Properties.ServerEncrypted)
}
func TestHandleFlatListResponse_UnknownColumnsIgnored(t *testing.T) {
md := arrow.NewMetadata([]string{"NextMarker"}, []string{""})
fields := []arrow.Field{
{Name: "Name", Type: arrow.BinaryTypes.String, Nullable: false},
{Name: "FutureField", Type: arrow.BinaryTypes.String, Nullable: true},
}
schema := arrow.NewSchema(fields, &md)
data := buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
b.Field(0).(*arrowArray.StringBuilder).Append("blob.txt")
b.Field(1).(*arrowArray.StringBuilder).Append("some-future-value")
})
resp := makeHTTPResponse(data, nil)
result, err := HandleFlatListResponse(resp)
require.NoError(t, err)
require.Len(t, result.Segment.BlobItems, 1)
require.Equal(t, "blob.txt", *result.Segment.BlobItems[0].Name)
}
func TestHandleFlatListResponse_MultiRecordBatch(t *testing.T) {
alloc := memory.DefaultAllocator
md := arrow.NewMetadata([]string{"NextMarker", "NumberOfRecords"}, []string{"", "3"})
schema := coreSchema(&md)
var buf bytes.Buffer
w := ipc.NewWriter(&buf, ipc.WithSchema(schema), ipc.WithAllocator(alloc))
// First batch: 2 rows
builder := arrowArray.NewRecordBuilder(alloc, schema)
for i := 0; i < 2; i++ {
builder.Field(0).(*arrowArray.StringBuilder).Append("batch1_blob")
for j := 1; j < 11; j++ {
builder.Field(j).AppendNull()
}
}
rec := builder.NewRecordBatch()
require.NoError(t, w.Write(rec))
rec.Release()
builder.Release()
// Second batch: 1 row
builder = arrowArray.NewRecordBuilder(alloc, schema)
builder.Field(0).(*arrowArray.StringBuilder).Append("batch2_blob")
for j := 1; j < 11; j++ {
builder.Field(j).AppendNull()
}
rec = builder.NewRecordBatch()
require.NoError(t, w.Write(rec))
rec.Release()
builder.Release()
require.NoError(t, w.Close())
resp := makeHTTPResponse(buf.Bytes(), nil)
result, err := HandleFlatListResponse(resp)
require.NoError(t, err)
require.Len(t, result.Segment.BlobItems, 3)
}
func TestHandleHierarchyListResponse_SeparatesBlobsAndPrefixes(t *testing.T) {
md := arrow.NewMetadata([]string{"NextMarker"}, []string{""})
fields := []arrow.Field{
{Name: "Name", Type: arrow.BinaryTypes.String, Nullable: false},
{Name: "ResourceType", Type: arrow.BinaryTypes.String, Nullable: true},
}
schema := arrow.NewSchema(fields, &md)
data := buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
// A regular blob
b.Field(0).(*arrowArray.StringBuilder).Append("folder/file.txt")
b.Field(1).(*arrowArray.StringBuilder).AppendNull()
// A blob prefix (virtual directory)
b.Field(0).(*arrowArray.StringBuilder).Append("folder/subfolder/")
b.Field(1).(*arrowArray.StringBuilder).Append("blobprefix")
})
resp := makeHTTPResponse(data, nil)
result, err := HandleHierarchyListResponse(resp)
require.NoError(t, err)
require.Len(t, result.Segment.BlobItems, 1)
require.Equal(t, "folder/file.txt", *result.Segment.BlobItems[0].Name)
require.Len(t, result.Segment.BlobPrefixes, 1)
require.Equal(t, "folder/subfolder/", *result.Segment.BlobPrefixes[0].Name)
}
func TestHandleFlatListResponse_ResponseHeaders(t *testing.T) {
md := arrow.NewMetadata([]string{"NextMarker"}, []string{""})
fields := []arrow.Field{
{Name: "Name", Type: arrow.BinaryTypes.String, Nullable: false},
}
schema := arrow.NewSchema(fields, &md)
data := buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
b.Field(0).(*arrowArray.StringBuilder).Append("blob.txt")
})
resp := makeHTTPResponse(data, map[string]string{
"x-ms-request-id": "req-123",
"x-ms-version": "2026-10-06",
"x-ms-client-request-id": "client-req-456",
"Date": "Mon, 01 Jan 2024 00:00:00 GMT",
})
result, err := HandleFlatListResponse(resp)
require.NoError(t, err)
require.Equal(t, "req-123", *result.RequestID)
require.Equal(t, "2026-10-06", *result.Version)
require.Equal(t, "client-req-456", *result.ClientRequestID)
require.NotNil(t, result.Date)
}
func TestHandleFlatListResponse_MapFields(t *testing.T) {
md := arrow.NewMetadata([]string{"NextMarker"}, []string{""})
fields := []arrow.Field{
{Name: "Name", Type: arrow.BinaryTypes.String, Nullable: false},
{Name: "Metadata", Type: arrow.MapOf(arrow.BinaryTypes.String, arrow.BinaryTypes.String), Nullable: true},
{Name: "Tags", Type: arrow.MapOf(arrow.BinaryTypes.String, arrow.BinaryTypes.String), Nullable: true},
}
schema := arrow.NewSchema(fields, &md)
data := buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
b.Field(0).(*arrowArray.StringBuilder).Append("blob.txt")
metaBuilder := b.Field(1).(*arrowArray.MapBuilder)
metaBuilder.Append(true)
metaBuilder.KeyBuilder().(*arrowArray.StringBuilder).Append("key1")
metaBuilder.ItemBuilder().(*arrowArray.StringBuilder).Append("value1")
tagBuilder := b.Field(2).(*arrowArray.MapBuilder)
tagBuilder.Append(true)
tagBuilder.KeyBuilder().(*arrowArray.StringBuilder).Append("env")
tagBuilder.ItemBuilder().(*arrowArray.StringBuilder).Append("prod")
})
resp := makeHTTPResponse(data, nil)
result, err := HandleFlatListResponse(resp)
require.NoError(t, err)
require.Len(t, result.Segment.BlobItems, 1)
item := result.Segment.BlobItems[0]
require.NotNil(t, item.Metadata)
require.Equal(t, "value1", *item.Metadata["key1"])
require.NotNil(t, item.BlobTags)
require.Len(t, item.BlobTags.BlobTagSet, 1)
}
func TestHandleFlatListResponse_BooleanFields(t *testing.T) {
md := arrow.NewMetadata([]string{"NextMarker"}, []string{""})
fields := []arrow.Field{
{Name: "Name", Type: arrow.BinaryTypes.String, Nullable: false},
{Name: "Deleted", Type: arrow.FixedWidthTypes.Boolean, Nullable: true},
{Name: "IsCurrentVersion", Type: arrow.FixedWidthTypes.Boolean, Nullable: true},
{Name: "ServerEncrypted", Type: arrow.FixedWidthTypes.Boolean, Nullable: true},
}
schema := arrow.NewSchema(fields, &md)
data := buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
b.Field(0).(*arrowArray.StringBuilder).Append("blob.txt")
b.Field(1).(*arrowArray.BooleanBuilder).Append(true)
b.Field(2).(*arrowArray.BooleanBuilder).Append(false)
b.Field(3).(*arrowArray.BooleanBuilder).Append(true)
})
resp := makeHTTPResponse(data, nil)
result, err := HandleFlatListResponse(resp)
require.NoError(t, err)
item := result.Segment.BlobItems[0]
require.True(t, *item.Deleted)
require.False(t, *item.IsCurrentVersion)
require.True(t, *item.Properties.ServerEncrypted)
}
func TestHandleFlatListResponse_VersionAndSnapshot(t *testing.T) {
md := arrow.NewMetadata([]string{"NextMarker"}, []string{""})
fields := []arrow.Field{
{Name: "Name", Type: arrow.BinaryTypes.String, Nullable: false},
{Name: "VersionId", Type: arrow.BinaryTypes.String, Nullable: true},
{Name: "Snapshot", Type: arrow.BinaryTypes.String, Nullable: true},
{Name: "IsCurrentVersion", Type: arrow.FixedWidthTypes.Boolean, Nullable: true},
}
schema := arrow.NewSchema(fields, &md)
data := buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
b.Field(0).(*arrowArray.StringBuilder).Append("versioned-blob.txt")
b.Field(1).(*arrowArray.StringBuilder).Append("2024-01-01T00:00:00.0000000Z")
b.Field(2).(*arrowArray.StringBuilder).Append("snap-123")
b.Field(3).(*arrowArray.BooleanBuilder).Append(true)
})
resp := makeHTTPResponse(data, nil)
result, err := HandleFlatListResponse(resp)
require.NoError(t, err)
item := result.Segment.BlobItems[0]
require.Equal(t, "2024-01-01T00:00:00.0000000Z", *item.VersionID)
require.Equal(t, "snap-123", *item.Snapshot)
require.True(t, *item.IsCurrentVersion)
}
-259
View File
@@ -1,259 +0,0 @@
//go:build !plan9 && !solaris && !js
// Package arrowlist implements the experimental "Blob Listing with Apache
// Arrow" feature on top of the released Azure azblob SDK.
//
// The Azure SDK for Go has support for Arrow listing on the unreleased
// feature/storage/bifrost branch (commit c6fa341ca22b) where the listing
// pagers decode Arrow IPC stream responses into the ordinary ListBlobs
// response types. Until that ships in a tagged azblob release this package
// fills the gap: it exposes the same options and pager interface as the
// experimental SDK, built only on the public API of the released SDK plus
// copies of two small internal auth policies (see sharedkey.go and
// challenge_policy.go).
//
// This package is temporary. When Arrow listing ships in a released azblob
// the whole package should be deleted and its callers pointed back at
// container.Client.NewListBlobsHierarchyPager - the option and response
// types here are deliberately source compatible with the SDK's to make that
// a mechanical change.
package arrowlist
import (
"context"
"errors"
"net/http"
"strconv"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
)
const (
// moduleName and moduleVersion identify this package in the azcore
// telemetry policy's User-Agent fragment.
moduleName = "github.com/rclone/rclone/backend/azureblob/arrowlist"
moduleVersion = "v0.0.1"
// serviceVersion is the x-ms-version sent with listing requests. Arrow
// responses and the startFrom/endBefore parameters need at least
// 2026-06-06.
serviceVersion = "2026-06-06"
// tokenScope is the OAuth scope for Azure Storage, as used by the SDK's
// own clients.
tokenScope = "https://storage.azure.com/.default"
)
// ErrEndBeforeXMLFallback is returned by the pager when the server answered
// with XML instead of Arrow while EndBefore was set. The service only honours
// endBefore on the Arrow listing path, so an XML page could silently contain
// blobs outside the requested range and cannot be used.
var ErrEndBeforeXMLFallback = errors.New("arrow listing fell back to XML with endBefore set")
// ListBlobsHierarchyOptions provides the configuration for
// Client.NewListBlobsHierarchyPager.
//
// It mirrors the experimental SDK's container.ListBlobsHierarchyOptions:
// the released SDK's options are embedded (providing Include, Marker,
// MaxResults, Prefix and StartFrom by promotion) with the two fields the
// released SDK lacks added alongside.
type ListBlobsHierarchyOptions struct {
container.ListBlobsHierarchyOptions
// EndBefore limits listing to blobs whose full container path is
// lexically before it (exclusive). Only honoured on Arrow responses.
EndBefore *string
// UseArrowFormat requests the response as an Apache Arrow IPC stream.
UseArrowFormat *bool
}
// ClientOptions contains the optional parameters for client creation.
type ClientOptions struct {
azcore.ClientOptions
}
// Client issues Arrow listing requests to a single container.
type Client struct {
endpoint string
azClient *azcore.Client
}
// newClient creates a Client for the container at containerURL sending
// requests through a pipeline with the given auth policy (which may be nil
// for anonymous or SAS access).
func newClient(containerURL string, authPolicy policy.Policy, options *ClientOptions) (*Client, error) {
if options == nil {
options = &ClientOptions{}
}
plOpts := runtime.PipelineOptions{}
if authPolicy != nil {
plOpts.PerRetry = []policy.Policy{authPolicy}
}
azClient, err := azcore.NewClient(moduleName, moduleVersion, plOpts, &options.ClientOptions)
if err != nil {
return nil, err
}
return &Client{
endpoint: containerURL,
azClient: azClient,
}, nil
}
// NewClient creates a Client authenticating with a token credential.
func NewClient(containerURL string, cred azcore.TokenCredential, options *ClientOptions) (*Client, error) {
return newClient(containerURL, NewStorageChallengePolicy(cred, tokenScope, false), options)
}
// NewClientWithNoCredential creates a Client with no authentication, for
// anonymous access or a containerURL carrying a SAS token in its query.
func NewClientWithNoCredential(containerURL string, options *ClientOptions) (*Client, error) {
return newClient(containerURL, nil, options)
}
// NewClientWithSharedKeyCredential creates a Client signing requests with the
// account's shared key.
func NewClientWithSharedKeyCredential(containerURL string, cred *SharedKeyCredential, options *ClientOptions) (*Client, error) {
return newClient(containerURL, NewSharedKeyCredPolicy(cred), options)
}
// NewListBlobsHierarchyPager returns a pager over the container's blobs,
// requesting Arrow responses when o.UseArrowFormat is set and returning the
// same response type as container.Client.NewListBlobsHierarchyPager.
//
// If the service does not support Arrow listing (the feature not enabled on
// the account, or an HNS account) it answers with XML, which is decoded
// transparently - unless EndBefore is set, in which case the fetcher returns
// ErrEndBeforeXMLFallback (see there).
func (c *Client) NewListBlobsHierarchyPager(delimiter string, o *ListBlobsHierarchyOptions) *runtime.Pager[container.ListBlobsHierarchyResponse] {
// Copy the options so advancing the Marker doesn't mutate the caller's struct.
opts := ListBlobsHierarchyOptions{}
if o != nil {
opts = *o
}
useArrow := opts.UseArrowFormat != nil && *opts.UseArrowFormat
return runtime.NewPager(runtime.PagingHandler[container.ListBlobsHierarchyResponse]{
More: func(page container.ListBlobsHierarchyResponse) bool {
return page.NextMarker != nil && len(*page.NextMarker) > 0
},
Fetcher: func(ctx context.Context, page *container.ListBlobsHierarchyResponse) (container.ListBlobsHierarchyResponse, error) {
if page != nil {
opts.Marker = page.NextMarker
}
req, err := c.listCreateRequest(ctx, delimiter, &opts, useArrow)
if err != nil {
return container.ListBlobsHierarchyResponse{}, err
}
resp, err := c.azClient.Pipeline().Do(req)
if err != nil {
return container.ListBlobsHierarchyResponse{}, err
}
if !runtime.HasStatusCode(resp, http.StatusOK) {
return container.ListBlobsHierarchyResponse{}, runtime.NewResponseError(resp)
}
if useArrow && resp.Header.Get("Content-Type") == ArrowContentType {
return HandleHierarchyListResponse(resp)
}
if opts.EndBefore != nil {
return container.ListBlobsHierarchyResponse{}, ErrEndBeforeXMLFallback
}
return handleXMLResponse(resp)
},
})
}
// listCreateRequest builds the ListBlobs request, mirroring the generated
// request builders in the SDK.
func (c *Client) listCreateRequest(ctx context.Context, delimiter string, opts *ListBlobsHierarchyOptions, useArrow bool) (*policy.Request, error) {
req, err := runtime.NewRequest(ctx, http.MethodGet, c.endpoint)
if err != nil {
return nil, err
}
// Reading the existing query first preserves any SAS token in the endpoint.
reqQP := req.Raw().URL.Query()
reqQP.Set("comp", "list")
reqQP.Set("restype", "container")
reqQP.Set("delimiter", delimiter)
if include := formatInclude(opts.Include); include != "" {
reqQP.Set("include", include)
}
if opts.Marker != nil {
reqQP.Set("marker", *opts.Marker)
}
if opts.MaxResults != nil {
reqQP.Set("maxresults", strconv.FormatInt(int64(*opts.MaxResults), 10))
}
if opts.Prefix != nil {
reqQP.Set("prefix", *opts.Prefix)
}
if opts.StartFrom != nil {
reqQP.Set("startFrom", *opts.StartFrom)
}
if useArrow && opts.EndBefore != nil {
reqQP.Set("endBefore", *opts.EndBefore)
}
req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
if useArrow {
// Stream the Arrow body rather than buffering it in the pipeline.
runtime.SkipBodyDownload(req)
req.Raw().Header["Accept"] = []string{ArrowAcceptHeader}
} else {
req.Raw().Header["Accept"] = []string{"application/xml"}
}
req.Raw().Header["x-ms-version"] = []string{serviceVersion}
return req, nil
}
// handleXMLResponse decodes an XML ListBlobs response, mirroring the SDK's
// generated ListBlobHierarchySegmentHandleResponse.
func handleXMLResponse(resp *http.Response) (container.ListBlobsHierarchyResponse, error) {
result := container.ListBlobsHierarchyResponse{}
extractResponseHeaders(resp, &result.ClientRequestID, &result.ContentType, &result.Date, &result.RequestID, &result.Version)
if err := runtime.UnmarshalAsXML(resp, &result.ListBlobsHierarchySegmentResponse); err != nil {
return container.ListBlobsHierarchyResponse{}, err
}
return result, nil
}
// formatInclude renders the include datasets as the comma separated query
// parameter value, in the same order as the SDK's unexported
// ListBlobsInclude.format.
func formatInclude(l container.ListBlobsInclude) string {
var include []string
if l.Copy {
include = append(include, "copy")
}
if l.Deleted {
include = append(include, "deleted")
}
if l.DeletedWithVersions {
include = append(include, "deletedwithversions")
}
if l.ImmutabilityPolicy {
include = append(include, "immutabilitypolicy")
}
if l.LegalHold {
include = append(include, "legalhold")
}
if l.Metadata {
include = append(include, "metadata")
}
if l.Snapshots {
include = append(include, "snapshots")
}
if l.Tags {
include = append(include, "tags")
}
if l.UncommittedBlobs {
include = append(include, "uncommittedblobs")
}
if l.Versions {
include = append(include, "versions")
}
if l.Permissions {
include = append(include, "permissions")
}
return strings.Join(include, ",")
}
@@ -1,199 +0,0 @@
//go:build !plan9 && !solaris && !js
package arrowlist
import (
"context"
"encoding/base64"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
"github.com/apache/arrow-go/v18/arrow"
arrowArray "github.com/apache/arrow-go/v18/arrow/array"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// arrowPage builds a one row Arrow IPC listing page with the given blob name
// and NextMarker.
func arrowPage(t *testing.T, name, nextMarker string) []byte {
md := arrow.NewMetadata([]string{"NextMarker", "NumberOfRecords"}, []string{nextMarker, "1"})
fields := []arrow.Field{
{Name: "Name", Type: arrow.BinaryTypes.String, Nullable: false},
{Name: "Content-Length", Type: arrow.PrimitiveTypes.Uint64, Nullable: true},
}
schema := arrow.NewSchema(fields, &md)
return buildArrowStream(t, schema, func(b *arrowArray.RecordBuilder) {
b.Field(0).(*arrowArray.StringBuilder).Append(name)
b.Field(1).(*arrowArray.Uint64Builder).Append(42)
})
}
func TestPagerArrow(t *testing.T) {
var requests []*http.Request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests = append(requests, r.Clone(context.Background()))
w.Header().Set("Content-Type", ArrowContentType)
if r.URL.Query().Get("marker") == "" {
_, _ = w.Write(arrowPage(t, "blob1.txt", "marker1"))
} else {
_, _ = w.Write(arrowPage(t, "blob2.txt", ""))
}
}))
defer srv.Close()
// SAS style query on the endpoint must be preserved in requests.
client, err := NewClientWithNoCredential(srv.URL+"/testcontainer?sv=fakesas", nil)
require.NoError(t, err)
opts := &ListBlobsHierarchyOptions{
ListBlobsHierarchyOptions: container.ListBlobsHierarchyOptions{
Include: container.ListBlobsInclude{Metadata: true, Tags: true},
Prefix: to.Ptr("dir/"),
MaxResults: to.Ptr(int32(1000)),
StartFrom: to.Ptr("testcontainer/dir/a"),
},
EndBefore: to.Ptr("testcontainer/dir/n"),
UseArrowFormat: to.Ptr(true),
}
pager := client.NewListBlobsHierarchyPager("/", opts)
var names []string
for pager.More() {
page, err := pager.NextPage(context.Background())
require.NoError(t, err)
assert.Equal(t, ArrowContentType, *page.ContentType)
for _, item := range page.Segment.BlobItems {
names = append(names, *item.Name)
assert.Equal(t, int64(42), *item.Properties.ContentLength)
}
}
assert.Equal(t, []string{"blob1.txt", "blob2.txt"}, names)
require.Len(t, requests, 2)
q := requests[0].URL.Query()
assert.Equal(t, "list", q.Get("comp"))
assert.Equal(t, "container", q.Get("restype"))
assert.Equal(t, "/", q.Get("delimiter"))
assert.Equal(t, "metadata,tags", q.Get("include"))
assert.Equal(t, "dir/", q.Get("prefix"))
assert.Equal(t, "1000", q.Get("maxresults"))
assert.Equal(t, "testcontainer/dir/a", q.Get("startFrom"))
assert.Equal(t, "testcontainer/dir/n", q.Get("endBefore"))
assert.Equal(t, "fakesas", q.Get("sv"))
assert.Empty(t, q.Get("marker"))
assert.Equal(t, ArrowAcceptHeader, requests[0].Header.Get("Accept"))
assert.Equal(t, serviceVersion, requests[0].Header.Get("x-ms-version"))
assert.Equal(t, "marker1", requests[1].URL.Query().Get("marker"))
// The caller's options must not have been mutated by paging.
assert.Nil(t, opts.Marker)
}
const xmlListResponse = `<?xml version="1.0" encoding="utf-8"?>
<EnumerationResults ServiceEndpoint="https://example.com/" ContainerName="testcontainer">
<Blobs>
<Blob>
<Name>file.txt</Name>
<Properties>
<Last-Modified>Mon, 01 Jan 2024 00:00:00 GMT</Last-Modified>
<Etag>0x8D1</Etag>
<Content-Length>7</Content-Length>
<BlobType>BlockBlob</BlobType>
</Properties>
</Blob>
<BlobPrefix>
<Name>subdir/</Name>
</BlobPrefix>
</Blobs>
<NextMarker />
</EnumerationResults>`
func TestPagerXMLFallback(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
_, _ = w.Write([]byte(xmlListResponse))
}))
defer srv.Close()
client, err := NewClientWithNoCredential(srv.URL+"/testcontainer", nil)
require.NoError(t, err)
pager := client.NewListBlobsHierarchyPager("/", &ListBlobsHierarchyOptions{
UseArrowFormat: to.Ptr(true),
})
page, err := pager.NextPage(context.Background())
require.NoError(t, err)
assert.False(t, pager.More())
assert.Equal(t, "application/xml", *page.ContentType)
require.Len(t, page.Segment.BlobItems, 1)
assert.Equal(t, "file.txt", *page.Segment.BlobItems[0].Name)
assert.Equal(t, int64(7), *page.Segment.BlobItems[0].Properties.ContentLength)
require.Len(t, page.Segment.BlobPrefixes, 1)
assert.Equal(t, "subdir/", *page.Segment.BlobPrefixes[0].Name)
}
func TestPagerXMLFallbackWithEndBefore(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
_, _ = w.Write([]byte(xmlListResponse))
}))
defer srv.Close()
client, err := NewClientWithNoCredential(srv.URL+"/testcontainer", nil)
require.NoError(t, err)
pager := client.NewListBlobsHierarchyPager("/", &ListBlobsHierarchyOptions{
EndBefore: to.Ptr("testcontainer/n"),
UseArrowFormat: to.Ptr(true),
})
_, err = pager.NextPage(context.Background())
require.ErrorIs(t, err, ErrEndBeforeXMLFallback)
}
func TestPagerSharedKeyAuth(t *testing.T) {
var gotAuth, gotDate string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotAuth = r.Header.Get("Authorization")
gotDate = r.Header.Get("x-ms-date")
w.Header().Set("Content-Type", ArrowContentType)
_, _ = w.Write(arrowPage(t, "blob.txt", ""))
}))
defer srv.Close()
cred, err := NewSharedKeyCredential("testaccount", base64.StdEncoding.EncodeToString([]byte("testkey")))
require.NoError(t, err)
client, err := NewClientWithSharedKeyCredential(srv.URL+"/testcontainer", cred, nil)
require.NoError(t, err)
pager := client.NewListBlobsHierarchyPager("/", &ListBlobsHierarchyOptions{
UseArrowFormat: to.Ptr(true),
})
_, err = pager.NextPage(context.Background())
require.NoError(t, err)
assert.True(t, strings.HasPrefix(gotAuth, "SharedKey testaccount:"), "Authorization = %q", gotAuth)
assert.NotEmpty(t, gotDate)
}
func TestPagerResponseError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("x-ms-error-code", string(bloberror.ContainerNotFound))
w.WriteHeader(http.StatusNotFound)
}))
defer srv.Close()
client, err := NewClientWithNoCredential(srv.URL+"/testcontainer", nil)
require.NoError(t, err)
pager := client.NewListBlobsHierarchyPager("/", &ListBlobsHierarchyOptions{
UseArrowFormat: to.Ptr(true),
})
_, err = pager.NextPage(context.Background())
require.Error(t, err)
assert.True(t, bloberror.HasCode(err, bloberror.ContainerNotFound), "expected ContainerNotFound, got %v", err)
}
@@ -1,121 +0,0 @@
//go:build !plan9 && !solaris && !js
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//
// Adapted from internal/shared/challenge_policy.go in the Azure SDK for Go
// github.com/Azure/azure-sdk-for-go/sdk/storage/azblob at v1.8.0. The
// released SDK does not export its storage bearer-challenge policy, which is
// needed for token authentication with the same semantics as the SDK's own
// clients.
package arrowlist
import (
"errors"
"net/http"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
)
type storageAuthorizer struct {
scopes []string
tenantID string
}
// NewStorageChallengePolicy returns a bearer token policy for cred which
// handles the storage service's authentication challenges.
func NewStorageChallengePolicy(cred azcore.TokenCredential, audience string, allowHTTP bool) policy.Policy {
s := storageAuthorizer{scopes: []string{audience}}
return runtime.NewBearerTokenPolicy(cred, []string{audience}, &policy.BearerTokenOptions{
AuthorizationHandler: policy.AuthorizationHandler{
OnRequest: s.onRequest,
OnChallenge: s.onChallenge,
},
InsecureAllowCredentialWithHTTP: allowHTTP,
})
}
func (s *storageAuthorizer) onRequest(req *policy.Request, authNZ func(policy.TokenRequestOptions) error) error {
return authNZ(policy.TokenRequestOptions{Scopes: s.scopes})
}
func (s *storageAuthorizer) onChallenge(req *policy.Request, resp *http.Response, authNZ func(policy.TokenRequestOptions) error) error {
// parse the challenge
err := s.parseChallenge(resp)
if err != nil {
return err
}
// TODO: Set tenantID when policy.TokenRequestOptions supports it. https://github.com/Azure/azure-sdk-for-go/issues/19841
return authNZ(policy.TokenRequestOptions{Scopes: s.scopes})
}
type challengePolicyError struct {
err error
}
func (c *challengePolicyError) Error() string {
return c.err.Error()
}
func (*challengePolicyError) NonRetriable() {
// marker method
}
func (c *challengePolicyError) Unwrap() error {
return c.err
}
// parses Tenant ID from auth challenge
// https://login.microsoftonline.com/00000000-0000-0000-0000-000000000000/oauth2/authorize
func parseTenant(url string) string {
if url == "" {
return ""
}
parts := strings.Split(url, "/")
if len(parts) >= 3 {
tenant := parts[3]
tenant = strings.ReplaceAll(tenant, ",", "")
return tenant
}
return ""
}
func (s *storageAuthorizer) parseChallenge(resp *http.Response) error {
authHeader := resp.Header.Get("WWW-Authenticate")
if authHeader == "" {
return &challengePolicyError{err: errors.New("response has no WWW-Authenticate header for challenge authentication")}
}
// Strip down to auth and resource
// Format is "Bearer authorization_uri=\"<site>\" resource_id=\"<site>\""
authHeader = strings.ReplaceAll(authHeader, "Bearer ", "")
parts := strings.Split(authHeader, " ")
vals := map[string]string{}
for _, part := range parts {
subParts := strings.Split(part, "=")
if len(subParts) == 2 {
stripped := strings.ReplaceAll(subParts[1], "\"", "")
stripped = strings.TrimSuffix(stripped, ",")
vals[subParts[0]] = stripped
}
}
s.tenantID = parseTenant(vals["authorization_uri"])
scope := vals["resource_id"]
if scope == "" {
return &challengePolicyError{err: errors.New("could not find a valid resource in the WWW-Authenticate header")}
}
if !strings.HasSuffix(scope, "/.default") {
scope += "/.default"
}
s.scopes = []string{scope}
return nil
}
-318
View File
@@ -1,318 +0,0 @@
//go:build !plan9 && !solaris && !js
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//
// Adapted from internal/exported/shared_key_credential.go in the Azure SDK
// for Go github.com/Azure/azure-sdk-for-go/sdk/storage/azblob at v1.8.0,
// with the internal header constants inlined. The released SDK does not
// export its shared key signing policy, so a copy is needed to sign the
// Arrow listing requests this package sends through its own pipeline.
package arrowlist
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"
"net/url"
"sort"
"strings"
"sync/atomic"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/rclone/rclone/fs"
)
// NewSharedKeyCredential creates an immutable SharedKeyCredential containing the
// storage account's name and either its primary or secondary key.
func NewSharedKeyCredential(accountName string, accountKey string) (*SharedKeyCredential, error) {
c := SharedKeyCredential{accountName: accountName}
if err := c.SetAccountKey(accountKey); err != nil {
return nil, err
}
return &c, nil
}
// SharedKeyCredential contains an account's name and its primary or secondary key.
type SharedKeyCredential struct {
// Only the NewSharedKeyCredential method should set these; all other methods should treat them as read-only
accountName string
accountKey atomic.Value // []byte
}
// AccountName returns the Storage account's name.
func (c *SharedKeyCredential) AccountName() string {
return c.accountName
}
// SetAccountKey replaces the existing account key with the specified account key.
func (c *SharedKeyCredential) SetAccountKey(accountKey string) error {
_bytes, err := base64.StdEncoding.DecodeString(accountKey)
if err != nil {
return fmt.Errorf("decode account key: %w", err)
}
c.accountKey.Store(_bytes)
return nil
}
// ComputeHMACSHA256 generates a hash signature for an HTTP request or for a SAS.
func (c *SharedKeyCredential) computeHMACSHA256(message string) (string, error) {
h := hmac.New(sha256.New, c.accountKey.Load().([]byte))
_, err := h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil)), err
}
func (c *SharedKeyCredential) buildStringToSign(req *http.Request) (string, error) {
// https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services
headers := req.Header
contentLength := getHeader("Content-Length", headers)
if contentLength == "0" {
contentLength = ""
}
canonicalizedResource, err := c.buildCanonicalizedResource(req.URL)
if err != nil {
return "", err
}
stringToSign := strings.Join([]string{
req.Method,
getHeader("Content-Encoding", headers),
getHeader("Content-Language", headers),
contentLength,
getHeader("Content-MD5", headers),
getHeader("Content-Type", headers),
"", // Empty date because x-ms-date is expected (as per web page above)
getHeader("If-Modified-Since", headers),
getHeader("If-Match", headers),
getHeader("If-None-Match", headers),
getHeader("If-Unmodified-Since", headers),
getHeader("Range", headers),
c.buildCanonicalizedHeader(headers),
canonicalizedResource,
}, "\n")
return stringToSign, nil
}
func getHeader(key string, headers map[string][]string) string {
if headers == nil {
return ""
}
if v, ok := headers[key]; ok {
if len(v) > 0 {
return v[0]
}
}
return ""
}
func getWeightTables() [][]int {
tableLv0 := [...]int{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x71c, 0x0, 0x71f, 0x721, 0x723, 0x725,
0x0, 0x0, 0x0, 0x72d, 0x803, 0x0, 0x0, 0x733, 0x0, 0xd03, 0xd1a, 0xd1c, 0xd1e,
0xd20, 0xd22, 0xd24, 0xd26, 0xd28, 0xd2a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0xe02, 0xe09, 0xe0a, 0xe1a, 0xe21, 0xe23, 0xe25, 0xe2c, 0xe32, 0xe35, 0xe36, 0xe48, 0xe51,
0xe70, 0xe7c, 0xe7e, 0xe89, 0xe8a, 0xe91, 0xe99, 0xe9f, 0xea2, 0xea4, 0xea6, 0xea7, 0xea9,
0x0, 0x0, 0x0, 0x743, 0x744, 0x748, 0xe02, 0xe09, 0xe0a, 0xe1a, 0xe21, 0xe23, 0xe25,
0xe2c, 0xe32, 0xe35, 0xe36, 0xe48, 0xe51, 0xe70, 0xe7c, 0xe7e, 0xe89, 0xe8a, 0xe91, 0xe99,
0xe9f, 0xea2, 0xea4, 0xea6, 0xea7, 0xea9, 0x0, 0x74c, 0x0, 0x750, 0x0,
}
tableLv2 := [...]int{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8012, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8212, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
}
tables := [][]int{tableLv0[:], tableLv2[:]}
return tables
}
// NewHeaderStringComparer performs a multi-level, weight-based comparison of two strings
func compareHeaders(lhs, rhs string, tables [][]int) int {
currLevel, i, j := 0, 0, 0
n := len(tables)
lhsLen := len(lhs)
rhsLen := len(rhs)
for currLevel < n {
if currLevel == (n-1) && i != j {
if i > j {
return -1
}
if i < j {
return 1
}
return 0
}
var w1, w2 int
// Check bounds before accessing lhs[i]
if i < lhsLen {
w1 = tables[currLevel][lhs[i]]
} else {
w1 = 0x1
}
// Check bounds before accessing rhs[j]
if j < rhsLen {
w2 = tables[currLevel][rhs[j]]
} else {
w2 = 0x1
}
if w1 == 0x1 && w2 == 0x1 {
i = 0
j = 0
currLevel++
} else if w1 == w2 {
i++
j++
} else if w1 == 0 {
i++
} else if w2 == 0 {
j++
} else {
if w1 < w2 {
return -1
}
if w1 > w2 {
return 1
}
return 0
}
}
return 0
}
func (c *SharedKeyCredential) buildCanonicalizedHeader(headers http.Header) string {
cm := map[string][]string{}
for k, v := range headers {
headerName := strings.TrimSpace(strings.ToLower(k))
if strings.HasPrefix(headerName, "x-ms-") {
cm[headerName] = v // NOTE: the value must not have any whitespace around it.
}
}
if len(cm) == 0 {
return ""
}
keys := make([]string, 0, len(cm))
for key := range cm {
keys = append(keys, key)
}
tables := getWeightTables()
// Sort the keys using the custom comparator
sort.Slice(keys, func(i, j int) bool {
return compareHeaders(keys[i], keys[j], tables) < 0
})
ch := bytes.NewBufferString("")
for i, key := range keys {
if i > 0 {
ch.WriteRune('\n')
}
ch.WriteString(key)
ch.WriteRune(':')
ch.WriteString(strings.Join(cm[key], ","))
}
return ch.String()
}
func (c *SharedKeyCredential) buildCanonicalizedResource(u *url.URL) (string, error) {
// https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services
cr := bytes.NewBufferString("/")
cr.WriteString(c.accountName)
if len(u.Path) > 0 {
// Any portion of the CanonicalizedResource string that is derived from
// the resource's URI should be encoded exactly as it is in the URI.
// -- https://msdn.microsoft.com/en-gb/library/azure/dd179428.aspx
cr.WriteString(u.EscapedPath())
} else {
// a slash is required to indicate the root path
cr.WriteString("/")
}
// params is a map[string][]string; param name is key; params values is []string
params, err := url.ParseQuery(u.RawQuery) // Returns URL decoded values
if err != nil {
return "", fmt.Errorf("failed to parse query params: %w", err)
}
if len(params) > 0 { // There is at least 1 query parameter
var paramNames []string // We use this to sort the parameter key names
for paramName := range params {
paramNames = append(paramNames, paramName) // paramNames must be lowercase
}
sort.Strings(paramNames)
for _, paramName := range paramNames {
paramValues := params[paramName]
sort.Strings(paramValues)
// Join the sorted key values separated by ','
// Then prepend "keyName:"; then add this string to the buffer
cr.WriteString("\n" + strings.ToLower(paramName) + ":" + strings.Join(paramValues, ","))
}
}
return cr.String(), nil
}
// ComputeHMACSHA256 is a helper for computing the signed string outside of this package.
func ComputeHMACSHA256(cred *SharedKeyCredential, message string) (string, error) {
return cred.computeHMACSHA256(message)
}
// SharedKeyCredPolicy is a pipeline policy that signs each request with the
// account's shared key.
type SharedKeyCredPolicy struct {
cred *SharedKeyCredential
}
// NewSharedKeyCredPolicy creates a SharedKeyCredPolicy signing with cred.
func NewSharedKeyCredPolicy(cred *SharedKeyCredential) *SharedKeyCredPolicy {
return &SharedKeyCredPolicy{cred: cred}
}
// Do signs the request and sends it via the next policy in the pipeline.
func (s *SharedKeyCredPolicy) Do(req *policy.Request) (*http.Response, error) {
// skip adding the authorization header if no SharedKeyCredential was provided.
// this prevents a panic that might be hard to diagnose and allows testing
// against http endpoints that don't require authentication.
if s.cred == nil {
return req.Next()
}
if d := getHeader("x-ms-date", req.Raw().Header); d == "" {
req.Raw().Header.Set("x-ms-date", time.Now().UTC().Format(http.TimeFormat))
}
stringToSign, err := s.cred.buildStringToSign(req.Raw())
if err != nil {
return nil, err
}
signature, err := s.cred.computeHMACSHA256(stringToSign)
if err != nil {
return nil, err
}
authHeader := strings.Join([]string{"SharedKey ", s.cred.AccountName(), ":", signature}, "")
req.Raw().Header.Set("Authorization", authHeader)
response, err := req.Next()
if err != nil && response != nil && response.StatusCode == http.StatusForbidden {
// Service failed to authenticate request, log it
fs.Debugf(nil, "azureblob arrowlist: HTTP Forbidden status, String-to-Sign: %q", stringToSign)
}
return response, err
}
+49 -120
View File
@@ -26,15 +26,12 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
"github.com/rclone/rclone/backend/azureblob/arrowlist"
"github.com/rclone/rclone/backend/azureblob/auth"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/chunksize"
@@ -242,40 +239,38 @@ avoid the time out.`,
Advanced: true,
}, {
Name: "use_arrow_list",
Help: `Use the experimental Apache Arrow listing format.
Help: `Use the Apache Arrow listing format.
If set, directory listings are fetched using the ListBlobs Apache
Arrow response format instead of XML. This can be faster for very
large containers.
Arrow response format instead of XML. Arrow responses are smaller and
much cheaper to parse, making listings of large containers several
times faster. Combine with "list_parallelism" for the biggest gains.
This is EXPERIMENTAL and requires the "Blob Listing with Apache Arrow"
preview feature to be enabled on the storage account. It is NOT
supported on accounts with a hierarchical namespace (ADLS Gen2) -
those return a 409 error. If the feature is not enabled the server
returns XML and the listing transparently falls back to the normal XML
path (logged at debug level).
Not supported with connection_string auth - falls back to normal
listing.`,
"Blob Listing with Apache Arrow" is in public preview at Microsoft and
is only supported on flat namespace accounts. On accounts with a
hierarchical namespace (ADLS Gen2), or where the feature is otherwise
unavailable, the server returns XML and the listing transparently
falls back to the normal XML path (logged at debug level).`,
Default: false,
Advanced: true,
Hide: fs.OptionHideBoth,
}, {
Name: "list_parallelism",
Help: `Number of parallel shards to list a directory with.
EXPERIMENTAL. If set greater than 1, the blob name keyspace of each
directory is split into this many ranges which are listed concurrently
using the Arrow startFrom/endBefore range parameters. This can
dramatically speed up listing containers with millions of objects, for
both recursive (ListR) and single directory listings.
If set greater than 1, the blob name keyspace of each directory is
split into ranges which are listed concurrently using the Arrow
startFrom/endBefore range parameters. This can dramatically speed up
listing containers with millions of objects, for both recursive
(ListR) and single directory listings. Speed keeps improving up to a
parallelism of around 30.
This has no effect unless "use_arrow_list" is also set, as Arrow is the
only listing path that supports server-side name ranges. The default of
0 (or 1) lists sequentially.`,
only listing path that supports server-side name ranges. If the
account does not support range listing (e.g. it has a hierarchical
namespace) the listing falls back to sequential. The default of 0 (or
1) lists sequentially.`,
Default: 0,
Advanced: true,
Hide: fs.OptionHideBoth,
}, {
Name: "access_tier",
Help: `Access tier of blob: hot, cool, cold or archive.
@@ -450,10 +445,8 @@ type Fs struct {
opt Options // parsed config options
ci *fs.ConfigInfo // global config
features *fs.Features // optional features
cntSVCcacheMu sync.Mutex // mutex to protect cntSVCcache and arrowCntSVCcache
cntSVCcacheMu sync.Mutex // mutex to protect cntSVCcache
cntSVCcache map[string]*container.Client // reference to containerClient per container
arrowCntSVCcache map[string]*arrowlist.Client // reference to arrowlist client per container
arrowClientOpts *arrowlist.ClientOptions // client options for the arrowlist clients
arrowXMLFallback atomic.Bool // set once the server answers XML so parallel listing is not retried
svc *service.Client // client to access azblob
cred azcore.TokenCredential // how to generate tokens (may be nil)
@@ -672,15 +665,14 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
ci := fs.GetConfig(ctx)
f := &Fs{
name: name,
opt: *opt,
ci: ci,
pacer: fs.NewPacer(ctx, pacer.NewS3(pacer.MinSleep(minSleep), pacer.MaxSleep(maxSleep), pacer.DecayConstant(decayConstant))),
uploadToken: pacer.NewTokenDispenser(ci.Transfers),
copyToken: pacer.NewTokenDispenser(opt.CopyTotalConcurrency),
cache: bucket.NewCache(),
cntSVCcache: make(map[string]*container.Client, 1),
arrowCntSVCcache: make(map[string]*arrowlist.Client, 1),
name: name,
opt: *opt,
ci: ci,
pacer: fs.NewPacer(ctx, pacer.NewS3(pacer.MinSleep(minSleep), pacer.MaxSleep(maxSleep), pacer.DecayConstant(decayConstant))),
uploadToken: pacer.NewTokenDispenser(ci.Transfers),
copyToken: pacer.NewTokenDispenser(opt.CopyTotalConcurrency),
cache: bucket.NewCache(),
cntSVCcache: make(map[string]*container.Client, 1),
}
f.publicAccess = container.PublicAccessType(opt.PublicAccess)
f.setRoot(root)
@@ -727,15 +719,6 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
f.usingSharedKeyCred = res.UsingSharedKeyCred
f.anonymous = res.Anonymous
// Client options for the arrowlist clients, using the same transport and
// gzip policy as the SDK clients built above.
f.arrowClientOpts = &arrowlist.ClientOptions{
ClientOptions: azcore.ClientOptions{
Transport: auth.Transporter(ctx),
PerCallPolicies: []policy.Policy{setAcceptEncodingGzip{}},
},
}
// if using Container level SAS put the container client into the cache
if opt.SASURL != "" && res.Container != "" {
_ = f.cntSVC(res.Container)
@@ -774,44 +757,6 @@ func (f *Fs) cntSVC(containerName string) (containerClient *container.Client) {
return containerClient
}
// errArrowAuthUnsupported is returned by arrowCntSVC when the configured
// credentials can't be used for Arrow listing.
var errArrowAuthUnsupported = errors.New("credentials not supported for Arrow listing (connection_string auth is not supported)")
// return the arrowlist client for the container passed in
//
// Returns errArrowAuthUnsupported if the configured credentials can't be
// reused for the arrowlist client's pipeline.
func (f *Fs) arrowCntSVC(containerName string) (client *arrowlist.Client, err error) {
// The container URL includes any SAS token in its query
url := f.cntSVC(containerName).URL()
f.cntSVCcacheMu.Lock()
defer f.cntSVCcacheMu.Unlock()
if client, ok := f.arrowCntSVCcache[containerName]; ok {
return client, nil
}
switch {
case f.usingSharedKeyCred:
// Covers account+key and the emulator (auth fills in Account/Key)
var cred *arrowlist.SharedKeyCredential
cred, err = arrowlist.NewSharedKeyCredential(f.opt.Account, f.opt.Key)
if err == nil {
client, err = arrowlist.NewClientWithSharedKeyCredential(url, cred, f.arrowClientOpts)
}
case f.cred != nil:
client, err = arrowlist.NewClient(url, f.cred, f.arrowClientOpts)
case f.anonymous || f.opt.SASURL != "":
client, err = arrowlist.NewClientWithNoCredential(url, f.arrowClientOpts)
default:
return nil, errArrowAuthUnsupported
}
if err != nil {
return nil, err
}
f.arrowCntSVCcache[containerName] = client
return client, nil
}
// Return an Object from a path
//
// If it can't be found it returns the error fs.ErrorObjectNotFound.
@@ -1188,27 +1133,25 @@ func (f *Fs) list(ctx context.Context, containerName, directory, prefix string,
delimiter = "/"
}
opts := &arrowlist.ListBlobsHierarchyOptions{
ListBlobsHierarchyOptions: container.ListBlobsHierarchyOptions{
// Copy, Metadata, Snapshots, UncommittedBlobs, Deleted, Tags, Versions, LegalHold, ImmutabilityPolicy, DeletedWithVersions bool
Include: container.ListBlobsInclude{
Copy: false,
Metadata: true,
Snapshots: false,
UncommittedBlobs: false,
Deleted: false,
},
Prefix: &directory,
MaxResults: &maxResults,
opts := &container.ListBlobsHierarchyOptions{
// Copy, Metadata, Snapshots, UncommittedBlobs, Deleted, Tags, Versions, LegalHold, ImmutabilityPolicy, DeletedWithVersions bool
Include: container.ListBlobsInclude{
Copy: false,
Metadata: true,
Snapshots: false,
UncommittedBlobs: false,
Deleted: false,
},
Prefix: &directory,
MaxResults: &maxResults,
}
// Experimental: request the Apache Arrow listing format. The arrowlist
// pager requests an Arrow IPC stream and decodes it, falling back to XML
// if the account doesn't have Arrow listing enabled. Skip the
// maxResults==1 probe (isEmpty) which doesn't benefit.
// Request the Apache Arrow listing format. The SDK pager requests an
// Arrow IPC stream and decodes it, falling back to XML if the account
// doesn't have Arrow listing enabled. Skip the maxResults==1 probe
// (isEmpty) which doesn't benefit.
useArrow := f.opt.UseArrowList && maxResults != 1
if useArrow {
opts.UseArrowFormat = to.Ptr(true)
opts.ResponseFormat = container.StorageResponseFormatArrow
}
var foundItems int
@@ -1239,25 +1182,11 @@ func (f *Fs) list(ctx context.Context, containerName, directory, prefix string,
// listBlobsPager runs the hierarchy listing described by opts, calling fn for
// each blob and subdirectory, and returns the number of raw items seen.
// delimiter selects flat (recurse) vs hierarchical listing. If opts requests
// the Apache Arrow format the listing goes through the arrowlist pager
// (falling back to the SDK pager if the credentials don't support it); if the
// service then answers with XML (Arrow listing not enabled) a debug message
// is logged.
func (f *Fs) listBlobsPager(ctx context.Context, containerName, directory, prefix string, addContainer bool, opts *arrowlist.ListBlobsHierarchyOptions, delimiter string, fn listFn) (foundItems int, err error) {
useArrow := opts.UseArrowFormat != nil && *opts.UseArrowFormat
var pager *runtime.Pager[container.ListBlobsHierarchyResponse]
if useArrow {
arrowSVC, err := f.arrowCntSVC(containerName)
if err != nil {
fs.Debugf(f, "Not using Arrow listing: %v", err)
useArrow = false
} else {
pager = arrowSVC.NewListBlobsHierarchyPager(delimiter, opts)
}
}
if pager == nil {
pager = f.cntSVC(containerName).NewListBlobsHierarchyPager(delimiter, &opts.ListBlobsHierarchyOptions)
}
// the Apache Arrow format and the service answers with XML (Arrow listing
// not enabled) a debug message is logged.
func (f *Fs) listBlobsPager(ctx context.Context, containerName, directory, prefix string, addContainer bool, opts *container.ListBlobsHierarchyOptions, delimiter string, fn listFn) (foundItems int, err error) {
useArrow := opts.ResponseFormat == container.StorageResponseFormatArrow
pager := f.cntSVC(containerName).NewListBlobsHierarchyPager(delimiter, opts)
checkedArrow := false
for pager.More() {
var response container.ListBlobsHierarchyResponse
@@ -1273,7 +1202,7 @@ func (f *Fs) listBlobsPager(ctx context.Context, containerName, directory, prefi
// but not accelerated.
if useArrow && !checkedArrow && err == nil {
checkedArrow = true
if response.ContentType == nil || !strings.HasPrefix(*response.ContentType, arrowlist.ArrowContentType) {
if response.ContentType == nil || !strings.HasPrefix(*response.ContentType, arrowContentType) {
fs.Debugf(f, "Apache Arrow listing requested but server returned XML - Blob Listing with Apache Arrow may not be enabled on this account")
}
}
+25 -1
View File
@@ -191,6 +191,11 @@ If the custom endpoint rewrites the requests for authentication,
e.g., in Cloudflare Workers, this header needs to be handled properly.
Leave blank if you want to use the endpoint provided by Backblaze.
Rclone will not follow a redirect from this endpoint which downgrades
HTTPS to HTTP, and will not send the "Authorization" header or the
SSE-C key headers once a redirect has left the host and port of this
endpoint.
The URL provided here SHOULD have the protocol and SHOULD NOT have
a trailing slash or specify the /file/bucket subpath as rclone will
request files with "{download_url}/file/{bucket_name}/{path}".
@@ -529,6 +534,25 @@ func (f *Fs) setRoot(root string) {
f.rootBucket, f.rootDirectory = bucket.Split(f.root)
}
// redirectSecretHeaders are the request headers carrying secrets that
// must not be forwarded when a redirect leaves the original host.
var redirectSecretHeaders = []string{
"Authorization",
sseAlgorithmHeader,
sseKeyHeader,
sseMd5Header,
}
// newClient makes the http client used for all B2 requests.
//
// The download_url option sends the requests to a host the user does
// not control so it strips headers on cross host redirects.
func newClient(ctx context.Context) *http.Client {
client := fshttp.NewClient(ctx)
client.CheckRedirect = rest.StripHeadersOnCrossHostRedirectFn(redirectSecretHeaders...)
return client
}
// NewFs constructs an Fs from the path, bucket:path
func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, error) {
// Parse config into Options struct
@@ -581,7 +605,7 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
name: name,
opt: *opt,
ci: ci,
srv: rest.NewClient(fshttp.NewClient(ctx)).SetErrorHandler(errorHandler),
srv: rest.NewClient(newClient(ctx)).SetErrorHandler(errorHandler),
cache: bucket.NewCache(),
_bucketID: make(map[string]string, 1),
_bucketType: make(map[string]string, 1),
+59
View File
@@ -4,6 +4,8 @@ import (
"context"
"crypto/sha1"
"fmt"
"net/http"
"net/http/httptest"
"path"
"sort"
"strings"
@@ -617,3 +619,60 @@ func (f *Fs) InternalTest(t *testing.T) {
}
var _ fstests.InternalTester = (*Fs)(nil)
// redirectTestHeaders is a deliberately literal copy of
// redirectSecretHeaders so that a header dropped from the production
// list fails TestRedirectSecretHeaders rather than silently losing
// test coverage.
var redirectTestHeaders = map[string]string{
"Authorization": "secret-token",
"X-Bz-Server-Side-Encryption-Customer-Algorithm": "AES256",
"X-Bz-Server-Side-Encryption-Customer-Key": "secret-key",
"X-Bz-Server-Side-Encryption-Customer-Key-Md5": "secret-md5",
}
func TestRedirectSecretHeaders(t *testing.T) {
want := make([]string, 0, len(redirectTestHeaders))
for header := range redirectTestHeaders {
want = append(want, header)
}
sort.Strings(want)
got := append([]string(nil), redirectSecretHeaders...)
sort.Strings(got)
assert.Equal(t, want, got)
}
// redirectTestRequest makes a GET request to rawURL carrying the
// secret headers plus a harmless marker header.
func redirectTestRequest(t *testing.T, rawURL string) *http.Request {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, rawURL, nil)
require.NoError(t, err)
for header, value := range redirectTestHeaders {
req.Header.Set(header, value)
}
req.Header.Set("X-Bz-Test-Mode", "marker")
return req
}
func TestClientStripsSecretHeadersOnCrossHostRedirect(t *testing.T) {
client := newClient(context.Background())
redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for header := range redirectTestHeaders {
assert.Empty(t, r.Header.Get(header), "%s should have been stripped", header)
}
assert.Equal(t, "marker", r.Header.Get("X-Bz-Test-Mode"))
w.WriteHeader(http.StatusOK)
}))
defer redirectServer.Close()
initialServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, redirectServer.URL, http.StatusTemporaryRedirect)
}))
defer initialServer.Close()
resp, err := client.Do(redirectTestRequest(t, initialServer.URL))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.NoError(t, resp.Body.Close())
}
+1 -1
View File
@@ -1461,7 +1461,7 @@ func (f *Fs) changeNotifyRunner(ctx context.Context, notifyFunc func(string, fs.
// Item in the cache has the same or newer SequenceID than
// this event. Ignore this event, it must be old.
f.itemMetaCacheMu.Unlock()
fs.Debugf(f, "%s ignored due to old SequenceID (%q)", eventDetails, itemMeta.SequenceID)
fs.Debugf(f, "%s ignored due to old SequenceID (%d)", eventDetails, itemMeta.SequenceID)
continue
}
+4 -1
View File
@@ -801,9 +801,12 @@ func (f *Fs) processEntries(ctx context.Context, origEntries fs.DirEntries, dirP
fs.Debugf(f, "skip orphan data chunk %q", remote)
break
}
if mainObject == nil && !f.useMeta {
if mainObject == nil && !f.useMeta && !isSpecial {
// this is the "nometa" case
// create dummy chunked object without metadata
//
// not for temporary or control chunks as they may be all
// that is left of an interrupted upload
mainObject = f.newObject(mainRemote, nil, nil)
byRemote[mainRemote] = mainObject
if !badEntry[mainRemote] {
+19
View File
@@ -627,6 +627,22 @@ func testChunkNumberOverflow(t *testing.T, f *Fs) {
_ = file.Remove(ctx)
}
// testOrphanTempChunks checks that temporary chunks left behind by an
// interrupted upload are not listed and don't break the listing
func testOrphanTempChunks(t *testing.T, f *Fs) {
const dir = "orphan"
ctx := context.Background()
defer func() {
_ = operations.Purge(ctx, f.base, dir)
}()
modTime := fstest.Time("2001-02-03T04:05:06.499999999Z")
item := fstest.Item{Path: f.makeChunkName(path.Join(dir, "interrupted"), 0, "", "abc123"), ModTime: modTime}
fstests.PutTestContents(ctx, t, f.base, &item, "data", true)
fstest.CheckListingWithRoot(t, f, dir, nil, nil, f.Precision())
}
func testMetadataInput(t *testing.T, f *Fs) {
const minChunkForTest = 50
if f.opt.ChunkSize < minChunkForTest {
@@ -928,6 +944,9 @@ func (f *Fs) InternalTest(t *testing.T) {
t.Run("BackwardsCompatibility", func(t *testing.T) {
testBackwardsCompatibility(t, f)
})
t.Run("OrphanTempChunks", func(t *testing.T) {
testOrphanTempChunks(t, f)
})
t.Run("ChunkerServerSideMove", func(t *testing.T) {
testChunkerServerSideMove(t, f)
})
+32
View File
@@ -1080,6 +1080,35 @@ type ObjectMetadata struct {
CompressionMetadataZstd *SzstdMetadata // Metadata for Zstd compression
}
// validate checks metadata read from the wrapped remote before it is used to
// construct a compressed reader.
func (meta *ObjectMetadata) validate() error {
if meta.Mode != Gzip {
return nil
}
if meta.CompressionMetadataGzip == nil {
return errors.New("missing gzip metadata")
}
if meta.CompressionMetadataGzip.BlockSize <= 0 {
return fmt.Errorf("invalid gzip block size %d", meta.CompressionMetadataGzip.BlockSize)
}
if meta.Size != meta.CompressionMetadataGzip.Size {
return errors.New("gzip metadata size does not match object size")
}
if meta.Size < 0 {
return errors.New("invalid gzip object size")
}
blockSize := int64(meta.CompressionMetadataGzip.BlockSize)
blocks := meta.Size / blockSize
if meta.Size%blockSize != 0 {
blocks++
}
if int64(len(meta.CompressionMetadataGzip.BlockData)) < blocks {
return errors.New("gzip block data is incomplete")
}
return nil
}
// Object with external metadata
type Object struct {
fs.Object // Wraps around data object for this object
@@ -1103,6 +1132,9 @@ func readMetadata(ctx context.Context, mo fs.Object) (meta *ObjectMetadata, err
if err = jr.Decode(meta); err != nil {
return nil, err
}
if err = meta.validate(); err != nil {
return nil, err
}
return meta, nil
}
+89
View File
@@ -6,6 +6,8 @@ import (
"path/filepath"
"testing"
"github.com/buengese/sgzip"
_ "github.com/rclone/rclone/backend/drive"
_ "github.com/rclone/rclone/backend/local"
_ "github.com/rclone/rclone/backend/s3"
@@ -14,6 +16,93 @@ import (
"github.com/rclone/rclone/fstest/fstests"
)
func TestObjectMetadataValidate(t *testing.T) {
tests := []struct {
name string
meta ObjectMetadata
want bool
}{
{
name: "nil gzip metadata",
meta: ObjectMetadata{Mode: Gzip},
want: true,
},
{
name: "zero block size",
meta: ObjectMetadata{
Mode: Gzip,
Size: 1024,
CompressionMetadataGzip: &sgzip.GzipMetadata{
BlockSize: 0,
Size: 1024,
BlockData: []uint32{0},
},
},
want: true,
},
{
name: "negative block size",
meta: ObjectMetadata{
Mode: Gzip,
Size: 1024,
CompressionMetadataGzip: &sgzip.GzipMetadata{
BlockSize: -1,
Size: 1024,
BlockData: []uint32{0},
},
},
want: true,
},
{
name: "mismatched sizes",
meta: ObjectMetadata{
Mode: Gzip,
Size: 1024,
CompressionMetadataGzip: &sgzip.GzipMetadata{
BlockSize: 512,
Size: 2048,
BlockData: []uint32{0, 1},
},
},
want: true,
},
{
name: "short block data",
meta: ObjectMetadata{
Mode: Gzip,
Size: 1025,
CompressionMetadataGzip: &sgzip.GzipMetadata{
BlockSize: 512,
Size: 1025,
BlockData: []uint32{0, 1},
},
},
want: true,
},
{
name: "valid",
meta: ObjectMetadata{
Mode: Gzip,
Size: 1025,
CompressionMetadataGzip: &sgzip.GzipMetadata{
BlockSize: 512,
Size: 1025,
BlockData: []uint32{0, 1, 2},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.meta.validate()
if (err != nil) != test.want {
t.Fatalf("validate() error = %v, want error: %v", err, test.want)
}
})
}
}
var defaultOpt = fstests.Opt{
RemoteName: "TestCompress:",
NilObject: (*Object)(nil),
+5 -5
View File
@@ -118,8 +118,8 @@ func (mode NameEncryptionMode) String() (out string) {
return out
}
// fileNameEncoding are the encoding methods dealing with encrypted file names
type fileNameEncoding interface {
// FileNameEncoding is the interface for the encoding methods dealing with encrypted file names
type FileNameEncoding interface {
EncodeToString(src []byte) string
DecodeString(s string) ([]byte, error)
}
@@ -154,7 +154,7 @@ func (caseInsensitiveBase32Encoding) DecodeString(s string) ([]byte, error) {
}
// NewNameEncoding creates a NameEncoding from a string
func NewNameEncoding(s string) (enc fileNameEncoding, err error) {
func NewNameEncoding(s string) (enc FileNameEncoding, err error) {
s = strings.ToLower(s)
switch s {
case "base32":
@@ -176,7 +176,7 @@ type Cipher struct {
nameTweak [nameCipherBlockSize]byte // used to tweak the name crypto
block gocipher.Block
mode NameEncryptionMode
fileNameEnc fileNameEncoding
fileNameEnc FileNameEncoding
buffers sync.Pool // encrypt/decrypt buffers
cryptoRand io.Reader // read crypto random numbers from here
dirNameEncrypt bool
@@ -185,7 +185,7 @@ type Cipher struct {
}
// newCipher initialises the cipher. If salt is "" then it uses a built in salt val
func newCipher(mode NameEncryptionMode, password, salt string, dirNameEncrypt bool, enc fileNameEncoding) (*Cipher, error) {
func newCipher(mode NameEncryptionMode, password, salt string, dirNameEncrypt bool, enc FileNameEncoding) (*Cipher, error) {
c := &Cipher{
mode: mode,
fileNameEnc: enc,
+33 -17
View File
@@ -692,13 +692,27 @@ func (f *Fs) Mkdir(ctx context.Context, dir string) error {
// deleteObject removes an object by ID
func (f *Fs) deleteObject(ctx context.Context, id string) error {
if f.opt.HardDelete {
// Deleting forever doesn't invalidate the server's cache of
// the parent folder listing, so the entry keeps being listed
// for about a second. Moving to the trash first does.
err := f.deleteEntry(ctx, id, false)
if err != nil {
return err
}
}
return f.deleteEntry(ctx, id, f.opt.HardDelete)
}
// deleteEntry moves the entry with ID to the trash or deletes it forever
func (f *Fs) deleteEntry(ctx context.Context, id string, deleteForever bool) error {
opts := rest.Opts{
Method: "POST",
Path: "/file-entries/delete",
}
request := api.DeleteRequest{
EntryIDs: []string{id},
DeleteForever: f.opt.HardDelete,
DeleteForever: deleteForever,
}
var result api.DeleteResponse
err := f.pacer.Call(func() (bool, error) {
@@ -1040,17 +1054,28 @@ func (f *Fs) copy(ctx context.Context, id, newDirID string) (item *api.Item, err
}
// copy and rename a file or folder to directoryID with leaf
func (f *Fs) copyTo(ctx context.Context, srcID, srcLeaf, dstLeaf, dstDirectoryID string) (info *api.Item, err error) {
// Can have duplicates so don't have to be careful here
//
// If existing is set it is removed once the copy has succeeded.
func (f *Fs) copyTo(ctx context.Context, srcID, dstLeaf, dstDirectoryID string, existing fs.Object) (info *api.Item, err error) {
// Copy to dstDirectoryID first
info, err = f.copy(ctx, srcID, dstDirectoryID)
if err != nil {
return nil, err
}
// The server names the copy "name (1)" if the destination
// directory already has an entry called "name" and refuses to
// rename it to "name" until that entry is removed.
if existing != nil {
fs.Debugf(existing, "Server side copy: removing existing object after successful copy")
err = existing.Remove(ctx)
if err != nil {
return nil, err
}
}
// Rename if required
if srcLeaf != dstLeaf {
if info.Name != f.opt.Enc.FromStandardName(dstLeaf) {
info, err = f.rename(ctx, info.ID.String(), dstLeaf)
if err != nil {
return nil, err
@@ -1074,8 +1099,6 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (dst fs.Obj
fs.Debugf(src, "Can't copy - not same remote type")
return nil, fs.ErrorCantCopy
}
srcLeaf := path.Base(srcObj.remote)
srcPath := srcObj.fs.rootSlash() + srcObj.remote
dstPath := f.rootSlash() + remote
if srcPath == dstPath {
@@ -1084,15 +1107,8 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (dst fs.Obj
// Find existing object
existingObj, err := f.NewObject(ctx, remote)
if err == nil {
defer func() {
// Don't remove existing object if returning an error
if err != nil {
return
}
fs.Debugf(existingObj, "Server side copy: removing existing object after successful copy")
err = existingObj.Remove(ctx)
}()
if err != nil {
existingObj = nil
}
// Create temporary object
@@ -1102,7 +1118,7 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (dst fs.Obj
}
// Copy the object
info, err := f.copyTo(ctx, srcObj.id, srcLeaf, dstLeaf, dstDirectoryID)
info, err := f.copyTo(ctx, srcObj.id, dstLeaf, dstDirectoryID, existingObj)
if err != nil {
return nil, err
}
+11 -8
View File
@@ -633,13 +633,8 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
return f, nil // our root it empty so we probably want to list shared folders
}
dir := path.Dir(f.root)
if dir == "." {
dir = f.root
}
// root is not empty so we have find the right shared folder if it exists
id, err := f.findSharedFolder(ctx, dir)
id, err := f.findSharedFolder(ctx, sharedFolderName(f.root))
if err != nil {
// if we didn't find the specified shared folder we have to bail out here
return nil, err
@@ -947,13 +942,21 @@ func (f *Fs) listSharedFolders(ctx context.Context, callback func(fs.DirEntry) e
return nil
}
// sharedFolderName returns the shared folder name in root, which is its first
// path component. root must be the trimmed root as produced by setRoot (no
// leading slash).
func sharedFolderName(root string) string {
name, _, _ := strings.Cut(root, "/")
return name
}
// findSharedFolder find the id for a given shared folder name
// somewhat annoyingly there is no endpoint to query a shared folder by it's name
// so our only option is to iterate over all shared folders
func (f *Fs) findSharedFolder(ctx context.Context, name string) (id string, err error) {
errFoundFile := errors.New("found file")
err = f.listSharedFolders(ctx, func(entry fs.DirEntry) error {
if entry.(*fs.Dir).Remote() == name {
if strings.EqualFold(entry.(*fs.Dir).Remote(), name) {
id = entry.(*fs.Dir).ID()
return errFoundFile
}
@@ -1035,7 +1038,7 @@ func (f *Fs) listReceivedFiles(ctx context.Context, callback func(fs.DirEntry) e
func (f *Fs) findSharedFile(ctx context.Context, name string) (o *Object, err error) {
errFoundFile := errors.New("found file")
err = f.listReceivedFiles(ctx, func(entry fs.DirEntry) error {
if entry.(*Object).remote == name {
if strings.EqualFold(entry.(*Object).remote, name) {
o = entry.(*Object)
return errFoundFile
}
+60
View File
@@ -188,6 +188,21 @@ func TestInternalCheckPathLength(t *testing.T) {
}
}
func TestInternalSharedFolderName(t *testing.T) {
for _, test := range []struct {
root string
want string
}{
{root: "", want: ""},
{root: "SharedFolder", want: "SharedFolder"},
{root: "SharedFolder/subdir", want: "SharedFolder"},
{root: "SharedFolder/subdir/deeper", want: "SharedFolder"},
{root: "SharedFolder/subdir/deeper/deepest", want: "SharedFolder"},
} {
assert.Equal(t, test.want, sharedFolderName(test.root), test.root)
}
}
func TestPaperExportRemote(t *testing.T) {
ctx := context.Background()
info := &files.FileMetadata{
@@ -475,3 +490,48 @@ func TestFindSharedFileResolvesDecodedName(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, encodedName, o.remote)
}
// sharedFoldersHandler serves a single shared folder from list_folders.
func sharedFoldersHandler(name, id string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.URL.Path, "list_folders") {
http.NotFound(w, r)
return
}
resp := sharing.ListFoldersResult{
Entries: []*sharing.SharedFolderMetadata{{Name: name, SharedFolderId: id}},
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}
}
// The Dropbox backend advertises CaseInsensitive: true, so a shared folder
// lookup must match regardless of the case of the requested name.
func TestInternalFindSharedFolderCaseInsensitive(t *testing.T) {
f := newSharingTestFs(t, sharedFoldersHandler("TestFolder", "folder-id"))
for _, name := range []string{"TestFolder", "testfolder", "TESTFOLDER"} {
id, err := f.findSharedFolder(context.Background(), name)
require.NoError(t, err, name)
assert.Equal(t, "folder-id", id, name)
}
_, err := f.findSharedFolder(context.Background(), "no-such-folder")
assert.ErrorIs(t, err, fs.ErrorDirNotFound)
}
// The Dropbox backend advertises CaseInsensitive: true, so a received file
// lookup must match regardless of the case of the requested name.
func TestInternalFindSharedFileCaseInsensitive(t *testing.T) {
f := newSharingTestFs(t, receivedFilesHandler(t, "TestFile.txt"))
for _, name := range []string{"TestFile.txt", "testfile.txt", "TESTFILE.TXT"} {
o, err := f.findSharedFile(context.Background(), name)
require.NoError(t, err, name)
assert.Equal(t, "TestFile.txt", o.remote, name)
}
_, err := f.findSharedFile(context.Background(), "no-such-file.txt")
assert.ErrorIs(t, err, fs.ErrorObjectNotFound)
}
+9 -1
View File
@@ -19,6 +19,7 @@ import (
"github.com/Files-com/files-sdk-go/v3/file"
file_migration "github.com/Files-com/files-sdk-go/v3/filemigration"
"github.com/Files-com/files-sdk-go/v3/folder"
files_sdk_lib "github.com/Files-com/files-sdk-go/v3/lib"
"github.com/Files-com/files-sdk-go/v3/session"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config"
@@ -176,6 +177,13 @@ func shouldRetry(ctx context.Context, err error) (bool, error) {
}
}
// Errors from the upload storage servers are of this type
var httpErr files_sdk_lib.ResponseError
if errors.As(err, &httpErr) && slices.Contains(retryErrorCodes, httpErr.StatusCode) {
fs.Debugf(nil, "Retrying HTTP error %v", err)
return true, err
}
return fserrors.ShouldRetry(err), err
}
@@ -858,7 +866,7 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
file.UploadWithProvidedMtime(src.ModTime(ctx)),
}
err := o.fs.pacer.Call(func() (bool, error) {
err := o.fs.pacer.CallNoRetry(func() (bool, error) {
err := o.fs.fileClient.Upload(uploadOpts...)
return shouldRetry(ctx, err)
})
+10 -30
View File
@@ -283,7 +283,7 @@ func (f *Fs) httpConnection(ctx context.Context, opt *Options) (isFile bool, err
if err != nil {
return false, err
}
u, err := rest.URLJoin(base, rest.URLPathEscape(f.root))
u, err := rest.URLJoinRoot(base, f.root)
if err != nil {
return false, err
}
@@ -526,40 +526,20 @@ func addHeaders(req *http.Request, opt *Options) {
// rest.ErrHTTPSDowngrade and strips the configured headers when the
// redirect chain has left the originally requested host at any point.
func checkRedirect(opt *Options) func(req *http.Request, via []*http.Request) error {
headers := make([]string, 0, len(opt.Headers)/2)
for i := 0; i < len(opt.Headers); i += 2 {
headers = append(headers, opt.Headers[i])
}
stripHeaders := rest.StripHeadersOnCrossHostRedirectFn(headers...)
return func(req *http.Request, via []*http.Request) error {
if err := rest.RefuseHTTPSDowngradeRedirectFn(req, via); err != nil {
if errors.Is(err, rest.ErrHTTPSDowngrade) {
err = fmt.Errorf("%w (the configured headers would be sent to the plaintext target)", err)
}
return err
err := stripHeaders(req, via)
if errors.Is(err, rest.ErrHTTPSDowngrade) {
err = fmt.Errorf("%w (the configured headers would be sent to the plaintext target)", err)
}
if redirectLeavesHost(req, via) {
for i := 0; i < len(opt.Headers); i += 2 {
req.Header.Del(opt.Headers[i])
}
}
return nil
return err
}
}
// redirectLeavesHost reports whether any hop in the redirect chain
// via plus the pending request req is to a different host from the
// original request via[0].
//
// net/http copies the headers afresh from the original request for
// every hop, so once the chain has visited another host the headers
// must be stripped from every subsequent hop, even one back to the
// original host, as the other host chose the URL.
func redirectLeavesHost(req *http.Request, via []*http.Request) bool {
origin := via[0].URL
for _, hop := range via {
if !rest.SameHost(hop.URL, origin) {
return true
}
}
return !rest.SameHost(req.URL, origin)
}
// Adds the configured headers to the request if any
func (f *Fs) addHeaders(req *http.Request) {
addHeaders(req, &f.opt)
+31
View File
@@ -760,3 +760,34 @@ func TestRedirectStripsHeadersOnHostChange(t *testing.T) {
assert.Empty(t, got.Values(headers[i]), "header %q leaked to redirect target", headers[i])
}
}
func TestRootCannotChangeHost(t *testing.T) {
// A root starting with "//" is a network-path reference under the
// URL standard. It must not be able to move the remote (and the
// configured headers) to a host other than the configured one.
var requests int
other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests++
_, _ = w.Write([]byte("hello"))
}))
defer other.Close()
m := prepareServer(t)
otherHost := strings.TrimPrefix(other.URL, "http://")
for _, root := range []string{
"//" + otherHost + "/",
"//" + otherHost + "/one%.txt",
"//" + otherHost,
"//user:pass@" + otherHost + "/",
} {
f, err := NewFs(context.Background(), remoteName, root, m)
assert.ErrorContains(t, err, "must not change the scheme, host or user", root)
assert.Nil(t, f, root)
}
assert.Equal(t, 0, requests, "request sent to the other host")
// A same-origin absolute path must still work
f, err := NewFs(context.Background(), remoteName, "/", m)
require.NoError(t, err)
assert.Equal(t, m["url"]+"/", f.String())
}
+1 -1
View File
@@ -92,7 +92,7 @@ func (f *Fs) findItem(ctx context.Context, dir string) (item *api.DriveItem, fou
item, resp, err = f.service.GetItemByPath(ctx, path.Join(f.root, dir))
return shouldRetry(ctx, resp, err)
}); err != nil {
if item == nil && resp.StatusCode == 404 {
if item == nil && resp != nil && resp.StatusCode == 404 {
return nil, false, nil
}
return nil, false, err
+2 -2
View File
@@ -477,7 +477,7 @@ func (f *Fs) Rmdir(ctx context.Context, dir string) (err error) {
FolderPath: f.EncodePath(path.Join(f.root, dir)),
})
if res.StatusCode == http.StatusNotFound {
if res != nil && res.StatusCode == http.StatusNotFound {
return false, fs.ErrorDirNotFound
}
@@ -502,7 +502,7 @@ func (f *Fs) Purge(ctx context.Context, dir string) (err error) {
FolderPath: f.EncodePath(remote),
})
if res.StatusCode == http.StatusNotFound {
if res != nil && res.StatusCode == http.StatusNotFound {
return false, fs.ErrorDirNotFound
}
+236 -109
View File
@@ -475,8 +475,10 @@ func (f *Fs) Rmdir(ctx context.Context, dir string) error {
if err != nil {
return err
}
if len(childFolders) > 0 {
return fs.ErrorDirectoryNotEmpty
for _, e := range childFolders {
if !recent.stale(e.UUID, id, e.PlainName) {
return fs.ErrorDirectoryNotEmpty
}
}
var childFiles []folders.File
@@ -488,8 +490,10 @@ func (f *Fs) Rmdir(ctx context.Context, dir string) error {
if err != nil {
return err
}
if len(childFiles) > 0 {
return fs.ErrorDirectoryNotEmpty
for _, e := range childFiles {
if !recent.stale(e.UUID, id, joinNameExt(e.PlainName, e.Type)) {
return fs.ErrorDirectoryNotEmpty
}
}
// Delete the directory
@@ -503,6 +507,7 @@ func (f *Fs) Rmdir(ctx context.Context, dir string) error {
if err != nil {
return err
}
recent.deleted(id)
f.dirCache.FlushDir(dir)
return nil
@@ -521,6 +526,9 @@ func (f *Fs) FindLeaf(ctx context.Context, pathID, leaf string) (string, bool, e
return "", false, err
}
for _, e := range entries {
if recent.stale(e.UUID, pathID, e.PlainName) {
continue
}
if f.opt.Encoding.ToStandardName(e.PlainName) == leaf {
return e.UUID, true, nil
}
@@ -557,46 +565,166 @@ func (f *Fs) CreateDir(ctx context.Context, pathID, leaf string) (string, error)
return resp.UUID, nil
}
// preUploadCheck checks if a file exists in the given directory
// Returns the file metadata if it exists, nil if not
func (f *Fs) preUploadCheck(ctx context.Context, leaf, directoryID string) (*folders.File, error) {
// Parse name and extension from the leaf
baseName := f.opt.Encoding.FromStandardName(leaf)
name := strings.TrimSuffix(baseName, path.Ext(baseName))
ext := strings.TrimPrefix(path.Ext(baseName), ".")
// splitNameExt splits a basename into the (plainName, type) pair that Internxt
// stores a file under.
func splitNameExt(baseName string) (name, ext string) {
ext = path.Ext(baseName)
if ext == baseName {
return baseName, ""
}
return strings.TrimSuffix(baseName, ext), strings.TrimPrefix(ext, ".")
}
checkResult, err := files.CheckFilesExistence(ctx, f.cfg, directoryID, []files.FileExistenceCheck{
{
PlainName: name,
Type: ext,
OriginalFile: struct{}{},
},
// joinNameExt reassembles the basename a stored (plainName, type) pair
// displays as, the way List builds its entries.
func joinNameExt(name, ext string) string {
if ext == "" {
return name
}
return name + "." + ext
}
// legacyNameExt splits a basename at the final dot, the way this backend used
// to before it adopted the convention the other clients share. It differs from
// splitNameExt only for a name whose sole dot leads it: ".bashrc" splits into
// an empty name of type "bashrc".
func legacyNameExt(baseName string) (name, ext string) {
return strings.TrimSuffix(baseName, path.Ext(baseName)), strings.TrimPrefix(path.Ext(baseName), ".")
}
// existenceCheck builds a lookup criterion for one (plainName, type) spelling.
func existenceCheck(name, ext string) files.FileExistenceCheck {
return files.FileExistenceCheck{
PlainName: name,
Type: ext,
OriginalFile: struct{}{},
}
}
// staleWindow is how long the API may carry on returning an item from
// where it was before this process moved or deleted it.
const staleWindow = time.Minute
// location is where this process last put an item. An empty parentUUID
// means the item was deleted.
type location struct {
parentUUID string
name string // encoded name, including any extension
expires time.Time
}
// recentChanges holds the files and folders this process has moved or
// deleted recently, keyed by UUID.
//
// The API serves lookups and listings from read replicas which lag behind
// writes, so an item can be returned from its old location for a while after
// it was moved or deleted. Acting on such a stale entry can be destructive,
// eg overwriting it would delete the file from where it was moved to.
type recentChanges struct {
mu sync.Mutex
items map[string]location
lastPrune time.Time
}
// recent is shared by all the Fs in the process as UUIDs are global.
var recent = recentChanges{items: map[string]location{}}
// set records the new location of uuid, pruning expired entries.
func (r *recentChanges) set(uuid string, loc location) {
r.mu.Lock()
defer r.mu.Unlock()
now := time.Now()
if now.Sub(r.lastPrune) > staleWindow {
for k, v := range r.items {
if now.After(v.expires) {
delete(r.items, k)
}
}
r.lastPrune = now
}
loc.expires = now.Add(staleWindow)
r.items[uuid] = loc
}
// moved records that uuid was moved to name in parentUUID.
func (r *recentChanges) moved(uuid, parentUUID, name string) {
r.set(uuid, location{parentUUID: parentUUID, name: name})
}
// deleted records that uuid was deleted.
func (r *recentChanges) deleted(uuid string) {
r.set(uuid, location{})
}
// stale reports whether an API result saying uuid is called name in
// parentUUID is out of date because of a change made by this process.
func (r *recentChanges) stale(uuid, parentUUID, name string) bool {
r.mu.Lock()
defer r.mu.Unlock()
loc, ok := r.items[uuid]
if !ok || time.Now().After(loc.expires) {
return false
}
return loc.parentUUID != parentUUID || loc.name != name
}
// findFile looks up a single file by name within directoryID. Returns
// (nil, nil) when the file does not exist; surfaces transport/API errors.
func (f *Fs) findFile(ctx context.Context, leaf, directoryID string) (*folders.File, error) {
encodedName := f.opt.Encoding.FromStandardName(leaf)
name, ext := splitNameExt(encodedName)
checks := []files.FileExistenceCheck{existenceCheck(name, ext)}
if encodedName != name {
checks = append(checks, existenceCheck(encodedName, ""))
}
if legacyName, legacyExt := legacyNameExt(encodedName); legacyExt != "" && legacyName != name {
checks = append(checks, existenceCheck(legacyName, legacyExt))
}
var checkResult *files.CheckFilesExistenceResponse
err := f.pacer.Call(func() (bool, error) {
var err error
checkResult, err = files.CheckFilesExistence(ctx, f.cfg, directoryID, checks)
if isNotFoundError(err) {
return true, err
}
return f.shouldRetry(ctx, err)
})
if err != nil {
// If existence check fails, assume file doesn't exist to allow upload to proceed
return nil, err
}
var uuid string
for _, result := range checkResult.Files {
if !result.FileExists() || result.UUID == "" {
continue
}
if recent.stale(result.UUID, directoryID, joinNameExt(result.PlainName, result.Type)) {
fs.Debugf(f, "Ignoring stale lookup of %q (UUID: %s) which was moved or deleted", leaf, result.UUID)
continue
}
if joinNameExt(result.PlainName, result.Type) == encodedName {
uuid = result.UUID
break
}
}
if uuid == "" {
return nil, nil
}
if len(checkResult.Files) > 0 && checkResult.Files[0].FileExists() {
result := checkResult.Files[0]
if result.Type != ext {
return nil, nil
}
existingUUID := result.UUID
if existingUUID != "" {
fileMeta, err := files.GetFileMeta(ctx, f.cfg, existingUUID)
if err == nil && fileMeta != nil {
return convertFileMetaToFile(fileMeta), nil
}
if err != nil {
return nil, err
}
}
var fileMeta *files.FileMeta
err = f.pacer.Call(func() (bool, error) {
var err error
fileMeta, err = files.GetFileMeta(ctx, f.cfg, uuid)
return f.shouldRetry(ctx, err)
})
if err != nil {
return nil, err
}
return nil, nil
if fileMeta == nil {
return nil, nil
}
return convertFileMetaToFile(fileMeta), nil
}
// convertFileMetaToFile converts files.FileMeta to folders.File
@@ -634,6 +762,9 @@ func (f *Fs) List(ctx context.Context, dir string) (fs.DirEntries, error) {
return nil, err
}
for _, e := range foldersList {
if recent.stale(e.UUID, dirID, e.PlainName) {
continue
}
remote := path.Join(dir, f.opt.Encoding.ToStandardName(e.PlainName))
out = append(out, fs.NewDir(remote, e.ModificationTime))
}
@@ -651,6 +782,9 @@ func (f *Fs) List(ctx context.Context, dir string) (fs.DirEntries, error) {
if len(e.Type) > 0 {
remote += "." + e.Type
}
if recent.stale(e.UUID, dirID, remote) {
continue
}
remote = path.Join(dir, f.opt.Encoding.ToStandardName(remote))
out = append(out, newObjectWithFile(f, remote, &e))
}
@@ -675,8 +809,7 @@ func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options .
return nil, err
}
// Check if file already exists
existingFile, err := f.preUploadCheck(ctx, leaf, directoryID)
existingFile, err := f.findFile(ctx, leaf, directoryID)
if err != nil {
return nil, err
}
@@ -724,44 +857,28 @@ func (f *Fs) Remove(ctx context.Context, remote string) error {
if err != nil {
return err
}
recent.deleted(dirID)
f.dirCache.FlushDir(remote)
return nil
}
// NewObject creates a new object
// NewObject creates a new object by looking up a single file's metadata.
func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
parentDir := path.Dir(remote)
if parentDir == "." {
parentDir = ""
}
dirID, err := f.dirCache.FindDir(ctx, parentDir, false)
leaf, directoryID, err := f.dirCache.FindPath(ctx, remote, false)
if err != nil {
return nil, fs.ErrorObjectNotFound
if err == fs.ErrorDirNotFound {
return nil, fs.ErrorObjectNotFound
}
return nil, err
}
var files []folders.File
err = f.pacer.Call(func() (bool, error) {
var err error
files, err = folders.ListAllFiles(ctx, f.cfg, dirID)
return f.shouldRetry(ctx, err)
})
file, err := f.findFile(ctx, leaf, directoryID)
if err != nil {
return nil, err
}
targetName := path.Base(remote)
for _, e := range files {
name := e.PlainName
if len(e.Type) > 0 {
name += "." + e.Type
}
decodedName := f.opt.Encoding.ToStandardName(name)
if decodedName == targetName {
return newObjectWithFile(f, remote, &e), nil
}
if file == nil {
return nil, fs.ErrorObjectNotFound
}
return nil, fs.ErrorObjectNotFound
return newObjectWithFile(f, remote, file), nil
}
// newObjectWithFile returns a new object by file info
@@ -865,19 +982,23 @@ func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object,
return nil, err
}
// Parse name and extension from the leaf
baseName := f.opt.Encoding.FromStandardName(leaf)
newName := strings.TrimSuffix(baseName, path.Ext(baseName))
newType := strings.TrimPrefix(path.Ext(baseName), ".")
newName, newType := splitNameExt(f.opt.Encoding.FromStandardName(leaf))
// Move the file server-side
err = f.pacer.Call(func() (bool, error) {
err := files.MoveFile(ctx, f.cfg, srcObj.uuid, directoryID, newName, newType)
// The move is checked against lagging read replicas so it can
// fail with 404 if the destination directory was just created
// or 409 if a file at the destination was just deleted.
if err != nil && isStaleReadError(err, true) {
return true, err
}
return f.shouldRetry(ctx, err)
})
if err != nil {
return nil, err
}
recent.moved(srcObj.uuid, directoryID, joinNameExt(newName, newType))
dstObj := &Object{
f: f,
@@ -913,11 +1034,17 @@ func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string
encodedLeaf := f.opt.Encoding.FromStandardName(dstLeaf)
err = f.pacer.Call(func() (bool, error) {
err := folders.MoveFolder(ctx, f.cfg, srcID, dstDirectoryID, encodedLeaf)
// Moving a large folder can outlast the gateway timeout (520/502)
// yet still complete, in which case the retry reports this.
if err != nil && isConflictError(err) && strings.Contains(err.Error(), "already moved to that location") {
return false, nil
}
return f.shouldRetry(ctx, err)
})
if err != nil {
return err
}
recent.moved(srcID, dstDirectoryID, encodedLeaf)
srcFs.dirCache.FlushDir(srcRemote)
return nil
@@ -965,9 +1092,7 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadClo
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
remote := o.remote
origBaseName := path.Base(remote)
origName := strings.TrimSuffix(origBaseName, path.Ext(origBaseName))
origType := strings.TrimPrefix(path.Ext(origBaseName), ".")
origName, origType := splitNameExt(path.Base(remote))
// Create directory if it doesn't exist
_, dirID, err := o.f.dirCache.FindPath(ctx, remote, true)
@@ -984,11 +1109,7 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
// Step 1: If file exists, rename to backup (preserves old file during upload)
if oldUUID != "" {
// Generate unique backup name
baseName := path.Base(remote)
name := strings.TrimSuffix(baseName, path.Ext(baseName))
ext := strings.TrimPrefix(path.Ext(baseName), ".")
name, ext := splitNameExt(path.Base(remote))
backupSuffix := fmt.Sprintf(".rclone-backup-%s", random.String(8))
backupName = o.f.opt.Encoding.FromStandardName(name + backupSuffix)
backupType = ext
@@ -1061,6 +1182,11 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
return o.f.tooLargeError(remote, tooLarge)
}
if err != nil && isStaleReadError(err, backupUUID != "") {
o.restoreBackupFile(ctx, backupUUID, origName, origType)
return fserrors.RetryError(err)
}
if err != nil {
meta, err = o.recoverFromTimeoutConflict(ctx, err, remote, dirID)
}
@@ -1099,6 +1225,7 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
backupName, backupType, backupUUID, err)
// Don't fail the upload just because backup deletion failed
} else {
recent.deleted(backupUUID)
fs.Debugf(o.f, "Successfully deleted backup file")
}
}
@@ -1118,6 +1245,19 @@ func isTimeoutError(err error) bool {
return false
}
// isNotFoundError reports whether err is a 404 from the API.
func isNotFoundError(err error) bool {
var httpErr *sdkerrors.HTTPError
return errors.As(err, &httpErr) && httpErr.StatusCode() == 404
}
// isStaleReadError reports whether err is the API contradicting a write this
// backend has already made, which it does because lookups are served by read
// replicas that lag behind the primary.
func isStaleReadError(err error, renamed bool) bool {
return isNotFoundError(err) || (renamed && isConflictError(err))
}
// isConflictError checks if an error indicates a file conflict (409)
func isConflictError(err error) bool {
errMsg := err.Error()
@@ -1159,40 +1299,22 @@ func (o *Object) recoverFromTimeoutConflict(ctx context.Context, uploadErr error
}
baseName := path.Base(remote)
encodedName := o.f.opt.Encoding.FromStandardName(baseName)
var meta *buckets.CreateMetaResponse
checkErr := o.f.pacer.Call(func() (bool, error) {
existingFile, err := o.f.preUploadCheck(ctx, encodedName, dirID)
if err != nil {
return o.f.shouldRetry(ctx, err)
}
if existingFile != nil {
name := strings.TrimSuffix(baseName, path.Ext(baseName))
ext := strings.TrimPrefix(path.Ext(baseName), ".")
meta = &buckets.CreateMetaResponse{
UUID: existingFile.UUID,
FileID: existingFile.FileID,
Name: name,
PlainName: name,
Type: ext,
Size: existingFile.Size,
}
o.id = existingFile.FileID
}
return false, nil
})
if checkErr != nil {
existingFile, err := o.f.findFile(ctx, baseName, dirID)
if err != nil || existingFile == nil {
return nil, uploadErr
}
if meta != nil {
return meta, nil
}
return nil, uploadErr
name, ext := splitNameExt(baseName)
o.id = existingFile.FileID
return &buckets.CreateMetaResponse{
UUID: existingFile.UUID,
FileID: existingFile.FileID,
Name: name,
PlainName: name,
Type: ext,
Size: existingFile.Size,
}, nil
}
// restoreBackupFile restores a backup file after upload failure
@@ -1210,8 +1332,13 @@ func (o *Object) restoreBackupFile(ctx context.Context, backupUUID, origName, or
// Remove deletes a file
func (o *Object) Remove(ctx context.Context) error {
return o.f.pacer.Call(func() (bool, error) {
err := o.f.pacer.Call(func() (bool, error) {
err := files.DeleteFile(ctx, o.f.cfg, o.uuid)
return o.f.shouldRetry(ctx, err)
})
if err != nil {
return err
}
recent.deleted(o.uuid)
return nil
}
+191
View File
@@ -0,0 +1,191 @@
package internxt
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/internxt/rclone-adapter/config"
"github.com/internxt/rclone-adapter/endpoints"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/lib/encoder"
"github.com/rclone/rclone/lib/pacer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// storedFile is a file as the service holds it: a (plainName, type) pair
// split however the client that uploaded it chose to split it.
type storedFile struct {
uuid string
plainName string
fileType string
}
// wireType renders a stored type the way the service does: a file with no
// extension comes back as JSON null, not an empty string.
func wireType(fileType string) any {
if fileType == "" {
return nil
}
return fileType
}
// existenceServer answers the existence check and file meta endpoints from
// files, matching plainName exactly and only constraining type when the
// criterion supplies one, as the service does.
func existenceServer(t *testing.T, stored []storedFile) *httptest.Server {
t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if strings.HasSuffix(r.URL.Path, "/meta") {
uuid := strings.Split(strings.TrimPrefix(r.URL.Path, "/drive/files/"), "/")[0]
for _, f := range stored {
if f.uuid == uuid {
require.NoError(t, json.NewEncoder(w).Encode(map[string]any{
"uuid": f.uuid, "plainName": f.plainName, "type": wireType(f.fileType),
}))
return
}
}
w.WriteHeader(http.StatusNotFound)
return
}
var req struct {
Files []struct {
PlainName string `json:"plainName"`
Type string `json:"type"`
} `json:"files"`
}
require.NoError(t, json.NewDecoder(r.Body).Decode(&req))
var existent []map[string]any
for _, f := range stored {
for _, c := range req.Files {
if c.PlainName == f.plainName && (c.Type == "" || c.Type == f.fileType) {
existent = append(existent, map[string]any{
"exists": true, "status": "EXISTS",
"uuid": f.uuid, "plainName": f.plainName, "type": wireType(f.fileType),
})
break
}
}
}
require.NoError(t, json.NewEncoder(w).Encode(map[string]any{"existentFiles": existent}))
}))
}
func testFs(t *testing.T, server *httptest.Server) *Fs {
t.Helper()
f := &Fs{
opt: Options{Encoding: encoder.EncodeInvalidUtf8},
cfg: &config.Config{
HTTPClient: server.Client(),
Endpoints: endpoints.NewConfig(server.URL),
},
}
f.pacer = fs.NewPacer(context.Background(), pacer.NewDefault(pacer.MinSleep(minSleep), pacer.MaxSleep(maxSleep), pacer.DecayConstant(decayConstant)))
return f
}
func TestSplitJoinNameExt(t *testing.T) {
for _, test := range []struct {
baseName string
name string
ext string
}{
{"foo.txt", "foo", "txt"},
{"foo.tar.gz", "foo.tar", "gz"},
{"README", "README", ""},
{".bashrc", ".bashrc", ""},
{"..hidden", ".", "hidden"},
{"", "", ""},
} {
name, ext := splitNameExt(test.baseName)
assert.Equal(t, test.name, name, test.baseName)
assert.Equal(t, test.ext, ext, test.baseName)
assert.Equal(t, test.baseName, joinNameExt(name, ext), test.baseName)
}
}
func TestFindFile(t *testing.T) {
for _, test := range []struct {
what string
stored []storedFile
leaf string
wantUUID string
}{{
what: "split as rclone splits it",
stored: []storedFile{{"uuid-1", "foo.tar", "gz"}},
leaf: "foo.tar.gz",
wantUUID: "uuid-1",
}, {
what: "whole name stored as plainName by another client",
stored: []storedFile{{"uuid-2", "foo.tar.gz", ""}},
leaf: "foo.tar.gz",
wantUUID: "uuid-2",
}, {
what: "extensionless name is not confused with an extended one",
stored: []storedFile{{"uuid-3", "README", "md"}, {"uuid-4", "README", ""}},
leaf: "README",
wantUUID: "uuid-4",
}, {
what: "dotfile stored the way the web client splits it",
stored: []storedFile{{"uuid-6", ".bashrc", ""}},
leaf: ".bashrc",
wantUUID: "uuid-6",
}, {
what: "dotfile stored the way older rclone versions split it",
stored: []storedFile{{"uuid-7", "", "bashrc"}},
leaf: ".bashrc",
wantUUID: "uuid-7",
}, {
what: "trailing dot kept in plainName by a rename",
stored: []storedFile{{"uuid-8", "trailing.", ""}},
leaf: "trailing.",
wantUUID: "uuid-8",
}, {
what: "missing file",
stored: []storedFile{{"uuid-5", "other", "txt"}},
leaf: "foo.txt",
}} {
t.Run(test.what, func(t *testing.T) {
server := existenceServer(t, test.stored)
defer server.Close()
file, err := testFs(t, server).findFile(context.Background(), test.leaf, "dir-uuid")
require.NoError(t, err)
if test.wantUUID == "" {
assert.Nil(t, file)
return
}
require.NotNil(t, file)
assert.Equal(t, test.wantUUID, file.UUID)
})
}
}
func TestFindFileIgnoresStale(t *testing.T) {
ctx := context.Background()
server := existenceServer(t, []storedFile{{"uuid-moved", "foo", "txt"}, {"uuid-deleted", "bar", "txt"}, {"uuid-renamed", "baz", "txt"}})
defer server.Close()
f := testFs(t, server)
recent.moved("uuid-moved", "other-dir-uuid", "foo.txt.bak")
recent.deleted("uuid-deleted")
recent.moved("uuid-renamed", "dir-uuid", "baz.txt")
file, err := f.findFile(ctx, "foo.txt", "dir-uuid")
require.NoError(t, err)
assert.Nil(t, file, "moved away")
file, err = f.findFile(ctx, "bar.txt", "dir-uuid")
require.NoError(t, err)
assert.Nil(t, file, "deleted")
file, err = f.findFile(ctx, "baz.txt", "dir-uuid")
require.NoError(t, err)
require.NotNil(t, file, "moved here")
assert.Equal(t, "uuid-renamed", file.UUID)
}
+1 -4
View File
@@ -7,7 +7,6 @@ import (
"io"
"path"
"sort"
"strings"
"sync"
"github.com/internxt/rclone-adapter/buckets"
@@ -263,9 +262,7 @@ func (w *internxtChunkWriter) Close(ctx context.Context) error {
}
// Create file metadata in Internxt Drive
baseName := w.f.opt.Encoding.FromStandardName(path.Base(w.remote))
name := strings.TrimSuffix(baseName, path.Ext(baseName))
ext := strings.TrimPrefix(path.Ext(baseName), ".")
name, ext := splitNameExt(w.f.opt.Encoding.FromStandardName(path.Base(w.remote)))
var meta *buckets.CreateMetaResponse
err = w.f.pacer.Call(func() (bool, error) {
+41 -25
View File
@@ -438,6 +438,7 @@ var (
errLinksAndCopyLinks = errors.New("can't use -l/--links with -L/--copy-links")
errLinksNeedsSuffix = errors.New("need \"" + fs.LinkSuffix + "\" suffix to refer to symlink when using -l/--links")
errPathEscapes = errors.New("file name is not a path within the local root - check the encoding")
errLinkNameInvalid = errors.New("translated link needs a file name before the \"" + fs.LinkSuffix + "\" suffix")
)
// NewFs constructs an Fs from the path
@@ -565,30 +566,26 @@ func (f *Fs) caseInsensitive() bool {
return runtime.GOOS == "windows" || runtime.GOOS == "darwin"
}
// translateLink checks whether the remote is a translated link
// and returns a new path, removing the suffix as needed,
// It also returns whether this is a translated link at all
//
// for regular files, localPath is returned unchanged
func translateLink(remote, localPath string) (newLocalPath string, isTranslatedLink bool) {
isTranslatedLink = strings.HasSuffix(remote, fs.LinkSuffix)
newLocalPath = strings.TrimSuffix(localPath, fs.LinkSuffix)
return newLocalPath, isTranslatedLink
}
// newObject makes a half completed Object
func (f *Fs) newObject(remote string) (*Object, error) {
translatedLink := false
localPath, err := f.localPath(remote)
name, translatedLink := remote, false
if f.opt.TranslateSymlinks {
name, translatedLink = strings.CutSuffix(remote, fs.LinkSuffix)
}
if translatedLink {
// Check this makes a valid native file name
native := f.nativePath(name)
switch native[strings.LastIndex(native, string(filepath.Separator))+1:] {
case "", ".", "..":
return nil, fserrors.NoRetryError(fmt.Errorf("%q: %w", remote, errLinkNameInvalid))
}
}
// Check the name after any suffix removal
localPath, err := f.localPath(name)
if err != nil {
return nil, err
}
if f.opt.TranslateSymlinks {
// Possibly receive a new name for localPath
localPath, translatedLink = translateLink(remote, localPath)
}
return &Object{
fs: f,
remote: remote,
@@ -637,15 +634,23 @@ func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
}
// Create new directory object from the info passed in
//
// Directories are never translated links, so the name is used as is
// whatever suffix it has.
func (f *Fs) newDirectory(dir string, fi os.FileInfo) (*Directory, error) {
o, err := f.newObject(dir)
localPath, err := f.localPath(dir)
if err != nil {
return nil, err
}
o.setMetadata(fi)
return &Directory{
Object: *o,
}, nil
d := &Directory{
Object: Object{
fs: f,
remote: dir,
path: localPath,
},
}
d.setMetadata(fi)
return d, nil
}
// List the objects and directories in dir into entries. The
@@ -785,6 +790,12 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e
continue
}
fso, err := f.newObjectWithInfo(newRemote, fi)
if errors.Is(err, errLinkNameInvalid) {
// Skip a badly named ".rclonelink" file
fs.Errorf(newRemote, "Listing error: %v", err)
_ = accounting.Stats(ctx).Error(err)
continue
}
if err != nil {
return nil, err
}
@@ -814,14 +825,19 @@ func (f *Fs) cleanRemote(dir, filename string) (remote string) {
return
}
// nativePath returns name in the OS encoding with OS path separators,
// relative to f.root, without cleaning it.
func (f *Fs) nativePath(name string) string {
return filepath.FromSlash(f.opt.Enc.FromStandardPath(name))
}
// localPath returns the OS path for the object called name, which is
// always underneath f.root.
//
// It returns errPathEscapes if name resolves outside f.root which can
// happen depending on the encoding.
func (f *Fs) localPath(name string) (string, error) {
native := filepath.FromSlash(f.opt.Enc.FromStandardPath(name))
localPath := filepath.Join(f.root, native)
localPath := filepath.Join(f.root, f.nativePath(name))
rel, err := filepath.Rel(f.root, localPath)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", fserrors.NoRetryError(fmt.Errorf("%q: %w", name, errPathEscapes))
@@ -0,0 +1,37 @@
//go:build windows
package local
import (
"os"
"testing"
"github.com/rclone/rclone/fs/fserrors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/windows"
)
func TestUpdateFatalIfNoSpaceWindows(t *testing.T) {
tests := []struct {
name string
err error
}{
{"ERROR_DISK_FULL", windows.ERROR_DISK_FULL},
{"openat PathError", &os.PathError{Op: "openat", Path: "test.txt", Err: windows.ERROR_DISK_FULL}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Run("off", func(t *testing.T) {
err := updateWithReader(t, false, test.err)
require.Error(t, err)
assert.False(t, fserrors.IsFatalError(err))
})
t.Run("on", func(t *testing.T) {
err := updateWithReader(t, true, test.err)
require.Error(t, err)
assert.True(t, fserrors.IsFatalError(err))
})
})
}
}
+166
View File
@@ -97,7 +97,19 @@ func TestVerifyCopy(t *testing.T) {
assert.Error(t, err)
}
// skipIfNoSymlinks skips the test if this process can't create symlinks.
//
// Windows grants the privilege only to an elevated process or one running with
// Developer Mode enabled, so an ordinary user gets ERROR_PRIVILEGE_NOT_HELD.
func skipIfNoSymlinks(t *testing.T) {
t.Helper()
if err := os.Symlink("target", filepath.Join(t.TempDir(), "link")); err != nil {
t.Skipf("Skipping as symlinks are unavailable: %v", err)
}
}
func TestSymlink(t *testing.T) {
skipIfNoSymlinks(t)
ctx := context.Background()
r := fstest.NewRun(t)
f := r.Flocal.(*Fs)
@@ -265,6 +277,7 @@ func linksMode(f *Fs) {
// faithful backup of the source) but must refuse to write through it, so
// nothing lands outside the destination (CWE-59).
func TestSymlinkEscapeWriteThroughBlocked(t *testing.T) {
skipIfNoSymlinks(t)
ctx := context.Background()
// A directory outside the destination the attacker wants to write into
@@ -300,6 +313,7 @@ func TestSymlinkEscapeWriteThroughBlocked(t *testing.T) {
// re-validated against the root, so the write-through is refused and nothing
// escapes.
func TestSymlinkEscapeNestedBlocked(t *testing.T) {
skipIfNoSymlinks(t)
ctx := context.Background()
evil := t.TempDir()
@@ -351,6 +365,7 @@ func TestSymlinkEscapeConcurrent(t *testing.T) {
// use: an in-tree symlink to a sibling directory can still be created and
// written through, since that write stays inside the destination.
func TestSymlinkInTreeWriteThroughWorks(t *testing.T) {
skipIfNoSymlinks(t)
ctx := context.Background()
r := fstest.NewRun(t)
@@ -606,6 +621,154 @@ func TestLocalPath(t *testing.T) {
}
}
// TestLinkSuffixEscapeBlocked checks that in --links mode a name which
// passes the root check with its ".rclonelink" suffix can't escape the
// destination once the suffix is removed: "...rclonelink" must not
// become "..", the parent of the root.
func TestLinkSuffixEscapeBlocked(t *testing.T) {
skipIfNoSymlinks(t)
ctx := context.Background()
outer := t.TempDir()
newLinksFs := func(dir, encoding string) *Fs {
fRaw, err := NewFs(ctx, "local", filepath.Join(outer, dir), configmap.Simple{"encoding": encoding})
require.NoError(t, err)
f := fRaw.(*Fs)
linksMode(f)
require.NoError(t, f.Mkdir(ctx, ""))
return f
}
f := newLinksFs("dst", encoder.OS.String())
s := newLinksFs("slash", "Slash")
// Stamp the parent of the destinations so a write to it shows up.
before := fstest.Time("2011-12-25T12:59:59Z")
require.NoError(t, os.Chtimes(outer, before, before))
parentUnchanged := func() {
t.Helper()
fi, err := os.Lstat(outer)
require.NoError(t, err)
assert.True(t, fi.ModTime().Equal(before), "the parent of the destination was modified")
}
// A directory called "...rclonelink" is created under its own name
// and its metadata goes there, not to the parent of the root which
// trimming the suffix off its path would address.
remote := ".." + fs.LinkSuffix
metadata := fs.Metadata{"mtime": "2001-02-03T04:05:06Z"}
_, err := f.MkdirMetadata(ctx, remote, metadata)
require.NoError(t, err)
parentUnchanged()
// A file called "...rclonelink" is a link called "..", which the
// default encoding stores as fullwidth dots like any other name.
require.NoError(t, putFile(ctx, f, remote, "target"))
fi, err := os.Lstat(filepath.Join(f.root, ".."))
require.NoError(t, err)
assert.NotZero(t, fi.Mode()&os.ModeSymlink)
parentUnchanged()
// Ordinary translated links still resolve to the file without the suffix.
o, err := f.newObject("sub/link" + fs.LinkSuffix)
require.NoError(t, err)
assert.True(t, o.translatedLink)
assert.Equal(t, filepath.Join(f.root, "sub", "link"), o.path)
// A name which merely contains the suffix is a regular file.
o, err = f.newObject("sub/link" + fs.LinkSuffix + ".txt")
require.NoError(t, err)
assert.False(t, o.translatedLink)
assert.Equal(t, filepath.Join(f.root, "sub", "link"+fs.LinkSuffix+".txt"), o.path)
// An encoding without Dot leaves ".." as a real ".." once the suffix
// is gone, so the name is refused instead.
require.ErrorIs(t, putFile(ctx, s, remote, "PWNED"), errLinkNameInvalid)
_, err = s.MkdirMetadata(ctx, remote, metadata)
require.NoError(t, err)
parentUnchanged()
// As is any other name which doesn't leave a file name once the
// suffix is removed and the encoding applied.
for _, name := range []string{
remote,
"sub/.." + fs.LinkSuffix,
"sub/." + fs.LinkSuffix,
"sub/.." + fs.LinkSuffix,
"sub/sub2/.." + fs.LinkSuffix,
"sub/." + fs.LinkSuffix,
"sub/" + fs.LinkSuffix,
fs.LinkSuffix,
} {
_, err = s.newObject(name)
assert.ErrorIs(t, err, errLinkNameInvalid, name)
assert.True(t, fserrors.IsNoRetryError(err), name)
}
// The root check still applies to the name of the link.
_, err = s.newObject("../link" + fs.LinkSuffix)
require.ErrorIs(t, err, errPathEscapes)
if runtime.GOOS == "windows" {
// A backslash is a path separator on Windows, so an encoding
// without BackSlash can hide ".." behind one.
w := newLinksFs("win", "Slash,Dot")
_, err = w.newObject(`sub\..` + fs.LinkSuffix)
assert.ErrorIs(t, err, errLinkNameInvalid)
}
}
// TestLinkSuffixListSkipsInvalidName checks that with --links a regular
// file whose name is just the suffix is reported as an error and skipped
// rather than failing the listing of its whole directory.
func TestLinkSuffixListSkipsInvalidName(t *testing.T) {
ctx := context.Background()
r := fstest.NewRun(t)
f := r.Flocal.(*Fs)
linksMode(f)
when := fstest.Time("2001-02-03T04:05:06Z")
r.WriteFile("sub/"+fs.LinkSuffix, "not a link", when)
r.WriteFile("sub/file.txt", "hello", when)
accounting.Stats(ctx).ResetErrors()
entries, err := f.List(ctx, "sub")
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "sub/file.txt", entries[0].Remote())
assert.Equal(t, int64(1), accounting.Stats(ctx).GetErrors())
accounting.Stats(ctx).ResetErrors()
}
// TestLinkSuffixDirectory checks that in --links mode a directory whose
// name ends in ".rclonelink" is an ordinary directory. Only files are
// translated into links, so the suffix must stay in its path rather
// than making it address a sibling.
func TestLinkSuffixDirectory(t *testing.T) {
ctx := context.Background()
r := fstest.NewRun(t)
f := r.Flocal.(*Fs)
linksMode(f)
dir := "sub" + fs.LinkSuffix
want := filepath.Join(f.root, dir)
mtime := fstest.Time("2001-02-03T04:05:06Z")
d, err := f.MkdirMetadata(ctx, dir, fs.Metadata{"mtime": mtime.Format(time.RFC3339Nano)})
require.NoError(t, err)
assert.Equal(t, want, d.(*Directory).path)
fi, err := os.Lstat(want)
require.NoError(t, err)
assert.True(t, fi.IsDir())
assert.True(t, fi.ModTime().Equal(mtime), "metadata was not written to the directory")
entries, err := f.List(ctx, "")
require.NoError(t, err)
require.Len(t, entries, 1)
got, ok := entries[0].(*Directory)
require.True(t, ok)
assert.Equal(t, dir, got.Remote())
assert.Equal(t, want, got.path)
assert.False(t, got.translatedLink)
}
func TestHashWithTypeNone(t *testing.T) {
ctx := context.Background()
r := fstest.NewRun(t)
@@ -683,6 +846,7 @@ func TestHashOnDelete(t *testing.T) {
}
func TestMetadata(t *testing.T) {
skipIfNoSymlinks(t)
ctx := context.Background()
r := fstest.NewRun(t)
const filePath = "metafile.txt"
@@ -959,6 +1123,7 @@ func TestFilter(t *testing.T) {
}
func testFilterSymlink(t *testing.T, copyLinks bool) {
skipIfNoSymlinks(t)
ctx := context.Background()
r := fstest.NewRun(t)
defer r.Finalise()
@@ -1063,6 +1228,7 @@ func TestFilterSymlinkLinks(t *testing.T) {
}
func TestCopySymlink(t *testing.T) {
skipIfNoSymlinks(t)
ctx := context.Background()
r := fstest.NewRun(t)
defer r.Finalise()
+7 -5
View File
@@ -236,9 +236,9 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
if err != nil {
return nil, fmt.Errorf("couldn't parse URL %q: %w", opt.Endpoint, err)
}
u, err := rest.URLJoin(base, rest.URLPathEscape(root))
u, err := rest.URLJoinRoot(base, root)
if err != nil {
return nil, fmt.Errorf("couldn't join URL %q and %q: %w", base.String(), root, err)
return nil, err
}
client := fshttp.NewClient(ctx)
@@ -573,10 +573,12 @@ func (f *Fs) ListR(ctx context.Context, dir string, callback fs.ListRCallback) (
fs.Errorf(nil, "Unable to parse URL %q: %v", f.endpointURL, err)
return fs.ErrorDirNotFound
}
resumeURL, err := rest.URLJoin(u, rest.URLPathEscape(resumeStart))
// A bad resume start must fail the listing rather than
// return fs.ErrorDirNotFound which sync treats as an empty
// directory
resumeURL, err := rest.URLJoinRoot(u, resumeStart)
if err != nil {
fs.Errorf(nil, "Unable to join URL %q for resumeStart %s: %v", f.endpointURL, resumeStart, err)
return fs.ErrorDirNotFound
return fmt.Errorf("unable to join URL %q for resumeStart %q: %w", f.endpointURL, resumeStart, err)
}
URL = resumeURL.String()
}
@@ -0,0 +1,61 @@
package netstorage
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/obscure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListResumeCannotChangeHost(t *testing.T) {
// The resume start returned by a list request comes from the
// server. It must not be able to move the signed continuation
// request to another host.
var requests int
other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests++
http.Error(w, "not found", http.StatusNotFound)
}))
defer other.Close()
otherHost := strings.TrimPrefix(other.URL, "http://")
var listRequests int
configured := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
action := r.Header.Get("X-Akamai-ACS-Action")
switch {
case strings.Contains(action, "action=stat"):
_, _ = w.Write([]byte(`<stat directory="/123456/dir"><file type="dir" name="dir" mtime="0"/></stat>`))
case strings.Contains(action, "action=list"):
listRequests++
_, _ = w.Write([]byte(`<list><file type="file" name="123456/dir/a.txt" size="1" mtime="0"/><resume start="//` + otherHost + `/123456/dir/b.txt"/></list>`))
default:
http.Error(w, "unexpected action "+action, http.StatusBadRequest)
}
}))
defer configured.Close()
m := configmap.Simple{
"type": "netstorage",
"protocol": "http",
"host": strings.TrimPrefix(configured.URL, "http://") + "/123456/",
"account": "account",
"secret": obscure.MustObscure("secret"),
}
f, err := NewFs(context.Background(), "TestNetStorage", "dir", m)
require.NoError(t, err)
// The listing must fail loudly rather than with
// fs.ErrorDirNotFound which sync treats as an empty directory
err = f.(*Fs).ListR(context.Background(), "", func(entries fs.DirEntries) error { return nil })
assert.ErrorContains(t, err, "must not change the scheme, host or user")
assert.NotErrorIs(t, err, fs.ErrorDirNotFound)
assert.Equal(t, 1, listRequests, "list request sent to the configured host")
assert.Equal(t, 0, requests, "request sent to the other host")
}
+5 -5
View File
@@ -20,7 +20,7 @@ import (
const (
dirMimeType = "inode/directory"
timeFormatIn = time.RFC3339
timeFormatOut = "2006-01-02T15:04:05.999Z" // mS for OneDrive Personal, otherwise only S
timeFormatOut = "2006-01-02T15:04:05.999Z" // keeps mS if present, though OneDrive only stores S
)
// system metadata keys which this backend owns
@@ -32,17 +32,17 @@ var systemMetadataInfo = map[string]fs.MetadataHelp{
ReadOnly: true,
},
"mtime": {
Help: "Time of last modification with S accuracy (mS for OneDrive Personal).",
Help: "Time of last modification with S accuracy.",
Type: "RFC 3339",
Example: "2006-01-02T15:04:05Z",
},
"btime": {
Help: "Time of file birth (creation) with S accuracy (mS for OneDrive Personal).",
Help: "Time of file birth (creation) with S accuracy.",
Type: "RFC 3339",
Example: "2006-01-02T15:04:05Z",
},
"utime": {
Help: "Time of upload with S accuracy (mS for OneDrive Personal).",
Help: "Time of upload with S accuracy.",
Type: "RFC 3339",
Example: "2006-01-02T15:04:05Z",
ReadOnly: true,
@@ -113,7 +113,7 @@ var systemMetadataInfo = map[string]fs.MetadataHelp{
ReadOnly: true,
},
"shared-time": {
Help: "Time when the item was shared, with S accuracy (mS for OneDrive Personal).",
Help: "Time when the item was shared, with S accuracy.",
Type: "RFC 3339",
Example: "2006-01-02T15:04:05Z",
ReadOnly: true,
+8 -8
View File
@@ -275,16 +275,13 @@ cases, rclone will fall back to normal copy (which will be slightly slower).`,
Default: false,
Help: `Remove all versions on modifying operations.
Onedrive for business creates versions when rclone uploads new files
Onedrive creates versions when rclone uploads new files
overwriting an existing one and when it sets the modification time.
These versions take up space out of the quota.
This flag checks for versions after file upload and setting
modification time and removes all but the last version.
**NB** Onedrive personal can't currently delete versions so don't use
this flag there.
`,
Advanced: true,
}, {
@@ -294,8 +291,7 @@ this flag there.
Normally files will get sent to the recycle bin on deletion. Setting
this flag causes them to be permanently deleted. Use with care.
OneDrive personal accounts do not support the permanentDelete API,
it only applies to OneDrive for Business and SharePoint document libraries.
This works with OneDrive for Business, SharePoint document libraries, and OneDrive personal accounts, including free accounts.
`,
Advanced: true,
Default: false,
@@ -324,14 +320,18 @@ it only applies to OneDrive for Business and SharePoint document libraries.
Help: "Creates a read-write link to the item.",
}, {
Value: "embed",
Help: "Creates an embeddable link to the item.",
Help: "Creates an embeddable link to the item.\nOnly available in OneDrive personal.",
}},
}, {
Name: "link_password",
Default: "",
Help: `Set the password for links created by the link command.
At the time of writing this only works with OneDrive personal paid accounts.
At the time of writing this works with OneDrive for Business and
OneDrive personal paid accounts.
OneDrive personal free accounts can't set a password or an expiry time
(with --expire) on links.
`,
Advanced: true,
Sensitive: true,
+3
View File
@@ -116,11 +116,14 @@ func useBYOKCopyObject(fs *Fs, request *objectstorage.CopyObjectRequest) {
}
if fs.opt.SSECustomerAlgorithm != "" {
request.OpcSseCustomerAlgorithm = common.String(fs.opt.SSECustomerAlgorithm)
request.OpcSourceSseCustomerAlgorithm = common.String(fs.opt.SSECustomerAlgorithm)
}
if fs.opt.SSECustomerKey != "" {
request.OpcSseCustomerKey = common.String(fs.opt.SSECustomerKey)
request.OpcSourceSseCustomerKey = common.String(fs.opt.SSECustomerKey)
}
if fs.opt.SSECustomerKeySha256 != "" {
request.OpcSseCustomerKeySha256 = common.String(fs.opt.SSECustomerKeySha256)
request.OpcSourceSseCustomerKeySha256 = common.String(fs.opt.SSECustomerKeySha256)
}
}
+25
View File
@@ -0,0 +1,25 @@
//go:build !plan9 && !solaris && !js
package oracleobjectstorage
import (
"testing"
"github.com/oracle/oci-go-sdk/v65/objectstorage"
"github.com/stretchr/testify/assert"
)
func TestUseBYOKCopyObject(t *testing.T) {
f := &Fs{opt: Options{
SSECustomerAlgorithm: "AES256",
SSECustomerKey: "customer-key",
SSECustomerKeySha256: "customer-key-sha256",
}}
req := &objectstorage.CopyObjectRequest{}
useBYOKCopyObject(f, req)
assert.Equal(t, &f.opt.SSECustomerAlgorithm, req.OpcSourceSseCustomerAlgorithm)
assert.Equal(t, &f.opt.SSECustomerKey, req.OpcSourceSseCustomerKey)
assert.Equal(t, &f.opt.SSECustomerKeySha256, req.OpcSourceSseCustomerKeySha256)
}
+4
View File
@@ -3,6 +3,7 @@
package oracleobjectstorage
import (
"bufio"
"bytes"
"context"
"encoding/base64"
@@ -396,6 +397,9 @@ func isZeroLength(streamReader io.Reader) bool {
return v.Len() == 0
case *strings.Reader:
return v.Len() == 0
case *bufio.Reader:
_, err := v.Peek(1)
return err == io.EOF
case *os.File:
fi, err := v.Stat()
if err != nil {
@@ -4,6 +4,7 @@
package oracleobjectstorage
import (
"bufio"
"context"
"fmt"
"io"
@@ -553,7 +554,7 @@ func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options .
// PutStream uploads to the remote path with the modTime given of indeterminate size
func (f *Fs) PutStream(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
return f.Put(ctx, in, src, options...)
return f.Put(ctx, bufio.NewReader(in), src, options...)
}
// Mkdir creates the bucket if it doesn't exist
@@ -3,11 +3,16 @@
package oracleobjectstorage
import (
"bufio"
"bytes"
"compress/gzip"
"context"
"crypto/md5"
"errors"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/rclone/rclone/fs"
@@ -15,6 +20,7 @@ import (
"github.com/rclone/rclone/fstest"
"github.com/rclone/rclone/fstest/fstests"
"github.com/rclone/rclone/lib/random"
"github.com/rclone/rclone/lib/readers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -31,6 +37,51 @@ func TestIntegration(t *testing.T) {
})
}
func TestIsZeroLength(t *testing.T) {
newFile := func(contents string) *os.File {
file, err := os.CreateTemp(t.TempDir(), "stream")
require.NoError(t, err)
_, err = file.WriteString(contents)
require.NoError(t, err)
_, err = file.Seek(0, io.SeekStart)
require.NoError(t, err)
t.Cleanup(func() { _ = file.Close() })
return file
}
closedFile := newFile("")
require.NoError(t, closedFile.Close())
for _, test := range []struct {
name string
in io.Reader
want bool
wantContents string
}{
{name: "EmptyBuffer", in: bytes.NewBuffer(nil), want: true},
{name: "NonEmptyBuffer", in: bytes.NewBufferString("contents"), want: false},
{name: "EmptyBytesReader", in: bytes.NewReader(nil), want: true},
{name: "NonEmptyBytesReader", in: bytes.NewReader([]byte("contents")), want: false},
{name: "EmptyStringsReader", in: strings.NewReader(""), want: true},
{name: "NonEmptyStringsReader", in: strings.NewReader("contents"), want: false},
{name: "EmptyBufferedStream", in: bufio.NewReader(io.TeeReader(bytes.NewReader(nil), io.Discard)), want: true},
{name: "NonEmptyBufferedStream", in: bufio.NewReader(io.TeeReader(bytes.NewBufferString("contents"), io.Discard)), want: false, wantContents: "contents"},
{name: "BufferedStreamError", in: bufio.NewReader(readers.ErrorReader{Err: errors.New("stream error")}), want: false},
{name: "EmptyFile", in: newFile(""), want: true},
{name: "NonEmptyFile", in: newFile("contents"), want: false},
{name: "ClosedFile", in: closedFile, want: false},
{name: "OpaqueEmptyStream", in: io.TeeReader(bytes.NewReader(nil), io.Discard), want: false},
} {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.want, isZeroLength(test.in))
if test.wantContents != "" {
contents, err := io.ReadAll(test.in)
require.NoError(t, err)
assert.Equal(t, test.wantContents, string(contents))
}
})
}
}
func gz(t *testing.T, s string) string {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
+21 -4
View File
@@ -14,6 +14,7 @@ import (
"net/http"
"net/url"
"path"
"regexp"
"strconv"
"strings"
"time"
@@ -56,6 +57,10 @@ var (
ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
RedirectURL: oauthutil.RedirectLocalhostURL,
}
// hostnameRe matches a lower case API hostname within pCloud's
// domain, eg api.pcloud.com or eapi.pcloud.com
hostnameRe = regexp.MustCompile(`^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+pcloud\.com$`)
)
// Update the TokenURL with the actual hostname
@@ -63,6 +68,19 @@ func updateTokenURL(oauthConfig *oauthutil.Config, hostname string) {
oauthConfig.TokenURL = "https://" + hostname + "/oauth2_token"
}
// callbackHostname checks and returns the API hostname pCloud sent back in the
// OAuth callback, lower cased, or defaultHostname if it sent none.
func callbackHostname(hostname string) (string, error) {
if hostname == "" {
return defaultHostname, nil
}
hostname = strings.ToLower(hostname)
if !hostnameRe.MatchString(hostname) {
return "", fmt.Errorf("invalid hostname %q in oauth response", hostname)
}
return hostname, nil
}
// Register with Fs
func init() {
updateTokenURL(oauthConfig, defaultHostname)
@@ -81,9 +99,9 @@ func init() {
if auth == nil || auth.Form == nil {
return errors.New("form not found in response")
}
hostname := auth.Form.Get("hostname")
if hostname == "" {
hostname = defaultHostname
hostname, err := callbackHostname(auth.Form.Get("hostname"))
if err != nil {
return err
}
// Save the hostname in the config
m.Set("hostname", hostname)
@@ -95,7 +113,6 @@ func init() {
return oauthutil.ConfigOut("", &oauthutil.Options{
OAuth2Config: oauthConfig,
CheckAuth: checkAuth,
StateBlankOK: true, // pCloud seems to drop the state parameter now - see #4210
})
},
Options: append(oauthutil.SharedOptions, []fs.Option{{
+46
View File
@@ -0,0 +1,46 @@
package pcloud
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestCallbackHostname checks that only hosts within pCloud's domain
// are accepted.
func TestCallbackHostname(t *testing.T) {
for _, test := range []struct {
in string
want string // "" means an error is expected
}{
{"", defaultHostname},
{"api.pcloud.com", "api.pcloud.com"},
{"eapi.pcloud.com", "eapi.pcloud.com"},
{"EAPI.pCloud.COM", "eapi.pcloud.com"},
{"new-region.api.pcloud.com", "new-region.api.pcloud.com"},
{"pcloud.com", ""},
{".pcloud.com", ""},
{"-.pcloud.com", ""},
{"a-.pcloud.com", ""},
{"a..pcloud.com", ""},
{"api.pcloud.com.", ""},
{"api.pclоud.com", ""}, // Cyrillic о
{"127.0.0.1:18443", ""},
{"evil.example.com", ""},
{"api.pcloud.com.evil.example.com", ""},
{"api.pcloud.com:443", ""},
{"api.pcloud.com/path", ""},
{"user@evil.example.com#.pcloud.com", ""},
{"evil.example.com?.pcloud.com", ""},
} {
t.Run("["+test.in+"]", func(t *testing.T) {
got, err := callbackHostname(test.in)
if test.want == "" {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.want, got)
})
}
}
+1 -1
View File
@@ -213,7 +213,7 @@ func (f *Fs) put(
// exist yet
params.Set("make_parents", "true")
return node, f.pacer.Call(func() (bool, error) {
return node, f.pacer.CallNoRetry(func() (bool, error) {
resp, err := f.srv.CallJSON(
ctx,
&rest.Opts{
+32 -2
View File
@@ -993,6 +993,13 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
}()
}
// The upload server truncates the file name at the first ";" so
// upload under a temporary name and rename it afterwards.
uploadLeaf := leaf
if strings.Contains(leaf, ";") {
uploadLeaf = "rclone-upload-" + random.String(16)
}
opts = rest.Opts{
Method: "POST",
RootURL: info.URL,
@@ -1000,8 +1007,8 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
MultipartParams: url.Values{
"token": {info.Token},
},
MultipartContentName: "file", // ..name of the parameter which is the attached file
MultipartFileName: leaf, // ..name of the file for the attached file
MultipartContentName: "file", // ..name of the parameter which is the attached file
MultipartFileName: uploadLeaf, // ..name of the file for the attached file
ContentLength: &size,
}
var result api.Response
@@ -1016,6 +1023,29 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
return fmt.Errorf("upload file: %w", err)
}
if uploadLeaf != leaf {
var newID string
var found bool
_, found, err = o.fs.listAll(ctx, directoryID, false, true, func(item *api.Item) bool {
if item.Name == uploadLeaf {
newID = item.ID
return true
}
return false
})
if err != nil {
return fmt.Errorf("upload find temporary file: %w", err)
}
if !found {
return fmt.Errorf("upload find temporary file %q: %w", uploadLeaf, fs.ErrorObjectNotFound)
}
err = o.fs.renameLeaf(ctx, true, newID, leaf)
if err != nil {
_ = o.fs.remove(ctx, newID)
return fmt.Errorf("upload rename temporary file: %w", err)
}
}
// on successful upload, remove old file if it exists
uploaded = true
if o.hasMetaData {
+4
View File
@@ -19,3 +19,7 @@ quirks:
force_path_style: true
list_url_encode: false
use_already_exists: false
# Ceph RGW (and reverse proxies such as Cloudflare in front of it) can
# mutate Accept-Encoding and break SigV4 when that header is signed.
# See: https://github.com/rclone/rclone/issues/8206
sign_accept_encoding: false
+5
View File
@@ -24,3 +24,8 @@ endpoint:
us-iad-10.linodeobjects.com: Washington, DC, US (us-iad-10)
acl: {}
bucket_acl: true
quirks:
# Linode Object Storage is Ceph-backed; signing Accept-Encoding causes
# SignatureDoesNotMatch with rclone 1.68+. Match the Ceph quirk.
# See: https://github.com/rclone/rclone/issues/8206
sign_accept_encoding: false
+41 -36
View File
@@ -1357,10 +1357,10 @@ func getClient(ctx context.Context, opt *Options) *http.Client {
}
// s3RedirectSecretHeaders are the request headers carrying origin-bound
// secrets that must not be forwarded when a redirect crosses a host or
// downgrades the scheme. Go strips Authorization on a hostname change but not
// on a scheme downgrade, and has no knowledge that the SSE-C headers hold raw
// encryption keys, so we strip them all ourselves.
// secrets that must not be forwarded when a redirect leaves the original
// host. Go strips Authorization on a hostname change but keeps it for a
// subdomain or a different port, and has no knowledge that the SSE-C
// headers hold raw encryption keys, so we strip them all ourselves.
var s3RedirectSecretHeaders = []string{
"X-Amz-Security-Token", // AWS STS session token
"X-Amz-S3session-Token", // S3 Express (directory bucket) session token
@@ -1375,36 +1375,10 @@ var s3RedirectSecretHeaders = []string{
"Referer", // may be a presigned request
}
func s3CheckRedirect(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
// Never follow a redirect that downgrades the transport from HTTPS to
// HTTP. An S3 endpoint has no legitimate reason to do this, and replaying
// the request over plaintext would expose whatever credential it carries.
if via[len(via)-1].URL.Scheme == "https" && req.URL.Scheme == "http" {
return fmt.Errorf("refusing to follow insecure redirect from HTTPS to HTTP: %s", req.URL.Redacted())
}
if s3RedirectCrossesHost(req, via) {
for _, header := range s3RedirectSecretHeaders {
req.Header.Del(header)
}
}
return nil
}
func s3RedirectCrossesHost(req *http.Request, via []*http.Request) bool {
if len(via) == 0 {
return false
}
scheme, host := via[0].URL.Scheme, via[0].URL.Host
for _, redirect := range via[1:] {
if redirect.URL.Host != host || redirect.URL.Scheme != scheme {
return true
}
}
return host != req.URL.Host || scheme != req.URL.Scheme
}
// s3CheckRedirect is the http.Client CheckRedirect policy for S3. It
// refuses HTTPS to HTTP downgrades and strips s3RedirectSecretHeaders
// once a redirect has left the original host.
var s3CheckRedirect = rest.StripHeadersOnCrossHostRedirectFn(s3RedirectSecretHeaders...)
// Fixup the request if needed.
//
@@ -2317,7 +2291,31 @@ func versionLess(a, b *types.ObjectVersion) bool {
// types.ObjectVersion with Size = isDeleteMarker to tell them apart
//
// We then merge them back into the Versions in the correct order
func mergeDeleteMarkers(oldVersions []types.ObjectVersion, deleteMarkers []types.DeleteMarkerEntry) (newVersions []types.ObjectVersion) {
func mergeDeleteMarkers(oldVersions []types.ObjectVersion, deleteMarkers []types.DeleteMarkerEntry, urlEncoded bool) (newVersions []types.ObjectVersion) {
encodedKeys := make(map[string]string)
if urlEncoded {
// URL encoding can change key order, so compare decoded keys and restore the encoded keys for the caller.
oldVersions = append([]types.ObjectVersion(nil), oldVersions...)
deleteMarkers = append([]types.DeleteMarkerEntry(nil), deleteMarkers...)
decodeKey := func(key **string) {
if *key == nil {
return
}
encodedKey := **key
decodedKey, err := url.QueryUnescape(encodedKey)
if err != nil {
return
}
encodedKeys[decodedKey] = encodedKey
*key = &decodedKey
}
for i := range oldVersions {
decodeKey(&oldVersions[i].Key)
}
for i := range deleteMarkers {
decodeKey(&deleteMarkers[i].Key)
}
}
newVersions = make([]types.ObjectVersion, 0, len(oldVersions)+len(deleteMarkers))
for _, deleteMarker := range deleteMarkers {
var obj types.ObjectVersion
@@ -2332,6 +2330,11 @@ func mergeDeleteMarkers(oldVersions []types.ObjectVersion, deleteMarkers []types
}
// Merge any remaining versions
newVersions = append(newVersions, oldVersions...)
for i := range newVersions {
if encodedKey, ok := encodedKeys[deref(newVersions[i].Key)]; ok {
newVersions[i].Key = &encodedKey
}
}
return newVersions
}
@@ -2371,7 +2374,7 @@ func (ls *versionsList) List(ctx context.Context) (resp *s3.ListObjectsV2Output,
// Merge in delete Markers as types.ObjectVersion if we need them
if ls.hidden || ls.usingVersionAt {
respVersions.Versions = mergeDeleteMarkers(respVersions.Versions, respVersions.DeleteMarkers)
respVersions.Versions = mergeDeleteMarkers(respVersions.Versions, respVersions.DeleteMarkers, ls.req.EncodingType == types.EncodingTypeUrl)
}
// Convert the Versions and the DeleteMarkers into an array of types.Object
@@ -2398,6 +2401,8 @@ func (ls *versionsList) List(ctx context.Context) (resp *s3.ListObjectsV2Output,
var obj types.Object
//structs.SetFrom(obj, objVersion)
setFrom_typesObject_typesObjectVersion(&obj, &objVersion)
// StorageClass has a different type in ObjectVersion so isn't copied by setFrom
obj.StorageClass = types.ObjectStorageClass(objVersion.StorageClass)
// Adjust the file names
if !ls.usingVersionAt && (!deref(objVersion.IsLatest) || objVersion.Size == isDeleteMarker) {
if obj.Key != nil && objVersion.LastModified != nil {
+57 -1
View File
@@ -7,6 +7,9 @@ import (
"crypto/md5"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"path"
"strings"
"testing"
@@ -302,11 +305,64 @@ func TestMergeDeleteMarkers(t *testing.T) {
},
},
} {
got := mergeDeleteMarkers(test.versions, test.markers)
got := mergeDeleteMarkers(test.versions, test.markers, false)
assert.Equal(t, test.want, got, fmt.Sprintf("%d: %+v", n, test))
}
}
func TestMergeDeleteMarkersWithURLEncodedKeys(t *testing.T) {
plainKey := "images/reservations/photo.png"
encodedKey := "images/%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82-01.jpg"
t1 := fstest.Time("2022-01-21T12:00:00+01:00")
t2 := fstest.Time("2022-01-21T12:00:01+01:00")
versions := []types.ObjectVersion{
{Key: &plainKey, LastModified: &t2},
{Key: &encodedKey, LastModified: &t1},
}
markers := []types.DeleteMarkerEntry{
{Key: &encodedKey, LastModified: &t2},
}
got := mergeDeleteMarkers(versions, markers, true)
want := []types.ObjectVersion{
{Key: &plainKey, LastModified: &t2},
{Key: &encodedKey, LastModified: &t2, Size: isDeleteMarker},
{Key: &encodedKey, LastModified: &t1},
}
assert.Equal(t, want, got)
}
func TestVersionsListStorageClass(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, `<ListVersionsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Name>bucket</Name>
<IsTruncated>false</IsTruncated>
<Version>
<Key>archived.bin</Key>
<VersionId>v1</VersionId>
<IsLatest>true</IsLatest>
<LastModified>2026-09-26T12:00:00.000Z</LastModified>
<ETag>"d41d8cd98f00b204e9800998ecf8427e"</ETag>
<Size>0</Size>
<StorageClass>DEEP_ARCHIVE</StorageClass>
</Version>
</ListVersionsResult>`)
}))
defer srv.Close()
f := &Fs{c: s3.New(s3.Options{
Region: "us-east-1",
BaseEndpoint: aws.String(srv.URL),
UsePathStyle: true,
Credentials: aws.AnonymousCredentials{},
})}
ls := f.newVersionsList(&s3.ListObjectsV2Input{Bucket: aws.String("bucket")}, false, time.Time{})
resp, _, err := ls.List(context.Background())
require.NoError(t, err)
require.Len(t, resp.Contents, 1)
assert.Equal(t, types.ObjectStorageClassDeepArchive, resp.Contents[0].StorageClass)
}
func TestRemoveAWSChunked(t *testing.T) {
ps := func(s string) *string {
return &s
-48
View File
@@ -228,54 +228,6 @@ func TestS3CheckRedirectRejectsSchemeDowngrade(t *testing.T) {
}
}
func TestRedirectCrossesHost(t *testing.T) {
mustReq := func(method, url string) *http.Request {
req, err := http.NewRequest(method, url, nil)
require.NoError(t, err)
return req
}
for _, test := range []struct {
name string
via []string
req string
want bool
}{
{
name: "SameHost",
via: []string{"https://bucket.example.com/"},
req: "https://bucket.example.com/redirected",
want: false,
},
{
name: "DifferentHost",
via: []string{"https://bucket.example.com/"},
req: "https://evil.example.com/redirected",
want: true,
},
{
name: "SchemeDowngradeSameHost",
via: []string{"https://bucket.example.com/"},
req: "http://bucket.example.com/redirected",
want: true,
},
{
name: "SchemeDowngradeMidChain",
via: []string{"https://bucket.example.com/", "http://bucket.example.com/middle"},
req: "http://bucket.example.com/final",
want: true,
},
} {
t.Run(test.name, func(t *testing.T) {
via := make([]*http.Request, len(test.via))
for i, url := range test.via {
via[i] = mustReq(http.MethodGet, url)
}
got := s3RedirectCrossesHost(mustReq(http.MethodGet, test.req), via)
assert.Equal(t, test.want, got)
})
}
}
func TestClientStopsAfterTenRedirects(t *testing.T) {
_, _, client := SetupS3Test(t)
+15 -21
View File
@@ -88,28 +88,22 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadClo
// But for unknown-sized objects (indicated by src.Size() == -1), Upload should either
// return an error or update the object properly (rather than e.g. calling panic).
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
// The upload sometimes return a temporary 500 error
// We cannot use the pacer to retry uploading the file as the upload link is single use only
for retry := 0; retry <= 3; retry++ {
uploadLink, err := o.fs.getUploadLink(ctx, o.libraryID)
if err != nil {
return err
}
uploaded, err := o.fs.upload(ctx, in, uploadLink, o.pathInLibrary)
if err == ErrorInternalDuringUpload {
// This is a temporary error, try again with a new upload link
continue
}
if err != nil {
return err
}
// Set the properties from the upload back to the object
o.size = uploaded.Size
return nil
uploadLink, err := o.fs.getUploadLink(ctx, o.libraryID)
if err != nil {
return err
}
return ErrorInternalDuringUpload
// The upload can't be retried here as the input stream can't be re-read and
// the upload link is single use, so upload returns a retry
// error for the caller to retry with a fresh stream.
uploaded, err := o.fs.upload(ctx, in, uploadLink, o.pathInLibrary)
if err != nil {
return err
}
// Set the properties from the upload back to the object
o.size = uploaded.Size
return nil
}
// Remove this object
+3 -2
View File
@@ -13,6 +13,7 @@ import (
"github.com/rclone/rclone/backend/seafile/api"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/fserrors"
"github.com/rclone/rclone/lib/readers"
"github.com/rclone/rclone/lib/rest"
)
@@ -721,8 +722,8 @@ func (f *Fs) upload(ctx context.Context, in io.Reader, uploadLink, filePath stri
return nil, fs.ErrorPermissionDenied
}
if resp.StatusCode == 500 {
// This is a temporary error - we will get a new upload link before retrying
return nil, ErrorInternalDuringUpload
// This is a temporary error - the caller will get a new upload link when it retries
return nil, fserrors.RetryError(ErrorInternalDuringUpload)
}
}
return nil, fmt.Errorf("failed to upload file: %w", err)
+2
View File
@@ -2791,6 +2791,8 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
}
_, err = file.ReadFrom(&sizeReader{Reader: in, size: src.Size()})
if err != nil {
// Close the handle otherwise the server keeps the removed file's space allocated
_ = file.Close()
o.fs.putSftpConnection(&c, err)
remove()
return fmt.Errorf("Update ReadFrom failed: %w", err)
+5 -3
View File
@@ -380,9 +380,11 @@ func (f *Fs) DirMove(ctx context.Context, src fs.Fs, srcRemote, dstRemote string
//Need to check if destination exists
fullPath := f.buildFullPath(dstRemote)
var response api.ListDirResponse
res, _ := f.callAPI(ctx, "GET", fmt.Sprintf("/%s/fs/attr?path=%s", f.drive, fullPath), &response)
if res.StatusCode != http.StatusNotFound {
res, err := f.callAPI(ctx, "GET", fmt.Sprintf("/%s/fs/attr?path=%s", f.drive, fullPath), &response)
if res == nil || res.StatusCode != http.StatusNotFound {
if err != nil {
return fmt.Errorf("failed to check destination directory: %w", err)
}
return fs.ErrorDirExists
}
+4 -2
View File
@@ -49,8 +49,7 @@ func init() {
Sensitive: true,
}, {
Name: "user",
Help: "SMB username.",
Default: currentUser,
Help: "SMB username.\n\nLeave blank to use the current user name.",
Sensitive: true,
}, {
Name: "port",
@@ -191,6 +190,9 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
if err != nil {
return nil, err
}
if opt.User == "" {
opt.User = currentUser
}
// if root is empty or ends with / (must be a directory)
isRootDir := isPathDir(root)
+31
View File
@@ -12,6 +12,7 @@ import (
"time"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/object"
"github.com/rclone/rclone/fstest"
"github.com/stretchr/testify/assert"
@@ -129,3 +130,33 @@ func TestIsPathDir(t *testing.T) {
})
}
}
func TestNewFsUser(t *testing.T) {
ctx := context.Background()
for _, test := range []struct {
user string
want string
}{
{user: "", want: currentUser},
{user: currentUser, want: currentUser},
{user: "someone", want: "someone"},
} {
m := configmap.Simple{"host": "localhost", "user": test.user}
f, err := NewFs(ctx, "TestSMB", "", m)
require.NoError(t, err)
assert.Equal(t, test.want, f.(*Fs).opt.User, "user=%q", test.user)
}
// No user in the config at all
f, err := NewFs(ctx, "TestSMB", "", configmap.Simple{"host": "localhost"})
require.NoError(t, err)
assert.Equal(t, currentUser, f.(*Fs).opt.User)
}
// The default is blank so that a user name which happens to match the
// current user is still written to the config file.
func TestUserDefault(t *testing.T) {
ri, err := fs.Find("smb")
require.NoError(t, err)
assert.Equal(t, "", ri.Options.Get("user").Default)
}
+76 -5
View File
@@ -135,6 +135,13 @@ The input format is comma separated list of key,value pairs. Standard
For example, to set a Cookie use 'Cookie,name=value', or '"Cookie","name=value"'.
You can set multiple headers, e.g. '"Cookie","name=value","Authorization","xxx"'.
The headers are only sent to the host in the configured URL. If the
server redirects to another host (including a subdomain or a different
port) the headers are not sent to it, or to any further hop in that
redirect chain. The exception is the directory listing and file
download requests when auth_redirect is set, as that keeps all
credentials across redirects.
`,
Default: fs.CommaSepList{},
Advanced: true,
@@ -235,6 +242,8 @@ type Fs struct {
canChunk bool // set if nextcloud and nextcloud_chunk_size is set
canRecalcHash bool // set if the server can recalculate checksums with PATCH (nextcloud)
authSingleflight *singleflight.Group
// redirect policy which keeps opt.Headers on the configured host
checkRedirect func(req *http.Request, via []*http.Request) error
}
// Object describes a webdav object
@@ -345,6 +354,41 @@ func itemIsDir(item *api.Response) bool {
return false
}
// sabreNotFound is the exception SabreDAV based servers report for a
// path which doesn't exist
var sabreNotFound = []byte(`Sabre\DAV\Exception\NotFound`)
// callPropfind does the PROPFIND in opts and decodes the response into result
//
// ownCloud 10.16 starts a 207 Multi-Status response before it finds
// that the path doesn't exist, then appends a Sabre NotFound error to
// it which makes it invalid XML. This is returned as the 404 *api.Error
// which servers normally give for a path which doesn't exist.
func (f *Fs) callPropfind(ctx context.Context, opts *rest.Opts, result *api.Multistatus) (resp *http.Response, err error) {
resp, err = f.srv.Call(ctx, opts)
if err != nil {
return resp, err
}
// Keep the start of the body to look for the error in
start := make([]byte, 4096)
n, _ := io.ReadFull(resp.Body, start)
start = start[:n]
body := resp.Body
resp.Body = struct {
io.Reader
io.Closer
}{io.MultiReader(bytes.NewReader(start), body), body}
err = rest.DecodeXML(resp, result)
if err != nil && bytes.Contains(start, sabreNotFound) {
return resp, &api.Error{
Exception: string(sabreNotFound),
Status: http.StatusText(http.StatusNotFound),
StatusCode: http.StatusNotFound,
}
}
return resp, err
}
// readMetaDataForPath reads the metadata from the path
func (f *Fs) readMetaDataForPath(ctx context.Context, path string) (info *api.Prop, err error) {
// FIXME how do we read back additional properties?
@@ -354,7 +398,7 @@ func (f *Fs) readMetaDataForPath(ctx context.Context, path string) (info *api.Pr
ExtraHeaders: map[string]string{
"Depth": "0",
},
CheckRedirect: rest.PreserveMethodRedirectFn,
CheckRedirect: f.preserveMethodRedirect,
}
if f.hasOCMD5 || f.hasOCSHA1 {
opts.Body = bytes.NewBuffer(owncloudProps)
@@ -365,7 +409,11 @@ func (f *Fs) readMetaDataForPath(ctx context.Context, path string) (info *api.Pr
var result api.Multistatus
var resp *http.Response
err = f.pacer.Call(func() (bool, error) {
resp, err = f.srv.CallXML(ctx, &opts, nil, &result)
var attempt api.Multistatus
resp, err = f.callPropfind(ctx, &opts, &attempt)
if err == nil {
result = attempt
}
return f.shouldRetry(ctx, resp, err)
})
if apiErr, ok := err.(*api.Error); ok {
@@ -516,8 +564,17 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
rt: ntlmssp.Negotiator{RoundTripper: t},
}
}
// Refuse redirects that downgrade HTTPS to plaintext HTTP.
client.CheckRedirect = rest.RefuseHTTPSDowngradeRedirectFn
// Refuse redirects that downgrade HTTPS to plaintext HTTP and
// don't send the configured headers to another host.
//
// The auth_redirect option bypasses this for the requests it applies
// to as it is an explicit opt-in to keep credentials across redirects.
headerNames := make([]string, 0, len(opt.Headers)/2)
for i := 0; i < len(opt.Headers); i += 2 {
headerNames = append(headerNames, opt.Headers[i])
}
f.checkRedirect = rest.StripHeadersOnCrossHostRedirectFn(headerNames...)
client.CheckRedirect = f.checkRedirect
f.srv = rest.NewClient(client).SetRoot(u.String())
f.features = (&fs.Features{
@@ -596,6 +653,16 @@ func (f *Fs) fetchBearerToken(cmd fs.SpaceSepList) (string, error) {
return stdoutString, nil
}
// preserveMethodRedirect is an http.Client.CheckRedirect function
// which applies f.checkRedirect and then preserves the original HTTP
// method like rest.PreserveMethodRedirectFn.
func (f *Fs) preserveMethodRedirect(req *http.Request, via []*http.Request) error {
if err := f.checkRedirect(req, via); err != nil {
return err
}
return rest.PreserveMethodRedirectFn(req, via)
}
// Adds the configured headers to the request if any
func (f *Fs) addHeaders(headers fs.CommaSepList) {
for i := 0; i < len(headers); i += 2 {
@@ -815,7 +882,11 @@ func (f *Fs) listAll(ctx context.Context, dir string, directoriesOnly bool, file
var result api.Multistatus
var resp *http.Response
err = f.pacer.Call(func() (bool, error) {
resp, err = f.srv.CallXML(ctx, &opts, nil, &result)
var attempt api.Multistatus
resp, err = f.callPropfind(ctx, &opts, &attempt)
if err == nil {
result = attempt
}
return f.shouldRetry(ctx, resp, err)
})
if err != nil {
+227
View File
@@ -3,9 +3,12 @@ package webdav_test
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"github.com/rclone/rclone/backend/webdav"
@@ -120,6 +123,114 @@ func TestListAllAuthRedirect(t *testing.T) {
assert.NotEmpty(t, targetAuth, "Authorization header should be preserved across redirect")
}
// fileInfoResponse is a PROPFIND response describing a single file.
const fileInfoResponse = `<d:multistatus xmlns:d="DAV:">
<d:response>
<d:href>/file.txt</d:href>
<d:propstat>
<d:prop>
<d:getcontentlength>10</d:getcontentlength>
<d:resourcetype/>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
</d:multistatus>`
// redirectTestHeaders are the headers option for the redirect tests.
// The one starting with "*" is sent without canonicalising.
var redirectTestHeaders = []string{"X-Potato", "sausage", "*x-marrow", "pea"}
// redirectHeaders makes a webdav remote with redirectTestHeaders
// pointing at a server which redirects every request to a second
// server on hostname, or back to itself if hostname is empty, and
// exercises each of the backend's redirect policies against it: the
// PROPFIND used to look up an object, the GET used to read it and the
// PROPFIND used to list a directory. It returns the headers the
// redirect target saw on each of those requests.
func redirectHeaders(t *testing.T, hostname string) []http.Header {
var target string
var mu sync.Mutex
var seen []http.Header
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/moved/") {
http.Redirect(w, r, target+"/moved"+r.URL.Path, http.StatusTemporaryRedirect)
return
}
mu.Lock()
seen = append(seen, r.Header.Clone())
mu.Unlock()
switch r.Method {
case "PROPFIND":
w.WriteHeader(http.StatusMultiStatus)
_, err := fmt.Fprint(w, fileInfoResponse)
assert.NoError(t, err)
case http.MethodGet:
_, err := fmt.Fprint(w, "hello")
assert.NoError(t, err)
default:
http.Error(w, "unexpected method", http.StatusMethodNotAllowed)
}
})
ts := httptest.NewServer(handler)
defer ts.Close()
if hostname == "" {
target = ts.URL
} else {
// Serve the final response from a second server on a different host
other := httptest.NewServer(handler)
defer other.Close()
target = strings.Replace(other.URL, "127.0.0.1", hostname, 1)
}
configfile.Install()
m := configmap.Simple{
"type": "webdav",
"url": ts.URL,
"headers": strings.Join(redirectTestHeaders, ","),
}
ctx := context.Background()
f, err := webdav.NewFs(ctx, remoteName, "", m)
require.NoError(t, err)
// PROPFIND with Depth 0 uses rest.PreserveMethodRedirectFn
o, err := f.NewObject(ctx, "file.txt")
require.NoError(t, err)
// GET uses the client's default redirect policy
fd, err := o.Open(ctx)
require.NoError(t, err)
data, err := io.ReadAll(fd)
require.NoError(t, err)
require.NoError(t, fd.Close())
assert.Equal(t, "hello", string(data))
// PROPFIND with Depth 1 uses the client's default redirect policy
_, err = f.List(ctx, "")
require.NoError(t, err)
mu.Lock()
defer mu.Unlock()
require.Len(t, seen, 3, "expected the redirect target to see all three requests")
return seen
}
func TestRedirectKeepsHeadersOnSameHost(t *testing.T) {
for _, got := range redirectHeaders(t, "") {
for i := 0; i < len(redirectTestHeaders); i += 2 {
assert.Equal(t, redirectTestHeaders[i+1], got.Get(strings.TrimPrefix(redirectTestHeaders[i], "*")))
}
}
}
func TestRedirectStripsHeadersOnHostChange(t *testing.T) {
// Redirect to a different host name and port. net/http strips
// Authorization on its own here so this checks the custom headers.
for _, got := range redirectHeaders(t, "localhost") {
for i := 0; i < len(redirectTestHeaders); i += 2 {
assert.Empty(t, got.Values(strings.TrimPrefix(redirectTestHeaders[i], "*")), "header %q leaked to redirect target", redirectTestHeaders[i])
}
}
}
// TestReservedCharactersInPathAreEscaped verifies that reserved characters
// like semicolons and equals signs in file paths are percent-encoded in
// HTTP requests to the WebDAV server (RFC 3986 compliance).
@@ -151,3 +262,119 @@ func TestReservedCharactersInPathAreEscaped(t *testing.T) {
assert.Contains(t, capturedPath, "my%3Btest", "semicolons in path should be percent-encoded")
assert.NotContains(t, capturedPath, "my;test", "raw semicolons should not appear in path")
}
// TestListAllRetryDoesNotConcatenate verifies that when a listing PROPFIND
// fails after having partially decoded and is retried, the final listing is
// exactly the retried response, with no entries carried over from the
// failed attempt.
func TestListAllRetryDoesNotConcatenate(t *testing.T) {
entryXML := func(i int) string {
name := fmt.Sprintf("file-%03d.bin", i)
return "<d:response><d:href>/" + name + "</d:href><d:propstat><d:prop>" +
"<d:displayname>" + name + "</d:displayname><d:getcontentlength>1024</d:getcontentlength>" +
"</d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>\n"
}
head := `<?xml version="1.0" encoding="utf-8"?><d:multistatus xmlns:d="DAV:">`
self := head + `<d:response><d:href>/</d:href><d:propstat><d:prop><d:resourcetype><d:collection xmlns:d="DAV:"/></d:resourcetype></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response></d:multistatus>`
writeEntries := func(w http.ResponseWriter, from, to int) {
_, err := fmt.Fprint(w, head)
require.NoError(t, err)
for i := from; i <= to; i++ {
_, err := fmt.Fprint(w, entryXML(i))
require.NoError(t, err)
}
}
var listCalls atomic.Int64
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "PROPFIND", r.Method)
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
if r.Header.Get("Depth") == "0" {
w.WriteHeader(207)
_, err := fmt.Fprint(w, self)
require.NoError(t, err)
return
}
if listCalls.Add(1) == 1 {
// return file-000..file-010 but kill the response before it
// completes, so the client has parsed those entries when it
// hits an unexpected EOF
w.Header().Set("Content-Length", "1000000")
w.WriteHeader(207)
writeEntries(w, 0, 10)
return
}
// return the disjoint file-011..file-030 as a complete listing
w.WriteHeader(207)
writeEntries(w, 11, 30)
_, err := fmt.Fprint(w, "</d:multistatus>")
require.NoError(t, err)
}))
defer ts.Close()
configfile.Install()
m := configmap.Simple{
"type": "webdav",
"url": ts.URL,
}
f, err := webdav.NewFs(context.Background(), remoteName, "", m)
require.NoError(t, err)
entries, err := f.List(context.Background(), "")
require.NoError(t, err)
var remotes []string
for _, e := range entries {
remotes = append(remotes, e.Remote())
}
var want []string
for i := 11; i <= 30; i++ {
want = append(want, fmt.Sprintf("file-%03d.bin", i))
}
assert.ElementsMatch(t, want, remotes)
}
// TestNotFoundInMultistatus checks that a missing path is detected when the
// server reports it by appending a Sabre NotFound error to a 207 response it
// has already started, as ownCloud 10.16 does, rather than by returning 404.
func TestNotFoundInMultistatus(t *testing.T) {
head := `<?xml version="1.0"?>` + "\n" + `<d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:oc="http://owncloud.org/ns"`
root := head + `><d:response><d:href>/</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response></d:multistatus>`
notFound := head + `<?xml version="1.0" encoding="utf-8"?>
<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
<s:exception>Sabre\DAV\Exception\NotFound</s:exception>
<s:message>File with name missing could not be located</s:message>
</d:error>
`
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "PROPFIND", r.Method)
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
w.WriteHeader(207)
body := notFound
if r.URL.Path == "/" {
body = root
}
_, err := fmt.Fprint(w, body)
require.NoError(t, err)
}))
defer ts.Close()
configfile.Install()
m := configmap.Simple{
"type": "webdav",
"url": ts.URL,
}
ctx := context.Background()
// A root which doesn't exist yet
_, err := webdav.NewFs(ctx, remoteName, "missing", m)
require.NoError(t, err)
f, err := webdav.NewFs(ctx, remoteName, "", m)
require.NoError(t, err)
_, err = f.NewObject(ctx, "missing")
assert.Equal(t, fs.ErrorObjectNotFound, err)
_, err = f.List(ctx, "missing")
assert.Equal(t, fs.ErrorDirNotFound, err)
}
+43
View File
@@ -4,6 +4,8 @@ import (
"archive/zip"
"bytes"
"context"
"os"
"path/filepath"
"strings"
"testing"
@@ -134,6 +136,47 @@ func testArchiveRemote(t *testing.T, fromLocal bool, subDir string, extension st
fstest.CheckListingWithPrecision(t, src, items, nil, fs.ModTimeNotSupported)
}
// test archiving to stdout when no destination is given
func TestArchiveStdout(t *testing.T) {
ctx := context.Background()
r := fstest.NewRun(t)
f1 := r.WriteObject(ctx, "file1.txt", "content 1", t1)
f2 := r.WriteObject(ctx, "dir1/sub1.txt", "sub content 1", t1)
fstest.CheckItems(t, r.Fremote, f1, f2)
// redirect stdout into a file on the local fs
archiveName := "stdout.zip"
require.NoError(t, r.Flocal.Mkdir(ctx, ""))
out, err := os.Create(filepath.Join(r.LocalName, archiveName))
require.NoError(t, err)
oldStdout := os.Stdout
os.Stdout = out
err = create.ArchiveCreate(ctx, nil, "", r.Fremote, "zip", "")
os.Stdout = oldStdout
require.NoError(t, out.Close())
require.NoError(t, err)
// check the archive written to stdout is readable
expected := map[string]bool{
"file1.txt": true,
"dir1/": true,
"dir1/sub1.txt": true,
}
listFile := func(ctx context.Context, f archives.FileInfo) error {
name := f.NameInArchive
if f.IsDir() && !strings.HasSuffix(name, "/") {
name += "/"
}
assert.True(t, expected[name], name)
delete(expected, name)
return nil
}
err = list.ArchiveList(ctx, r.Flocal, archiveName, listFile)
require.NoError(t, err)
assert.Equal(t, 0, len(expected), expected)
}
func testArchive(t *testing.T) {
var extensions = []string{
"zip",
+6 -5
View File
@@ -118,7 +118,6 @@ will make an archive with the contents:
cmd.CheckArgs(1, 2, command, args)
}
cmd.Run(false, false, command, func() error {
fmt.Printf("dst=%v, dstFile=%q, src=%v, format=%q, prefix=%q\n", dst, dstFile, src, format, prefix)
if prefix != "" {
return ArchiveCreate(context.Background(), dst, dstFile, src, format, prefix)
} else if fullPath {
@@ -301,10 +300,12 @@ func ArchiveCreate(ctx context.Context, dst fs.Fs, dstFile string, src fs.Fs, fo
var compArchive archives.CompressedArchive
var totalLength int64
// check id dst is valid
err = CheckValidDestination(ctx, dst, dstFile)
if err != nil {
return err
// check if dst is valid, nil dst means write to stdout
if dst != nil {
err = CheckValidDestination(ctx, dst, dstFile)
if err != nil {
return err
}
}
ci := fs.GetConfig(ctx)
+1 -1
View File
@@ -69,7 +69,7 @@ var initDate = time.Date(2000, time.January, 1, 0, 0, 0, 0, bisync.TZ)
// go run ./fstest/test_all -remotes local,TestCrypt:,TestDrive:,TestOneDrive:,TestOneDriveBusiness:,TestDropbox:,TestCryptDrive:,TestOpenDrive:,TestChunker:,:memory:,TestCryptNoEncryption:,TestCombine:DirA,TestFTPRclone:,TestWebdavRclone:,TestS3Rclone:,TestSFTPRclone:,TestSFTPRcloneSSH:,TestNextcloud:,TestChunkerNometaLocal:,TestChunkerChunk3bLocal:,TestChunkerLocal:,TestChunkerChunk3bNometaLocal:,TestStorj: -run '^TestBisync.*$' -timeout 3h -verbose -maxtries 5
// go test -timeout 3h -run '^TestBisync.*$' github.com/rclone/rclone/cmd/bisync -remote TestDrive:Bisync -v
// go test -timeout 3h -run '^TestBisyncRemoteRemote/basic$' github.com/rclone/rclone/cmd/bisync -remote TestDropbox:Bisync -v
// TestFTPProftpd:,TestFTPPureftpd:,TestFTPRclone:,TestFTPVsftpd:,TestHdfs:,TestS3Minio:,TestS3MinioEdge:,TestS3Rclone:,TestSeafile:,TestSeafileEncrypted:,TestSeafileV6:,TestSFTPOpenssh:,TestSFTPRclone:,TestSFTPRcloneSSH:,TestSia:,TestSwiftAIO:,TestWebdavNextcloud:,TestWebdavOwncloud:,TestWebdavRclone:
// TestFTPProftpd:,TestFTPPureftpd:,TestFTPRclone:,TestFTPVsftpd:,TestHdfs:,TestS3Minio:,TestS3Rclone:,TestSeafile:,TestSeafileEncrypted:,TestSeafileV6:,TestSFTPOpenssh:,TestSFTPRclone:,TestSFTPRcloneSSH:,TestSia:,TestSwiftAIO:,TestWebdavNextcloud:,TestWebdavOwncloud:,TestWebdavRclone:
// logReplacements make modern test logs comparable with golden dir.
// It is a string slice of even length with this structure:
-648
View File
@@ -26785,654 +26785,6 @@
"program": "./cmd/bisync",
"args": ["-remote", "TestS3Minio:", "-remote2", "TestS3Minio:", "-case", "test_volatile", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_all_changed LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_all_changed", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_all_changed RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_all_changed", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_all_changed RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_all_changed", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_backupdir LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_backupdir", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_backupdir RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_backupdir", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_backupdir RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_backupdir", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_basic LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_basic", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_basic RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_basic", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_basic RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_basic", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_changes LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_changes", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_changes RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_changes", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_changes RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_changes", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_access LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_check_access", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_access RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_check_access", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_access RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_check_access", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_access_filters LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_check_access_filters", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_access_filters RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_check_access_filters", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_access_filters RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_check_access_filters", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_filename LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_check_filename", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_filename RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_check_filename", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_filename RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_check_filename", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_sync LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_check_sync", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_sync RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_check_sync", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_check_sync RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_check_sync", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_compare_all LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_compare_all", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_compare_all RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_compare_all", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_compare_all RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_compare_all", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_createemptysrcdirs LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_createemptysrcdirs", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_createemptysrcdirs RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_createemptysrcdirs", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_createemptysrcdirs RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_createemptysrcdirs", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_dry_run LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_dry_run", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_dry_run RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_dry_run", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_dry_run RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_dry_run", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_equal LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_equal", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_equal RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_equal", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_equal RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_equal", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_ext_paths LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_ext_paths", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_ext_paths RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_ext_paths", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_ext_paths RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_ext_paths", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_extended_filenames LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_extended_filenames", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_extended_filenames RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_extended_filenames", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_extended_filenames RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_extended_filenames", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_filters LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_filters", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_filters RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_filters", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_filters RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_filters", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_filtersfile_checks LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_filtersfile_checks", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_filtersfile_checks RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_filtersfile_checks", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_filtersfile_checks RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_filtersfile_checks", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_ignorelistingchecksum LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_ignorelistingchecksum", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_ignorelistingchecksum RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_ignorelistingchecksum", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_ignorelistingchecksum RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_ignorelistingchecksum", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_max_delete_path1 LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_max_delete_path1", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_max_delete_path1 RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_max_delete_path1", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_max_delete_path1 RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_max_delete_path1", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_max_delete_path2_force LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_max_delete_path2_force", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_max_delete_path2_force RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_max_delete_path2_force", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_max_delete_path2_force RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_max_delete_path2_force", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_nomodtime LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_nomodtime", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_nomodtime RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_nomodtime", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_nomodtime RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_nomodtime", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_normalization LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_normalization", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_normalization RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_normalization", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_normalization RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_normalization", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_rclone_args LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_rclone_args", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_rclone_args RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_rclone_args", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_rclone_args RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_rclone_args", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_resolve LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_resolve", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_resolve RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_resolve", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_resolve RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_resolve", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_resync LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_resync", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_resync RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_resync", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_resync RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_resync", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_resync_modes LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_resync_modes", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_resync_modes RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_resync_modes", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_resync_modes RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_resync_modes", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_rmdirs LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_rmdirs", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_rmdirs RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_rmdirs", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_rmdirs RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_rmdirs", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_volatile LocalRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "local", "-remote2", "TestS3MinioEdge:", "-case", "test_volatile", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_volatile RemoteLocal",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "local", "-case", "test_volatile", "-no-cleanup"]
},
{
"name": "Test TestS3MinioEdge: test_volatile RemoteRemote",
"type": "go",
"request": "launch",
"mode": "test",
"program": "./cmd/bisync",
"args": ["-remote", "TestS3MinioEdge:", "-remote2", "TestS3MinioEdge:", "-case", "test_volatile", "-no-cleanup"]
},
{
"name": "Test TestS3Wasabi: test_all_changed LocalRemote",
"type": "go",
+23 -2
View File
@@ -379,6 +379,27 @@ func StartStats() func() {
}
}
var (
rcServer *rcserver.Server
metricsServer *rcserver.MetricsServer
)
// StopRemoteControl shuts down the remote control and metrics servers started by initConfig.
func StopRemoteControl() {
if rcServer != nil {
if err := rcServer.Shutdown(); err != nil {
fs.Errorf(nil, "Failed to stop remote control: %v", err)
}
rcServer = nil
}
if metricsServer != nil {
if err := metricsServer.Shutdown(); err != nil {
fs.Errorf(nil, "Failed to stop metrics server: %v", err)
}
metricsServer = nil
}
}
// initConfig is run by cobra after initialising the flags
func initConfig() {
// Set the global options from the flags
@@ -422,14 +443,14 @@ func initConfig() {
}
// Start the remote control server if configured
_, err = rcserver.Start(ctx, &rc.Opt)
rcServer, err = rcserver.Start(ctx, &rc.Opt)
if err != nil {
fs.Fatalf(nil, "Failed to start remote control: %v", err)
}
// Start the metrics server if configured and not running the "rc" command
if len(os.Args) >= 2 && os.Args[1] != "rc" {
_, err = rcserver.MetricsStart(ctx, &rc.Opt)
metricsServer, err = rcserver.MetricsStart(ctx, &rc.Opt)
if err != nil {
fs.Fatalf(nil, "Failed to start metrics server: %v", err)
}
+52
View File
@@ -0,0 +1,52 @@
package cmd
import (
"context"
"net"
"testing"
"github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/fs/rc/rcserver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func freeAddr(t *testing.T) string {
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
addr := l.Addr().String()
require.NoError(t, l.Close())
return addr
}
func TestStopRemoteControl(t *testing.T) {
ctx := context.Background()
rcAddr, metricsAddr := freeAddr(t), freeAddr(t)
opt := rc.Opt
opt.Enabled = true
opt.HTTP.ListenAddr = []string{rcAddr}
opt.MetricsHTTP.ListenAddr = []string{metricsAddr}
var err error
rcServer, err = rcserver.Start(ctx, &opt)
require.NoError(t, err)
metricsServer, err = rcserver.MetricsStart(ctx, &opt)
require.NoError(t, err)
require.NotNil(t, rcServer)
require.NotNil(t, metricsServer)
for _, addr := range []string{rcAddr, metricsAddr} {
_, err := net.Listen("tcp", addr)
require.Error(t, err, "%s should be in use while the servers run", addr)
}
StopRemoteControl()
assert.Nil(t, rcServer)
assert.Nil(t, metricsServer)
for _, addr := range []string{rcAddr, metricsAddr} {
l, err := net.Listen("tcp", addr)
require.NoError(t, err, "%s should be free after StopRemoteControl", addr)
require.NoError(t, l.Close())
}
}
+29
View File
@@ -536,27 +536,56 @@ func (fsys *FS) Fsyncdir(path string, datasync bool, fh uint64) (errc int) {
return 0
}
// #9908: simulate macFUSE behaviour (discard setxattr calls) - otherwise Finder cannot copy files onto the mount
func shouldIgnoreAppleXattr(opt *mountlib.Options) bool {
if !opt.NoAppleXattr {
return false
}
if runtime.GOOS != "darwin" {
return false
}
// only flip the behaviour on FSKit
for _, o := range opt.ExtraOptions {
if strings.TrimSpace(o) == "backend=fskit" {
return true
}
}
return false
}
// Setxattr sets extended attributes.
func (fsys *FS) Setxattr(path string, name string, value []byte, flags int) (errc int) {
defer log.Trace(path, "name=%q, value=%q, flags=%d", name, value, flags)("errc=%d", &errc)
if shouldIgnoreAppleXattr(fsys.opt) && strings.HasPrefix(name, "com.apple.") {
return 0
}
return -fuse.ENOSYS
}
// Getxattr gets extended attributes.
func (fsys *FS) Getxattr(path string, name string) (errc int, value []byte) {
defer log.Trace(path, "name=%q", name)("errc=%d, value=%q", &errc, &value)
if shouldIgnoreAppleXattr(fsys.opt) && strings.HasPrefix(name, "com.apple.") {
return -fuse.ENOATTR, nil
}
return -fuse.ENOSYS, nil
}
// Removexattr removes extended attributes.
func (fsys *FS) Removexattr(path string, name string) (errc int) {
defer log.Trace(path, "name=%q", name)("errc=%d", &errc)
if shouldIgnoreAppleXattr(fsys.opt) && strings.HasPrefix(name, "com.apple.") {
return 0
}
return -fuse.ENOSYS
}
// Listxattr lists extended attributes.
func (fsys *FS) Listxattr(path string, fill func(name string) bool) (errc int) {
defer log.Trace(path, "fill=%p", fill)("errc=%d", &errc)
if shouldIgnoreAppleXattr(fsys.opt) {
return 0
}
return -fuse.ENOSYS
}
+4
View File
@@ -26,6 +26,10 @@ Note that |ls| and |lsl| recurse by default - use |--max-depth 1| to stop the re
The other list commands |lsd|,|lsf|,|lsjson| do not recurse by default -
use |-R| to make them recurse.
On the bucket-based remotes (e.g. s3, gcs, b2) a directory is a key prefix
rather than an object, so a listing without |-R| shows each prefix as one
directory entry and not the objects stored under it.
List commands prefer a recursive method that uses more memory but fewer
transactions by default. Use |--disable ListR| to suppress the behavior.
See [|--fast-list|](/docs/#fast-list) for more details.
+4
View File
@@ -299,6 +299,10 @@ func NewMountCommand(commandName string, hidden bool, mount MountFn) *cobra.Comm
if Opt.Daemon {
config.PassConfigKeyForDaemonization = true
if !fs.IsDaemon() {
// The daemon child starts these servers on the same addresses
cmd.StopRemoteControl()
}
}
if os.Getenv("PATH") == "" && runtime.GOOS != "windows" {
+17 -7
View File
@@ -133,18 +133,28 @@ func GetVersion(ctx context.Context, beta bool, version string) (newVersion, sit
if err != nil {
return "", siteURL, fmt.Errorf("failed to get list of releases: %w", err)
}
reSubver := fmt.Sprintf(`href="\./%s\.\d+/"`, regexp.QuoteMeta(newVersion))
allSubvers := regexp.MustCompile(reSubver).FindAllString(string(html), -1)
if allSubvers == nil {
return "", siteURL, errors.New("could not find the minor release")
newVersion, err = latestMicroRelease(html, newVersion)
if err != nil {
return "", siteURL, err
}
// Use the fact that releases in the index are sorted by date
lastSubver := allSubvers[len(allSubvers)-1]
newVersion = lastSubver[8 : len(lastSubver)-2]
}
return
}
// latestMicroRelease returns the latest micro release of the
// major.minor release version linked to from the HTML index of
// releases.
func latestMicroRelease(html []byte, version string) (string, error) {
// The links may or may not start with ./ depending on what made the index
reSubver := fmt.Sprintf(`href="(?:\./)?(%s\.\d+)/"`, regexp.QuoteMeta(version))
allSubvers := regexp.MustCompile(reSubver).FindAllSubmatch(html, -1)
if allSubvers == nil {
return "", errors.New("could not find the minor release")
}
// Use the fact that releases in the index are sorted by date
return string(allSubvers[len(allSubvers)-1][1]), nil
}
// InstallUpdate performs rclone self-update
func InstallUpdate(ctx context.Context, opt *Options) error {
// Find the latest release number
+51 -1
View File
@@ -45,6 +45,50 @@ func TestGetVersion(t *testing.T) {
assert.Equal(t, "v1.52.3", resultVer)
}
func TestLatestMicroRelease(t *testing.T) {
for _, test := range []struct {
name string
html string
want string
}{
{
name: "caddy",
html: `<a href="./v1.51.0/">v1.51.0/</a>
<a href="./v1.52.0/">v1.52.0/</a>
<a href="./v1.52.3/">v1.52.3/</a>
<a href="./v1.53.0/">v1.53.0/</a>
<a href="./v1.52-DEV/">v1.52-DEV/</a>
<a href="./v1.52.3/rclone-v1.52.3-linux-amd64.zip">rclone</a>`,
want: "v1.52.3",
},
{
name: "no prefix",
html: `<a href="v1.51.0/">v1.51.0/</a>
<a href="v1.52.0/">v1.52.0/</a>
<a href="v1.52.3/">v1.52.3/</a>
<a href="v1.53.0/">v1.53.0/</a>`,
want: "v1.52.3",
},
{
name: "other prefix",
html: `<a href="../v1.52.0/">v1.52.0/</a>
<a href="https://example.com/v1.52.1/">v1.52.1/</a>
<a href="v1.52.2/index.html">v1.52.2/</a>
<a href="v1x52.3/">v1x52.3/</a>`,
},
} {
t.Run(test.name, func(t *testing.T) {
got, err := latestMicroRelease([]byte(test.html), "v1.52")
if test.want == "" {
assert.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, test.want, got)
})
}
}
func TestInstallOnLinux(t *testing.T) {
testy.SkipUnreliable(t)
if runtime.GOOS != "linux" {
@@ -56,6 +100,12 @@ func TestInstallOnLinux(t *testing.T) {
testDir := t.TempDir()
path := filepath.Join(testDir, "rclone")
// Pin the running version so the checks below do not depend on
// whether this build happens to be the latest beta.
oldVersion := fs.Version
fs.Version = "v1.0.0"
t.Cleanup(func() { fs.Version = oldVersion })
regexVer := regexp.MustCompile(`v[0-9]\S+`)
betaVer, _, err := GetVersion(ctx, true, "")
@@ -71,7 +121,7 @@ func TestInstallOnLinux(t *testing.T) {
_ = os.Chmod(path, 0644)
}()
err = (InstallUpdate(ctx, &Options{Beta: true, Output: path}))
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "run self-update as root")
// Must keep non-standard permissions
+2 -1
View File
@@ -86,7 +86,8 @@ func verifyHashsumDownloaded(ctx context.Context, sumsBuf []byte, archive string
return fmt.Errorf("invalid hashsum signature: %w", err)
}
wantHash, err := findFileHash(sumsBuf, archive)
// Only read hashes from the the signed text
wantHash, err := findFileHash(block.Bytes, archive)
if err != nil {
return err
}
+19
View File
@@ -6,6 +6,7 @@ import (
"context"
"encoding/hex"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -39,4 +40,22 @@ func TestVerify(t *testing.T) {
err = verifyHashsumDownloaded(ctx, sumsBuf, "archive.zipX", hash)
assert.ErrorContains(t, err, "unable to find hash")
})
t.Run("UnsignedPrefix", func(t *testing.T) {
hash[0] ^= 1 // forge an unsigned hash line before the signed block
prefixed := append([]byte(hex.EncodeToString(hash)+" archive.zip\n"), sumsBuf...)
err = verifyHashsumDownloaded(ctx, prefixed, "archive.zip", hash)
assert.ErrorContains(t, err, "archive hash mismatch")
hash[0] ^= 1 // undo the change
})
t.Run("UnsignedInArmor", func(t *testing.T) {
// Dash escaping the signed line doesn't change the signed text but hides
// it from a raw scan, and lines after the armor checksum are ignored.
badHash := hex.EncodeToString([]byte{0x01})
armored := strings.Replace(string(sumsBuf), "b20b", "- b20b", 1)
armored = strings.Replace(armored, "=1GTr\n", "=1GTr\n"+badHash+" archive.zip\n", 1)
err = verifyHashsumDownloaded(ctx, []byte(armored), "archive.zip", []byte{0x01})
assert.ErrorContains(t, err, "archive hash mismatch")
err = verifyHashsumDownloaded(ctx, []byte(armored), "archive.zip", hash)
require.NoError(t, err)
})
}
+1 -1
View File
@@ -153,7 +153,7 @@ func (lrw *loggingResponseWriter) logRequest(code int, err any) {
err = ""
}
fs.LogLevelPrintf(level, lrw.request.URL, "%s %s %d %s %s",
fs.LogLevelPrintf(level, lrw.request.URL.Path, "%s %s %d %s %s",
lrw.request.RemoteAddr, lrw.request.Method, code,
lrw.request.Header.Get("SOAPACTION"), err)
}
+13
View File
@@ -1,8 +1,11 @@
package dlna
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/rclone/rclone/cmd/bisync/bilib"
"github.com/stretchr/testify/assert"
)
@@ -46,3 +49,13 @@ func TestAdjustXML(t *testing.T) {
})
}
}
func TestLoggingUnescapedPath(t *testing.T) {
handler := logging(http.NotFoundHandler())
req := httptest.NewRequest("GET", "/r/%D0%91%D0%B0%D1%80%D0%B4%D1%8B/file%20name.mp3", nil)
logged := bilib.CaptureOutput(func() {
handler.ServeHTTP(httptest.NewRecorder(), req)
})
assert.Contains(t, string(logged), "ERROR : /r/Барды/file name.mp3: ")
}
+50 -2
View File
@@ -6,14 +6,17 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
"time"
@@ -398,6 +401,50 @@ func (a *APIClient) request(path string, in, out any, wantErr bool) {
time.Sleep(tempDelay)
}
// writeMountedFile writes data to a file on a FUSE mount served by this
// process.
//
// It avoids os.WriteFile as files opened with os.OpenFile are handed
// to the Go runtime poller and the kernel then asks the FUSE server
// to poll them from epoll_ctl and epoll_wait. The server lives in
// this process and a thread waiting inside epoll cannot be preempted,
// so a garbage collection starting while a poll is outstanding stops
// the world for good. The poll is never answered, the process cannot
// be killed even with SIGKILL and the mount is left behind.
// See: https://github.com/golang/go/issues/21014
//
// os.NewFile only returns a pollable file for a descriptor which is
// already in non-blocking mode, so a descriptor straight from open(2)
// stays out of the poller.
func writeMountedFile(path string, data []byte, perm uint32) (err error) {
fd, err := syscall.Open(path, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_TRUNC, perm)
if err != nil {
return err
}
f := os.NewFile(uintptr(fd), path)
defer fs.CheckClose(f, &err)
// Deadlines work on polled files only
if err := f.SetDeadline(time.Time{}); !errors.Is(err, os.ErrNoDeadline) {
return fmt.Errorf("%s is being polled by the runtime which risks deadlocking the test: %w", path, err)
}
_, err = f.Write(data)
return err
}
// checkUnmounted fails the test and tears the mount down if mountpoint
// is still mounted.
func checkUnmounted(t *testing.T, mountpoint string) {
// CheckMountReady and fusermount are both Linux only
if !mountlib.CanCheckMountReady || mountlib.CheckMountReady(mountpoint) != nil {
return
}
assert.Fail(t, fmt.Sprintf("mountpoint %s was left mounted", mountpoint))
// Lazy unmount as the mount may still be busy
if out, err := exec.Command("fusermount", "-uz", mountpoint).CombinedOutput(); err != nil {
t.Logf("Failed to unmount %s: %v: %s", mountpoint, err, out)
}
}
func testMountAPI(t *testing.T, sockAddr string) {
// Disable tests under macOS and linux in the CI since they are locking up
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
@@ -412,6 +459,7 @@ func testMountAPI(t *testing.T, sockAddr string) {
testDir, testFs := initialise(ctx, t)
err := config.SetCacheDir(testDir)
require.NoError(t, err)
mount1 := filepath.Join(testDir, "vol1")
defer func() {
_ = config.SetCacheDir(oldCacheDir)
if !t.Failed() {
@@ -419,6 +467,7 @@ func testMountAPI(t *testing.T, sockAddr string) {
_ = os.RemoveAll(testDir)
}
}()
defer checkUnmounted(t, mount1)
// Prepare API client
var cli *APIClient
@@ -457,7 +506,6 @@ func testMountAPI(t *testing.T, sockAddr string) {
// Run test sequence
path1 := filepath.Join(testDir, "path1")
require.NoError(t, file.MkdirAll(path1, 0755))
mount1 := filepath.Join(testDir, "vol1")
res := ""
cli.request("Activate", "{}", &res, false)
@@ -485,7 +533,7 @@ func testMountAPI(t *testing.T, sockAddr string) {
assert.Contains(t, res, "volume is in use")
text := []byte("banana")
err = os.WriteFile(filepath.Join(mount1, "txt"), text, 0644)
err = writeMountedFile(filepath.Join(mount1, "txt"), text, 0644)
assert.NoError(t, err)
time.Sleep(tempDelay)
+3
View File
@@ -140,6 +140,9 @@ func (vol *Volume) restoreState(ctx context.Context, drv *Driver) error {
volOpt := vol.Options
volOpt["fs"] = vol.Fs
volOpt["type"] = vol.Type
// applyOptions consumes "path" into vol.Path rather than leaving it in
// vol.Options, so it must be fed back explicitly like fs and type.
volOpt["path"] = vol.Path
if err := vol.applyOptions(volOpt); err != nil {
return err
}
+99
View File
@@ -0,0 +1,99 @@
package docker
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "github.com/rclone/rclone/backend/local"
)
// persistedCopy returns vol as it comes back from the plugin state file:
// only the exported fields survive being marshalled and reloaded.
func persistedCopy(t *testing.T, vol *Volume) *Volume {
t.Helper()
vol.prepareState()
data, err := json.Marshal(vol)
require.NoError(t, err)
restored := &Volume{}
require.NoError(t, json.Unmarshal(data, restored))
return restored
}
// TestRestoreStatePath checks that the on-remote path of a volume survives
// being reloaded from the plugin state file, for each of the option
// combinations that can carry one.
func TestRestoreStatePath(t *testing.T) {
ctx := context.Background()
for _, test := range []struct {
name string
volOpt VolOpts
fsString string
}{{
name: "type and path",
volOpt: VolOpts{"type": "local", "path": "/tmp/path"},
fsString: ":local:/tmp/path",
}, {
name: "remote only",
volOpt: VolOpts{"remote": "/tmp/remote"},
fsString: ":local:/tmp/remote",
}, {
// an explicit path overrides the path of the connection string,
// and must keep doing so after a restart
name: "remote and path",
volOpt: VolOpts{"remote": "/tmp/remote", "path": "/tmp/path"},
fsString: ":local:/tmp/path",
}, {
name: "type only",
volOpt: VolOpts{"type": "local"},
fsString: ":local:",
}} {
t.Run(test.name, func(t *testing.T) {
drv := &Driver{
root: t.TempDir(),
volumes: map[string]*Volume{},
dummy: true,
}
vol, err := newVolume(ctx, "vol1", test.volOpt, drv)
require.NoError(t, err)
require.Equal(t, test.fsString, vol.fsString)
restored := persistedCopy(t, vol)
require.NoError(t, restored.restoreState(ctx, drv))
assert.Equal(t, test.fsString, restored.fsString)
assert.Equal(t, vol.Path, restored.Path)
})
}
}
// TestDriverRestoreStatePath checks the same through the real state file: a
// volume persisted by a previous plugin instance is reloaded with its path.
func TestDriverRestoreStatePath(t *testing.T) {
ctx := context.Background()
testDir := t.TempDir()
state := fmt.Sprintf(`[{"name":"vol1","mountpoint":%q,"created":%q,"fs":"","type":"local","path":"/tmp/path","options":{},"mounts":[]}]`,
filepath.Join(testDir, "vol1"), time.Now().Format(time.RFC3339))
statePath := filepath.Join(testDir, stateFile)
require.NoError(t, os.WriteFile(statePath, []byte(state), 0600))
drv := &Driver{
root: testDir,
statePath: statePath,
volumes: map[string]*Volume{},
dummy: true,
}
require.NoError(t, drv.restoreState(ctx))
vol, err := drv.getVolume("vol1")
require.NoError(t, err)
assert.Equal(t, ":local:/tmp/path", vol.fsString)
assert.Equal(t, "/tmp/path", vol.Path)
}
+50 -13
View File
@@ -16,6 +16,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/rclone/rclone/cmd"
@@ -189,7 +190,7 @@ func init() {
var passivePortsRe = regexp.MustCompile(`^\s*\d+\s*-\s*\d+\s*$`)
// Make a new FTP to serve the remote
func newServer(ctx context.Context, f fs.Fs, opt *Options, vfsOpt *vfscommon.Options, proxyOpt *proxy.Options) (*driver, error) {
func newServer(ctx context.Context, f fs.Fs, opt *Options, vfsOpt *vfscommon.Options, proxyOpt *proxy.Options) (_ *driver, err error) {
host, port, err := net.SplitHostPort(opt.ListenAddr)
if err != nil {
return nil, fmt.Errorf("failed to parse host:port from %q", opt.ListenAddr)
@@ -358,13 +359,27 @@ func (d *driver) CheckPasswd(sctx *ftp.Context, user, pass string) (ok bool, err
return true, nil
}
// getVFS returns the VFS for this connection.
// getVFS returns the VFS for this connection, held so it can't be
// shut down while it is in use. The caller must call Shutdown on it
// when it has finished with it.
//
// In proxy mode, getVFS calls proxy.Call on each FTP command which refreshes
// the proxy cache timer (like http/webdav). Therefore, connection-level pinning
// is not used; only individual transfers exceeding the cache expiry window
// could be affected.
// In proxy mode, getVFS calls proxy.Call on each FTP command which
// refreshes the proxy cache timer (like http/webdav). The proxy shuts
// the VFS down when that timer expires, which a single transfer can
// outlast, so the timer alone isn't enough to keep the VFS alive.
func (d *driver) getVFS(sctx *ftp.Context) (VFS *vfs.VFS, err error) {
VFS, err = d.findVFS(sctx)
if err != nil {
return nil, err
}
if !VFS.Hold() {
return nil, errors.New("VFS has been shut down")
}
return VFS, nil
}
// findVFS returns the VFS for this connection without holding it.
func (d *driver) findVFS(sctx *ftp.Context) (VFS *vfs.VFS, err error) {
if !d.provider.IsProxy() {
// If no proxy always use the same VFS
return d.provider.VFS(), nil
@@ -392,6 +407,7 @@ func (d *driver) Stat(sctx *ftp.Context, path string) (fi iofs.FileInfo, err err
if err != nil {
return nil, err
}
defer VFS.Shutdown()
n, err := VFS.Stat(path)
if err != nil {
return nil, err
@@ -406,6 +422,7 @@ func (d *driver) ChangeDir(sctx *ftp.Context, path string) (err error) {
if err != nil {
return err
}
defer VFS.Shutdown()
n, err := VFS.Stat(path)
if err != nil {
return err
@@ -423,6 +440,7 @@ func (d *driver) ListDir(sctx *ftp.Context, path string, callback func(iofs.File
if err != nil {
return err
}
defer VFS.Shutdown()
node, err := VFS.Stat(path)
if err == vfs.ENOENT {
return errors.New("directory not found")
@@ -461,6 +479,7 @@ func (d *driver) DeleteDir(sctx *ftp.Context, path string) (err error) {
if err != nil {
return err
}
defer VFS.Shutdown()
node, err := VFS.Stat(path)
if err != nil {
return err
@@ -482,6 +501,7 @@ func (d *driver) DeleteFile(sctx *ftp.Context, path string) (err error) {
if err != nil {
return err
}
defer VFS.Shutdown()
node, err := VFS.Stat(path)
if err != nil {
return err
@@ -503,6 +523,7 @@ func (d *driver) Rename(sctx *ftp.Context, oldName, newName string) (err error)
if err != nil {
return err
}
defer VFS.Shutdown()
return VFS.Rename(oldName, newName)
}
@@ -513,12 +534,8 @@ func (d *driver) MakeDir(sctx *ftp.Context, path string) (err error) {
if err != nil {
return err
}
dir, leaf, err := VFS.StatParent(path)
if err != nil {
return err
}
_, err = dir.Mkdir(leaf)
return err
defer VFS.Shutdown()
return VFS.Mkdir(path, 0777)
}
// GetFile download a file
@@ -528,6 +545,12 @@ func (d *driver) GetFile(sctx *ftp.Context, path string, offset int64) (size int
if err != nil {
return 0, nil, err
}
// The returned file takes over the hold on the VFS
defer func() {
if err != nil {
VFS.Shutdown()
}
}()
node, err := VFS.Stat(path)
if err == vfs.ENOENT {
fs.Infof(path, "File not found")
@@ -552,7 +575,20 @@ func (d *driver) GetFile(sctx *ftp.Context, path string, offset int64) (size int
tr := accounting.GlobalStats().NewTransferRemoteSize(path, node.Size(), d.f, nil)
defer tr.Done(d.ctx, nil)
return node.Size(), handle, nil
return node.Size(), &heldFile{ReadCloser: handle, VFS: VFS}, nil
}
// heldFile is an open file which holds its VFS until it is closed.
type heldFile struct {
io.ReadCloser
VFS *vfs.VFS
release sync.Once
}
// Close closes the file and releases the hold on the VFS.
func (f *heldFile) Close() error {
defer f.release.Do(f.VFS.Shutdown)
return f.ReadCloser.Close()
}
// PutFile upload a file
@@ -564,6 +600,7 @@ func (d *driver) PutFile(sctx *ftp.Context, path string, data io.Reader, offset
if err != nil {
return 0, err
}
defer VFS.Shutdown()
fi, err := VFS.Stat(path)
if err == nil {
isExist = true
+143
View File
@@ -9,8 +9,14 @@ package ftp
import (
"context"
"io"
"os"
"path/filepath"
"sync"
"testing"
"time"
ftpclient "github.com/jlaffaye/ftp"
_ "github.com/rclone/rclone/backend/local"
"github.com/rclone/rclone/cmd/serve/proxy"
"github.com/rclone/rclone/cmd/serve/servetest"
@@ -19,6 +25,8 @@ import (
"github.com/rclone/rclone/fs/config/obscure"
"github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/lib/israce"
"github.com/rclone/rclone/lib/random"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfscommon"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -122,6 +130,32 @@ func TestNewServerPerServerAuthProxy(t *testing.T) {
assert.Nil(t, d.provider.VFS(), "expected no fixed VFS when auth proxy is in use")
}
// TestMakeDirRoot checks that "MKD /" makes the directory being
// served. The ftp backend never sends this so TestFTP doesn't cover it.
func TestMakeDirRoot(t *testing.T) {
root := filepath.Join(t.TempDir(), "root")
f, err := fs.NewFs(context.Background(), root)
require.NoError(t, err)
opt := Opt
opt.ListenAddr = testHOST + ":" + testPORT
opt.PassivePorts = testPASSIVEPORTRANGE
d, err := newServer(context.Background(), f, &opt, &vfscommon.Opt, &proxy.Opt)
require.NoError(t, err)
defer d.provider.Shutdown()
require.NoError(t, d.MakeDir(nil, "/"))
fi, err := os.Stat(root)
require.NoError(t, err)
assert.True(t, fi.IsDir())
require.NoError(t, d.MakeDir(nil, "/dir"))
fi, err = os.Stat(filepath.Join(root, "dir"))
require.NoError(t, err)
assert.True(t, fi.IsDir())
}
func TestRc(t *testing.T) {
if israce.Enabled {
t.Skip("Skipping under race detector as underlying library is racy")
@@ -131,3 +165,112 @@ func TestRc(t *testing.T) {
"vfs_cache_mode": "off",
})
}
// TestNewServerError checks that a server initialisation failure is
// returned as an error and does not leak the VFS it created.
func TestNewServerError(t *testing.T) {
f, err := fs.NewFs(context.Background(), t.TempDir())
require.NoError(t, err)
opt := Opt
opt.ListenAddr = testHOST + ":" + testPORT
opt.PassivePorts = "not-a-port-range"
before := vfs.ActiveCount()
d, err := newServer(context.Background(), f, &opt, &vfscommon.Opt, &proxy.Opt)
require.Error(t, err)
assert.Nil(t, d)
assert.Equal(t, before, vfs.ActiveCount(), "VFS leaked after failed server creation")
}
// TestAuthProxyTransferOutlivesCache checks transfers in progress
// carry on working when the auth proxy drops their VFS from its cache,
// as it does when a transfer takes longer than the cache expiry time.
func TestAuthProxyTransferOutlivesCache(t *testing.T) {
const addr = "127.0.0.1:" + testPORT
root := t.TempDir()
contents := random.String(32 * 1024 * 1024)
require.NoError(t, os.WriteFile(filepath.Join(root, "download.bin"), []byte(contents), 0666))
prog, err := filepath.Abs("../servetest/proxy_code.go")
require.NoError(t, err)
opt := Opt
opt.ListenAddr = addr
opt.PassivePorts = testPASSIVEPORTRANGE
proxyOpt := proxy.Opt
proxyOpt.AuthProxy = "go run " + prog + " " + root
d, err := newServer(context.Background(), nil, &opt, &vfscommon.Opt, &proxyOpt)
require.NoError(t, err)
quit := make(chan struct{})
go func() {
assert.NoError(t, d.Serve())
close(quit)
}()
defer func() {
assert.NoError(t, d.Shutdown())
<-quit
}()
var c *ftpclient.ServerConn
require.Eventually(t, func() bool {
c, err = ftpclient.Dial(addr)
return err == nil
}, 10*time.Second, 10*time.Millisecond)
defer func() { _ = c.Quit() }()
require.NoError(t, c.Login(testUSER, testPASS))
// Only the IP of the address is used, which is the same as the client's
_, vfsKey, err := d.provider.Proxy().Call(testUSER, testPASS, false, addr)
require.NoError(t, err)
// expire waits for a transfer to be using the VFS, then drops
// everything from the proxy's cache as if it had expired.
expire := func(t *testing.T) {
var VFS *vfs.VFS
require.Eventually(t, func() bool {
VFS = d.provider.Proxy().Get(vfsKey)
return VFS != nil && VFS.Stats()["inUse"] == int32(2)
}, 10*time.Second, 10*time.Millisecond, "transfer isn't holding the VFS")
d.provider.Proxy().Shutdown()
assert.Equal(t, int32(1), VFS.Stats()["inUse"], "VFS not held by the transfer alone")
}
t.Run("Download", func(t *testing.T) {
resp, err := c.Retr("download.bin")
require.NoError(t, err)
defer func() { _ = resp.Close() }()
start := make([]byte, 1024)
_, err = io.ReadFull(resp, start)
require.NoError(t, err)
expire(t)
rest, err := io.ReadAll(resp)
require.NoError(t, err)
require.NoError(t, resp.Close())
assert.True(t, contents == string(start)+string(rest), "download corrupted")
})
t.Run("Upload", func(t *testing.T) {
pr, pw := io.Pipe()
var storErr error
var wg sync.WaitGroup
wg.Go(func() {
storErr = c.Stor("upload.bin", pr)
})
// Finish the upload before the connection is used again
defer func() {
_ = pw.Close()
wg.Wait()
}()
_, err := io.WriteString(pw, contents[:1024*1024])
require.NoError(t, err)
expire(t)
_, err = io.WriteString(pw, contents[1024*1024:])
require.NoError(t, err)
require.NoError(t, pw.Close())
wg.Wait()
require.NoError(t, storErr)
got, err := os.ReadFile(filepath.Join(root, "upload.bin"))
require.NoError(t, err)
assert.True(t, contents == string(got), "upload corrupted")
})
}
+5 -3
View File
@@ -6,6 +6,7 @@ import (
_ "embed"
"fmt"
"io"
"mime"
"net"
"net/http"
"os"
@@ -170,8 +171,8 @@ func (s *HTTP) auth(r *http.Request, user, pass string) (value any, err error) {
return VFS, err
}
func newServer(ctx context.Context, f fs.Fs, opt *Options, vfsOpt *vfscommon.Options, proxyOpt *proxy.Options) (s *HTTP, err error) {
s = &HTTP{
func newServer(ctx context.Context, f fs.Fs, opt *Options, vfsOpt *vfscommon.Options, proxyOpt *proxy.Options) (_ *HTTP, err error) {
s := &HTTP{
f: f,
ctx: ctx,
opt: *opt,
@@ -198,6 +199,7 @@ func newServer(ctx context.Context, f fs.Fs, opt *Options, vfsOpt *vfscommon.Opt
router := s.server.Router()
router.Use(
s.provider.HoldVFS,
middleware.Compress(5),
middleware.SetHeader("Accept-Ranges", "bytes"),
middleware.SetHeader("Server", "rclone/"+fs.Version),
@@ -295,7 +297,7 @@ func (s *HTTP) serveDir(w http.ResponseWriter, r *http.Request, dirRemote string
if dirRemote == "" {
zipName = "root"
}
w.Header().Set("Content-Disposition", "attachment; filename=\""+zipName+".zip\"")
w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": zipName + ".zip"}))
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
err := vfs.CreateZip(ctx, dir, w)
+55
View File
@@ -22,6 +22,7 @@ import (
"github.com/rclone/rclone/fs/filter"
"github.com/rclone/rclone/fs/rc"
libhttp "github.com/rclone/rclone/lib/http"
"github.com/rclone/rclone/lib/random"
"github.com/rclone/rclone/vfs/vfscommon"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -542,3 +543,57 @@ func TestRc(t *testing.T) {
"vfs_cache_mode": "off",
})
}
// TestNewServerError checks that a server initialisation failure is
// returned as an error rather than panicking in the cleanup.
func TestNewServerError(t *testing.T) {
ctx := context.Background()
f, err := fs.NewFs(ctx, "testdata/files")
require.NoError(t, err)
opts := Options{
HTTP: libhttp.DefaultCfg(),
}
opts.HTTP.ListenAddr = []string{"localhost:-1"}
s, err := newServer(ctx, f, &opts, &vfscommon.Opt, &proxy.Opt)
require.Error(t, err)
assert.Nil(t, s)
}
// TestAuthProxyDownloadOutlivesCache checks a download in progress
// carries on working when the auth proxy drops its VFS from its cache,
// as it does when a download takes longer than the cache expiry time.
func TestAuthProxyDownloadOutlivesCache(t *testing.T) {
root := t.TempDir()
contents := random.String(32 * 1024 * 1024)
require.NoError(t, os.WriteFile(filepath.Join(root, "download.bin"), []byte(contents), 0666))
prog, err := filepath.Abs("../servetest/proxy_code.go")
require.NoError(t, err)
// FIXME this is untidy setting a global variable!
proxy.Opt.AuthProxy = "go run " + prog + " " + root
defer func() {
proxy.Opt.AuthProxy = ""
}()
s, testURL := start(context.Background(), t, nil)
defer func() { assert.NoError(t, s.Shutdown()) }()
req, err := http.NewRequest("GET", testURL+"download.bin", nil)
require.NoError(t, err)
req.SetBasicAuth(testUser, testPass)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer func() { _ = resp.Body.Close() }()
require.Equal(t, http.StatusOK, resp.StatusCode)
start := make([]byte, 1024)
_, err = io.ReadFull(resp.Body, start)
require.NoError(t, err)
// Drop everything from the proxy's cache as if it had expired
s.provider.Proxy().Shutdown()
rest, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.True(t, contents == string(start)+string(rest), "download corrupted")
}
+59
View File
@@ -3,9 +3,15 @@
package nfs
import (
"io"
"os"
"strings"
"testing"
_ "github.com/rclone/rclone/backend/memory"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/operations"
"github.com/rclone/rclone/fstest"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfscommon"
"github.com/stretchr/testify/assert"
@@ -39,3 +45,56 @@ func TestChmodDanglingSymlink(t *testing.T) {
assert.ErrorIs(t, bfs.Chmod("missing", 0777), vfs.ENOENT)
assert.ErrorIs(t, bfs.Chown("missing", 1000, 1000), vfs.ENOENT)
}
// go-nfs joins the file name from the client onto the directory path
// with Join without checking it for ".." elements or a "/", so such a
// name must not reach outside the directory served.
//
// The memory backend is used because it resolves ".." by joining paths,
// unlike the local backend which refuses paths outside its root.
func TestPathTraversal(t *testing.T) {
ctx := t.Context()
modTime := fstest.Time("2001-02-03T04:05:06.499999999Z")
// outside is the parent of the directory served and holds an object
// which must stay unreachable through the server.
outside, err := fs.NewFs(ctx, ":memory:nfs-traversal-test")
require.NoError(t, err)
_, err = operations.Rcat(ctx, outside, "outside-secret.txt", io.NopCloser(strings.NewReader("SECRET")), modTime, nil)
require.NoError(t, err)
f, err := fs.NewFs(ctx, ":memory:nfs-traversal-test/served-root")
require.NoError(t, err)
_, err = operations.Rcat(ctx, f, "inside.txt", io.NopCloser(strings.NewReader("INSIDE")), modTime, nil)
require.NoError(t, err)
opt := vfscommon.Opt
opt.Links = true
opt.CacheMode = vfscommon.CacheModeWrites
v := vfs.New(ctx, f, &opt)
defer v.Shutdown()
bfs := &FS{vfs: v}
// The MkdirAll calls come first so that, should one succeed, the
// later calls are routed through the directory node it made.
assert.Error(t, bfs.MkdirAll(bfs.Join("", "../new-dir"), 0777))
assert.Error(t, bfs.MkdirAll(bfs.Join("", ".."), 0777))
_, err = bfs.Create(bfs.Join("", "../outside-write.txt"))
assert.Error(t, err)
_, err = bfs.OpenFile(bfs.Join("", "../outside-secret.txt"), os.O_WRONLY|os.O_TRUNC, 0666)
assert.Error(t, err)
_, err = bfs.Open(bfs.Join("", "../outside-secret.txt"))
assert.Error(t, err)
assert.Error(t, bfs.Symlink("target", bfs.Join("", "../outside-link")))
assert.Error(t, bfs.Rename("inside.txt", bfs.Join("", "../outside-move.txt")))
assert.Error(t, bfs.Rename(bfs.Join("", "../outside-secret.txt"), "stolen.txt"))
assert.Error(t, bfs.Remove(bfs.Join("", "../outside-secret.txt")))
// Nothing was created, modified, moved or removed, either outside
// the served root or inside it.
fstest.CheckListingWithPrecision(t, outside, []fstest.Item{
fstest.NewItem("outside-secret.txt", "SECRET", modTime),
fstest.NewItem("served-root/inside.txt", "INSIDE", modTime),
}, []string{"served-root"}, fs.GetModifyWindow(ctx, outside))
}
+38 -2
View File
@@ -12,6 +12,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/netip"
"os/exec"
"strings"
@@ -27,6 +28,7 @@ import (
libhttp "github.com/rclone/rclone/lib/http"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfscommon"
"golang.org/x/sync/singleflight"
)
// Help contains text describing how to use the proxy
@@ -179,7 +181,8 @@ type Proxy struct {
ctx context.Context // for global config
Opt Options
vfsOpt vfscommon.Options
accessKeyMu sync.Mutex // serialises replacing a cached access key entry
accessKeyMu sync.Mutex // serialises replacing a cached access key entry
calls singleflight.Group // shares the calls made by logins which arrive together
}
// cacheEntry is what is stored in the vfsCache
@@ -232,6 +235,13 @@ func New(ctx context.Context, opt *Options, vfsOpt *vfscommon.Options) *Proxy {
entry.vfs.Shutdown()
}
})
// Shutting down a VFS stops it writing, so keep one which still
// has data to write, e.g. files in the VFS cache waiting to be
// uploaded after the transfer which wrote them has finished.
p.vfsCache.SetCanExpire(func(value any) bool {
entry, ok := value.(cacheEntry)
return !ok || entry.vfs == nil || !entry.vfs.Busy()
})
return p
}
@@ -449,7 +459,13 @@ func (p *Proxy) Call(user, auth string, isPublicKey bool, remoteAddr string) (VF
if isPublicKey {
kind = authPublicKey
}
value, err = p.call(user, auth, kind, clientIP)
// Logins with the same credentials which arrive together
// share one call, as each call which made its own cache
// entry would take a reference to the VFS but only the
// last entry made would be kept to give its reference back.
value, err, _ = p.calls.Do(cacheKey, func() (any, error) {
return p.call(user, auth, kind, clientIP)
})
if err != nil {
return nil, "", err
}
@@ -577,6 +593,26 @@ func (p *Provider) Get(ctx context.Context) (*vfs.VFS, error) {
return VFS, nil
}
// HoldVFS is HTTP middleware which holds the VFS for the request until
// the request has been served.
//
// An auth proxy shuts down a VFS when it expires from its cache, which
// a long upload or download can outlast as only the start of a request
// counts as a use of the VFS.
func (p *Provider) HoldVFS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Requests which don't need auth, eg CORS preflight, have no VFS
if VFS, err := p.Get(r.Context()); err == nil {
if !VFS.Hold() {
http.Error(w, "VFS has been shut down", http.StatusServiceUnavailable)
return
}
defer VFS.Shutdown()
}
next.ServeHTTP(w, r)
})
}
// VFS returns the fixed VFS, or nil if using an auth proxy.
func (p *Provider) VFS() *vfs.VFS {
if p == nil {
+10
View File
@@ -17,6 +17,16 @@ func main() {
log.Fatal(err)
}
// Note each run in a file if asked to
if runLog := os.Getenv("RCLONE_TEST_PROXY_RUN_LOG"); runLog != "" {
f, err := os.OpenFile(runLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
log.Fatal(err)
}
_, _ = f.WriteString(in["user"] + "\n")
_ = f.Close()
}
// Write the output
var out = map[string]string{}
for k, v := range in {
+60
View File
@@ -6,6 +6,8 @@ import (
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"os"
"path/filepath"
"strings"
"sync"
"testing"
@@ -475,3 +477,61 @@ func TestCallAccessKey(t *testing.T) {
_, _, err = p.CallAccessKey("AKID", remoteAddr, false)
require.ErrorContains(t, err, "revoked")
}
// TestBusyVFSNotExpired checks a VFS which still has data to write
// isn't expired from the cache, and so shut down, until it is written.
func TestBusyVFSNotExpired(t *testing.T) {
opt := Opt
opt.AuthProxy = "go run ../servetest/proxy_code.go " + t.TempDir()
p := New(context.Background(), &opt, &vfscommon.Opt)
defer p.Shutdown()
p.vfsCache.SetExpireDuration(time.Millisecond).SetExpireInterval(10 * time.Millisecond)
VFS, _, err := p.Call("user", "pass", false, "192.0.2.1:1024")
require.NoError(t, err)
fh, err := VFS.Create("file.txt")
require.NoError(t, err)
assert.Never(t, func() bool {
return p.vfsCache.Entries() == 0
}, 250*time.Millisecond, 10*time.Millisecond, "VFS expired with a file open for write")
assert.Equal(t, int32(1), VFS.Stats()["inUse"], "VFS shut down with a file open for write")
require.NoError(t, fh.Close())
assert.Eventually(t, func() bool {
return p.vfsCache.Entries() == 0
}, 10*time.Second, 10*time.Millisecond, "VFS not expired once it had nothing to write")
assert.Equal(t, int32(0), VFS.Stats()["inUse"], "VFS not shut down once it had nothing to write")
}
// TestSimultaneousLogins checks logins with the same credentials which
// arrive together share one run of the proxy and one reference to the
// VFS, so the VFS is shut down when it expires from the cache.
func TestSimultaneousLogins(t *testing.T) {
runLog := filepath.Join(t.TempDir(), "runs")
t.Setenv("RCLONE_TEST_PROXY_RUN_LOG", runLog)
opt := Opt
opt.AuthProxy = "go run proxy_code.go"
p := New(context.Background(), &opt, &vfscommon.Opt)
defer p.Shutdown()
var wg sync.WaitGroup
VFSes := make([]*vfs.VFS, 4)
for i := range VFSes {
wg.Go(func() {
var err error
VFSes[i], _, err = p.Call("user", "pass", false, "192.0.2.1:1024")
assert.NoError(t, err)
})
}
wg.Wait()
runs, err := os.ReadFile(runLog)
require.NoError(t, err)
assert.Equal(t, "user\n", string(runs), "proxy run more than once")
for _, VFS := range VFSes {
require.Same(t, VFSes[0], VFS)
}
assert.Equal(t, 1, p.vfsCache.Entries())
assert.Equal(t, int32(1), VFSes[0].Stats()["inUse"], "VFS referenced more than once")
}
+57 -13
View File
@@ -13,6 +13,7 @@ import (
"path"
"regexp"
"strings"
"sync"
"time"
"github.com/go-chi/chi/v5"
@@ -208,11 +209,13 @@ with a path of ` + "`/<username>/`" + `.
stdout: os.Stdout,
}
httpSrv := &http2.Server{}
opts := &http2.ServeConnOpts{
// http.Server can't serve a single net.Conn so the
// deprecated http2.Server is still needed here.
httpSrv := &http2.Server{} //nolint:staticcheck // see above
opts := &http2.ServeConnOpts{ //nolint:staticcheck // see above
Handler: s.server.Router(),
}
httpSrv.ServeConn(conn, opts)
httpSrv.ServeConn(conn, opts) //nolint:staticcheck // see above
return nil
}
fs.Logf(s.f, "Serving restic REST API on %s", s.server.URLs())
@@ -283,19 +286,28 @@ func checkPrivate(next http.Handler) http.Handler {
})
}
// uploadLock serializes uploads to one remote.
type uploadLock struct {
uploadingMu sync.Mutex
refs int // protected by server.uploadLocksMu
}
// server contains everything to run the server
type server struct {
server *libhttp.Server
f fs.Fs
cache *cache
opt Options
server *libhttp.Server
f fs.Fs
cache *cache
opt Options
uploadLocksMu sync.Mutex
uploadLocks map[string]*uploadLock
}
func newServer(ctx context.Context, f fs.Fs, opt *Options) (s *server, err error) {
s = &server{
f: f,
cache: newCache(opt.CacheObjects),
opt: *opt,
f: f,
cache: newCache(opt.CacheObjects),
opt: *opt,
uploadLocks: make(map[string]*uploadLock),
}
// Don't bind any HTTP listeners if running with --stdio
if opt.Stdio {
@@ -313,6 +325,30 @@ func newServer(ctx context.Context, f fs.Fs, opt *Options) (s *server, err error
return s, nil
}
// lockUpload locks uploads to remote and returns its unlock function.
func (s *server) lockUpload(remote string) func() {
s.uploadLocksMu.Lock()
lock := s.uploadLocks[remote]
if lock == nil {
lock = new(uploadLock)
s.uploadLocks[remote] = lock
}
lock.refs++
s.uploadLocksMu.Unlock()
lock.uploadingMu.Lock()
return func() {
lock.uploadingMu.Unlock()
s.uploadLocksMu.Lock()
lock.refs--
if lock.refs == 0 {
delete(s.uploadLocks, remote)
}
s.uploadLocksMu.Unlock()
}
}
// Serve restic until the server is shutdown
func (s *server) Serve() error {
s.server.Serve()
@@ -377,7 +413,8 @@ func (s *server) Bind(router chi.Router) {
}
}
var matchData = regexp.MustCompile("(?:^|/)data/([^/]{2,})$")
// Restic pack names are lowercase hexadecimal storage IDs.
var matchData = regexp.MustCompile("(?:^|/)data/([0-9a-f]{2,})$")
// newObject returns an object with the remote given either from the
// cache or directly
@@ -422,12 +459,19 @@ func (s *server) postObject(w http.ResponseWriter, r *http.Request) {
return
}
if s.opt.AppendOnly {
// make sure the file does not exist yet
unlock := s.lockUpload(remote)
defer unlock()
// Only a definitive not-found result permits creating the object.
_, err := s.newObject(r.Context(), remote)
if err == nil {
fs.Errorf(remote, "Post request: file already exists, refusing to overwrite in append-only mode")
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
if !errors.Is(err, fs.ErrorObjectNotFound) {
fs.Errorf(remote, "Post request: failed to check whether file exists: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
@@ -4,13 +4,17 @@ import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/rclone/rclone/cmd"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configfile"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -135,3 +139,60 @@ func TestResticHandler(t *testing.T) {
})
}
}
func TestAppendOnlyFailsClosedOnLookupError(t *testing.T) {
ctx := context.Background()
configfile.Install()
underlying := cmd.NewFsSrc([]string{t.TempDir()})
f := &newObjectErrorFs{
Fs: underlying,
err: errors.New("lookup failed"),
}
opt := newOpt()
opt.AppendOnly = true
s, err := newServer(ctx, f, &opt)
require.NoError(t, err)
req := newRequest(t, "POST", "/config", strings.NewReader("replacement"))
checkRequest(t, s.server.Router().ServeHTTP, req, []wantFunc{wantCode(http.StatusInternalServerError)})
_, err = underlying.NewObject(ctx, "config")
assert.ErrorIs(t, err, fs.ErrorObjectNotFound)
}
func TestAppendOnlyConcurrentUploads(t *testing.T) {
configfile.Install()
opt := newOpt()
opt.AppendOnly = true
f := cmd.NewFsSrc([]string{t.TempDir()})
s, err := newServer(context.Background(), f, &opt)
require.NoError(t, err)
handler := s.server.Router().ServeHTTP
const uploads = 10
start := make(chan struct{})
results := make(chan int, uploads)
for range uploads {
go func() {
req := newRequest(t, "POST", "/config", strings.NewReader("config data"))
recorder := httptest.NewRecorder()
<-start
handler(recorder, req)
results <- recorder.Code
}()
}
close(start)
succeeded := 0
for range uploads {
if code := <-results; code == http.StatusOK {
succeeded++
} else {
assert.Equal(t, http.StatusForbidden, code)
}
}
assert.Equal(t, 1, succeeded)
checkRequest(t, handler, newRequest(t, "GET", "/config", nil),
[]wantFunc{wantCode(http.StatusOK), wantBody("config data")})
}
+53
View File
@@ -10,6 +10,8 @@ import (
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
_ "github.com/rclone/rclone/backend/all"
@@ -106,6 +108,17 @@ func TestMakeRemote(t *testing.T) {
{"/data/12", "data/12/12"},
{"/data/123", "data/12/123"},
{"/data/123/", "data/12/123"},
{"/data/abcdef", "data/ab/abcdef"},
{"/data/config", "data/config"},
{"/data/data/", "data/data"},
{"/data/index/", "data/index"},
{"/data/keys/", "data/keys"},
{"/data/locks/", "data/locks"},
{"/data/snapshots/", "data/snapshots"},
{"/data/data/abcdef", "data/data/ab/abcdef"},
{"/repo/data/config", "repo/data/config"},
{"/repo/data/keys/", "repo/data/keys"},
{"/repo/data/data/abcdef", "repo/data/data/ab/abcdef"},
{"/keys", "keys"},
{"/keys/1", "keys/1"},
{"/keys/12", "keys/12"},
@@ -123,6 +136,46 @@ func TestMakeRemote(t *testing.T) {
}
}
func TestResticDataRepository(t *testing.T) {
for _, repo := range []string{"data", "repo/data"} {
t.Run(repo, func(t *testing.T) {
tempdir := t.TempDir()
f := cmd.NewFsSrc([]string{tempdir})
opt := newOpt()
s, err := newServer(context.Background(), f, &opt)
require.NoError(t, err)
handler := s.server.Router().ServeHTTP
checkRequest(t, handler, newRequest(t, "POST", "/"+repo+"/?create=true", nil),
[]wantFunc{wantCode(http.StatusOK)})
const id = "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
for _, object := range []struct {
url, remote string
}{
{"config", "config"},
{"keys/" + id, "keys/" + id},
{"data/" + id, "data/ab/" + id},
} {
url := "/" + repo + "/" + object.url
checkRequest(t, handler, newRequest(t, "POST", url, strings.NewReader("test content")),
[]wantFunc{wantCode(http.StatusOK)})
checkRequest(t, handler, newRequest(t, "GET", url, nil),
[]wantFunc{wantCode(http.StatusOK), wantBody("test content")})
content, err := os.ReadFile(filepath.Join(tempdir, repo, object.remote))
require.NoError(t, err)
assert.Equal(t, "test content", string(content))
}
for _, dir := range []string{"keys", "data"} {
checkRequest(t, handler, newRequest(t, "GET", "/"+repo+"/"+dir+"/", nil),
[]wantFunc{wantCode(http.StatusOK), func(t testing.TB, res *httptest.ResponseRecorder) {
assert.JSONEq(t, `[{"name":"`+id+`","size":12}]`, res.Body.String())
}})
}
})
}
}
type listErrorFs struct {
fs.Fs
}
+160 -85
View File
@@ -6,13 +6,17 @@ import (
"encoding/hex"
"io"
"maps"
"math"
"os"
"path"
"slices"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ncw/swift/v2"
"github.com/rclone/gofakes3"
"github.com/rclone/rclone/fs"
@@ -38,12 +42,14 @@ const putObjectPrefix = tempObjectPrefix + "put_"
// multipart uploads stream straight through to the underlying Fs via
// PutStream, instead of being buffered in memory by gofakes3.
type s3Backend struct {
s *Server
meta *sync.Map
s *Server
// multipartUploads tracks in-flight streaming multipart uploads,
// keyed by gofakes3.UploadID.
multipartUploads sync.Map
tenantsMu sync.Mutex
tenants map[string]*tenant // per-user state by tenant ID
// warnMaxUploadsOnce logs a single NOTICE the first time a multipart
// upload is refused by --multipart-max-uploads.
warnMaxUploadsOnce sync.Once
// warnInMemoryOnce logs a single NOTICE the first time a multipart
// upload falls back to being buffered in memory.
@@ -53,15 +59,149 @@ type s3Backend struct {
reaperStop sync.Once
}
// tenant is the state kept between requests for one user of the server.
//
// Anything remembered about a user's objects or uploads must be reached
// through their tenant, as different users may be serving different
// backends.
type tenant struct {
b *s3Backend
id string
// uploads tracks in-flight streaming multipart uploads,
// keyed by gofakes3.UploadID.
uploads sync.Map
// nUploads counts the in-flight uploads, limited by
// --multipart-max-uploads.
nUploads atomic.Int64
// budget limits the memory buffered by all the multipart uploads,
// set by --multipart-streaming-buffer-total.
budget *bufferBudget
// meta is the stored metadata of objects, by object path.
meta *lru.Cache[string, objectMeta]
}
// objectMeta is the metadata stored for an object, with the size and
// modification time the object had when it was stored.
type objectMeta struct {
meta map[string]string
size int64
modTime time.Time
}
// newMetadataStore makes a store for the metadata of up to maxObjects
// objects, forgetting that of the least recently used after that. If
// maxObjects <= 0 there is no limit.
func newMetadataStore(maxObjects int) *lru.Cache[string, objectMeta] {
if maxObjects <= 0 {
maxObjects = math.MaxInt
}
meta, err := lru.New[string, objectMeta](maxObjects)
if err != nil {
panic(err) // only fails if maxObjects <= 0
}
return meta
}
// newBackend creates a new SimpleBucketBackend.
func newBackend(s *Server) *s3Backend {
return &s3Backend{
s: s,
meta: new(sync.Map),
tenants: map[string]*tenant{},
reaperQuit: make(chan struct{}),
}
}
// tenant returns the state of the user making the request in ctx.
func (b *s3Backend) tenant(ctx context.Context) *tenant {
id := b.s.getTenant(ctx)
b.tenantsMu.Lock()
defer b.tenantsMu.Unlock()
t, ok := b.tenants[id]
if !ok {
t = &tenant{
b: b,
id: id,
budget: newBufferBudget(int64(b.s.opt.MultipartStreamingBufferTotal)),
meta: newMetadataStore(b.s.opt.MetadataMaxObjects),
}
b.tenants[id] = t
}
return t
}
// allTenants returns the state of every user seen so far.
func (b *s3Backend) allTenants() []*tenant {
b.tenantsMu.Lock()
defer b.tenantsMu.Unlock()
return slices.Collect(maps.Values(b.tenants))
}
// context returns a context for requests made on behalf of t.
func (t *tenant) context() context.Context {
return context.WithValue(context.Background(), ctxKeyAccessKeyID, t.id)
}
// getMeta returns the stored metadata of node, the object at fp.
//
// Nothing is returned if the object has changed since the metadata was
// stored, as it has been replaced by something other than this user.
func (t *tenant) getMeta(ctx context.Context, fp string, node vfs.Node) (meta map[string]string, ok bool) {
om, ok := t.meta.Get(fp)
if !ok || om.size != node.Size() {
return nil, false
}
window := fs.GetModifyWindow(ctx, node.VFS().Fs())
if window != fs.ModTimeNotSupported {
if dt := node.ModTime().Sub(om.modTime); dt < -window || dt > window {
return nil, false
}
}
return om.meta, true
}
// storeMeta stores meta as the metadata of the object at fp in _vfs.
//
// If meta has a modification time in "X-Amz-Meta-Mtime" or "mtime" the
// object's modification time is set to it, returning any error, and
// both are set to it in meta.
func (t *tenant) storeMeta(_vfs *vfs.VFS, fp string, meta map[string]string) (err error) {
for _, key := range []string{"X-Amz-Meta-Mtime", "mtime"} {
val, ok := meta[key]
if !ok {
continue
}
ti, parseErr := swift.FloatStringToTime(val)
if parseErr != nil {
continue
}
meta["X-Amz-Meta-Mtime"] = val
meta["mtime"] = val
err = _vfs.Chtimes(fp, ti, ti)
break
}
// Stat after setting the modification time as getMeta compares with it
node, statErr := _vfs.Stat(fp)
if statErr != nil {
t.removeMeta(fp)
return err
}
t.meta.Add(fp, objectMeta{
meta: meta,
size: node.Size(),
modTime: node.ModTime(),
})
return err
}
// removeMeta forgets the metadata of the object at fp.
func (t *tenant) removeMeta(fp string) {
t.meta.Remove(fp)
}
// ListBuckets always returns the default bucket.
func (b *s3Backend) ListBuckets(ctx context.Context) ([]gofakes3.BucketInfo, error) {
_vfs, err := b.s.getVFS(ctx)
@@ -111,7 +251,7 @@ func (b *s3Backend) ListBucket(ctx context.Context, bucket string, prefix *gofak
response := gofakes3.NewObjectList()
path, remaining := prefixParser(prefix)
err = b.entryListR(_vfs, bucket, path, remaining, prefix.HasDelimiter, response)
err = b.listPage(_vfs, bucket, path, remaining, prefix.HasDelimiter, page, response)
if err == gofakes3.ErrNoSuchKey {
// AWS just returns an empty list
response = gofakes3.NewObjectList()
@@ -119,7 +259,7 @@ func (b *s3Backend) ListBucket(ctx context.Context, bucket string, prefix *gofak
return nil, err
}
return b.pager(response, page)
return response, nil
}
// formatHeaderTime makes an timestamp which is the same as that used by AWS.
@@ -163,7 +303,7 @@ func (b *s3Backend) HeadObject(ctx context.Context, bucketName, objectName strin
// to hashing the VFS cache when the backing object is not available yet.
entry := node.DirEntry()
size := node.Size()
hash := getFileHashByte(node, b.s.etagHashType)
hash := getFileHashByte(node, b.s.etagHash(_vfs))
mimeType := fs.MimeTypeFromName(objectName)
if fobj, ok := entry.(fs.Object); ok {
@@ -175,8 +315,7 @@ func (b *s3Backend) HeadObject(ctx context.Context, bucketName, objectName strin
"Content-Type": mimeType,
}
if val, ok := b.meta.Load(fp); ok {
metaMap := val.(map[string]string)
if metaMap, ok := b.tenant(ctx).getMeta(ctx, fp, node); ok {
maps.Copy(meta, metaMap)
}
@@ -220,7 +359,7 @@ func (b *s3Backend) GetObject(ctx context.Context, bucketName, objectName string
file := node.(*vfs.File)
size := node.Size()
hash := getFileHashByte(node, b.s.etagHashType)
hash := getFileHashByte(node, b.s.etagHash(_vfs))
in, err := file.Open(os.O_RDONLY)
if err != nil {
@@ -256,8 +395,7 @@ func (b *s3Backend) GetObject(ctx context.Context, bucketName, objectName string
"Content-Type": mimeType,
}
if val, ok := b.meta.Load(fp); ok {
metaMap := val.(map[string]string)
if metaMap, ok := b.tenant(ctx).getMeta(ctx, fp, node); ok {
maps.Copy(meta, metaMap)
}
@@ -271,14 +409,6 @@ func (b *s3Backend) GetObject(ctx context.Context, bucketName, objectName string
}, nil
}
// storeModtime sets both "mtime" and "X-Amz-Meta-Mtime" to val in b.meta.
// Call this whenever modtime is updated.
func (b *s3Backend) storeModtime(fp string, meta map[string]string, val string) {
meta["X-Amz-Meta-Mtime"] = val
meta["mtime"] = val
b.meta.Store(fp, meta)
}
// TouchObject creates or updates meta on specified object.
func (b *s3Backend) TouchObject(ctx context.Context, fp string, meta map[string]string) (result gofakes3.PutObjectResult, err error) {
_vfs, err := b.s.getVFS(ctx)
@@ -302,27 +432,7 @@ func (b *s3Backend) TouchObject(ctx context.Context, fp string, meta map[string]
return result, err
}
b.meta.Store(fp, meta)
if val, ok := meta["X-Amz-Meta-Mtime"]; ok {
ti, err := swift.FloatStringToTime(val)
if err == nil {
b.storeModtime(fp, meta, val)
return result, _vfs.Chtimes(fp, ti, ti)
}
// ignore error since the file is successfully created
}
if val, ok := meta["mtime"]; ok {
ti, err := swift.FloatStringToTime(val)
if err == nil {
b.storeModtime(fp, meta, val)
return result, _vfs.Chtimes(fp, ti, ti)
}
// ignore error since the file is successfully created
}
return result, nil
return result, b.tenant(ctx).storeMeta(_vfs, fp, meta)
}
// PutObject creates or overwrites the object with the given name.
@@ -423,27 +533,7 @@ func (b *s3Backend) PutObject(
return result, err
}
b.meta.Store(fp, meta)
if val, ok := meta["X-Amz-Meta-Mtime"]; ok {
ti, err := swift.FloatStringToTime(val)
if err == nil {
b.storeModtime(fp, meta, val)
return result, _vfs.Chtimes(fp, ti, ti)
}
// ignore error since the file is successfully created
}
if val, ok := meta["mtime"]; ok {
ti, err := swift.FloatStringToTime(val)
if err == nil {
b.storeModtime(fp, meta, val)
return result, _vfs.Chtimes(fp, ti, ti)
}
// ignore error since the file is successfully created
}
return result, nil
return result, b.tenant(ctx).storeMeta(_vfs, fp, meta)
}
// DeleteMulti deletes multiple objects in a single request.
@@ -491,6 +581,7 @@ func (b *s3Backend) deleteObject(ctx context.Context, bucketName, objectName str
if err := _vfs.Remove(fp); err != nil && !os.IsNotExist(err) {
return err
}
b.tenant(ctx).removeMeta(fp)
// FIXME: unsafe operation
rmdirRecursive(fp, _vfs)
@@ -560,28 +651,12 @@ func (b *s3Backend) CopyObject(ctx context.Context, srcBucket, srcKey, dstBucket
if err != nil {
return result, err
}
if srcBucket == dstBucket && srcKey == dstKey {
b.meta.Store(fp, meta)
val, ok := meta["X-Amz-Meta-Mtime"]
if !ok {
if val, ok = meta["mtime"]; !ok {
return
}
}
// update modtime
ti, err := swift.FloatStringToTime(val)
if err != nil {
return result, nil
}
b.storeModtime(fp, meta, val)
return result, _vfs.Chtimes(fp, ti, ti)
}
cStat, err := _vfs.Stat(fp)
if err != nil {
return
if err != nil || !cStat.IsFile() {
return result, gofakes3.KeyNotFound(srcKey)
}
if srcBucket == dstBucket && srcKey == dstKey {
return result, b.tenant(ctx).storeMeta(_vfs, fp, meta)
}
c, err := b.GetObject(ctx, srcBucket, srcKey, nil)
+127
View File
@@ -2,17 +2,23 @@ package s3
import (
"context"
"crypto/md5"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/rclone/gofakes3"
_ "github.com/rclone/rclone/backend/crypt"
_ "github.com/rclone/rclone/backend/local"
"github.com/rclone/rclone/cmd/serve/proxy"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/obscure"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/fstest"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfscommon"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -180,3 +186,124 @@ func TestBucketDirPath(t *testing.T) {
}
}
}
// TestEtagHashAuto checks that --etag-hash auto uses the best hash of
// the backend of each auth proxy user rather than of the remote the
// server was started with, which may have none or not exist at all.
func TestEtagHashAuto(t *testing.T) {
fstest.Initialise()
ctx := context.Background()
opt := Opt
opt.EtagHash = "auto"
opt.HTTP.ListenAddr = []string{endpoint}
proxyOpt := proxy.Opt
proxyOpt.AuthProxy = "/path/to/auth/proxy"
fCrypt, err := fs.NewFs(ctx, ":crypt,remote='"+t.TempDir()+"',password="+obscure.MustObscure("password")+":")
require.NoError(t, err)
require.Equal(t, hash.None, fCrypt.Hashes().GetOne(), "crypt has no hashes")
for _, f := range []fs.Fs{nil, fCrypt} {
w, err := newServer(ctx, f, &opt, &vfscommon.Opt, &proxyOpt)
require.NoError(t, err)
t.Cleanup(func() { _ = w.Shutdown() })
fUser, err := fs.NewFs(ctx, t.TempDir())
require.NoError(t, err)
userVFS := vfs.New(ctx, fUser, &vfscommon.Opt)
t.Cleanup(userVFS.Shutdown)
ctx := context.WithValue(ctx, ctxKeyID, userVFS)
require.NoError(t, w.backend.CreateBucket(ctx, "bucket"))
_, err = w.backend.PutObject(ctx, "bucket", "object", nil, strings.NewReader("data"), 4)
require.NoError(t, err)
obj, err := w.backend.HeadObject(ctx, "bucket", "object")
require.NoError(t, err)
want := md5.Sum([]byte("data"))
assert.Equal(t, want[:], obj.Hash)
}
}
// TestCopyObjectMissingSource checks that copying an object which
// doesn't exist fails with NoSuchKey, including a copy onto itself to
// replace its metadata.
func TestCopyObjectMissingSource(t *testing.T) {
b, _ := newTestBackend(t)
ctx := context.Background()
for _, dstKey := range []string{"missing.txt", "other.txt"} {
_, err := b.CopyObject(ctx, "bucket", "missing.txt", "bucket", dstKey, map[string]string{"X-Amz-Meta-Colour": "red"})
assert.True(t, gofakes3.HasErrorCode(err, gofakes3.ErrNoSuchKey), "copy to %q: %v", dstKey, err)
}
_, err := b.HeadObject(ctx, "bucket", "other.txt")
assert.True(t, gofakes3.HasErrorCode(err, gofakes3.ErrNoSuchKey))
}
// tenantCtx returns the context of a request authenticated by the auth
// proxy as accessKeyID.
func tenantCtx(accessKeyID string) context.Context {
return context.WithValue(context.Background(), ctxKeyAccessKeyID, accessKeyID)
}
// TestMetadataOtherTenant checks that with an auth proxy the metadata
// one user (access key ID) stores for an object isn't returned for
// another user's object at the same path.
func TestMetadataOtherTenant(t *testing.T) {
b, _ := newTestBackend(t)
ctxA, ctxB := tenantCtx("tenantA"), tenantCtx("tenantB")
const secretKey = "X-Amz-Meta-Secret"
put := func(ctx context.Context, meta map[string]string) {
_, err := b.PutObject(ctx, "bucket", "meta.txt", meta, strings.NewReader("data"), 4)
require.NoError(t, err)
}
head := func(ctx context.Context) map[string]string {
obj, err := b.HeadObject(ctx, "bucket", "meta.txt")
require.NoError(t, err)
return obj.Metadata
}
put(ctxB, map[string]string{})
put(ctxA, map[string]string{secretKey: "tenantA's"})
assert.Equal(t, "tenantA's", head(ctxA)[secretKey])
assert.NotContains(t, head(ctxB), secretKey)
obj, err := b.GetObject(ctxB, "bucket", "meta.txt", nil)
require.NoError(t, err)
require.NoError(t, obj.Contents.Close())
assert.NotContains(t, obj.Metadata, secretKey)
}
// TestMetadataStale checks that the metadata stored for an object isn't
// returned once the object has been changed by someone else: another
// auth proxy user sharing the backend or a change to the backend itself.
func TestMetadataStale(t *testing.T) {
b, root := newTestBackend(t)
ctxA, ctxB := tenantCtx("tenantA"), tenantCtx("tenantB")
const key = "X-Amz-Meta-Colour"
objPath := filepath.Join(root, "bucket", "meta.txt")
put := func(ctx context.Context, meta map[string]string, data string) {
_, err := b.PutObject(ctx, "bucket", "meta.txt", meta, strings.NewReader(data), int64(len(data)))
require.NoError(t, err)
}
colour := func(ctx context.Context) string {
_vfs, err := b.s.getVFS(ctx)
require.NoError(t, err)
b.forgetPath(_vfs, "bucket/meta.txt")
obj, err := b.HeadObject(ctx, "bucket", "meta.txt")
require.NoError(t, err)
return obj.Metadata[key]
}
put(ctxA, map[string]string{key: "red"}, "data")
assert.Equal(t, "red", colour(ctxA))
put(ctxB, map[string]string{}, "other data")
assert.Equal(t, "", colour(ctxA), "metadata kept after another user replaced the object")
put(ctxA, map[string]string{key: "blue"}, "data")
assert.Equal(t, "blue", colour(ctxA))
later := time.Now().Add(time.Hour)
require.NoError(t, os.Chtimes(objPath, later, later))
assert.Equal(t, "", colour(ctxA), "metadata kept after the object was changed on the backend")
}
+168 -14
View File
@@ -1,10 +1,14 @@
package s3
import (
"errors"
"path"
"slices"
"strings"
"github.com/rclone/gofakes3"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/vfs"
)
@@ -13,17 +17,139 @@ import (
// (rclone v1.75); leftovers from an older server are still hidden.
const legacyMultipartUploadPrefix = ".rclone_multipart_upload_"
func (b *s3Backend) entryListR(_vfs *vfs.VFS, bucketName, fdPath, name string, addPrefix bool, response *gofakes3.ObjectList) error {
fp, err := bucketDirPath(bucketName, fdPath)
// errPageFull is returned by the listing walk once the page has been filled
// and one further key has been seen, so the walk can unwind early. It never
// escapes ListBucket.
var errPageFull = errors.New("listing page full")
// lister walks a directory tree emitting one page of an S3 listing.
//
// The walk emits keys in flat S3 order and stops as soon as the page is full,
// so the cost of a page is proportional to the keys it returns rather than to
// the size of the subtree below the prefix.
type lister struct {
vfs *vfs.VFS
hashType hash.Type // hash used for ETags
bucket string
marker string // keys at or before this one have already been returned
hasMarker bool
max int // number of keys wanted in this page
response *gofakes3.ObjectList
keys int // keys added to the page so far
lastKey string // the last key added to the page
dirsRead int // directories read, for debug logging
}
// newLister makes a lister which fills response with at most one page of the
// listing of bucket, as described by page.
func newLister(b *s3Backend, _vfs *vfs.VFS, bucket string, page gofakes3.ListBucketPage, response *gofakes3.ObjectList) *lister {
max := int(page.MaxKeys)
if max <= 0 {
// A missing or zero max-keys means the client didn't ask for a
// particular page size, so use the S3 maximum.
max = 1000
}
return &lister{
vfs: _vfs,
hashType: b.s.etagHash(_vfs),
bucket: bucket,
marker: page.Marker,
hasMarker: page.HasMarker,
max: max,
response: response,
}
}
// truncate marks the page as truncated after the last key added to it, so
// that the next page resumes from there, and returns errPageFull.
func (l *lister) truncate() error {
l.response.IsTruncated = true
l.response.NextMarker = l.lastKey
return errPageFull
}
// sortKey returns the S3 key an entry contributes to the listing.
//
// Directories sort as if they carried their trailing slash, which is what
// makes a depth first walk emit keys in the same order as the flat keyspace
// of a real S3 bucket.
func sortKey(entry vfs.Node) string {
if entry.IsDir() {
return entry.Name() + "/"
}
return entry.Name()
}
// skipKey reports whether key has already been returned by an earlier page.
func (l *lister) skipKey(key string) bool {
return l.hasMarker && key <= l.marker
}
// skipDir reports whether every key under the directory prefix p was returned
// by an earlier page, so the subtree need not be read at all.
//
// A marker inside the subtree has p as a prefix and is never skipped: that
// subtree is descended and filtered key by key.
func (l *lister) skipDir(p string) bool {
return l.hasMarker && l.marker > p && !strings.HasPrefix(l.marker, p)
}
// addCommonPrefix adds a common prefix to the page, returning errPageFull if
// the page is now complete.
func (l *lister) addCommonPrefix(prefix string) error {
if l.keys >= l.max {
return l.truncate()
}
l.response.AddPrefix(prefix)
l.keys++
l.lastKey = prefix
return nil
}
// addObject adds an object to the page, returning errPageFull if the page is
// now complete.
func (l *lister) addObject(key string, entry vfs.Node) error {
if l.keys >= l.max {
return l.truncate()
}
l.response.Add(&gofakes3.Content{
Key: key,
LastModified: gofakes3.NewContentTime(entry.ModTime()),
ETag: getFileHash(entry, l.hashType),
Size: entry.Size(),
StorageClass: gofakes3.StorageStandard,
})
l.keys++
l.lastKey = key
return nil
}
// list walks the directory fdPath, adding the entries whose leaf name starts
// with name to the page.
//
// If addPrefix is set, subdirectories are reported as common prefixes,
// otherwise they are descended into.
//
// It returns errPageFull once the page is complete, and gofakes3.ErrNoSuchKey
// if fdPath is not a directory.
func (l *lister) list(fdPath, name string, addPrefix bool) error {
fp, err := bucketDirPath(l.bucket, fdPath)
if err != nil {
// A listing prefix that can't be represented as a path matches nothing.
return gofakes3.ErrNoSuchKey
}
dirEntries, err := getDirEntries(fp, _vfs)
dirEntries, err := getDirEntries(fp, l.vfs)
if err != nil {
return err
}
l.dirsRead++
// Emit the entries in the order their keys have in a flat keyspace,
// which is not the plain name order getDirEntries returns.
slices.SortFunc(dirEntries, func(a, b vfs.Node) int {
return strings.Compare(sortKey(a), sortKey(b))
})
for _, entry := range dirEntries {
object := entry.Name()
@@ -41,25 +167,53 @@ func (b *s3Backend) entryListR(_vfs *vfs.VFS, bucketName, fdPath, name string, a
}
if entry.IsDir() {
prefixWithTrailingSlash := objectPath + "/"
if addPrefix {
prefixWithTrailingSlash := objectPath + "/"
response.AddPrefix(prefixWithTrailingSlash)
if l.skipKey(prefixWithTrailingSlash) {
continue
}
if err := l.addCommonPrefix(prefixWithTrailingSlash); err != nil {
return err
}
continue
}
err := b.entryListR(_vfs, bucketName, path.Join(fdPath, object), "", false, response)
if err != nil {
if l.skipDir(prefixWithTrailingSlash) {
continue
}
err := l.list(objectPath, "", false)
if errors.Is(err, gofakes3.ErrNoSuchKey) {
// The directory went away while we were listing it, so
// there is nothing below it to report.
continue
} else if err != nil {
return err
}
} else {
item := &gofakes3.Content{
Key: objectPath,
LastModified: gofakes3.NewContentTime(entry.ModTime()),
ETag: getFileHash(entry, b.s.etagHashType),
Size: entry.Size(),
StorageClass: gofakes3.StorageStandard,
if l.skipKey(objectPath) {
continue
}
if err := l.addObject(objectPath, entry); err != nil {
return err
}
response.Add(item)
}
}
return nil
}
// listPage fills response with one page of the listing of the directory
// fdPath in bucket, as described by page.
//
// Only entries whose leaf name starts with name are listed. If addPrefix is
// set, subdirectories are reported as common prefixes, otherwise the whole
// subtree below fdPath is listed.
//
// It returns gofakes3.ErrNoSuchKey if fdPath is not a directory.
func (b *s3Backend) listPage(_vfs *vfs.VFS, bucket, fdPath, name string, addPrefix bool, page gofakes3.ListBucketPage, response *gofakes3.ObjectList) error {
l := newLister(b, _vfs, bucket, page, response)
err := l.list(fdPath, name, addPrefix)
if err != nil && !errors.Is(err, errPageFull) {
return err
}
fs.Debugf("serve s3", "Listed %d keys from %d directories (truncated=%v)", l.keys, l.dirsRead, response.IsTruncated)
return nil
}
Loaded 100 of 230 files, more files were not shown because too many files have changed in this diff. Show more