Skip to content

fix(文件操作): 修正目录创建和文件写入错误处理 - #419

Open
Mikachu2333 wants to merge 7 commits into
RubyMetric:devfrom
Mikachu2333:split/env-file-io
Open

Mikachu2333 wants to merge 7 commits into
RubyMetric:devfrom
Mikachu2333:split/env-file-io

Conversation

@Mikachu2333

Copy link
Copy Markdown
Collaborator

问题描述

空串写入、目录路径处理和缓冲区关闭失败存在误报或漏报,部分失败路径还会泄漏资源。

关联 #402。

方案与实现

完善目录创建和追加/前插/覆写的错误处理,增加可返回失败的覆写入口,覆盖空写入与 /dev/full。

本 PR 从 #402 独立拆出,直接基于 dev,可以单独审查和合并。

原提交中的备份路径修复随“通用文件更新层”单独提交;本 PR 可直接基于 dev 合并。

验证:此分支独立通过 Windows just build/test/bd、WSL Debian make build/test/bd,以及调试程序的只读查询。

@github-actions

Copy link
Copy Markdown

Hi @Mikachu2333

❤️ 感谢你的贡献!我们将在最少半小时,最多5天内阅读此 PR 并回复你

Comment thread src/framework/OS.c Resolved
@Mikachu2333
Mikachu2333 requested a review from ccmywish November 12, 2026 05:03
@Mikachu2333
Mikachu2333 force-pushed the split/env-file-io branch 4 times, most recently from db282d3 to 7845d88 Compare November 13, 2026 14:31

@ccmywish ccmywish left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

引入了两个 try_ 函数主要是想解决什么问题?

overwrite 似乎可以不用 try,如果想判断返回值,直接把原函数的返回值类型从 void 改为 bool,append 和 prepend 同理。

Comment thread src/framework/OS.c Resolved
Comment thread src/framework/OS.c Resolved
Mikachu2333 and others added 5 commits December 5, 2026 17:05
空串写入、目录路径处理和缓冲区关闭失败存在误报或漏报,部分失败路径还会泄漏资源。

完善目录创建和追加/前插/覆写的错误处理,增加可返回失败的覆写入口,覆盖空写入与 /dev/full。

从 RubyMetric#402 按功能拆出,直接基于 dev;Windows/POSIX 独立构建及现有测试通过。
合并两个 try 接口,目录创建及追加、前插、覆写直接返回 bool;同步检查 recipe 调用结果,保留失败时退出码 4。

按维护者要求保留原 shell 实现注释;补齐 Dry Run、空写入、路径冲突及 /dev/full 短写入和缓冲关闭失败测试。

验证:Windows just build/bd/test;WSL Debian make build/bd/test 及修正后的 test-fw;两平台调试查询与隔离项目写入失败退出码验证。
@Mikachu2333
Mikachu2333 force-pushed the split/env-file-io branch 3 times, most recently from 3befa16 to fa72ca8 Compare December 5, 2026 09:10
@Mikachu2333

Copy link
Copy Markdown
Collaborator Author

震惊,tmpfs 目录:fseek 会返回 -1/EINVAL 并提前退出;而 ext4 目录的 ftell 返回 9223372036854775807,随后申请 8EB 内存,分配失败后执行 memset(NULL, …)。这怎么debian和Ubuntu处理方式还不一样呢……怪不得我本地的Debian WSL一切正常,而GitHub的Ubuntu就崩溃

@ccmywish

ccmywish commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

fseek 会返回 -1/EINVAL 并提前退出;而 ext4 目录的 ftell 返回 9223372036854775807

fseek(), ftell() 应该只能对文件操作吧,目录用的是 seekdir() telldir() 这些。


我看了你新改的代码,那个 xy_file_read() 是读文件的,不能读目录。正常不会返回极大值。

@Mikachu2333

Mikachu2333 commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator Author

我看了你新改的代码,那个 xy_file_read() 是读文件的,不能读目录。正常不会返回极大值。

旧test是这么写的(

想测试 无法创建目录/打开文件,然后故意传入无效路径,崩溃发生在 把目录当文件执行 prepend 这个操作的时候,而我本地Debian WSL由于对fseek和ftell的实现似乎与Ubuntu有些微的区别导致提前崩溃退出,误打误撞通过了测试,而Ubuntu测试挂了

之前的读取函数把 fseek/ftell 的结果直接当成文件长度

Comment thread src/framework/OS.c Resolved
Comment thread src/framework/OS.c Resolved
Comment thread test/fw.c Resolved
@ccmywish

ccmywish commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

我建议引入 chsrc_MUST_ 系列函数,比如

  1. chsrc_MUST_append_to_file()
  2. chsrc_MUST_prepend_to_file()
  3. chsrc_MUST_overwrite_file()

它们的行为是:如果失败,则 exit() 退出。

这样只用把 recipe 改成用 chsrc_MUST_ 系列函数就行了,就不用大量加 if 条件判断是否执行成功与否。其实这也是之前文件操作函数的行为。


所以我还是不知道

引入了两个 try_ 函数主要是想解决什么问题?

是为了判断返回值?

@Mikachu2333
Mikachu2333 requested a review from ccmywish December 5, 2026 11:59

@ccmywish ccmywish left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有 "将" 替换为 "计划"

Comment thread lib/xy.h
*/
static char *
xy_file_read (const char *path)
xy_file_read_checked (const char *path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还是只留 xy_file_read() 吧,返回 NULL 也是可以的,不用为了兼容以前的行为刻意再加一个函数。

Comment thread src/recipe/lang/Julia.c
char *w = xy_strcat (3, "ENV[\"JULIA_PKG_SERVER\"] = \"", source.url, "\"");

chsrc_append_to_file (w, PL_Julia_Config);
if (!chsrc_append_to_file (w, PL_Julia_Config)) exit (Exit_UserCause);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全部用 chsrc_MUST 系列函数,不用再这样判断。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

改善加强 改善加强

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants