From f075a75aeedfc88f4c9932d2d8922f3c60371fe9 Mon Sep 17 00:00:00 2001 From: Laurent Louf Date: Sat, 25 Jul 2020 01:20:08 +0200 Subject: [PATCH 1/3] Disable audio cache when there is an error trying to create a file --- core/src/cache.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/src/cache.rs b/core/src/cache.rs index 9ee0ae18..3a5385b4 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -86,8 +86,13 @@ impl Cache { mkdir_existing(path.parent().unwrap()).unwrap(); - let mut cache_file = File::create(path).unwrap(); - ::std::io::copy(contents, &mut cache_file).unwrap(); + let mut cache_file = File::create(path) + match cache_file { + Ok(file) => ::std::io::copy(contents, &mut file).unwrap(), + Err(error) => { + self.use_audio_cache = false + }, + }; } } } From 9d832baf83adf653243ac6e0b6de0b12b53b0a37 Mon Sep 17 00:00:00 2001 From: Laurent Louf Date: Mon, 3 Aug 2020 13:18:23 +0200 Subject: [PATCH 2/3] Instead of disabling audio cache, just clean the audio cache and start caching again from the current file --- core/src/cache.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/core/src/cache.rs b/core/src/cache.rs index 3a5385b4..65d31ba0 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -83,15 +83,22 @@ impl Cache { pub fn save_file(&self, file: FileId, contents: &mut dyn Read) { if self.use_audio_cache { let path = self.file_path(file); - mkdir_existing(path.parent().unwrap()).unwrap(); - let mut cache_file = File::create(path) - match cache_file { - Ok(file) => ::std::io::copy(contents, &mut file).unwrap(), - Err(error) => { - self.use_audio_cache = false - }, + let file_create = File::create(path); + match file_create { + Ok(mut cache_file) => { + ::std::io::copy(contents, &mut cache_file).unwrap(); + } + Err(_error) => { + ::std::fs::remove_dir_all(&self.root.join("files")).unwrap(); + mkdir_existing(&self.root.join("files")).unwrap(); + + let path = self.file_path(file); + mkdir_existing(path.parent().unwrap()).unwrap(); + let mut cache_file = File::create(path).unwrap(); + ::std::io::copy(contents, &mut cache_file).unwrap(); + } }; } } From 5f11ddea50c12ea43b7ab57400c211c7f1f671b2 Mon Sep 17 00:00:00 2001 From: Laurent Louf Date: Tue, 4 Aug 2020 12:25:32 +0200 Subject: [PATCH 3/3] Main issue is probably when copying the content to the file, not during the file creation, but handle both cases just to be sure --- core/src/cache.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/core/src/cache.rs b/core/src/cache.rs index 65d31ba0..194240ea 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -85,21 +85,23 @@ impl Cache { let path = self.file_path(file); mkdir_existing(path.parent().unwrap()).unwrap(); - let file_create = File::create(path); - match file_create { - Ok(mut cache_file) => { - ::std::io::copy(contents, &mut cache_file).unwrap(); - } - Err(_error) => { - ::std::fs::remove_dir_all(&self.root.join("files")).unwrap(); - mkdir_existing(&self.root.join("files")).unwrap(); + let mut cache_file = File::create(path).unwrap_or_else(|_e| { + ::std::fs::remove_dir_all(&self.root.join("files")).unwrap(); + mkdir_existing(&self.root.join("files")).unwrap(); - let path = self.file_path(file); - mkdir_existing(path.parent().unwrap()).unwrap(); - let mut cache_file = File::create(path).unwrap(); - ::std::io::copy(contents, &mut cache_file).unwrap(); - } - }; + let path = self.file_path(file); + mkdir_existing(path.parent().unwrap()).unwrap(); + File::create(path).unwrap() + }); + ::std::io::copy(contents, &mut cache_file).unwrap_or_else(|_e| { + ::std::fs::remove_dir_all(&self.root.join("files")).unwrap(); + mkdir_existing(&self.root.join("files")).unwrap(); + + let path = self.file_path(file); + mkdir_existing(path.parent().unwrap()).unwrap(); + let mut file = File::create(path).unwrap(); + ::std::io::copy(contents, &mut file).unwrap() + }); } } }