Files
Reims-Go/internal/runtime/selection.go
T

127 lines
3.3 KiB
Go

package runtime
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/i18n"
)
func Activate(ctx context.Context, cfg *config.Config, commit string) error {
return saveSelection(ctx, cfg, commit, nil, host.SyncQEMUASLR)
}
func SetQEMUASLR(ctx context.Context, cfg *config.Config, enabled bool) error {
return saveSelection(ctx, cfg, "", &enabled, host.SyncQEMUASLR)
}
// RefreshSelection reads the last atomically published selection, without
// waiting for a concurrent install or a pending Windows consent prompt.
func RefreshSelection(cfg *config.Config) error {
saved, err := config.LoadExisting(filepath.Join(cfg.DataRoot, "reims-go.json"))
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return err
}
cfg.ActiveRuntime, cfg.QEMUASLR = saved.ActiveRuntime, saved.QEMUASLR
return nil
}
// SavePreferences keeps an unrelated GUI edit from overwriting a CLI switch.
func SavePreferences(cfg *config.Config) error {
release, err := lockRuntimeDirectories(cfg.RuntimesDir())
if err != nil {
return err
}
defer release()
if err := RefreshSelection(cfg); err != nil {
return err
}
return config.Save("", cfg)
}
func qemuASLRPath(cfg *config.Config) (string, error) {
if !cfg.QEMUASLR {
return "", nil
}
active, err := ResolveActive(cfg)
if errors.Is(err, ErrNoActiveRuntime) {
return "", nil // The first runtime install will apply the preference.
}
if err != nil {
return "", err
}
return active.QemuPath(), nil
}
func saveSelection(ctx context.Context, cfg *config.Config, commit string, aslr *bool, syncPolicy func(context.Context, string, string) error) error {
root := cfg.RuntimesDir()
release, err := lockRuntimeDirectories(root)
if err != nil {
return err
}
defer release()
// Another client may have selected a runtime or changed ASLR since startup.
path := filepath.Join(cfg.DataRoot, "reims-go.json")
prev := *cfg
if saved, err := config.LoadExisting(path); err == nil {
prev = *saved
prev.DataRoot = cfg.DataRoot
} else if !errors.Is(err, os.ErrNotExist) {
return err
}
next := prev
if aslr != nil {
next.QEMUASLR = *aslr
} else {
installed, err := List(&prev)
if err != nil {
return err
}
if !slices.ContainsFunc(installed, func(r Installed) bool {
return r.Commit == commit || r.Kind+"/"+r.Commit == commit
}) {
return fmt.Errorf("%w: %s", ErrNotFound, commit)
}
next.ActiveRuntime = commit
}
managePolicy := aslr != nil || next.QEMUASLR
if managePolicy {
target, pathErr := qemuASLRPath(&next)
if pathErr != nil {
return pathErr
}
err = syncPolicy(ctx, root, target)
if errors.Is(err, host.ErrElevationDeclined) {
return i18n.NewCodedError(i18n.EffectiveLocale(cfg.Locale), "E_QEMU_ASLR_CANCELLED", "err.qemu_aslr.cancelled", nil, err)
}
if errors.Is(err, context.Canceled) {
return err
}
}
if err == nil {
err = config.Save(path, &next)
}
if err != nil && managePolicy {
previousPath, restoreErr := qemuASLRPath(&prev)
if restoreErr == nil {
restoreErr = syncPolicy(context.WithoutCancel(ctx), root, previousPath)
}
return errors.Join(err, restoreErr)
}
if err != nil {
return err
}
cfg.ActiveRuntime, cfg.QEMUASLR = next.ActiveRuntime, next.QEMUASLR
return nil
}