Revert "llnl.util.filesystem.find: restore old error handling (#47463)"

This reverts commit a31c525778.
This commit is contained in:
Harmen Stoppels 2024-11-07 20:53:28 +01:00 committed by Peter Scheibel
parent 60ba61f6b2
commit 4fbdf2f2c0

View File

@ -1741,11 +1741,6 @@ def find(root, files, recursive=True, max_depth: Optional[int] = None):
return result
def _log_file_access_issue(e: OSError, path: str) -> None:
errno_name = errno.errorcode.get(e.errno, "UNKNOWN")
tty.debug(f"find must skip {path}: {errno_name} {e}")
@system_path_filter(arg_slice=slice(1))
def find_max_depth(root, globs, max_depth: Optional[int] = None):
"""Given a set of non-recursive glob file patterns, finds all
@ -1759,10 +1754,19 @@ def find_max_depth(root, globs, max_depth: Optional[int] = None):
If ``globs`` is a list, files matching earlier entries are placed
in the return value before files matching later entries.
"""
# If root doesn't exist, then we say we found nothing. If it
# exists but is not a dir, we assume the user would want to
# know; likewise if it exists but we do not have permission to
# access it.
try:
stat_root = os.stat(root)
except OSError:
except OSError as e:
if e.errno == errno.ENOENT:
return []
else:
raise
if not stat.S_ISDIR(stat_root.st_mode):
raise ValueError(f"{root} is not a directory")
if max_depth is None:
max_depth = sys.maxsize
@ -1786,6 +1790,10 @@ def _dir_id(stat_info):
# https://github.com/python/cpython/blob/3.9/Python/fileutils.c
return (stat_info.st_ino, stat_info.st_dev)
def _log_file_access_issue(e):
errno_name = errno.errorcode.get(e.errno, "UNKNOWN")
tty.debug(f"find must skip {dir_entry.path}: {errno_name} {str(e)}")
visited_dirs = set([_dir_id(stat_root)])
# Each queue item stores the depth and path
@ -1800,8 +1808,9 @@ def _dir_id(stat_info):
depth, next_dir = dir_queue.pop()
try:
dir_iter = os.scandir(next_dir)
except OSError as e:
_log_file_access_issue(e, next_dir)
except OSError:
# Most commonly, this would be a permissions issue, for
# example if we are scanning an external directory like /usr
continue
with dir_iter:
@ -1812,7 +1821,7 @@ def _dir_id(stat_info):
except OSError as e:
# Possible permission issue, or a symlink that cannot
# be resolved (ELOOP).
_log_file_access_issue(e, dir_entry.path)
_log_file_access_issue(e)
continue
if it_is_a_dir and (depth < max_depth):
@ -1828,7 +1837,7 @@ def _dir_id(stat_info):
else:
stat_info = dir_entry.stat(follow_symlinks=True)
except OSError as e:
_log_file_access_issue(e, dir_entry.path)
_log_file_access_issue(e)
continue
dir_id = _dir_id(stat_info)