Skip to content

Commit

Permalink
Try harder to create rules backup (#967)
Browse files Browse the repository at this point in the history
Add a fallback for when a rename does not succeed.

In the case where tempdir is on a different filesystem the
`std::fs::rename` call will fail.

```
This function will return an error in the following situations, but is not limited to just these cases:

  - from does not exist.
  - The user lacks permissions to view contents.
  - from and to are on separate filesystems.
```

https://doc.rust-lang.org/std/fs/fn.rename.html

This commit updates the logic to fallback to a copy and delete.

Closes #965
  • Loading branch information
jw3 authored Dec 19, 2023
1 parent 5999330 commit 101763a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/daemon/src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ impl Profiler {
// create a temp file as the backup location
let backup = NamedTempFile::new()?;
// move original compiled to backup location
fs::rename(&compiled, &backup)?;
fs::rename(&compiled, &backup).or_else(|x| {
log::debug!("rename fallback copy");
fs::copy(&compiled, &backup)
.and_then(|_| fs::remove_file(&compiled))
.or(Err(x))
})?;
// write compiled rules for the profiling run
write::compiled_rules(db, &compiled)?;
log::debug!("rules backed up to {:?}", backup.path());
Expand Down

0 comments on commit 101763a

Please sign in to comment.