Skip to content

Replace an existing file with cp_file and mv, as rclone does - #470

Draft
shcheklein wants to merge 4 commits into
write-replaces-existing-filefrom
cp-mv-replace-target
Draft

shcheklein wants to merge 4 commits into
write-replaces-existing-filefrom
cp-mv-replace-target

Conversation

@shcheklein

Copy link
Copy Markdown
Member

Stacked on #455 (base branch write-replaces-existing-file). cp_file needs #455's known-file cache to find a target without a lookup on every copy. Merge #455 first; then this PR needs a rebase onto main that drops #455's commits.

Before and after

fs.pipe_file("root/data/a.csv", b"new")
fs.pipe_file("root/data/b.csv", b"old")

fs.cp_file("root/data/a.csv", "root/data/b.csv")
# before: two files named b.csv; reads return the one with the lower ID
# after:  one b.csv with a.csv's bytes; the old b.csv is in the trash

fs.mv("root/data/a.csv", "root/data/b.csv")
# before: NotADirectoryError
# after:  a.csv becomes b.csv (same ID); the old b.csv is in the trash

With trash_only=False the old file is deleted instead.

What it does, as rclone does

Drive can't copy or move onto an existing file, so the old file is removed afterwards. rclone's Drive backend does the same (drive.go Copy copies, then removes the existing object; operations.Move deletes the existing destination, then moves).

  • cp_file replaces a file the filesystem knows at the path, by the same rule as writes in Replace a known file on a write, without a lookup per write #455: files it wrote, copied or found by path (exists(), info(), a read). Before removing anything it looks the path up once more, so only a file that's still there is removed. For any other path it copies without asking Drive, as today, so a file it doesn't know can still end up duplicated. The copy is a new file with its own ID, so the old file's sharing and links don't carry over.
  • mv already looked the target up (exists()), so it replaces any file there. Unlike rclone, it moves first and removes the old file afterwards, so a failed move leaves the target untouched. The moved file keeps its ID.
  • What doesn't change:
    • a folder at the target keeps its meaning: mv moves the source into it, and cp_file behaves as before;
    • a folder as the source onto a file still raises NotADirectoryError;
    • if several files share the target path, the one reads return (lowest ID) is replaced and the others stay;
    • copying or moving a file onto itself changes nothing.
  • If the removal fails (for example missing permission on a shared drive), the error is raised after the copy or move, and both files are at the path.

Requests (counted in the live tests)

Operation onto an existing file Requests
cp_file, target known source lookup, target lookup, copy, trash/delete = 4 (main: 2, leaving a duplicate)
cp_file, target unknown source lookup, copy = 2 (unchanged)
mv, same folder target lookup, source lookup, rename, trash/delete = 4 (main: raised after 3)
mv, another folder + one parents read, as any move = 5

Tests

Four live tests on real Drive. All four fail on #455's code (run, test commit only, branch since deleted):

  • cp_file after a write to the target: 4 requests; one target with the source's bytes; old file trashed.
  • cp_file after exists() found the target, trash_only=False: the old file is deleted.
  • mv onto a file another client made, in another folder: the moved file keeps its ID; the source path is gone; old file trashed.
  • mv onto a file in the same folder, trash_only=False: 4 requests; old file deleted.

The offline test that expected NotADirectoryError for mv onto a file now covers the replacement, plus a folder source (still raises) and a file onto itself. #455's offline cp-then-write test now writes to the copy.

Docs: fsspec.rst (Writing files) and the upgrade guide.

Overlaps

#453 (rm) and #456 also edit pydrive2/fs/spec.py.

🤖 Generated with Claude Code

shcheklein and others added 2 commits December 4, 2026 20:43
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cp_file onto a file the filesystem knows copies, then trashes the old file
(deleted with trash_only=False), after looking the path up once more. mv
already looks the target up, so it moves the source onto any file there and
removes the old file after the move. Copying or moving a file onto itself
changes nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical cache inconsistencies after copy or move operations, including cleanup failures, can cause subsequent writes to update the wrong file or overwrite moved content.

3 open issues
What changed in this PR

Extends the filesystem’s known-file replacement behavior to copying and moving files on Drive.

Changes:

  • Makes cp_file() replace known targets and mv() replace existing file targets.
  • Removes old targets after successful operations, honoring trash_only.
  • Adds replacement tests and updates user documentation.
File Description
pydrive2/​test/​test_fs.py Adds live replacement and request-count tests.
pydrive2/​test/​test_fs_unit.py Tests replacement, self-operations, and subsequent writes.
pydrive2/​fs/​spec.py Implements copy and move replacement behavior.
docs/​upgrading.rst Documents changed overwrite behavior.
docs/​fsspec.rst Explains replacement rules and file identity.

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
The copied or moved file is remembered even when trashing the old file
fails, the moved file is forgotten at its old path, and a file left at the
path with a lower ID than the copy stays the one writes replace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The cache-dependent self-copy guard can create duplicates for files with multiple or unresolved parents, requiring a cache-independent path comparison and regression test.

1 open issue
3 resolved since last review

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Concurrent mv() calls can race during destination replacement in default trash mode, leaving duplicate destination files despite both calls succeeding.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants