Skip to content
Open
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
27 changes: 26 additions & 1 deletion local/src/devflow_cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import subprocess
import sys
import tempfile
import uuid
from pathlib import Path
from typing import Dict, List, Optional, Tuple

Expand Down Expand Up @@ -91,7 +92,31 @@ def save_state(project: Path, state: dict) -> None:
path = project / STATE_PATH
path.parent.mkdir(parents=True, exist_ok=True)
state["installer_version"] = __version__
path.write_text(json.dumps(state, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
payload = json.dumps(state, ensure_ascii=False, indent=2) + "\n"
try:
previous_mode = stat.S_IMODE(path.stat().st_mode)
except FileNotFoundError:
previous_mode = None
temporary = path.parent / f".install-state-{uuid.uuid4().hex}"
creation_mode = 0o600 if previous_mode is not None else 0o666
descriptor = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL, creation_mode)
try:
handle = os.fdopen(descriptor, "w", encoding="utf-8")
descriptor = None
with handle:
handle.write(payload)
handle.flush()
os.fsync(handle.fileno())
if previous_mode is not None:
temporary.chmod(previous_mode)
os.replace(temporary, path)
finally:
if descriptor is not None:
try:
os.close(descriptor)
except OSError:
pass
temporary.unlink(missing_ok=True)


def snapshot(root: Path) -> Dict[str, Tuple[bytes, int]]:
Expand Down
47 changes: 46 additions & 1 deletion local/tests/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from contextlib import redirect_stdout
from io import StringIO
from pathlib import Path
from unittest.mock import patch

from devflow_cli.cli import main
from devflow_cli.core import DevFlowError, apply_install, doctor, status_rows, uninstall
from devflow_cli.core import DevFlowError, apply_install, doctor, load_state, save_state, status_rows, uninstall
from devflow_cli.editions import DEFAULT_EDITION, EDITION_SPECS, HOSTS, source_for


Expand Down Expand Up @@ -315,6 +316,50 @@ def test_classic_install_makes_observability_hook_executable(self):
self.assertTrue(hook.is_file(), relative)
self.assertTrue(hook.stat().st_mode & stat.S_IXUSR, relative)

@unittest.skipIf(os.name == "nt", "POSIX permission contract")
def test_state_replace_preserves_existing_mode(self):
with tempfile.TemporaryDirectory() as temporary:
project = self.project(temporary)
apply_install(project, "codex", "classic")
path = project / ".devflow/install-state.json"
path.chmod(0o640)
save_state(project, load_state(project))
self.assertEqual(stat.S_IMODE(path.stat().st_mode), 0o640)

@unittest.skipIf(os.name == "nt", "POSIX permission contract")
def test_new_state_respects_creation_umask(self):
script = (
"import os,sys; from pathlib import Path; "
"from devflow_cli.core import save_state; "
"os.umask(int(sys.argv[2], 8)); "
"save_state(Path(sys.argv[1]), {'version': 1, 'installations': {}})"
)
for mask, expected in (("022", 0o644), ("027", 0o640), ("077", 0o600)):
with self.subTest(mask=mask), tempfile.TemporaryDirectory() as temporary:
project = Path(temporary) / "project"
environment = dict(os.environ, PYTHONPATH=str(ROOT / "src"))
subprocess.run([sys.executable, "-c", script, str(project), mask],
env=environment, check=True)
path = project / ".devflow/install-state.json"
self.assertEqual(stat.S_IMODE(path.stat().st_mode), expected)

def test_failed_state_replace_preserves_previous_records(self):
with tempfile.TemporaryDirectory() as temporary:
project = self.project(temporary)
apply_install(project, "codex", "classic")
path = project / ".devflow/install-state.json"
original = path.read_bytes()

state = load_state(project)
state["installations"] = {}
with patch("devflow_cli.core.os.replace", side_effect=OSError("cannot replace")):
with self.assertRaisesRegex(OSError, "cannot replace"):
save_state(project, state)

self.assertEqual(original, path.read_bytes())
self.assertEqual([path], list(path.parent.iterdir()))
self.assertIn("classic:codex", load_state(project)["installations"])

def test_update_restores_missing_executable_bit_on_hook(self):
with tempfile.TemporaryDirectory() as temporary:
project = self.project(temporary)
Expand Down