143 lines
3.8 KiB
Go
143 lines
3.8 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"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 {
|
|
if vms, err := a.polledVMs(context.Background()); err == nil {
|
|
for _, vm := range vms {
|
|
if vm.Name == name && vm.Dir != "" {
|
|
return vm.Dir
|
|
}
|
|
}
|
|
}
|
|
if d, err := a.GetVMDetail(name); err == nil {
|
|
return d.Directory
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// GetVMNote returns the optional single-line note saved for a VM.
|
|
func (a *App) GetVMNote(name string) string {
|
|
dir := a.vmDirectory(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(name, note string) error {
|
|
note = sanitizeVMNote(note)
|
|
dir := a.vmDirectory(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")
|
|
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
|
|
}
|