fix: 恢复移动硬盘更换盘符后的虚拟机注册路径(#197)
This commit is contained in:
5 files changed
+161
-55
No files matched your search
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"group": "fixes",
|
||||
"order": 0,
|
||||
"authors": ["Nwflower"],
|
||||
"zh": "移动硬盘换盘符后,即使旧版已保存新的数据目录,客户端启动或重新选择目录时仍会修复默认位置虚拟机的旧路径。([#181](https://github.com/Nwflower/Reims-Go/issues/181))",
|
||||
"en": "Repair stale paths for VMs in the default location when the client opens or reselects a data folder after an external drive changes letters, even if an older version already saved the new folder. ([#181](https://github.com/Nwflower/Reims-Go/issues/181))"
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package app
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
@@ -27,7 +28,9 @@ func (a *App) openDataRoot() dataroot.Status {
|
||||
a.syncClipboardStagingLocked()
|
||||
a.mu.Unlock()
|
||||
a.logDataRoot(st)
|
||||
if st.Writable() && explicitLaunch {
|
||||
// Older clients could save the new data folder while leaving its registry stale.
|
||||
// A registry explicitly shared outside the data folder still requires an explicit launch.
|
||||
if st.Writable() && (explicitLaunch || strings.TrimSpace(os.Getenv("REIMS_CTL_CONFIG_DIR")) == "") {
|
||||
a.relocateRegisteredVMs(dir, prevRoot)
|
||||
}
|
||||
return st
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/cli"
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
"github.com/Nwflower/reims-go/internal/datamigrate"
|
||||
"github.com/Nwflower/reims-go/internal/dataroot"
|
||||
@@ -181,22 +182,7 @@ func TestSetDataRoot_RelocatesMovedVMRegistry(t *testing.T) {
|
||||
if err := config.Save(filepath.Join(newRoot, "reims-go.json"), oldCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newVMDir := filepath.Join((&config.Config{DataRoot: newRoot}).XDGDataDir(), "reims-ctl", "vms", "macos-tahoe")
|
||||
if err := os.MkdirAll(newVMDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(newVMDir, "vm.toml"), []byte("disk = \"disk.qcow2\"\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldVMDir := filepath.Join((&config.Config{DataRoot: oldRoot}).XDGDataDir(), "reims-ctl", "vms", "macos-tahoe")
|
||||
regDir := filepath.Join((&config.Config{DataRoot: newRoot}).XDGConfigDir(), "reims-ctl")
|
||||
if err := os.MkdirAll(regDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vmsToml := "[[vm]]\nname = 'macos-tahoe'\ndir = '" + strings.ReplaceAll(oldVMDir, "'", "''") + "'\n"
|
||||
if err := os.WriteFile(filepath.Join(regDir, "vms.toml"), []byte(vmsToml), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, newVMDir := writeMovedVMRegistry(t, oldRoot, newRoot)
|
||||
|
||||
a := &App{
|
||||
cfg: &config.Config{DataRoot: oldRoot, Locale: "zh-CN"},
|
||||
@@ -206,15 +192,67 @@ func TestSetDataRoot_RelocatesMovedVMRegistry(t *testing.T) {
|
||||
if err := a.setDataRoot(newRoot, noopASLR); err != nil {
|
||||
t.Fatalf("setDataRoot: %v", err)
|
||||
}
|
||||
assertRegisteredVMDir(t, newRoot, newVMDir)
|
||||
}
|
||||
|
||||
vms, err := reims.ListRegisteredVMs(newRoot)
|
||||
func TestOpenDataRoot_RepairsPreviouslySelectedVMRegistry(t *testing.T) {
|
||||
t.Setenv("REIMS_CTL_CONFIG_DIR", "")
|
||||
t.Setenv("REIMS_GO_DATA_ROOT", "")
|
||||
base := t.TempDir()
|
||||
t.Setenv("REIMS_GO_LOCATION_FILE", filepath.Join(base, "location.json"))
|
||||
oldRoot := filepath.Join(base, "old-drive")
|
||||
newRoot := filepath.Join(base, "new-drive")
|
||||
oldVMDir, newVMDir := writeMovedVMRegistry(t, oldRoot, newRoot)
|
||||
|
||||
// An older release saved the new data root but left the old VM registration.
|
||||
cfg := &config.Config{DataRoot: newRoot}
|
||||
if err := config.Save("", cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := config.SaveLocation(newRoot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assertRegisteredVMDir(t, newRoot, oldVMDir)
|
||||
a, _ := New(nil, cli.WindowArgs{})
|
||||
if st := a.openDataRoot(); !st.Writable() {
|
||||
t.Fatalf("openDataRoot: %+v", st)
|
||||
}
|
||||
assertRegisteredVMDir(t, newRoot, newVMDir)
|
||||
}
|
||||
|
||||
func writeMovedVMRegistry(t *testing.T, oldRoot, newRoot string) (oldVMDir, newVMDir string) {
|
||||
t.Helper()
|
||||
newVMDir = filepath.Join(datamigrate.VMRoot(newRoot), "macos-tahoe")
|
||||
if err := os.MkdirAll(newVMDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(newVMDir, "vm.toml"), []byte("name = \"macos-tahoe\"\ndisk = \"disk.qcow2\"\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldVMDir = filepath.Join(datamigrate.VMRoot(oldRoot), "macos-tahoe")
|
||||
regDir := filepath.Join((&config.Config{DataRoot: newRoot}).XDGConfigDir(), "reims-ctl")
|
||||
if err := os.MkdirAll(regDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vmsToml := "[[vm]]\nname = 'macos-tahoe'\ndir = '" + strings.ReplaceAll(oldVMDir, "'", "''") + "'\n"
|
||||
if err := os.WriteFile(filepath.Join(regDir, "vms.toml"), []byte(vmsToml), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return oldVMDir, newVMDir
|
||||
}
|
||||
|
||||
func assertRegisteredVMDir(t *testing.T, root, want string) {
|
||||
t.Helper()
|
||||
vms, err := reims.ListRegisteredVMs(root)
|
||||
if err != nil {
|
||||
t.Fatalf("ListRegisteredVMs: %v", err)
|
||||
}
|
||||
if len(vms) != 1 {
|
||||
t.Fatalf("got %d vms, want 1", len(vms))
|
||||
}
|
||||
if !strings.EqualFold(filepath.Clean(vms[0].Directory), filepath.Clean(newVMDir)) {
|
||||
t.Fatalf("vm dir = %q, want %q", vms[0].Directory, newVMDir)
|
||||
if vms[0].Directory != want {
|
||||
t.Fatalf("vm dir = %q, want %q", vms[0].Directory, want)
|
||||
}
|
||||
}
|
||||
@@ -87,24 +87,37 @@ func relocateRegisteredVMsInDir(registryDir, dataRoot, previousDataRoot string)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var registry registeredVMRegistry
|
||||
// Preserve fields written by other versions of reims-ctl.
|
||||
var registry map[string]any
|
||||
if err := toml.Unmarshal(raw, ®istry); err != nil {
|
||||
return 0, fmt.Errorf("parse %s: %w", path, err)
|
||||
}
|
||||
entries, ok := registry["vm"].([]any)
|
||||
if !ok && registry["vm"] != nil {
|
||||
return 0, fmt.Errorf("invalid VM entries in %s", path)
|
||||
}
|
||||
|
||||
defaultVMsDir := filepath.Join((&config.Config{DataRoot: dataRoot}).XDGDataDir(), "reims-ctl", "vms")
|
||||
updated := 0
|
||||
for i := range registry.VMs {
|
||||
entry := ®istry.VMs[i]
|
||||
if entry.Name == "" || strings.TrimSpace(entry.Dir) == "" {
|
||||
for _, value := range entries {
|
||||
entry, ok := value.(map[string]any)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("invalid VM entry in %s", path)
|
||||
}
|
||||
name, nameOK := entry["name"].(string)
|
||||
dir, dirOK := entry["dir"].(string)
|
||||
if (!nameOK && entry["name"] != nil) || (!dirOK && entry["dir"] != nil) {
|
||||
return 0, fmt.Errorf("invalid VM name or directory in %s", path)
|
||||
}
|
||||
if name == "" || strings.TrimSpace(dir) == "" {
|
||||
continue
|
||||
}
|
||||
cleanDir := filepath.Clean(entry.Dir)
|
||||
nextDir, ok := relocatedVMDir(cleanDir, dataRoot, previousDataRoot, defaultVMsDir)
|
||||
cleanDir := filepath.Clean(dir)
|
||||
nextDir, ok := relocatedVMDir(name, cleanDir, dataRoot, previousDataRoot, defaultVMsDir)
|
||||
if !ok || samePathString(cleanDir, nextDir) {
|
||||
continue
|
||||
}
|
||||
entry.Dir = nextDir
|
||||
entry["dir"] = nextDir
|
||||
updated++
|
||||
}
|
||||
if updated == 0 {
|
||||
@@ -120,27 +133,30 @@ func relocateRegisteredVMsInDir(registryDir, dataRoot, previousDataRoot string)
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func relocatedVMDir(cleanDir, dataRoot, previousDataRoot, defaultVMsDir string) (string, bool) {
|
||||
func relocatedVMDir(name, cleanDir, dataRoot, previousDataRoot, defaultVMsDir string) (string, bool) {
|
||||
if previousDataRoot != "" && !samePathString(previousDataRoot, dataRoot) {
|
||||
rel, ok := relativeSubpath(previousDataRoot, cleanDir)
|
||||
if !ok {
|
||||
return "", false
|
||||
if rel, ok := relativeSubpath(previousDataRoot, cleanDir); ok {
|
||||
candidate := filepath.Join(dataRoot, rel)
|
||||
if hasVMConfig(candidate) || (!hasVMConfig(cleanDir) && isExistingDir(candidate)) {
|
||||
return candidate, true
|
||||
}
|
||||
}
|
||||
candidate := filepath.Join(dataRoot, rel)
|
||||
if hasVMConfig(candidate) || (!hasVMConfig(cleanDir) && isExistingDir(candidate)) {
|
||||
return candidate, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
if hasVMConfig(cleanDir) {
|
||||
return "", false
|
||||
}
|
||||
// The saved data root may already have been updated by an older client.
|
||||
// Fall back to the managed layout even if the previous root was unrelated.
|
||||
leaf, ok := managedDefaultVMLeaf(cleanDir)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(cleanDir, "vm.toml")); !errors.Is(err, os.ErrNotExist) {
|
||||
return "", false
|
||||
}
|
||||
candidate := filepath.Join(defaultVMsDir, leaf)
|
||||
if hasVMConfig(candidate) {
|
||||
var cfg struct {
|
||||
Name string `toml:"name"`
|
||||
}
|
||||
if raw, err := readVMMetadata(filepath.Join(candidate, "vm.toml")); err == nil &&
|
||||
toml.Unmarshal(raw, &cfg) == nil && cfg.Name == name {
|
||||
return candidate, true
|
||||
}
|
||||
return "", false
|
||||
@@ -150,6 +166,9 @@ func relocatedVMDir(cleanDir, dataRoot, previousDataRoot, defaultVMsDir string)
|
||||
// the managed default layout (<root>/data/reims-ctl/vms/<leaf>), so an
|
||||
// unmounted external or custom VM path is never mistaken for a moved default VM.
|
||||
func managedDefaultVMLeaf(cleanDir string) (string, bool) {
|
||||
if !filepath.IsAbs(cleanDir) {
|
||||
return "", false
|
||||
}
|
||||
leaf := strings.TrimSpace(filepath.Base(cleanDir))
|
||||
if leaf == "" || leaf == "." || leaf == ".." || leaf == string(filepath.Separator) {
|
||||
return "", false
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package reims
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -14,7 +15,8 @@ func writeTestVMConfig(t *testing.T, dir string) {
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatalf("mkdir %s: %v", dir, err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "vm.toml"), []byte("disk = \"disk.qcow2\"\n"), 0o644); err != nil {
|
||||
contents := fmt.Sprintf("name = %q\ndisk = \"disk.qcow2\"\n", filepath.Base(dir))
|
||||
if err := os.WriteFile(filepath.Join(dir, "vm.toml"), []byte(contents), 0o644); err != nil {
|
||||
t.Fatalf("write vm.toml: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -85,7 +87,7 @@ func TestRelocateRegisteredVMs_WithPreviousRootAndExternalVM(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelocateRegisteredVMs_FallbackWithoutPreviousRoot(t *testing.T) {
|
||||
func TestRelocateRegisteredVMs_FallbackWithUnknownSourceRoot(t *testing.T) {
|
||||
t.Setenv("REIMS_CTL_CONFIG_DIR", "")
|
||||
base := t.TempDir()
|
||||
missingOldRoot := filepath.Join(base, "gone-data")
|
||||
@@ -95,24 +97,61 @@ func TestRelocateRegisteredVMs_FallbackWithoutPreviousRoot(t *testing.T) {
|
||||
writeTestVMConfig(t, newVMDir)
|
||||
|
||||
oldVMDir := filepath.Join((&config.Config{DataRoot: missingOldRoot}).XDGDataDir(), "reims-ctl", "vms", "mac-dev")
|
||||
writeTestRegistry(t, newRoot, []registeredVMEntry{
|
||||
{Name: "mac-dev", Dir: oldVMDir},
|
||||
})
|
||||
for name, previousRoot := range map[string]string{
|
||||
"unrecorded": "",
|
||||
"overwritten": filepath.Join(base, "unrelated-data"),
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
writeTestRegistry(t, newRoot, []registeredVMEntry{{Name: "mac-dev", Dir: oldVMDir}})
|
||||
updated, err := RelocateRegisteredVMs(newRoot, previousRoot)
|
||||
if err != nil || updated != 1 {
|
||||
t.Fatalf("RelocateRegisteredVMs = %d, %v, want 1, nil", updated, err)
|
||||
}
|
||||
vms, err := ListRegisteredVMs(newRoot)
|
||||
if err != nil || len(vms) != 1 || !samePathString(vms[0].Directory, newVMDir) {
|
||||
t.Fatalf("ListRegisteredVMs = %+v, %v, want dir %q", vms, err, newVMDir)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelocateRegisteredVMs_FallbackChecksVMNameAndRetainsMetadata(t *testing.T) {
|
||||
t.Setenv("REIMS_CTL_CONFIG_DIR", "")
|
||||
base := t.TempDir()
|
||||
oldRoot := filepath.Join(base, "old-data")
|
||||
newRoot := filepath.Join(base, "new-data")
|
||||
oldVMs := filepath.Join((&config.Config{DataRoot: oldRoot}).XDGDataDir(), "reims-ctl", "vms")
|
||||
newVMs := filepath.Join((&config.Config{DataRoot: newRoot}).XDGDataDir(), "reims-ctl", "vms")
|
||||
oldMoved, oldOther := filepath.Join(oldVMs, "moved"), filepath.Join(oldVMs, "other")
|
||||
newMoved, newOther := filepath.Join(newVMs, "moved"), filepath.Join(newVMs, "other")
|
||||
writeTestVMConfig(t, newMoved)
|
||||
writeTestVMConfig(t, newOther)
|
||||
path := writeTestRegistry(t, newRoot, nil)
|
||||
raw := fmt.Sprintf("schema = 2\n[[vm]]\nname = 'moved'\ndir = '%s'\nlabel = 'retain'\n[[vm]]\nname = 'external'\ndir = '%s'\n", oldMoved, oldOther)
|
||||
if err := os.WriteFile(path, []byte(raw), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
updated, err := RelocateRegisteredVMs(newRoot, "")
|
||||
if err != nil {
|
||||
t.Fatalf("RelocateRegisteredVMs: %v", err)
|
||||
if err != nil || updated != 1 {
|
||||
t.Fatalf("RelocateRegisteredVMs = %d, %v, want 1, nil", updated, err)
|
||||
}
|
||||
if updated != 1 {
|
||||
t.Fatalf("updated = %d, want 1", updated)
|
||||
}
|
||||
|
||||
vms, err := ListRegisteredVMs(newRoot)
|
||||
if err != nil {
|
||||
t.Fatalf("ListRegisteredVMs: %v", err)
|
||||
if err != nil || len(vms) != 2 || vms[0].Directory != newMoved || vms[1].Directory != oldOther {
|
||||
t.Fatalf("ListRegisteredVMs = %+v, %v, want moved VM and unchanged external VM", vms, err)
|
||||
}
|
||||
if len(vms) != 1 || !samePathString(vms[0].Directory, newVMDir) {
|
||||
t.Fatalf("vms = %+v, want dir %q", vms, newVMDir)
|
||||
contents, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var metadata struct {
|
||||
Schema int `toml:"schema"`
|
||||
VMs []struct {
|
||||
Label string `toml:"label"`
|
||||
} `toml:"vm"`
|
||||
}
|
||||
if err := toml.Unmarshal(contents, &metadata); err != nil || metadata.Schema != 2 || len(metadata.VMs) != 2 || metadata.VMs[0].Label != "retain" {
|
||||
t.Fatalf("registry lost metadata: %s (%v)", contents, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user