:py:mod:`dissect.target.helpers.fsutil` ======================================= .. py:module:: dissect.target.helpers.fsutil .. autoapi-nested-parse:: Filesystem and path related utilities. Re-Exports ~~~~~~~~~~ .. autoapisummary:: dissect.target.helpers.polypath.abspath dissect.target.helpers.polypath.basename dissect.target.helpers.polypath.commonpath dissect.target.helpers.polypath.dirname dissect.target.helpers.polypath.isabs dissect.target.helpers.polypath.isreserved dissect.target.helpers.polypath.join dissect.target.helpers.polypath.normalize dissect.target.helpers.polypath.normpath dissect.target.helpers.polypath.relpath dissect.target.helpers.polypath.split dissect.target.helpers.polypath.splitdrive dissect.target.helpers.polypath.splitext dissect.target.helpers.polypath.splitroot Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: dissect.target.helpers.fsutil.stat_result Functions ~~~~~~~~~ .. autoapisummary:: :nosignatures: dissect.target.helpers.fsutil.generate_addr dissect.target.helpers.fsutil.walk dissect.target.helpers.fsutil.walk_ext dissect.target.helpers.fsutil.recurse dissect.target.helpers.fsutil.glob_split dissect.target.helpers.fsutil.glob_ext dissect.target.helpers.fsutil.has_glob_magic dissect.target.helpers.fsutil.resolve_link dissect.target.helpers.fsutil.open_decompress dissect.target.helpers.fsutil.reverse_read dissect.target.helpers.fsutil.reverse_readlines dissect.target.helpers.fsutil.fs_attrs .. py:function:: generate_addr(path: str | pathlib.Path, alt_separator: str = '') -> int .. py:class:: stat_result(s: collections.abc.Sequence[Any]) Custom stat_result object, designed to mimick os.stat_result. The real stat_result is a CPython internal StructSeq, which kind of behaves like a namedtuple on steroids. We try to emulate some of that behaviour here. For consistency this class is also called stat_result. .. py:attribute:: __slots__ .. py:attribute:: st_mode .. py:attribute:: st_ino .. py:attribute:: st_dev .. py:attribute:: st_nlink .. py:attribute:: st_uid .. py:attribute:: st_gid .. py:attribute:: st_size .. py:attribute:: st_atime .. py:attribute:: st_mtime .. py:attribute:: st_ctime .. py:attribute:: st_atime_ns .. py:attribute:: st_mtime_ns .. py:attribute:: st_ctime_ns .. py:attribute:: st_blksize .. py:attribute:: st_blocks .. py:attribute:: st_rdev .. py:attribute:: st_flags .. py:attribute:: st_gen .. py:attribute:: st_birthtime .. py:attribute:: st_file_attributes .. py:attribute:: st_fstype .. py:attribute:: st_reparse_tag .. py:attribute:: st_birthtime_ns .. py:method:: __eq__(other: object) -> bool .. py:method:: __ne__(other: object) -> bool .. py:method:: __getitem__(item: int) -> int .. py:method:: __iter__() -> collections.abc.Iterator[int] .. py:method:: __repr__() -> str .. py:method:: copy(other: stat_result) -> Self :classmethod: .. py:function:: walk(path_entry: dissect.target.filesystem.FilesystemEntry, topdown: bool = True, onerror: collections.abc.Callable[[Exception], None] | None = None, followlinks: bool = False) -> collections.abc.Iterator[tuple[list[dissect.target.filesystem.FilesystemEntry], list[dissect.target.filesystem.FilesystemEntry], list[dissect.target.filesystem.FilesystemEntry]]] .. py:function:: walk_ext(path_entry: dissect.target.filesystem.FilesystemEntry, topdown: bool = True, onerror: collections.abc.Callable[[Exception], None] | None = None, followlinks: bool = False) -> collections.abc.Iterator[tuple[list[dissect.target.filesystem.FilesystemEntry], list[dissect.target.filesystem.FilesystemEntry], list[dissect.target.filesystem.FilesystemEntry]]] .. py:function:: recurse(entry: dissect.target.filesystem.FilesystemEntry) -> collections.abc.Iterator[dissect.target.filesystem.FilesystemEntry] Recursively walk the given :class:`FilesystemEntry`, yields :class:`DirEntry` instances. .. py:function:: glob_split(pattern: str, alt_separator: str = '') -> tuple[str, str] Split a pattern on path part boundaries on the first path part with a glob pattern. :param pattern: A glob pattern to match names of filesystem entries against. :param alt_separator: An alternative path separator in use by the filesystem being matched. :returns: A tuple of a string with path parts up to the first path part that has a glob pattern and a string of the remaining path parts. .. py:function:: glob_ext(direntry: dissect.target.filesystem.FilesystemEntry, pattern: str) -> collections.abc.Iterator[dissect.target.filesystem.FilesystemEntry] Recursively search and return filesystem entries matching a given glob pattern. :param direntry: The filesystem entry relative to which to search. :param pattern: A glob pattern to match names of filesystem entries against. :Yields: Matching filesystem entries (files and/or directories). .. py:function:: has_glob_magic(s: str) -> bool .. py:function:: resolve_link(fs: dissect.target.filesystem.Filesystem, link: str, path: str, *, alt_separator: str = '', previous_links: set[str] | None = None) -> dissect.target.filesystem.FilesystemEntry Resolves a symlink to its actual path. It stops resolving once it detects an infinite recursion loop. .. py:function:: open_decompress(path: dissect.target.helpers.compat.path_313.TargetPath | None = None, mode: str = 'rb', *, fileobj: BinaryIO | None = None, encoding: str | None = 'UTF-8', errors: str | None = 'backslashreplace', newline: str | None = None) -> BinaryIO | TextIO Open and decompress a file. Handles gz, bz2 and zstd files. Uncompressed files are opened as-is. When passing in an already opened ``fileobj``, the mode, encoding, errors and newline arguments are ignored. :param path: The path to the file to open and decompress. It is assumed this path exists. :param mode: The mode in which to open the file. :param fileobj: The file-like object to open and decompress. This is mutually exclusive with path. :param encoding: The decoding for text streams. By default UTF-8 encoding is used. :param errors: The error handling for text streams. By default we're more lenient and use ``backslashreplace``. :param newline: How newlines are handled for text streams. :returns: An binary or text IO stream, depending on the mode with which the file was opened. :raises ValueError: path and fileobj are mutually exclusive, but one of them is required .. rubric:: Example .. code-block:: python bytes_buf = open_decompress(Path("/dir/file.gz")).read() for line in open_decompress(Path("/dir/file.gz"), "rt"): print(line) .. py:function:: reverse_read(fh: BinaryIO, chunk_size: int = io.DEFAULT_BUFFER_SIZE, reverse_chunk: bool = True) -> collections.abc.Iterator[bytes] Like iterating over chunks of a binary file-like object, but starting from the end of the file. :param fh: The file-like object (opened in binary mode) to read from. :param chunk_size: The chunk size to use for iterating over bytes (default: 8KB). :param reverse_chunk: Whether we should reverse the bytes of each chunk (default: True). :returns: An iterator of byte chunks, starting from the end of the file-like object and moving to the start. .. py:function:: reverse_readlines(fh: TextIO, chunk_size: int = 1024 * 1024 * 8) -> collections.abc.Iterator[str] Like iterating over a ``TextIO`` file-like object, but starting from the end of the file. :param fh: The file-like object (opened in text mode) to iterate lines from. :param chunk_size: The chunk size to use for iterating over lines. :returns: An iterator of lines from the file-like object, in reverse. .. py:function:: fs_attrs(path: os.PathLike | str | bytes, follow_symlinks: bool = True) -> dict[os.PathLike | str | bytes, bytes] Return the extended attributes for a given path on the local filesystem. This is currently only implemented for Linux using os.listxattr and related functions. :param path: The path to get the extended attributes for. :param follow_symlinks: Wether to follow the symlink if the given path is a symlink. :returns: A dict containing the attribute names as keys and their values.