From b15f3a6d4ac065060dbb4dac72919e2d89128896 Mon Sep 17 00:00:00 2001 From: tsukumi <39271166+tsukumijima@users.noreply.github.com> Date: Sat, 9 Aug 2025 21:23:44 +0900 Subject: [PATCH] Ignore inaccessible paths (PermissionError) in scanning of directories (#427) * Fix an error that occurred when trying to scan an inaccessible directory for example, when the directory permissions were set to 700. * Fix redundant error handling * Fix format --------- Co-authored-by: tsukumi --- src/serena/util/file_system.py | 56 ++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/serena/util/file_system.py b/src/serena/util/file_system.py index 6f2aaa5f..5f8ca826 100644 --- a/src/serena/util/file_system.py +++ b/src/serena/util/file_system.py @@ -40,31 +40,41 @@ def scan_directory( abs_path = os.path.abspath(path) rel_base = os.path.abspath(relative_to) if relative_to else None - with os.scandir(abs_path) as entries: - for entry in entries: - entry_path = entry.path + try: + with os.scandir(abs_path) as entries: + for entry in entries: + try: + entry_path = entry.path - if rel_base: - result_path = os.path.relpath(entry_path, rel_base) - else: - result_path = entry_path + if rel_base: + result_path = os.path.relpath(entry_path, rel_base) + else: + result_path = entry_path - if entry.is_file(): - if not is_ignored_file(entry_path): - files.append(result_path) - elif entry.is_dir(): - if not is_ignored_dir(entry_path): - directories.append(result_path) - if recursive: - sub_result = scan_directory( - entry_path, - recursive=True, - relative_to=relative_to, - is_ignored_dir=is_ignored_dir, - is_ignored_file=is_ignored_file, - ) - files.extend(sub_result.files) - directories.extend(sub_result.directories) + if entry.is_file(): + if not is_ignored_file(entry_path): + files.append(result_path) + elif entry.is_dir(): + if not is_ignored_dir(entry_path): + directories.append(result_path) + if recursive: + sub_result = scan_directory( + entry_path, + recursive=True, + relative_to=relative_to, + is_ignored_dir=is_ignored_dir, + is_ignored_file=is_ignored_file, + ) + files.extend(sub_result.files) + directories.extend(sub_result.directories) + except PermissionError as ex: + # Skip files/directories that cannot be accessed due to permission issues + log.debug(f"Skipping entry due to permission error: {entry.path}", exc_info=ex) + continue + except PermissionError as ex: + # Skip the entire directory if it cannot be accessed + log.debug(f"Skipping directory due to permission error: {abs_path}", exc_info=ex) + return ScanResult([], []) return ScanResult(directories, files)