Files
Reims-Go/internal/driver/reims/helper_args.go
T
Nwflower faa2b71202 feat(guest-tools): add token-authenticated clipboard & shared folder bridge and update roadmap
- Pure-Go Win32 clipboard + localhost HTTP bridge (127.0.0.1:19847) with per-VM 256-bit random Bearer token and jailed shared directory (<DataRoot>/shared/<vm>)
- Pure-Go HFS+ REIMSTOOLS virtual USB disk with stock-macOS POSIX LaunchAgent installer (no Xcode or Python needed) for two-way UTF-8 clipboard and ~/Desktop/ReimsShare sync
- VMPage Guest Tools card with live connection badge, Open shared folder, Attach/Eject tools disk, and one-click QMP install command
- Update docs/roadmap.md with v0.3/v0.4 competitor-aligned follow-up tasks
2026-11-28 08:41:09 -07:00

126 lines
3.9 KiB
Go

package reims
import (
"fmt"
"strings"
)
// IsOfflineInstallerPath reports whether an attached installer path is a
// reims-util offline installer image.
func IsOfflineInstallerPath(path string) bool {
return strings.Contains(strings.ToLower(path), "offline-installer")
}
// StripOfflineHelperArgs removes any reims-go managed offline helper USB disk
// arguments from QEMU extra-args while leaving all other arguments intact.
func StripOfflineHelperArgs(existing string) string {
tokens := splitShellTokens(existing)
cleaned := make([]string, 0, len(tokens))
for i := 0; i < len(tokens); i++ {
tok := tokens[i]
if tok == "-drive" && i+1 < len(tokens) && strings.Contains(tokens[i+1], "id=reimshelp,") {
i++
continue
}
if tok == "-device" && i+1 < len(tokens) && strings.Contains(tokens[i+1], "id=reimshelp-dev,") {
i++
continue
}
cleaned = append(cleaned, tok)
}
return strings.Join(cleaned, " ")
}
// MergeOfflineHelperArgs idempotently attaches helperImgPath as a read-only USB
// storage volume on xhci.0 (appearing as /Volumes/REIMSHELP in macOS Recovery).
func MergeOfflineHelperArgs(existing, helperImgPath string) string {
base := StripOfflineHelperArgs(existing)
// QEMU drive option syntax escapes commas by doubling them, and shlex
// preserves single-quoted paths containing spaces or backslashes.
escapedPath := strings.ReplaceAll(helperImgPath, ",", ",,")
escapedPath = strings.ReplaceAll(escapedPath, `\`, `/`)
driveVal := fmt.Sprintf("id=reimshelp,if=none,format=raw,readonly=on,file=%s", escapedPath)
if strings.ContainsAny(driveVal, " \t") {
driveVal = `"` + strings.ReplaceAll(driveVal, `"`, `\"`) + `"`
}
helperArgs := fmt.Sprintf("-drive %s -device usb-storage,id=reimshelp-dev,drive=reimshelp,bus=xhci.0", driveVal)
if base == "" {
return helperArgs
}
return base + " " + helperArgs
}
// HasGuestToolsArgs reports whether the REIMSTOOLS virtual USB disk is attached.
func HasGuestToolsArgs(existing string) bool {
return strings.Contains(existing, "id=reimstools,")
}
// StripGuestToolsArgs removes any reims-go managed REIMSTOOLS USB disk args.
func StripGuestToolsArgs(existing string) string {
tokens := splitShellTokens(existing)
cleaned := make([]string, 0, len(tokens))
for i := 0; i < len(tokens); i++ {
tok := tokens[i]
if tok == "-drive" && i+1 < len(tokens) && strings.Contains(tokens[i+1], "id=reimstools,") {
i++
continue
}
if tok == "-device" && i+1 < len(tokens) && strings.Contains(tokens[i+1], "id=reimstools-dev,") {
i++
continue
}
cleaned = append(cleaned, tok)
}
return strings.Join(cleaned, " ")
}
// MergeGuestToolsArgs idempotently attaches toolsImgPath as a read-only USB
// storage volume on xhci.0 (appearing as /Volumes/REIMSTOOLS in macOS).
func MergeGuestToolsArgs(existing, toolsImgPath string) string {
base := StripGuestToolsArgs(existing)
escapedPath := strings.ReplaceAll(toolsImgPath, ",", ",,")
escapedPath = strings.ReplaceAll(escapedPath, `\`, `/`)
driveVal := fmt.Sprintf("id=reimstools,if=none,format=raw,readonly=on,file=%s", escapedPath)
if strings.ContainsAny(driveVal, " \t") {
driveVal = `"` + strings.ReplaceAll(driveVal, `"`, `\"`) + `"`
}
toolsArgs := fmt.Sprintf("-drive %s -device usb-storage,id=reimstools-dev,drive=reimstools,bus=xhci.0", driveVal)
if base == "" {
return toolsArgs
}
return base + " " + toolsArgs
}
func splitShellTokens(s string) []string {
var out []string
var cur strings.Builder
var quote byte
for i := 0; i < len(s); i++ {
c := s[i]
if quote != 0 {
cur.WriteByte(c)
if c == quote {
quote = 0
}
continue
}
if c == '\'' || c == '"' {
quote = c
cur.WriteByte(c)
continue
}
if c == ' ' || c == '\t' || c == '\n' || c == '\r' {
if cur.Len() > 0 {
out = append(out, cur.String())
cur.Reset()
}
continue
}
cur.WriteByte(c)
}
if cur.Len() > 0 {
out = append(out, cur.String())
}
return out
}