Repository navigation
Expand file tree
/
Copy pathsqlite_util.py
More file actions
239 lines (207 loc) · 8.5 KB
/
Copy pathsqlite_util.py
File metadata and controls
239 lines (207 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""SQLite lock-contention helpers shared by the LCM engine.
Isolated from ``engine.py`` (WS5 seam): lock-contention detection, bounded
``busy_timeout`` changes, and transaction-preserving savepoints are pure SQLite
concerns with no engine state. Callers keep their own policy constants (for
example the session-end timeout budget).
"""
from __future__ import annotations
import errno
import os
from pathlib import Path
import sqlite3
import stat
import uuid
from contextlib import contextmanager
from typing import Iterator, List
_SQLITE_SIDECAR_SUFFIXES = ("-wal", "-shm", "-journal")
def _sqlite_artifact_error(path: Path, reason: str) -> OSError:
return OSError(errno.EPERM, f"refusing SQLite artifact {path.name!r}: {reason}", str(path))
def _same_file_identity(left: os.stat_result, right: os.stat_result) -> bool:
return (left.st_dev, left.st_ino) == (right.st_dev, right.st_ino)
def _validate_sqlite_artifact(path: Path, file_stat: os.stat_result) -> None:
if not stat.S_ISREG(file_stat.st_mode):
raise _sqlite_artifact_error(path, "not a regular file")
if file_stat.st_nlink != 1:
raise _sqlite_artifact_error(path, "link count is not one")
def _require_sqlite_artifact_absent(path: Path, *, directory_fd: int) -> None:
"""Accept a vanished sidecar only while its directory entry stays absent."""
try:
current = os.stat(path.name, dir_fd=directory_fd, follow_symlinks=False)
except FileNotFoundError:
return
_validate_sqlite_artifact(path, current)
raise _sqlite_artifact_error(path, "directory entry changed while opening")
def _open_private_sqlite_directory(path: Path) -> int:
directory = path.parent
expected = os.stat(directory, follow_symlinks=False)
if not stat.S_ISDIR(expected.st_mode):
raise _sqlite_artifact_error(path, "parent is not a regular directory")
if expected.st_mode & 0o022:
raise _sqlite_artifact_error(path, "parent directory is writable by another user")
flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
flags |= getattr(os, "O_CLOEXEC", 0) | getattr(os, "O_BINARY", 0)
directory_fd = os.open(directory, flags)
opened = os.fstat(directory_fd)
if not stat.S_ISDIR(opened.st_mode) or not _same_file_identity(expected, opened):
os.close(directory_fd)
raise _sqlite_artifact_error(path, "parent directory changed while opening")
return directory_fd
def _chmod_sqlite_artifact_at(
path: Path,
*,
directory_fd: int,
create: bool,
allow_sidecar_disappearance: bool = False,
) -> bool:
expected: os.stat_result | None
try:
expected = os.stat(path.name, dir_fd=directory_fd, follow_symlinks=False)
except FileNotFoundError:
if not create:
return False
expected = None
if expected is not None:
_validate_sqlite_artifact(path, expected)
flags = os.O_RDWR | getattr(os, "O_CLOEXEC", 0) | getattr(os, "O_NOFOLLOW", 0)
flags |= getattr(os, "O_NONBLOCK", 0) | getattr(os, "O_BINARY", 0)
if expected is None:
flags |= os.O_CREAT | os.O_EXCL
try:
fd = os.open(path.name, flags, 0o600, dir_fd=directory_fd)
except FileExistsError:
if expected is not None:
raise
return _chmod_sqlite_artifact_at(
path,
directory_fd=directory_fd,
create=False,
)
except FileNotFoundError:
if not allow_sidecar_disappearance:
raise
_require_sqlite_artifact_absent(path, directory_fd=directory_fd)
return False
try:
opened = os.fstat(fd)
if not stat.S_ISREG(opened.st_mode):
raise _sqlite_artifact_error(path, "not a regular file")
if expected is not None and not _same_file_identity(expected, opened):
raise _sqlite_artifact_error(path, "directory entry changed while opening")
if opened.st_nlink == 0 and allow_sidecar_disappearance:
_require_sqlite_artifact_absent(path, directory_fd=directory_fd)
return False
_validate_sqlite_artifact(path, opened)
os.fchmod(fd, 0o600)
restricted = os.fstat(fd)
if restricted.st_nlink == 0 and allow_sidecar_disappearance:
_require_sqlite_artifact_absent(path, directory_fd=directory_fd)
return False
if restricted.st_nlink != 1:
raise _sqlite_artifact_error(path, "link count changed while restricting permissions")
finally:
os.close(fd)
return True
def _restrict_existing_sqlite_artifacts(db_path: Path) -> None:
"""Restrict verified, single-link SQLite files without following links."""
if os.name != "posix": # pragma: no cover - Windows compatibility fallback
for artifact in (
db_path,
*(db_path.with_name(db_path.name + suffix) for suffix in _SQLITE_SIDECAR_SUFFIXES),
):
try:
artifact.chmod(0o600)
except FileNotFoundError:
continue
return
directory_fd = _open_private_sqlite_directory(db_path)
try:
_chmod_sqlite_artifact_at(
db_path,
directory_fd=directory_fd,
create=False,
)
for suffix in _SQLITE_SIDECAR_SUFFIXES:
_chmod_sqlite_artifact_at(
db_path.with_name(db_path.name + suffix),
directory_fd=directory_fd,
create=False,
allow_sidecar_disappearance=True,
)
finally:
os.close(directory_fd)
def _prepare_private_sqlite_file(path: Path) -> None:
"""Create or tighten one SQLite file and its existing sidecars safely."""
if os.name != "posix": # pragma: no cover - Windows compatibility fallback
flags = os.O_RDWR | os.O_CREAT | getattr(os, "O_CLOEXEC", 0)
fd = os.open(path, flags, 0o600)
try:
if hasattr(os, "fchmod"):
os.fchmod(fd, 0o600)
else:
path.chmod(0o600)
finally:
os.close(fd)
_restrict_existing_sqlite_artifacts(path)
return
directory_fd = _open_private_sqlite_directory(path)
try:
_chmod_sqlite_artifact_at(path, directory_fd=directory_fd, create=True)
for suffix in _SQLITE_SIDECAR_SUFFIXES:
_chmod_sqlite_artifact_at(
path.with_name(path.name + suffix),
directory_fd=directory_fd,
create=False,
allow_sidecar_disappearance=True,
)
finally:
os.close(directory_fd)
def _is_sqlite_locked_error(exc: BaseException) -> bool:
"""Return True when an exception chain represents SQLite lock contention."""
seen: set[int] = set()
current: BaseException | None = exc
while current is not None and id(current) not in seen:
seen.add(id(current))
message = str(current).lower()
if isinstance(current, sqlite3.Error) and "locked" in message:
return True
current = current.__cause__ or current.__context__
return False
def _sqlite_busy_timeout_ms(conn: sqlite3.Connection) -> int:
row = conn.execute("PRAGMA busy_timeout").fetchone()
return int(row[0]) if row and row[0] is not None else 0
@contextmanager
def _sqlite_savepoint(conn: sqlite3.Connection) -> Iterator[None]:
"""Isolate helper writes without taking ownership of a caller transaction."""
# UUID hex contains only identifier-safe characters and keeps every nested
# helper's SAVEPOINT name unique with a fixed upper bound on name length.
name = f"lcm_{uuid.uuid4().hex}"
conn.execute(f"SAVEPOINT {name}")
try:
yield
except BaseException:
try:
conn.execute(f"ROLLBACK TO SAVEPOINT {name}")
finally:
conn.execute(f"RELEASE SAVEPOINT {name}")
raise
else:
conn.execute(f"RELEASE SAVEPOINT {name}")
@contextmanager
def _temporary_sqlite_busy_timeout(
connections: List[sqlite3.Connection | None],
timeout_ms: int,
) -> Iterator[None]:
"""Temporarily bound SQLite lock waits for gateway-critical paths."""
bounded_timeout = max(0, int(timeout_ms))
originals: list[tuple[sqlite3.Connection, int]] = []
for conn in connections:
if conn is None:
continue
original = _sqlite_busy_timeout_ms(conn)
conn.execute(f"PRAGMA busy_timeout={bounded_timeout}")
originals.append((conn, original))
try:
yield
finally:
for conn, original in reversed(originals):
conn.execute(f"PRAGMA busy_timeout={original}")