From 8ca5ae24247c56989933a70211a6ef907dd1d654 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Wed, 23 Sep 2026 14:32:39 +0800 Subject: [PATCH] fix(adopt): flatten a backup's source path on Windows too The source path is squashed into one file name so a backup says whose it is. It only replaced `/`. On Windows the separator is `\`, so the name kept its backslashes -- and `Path::join` given an absolute path discards the base entirely. The "file inside the backup directory" quietly became the user's own configuration file. What happened next was `create_new`, reporting that the file already exists. That complaint is the thing that saved us: without it the backup step would have written backup text over the user's configuration, and not damaging other people's files is the entire reason this module exists. The colon goes too. The one in `C:` is not legal in a Windows file name. The test runs on macOS and fails against the old implementation, which is the part worth keeping: a `\` separator survives string replacement of `/` on any platform, so this never needed a Windows machine to be caught. It was caught by one only because nothing had asked. Found by the Windows CI job in #119. Co-Authored-By: Claude Opus 5 --- crates/tw-adopt/src/foreign.rs | 57 +++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/crates/tw-adopt/src/foreign.rs b/crates/tw-adopt/src/foreign.rs index 6cb32949..c9a8edff 100644 --- a/crates/tw-adopt/src/foreign.rs +++ b/crates/tw-adopt/src/foreign.rs @@ -224,6 +224,28 @@ pub fn backup_root() -> PathBuf { /// 同一毫秒里最多几份备份。补零宽度跟着它走,超了排序就不对了。 const BACKUP_SEQ_MAX: u32 = 9999; +/// 把来源路径压成**一个**文件名,放进备份目录里。 +/// +/// 一眼能看出这份备份是谁的:`/Users/x/.claude/settings.json` 变成 +/// `Users%x%.claude%settings.json`。 +/// +/// # 两种分隔符和那个冒号 +/// +/// 只处理 `/` 的话,Windows 上 `C:\Users\x\c.json` 原样留着反斜杠 —— 而 +/// **`Path::join` 碰上一个绝对路径会把前面整个丢掉**,于是「备份目录里的 +/// 那个文件」悄悄变回了用户原本那个配置文件。 +/// +/// 那条路上接着是 `create_new`,它报「文件已存在」——**那一声是它救了一命**: +/// 没有它,备份这一步会拿备份内容去覆盖用户自己的配置,而这个模块存在的 +/// 全部理由就是不弄坏别人的文件。 +/// +/// 冒号也要换掉:`C:` 里那个在 Windows 的文件名中非法。 +fn flat_name(real: &Path) -> String { + real.to_string_lossy() + .trim_start_matches(['/', '\\']) + .replace(['/', '\\', ':'], "%") +} + fn backup_to(root: &Path, real: &Path, text: &str) -> Result { backup_at(root, real, text, now_ms()) } @@ -231,10 +253,7 @@ fn backup_to(root: &Path, real: &Path, text: &str) -> Result Result { // 目录名里带上来源路径的形状,一眼能看出这是谁的备份 - let flat = real - .to_string_lossy() - .trim_start_matches('/') - .replace('/', "%"); + let flat = flat_name(real); std::fs::create_dir_all(root).map_err(|source| ForeignError::Write { path: root.to_path_buf(), source, @@ -505,6 +524,36 @@ mod tests { assert!(!a.created); } + /// **压出来的必须是一个文件名,不是一条路径。** + /// + /// 这个测试在 macOS 上就能抓到那个 Windows 的 bug:那里的分隔符是反斜杠, + /// 只换 `/` 的话它们原样留着,而一个还带着分隔符的「文件名」`join` 上去 + /// 就不再落在备份目录里 —— Windows 上更狠,绝对路径会让 `join` 把前面 + /// 整个丢掉,于是那个路径指回了用户自己的配置文件。 + #[test] + fn a_source_path_is_flattened_into_a_single_name() { + for p in [ + "/Users/x/.claude/settings.json", + "C:\\Users\\x\\.claude\\settings.json", + "\\\\server\\share\\c.json", + ] { + let n = flat_name(Path::new(p)); + assert!(!n.contains('/'), "{p} -> {n}"); + assert!(!n.contains('\\'), "{p} -> {n}"); + assert!(!n.contains(':'), "{p} -> {n}"); + assert!(!n.is_empty(), "{p} -> 空"); + assert!( + !Path::new(&n).is_absolute(), + "{p} -> {n} 还是绝对路径,join 会把备份目录丢掉" + ); + } + // 老样子不变:unix 的路径压出来还是原来那个名字 + assert_eq!( + flat_name(Path::new("/Users/x/.claude/settings.json")), + "Users%x%.claude%settings.json" + ); + } + #[test] fn two_backups_in_the_same_millisecond_do_not_overwrite_each_other() { // 连着接管两次时第二份备份里已经是我们的密钥;它要是盖掉第一份,