From 8d7e6168b8388b72da776aab2c599fdf751679d8 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Wed, 17 Feb 2021 12:17:51 -0500 Subject: [PATCH] check if file exists --- lightning-persister/src/util.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lightning-persister/src/util.rs b/lightning-persister/src/util.rs index 774046d2e..86f5dcba3 100644 --- a/lightning-persister/src/util.rs +++ b/lightning-persister/src/util.rs @@ -101,14 +101,18 @@ pub(crate) fn write_to_file(path: String, filename: String, da // winapi::um::winbase::MOVEFILE_WRITE_THROUGH | winapi::um::winbase::MOVEFILE_REPLACE_EXISTING // )}); let backup_filepath = PathBuf::from(format!("{}.backup", filename_with_path.clone())); - // call!(unsafe {winapi::um::winbase::ReplaceFileW( - // path_to_windows_str(src).as_ptr(), path_to_windows_str(dst).as_ptr(), path_to_windows_str(backup_filepath).as_ptr(), - // winapi::um::winbase::MOVEFILE_WRITE_THROUGH | winapi::um::winbase::MOVEFILE_REPLACE_EXISTING, std::ptr::null_mut() as *mut winapi::ctypes::c_void, std::ptr::null_mut() as *mut winapi::ctypes::c_void - // )}); - unsafe {winapi::um::winbase::ReplaceFileW( - path_to_windows_str(src).as_ptr(), path_to_windows_str(dst).as_ptr(), path_to_windows_str(backup_filepath).as_ptr(), - winapi::um::winbase::REPLACEFILE_IGNORE_MERGE_ERRORS, std::ptr::null_mut() as *mut winapi::ctypes::c_void, std::ptr::null_mut() as *mut winapi::ctypes::c_void - )}; + + if Path::new(&filename_with_path.clone()).exists() { + unsafe {winapi::um::winbase::ReplaceFileW( + path_to_windows_str(src).as_ptr(), path_to_windows_str(dst).as_ptr(), path_to_windows_str(backup_filepath).as_ptr(), + winapi::um::winbase::REPLACEFILE_IGNORE_MERGE_ERRORS, std::ptr::null_mut() as *mut winapi::ctypes::c_void, std::ptr::null_mut() as *mut winapi::ctypes::c_void + )}; + } else { + call!(unsafe {winapi::um::winbase::MoveFileExW( + path_to_windows_str(src).as_ptr(), path_to_windows_str(dst).as_ptr(), + winapi::um::winbase::MOVEFILE_WRITE_THROUGH | winapi::um::winbase::MOVEFILE_REPLACE_EXISTING + )}); + } println!("VMW: renamed"); } Ok(()) -- 2.39.5