Files
magpie/internal/cli/data_cli.go
T
yetone bb4dd86790 appdir: magpie's files and cache move to another disk, a link left where they were (CHENGJIAHEGOXIS and buhaodeshiyehao on X)
CHENGJIAHEGOXIS asked to keep magpie's data and cache on an external SSD
instead of the Mac's own disk. buhaodeshiyehao asked to keep magpie's
personal data on another disk. Settings › Sync now has a Data folder row.
It shows where magpie's files are, with Show, Move… and Move back.
`magpie data` does the same from a terminal.

- What moves: appdir.Config() and Cache() only, into <picked>/magpie/config
  and /cache. The agents' own folders and session histories stay where
  each agent reads them. A link is left at each old path, so every path
  the agents were given still works. On Windows, when symlinks need a
  privilege (#973), the link is a junction; library's junction code moved
  into appdir for this.
- Never under a running magpie. Move… explains first in the app's own
  dialog, then asks for the move (move-to.json). The app restarts itself
  through the update path's relaunch and quits, and the new process makes
  the move in cli.Main before it reads anything, once. A failed move is
  kept in move-result.json and shown under the row. When another magpie
  serves the gateway, or for magpie web, the app doesn't restart, and the
  row says the move waits, with Cancel.
- Refused before anything quits: a relative or missing folder, a
  magpie folder there with files in it, or a folder inside magpie's own.
- One disk: a rename. Across disks: a copy. The old folder is put aside
  until the link is made, and a failed copy leaves everything as it was.
- Disk unplugged: magpie stops with an error (and a native alert from the
  app, as #989) instead of starting empty and writing over the link.
- Usage history in auto backups is left for a follow-up: auto backups are
  plain JSON kept 7 times, and usage is large and synced separately.

Docs: sync-backup.md has a "Data folder on another disk" section, and
docs/start's #data-folder (en, zh, ja) describes Move….

Run (mote, at this tree on 0791c91c5):
- gofmt -l: clean
- go vet: darwin, linux -tags nogui and windows, for appdir, cli, gui and
  library: ok
- go test -count=1 ./internal/appdir ./internal/cli ./internal/library
  ./internal/gui: ok. cli was red first, on TestEveryRouteHasACommandOrIsNamed;
  the routes are now mapped to magpie data.
- Windows (xiaomi-gpu): appdir's Move tests, including TestMoveByJunction
  with symlink forced to fail with ERROR_PRIVILEGE_NOT_HELD, and
  TestDataFolderMoveRestarts: ok. TestFoldersJoinedOnce is red there,
  which is #4846 (appdir.go is untouched).
- GUI, chromium + webkit: data-folder (5 languages x 1100/420px),
  auto-backup, sync-*, settings-sections, segs-press-redraw, old-webkit,
  github-sync, s3-sync, skill-how, usage-computers,
  webdav-settings-refresh, i18n-keys and i18n-en-no-cjk: ok except
  phone-no-sideways de 375 (#5869) and s3-sync webkit en (#3413/#5872,
  red 3 of 3 on this tree too), both already red on main.
- Linux (WSL magpie-desktop): appdir -run Move and
  TestDataFolderMoveRestarts (nogui): ok.

Again after rebasing on 0a44c1c84 (mote): gofmt, the three vets, go test
./internal/appdir ./internal/cli ./internal/gui, and the GUI files
data-folder, i18n-keys, i18n-en-no-cjk, auto-backup and settings-sections
in both engines (60 of 60): ok. Pushed on 5426c43cc, which touches only
the gateway's dsh_compact and the routing plugin, none of these files.

Broken by hand (each went red):
- no removal of a half copy: TestMoveCopyFailsLeavesAll "a half copy
  is left on the disk"
- no Unreachable check for a gone disk: TestMovedDiskGoneIsUnreachable
- restarting with another magpie serving: TestDataFolderMoveRestarts
  "2 restarts"

Not tried: a real app restart making a real move on a real external disk
(the restart is the update path's relaunch, faked in the test); a real
unplug.
2026-11-10 13:46:13 +08:00

137 lines
4.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cli
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/codexrelay"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/update"
)
// magpie data: where magpie keeps its files, and moving them to another
// disk (CHENGJIAHEGOXIS and buhaodeshiyehao on X; appdir/move.go). A move
// is made at once when no magpie serves the gateway, and otherwise at the
// next start of the app, which Settings › Sync's Data folder restarts
// it for.
func dataCmd(args []string) error {
if len(args) == 0 || args[0] == "where" {
p := appdir.Where()
fmt.Println("files:", p.Config)
if p.ConfigAt != "" {
fmt.Println(" →", p.ConfigAt)
}
fmt.Println("cache:", p.Cache)
if p.CacheAt != "" {
fmt.Println(" →", p.CacheAt)
}
if p.Portable {
fmt.Println("portable: they move with magpie's data folder")
}
switch p.Pending {
case "":
case "home":
fmt.Println("moving back to the default folders when magpie next starts")
default:
fmt.Println("moving to", p.Pending, "when magpie next starts")
}
if p.Error != "" {
fmt.Println("the last move wasn't made:", p.Error)
}
return appdir.Unreachable()
}
switch args[0] {
case "move":
if len(args) != 2 || strings.HasPrefix(args[1], "-") && args[1] != "--home" {
return errors.New("usage: magpie data move <folder> | --home")
}
dir := args[1]
if dir == "--home" {
dir = ""
} else if a, err := filepath.Abs(dir); err == nil {
dir = a
}
if gatewayUp() {
root, err := appdir.AskMove(dir)
if err != nil {
return err
}
fmt.Printf("magpie is running: its files move to %s when it next starts (quit it and open it again)\n", homeOr(root))
return nil
}
root, err := appdir.Target(dir)
if err != nil {
return err
}
appdir.CancelMove()
if err := appdir.Move(root); err != nil {
return err
}
fmt.Println(green.Render("●"), "magpie's files are in", homeOr(root), "now")
return nil
case "cancel":
return appdir.CancelMove()
case "-h", "--help", "help":
fmt.Print(dataUsage)
return nil
}
return fmt.Errorf("unknown data command %q\n%s", args[0], dataUsage)
}
const dataUsage = `magpie data where magpie keeps its files and its cache
magpie data move <folder> move them into <folder>/magpie (another disk), leaving a link where they were
magpie data move --home move them back
magpie data cancel forget a move asked for while magpie was running
`
func homeOr(root string) string {
if root == "" {
return appdir.Where().Config
}
return root
}
// gatewayUp says whether a magpie serves the gateway: one running holds
// files in the folders a move would take away.
var gatewayUp = func() bool {
codexrelay.Leave() // what a quitting magpie left at the port lets go of it
return gateway.ServedBy().Running
}
// movePending makes the move Settings asked for, as the app starts again
// for it: after the magpie it replaces has exited, and before anything is
// read or written. With another magpie still serving the gateway (magpie
// serve, a launch agent) the move waits for a start without one.
func movePending() {
if !appdir.MoveAsked() {
return
}
update.AwaitPredecessor()
if gatewayUp() {
fmt.Fprintln(os.Stderr, "magpie: its folders move when no other magpie is running")
return
}
if _, err := appdir.MovePending(); err != nil {
fmt.Fprintln(os.Stderr, "magpie: its folders weren't moved:", err)
}
}
// unreachableStop ends magpie when its folders were moved to a disk that
// isn't connected, saying so in a box for the app, which has no terminal.
// magpie data still answers, to say where they are.
func unreachableStop(args []string) {
err := appdir.Unreachable()
if err == nil || len(args) > 0 && args[0] == "data" {
return
}
fmt.Fprintln(os.Stderr, "magpie:", err)
if hasGUI && (len(args) == 0 || servesPages(args) && args[0] != "serve" && args[0] != "tui" && args[0] != "web") {
alert("magpie", err.Error())
}
os.Exit(1)
}