refactor: use explicit fsync

We were previously relying on scoped lifetimes and automatic
`Drop` invocations to sync files but sadly that doesn't seem to block
the program properly to allow the `fsync` to happen before the next
loop iteration, so now we're explicitly calling `File::sync_all` to
see if it is able to avoid resource starvation on the disk by waiting
for the sync to complete before working on the next file.
This commit is contained in:
Harsh Shandilya 2023-11-09 15:04:41 +05:30
parent cacddee6e8
commit 11d44d3ccd
No known key found for this signature in database
1 changed files with 15 additions and 19 deletions

View File

@ -46,34 +46,30 @@ async fn run() -> Result<()> {
let target = config.bucket.target.join(source.file_name().unwrap());
let target = target.as_path();
{
let mut src_file = File::open(source)?;
let mut dst_file = File::create(target)?;
let mut src_file = File::open(source)?;
let mut dst_file = File::create(target)?;
debug!("Moving {} to {}", source.display(), target.display());
std::io::copy(&mut src_file, &mut dst_file).context(format!(
"src={}, dest={}",
source.display(),
target.display(),
))?;
}
debug!("Moving {} to {}", source.display(), target.display());
std::io::copy(&mut src_file, &mut dst_file).context(format!(
"src={}, dest={}",
source.display(),
target.display(),
))?;
src_file.sync_all()?;
dst_file.sync_all()?;
{
let source = File::open(source)?;
let target = File::open(target)?;
let src_len = source.metadata()?.len();
let dst_len = target.metadata()?.len();
let src_len = src_file.metadata()?.len();
let dst_len = dst_file.metadata()?.len();
if src_len != dst_len {
return Err(anyhow!("Destination file length does not match! Source file was {src_len} bytes but {dst_len} bytes were written"));
}
if src_len != dst_len {
return Err(anyhow!("Destination file length does not match! Source file was {src_len} bytes but {dst_len} bytes were written"));
}
std::fs::remove_file(source).context(format!("{}", source.display()))?;
debug!(
"Successfully moved {} to {}",
name.display(),
target.display()
target.display(),
);
}
}