pathrs 0.2.5__tar.gz → 0.2.7__tar.gz
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.
- {pathrs-0.2.5 → pathrs-0.2.7}/PKG-INFO +5 -5
- {pathrs-0.2.5 → pathrs-0.2.7}/README.md +3 -3
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs/__init__.py +1 -2
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs/_internal.py +31 -27
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs/_libpathrs_cffi/lib.pyi +24 -33
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs/_pathrs.py +12 -15
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs/pathrs_build.py +7 -5
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs/procfs.py +15 -10
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs.egg-info/PKG-INFO +5 -5
- {pathrs-0.2.5 → pathrs-0.2.7}/pyproject.toml +2 -2
- {pathrs-0.2.5 → pathrs-0.2.7}/setup.py +3 -3
- {pathrs-0.2.5 → pathrs-0.2.7}/COPYING +0 -0
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs/_libpathrs_cffi/__init__.pyi +0 -0
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs/py.typed +0 -0
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs.egg-info/SOURCES.txt +0 -0
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs.egg-info/dependency_links.txt +0 -0
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs.egg-info/requires.txt +0 -0
- {pathrs-0.2.5 → pathrs-0.2.7}/pathrs.egg-info/top_level.txt +0 -0
- {pathrs-0.2.5 → pathrs-0.2.7}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pathrs
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.7
|
|
4
4
|
Summary: Python bindings for libpathrs, a safe path resolution library for Linux.
|
|
5
5
|
Author-email: Aleksa Sarai <cyphar@cyphar.com>
|
|
6
6
|
Maintainer-email: Aleksa Sarai <cyphar@cyphar.com>
|
|
@@ -19,7 +19,7 @@ Classifier: Operating System :: POSIX :: Linux
|
|
|
19
19
|
Classifier: Programming Language :: Python
|
|
20
20
|
Classifier: Programming Language :: Python :: 3
|
|
21
21
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
-
Requires-Python: >=3.
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
23
|
Description-Content-Type: text/markdown
|
|
24
24
|
License-File: COPYING
|
|
25
25
|
Requires-Dist: cffi>=1.10.0
|
|
@@ -67,11 +67,11 @@ RENAME_EXCHANGE = 0x2
|
|
|
67
67
|
|
|
68
68
|
with pathrs.Root("/path/to/rootfs") as root:
|
|
69
69
|
# symlink
|
|
70
|
-
root.symlink("foo", "bar")
|
|
70
|
+
root.symlink("foo", "bar") # foo -> bar
|
|
71
71
|
# link
|
|
72
|
-
root.hardlink("a", "b")
|
|
72
|
+
root.hardlink("a", "b") # a -> b
|
|
73
73
|
# rename(at2)
|
|
74
|
-
root.rename("foo", "b", flags=RENAME_EXCHANGE)
|
|
74
|
+
root.rename("foo", "b", flags=RENAME_EXCHANGE) # foo <-> b
|
|
75
75
|
# open(O_CREAT)
|
|
76
76
|
with root.creat("newfile", "w+") as f:
|
|
77
77
|
f.write("Some contents.")
|
|
@@ -38,11 +38,11 @@ RENAME_EXCHANGE = 0x2
|
|
|
38
38
|
|
|
39
39
|
with pathrs.Root("/path/to/rootfs") as root:
|
|
40
40
|
# symlink
|
|
41
|
-
root.symlink("foo", "bar")
|
|
41
|
+
root.symlink("foo", "bar") # foo -> bar
|
|
42
42
|
# link
|
|
43
|
-
root.hardlink("a", "b")
|
|
43
|
+
root.hardlink("a", "b") # a -> b
|
|
44
44
|
# rename(at2)
|
|
45
|
-
root.rename("foo", "b", flags=RENAME_EXCHANGE)
|
|
45
|
+
root.rename("foo", "b", flags=RENAME_EXCHANGE) # foo <-> b
|
|
46
46
|
# open(O_CREAT)
|
|
47
47
|
with root.creat("newfile", "w+") as f:
|
|
48
48
|
f.write("Some contents.")
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/python3
|
|
2
1
|
# SPDX-License-Identifier: MPL-2.0
|
|
3
2
|
#
|
|
4
3
|
# libpathrs: safe path resolution on Linux
|
|
@@ -13,7 +12,7 @@ import importlib
|
|
|
13
12
|
import importlib.metadata
|
|
14
13
|
|
|
15
14
|
from . import _pathrs
|
|
16
|
-
from ._pathrs import * #
|
|
15
|
+
from ._pathrs import * # We just re-export everything.
|
|
17
16
|
|
|
18
17
|
# In order get pydoc to include the documentation for the re-exported code from
|
|
19
18
|
# _pathrs, we need to include all of the members in __all__. Rather than
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/python3
|
|
2
1
|
# SPDX-License-Identifier: MPL-2.0
|
|
3
2
|
#
|
|
4
3
|
# libpathrs: safe path resolution on Linux
|
|
@@ -9,19 +8,18 @@
|
|
|
9
8
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
10
9
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
11
10
|
|
|
12
|
-
import io
|
|
13
|
-
import os
|
|
14
|
-
import sys
|
|
15
11
|
import copy
|
|
16
12
|
import errno
|
|
17
13
|
import fcntl
|
|
18
|
-
|
|
14
|
+
import io
|
|
15
|
+
import os
|
|
16
|
+
import sys
|
|
19
17
|
import typing
|
|
20
18
|
from types import TracebackType
|
|
21
|
-
from typing import
|
|
19
|
+
from typing import IO, Any, ClassVar, TextIO, TypeAlias, TypeVar
|
|
22
20
|
|
|
23
21
|
# TODO: Remove this once we only support Python >= 3.11.
|
|
24
|
-
from typing_extensions import Self
|
|
22
|
+
from typing_extensions import Self
|
|
25
23
|
|
|
26
24
|
from ._libpathrs_cffi import lib as libpathrs_so
|
|
27
25
|
|
|
@@ -42,6 +40,12 @@ else:
|
|
|
42
40
|
|
|
43
41
|
|
|
44
42
|
def _cstr(pystr: str) -> CString:
|
|
43
|
+
# C cannot express embedded nul bytes which means that paths and other
|
|
44
|
+
# string arguments get silently truncated (as ffi.new("char[]") happily
|
|
45
|
+
# copies them over), which results in unexpected behaviour for Python
|
|
46
|
+
# users. Python raises a ValueError in this case so just emulate that.
|
|
47
|
+
if "\0" in pystr:
|
|
48
|
+
raise ValueError(f"embedded null character in string {pystr!r}")
|
|
45
49
|
return ffi.new("char[]", pystr.encode("utf8"))
|
|
46
50
|
|
|
47
51
|
|
|
@@ -52,7 +56,7 @@ def _pystr(cstr: CString) -> str:
|
|
|
52
56
|
|
|
53
57
|
|
|
54
58
|
def _cbuffer(size: int) -> CBuffer:
|
|
55
|
-
return ffi.new("char[
|
|
59
|
+
return ffi.new(f"char[{size}]")
|
|
56
60
|
|
|
57
61
|
|
|
58
62
|
def _is_pathrs_err(ret: int) -> bool:
|
|
@@ -68,10 +72,10 @@ class PathrsError(Exception):
|
|
|
68
72
|
"""
|
|
69
73
|
|
|
70
74
|
message: str
|
|
71
|
-
errno:
|
|
72
|
-
strerror:
|
|
75
|
+
errno: int | None
|
|
76
|
+
strerror: str | None
|
|
73
77
|
|
|
74
|
-
def __init__(self, message: str, /, *, errno:
|
|
78
|
+
def __init__(self, message: str, /, *, errno: int | None = None):
|
|
75
79
|
# Construct Exception.
|
|
76
80
|
super().__init__(message)
|
|
77
81
|
|
|
@@ -88,7 +92,7 @@ class PathrsError(Exception):
|
|
|
88
92
|
self.strerror = str(errno)
|
|
89
93
|
|
|
90
94
|
@classmethod
|
|
91
|
-
def _fetch(cls, err_id: int, /) ->
|
|
95
|
+
def _fetch(cls, err_id: int, /) -> Self | None:
|
|
92
96
|
if err_id >= 0:
|
|
93
97
|
return None
|
|
94
98
|
|
|
@@ -109,10 +113,10 @@ class PathrsError(Exception):
|
|
|
109
113
|
if self.errno is None:
|
|
110
114
|
return self.message
|
|
111
115
|
else:
|
|
112
|
-
return "
|
|
116
|
+
return f"{self.message} ({self.strerror})"
|
|
113
117
|
|
|
114
118
|
def __repr__(self) -> str:
|
|
115
|
-
return "Error(
|
|
119
|
+
return f"Error({self.message!r}, errno={self.errno!r})"
|
|
116
120
|
|
|
117
121
|
def pprint(self, out: TextIO = sys.stdout) -> None:
|
|
118
122
|
"Pretty-print the error to the given @out file."
|
|
@@ -120,8 +124,8 @@ class PathrsError(Exception):
|
|
|
120
124
|
if self.errno is None:
|
|
121
125
|
print("pathrs error:", file=out)
|
|
122
126
|
else:
|
|
123
|
-
print("pathrs error [
|
|
124
|
-
print("
|
|
127
|
+
print(f"pathrs error [{self.strerror}]:", file=out)
|
|
128
|
+
print(f" {self.message}", file=out)
|
|
125
129
|
|
|
126
130
|
|
|
127
131
|
INTERNAL_ERROR = PathrsError("tried to fetch libpathrs error but no error found")
|
|
@@ -131,7 +135,7 @@ class FilenoFile(typing.Protocol):
|
|
|
131
135
|
def fileno(self) -> int: ...
|
|
132
136
|
|
|
133
137
|
|
|
134
|
-
FileLike =
|
|
138
|
+
FileLike = FilenoFile | int
|
|
135
139
|
|
|
136
140
|
|
|
137
141
|
def _fileno(file: FileLike) -> int:
|
|
@@ -151,7 +155,7 @@ def _clonefile(file: FileLike) -> int:
|
|
|
151
155
|
Fd = TypeVar("Fd", bound="WrappedFd")
|
|
152
156
|
|
|
153
157
|
|
|
154
|
-
class WrappedFd
|
|
158
|
+
class WrappedFd:
|
|
155
159
|
"""
|
|
156
160
|
Represents a file descriptor that allows for manual lifetime management,
|
|
157
161
|
unlike os.fdopen() which are tracked by the GC with no way of "leaking" the
|
|
@@ -160,7 +164,7 @@ class WrappedFd(object):
|
|
|
160
164
|
pathrs will return WrappedFds for most operations that return an fd.
|
|
161
165
|
"""
|
|
162
166
|
|
|
163
|
-
_fd:
|
|
167
|
+
_fd: int | None = None
|
|
164
168
|
|
|
165
169
|
def __init__(self, file: FileLike, /):
|
|
166
170
|
"""
|
|
@@ -233,12 +237,12 @@ class WrappedFd(object):
|
|
|
233
237
|
raise
|
|
234
238
|
|
|
235
239
|
@classmethod
|
|
236
|
-
def from_raw_fd(cls
|
|
240
|
+
def from_raw_fd(cls, fd: int, /) -> Self:
|
|
237
241
|
"Shorthand for WrappedFd(fd)."
|
|
238
242
|
return cls(fd)
|
|
239
243
|
|
|
240
244
|
@classmethod
|
|
241
|
-
def from_file(cls
|
|
245
|
+
def from_file(cls, file: FileLike, /) -> Self:
|
|
242
246
|
"Shorthand for WrappedFd(file)."
|
|
243
247
|
return cls(file)
|
|
244
248
|
|
|
@@ -288,7 +292,7 @@ class WrappedFd(object):
|
|
|
288
292
|
# A "shallow copy" of a file is the same as a deep copy.
|
|
289
293
|
return copy.deepcopy(self)
|
|
290
294
|
|
|
291
|
-
def __deepcopy__(self, memo:
|
|
295
|
+
def __deepcopy__(self, memo: dict[int, Any]) -> Self:
|
|
292
296
|
"Identical to WrappedFd.clone()"
|
|
293
297
|
return self.clone()
|
|
294
298
|
|
|
@@ -301,9 +305,9 @@ class WrappedFd(object):
|
|
|
301
305
|
|
|
302
306
|
def __exit__(
|
|
303
307
|
self,
|
|
304
|
-
exc_type:
|
|
305
|
-
exc_value:
|
|
306
|
-
exc_traceback:
|
|
308
|
+
exc_type: type[BaseException] | None,
|
|
309
|
+
exc_value: BaseException | None,
|
|
310
|
+
exc_traceback: TracebackType | None,
|
|
307
311
|
) -> None:
|
|
308
312
|
self.close()
|
|
309
313
|
|
|
@@ -349,9 +353,9 @@ def _convert_mode(mode: str) -> int:
|
|
|
349
353
|
class SingletonClass(type):
|
|
350
354
|
"""Metaclass used to create singleton classes."""
|
|
351
355
|
|
|
352
|
-
_instances: dict[type,
|
|
356
|
+
_instances: ClassVar[dict[type, type[Any]]] = {}
|
|
353
357
|
|
|
354
358
|
def __call__(cls, *args, **kwargs): # type: ignore[no-untyped-def] # TODO: Not clear what annotations to use, and mypy appears to be confused by metaclasses.
|
|
355
359
|
if cls not in cls._instances:
|
|
356
|
-
cls._instances[cls] = super(
|
|
360
|
+
cls._instances[cls] = super().__call__(*args, **kwargs)
|
|
357
361
|
return cls._instances[cls]
|
|
@@ -8,10 +8,7 @@
|
|
|
8
8
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
9
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
10
10
|
|
|
11
|
-
from typing import
|
|
12
|
-
|
|
13
|
-
# TODO: Remove this once we only support Python >= 3.10.
|
|
14
|
-
from typing_extensions import TypeAlias, Literal
|
|
11
|
+
from typing import Literal, TypeAlias, type_check_only
|
|
15
12
|
|
|
16
13
|
from .._pathrs import CBuffer, CString
|
|
17
14
|
from ..procfs import ProcfsBase
|
|
@@ -29,7 +26,7 @@ __PATHRS_MAX_ERR_VALUE: ErrorId
|
|
|
29
26
|
|
|
30
27
|
# TODO: We actually return Union[CError, cffi.FFI.NULL] but we can't express
|
|
31
28
|
# this using the typing stubs for CFFI...
|
|
32
|
-
def pathrs_errorinfo(err_id:
|
|
29
|
+
def pathrs_errorinfo(err_id: ErrorId | int) -> CError: ...
|
|
33
30
|
def pathrs_errorinfo_free(err: CError) -> None: ...
|
|
34
31
|
|
|
35
32
|
# pathrs_version_info_t *
|
|
@@ -37,7 +34,7 @@ def pathrs_errorinfo_free(err: CError) -> None: ...
|
|
|
37
34
|
class VersionInfo:
|
|
38
35
|
version_string: CString
|
|
39
36
|
|
|
40
|
-
def pathrs_version(info: VersionInfo, size: int) ->
|
|
37
|
+
def pathrs_version(info: VersionInfo, size: int) -> ErrorId | int: ...
|
|
41
38
|
|
|
42
39
|
# uint64_t
|
|
43
40
|
ProcfsOpenFlags: TypeAlias = int
|
|
@@ -58,70 +55,64 @@ __PATHRS_PROC_TYPE_PID: ProcfsBase
|
|
|
58
55
|
PATHRS_PROC_DEFAULT_ROOTFD: RawFd
|
|
59
56
|
|
|
60
57
|
# procfs API
|
|
61
|
-
def pathrs_procfs_open(how: ProcfsOpenHow, size: int) ->
|
|
58
|
+
def pathrs_procfs_open(how: ProcfsOpenHow, size: int) -> RawFd | ErrorId: ...
|
|
62
59
|
def pathrs_proc_open(
|
|
63
60
|
base: ProcfsBase, path: CString, flags: int
|
|
64
|
-
) ->
|
|
61
|
+
) -> RawFd | ErrorId: ...
|
|
65
62
|
def pathrs_proc_openat(
|
|
66
63
|
proc_root_fd: RawFd, base: ProcfsBase, path: CString, flags: int
|
|
67
|
-
) ->
|
|
64
|
+
) -> RawFd | ErrorId: ...
|
|
68
65
|
def pathrs_proc_readlink(
|
|
69
66
|
base: ProcfsBase, path: CString, linkbuf: CBuffer, linkbuf_size: int
|
|
70
|
-
) ->
|
|
67
|
+
) -> int | ErrorId: ...
|
|
71
68
|
def pathrs_proc_readlinkat(
|
|
72
69
|
proc_root_fd: RawFd,
|
|
73
70
|
base: ProcfsBase,
|
|
74
71
|
path: CString,
|
|
75
72
|
linkbuf: CBuffer,
|
|
76
73
|
linkbuf_size: int,
|
|
77
|
-
) ->
|
|
74
|
+
) -> int | ErrorId: ...
|
|
78
75
|
|
|
79
76
|
# core API
|
|
80
|
-
def pathrs_open_root(path: CString) ->
|
|
81
|
-
def pathrs_reopen(fd: RawFd, flags: int) ->
|
|
82
|
-
def pathrs_inroot_resolve(rootfd: RawFd, path: CString) ->
|
|
83
|
-
def pathrs_inroot_resolve_nofollow(
|
|
84
|
-
|
|
85
|
-
) -> Union[RawFd, ErrorId]: ...
|
|
86
|
-
def pathrs_inroot_open(
|
|
87
|
-
rootfd: RawFd, path: CString, flags: int
|
|
88
|
-
) -> Union[RawFd, ErrorId]: ...
|
|
77
|
+
def pathrs_open_root(path: CString) -> RawFd | ErrorId: ...
|
|
78
|
+
def pathrs_reopen(fd: RawFd, flags: int) -> RawFd | ErrorId: ...
|
|
79
|
+
def pathrs_inroot_resolve(rootfd: RawFd, path: CString) -> RawFd | ErrorId: ...
|
|
80
|
+
def pathrs_inroot_resolve_nofollow(rootfd: RawFd, path: CString) -> RawFd | ErrorId: ...
|
|
81
|
+
def pathrs_inroot_open(rootfd: RawFd, path: CString, flags: int) -> RawFd | ErrorId: ...
|
|
89
82
|
def pathrs_inroot_creat(
|
|
90
83
|
rootfd: RawFd, path: CString, flags: int, filemode: int
|
|
91
|
-
) ->
|
|
84
|
+
) -> RawFd | ErrorId: ...
|
|
92
85
|
def pathrs_inroot_rename(
|
|
93
86
|
old_rootfd: RawFd,
|
|
94
87
|
old_path: CString,
|
|
95
88
|
new_rootfd: RawFd,
|
|
96
89
|
new_path: CString,
|
|
97
90
|
flags: int,
|
|
98
|
-
) ->
|
|
99
|
-
def pathrs_inroot_rmdir(rootfd: RawFd, path: CString) ->
|
|
100
|
-
def pathrs_inroot_unlink(
|
|
101
|
-
|
|
102
|
-
) -> Union[Literal[0], ErrorId]: ...
|
|
103
|
-
def pathrs_inroot_remove_all(rootfd: RawFd, path: CString) -> Union[RawFd, ErrorId]: ...
|
|
91
|
+
) -> Literal[0] | ErrorId: ...
|
|
92
|
+
def pathrs_inroot_rmdir(rootfd: RawFd, path: CString) -> Literal[0] | ErrorId: ...
|
|
93
|
+
def pathrs_inroot_unlink(rootfd: RawFd, path: CString) -> Literal[0] | ErrorId: ...
|
|
94
|
+
def pathrs_inroot_remove_all(rootfd: RawFd, path: CString) -> RawFd | ErrorId: ...
|
|
104
95
|
def pathrs_inroot_mkdir(
|
|
105
96
|
rootfd: RawFd, path: CString, mode: int
|
|
106
|
-
) ->
|
|
97
|
+
) -> Literal[0] | ErrorId: ...
|
|
107
98
|
def pathrs_inroot_mkdir_all(
|
|
108
99
|
rootfd: RawFd, path: CString, mode: int
|
|
109
|
-
) ->
|
|
100
|
+
) -> Literal[0] | ErrorId: ...
|
|
110
101
|
def pathrs_inroot_mknod(
|
|
111
102
|
rootfd: RawFd, path: CString, mode: int, dev: int
|
|
112
|
-
) ->
|
|
103
|
+
) -> Literal[0] | ErrorId: ...
|
|
113
104
|
def pathrs_inroot_hardlink(
|
|
114
105
|
old_rootfd: RawFd,
|
|
115
106
|
old_path: CString,
|
|
116
107
|
new_rootfd: RawFd,
|
|
117
108
|
new_path: CString,
|
|
118
109
|
flags: int,
|
|
119
|
-
) ->
|
|
110
|
+
) -> Literal[0] | ErrorId: ...
|
|
120
111
|
def pathrs_inroot_symlink(
|
|
121
112
|
target: CString,
|
|
122
113
|
rootfd: RawFd,
|
|
123
114
|
linkpath: CString,
|
|
124
|
-
) ->
|
|
115
|
+
) -> Literal[0] | ErrorId: ...
|
|
125
116
|
def pathrs_inroot_readlink(
|
|
126
117
|
rootfd: RawFd, path: CString, linkbuf: CBuffer, linkbuf_size: int
|
|
127
|
-
) ->
|
|
118
|
+
) -> int | ErrorId: ...
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/python3
|
|
2
1
|
# SPDX-License-Identifier: MPL-2.0
|
|
3
2
|
#
|
|
4
3
|
# libpathrs: safe path resolution on Linux
|
|
@@ -10,29 +9,27 @@
|
|
|
10
9
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
11
10
|
|
|
12
11
|
import os
|
|
13
|
-
|
|
14
12
|
import typing
|
|
15
|
-
from typing import Any, IO, Union, cast
|
|
16
13
|
import warnings
|
|
17
14
|
|
|
18
15
|
# TODO: Remove this once we only support Python >= 3.11.
|
|
19
|
-
from
|
|
16
|
+
from typing import IO, Any, TypeAlias, cast
|
|
20
17
|
|
|
21
18
|
from ._internal import (
|
|
22
|
-
|
|
23
|
-
SingletonClass,
|
|
19
|
+
INTERNAL_ERROR,
|
|
24
20
|
# File type helpers.
|
|
25
21
|
FileLike,
|
|
26
|
-
WrappedFd,
|
|
27
|
-
_convert_mode,
|
|
28
22
|
# Error API.
|
|
29
23
|
PathrsError,
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
# Generic helpers.
|
|
25
|
+
SingletonClass,
|
|
26
|
+
WrappedFd,
|
|
27
|
+
_cbuffer,
|
|
28
|
+
_convert_mode,
|
|
32
29
|
# CFFI helpers.
|
|
33
30
|
_cstr,
|
|
31
|
+
_is_pathrs_err,
|
|
34
32
|
_pystr,
|
|
35
|
-
_cbuffer,
|
|
36
33
|
)
|
|
37
34
|
from ._libpathrs_cffi import lib as libpathrs_so
|
|
38
35
|
|
|
@@ -52,12 +49,12 @@ else:
|
|
|
52
49
|
CBuffer: TypeAlias = ffi.CData
|
|
53
50
|
|
|
54
51
|
__all__ = [
|
|
55
|
-
# Core api.
|
|
56
|
-
"Root",
|
|
57
52
|
"Handle",
|
|
58
|
-
"library_version",
|
|
59
53
|
# Error api (re-export).
|
|
60
54
|
"PathrsError",
|
|
55
|
+
# Core api.
|
|
56
|
+
"Root",
|
|
57
|
+
"library_version",
|
|
61
58
|
]
|
|
62
59
|
|
|
63
60
|
|
|
@@ -133,7 +130,7 @@ class Root(WrappedFd):
|
|
|
133
130
|
relative to.
|
|
134
131
|
"""
|
|
135
132
|
|
|
136
|
-
def __init__(self, file_or_path:
|
|
133
|
+
def __init__(self, file_or_path: FileLike | str, /):
|
|
137
134
|
"""
|
|
138
135
|
Create a handle from a file-like object or a path to a directory.
|
|
139
136
|
|
|
@@ -13,12 +13,14 @@
|
|
|
13
13
|
# build of libpathrs, and can be redistributed alongside the pathrs.py wrapping
|
|
14
14
|
# library). It's much better than the ABI-mode of CFFI.
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
# TODO: Remove this once we only support Python >= 3.10.
|
|
17
|
+
from __future__ import annotations # PEP 604
|
|
18
|
+
|
|
17
19
|
import os
|
|
20
|
+
import re
|
|
18
21
|
import sys
|
|
19
|
-
|
|
20
|
-
from typing import Any, Optional
|
|
21
22
|
from collections.abc import Iterable
|
|
23
|
+
from typing import Any
|
|
22
24
|
|
|
23
25
|
import cffi
|
|
24
26
|
|
|
@@ -97,7 +99,7 @@ def find_rootdir() -> str:
|
|
|
97
99
|
return root_dir
|
|
98
100
|
|
|
99
101
|
|
|
100
|
-
def srcdir_ffibuilder(root_dir:
|
|
102
|
+
def srcdir_ffibuilder(root_dir: str | None = None) -> cffi.FFI:
|
|
101
103
|
"""
|
|
102
104
|
Build the CFFI bindings using the provided root_dir as the root of a
|
|
103
105
|
pathrs source tree which has compiled cdylibs ready in target/*.
|
|
@@ -108,7 +110,7 @@ def srcdir_ffibuilder(root_dir: Optional[str] = None) -> cffi.FFI:
|
|
|
108
110
|
|
|
109
111
|
# Figure out which libs are usable.
|
|
110
112
|
library_dirs: Iterable[str] = (
|
|
111
|
-
os.path.join(root_dir, "target
|
|
113
|
+
os.path.join(root_dir, f"target/{mode}/libpathrs.so")
|
|
112
114
|
for mode in ("debug", "release")
|
|
113
115
|
)
|
|
114
116
|
library_dirs = (so_path for so_path in library_dirs if os.path.exists(so_path))
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/python3
|
|
2
1
|
# SPDX-License-Identifier: MPL-2.0
|
|
3
2
|
#
|
|
4
3
|
# libpathrs: safe path resolution on Linux
|
|
@@ -10,22 +9,22 @@
|
|
|
10
9
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
11
10
|
|
|
12
11
|
import typing
|
|
13
|
-
from typing import Any,
|
|
12
|
+
from typing import IO, Any, TypeAlias, cast
|
|
14
13
|
|
|
15
14
|
# TODO: Remove this once we only support Python >= 3.11.
|
|
16
|
-
from typing_extensions import Self
|
|
15
|
+
from typing_extensions import Self
|
|
17
16
|
|
|
18
17
|
from ._internal import (
|
|
18
|
+
INTERNAL_ERROR,
|
|
19
|
+
# Error API.
|
|
20
|
+
PathrsError,
|
|
19
21
|
# File type helpers.
|
|
20
22
|
WrappedFd,
|
|
23
|
+
_cbuffer,
|
|
21
24
|
_convert_mode,
|
|
22
|
-
# Error API.
|
|
23
|
-
PathrsError,
|
|
24
|
-
_is_pathrs_err,
|
|
25
|
-
INTERNAL_ERROR,
|
|
26
25
|
# CFFI helpers.
|
|
27
26
|
_cstr,
|
|
28
|
-
|
|
27
|
+
_is_pathrs_err,
|
|
29
28
|
)
|
|
30
29
|
from ._libpathrs_cffi import lib as libpathrs_so
|
|
31
30
|
|
|
@@ -45,10 +44,10 @@ else:
|
|
|
45
44
|
CBuffer: TypeAlias = ffi.CData
|
|
46
45
|
|
|
47
46
|
__all__ = [
|
|
47
|
+
"PROC_PID",
|
|
48
48
|
"PROC_ROOT",
|
|
49
49
|
"PROC_SELF",
|
|
50
50
|
"PROC_THREAD_SELF",
|
|
51
|
-
"PROC_PID",
|
|
52
51
|
"ProcfsHandle",
|
|
53
52
|
# Shorthand for ProcfsHandle.cached().<foo>.
|
|
54
53
|
"open",
|
|
@@ -94,7 +93,13 @@ def PROC_PID(pid: int) -> ProcfsBase:
|
|
|
94
93
|
|
|
95
94
|
|
|
96
95
|
class ProcfsHandle(WrappedFd):
|
|
97
|
-
"""
|
|
96
|
+
"""
|
|
97
|
+
A handle to a procfs root that can be operated on safely.
|
|
98
|
+
|
|
99
|
+
While you can create your own custom handles with ProcfsHandle.new(), most
|
|
100
|
+
users should use the module-level procfs.* helper functions, which are all
|
|
101
|
+
shorthand for ProcfsHandle.cached().*.
|
|
102
|
+
"""
|
|
98
103
|
|
|
99
104
|
_PROCFS_OPEN_HOW_TYPE = "pathrs_procfs_open_how *"
|
|
100
105
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pathrs
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.7
|
|
4
4
|
Summary: Python bindings for libpathrs, a safe path resolution library for Linux.
|
|
5
5
|
Author-email: Aleksa Sarai <cyphar@cyphar.com>
|
|
6
6
|
Maintainer-email: Aleksa Sarai <cyphar@cyphar.com>
|
|
@@ -19,7 +19,7 @@ Classifier: Operating System :: POSIX :: Linux
|
|
|
19
19
|
Classifier: Programming Language :: Python
|
|
20
20
|
Classifier: Programming Language :: Python :: 3
|
|
21
21
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
-
Requires-Python: >=3.
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
23
|
Description-Content-Type: text/markdown
|
|
24
24
|
License-File: COPYING
|
|
25
25
|
Requires-Dist: cffi>=1.10.0
|
|
@@ -67,11 +67,11 @@ RENAME_EXCHANGE = 0x2
|
|
|
67
67
|
|
|
68
68
|
with pathrs.Root("/path/to/rootfs") as root:
|
|
69
69
|
# symlink
|
|
70
|
-
root.symlink("foo", "bar")
|
|
70
|
+
root.symlink("foo", "bar") # foo -> bar
|
|
71
71
|
# link
|
|
72
|
-
root.hardlink("a", "b")
|
|
72
|
+
root.hardlink("a", "b") # a -> b
|
|
73
73
|
# rename(at2)
|
|
74
|
-
root.rename("foo", "b", flags=RENAME_EXCHANGE)
|
|
74
|
+
root.rename("foo", "b", flags=RENAME_EXCHANGE) # foo <-> b
|
|
75
75
|
# open(O_CREAT)
|
|
76
76
|
with root.creat("newfile", "w+") as f:
|
|
77
77
|
f.write("Some contents.")
|
|
@@ -21,7 +21,7 @@ build-backend = "setuptools.build_meta"
|
|
|
21
21
|
[project]
|
|
22
22
|
name = "pathrs"
|
|
23
23
|
# TODO: Figure out a way to keep this version up-to-date with Cargo.toml.
|
|
24
|
-
version = "0.2.
|
|
24
|
+
version = "0.2.7"
|
|
25
25
|
description = "Python bindings for libpathrs, a safe path resolution library for Linux."
|
|
26
26
|
readme = "README.md"
|
|
27
27
|
keywords = ["libpathrs", "pathrs"]
|
|
@@ -44,7 +44,7 @@ classifiers = [
|
|
|
44
44
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
45
45
|
]
|
|
46
46
|
|
|
47
|
-
requires-python = ">= 3.
|
|
47
|
+
requires-python = ">= 3.10"
|
|
48
48
|
dependencies = [
|
|
49
49
|
"cffi>=1.10.0",
|
|
50
50
|
"typing_extensions>=4.0.0", # TODO: Remove this once we only support Python >= 3.11.
|
|
@@ -9,13 +9,13 @@
|
|
|
9
9
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
10
10
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
11
11
|
|
|
12
|
-
import
|
|
12
|
+
from typing import Any
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
import setuptools
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
# This is only needed for backwards compatibility with older versions.
|
|
18
|
-
def parse_pyproject() ->
|
|
18
|
+
def parse_pyproject() -> dict[str, Any]:
|
|
19
19
|
try:
|
|
20
20
|
import tomllib
|
|
21
21
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|