dissect.target.helpers.compat.path_313

A pathlib.Path compatible implementation for dissect.target.

This allows for the majority of the pathlib.Path API to “just work” on dissect.target filesystems.

Most of this consists of subclassed internal classes with dissect.target specific patches, but sometimes the change to a function is small, so the entire internal function is copied and only a small part changed. To ease updating this code, the order of functions, comments and code style is kept largely the same as the original pathlib.py.

Yes, we know, this is playing with fire and it can break on new CPython releases.

The implementation is split up in multiple files, one for each CPython version. You’re currently looking at the CPython 3.13 implementation.

Commit hash we’re in sync with: 094d95f

Notes

Module Contents

Classes

PureDissectPath

Base class for manipulating paths without I/O.

TargetPath

PurePath subclass that can make system calls.

class dissect.target.helpers.compat.path_313.PureDissectPath(fs: dissect.target.filesystem.Filesystem, *pathsegments)

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.

parser: _DissectParser
__reduce__() tuple
with_segments(*pathsegments) TargetPath
class dissect.target.helpers.compat.path_313.TargetPath(fs: dissect.target.filesystem.Filesystem, *pathsegments)

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() dissect.target.filesystem.FilesystemEntry
stat(*, follow_symlinks: bool = True) dissect.target.helpers.fsutil.stat_result

Return the result of the stat() system call on this path, like os.stat() does.

exists(*, follow_symlinks: bool = True) bool

Whether this path exists.

This method normally follows symlinks; to check whether a symlink exists, add the argument follow_symlinks=False.

is_mount

Check if this path is a POSIX mount point

is_junction() bool

Whether this path is a junction.

open(mode: str = 'rb', buffering: int = 0, encoding: str | None = None, errors: str | None = None, newline: str | None = None) IO

Open file and return a stream.

Supports a subset of features of the real pathlib.open/io.open.

Note: in contrast to regular Python, the mode is binary by default. Text mode has to be explicitly specified. Buffering is also disabled by default.

write_bytes(data: bytes) int

Open the file in bytes mode, write to it, and close the file.

write_text(data: str, encoding: str | None = None, errors: str | None = None, newline: str | None = None) int

Open the file in text mode, write to it, and close the file.

iterdir() Iterator[TargetPath]

Yield path objects of the directory contents.

The children are yielded in arbitrary order, and the special entries ‘.’ and ‘..’ are not included.

glob(pattern: str, *, case_sensitive: bool | None = None, recurse_symlinks: bool = False) Iterator[TargetPath]

Iterate over this subtree and yield all existing files (of any kind, including directories) matching the given relative pattern.

rglob(pattern: str, *, case_sensitive: bool | None = None, recurse_symlinks: str = False) Iterator[TargetPath]

Recursively yield all existing files (of any kind, including directories) matching the given relative pattern, anywhere in this subtree.

walk(top_down: bool = True, on_error: Callable[[Exception], None] = None, follow_symlinks: bool = False) Iterator[tuple[TargetPath, list[str], list[str]]]

Walk the directory tree from this directory, similar to os.walk().

absolute() TargetPath

Return an absolute version of this path No normalization or symlink resolution is performed.

Use resolve() to resolve symlinks and remove ‘..’ segments.

classmethod cwd() TargetPath

Return a new path pointing to the current working directory.

expanduser() TargetPath

Return a new path with expanded ~ and ~user constructs (as returned by os.path.expanduser)

classmethod home() TargetPath

Return a new path pointing to the user’s home directory (as returned by os.path.expanduser(‘~’)).

Return the path to which the symbolic link points.

resolve(strict: bool = False) TargetPath

Make the path absolute, resolving all symlinks on the way and also normalizing it.

Make this path a symlink pointing to the target path. Note the order of arguments (link, target) is the reverse of os.symlink.

Make this path a hard link pointing to the same file as target.

Note the order of arguments (self, target) is the reverse of os.link’s.

touch(mode: int = 438, exist_ok: bool = True) None

Create this file with the given access mode, if it doesn’t exist.

mkdir(mode: int = 511, parents: bool = False, exist_ok: bool = False) None

Create a new directory at this given path.

rename(target: str) TargetPath

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.

replace(target: str) TargetPath

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.

chmod(mode: int, *, follow_symlinks: bool = True) None

Change the permissions of the path, like os.chmod().

lchmod(mode: int) None

Like chmod(), except if the path points to a symlink, the symlink’s permissions are changed, rather than its target’s.

Remove this file or link. If the path is a directory, use rmdir() instead.

rmdir() None

Remove this directory. The directory must be empty.

owner() str

Return the login name of the file owner.

group() str

Return the group name of the file gid.