check if file exists
authorValentine Wallace <vwallace@protonmail.com>
Wed, 17 Feb 2021 17:17:51 +0000 (12:17 -0500)
committerValentine Wallace <vwallace@protonmail.com>
Wed, 17 Feb 2021 18:10:47 +0000 (13:10 -0500)
lightning-persister/src/util.rs

index 774046d2e5e2231a214ae8aa6da17510c0b0f718..86f5dcba3b971f799e661d6234cb7db4992af234 100644 (file)
@@ -101,14 +101,18 @@ pub(crate) fn write_to_file<D: DiskWriteable>(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(())