Files
Reims-Go/internal/app/dataroot_flow.go
T

123 lines
3.5 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"
"github.com/Nwflower/reims-go/internal/version"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// 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.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()
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)
}
// saveConfigLocked persists a.cfg; a.mu must be held. Before first-run setup
// a.cfg points at a suggested folder the user has not chosen, so changes stay
// in memory until CompleteSetup writes them into the chosen folder. A folder
// open read-only refuses the write.
func (a *App) saveConfigLocked() error {
if a.needsSetup {
return nil
}
if err := dataroot.Error(i18n.EffectiveLocale(a.cfg.Locale), a.root); err != nil {
return err
}
return config.Save("", a.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.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.mu.Unlock()
a.logDataRoot(st)
if wasReadOnly && st.Writable() {
a.resetDataRootCaches()
a.reloadPlugins()
a.wakePoll()
}
if a.ctx != nil {
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
}
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 ""
}