JustinBinber commented on code in PR #41748:
URL: https://github.com/apache/beam/pull/41748#discussion_r4770839591
##########
sdks/python/apache_beam/io/localfilesystem.py:
##########
@@ -221,19 +226,75 @@ 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 _resolve_path(path):
+ try:
+ return path.resolve()
+ except RuntimeError as error:
+ # Before Python 3.13 pathlib reports symlink loops as RuntimeError.
+ raise OSError(errno.ELOOP, str(error)) from error
+
+ 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(_resolve_path(entry.parent)), 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)
Review Comment:
Updated in
[8ff574f](https://github.com/apache/beam/commit/8ff574f573d5de60cfa4d9d0552f99e72246d59d).
The implementation now uses `realpath` plus case-normalized `commonpath` and
copies from the resolved source, removing the custom ancestor/readlink walk and
pathlib exception handling. A source link under an overwritten destination can
therefore point to data outside it without invalidating the copy.
One small additional check is necessary: the resolved destination parent
must also be outside a directory source. A destination *file* symlink inside
the source can resolve outside it, but `os.remove()` then unlinks it and
`copytree` would recreate recursive output inside the source. A bounded
regression failed before this check and now verifies rejection before the link
or data is removed.
Paths are decoded consistently for bytes/string compatibility. The three
explicit offline filesystem suites pass: **134 tests and 50 subtests**, with
configured YAPF/Ruff hooks and Pyrefly also passing on Windows / Python 3.11.7.
##########
sdks/python/apache_beam/io/localfilesystem_test.py:
##########
@@ -424,6 +424,411 @@ def test_copy_directory(self):
self.fs.copy([path_t1], [path_t2])
self.assertTrue(filecmp.cmp(path1, path2))
+ @parameterized.expand([
Review Comment:
Simplified in
[8ff574f](https://github.com/apache/beam/commit/8ff574f573d5de60cfa4d9d0552f99e72246d59d):
the added test block is now **178 lines instead of 405**, with **21 cases
instead of 32**. It reuses `make_tree` / `check_tree` and drops the custom
ancestor/link-walk combinations.
The remaining cases verify public copy outcomes, source/destination data
preservation, ordinary overwrites, path representations and batch errors. The
`copytree` tripwires are retained only where an incorrect guard could otherwise
recurse without bound; there are no internal helper-call assertions. The
complete three-file regression run passes **134 tests and 50 subtests**.
--
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]