Events to the frontend go through App.emit, and a test keeps their names in step with wails.ts. QEMU argument splitting, hidden console processes, the macOS release record, the running check and a few smaller helpers now exist once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
205 lines
5.8 KiB
Go
205 lines
5.8 KiB
Go
package app
|
|
|
|
// First launch: the user picks the data folder before anything else is written
|
|
// (runtimes, VMs, installers and caches all live under it).
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/dataroot"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/version"
|
|
)
|
|
|
|
// DataRootChoice is a suggested data folder on one drive.
|
|
type DataRootChoice struct {
|
|
Path string `json:"path"`
|
|
FreeGB float64 `json:"free_gb"`
|
|
System bool `json:"system"`
|
|
External bool `json:"external"` // USB disk: may be unplugged while a VM runs
|
|
Recommended bool `json:"recommended"`
|
|
}
|
|
|
|
// DataRootCheck describes a folder the user is about to choose.
|
|
type DataRootCheck struct {
|
|
Path string `json:"path"`
|
|
FreeGB float64 `json:"free_gb"`
|
|
System bool `json:"system"`
|
|
External bool `json:"external"`
|
|
HasConfig bool `json:"has_config"` // an earlier reims-go data folder: its VMs are kept
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// SuggestDataRoots offers <drive>\Data\reims-go on each fixed drive, the
|
|
// non-system drive with the most free space first.
|
|
func (a *App) SuggestDataRoots() []DataRootChoice {
|
|
var out []DataRootChoice
|
|
for _, d := range host.ListDrives() {
|
|
out = append(out, DataRootChoice{
|
|
Path: filepath.Join(d.Root, "Data", "reims-go"),
|
|
FreeGB: d.FreeGB,
|
|
System: d.System,
|
|
External: d.External,
|
|
})
|
|
}
|
|
// Internal non-system drives first, then the system drive, USB disks last;
|
|
// most free space first within each. A USB disk is never recommended.
|
|
rank := func(c DataRootChoice) int {
|
|
switch {
|
|
case c.External:
|
|
return 2
|
|
case c.System:
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool {
|
|
if rank(out[i]) != rank(out[j]) {
|
|
return rank(out[i]) < rank(out[j])
|
|
}
|
|
return out[i].FreeGB > out[j].FreeGB
|
|
})
|
|
if len(out) > 0 && !out[0].External {
|
|
out[0].Recommended = true
|
|
}
|
|
return out
|
|
}
|
|
|
|
// CheckDataRoot reports free space and whether dir already holds reims-go data.
|
|
func (a *App) CheckDataRoot(dir string) DataRootCheck {
|
|
a.mu.Lock()
|
|
loc := a.cfg.Locale
|
|
a.mu.Unlock()
|
|
dir = strings.TrimSpace(dir)
|
|
c := DataRootCheck{Path: dir}
|
|
if err := validateDataRoot(dir, loc); err != nil {
|
|
c.Error = err.Error()
|
|
return c
|
|
}
|
|
c.FreeGB, _ = host.FreeGB(existingAncestor(dir))
|
|
c.System = isSystemVolume(dir)
|
|
vol := strings.ToUpper(filepath.VolumeName(dir))
|
|
for _, d := range host.ListDrives() {
|
|
if strings.EqualFold(d.Root[:2], vol) {
|
|
c.External = d.External
|
|
}
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "reims-go.json")); err == nil {
|
|
c.HasConfig = true
|
|
}
|
|
return c
|
|
}
|
|
|
|
// CompleteSetup makes dir the data folder: creates its layout, keeps an
|
|
// existing reims-go config there (its runtimes and VMs), records the choice for
|
|
// the next launch and leaves first-launch setup.
|
|
func (a *App) CompleteSetup(dir string) error {
|
|
a.mu.Lock()
|
|
locale := a.cfg.Locale
|
|
a.mu.Unlock()
|
|
dir = filepath.Clean(strings.TrimSpace(dir))
|
|
if err := validateDataRoot(dir, locale); err != nil {
|
|
return err
|
|
}
|
|
// Check the folder before writing anything into it: one written by a
|
|
// newer release, or with a damaged version record, is refused untouched.
|
|
if st := dataroot.Open(dir, version.Version); st.ReadOnly {
|
|
return dataroot.Error(i18n.EffectiveLocale(locale), st)
|
|
}
|
|
|
|
cfgPath := filepath.Join(dir, "reims-go.json")
|
|
cfg, err := config.Load(cfgPath)
|
|
if err != nil {
|
|
return fmt.Errorf("read %s: %w", cfgPath, err)
|
|
}
|
|
cfg.DataRoot = dir
|
|
if _, statErr := os.Stat(cfgPath); statErr != nil {
|
|
cfg.Locale = locale // a new folder keeps the language picked on this screen
|
|
}
|
|
if err := cfg.EnsureDirs(); err != nil {
|
|
return err
|
|
}
|
|
if err := writable(dir); err != nil {
|
|
return err
|
|
}
|
|
if err := config.Save(cfgPath, cfg); err != nil {
|
|
return err
|
|
}
|
|
if err := config.SaveLocation(dir); err != nil {
|
|
return err
|
|
}
|
|
// A folder created just now gets its version stamp here.
|
|
st := dataroot.Open(dir, version.Version)
|
|
a.mu.Lock()
|
|
a.cfg = cfg
|
|
a.needsSetup = false
|
|
a.root = st
|
|
a.syncClipboardStagingLocked()
|
|
a.mu.Unlock()
|
|
a.logDataRoot(st)
|
|
// Nothing touching the data folder ran before setup (startup), so plugins
|
|
// start now whichever folder was chosen.
|
|
a.resetDataRootCaches()
|
|
a.reloadPlugins()
|
|
a.wakePoll()
|
|
a.emitStateChanged()
|
|
return nil
|
|
}
|
|
|
|
func validateDataRoot(dir string, loc i18n.Locale) error {
|
|
if dir == "" {
|
|
return errors.New(i18n.T(loc, "setup.err.empty"))
|
|
}
|
|
if !filepath.IsAbs(dir) {
|
|
return errors.New(i18n.T(loc, "setup.err.not_abs", dir))
|
|
}
|
|
if filepath.Clean(dir) == filepath.VolumeName(dir)+string(filepath.Separator) || filepath.Dir(dir) == dir {
|
|
return errors.New(i18n.T(loc, "setup.err.whole_drive"))
|
|
}
|
|
if strings.ContainsAny(dir, ",") {
|
|
// reims-ctl passes kext paths to reims-util as "PATH,MIN,MAX" and refuses ','
|
|
// (upstream 3731afd reims_util.rs:308-317); the data folder holds them.
|
|
return errors.New(i18n.T(loc, "setup.err.comma"))
|
|
}
|
|
if info, err := os.Stat(dir); err == nil && !info.IsDir() {
|
|
return errors.New(i18n.T(loc, "setup.err.is_file", dir))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// existingAncestor is dir or its nearest existing parent (for free-space checks
|
|
// before the folder exists).
|
|
func existingAncestor(dir string) string {
|
|
for d := dir; ; d = filepath.Dir(d) {
|
|
if _, err := os.Stat(d); err == nil {
|
|
return d
|
|
}
|
|
if filepath.Dir(d) == d {
|
|
return d
|
|
}
|
|
}
|
|
}
|
|
|
|
func isSystemVolume(dir string) bool {
|
|
sys := os.Getenv("SystemDrive")
|
|
vol := filepath.VolumeName(dir)
|
|
return sys != "" && vol != "" && strings.EqualFold(sys, vol)
|
|
}
|
|
|
|
func writable(dir string) error {
|
|
f, err := os.CreateTemp(dir, ".write-test-*")
|
|
if err != nil {
|
|
return fmt.Errorf("can't write to %s: %w", dir, err)
|
|
}
|
|
name := f.Name()
|
|
f.Close()
|
|
return os.Remove(name)
|
|
}
|