From ea66959cf475113e89be717c84f221b6d0aab2ff Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 30 Aug 2021 13:24:00 +0200 Subject: [PATCH] Only insert/replace content for a file if it was able to be read successfully --- .../src/services/config_watcher.rs | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/syndicate-server/src/services/config_watcher.rs b/syndicate-server/src/services/config_watcher.rs index 4fa7d10..ca902c8 100644 --- a/syndicate-server/src/services/config_watcher.rs +++ b/syndicate-server/src/services/config_watcher.rs @@ -58,7 +58,11 @@ fn assertions_at_existing_file(t: &mut Activation, ds: &Arc, path: &PathBuf let fh = fs::File::open(path)?; let mut src = IOBinarySource::new(fh); let mut r = src.text::<_, AnyValue, _>(ViaCodec::new(NoEmbeddedDomainCodec)); + let mut values = Vec::new(); while let Some(value) = Reader::<_, AnyValue>::next(&mut r, true)? { + values.push(value); + } + for value in values.into_iter() { if let Some(handle) = ds.assert(t, value.clone()) { tracing::debug!("asserted {:?} -> {:?}", value, handle); handles.insert(handle); @@ -94,16 +98,22 @@ fn scan_file( path_state: &mut Map>, ds: &Arc, path: &PathBuf, -) { +) -> bool { if is_hidden(path) { - return; + return true; } tracing::info!("scan_file: {:?}", path); match assertions_at_path(t, ds, path) { - Ok(new_handles) => if !new_handles.is_empty() { - path_state.insert(path.clone(), new_handles); + Ok(new_handles) => { + if !new_handles.is_empty() { + path_state.insert(path.clone(), new_handles); + } + true }, - Err(e) => tracing::warn!("scan_file: {:?}: {:?}", path, e), + Err(e) => { + tracing::warn!("scan_file: {:?}: {:?}", path, e); + false + } } } @@ -118,7 +128,7 @@ fn initial_scan( } match fs::metadata(path) { Ok(md) => if md.is_file() { - scan_file(t, path_state, ds, path) + scan_file(t, path_state, ds, path); } else { match fs::read_dir(path) { Ok(entries) => for er in entries { @@ -169,10 +179,15 @@ fn run(t: &mut Activation, ds: Arc, captures: AnyValue) -> ActorResult { facet.activate(Arc::clone(&account), |t| { let mut to_retract = Set::new(); for path in paths.into_iter() { - if let Some(handles) = path_state.remove(&path) { - to_retract.extend(handles.into_iter()); + let maybe_handles = path_state.remove(&path); + let new_content_ok = scan_file(t, &mut path_state, &ds, &path); + if let Some(old_handles) = maybe_handles { + if new_content_ok { + to_retract.extend(old_handles.into_iter()); + } else { + path_state.insert(path, old_handles); + } } - scan_file(t, &mut path_state, &ds, &path); } for h in to_retract.into_iter() { tracing::debug!("retract {:?}", h);