CFBox v0.2.0 — 优化与更新
代码质量全面优化:二进制体积减少 14%,消除 iostream 运行时依赖,修复潜在 abort 风险。
亮点
- 体积缩减 14%:size-opt Release 从 473 KB 降至 406 KB
- 彻底消除 iostream 依赖:全量移除
<fstream>/<sstream>/<iostream>,零 fstream 符号残留 std::stoi全部替换为std::strtol:避免-fno-exceptions下stoi解析失败时直接abort()- 353 测试全绿,零编译 warning,ASan 零泄漏
体积对比
| 指标 | v0.1.0 | v0.2.0 | 变化 |
|---|---|---|---|
| size-opt Release | 473 KB | 406 KB | -67 KB (-14%) |
| fstream 符号 | 有 | 0 | 彻底消除 |
std::stoi 调用 |
多处 | 0 | 全部替换 |
| GTest 通过 | 353 | 353 | 不变 |
| 编译 warning | 0 | 0 | 不变 |
| Applet 数量 | 115 | 115 | 不变 |
新增 Applet(6 个)
chgrp, chmod, chown, clear, mountpoint, which
变更详情
Phase 1 — 零风险编译选项与基础设施
- 全局启用
-fno-exceptions -fno-rtti - 新增
-Wformat-signedness-Wstrict-aliasing编译警告 - 修复
install.cpp唯一try/catch→strtoul+ errno - 修复多处
%d/%u格式不匹配(unsigned 值配%d)
Phase 2 — 明确 Bug 修复
dmesg.cpp:FILE* 泄漏 →io::unique_fileRAIIinit.hpp:信号标志bool→volatile sig_atomic_t
Phase 3 — std::string / string_view 清理
- 消除多处循环内不必要的
std::string{string_view}构造 - 涉及:
term.hpp,grep.cpp,io.hpp,test.cpp,mkdir.cpp,ls.cpp
Phase 4 — 统一错误报告宏
- 定义
CFBOX_ERR(cmd, fmt, ...)/CFBOX_ERR_V(cmd, fmt, ...)宏 - 102 个 applet 全量迁移
fprintf(stderr, "cfbox xxx: ...")→ 宏
Phase 5 — fs_util 扩展
fs_util.hpp新增chown()/lchown()/for_each_entry()- 重构
chown.cpp,chgrp.cpp,chmod.cpp使用新 API,消除重复递归遍历代码
Phase 6 — iostream 清理
hexdump.cpp,more.cpp:移除未使用的<fstream>rev.cpp:istream→FILE*+fgetspmap.cpp:ifstream/istringstream→FILE*+fgets+sscanfsysctl.cpp:ifstream/ofstream→FILE*+fgets/fprintfproc.hpp:8 处ifstream/istringstream/ostringstream全部转为<cstdio>io.hpp:read_all()修复/proc虚拟文件ftell返回 0 的问题
Phase 7 — stoi 安全化 + I/O 审计
cut.cpp:5 处std::stoi→std::strtolfuser.cpp:std::stoi→std::strtol- D3 I/O 审计:现有
read_all()调用者均需全量数据,无需流式化
新增基础设施
| 组件 | 说明 |
|---|---|
CFBOX_ERR / CFBOX_ERR_V |
统一错误输出宏(Phase 4) |
fs::chown() / fs::lchown() |
chown 系统调用封装(Phase 5) |
fs::for_each_entry() |
递归/非递归目录遍历模板(Phase 5) |
本轮额外完成
- Phase 9:测试覆盖补全 — 为
cat,echo,head,tail,wc补 GTest 单元测试(+26 测试,379 全绿) - CI ccache 加速 — GitHub Actions 全部构建作业配置 ccache + 缓存
完整变更记录:v0.1.0...v0.2.0
English Version
CFBox v0.2.0 — Optimization & Updates
Comprehensive code quality optimization: 14% binary size reduction, iostream runtime dependency eliminated, potential abort risks fixed.
Highlights
- 14% size reduction: size-opt Release shrinks from 473 KB to 406 KB
- iostream dependency fully removed: all
<fstream>/<sstream>/<iostream>eliminated, zero fstream symbols remaining - All
std::stoireplaced withstd::strtol: preventsabort()on parse failure under-fno-exceptions - 353 tests passing, zero compiler warnings, ASan zero leaks
Size Comparison
| Metric | v0.1.0 | v0.2.0 | Change |
|---|---|---|---|
| size-opt Release | 473 KB | 406 KB | -67 KB (-14%) |
| fstream symbols | Yes | 0 | Eliminated |
std::stoi calls |
Multiple | 0 | All replaced |
| GTest passing | 353 | 353 | Unchanged |
| Compiler warnings | 0 | 0 | Unchanged |
| Applet count | 115 | 115 | Unchanged |
Additionally Completed in This Release
- Phase 9: Test coverage — GTest tests for
cat,echo,head,tail,wc(+26 tests, 379 all passing) - CI ccache — ccache + cache configured for all GitHub Actions build jobs
New Applets (6)
chgrp, chmod, chown, clear, mountpoint, which
Changes
Phase 1 — Compiler Flags & Infrastructure
- Globally enabled
-fno-exceptions -fno-rtti - Added
-Wformat-signedness-Wstrict-aliasingwarning flags - Fixed sole
try/catchininstall.cpp→strtoul+ errno - Fixed multiple
%d/%uformat mismatches
Phase 2 — Bug Fixes
dmesg.cpp: FILE* leak →io::unique_fileRAIIinit.hpp: signal flagsbool→volatile sig_atomic_t
Phase 3 — std::string / string_view Cleanup
- Eliminated unnecessary
std::string{string_view}constructions in hot paths - Files:
term.hpp,grep.cpp,io.hpp,test.cpp,mkdir.cpp,ls.cpp
Phase 4 — Unified Error Reporting Macros
- Defined
CFBOX_ERR(cmd, fmt, ...)/CFBOX_ERR_V(cmd, fmt, ...)macros - Migrated all 102 applets from
fprintf(stderr, "cfbox xxx: ...")to macros
Phase 5 — fs_util Extensions
- Added
chown()/lchown()/for_each_entry()tofs_util.hpp - Refactored
chown.cpp,chgrp.cpp,chmod.cppto use new APIs
Phase 6 — iostream Cleanup
- Removed unused
<fstream>fromhexdump.cpp,more.cpp - Converted
rev.cpp,pmap.cpp,sysctl.cppfromifstream/ofstreamtoFILE*+<cstdio> - Converted all 8
ifstream/istringstream/ostringstreamusages inproc.hppto<cstdio> - Fixed
io::read_all()for/procvirtual files (ftell returns 0)
Phase 7 — stoi Safety & I/O Audit
- Replaced
std::stoiwithstd::strtolincut.cpp(5 sites) andfuser.cpp - I/O audit: all
read_all()callers require full data; no safe streaming conversions found
New Infrastructure
| Component | Description |
|---|---|
CFBOX_ERR / CFBOX_ERR_V |
Unified error output macros (Phase 4) |
fs::chown() / fs::lchown() |
chown syscall wrappers (Phase 5) |
fs::for_each_entry() |
Recursive/non-recursive directory traversal template (Phase 5) |
Full Changelog: v0.1.0...v0.2.0
下一步:Phase 2 — 核心命令深化(v0.3.0 规划)
将现有命令功能深度从 ~30% 提升到 ~70%,按运维频率分批推进。
第一批(v0.3.0)
| 命令 | 关键补充 | 优先级 |
|---|---|---|
tail |
-f follow 模式(inotify/polling) |
P0 |
cp |
-a archive 模式(保留权限/链接/时间戳/递归) |
P0 |
test |
全面 POSIX:-a/-o/字符串比较/文件类型/-nt/-ot |
P0 |
ls |
-R 递归 + --color 彩色输出 |
P1 |
第二批(v0.4.0)
| 命令 | 关键补充 |
|---|---|
grep |
-A/-B/-C 上下文行、--include/--exclude |
tar |
-z gzip、-v verbose、-C 目录切换 |
sed |
-i 原地编辑、多命令 -e |
sort |
-k 按列排序、-n 数值排序、-u 去重 |
第三批(v0.5.0)
| 命令 | 关键补充 |
|---|---|
find |
布尔表达式(-and/-or/!)、-type f/-name |
sh |
case/heredoc/函数/return |
ps |
-aux/-ef、线程显示 |
df/du |
-h 人类可读、-T 文件系统类型 |