Skip to content

Releases: Awesome-Embedded-Learning-Studio/CFBox

CFBox v0.2.0 — 优化与更新

27 May 03:50
a96ec85

Choose a tag to compare

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 文件系统类型

V0.1.0 Release

08 May 04:46
780cf9f

Choose a tag to compare

CFBox v0.1.0 — 首个正式发布

C++23 BusyBox 替代品。109 个 applet,零外部运行时依赖,可作为 PID 1 运行。

亮点

  • 109 个 applet,覆盖 shell、coreutils、findutils、文本处理、归档压缩、进程管理和 init 系统
  • 完整 POSIX Shell——管道、重定向、控制流、变量/命令展开、glob、15 个内建命令
  • 完整 AWK 解释器——模式匹配、关联数组、printf、用户自定义函数、正则
  • Init 系统——inittab 解析、运行级别管理、服务监控与 respawn、优雅关机;已在 QEMU aarch64 上以 PID 1 验证
  • 零外部运行时依赖——自研 deflate/inflate 替代 zlib,单文件静态链接
  • 446 KB size-opt Release 构建(visibility hidden + section GC + strip)
  • 331 单元测试 + 54 集成测试,全部通过,ASan 零泄漏

Applet 列表

Shell

sh — 交互式 POSIX shell(词法/语法/执行器,7 个模块 ~2400 行)

Coreutils(60 个)

basename, cat, chgrp, chmod, chown, cksum, comm, cp, cut, date, df, dirname, du, echo, env, expand, expr, factor, false, fold, hostid, hostname, id, install, link, ln, logname, ls, md5sum, mkdir, mkfifo, mknod, mktemp, mv, nl, nproc, od, paste, printenv, printf, pwd, readlink, realpath, rm, rmdir, seq, shuf, sleep, sort, split, stat, sum, sync, tac, tee, test, touch, tr, true, truncate, tsort, tty, uname, unlink, usleep, wc, who, whoami, yes

文本处理

awk, diff, patch, ed, grep, sed

归档与压缩

tar, cpio, ar, unzip, gzip, gunzip

findutils

find, xargs

procps(15 个)

ps, kill, free, uptime, top, watch, pgrep, pkill, pidof, sysctl, pwdx, pstree, pmap, fuser, iostat

util-linux

dmesg, hexdump, more, rev, cal, renice

Init

init

基础设施

