Skip to content

Commit 8bdce4b

Browse files
fix: address outstanding Sourcery review findings (#17)
* fix: address outstanding Sourcery review findings * fix: address outstanding Sourcery review findings * fix: address outstanding Sourcery review findings * fix: address outstanding Sourcery review findings * fix: address outstanding Sourcery review findings * fix: address outstanding Sourcery review findings * fix: accept scoped IPv6 and IPvFuture URL hosts * fix: use reader cancellation state consistently * test: clear selected model after discarded download * fix: repair URL host validation implementation * test: cover malformed IPv6 zone and IPvFuture URLs * test: exercise Windows completion wiring on every platform * fix: preserve successful ingest completion state * fix: only clear the discarded managed model selection * fix: recognize symlinked cache ancestors * fix: avoid marking normally finished jobs cancelled * test: cover symlinked cache ancestors * test: preserve unrelated custom model selection on discard * test: preserve custom model selection on discard * fix: compare model selection paths as owned strings * fix: report direct-download cleanup failures * fix: repair IPvFuture URL regression assertions * fix: rewrite malformed URL regression test block * fix: satisfy formatting and borrow checks * fix: satisfy formatting and borrow checks * fix: satisfy formatting and borrow checks * fix: satisfy formatting and borrow checks * fix: satisfy formatting and borrow checks * fix: address follow-up Sourcery review findings * fix: address follow-up Sourcery review findings * fix: address follow-up Sourcery review findings * fix: address follow-up Sourcery review findings * fix: address follow-up Sourcery review findings * fix: handle cancellation warnings in all frontends * test: cover transient cache-lock retries * fix: surface only cancellation cleanup warnings * fix: repair URL validation and UNC detection * test: align cache doctor expectation * fix: clean returned audio when temp cleanup fails * fix: preserve pipeline errors during ingest teardown * fix: normalize discarded model selection paths * test: verify ingest drop releases blocked readers * test: cover equivalent managed model paths * fix: restore IPvFuture host predicate * style: format cancellation warning handling * style: format audio cleanup errors * style: format cache retry and path test * style: format URL and path validation * style: format terminal worker event match * fix: fail cache checks on inspection errors * fix: handle symlinked selections and longer cache locks * fix: restore valid IPvFuture predicate * test: synchronize ingest backpressure teardown * fix: compare managed model paths across Windows casing * style: format cache permission test * style: format symlink discard test * test: serialize shared model cache overrides * fix: satisfy clippy cleanup control flow * style: apply rustfmt to cleanup condition
1 parent 79dfb06 commit 8bdce4b

7 files changed

Lines changed: 663 additions & 55 deletions

File tree

‎src/app.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ impl App {
341341
WorkerEventKind::Segment(s) => self.on_segment(s),
342342
WorkerEventKind::Done => self.on_done(),
343343
WorkerEventKind::Cancelled => self.on_cancelled(),
344+
WorkerEventKind::CancelledWithWarning(message) => {
345+
self.on_cancelled_with_warning(message)
346+
}
344347
WorkerEventKind::Error(e) => self.on_error(e),
345348
}
346349
true
@@ -467,6 +470,12 @@ impl App {
467470
));
468471
}
469472

