Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return errors when swap_nonatomic fails #8

Merged
merged 2 commits into from
Sep 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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