组件 说明
args.hpp GNU 风格长选项解析器
help.hpp 统一 --help / --version 输出
term.hpp ANSI 彩色输出(尊重 NO_COLOR
utf8.hpp Unicode 感知的字符计数与宽度
stream.hpp 流式行读取器(for_each_line
checksum.hpp CRC-32、MD5、BSD/SysV sum
compress.hpp 轻量 deflate/inflate(替代 zlib)
terminal.hpp Raw mode RAII、光标控制、备用屏幕缓冲
tui.hpp 全屏 TUI 框架(ScreenBuffer、SIGWINCH)
proc.hpp 集中式 /proc 解析器
fs_util.hpp 扩展文件系统工具
error.hpp std::expected 错误处理(CFBOX_TRY

性能优化

  • Myers O(ND) diff 算法——替代 O(mn) LCS
  • POSIX regex_t——替代 std::regex(grep/sed 加速)
  • 流式 I/O——grep/cat/wc 可处理无限输入
  • 预计算排序 key——避免比较器中重复内存分配
  • 全局 reserve()——diff/ls/timeout/unzip/find 等场景预分配
  • 72 个函数添加 noexcept / [[nodiscard]] / constexpr 标注
  • RAII 封装——unique_filescoped_regexunique_pipe(ASan 验证零泄漏)

构建与 CI

  • CMake 支持 per-applet CFBOX_ENABLE_<APPLET> 开关及预设 profile(embedded / desktop / minimal / full
  • CI 覆盖原生构建、交叉编译(armhf、aarch64)和 QEMU 启动测试
  • 体积优化:cmake -DCMAKE_BUILD_TYPE=Release -DCFBOX_SIZE_OPT=ON

已验证平台

  • x86_64(原生)
  • aarch64(QEMU,含 PID 1 init 启动)
  • armhf(QEMU)

下一步计划

  • Phase 5:vi 可视化编辑器
  • Phase 6:网络工具 + 登录管理 + 系统日志
  • Phase 7:剩余 applet + 全面 POSIX 合规验证

完整变更记录https://github.com/Awesome-Embedded-Learning-Studio/cfbox/commits/v0.1.0


English Version

CFBox v0.1.0 — First Release

C++23 BusyBox alternative. 109 applets, zero external runtime dependencies, runs as PID 1.

Highlights

  • 109 applets across shell, coreutils, findutils, text processing, archiving, compression, process management, and init
  • Full POSIX Shell — pipes, redirections, control flow, variable/command expansion, glob, 15 builtins
  • Complete AWK interpreter — patterns, associative arrays, printf, user functions, regex
  • Init system — inittab parser, runlevel management, service monitoring with respawn, clean shutdown; verified on QEMU aarch64 as PID 1
  • Zero external runtime deps — custom deflate/inflate replaces zlib; static-linked single binary
  • 446 KB size-optimized Release build (visibility hidden + section GC + strip)
  • 331 unit tests + 54 integration tests, all passing, ASan zero leaks

Applets

Shell

sh — interactive POSIX shell (lexer/parser/executor, 7 modules, ~2400 LOC)

Coreutils (60)

basename, cat, chgrp, chmod, chown, cksum, comm, cp, cut, date, df, dirname, du, echo, env, expand, expr, factor, false, fold, hostid, hostname, id, install, link, ln, logname, ls, md5sum, mkdir, mkfifo, mknod, mktemp, mv, nl, nproc, od, paste, printenv, printf, pwd, readlink, realpath, rm, rmdir, seq, shuf, sleep, sort, split, stat, sum, sync, tac, tee, test, touch, tr, true, truncate, tsort, tty, uname, unlink, usleep, wc, who, whoami, yes

Text Processing

awk, diff, patch, ed, grep, sed

Archiving & Compression

tar, cpio, ar, unzip, gzip, gunzip

findutils

find, xargs

procps (15)

ps, kill, free, uptime, top, watch, pgrep, pkill, pidof, sysctl, pwdx, pstree, pmap, fuser, iostat

util-linux

dmesg, hexdump, more, rev, cal, renice

Init

init

Infrastructure

Component Description
args.hpp GNU-style long option parser
help.hpp Unified --help / --version for all applets
term.hpp ANSI color output (respects NO_COLOR)
utf8.hpp Unicode-aware character counting & width
stream.hpp Streaming line reader (for_each_line)
checksum.hpp CRC-32, MD5, BSD/SysV sum
compress.hpp Lightweight deflate/inflate (replaces zlib)
terminal.hpp Raw mode RAII, cursor control, alternate screen
tui.hpp Full-screen TUI framework (ScreenBuffer, SIGWINCH)
proc.hpp Centralized /proc parser
fs_util.hpp Extended filesystem utilities
error.hpp std::expected error handling (CFBOX_TRY)

Optimizations

  • Myers O(ND) diff — replaced O(mn) LCS algorithm
  • POSIX regex_t — replaced std::regex in grep/sed
  • Streaming I/O — grep/cat/wc process unbounded input without buffering
  • Pre-computed sort keys — avoids repeated allocations in comparator
  • Global reserve() — diff, ls, timeout, unzip, find pre-allocate known sizes
  • 72 functions annotated with noexcept / [[nodiscard]] / constexpr
  • RAII wrappersunique_file, scoped_regex, unique_pipe (ASan-verified zero leak)

Build & CI

  • CMake with per-applet CFBOX_ENABLE_<APPLET> toggles and preset profiles (embedded / desktop / minimal / full)
  • CI covers native build, cross-compilation (armhf, aarch64), and QEMU boot tests
  • Size-optimized profile: cmake -DCMAKE_BUILD_TYPE=Release -DCFBOX_SIZE_OPT=ON

Tested On

  • x86_64 (native)
  • aarch64 (QEMU, including PID 1 init)
  • armhf (QEMU)

What's Next

  • Phase 5: vi visual editor
  • Phase 6: Networking + login management + syslog
  • Phase 7: Remaining applets + full POSIX compliance verification

Full Changelog: https://github.com/Awesome-Embedded-Learning-Studio/cfbox/commits/v0.1.0