|
44 | 44 | } |
45 | 45 | _AT_FDCWD = -100 |
46 | 46 | _RENAME_NOREPLACE = 1 |
| 47 | +_RENAME_EXCL = 0x4 |
47 | 48 | _RENAMEAT2_UNAVAILABLE = { |
48 | 49 | errno.ENOSYS, |
49 | 50 | errno.EINVAL, |
@@ -177,52 +178,82 @@ def _validate_optimizer_config_for_copy(path: Path) -> None: |
177 | 178 | _validate_sensitive_config_values(payload) |
178 | 179 |
|
179 | 180 |
|
180 | | -def _target_exists(path: Path) -> bool: |
181 | | - return path.exists() or path.is_symlink() |
182 | | - |
183 | | - |
184 | 181 | def _rename_directory_no_replace(source: Path, target: Path) -> None: |
185 | 182 | """Atomically publish a directory without replacing an existing target. |
186 | 183 |
|
187 | | - Linux uses renameat2 with RENAME_NOREPLACE. Platforms without that primitive |
188 | | - use a controlled fallback that rechecks the target immediately before rename. |
| 184 | + Each supported platform uses an atomic no-replace primitive. Platforms |
| 185 | + without that primitive fail closed rather than risking a replacement race. |
189 | 186 | """ |
190 | 187 | if sys.platform.startswith("linux"): |
191 | 188 | try: |
192 | 189 | libc = ctypes.CDLL(None, use_errno=True) |
193 | 190 | renameat2 = libc.renameat2 |
194 | 191 | except (AttributeError, OSError): |
195 | | - renameat2 = None |
196 | | - if renameat2 is not None: |
197 | | - renameat2.argtypes = [ |
198 | | - ctypes.c_int, |
199 | | - ctypes.c_char_p, |
200 | | - ctypes.c_int, |
201 | | - ctypes.c_char_p, |
202 | | - ctypes.c_uint, |
203 | | - ] |
204 | | - renameat2.restype = ctypes.c_int |
205 | | - ctypes.set_errno(0) |
206 | | - result = renameat2( |
207 | | - _AT_FDCWD, |
208 | | - os.fsencode(source), |
209 | | - _AT_FDCWD, |
210 | | - os.fsencode(target), |
211 | | - _RENAME_NOREPLACE, |
| 192 | + raise ArtifactWriteError( |
| 193 | + "atomic no-replace unavailable: Linux renameat2 is unavailable" |
212 | 194 | ) |
213 | | - if result == 0: |
214 | | - return |
215 | | - error_number = ctypes.get_errno() |
216 | | - if error_number == errno.EEXIST: |
217 | | - raise ArtifactWriteError( |
218 | | - f"report directory already exists: {target}" |
219 | | - ) |
220 | | - if error_number not in _RENAMEAT2_UNAVAILABLE: |
221 | | - raise OSError(error_number, os.strerror(error_number), target) |
| 195 | + renameat2.argtypes = [ |
| 196 | + ctypes.c_int, |
| 197 | + ctypes.c_char_p, |
| 198 | + ctypes.c_int, |
| 199 | + ctypes.c_char_p, |
| 200 | + ctypes.c_uint, |
| 201 | + ] |
| 202 | + renameat2.restype = ctypes.c_int |
| 203 | + ctypes.set_errno(0) |
| 204 | + result = renameat2( |
| 205 | + _AT_FDCWD, |
| 206 | + os.fsencode(source), |
| 207 | + _AT_FDCWD, |
| 208 | + os.fsencode(target), |
| 209 | + _RENAME_NOREPLACE, |
| 210 | + ) |
| 211 | + if result == 0: |
| 212 | + return |
| 213 | + error_number = ctypes.get_errno() |
| 214 | + if error_number == errno.EEXIST: |
| 215 | + raise ArtifactWriteError(f"report directory already exists: {target}") |
| 216 | + if error_number in _RENAMEAT2_UNAVAILABLE: |
| 217 | + raise ArtifactWriteError( |
| 218 | + "atomic no-replace unavailable: Linux renameat2 does not support " |
| 219 | + f"RENAME_NOREPLACE ({os.strerror(error_number)})" |
| 220 | + ) |
| 221 | + raise OSError(error_number, os.strerror(error_number), target) |
| 222 | + |
| 223 | + if sys.platform.startswith("win"): |
| 224 | + try: |
| 225 | + os.rename(source, target) |
| 226 | + except FileExistsError as exc: |
| 227 | + raise ArtifactWriteError(f"report directory already exists: {target}") from exc |
| 228 | + return |
| 229 | + |
| 230 | + if sys.platform == "darwin": |
| 231 | + try: |
| 232 | + libc = ctypes.CDLL(None, use_errno=True) |
| 233 | + renamex_np = libc.renamex_np |
| 234 | + except (AttributeError, OSError): |
| 235 | + raise ArtifactWriteError( |
| 236 | + "atomic no-replace unavailable: Darwin renamex_np is unavailable" |
| 237 | + ) |
| 238 | + renamex_np.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_uint] |
| 239 | + renamex_np.restype = ctypes.c_int |
| 240 | + ctypes.set_errno(0) |
| 241 | + result = renamex_np(os.fsencode(source), os.fsencode(target), _RENAME_EXCL) |
| 242 | + if result == 0: |
| 243 | + return |
| 244 | + error_number = ctypes.get_errno() |
| 245 | + if error_number == errno.EEXIST: |
| 246 | + raise ArtifactWriteError(f"report directory already exists: {target}") |
| 247 | + if error_number in _RENAMEAT2_UNAVAILABLE: |
| 248 | + raise ArtifactWriteError( |
| 249 | + "atomic no-replace unavailable: Darwin renamex_np does not support " |
| 250 | + f"RENAME_EXCL ({os.strerror(error_number)})" |
| 251 | + ) |
| 252 | + raise OSError(error_number, os.strerror(error_number), target) |
222 | 253 |
|
223 | | - if _target_exists(target): |
224 | | - raise ArtifactWriteError(f"report directory already exists: {target}") |
225 | | - source.rename(target) |
| 254 | + raise ArtifactWriteError( |
| 255 | + f"atomic no-replace unavailable: unsupported platform {sys.platform}" |
| 256 | + ) |
226 | 257 |
|
227 | 258 |
|
228 | 259 | def _published_relative_path(root: Path, path: Path) -> str: |
|
0 commit comments