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 <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-27 07:31:23 +02:00
commit 6cfb86d2a7
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
2 changed files with 8 additions and 5 deletions

View file

@ -276,9 +276,10 @@ class FileContext(abc.ABC):
async def file_exists(self, path: str) -> bool: async def file_exists(self, path: str) -> bool:
return await self._file_exists(self._chroot(path)) 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: try:
return S_ISDIR(await self._stat(path).st_mode) return stat.S_ISDIR((await self._stat(path, follow_symlinks)).mode)
except NotImplementedError: except NotImplementedError:
log(DEBUG, f'{self.log_name} doesn\'t implement stat(), judging by trailing slash if {path} is a directory') log(DEBUG, f'{self.log_name} doesn\'t implement stat(), judging by trailing slash if {path} is a directory')
return path[-1] == '/' return path[-1] == '/'
@ -290,8 +291,8 @@ class FileContext(abc.ABC):
raise raise
return False return False
async def is_dir(self, path: str) -> bool: async def is_dir(self, path: str, follow_symlinks=True) -> bool:
return self._is_dir(self._chroot(path)) return await self._is_dir(self._chroot(path), follow_symlinks=follow_symlinks)
@classmethod @classmethod
def create(cls, uri: str, *args, **kwargs) -> Self: def create(cls, uri: str, *args, **kwargs) -> Self:

View file

@ -172,5 +172,7 @@ class Local(Base):
async def _chmod(self, path: str, mode: int) -> None: async def _chmod(self, path: str, mode: int) -> None:
os.chmod(path, mode) 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) return os.path.isdir(path)