From 33a3d825a9addb29d828b09d65ef5a0442475c79 Mon Sep 17 00:00:00 2001 From: Martin Vidner Date: Wed, 28 Jun 2006 13:14:03 +0000 Subject: [PATCH] ported to libzypp --- devel/devel.mvidner/installation_sources.cc | 318 ++++++++++++++++++++++------ 1 file changed, 251 insertions(+), 67 deletions(-) diff --git a/devel/devel.mvidner/installation_sources.cc b/devel/devel.mvidner/installation_sources.cc index 42f20c7..8148fb3 100644 --- a/devel/devel.mvidner/installation_sources.cc +++ b/devel/devel.mvidner/installation_sources.cc @@ -1,121 +1,305 @@ +/** + * installation_sources + */ + #include +#include #include #include #include -#undef Y2LOG -#define Y2LOG "PM_installation_sources" -#include - -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include -#include -#include -#include +#undef ZYPP_BASE_LOGGER_LOGGROUP +#define ZYPP_BASE_LOGGER_LOGGROUP "installation_sources" using namespace std; +using namespace zypp; + +bool callbackAnswer = false; + +static +bool readCallbackAnswer() +{ + return callbackAnswer; +} + +/////////////////////////////////////////////////////////////////// +// KeyRingReceive +/////////////////////////////////////////////////////////////////// +struct KeyRingReceive : public zypp::callback::ReceiveReport +{ + bool _enabled; + KeyRingReceive() + : _enabled( true ) + { + } + + void disable( bool value ) + { + _enabled = !value; + MIL << "KeyRingReceive is now " << (_enabled ? "en" : "dis") << "abled." << std::endl; + } + + virtual bool askUserToAcceptUnsignedFile( const std::string &file ) + { + if (!_enabled) return true; + DBG << "21|" << file << std::endl; + std::cout << "21|" << file << std::endl; + return readCallbackAnswer(); + } + virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint ) + { + if (!_enabled) return true; + DBG << "22|" << file << "|" << keyid << "|" << keyname << "|" << fingerprint << std::endl; + std::cout << "22|" << file << "|" << keyid << "|" << keyname << "|" << fingerprint << std::endl; + return readCallbackAnswer(); + } + virtual bool askUserToTrustKey( const std::string &keyid, const std::string &keyname, const std::string &fingerprint ) + { + if (!_enabled) return true; + DBG << "23|" << keyid << "|" << keyname << "|" << fingerprint << std::endl; + std::cout << "23|" << keyid << "|" << keyname << "|" << fingerprint << std::endl; + return readCallbackAnswer(); + } + virtual bool askUserToAcceptVerificationFailed( const std::string &file, const std::string &keyid, const std::string &keyname, const std::string &fingerprint ) + { + if (!_enabled) return true; + DBG << "24|" << file << "|" << keyid << "|" << keyname << "|" << fingerprint << std::endl; + std::cout << "24|" << file << "|" << keyid << "|" << keyname << "|" << fingerprint << std::endl; + return readCallbackAnswer(); + } +}; + + +struct DigestReceive : public zypp::callback::ReceiveReport +{ + bool _enabled; + DigestReceive() + : _enabled( true ) + { + } + + void disable( bool value ) + { + _enabled = !value; + MIL << "DigestReceive is now " << (_enabled ? "en" : "dis") << "abled." << std::endl; + } + + virtual bool askUserToAcceptNoDigest( const zypp::Pathname &file ) + { + if (!_enabled) return true; + DBG << "25|" << file << std::endl; + std::cout << "25|" << file << std::endl; + return readCallbackAnswer(); + } + virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name ) + { + if (!_enabled) return true; + DBG << "26|" << file << "|" << name << std::endl; + std::cout << "26|" << file << "|" << name << std::endl; + return readCallbackAnswer(); + } + virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found ) + { + if (!_enabled) return true; + DBG << "27|" << file << "|" << requested << "|" << found << std::endl; + std::cout << "27|" << file << "|" << requested << "|" << found << std::endl; + return readCallbackAnswer(); + } +}; + +static +std::string timestamp () +{ + time_t t = time(NULL); + struct tm * tmp = localtime(&t); + + if (tmp == NULL) { + return ""; + } + + char outstr[50]; + if (strftime(outstr, sizeof(outstr), "%Y%m%d-%H%M%S", tmp) == 0) { + return ""; + } + return outstr; +} void usage() { + // TODO: -r remove cout << "Usage:" << endl - << " installation_sources [-e|-d] -a url Add source at given URL." << endl + << " installation_sources [options] -a url Add source at given URL." << endl + << " -n name Name the source." << endl << " -e Enable source. This is the default." << endl << " -d Disable source." << endl + << " -f Autorefresh source." << endl + << " -Y Answer Yes to all checksum and signature questions." << endl << " installation_sources -s Show all available sources." << endl; exit( 1 ); } /****************************************************************** -** -** -** FUNCTION NAME : main -** FUNCTION TYPE : int -** -** DESCRIPTION : -*/ + ** + ** + ** FUNCTION NAME : main + ** FUNCTION TYPE : int + ** + ** DESCRIPTION : + */ int main( int argc, char **argv ) { + const char *logfile = getenv("ZYPP_LOGFILE"); + if (logfile == NULL) + logfile = "/var/log/YaST2/y2log"; + zypp::base::LogControl::instance().logfile( logfile ); +/* + else + zypp::base::LogControl::instance().logfile( ZMD_BACKEND_LOG ); +*/ + MIL << "START" << endl; const char *urlStr = 0; + string alias; bool showSources = false; bool addSource = false; bool enableSource = true; // -e per default + bool autoRefresh = false; int c; while( 1 ) { - c = getopt( argc, argv, "desa:" ); + c = getopt( argc, argv, "desa:fYn:" ); if ( c < 0 ) break; switch ( c ) { - case 's': - showSources = true; - break; - case 'a': - addSource = true; - urlStr = optarg; - break; - case 'd': - enableSource = false; - break; - case 'e': - enableSource = true; - break; - default: - cerr << "Error parsing command line." << endl; - case '?': - case 'h': - usage(); + case 's': + showSources = true; + break; + case 'a': + addSource = true; + urlStr = optarg; + break; + case 'n': + alias = optarg; + break; + case 'd': + enableSource = false; + break; + case 'e': + enableSource = true; + break; + case 'f': + autoRefresh = true; + break; + case 'Y': + callbackAnswer = true; + break; + default: + cerr << "Error parsing command line." << endl; + case '?': + case 'h': + usage(); } } if ( showSources && addSource ) usage(); if ( !showSources && !addSource ) usage(); - Y2PM::noAutoInstSrcManager(); - InstSrcManager &instSrcMgr = Y2PM::instSrcManager(); + try { - InstSrcManager::ISrcIdList sourceIds; - if ( addSource ) { - Url url( urlStr ); - if ( !url.isValid() ) { - cerr << "URL is invalid." << endl; - exit( 1 ); + ZYpp::Ptr Z = NULL; + try { + Z = getZYpp(); + } + catch ( const Exception & excpt_r ) { + ZYPP_CAUGHT (excpt_r); + cerr << "A transaction is already in progress." << endl; + exit(1); } - PMError error = instSrcMgr.scanMedia( sourceIds, url ); - if ( error ) { - cerr << error << endl; - exit( 1 ); + KeyRingReceive keyring_rcv; + keyring_rcv.connect (); + DigestReceive digest_rcv; + digest_rcv.connect (); + + SourceManager_Ptr manager = SourceManager::sourceManager(); + manager->restore ("/", true /*use_cache*/); + + list sourceIds; + + if ( addSource ) { + Url url; + try { + url = Url( urlStr ); + } + catch ( const Exception & excpt_r ) { + ZYPP_CAUGHT( excpt_r ); + cerr << "URL is invalid." << endl; + cerr << excpt_r.asUserString() << endl; + exit( 1 ); + } + + Pathname path; + Pathname cache; + bool is_base = false; + if (alias.empty ()) + alias = timestamp (); + // more products? + // try + Source_Ref source = SourceFactory().createFrom( url, path, alias, cache, is_base ); + SourceManager::SourceId sourceId = manager->addSource( source ); + + if (enableSource) + source.enable(); + else + source.disable(); + source.setAutorefresh (autoRefresh); + + sourceIds.push_back( sourceId ); + cout << "Added Installation Sources:" << endl; + } + + if ( showSources ) { + sourceIds = manager->allSources (); + cout << "Installation Sources:" << endl; } - InstSrcManager::ISrcIdList::const_iterator it; + list::const_iterator it; for( it = sourceIds.begin(); it != sourceIds.end(); ++it ) { - error = instSrcMgr.enableSource( *it ); - if ( error ) { - cerr << error << endl; - exit( 1 ); - } - instSrcMgr.setAutoenable( *it, enableSource ); + Source_Ref source = manager->findSource(*it); + cout << ( source.enabled() ? "[x]" : "[ ]" ); + cout << ( source.autorefresh() ? "* " : " " ); + cout << source.alias() << " (" << source.url() << ")" << endl; + } + if ( addSource ) { + manager->store( "/", true /*metadata_cache*/ ); } - cout << "Added Installation Sources:" << endl; - } - if ( showSources ) { - instSrcMgr.getSources( sourceIds ); - cout << "Installation Sources:" << endl; + digest_rcv.disconnect (); + keyring_rcv.disconnect (); } - - InstSrcManager::ISrcIdList::const_iterator it; - for( it = sourceIds.begin(); it != sourceIds.end(); ++it ) { - constInstSrcDescrPtr descr = (*it)->descr(); - cout << ( descr->default_activate() ? "[x]" : "[ ]" ); - cout << ( descr->default_refresh() ? "* " : " " ); - cout << descr->content_label() << " (" << descr->url() << ")" << endl; + catch ( const Exception & excpt_r ) { + ZYPP_CAUGHT( excpt_r ); + cerr << excpt_r.asUserString() << endl; + exit( 1 ); + } + catch ( const exception & excpt_r ) { + cerr << excpt_r.what() << endl; + exit( 1 ); } MIL << "END" << endl; -- 2.7.4