refactor(tools): share search directory lifetime checks
This commit is contained in:
1 parent
cdb3fd835f
commit
b9c1fc7196
1 file changed
+21
-30
+21
-30
@@ -243,6 +243,7 @@ class FileSearch:
|
|||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def _open_search_directory(self, directory: Path) -> Generator[int]:
|
def _open_search_directory(self, directory: Path) -> Generator[int]:
|
||||||
|
"""Pin a directory for a scan and validate it before releasing results."""
|
||||||
relative = directory.relative_to(self.root)
|
relative = directory.relative_to(self.root)
|
||||||
if ".." in relative.parts:
|
if ".." in relative.parts:
|
||||||
raise ValueError("Expected a directory within the search root")
|
raise ValueError("Expected a directory within the search root")
|
||||||
@@ -257,16 +258,20 @@ class FileSearch:
|
|||||||
os.close(fd)
|
os.close(fd)
|
||||||
fd = child
|
fd = child
|
||||||
yield fd
|
yield fd
|
||||||
|
# A descriptor survives a rename. Discard its results if the
|
||||||
|
# authorized path no longer points to the directory we scanned.
|
||||||
|
try:
|
||||||
|
if (
|
||||||
|
Path(os.path.realpath(directory, strict=True)) == directory
|
||||||
|
and os.path.samestat(os.fstat(fd), directory.stat(follow_symlinks=False))
|
||||||
|
):
|
||||||
|
return
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
raise PermissionError("Search directory changed during the scan; retry the search.")
|
||||||
finally:
|
finally:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _directory_is_current(directory: Path, fd: int) -> bool:
|
|
||||||
return (
|
|
||||||
Path(os.path.realpath(directory, strict=True)) == directory
|
|
||||||
and os.path.samestat(os.fstat(fd), directory.stat(follow_symlinks=False))
|
|
||||||
)
|
|
||||||
|
|
||||||
def modified_children(
|
def modified_children(
|
||||||
self, directory: Path, names: list[str], *, checkpoint: Callable[[], None],
|
self, directory: Path, names: list[str], *, checkpoint: Callable[[], None],
|
||||||
) -> list[float]:
|
) -> list[float]:
|
||||||
@@ -292,10 +297,7 @@ class FileSearch:
|
|||||||
self.modified(directory / name) if stat.S_ISLNK(info.st_mode)
|
self.modified(directory / name) if stat.S_ISLNK(info.st_mode)
|
||||||
else info.st_mtime
|
else info.st_mtime
|
||||||
)
|
)
|
||||||
# An open directory survives a rename. Do not use its batch if
|
return times
|
||||||
# the authorized path no longer points to that directory.
|
|
||||||
if self._directory_is_current(directory, fd):
|
|
||||||
return times
|
|
||||||
except OSError:
|
except OSError:
|
||||||
pass # Reauthorize each child if the directory could not be pinned.
|
pass # Reauthorize each child if the directory could not be pinned.
|
||||||
|
|
||||||
@@ -345,38 +347,27 @@ class FileSearch:
|
|||||||
fd: int | None = None
|
fd: int | None = None
|
||||||
attempted = False
|
attempted = False
|
||||||
with ExitStack() as handle:
|
with ExitStack() as handle:
|
||||||
def check_parent() -> None:
|
|
||||||
if parent is not None and fd is not None:
|
|
||||||
try:
|
|
||||||
if self._directory_is_current(parent, fd):
|
|
||||||
return
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
raise PermissionError("Search directory changed during the scan; retry the search.")
|
|
||||||
|
|
||||||
def read(path: Path, limit: int) -> SearchBytes | None:
|
def read(path: Path, limit: int) -> SearchBytes | None:
|
||||||
nonlocal parent, fd, attempted
|
nonlocal parent, fd, attempted
|
||||||
if path.parent != parent:
|
directory = path.parent
|
||||||
check_parent()
|
if directory != parent:
|
||||||
handle.close()
|
handle.close()
|
||||||
parent, fd, attempted = path.parent, None, False
|
parent, fd, attempted = directory, None, False
|
||||||
return self._read_search_bytes(path, limit)
|
return self._read_search_bytes(path, limit)
|
||||||
if not attempted:
|
if not attempted:
|
||||||
attempted = True
|
attempted = True
|
||||||
# Only open directories with multiple candidates. If the host
|
# Only open directories with multiple candidates. If the host
|
||||||
# cannot pin this path, keep using per-file authorization.
|
# cannot pin this path, keep using per-file authorization.
|
||||||
if parent is not None and parent.is_relative_to(self.root):
|
try:
|
||||||
try:
|
_reject_device(directory, resolved=True)
|
||||||
_reject_device(parent, resolved=True)
|
fd = handle.enter_context(self._open_search_directory(directory))
|
||||||
fd = handle.enter_context(self._open_search_directory(parent))
|
except (OSError, ValueError):
|
||||||
except (OSError, ValueError):
|
pass
|
||||||
pass
|
|
||||||
if path.name in ("", ".", ".."):
|
if path.name in ("", ".", ".."):
|
||||||
return self._read_search_bytes(path, limit)
|
return self._read_search_bytes(path, limit)
|
||||||
return self._read_search_bytes(path, limit, fd)
|
return self._read_search_bytes(path, limit, fd)
|
||||||
|
|
||||||
yield read
|
yield read
|
||||||
check_parent()
|
|
||||||
|
|
||||||
|
|
||||||
def _regular_file(target: Path, *, suffix: str | None = None) -> ReadableFile:
|
def _regular_file(target: Path, *, suffix: str | None = None) -> ReadableFile:
|
||||||
|
|||||||
Reference in new issue
Block a user