From 7adaecdafc231f8e7c34ba5221712c97d9e1c37c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Sun, 5 Dec 2021 23:31:19 +0100 Subject: [PATCH] re-enable synchronous mode more robustly through a drop guard --- src/filesystem.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/filesystem.rs b/src/filesystem.rs index c900325..2d08e63 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -78,6 +78,26 @@ pub async fn rescan_vault( .unwrap(); } } +struct PragmaSynchronousGuard<'a>(&'a DbPool); + +impl Drop for PragmaSynchronousGuard<'_> { + fn drop(&mut self) { + debug!("Re-enabling synchronous mode."); + let connection = self.0.get(); + let res: Result<_, String> = match connection { + Ok(connection) => connection + .execute("PRAGMA synchronous = NORMAL;") + .map_err(|err| format!("{}", err)), + Err(err) => Err(format!("{}", err)), + }; + if let Err(err) = res { + error!( + "Error setting synchronous mode back to NORMAL! Data loss possible! {}", + err + ); + } + } +} type UpdatePathResult = Result; @@ -105,6 +125,7 @@ fn _rescan_vault>( // Disable syncing in SQLite for the duration of the import debug!("Disabling SQLite synchronous mode"); pool.get()?.execute("PRAGMA synchronous = OFF;")?; + let _guard = PragmaSynchronousGuard(&pool); // Walk through the vault, find all paths debug!("Traversing vault directory"); @@ -153,6 +174,8 @@ fn _rescan_vault>( }) .collect(); + debug!("Processing done, cleaning up..."); + let existing_files = existing_files.read().unwrap(); let connection = pool.get()?; @@ -199,8 +222,7 @@ fn _rescan_vault>( } // Re-enable SQLite syncing - debug!("Re-enabling synchronous mode."); - pool.get()?.execute("PRAGMA synchronous = NORMAL;")?; + drop(_guard); info!( "Finished updating {} ({} created, {} deleted, {} left unchanged). Took {}s.",