Skip to content

Commit

Permalink
Revert "llnl.util.filesystem.find: restore old error handling (spack#…
Browse files Browse the repository at this point in the history
…47463)"

This reverts commit a31c525.
  • Loading branch information
haampie authored and scheibelp committed Nov 7, 2024
1 parent 60ba61f commit 4fbdf2f
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/spack/llnl/util/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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:
return []
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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand All @@ -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)
Expand Down

0 comments on commit 4fbdf2f

Please sign in to comment.