Skip to content

CFBox v0.2.0 — 优化与更新

Latest

Choose a tag to compare

@Charliechen114514 Charliechen114514 released this 27 May 03:50
a96ec85

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-exceptionsstoi 解析失败时直接 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/catchstrtoul + errno
  • 修复多处 %d / %u 格式不匹配(unsigned 值配 %d

Phase 2 — 明确 Bug 修复

  • dmesg.cpp:FILE* 泄漏 → io::unique_file RAII
  • init.hpp:信号标志 boolvolatile 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.cppistreamFILE* + fgets
  • pmap.cppifstream / istringstreamFILE* + fgets + sscanf
  • sysctl.cppifstream / ofstreamFILE* + fgets / fprintf
  • proc.hpp:8 处 ifstream / istringstream / ostringstream 全部转为 <cstdio>
  • io.hppread_all() 修复 /proc 虚拟文件 ftell 返回 0 的问题

Phase 7 — stoi 安全化 + I/O 审计

  • cut.cpp:5 处 std::stoistd::strtol
  • fuser.cppstd::stoistd::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::stoi replaced with std::strtol: prevents abort() 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-aliasing warning flags
  • Fixed sole try/catch in install.cppstrtoul + errno
  • Fixed multiple %d / %u format mismatches

Phase 2 — Bug Fixes

  • dmesg.cpp: FILE* leak → io::unique_file RAII
  • init.hpp: signal flags boolvolatile 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() to fs_util.hpp
  • Refactored chown.cpp, chgrp.cpp, chmod.cpp to use new APIs

Phase 6 — iostream Cleanup

  • Removed unused <fstream> from hexdump.cpp, more.cpp
  • Converted rev.cpp, pmap.cpp, sysctl.cpp from ifstream/ofstream to FILE* + <cstdio>
  • Converted all 8 ifstream/istringstream/ostringstream usages in proc.hpp to <cstdio>
  • Fixed io::read_all() for /proc virtual files (ftell returns 0)

Phase 7 — stoi Safety & I/O Audit

  • Replaced std::stoi with std::strtol in cut.cpp (5 sites) and fuser.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 文件系统类型