From 3c65bb97250202554f1cd0c4802da98d571f1aee Mon Sep 17 00:00:00 2001 From: Jan Kupec Date: Wed, 1 Aug 2007 12:33:18 +0000 Subject: [PATCH] - better error handling in init_repos() --- src/zypper-sources.cc | 52 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/zypper-sources.cc b/src/zypper-sources.cc index 519e1fc..a55e646 100644 --- a/src/zypper-sources.cc +++ b/src/zypper-sources.cc @@ -57,11 +57,12 @@ static int do_init_repos() else gData.repos = manager.knownRepositories(); + for (std::list::iterator it = gData.repos.begin(); it != gData.repos.end(); ++it) { RepoInfo repo(*it); - MIL << "initializing " << repo.alias() << endl; + MIL << "checking if to refresh " << repo.alias() << endl; //! \todo honor command line options/commands bool do_refresh = repo.enabled() && repo.autorefresh(); @@ -73,21 +74,46 @@ static int do_init_repos() << endl; MIL << "calling refresh for " << repo.alias() << endl; - try { manager.refreshMetadata(repo); } - catch (const RepoException & ex) + // handle root user differently + if (geteuid() == 0) { - cerr << format(_("Repository %s is invalid.")) % repo.alias() << endl; - cerr_v << _("Reason: ") << ex.asUserString() << endl; - ERR << repo.alias() << " is invalid, disabling it" << endl; - it->setEnabled(false); + try { manager.refreshMetadata(repo, RepoManager::RefreshIfNeeded); } + //! \todo handle the rest of special exceptions + catch (const RepoException & ex) + { + cerr << format(_("Repository '%s' is invalid.")) % repo.alias() << endl; + cerr_v << _("Reason: ") << ex.asUserString() << endl; + ERR << repo.alias() << " is invalid, disabling it" << endl; + it->setEnabled(false); + } + catch (const Exception & ex) + { + cerr << format(_("Error while refreshing repository %s:")) % repo.alias() + << endl; + cerr << ex.asUserString() << endl; + ERR << "Error while refreshing " << repo.alias() << ", disabling it" << endl; + it->setEnabled(false); + } } - catch (const Exception & ex) + // non-root user + else { - cerr << format(_("Error while refreshing repository %s:")) % repo.alias() - << endl; - cerr << ex.asUserString() << endl; - ERR << "Error while refreshing " << repo.alias() << ", disabling it" << endl; - it->setEnabled(false); + try { manager.refreshMetadata(repo, RepoManager::RefreshIfNeeded); } + // any exception thrown means zypp attempted to refresh the repo + // i.e. it is out-of-date. Thus, just display refresh hint for non-root + // user + catch (const Exception & ex) + { + cout << format(_( + "Repository '%s' is out-of-date. You can run 'zypper refresh'" + " as root to update it.")) % repo.alias() + << endl; + + string nonroot = + "We're running as non-root, skipping refresh of " + repo.alias(); + MIL << nonroot << endl; + cout_vv << nonroot << endl; + } } } } -- 2.7.4