Skip to content
Merged
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
4 changes: 3 additions & 1 deletion docs/operating.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ runtime=directory presence=present compatibility=compatible action=configure tou
A target with no prior Cortex ownership, including a path that does not exist, is refused
with `uninstall=not_installed` and exit 3; nothing is created or removed.
- `cortex doctor --target-dir <dir>` reports `presence`, `writability`, and `ownership`
(`unowned`, `owned`, `invalid`) for the directory; an unwritable directory or an unreadable
(`unowned`, `owned`, `invalid`) for the directory. Only a missing
`.cortex/install-state.json` is `unowned`; a state file that exists but cannot be read or
decoded is `invalid`. An unwritable directory or an unreadable
prior state exits with the same conflict code install would hit.

## Running update
Expand Down
9 changes: 6 additions & 3 deletions internal/cli/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,16 @@ func runDoctorDirectoryTarget(stdout io.Writer, targetDir string) int {

// doctorTargetOwnership classifies the prior state of a directory target:
// unowned (no state file), owned (a canonical state this build decodes), or
// invalid (a state file it cannot classify, which blocks install like the
// canonical prior-state blocker does).
// invalid (a state file it cannot read or classify, which blocks install like
// the canonical prior-state blocker does).
func doctorTargetOwnership(targetDir string) string {
state, err := os.ReadFile(filepath.Join(targetDir, ".cortex", "install-state.json"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "unowned"
}
if err != nil {
return "invalid"
}
manifest, err := installstate.Decode(state)
if err != nil || manifest.RuntimeID() != runtimematrix.RuntimeDirectory {
return "invalid"
Expand Down
40 changes: 40 additions & 0 deletions internal/cli/doctor_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,43 @@ func TestDoctorDirectoryTargetUnwritable(t *testing.T) {
t.Fatalf("unwritable exit = %d, want conflict", code)
}
}

func TestDoctorDirectoryTargetUnreadableStateIsInvalid(t *testing.T) {
home := canonicalHome(t)
writeState := func(name string, data []byte) string {
t.Helper()
target := filepath.Join(home, name)
if err := os.MkdirAll(filepath.Join(target, ".cortex"), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(target, ".cortex", "install-state.json"), data, 0o600); err != nil {
t.Fatal(err)
}
return target
}

// An undecodable state is the existing invalid classification.
garbage := writeState("garbage", []byte("not json"))
garbageCode, garbageOut, garbageErr := runDoctorOnTarget(t, garbage)
if garbageErr != "" || garbageOut != "target="+garbage+" presence=present writability=writable ownership=invalid\n" {
t.Fatalf("garbage: exit = %d stdout = %q stderr = %q", garbageCode, garbageOut, garbageErr)
}

// An unreadable state exists, so it is invalid exactly like an undecodable one.
unreadable := writeState("unreadable", []byte("{}"))
statePath := filepath.Join(unreadable, ".cortex", "install-state.json")
if err := os.Chmod(statePath, 0o000); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chmod(statePath, 0o600) })
if os.Getuid() == 0 {
t.Skip("root can read a mode 0000 file")
}
code, stdout, stderr := runDoctorOnTarget(t, unreadable)
if want := "target=" + unreadable + " presence=present writability=writable ownership=invalid\n"; stdout != want {
t.Fatalf("unreadable stdout = %q, want %q", stdout, want)
}
if stderr != "" || code != exitOK || garbageCode != exitOK {
t.Fatalf("unreadable: exit = %d (invalid exits %d) stderr = %q", code, garbageCode, stderr)
}
}
Loading