fix(fs): 恢复「指向根的链接可删」——根守卫改用 lstat,只看条目自己
上一提交的守卫用 stat,会跟随符号链接,于是「指向根的链接」被解析成根本身而被拒绝: 用户再也删不掉自己建的那个快捷方式。原代码比的是字符串,链接路径 ≠ 根路径,放行后 走 lstat 的 unlink 分支,只删链接本身、不递归 —— 这是个可用的行为,不该被顺手关掉。 改成 lstat:问的是「这一行条目本身是不是根」,链接就是它自己,不是它指向的东西。 于是链接恢复可删,而真正造成过事故的那些拼写 —— 大小写变体、\?\ 前缀、8.3 短名、 映射盘 —— 仍然全部被拒。 顺带消掉了「验身份用 stat、做动作用 lstat」的不一致:现在两次调用是同一个。 测试:fs-operations 那条 symlink→根 的用例由「拒绝」翻转为「删掉链接、项目完好」。 注意本机无创建软链接权限,该用例走 runIf(canSymlink) 被跳过,未能在本机实跑。
This commit is contained in:
1 parent
fe7125ae3b
commit
832ff586b1
2 files changed
+16
-10
No files matched your search
+10
-4
@@ -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<boolean> {
|
||||
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
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user