fscryptography 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ """Filesystem-oriented cryptographic operations."""
2
+
3
+ from .crypto import Manager, generate
4
+ from ._journal import RecoveryRecord
5
+
6
+ from .errors import (
7
+ AtomicOperationError,
8
+ AuthenticationError,
9
+ BackendError,
10
+ ClosedInstanceError,
11
+ DestinationConflictError,
12
+ FormatError,
13
+ FsCryptographyError,
14
+ InvalidPathError,
15
+ KeyDerivationError,
16
+ NameConstraintError,
17
+ RecoveryError,
18
+ UnsupportedFormatVersionError,
19
+ UnsupportedResourceError,
20
+ )
21
+
22
+ __all__ = [
23
+ "Manager",
24
+ "generate",
25
+ "RecoveryRecord",
26
+ "AtomicOperationError",
27
+ "AuthenticationError",
28
+ "BackendError",
29
+ "ClosedInstanceError",
30
+ "DestinationConflictError",
31
+ "FormatError",
32
+ "FsCryptographyError",
33
+ "InvalidPathError",
34
+ "KeyDerivationError",
35
+ "NameConstraintError",
36
+ "RecoveryError",
37
+ "UnsupportedFormatVersionError",
38
+ "UnsupportedResourceError",
39
+ ]
40
+
41
+ __version__ = "0.1.0"
@@ -0,0 +1,99 @@
1
+ """Descriptor-relative directory operations for GNU/Linux filesystem safety."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import ctypes
6
+ import errno
7
+ import os
8
+ import stat
9
+ import sys
10
+ from pathlib import Path
11
+
12
+ from .errors import AtomicOperationError, DestinationConflictError, InvalidPathError, UnsupportedResourceError
13
+
14
+ _RENAME_NOREPLACE = 1
15
+
16
+
17
+ def _renameat2(source_fd: int, source_name: str, destination_fd: int, destination_name: str) -> None:
18
+ if not sys.platform.startswith("linux"):
19
+ raise AtomicOperationError("descriptor-relative no-replace rename is only available on GNU/Linux")
20
+ try:
21
+ libc = ctypes.CDLL(None, use_errno=True)
22
+ renameat2 = libc.renameat2
23
+ except (AttributeError, OSError) as exc:
24
+ raise AtomicOperationError("renameat2(RENAME_NOREPLACE) is unavailable") from exc
25
+ renameat2.argtypes = (ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_uint)
26
+ renameat2.restype = ctypes.c_int
27
+ result = renameat2(
28
+ source_fd, os.fsencode(source_name),
29
+ destination_fd, os.fsencode(destination_name),
30
+ _RENAME_NOREPLACE,
31
+ )
32
+ if result == 0:
33
+ return
34
+ error = ctypes.get_errno()
35
+ if error == errno.EEXIST:
36
+ raise DestinationConflictError("destination already exists")
37
+ if error in {errno.ENOSYS, errno.EINVAL, errno.EOPNOTSUPP}:
38
+ raise AtomicOperationError(
39
+ f"Linux no-replace rename is unsupported: {os.strerror(error)}"
40
+ ) from OSError(error, os.strerror(error))
41
+ raise OSError(error, os.strerror(error), destination_name)
42
+
43
+
44
+ class DirectoryAnchor:
45
+ """Hold a verified directory descriptor and operate on direct children only."""
46
+
47
+ def __init__(self, path: str | os.PathLike[str]) -> None:
48
+ if not sys.platform.startswith("linux"):
49
+ raise UnsupportedResourceError("directory descriptor anchoring is currently GNU/Linux-only")
50
+ self.path = Path(os.path.abspath(os.fspath(path)))
51
+ flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
52
+ try:
53
+ self.fd = os.open(self.path, flags)
54
+ except OSError as exc:
55
+ raise InvalidPathError("parent directory could not be opened without following a symlink") from exc
56
+ self.closed = False
57
+ try:
58
+ self.info = os.fstat(self.fd)
59
+ if not stat.S_ISDIR(self.info.st_mode):
60
+ raise InvalidPathError("parent is not a directory")
61
+ except BaseException:
62
+ os.close(self.fd)
63
+ self.closed = True
64
+ raise
65
+
66
+ def stat(self, name: str) -> os.stat_result:
67
+ return os.stat(name, dir_fd=self.fd, follow_symlinks=False)
68
+
69
+ def open(self, name: str, flags: int, mode: int = 0o600) -> int:
70
+ return os.open(name, flags | getattr(os, "O_NOFOLLOW", 0), mode, dir_fd=self.fd)
71
+
72
+ def mkdir(self, name: str, mode: int = 0o700) -> None:
73
+ os.mkdir(name, mode, dir_fd=self.fd)
74
+
75
+ def unlink(self, name: str) -> None:
76
+ os.unlink(name, dir_fd=self.fd)
77
+
78
+ def rename_noreplace(self, source_name: str, destination_fd: int, destination_name: str) -> None:
79
+ _renameat2(self.fd, source_name, destination_fd, destination_name)
80
+
81
+ def sync(self) -> None:
82
+ os.fsync(self.fd)
83
+
84
+ def verify_path_still_names_anchor(self) -> None:
85
+ """Reject a replaced parent pathname while retaining the original anchor."""
86
+ current = os.stat(self.path, follow_symlinks=False)
87
+ if stat.S_ISLNK(current.st_mode) or (current.st_dev, current.st_ino) != (self.info.st_dev, self.info.st_ino):
88
+ raise InvalidPathError("parent directory was replaced during the operation")
89
+
90
+ def close(self) -> None:
91
+ if not self.closed:
92
+ os.close(self.fd)
93
+ self.closed = True
94
+
95
+ def __enter__(self) -> "DirectoryAnchor":
96
+ return self
97
+
98
+ def __exit__(self, exc_type, exc, tb) -> None:
99
+ self.close()