From b6491b6596af57507ddacfe3d07b3b9aa020f8fe Mon Sep 17 00:00:00 2001 From: Jan Kupec Date: Fri, 25 Jul 2008 16:12:14 +0000 Subject: [PATCH] - use alias and name in services much like in repos - should Service be renamed to ServiceInfo (it's very similar to RepoInfo). --- tests/zypp/RepoManager_test.cc | 2 +- zypp/RepoManager.cc | 95 ++++++++++++++++++++++---------------- zypp/RepoManager.h | 83 ++++++++++++++++++--------------- zypp/Service.cc | 19 +++++--- zypp/Service.h | 16 ++++++- zypp/parser/RepoindexFileReader.cc | 1 + zypp/parser/ServiceFileReader.cc | 19 +++++--- 7 files changed, 144 insertions(+), 91 deletions(-) diff --git a/tests/zypp/RepoManager_test.cc b/tests/zypp/RepoManager_test.cc index dd2d8e9..4afd7b6 100644 --- a/tests/zypp/RepoManager_test.cc +++ b/tests/zypp/RepoManager_test.cc @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(repomanager_test) service.setUrl(urlS); service.setEnabled(true); - manager.modifyService(service.name(),service); + manager.modifyService(service.alias(), service); manager.refreshServices(); BOOST_CHECK_EQUAL(manager.repoSize(), (unsigned) 6); //-1 from new repoindex diff --git a/zypp/RepoManager.cc b/zypp/RepoManager.cc index 0db0381..136b687 100644 --- a/zypp/RepoManager.cc +++ b/zypp/RepoManager.cc @@ -264,16 +264,19 @@ namespace zypp void saveService( const Service& service ) const; Pathname generateNonExistingName( const Pathname &dir, - const std::string &basefilename ) const; + const std::string &basefilename ) const; std::string generateFilename( const RepoInfo &info ) const; std::string generateFilename( const Service &info ) const; - struct ServiceCollector { - ServiceCollector(ServiceSet& services_) : services(services_) {} + struct ServiceCollector + { + ServiceCollector(ServiceSet & services_) : services(services_) {} + bool collect(Service service) { services.insert(service); return true; } - private: - ServiceSet& services; + + private: + ServiceSet & services; }; void knownServices(); @@ -1016,12 +1019,12 @@ namespace zypp std::string RepoManager::Impl::generateFilename( const Service &info ) const { - std::string filename = info.name(); + std::string filename = info.alias(); // replace slashes with underscores str::replaceAll( filename, "/", "_" ); filename = Pathname(filename).extend(".service").asString(); - MIL << "generating filename for service [" << info.name() << "] : '" << filename << "'" << endl; + MIL << "generating filename for service [" << info.alias() << "] : '" << filename << "'" << endl; return filename; } @@ -1304,12 +1307,12 @@ namespace zypp ZYPP_THROW(RepoNotFoundException(info)); } - void RepoManager::addService( const std::string& name, const Url& url ) + void RepoManager::addService( const std::string & alias, const Url & url ) { - addService( Service(name,url) ); + addService( Service(alias, url) ); } - void RepoManager::addService( const Service& service ) + void RepoManager::addService( const Service & service ) { //check if service isn't already exist if( _pimpl->services.find(service)!= _pimpl->services.end() ) @@ -1321,11 +1324,11 @@ namespace zypp _pimpl->saveService( savedService ); } - void RepoManager::removeService( const string& name) + void RepoManager::removeService( const string & alias) { - MIL << "Going to delete repo " << name << endl; + MIL << "Going to delete repo " << alias << endl; - const Service& service = getService( name ); + const Service& service = getService( alias ); Pathname location = service.location(); if( location.empty() ) @@ -1345,7 +1348,7 @@ namespace zypp { ZYPP_THROW(RepoException("Can't delete " + location.asString())); } - MIL << name << " sucessfully deleted." << endl; + MIL << alias << " sucessfully deleted." << endl; } else { @@ -1357,20 +1360,21 @@ namespace zypp for_(it, tmpSet.begin(), tmpSet.end()) { - if( it->name() != name ) + if( it->alias() != alias ) it->dumpServiceOn(file); } - MIL << name << " sucessfully deleted from file " << location << endl; + MIL << alias << " sucessfully deleted from file " << location << endl; } //now remove all repositories added by this service - getRepositoriesInService( name, - boost::make_function_output_iterator(bind(&RepoManager::removeRepository, this, _1, ProgressData::ReceiverFnc()) ) ); + getRepositoriesInService( alias, + boost::make_function_output_iterator( + bind(&RepoManager::removeRepository, this, _1, ProgressData::ReceiverFnc()) ) ); } - void RepoManager::Impl::saveService( const Service& service ) const + void RepoManager::Impl::saveService( const Service & service ) const { filesystem::assert_dir( options.knownServicesPath ); @@ -1390,9 +1394,9 @@ namespace zypp MIL << "done" << endl; } - Service RepoManager::getService( const std::string& name ) const + Service RepoManager::getService( const std::string & alias ) const { - ServiceConstIterator it = _pimpl->services.find(name); + ServiceConstIterator it = _pimpl->services.find(alias); if ( it == serviceEnd() ) return Service::noService; else @@ -1425,16 +1429,20 @@ namespace zypp if ( !it->enabled() ) continue; - MIL << "refresh: " << it->name() << " with url: "<< it->url().asString() << endl; + MIL << "refresh: " << it->alias() << " with url: "<< it->url().asString() << endl; refreshService(*it); } } - void RepoManager::refreshService( const Service& service ) + void RepoManager::refreshService( const Service & service ) { - //download index file + //! \todo add callbacks for apps (start, end, repo removed, repo added, repo changed) + + // download the repo index file media::MediaManager mediamanager; - media::MediaAccessId mid = mediamanager.open( service.url() ); //FIXME check if url is not empty + //if (service.url().empty()) + // throw RepoNoUrlException(); + media::MediaAccessId mid = mediamanager.open( service.url() ); mediamanager.attachDesiredMedia( mid ); mediamanager.provideFile( mid, "repo/repoindex.xml" ); Pathname path = mediamanager.localPath(mid, "repo/repoindex.xml" ); @@ -1449,19 +1457,20 @@ namespace zypp // set base url for all collected repositories for_( it, collector.repos.begin(), collector.repos.end()) { + //repoUrl = it->baseUrlsBegin(); it->setBaseUrl( service.url() ); - it->setService(service.name()); + it->setService( service.alias() ); } - //compare old and new repositories (hope not to much, if it change + // compare old and new repositories (hope not to much, if it change // then construct set and use set operation on it) std::list oldRepos; - getRepositoriesInService(service.name(), + getRepositoriesInService(service.alias(), insert_iterator > (oldRepos,oldRepos.begin())); - //find old to remove + // find old to remove for_( it, oldRepos.begin(), oldRepos.end() ) { bool found = false; @@ -1492,19 +1501,19 @@ namespace zypp if (!found) addRepository( *it ); } - } - void RepoManager::modifyService(const std::string& oldName, const Service& service) + void RepoManager::modifyService(const std::string & oldAlias, const Service & service) { - MIL << "Going to modify service " << oldName << endl; + MIL << "Going to modify service " << oldAlias << endl; - const Service& oldService = getService(oldName); + const Service& oldService = getService(oldAlias); Pathname location = oldService.location(); if( location.empty() ) { - ZYPP_THROW(RepoException("Can't figure where the service is stored")); + ZYPP_THROW(RepoException( + "Cannot figure out where the service file is stored.")); } ServiceSet tmpSet; @@ -1519,7 +1528,7 @@ namespace zypp for_(it, tmpSet.begin(), tmpSet.end()) { - if( *it != oldName ) + if( *it != oldAlias ) it->dumpServiceOn(file); } @@ -1527,21 +1536,29 @@ namespace zypp file.close(); - _pimpl->services.erase(oldName); + _pimpl->services.erase(oldAlias); _pimpl->services.insert(service); - if( oldName != service.name() ) //changed name, must change also repository + // changed name, must change also repositories + if( oldAlias != service.alias() ) { std::vector toModify; - getRepositoriesInService(oldName, + getRepositoriesInService(oldAlias, insert_iterator >( toModify, toModify.begin() )); for_( it, toModify.begin(), toModify.end() ) { - it->setService(service.name()); + it->setService(service.alias()); modifyRepository(it->alias(), *it); } } + //! \todo changed enabled status + if ( oldService.enabled() != service.enabled()) + { + + } + + //! \todo refresh the service automatically if url is changed? } void RepoManager::Impl::knownServices() diff --git a/zypp/RepoManager.h b/zypp/RepoManager.h index cf40616..31a52c3 100644 --- a/zypp/RepoManager.h +++ b/zypp/RepoManager.h @@ -361,8 +361,11 @@ namespace zypp * * \throws repo::RepoAlreadyExistsException If the repo clash some * unique attribute like alias - * \throws RepoUnknownType If repository type can't be determined - * \throws RepoException If the access to the url fails (while probing). + * \throws RepoUnknownType + * If RepoManagerOptions::probe is true + * and repository type can't be determined. + * \throws RepoException + * If RepoManagerOptions::probe is true and access to the url fails. * \throws Exception On other errors. */ void addRepository( const RepoInfo &info, @@ -373,9 +376,7 @@ namespace zypp * \param url Url of the repo file * * \throws repo::RepoAlreadyExistsException If the repo clash some - * unique attribute like alias - * - * \throws RepoAlreadyExistsException + * unique attribute like alias * \throws MediaException If the access to the url fails * \throws ParseException If the file parsing fails * \throws RepoUnknownType If repository type can't be determined @@ -445,19 +446,19 @@ namespace zypp const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() ); /** - * Adds new service by it's name and url + * Adds new service by it's alias and url * - * \param name unique name of service + * \param alias unique identifier of the service * \param url url to service * * \throws FIXME RepoAlreadyExistException and as reponame is service name */ - void addService( const std::string& name, const Url& url ); + void addService( const std::string & alias, const Url& url ); /** * Adds new service * - * \param name service info + * \param service service info * * \throws FIXME RepoAlreadyExistException and as reponame is service name */ @@ -466,12 +467,12 @@ namespace zypp /** * Removes service specified by its name * - * \param name name of service to remove + * \param alias unique indientifier of the service to remove * * \throws RepoException if service is not found or file with Service cannot be deleted * \throws Exception if file contain more services and rewrite file failed */ - void removeService( const std::string& name ); + void removeService( const std::string & alias ); /** * Gets true if no service is in RepoManager (so no one in specified location) @@ -501,51 +502,60 @@ namespace zypp ServiceConstIterator serviceEnd() const; /** - * Finds Service by name or return noService + * Finds Service by alias or return noService * - * \param name unique name of service + * \param alias unique identifier of service * \return information about Service */ - Service getService( const std::string& name ) const; + Service getService( const std::string & alias ) const; /** - * Refreshs all services - * \see refreshService + * Refreshes all enabled services. + * + * \see refreshService(Service) */ void refreshServices(); /** * Refresh specific service. - * \throws Exception if cannot download file + * * \param name service structure + * \throws MediaException If there's a problem downloading the repo index file. */ - void refreshService( const Service& name ); + void refreshService( const Service & service ); /** - * modify service, rewrite Service to filesystem. - * If change Service name, then can escalate to rewrite all repositories which it contain. - * - * \param oldName oldName of service - * \param service Structure containing new datas - * - * \throws RepoException if sservice with oldName is not known + * Modifies service file (rewrites it with new values) and underlying + * repositories if needed. + * + * Modifications of a service can lead to rewrite of all .repo files of + * contained repositories. Particularily, disabling a service (changing + * Service::enabled() from true to false) will disable all contained + * repositories. Renaming of a service will modify the "service" key + * of all contained repositories. + * + * \param oldAlias Old alias of the service + * \param service Service object containing new data + * + * \throws RepoException if sservice with oldAlias is not known * \throws Exception if have problems with files */ - void modifyService(const std::string& oldName, const Service& service); + void modifyService(const std::string & oldAlias, const Service & service); - private: + private: /** * Functor thats filter RepoInfo by service which belongs to. */ - struct MatchServiceName { + struct MatchServiceAlias + { private: - std::string name; + std::string alias; public: - MatchServiceName( const std::string& name_ ) : name(name_) {} - bool match( const RepoInfo& info ) { return info.service()==name; } + MatchServiceAlias( const std::string & alias_ ) : alias(alias_) {} + bool match( const RepoInfo & info ) { return info.service() == alias; } }; - public: + public: /** * fill to output iterator repositories in service name. This output iterator can perform * any action on with Repo or service Container, because it is sets and it isn't dynamic recreate. @@ -570,13 +580,14 @@ namespace zypp */ template - void getRepositoriesInService( const std::string& name, OutputIterator out ) const + void getRepositoriesInService( const std::string & alias, + OutputIterator out ) const { - MatchServiceName filter(name); + MatchServiceAlias filter(alias); - std::copy(boost::make_filter_iterator(bind(&MatchServiceName::match, + std::copy(boost::make_filter_iterator(bind(&MatchServiceAlias::match, filter, _1),repoBegin(),repoEnd()), boost::make_filter_iterator( - bind(&MatchServiceName::match, filter, _1),repoEnd(),repoEnd()), + bind(&MatchServiceAlias::match, filter, _1), repoEnd(), repoEnd()), out ); } diff --git a/zypp/Service.cc b/zypp/Service.cc index 75b3816..e654763 100644 --- a/zypp/Service.cc +++ b/zypp/Service.cc @@ -40,14 +40,15 @@ namespace zypp class Service::Impl { public: + string alias; string name; Url url; DefaultIntegral enabled; Pathname loc; - Impl() : name("") {}; - Impl(const string& name_) : name(name_) {}; - Impl(const string& name_, const Url& url_) : name(name_), url(url_) {}; + Impl() {} + Impl(const string & alias_) : alias(alias_) {} + Impl(const string & alias_, const Url& url_) : alias(alias_), url(url_) {} private: friend Impl * rwcowClone( const Impl * rhs ); @@ -60,11 +61,15 @@ namespace zypp const Service Service::noService; - Service::Service() : _pimpl( new Impl() ) {}; + Service::Service() : _pimpl( new Impl() ) {} - Service::Service(const string& name) : _pimpl( new Impl(name) ) {}; - Service::Service(const string& name, const Url& url) - : _pimpl( new Impl(name,url) ) {}; + Service::Service(const string & alias) : _pimpl( new Impl(alias) ) {} + Service::Service(const string & alias, const Url & url) + : _pimpl( new Impl(alias, url) ) {} + + + string Service::alias() const { return _pimpl->alias; } + void Service::setAlias( const std::string & alias ) { _pimpl->alias = alias; } string Service::name() const { return _pimpl->name; } void Service::setName( const std::string& name ) { _pimpl->name = name; } diff --git a/zypp/Service.h b/zypp/Service.h index 6a71c71..c979953 100644 --- a/zypp/Service.h +++ b/zypp/Service.h @@ -53,6 +53,13 @@ namespace zypp static const Service noService; public: + /** + * Gets unique alias. + * + * \return alias as string + */ + std::string alias() const; + /** * Gets unique name * @@ -86,7 +93,14 @@ namespace zypp Pathname location() const; public: - + + /** + * Sets alias for this service + * + * \param alias new alias of this service + */ + void setAlias( const std::string & alias ); + /** * Sets file where this service is stored. * diff --git a/zypp/parser/RepoindexFileReader.cc b/zypp/parser/RepoindexFileReader.cc index edcd59a..11ea3ad 100644 --- a/zypp/parser/RepoindexFileReader.cc +++ b/zypp/parser/RepoindexFileReader.cc @@ -95,6 +95,7 @@ namespace zypp if ( reader_r->name() == "repo" ) { RepoInfo info; + //! \todo FIXME this hardcodes the "/repo/" fragment - should not be if we want it to be usable by others! info.setPath(Pathname(string("/repo/")+reader_r->getAttribute("path").asString())); info.setAlias(reader_r->getAttribute("alias").asString()); info.setType(repo::RepoType::RPMMD_e); //TODO hardwired rpmmd??? diff --git a/zypp/parser/ServiceFileReader.cc b/zypp/parser/ServiceFileReader.cc index d10df43..2c4468a 100644 --- a/zypp/parser/ServiceFileReader.cc +++ b/zypp/parser/ServiceFileReader.cc @@ -29,10 +29,11 @@ namespace zypp namespace parser { ///////////////////////////////////////////////////////////////// - class ServiceFileReader::Impl { - public: - static void parseServices( const Pathname &file, - const ServiceFileReader::ProcessService& callback ); + class ServiceFileReader::Impl + { + public: + static void parseServices( const Pathname & file, + const ServiceFileReader::ProcessService & callback ); }; void ServiceFileReader::Impl::parseServices( const Pathname &file, @@ -58,16 +59,20 @@ namespace zypp it != dict.entriesEnd(*its); ++it ) { - //MIL << (*it).first << endl; - if ( it->first == "url" ) + // MIL << (*it).first << endl; + if ( it->first == "name" ) + service.setName( it->second ); + else if ( it->first == "url" ) service.setUrl( Url (it->second) ); else if ( it->first == "enabled" ) service.setEnabled( str::strToTrue( it->second ) ); else ERR << "Unknown attribute " << it->second << " ignored" << endl; } - MIL << "Linking repo info with file " << file << endl; + + MIL << "Linking Service with file " << file << endl; service.setLocation(file); + // add it to the list. if ( !callback(service) ) ZYPP_THROW(AbortRequestException()); -- 2.7.4