1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoManager.cc
20 #include "zypp/base/InputStream.h"
21 #include "zypp/base/Logger.h"
22 #include "zypp/base/Gettext.h"
23 #include "zypp/base/Function.h"
24 #include "zypp/base/Regex.h"
25 #include "zypp/PathInfo.h"
26 #include "zypp/TmpPath.h"
27 #include "zypp/Service.h"
29 #include "zypp/repo/RepoException.h"
30 #include "zypp/RepoManager.h"
32 #include "zypp/media/MediaManager.h"
33 #include "zypp/MediaSetAccess.h"
34 #include "zypp/ExternalProgram.h"
35 #include "zypp/ManagedFile.h"
37 #include "zypp/parser/RepoFileReader.h"
38 #include "zypp/parser/ServiceFileReader.h"
39 #include "zypp/parser/RepoindexFileReader.h"
40 #include "zypp/repo/yum/Downloader.h"
41 #include "zypp/repo/susetags/Downloader.h"
42 #include "zypp/parser/plaindir/RepoParser.h"
44 #include "zypp/ZYppCallbacks.h"
47 #include "satsolver/pool.h"
48 #include "satsolver/repo.h"
49 #include "satsolver/repo_solv.h"
53 using namespace zypp::repo;
54 using namespace zypp::filesystem;
56 using namespace zypp::repo;
58 ///////////////////////////////////////////////////////////////////
60 { /////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////
64 // CLASS NAME : RepoManagerOptions
66 ///////////////////////////////////////////////////////////////////
68 RepoManagerOptions::RepoManagerOptions( const Pathname & root_r )
70 repoCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoCachePath() );
71 repoRawCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoMetadataPath() );
72 repoSolvCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoSolvfilesPath() );
73 repoPackagesCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoPackagesPath() );
74 knownReposPath = Pathname::assertprefix( root_r, ZConfig::instance().knownReposPath() );
75 knownServicesPath = Pathname::assertprefix( root_r, ZConfig::instance().knownServicesPath() );
76 probe = ZConfig::instance().repo_add_probe();
79 RepoManagerOptions RepoManagerOptions::makeTestSetup( const Pathname & root_r )
81 RepoManagerOptions ret;
82 ret.repoCachePath = root_r;
83 ret.repoRawCachePath = root_r/"raw";
84 ret.repoSolvCachePath = root_r/"solv";
85 ret.repoPackagesCachePath = root_r/"packages";
86 ret.knownReposPath = root_r/"repos.d";
87 ret.knownServicesPath = root_r/"services.d";
91 ////////////////////////////////////////////////////////////////////////////
94 * \short Simple callback to collect the results
96 * Classes like RepoFileParser call the callback
97 * once per each repo in a file.
99 * Passing this functor as callback, you can collect
100 * all resuls at the end, without dealing with async
111 bool collect( const RepoInfo &repo )
113 repos.push_back(repo);
120 ////////////////////////////////////////////////////////////////////////////
123 * Reads RepoInfo's from a repo file.
125 * \param file pathname of the file to read.
127 static std::list<RepoInfo> repositories_in_file( const Pathname & file )
129 MIL << "repo file: " << file << endl;
130 RepoCollector collector;
131 parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
132 return collector.repos;
135 ////////////////////////////////////////////////////////////////////////////
137 std::list<RepoInfo> readRepoFile(const Url & repo_file)
139 // no interface to download a specific file, using workaround:
140 //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
142 Pathname path(url.getPathName());
143 url.setPathName ("/");
144 MediaSetAccess access(url);
145 Pathname local = access.provideFile(path);
147 DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
149 return repositories_in_file(local);
152 ////////////////////////////////////////////////////////////////////////////
155 * \short List of RepoInfo's from a directory
157 * Goes trough every file ending with ".repo" in a directory and adds all
158 * RepoInfo's contained in that file.
160 * \param dir pathname of the directory to read.
162 static std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
164 MIL << "directory " << dir << endl;
165 list<RepoInfo> repos;
166 list<Pathname> entries;
167 if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
168 ZYPP_THROW(Exception("failed to read directory"));
170 str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
171 for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
173 if (str::regex_match(it->extension(), allowedRepoExt))
175 list<RepoInfo> tmp = repositories_in_file( *it );
176 repos.insert( repos.end(), tmp.begin(), tmp.end() );
178 //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
179 //MIL << "ok" << endl;
185 ////////////////////////////////////////////////////////////////////////////
187 static void assert_alias( const RepoInfo &info )
189 if (info.alias().empty())
190 ZYPP_THROW(RepoNoAliasException());
193 ////////////////////////////////////////////////////////////////////////////
195 static void assert_urls( const RepoInfo &info )
197 if (info.baseUrlsEmpty())
198 ZYPP_THROW(RepoNoUrlException());
201 ////////////////////////////////////////////////////////////////////////////
204 * \short Calculates the raw cache path for a repository
206 static Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
209 return opt.repoRawCachePath / info.escaped_alias();
213 * \short Calculates the packages cache path for a repository
215 static Pathname packagescache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
218 return opt.repoPackagesCachePath / info.escaped_alias();
221 static Pathname solv_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info)
224 return opt.repoSolvCachePath / info.escaped_alias();
227 ///////////////////////////////////////////////////////////////////
229 // CLASS NAME : RepoManager::Impl
231 ///////////////////////////////////////////////////////////////////
234 * \short RepoManager implementation.
236 struct RepoManager::Impl
238 Impl( const RepoManagerOptions &opt )
251 RepoManagerOptions options;
258 /** Offer default Impl. */
259 static shared_ptr<Impl> nullimpl()
261 static shared_ptr<Impl> _nullimpl( new Impl );
265 void saveService( const Service& service ) const;
267 Pathname generateNonExistingName( const Pathname &dir,
268 const std::string &basefilename ) const;
270 std::string generateFilename( const RepoInfo &info ) const;
271 std::string generateFilename( const Service &info ) const;
273 struct ServiceCollector {
274 ServiceCollector(ServiceSet& services_) : services(services_) {}
275 bool collect(Service service) { services.insert(service); return true; }
277 ServiceSet& services;
280 void knownServices();
282 void knownRepositories();
285 friend Impl * rwcowClone<Impl>( const Impl * rhs );
286 /** clone for RWCOW_pointer */
288 { return new Impl( *this ); }
291 ///////////////////////////////////////////////////////////////////
293 /** \relates RepoManager::Impl Stream output */
294 inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
296 return str << "RepoManager::Impl";
299 ///////////////////////////////////////////////////////////////////
301 // CLASS NAME : RepoManager
303 ///////////////////////////////////////////////////////////////////
305 RepoManager::RepoManager( const RepoManagerOptions &opt )
306 : _pimpl( new Impl(opt) )
309 ////////////////////////////////////////////////////////////////////////////
311 RepoManager::~RepoManager()
314 ////////////////////////////////////////////////////////////////////////////
316 bool RepoManager::repoEmpty() const { return _pimpl->repos.empty(); }
317 RepoManager::RepoSizeType RepoManager::repoSize() const
318 { return _pimpl->repos.size(); }
319 RepoManager::RepoConstIterator RepoManager::repoBegin() const
320 { return _pimpl->repos.begin(); }
321 RepoManager::RepoConstIterator RepoManager::repoEnd() const
322 { return _pimpl->repos.end(); }
325 std::list<RepoInfo> RepoManager::knownRepositories() const
327 return std::list<RepoInfo>(repoBegin(),repoEnd());
330 void RepoManager::Impl::knownRepositories()
332 MIL << "start construct known repos" << endl;
334 if ( PathInfo(options.knownReposPath).isExist() )
336 RepoInfoList repol = repositories_in_dir(options.knownReposPath);
337 for ( RepoInfoList::iterator it = repol.begin();
341 // set the metadata path for the repo
342 Pathname metadata_path = rawcache_path_for_repoinfo(options, (*it));
343 (*it).setMetadataPath(metadata_path);
345 // set the downloaded packages path for the repo
346 Pathname packages_path = packagescache_path_for_repoinfo(options, (*it));
347 (*it).setPackagesPath(packages_path);
353 MIL << "end construct known repos" << endl;
356 ////////////////////////////////////////////////////////////////////////////
358 Pathname RepoManager::metadataPath( const RepoInfo &info ) const
360 return rawcache_path_for_repoinfo(_pimpl->options, info );
363 Pathname RepoManager::packagesPath( const RepoInfo &info ) const
365 return packagescache_path_for_repoinfo(_pimpl->options, info );
368 ////////////////////////////////////////////////////////////////////////////
370 RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
372 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
373 RepoType repokind = info.type();
376 switch ( repokind.toEnum() )
378 case RepoType::NONE_e:
379 // unknown, probe the local metadata
380 repokind = probe(rawpath.asUrl());
386 switch ( repokind.toEnum() )
388 case RepoType::RPMMD_e :
390 status = RepoStatus( rawpath + "/repodata/repomd.xml");
394 case RepoType::YAST2_e :
396 status = RepoStatus( rawpath + "/content") && (RepoStatus( rawpath + "/media.1/media"));
400 case RepoType::RPMPLAINDIR_e :
402 if ( PathInfo(Pathname(rawpath + "/cookie")).isExist() )
403 status = RepoStatus( rawpath + "/cookie");
407 case RepoType::NONE_e :
408 // Return default RepoStatus in case of RepoType::NONE
409 // indicating it should be created?
410 // ZYPP_THROW(RepoUnknownTypeException());
416 void RepoManager::touchIndexFile(const RepoInfo & info)
418 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
420 RepoType repokind = info.type();
421 if ( repokind.toEnum() == RepoType::NONE_e )
422 // unknown, probe the local metadata
423 repokind = probe(rawpath.asUrl());
424 // if still unknown, just return
425 if (repokind == RepoType::NONE_e)
429 switch ( repokind.toEnum() )
431 case RepoType::RPMMD_e :
432 p = Pathname(rawpath + "/repodata/repomd.xml");
435 case RepoType::YAST2_e :
436 p = Pathname(rawpath + "/content");
439 case RepoType::RPMPLAINDIR_e :
440 p = Pathname(rawpath + "/cookie");
443 case RepoType::NONE_e :
448 // touch the file, ignore error (they are logged anyway)
449 filesystem::touch(p);
452 RepoManager::RefreshCheckStatus RepoManager::checkIfToRefreshMetadata(
453 const RepoInfo &info,
455 RawMetadataRefreshPolicy policy )
459 RepoStatus oldstatus;
460 RepoStatus newstatus;
464 MIL << "Going to try to check whether refresh is needed for " << url << endl;
466 repo::RepoType repokind = info.type();
468 // if the type is unknown, try probing.
469 switch ( repokind.toEnum() )
471 case RepoType::NONE_e:
473 repokind = probe(url);
479 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
480 filesystem::assert_dir(rawpath);
481 oldstatus = metadataStatus(info);
483 // now we've got the old (cached) status, we can decide repo.refresh.delay
484 if (policy != RefreshForced && policy != RefreshIfNeededIgnoreDelay)
486 // difference in seconds
487 double diff = difftime(
488 (Date::ValueType)Date::now(),
489 (Date::ValueType)oldstatus.timestamp()) / 60;
491 DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
492 DBG << "current time: " << (Date::ValueType)Date::now() << endl;
493 DBG << "last refresh = " << diff << " minutes ago" << endl;
495 if (diff < ZConfig::instance().repo_refresh_delay())
497 MIL << "Repository '" << info.alias()
498 << "' has been refreshed less than repo.refresh.delay ("
499 << ZConfig::instance().repo_refresh_delay()
500 << ") minutes ago. Advising to skip refresh" << endl;
501 return REPO_CHECK_DELAYED;
505 // create temp dir as sibling of rawpath
506 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
508 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
509 ( repokind.toEnum() == RepoType::YAST2_e ) )
511 MediaSetAccess media(url);
512 shared_ptr<repo::Downloader> downloader_ptr;
514 if ( repokind.toEnum() == RepoType::RPMMD_e )
515 downloader_ptr.reset(new yum::Downloader(info.path()));
517 downloader_ptr.reset( new susetags::Downloader(info.path()));
519 RepoStatus newstatus = downloader_ptr->status(media);
520 bool refresh = false;
521 if ( oldstatus.checksum() == newstatus.checksum() )
523 MIL << "repo has not changed" << endl;
524 if ( policy == RefreshForced )
526 MIL << "refresh set to forced" << endl;
532 MIL << "repo has changed, going to refresh" << endl;
537 touchIndexFile(info);
539 return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
541 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
543 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
544 bool refresh = false;
545 if ( oldstatus.checksum() == newstatus.checksum() )
547 MIL << "repo has not changed" << endl;
548 if ( policy == RefreshForced )
550 MIL << "refresh set to forced" << endl;
556 MIL << "repo has changed, going to refresh" << endl;
561 touchIndexFile(info);
563 return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
567 ZYPP_THROW(RepoUnknownTypeException(info));
570 catch ( const Exception &e )
573 ERR << "refresh check failed for " << url << endl;
577 return REFRESH_NEEDED; // default
580 void RepoManager::refreshMetadata( const RepoInfo &info,
581 RawMetadataRefreshPolicy policy,
582 const ProgressData::ReceiverFnc & progress )
587 // we will throw this later if no URL checks out fine
588 RepoException rexception(_("Valid metadata not found at specified URL(s)"));
590 // try urls one by one
591 for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
597 // check whether to refresh metadata
598 // if the check fails for this url, it throws, so another url will be checked
599 if (checkIfToRefreshMetadata(info, url, policy)!=REFRESH_NEEDED)
602 MIL << "Going to refresh metadata from " << url << endl;
604 repo::RepoType repokind = info.type();
606 // if the type is unknown, try probing.
607 switch ( repokind.toEnum() )
609 case RepoType::NONE_e:
611 repokind = probe(*it);
613 if (repokind.toEnum() != RepoType::NONE_e)
615 // Adjust the probed type in RepoInfo
616 info.setProbedType( repokind ); // lazy init!
617 //save probed type only for repos in system
618 for_( it, repoBegin(), repoEnd() )
620 if ( info.alias() == (*it).alias() )
622 RepoInfo modifiedrepo = info;
623 modifiedrepo.setType(repokind);
624 modifyRepository(info.alias(),modifiedrepo);
633 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
634 filesystem::assert_dir(rawpath);
636 // create temp dir as sibling of rawpath
637 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
639 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
640 ( repokind.toEnum() == RepoType::YAST2_e ) )
642 MediaSetAccess media(url);
643 shared_ptr<repo::Downloader> downloader_ptr;
645 if ( repokind.toEnum() == RepoType::RPMMD_e )
646 downloader_ptr.reset(new yum::Downloader(info.path()));
648 downloader_ptr.reset( new susetags::Downloader(info.path()));
651 * Given a downloader, sets the other repos raw metadata
652 * path as cache paths for the fetcher, so if another
653 * repo has the same file, it will not download it
654 * but copy it from the other repository
656 for_( it, repoBegin(), repoEnd() )
658 Pathname cachepath(rawcache_path_for_repoinfo( _pimpl->options, *it ));
659 if ( PathInfo(cachepath).isExist() )
660 downloader_ptr->addCachePath(cachepath);
663 downloader_ptr->download( media, tmpdir.path());
665 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
667 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
669 std::ofstream file(( tmpdir.path() + "/cookie").c_str());
671 ZYPP_THROW (Exception( "Can't open " + tmpdir.path().asString() + "/cookie" ) );
674 file << newstatus.checksum() << endl;
680 ZYPP_THROW(RepoUnknownTypeException());
683 // ok we have the metadata, now exchange
686 TmpDir oldmetadata( TmpDir::makeSibling( rawpath ) );
687 filesystem::rename( rawpath, oldmetadata.path() );
688 // move the just downloaded there
689 filesystem::rename( tmpdir.path(), rawpath );
694 catch ( const Exception &e )
697 ERR << "Trying another url..." << endl;
699 // remember the exception caught for the *first URL*
700 // if all other URLs fail, the rexception will be thrown with the
701 // cause of the problem of the first URL remembered
702 if (it == info.baseUrlsBegin())
703 rexception.remember(e);
706 ERR << "No more urls..." << endl;
707 ZYPP_THROW(rexception);
710 ////////////////////////////////////////////////////////////////////////////
712 void RepoManager::cleanMetadata( const RepoInfo &info,
713 const ProgressData::ReceiverFnc & progressfnc )
715 ProgressData progress(100);
716 progress.sendTo(progressfnc);
718 filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_pimpl->options, info));
722 void RepoManager::cleanPackages( const RepoInfo &info,
723 const ProgressData::ReceiverFnc & progressfnc )
725 ProgressData progress(100);
726 progress.sendTo(progressfnc);
728 filesystem::recursive_rmdir(packagescache_path_for_repoinfo(_pimpl->options, info));
732 void RepoManager::buildCache( const RepoInfo &info,
733 CacheBuildPolicy policy,
734 const ProgressData::ReceiverFnc & progressrcv )
737 Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
739 filesystem::assert_dir(_pimpl->options.repoCachePath);
740 RepoStatus raw_metadata_status = metadataStatus(info);
741 if ( raw_metadata_status.empty() )
743 /* if there is no cache at this point, we refresh the raw
744 in case this is the first time - if it's !autorefresh,
745 we may still refresh */
746 refreshMetadata(info, RefreshIfNeeded, progressrcv );
747 raw_metadata_status = metadataStatus(info);
750 bool needs_cleaning = false;
751 if ( isCached( info ) )
753 MIL << info.alias() << " is already cached." << endl;
754 RepoStatus cache_status = cacheStatus(info);
756 if ( cache_status.checksum() == raw_metadata_status.checksum() )
758 MIL << info.alias() << " cache is up to date with metadata." << endl;
759 if ( policy == BuildIfNeeded ) {
763 MIL << info.alias() << " cache rebuild is forced" << endl;
767 needs_cleaning = true;
770 ProgressData progress(100);
771 callback::SendReport<ProgressReport> report;
772 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
773 progress.name(str::form(_("Building repository '%s' cache"), info.name().c_str()));
781 MIL << info.alias() << " building cache..." << endl;
783 Pathname base = solv_path_for_repoinfo( _pimpl->options, info);
784 filesystem::assert_dir(base);
785 Pathname solvfile = base / "solv";
788 repo::RepoType repokind = info.type();
790 // if the type is unknown, try probing.
791 switch ( repokind.toEnum() )
793 case RepoType::NONE_e:
794 // unknown, probe the local metadata
795 repokind = probe(rawpath.asUrl());
801 MIL << "repo type is " << repokind << endl;
803 switch ( repokind.toEnum() )
805 case RepoType::RPMMD_e :
806 case RepoType::YAST2_e :
807 case RepoType::RPMPLAINDIR_e :
809 // Take care we unlink the solvfile on exception
810 ManagedFile guard( solvfile, filesystem::unlink );
813 std::string toFile( str::gsub(solvfile.asString(),"\"","\\\"") );
814 if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
816 cmd << str::form( "repo2solv.sh \"%s\" > \"%s\"",
817 str::gsub( info.baseUrlsBegin()->getPathName(),"\"","\\\"" ).c_str(),
822 cmd << str::form( "repo2solv.sh \"%s\" > \"%s\"",
823 str::gsub( rawpath.asString(),"\"","\\\"" ).c_str(),
826 MIL << "Executing: " << cmd.str() << endl;
827 ExternalProgram prog( cmd.str(), ExternalProgram::Stderr_To_Stdout );
830 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
831 WAR << " " << output;
832 cmd << " " << output;
835 int ret = prog.close();
838 RepoException ex(str::form("Failed to cache repo (%d).", ret));
839 ex.remember( cmd.str() );
844 guard.resetDispose();
848 ZYPP_THROW(RepoUnknownTypeException("Unhandled repository type"));
852 switch ( repokind.toEnum() )
854 case RepoType::RPMMD_e :
857 CombinedProgressData subprogrcv( progress, 100);
858 parser::yum::RepoParser parser(id, store, parser::yum::RepoParserOpts(), subprogrcv);
859 parser.parse(rawpath);
863 case RepoType::YAST2_e :
866 CombinedProgressData subprogrcv( progress, 100);
867 parser::susetags::RepoParser parser(id, store, subprogrcv);
868 parser.parse(rawpath);
874 ZYPP_THROW(RepoUnknownTypeException());
877 // update timestamp and checksum
878 //store.updateRepositoryStatus(id, raw_metadata_status);
879 setCacheStatus(info, raw_metadata_status);
880 MIL << "Commit cache.." << endl;
885 ////////////////////////////////////////////////////////////////////////////
887 repo::RepoType RepoManager::probe( const Url &url ) const
889 if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName() ).isDir() )
891 // Handle non existing local directory in advance, as
892 // MediaSetAccess does not support it.
893 return repo::RepoType::NONE;
898 MediaSetAccess access(url);
899 if ( access.doesFileExist("/repodata/repomd.xml") )
900 return repo::RepoType::RPMMD;
901 if ( access.doesFileExist("/content") )
902 return repo::RepoType::YAST2;
904 // if it is a local url of type dir
905 if ( (! media::MediaManager::downloads(url)) && ( url.getScheme() == "dir" ) )
907 Pathname path = Pathname(url.getPathName());
908 if ( PathInfo(path).isDir() )
910 // allow empty dirs for now
911 return repo::RepoType::RPMPLAINDIR;
915 catch ( const media::MediaException &e )
918 RepoException enew("Error trying to read from " + url.asString());
922 catch ( const Exception &e )
925 Exception enew("Unknown error reading from " + url.asString());
930 return repo::RepoType::NONE;
933 ////////////////////////////////////////////////////////////////////////////
935 void RepoManager::cleanCache( const RepoInfo &info,
936 const ProgressData::ReceiverFnc & progressrcv )
938 ProgressData progress(100);
939 progress.sendTo(progressrcv);
942 filesystem::recursive_rmdir(solv_path_for_repoinfo(_pimpl->options, info));
947 ////////////////////////////////////////////////////////////////////////////
949 bool RepoManager::isCached( const RepoInfo &info ) const
951 return PathInfo(solv_path_for_repoinfo( _pimpl->options, info ) / "solv").isExist();
954 RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
957 Pathname cookiefile = solv_path_for_repoinfo(_pimpl->options, info) / "cookie";
959 return RepoStatus::fromCookieFile(cookiefile);
962 void RepoManager::setCacheStatus( const RepoInfo &info, const RepoStatus &status )
964 Pathname base = solv_path_for_repoinfo(_pimpl->options, info);
965 filesystem::assert_dir(base);
966 Pathname cookiefile = base / "cookie";
968 status.saveToCookieFile(cookiefile);
971 void RepoManager::loadFromCache( const RepoInfo & info,
972 const ProgressData::ReceiverFnc & progressrcv )
975 Pathname solvfile = solv_path_for_repoinfo(_pimpl->options, info) / "solv";
977 if ( ! PathInfo(solvfile).isExist() )
978 ZYPP_THROW(RepoNotCachedException(info));
982 sat::Pool::instance().addRepoSolv( solvfile, info );
984 catch ( const Exception & exp )
987 MIL << "Try to handle exception by rebuilding the solv-file" << endl;
988 cleanCache( info, progressrcv );
989 buildCache( info, BuildIfNeeded, progressrcv );
991 sat::Pool::instance().addRepoSolv( solvfile, info );
995 ////////////////////////////////////////////////////////////////////////////
998 * Generate a non existing filename in a directory, using a base
999 * name. For example if a directory contains 3 files
1005 * If you try to generate a unique filename for this directory,
1006 * based on "ruu" you will get "ruu", but if you use the base
1007 * "foo" you will get "foo_1"
1009 * \param dir Directory where the file needs to be unique
1010 * \param basefilename string to base the filename on.
1012 Pathname RepoManager::Impl::generateNonExistingName( const Pathname &dir,
1013 const std::string &basefilename ) const
1015 string final_filename = basefilename;
1017 while ( PathInfo(dir + final_filename).isExist() )
1019 final_filename = basefilename + "_" + str::numstring(counter);
1022 return dir + Pathname(final_filename);
1025 ////////////////////////////////////////////////////////////////////////////
1028 * \short Generate a related filename from a repo info
1030 * From a repo info, it will try to use the alias as a filename
1031 * escaping it if necessary. Other fallbacks can be added to
1032 * this function in case there is no way to use the alias
1034 std::string RepoManager::Impl::generateFilename( const RepoInfo &info ) const
1036 std::string filename = info.alias();
1037 // replace slashes with underscores
1038 str::replaceAll( filename, "/", "_" );
1040 filename = Pathname(filename).extend(".repo").asString();
1041 MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
1045 std::string RepoManager::Impl::generateFilename( const Service &info ) const
1047 std::string filename = info.name();
1048 // replace slashes with underscores
1049 str::replaceAll( filename, "/", "_" );
1051 filename = Pathname(filename).extend(".service").asString();
1052 MIL << "generating filename for service [" << info.name() << "] : '" << filename << "'" << endl;
1056 ////////////////////////////////////////////////////////////////////////////
1058 void RepoManager::addRepository( const RepoInfo &info,
1059 const ProgressData::ReceiverFnc & progressrcv )
1063 ProgressData progress(100);
1064 callback::SendReport<ProgressReport> report;
1065 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1066 progress.name(str::form(_("Adding repository '%s'"), info.name().c_str()));
1069 RepoInfo tosave = info;
1070 if(_pimpl->repos.find(tosave)!= _pimpl->repos.end())
1071 ZYPP_THROW(RepoAlreadyExistsException(info));
1074 // check the first url for now
1075 if ( _pimpl->options.probe )
1077 DBG << "unknown repository type, probing" << endl;
1079 RepoType probedtype;
1080 probedtype = probe(*tosave.baseUrlsBegin());
1081 if ( tosave.baseUrlsSize() > 0 )
1083 if ( probedtype == RepoType::NONE )
1084 ZYPP_THROW(RepoUnknownTypeException());
1086 tosave.setType(probedtype);
1092 // assert the directory exists
1093 filesystem::assert_dir(_pimpl->options.knownReposPath);
1095 Pathname repofile = _pimpl->generateNonExistingName(
1096 _pimpl->options.knownReposPath, _pimpl->generateFilename(tosave));
1097 // now we have a filename that does not exists
1098 MIL << "Saving repo in " << repofile << endl;
1100 std::ofstream file(repofile.c_str());
1102 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1105 tosave.dumpRepoOn(file);
1106 tosave.setFilepath(repofile);
1107 _pimpl->repos.insert(tosave);
1109 MIL << "done" << endl;
1112 void RepoManager::addRepositories( const Url &url,
1113 const ProgressData::ReceiverFnc & progressrcv )
1115 std::list<RepoInfo> repos = readRepoFile(url);
1116 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1120 // look if the alias is in the known repos.
1121 for_ ( kit, repoBegin(), repoEnd() )
1123 if ( (*it).alias() == (*kit).alias() )
1125 ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1126 ZYPP_THROW(RepoAlreadyExistsException(*it));
1131 string filename = Pathname(url.getPathName()).basename();
1133 if ( filename == Pathname() )
1134 ZYPP_THROW(RepoException("Invalid repo file name at " + url.asString() ));
1136 // assert the directory exists
1137 filesystem::assert_dir(_pimpl->options.knownReposPath);
1139 Pathname repofile = _pimpl->generateNonExistingName(_pimpl->options.knownReposPath, filename);
1140 // now we have a filename that does not exists
1141 MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1143 std::ofstream file(repofile.c_str());
1145 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1148 for ( std::list<RepoInfo>::iterator it = repos.begin();
1152 MIL << "Saving " << (*it).alias() << endl;
1153 it->setFilepath(repofile.asString());
1154 it->dumpRepoOn(file);
1155 _pimpl->repos.insert(*it);
1157 MIL << "done" << endl;
1160 ////////////////////////////////////////////////////////////////////////////
1162 void RepoManager::removeRepository( const RepoInfo & info,
1163 const ProgressData::ReceiverFnc & progressrcv)
1165 ProgressData progress;
1166 callback::SendReport<ProgressReport> report;
1167 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1168 progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
1170 MIL << "Going to delete repo " << info.alias() << endl;
1172 for_( it, repoBegin(), repoEnd() )
1174 // they can be the same only if the provided is empty, that means
1175 // the provided repo has no alias
1177 if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1180 // TODO match by url
1182 // we have a matcing repository, now we need to know
1183 // where it does come from.
1184 RepoInfo todelete = *it;
1185 if (todelete.filepath().empty())
1187 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1191 // figure how many repos are there in the file:
1192 std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1193 if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
1195 // easy, only this one, just delete the file
1196 if ( filesystem::unlink(todelete.filepath()) != 0 )
1198 ZYPP_THROW(RepoException("Can't delete " + todelete.filepath().asString()));
1200 MIL << todelete.alias() << " sucessfully deleted." << endl;
1204 // there are more repos in the same file
1205 // write them back except the deleted one.
1207 //std::ofstream file(tmp.path().c_str());
1209 // assert the directory exists
1210 filesystem::assert_dir(todelete.filepath().dirname());
1212 std::ofstream file(todelete.filepath().c_str());
1214 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1215 ZYPP_THROW (Exception( "Can't open " + todelete.filepath().asString() ) );
1217 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1218 fit != filerepos.end();
1221 if ( (*fit).alias() != todelete.alias() )
1222 (*fit).dumpRepoOn(file);
1226 CombinedProgressData subprogrcv(progress, 70);
1227 CombinedProgressData cleansubprogrcv(progress, 30);
1228 // now delete it from cache
1229 if ( isCached(todelete) )
1230 cleanCache( todelete, subprogrcv);
1231 // now delete metadata (#301037)
1232 cleanMetadata( todelete, cleansubprogrcv);
1233 _pimpl->repos.erase(todelete);
1234 MIL << todelete.alias() << " sucessfully deleted." << endl;
1236 } // else filepath is empty
1239 // should not be reached on a sucess workflow
1240 ZYPP_THROW(RepoNotFoundException(info));
1243 ////////////////////////////////////////////////////////////////////////////
1245 void RepoManager::modifyRepository( const std::string &alias,
1246 const RepoInfo & newinfo,
1247 const ProgressData::ReceiverFnc & progressrcv )
1249 RepoInfo toedit = getRepositoryInfo(alias);
1251 // check if the new alias already exists when renaming the repo
1252 if (alias != newinfo.alias())
1254 for_( it, repoBegin(), repoEnd() )
1256 if ( newinfo.alias() == (*it).alias() )
1257 ZYPP_THROW(RepoAlreadyExistsException(newinfo));
1261 if (toedit.filepath().empty())
1263 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1267 // figure how many repos are there in the file:
1268 std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1270 // there are more repos in the same file
1271 // write them back except the deleted one.
1273 //std::ofstream file(tmp.path().c_str());
1275 // assert the directory exists
1276 filesystem::assert_dir(toedit.filepath().dirname());
1278 std::ofstream file(toedit.filepath().c_str());
1280 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1281 ZYPP_THROW (Exception( "Can't open " + toedit.filepath().asString() ) );
1283 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1284 fit != filerepos.end();
1287 // if the alias is different, dump the original
1288 // if it is the same, dump the provided one
1289 if ( (*fit).alias() != toedit.alias() )
1290 (*fit).dumpRepoOn(file);
1292 newinfo.dumpRepoOn(file);
1295 _pimpl->repos.erase(toedit);
1296 _pimpl->repos.insert(newinfo);
1300 ////////////////////////////////////////////////////////////////////////////
1302 RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
1303 const ProgressData::ReceiverFnc & progressrcv )
1306 info.setAlias(alias);
1307 RepoConstIterator it = _pimpl->repos.find( info );
1308 if( it == repoEnd() )
1309 ZYPP_THROW(RepoNotFoundException(info));
1314 ////////////////////////////////////////////////////////////////////////////
1316 RepoInfo RepoManager::getRepositoryInfo( const Url & url,
1317 const url::ViewOption & urlview,
1318 const ProgressData::ReceiverFnc & progressrcv )
1320 for_( it, repoBegin(), repoEnd() )
1322 for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1323 urlit != (*it).baseUrlsEnd();
1326 if ((*urlit).asString(urlview) == url.asString(urlview))
1331 info.setBaseUrl(url);
1332 ZYPP_THROW(RepoNotFoundException(info));
1335 void RepoManager::addService( const std::string& name, const Url& url )
1337 addService( Service(name,url) );
1340 void RepoManager::addService( const Service& service )
1342 //check if service isn't already exist
1343 if( _pimpl->services.find(service)!= _pimpl->services.end() )
1344 return; //FIXME ZYPP_THROW(RepoAlreadyExistsException(service.name()));
1346 //this is need to save location to correct service
1347 const Service& savedService = *(_pimpl->services.insert( service )).first;
1349 _pimpl->saveService( savedService );
1352 void RepoManager::removeService( const string& name)
1354 MIL << "Going to delete repo " << name << endl;
1356 const Service& service = getService( name );
1358 Pathname location = service.location();
1359 if( location.empty() )
1361 ZYPP_THROW(RepoException("Can't figure where the service is stored"));
1365 Impl::ServiceCollector collector(tmpSet);
1367 parser::ServiceFileReader reader( location,
1368 bind(&Impl::ServiceCollector::collect,collector,_1) );
1370 if ( tmpSet.size() == 1 ) //only one record
1372 if ( filesystem::unlink(location) != 0 )
1374 ZYPP_THROW(RepoException("Can't delete " + location.asString()));
1376 MIL << name << " sucessfully deleted." << endl;
1380 filesystem::assert_dir(location.dirname());
1382 std::ofstream file(location.c_str());
1384 ZYPP_THROW(Exception("failed open file to write"));
1386 for_(it, tmpSet.begin(), tmpSet.end())
1388 if( it->name() != name )
1389 it->dumpServiceOn(file);
1392 MIL << name << " sucessfully deleted from file " << location << endl;
1395 //now remove all repositories added by this service
1396 getRepositoriesInService( name,
1397 boost::make_function_output_iterator(bind(&RepoManager::removeRepository, this, _1, ProgressData::ReceiverFnc()) ) );
1401 void RepoManager::Impl::saveService( const Service& service ) const
1403 filesystem::assert_dir( options.knownServicesPath );
1405 Pathname servfile = generateNonExistingName( options.knownServicesPath,
1406 generateFilename( service ) );
1408 MIL << "saving service in " << servfile << endl;
1410 std::ofstream file(servfile.c_str());
1412 ZYPP_THROW (Exception( "Can't open " + servfile.asString() ) );
1415 service.dumpServiceOn( file );
1417 const_cast<Service&>(service).setLocation( servfile );
1418 MIL << "done" << endl;
1421 Service RepoManager::getService( const std::string& name ) const
1423 ServiceConstIterator it = _pimpl->services.find(name);
1424 if ( it == serviceEnd() )
1425 return Service::noService;
1430 bool RepoManager::serviceEmpty() const { return _pimpl->services.empty(); }
1432 RepoManager::ServiceSizeType RepoManager::serviceSize() const
1434 return _pimpl->services.size();
1437 RepoManager::ServiceConstIterator RepoManager::serviceBegin() const
1439 return _pimpl->services.begin();
1442 RepoManager::ServiceConstIterator RepoManager::serviceEnd() const
1444 return _pimpl->services.end();
1447 void RepoManager::refreshServices()
1449 //cannot user for_, because it uses const version
1450 for (std::set<Service>::iterator it = _pimpl->services.begin();
1451 it != _pimpl->services.end(); ++it)
1453 MIL << "refresh: "<<it->name() << " with url: "<< it->url().asString() << endl;
1454 refreshService(*it);
1458 void RepoManager::refreshService( const Service& service )
1460 //download index file
1461 media::MediaManager mediamanager;
1462 media::MediaAccessId mid = mediamanager.open( service.url() ); //FIXME check if url is not empty
1463 mediamanager.attachDesiredMedia( mid );
1464 mediamanager.provideFile( mid, "repo/repoindex.xml" );
1465 Pathname path = mediamanager.localPath(mid, "repo/repoindex.xml" );
1468 RepoCollector collector;
1469 parser::RepoindexFileReader reader( path,
1470 bind( &RepoCollector::collect, &collector, _1 ) );
1471 mediamanager.release( mid );
1472 mediamanager.close( mid );
1474 // set base url for all collected repositories
1475 for_( it, collector.repos.begin(), collector.repos.end())
1477 it->setBaseUrl( service.url() );
1478 it->setService(service.name());
1481 //compare old and new repositories (hope not to much, if it change
1482 // then construct set and use set operation on it)
1484 std::list<RepoInfo> oldRepos;
1485 getRepositoriesInService(service.name(),
1486 insert_iterator<std::list<RepoInfo> >
1487 (oldRepos,oldRepos.begin()));
1489 //find old to remove
1490 for_( it, oldRepos.begin(), oldRepos.end() )
1494 for_( it2, collector.repos.begin(), collector.repos.end() )
1495 if ( it->alias() == it2->alias() )
1502 removeRepository( *it );
1506 for_( it, collector.repos.begin(), collector.repos.end() )
1510 for_( it2, oldRepos.begin(), oldRepos.end() )
1511 if( it->alias() == it2->alias() )
1518 addRepository( *it );
1523 void RepoManager::modifyService(const std::string& oldName, const Service& service)
1525 MIL << "Going to modify service " << oldName << endl;
1527 const Service& oldService = getService(oldName);
1529 Pathname location = oldService.location();
1530 if( location.empty() )
1532 ZYPP_THROW(RepoException("Can't figure where the service is stored"));
1536 Impl::ServiceCollector collector(tmpSet);
1538 parser::ServiceFileReader reader( location,
1539 bind(&Impl::ServiceCollector::collect,collector,_1) );
1541 filesystem::assert_dir(location.dirname());
1543 std::ofstream file(location.c_str());
1545 for_(it, tmpSet.begin(), tmpSet.end())
1547 if( *it != oldName )
1548 it->dumpServiceOn(file);
1551 service.dumpServiceOn(file);
1555 _pimpl->services.erase(oldName);
1556 _pimpl->services.insert(service);
1558 if( oldName != service.name() ) //changed name, must change also repository
1560 std::vector<RepoInfo> toModify;
1561 getRepositoriesInService(oldName,
1562 insert_iterator<std::vector<RepoInfo> >( toModify, toModify.begin() ));
1563 for_( it, toModify.begin(), toModify.end() )
1565 it->setService(service.name());
1566 modifyRepository(it->alias(), *it);
1572 void RepoManager::Impl::knownServices()
1574 ServiceCollector collector(services);
1575 Pathname dir = options.knownServicesPath;
1576 list<Pathname> entries;
1577 if (PathInfo(dir).isExist())
1579 if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
1580 ZYPP_THROW(Exception("failed to read directory"));
1582 str::regex allowedServiceExt("^\\.service(_[0-9]+)?$");
1583 for_(it, entries.begin(), entries.end() )
1585 parser::ServiceFileReader reader(*it,
1586 bind(&ServiceCollector::collect, collector, _1) );
1591 ////////////////////////////////////////////////////////////////////////////
1593 std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
1595 return str << *obj._pimpl;
1598 /////////////////////////////////////////////////////////////////
1600 ///////////////////////////////////////////////////////////////////