lib.FileContext: Support chroot

Add a bool parameter "chroot" to __init__(). If passed, all
path-centric operations act as if .root was prepended to it.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-22 07:20:54 +02:00
commit 7547d13a6d
Signed by: jan
GPG key ID: 3750640C9E25DD61

View file

@ -14,9 +14,10 @@ from .base import Input, InputMode, Result, StatResult
class FileContext(abc.ABC):
def __init__(self, uri: str, interactive: bool|None=None, verbose_default=False):
def __init__(self, uri: str, interactive: bool|None=None, verbose_default=False, chroot: bool=False):
self.__uri = uri
self.__id, self.__root = self.split_uri(uri)
self.__chroot = chroot
self.__interactive = interactive
self.__verbose_default = verbose_default
self.__log_name: str|None = None
@ -28,6 +29,15 @@ class FileContext(abc.ABC):
async def __aexit__(self, exc_type, exc, tb):
await self.close()
def _chroot(self, path: str) -> str:
if not self.__chroot:
return path
if not len(path):
return self.__root
if path[-1] == '/':
return self.__root + path
return self.__root + '/' + path
@classmethod
def schema_from_uri(cls, uri: str) -> str:
tokens = re.split(r'://', uri)
@ -98,7 +108,7 @@ class FileContext(abc.ABC):
group: str|None=None,
mode: str|None=None,
) -> Result:
return await self._get(path, wd=wd, throw=throw, verbose=verbose, title=title)
return await self._get(self._chroot(path), wd=wd, throw=throw, verbose=verbose, title=title)
async def _put(
self,
@ -129,20 +139,20 @@ class FileContext(abc.ABC):
atomic: bool = False
) -> Result:
mode_str = None if mode is None else oct(mode).replace('0o', '0')
return await self._put(path, content, wd=wd, throw=throw, verbose=verbose,
return await self._put(self._chroot(path), content, wd=wd, throw=throw, verbose=verbose,
title=title, owner=owner, group=group, mode=mode_str, atomic=atomic)
async def _unlink(self, path: str) -> None:
raise NotImplementedError(f'{self.log_name}: unlink("{path}") is not implemented')
async def unlink(self, path: str) -> None:
return await self._unlink(path)
return await self._unlink(self._chroot(path))
async def _erase(self, path: str) -> None:
raise NotImplementedError(f'{self.log_name}: erase("{path}") is not implemented')
async def erase(self, path: str) -> None:
return await self._erase(path)
return await self._erase(self._chroot(path))
async def _rename(self, src: str, dst: str) -> None:
raise NotImplementedError(f'{self.log_name}: rename("{path}") is not implemented')
@ -154,7 +164,7 @@ class FileContext(abc.ABC):
raise NotImplementedError(f'{self.log_name}: mktemp("{path}") is not implemented')
async def mktemp(self, tmpl: str, directory: bool=False) -> None:
return await self._mktemp(tmpl, directory)
return await self._mktemp(_chroot(tmpl), directory)
async def _chown(self, path: str, owner: str|None, group: str|None) -> None:
raise NotImplementedError(f'{self.log_name}: chown("{path}") is not implemented')
@ -162,13 +172,13 @@ class FileContext(abc.ABC):
async def chown(self, path: str, owner: str|None=None, group: str|None=None) -> None:
if owner is None and group is None:
raise ValueError(f'Tried to change ownership of {path} specifying neither owner nor group')
return await self._chown(path, owner, group)
return await self._chown(self._chroot(path), owner, group)
async def _chmod(self, path: str, mode: int) -> None:
raise NotImplementedError(f'{self.log_name}: chmod("{path}") is not implemented')
async def chmod(self, path: str, mode: int) -> None:
return await self._chmod(path, mode)
return await self._chmod(self._chroot(path), mode)
async def _stat(self, path: str, follow_symlinks: bool) -> StatResult:
raise NotImplementedError(f'{self.log_name}: lstat("{path}") is not implemented')
@ -176,7 +186,7 @@ class FileContext(abc.ABC):
async def stat(self, path: str, follow_symlinks: bool=True) -> StatResult:
if not isinstance(path, str):
raise TypeError(f"path must be str, got {type(path).__name__}")
return await self._stat(path, follow_symlinks)
return await self._stat(self._chroot(path), follow_symlinks)
async def _file_exists(self, path: str) -> bool:
try:
@ -190,7 +200,7 @@ class FileContext(abc.ABC):
return True
async def file_exists(self, path: str) -> bool:
return await self._file_exists(path)
return await self._file_exists(self._chroot(path))
async def _is_dir(self, path: str) -> bool:
try:
@ -207,9 +217,7 @@ class FileContext(abc.ABC):
return False
async def is_dir(self, path: str) -> bool:
if not path:
raise ValueError('Tried to investigate file system resource with empty path')
return self._is_dir(path)
return self._is_dir(self._chroot(path))
async def _close(self) -> None:
pass