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/parser/RepoindexFileReader.h"
42 #include "zypp/repo/yum/Downloader.h"
43 #include "zypp/repo/susetags/Downloader.h"
44 #include "zypp/parser/plaindir/RepoParser.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 ///////////////////////////////////////////////////////////////////
60 { /////////////////////////////////////////////////////////////////
64 /** Simple media mounter to access non-downloading URLs e.g. for non-local plaindir repos.
70 /** Ctor provides media access. */
71 MediaMounter( const Url & url_r )
73 media::MediaManager mediamanager;
74 _mid = mediamanager.open( url_r );
75 mediamanager.attach( _mid );
78 /** Ctor releases the media. */
81 media::MediaManager mediamanager;
82 mediamanager.release( _mid );
83 mediamanager.close( _mid );
86 /** Convert a path relative to the media into an absolute path.
88 * Called without argument it returns the path to the medias root directory.
90 Pathname getPathName( const Pathname & path_r = Pathname() ) const
92 media::MediaManager mediamanager;
93 return mediamanager.localPath( _mid, path_r );
97 media::MediaAccessId _mid;
100 /** Check if alias_r is present in repo/service container. */
101 template <class Iterator>
102 inline bool foundAliasIn( const std::string & alias_r, Iterator begin_r, Iterator end_r )
104 for_( it, begin_r, end_r )
105 if ( it->alias() == alias_r )
110 template <class Container>
111 inline bool foundAliasIn( const std::string & alias_r, const Container & cont_r )
112 { return foundAliasIn( alias_r, cont_r.begin(), cont_r.end() ); }
114 /** Find alias_r in repo/service container. */
115 template <class Iterator>
116 inline Iterator findAlias( const std::string & alias_r, Iterator begin_r, Iterator end_r )
118 for_( it, begin_r, end_r )
119 if ( it->alias() == alias_r )
124 template <class Container>
125 inline typename Container::iterator findAlias( const std::string & alias_r, Container & cont_r )
126 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
128 template <class Container>
129 inline typename Container::const_iterator findAlias( const std::string & alias_r, const Container & cont_r )
130 { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
133 ///////////////////////////////////////////////////////////////////
135 // CLASS NAME : RepoManagerOptions
137 ///////////////////////////////////////////////////////////////////
139 RepoManagerOptions::RepoManagerOptions( const Pathname & root_r )
141 repoCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoCachePath() );
142 repoRawCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoMetadataPath() );
143 repoSolvCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoSolvfilesPath() );
144 repoPackagesCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoPackagesPath() );
145 knownReposPath = Pathname::assertprefix( root_r, ZConfig::instance().knownReposPath() );
146 knownServicesPath = Pathname::assertprefix( root_r, ZConfig::instance().knownServicesPath() );
147 probe = ZConfig::instance().repo_add_probe();
152 RepoManagerOptions RepoManagerOptions::makeTestSetup( const Pathname & root_r )
154 RepoManagerOptions ret;
155 ret.repoCachePath = root_r;
156 ret.repoRawCachePath = root_r/"raw";
157 ret.repoSolvCachePath = root_r/"solv";
158 ret.repoPackagesCachePath = root_r/"packages";
159 ret.knownReposPath = root_r/"repos.d";
160 ret.knownServicesPath = root_r/"services.d";
161 ret.rootDir = root_r;
165 ////////////////////////////////////////////////////////////////////////////
168 * \short Simple callback to collect the results
170 * Classes like RepoFileParser call the callback
171 * once per each repo in a file.
173 * Passing this functor as callback, you can collect
174 * all results at the end, without dealing with async
177 * If targetDistro is set, all repos with non-empty RepoInfo::targetDistribution()
180 * \todo do this through a separate filter
182 struct RepoCollector : private base::NonCopyable
187 RepoCollector(const std::string & targetDistro_)
188 : targetDistro(targetDistro_)
191 bool collect( const RepoInfo &repo )
193 // skip repositories meant for other distros than specified
194 if (!targetDistro.empty()
195 && !repo.targetDistribution().empty()
196 && repo.targetDistribution() != targetDistro)
199 << "Skipping repository meant for '" << targetDistro
200 << "' distribution (current distro is '"
201 << repo.targetDistribution() << "')." << endl;
206 repos.push_back(repo);
211 std::string targetDistro;
214 ////////////////////////////////////////////////////////////////////////////
217 * Reads RepoInfo's from a repo file.
219 * \param file pathname of the file to read.
221 static std::list<RepoInfo> repositories_in_file( const Pathname & file )
223 MIL << "repo file: " << file << endl;
224 RepoCollector collector;
225 parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
226 return collector.repos;
229 ////////////////////////////////////////////////////////////////////////////
232 * \short List of RepoInfo's from a directory
234 * Goes trough every file ending with ".repo" in a directory and adds all
235 * RepoInfo's contained in that file.
237 * \param dir pathname of the directory to read.
239 static std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
241 MIL << "directory " << dir << endl;
242 std::list<RepoInfo> repos;
243 std::list<Pathname> entries;
244 if ( filesystem::readdir( entries, dir, false ) != 0 )
246 // TranslatorExplanation '%s' is a pathname
247 ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir.c_str())));
250 str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
251 for ( std::list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
253 if (str::regex_match(it->extension(), allowedRepoExt))
255 std::list<RepoInfo> tmp = repositories_in_file( *it );
256 repos.insert( repos.end(), tmp.begin(), tmp.end() );
258 //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
259 //MIL << "ok" << endl;
265 ////////////////////////////////////////////////////////////////////////////
267 std::list<RepoInfo> readRepoFile(const Url & repo_file)
269 // no interface to download a specific file, using workaround:
270 //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
272 Pathname path(url.getPathName());
273 url.setPathName ("/");
274 MediaSetAccess access(url);
275 Pathname local = access.provideFile(path);
277 DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
279 return repositories_in_file(local);
282 ////////////////////////////////////////////////////////////////////////////
284 inline void assert_alias( const RepoInfo & info )
286 if ( info.alias().empty() )
287 ZYPP_THROW( RepoNoAliasException() );
290 inline void assert_alias( const ServiceInfo & info )
292 if ( info.alias().empty() )
293 ZYPP_THROW( ServiceNoAliasException() );
296 ////////////////////////////////////////////////////////////////////////////
298 inline void assert_urls( const RepoInfo & info )
300 if ( info.baseUrlsEmpty() )
301 ZYPP_THROW( RepoNoUrlException( info ) );
304 inline void assert_url( const ServiceInfo & info )
306 if ( ! info.url().isValid() )
307 ZYPP_THROW( ServiceNoUrlException( info ) );
310 ////////////////////////////////////////////////////////////////////////////
313 * \short Calculates the raw cache path for a repository, this is usually
314 * /var/cache/zypp/alias
316 inline Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
319 return opt.repoRawCachePath / info.escaped_alias();
323 * \short Calculates the raw metadata cache path for a repository, this is
324 * inside the raw cache dir, plus the path where the metadata is.
326 * It should be different only for repositories that are not in the root of
328 * for example /var/cache/zypp/alias/addondir
330 inline Pathname rawmetadata_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
333 return opt.repoRawCachePath / info.escaped_alias() / info.path();
338 * \short Calculates the packages cache path for a repository
340 inline Pathname packagescache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
343 return opt.repoPackagesCachePath / info.escaped_alias();
347 * \short Calculates the solv cache path for a repository
349 inline Pathname solv_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info)
352 return opt.repoSolvCachePath / info.escaped_alias();
355 ////////////////////////////////////////////////////////////////////////////
357 /** Functor collecting ServiceInfos into a ServiceSet. */
358 class ServiceCollector
361 typedef std::set<ServiceInfo> ServiceSet;
363 ServiceCollector( ServiceSet & services_r )
364 : _services( services_r )
367 bool operator()( const ServiceInfo & service_r ) const
369 _services.insert( service_r );
374 ServiceSet & _services;
377 ////////////////////////////////////////////////////////////////////////////
379 ///////////////////////////////////////////////////////////////////
381 // CLASS NAME : RepoManager::Impl
383 ///////////////////////////////////////////////////////////////////
386 * \short RepoManager implementation.
388 struct RepoManager::Impl
390 Impl( const RepoManagerOptions &opt )
393 init_knownServices();
394 init_knownRepositories();
397 RepoManagerOptions options;
405 void saveService( ServiceInfo & service ) const;
407 Pathname generateNonExistingName( const Pathname &dir,
408 const std::string &basefilename ) const;
410 std::string generateFilename( const RepoInfo & info ) const;
411 std::string generateFilename( const ServiceInfo & info ) const;
415 void init_knownServices();
416 void init_knownRepositories();
419 friend Impl * rwcowClone<Impl>( const Impl * rhs );
420 /** clone for RWCOW_pointer */
422 { return new Impl( *this ); }
425 ///////////////////////////////////////////////////////////////////
427 /** \relates RepoManager::Impl Stream output */
428 inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
430 return str << "RepoManager::Impl";
433 ///////////////////////////////////////////////////////////////////
435 void RepoManager::Impl::saveService( ServiceInfo & service ) const
437 filesystem::assert_dir( options.knownServicesPath );
438 Pathname servfile = generateNonExistingName( options.knownServicesPath,
439 generateFilename( service ) );
440 service.setFilepath( servfile );
442 MIL << "saving service in " << servfile << endl;
444 std::ofstream file( servfile.c_str() );
447 // TranslatorExplanation '%s' is a filename
448 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), servfile.c_str() )));
450 service.dumpAsIniOn( file );
451 MIL << "done" << endl;
455 * Generate a non existing filename in a directory, using a base
456 * name. For example if a directory contains 3 files
462 * If you try to generate a unique filename for this directory,
463 * based on "ruu" you will get "ruu", but if you use the base
464 * "foo" you will get "foo_1"
466 * \param dir Directory where the file needs to be unique
467 * \param basefilename string to base the filename on.
469 Pathname RepoManager::Impl::generateNonExistingName( const Pathname & dir,
470 const std::string & basefilename ) const
472 std::string final_filename = basefilename;
474 while ( PathInfo(dir + final_filename).isExist() )
476 final_filename = basefilename + "_" + str::numstring(counter);
479 return dir + Pathname(final_filename);
482 ////////////////////////////////////////////////////////////////////////////
485 * \short Generate a related filename from a repo info
487 * From a repo info, it will try to use the alias as a filename
488 * escaping it if necessary. Other fallbacks can be added to
489 * this function in case there is no way to use the alias
491 std::string RepoManager::Impl::generateFilename( const RepoInfo & info ) const
493 std::string filename = info.alias();
494 // replace slashes with underscores
495 str::replaceAll( filename, "/", "_" );
497 filename = Pathname(filename).extend(".repo").asString();
498 MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
502 std::string RepoManager::Impl::generateFilename( const ServiceInfo & info ) const
504 std::string filename = info.alias();
505 // replace slashes with underscores
506 str::replaceAll( filename, "/", "_" );
508 filename = Pathname(filename).extend(".service").asString();
509 MIL << "generating filename for service [" << info.alias() << "] : '" << filename << "'" << endl;
514 void RepoManager::Impl::init_knownServices()
516 Pathname dir = options.knownServicesPath;
517 std::list<Pathname> entries;
518 if (PathInfo(dir).isExist())
520 if ( filesystem::readdir( entries, dir, false ) != 0 )
522 // TranslatorExplanation '%s' is a pathname
523 ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir.c_str())));
526 //str::regex allowedServiceExt("^\\.service(_[0-9]+)?$");
527 for_(it, entries.begin(), entries.end() )
529 parser::ServiceFileReader(*it, ServiceCollector(services));
534 void RepoManager::Impl::init_knownRepositories()
536 MIL << "start construct known repos" << endl;
538 if ( PathInfo(options.knownReposPath).isExist() )
540 RepoInfoList repol = repositories_in_dir(options.knownReposPath);
541 for ( RepoInfoList::iterator it = repol.begin();
545 // set the metadata path for the repo
546 Pathname metadata_path = rawmetadata_path_for_repoinfo(options, (*it));
547 (*it).setMetadataPath(metadata_path);
549 // set the downloaded packages path for the repo
550 Pathname packages_path = packagescache_path_for_repoinfo(options, (*it));
551 (*it).setPackagesPath(packages_path);
557 MIL << "end construct known repos" << endl;
560 ///////////////////////////////////////////////////////////////////
562 // CLASS NAME : RepoManager
564 ///////////////////////////////////////////////////////////////////
566 RepoManager::RepoManager( const RepoManagerOptions &opt )
567 : _pimpl( new Impl(opt) )
570 ////////////////////////////////////////////////////////////////////////////
572 RepoManager::~RepoManager()
575 ////////////////////////////////////////////////////////////////////////////
577 bool RepoManager::repoEmpty() const
578 { return _pimpl->repos.empty(); }
580 RepoManager::RepoSizeType RepoManager::repoSize() const
581 { return _pimpl->repos.size(); }
583 RepoManager::RepoConstIterator RepoManager::repoBegin() const
584 { return _pimpl->repos.begin(); }
586 RepoManager::RepoConstIterator RepoManager::repoEnd() const
587 { return _pimpl->repos.end(); }
589 RepoInfo RepoManager::getRepo( const std::string & alias ) const
591 for_( it, repoBegin(), repoEnd() )
592 if ( it->alias() == alias )
594 return RepoInfo::noRepo;
597 bool RepoManager::hasRepo( const std::string & alias ) const
599 for_( it, repoBegin(), repoEnd() )
600 if ( it->alias() == alias )
605 std::string RepoManager::makeStupidAlias( const Url & url_r )
607 std::string ret( url_r.getScheme() );
613 std::string host( url_r.getHost() );
614 if ( ! host.empty() )
620 static Date::ValueType serial = Date::now();
621 ret += Digest::digest( Digest::sha1(), str::hexstring( ++serial ) +url_r.asCompleteString() ).substr(0,8);
625 ////////////////////////////////////////////////////////////////////////////
627 Pathname RepoManager::metadataPath( const RepoInfo &info ) const
629 return rawmetadata_path_for_repoinfo(_pimpl->options, info );
632 Pathname RepoManager::packagesPath( const RepoInfo &info ) const
634 return packagescache_path_for_repoinfo(_pimpl->options, info );
637 ////////////////////////////////////////////////////////////////////////////
639 RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
641 Pathname rawpath = rawmetadata_path_for_repoinfo( _pimpl->options, info );
642 Pathname mediarootpath = rawcache_path_for_repoinfo( _pimpl->options, info );
643 RepoType repokind = info.type();
646 switch ( repokind.toEnum() )
648 case RepoType::NONE_e:
649 // unknown, probe the local metadata
650 repokind = probe(rawpath.asUrl());
656 switch ( repokind.toEnum() )
658 case RepoType::RPMMD_e :
660 status = RepoStatus( rawpath + "/repodata/repomd.xml");
664 case RepoType::YAST2_e :
666 status = RepoStatus( rawpath + "/content") && (RepoStatus( mediarootpath + "/media.1/media"));
670 case RepoType::RPMPLAINDIR_e :
672 if ( PathInfo(Pathname(rawpath + "/cookie")).isExist() )
673 status = RepoStatus( rawpath + "/cookie");
677 case RepoType::NONE_e :
678 // Return default RepoStatus in case of RepoType::NONE
679 // indicating it should be created?
680 // ZYPP_THROW(RepoUnknownTypeException());
686 void RepoManager::touchIndexFile(const RepoInfo & info)
688 Pathname rawpath = rawmetadata_path_for_repoinfo( _pimpl->options, info );
690 RepoType repokind = info.type();
691 if ( repokind.toEnum() == RepoType::NONE_e )
692 // unknown, probe the local metadata
693 repokind = probe(rawpath.asUrl());
694 // if still unknown, just return
695 if (repokind == RepoType::NONE_e)
699 switch ( repokind.toEnum() )
701 case RepoType::RPMMD_e :
702 p = Pathname(rawpath + "/repodata/repomd.xml");
705 case RepoType::YAST2_e :
706 p = Pathname(rawpath + "/content");
709 case RepoType::RPMPLAINDIR_e :
710 p = Pathname(rawpath + "/cookie");
713 case RepoType::NONE_e :
718 // touch the file, ignore error (they are logged anyway)
719 filesystem::touch(p);
722 RepoManager::RefreshCheckStatus RepoManager::checkIfToRefreshMetadata(
723 const RepoInfo &info,
725 RawMetadataRefreshPolicy policy )
729 RepoStatus oldstatus;
730 RepoStatus newstatus;
734 MIL << "Going to try to check whether refresh is needed for " << url << endl;
736 // first check old (cached) metadata
737 Pathname rawpath = rawmetadata_path_for_repoinfo( _pimpl->options, info );
738 filesystem::assert_dir(rawpath);
739 oldstatus = metadataStatus(info);
741 if ( oldstatus.empty() )
743 MIL << "No cached metadata, going to refresh" << endl;
744 return REFRESH_NEEDED;
748 std::string scheme( url.getScheme() );
749 if ( scheme == "cd" || scheme == "dvd" )
751 MIL << "never refresh CD/DVD" << endl;
752 return REPO_UP_TO_DATE;
756 // now we've got the old (cached) status, we can decide repo.refresh.delay
757 if (policy != RefreshForced && policy != RefreshIfNeededIgnoreDelay)
759 // difference in seconds
760 double diff = difftime(
761 (Date::ValueType)Date::now(),
762 (Date::ValueType)oldstatus.timestamp()) / 60;
764 DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
765 DBG << "current time: " << (Date::ValueType)Date::now() << endl;
766 DBG << "last refresh = " << diff << " minutes ago" << endl;
768 if (diff < ZConfig::instance().repo_refresh_delay())
770 MIL << "Repository '" << info.alias()
771 << "' has been refreshed less than repo.refresh.delay ("
772 << ZConfig::instance().repo_refresh_delay()
773 << ") minutes ago. Advising to skip refresh" << endl;
774 return REPO_CHECK_DELAYED;
778 // To test the new matadta create temp dir as sibling of rawpath
779 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
781 repo::RepoType repokind = info.type();
782 // if the type is unknown, try probing.
783 switch ( repokind.toEnum() )
785 case RepoType::NONE_e:
787 repokind = probe(url);
793 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
794 ( repokind.toEnum() == RepoType::YAST2_e ) )
796 MediaSetAccess media(url);
797 shared_ptr<repo::Downloader> downloader_ptr;
799 if ( repokind.toEnum() == RepoType::RPMMD_e )
800 downloader_ptr.reset(new yum::Downloader(info));
802 downloader_ptr.reset( new susetags::Downloader(info));
804 RepoStatus newstatus = downloader_ptr->status(media);
805 bool refresh = false;
806 if ( oldstatus.checksum() == newstatus.checksum() )
808 MIL << "repo has not changed" << endl;
809 if ( policy == RefreshForced )
811 MIL << "refresh set to forced" << endl;
817 MIL << "repo has changed, going to refresh" << endl;
822 touchIndexFile(info);
824 return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
826 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
828 MediaMounter media( url );
829 RepoStatus newstatus = parser::plaindir::dirStatus(media.getPathName());
830 bool refresh = false;
831 if ( oldstatus.checksum() == newstatus.checksum() )
833 MIL << "repo has not changed" << endl;
834 if ( policy == RefreshForced )
836 MIL << "refresh set to forced" << endl;
842 MIL << "repo has changed, going to refresh" << endl;
847 touchIndexFile(info);
849 return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
853 ZYPP_THROW(RepoUnknownTypeException(info));
856 catch ( const Exception &e )
859 ERR << "refresh check failed for " << url << endl;
863 return REFRESH_NEEDED; // default
866 void RepoManager::refreshMetadata( const RepoInfo &info,
867 RawMetadataRefreshPolicy policy,
868 const ProgressData::ReceiverFnc & progress )
873 // we will throw this later if no URL checks out fine
874 RepoException rexception(_("Valid metadata not found at specified URL(s)"));
876 // try urls one by one
877 for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
883 // check whether to refresh metadata
884 // if the check fails for this url, it throws, so another url will be checked
885 if (checkIfToRefreshMetadata(info, url, policy)!=REFRESH_NEEDED)
888 MIL << "Going to refresh metadata from " << url << endl;
890 repo::RepoType repokind = info.type();
892 // if the type is unknown, try probing.
893 switch ( repokind.toEnum() )
895 case RepoType::NONE_e:
897 repokind = probe(*it);
899 if (repokind.toEnum() != RepoType::NONE_e)
901 // Adjust the probed type in RepoInfo
902 info.setProbedType( repokind ); // lazy init!
903 //save probed type only for repos in system
904 for_( it, repoBegin(), repoEnd() )
906 if ( info.alias() == (*it).alias() )
908 RepoInfo modifiedrepo = info;
909 modifiedrepo.setType( repokind );
910 modifyRepository( info.alias(), modifiedrepo );
920 Pathname rawpath = rawmetadata_path_for_repoinfo( _pimpl->options, info );
921 filesystem::assert_dir(rawpath);
923 // create temp dir as sibling of rawpath
924 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
926 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
927 ( repokind.toEnum() == RepoType::YAST2_e ) )
929 MediaSetAccess media(url);
930 shared_ptr<repo::Downloader> downloader_ptr;
932 MIL << "Creating downloader for [ " << info.name() << " ]" << endl;
934 if ( repokind.toEnum() == RepoType::RPMMD_e )
935 downloader_ptr.reset(new yum::Downloader(info));
937 downloader_ptr.reset( new susetags::Downloader(info) );
940 * Given a downloader, sets the other repos raw metadata
941 * path as cache paths for the fetcher, so if another
942 * repo has the same file, it will not download it
943 * but copy it from the other repository
945 for_( it, repoBegin(), repoEnd() )
947 Pathname cachepath(rawmetadata_path_for_repoinfo( _pimpl->options, *it ));
948 if ( PathInfo(cachepath).isExist() )
949 downloader_ptr->addCachePath(cachepath);
952 downloader_ptr->download( media, tmpdir.path() );
954 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
956 MediaMounter media( url );
957 RepoStatus newstatus = parser::plaindir::dirStatus(media.getPathName());
959 std::ofstream file(( tmpdir.path() + "/cookie").c_str());
962 // TranslatorExplanation '%s' is a filename
963 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), (tmpdir.path()/"cookie").c_str() )));
966 file << newstatus.checksum() << endl;
972 ZYPP_THROW(RepoUnknownTypeException());
975 // ok we have the metadata, now exchange
977 filesystem::exchange( tmpdir.path(), rawpath );
982 catch ( const Exception &e )
985 ERR << "Trying another url..." << endl;
987 // remember the exception caught for the *first URL*
988 // if all other URLs fail, the rexception will be thrown with the
989 // cause of the problem of the first URL remembered
990 if (it == info.baseUrlsBegin())
991 rexception.remember(e);
994 ERR << "No more urls..." << endl;
995 ZYPP_THROW(rexception);
998 ////////////////////////////////////////////////////////////////////////////
1000 void RepoManager::cleanMetadata( const RepoInfo &info,
1001 const ProgressData::ReceiverFnc & progressfnc )
1003 ProgressData progress(100);
1004 progress.sendTo(progressfnc);
1006 filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_pimpl->options, info));
1010 void RepoManager::cleanPackages( const RepoInfo &info,
1011 const ProgressData::ReceiverFnc & progressfnc )
1013 ProgressData progress(100);
1014 progress.sendTo(progressfnc);
1016 filesystem::recursive_rmdir(packagescache_path_for_repoinfo(_pimpl->options, info));
1020 void RepoManager::buildCache( const RepoInfo &info,
1021 CacheBuildPolicy policy,
1022 const ProgressData::ReceiverFnc & progressrcv )
1025 Pathname rawpath = rawmetadata_path_for_repoinfo(_pimpl->options, info);
1027 filesystem::assert_dir(_pimpl->options.repoCachePath);
1028 RepoStatus raw_metadata_status = metadataStatus(info);
1029 if ( raw_metadata_status.empty() )
1031 /* if there is no cache at this point, we refresh the raw
1032 in case this is the first time - if it's !autorefresh,
1033 we may still refresh */
1034 refreshMetadata(info, RefreshIfNeeded, progressrcv );
1035 raw_metadata_status = metadataStatus(info);
1038 bool needs_cleaning = false;
1039 if ( isCached( info ) )
1041 MIL << info.alias() << " is already cached." << endl;
1042 RepoStatus cache_status = cacheStatus(info);
1044 if ( cache_status.checksum() == raw_metadata_status.checksum() )
1046 MIL << info.alias() << " cache is up to date with metadata." << endl;
1047 if ( policy == BuildIfNeeded ) {
1051 MIL << info.alias() << " cache rebuild is forced" << endl;
1055 needs_cleaning = true;
1058 ProgressData progress(100);
1059 callback::SendReport<ProgressReport> report;
1060 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1061 progress.name(str::form(_("Building repository '%s' cache"), info.name().c_str()));
1069 MIL << info.alias() << " building cache..." << endl;
1071 Pathname base = solv_path_for_repoinfo( _pimpl->options, info);
1072 filesystem::assert_dir(base);
1073 Pathname solvfile = base / "solv";
1076 repo::RepoType repokind = info.type();
1078 // if the type is unknown, try probing.
1079 switch ( repokind.toEnum() )
1081 case RepoType::NONE_e:
1082 // unknown, probe the local metadata
1083 repokind = probe(rawpath.asUrl());
1089 MIL << "repo type is " << repokind << endl;
1091 switch ( repokind.toEnum() )
1093 case RepoType::RPMMD_e :
1094 case RepoType::YAST2_e :
1095 case RepoType::RPMPLAINDIR_e :
1097 // Take care we unlink the solvfile on exception
1098 ManagedFile guard( solvfile, filesystem::unlink );
1099 scoped_ptr<MediaMounter> forPlainDirs;
1101 ExternalProgram::Arguments cmd;
1102 cmd.push_back( "repo2solv.sh" );
1104 // repo2solv expects -o as 1st arg!
1105 cmd.push_back( "-o" );
1106 cmd.push_back( solvfile.asString() );
1108 if ( repokind == RepoType::RPMPLAINDIR )
1110 forPlainDirs.reset( new MediaMounter( *info.baseUrlsBegin() ) );
1111 // recusive for plaindir as 2nd arg!
1112 cmd.push_back( "-R" );
1113 // FIXME this does only work form dir: URLs
1114 cmd.push_back( forPlainDirs->getPathName().c_str() );
1117 cmd.push_back( rawpath.asString() );
1119 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
1120 std::string errdetail;
1122 for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
1123 WAR << " " << output;
1124 if ( errdetail.empty() ) {
1125 errdetail = prog.command();
1128 errdetail += output;
1131 int ret = prog.close();
1134 RepoException ex(str::form( _("Failed to cache repo (%d)."), ret ));
1135 ex.remember( errdetail );
1140 guard.resetDispose();
1144 ZYPP_THROW(RepoUnknownTypeException( _("Unhandled repository type") ));
1147 // update timestamp and checksum
1148 setCacheStatus(info, raw_metadata_status);
1149 MIL << "Commit cache.." << endl;
1153 ////////////////////////////////////////////////////////////////////////////
1155 repo::RepoType RepoManager::probe( const Url &url ) const
1157 MIL << "going to probe the type of the repo " << endl;
1159 if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName() ).isDir() )
1161 // Handle non existing local directory in advance, as
1162 // MediaSetAccess does not support it.
1163 MIL << "Probed type NONE (not exists) at " << url << endl;
1164 return repo::RepoType::NONE;
1167 // prepare exception to be thrown if the type could not be determined
1168 // due to a media exception. We can't throw right away, because of some
1169 // problems with proxy servers returning an incorrect error
1170 // on ftp file-not-found(bnc #335906). Instead we'll check another types
1173 // TranslatorExplanation '%s' is an URL
1174 RepoException enew(str::form( _("Error trying to read from '%s'"), url.asString().c_str() ));
1175 bool gotMediaException = false;
1178 MediaSetAccess access(url);
1181 if ( access.doesFileExist("/repodata/repomd.xml") )
1183 MIL << "Probed type RPMMD at " << url << endl;
1184 return repo::RepoType::RPMMD;
1187 catch ( const media::MediaException &e )
1190 DBG << "problem checking for repodata/repomd.xml file" << endl;
1192 gotMediaException = true;
1197 if ( access.doesFileExist("/content") )
1199 MIL << "Probed type YAST2 at " << url << endl;
1200 return repo::RepoType::YAST2;
1203 catch ( const media::MediaException &e )
1206 DBG << "problem checking for content file" << endl;
1208 gotMediaException = true;
1211 // if it is a non-downloading URL denoting a directory
1212 if ( ! url.schemeIsDownloading() )
1214 MediaMounter media( url );
1215 if ( PathInfo(media.getPathName()).isDir() )
1217 // allow empty dirs for now
1218 MIL << "Probed type RPMPLAINDIR at " << url << endl;
1219 return repo::RepoType::RPMPLAINDIR;
1223 catch ( const Exception &e )
1226 // TranslatorExplanation '%s' is an URL
1227 Exception enew(str::form( _("Unknown error reading from '%s'"), url.asString().c_str() ));
1232 if (gotMediaException)
1235 MIL << "Probed type NONE at " << url << endl;
1236 return repo::RepoType::NONE;
1239 ////////////////////////////////////////////////////////////////////////////
1241 void RepoManager::cleanCache( const RepoInfo &info,
1242 const ProgressData::ReceiverFnc & progressrcv )
1244 ProgressData progress(100);
1245 progress.sendTo(progressrcv);
1248 filesystem::recursive_rmdir(solv_path_for_repoinfo(_pimpl->options, info));
1253 ////////////////////////////////////////////////////////////////////////////
1255 bool RepoManager::isCached( const RepoInfo &info ) const
1257 return PathInfo(solv_path_for_repoinfo( _pimpl->options, info ) / "solv").isExist();
1260 RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
1263 Pathname cookiefile = solv_path_for_repoinfo(_pimpl->options, info) / "cookie";
1265 return RepoStatus::fromCookieFile(cookiefile);
1268 void RepoManager::setCacheStatus( const RepoInfo &info, const RepoStatus &status )
1270 Pathname base = solv_path_for_repoinfo(_pimpl->options, info);
1271 filesystem::assert_dir(base);
1272 Pathname cookiefile = base / "cookie";
1274 status.saveToCookieFile(cookiefile);
1277 void RepoManager::loadFromCache( const RepoInfo & info,
1278 const ProgressData::ReceiverFnc & progressrcv )
1281 Pathname solvfile = solv_path_for_repoinfo(_pimpl->options, info) / "solv";
1283 if ( ! PathInfo(solvfile).isExist() )
1284 ZYPP_THROW(RepoNotCachedException(info));
1288 sat::Pool::instance().addRepoSolv( solvfile, info );
1290 catch ( const Exception & exp )
1293 MIL << "Try to handle exception by rebuilding the solv-file" << endl;
1294 cleanCache( info, progressrcv );
1295 buildCache( info, BuildIfNeeded, progressrcv );
1297 sat::Pool::instance().addRepoSolv( solvfile, info );
1301 ////////////////////////////////////////////////////////////////////////////
1303 void RepoManager::addRepository( const RepoInfo &info,
1304 const ProgressData::ReceiverFnc & progressrcv )
1308 ProgressData progress(100);
1309 callback::SendReport<ProgressReport> report;
1310 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1311 progress.name(str::form(_("Adding repository '%s'"), info.name().c_str()));
1314 MIL << "Try adding repo " << info << endl;
1316 RepoInfo tosave = info;
1317 if(_pimpl->repos.find(tosave)!= _pimpl->repos.end())
1318 ZYPP_THROW(RepoAlreadyExistsException(info));
1320 // check the first url for now
1321 if ( _pimpl->options.probe )
1323 DBG << "unknown repository type, probing" << endl;
1325 RepoType probedtype;
1326 probedtype = probe(*tosave.baseUrlsBegin());
1327 if ( tosave.baseUrlsSize() > 0 )
1329 if ( probedtype == RepoType::NONE )
1330 ZYPP_THROW(RepoUnknownTypeException());
1332 tosave.setType(probedtype);
1338 // assert the directory exists
1339 filesystem::assert_dir(_pimpl->options.knownReposPath);
1341 Pathname repofile = _pimpl->generateNonExistingName(
1342 _pimpl->options.knownReposPath, _pimpl->generateFilename(tosave));
1343 // now we have a filename that does not exists
1344 MIL << "Saving repo in " << repofile << endl;
1346 std::ofstream file(repofile.c_str());
1349 // TranslatorExplanation '%s' is a filename
1350 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), repofile.c_str() )));
1353 tosave.dumpAsIniOn(file);
1354 tosave.setFilepath(repofile);
1355 tosave.setMetadataPath( metadataPath( tosave ) );
1356 tosave.setPackagesPath( packagesPath( tosave ) );
1358 _pimpl->repos.insert(tosave);
1362 // check for credentials in Urls
1363 bool havePasswords = false;
1364 for_( urlit, tosave.baseUrlsBegin(), tosave.baseUrlsEnd() )
1365 if ( urlit->hasCredentialsInAuthority() )
1367 havePasswords = true;
1370 // save the credentials
1371 if ( havePasswords )
1373 media::CredentialManager cm(
1374 media::CredManagerOptions(_pimpl->options.rootDir) );
1376 for_(urlit, tosave.baseUrlsBegin(), tosave.baseUrlsEnd())
1377 if (urlit->hasCredentialsInAuthority())
1378 //! \todo use a method calling UI callbacks to ask where to save creds?
1379 cm.saveInUser(media::AuthData(*urlit));
1382 HistoryLog().addRepository(tosave);
1385 MIL << "done" << endl;
1388 void RepoManager::addRepositories( const Url &url,
1389 const ProgressData::ReceiverFnc & progressrcv )
1391 std::list<RepoInfo> repos = readRepoFile(url);
1392 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1396 // look if the alias is in the known repos.
1397 for_ ( kit, repoBegin(), repoEnd() )
1399 if ( (*it).alias() == (*kit).alias() )
1401 ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1402 ZYPP_THROW(RepoAlreadyExistsException(*it));
1407 std::string filename = Pathname(url.getPathName()).basename();
1409 if ( filename == Pathname() )
1411 // TranslatorExplanation '%s' is an URL
1412 ZYPP_THROW(RepoException(str::form( _("Invalid repo file name at '%s'"), url.asString().c_str() )));
1415 // assert the directory exists
1416 filesystem::assert_dir(_pimpl->options.knownReposPath);
1418 Pathname repofile = _pimpl->generateNonExistingName(_pimpl->options.knownReposPath, filename);
1419 // now we have a filename that does not exists
1420 MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1422 std::ofstream file(repofile.c_str());
1425 // TranslatorExplanation '%s' is a filename
1426 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), repofile.c_str() )));
1429 for ( std::list<RepoInfo>::iterator it = repos.begin();
1433 MIL << "Saving " << (*it).alias() << endl;
1434 it->setFilepath(repofile.asString());
1435 it->dumpAsIniOn(file);
1436 _pimpl->repos.insert(*it);
1438 HistoryLog(_pimpl->options.rootDir).addRepository(*it);
1441 MIL << "done" << endl;
1444 ////////////////////////////////////////////////////////////////////////////
1446 void RepoManager::removeRepository( const RepoInfo & info,
1447 const ProgressData::ReceiverFnc & progressrcv)
1449 ProgressData progress;
1450 callback::SendReport<ProgressReport> report;
1451 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1452 progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
1454 MIL << "Going to delete repo " << info.alias() << endl;
1456 for_( it, repoBegin(), repoEnd() )
1458 // they can be the same only if the provided is empty, that means
1459 // the provided repo has no alias
1461 if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1464 // TODO match by url
1466 // we have a matcing repository, now we need to know
1467 // where it does come from.
1468 RepoInfo todelete = *it;
1469 if (todelete.filepath().empty())
1471 ZYPP_THROW(RepoException( _("Can't figure out where the repo is stored.") ));
1475 // figure how many repos are there in the file:
1476 std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1477 if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
1479 // easy, only this one, just delete the file
1480 if ( filesystem::unlink(todelete.filepath()) != 0 )
1482 // TranslatorExplanation '%s' is a filename
1483 ZYPP_THROW(RepoException(str::form( _("Can't delete '%s'"), todelete.filepath().c_str() )));
1485 MIL << todelete.alias() << " sucessfully deleted." << endl;
1489 // there are more repos in the same file
1490 // write them back except the deleted one.
1492 //std::ofstream file(tmp.path().c_str());
1494 // assert the directory exists
1495 filesystem::assert_dir(todelete.filepath().dirname());
1497 std::ofstream file(todelete.filepath().c_str());
1500 // TranslatorExplanation '%s' is a filename
1501 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), todelete.filepath().c_str() )));
1503 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1504 fit != filerepos.end();
1507 if ( (*fit).alias() != todelete.alias() )
1508 (*fit).dumpAsIniOn(file);
1512 CombinedProgressData subprogrcv(progress, 70);
1513 CombinedProgressData cleansubprogrcv(progress, 30);
1514 // now delete it from cache
1515 if ( isCached(todelete) )
1516 cleanCache( todelete, subprogrcv);
1517 // now delete metadata (#301037)
1518 cleanMetadata( todelete, cleansubprogrcv);
1519 _pimpl->repos.erase(todelete);
1520 MIL << todelete.alias() << " sucessfully deleted." << endl;
1521 HistoryLog(_pimpl->options.rootDir).removeRepository(todelete);
1523 } // else filepath is empty
1526 // should not be reached on a sucess workflow
1527 ZYPP_THROW(RepoNotFoundException(info));
1530 ////////////////////////////////////////////////////////////////////////////
1532 void RepoManager::modifyRepository( const std::string &alias,
1533 const RepoInfo & newinfo_r,
1534 const ProgressData::ReceiverFnc & progressrcv )
1536 RepoInfo toedit = getRepositoryInfo(alias);
1537 RepoInfo newinfo( newinfo_r ); // need writable copy to upadte housekeeping data
1539 // check if the new alias already exists when renaming the repo
1540 if ( alias != newinfo.alias() && hasRepo( newinfo.alias() ) )
1542 ZYPP_THROW(RepoAlreadyExistsException(newinfo));
1545 if (toedit.filepath().empty())
1547 ZYPP_THROW(RepoException( _("Can't figure out where the repo is stored.") ));
1551 // figure how many repos are there in the file:
1552 std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1554 // there are more repos in the same file
1555 // write them back except the deleted one.
1557 //std::ofstream file(tmp.path().c_str());
1559 // assert the directory exists
1560 filesystem::assert_dir(toedit.filepath().dirname());
1562 std::ofstream file(toedit.filepath().c_str());
1565 // TranslatorExplanation '%s' is a filename
1566 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), toedit.filepath().c_str() )));
1568 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1569 fit != filerepos.end();
1572 // if the alias is different, dump the original
1573 // if it is the same, dump the provided one
1574 if ( (*fit).alias() != toedit.alias() )
1575 (*fit).dumpAsIniOn(file);
1577 newinfo.dumpAsIniOn(file);
1580 newinfo.setFilepath(toedit.filepath());
1581 _pimpl->repos.erase(toedit);
1582 _pimpl->repos.insert(newinfo);
1583 HistoryLog(_pimpl->options.rootDir).modifyRepository(toedit, newinfo);
1584 MIL << "repo " << alias << " modified" << endl;
1588 ////////////////////////////////////////////////////////////////////////////
1590 RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
1591 const ProgressData::ReceiverFnc & progressrcv )
1594 info.setAlias(alias);
1595 RepoConstIterator it = _pimpl->repos.find( info );
1596 if( it == repoEnd() )
1597 ZYPP_THROW(RepoNotFoundException(info));
1602 ////////////////////////////////////////////////////////////////////////////
1604 RepoInfo RepoManager::getRepositoryInfo( const Url & url,
1605 const url::ViewOption & urlview,
1606 const ProgressData::ReceiverFnc & progressrcv )
1608 for_( it, repoBegin(), repoEnd() )
1610 for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1611 urlit != (*it).baseUrlsEnd();
1614 if ((*urlit).asString(urlview) == url.asString(urlview))
1619 info.setBaseUrl(url);
1620 ZYPP_THROW(RepoNotFoundException(info));
1623 ////////////////////////////////////////////////////////////////////////////
1627 ////////////////////////////////////////////////////////////////////////////
1629 bool RepoManager::serviceEmpty() const
1630 { return _pimpl->services.empty(); }
1632 RepoManager::ServiceSizeType RepoManager::serviceSize() const
1633 { return _pimpl->services.size(); }
1635 RepoManager::ServiceConstIterator RepoManager::serviceBegin() const
1636 { return _pimpl->services.begin(); }
1638 RepoManager::ServiceConstIterator RepoManager::serviceEnd() const
1639 { return _pimpl->services.end(); }
1641 ServiceInfo RepoManager::getService( const std::string & alias ) const
1643 for_( it, serviceBegin(), serviceEnd() )
1644 if ( it->alias() == alias )
1646 return ServiceInfo::noService;
1649 bool RepoManager::hasService( const std::string & alias ) const
1651 for_( it, serviceBegin(), serviceEnd() )
1652 if ( it->alias() == alias )
1657 ////////////////////////////////////////////////////////////////////////////
1659 void RepoManager::addService( const std::string & alias, const Url & url )
1661 addService( ServiceInfo(alias, url) );
1664 void RepoManager::addService( const ServiceInfo & service )
1666 assert_alias( service );
1668 // check if service already exists
1669 if ( hasService( service.alias() ) )
1670 ZYPP_THROW( ServiceAlreadyExistsException( service ) );
1672 // Writable ServiceInfo is needed to save the location
1673 // of the .service file. Finaly insert into the service list.
1674 ServiceInfo toSave( service );
1675 _pimpl->saveService( toSave );
1676 _pimpl->services.insert( toSave );
1678 // check for credentials in Url (username:password, not ?credentials param)
1679 if ( toSave.url().hasCredentialsInAuthority() )
1681 media::CredentialManager cm(
1682 media::CredManagerOptions(_pimpl->options.rootDir) );
1684 //! \todo use a method calling UI callbacks to ask where to save creds?
1685 cm.saveInUser(media::AuthData(toSave.url()));
1688 MIL << "added service " << toSave.alias() << endl;
1691 ////////////////////////////////////////////////////////////////////////////
1693 void RepoManager::removeService( const std::string & alias )
1695 MIL << "Going to delete repo " << alias << endl;
1697 const ServiceInfo & service = getService( alias );
1699 Pathname location = service.filepath();
1700 if( location.empty() )
1702 ZYPP_THROW(RepoException( _("Can't figure out where the service is stored.") ));
1706 parser::ServiceFileReader( location, ServiceCollector(tmpSet) );
1708 // only one service definition in the file
1709 if ( tmpSet.size() == 1 )
1711 if ( filesystem::unlink(location) != 0 )
1713 // TranslatorExplanation '%s' is a filename
1714 ZYPP_THROW(RepoException(str::form( _("Can't delete '%s'"), location.c_str() )));
1716 MIL << alias << " sucessfully deleted." << endl;
1720 filesystem::assert_dir(location.dirname());
1722 std::ofstream file(location.c_str());
1725 // TranslatorExplanation '%s' is a filename
1726 ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), location.c_str() )));
1729 for_(it, tmpSet.begin(), tmpSet.end())
1731 if( it->alias() != alias )
1732 it->dumpAsIniOn(file);
1735 MIL << alias << " sucessfully deleted from file " << location << endl;
1738 // now remove all repositories added by this service
1739 RepoCollector rcollector;
1740 getRepositoriesInService( alias,
1741 boost::make_function_output_iterator(
1742 bind( &RepoCollector::collect, &rcollector, _1 ) ) );
1743 // cannot do this directly in getRepositoriesInService - would invalidate iterators
1744 for_(rit, rcollector.repos.begin(), rcollector.repos.end())
1745 removeRepository(*rit);
1748 void RepoManager::removeService( const ServiceInfo & service )
1749 { removeService(service.alias()); }
1751 ////////////////////////////////////////////////////////////////////////////
1753 void RepoManager::refreshServices()
1755 // copy the set of services since refreshService
1756 // can eventually invalidate the iterator
1757 ServiceSet services( serviceBegin(), serviceEnd() );
1758 for_( it, services.begin(), services.end() )
1760 if ( !it->enabled() )
1763 refreshService(*it);
1767 void RepoManager::refreshService( const ServiceInfo & service )
1768 { refreshService( service.alias() ); }
1770 void RepoManager::refreshService( const std::string & alias )
1772 ServiceInfo service( getService( alias ) );
1773 assert_alias( service );
1774 assert_url( service );
1775 // NOTE: It might be necessary to modify and rewrite the service info.
1776 // Either when probing the type, or when adjusting the repositories
1777 // enable/disable state.:
1778 bool serviceModified = false;
1779 MIL << "going to refresh service '" << service.alias() << "', url: "<< service.url() << endl;
1781 //! \todo add callbacks for apps (start, end, repo removed, repo added, repo changed)
1783 // if the type is unknown, try probing.
1784 if ( service.type() == repo::ServiceType::NONE )
1786 repo::ServiceType type = probeService( service.url() );
1787 if ( type != ServiceType::NONE )
1789 service.setProbedType( type ); // lazy init!
1790 serviceModified = true;
1794 // download the repo index file
1795 media::MediaManager mediamanager;
1796 media::MediaAccessId mid = mediamanager.open( service.url() );
1797 mediamanager.attach( mid );
1798 mediamanager.provideFile( mid, "repo/repoindex.xml" );
1799 Pathname path = mediamanager.localPath(mid, "repo/repoindex.xml" );
1801 // get target distro identifier
1802 std::string servicesTargetDistro = _pimpl->options.servicesTargetDistro;
1803 if ( servicesTargetDistro.empty() && getZYpp()->getTarget() )
1804 servicesTargetDistro = getZYpp()->target()->targetDistribution();
1805 DBG << "servicesTargetDistro: " << servicesTargetDistro << endl;
1808 RepoCollector collector(servicesTargetDistro);
1809 parser::RepoindexFileReader reader( path, bind( &RepoCollector::collect, &collector, _1 ) );
1810 mediamanager.release( mid );
1811 mediamanager.close( mid );
1814 // set service alias and base url for all collected repositories
1815 for_( it, collector.repos.begin(), collector.repos.end() )
1817 // if the repo url was not set by the repoindex parser, set service's url
1820 if ( it->baseUrlsEmpty() )
1821 url = service.url();
1824 // service repo can contain only one URL now, so no need to iterate.
1825 url = *it->baseUrlsBegin();
1828 // libzypp currently has problem with separate url + path handling
1829 // so just append the path to the baseurl
1830 if ( !it->path().empty() )
1832 Pathname path(url.getPathName());
1834 url.setPathName( path.asString() );
1838 // Prepend service alias:
1839 it->setAlias( str::form( "%s:%s", service.alias().c_str(), it->alias().c_str() ) );
1842 it->setBaseUrl( url );
1843 // set refrence to the parent service
1844 it->setService( service.alias() );
1847 ////////////////////////////////////////////////////////////////////////////
1848 // Now compare collected repos with the ones in the system...
1850 RepoInfoList oldRepos;
1851 getRepositoriesInService( service.alias(), std::back_inserter( oldRepos ) );
1853 // find old repositories to remove...
1854 for_( it, oldRepos.begin(), oldRepos.end() )
1856 if ( ! foundAliasIn( it->alias(), collector.repos ) )
1858 removeRepository( *it );
1862 ////////////////////////////////////////////////////////////////////////////
1863 // create missing repositories and modify exising ones if needed...
1864 for_( it, collector.repos.begin(), collector.repos.end() )
1866 // Service explicitly requests the repo being enabled?
1867 // Service explicitly requests the repo being disabled?
1868 // And hopefully not both ;) If so, enable wins.
1869 bool beEnabled = service.repoToEnableFind( it->alias() );
1870 bool beDisabled = service.repoToDisableFind( it->alias() );
1874 // Remove from enable request list.
1875 // NOTE: repoToDisable is handled differently.
1876 // It gets cleared on each refresh.
1877 service.delRepoToEnable( it->alias() );
1878 serviceModified = true;
1881 RepoInfoList::iterator oldRepo( findAlias( it->alias(), oldRepos ) );
1882 if ( oldRepo == oldRepos.end() )
1884 // Not found in oldRepos ==> a new repo to add
1886 // Make sure the service repo is created with the
1887 // appropriate enable and autorefresh true.
1888 it->setEnabled( beEnabled );
1889 it->setAutorefresh( true );
1891 // At that point check whether a repo with the same alias
1892 // exists outside this service. Maybe forcefully re-alias
1893 // the existing repo?
1894 addRepository( *it );
1896 // save repo credentials
1897 // ma@: task for modifyRepository?
1901 // ==> an exising repo to check
1902 bool oldRepoModified = false;
1907 if ( ! oldRepo->enabled() )
1909 oldRepo->setEnabled( true );
1910 oldRepoModified = true;
1913 else if ( beDisabled )
1915 if ( oldRepo->enabled() )
1917 oldRepo->setEnabled( false );
1918 oldRepoModified = true;
1923 // service repo can contain only one URL now, so no need to iterate.
1924 if ( oldRepo->url() != it->url() )
1926 oldRepo->setBaseUrl( it->url() );
1927 oldRepoModified = true;
1930 // save if modified:
1931 if ( oldRepoModified )
1933 modifyRepository( oldRepo->alias(), *oldRepo );
1938 // Unlike reposToEnable, reposToDisable is always cleared after refresh.
1939 if ( ! service.reposToDisableEmpty() )
1941 service.clearReposToDisable();
1942 serviceModified = true;
1945 ////////////////////////////////////////////////////////////////////////////
1946 // save service if modified:
1947 if ( serviceModified )
1949 // write out modified service file.
1950 modifyService( service.alias(), service );
1954 ////////////////////////////////////////////////////////////////////////////
1956 void RepoManager::modifyService(const std::string & oldAlias, const ServiceInfo & service)
1958 MIL << "Going to modify service " << oldAlias << endl;
1960 const ServiceInfo & oldService = getService(oldAlias);
1962 Pathname location = oldService.filepath();
1963 if( location.empty() )
1965 ZYPP_THROW(RepoException( _("Can't figure out where the service is stored.") ));
1968 // remember: there may multiple services being defined in one file:
1970 parser::ServiceFileReader( location, ServiceCollector(tmpSet) );
1972 filesystem::assert_dir(location.dirname());
1973 std::ofstream file(location.c_str());
1974 for_(it, tmpSet.begin(), tmpSet.end())
1976 if( *it != oldAlias )
1977 it->dumpAsIniOn(file);
1979 service.dumpAsIniOn(file);
1982 _pimpl->services.erase(oldAlias);
1983 _pimpl->services.insert(service);
1985 // changed properties affecting also repositories
1986 if( oldAlias != service.alias() // changed alias
1987 || oldService.enabled() != service.enabled() // changed enabled status
1990 std::vector<RepoInfo> toModify;
1991 getRepositoriesInService(oldAlias, std::back_inserter(toModify));
1992 for_( it, toModify.begin(), toModify.end() )
1994 if (oldService.enabled() && !service.enabled())
1995 it->setEnabled(false);
1996 else if (!oldService.enabled() && service.enabled())
1998 //! \todo do nothing? the repos will be enabled on service refresh
1999 //! \todo how to know the service needs a (auto) refresh????
2002 it->setService(service.alias());
2003 modifyRepository(it->alias(), *it);
2007 //! \todo refresh the service automatically if url is changed?
2010 ////////////////////////////////////////////////////////////////////////////
2012 repo::ServiceType RepoManager::probeService( const Url &url ) const
2016 MediaSetAccess access(url);
2017 if ( access.doesFileExist("/repo/repoindex.xml") )
2018 return repo::ServiceType::RIS;
2020 catch ( const media::MediaException &e )
2023 // TranslatorExplanation '%s' is an URL
2024 RepoException enew(str::form( _("Error trying to read from '%s'"), url.asString().c_str() ));
2028 catch ( const Exception &e )
2031 // TranslatorExplanation '%s' is an URL
2032 Exception enew(str::form( _("Unknown error reading from '%s'"), url.asString().c_str() ));
2037 return repo::ServiceType::NONE;
2040 ////////////////////////////////////////////////////////////////////////////
2042 std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
2044 return str << *obj._pimpl;
2047 /////////////////////////////////////////////////////////////////
2049 ///////////////////////////////////////////////////////////////////