Skip to content

Commit

Permalink
Use fs::copy() if fs::rename() fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Zireael-N committed Jul 18, 2020
1 parent 68816f7 commit 762e743
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion localize_npc_names/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "localize_npc_names"
version = "0.1.0"
version = "0.1.1"
authors = ["Velithris"]
edition = "2018"

Expand Down
10 changes: 8 additions & 2 deletions localize_npc_names/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,15 @@ pub(crate) fn write_to_dir(
tmp_file.flush().map_err(|e| (tmp_path.clone(), e))?;

drop(to_file);
if let Err(e) = fs::rename(&tmp_path, &to_path) {

// Fails if files belong to different filesystems
if let Err(_) = fs::rename(&tmp_path, &to_path) {
let copy_result = fs::copy(&tmp_path, &to_path);
fs::remove_file(&tmp_path).map_err(|e| (tmp_path, e))?;
return Err((to_path, e));

if let Err(e) = copy_result {
return Err((to_path, e));
}
}
}
}
Expand Down

0 comments on commit 762e743

Please sign in to comment.