Skip to content

Emulated mkdir/rm/touch: resolve operands through the cwd anchor under a deleted cwd - #3331

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/3276-simulated-mkdir-rm-touch-dead-cwd
Nov 11, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/3276-simulated-mkdir-rm-touch-dead-cwd

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3276

背景

#3268 家族普查(设计卷宗 fix-3268-ade/DESIGN.md §6.1)在 master a78f0cde 实测:进程内模拟 mkdir / rm / touch 在已删 cwd 下与模拟 cp 同红——操作数一律经会话 cwd 字符串拼接解析,死分量处断裂。本 PR 在现 master 5d8867fd(含 (d) 收敛层)上重新探针定界:19 格探针中 13 格与 GNU 分叉,含三处硬伤——mkdir -p ../pdir/deep 把已删目录复活并建在其下、rm marker 在目录被原地重建后删掉了幽灵目录的文件、touch marker 同形穿越盖戳。

GNU 锚

GNU bash 不实现 mkdir/rm/touch:execute_cmd.c:5771 execute_disk_command fork 出的子进程继承 cwd inode,coreutils 工具的相对操作数由内核自该 inode 行走。本 PR 的 oracle = GNU bash 5.3.20 + 系统 coreutils 逐格实测(探针与双边日志随车道卷宗存档):逃逸父级成功、向死目录内创建/删除由内核 ENOENT 裁决、幽灵不穿越。

修法(机制级,复用既有收敛层,不另起门)

  • 三个处理器的每个操作数在入口经 既有 Executor::cp_operand_root((d) 面所立)收敛一次为 CpNode 根:相对操作数 + 会话 cwd 不可解析(删后原地重建目录的幽灵穿越:静止会话经复活路径读到新目录内容(GNU 仍坐死 inode 报 ENOENT) #3275 身份判词)+ 锚命名该 cwd → 锚相对根;其余一切形状保持 Plain 拼接串逐字节不变。
  • CpNode 新增四个节点方法,全部 Plain 臂 = 现行 std 调用原样、At 臂 = *at 族对锚句柄操作:create_dir(mkdirat)、remove_dir(unlinkat + AT_REMOVEDIR)、remove_dir_all(节点递归,符号链接不跟随)、stamp_existing(utimensat 的 (dirfd, rel) 形;既有 touch_utimensat 拆出同一核心,AT_FDCWD 形为其特例)。
  • mkdir:单级走 create_dir、-p 走既有 create_dir_all 节点形。
  • rm:存在性/类型判定、./.. 拒绝护栏的存在性判定全走节点 stat;preserve-root 在锚根操作数下改以 (dev, ino) 身份比较(字符串 canonicalize 穿不过死分量),Plain 操作数保持原字符串判词原样;删除分发走 remove_file / remove_dir / remove_dir_all 节点形。
  • touch:操作数盖戳与创建走节点;-r 参考文件同为路径入参,一并经同一根收敛读其 atime/mtime。

钉

新茎 tests/issue3276_simulated_tools_dead_cwd.rs,19 格(逐处理器 fail-first + 回归锁 + 活 cwd 对照)。基底 5d8867fd 上 fail-first:6 过 / 13 红,红集恰缺陷格(mkdir 逃逸/-p 复活/-p 入死/幽灵、rm 逃逸/树逃逸/无 -r 侧别/dotdot 护栏/幽灵、touch 逃逸创建/逃逸盖戳/参考逃逸/幽灵)。改后数字见车道报告。

本 PR 删除了什么

无删除。本 PR 为纯新增节点方法与处理器接线:touch_utimensat 的函数体拆为 (dirfd, rel) 核心 + AT_FDCWD 薄封装,属同函数内重构,原调用点签名不变;无文件、无公开面、无既有行为面被删除。

…r a deleted cwd

GNU bash has no mkdir/rm/touch builtin: execute_cmd.c:5771
execute_disk_command forks a child that inherits the session's
cwd INODE, and the coreutils tool's relative operands are walked
by the kernel from that inode. The emulated tools instead
resolved every operand through the session-cwd string, which
dies at the deleted component — escapes to the parent failed
ENOENT, and once the path was recreated in place the string
walked into the recreated ghost directory (rm deleted the
ghost's files; mkdir -p even resurrected the deleted directory
and built inside it).

Each operand root now converges ONCE at the handler entry
through the existing Executor::cp_operand_root (the convergence
the emulated-cp face built on the identity verdict of the ghost
fix) — no second gate. CpNode gains create_dir (mkdirat),
remove_dir (unlinkat with AT_REMOVEDIR), remove_dir_all (a node
walk that never follows symlinks) and stamp_existing (utimensat
against an explicit dirfd; the existing touch stamp splits into
that core plus its AT_FDCWD form). rm's guards read the same
node: the dot/dotdot existence question is the node's stat, and
preserve-root under an anchor root compares (dev, ino) identity
where the string canonicalize cannot walk. touch's -r reference
resolves through the same root. Plain roots keep the exact
pre-face std calls, byte for byte.

New pin stem tests/issue3276_simulated_tools_dead_cwd.rs:
19 cells (per-handler fail-first, regression locks, a live-cwd
control). On the base the stem reads 6 passed / 13 failed with
the red set exactly the defect cells; with this change 19/19.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 11, 2026 08:50
@caomengxuan666
caomengxuan666 merged commit c30dab3 into master Nov 11, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant