1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoManager.cc
21 #include "zypp/base/InputStream.h"
22 #include "zypp/base/LogTools.h"
23 #include "zypp/base/Gettext.h"
24 #include "zypp/base/Function.h"
25 #include "zypp/base/Regex.h"
26 #include "zypp/PathInfo.h"
27 #include "zypp/TmpPath.h"
29 #include "zypp/ServiceInfo.h"
30 #include "zypp/repo/RepoException.h"
31 #include "zypp/RepoManager.h"
33 #include "zypp/media/MediaManager.h"
34 #include "zypp/media/CredentialManager.h"
35 #include "zypp/MediaSetAccess.h"
36 #include "zypp/ExternalProgram.h"
37 #include "zypp/ManagedFile.h"
39 #include "zypp/parser/RepoFileReader.h"
40 #include "zypp/parser/ServiceFileReader.h"
41 #include "zypp/repo/ServiceRepos.h"
42 #include "zypp/repo/yum/Downloader.h"
43 #include "zypp/repo/susetags/Downloader.h"
44 #include "zypp/repo/PluginServices.h"
46 #include "zypp/Target.h" // for Target::targetDistribution() for repo index services
47 #include "zypp/ZYppFactory.h" // to get the Target from ZYpp instance
48 #include "zypp/HistoryLog.h" // to write history :O)
50 #include "zypp/ZYppCallbacks.h"
56 using namespace zypp::repo;
58 #define OPT_PROGRESS const ProgressData::ReceiverFnc & = ProgressData::ReceiverFnc()
60 ///////////////////////////////////////////////////////////////////
63 ///////////////////////////////////////////////////////////////////
66 /** Simple media mounter to access non-downloading URLs e.g. for non-local plaindir repos.
72 /** Ctor provides media access. */
73 MediaMounter( const Url & url_r )
75 media::MediaManager mediamanager;
76 _mid = mediamanager.open( url_r );
77 mediamanager.attach( _mid );
80 /** Ctor releases the media. */
83 media::MediaManager mediamanager;
84 mediamanager.release( _mid );
85 mediamanager.close( _mid );
88 /** Convert a path relative to the media into an absolute path.
90 * Called without argument it returns the path to the medias root directory.
92 Pathname getPathName( const Pathname & path_r = Pathname() ) const
94 media::MediaManager mediamanager;
95 return mediamanager.localPath( _mid, path_r );
99 media::MediaAccessId _mid;
101 ///////////////////////////////////////////////////////////////////
103 /** Check if alias_r is present in repo/service container. */
104 template <class Iterator>
105 inline bool foundAliasIn( const std::string & alias_r, Iterator begin_r, Iterator end_r )
107 for_( it, begin_r, end_r )
108 if ( it->alias() == alias_r )
113 template <class Container>
114 inline bool foundAliasIn( const std::string & alias_r, const Container & cont_r )
115 { return foundAliasIn( alias_r, cont_r.begin(), cont_r.end() ); }
117 /** Find alias_r in repo/service container. */
118 template <class Iterator>
119 inline Iterator findAlias( const std::string & alias_r, Iterator begin_r, Iterator end_r )
121 for_( it, begin_r, end_r )
122 if ( it->alias() == alias_r )
127 template <class Container>
128 inline typename Container::iterator findAlias( const std::string & alias_r, Container & cont_r )
129 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
131 template <class Container>
132 inline typename Container::const_iterator findAlias( const std::string & alias_r, const Container & cont_r )
133 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
136 /** \short Generate a related filename from a repo/service infos alias */
137 inline std::string filenameFromAlias( const std::string & alias_r, const std::string & stem_r )
139 std::string filename( alias_r );
140 // replace slashes with underscores
141 str::replaceAll( filename, "/", "_" );
143 filename = Pathname(filename).extend("."+stem_r).asString();
144 MIL << "generating filename for " << stem_r << " [" << alias_r << "] : '" << filename << "'" << endl;
149 * \short Simple callback to collect the results
151 * Classes like RepoFileParser call the callback
152 * once per each repo in a file.
154 * Passing this functor as callback, you can collect
155 * all results at the end, without dealing with async
158 * If targetDistro is set, all repos with non-empty RepoInfo::targetDistribution()
161 * \todo do this through a separate filter
163 struct RepoCollector : private base::NonCopyable
168 RepoCollector(const std::string & targetDistro_)
169 : targetDistro(targetDistro_)
172 bool collect( const RepoInfo &repo )
174 // skip repositories meant for other distros than specified
175 if (!targetDistro.empty()
176 && !repo.targetDistribution().empty()
177 && repo.targetDistribution() != targetDistro)
180 << "Skipping repository meant for '" << repo.targetDistribution()
181 << "' distribution (current distro is '"
182 << targetDistro << "')." << endl;
187 repos.push_back(repo);
192 std::string targetDistro;
194 ////////////////////////////////////////////////////////////////////////////
197 * Reads RepoInfo's from a repo file.
199 * \param file pathname of the file to read.
201 std::list<RepoInfo> repositories_in_file( const Pathname & file )
203 MIL << "repo file: " << file << endl;
204 RepoCollector collector;
205 parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
206 return collector.repos;
209 ////////////////////////////////////////////////////////////////////////////
212 * \short List of RepoInfo's from a directory
214 * Goes trough every file ending with ".repo" in a directory and adds all
215 * RepoInfo's contained in that file.
217 * \param dir pathname of the directory to read.
219 std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
221 MIL << "directory " << dir << endl;
222 std::list<RepoInfo> repos;
223 std::list<Pathname> entries;
224 if ( filesystem::readdir( entries, dir, false ) != 0 )
226 // TranslatorExplanation '%s' is a pathname
227 ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir.c_str())));
230 str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
231 for ( std::list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
233 if (str::regex_match(it->extension(), allowedRepoExt))
235 std::list<RepoInfo> tmp = repositories_in_file( *it );
236 repos.insert( repos.end(), tmp.begin(), tmp.end() );
238 //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
239 //MIL << "ok" << endl;
245 ////////////////////////////////////////////////////////////////////////////
247 inline void assert_alias( const RepoInfo & info )
249 if ( info.alias().empty() )
250 ZYPP_THROW( RepoNoAliasException() );
251 // bnc #473834. Maybe we can match the alias against a regex to define
252 // and check for valid aliases
253 if ( info.alias()[0] == '.')
254 ZYPP_THROW(RepoInvalidAliasException(
255 info, _("Repository alias cannot start with dot.")));
258 inline void assert_alias( const ServiceInfo & info )
260 if ( info.alias().empty() )
261 ZYPP_THROW( ServiceNoAliasException() );
262 // bnc #473834. Maybe we can match the alias against a regex to define
263 // and check for valid aliases
264 if ( info.alias()[0] == '.')
265 ZYPP_THROW(ServiceInvalidAliasException(
266 info, _("Service alias cannot start with dot.")));
269 ////////////////////////////////////////////////////////////////////////////
271 inline void assert_urls( const RepoInfo & info )
273 if ( info.baseUrlsEmpty() )
274 ZYPP_THROW( RepoNoUrlException( info ) );
277 inline void assert_url( const ServiceInfo & info )
279 if ( ! info.url().isValid() )
280 ZYPP_THROW( ServiceNoUrlException( info ) );
283 ////////////////////////////////////////////////////////////////////////////
286 * \short Calculates the raw cache path for a repository, this is usually
287 * /var/cache/zypp/alias
289 inline Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
292 return opt.repoRawCachePath / info.escaped_alias();
296 * \short Calculates the raw product metadata path for a repository, this is
297 * inside the raw cache dir, plus an optional path where the metadata is.
299 * It should be different only for repositories that are not in the root of
301 * for example /var/cache/zypp/alias/addondir
303 inline Pathname rawproductdata_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
306 return opt.repoRawCachePath / info.escaped_alias() / info.path();
310 * \short Calculates the packages cache path for a repository
312 inline Pathname packagescache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
315 return opt.repoPackagesCachePath / info.escaped_alias();
319 * \short Calculates the solv cache path for a repository
321 inline Pathname solv_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info)
324 return opt.repoSolvCachePath / info.escaped_alias();
327 ////////////////////////////////////////////////////////////////////////////
329 /** Functor collecting ServiceInfos into a ServiceSet. */
330 class ServiceCollector
333 typedef std::set<ServiceInfo> ServiceSet;
335 ServiceCollector( ServiceSet & services_r )
336 : _services( services_r )
339 bool operator()( const ServiceInfo & service_r ) const
341 _services.insert( service_r );
346 ServiceSet & _services;
348 ////////////////////////////////////////////////////////////////////////////
351 ///////////////////////////////////////////////////////////////////
353 std::list<RepoInfo> readRepoFile( const Url & repo_file )
355 // no interface to download a specific file, using workaround:
356 //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
358 Pathname path(url.getPathName());
359 url.setPathName ("/");
360 MediaSetAccess access(url);
361 Pathname local = access.provideFile(path);
363 DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
365 return repositories_in_file(local);
368 ///////////////////////////////////////////////////////////////////
370 // class RepoManagerOptions
372 ////////////////////////////////////////////////////////////////////
374 RepoManagerOptions::RepoManagerOptions( const Pathname & root_r )
376 repoCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoCachePath() );
377 repoRawCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoMetadataPath() );
378 repoSolvCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoSolvfilesPath() );
379 repoPackagesCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoPackagesPath() );
380 knownReposPath = Pathname::assertprefix( root_r, ZConfig::instance().knownReposPath() );
381 knownServicesPath = Pathname::assertprefix( root_r, ZConfig::instance().knownServicesPath() );
382 pluginsPath = Pathname::assertprefix( root_r, ZConfig::instance().pluginsPath() );
383 probe = ZConfig::instance().repo_add_probe();
388 RepoManagerOptions RepoManagerOptions::makeTestSetup( const Pathname & root_r )
390 RepoManagerOptions ret;
391 ret.repoCachePath = root_r;
392 ret.repoRawCachePath = root_r/"raw";
393 ret.repoSolvCachePath = root_r/"solv";
394 ret.repoPackagesCachePath = root_r/"packages";
395 ret.knownReposPath = root_r/"repos.d";
396 ret.knownServicesPath = root_r/"services.d";
397 ret.pluginsPath = root_r/"plugins";
398 ret.rootDir = root_r;
402 ///////////////////////////////////////////////////////////////////
403 /// \class RepoManager::Impl
404 /// \brief RepoManager implementation.
406 ///////////////////////////////////////////////////////////////////
407 struct RepoManager::Impl
410 Impl( const RepoManagerOptions &opt )
413 init_knownServices();
414 init_knownRepositories();
418 bool repoEmpty() const { return _repos.empty(); }
419 RepoSizeType repoSize() const { return _repos.size(); }
420 RepoConstIterator repoBegin() const { return _repos.begin(); }
421 RepoConstIterator repoEnd() const { return _repos.end(); }
423 bool hasRepo( const std::string & alias ) const
424 { return foundAliasIn( alias, _repos ); }
426 RepoInfo getRepo( const std::string & alias ) const
428 RepoConstIterator it( findAlias( alias, _repos ) );
429 return it == _repos.end() ? RepoInfo::noRepo : *it;
433 Pathname metadataPath( const RepoInfo & info ) const
434 { return rawcache_path_for_repoinfo( _options, info ); }
436 Pathname packagesPath( const RepoInfo & info ) const
437 { return packagescache_path_for_repoinfo( _options, info ); }
439 RepoStatus metadataStatus( const RepoInfo & info ) const;
441 RefreshCheckStatus checkIfToRefreshMetadata( const RepoInfo & info, const Url & url, RawMetadataRefreshPolicy policy );
443 void refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, OPT_PROGRESS );
445 void cleanMetadata( const RepoInfo & info, OPT_PROGRESS );
447 void cleanPackages( const RepoInfo & info, OPT_PROGRESS );
449 void buildCache( const RepoInfo & info, CacheBuildPolicy policy, OPT_PROGRESS );
451 repo::RepoType probe( const Url & url, const Pathname & path = Pathname() ) const;
453 void cleanCacheDirGarbage( OPT_PROGRESS );
455 void cleanCache( const RepoInfo & info, OPT_PROGRESS );
457 bool isCached( const RepoInfo & info ) const
458 { return PathInfo(solv_path_for_repoinfo( _options, info ) / "solv").isExist(); }
460 RepoStatus cacheStatus( const RepoInfo & info ) const
461 { return RepoStatus::fromCookieFile(solv_path_for_repoinfo(_options, info) / "cookie"); }
463 void loadFromCache( const RepoInfo & info, OPT_PROGRESS );
465 void addRepository( const RepoInfo & info, OPT_PROGRESS );
467 void addRepositories( const Url & url, OPT_PROGRESS );
469 void removeRepository( const RepoInfo & info, OPT_PROGRESS );
471 void modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, OPT_PROGRESS );
473 RepoInfo getRepositoryInfo( const std::string & alias, OPT_PROGRESS );
474 RepoInfo getRepositoryInfo( const Url & url, const url::ViewOption & urlview, OPT_PROGRESS );
477 bool serviceEmpty() const { return _services.empty(); }
478 ServiceSizeType serviceSize() const { return _services.size(); }
479 ServiceConstIterator serviceBegin() const { return _services.begin(); }
480 ServiceConstIterator serviceEnd() const { return _services.end(); }
482 bool hasService( const std::string & alias ) const
483 { return foundAliasIn( alias, _services ); }
485 ServiceInfo getService( const std::string & alias ) const
487 ServiceConstIterator it( findAlias( alias, _services ) );
488 return it == _services.end() ? ServiceInfo::noService : *it;
492 void addService( const ServiceInfo & service );
493 void addService( const std::string & alias, const Url & url )
494 { addService( ServiceInfo( alias, url ) ); }
496 void removeService( const std::string & alias );
497 void removeService( const ServiceInfo & service )
498 { removeService( service.alias() ); }
500 void refreshServices();
502 void refreshService( const std::string & alias );
503 void refreshService( const ServiceInfo & service )
504 { refreshService( service.alias() ); }
506 void modifyService( const std::string & oldAlias, const ServiceInfo & newService );
508 repo::ServiceType probeService( const Url & url ) const;
511 void saveService( ServiceInfo & service ) const;
513 Pathname generateNonExistingName( const Pathname & dir, const std::string & basefilename ) const;
515 std::string generateFilename( const RepoInfo & info ) const
516 { return filenameFromAlias( info.alias(), "repo" ); }
518 std::string generateFilename( const ServiceInfo & info ) const
519 { return filenameFromAlias( info.alias(), "service" ); }
521 void setCacheStatus( const RepoInfo & info, const RepoStatus & status )
523 Pathname base = solv_path_for_repoinfo( _options, info );
524 filesystem::assert_dir(base);
525 status.saveToCookieFile( base / "cookie" );
528 void touchIndexFile( const RepoInfo & info );
530 template<typename OutputIterator>
531 void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
533 MatchServiceAlias filter( alias );
534 std::copy( boost::make_filter_iterator( filter, _repos.begin(), _repos.end() ),
535 boost::make_filter_iterator( filter, _repos.end(), _repos.end() ),
540 void init_knownServices();
541 void init_knownRepositories();
544 RepoManagerOptions _options;
546 ServiceSet _services;
549 friend Impl * rwcowClone<Impl>( const Impl * rhs );
550 /** clone for RWCOW_pointer */
552 { return new Impl( *this ); }
554 ///////////////////////////////////////////////////////////////////
556 /** \relates RepoManager::Impl Stream output */
557 inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
558 { return str << "RepoManager::Impl"; }
560 ///////////////////////////////////////////////////////////////////
562 void RepoManager::Impl::saveService( ServiceInfo & service ) const
564 filesystem::assert_dir( _options.knownServicesPath );
565 Pathname servfile = generateNonExistingName( _options.knownServicesPath,
566 generateFilename( service ) );
567 service.setFilepath( servfile );
569 MIL << "saving service in " << servfile << endl;
571 std::ofstream file( servfile.c_str() );
574 // TranslatorExplanation '%s' is a filename
575 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), servfile.c_str() )));
577 service.dumpAsIniOn( file );
578 MIL << "done" << endl;
582 * Generate a non existing filename in a directory, using a base
583 * name. For example if a directory contains 3 files
589 * If you try to generate a unique filename for this directory,
590 * based on "ruu" you will get "ruu", but if you use the base
591 * "foo" you will get "foo_1"
593 * \param dir Directory where the file needs to be unique
594 * \param basefilename string to base the filename on.
596 Pathname RepoManager::Impl::generateNonExistingName( const Pathname & dir,
597 const std::string & basefilename ) const
599 std::string final_filename = basefilename;
601 while ( PathInfo(dir + final_filename).isExist() )
603 final_filename = basefilename + "_" + str::numstring(counter);
606 return dir + Pathname(final_filename);
609 ////////////////////////////////////////////////////////////////////////////
611 void RepoManager::Impl::init_knownServices()
613 Pathname dir = _options.knownServicesPath;
614 std::list<Pathname> entries;
615 if (PathInfo(dir).isExist())
617 if ( filesystem::readdir( entries, dir, false ) != 0 )
619 // TranslatorExplanation '%s' is a pathname
620 ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir.c_str())));
623 //str::regex allowedServiceExt("^\\.service(_[0-9]+)?$");
624 for_(it, entries.begin(), entries.end() )
626 parser::ServiceFileReader(*it, ServiceCollector(_services));
630 repo::PluginServices(_options.pluginsPath/"services", ServiceCollector(_services));
633 void RepoManager::Impl::init_knownRepositories()
635 MIL << "start construct known repos" << endl;
637 if ( PathInfo(_options.knownReposPath).isExist() )
639 std::list<std::string> repoEscAliases;
640 for ( RepoInfo & repoInfo : repositories_in_dir(_options.knownReposPath) )
642 // set the metadata path for the repo
643 repoInfo.setMetadataPath( rawcache_path_for_repoinfo(_options, repoInfo) );
644 // set the downloaded packages path for the repo
645 repoInfo.setPackagesPath( packagescache_path_for_repoinfo(_options, repoInfo) );
647 _repos.insert( repoInfo );
648 repoEscAliases.push_back(repoInfo.escaped_alias());
650 repoEscAliases.sort();
652 // delete metadata folders without corresponding repo (e.g. old tmp directories)
653 for ( const Pathname & cachePath : { _options.repoRawCachePath
654 , _options.repoSolvCachePath } )
656 std::list<std::string> entries;
657 if ( filesystem::readdir( entries, cachePath, false ) == 0 )
660 std::set<std::string> oldfiles;
661 set_difference( entries.begin(), entries.end(), repoEscAliases.begin(), repoEscAliases.end(),
662 std::inserter( oldfiles, oldfiles.end() ) );
663 for ( const std::string & old : oldfiles )
665 if ( old == Repository::systemRepoAlias() ) // don't remove the @System solv file
667 filesystem::recursive_rmdir( cachePath / old );
672 MIL << "end construct known repos" << endl;
675 ///////////////////////////////////////////////////////////////////
677 RepoStatus RepoManager::Impl::metadataStatus( const RepoInfo & info ) const
679 Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
680 Pathname productdatapath = rawproductdata_path_for_repoinfo( _options, info );
682 RepoType repokind = info.type();
683 // If unknown, probe the local metadata
684 if ( repokind == RepoType::NONE )
685 repokind = probe( productdatapath.asUrl() );
688 switch ( repokind.toEnum() )
690 case RepoType::RPMMD_e :
691 status = RepoStatus( productdatapath/"repodata/repomd.xml");
694 case RepoType::YAST2_e :
695 status = RepoStatus( productdatapath/"content" ) && RepoStatus( mediarootpath/"media.1/media" );
698 case RepoType::RPMPLAINDIR_e :
699 status = RepoStatus::fromCookieFile( productdatapath/"cookie" );
702 case RepoType::NONE_e :
703 // Return default RepoStatus in case of RepoType::NONE
704 // indicating it should be created?
705 // ZYPP_THROW(RepoUnknownTypeException());
712 void RepoManager::Impl::touchIndexFile( const RepoInfo & info )
714 Pathname productdatapath = rawproductdata_path_for_repoinfo( _options, info );
716 RepoType repokind = info.type();
717 if ( repokind.toEnum() == RepoType::NONE_e )
718 // unknown, probe the local metadata
719 repokind = probe( productdatapath.asUrl() );
720 // if still unknown, just return
721 if (repokind == RepoType::NONE_e)
725 switch ( repokind.toEnum() )
727 case RepoType::RPMMD_e :
728 p = Pathname(productdatapath + "/repodata/repomd.xml");
731 case RepoType::YAST2_e :
732 p = Pathname(productdatapath + "/content");
735 case RepoType::RPMPLAINDIR_e :
736 p = Pathname(productdatapath + "/cookie");
739 case RepoType::NONE_e :
744 // touch the file, ignore error (they are logged anyway)
745 filesystem::touch(p);
749 RepoManager::RefreshCheckStatus RepoManager::Impl::checkIfToRefreshMetadata( const RepoInfo & info, const Url & url, RawMetadataRefreshPolicy policy )
754 MIL << "Going to try to check whether refresh is needed for " << url << endl;
756 // first check old (cached) metadata
757 Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
758 filesystem::assert_dir( mediarootpath );
759 RepoStatus oldstatus = metadataStatus( info );
761 if ( oldstatus.empty() )
763 MIL << "No cached metadata, going to refresh" << endl;
764 return REFRESH_NEEDED;
768 if ( url.schemeIsVolatile() )
770 MIL << "never refresh CD/DVD" << endl;
771 return REPO_UP_TO_DATE;
773 if ( url.schemeIsLocal() )
775 policy = RefreshIfNeededIgnoreDelay;
779 // now we've got the old (cached) status, we can decide repo.refresh.delay
780 if (policy != RefreshForced && policy != RefreshIfNeededIgnoreDelay)
782 // difference in seconds
783 double diff = difftime(
784 (Date::ValueType)Date::now(),
785 (Date::ValueType)oldstatus.timestamp()) / 60;
787 DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
788 DBG << "current time: " << (Date::ValueType)Date::now() << endl;
789 DBG << "last refresh = " << diff << " minutes ago" << endl;
791 if ( diff < ZConfig::instance().repo_refresh_delay() )
795 WAR << "Repository '" << info.alias() << "' was refreshed in the future!" << endl;
799 MIL << "Repository '" << info.alias()
800 << "' has been refreshed less than repo.refresh.delay ("
801 << ZConfig::instance().repo_refresh_delay()
802 << ") minutes ago. Advising to skip refresh" << endl;
803 return REPO_CHECK_DELAYED;
808 // To test the new matadta create temp dir as sibling of mediarootpath
809 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( mediarootpath ) );
811 repo::RepoType repokind = info.type();
812 // if unknown: probe it
813 if ( repokind == RepoType::NONE )
814 repokind = probe( url, info.path() );
816 // retrieve newstatus
817 RepoStatus newstatus;
818 switch ( repokind.toEnum() )
820 case RepoType::RPMMD_e:
822 MediaSetAccess media( url );
823 newstatus = yum::Downloader( info, mediarootpath ).status( media );
827 case RepoType::YAST2_e:
829 MediaSetAccess media( url );
830 newstatus = susetags::Downloader( info, mediarootpath ).status( media );
834 case RepoType::RPMPLAINDIR_e:
835 newstatus = RepoStatus( MediaMounter(url).getPathName(info.path()) ); // dir status
839 case RepoType::NONE_e:
840 ZYPP_THROW( RepoUnknownTypeException( info ) );
845 // DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
846 // DBG << " " << oldstatus.checksum() << endl;
847 // DBG << "newstatus: " << (Date::ValueType)newstatus.timestamp() << endl;
848 // DBG << " " << newstatus.checksum() << endl;
849 bool refresh = false;
850 if ( oldstatus.checksum() == newstatus.checksum() )
852 MIL << "repo has not changed" << endl;
853 if ( policy == RefreshForced )
855 MIL << "refresh set to forced" << endl;
861 MIL << "repo has changed, going to refresh" << endl;
866 touchIndexFile(info);
868 return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
871 catch ( const Exception &e )
874 ERR << "refresh check failed for " << url << endl;
878 return REFRESH_NEEDED; // default
882 void RepoManager::Impl::refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, const ProgressData::ReceiverFnc & progress )
887 // we will throw this later if no URL checks out fine
888 RepoException rexception(_PL("Valid metadata not found at specified URL",
889 "Valid metadata not found at specified URLs",
890 info.baseUrlsSize() ) );
892 // try urls one by one
893 for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
899 // check whether to refresh metadata
900 // if the check fails for this url, it throws, so another url will be checked
901 if (checkIfToRefreshMetadata(info, url, policy)!=REFRESH_NEEDED)
904 MIL << "Going to refresh metadata from " << url << endl;
906 repo::RepoType repokind = info.type();
908 // if the type is unknown, try probing.
909 if ( repokind == RepoType::NONE )
912 repokind = probe( *it, info.path() );
914 if (repokind.toEnum() != RepoType::NONE_e)
916 // Adjust the probed type in RepoInfo
917 info.setProbedType( repokind ); // lazy init!
918 //save probed type only for repos in system
919 for_( it, repoBegin(), repoEnd() )
921 if ( info.alias() == (*it).alias() )
923 RepoInfo modifiedrepo = info;
924 modifiedrepo.setType( repokind );
925 modifyRepository( info.alias(), modifiedrepo );
932 Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
933 if( filesystem::assert_dir(mediarootpath) )
935 Exception ex(str::form( _("Can't create %s"), mediarootpath.c_str()) );
939 // create temp dir as sibling of mediarootpath
940 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( mediarootpath ) );
941 if( tmpdir.path().empty() )
943 Exception ex(_("Can't create metadata cache directory."));
947 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
948 ( repokind.toEnum() == RepoType::YAST2_e ) )
950 MediaSetAccess media(url);
951 shared_ptr<repo::Downloader> downloader_ptr;
953 MIL << "Creating downloader for [ " << info.alias() << " ]" << endl;
955 if ( repokind.toEnum() == RepoType::RPMMD_e )
956 downloader_ptr.reset(new yum::Downloader(info, mediarootpath));
958 downloader_ptr.reset( new susetags::Downloader(info, mediarootpath) );
961 * Given a downloader, sets the other repos raw metadata
962 * path as cache paths for the fetcher, so if another
963 * repo has the same file, it will not download it
964 * but copy it from the other repository
966 for_( it, repoBegin(), repoEnd() )
968 Pathname cachepath(rawcache_path_for_repoinfo( _options, *it ));
969 if ( PathInfo(cachepath).isExist() )
970 downloader_ptr->addCachePath(cachepath);
973 downloader_ptr->download( media, tmpdir.path() );
975 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
977 MediaMounter media( url );
978 RepoStatus newstatus = RepoStatus( media.getPathName( info.path() ) ); // dir status
980 Pathname productpath( tmpdir.path() / info.path() );
981 filesystem::assert_dir( productpath );
982 newstatus.saveToCookieFile( productpath/"cookie" );
986 ZYPP_THROW(RepoUnknownTypeException());
989 // ok we have the metadata, now exchange
991 filesystem::exchange( tmpdir.path(), mediarootpath );
996 catch ( const Exception &e )
999 ERR << "Trying another url..." << endl;
1001 // remember the exception caught for the *first URL*
1002 // if all other URLs fail, the rexception will be thrown with the
1003 // cause of the problem of the first URL remembered
1004 if (it == info.baseUrlsBegin())
1005 rexception.remember(e);
1008 ERR << "No more urls..." << endl;
1009 ZYPP_THROW(rexception);
1012 ////////////////////////////////////////////////////////////////////////////
1014 void RepoManager::Impl::cleanMetadata( const RepoInfo & info, const ProgressData::ReceiverFnc & progressfnc )
1016 ProgressData progress(100);
1017 progress.sendTo(progressfnc);
1019 filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_options, info));
1024 void RepoManager::Impl::cleanPackages( const RepoInfo & info, const ProgressData::ReceiverFnc & progressfnc )
1026 ProgressData progress(100);
1027 progress.sendTo(progressfnc);
1029 filesystem::recursive_rmdir(packagescache_path_for_repoinfo(_options, info));
1034 void RepoManager::Impl::buildCache( const RepoInfo & info, CacheBuildPolicy policy, const ProgressData::ReceiverFnc & progressrcv )
1037 Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
1038 Pathname productdatapath = rawproductdata_path_for_repoinfo( _options, info );
1040 if( filesystem::assert_dir(_options.repoCachePath) )
1042 Exception ex(str::form( _("Can't create %s"), _options.repoCachePath.c_str()) );
1045 RepoStatus raw_metadata_status = metadataStatus(info);
1046 if ( raw_metadata_status.empty() )
1048 /* if there is no cache at this point, we refresh the raw
1049 in case this is the first time - if it's !autorefresh,
1050 we may still refresh */
1051 refreshMetadata(info, RefreshIfNeeded, progressrcv );
1052 raw_metadata_status = metadataStatus(info);
1055 bool needs_cleaning = false;
1056 if ( isCached( info ) )
1058 MIL << info.alias() << " is already cached." << endl;
1059 RepoStatus cache_status = cacheStatus(info);
1061 if ( cache_status.checksum() == raw_metadata_status.checksum() )
1063 MIL << info.alias() << " cache is up to date with metadata." << endl;
1064 if ( policy == BuildIfNeeded ) {
1068 MIL << info.alias() << " cache rebuild is forced" << endl;
1072 needs_cleaning = true;
1075 ProgressData progress(100);
1076 callback::SendReport<ProgressReport> report;
1077 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1078 progress.name(str::form(_("Building repository '%s' cache"), info.label().c_str()));
1086 MIL << info.alias() << " building cache..." << info.type() << endl;
1088 Pathname base = solv_path_for_repoinfo( _options, info);
1090 if( filesystem::assert_dir(base) )
1092 Exception ex(str::form( _("Can't create %s"), base.c_str()) );
1096 if( ! PathInfo(base).userMayW() )
1098 Exception ex(str::form( _("Can't create cache at %s - no writing permissions."), base.c_str()) );
1101 Pathname solvfile = base / "solv";
1104 repo::RepoType repokind = info.type();
1106 // if the type is unknown, try probing.
1107 switch ( repokind.toEnum() )
1109 case RepoType::NONE_e:
1110 // unknown, probe the local metadata
1111 repokind = probe( productdatapath.asUrl() );
1117 MIL << "repo type is " << repokind << endl;
1119 switch ( repokind.toEnum() )
1121 case RepoType::RPMMD_e :
1122 case RepoType::YAST2_e :
1123 case RepoType::RPMPLAINDIR_e :
1125 // Take care we unlink the solvfile on exception
1126 ManagedFile guard( solvfile, filesystem::unlink );
1127 scoped_ptr<MediaMounter> forPlainDirs;
1129 ExternalProgram::Arguments cmd;
1130 cmd.push_back( "repo2solv.sh" );
1131 // repo2solv expects -o as 1st arg!
1132 cmd.push_back( "-o" );
1133 cmd.push_back( solvfile.asString() );
1134 cmd.push_back( "-X" ); // autogenerate pattern from pattern-package
1136 if ( repokind == RepoType::RPMPLAINDIR )
1138 forPlainDirs.reset( new MediaMounter( *info.baseUrlsBegin() ) );
1139 // recusive for plaindir as 2nd arg!
1140 cmd.push_back( "-R" );
1141 // FIXME this does only work form dir: URLs
1142 cmd.push_back( forPlainDirs->getPathName( info.path() ).c_str() );
1145 cmd.push_back( productdatapath.asString() );
1147 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
1148 std::string errdetail;
1150 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
1151 WAR << " " << output;
1152 if ( errdetail.empty() ) {
1153 errdetail = prog.command();
1156 errdetail += output;
1159 int ret = prog.close();
1162 RepoException ex(str::form( _("Failed to cache repo (%d)."), ret ));
1163 ex.remember( errdetail );
1168 guard.resetDispose();
1172 ZYPP_THROW(RepoUnknownTypeException( _("Unhandled repository type") ));
1175 // update timestamp and checksum
1176 setCacheStatus(info, raw_metadata_status);
1177 MIL << "Commit cache.." << endl;
1181 ////////////////////////////////////////////////////////////////////////////
1183 repo::RepoType RepoManager::Impl::probe( const Url & url, const Pathname & path ) const
1185 MIL << "going to probe the repo type at " << url << " (" << path << ")" << endl;
1187 if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName()/path ).isDir() )
1189 // Handle non existing local directory in advance, as
1190 // MediaSetAccess does not support it.
1191 MIL << "Probed type NONE (not exists) at " << url << " (" << path << ")" << endl;
1192 return repo::RepoType::NONE;
1195 // prepare exception to be thrown if the type could not be determined
1196 // due to a media exception. We can't throw right away, because of some
1197 // problems with proxy servers returning an incorrect error
1198 // on ftp file-not-found(bnc #335906). Instead we'll check another types
1201 // TranslatorExplanation '%s' is an URL
1202 RepoException enew(str::form( _("Error trying to read from '%s'"), url.asString().c_str() ));
1203 bool gotMediaException = false;
1206 MediaSetAccess access(url);
1209 if ( access.doesFileExist(path/"/repodata/repomd.xml") )
1211 MIL << "Probed type RPMMD at " << url << " (" << path << ")" << endl;
1212 return repo::RepoType::RPMMD;
1215 catch ( const media::MediaException &e )
1218 DBG << "problem checking for repodata/repomd.xml file" << endl;
1220 gotMediaException = true;
1225 if ( access.doesFileExist(path/"/content") )
1227 MIL << "Probed type YAST2 at " << url << " (" << path << ")" << endl;
1228 return repo::RepoType::YAST2;
1231 catch ( const media::MediaException &e )
1234 DBG << "problem checking for content file" << endl;
1236 gotMediaException = true;
1239 // if it is a non-downloading URL denoting a directory
1240 if ( ! url.schemeIsDownloading() )
1242 MediaMounter media( url );
1243 if ( PathInfo(media.getPathName()/path).isDir() )
1245 // allow empty dirs for now
1246 MIL << "Probed type RPMPLAINDIR at " << url << " (" << path << ")" << endl;
1247 return repo::RepoType::RPMPLAINDIR;
1251 catch ( const Exception &e )
1254 // TranslatorExplanation '%s' is an URL
1255 Exception enew(str::form( _("Unknown error reading from '%s'"), url.asString().c_str() ));
1260 if (gotMediaException)
1263 MIL << "Probed type NONE at " << url << " (" << path << ")" << endl;
1264 return repo::RepoType::NONE;
1267 ////////////////////////////////////////////////////////////////////////////
1269 void RepoManager::Impl::cleanCacheDirGarbage( const ProgressData::ReceiverFnc & progressrcv )
1271 MIL << "Going to clean up garbage in cache dirs" << endl;
1273 ProgressData progress(300);
1274 progress.sendTo(progressrcv);
1277 std::list<Pathname> cachedirs;
1278 cachedirs.push_back(_options.repoRawCachePath);
1279 cachedirs.push_back(_options.repoPackagesCachePath);
1280 cachedirs.push_back(_options.repoSolvCachePath);
1282 for_( dir, cachedirs.begin(), cachedirs.end() )
1284 if ( PathInfo(*dir).isExist() )
1286 std::list<Pathname> entries;
1287 if ( filesystem::readdir( entries, *dir, false ) != 0 )
1288 // TranslatorExplanation '%s' is a pathname
1289 ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir->c_str())));
1291 unsigned sdircount = entries.size();
1292 unsigned sdircurrent = 1;
1293 for_( subdir, entries.begin(), entries.end() )
1295 // if it does not belong known repo, make it disappear
1297 for_( r, repoBegin(), repoEnd() )
1298 if ( subdir->basename() == r->escaped_alias() )
1299 { found = true; break; }
1302 filesystem::recursive_rmdir( *subdir );
1304 progress.set( progress.val() + sdircurrent * 100 / sdircount );
1309 progress.set( progress.val() + 100 );
1314 ////////////////////////////////////////////////////////////////////////////
1316 void RepoManager::Impl::cleanCache( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1318 ProgressData progress(100);
1319 progress.sendTo(progressrcv);
1322 MIL << "Removing raw metadata cache for " << info.alias() << endl;
1323 filesystem::recursive_rmdir(solv_path_for_repoinfo(_options, info));
1328 ////////////////////////////////////////////////////////////////////////////
1330 void RepoManager::Impl::loadFromCache( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1333 Pathname solvfile = solv_path_for_repoinfo(_options, info) / "solv";
1335 if ( ! PathInfo(solvfile).isExist() )
1336 ZYPP_THROW(RepoNotCachedException(info));
1338 sat::Pool::instance().reposErase( info.alias() );
1341 Repository repo = sat::Pool::instance().addRepoSolv( solvfile, info );
1342 // test toolversion in order to rebuild solv file in case
1343 // it was written by an old libsolv-tool parser.
1345 // Known version strings used:
1349 sat::LookupRepoAttr toolversion( sat::SolvAttr::repositoryToolVersion, repo );
1350 if ( toolversion.begin().asString().empty() )
1352 repo.eraseFromPool();
1353 ZYPP_THROW(Exception("Solv-file was created by old parser."));
1355 // else: up-to-date (or even newer).
1357 catch ( const Exception & exp )
1360 MIL << "Try to handle exception by rebuilding the solv-file" << endl;
1361 cleanCache( info, progressrcv );
1362 buildCache( info, BuildIfNeeded, progressrcv );
1364 sat::Pool::instance().addRepoSolv( solvfile, info );
1368 ////////////////////////////////////////////////////////////////////////////
1370 void RepoManager::Impl::addRepository( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1374 ProgressData progress(100);
1375 callback::SendReport<ProgressReport> report;
1376 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1377 progress.name(str::form(_("Adding repository '%s'"), info.label().c_str()));
1380 MIL << "Try adding repo " << info << endl;
1382 RepoInfo tosave = info;
1383 if ( _repos.find(tosave) != _repos.end() )
1384 ZYPP_THROW(RepoAlreadyExistsException(info));
1386 // check the first url for now
1387 if ( _options.probe )
1389 DBG << "unknown repository type, probing" << endl;
1391 RepoType probedtype;
1392 probedtype = probe( *tosave.baseUrlsBegin(), info.path() );
1393 if ( tosave.baseUrlsSize() > 0 )
1395 if ( probedtype == RepoType::NONE )
1396 ZYPP_THROW(RepoUnknownTypeException());
1398 tosave.setType(probedtype);
1404 // assert the directory exists
1405 filesystem::assert_dir(_options.knownReposPath);
1407 Pathname repofile = generateNonExistingName(
1408 _options.knownReposPath, generateFilename(tosave));
1409 // now we have a filename that does not exists
1410 MIL << "Saving repo in " << repofile << endl;
1412 std::ofstream file(repofile.c_str());
1415 // TranslatorExplanation '%s' is a filename
1416 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), repofile.c_str() )));
1419 tosave.dumpAsIniOn(file);
1420 tosave.setFilepath(repofile);
1421 tosave.setMetadataPath( metadataPath( tosave ) );
1422 tosave.setPackagesPath( packagesPath( tosave ) );
1424 // We chould fix the API as we must injet those paths
1425 // into the repoinfo in order to keep it usable.
1426 RepoInfo & oinfo( const_cast<RepoInfo &>(info) );
1427 oinfo.setMetadataPath( metadataPath( tosave ) );
1428 oinfo.setPackagesPath( packagesPath( tosave ) );
1430 _repos.insert(tosave);
1434 // check for credentials in Urls
1435 bool havePasswords = false;
1436 for_( urlit, tosave.baseUrlsBegin(), tosave.baseUrlsEnd() )
1437 if ( urlit->hasCredentialsInAuthority() )
1439 havePasswords = true;
1442 // save the credentials
1443 if ( havePasswords )
1445 media::CredentialManager cm(
1446 media::CredManagerOptions(_options.rootDir) );
1448 for_(urlit, tosave.baseUrlsBegin(), tosave.baseUrlsEnd())
1449 if (urlit->hasCredentialsInAuthority())
1450 //! \todo use a method calling UI callbacks to ask where to save creds?
1451 cm.saveInUser(media::AuthData(*urlit));
1454 HistoryLog().addRepository(tosave);
1457 MIL << "done" << endl;
1461 void RepoManager::Impl::addRepositories( const Url & url, const ProgressData::ReceiverFnc & progressrcv )
1463 std::list<RepoInfo> repos = readRepoFile(url);
1464 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1468 // look if the alias is in the known repos.
1469 for_ ( kit, repoBegin(), repoEnd() )
1471 if ( (*it).alias() == (*kit).alias() )
1473 ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1474 ZYPP_THROW(RepoAlreadyExistsException(*it));
1479 std::string filename = Pathname(url.getPathName()).basename();
1481 if ( filename == Pathname() )
1483 // TranslatorExplanation '%s' is an URL
1484 ZYPP_THROW(RepoException(str::form( _("Invalid repo file name at '%s'"), url.asString().c_str() )));
1487 // assert the directory exists
1488 filesystem::assert_dir(_options.knownReposPath);
1490 Pathname repofile = generateNonExistingName(_options.knownReposPath, filename);
1491 // now we have a filename that does not exists
1492 MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1494 std::ofstream file(repofile.c_str());
1497 // TranslatorExplanation '%s' is a filename
1498 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), repofile.c_str() )));
1501 for ( std::list<RepoInfo>::iterator it = repos.begin();
1505 MIL << "Saving " << (*it).alias() << endl;
1506 it->setFilepath(repofile.asString());
1507 it->dumpAsIniOn(file);
1510 HistoryLog(_options.rootDir).addRepository(*it);
1513 MIL << "done" << endl;
1516 ////////////////////////////////////////////////////////////////////////////
1518 void RepoManager::Impl::removeRepository( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1520 ProgressData progress;
1521 callback::SendReport<ProgressReport> report;
1522 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1523 progress.name(str::form(_("Removing repository '%s'"), info.label().c_str()));
1525 MIL << "Going to delete repo " << info.alias() << endl;
1527 for_( it, repoBegin(), repoEnd() )
1529 // they can be the same only if the provided is empty, that means
1530 // the provided repo has no alias
1532 if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1535 // TODO match by url
1537 // we have a matcing repository, now we need to know
1538 // where it does come from.
1539 RepoInfo todelete = *it;
1540 if (todelete.filepath().empty())
1542 ZYPP_THROW(RepoException( _("Can't figure out where the repo is stored.") ));
1546 // figure how many repos are there in the file:
1547 std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1548 if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
1550 // easy, only this one, just delete the file
1551 if ( filesystem::unlink(todelete.filepath()) != 0 )
1553 // TranslatorExplanation '%s' is a filename
1554 ZYPP_THROW(RepoException(str::form( _("Can't delete '%s'"), todelete.filepath().c_str() )));
1556 MIL << todelete.alias() << " sucessfully deleted." << endl;
1560 // there are more repos in the same file
1561 // write them back except the deleted one.
1563 //std::ofstream file(tmp.path().c_str());
1565 // assert the directory exists
1566 filesystem::assert_dir(todelete.filepath().dirname());
1568 std::ofstream file(todelete.filepath().c_str());
1571 // TranslatorExplanation '%s' is a filename
1572 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), todelete.filepath().c_str() )));
1574 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1575 fit != filerepos.end();
1578 if ( (*fit).alias() != todelete.alias() )
1579 (*fit).dumpAsIniOn(file);
1583 CombinedProgressData cSubprogrcv(progress, 20);
1584 CombinedProgressData mSubprogrcv(progress, 40);
1585 CombinedProgressData pSubprogrcv(progress, 40);
1586 // now delete it from cache
1587 if ( isCached(todelete) )
1588 cleanCache( todelete, cSubprogrcv);
1589 // now delete metadata (#301037)
1590 cleanMetadata( todelete, mSubprogrcv );
1591 cleanPackages( todelete, pSubprogrcv );
1592 _repos.erase(todelete);
1593 MIL << todelete.alias() << " sucessfully deleted." << endl;
1594 HistoryLog(_options.rootDir).removeRepository(todelete);
1596 } // else filepath is empty
1599 // should not be reached on a sucess workflow
1600 ZYPP_THROW(RepoNotFoundException(info));
1603 ////////////////////////////////////////////////////////////////////////////
1605 void RepoManager::Impl::modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, const ProgressData::ReceiverFnc & progressrcv )
1607 RepoInfo toedit = getRepositoryInfo(alias);
1608 RepoInfo newinfo( newinfo_r ); // need writable copy to upadte housekeeping data
1610 // check if the new alias already exists when renaming the repo
1611 if ( alias != newinfo.alias() && hasRepo( newinfo.alias() ) )
1613 ZYPP_THROW(RepoAlreadyExistsException(newinfo));
1616 if (toedit.filepath().empty())
1618 ZYPP_THROW(RepoException( _("Can't figure out where the repo is stored.") ));
1622 // figure how many repos are there in the file:
1623 std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1625 // there are more repos in the same file
1626 // write them back except the deleted one.
1628 //std::ofstream file(tmp.path().c_str());
1630 // assert the directory exists
1631 filesystem::assert_dir(toedit.filepath().dirname());
1633 std::ofstream file(toedit.filepath().c_str());
1636 // TranslatorExplanation '%s' is a filename
1637 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), toedit.filepath().c_str() )));
1639 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1640 fit != filerepos.end();
1643 // if the alias is different, dump the original
1644 // if it is the same, dump the provided one
1645 if ( (*fit).alias() != toedit.alias() )
1646 (*fit).dumpAsIniOn(file);
1648 newinfo.dumpAsIniOn(file);
1651 newinfo.setFilepath(toedit.filepath());
1652 _repos.erase(toedit);
1653 _repos.insert(newinfo);
1654 HistoryLog(_options.rootDir).modifyRepository(toedit, newinfo);
1655 MIL << "repo " << alias << " modified" << endl;
1659 ////////////////////////////////////////////////////////////////////////////
1661 RepoInfo RepoManager::Impl::getRepositoryInfo( const std::string & alias, const ProgressData::ReceiverFnc & progressrcv )
1663 RepoConstIterator it( findAlias( alias, _repos ) );
1664 if ( it != _repos.end() )
1667 info.setAlias( alias );
1668 ZYPP_THROW( RepoNotFoundException(info) );
1672 RepoInfo RepoManager::Impl::getRepositoryInfo( const Url & url, const url::ViewOption & urlview, const ProgressData::ReceiverFnc & progressrcv )
1674 for_( it, repoBegin(), repoEnd() )
1676 for_( urlit, (*it).baseUrlsBegin(), (*it).baseUrlsEnd() )
1678 if ( (*urlit).asString(urlview) == url.asString(urlview) )
1683 info.setBaseUrl( url );
1684 ZYPP_THROW( RepoNotFoundException(info) );
1687 ////////////////////////////////////////////////////////////////////////////
1691 ////////////////////////////////////////////////////////////////////////////
1693 void RepoManager::Impl::addService( const ServiceInfo & service )
1695 assert_alias( service );
1697 // check if service already exists
1698 if ( hasService( service.alias() ) )
1699 ZYPP_THROW( ServiceAlreadyExistsException( service ) );
1701 // Writable ServiceInfo is needed to save the location
1702 // of the .service file. Finaly insert into the service list.
1703 ServiceInfo toSave( service );
1704 saveService( toSave );
1705 _services.insert( toSave );
1707 // check for credentials in Url (username:password, not ?credentials param)
1708 if ( toSave.url().hasCredentialsInAuthority() )
1710 media::CredentialManager cm(
1711 media::CredManagerOptions(_options.rootDir) );
1713 //! \todo use a method calling UI callbacks to ask where to save creds?
1714 cm.saveInUser(media::AuthData(toSave.url()));
1717 MIL << "added service " << toSave.alias() << endl;
1720 ////////////////////////////////////////////////////////////////////////////
1722 void RepoManager::Impl::removeService( const std::string & alias )
1724 MIL << "Going to delete repo " << alias << endl;
1726 const ServiceInfo & service = getService( alias );
1728 Pathname location = service.filepath();
1729 if( location.empty() )
1731 ZYPP_THROW(RepoException( _("Can't figure out where the service is stored.") ));
1735 parser::ServiceFileReader( location, ServiceCollector(tmpSet) );
1737 // only one service definition in the file
1738 if ( tmpSet.size() == 1 )
1740 if ( filesystem::unlink(location) != 0 )
1742 // TranslatorExplanation '%s' is a filename
1743 ZYPP_THROW(RepoException(str::form( _("Can't delete '%s'"), location.c_str() )));
1745 MIL << alias << " sucessfully deleted." << endl;
1749 filesystem::assert_dir(location.dirname());
1751 std::ofstream file(location.c_str());
1754 // TranslatorExplanation '%s' is a filename
1755 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), location.c_str() )));
1758 for_(it, tmpSet.begin(), tmpSet.end())
1760 if( it->alias() != alias )
1761 it->dumpAsIniOn(file);
1764 MIL << alias << " sucessfully deleted from file " << location << endl;
1767 // now remove all repositories added by this service
1768 RepoCollector rcollector;
1769 getRepositoriesInService( alias,
1770 boost::make_function_output_iterator( bind( &RepoCollector::collect, &rcollector, _1 ) ) );
1771 // cannot do this directly in getRepositoriesInService - would invalidate iterators
1772 for_(rit, rcollector.repos.begin(), rcollector.repos.end())
1773 removeRepository(*rit);
1776 ////////////////////////////////////////////////////////////////////////////
1778 void RepoManager::Impl::refreshServices()
1780 // copy the set of services since refreshService
1781 // can eventually invalidate the iterator
1782 ServiceSet services( serviceBegin(), serviceEnd() );
1783 for_( it, services.begin(), services.end() )
1785 if ( !it->enabled() )
1789 refreshService(*it);
1791 catch ( const repo::ServicePluginInformalException & e )
1792 { ;/* ignore ServicePluginInformalException */ }
1796 void RepoManager::Impl::refreshService( const std::string & alias )
1798 ServiceInfo service( getService( alias ) );
1799 assert_alias( service );
1800 assert_url( service );
1801 // NOTE: It might be necessary to modify and rewrite the service info.
1802 // Either when probing the type, or when adjusting the repositories
1803 // enable/disable state.:
1804 bool serviceModified = false;
1805 MIL << "Going to refresh service '" << service.alias() << "', url: "<< service.url() << endl;
1807 //! \todo add callbacks for apps (start, end, repo removed, repo added, repo changed)
1809 // if the type is unknown, try probing.
1810 if ( service.type() == repo::ServiceType::NONE )
1812 repo::ServiceType type = probeService( service.url() );
1813 if ( type != ServiceType::NONE )
1815 service.setProbedType( type ); // lazy init!
1816 serviceModified = true;
1820 // get target distro identifier
1821 std::string servicesTargetDistro = _options.servicesTargetDistro;
1822 if ( servicesTargetDistro.empty() )
1824 servicesTargetDistro = Target::targetDistribution( Pathname() );
1826 DBG << "ServicesTargetDistro: " << servicesTargetDistro << endl;
1829 RepoCollector collector(servicesTargetDistro);
1830 // FIXME Ugly hack: ServiceRepos may throw ServicePluginInformalException
1831 // which is actually a notification. Using an exception for this
1832 // instead of signal/callback is bad. Needs to be fixed here, in refreshServices()
1834 std::pair<DefaultIntegral<bool,false>, repo::ServicePluginInformalException> uglyHack;
1836 ServiceRepos repos(service, bind( &RepoCollector::collect, &collector, _1 ));
1838 catch ( const repo::ServicePluginInformalException & e )
1840 /* ignore ServicePluginInformalException and throw later */
1841 uglyHack.first = true;
1842 uglyHack.second = e;
1845 // set service alias and base url for all collected repositories
1846 for_( it, collector.repos.begin(), collector.repos.end() )
1848 // if the repo url was not set by the repoindex parser, set service's url
1851 if ( it->baseUrlsEmpty() )
1852 url = service.url();
1855 // service repo can contain only one URL now, so no need to iterate.
1856 url = *it->baseUrlsBegin();
1859 // libzypp currently has problem with separate url + path handling
1860 // so just append the path to the baseurl
1861 if ( !it->path().empty() )
1863 Pathname path(url.getPathName());
1865 url.setPathName( path.asString() );
1869 // Prepend service alias:
1870 it->setAlias( str::form( "%s:%s", service.alias().c_str(), it->alias().c_str() ) );
1873 it->setBaseUrl( url );
1874 // set refrence to the parent service
1875 it->setService( service.alias() );
1878 ////////////////////////////////////////////////////////////////////////////
1879 // Now compare collected repos with the ones in the system...
1881 RepoInfoList oldRepos;
1882 getRepositoriesInService( service.alias(), std::back_inserter( oldRepos ) );
1884 // find old repositories to remove...
1885 for_( it, oldRepos.begin(), oldRepos.end() )
1887 if ( ! foundAliasIn( it->alias(), collector.repos ) )
1889 if ( it->enabled() && ! service.repoToDisableFind( it->alias() ) )
1891 DBG << "Service removes enabled repo " << it->alias() << endl;
1892 service.addRepoToEnable( it->alias() );
1893 serviceModified = true;
1897 DBG << "Service removes disabled repo " << it->alias() << endl;
1899 removeRepository( *it );
1903 ////////////////////////////////////////////////////////////////////////////
1904 // create missing repositories and modify exising ones if needed...
1905 for_( it, collector.repos.begin(), collector.repos.end() )
1907 // Service explicitly requests the repo being enabled?
1908 // Service explicitly requests the repo being disabled?
1909 // And hopefully not both ;) If so, enable wins.
1910 bool beEnabled = service.repoToEnableFind( it->alias() );
1911 bool beDisabled = service.repoToDisableFind( it->alias() );
1913 // Make sure the service repo is created with the
1914 // appropriate enable
1915 if ( beEnabled ) it->setEnabled(true);
1916 if ( beDisabled ) it->setEnabled(false);
1920 // Remove from enable request list.
1921 // NOTE: repoToDisable is handled differently.
1922 // It gets cleared on each refresh.
1923 service.delRepoToEnable( it->alias() );
1924 serviceModified = true;
1927 RepoInfoList::iterator oldRepo( findAlias( it->alias(), oldRepos ) );
1928 if ( oldRepo == oldRepos.end() )
1930 // Not found in oldRepos ==> a new repo to add
1932 // At that point check whether a repo with the same alias
1933 // exists outside this service. Maybe forcefully re-alias
1934 // the existing repo?
1935 DBG << "Service adds repo " << it->alias() << " " << (it->enabled()?"enabled":"disabled") << endl;
1936 addRepository( *it );
1938 // save repo credentials
1939 // ma@: task for modifyRepository?
1943 // ==> an exising repo to check
1944 bool oldRepoModified = false;
1949 if ( ! oldRepo->enabled() )
1951 DBG << "Service repo " << it->alias() << " gets enabled" << endl;
1952 oldRepo->setEnabled( true );
1953 oldRepoModified = true;
1957 DBG << "Service repo " << it->alias() << " stays enabled" << endl;
1960 else if ( beDisabled )
1962 if ( oldRepo->enabled() )
1964 DBG << "Service repo " << it->alias() << " gets disabled" << endl;
1965 oldRepo->setEnabled( false );
1966 oldRepoModified = true;
1970 DBG << "Service repo " << it->alias() << " stays disabled" << endl;
1975 DBG << "Service repo " << it->alias() << " stays " << (oldRepo->enabled()?"enabled":"disabled") << endl;
1979 // service repo can contain only one URL now, so no need to iterate.
1980 if ( oldRepo->url() != it->url() )
1982 DBG << "Service repo " << it->alias() << " gets new URL " << it->url() << endl;
1983 oldRepo->setBaseUrl( it->url() );
1984 oldRepoModified = true;
1987 // save if modified:
1988 if ( oldRepoModified )
1990 modifyRepository( oldRepo->alias(), *oldRepo );
1995 // Unlike reposToEnable, reposToDisable is always cleared after refresh.
1996 if ( ! service.reposToDisableEmpty() )
1998 service.clearReposToDisable();
1999 serviceModified = true;
2002 ////////////////////////////////////////////////////////////////////////////
2003 // save service if modified:
2004 if ( serviceModified )
2006 // write out modified service file.
2007 modifyService( service.alias(), service );
2010 if ( uglyHack.first )
2012 throw( uglyHack.second ); // intentionally not ZYPP_THROW
2016 ////////////////////////////////////////////////////////////////////////////
2018 void RepoManager::Impl::modifyService( const std::string & oldAlias, const ServiceInfo & newService )
2020 MIL << "Going to modify service " << oldAlias << endl;
2022 // we need a writable copy to link it to the file where
2023 // it is saved if we modify it
2024 ServiceInfo service(newService);
2026 if ( service.type() == ServiceType::PLUGIN )
2028 MIL << "Not modifying plugin service '" << oldAlias << "'" << endl;
2032 const ServiceInfo & oldService = getService(oldAlias);
2034 Pathname location = oldService.filepath();
2035 if( location.empty() )
2037 ZYPP_THROW(RepoException( _("Can't figure out where the service is stored.") ));
2040 // remember: there may multiple services being defined in one file:
2042 parser::ServiceFileReader( location, ServiceCollector(tmpSet) );
2044 filesystem::assert_dir(location.dirname());
2045 std::ofstream file(location.c_str());
2046 for_(it, tmpSet.begin(), tmpSet.end())
2048 if( *it != oldAlias )
2049 it->dumpAsIniOn(file);
2051 service.dumpAsIniOn(file);
2053 service.setFilepath(location);
2055 _services.erase(oldAlias);
2056 _services.insert(service);
2058 // changed properties affecting also repositories
2059 if( oldAlias != service.alias() // changed alias
2060 || oldService.enabled() != service.enabled() // changed enabled status
2063 std::vector<RepoInfo> toModify;
2064 getRepositoriesInService(oldAlias, std::back_inserter(toModify));
2065 for_( it, toModify.begin(), toModify.end() )
2067 if (oldService.enabled() && !service.enabled())
2068 it->setEnabled(false);
2069 else if (!oldService.enabled() && service.enabled())
2071 //! \todo do nothing? the repos will be enabled on service refresh
2072 //! \todo how to know the service needs a (auto) refresh????
2075 it->setService(service.alias());
2076 modifyRepository(it->alias(), *it);
2080 //! \todo refresh the service automatically if url is changed?
2083 ////////////////////////////////////////////////////////////////////////////
2085 repo::ServiceType RepoManager::Impl::probeService( const Url & url ) const
2089 MediaSetAccess access(url);
2090 if ( access.doesFileExist("/repo/repoindex.xml") )
2091 return repo::ServiceType::RIS;
2093 catch ( const media::MediaException &e )
2096 // TranslatorExplanation '%s' is an URL
2097 RepoException enew(str::form( _("Error trying to read from '%s'"), url.asString().c_str() ));
2101 catch ( const Exception &e )
2104 // TranslatorExplanation '%s' is an URL
2105 Exception enew(str::form( _("Unknown error reading from '%s'"), url.asString().c_str() ));
2110 return repo::ServiceType::NONE;
2113 ///////////////////////////////////////////////////////////////////
2115 // CLASS NAME : RepoManager
2117 ///////////////////////////////////////////////////////////////////
2119 RepoManager::RepoManager( const RepoManagerOptions & opt )
2120 : _pimpl( new Impl(opt) )
2123 RepoManager::~RepoManager()
2126 bool RepoManager::repoEmpty() const
2127 { return _pimpl->repoEmpty(); }
2129 RepoManager::RepoSizeType RepoManager::repoSize() const
2130 { return _pimpl->repoSize(); }
2132 RepoManager::RepoConstIterator RepoManager::repoBegin() const
2133 { return _pimpl->repoBegin(); }
2135 RepoManager::RepoConstIterator RepoManager::repoEnd() const
2136 { return _pimpl->repoEnd(); }
2138 RepoInfo RepoManager::getRepo( const std::string & alias ) const
2139 { return _pimpl->getRepo( alias ); }
2141 bool RepoManager::hasRepo( const std::string & alias ) const
2142 { return _pimpl->hasRepo( alias ); }
2144 std::string RepoManager::makeStupidAlias( const Url & url_r )
2146 std::string ret( url_r.getScheme() );
2152 std::string host( url_r.getHost() );
2153 if ( ! host.empty() )
2159 static Date::ValueType serial = Date::now();
2160 ret += Digest::digest( Digest::sha1(), str::hexstring( ++serial ) +url_r.asCompleteString() ).substr(0,8);
2164 RepoStatus RepoManager::metadataStatus( const RepoInfo & info ) const
2165 { return _pimpl->metadataStatus( info ); }
2167 RepoManager::RefreshCheckStatus RepoManager::checkIfToRefreshMetadata( const RepoInfo &info, const Url &url, RawMetadataRefreshPolicy policy )
2168 { return _pimpl->checkIfToRefreshMetadata( info, url, policy ); }
2170 Pathname RepoManager::metadataPath( const RepoInfo &info ) const
2171 { return _pimpl->metadataPath( info ); }
2173 Pathname RepoManager::packagesPath( const RepoInfo &info ) const
2174 { return _pimpl->packagesPath( info ); }
2176 void RepoManager::refreshMetadata( const RepoInfo &info, RawMetadataRefreshPolicy policy, const ProgressData::ReceiverFnc & progressrcv )
2177 { return _pimpl->refreshMetadata( info, policy, progressrcv ); }
2179 void RepoManager::cleanMetadata( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2180 { return _pimpl->cleanMetadata( info, progressrcv ); }
2182 void RepoManager::cleanPackages( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2183 { return _pimpl->cleanPackages( info, progressrcv ); }
2185 RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
2186 { return _pimpl->cacheStatus( info ); }
2188 void RepoManager::buildCache( const RepoInfo &info, CacheBuildPolicy policy, const ProgressData::ReceiverFnc & progressrcv )
2189 { return _pimpl->buildCache( info, policy, progressrcv ); }
2191 void RepoManager::cleanCache( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2192 { return _pimpl->cleanCache( info, progressrcv ); }
2194 bool RepoManager::isCached( const RepoInfo &info ) const
2195 { return _pimpl->isCached( info ); }
2197 void RepoManager::loadFromCache( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2198 { return _pimpl->loadFromCache( info, progressrcv ); }
2200 void RepoManager::cleanCacheDirGarbage( const ProgressData::ReceiverFnc & progressrcv )
2201 { return _pimpl->cleanCacheDirGarbage( progressrcv ); }
2203 repo::RepoType RepoManager::probe( const Url & url, const Pathname & path ) const
2204 { return _pimpl->probe( url, path ); }
2206 repo::RepoType RepoManager::probe( const Url & url ) const
2207 { return _pimpl->probe( url ); }
2209 void RepoManager::addRepository( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2210 { return _pimpl->addRepository( info, progressrcv ); }
2212 void RepoManager::addRepositories( const Url &url, const ProgressData::ReceiverFnc & progressrcv )
2213 { return _pimpl->addRepositories( url, progressrcv ); }
2215 void RepoManager::removeRepository( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
2216 { return _pimpl->removeRepository( info, progressrcv ); }
2218 void RepoManager::modifyRepository( const std::string &alias, const RepoInfo & newinfo, const ProgressData::ReceiverFnc & progressrcv )
2219 { return _pimpl->modifyRepository( alias, newinfo, progressrcv ); }
2221 RepoInfo RepoManager::getRepositoryInfo( const std::string &alias, const ProgressData::ReceiverFnc & progressrcv )
2222 { return _pimpl->getRepositoryInfo( alias, progressrcv ); }
2224 RepoInfo RepoManager::getRepositoryInfo( const Url & url, const url::ViewOption & urlview, const ProgressData::ReceiverFnc & progressrcv )
2225 { return _pimpl->getRepositoryInfo( url, urlview, progressrcv ); }
2227 bool RepoManager::serviceEmpty() const
2228 { return _pimpl->serviceEmpty(); }
2230 RepoManager::ServiceSizeType RepoManager::serviceSize() const
2231 { return _pimpl->serviceSize(); }
2233 RepoManager::ServiceConstIterator RepoManager::serviceBegin() const
2234 { return _pimpl->serviceBegin(); }
2236 RepoManager::ServiceConstIterator RepoManager::serviceEnd() const
2237 { return _pimpl->serviceEnd(); }
2239 ServiceInfo RepoManager::getService( const std::string & alias ) const
2240 { return _pimpl->getService( alias ); }
2242 bool RepoManager::hasService( const std::string & alias ) const
2243 { return _pimpl->hasService( alias ); }
2245 repo::ServiceType RepoManager::probeService( const Url &url ) const
2246 { return _pimpl->probeService( url ); }
2248 void RepoManager::addService( const std::string & alias, const Url& url )
2249 { return _pimpl->addService( alias, url ); }
2251 void RepoManager::addService( const ServiceInfo & service )
2252 { return _pimpl->addService( service ); }
2254 void RepoManager::removeService( const std::string & alias )
2255 { return _pimpl->removeService( alias ); }
2257 void RepoManager::removeService( const ServiceInfo & service )
2258 { return _pimpl->removeService( service ); }
2260 void RepoManager::refreshServices()
2261 { return _pimpl->refreshServices(); }
2263 void RepoManager::refreshService( const std::string & alias )
2264 { return _pimpl->refreshService( alias ); }
2266 void RepoManager::refreshService( const ServiceInfo & service )
2267 { return _pimpl->refreshService( service ); }
2269 void RepoManager::modifyService( const std::string & oldAlias, const ServiceInfo & service )
2270 { return _pimpl->modifyService( oldAlias, service ); }
2272 ////////////////////////////////////////////////////////////////////////////
2274 std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
2275 { return str << *obj._pimpl; }
2277 /////////////////////////////////////////////////////////////////
2279 ///////////////////////////////////////////////////////////////////