Repository navigation
[Python] Reject overlapping local copies before deleting data - #41748
JustinBinber wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR adds preflight ancestor/descendant overlap checks to LocalFileSystem.copy, preventing destructive shutil.rmtree calls when copying a source onto an ancestor directory and preventing recursive infinite copies when copying a directory into its own descendant. The implementation thoughtfully handles symlinks, relative paths (..), and Windows drive-relative paths.
🔍 General Feedback
- There is one edge case when overwriting a broken symlink (
os.path.exists(destination)isFalse, whileos.path.lexists(destination)isTrue) whensourceis a file, wheredestination_path.resolve()can raiseRuntimeErroron a symlink cycle before reachingshutil.copy2, or where broken symlinks atdestinationare not removed prior to copying. - Otherwise, the ancestor/descendant resolution logic and test coverage are thorough and well-structured.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
| 'Cannot copy between ancestor and descendant paths: %r, %r' % | ||
| (source, destination)) | ||
| if destination_exists: | ||
| if destination_is_directory: |
There was a problem hiding this comment.
🟡 Medium: When source is a directory (source_is_directory is True) and destination is a broken symlink with a symlink cycle (so destination_is_directory is False and destination_exists is False), calling destination_path.resolve() on line 284 will raise RuntimeError: Symlink loop from ... on Python < 3.13 instead of OSError (note that test_copy_disjoint_link_cycle_fails_without_mutating_source currently catches RuntimeError only because _copy_path is wrapped in except Exception as e: in copy(), rather than turning it into an IOError).
Additionally, because destination_in_source only needs destination_path.resolve() when destination actually resolves to an existing directory/file (or you can catch RuntimeError or check destination_exists before resolving destination_path), guarding destination_path.resolve() with destination_exists avoids unnecessary resolution and RuntimeError on broken/cyclic destination symlinks (since destination_entry already resolves destination_path.parent).
| if destination_is_directory: | |
| for output in ((destination_entry, destination_path.resolve()) if destination_exists else (destination_entry,)) |
There was a problem hiding this comment.
Addressed in 6f5165d. Full destination-target resolution is now limited to existing targets. A narrowly scoped resolver converts pathlib's pre-3.13 symlink-loop RuntimeError to OSError(ELOOP), covering cyclic parents as well as cyclic leaf destinations.
The three focused cases (cyclic leaf, cyclic parent, and a broken alias into a missing source child) pass and verify entry/source preservation plus OSError details inside BeamIOError. The complete related suites give 144 passed, 1 platform skip, 50 subtests passed.
For the file-source case mentioned in the summary, a broken destination has neither directory flag set, so it does not enter the ancestry-resolution block; its existing copy2 behavior is unchanged.
|
Assigning reviewers: R: @shunping for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Fixes #41747.
Copying a local source file/directory onto an ancestor directory removes the destination tree, including the source and unrelated siblings, before failing. Directory copies into their own output descendants can also recurse into the output.
Check source access dependencies and output containment before destination removal. The source check follows the argument's ancestors and link targets, including intermediate links; drive-relative Windows paths are anchored without discarding symlink-sensitive
..components. Output checks use the actual entry and target locations so a safesource/../neighborcopy remains valid. Existing self-copy/hard-link checks, ordinary overwrites, byte paths and per-pair error aggregation remain intact. Cyclic leaf and parent paths retain filesystemOSErrordetails insideBeamIOError, including on Python versions where pathlib reports a symlink loop asRuntimeError. Ordinary file-to-file copies skip the ancestry traversal.Validation on Windows / Python 3.11.7:
..semantics).localfilesystem_test.py,filesystem_test.pyandfilesystems_test.py: 144 passed, 1 skipped, 50 subtests passed. Coverage includes the real publicFileSystems.copy, source/sibling preservation, link aliases and chains, cycle termination, C:/D: drive-relative probes, valid sibling/overwrite copies and mixed-batch errors. Tripwires prevent unsafe recursive-copy/deletion entry in the descendant/link tests.git diff --checkpasses.The source checkout reused released 2.78.0 generated protobuf bindings in the ignored API directory. A fresh SDK/protobuf build, live POSIX run and full runner/OS matrix were not run locally. This is a per-pair preflight check; concurrent namespace mutation, atomic copying and symlink graphs nested inside copied directory contents retain existing behavior.