fix(security): [#1981] 修复原子写入fsync、优化错误提示及平台兼容

This commit is contained in:
2026-05-10 02:20:03 +08:00
parent e3afbbe3be
commit fdf99c7184
4 changed files with 50 additions and 19 deletions
+4 -4
View File
@@ -206,16 +206,16 @@ fn write_atomic(path: &Path, contents: &str) -> Result<()> {
let tmp_path = parent.join(format!(".l-s-tmp-{}-{nanos}", std::process::id()));
let result = (|| -> Result<()> {
let mut file = OpenOptions::new()
let mut temp = OpenOptions::new()
.write(true)
.create_new(true)
.open(&tmp_path)
.with_context(|| format!("无法创建临时文件: {}", tmp_path.display()))?;
file.write_all(contents.as_bytes())
temp.write_all(contents.as_bytes())
.with_context(|| format!("无法写入临时文件: {}", tmp_path.display()))?;
file.sync_all()
temp.sync_all()
.with_context(|| format!("无法同步临时文件: {}", tmp_path.display()))?;
drop(file);
drop(temp);
fs::rename(&tmp_path, path).with_context(|| {
format!(