From 6cfb86d2a7861114ec9be3577713ea0b90b75c00 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Mon, 27 Apr 2026 07:31:23 +0200 Subject: [PATCH] lib.FileContext.is_dir(): Add follow_symlinks Make FileContext.is_dir() usable: - Add follow_symlinks parameter meant to do the obvious - Fix missing await Signed-off-by: Jan Lindemann --- src/python/jw/pkg/lib/FileContext.py | 9 +++++---- src/python/jw/pkg/lib/ec/Local.py | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/python/jw/pkg/lib/FileContext.py b/src/python/jw/pkg/lib/FileContext.py index 1672f317..e238c7c3 100644 --- a/src/python/jw/pkg/lib/FileContext.py +++ b/src/python/jw/pkg/lib/FileContext.py @@ -276,9 +276,10 @@ class FileContext(abc.ABC): async def file_exists(self, path: str) -> bool: return await self._file_exists(self._chroot(path)) - async def _is_dir(self, path: str) -> bool: + async def _is_dir(self, path: str, follow_symlinks: bool) -> bool: + import stat try: - return S_ISDIR(await self._stat(path).st_mode) + return stat.S_ISDIR((await self._stat(path, follow_symlinks)).mode) except NotImplementedError: log(DEBUG, f'{self.log_name} doesn\'t implement stat(), judging by trailing slash if {path} is a directory') return path[-1] == '/' @@ -290,8 +291,8 @@ class FileContext(abc.ABC): raise return False - async def is_dir(self, path: str) -> bool: - return self._is_dir(self._chroot(path)) + async def is_dir(self, path: str, follow_symlinks=True) -> bool: + return await self._is_dir(self._chroot(path), follow_symlinks=follow_symlinks) @classmethod def create(cls, uri: str, *args, **kwargs) -> Self: diff --git a/src/python/jw/pkg/lib/ec/Local.py b/src/python/jw/pkg/lib/ec/Local.py index f150b2f8..84787b34 100644 --- a/src/python/jw/pkg/lib/ec/Local.py +++ b/src/python/jw/pkg/lib/ec/Local.py @@ -172,5 +172,7 @@ class Local(Base): async def _chmod(self, path: str, mode: int) -> None: os.chmod(path, mode) - async def _is_dir(self, path: str) -> bool: + async def _is_dir(self, path: str, follow_symlinks: bool) -> bool: + if (not follow_symlinks) and os.islink(path): + return False return os.path.isdir(path)