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/host" "github.com/Nwflower/reims-go/internal/i18n" wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime" ) // 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 \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 } 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.mu.Lock() prevRoot := "" if a.cfg != nil { prevRoot = a.cfg.DataRoot } a.cfg = cfg a.needsSetup = false a.mu.Unlock() if filepath.Clean(prevRoot) != filepath.Clean(dir) { a.resetDataRootCaches() a.reloadPlugins() } a.wakePoll() if a.ctx != nil { wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true) } 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) }