| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | ) |
| 8 | |
| 9 | // LastKnownGoodConfigPath is the fixed path of the most recent verified user |
| 10 | // config snapshot. Written by repair.RecordHealthyConfig after a successful |
| 11 | // desktop boot; used only as an in-memory recovery source when the live file |
| 12 | // cannot be parsed. The original user config is never overwritten by a load. |
| 13 | func LastKnownGoodConfigPath() string { |
| 14 | root := MemoryUserDir() |
| 15 | if root == "" { |
| 16 | return "" |
| 17 | } |
| 18 | return filepath.Join(root, "repair", "config.toml.last-known-good") |
| 19 | } |
| 20 | |
| 21 | // loadLastKnownGoodUserConfig merges a validated LKG snapshot into cfg. |
| 22 | // Returns an error when no usable snapshot exists. |
| 23 | func loadLastKnownGoodUserConfig(cfg *Config) error { |
| 24 | path := LastKnownGoodConfigPath() |
| 25 | if path == "" { |
| 26 | return fmt.Errorf("last-known-good path unavailable") |
| 27 | } |
| 28 | data, err := os.ReadFile(path) |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | if err := ValidateBytes(data); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | if _, err := decodeTOMLBytes(data, cfg); err != nil { |
| 36 | return fmt.Errorf("last-known-good: %w", err) |
| 37 | } |
| 38 | return nil |
| 39 | } |
| 40 |