Skip to content

Commit a551c4a

Browse files
committed
relink.py: Refactor to new fn handle_non_dir().
1 parent 9319a84 commit a551c4a

1 file changed

Lines changed: 29 additions & 10 deletions

File tree

relink.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ def always(self, message, *args, **kwargs):
3434
logging.Logger.always = always
3535

3636

37+
def handle_non_dir(entry, user_uid):
38+
"""
39+
Check if a non-directory entry is owned by the user and should be processed.
40+
41+
Args:
42+
entry (os.DirEntry): A directory entry from os.scandir().
43+
user_uid (int): The UID of the user whose files to find.
44+
45+
Returns:
46+
str or None: The absolute path to the file if it's owned by the user
47+
and is a regular file (not a symlink), otherwise None.
48+
"""
49+
# Is this even owned by the user?
50+
if entry.stat(follow_symlinks=False).st_uid == user_uid:
51+
52+
# Return if it's a file (not following symlinks)
53+
if entry.is_file(follow_symlinks=False):
54+
return entry.path
55+
56+
# Log about skipping symlinks
57+
if entry.is_symlink():
58+
logger.debug("Skipping symlink: %s", entry.path)
59+
60+
return None
61+
62+
3763
def find_owned_files_scandir(directory, user_uid):
3864
"""
3965
Efficiently find all files owned by a specific user using os.scandir().
@@ -56,16 +82,9 @@ def find_owned_files_scandir(directory, user_uid):
5682
if entry.is_dir(follow_symlinks=False):
5783
yield from find_owned_files_scandir(entry.path, user_uid)
5884

59-
# Is this owned by the user?
60-
elif entry.stat(follow_symlinks=False).st_uid == user_uid:
61-
62-
# Return if it's a file (not following symlinks)
63-
if entry.is_file(follow_symlinks=False):
64-
yield entry.path
65-
66-
# Skip symlinks
67-
elif entry.is_symlink():
68-
logger.debug("Skipping symlink: %s", entry.path)
85+
# Things other than directories are handled separately
86+
elif (entry_path := handle_non_dir(entry, user_uid)) is not None:
87+
yield entry_path
6988

7089
except (OSError, PermissionError) as e:
7190
logger.debug("Error accessing %s: %s. Skipping.", entry.path, e)

0 commit comments

Comments
 (0)