diff --git a/rust/tool/systemd/src/install.rs b/rust/tool/systemd/src/install.rs index b04ad1b..2db01a7 100644 --- a/rust/tool/systemd/src/install.rs +++ b/rust/tool/systemd/src/install.rs @@ -465,17 +465,29 @@ fn assemble_kernel_cmdline(init: &Path, kernel_params: Vec) -> Vec Result<()> { - let to_tmp = to.with_extension(".tmp"); - - fs::copy(from, &to_tmp) - .with_context(|| format!("Failed to copy from {from:?} to {to_tmp:?}",))?; - - fs::rename(&to_tmp, to).with_context(|| { - format!("Failed to move temporary file {to_tmp:?} to final location {to:?}") - }) + let tmp = to.with_extension(".tmp"); + { + let mut from_file = + File::open(from).with_context(|| format!("Failed to read the source file {from:?}"))?; + let mut tmp_file = File::create(&tmp) + .with_context(|| format!("Failed to create the temporary file {tmp:?}"))?; + std::io::copy(&mut from_file, &mut tmp_file).with_context(|| { + format!("Failed to copy from {from:?} to the temporary file {tmp:?}") + })?; + tmp_file + .sync_all() + .with_context(|| format!("Failed to sync the temporary file {tmp:?}"))?; + } + fs::rename(&tmp, to) + .with_context(|| format!("Failed to move temporary file {tmp:?} to target {to:?}")) } /// Set the octal permission bits of the specified file.