Skip to content

Commit

Permalink
Merge pull request #8 from ngotchac/error-handling
Browse files Browse the repository at this point in the history
Return errors when `swap_nonatomic` fails
  • Loading branch information
debris authored Sep 11, 2018
2 parents 0fb73eb + ad6b44f commit c9644f4
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ pub fn swap_nonatomic<A, B>(a: A, b: B) -> io::Result<()> where A: AsRef<Path>,

match fs::rename(b, a) {
Ok(_) => (),
Err(_) => {
error => {
// let's try to recover the previous state
// if it fails, there is nothing we can do
return fs::rename(&tmp, a);
fs::rename(&tmp, a)?;
return error
},
}

// rename tmp to b
match fs::rename(&tmp, b) {
Ok(_) => Ok(()),
Err(_) => {
error => {
// let's try to recover to previous state
// if it fails, there is nothing we can do
fs::rename(a, b)?;
fs::rename(&tmp, a)
fs::rename(&tmp, a)?;
error
},
}
}
Expand Down

0 comments on commit c9644f4

Please sign in to comment.