From 832ff586b1a6d03208ee3e9f64451c708e1e3e13 Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Mon, 5 Oct 2026 10:13:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(fs):=20=E6=81=A2=E5=A4=8D=E3=80=8C=E6=8C=87?= =?UTF-8?q?=E5=90=91=E6=A0=B9=E7=9A=84=E9=93=BE=E6=8E=A5=E5=8F=AF=E5=88=A0?= =?UTF-8?q?=E3=80=8D=E2=80=94=E2=80=94=E6=A0=B9=E5=AE=88=E5=8D=AB=E6=94=B9?= =?UTF-8?q?=E7=94=A8=20lstat=EF=BC=8C=E5=8F=AA=E7=9C=8B=E6=9D=A1=E7=9B=AE?= =?UTF-8?q?=E8=87=AA=E5=B7=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上一提交的守卫用 stat,会跟随符号链接,于是「指向根的链接」被解析成根本身而被拒绝: 用户再也删不掉自己建的那个快捷方式。原代码比的是字符串,链接路径 ≠ 根路径,放行后 走 lstat 的 unlink 分支,只删链接本身、不递归 —— 这是个可用的行为,不该被顺手关掉。 改成 lstat:问的是「这一行条目本身是不是根」,链接就是它自己,不是它指向的东西。 于是链接恢复可删,而真正造成过事故的那些拼写 —— 大小写变体、\?\ 前缀、8.3 短名、 映射盘 —— 仍然全部被拒。 顺带消掉了「验身份用 stat、做动作用 lstat」的不一致:现在两次调用是同一个。 测试:fs-operations 那条 symlink→根 的用例由「拒绝」翻转为「删掉链接、项目完好」。 注意本机无创建软链接权限,该用例走 runIf(canSymlink) 被跳过,未能在本机实跑。 --- src/fs-operations.ts | 14 ++++++++++---- tests/fs-operations.spec.ts | 12 ++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/fs-operations.ts b/src/fs-operations.ts index 51b309c4..68d58c47 100644 --- a/src/fs-operations.ts +++ b/src/fs-operations.ts @@ -158,13 +158,19 @@ function sameSpelling(a: string, b: string): boolean { * drive. Each of them walked straight past this guard and turned "delete this * row" into a recursive delete of the whole project. `dev`+`ino` is the * identity the filesystem itself uses, and it does not care how the path is - * spelled. `stat` (not `lstat`) is deliberate: a symlink aimed AT the root is - * refused too — no link is worth the project behind it. A target that cannot - * be stat'ed (it may legitimately be gone) falls back to the spelling. + * spelled. + * + * `lstat` (not `stat`) is deliberate, and so is the removal below using the + * same call: the question is whether THIS ENTRY is the root, so a symlink is + * its own identity, never its target's. A link pointed at the root therefore + * stays deletable — only the link goes, no recursion — which is what the + * string compare did. The spellings above, the actual way a project was lost, + * are all still refused. A target that cannot be lstat'ed (it may legitimately + * be gone) falls back to the spelling. */ async function isWorkspaceRoot(target: string, root: string): Promise { try { - const [entry, base] = await Promise.all([stat(target), stat(root)]) + const [entry, base] = await Promise.all([lstat(target), lstat(root)]) // No inode = no identity: fall back to the spelling. if (entry.ino === 0 || base.ino === 0) return sameSpelling(target, root) return entry.dev === base.dev && entry.ino === base.ino diff --git a/tests/fs-operations.spec.ts b/tests/fs-operations.spec.ts index 42fe975f..60e80bec 100644 --- a/tests/fs-operations.spec.ts +++ b/tests/fs-operations.spec.ts @@ -275,16 +275,16 @@ describe('removeWorkspaceEntry', () => { .rejects.toMatchObject({ code: 'fs-error' }) }) - it.runIf(canSymlink)('refuses a symlink aimed AT the root', async () => { - // The guard resolves the target's identity on purpose: this link stands - // for the project itself, so it is refused instead of unlinked. + it.runIf(canSymlink)('unlinks a symlink aimed AT the root, leaving the project intact', async () => { + // The guard compares the ENTRY's own identity (the same `lstat` the + // removal below uses), never what the link resolves to: the link goes and + // the project behind it stays — the behaviour the string compare had. const { ws, inner } = makeProject() symlinkSync(inner, join(ws, 'link-to-proj')) try { - await expect(removeWorkspaceEntry({ cwd: inner, path: join(ws, 'link-to-proj') })) - .rejects.toMatchObject({ code: 'fs-error' }) + await removeWorkspaceEntry({ cwd: inner, path: join(ws, 'link-to-proj') }) + expect(existsSync(join(ws, 'link-to-proj'))).toBe(false) expect(existsSync(join(inner, 'src', 'keep.ts'))).toBe(true) - expect(existsSync(join(ws, 'link-to-proj'))).toBe(true) } finally { rmSync(ws, { recursive: true, force: true }) }