138 lines
3.7 KiB
Go
138 lines
3.7 KiB
Go
package guest
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/internal/plugin/builtin"
|
|
)
|
|
|
|
// retiredIDs are historical plugin IDs whose macOS state moves to ID.
|
|
var retiredIDs = []string{LegacyInstallGuideID}
|
|
|
|
// MigrateState moves retired plugin records and former core macOS settings to ID
|
|
// in memory. Explicit values already stored on the macOS plugin win, then explicit
|
|
// former core values, then defaults. All records are changed before one atomic save.
|
|
func MigrateState(st *plugin.StateFile) bool {
|
|
if st == nil {
|
|
return false
|
|
}
|
|
if st.Plugins == nil {
|
|
st.Plugins = map[string]*plugin.PluginRecord{}
|
|
}
|
|
legacy := false
|
|
for _, id := range retiredIDs {
|
|
if _, ok := st.Plugins[id]; ok {
|
|
legacy = true
|
|
}
|
|
}
|
|
core := st.Plugins[builtin.IDCore]
|
|
coreSettings := core != nil && (hasKey(core.AppSettings, SettingWHPXFallback) || hasKey(core.AppSettings, SettingWin10ColdReboot))
|
|
if !legacy && !coreSettings {
|
|
return false
|
|
}
|
|
target := st.Plugins[ID]
|
|
if target == nil {
|
|
target = &plugin.PluginRecord{ID: ID, Source: plugin.SourceBuiltin, Trust: plugin.TrustOfficial, Enabled: true}
|
|
st.Plugins[ID] = target
|
|
}
|
|
if target.AppSettings == nil {
|
|
target.AppSettings = map[string]any{}
|
|
}
|
|
for _, key := range []string{SettingWHPXFallback, SettingWin10ColdReboot} {
|
|
if hasKey(target.AppSettings, key) {
|
|
continue
|
|
}
|
|
if coreSettings {
|
|
if v, ok := core.AppSettings[key]; ok {
|
|
target.AppSettings[key] = v
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
if !hasKey(target.AppSettings, SettingInstallGuide) {
|
|
if rec := st.Plugins[LegacyInstallGuideID]; rec != nil {
|
|
target.AppSettings[SettingInstallGuide] = rec.Enabled
|
|
}
|
|
}
|
|
if coreSettings {
|
|
delete(core.AppSettings, SettingWHPXFallback)
|
|
delete(core.AppSettings, SettingWin10ColdReboot)
|
|
}
|
|
for _, id := range retiredIDs {
|
|
delete(st.Plugins, id)
|
|
}
|
|
target.UpdatedAt = time.Now()
|
|
return true
|
|
}
|
|
|
|
func hasKey(values map[string]any, key string) bool {
|
|
_, ok := values[key]
|
|
return ok
|
|
}
|
|
|
|
// MigrateFiles upgrades plugin state and every macOS VM argument ledger. It
|
|
// is idempotent and is safe to call before plugin.NewManager loads state.
|
|
func MigrateFiles(dataRoot string) error {
|
|
store := plugin.NewStateStore(plugin.NewPluginsLayout(dataRoot).Root)
|
|
st, err := store.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if MigrateState(st) {
|
|
if err := store.Save(st); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return MigrateLedgers(dataRoot)
|
|
}
|
|
|
|
// MigrateLedgers upgrades argument ownership without rewriting plugin state.
|
|
func MigrateLedgers(dataRoot string) error {
|
|
root := filepath.Join((&config.Config{DataRoot: dataRoot}).XDGDataDir(), "reims-ctl", "vms")
|
|
entries, err := os.ReadDir(root)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, entry := range entries {
|
|
if !entry.IsDir() {
|
|
continue
|
|
}
|
|
dir := filepath.Join(root, entry.Name())
|
|
ledger, err := plugin.LoadArgLedger(dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !MigrateLedger(ledger) {
|
|
continue
|
|
}
|
|
path := filepath.Join(dir, plugin.ArgLedgerFileName)
|
|
if raw, err := os.ReadFile(path); err == nil {
|
|
if err := os.WriteFile(path+".bak", raw, 0o644); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := plugin.SaveArgLedger(dir, ledger); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// MigrateLedger carries only known macOS-owned fragments away from the old
|
|
// core and retired IDs; user arguments and other plugins stay untouched.
|
|
func MigrateLedger(l *plugin.ArgLedger) bool {
|
|
changed := l.MergeOwners(retiredIDs, ID)
|
|
if l.MoveOwnerKeys(builtin.IDCore, ID, []string{keyOfflineHelper, keyKernelIrqchip, keyWin10ColdReboot}) {
|
|
changed = true
|
|
}
|
|
return changed
|
|
}
|