diff --git a/docs/operating.md b/docs/operating.md
index ee4ed5e..d384a67 100644
--- a/docs/operating.md
+++ b/docs/operating.md
@@ -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
` 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
diff --git a/internal/cli/doctor.go b/internal/cli/doctor.go
index 909b96e..98da2ff 100644
--- a/internal/cli/doctor.go
+++ b/internal/cli/doctor.go
@@ -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"
diff --git a/internal/cli/doctor_directory_test.go b/internal/cli/doctor_directory_test.go
index 094cb4c..b34e2e0 100644
--- a/internal/cli/doctor_directory_test.go
+++ b/internal/cli/doctor_directory_test.go
@@ -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)
+ }
+}