183 lines
4.6 KiB
Go
183 lines
4.6 KiB
Go
package app
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/dataroot"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
"github.com/Nwflower/reims-go/internal/version"
|
|
)
|
|
|
|
// openDataRoot opens the current data folder (dataroot.Open: version check,
|
|
// migration under the folder lock) and records whether it may be written.
|
|
func (a *App) openDataRoot() dataroot.Status {
|
|
a.mu.Lock()
|
|
dir := a.cfg.DataRoot
|
|
a.mu.Unlock()
|
|
st := dataroot.Open(dir, version.Version)
|
|
a.mu.Lock()
|
|
a.root = st
|
|
a.syncClipboardStagingLocked()
|
|
a.mu.Unlock()
|
|
a.logDataRoot(st)
|
|
return st
|
|
}
|
|
|
|
func (a *App) logDataRoot(st dataroot.Status) {
|
|
switch {
|
|
case st.ReadOnly:
|
|
msg := fmt.Sprintf("Data folder %s opened read-only (%s)", st.Dir, st.Reason)
|
|
if st.Detail != "" {
|
|
msg += ": " + st.Detail
|
|
}
|
|
a.emitLog("app", msg)
|
|
case len(st.Migrated) > 0:
|
|
a.emitLog("app", fmt.Sprintf("Data folder upgraded to layout v%d (%s)", st.Version, strings.Join(st.Migrated, ", ")))
|
|
}
|
|
}
|
|
|
|
// dataRootUsable reports whether anything may read the data folder through
|
|
// reims-ctl or write to it: not before first-run setup has chosen a folder,
|
|
// and not while the folder is open read-only.
|
|
func (a *App) dataRootUsable() bool {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return !a.needsSetup && !a.root.ReadOnly
|
|
}
|
|
|
|
// requireWritableDataRoot is the guard for bindings that change the data
|
|
// folder and can be reached without the main window, such as a VM start
|
|
// handed over from the CLI.
|
|
func (a *App) requireWritableDataRoot() error {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return a.requireWritableDataRootLocked()
|
|
}
|
|
|
|
func (a *App) requireWritableDataRootLocked() error {
|
|
if a.needsSetup {
|
|
return i18n.NewCodedError(i18n.EffectiveLocale(a.cfg.Locale), "E_DATAROOT_SETUP", "err.dataroot.needs_setup", nil, nil)
|
|
}
|
|
return dataroot.Error(i18n.EffectiveLocale(a.cfg.Locale), a.root)
|
|
}
|
|
|
|
func (a *App) setPreference(edit func(*config.Config)) error {
|
|
a.configMu.Lock()
|
|
defer a.configMu.Unlock()
|
|
a.mu.Lock()
|
|
cfg := *a.cfg
|
|
setup := a.needsSetup
|
|
err := a.requireWritableDataRootLocked()
|
|
a.mu.Unlock()
|
|
if err != nil && !setup {
|
|
return err
|
|
}
|
|
edit(&cfg)
|
|
if !setup {
|
|
if err := rt.SavePreferences(&cfg); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
a.mu.Lock()
|
|
a.cfg = &cfg
|
|
a.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// Runtime policy may wait for UAC. Publish only its two fields over the
|
|
// current settings once the disk transaction has finished.
|
|
func (a *App) updateSelection(persist func(*config.Config) error) error {
|
|
a.selectionMu.Lock()
|
|
defer a.selectionMu.Unlock()
|
|
a.mu.Lock()
|
|
err := a.requireWritableDataRootLocked()
|
|
cfg := *a.cfg
|
|
a.mu.Unlock()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := persist(&cfg); err != nil {
|
|
return err
|
|
}
|
|
a.configMu.Lock()
|
|
defer a.configMu.Unlock()
|
|
a.mu.Lock()
|
|
latest := *a.cfg
|
|
latest.ActiveRuntime, latest.QEMUASLR = cfg.ActiveRuntime, cfg.QEMUASLR
|
|
a.cfg = &latest
|
|
a.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// runtimeConfig includes the last selection made by either the GUI or CLI.
|
|
func (a *App) runtimeConfig() config.Config {
|
|
a.mu.Lock()
|
|
if a.cfg == nil {
|
|
a.mu.Unlock()
|
|
return config.Config{}
|
|
}
|
|
cfg := *a.cfg
|
|
usable := !a.needsSetup && !a.root.ReadOnly
|
|
a.mu.Unlock()
|
|
if usable {
|
|
if err := rt.RefreshSelection(&cfg); err != nil {
|
|
a.emitLog("runtime", "Read runtime selection: "+err.Error())
|
|
}
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// ReopenDataRoot opens the current data folder again: after another process
|
|
// finished upgrading it, after a failed upgrade, or with repair set, to rebuild
|
|
// a damaged version record on the user's request. When the folder becomes
|
|
// writable, the parts that were held back start.
|
|
func (a *App) ReopenDataRoot(repair bool) (dataroot.Status, error) {
|
|
a.selectionMu.Lock()
|
|
defer a.selectionMu.Unlock()
|
|
a.configMu.Lock()
|
|
defer a.configMu.Unlock()
|
|
a.mu.Lock()
|
|
if a.needsSetup {
|
|
st := a.root
|
|
a.mu.Unlock()
|
|
return st, nil
|
|
}
|
|
dir := a.cfg.DataRoot
|
|
wasReadOnly := a.root.ReadOnly
|
|
a.mu.Unlock()
|
|
|
|
var st dataroot.Status
|
|
if repair {
|
|
st = dataroot.Repair(dir, version.Version)
|
|
} else {
|
|
st = dataroot.Open(dir, version.Version)
|
|
}
|
|
a.mu.Lock()
|
|
a.root = st
|
|
a.syncClipboardStagingLocked()
|
|
a.mu.Unlock()
|
|
a.logDataRoot(st)
|
|
|
|
if wasReadOnly && st.Writable() {
|
|
a.resetDataRootCaches()
|
|
a.reloadPlugins()
|
|
a.wakePoll()
|
|
}
|
|
a.emitStateChanged()
|
|
return st, nil
|
|
}
|
|
|
|
// dataRootMessage is the localized explanation the read-only screen shows,
|
|
// without the error code and raw detail (shown separately).
|
|
func dataRootMessage(loc i18n.Locale, st dataroot.Status) string {
|
|
var ce *i18n.CodedError
|
|
if errors.As(dataroot.Error(loc, st), &ce) {
|
|
return ce.Summary(loc)
|
|
}
|
|
return ""
|
|
}
|