pathrs 0.2.4__tar.gz → 0.2.6__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.4 → pathrs-0.2.6}/PKG-INFO +5 -5
- {pathrs-0.2.4 → pathrs-0.2.6}/README.md +3 -3
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs/__init__.py +1 -2
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs/_internal.py +35 -26
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs/_libpathrs_cffi/lib.pyi +43 -35
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs/_pathrs.py +58 -22
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs/pathrs_build.py +7 -5
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs/procfs.py +15 -10
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs.egg-info/PKG-INFO +5 -5
- {pathrs-0.2.4 → pathrs-0.2.6}/pyproject.toml +2 -2
- {pathrs-0.2.4 → pathrs-0.2.6}/setup.py +4 -4
- {pathrs-0.2.4 → pathrs-0.2.6}/COPYING +0 -0
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs/_libpathrs_cffi/__init__.pyi +0 -0
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs/py.typed +0 -0
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs.egg-info/SOURCES.txt +0 -0
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs.egg-info/dependency_links.txt +0 -0
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs.egg-info/requires.txt +0 -0
- {pathrs-0.2.4 → pathrs-0.2.6}/pathrs.egg-info/top_level.txt +0 -0
- {pathrs-0.2.4 → pathrs-0.2.6}/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.6
|
|
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
|
|
|
@@ -52,7 +50,7 @@ def _pystr(cstr: CString) -> str:
|
|
|
52
50
|
|
|
53
51
|
|
|
54
52
|
def _cbuffer(size: int) -> CBuffer:
|
|
55
|
-
return ffi.new("char[
|
|
53
|
+
return ffi.new(f"char[{size}]")
|
|
56
54
|
|
|
57
55
|
|
|
58
56
|
def _is_pathrs_err(ret: int) -> bool:
|
|
@@ -68,10 +66,10 @@ class PathrsError(Exception):
|
|
|
68
66
|
"""
|
|
69
67
|
|
|
70
68
|
message: str
|
|
71
|
-
errno:
|
|
72
|
-
strerror:
|
|
69
|
+
errno: int | None
|
|
70
|
+
strerror: str | None
|
|
73
71
|
|
|
74
|
-
def __init__(self, message: str, /, *, errno:
|
|
72
|
+
def __init__(self, message: str, /, *, errno: int | None = None):
|
|
75
73
|
# Construct Exception.
|
|
76
74
|
super().__init__(message)
|
|
77
75
|
|
|
@@ -88,12 +86,12 @@ class PathrsError(Exception):
|
|
|
88
86
|
self.strerror = str(errno)
|
|
89
87
|
|
|
90
88
|
@classmethod
|
|
91
|
-
def _fetch(cls, err_id: int, /) ->
|
|
89
|
+
def _fetch(cls, err_id: int, /) -> Self | None:
|
|
92
90
|
if err_id >= 0:
|
|
93
91
|
return None
|
|
94
92
|
|
|
95
93
|
err = libpathrs_so.pathrs_errorinfo(err_id)
|
|
96
|
-
if err == ffi.NULL: # type: ignore # TODO: Make this check nicer...
|
|
94
|
+
if err == ffi.NULL: # type: ignore[comparison-overlap,unused-ignore] # TODO: Make this check nicer...
|
|
97
95
|
return None
|
|
98
96
|
|
|
99
97
|
description = _pystr(err.description)
|
|
@@ -109,10 +107,10 @@ class PathrsError(Exception):
|
|
|
109
107
|
if self.errno is None:
|
|
110
108
|
return self.message
|
|
111
109
|
else:
|
|
112
|
-
return "
|
|
110
|
+
return f"{self.message} ({self.strerror})"
|
|
113
111
|
|
|
114
112
|
def __repr__(self) -> str:
|
|
115
|
-
return "Error(
|
|
113
|
+
return f"Error({self.message!r}, errno={self.errno!r})"
|
|
116
114
|
|
|
117
115
|
def pprint(self, out: TextIO = sys.stdout) -> None:
|
|
118
116
|
"Pretty-print the error to the given @out file."
|
|
@@ -120,8 +118,8 @@ class PathrsError(Exception):
|
|
|
120
118
|
if self.errno is None:
|
|
121
119
|
print("pathrs error:", file=out)
|
|
122
120
|
else:
|
|
123
|
-
print("pathrs error [
|
|
124
|
-
print("
|
|
121
|
+
print(f"pathrs error [{self.strerror}]:", file=out)
|
|
122
|
+
print(f" {self.message}", file=out)
|
|
125
123
|
|
|
126
124
|
|
|
127
125
|
INTERNAL_ERROR = PathrsError("tried to fetch libpathrs error but no error found")
|
|
@@ -131,7 +129,7 @@ class FilenoFile(typing.Protocol):
|
|
|
131
129
|
def fileno(self) -> int: ...
|
|
132
130
|
|
|
133
131
|
|
|
134
|
-
FileLike =
|
|
132
|
+
FileLike = FilenoFile | int
|
|
135
133
|
|
|
136
134
|
|
|
137
135
|
def _fileno(file: FileLike) -> int:
|
|
@@ -151,7 +149,7 @@ def _clonefile(file: FileLike) -> int:
|
|
|
151
149
|
Fd = TypeVar("Fd", bound="WrappedFd")
|
|
152
150
|
|
|
153
151
|
|
|
154
|
-
class WrappedFd
|
|
152
|
+
class WrappedFd:
|
|
155
153
|
"""
|
|
156
154
|
Represents a file descriptor that allows for manual lifetime management,
|
|
157
155
|
unlike os.fdopen() which are tracked by the GC with no way of "leaking" the
|
|
@@ -160,7 +158,7 @@ class WrappedFd(object):
|
|
|
160
158
|
pathrs will return WrappedFds for most operations that return an fd.
|
|
161
159
|
"""
|
|
162
160
|
|
|
163
|
-
_fd:
|
|
161
|
+
_fd: int | None
|
|
164
162
|
|
|
165
163
|
def __init__(self, file: FileLike, /):
|
|
166
164
|
"""
|
|
@@ -233,12 +231,12 @@ class WrappedFd(object):
|
|
|
233
231
|
raise
|
|
234
232
|
|
|
235
233
|
@classmethod
|
|
236
|
-
def from_raw_fd(cls
|
|
234
|
+
def from_raw_fd(cls, fd: int, /) -> Self:
|
|
237
235
|
"Shorthand for WrappedFd(fd)."
|
|
238
236
|
return cls(fd)
|
|
239
237
|
|
|
240
238
|
@classmethod
|
|
241
|
-
def from_file(cls
|
|
239
|
+
def from_file(cls, file: FileLike, /) -> Self:
|
|
242
240
|
"Shorthand for WrappedFd(file)."
|
|
243
241
|
return cls(file)
|
|
244
242
|
|
|
@@ -288,7 +286,7 @@ class WrappedFd(object):
|
|
|
288
286
|
# A "shallow copy" of a file is the same as a deep copy.
|
|
289
287
|
return copy.deepcopy(self)
|
|
290
288
|
|
|
291
|
-
def __deepcopy__(self, memo:
|
|
289
|
+
def __deepcopy__(self, memo: dict[int, Any]) -> Self:
|
|
292
290
|
"Identical to WrappedFd.clone()"
|
|
293
291
|
return self.clone()
|
|
294
292
|
|
|
@@ -301,9 +299,9 @@ class WrappedFd(object):
|
|
|
301
299
|
|
|
302
300
|
def __exit__(
|
|
303
301
|
self,
|
|
304
|
-
exc_type:
|
|
305
|
-
exc_value:
|
|
306
|
-
exc_traceback:
|
|
302
|
+
exc_type: type[BaseException] | None,
|
|
303
|
+
exc_value: BaseException | None,
|
|
304
|
+
exc_traceback: TracebackType | None,
|
|
307
305
|
) -> None:
|
|
308
306
|
self.close()
|
|
309
307
|
|
|
@@ -344,3 +342,14 @@ def _convert_mode(mode: str) -> int:
|
|
|
344
342
|
|
|
345
343
|
# We don't care about "b" or "t" since that's just a Python thing.
|
|
346
344
|
return flags
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
class SingletonClass(type):
|
|
348
|
+
"""Metaclass used to create singleton classes."""
|
|
349
|
+
|
|
350
|
+
_instances: ClassVar[dict[type, type[Any]]] = {}
|
|
351
|
+
|
|
352
|
+
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.
|
|
353
|
+
if cls not in cls._instances:
|
|
354
|
+
cls._instances[cls] = super().__call__(*args, **kwargs)
|
|
355
|
+
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,9 +26,16 @@ __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
|
|
|
32
|
+
# pathrs_version_info_t *
|
|
33
|
+
@type_check_only
|
|
34
|
+
class VersionInfo:
|
|
35
|
+
version_string: CString
|
|
36
|
+
|
|
37
|
+
def pathrs_version(info: VersionInfo, size: int) -> ErrorId | int: ...
|
|
38
|
+
|
|
35
39
|
# uint64_t
|
|
36
40
|
ProcfsOpenFlags: TypeAlias = int
|
|
37
41
|
PATHRS_PROCFS_NEW_UNMASKED: ProcfsOpenFlags
|
|
@@ -51,60 +55,64 @@ __PATHRS_PROC_TYPE_PID: ProcfsBase
|
|
|
51
55
|
PATHRS_PROC_DEFAULT_ROOTFD: RawFd
|
|
52
56
|
|
|
53
57
|
# procfs API
|
|
54
|
-
def pathrs_procfs_open(how: ProcfsOpenHow, size: int) ->
|
|
58
|
+
def pathrs_procfs_open(how: ProcfsOpenHow, size: int) -> RawFd | ErrorId: ...
|
|
55
59
|
def pathrs_proc_open(
|
|
56
60
|
base: ProcfsBase, path: CString, flags: int
|
|
57
|
-
) ->
|
|
61
|
+
) -> RawFd | ErrorId: ...
|
|
58
62
|
def pathrs_proc_openat(
|
|
59
63
|
proc_root_fd: RawFd, base: ProcfsBase, path: CString, flags: int
|
|
60
|
-
) ->
|
|
64
|
+
) -> RawFd | ErrorId: ...
|
|
61
65
|
def pathrs_proc_readlink(
|
|
62
66
|
base: ProcfsBase, path: CString, linkbuf: CBuffer, linkbuf_size: int
|
|
63
|
-
) ->
|
|
67
|
+
) -> int | ErrorId: ...
|
|
64
68
|
def pathrs_proc_readlinkat(
|
|
65
69
|
proc_root_fd: RawFd,
|
|
66
70
|
base: ProcfsBase,
|
|
67
71
|
path: CString,
|
|
68
72
|
linkbuf: CBuffer,
|
|
69
73
|
linkbuf_size: int,
|
|
70
|
-
) ->
|
|
74
|
+
) -> int | ErrorId: ...
|
|
71
75
|
|
|
72
76
|
# core API
|
|
73
|
-
def pathrs_open_root(path: CString) ->
|
|
74
|
-
def pathrs_reopen(fd: RawFd, flags: int) ->
|
|
75
|
-
def pathrs_inroot_resolve(rootfd: RawFd, path: CString) ->
|
|
76
|
-
def pathrs_inroot_resolve_nofollow(
|
|
77
|
-
|
|
78
|
-
) -> Union[RawFd, ErrorId]: ...
|
|
79
|
-
def pathrs_inroot_open(
|
|
80
|
-
rootfd: RawFd, path: CString, flags: int
|
|
81
|
-
) -> 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: ...
|
|
82
82
|
def pathrs_inroot_creat(
|
|
83
83
|
rootfd: RawFd, path: CString, flags: int, filemode: int
|
|
84
|
-
) ->
|
|
84
|
+
) -> RawFd | ErrorId: ...
|
|
85
85
|
def pathrs_inroot_rename(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
) ->
|
|
92
|
-
def
|
|
86
|
+
old_rootfd: RawFd,
|
|
87
|
+
old_path: CString,
|
|
88
|
+
new_rootfd: RawFd,
|
|
89
|
+
new_path: CString,
|
|
90
|
+
flags: int,
|
|
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: ...
|
|
93
95
|
def pathrs_inroot_mkdir(
|
|
94
96
|
rootfd: RawFd, path: CString, mode: int
|
|
95
|
-
) ->
|
|
97
|
+
) -> Literal[0] | ErrorId: ...
|
|
96
98
|
def pathrs_inroot_mkdir_all(
|
|
97
99
|
rootfd: RawFd, path: CString, mode: int
|
|
98
|
-
) ->
|
|
100
|
+
) -> Literal[0] | ErrorId: ...
|
|
99
101
|
def pathrs_inroot_mknod(
|
|
100
102
|
rootfd: RawFd, path: CString, mode: int, dev: int
|
|
101
|
-
) ->
|
|
103
|
+
) -> Literal[0] | ErrorId: ...
|
|
102
104
|
def pathrs_inroot_hardlink(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
+
old_rootfd: RawFd,
|
|
106
|
+
old_path: CString,
|
|
107
|
+
new_rootfd: RawFd,
|
|
108
|
+
new_path: CString,
|
|
109
|
+
flags: int,
|
|
110
|
+
) -> Literal[0] | ErrorId: ...
|
|
105
111
|
def pathrs_inroot_symlink(
|
|
106
|
-
|
|
107
|
-
|
|
112
|
+
target: CString,
|
|
113
|
+
rootfd: RawFd,
|
|
114
|
+
linkpath: CString,
|
|
115
|
+
) -> Literal[0] | ErrorId: ...
|
|
108
116
|
def pathrs_inroot_readlink(
|
|
109
117
|
rootfd: RawFd, path: CString, linkbuf: CBuffer, linkbuf_size: int
|
|
110
|
-
) ->
|
|
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,25 +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
|
-
|
|
13
|
+
import warnings
|
|
16
14
|
|
|
17
15
|
# TODO: Remove this once we only support Python >= 3.11.
|
|
18
|
-
from
|
|
16
|
+
from typing import IO, Any, TypeAlias, cast
|
|
19
17
|
|
|
20
18
|
from ._internal import (
|
|
19
|
+
INTERNAL_ERROR,
|
|
21
20
|
# File type helpers.
|
|
22
21
|
FileLike,
|
|
23
|
-
WrappedFd,
|
|
24
|
-
_convert_mode,
|
|
25
22
|
# Error API.
|
|
26
23
|
PathrsError,
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
# Generic helpers.
|
|
25
|
+
SingletonClass,
|
|
26
|
+
WrappedFd,
|
|
27
|
+
_cbuffer,
|
|
28
|
+
_convert_mode,
|
|
29
29
|
# CFFI helpers.
|
|
30
30
|
_cstr,
|
|
31
|
-
|
|
31
|
+
_is_pathrs_err,
|
|
32
|
+
_pystr,
|
|
32
33
|
)
|
|
33
34
|
from ._libpathrs_cffi import lib as libpathrs_so
|
|
34
35
|
|
|
@@ -48,14 +49,49 @@ else:
|
|
|
48
49
|
CBuffer: TypeAlias = ffi.CData
|
|
49
50
|
|
|
50
51
|
__all__ = [
|
|
51
|
-
# Core api.
|
|
52
|
-
"Root",
|
|
53
52
|
"Handle",
|
|
54
53
|
# Error api (re-export).
|
|
55
54
|
"PathrsError",
|
|
55
|
+
# Core api.
|
|
56
|
+
"Root",
|
|
57
|
+
"library_version",
|
|
56
58
|
]
|
|
57
59
|
|
|
58
60
|
|
|
61
|
+
class VersionInfo(metaclass=SingletonClass):
|
|
62
|
+
"""Stores information about the runtime version of libpathrs.so."""
|
|
63
|
+
|
|
64
|
+
_VERSION_INFO_TYPE = "pathrs_version_info_t *"
|
|
65
|
+
|
|
66
|
+
version_string: str
|
|
67
|
+
|
|
68
|
+
def __init__(self): # type: ignore[no-untyped-def] # TODO: mypy appears to struggle with metaclasses.
|
|
69
|
+
info = cast("libpathrs_so.VersionInfo", ffi.new(self._VERSION_INFO_TYPE))
|
|
70
|
+
info_size = ffi.sizeof(cast("Any", info))
|
|
71
|
+
|
|
72
|
+
size = libpathrs_so.pathrs_version(info, info_size)
|
|
73
|
+
if _is_pathrs_err(size):
|
|
74
|
+
raise PathrsError._fetch(size) or INTERNAL_ERROR
|
|
75
|
+
if size > 0:
|
|
76
|
+
# TODO: We should also output a warning if the size of
|
|
77
|
+
# pathrs_version_info_t is bigger than the number of fields in the
|
|
78
|
+
# python-native VersionInfo. Otherwise a rebuild will mask that
|
|
79
|
+
# Python callers cannot see any new fields.
|
|
80
|
+
warnings.warn(
|
|
81
|
+
f"pathrs_version returned extra data we could not parse (need {size} bytes but buffer was only {info_size})"
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
self.version_string = _pystr(info.version_string)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def library_version() -> VersionInfo:
|
|
88
|
+
"""
|
|
89
|
+
Returns information about the runtime version of libpathrs.so.
|
|
90
|
+
This is just shorthand for VersionInfo().
|
|
91
|
+
"""
|
|
92
|
+
return VersionInfo() # type: ignore[no-untyped-call] # TODO: mypy appears to struggle with metaclasses.
|
|
93
|
+
|
|
94
|
+
|
|
59
95
|
class Handle(WrappedFd):
|
|
60
96
|
"A handle to a filesystem object, usually resolved using Root.resolve()."
|
|
61
97
|
|
|
@@ -94,7 +130,7 @@ class Root(WrappedFd):
|
|
|
94
130
|
relative to.
|
|
95
131
|
"""
|
|
96
132
|
|
|
97
|
-
def __init__(self, file_or_path:
|
|
133
|
+
def __init__(self, file_or_path: FileLike | str, /):
|
|
98
134
|
"""
|
|
99
135
|
Create a handle from a file-like object or a path to a directory.
|
|
100
136
|
|
|
@@ -266,7 +302,7 @@ class Root(WrappedFd):
|
|
|
266
302
|
"""
|
|
267
303
|
# TODO: Should we have a separate Root.swap() operation?
|
|
268
304
|
err = libpathrs_so.pathrs_inroot_rename(
|
|
269
|
-
self.fileno(), _cstr(src), _cstr(dst), flags
|
|
305
|
+
self.fileno(), _cstr(src), self.fileno(), _cstr(dst), flags
|
|
270
306
|
)
|
|
271
307
|
if _is_pathrs_err(err):
|
|
272
308
|
raise PathrsError._fetch(err) or INTERNAL_ERROR
|
|
@@ -359,35 +395,35 @@ class Root(WrappedFd):
|
|
|
359
395
|
if _is_pathrs_err(err):
|
|
360
396
|
raise PathrsError._fetch(err) or INTERNAL_ERROR
|
|
361
397
|
|
|
362
|
-
def hardlink(self,
|
|
398
|
+
def hardlink(self, target: str, linkname: str, /) -> None:
|
|
363
399
|
"""
|
|
364
400
|
Create a hardlink between two paths inside the Root.
|
|
365
401
|
|
|
366
|
-
|
|
367
|
-
*existing* file.
|
|
402
|
+
linkname is the path to the *new* hardlink, and target is a path to
|
|
403
|
+
the *existing* file.
|
|
368
404
|
|
|
369
405
|
A pathrs.Error is raised if the path for the new hardlink already
|
|
370
406
|
exists.
|
|
371
407
|
"""
|
|
372
408
|
err = libpathrs_so.pathrs_inroot_hardlink(
|
|
373
|
-
self.fileno(), _cstr(
|
|
409
|
+
self.fileno(), _cstr(target), self.fileno(), _cstr(linkname), 0
|
|
374
410
|
)
|
|
375
411
|
if _is_pathrs_err(err):
|
|
376
412
|
raise PathrsError._fetch(err) or INTERNAL_ERROR
|
|
377
413
|
|
|
378
|
-
def symlink(self,
|
|
414
|
+
def symlink(self, target: str, linkname: str, /) -> None:
|
|
379
415
|
"""
|
|
380
416
|
Create a symlink at the given path in the Root.
|
|
381
417
|
|
|
382
|
-
|
|
383
|
-
will point to. Note that
|
|
384
|
-
so there are no restrictions on what target you put.
|
|
418
|
+
linkname is the path to the *new* symlink, and target is what the
|
|
419
|
+
symlink will point to. Note that symlink contents are not verified on
|
|
420
|
+
Linux, so there are no restrictions on what target you put.
|
|
385
421
|
|
|
386
422
|
A pathrs.Error is raised if the path for the new symlink already
|
|
387
423
|
exists.
|
|
388
424
|
"""
|
|
389
425
|
err = libpathrs_so.pathrs_inroot_symlink(
|
|
390
|
-
|
|
426
|
+
_cstr(target), self.fileno(), _cstr(linkname)
|
|
391
427
|
)
|
|
392
428
|
if _is_pathrs_err(err):
|
|
393
429
|
raise PathrsError._fetch(err) or INTERNAL_ERROR
|
|
@@ -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.6
|
|
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.6"
|
|
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,20 +9,20 @@
|
|
|
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
|
|
|
22
22
|
openmode = "rb"
|
|
23
23
|
except ImportError:
|
|
24
24
|
# TODO: Remove this once we only support Python >= 3.11.
|
|
25
|
-
import toml as tomllib # type: ignore
|
|
25
|
+
import toml as tomllib # type: ignore # The types are not compatible.
|
|
26
26
|
|
|
27
27
|
openmode = "r"
|
|
28
28
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|