From: Valentine Wallace Date: Wed, 17 Feb 2021 17:17:51 +0000 (-0500) Subject: check if file exists X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=8d7e6168b8388b72da776aab2c599fdf751679d8;p=rust-lightning check if file exists --- 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(())