Files
Reims-Go/internal/app/extras_flow.go
T
千川白浪 e0ff794b0e feat: add experimental macOS desktop support on Apple Silicon (#207)
* feat: add experimental Apple Silicon support with Nivfold

Integrate the standalone Nivfold protocol 1 runner; tested against Nivfold f436fb6. The Windows Reims vGPU runtime remains independent.

* fix: open QQ invites and VM SSH on macOS

* fix: move VM, View and Help to the macOS menu bar

* feat(macos): add resumable Apple restore image downloads

Expose the compatible restore image in the VM wizard and Images page, validate downloads before caching, and fix macOS memory reporting and plugin SVG rendering.

* fix(macos): localize the native application menu

* fix(macos): address native desktop review findings
2026-12-06 00:47:07 +08:00

146 lines
4.3 KiB
Go

package app
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"github.com/Nwflower/reims-go/internal/macos/guest"
"github.com/Nwflower/reims-go/internal/macos/installer"
)
const vmNoteFileName = ".reims-go-note"
// VMStorageItem is one VM's disk footprint under DataRoot.
type VMStorageItem struct {
Name string `json:"name"`
Bytes int64 `json:"bytes"`
}
// StorageBreakdown summarizes how DataRoot space is distributed across VM files,
// cached macOS installers, and all other Reims-Go data.
type StorageBreakdown struct {
VMsBytes int64 `json:"vms_bytes"`
InstallersBytes int64 `json:"installers_bytes"`
RuntimesBytes int64 `json:"runtimes_bytes"`
TotalBytes int64 `json:"total_bytes"`
VMs []VMStorageItem `json:"vms"`
}
// vmDirectory finds a VM's folder from the list pollVMs already holds, so
// per-VM file reads (the note) don't each run `reims-ctl show`; it falls
// back to show when the list has no entry or no folder for it.
func (a *App) vmDirectory(name string) string { return a.vmDirectoryForDriver("", name) }
// vmDirectoryForDriver does the same for one driver's VM, so two VMs with the
// same name under different drivers keep separate folders.
func (a *App) vmDirectoryForDriver(driverID, name string) string {
if vm, ok := a.polledVMForDriver(driverID, name); ok && vm.Dir != "" {
return vm.Dir
}
if info, err := a.resolveVMInfo(driverID, name); err == nil {
return info.Directory
}
return ""
}
// GetVMNote returns the optional single-line note saved for a VM.
func (a *App) GetVMNote(driverID, name string) string {
dir := a.vmDirectoryForDriver(driverID, name)
if dir == "" {
return ""
}
b, err := os.ReadFile(filepath.Join(dir, vmNoteFileName))
if err != nil {
return ""
}
return strings.TrimSpace(string(b))
}
// SetVMNote stores a short single-line note next to the VM directory.
func (a *App) SetVMNote(driverID, name, note string) error {
note = sanitizeVMNote(note)
dir := a.vmDirectoryForDriver(driverID, name)
if dir == "" {
return fmt.Errorf("VM directory not found")
}
p := filepath.Join(dir, vmNoteFileName)
if note == "" {
_ = os.Remove(p)
return nil
}
return os.WriteFile(p, []byte(note+"\n"), 0o644)
}
func sanitizeVMNote(note string) string {
note = strings.ReplaceAll(note, "\r", " ")
note = strings.ReplaceAll(note, "\n", " ")
note = strings.TrimSpace(note)
runes := []rune(note)
if len(runes) > 120 {
return string(runes[:120])
}
return note
}
// GetStorageBreakdown computes disk usage across DataRoot subdirectories.
func (a *App) GetStorageBreakdown() StorageBreakdown {
a.mu.Lock()
cfgCopy := *a.cfg
a.mu.Unlock()
vmsDir := filepath.Join(cfgCopy.XDGDataDir(), "reims-ctl", "vms")
if guest.UsesAppleVirtualization() {
return computeStorageBreakdown(cfgCopy.DataRoot, guest.AppleVMRoot(cfgCopy.DataRoot), installer.RestoreDir(&cfgCopy), "")
}
return computeStorageBreakdown(cfgCopy.DataRoot, vmsDir, installer.Dir(&cfgCopy), cfgCopy.ImagesDir())
}
func computeStorageBreakdown(dataRoot, vmsDir, installersDir, imagesDir string) StorageBreakdown {
var out StorageBreakdown
if entries, err := os.ReadDir(vmsDir); err == nil {
for _, e := range entries {
if !e.IsDir() {
continue
}
sz := dirSizeBytes(filepath.Join(vmsDir, e.Name()))
out.VMs = append(out.VMs, VMStorageItem{Name: e.Name(), Bytes: sz})
out.VMsBytes += sz
}
}
if imgBytes := dirSizeBytes(imagesDir); imgBytes > 0 {
out.VMsBytes += imgBytes
}
sort.Slice(out.VMs, func(i, j int) bool {
return out.VMs[i].Bytes > out.VMs[j].Bytes
})
out.InstallersBytes = dirSizeBytes(installersDir)
// Keep the existing JSON field for frontend compatibility, but attribute all
// remaining DataRoot usage (runtimes, config, cache, releases, etc.) to Reims-Go.
out.RuntimesBytes = dirSizeBytes(dataRoot) - out.VMsBytes - out.InstallersBytes
if out.RuntimesBytes < 0 {
out.RuntimesBytes = 0
}
out.TotalBytes = out.VMsBytes + out.InstallersBytes + out.RuntimesBytes
return out
}
func dirSizeBytes(root string) int64 {
if root == "" {
return 0
}
var total int64
_ = filepath.WalkDir(root, func(_ string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return nil
}
if info, err := d.Info(); err == nil {
total += info.Size()
}
return nil
})
return total
}