dissect.target.helpers.fsutil
#
Pathlib like abstraction helpers for target filesystem.
Also contains some other filesystem related utilities.
Module Contents#
Classes#
Custom stat_result object, designed to mimick os.stat_result. |
|
Base class for manipulating paths without I/O. |
|
PurePath subclass that can make system calls. |
Functions#
Resolves a symlink to its actual path. |
|
Open and decompress a file. Handles gz and bz2 files. Uncompressed files are opened as-is. |
|
Like iterating over a |
|
Return the extended attributes for a given path on the local filesystem. |
Attributes#
- dissect.target.helpers.fsutil.HAVE_BZ2 = True#
- dissect.target.helpers.fsutil.log#
- dissect.target.helpers.fsutil.re_normalize_path#
- dissect.target.helpers.fsutil.re_normalize_sbs_path#
- dissect.target.helpers.fsutil.re_glob_magic#
- dissect.target.helpers.fsutil.re_glob_index#
- dissect.target.helpers.fsutil.normalize(path: str, alt_separator: str = '') str #
- dissect.target.helpers.fsutil.join(*args, alt_separator: str = '') str #
- dissect.target.helpers.fsutil.dirname(path: str, alt_separator: str = '') str #
- dissect.target.helpers.fsutil.basename(path: str, alt_separator: str = '') str #
- dissect.target.helpers.fsutil.split(path: str, alt_separator: str = '') str #
- dissect.target.helpers.fsutil.isabs(path: str, alt_separator: str = '') str #
- dissect.target.helpers.fsutil.normpath(path: str, alt_separator: str = '') str #
- dissect.target.helpers.fsutil.abspath(path: str, cwd: str = '', alt_separator: str = '') str #
- dissect.target.helpers.fsutil.relpath(path: str, start: str, alt_separator: str = '') str #
- dissect.target.helpers.fsutil.generate_addr(path: str | pathlib.Path, alt_separator: str = '') int #
- dissect.target.helpers.fsutil.splitext#
- 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 #
- class dissect.target.helpers.fsutil.PureDissectPath#
Bases:
pathlib.PurePath
Base class for manipulating paths without I/O.
PurePath represents a filesystem path and offers operations which don’t imply any actual filesystem I/O. Depending on your system, instantiating a PurePath will return either a PurePosixPath or a PureWindowsPath object. You can also instantiate either of these classes directly, regardless of your system.
- property parent#
The logical parent of the path.
- property parents#
A sequence of this path’s logical parents.
- __reduce__()#
Helper for pickle.
- with_name(name)#
Return a new path with the file name changed.
- with_stem(stem)#
Return a new path with the stem changed.
- with_suffix(suffix)#
Return a new path with the file suffix changed. If the path has no suffix, add given suffix. If the given suffix is an empty string, remove the suffix from the path.
- relative_to(*other)#
Return the relative path to another path identified by the passed arguments. If the operation is not possible (because this is not a subpath of the other path), raise ValueError.
- __rtruediv__(key)#
- class dissect.target.helpers.fsutil.TargetPath#
Bases:
pathlib.Path
,PureDissectPath
PurePath subclass that can make system calls.
Path represents a filesystem path but unlike PurePath, also offers methods to do system calls on path objects. Depending on your system, instantiating a Path will return either a PosixPath or a WindowsPath object. You can also instantiate a PosixPath or WindowsPath directly, but cannot instantiate a WindowsPath on a POSIX system or vice versa.
- __slots__ = '_entry'#
- get()#
- abstract classmethod cwd()#
Return a new path pointing to the current working directory (as returned by os.getcwd()).
- abstract classmethod home()#
Return a new path pointing to the user’s home directory (as returned by os.path.expanduser(‘~’)).
- iterdir()#
Iterate over the files in this directory. Does not yield any result for the special paths ‘.’ and ‘..’.
- abstract absolute()#
Return an absolute version of this path. This function works even if the path doesn’t point to anything.
No normalization is done, i.e. all ‘.’ and ‘..’ will be kept along. Use resolve() to get the canonical path to a file.
- resolve(strict=False)#
Make the path absolute, resolving all symlinks on the way and also normalizing it (for example turning slashes into backslashes under Windows).
- stat(*, follow_symlinks=True)#
Return the result of the stat() system call on this path, like os.stat() does.
- abstract owner()#
Return the login name of the file owner.
- abstract group()#
Return the group name of the file gid.
- open(mode='rb', buffering=0, encoding=None, errors=None, newline=None)#
Open the file pointed by this path and return a file object, as the built-in open() function does.
- abstract write_bytes(*args, **kwargs)#
Open the file in bytes mode, write to it, and close the file.
- abstract write_text(*args, **kwargs)#
Open the file in text mode, write to it, and close the file.
- readlink()#
Return the path to which the symbolic link points.
- abstract touch(*args, **kwargs)#
Create this file with the given access mode, if it doesn’t exist.
- abstract mkdir(*args, **kwargs)#
Create a new directory at this given path.
- abstract chmod(*args, **kwargs)#
Change the permissions of the path, like os.chmod().
- abstract lchmod(*args, **kwargs)#
Like chmod(), except if the path points to a symlink, the symlink’s permissions are changed, rather than its target’s.
- abstract unlink()#
Remove this file or link. If the path is a directory, use rmdir() instead.
- abstract rmdir()#
Remove this directory. The directory must be empty.
- abstract rename(*args, **kwargs)#
Rename this path to the target path.
The target path may be absolute or relative. Relative paths are interpreted relative to the current working directory, not the directory of the Path object.
Returns the new Path instance pointing to the target path.
- abstract replace(*args, **kwargs)#
Rename this path to the target path, overwriting if that path exists.
The target path may be absolute or relative. Relative paths are interpreted relative to the current working directory, not the directory of the Path object.
Returns the new Path instance pointing to the target path.
- abstract symlink_to(*args, **kwargs)#
Make this path a symlink pointing to the target path. Note the order of arguments (link, target) is the reverse of os.symlink.
- abstract hardlink_to(target)#
- abstract link_to(*args, **kwargs)#
Make the target path a hard link pointing to this path.
Note this function does not make this path a hard link to target, despite the implication of the function and argument names. The order of arguments (target, link) is the reverse of Path.symlink_to, but matches that of os.link.
- exists()#
Whether this path exists.
- is_dir()#
Whether this path is a directory.
- is_file()#
Whether this path is a regular file (also True for symlinks pointing to regular files).
- is_symlink()#
Whether this path is a symbolic link.
- abstract expanduser()#
Return a new path with expanded ~ and ~user constructs (as returned by os.path.expanduser)
- 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 = '') str #
- dissect.target.helpers.fsutil.glob_ext(direntry: dissect.target.filesystem.FilesystemEntry, pattern: str) dissect.target.filesystem.FilesystemEntry #
- dissect.target.helpers.fsutil.glob_ext1(direntry: dissect.target.filesystem.FilesystemEntry, pattern: str) dissect.target.filesystem.FilesystemEntry #
- dissect.target.helpers.fsutil.glob_ext0(direntry: dissect.target.filesystem.FilesystemEntry, base_name: str) list[dissect.target.filesystem.FilesystemEntry] #
- dissect.target.helpers.fsutil.has_glob_magic(s) bool #
- dissect.target.helpers.fsutil.resolve_link(fs: dissect.target.filesystem.Filesystem, entry: dissect.target.filesystem.FilesystemEntry, previous_links: set[str] = None) dissect.target.filesystem.FilesystemEntry #
Resolves a symlink to its actual path.
It stops resolving once it detects an infinite recursion loop.
- dissect.target.helpers.fsutil.open_decompress(path: TargetPath, mode: str = 'rb', encoding: str | None = 'UTF-8', errors: str | None = 'backslashreplace', newline: str | None = None) BinaryIO | TextIO #
Open and decompress a file. Handles gz and bz2 files. Uncompressed files are opened as-is.
- 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.
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.