Skip to content

Commit cb93a96

Browse files
committed
context: print metadata cache age after loading repos
DNF4 displayed "Last metadata expiration check: H:MM:SS ago on <date>." after loading repository metadata. DNF5 dropped this output, making it hard for users to know whether they are working with stale repo data. Restore the behaviour by iterating over enabled non-system repos after load_repos() completes. Show the oldest cache age (most stale repo) paired with the newest repomd primary timestamp, matching dnf4 logic. Use libdnf5::utils::sformat for the translated output string. The message is suppressed when no repos have valid timestamps (e.g. all repos were just refreshed from the network and have age 0, or no cache exists yet). Closes: #1491 Signed-off-by: Devesh B <98201065+DeveshB-1@users.noreply.github.com>
1 parent 24b7122 commit cb93a96

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

dnf5/context.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
#include <libdnf5/rpm/package_query.hpp>
3939
#include <libdnf5/rpm/package_set.hpp>
4040
#include <libdnf5/rpm/rpm_signature.hpp>
41+
#include <libdnf5/repo/repo_query.hpp>
42+
43+
#include <ctime>
44+
#include <limits>
4145
#include <libdnf5/transaction/offline.hpp>
4246
#include <libdnf5/transaction/transaction_item_action.hpp>
4347
#include <libdnf5/utils/bgettext/bgettext-lib.h>
@@ -263,6 +267,45 @@ void Context::Impl::load_repos(bool load_system, bool load_available) {
263267
download_callbacks->reset_progress_bar();
264268
}
265269
if (load_available) {
270+
// Print metadata cache age, mirroring dnf4's "Last metadata expiration check" output.
271+
// Show oldest age (most stale repo) with the newest repomd timestamp.
272+
int64_t min_age = std::numeric_limits<int64_t>::max();
273+
int64_t max_ts = 0;
274+
275+
libdnf5::repo::RepoQuery loaded_repos(base);
276+
loaded_repos.filter_enabled(true);
277+
loaded_repos.filter_type(libdnf5::repo::Repo::Type::SYSTEM, libdnf5::sack::QueryCmp::NEQ);
278+
279+
for (const auto & repo : loaded_repos) {
280+
int64_t ts = repo->get_timestamp();
281+
if (ts > 0) {
282+
int64_t age = repo->get_age();
283+
if (age < min_age) {
284+
min_age = age;
285+
}
286+
if (ts > max_ts) {
287+
max_ts = ts;
288+
}
289+
}
290+
}
291+
292+
if (min_age != std::numeric_limits<int64_t>::max() && max_ts > 0) {
293+
long total_secs = static_cast<long>(min_age);
294+
long hours = total_secs / 3600;
295+
long mins = (total_secs % 3600) / 60;
296+
long secs = total_secs % 60;
297+
298+
time_t ts_time = static_cast<time_t>(max_ts);
299+
struct tm tm_buf;
300+
localtime_r(&ts_time, &tm_buf);
301+
char date_buf[128];
302+
strftime(date_buf, sizeof(date_buf), "%c", &tm_buf);
303+
304+
print_info(libdnf5::utils::sformat(
305+
_("Last metadata expiration check: {}:{:02d}:{:02d} ago on {}."),
306+
hours, mins, secs, date_buf));
307+
}
308+
266309
print_info(_("Repositories loaded."));
267310
}
268311
}
@@ -1197,3 +1240,4 @@ std::vector<std::string> Context::match_specs(
11971240
}
11981241

11991242
} // namespace dnf5
1243+

0 commit comments

Comments
 (0)