From 3350b4fa69a5b39edda93607e2e8656c0561ecb6 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 8 Feb 2020 21:02:43 +1000 Subject: [PATCH] hold the DB open for the duration of the check --- rslib/src/backend.rs | 5 ++++- rslib/src/media/check.rs | 7 +++++-- rslib/src/media/mod.rs | 11 ++++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/rslib/src/backend.rs b/rslib/src/backend.rs index 76e6df462..0e7ed5819 100644 --- a/rslib/src/backend.rs +++ b/rslib/src/backend.rs @@ -314,7 +314,10 @@ impl Backend { fn add_file_to_media_folder(&mut self, input: pt::AddFileToMediaFolderIn) -> Result { let mgr = MediaManager::new(&self.media_folder, &self.media_db)?; - Ok(mgr.add_file(&input.desired_name, &input.data)?.into()) + let mut ctx = mgr.dbctx(); + Ok(mgr + .add_file(&mut ctx, &input.desired_name, &input.data)? + .into()) } fn sync_media(&self, input: SyncMediaIn) -> Result<()> { diff --git a/rslib/src/media/check.rs b/rslib/src/media/check.rs index 3758f901a..750361a2a 100644 --- a/rslib/src/media/check.rs +++ b/rslib/src/media/check.rs @@ -2,6 +2,7 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html use crate::err::{AnkiError, Result}; +use crate::media::database::MediaDatabaseContext; use crate::media::files::{ data_for_file, filename_if_normalized, remove_files, MEDIA_SYNC_FILESIZE_LIMIT, }; @@ -60,6 +61,7 @@ where let mut oversize = vec![]; let mut all_files = vec![]; let mut renamed_files = vec![]; + let mut ctx = self.mgr.dbctx(); for dentry in self.mgr.media_folder.read_dir()? { let dentry = dentry?; @@ -92,7 +94,7 @@ where } // rename if required - let (norm_name, renamed) = self.normalize_and_maybe_rename(&disk_fname)?; + let (norm_name, renamed) = self.normalize_and_maybe_rename(&mut ctx, &disk_fname)?; if renamed { renamed_files.push(RenamedFile { current_fname: norm_name.to_string(), @@ -114,6 +116,7 @@ where /// Returns (normalized_form, needs_rename) fn normalize_and_maybe_rename<'a>( &mut self, + ctx: &mut MediaDatabaseContext, disk_fname: &'a str, ) -> Result<(Cow<'a, str>, bool)> { // already normalized? @@ -127,7 +130,7 @@ where info: "file disappeared".into(), } })?; - let fname = self.mgr.add_file(disk_fname, &data)?; + let fname = self.mgr.add_file(ctx, disk_fname, &data)?; debug!("renamed {} to {}", disk_fname, fname); assert_ne!(fname.as_ref(), disk_fname); diff --git a/rslib/src/media/mod.rs b/rslib/src/media/mod.rs index 13b8df631..911921c5e 100644 --- a/rslib/src/media/mod.rs +++ b/rslib/src/media/mod.rs @@ -39,7 +39,12 @@ impl MediaManager { /// appended to the name. /// /// Also notes the file in the media database. - pub fn add_file<'a>(&self, desired_name: &'a str, data: &[u8]) -> Result> { + pub fn add_file<'a>( + &self, + ctx: &mut MediaDatabaseContext, + desired_name: &'a str, + data: &[u8], + ) -> Result> { let pre_add_folder_mtime = mtime_as_i64(&self.media_folder)?; // add file to folder @@ -50,7 +55,7 @@ impl MediaManager { let post_add_folder_mtime = mtime_as_i64(&self.media_folder)?; // add to the media DB - self.dbctx().transact(|ctx| { + ctx.transact(|ctx| { let existing_entry = ctx.get_entry(&chosen_fname)?; let new_sha1 = Some(data_hash); @@ -94,7 +99,7 @@ impl MediaManager { syncer.sync(hkey).await } - fn dbctx(&self) -> MediaDatabaseContext { + pub fn dbctx(&self) -> MediaDatabaseContext { MediaDatabaseContext::new(&self.db) } }