JustinBinber commented on code in PR #41748:
URL: https://github.com/apache/beam/pull/41748#discussion_r4731824155
##########
sdks/python/apache_beam/io/localfilesystem.py:
##########
@@ -221,19 +226,66 @@ def copy(self, source_file_names, destination_file_names):
"be equal in length")
assert len(source_file_names) == len(destination_file_names), err_msg
+ def _absolute_path(path):
+ path = Path(os.fsdecode(path))
+ if path.drive and not path.root:
+ # Expand the drive anchor without collapsing symlink-sensitive '..'.
+ path = Path(os.path.abspath(path.drive + '.'), *path.parts[1:])
+ return path.absolute()
+
+ def _ancestors(path):
+ pending = [_absolute_path(path)]
+ seen_links = set()
+ while pending:
+ current = pending.pop()
+ yield from current.parents
+ for entry in (current, *current.parents):
+ try:
+ target = entry.readlink()
+ except OSError as error:
+ # Windows 4390 means the path is not a reparse point.
+ if (error.errno in (errno.EINVAL, errno.ENOENT, errno.ENOTDIR) or
+ getattr(error, 'winerror', None) == 4390):
+ continue
+ raise
+ link = (str(entry.parent.resolve()), entry.name)
+ if link not in seen_links:
+ seen_links.add(link)
+ pending.append(_absolute_path(entry.parent / target))
+
def _copy_path(source, destination):
"""Recursively copy the file tree from the source to the destination
"""
try:
- if os.path.exists(destination):
- if os.path.samefile(source, destination):
- raise shutil.SameFileError(
- '%r and %r are the same file' % (source, destination))
- if os.path.isdir(destination):
+ source_is_directory = os.path.isdir(source)
+ destination_is_directory = os.path.isdir(destination)
+ destination_exists = os.path.exists(destination)
+ if destination_exists and os.path.samefile(source, destination):
+ raise shutil.SameFileError(
+ '%r and %r are the same file' % (source, destination))
+ if source_is_directory or destination_is_directory:
+ # Include entries and intermediate link targets needed by the copy.
+ source_in_destination = destination_is_directory and any(
+ parent.exists() and os.path.samefile(parent, destination)
+ for parent in _ancestors(source))
+ # Normalize the output location so harmless sibling paths using ..
work.
+ destination_path = _absolute_path(destination)
+ destination_entry = (
+ destination_path.parent.resolve() / destination_path.name)
+ destination_in_source = source_is_directory and any(
+ parent.exists() and os.path.samefile(parent, source)
+ for output in (destination_entry, destination_path.resolve())
+ for parent in output.parents)
+ if source_in_destination or destination_in_source:
+ raise IOError(
+ 'Cannot copy between ancestor and descendant paths: %r, %r' %
+ (source, destination))
+ if destination_exists:
+ if destination_is_directory:
Review Comment:
Addressed in
[6f5165d](https://github.com/apache/beam/commit/6f5165de1b2420ba13f326e3ff90ab23736ab068).
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]