From cac7fba11c6fd315eb54fc6843c1705666133c24 Mon Sep 17 00:00:00 2001 From: diviocity Date: Tue, 10 Nov 2026 23:32:33 +0100 Subject: [PATCH] fix: do not print file retrieval errors unless fallbacks fail --- src/frontend_test.v | 70 +++++++++++++++++++++++++++++++++++++++++++++ src/ui.v | 34 ++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/src/frontend_test.v b/src/frontend_test.v index 010b9cc..e72bfe5 100644 --- a/src/frontend_test.v +++ b/src/frontend_test.v @@ -2051,6 +2051,76 @@ fn test_a_failed_refresh_is_an_error() ! { assert se.err != '' assert fy.err != '' assert s.installed()!['foo']! == '1.0-1' + // no server left to fall through to: the held 404 is said, an error + said := se.of('message').filter(it['text'] or { json2.Any('') }.str().contains('failed retrieving')) + assert said.len == 1 + assert said[0]['error'] or { json2.Any(false) }.bool() +} + +// a mirror that lacks the package falls through to the next server: libalpm +// tries a repo's servers in order, and a file the first one does not have +// moves it on — the way a mirror that has not synced the archive yet is met. +// the first server here serves the db alone, the way a lagging mirror does, +// and the archive lives on the second +fn test_a_mirror_without_the_package_falls_through_to_the_next() ! { + s := fake_system('fallback')! + defer { os.rmdir_all(s.tmp) or {} } + s.install_local(foo('1.0-1', 'one'))! + s.publish([foo('2.0-1', 'two')])! + // the lagging mirror: the db the primary serves, none of the archives + lag := s.path('lag') + os.mkdir_all(lag)! + os.cp(s.path('repo/core.db'), os.join_path(lag, 'core.db'))! + os.cp(s.path('repo/core.files'), os.join_path(lag, 'core.files'))! + up := s.run('', fn [s, lag] () ! { + cfg_repos = [CfgRepo{ + name: 'core' + kind: 'sync' + servers: ['file://' + lag, 'file://' + s.path('repo')] + }] + upgrade(ConfigUpgrade{ noconfirm: true })! + })! + assert up.err == '', up.err + assert s.installed()!['foo']! == '2.0-1' + // the fallback is an event a frontend sees: the archive's download says + // retry, then completes + retries := up.of('download').filter(it['retry'] or { json2.Any(false) }.bool()) + assert retries.len == 1 + assert retries[0]['file'].str() == 'foo-2.0-1-any.pkg.tar' + // the 404 the lagging mirror got is not said: a mirror served the file + said := up.of('message').filter(it['text'] or { json2.Any('') }.str().contains('failed retrieving')) + assert said.len == 0 +} + +// --verbose says the mirror's 404 as it comes, even when the fallback finds +// the file on the next mirror: the verbose log tells a run's whole story +fn test_verbose_says_the_fallback_error() ! { + s := fake_system('fallbackverbose')! + defer { os.rmdir_all(s.tmp) or {} } + s.install_local(foo('1.0-1', 'one'))! + s.publish([foo('2.0-1', 'two')])! + lag := s.path('lag') + os.mkdir_all(lag)! + os.cp(s.path('repo/core.db'), os.join_path(lag, 'core.db'))! + os.cp(s.path('repo/core.files'), os.join_path(lag, 'core.files'))! + saved_verbose := verbose_flag + defer { + verbose_flag = saved_verbose + } + up := s.run('', fn [s, lag] () ! { + verbose_flag = true + cfg_repos = [CfgRepo{ + name: 'core' + kind: 'sync' + servers: ['file://' + lag, 'file://' + s.path('repo')] + }] + upgrade(ConfigUpgrade{ noconfirm: true })! + })! + assert up.err == '', up.err + assert s.installed()!['foo']! == '2.0-1' + said := up.of('message').filter(it['text'] or { json2.Any('') }.str().contains('failed retrieving')) + assert said.len == 1 + assert said[0]['text'].str().contains('from disk') } // check is an assertion inside a run's closure, where a failed assert would diff --git a/src/ui.v b/src/ui.v index 6bc7a33..8029280 100644 --- a/src/ui.v +++ b/src/ui.v @@ -165,6 +165,12 @@ mut: // send twice dlast map[string]DownloadSent plast ProgressEvent + // dfails holds the 'failed retrieving file' lines libalpm logged for + // mirrors that did not have a download: the line is logged before anyone + // knows a later mirror serves the file, so it is held until the download + // ends, and said only when no mirror served it. --verbose says it as it + // comes + dfails map[string]string } // TransUIOptions are what a command tells its TransUI @@ -533,6 +539,7 @@ fn cb_download(ctx voidptr, filename &u8, event alpm.DownloadEventType, data voi // and nothing was fetched, 0 a finished download, -1 an error. a // download can finish before its first progress event, so the bar // may not exist yet; only result 1 is labelled up to date + ui.download_ended(name, c.result == -1) on_end := if c.result == 1 { '${name} is up to date' } else { '' } bar := ui.download_bar(name, c.total, on_end, false) bar.set(c.total) @@ -550,6 +557,19 @@ fn (mut ui TransUI) restart_download_bar(name string) { bar.set(0) } +// download_ended releases the 'failed retrieving file' line a download held +// while it fell through the mirrors: a download no mirror served says its +// line as an error now, one a mirror served keeps it quiet +fn (mut ui TransUI) download_ended(name string, failed bool) { + ui.mu.lock() + held := ui.dfails[name] or { '' } + ui.dfails.delete(name) + ui.mu.unlock() + if held != '' && failed { + ui.alpm_said(true, held) + } +} + // DownloadSent is what a download last reported under --ui ipc: its percent, // and when, in the monotonic clock's nanoseconds struct DownloadSent { @@ -610,6 +630,9 @@ fn ipc_download(mut ui TransUI, name string, event alpm.DownloadEventType, data } .completed { c := unsafe { &alpm.DownloadEventCompleted(data) } + // the error goes out before the completed event, the order a + // frontend reads a failure in + ui.download_ended(name, c.result == -1) emit(DownloadEvent{ file: name downloaded: c.total @@ -814,6 +837,17 @@ fn alpm_log_line(ctx voidptr, level int, text &char) { if msg == '' { return } + // a mirror that lacks a download is logged as an error before anyone + // knows a later mirror serves the file: without --verbose it is held + // until the download ends, and said as an error only when no mirror + // served it (download_ended) + if level == alpm_log_error && msg.starts_with("failed retrieving file '") && !verbose_on() { + file := msg.all_after("failed retrieving file '").all_before("'") + ui.mu.lock() + ui.dfails[file] = msg + ui.mu.unlock() + return + } ui.alpm_said(level == alpm_log_error, msg) } -- 2.56.1