473+
pub fn on_cancelled_with_warning(&mut self, message: String) {
474+
self.on_cancelled();
475+
self.set_message(format!("cancelled, but cleanup failed: {message}"));
476+
self.overlay = Overlay::Message;
477+
}
478+
470479
pub fn on_error(&mut self, message: String) {
471480
self.settle_pause();
472481
self.status = Status::Error;

‎src/doctor.rs‎

Lines changed: 113 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,36 @@ struct Row {
2626
}
2727

2828
fn cache_status(cache: &std::path::Path) -> (Verdict, &'static str) {
29-
if cache.is_dir() {
30-
(Verdict::Ok, "directory exists; write access not tested")
31-
} else if cache.exists() {
32-
(Verdict::Fail, "path exists but is not a directory")
33-
} else if cache.parent().is_some_and(std::path::Path::is_dir) {
34-
(
35-
Verdict::Warn,
36-
"will be created on first download; write access not tested",
37-
)
29+
let cache = if cache.is_absolute() {
30+
cache.to_path_buf()
3831
} else {
39-
(Verdict::Fail, "parent directory is missing")
32+
match std::env::current_dir() {
33+
Ok(current) => current.join(cache),
34+
Err(_) => return (Verdict::Fail, "cannot resolve relative cache path"),
35+
}
36+
};
37+
if cache.is_dir() {
38+
return (Verdict::Ok, "directory exists; write access not tested");
39+
}
40+
match std::fs::symlink_metadata(&cache) {
41+
Ok(_) => return (Verdict::Fail, "path exists but is not a usable directory"),
42+
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
43+
Err(_) => return (Verdict::Fail, "cannot inspect cache path"),
4044
}
45+
for ancestor in cache.ancestors().skip(1) {
46+
match std::fs::symlink_metadata(ancestor) {
47+
Ok(metadata) if metadata.is_dir() || ancestor.is_dir() => {
48+
return (
49+
Verdict::Warn,
50+
"will be created on first download; write access not tested",
51+
);
52+
}
53+
Ok(_) => return (Verdict::Fail, "an ancestor is not a directory"),
54+
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
55+
Err(_) => return (Verdict::Fail, "cannot inspect cache path"),
56+
}
57+
}
58+
(Verdict::Fail, "no existing parent directory")
4159
}
4260

4361
fn version(binary: &std::path::Path, args: &[&str]) -> Option<String> {
@@ -313,11 +331,95 @@ mod tests {
313331
std::fs::write(&cache, b"not a directory").unwrap();
314332
assert_eq!(
315333
cache_status(&cache),
316-
(Verdict::Fail, "path exists but is not a directory")
334+
(Verdict::Fail, "path exists but is not a usable directory")
335+
);
336+
std::fs::remove_dir_all(&dir).unwrap();
337+
}
338+
339+
#[test]
340+
fn cache_status_rejects_uninspectable_cache_paths() {
341+
let dir = std::env::temp_dir().join(format!(
342+
"scribe-doctor-uninspectable-{}",
343+
std::process::id()
344+
));
345+
let _ = std::fs::remove_dir_all(&dir);
346+
std::fs::create_dir_all(&dir).unwrap();
347+
let parent = dir.join("not-a-directory");
348+
std::fs::write(&parent, b"x").unwrap();
349+
let cache = parent.join("models");
350+
assert_eq!(
351+
cache_status(&cache),
352+
(Verdict::Fail, "cannot inspect cache path")
353+
);
354+
std::fs::remove_dir_all(&dir).unwrap();
355+
}
356+
357+
#[cfg(unix)]
358+
#[test]
359+
fn cache_status_rejects_dangling_symlinks() {
360+
use std::os::unix::fs::symlink;
361+
let dir = std::env::temp_dir().join(format!("scribe-doctor-link-{}", std::process::id()));
362+
std::fs::create_dir_all(&dir).unwrap();
363+
let cache = dir.join("models");
364+
symlink(dir.join("missing"), &cache).unwrap();
365+
assert_eq!(
366+
cache_status(&cache),
367+
(Verdict::Fail, "path exists but is not a usable directory")
368+
);
369+
std::fs::remove_file(&cache).unwrap();
370+
std::fs::remove_dir_all(&dir).unwrap();
371+
}
372+
373+
#[test]
374+
fn cache_status_accepts_relative_missing_paths() {
375+
let cache = std::path::PathBuf::from(format!(
376+
"scribe-doctor-relative-{}/nested/models",
377+
std::process::id()
378+
));
379+
assert_eq!(
380+
cache_status(&cache),
381+
(
382+
Verdict::Warn,
383+
"will be created on first download; write access not tested"
384+
)
385+
);
386+
}
387+
388+
#[cfg(unix)]
389+
#[test]
390+
fn cache_status_accepts_symlinked_directory_ancestors() {
391+
use std::os::unix::fs::symlink;
392+
let dir =
393+
std::env::temp_dir().join(format!("scribe-doctor-symlink-{}", std::process::id()));
394+
let _ = std::fs::remove_dir_all(&dir);
395+
std::fs::create_dir_all(dir.join("target")).unwrap();
396+
symlink(dir.join("target"), dir.join("linked")).unwrap();
397+
let cache = dir.join("linked").join("nested").join("models");
398+
assert_eq!(
399+
cache_status(&cache),
400+
(
401+
Verdict::Warn,
402+
"will be created on first download; write access not tested"
403+
)
317404
);
318405
std::fs::remove_dir_all(&dir).unwrap();
319406
}
320407

408+
#[test]
409+
fn cache_status_accepts_missing_parent_trees() {
410+
let dir = std::env::temp_dir().join(format!("scribe-doctor-nested-{}", std::process::id()));
411+
let _ = std::fs::remove_dir_all(&dir);
412+
assert!(!dir.exists());
413+
let cache = dir.join("one").join("two").join("models");
414+
assert_eq!(
415+
cache_status(&cache),
416+
(
417+
Verdict::Warn,
418+
"will be created on first download; write access not tested"
419+
)
420+
);
421+
}
422+
321423
#[test]
322424
fn verdict_labels_are_short() {
323425
assert_eq!(Verdict::Ok.label(), "ok");

‎src/fetch.rs‎

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ async fn fetch_async(url: &str, cancel: &Arc<AtomicBool>) -> Result<FetchedAudio
154154
_ = cancelled(cancel) => None,
155155
};
156156
let Some(crate_result) = crate_result else {
157-
let _ = std::fs::remove_file(&direct);
158-
return Err("cancelled".into());
157+
let cleanup = cleanup_paths(&[&direct]);
158+
return Err(format!("cancelled{}", cleanup_suffix(cleanup)));
159159
};
160160
let path = match crate_result {
161161
Ok(p) if p.is_absolute() => p,
@@ -187,44 +187,58 @@ async fn fetch_async(url: &str, cancel: &Arc<AtomicBool>) -> Result<FetchedAudio
187187
{
188188
Ok(child) => child,
189189
Err(e) => {
190-
cleanup_fallback(&fallback, &direct);
191-
return Err(format!("could not download audio: {e}"));
190+
let cleanup = cleanup_fallback(&fallback, &direct);
191+
return Err(format!(
192+
"could not download audio: {e}{}",
193+
cleanup_suffix(cleanup)
194+
));
192195
}
193196
};
194197
let status = tokio::select! {
195198
result = child.wait() => match result {
196199
Ok(status) => status,
197200
Err(e) => {
198201
stop_child(&mut child).await;
199-
cleanup_fallback(&fallback, &direct);
200-
return Err(format!("could not download audio: {e}"));
202+
let cleanup = cleanup_fallback(&fallback, &direct);
203+
return Err(format!("could not download audio: {e}{}", cleanup_suffix(cleanup)));
201204
}
202205
},
203206
_ = cancelled(cancel) => {
204207
stop_child(&mut child).await;
205-
cleanup_fallback(&fallback, &direct);
206-
return Err("cancelled".into());
208+
let cleanup = cleanup_fallback(&fallback, &direct);
209+
return Err(format!("cancelled{}", cleanup_suffix(cleanup)));
207210
}
208211
};
209212
if !status.success() || !fallback.is_file() {
210-
cleanup_fallback(&fallback, &direct);
213+
let cleanup = cleanup_fallback(&fallback, &direct);
211214
return Err(format!(
212-
"no audio came out. the http path said: {crate_err}"
215+
"no audio came out. the http path said: {crate_err}{}",
216+
cleanup_suffix(cleanup)
213217
));
214218
}
215219
fallback
216220
}
217221
};
218222

219223
if cancel.load(Ordering::Relaxed) {
220-
let _ = std::fs::remove_file(&path);
221-
let _ = std::fs::remove_file(&direct);
222-
return Err("cancelled".into());
224+
let cleanup = cleanup_paths(&[&path, &direct]);
225+
return Err(format!("cancelled{}", cleanup_suffix(cleanup)));
223226
}
224227
// the crate may have written to `direct` even when it reported another
225228
// path; keep only the file we actually return.
226-
if direct != path {
227-
let _ = std::fs::remove_file(&direct);
229+
if direct != path
230+
&& let Err(error) = cleanup_paths(&[&direct])
231+
{
232+
let returned_cleanup = cleanup_paths(&[&path]);
233+
let detail = match returned_cleanup {
234+
Ok(()) => error,
235+
Err(returned_error) => {
236+
format!("{error}; returned audio cleanup also failed: {returned_error}")
237+
}
238+
};
239+
return Err(format!(
240+
"audio was downloaded but temporary-file cleanup failed: {detail}"
241+
));
228242
}
229243

230244
Ok(FetchedAudio {
@@ -272,9 +286,34 @@ pub fn ffmpeg_path() -> Option<PathBuf> {
272286
p.is_file().then_some(p)
273287
}
274288

275-
fn cleanup_fallback(fallback: &Path, direct: &Path) {
276-
let _ = std::fs::remove_file(fallback);
277-
let _ = std::fs::remove_file(direct);
289+
fn cleanup_paths(paths: &[&Path]) -> Result<(), String> {
290+
let mut failures = Vec::new();
291+
for path in paths {
292+
match std::fs::remove_file(path) {
293+
Ok(()) => {}
294+
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
295+
Err(error) => failures.push(format!("{}: {error}", path.display())),
296+
}
297+
}
298+
if failures.is_empty() {
299+
Ok(())
300+
} else {
301+
Err(format!(
302+
"could not remove partial download(s): {}",
303+
failures.join("; ")
304+
))
305+
}
306+
}
307+
308+
fn cleanup_fallback(fallback: &Path, direct: &Path) -> Result<(), String> {
309+
cleanup_paths(&[fallback, direct])
310+
}
311+
312+
fn cleanup_suffix(result: Result<(), String>) -> String {
313+
result
314+
.err()
315+
.map(|error| format!("; {error}"))
316+
.unwrap_or_default()
278317
}
279318

280319
/// remove a partial audio file left behind by a cancelled/interrupted fetch.
@@ -330,13 +369,31 @@ mod tests {
330369
std::fs::write(&fallback, b"partial").unwrap();
331370
std::fs::write(&direct, b"partial").unwrap();
332371

333-
cleanup_fallback(&fallback, &direct);
372+
cleanup_fallback(&fallback, &direct).unwrap();
334373

335374
assert!(!fallback.exists());
336375
assert!(!direct.exists());
337376
std::fs::remove_dir_all(&dir).unwrap();
338377
}
339378

379+
#[test]
380+
fn fallback_cleanup_reports_filesystem_failures() {
381+
let dir =
382+
std::env::temp_dir().join(format!("scribe-fallback-error-{}", std::process::id()));
383+
let _ = std::fs::remove_dir_all(&dir);
384+
std::fs::create_dir_all(&dir).unwrap();
385+
let fallback = dir.join("fallback.webm");
386+
let direct = dir.join("direct.m4a");
387+
std::fs::create_dir_all(&fallback).unwrap();
388+
std::fs::write(&direct, b"partial").unwrap();
389+
390+
let error = cleanup_fallback(&fallback, &direct).unwrap_err();
391+
392+
assert!(error.contains("fallback.webm"));
393+
assert!(!direct.exists());
394+
std::fs::remove_dir_all(&dir).unwrap();
395+
}
396+
340397
#[test]
341398
fn cleanup_removes_partial_audio() {
342399
let dir = std::env::temp_dir().join(format!("scribe-fetch-{}", std::process::id()));

0 commit comments

Comments
 (0)