dissect.target.helpers.fsutil#

Filesystem and path related utilities.

Module Contents#

Classes#

stat_result

Custom stat_result object, designed to mimick os.stat_result.

Functions#

abspath

basename

commonpath

dirname

isabs

join

normalize

normpath

relpath

split

splitroot

generate_addr

walk

walk_ext

glob_split

Split a pattern on path part boundaries on the first path part with a glob pattern.

glob_ext

Recursively search and return filesystem entries matching a given glob pattern.

has_glob_magic

resolve_link

Resolves a symlink to its actual path.

open_decompress

Open and decompress a file. Handles gz, bz2 and zstd files. Uncompressed files are opened as-is.

reverse_readlines

Like iterating over a TextIO file-like object, but starting from the end of the file.

fs_attrs

Return the extended attributes for a given path on the local filesystem.

Attributes#

dissect.target.helpers.fsutil.abspath(path: str, cwd: str = '', alt_separator: str = '') str#
dissect.target.helpers.fsutil.basename(path: str, alt_separator: str = '') str#
dissect.target.helpers.fsutil.commonpath(paths: list[str], alt_separator: str = '') str#
dissect.target.helpers.fsutil.dirname(path: str, alt_separator: str = '') str#
dissect.target.helpers.fsutil.isabs(path: str, alt_separator: str = '') bool#
dissect.target.helpers.fsutil.join(*args, alt_separator: str = '') str#
dissect.target.helpers.fsutil.normalize(path: str, alt_separator: str = '') str#
dissect.target.helpers.fsutil.normpath(path: str, alt_separator: str = '') str#
dissect.target.helpers.fsutil.relpath(path: str, start: str, alt_separator: str = '') str#
dissect.target.helpers.fsutil.split(path: str, alt_separator: str = '') str#
dissect.target.helpers.fsutil.splitdrive#
dissect.target.helpers.fsutil.splitext#
dissect.target.helpers.fsutil.splitroot(path: str, alt_separator: str = '') tuple[str, str]#
dissect.target.helpers.fsutil.generate_addr(path: str | pathlib.Path, alt_separator: str = '') int#
class dissect.target.helpers.fsutil.stat_result(s: 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.

__slots__#
__eq__(other) bool#

Return self==value.

__ne__(other) bool#

Return self!=value.

__getitem__(item) int#
__iter__() Iterator[int]#
__repr__() str#

Return repr(self).

classmethod copy(other) stat_result#
dissect.target.helpers.fsutil.walk(path_entry, topdown=True, onerror=None, followlinks=False)#
dissect.target.helpers.fsutil.walk_ext(path_entry, topdown=True, onerror=None, followlinks=False)#
dissect.target.helpers.fsutil.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.

Parameters:
  • pattern – A glob pattern to match names of filesystem entries against.

  • alt_separator – An optional 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.

dissect.target.helpers.fsutil.glob_ext(direntry: dissect.target.filesystem.FilesystemEntry, pattern: str) Iterator[dissect.target.filesystem.FilesystemEntry]#

Recursively search and return filesystem entries matching a given glob pattern.

Parameters:
  • direntry – The filesystem entry relative to which to search.

  • pattern – A glob pattern to match names of filesystem entries against.

Yields:

Matching filesystem entries (files and/or directories).

dissect.target.helpers.fsutil.has_glob_magic(s) bool#

Resolves a symlink to its actual path.

It stops resolving once it detects an infinite recursion loop.

dissect.target.helpers.fsutil.open_decompress(path: dissect.target.helpers.compat.path_312.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.

Parameters:
  • path – The path to the file to open and decompress. It is assumed this path exists.

  • mode – The mode in which to open the file.

  • fileobj – The file-like object to open and decompress. This is mutually exclusive with path.

  • encoding – The decoding for text streams. By default UTF-8 encoding is used.

  • errors – The error handling for text streams. By default we’re more lenient and use backslashreplace.

  • 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.

Example

bytes_buf = open_decompress(Path(“/dir/file.gz”)).read()

for line in open_decompress(Path(“/dir/file.gz”), “rt”):

print(line)

dissect.target.helpers.fsutil.reverse_readlines(fh: TextIO, chunk_size: int = 1024 * 1024 * 8) Iterator[str]#

Like iterating over a TextIO file-like object, but starting from the end of the file.

Parameters:
  • fh – The file-like object (opened in text mode) to iterate lines from.

  • chunk_size – The chunk size to use for iterating over lines.

Returns:

An iterator of lines from the file-like object, in reverse.

dissect.target.helpers.fsutil.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.

Parameters:
  • path – The path to get the extended attributes for.

  • 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.