Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions scripts/ai-accounts/ai-accounts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ from __future__ import annotations

import argparse
import base64
import ctypes
import datetime as dt
import errno
from email.utils import parsedate_to_datetime
Expand Down Expand Up @@ -201,9 +202,20 @@ def sync_codex_home(home: Path, *, create: bool = False) -> None:
_sync_codex_home(home)


def _copy_across_devices(entry: Path, target: Path) -> None:
import ctypes
def _rename_noreplace(source: Path, target: Path) -> None:
"""Publish a shared entry atomically, including directories."""
libc = ctypes.CDLL(None, use_errno=True)
renameat2 = getattr(libc, "renameat2", None)
if renameat2 is None:
raise OSError(errno.ENOSYS, "atomic no-replace promotion requires renameat2")
renameat2.argtypes = (ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_uint)
at_fdcwd, rename_noreplace = -100, 1
if renameat2(at_fdcwd, os.fsencode(source), at_fdcwd, os.fsencode(target), rename_noreplace):
error = ctypes.get_errno()
raise OSError(error, os.strerror(error), source, None, target)


def _copy_across_devices(entry: Path, target: Path) -> None:
# The private staging directory is also excluded from future mirroring
# by shared() if the process is interrupted before cleanup.
with tempfile.TemporaryDirectory(dir=target.parent, prefix=".codex-sync-", suffix=".lock") as directory:
Expand All @@ -214,16 +226,7 @@ def _copy_across_devices(entry: Path, target: Path) -> None:
else:
shutil.copy2(entry, staged, follow_symlinks=False)

# Linux/WSL renameat2 with RENAME_NOREPLACE publishes the complete
# entry in one operation without replacing a competing destination.
rename = getattr(ctypes.CDLL(None, use_errno=True), "renameat2", None)
if rename is None:
raise OSError(errno.ENOSYS, "Atomic cross-device publication is unavailable", target)
if rename(-100, os.fsencode(staged), -100, os.fsencode(target), 1):
error = ctypes.get_errno()
if error == errno.EEXIST:
return
raise OSError(error, os.strerror(error), target)
_rename_noreplace(staged, target)
if is_directory:
shutil.rmtree(entry)
else:
Expand All @@ -237,13 +240,16 @@ def _sync_codex_home(home: Path) -> None:
if shared(entry) and not entry.is_symlink() and not target.exists() \
and not target.is_symlink():
try:
os.rename(entry, target)
except FileNotFoundError:
pass
try:
_rename_noreplace(entry, target)
except OSError as exc:
if exc.errno != errno.EXDEV:
raise
_copy_across_devices(entry, target)
except OSError as exc:
if exc.errno != errno.EXDEV:
if exc.errno not in {errno.EEXIST, errno.ENOENT, errno.ENOSYS, errno.EINVAL,
errno.EOPNOTSUPP}:
raise
_copy_across_devices(entry, target)
for src in CODEX_MAIN.iterdir():
dst = home / src.name
if shared(src) and not dst.exists() and not dst.is_symlink():
Expand Down
Loading
Loading