shunping commented on code in PR #41748:
URL: https://github.com/apache/beam/pull/41748#discussion_r4748435962
##########
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:
I think the logic is very complicated.
Could we simplify this to `os.path.realpath` on both sides plus an
`os.path.commonpath` containment check, the same idea as `shutil.move`'s
`_destinsrc`? Copying from the resolved source path also removes the need to
walk intermediate symlinks. That would cut the logic down to about 10 lines and
drop the custom readlink, `winerror 4390` and `RuntimeError` handling.
```python
def _is_within(path, parent):
# Both arguments should already be realpath'd.
try:
return os.path.commonpath([path, parent]) == parent
except ValueError: # different drives on Windows
return False
def _copy_path(source, destination):
real_src = os.path.realpath(source)
real_dst = os.path.realpath(destination)
if os.path.exists(destination):
if os.path.samefile(real_src, real_dst):
raise shutil.SameFileError(...)
if _is_within(real_src, real_dst):
raise IOError('Destination %r contains source %r' % (destination,
source))
if os.path.isdir(real_src) and _is_within(real_dst, real_src):
raise IOError('Cannot copy %r into itself %r' % (source, destination))
# ... existing remove + copy, but read from real_src
```
##########
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:
I think the logic here is very complicated.
Could we simplify this to `os.path.realpath` on both sides plus an
`os.path.commonpath` containment check, the same idea as `shutil.move`'s
`_destinsrc`? Copying from the resolved source path also removes the need to
walk intermediate symlinks. That would cut the logic down to about 10 lines and
drop the custom readlink, `winerror 4390` and `RuntimeError` handling.
```python
def _is_within(path, parent):
# Both arguments should already be realpath'd.
try:
return os.path.commonpath([path, parent]) == parent
except ValueError: # different drives on Windows
return False
def _copy_path(source, destination):
real_src = os.path.realpath(source)
real_dst = os.path.realpath(destination)
if os.path.exists(destination):
if os.path.samefile(real_src, real_dst):
raise shutil.SameFileError(...)
if _is_within(real_src, real_dst):
raise IOError('Destination %r contains source %r' % (destination,
source))
if os.path.isdir(real_src) and _is_within(real_dst, real_src):
raise IOError('Cannot copy %r into itself %r' % (source, destination))
# ... existing remove + copy, but read from real_src
```
--
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]