fix(security): [#1981] 修复原子写入fsync、优化错误提示及平台兼容
This commit is contained in:
+4
-4
@@ -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!(
|
||||
|
||||
Reference in New Issue
Block a user