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/repo/yum/Downloader.h"
40 #include "zypp/parser/yum/RepoParser.h"
41 #include "zypp/repo/susetags/Downloader.h"
42 #include "zypp/parser/susetags/RepoParser.h"
43 #include "zypp/parser/plaindir/RepoParser.h"
45 #include "zypp/ZYppCallbacks.h"
48 #include "satsolver/pool.h"
49 #include "satsolver/repo.h"
50 #include "satsolver/repo_solv.h"
54 using namespace zypp::repo;
55 using namespace zypp::filesystem;
57 using namespace zypp::repo;
59 ///////////////////////////////////////////////////////////////////
61 { /////////////////////////////////////////////////////////////////
63 ///////////////////////////////////////////////////////////////////
65 // CLASS NAME : RepoManagerOptions
67 ///////////////////////////////////////////////////////////////////
69 RepoManagerOptions::RepoManagerOptions( const Pathname & root_r )
71 repoCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoCachePath() );
72 repoRawCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoMetadataPath() );
73 repoSolvCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoSolvfilesPath() );
74 repoPackagesCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoPackagesPath() );
75 knownReposPath = Pathname::assertprefix( root_r, ZConfig::instance().knownReposPath() );
76 knownServicesPath = Pathname::assertprefix( root_r, ZConfig::instance().knownServicesPath() );
77 probe = ZConfig::instance().repo_add_probe();
80 RepoManagerOptions RepoManagerOptions::makeTestSetup( const Pathname & root_r )
82 RepoManagerOptions ret;
83 ret.repoCachePath = root_r;
84 ret.repoRawCachePath = root_r/"raw";
85 ret.repoSolvCachePath = root_r/"solv";
86 ret.repoPackagesCachePath = root_r/"packages";
87 ret.knownReposPath = root_r/"repos.d";
88 ret.knownServicesPath = root_r/"services.d";
92 ////////////////////////////////////////////////////////////////////////////
95 * \short Simple callback to collect the results
97 * Classes like RepoFileParser call the callback
98 * once per each repo in a file.
100 * Passing this functor as callback, you can collect
101 * all resuls at the end, without dealing with async
112 bool collect( const RepoInfo &repo )
114 repos.push_back(repo);
121 ////////////////////////////////////////////////////////////////////////////
124 * Reads RepoInfo's from a repo file.
126 * \param file pathname of the file to read.
128 static std::list<RepoInfo> repositories_in_file( const Pathname & file )
130 MIL << "repo file: " << file << endl;
131 RepoCollector collector;
132 parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
133 return collector.repos;
136 ////////////////////////////////////////////////////////////////////////////
138 std::list<RepoInfo> readRepoFile(const Url & repo_file)
140 // no interface to download a specific file, using workaround:
141 //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
143 Pathname path(url.getPathName());
144 url.setPathName ("/");
145 MediaSetAccess access(url);
146 Pathname local = access.provideFile(path);
148 DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
150 return repositories_in_file(local);
153 ////////////////////////////////////////////////////////////////////////////
156 * \short List of RepoInfo's from a directory
158 * Goes trough every file ending with ".repo" in a directory and adds all
159 * RepoInfo's contained in that file.
161 * \param dir pathname of the directory to read.
163 static std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
165 MIL << "directory " << dir << endl;
166 list<RepoInfo> repos;
167 list<Pathname> entries;
168 if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
169 ZYPP_THROW(Exception("failed to read directory"));
171 str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
172 for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
174 if (str::regex_match(it->extension(), allowedRepoExt))
176 list<RepoInfo> tmp = repositories_in_file( *it );
177 repos.insert( repos.end(), tmp.begin(), tmp.end() );
179 //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
180 //MIL << "ok" << endl;
186 ////////////////////////////////////////////////////////////////////////////
188 static void assert_alias( const RepoInfo &info )
190 if (info.alias().empty())
191 ZYPP_THROW(RepoNoAliasException());
194 ////////////////////////////////////////////////////////////////////////////
196 static void assert_urls( const RepoInfo &info )
198 if (info.baseUrlsEmpty())
199 ZYPP_THROW(RepoNoUrlException());
202 ////////////////////////////////////////////////////////////////////////////
205 * \short Calculates the raw cache path for a repository
207 static Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
210 return opt.repoRawCachePath / info.escaped_alias();
214 * \short Calculates the packages cache path for a repository
216 static Pathname packagescache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
219 return opt.repoPackagesCachePath / info.escaped_alias();
222 static Pathname solv_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info)
225 return opt.repoSolvCachePath / info.escaped_alias();
228 ///////////////////////////////////////////////////////////////////
230 // CLASS NAME : RepoManager::Impl
232 ///////////////////////////////////////////////////////////////////
235 * \short RepoManager implementation.
237 struct RepoManager::Impl
239 Impl( const RepoManagerOptions &opt )
250 RepoManagerOptions options;
252 map<string, RepoInfo> _repoinfos;
257 /** Offer default Impl. */
258 static shared_ptr<Impl> nullimpl()
260 static shared_ptr<Impl> _nullimpl( new Impl );
264 void saveService( const Service& service );
266 Pathname generateNonExistingName( const Pathname &dir,
267 const std::string &basefilename ) const;
269 std::string generateFilename( const RepoInfo &info ) const;
270 std::string generateFilename( const Service &info ) const;
272 struct ServiceCollector {
273 ServiceCollector(ServiceSet& services_) : services(services_) {}
274 bool collect(Service service) { services.insert(service); return true; }
276 ServiceSet& services;
279 void knownServices();
282 friend Impl * rwcowClone<Impl>( const Impl * rhs );
283 /** clone for RWCOW_pointer */
285 { return new Impl( *this ); }
288 ///////////////////////////////////////////////////////////////////
290 /** \relates RepoManager::Impl Stream output */
291 inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
293 return str << "RepoManager::Impl";
296 ///////////////////////////////////////////////////////////////////
298 // CLASS NAME : RepoManager
300 ///////////////////////////////////////////////////////////////////
302 RepoManager::RepoManager( const RepoManagerOptions &opt )
303 : _pimpl( new Impl(opt) )
306 ////////////////////////////////////////////////////////////////////////////
308 RepoManager::~RepoManager()
311 ////////////////////////////////////////////////////////////////////////////
313 std::list<RepoInfo> RepoManager::knownRepositories() const
317 if ( PathInfo(_pimpl->options.knownReposPath).isExist() )
319 RepoInfoList repos = repositories_in_dir(_pimpl->options.knownReposPath);
320 for ( RepoInfoList::iterator it = repos.begin();
324 // set the metadata path for the repo
325 Pathname metadata_path = rawcache_path_for_repoinfo(_pimpl->options, (*it));
326 (*it).setMetadataPath(metadata_path);
328 // set the downloaded packages path for the repo
329 Pathname packages_path = packagescache_path_for_repoinfo(_pimpl->options, (*it));
330 (*it).setPackagesPath(packages_path);
335 return std::list<RepoInfo>();
340 ////////////////////////////////////////////////////////////////////////////
342 Pathname RepoManager::metadataPath( const RepoInfo &info ) const
344 return rawcache_path_for_repoinfo(_pimpl->options, info );
347 Pathname RepoManager::packagesPath( const RepoInfo &info ) const
349 return packagescache_path_for_repoinfo(_pimpl->options, info );
352 ////////////////////////////////////////////////////////////////////////////
354 RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
356 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
357 RepoType repokind = info.type();
360 switch ( repokind.toEnum() )
362 case RepoType::NONE_e:
363 // unknown, probe the local metadata
364 repokind = probe(rawpath.asUrl());
370 switch ( repokind.toEnum() )
372 case RepoType::RPMMD_e :
374 status = RepoStatus( rawpath + "/repodata/repomd.xml");
378 case RepoType::YAST2_e :
380 status = RepoStatus( rawpath + "/content") && (RepoStatus( rawpath + "/media.1/media"));
384 case RepoType::RPMPLAINDIR_e :
386 if ( PathInfo(Pathname(rawpath + "/cookie")).isExist() )
387 status = RepoStatus( rawpath + "/cookie");
391 case RepoType::NONE_e :
392 // Return default RepoStatus in case of RepoType::NONE
393 // indicating it should be created?
394 // ZYPP_THROW(RepoUnknownTypeException());
400 void RepoManager::touchIndexFile(const RepoInfo & info)
402 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
404 RepoType repokind = info.type();
405 if ( repokind.toEnum() == RepoType::NONE_e )
406 // unknown, probe the local metadata
407 repokind = probe(rawpath.asUrl());
408 // if still unknown, just return
409 if (repokind == RepoType::NONE_e)
413 switch ( repokind.toEnum() )
415 case RepoType::RPMMD_e :
416 p = Pathname(rawpath + "/repodata/repomd.xml");
419 case RepoType::YAST2_e :
420 p = Pathname(rawpath + "/content");
423 case RepoType::RPMPLAINDIR_e :
424 p = Pathname(rawpath + "/cookie");
427 case RepoType::NONE_e :
432 // touch the file, ignore error (they are logged anyway)
433 filesystem::touch(p);
436 RepoManager::RefreshCheckStatus RepoManager::checkIfToRefreshMetadata(
437 const RepoInfo &info,
439 RawMetadataRefreshPolicy policy )
443 RepoStatus oldstatus;
444 RepoStatus newstatus;
448 MIL << "Going to try to check whether refresh is needed for " << url << endl;
450 repo::RepoType repokind = info.type();
452 // if the type is unknown, try probing.
453 switch ( repokind.toEnum() )
455 case RepoType::NONE_e:
457 repokind = probe(url);
463 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
464 filesystem::assert_dir(rawpath);
465 oldstatus = metadataStatus(info);
467 // now we've got the old (cached) status, we can decide repo.refresh.delay
468 if (policy != RefreshForced && policy != RefreshIfNeededIgnoreDelay)
470 // difference in seconds
471 double diff = difftime(
472 (Date::ValueType)Date::now(),
473 (Date::ValueType)oldstatus.timestamp()) / 60;
475 DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
476 DBG << "current time: " << (Date::ValueType)Date::now() << endl;
477 DBG << "last refresh = " << diff << " minutes ago" << endl;
479 if (diff < ZConfig::instance().repo_refresh_delay())
481 MIL << "Repository '" << info.alias()
482 << "' has been refreshed less than repo.refresh.delay ("
483 << ZConfig::instance().repo_refresh_delay()
484 << ") minutes ago. Advising to skip refresh" << endl;
485 return REPO_CHECK_DELAYED;
489 // create temp dir as sibling of rawpath
490 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
492 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
493 ( repokind.toEnum() == RepoType::YAST2_e ) )
495 MediaSetAccess media(url);
496 shared_ptr<repo::Downloader> downloader_ptr;
498 if ( repokind.toEnum() == RepoType::RPMMD_e )
499 downloader_ptr.reset(new yum::Downloader(info.path()));
501 downloader_ptr.reset( new susetags::Downloader(info.path()));
503 RepoStatus newstatus = downloader_ptr->status(media);
504 bool refresh = false;
505 if ( oldstatus.checksum() == newstatus.checksum() )
507 MIL << "repo has not changed" << endl;
508 if ( policy == RefreshForced )
510 MIL << "refresh set to forced" << endl;
516 MIL << "repo has changed, going to refresh" << endl;
521 touchIndexFile(info);
523 return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
525 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
527 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
528 bool refresh = false;
529 if ( oldstatus.checksum() == newstatus.checksum() )
531 MIL << "repo has not changed" << endl;
532 if ( policy == RefreshForced )
534 MIL << "refresh set to forced" << endl;
540 MIL << "repo has changed, going to refresh" << endl;
545 touchIndexFile(info);
547 return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
551 ZYPP_THROW(RepoUnknownTypeException(info));
554 catch ( const Exception &e )
557 ERR << "refresh check failed for " << url << endl;
561 return REFRESH_NEEDED; // default
564 void RepoManager::refreshMetadata( const RepoInfo &info,
565 RawMetadataRefreshPolicy policy,
566 const ProgressData::ReceiverFnc & progress )
571 // we will throw this later if no URL checks out fine
572 RepoException rexception(_("Valid metadata not found at specified URL(s)"));
574 // try urls one by one
575 for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
581 // check whether to refresh metadata
582 // if the check fails for this url, it throws, so another url will be checked
583 if (checkIfToRefreshMetadata(info, url, policy)!=REFRESH_NEEDED)
586 MIL << "Going to refresh metadata from " << url << endl;
588 repo::RepoType repokind = info.type();
590 // if the type is unknown, try probing.
591 switch ( repokind.toEnum() )
593 case RepoType::NONE_e:
595 repokind = probe(*it);
597 if (repokind.toEnum() != RepoType::NONE_e)
599 // Adjust the probed type in RepoInfo
600 info.setProbedType( repokind ); // lazy init!
601 //save probed type only for repos in system
602 std::list<RepoInfo> repos = knownRepositories();
603 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
604 it != repos.end(); ++it )
606 if ( info.alias() == (*it).alias() )
608 RepoInfo modifiedrepo = info;
609 modifiedrepo.setType(repokind);
610 modifyRepository(info.alias(),modifiedrepo);
619 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
620 filesystem::assert_dir(rawpath);
622 // create temp dir as sibling of rawpath
623 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
625 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
626 ( repokind.toEnum() == RepoType::YAST2_e ) )
628 MediaSetAccess media(url);
629 shared_ptr<repo::Downloader> downloader_ptr;
631 if ( repokind.toEnum() == RepoType::RPMMD_e )
632 downloader_ptr.reset(new yum::Downloader(info.path()));
634 downloader_ptr.reset( new susetags::Downloader(info.path()));
637 * Given a downloader, sets the other repos raw metadata
638 * path as cache paths for the fetcher, so if another
639 * repo has the same file, it will not download it
640 * but copy it from the other repository
642 std::list<RepoInfo> repos = knownRepositories();
643 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
647 Pathname cachepath(rawcache_path_for_repoinfo( _pimpl->options, *it ));
648 if ( PathInfo(cachepath).isExist() )
649 downloader_ptr->addCachePath(cachepath);
652 downloader_ptr->download( media, tmpdir.path());
654 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
656 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
658 std::ofstream file(( tmpdir.path() + "/cookie").c_str());
660 ZYPP_THROW (Exception( "Can't open " + tmpdir.path().asString() + "/cookie" ) );
663 file << newstatus.checksum() << endl;
669 ZYPP_THROW(RepoUnknownTypeException());
672 // ok we have the metadata, now exchange
675 TmpDir oldmetadata( TmpDir::makeSibling( rawpath ) );
676 filesystem::rename( rawpath, oldmetadata.path() );
677 // move the just downloaded there
678 filesystem::rename( tmpdir.path(), rawpath );
683 catch ( const Exception &e )
686 ERR << "Trying another url..." << endl;
688 // remember the exception caught for the *first URL*
689 // if all other URLs fail, the rexception will be thrown with the
690 // cause of the problem of the first URL remembered
691 if (it == info.baseUrlsBegin())
692 rexception.remember(e);
695 ERR << "No more urls..." << endl;
696 ZYPP_THROW(rexception);
699 ////////////////////////////////////////////////////////////////////////////
701 void RepoManager::cleanMetadata( const RepoInfo &info,
702 const ProgressData::ReceiverFnc & progressfnc )
704 ProgressData progress(100);
705 progress.sendTo(progressfnc);
707 filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_pimpl->options, info));
711 void RepoManager::cleanPackages( const RepoInfo &info,
712 const ProgressData::ReceiverFnc & progressfnc )
714 ProgressData progress(100);
715 progress.sendTo(progressfnc);
717 filesystem::recursive_rmdir(packagescache_path_for_repoinfo(_pimpl->options, info));
721 void RepoManager::buildCache( const RepoInfo &info,
722 CacheBuildPolicy policy,
723 const ProgressData::ReceiverFnc & progressrcv )
726 Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
728 filesystem::assert_dir(_pimpl->options.repoCachePath);
729 RepoStatus raw_metadata_status = metadataStatus(info);
730 if ( raw_metadata_status.empty() )
732 /* if there is no cache at this point, we refresh the raw
733 in case this is the first time - if it's !autorefresh,
734 we may still refresh */
735 refreshMetadata(info, RefreshIfNeeded, progressrcv );
736 raw_metadata_status = metadataStatus(info);
739 bool needs_cleaning = false;
740 if ( isCached( info ) )
742 MIL << info.alias() << " is already cached." << endl;
743 //data::RecordId id = store.lookupRepository(info.alias());
744 RepoStatus cache_status = cacheStatus(info);
746 if ( cache_status.checksum() == raw_metadata_status.checksum() )
748 MIL << info.alias() << " cache is up to date with metadata." << endl;
749 if ( policy == BuildIfNeeded ) {
753 MIL << info.alias() << " cache rebuild is forced" << endl;
757 needs_cleaning = true;
760 ProgressData progress(100);
761 callback::SendReport<ProgressReport> report;
762 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
763 progress.name(str::form(_("Building repository '%s' cache"), info.name().c_str()));
771 MIL << info.alias() << " building cache..." << endl;
773 Pathname base = solv_path_for_repoinfo( _pimpl->options, info);
774 filesystem::assert_dir(base);
775 Pathname solvfile = base / "solv";
778 repo::RepoType repokind = info.type();
780 // if the type is unknown, try probing.
781 switch ( repokind.toEnum() )
783 case RepoType::NONE_e:
784 // unknown, probe the local metadata
785 repokind = probe(rawpath.asUrl());
791 MIL << "repo type is " << repokind << endl;
793 switch ( repokind.toEnum() )
795 case RepoType::RPMMD_e :
796 case RepoType::YAST2_e :
797 case RepoType::RPMPLAINDIR_e :
799 // Take care we unlink the solvfile on exception
800 ManagedFile guard( solvfile, filesystem::unlink );
803 std::string toFile( str::gsub(solvfile.asString(),"\"","\\\"") );
804 if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
806 cmd << str::form( "repo2solv.sh \"%s\" > \"%s\"",
807 str::gsub( info.baseUrlsBegin()->getPathName(),"\"","\\\"" ).c_str(),
812 cmd << str::form( "repo2solv.sh \"%s\" > \"%s\"",
813 str::gsub( rawpath.asString(),"\"","\\\"" ).c_str(),
816 MIL << "Executing: " << cmd.str() << endl;
817 ExternalProgram prog( cmd.str(), ExternalProgram::Stderr_To_Stdout );
820 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
821 WAR << " " << output;
822 cmd << " " << output;
825 int ret = prog.close();
828 RepoException ex(str::form("Failed to cache repo (%d).", ret));
829 ex.remember( cmd.str() );
834 guard.resetDispose();
838 ZYPP_THROW(RepoUnknownTypeException("Unhandled repository type"));
842 switch ( repokind.toEnum() )
844 case RepoType::RPMMD_e :
847 CombinedProgressData subprogrcv( progress, 100);
848 parser::yum::RepoParser parser(id, store, parser::yum::RepoParserOpts(), subprogrcv);
849 parser.parse(rawpath);
853 case RepoType::YAST2_e :
856 CombinedProgressData subprogrcv( progress, 100);
857 parser::susetags::RepoParser parser(id, store, subprogrcv);
858 parser.parse(rawpath);
864 ZYPP_THROW(RepoUnknownTypeException());
867 // update timestamp and checksum
868 //store.updateRepositoryStatus(id, raw_metadata_status);
869 setCacheStatus(info, raw_metadata_status);
870 MIL << "Commit cache.." << endl;
875 ////////////////////////////////////////////////////////////////////////////
877 repo::RepoType RepoManager::probe( const Url &url ) const
879 if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName() ).isDir() )
881 // Handle non existing local directory in advance, as
882 // MediaSetAccess does not support it.
883 return repo::RepoType::NONE;
888 MediaSetAccess access(url);
889 if ( access.doesFileExist("/repodata/repomd.xml") )
890 return repo::RepoType::RPMMD;
891 if ( access.doesFileExist("/content") )
892 return repo::RepoType::YAST2;
894 // if it is a local url of type dir
895 if ( (! media::MediaManager::downloads(url)) && ( url.getScheme() == "dir" ) )
897 Pathname path = Pathname(url.getPathName());
898 if ( PathInfo(path).isDir() )
900 // allow empty dirs for now
901 return repo::RepoType::RPMPLAINDIR;
905 catch ( const media::MediaException &e )
908 RepoException enew("Error trying to read from " + url.asString());
912 catch ( const Exception &e )
915 Exception enew("Unknown error reading from " + url.asString());
920 return repo::RepoType::NONE;
923 ////////////////////////////////////////////////////////////////////////////
925 void RepoManager::cleanCache( const RepoInfo &info,
926 const ProgressData::ReceiverFnc & progressrcv )
928 ProgressData progress(100);
929 progress.sendTo(progressrcv);
932 filesystem::recursive_rmdir(solv_path_for_repoinfo(_pimpl->options, info));
937 ////////////////////////////////////////////////////////////////////////////
939 bool RepoManager::isCached( const RepoInfo &info ) const
941 return PathInfo(solv_path_for_repoinfo( _pimpl->options, info ) / "solv").isExist();
944 RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
947 Pathname cookiefile = solv_path_for_repoinfo(_pimpl->options, info) / "cookie";
949 return RepoStatus::fromCookieFile(cookiefile);
952 void RepoManager::setCacheStatus( const RepoInfo &info, const RepoStatus &status )
954 Pathname base = solv_path_for_repoinfo(_pimpl->options, info);
955 filesystem::assert_dir(base);
956 Pathname cookiefile = base / "cookie";
958 status.saveToCookieFile(cookiefile);
961 void RepoManager::loadFromCache( const RepoInfo & info,
962 const ProgressData::ReceiverFnc & progressrcv )
965 Pathname solvfile = solv_path_for_repoinfo(_pimpl->options, info) / "solv";
967 if ( ! PathInfo(solvfile).isExist() )
968 ZYPP_THROW(RepoNotCachedException(info));
972 sat::Pool::instance().addRepoSolv( solvfile, info );
974 catch ( const Exception & exp )
977 MIL << "Try to handle exception by rebuilding the solv-file" << endl;
978 cleanCache( info, progressrcv );
979 buildCache( info, BuildIfNeeded, progressrcv );
981 sat::Pool::instance().addRepoSolv( solvfile, info );
985 ////////////////////////////////////////////////////////////////////////////
988 * Generate a non existing filename in a directory, using a base
989 * name. For example if a directory contains 3 files
995 * If you try to generate a unique filename for this directory,
996 * based on "ruu" you will get "ruu", but if you use the base
997 * "foo" you will get "foo_1"
999 * \param dir Directory where the file needs to be unique
1000 * \param basefilename string to base the filename on.
1002 Pathname RepoManager::Impl::generateNonExistingName( const Pathname &dir,
1003 const std::string &basefilename ) const
1005 string final_filename = basefilename;
1007 while ( PathInfo(dir + final_filename).isExist() )
1009 final_filename = basefilename + "_" + str::numstring(counter);
1012 return dir + Pathname(final_filename);
1015 ////////////////////////////////////////////////////////////////////////////
1018 * \short Generate a related filename from a repo info
1020 * From a repo info, it will try to use the alias as a filename
1021 * escaping it if necessary. Other fallbacks can be added to
1022 * this function in case there is no way to use the alias
1024 std::string RepoManager::Impl::generateFilename( const RepoInfo &info ) const
1026 std::string filename = info.alias();
1027 // replace slashes with underscores
1028 str::replaceAll( filename, "/", "_" );
1030 filename = Pathname(filename).extend(".repo").asString();
1031 MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
1035 std::string RepoManager::Impl::generateFilename( const Service &info ) const
1037 std::string filename = info.name();
1038 // replace slashes with underscores
1039 str::replaceAll( filename, "/", "_" );
1041 filename = Pathname(filename).extend(".service").asString();
1042 MIL << "generating filename for service [" << info.name() << "] : '" << filename << "'" << endl;
1046 ////////////////////////////////////////////////////////////////////////////
1048 void RepoManager::addRepository( const RepoInfo &info,
1049 const ProgressData::ReceiverFnc & progressrcv )
1053 ProgressData progress(100);
1054 callback::SendReport<ProgressReport> report;
1055 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1056 progress.name(str::form(_("Adding repository '%s'"), info.name().c_str()));
1059 std::list<RepoInfo> repos = knownRepositories();
1060 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1064 if ( info.alias() == (*it).alias() )
1065 ZYPP_THROW(RepoAlreadyExistsException(info));
1068 RepoInfo tosave = info;
1070 // check the first url for now
1071 if ( _pimpl->options.probe )
1073 DBG << "unknown repository type, probing" << endl;
1075 RepoType probedtype;
1076 probedtype = probe(*tosave.baseUrlsBegin());
1077 if ( tosave.baseUrlsSize() > 0 )
1079 if ( probedtype == RepoType::NONE )
1080 ZYPP_THROW(RepoUnknownTypeException());
1082 tosave.setType(probedtype);
1088 // assert the directory exists
1089 filesystem::assert_dir(_pimpl->options.knownReposPath);
1091 Pathname repofile = _pimpl->generateNonExistingName(
1092 _pimpl->options.knownReposPath, _pimpl->generateFilename(tosave));
1093 // now we have a filename that does not exists
1094 MIL << "Saving repo in " << repofile << endl;
1096 std::ofstream file(repofile.c_str());
1098 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1101 tosave.dumpRepoOn(file);
1103 MIL << "done" << endl;
1106 void RepoManager::addRepositories( const Url &url,
1107 const ProgressData::ReceiverFnc & progressrcv )
1109 std::list<RepoInfo> knownrepos = knownRepositories();
1110 std::list<RepoInfo> repos = readRepoFile(url);
1111 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1115 // look if the alias is in the known repos.
1116 for ( std::list<RepoInfo>::const_iterator kit = knownrepos.begin();
1117 kit != knownrepos.end();
1120 if ( (*it).alias() == (*kit).alias() )
1122 ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1123 ZYPP_THROW(RepoAlreadyExistsException(*it));
1128 string filename = Pathname(url.getPathName()).basename();
1130 if ( filename == Pathname() )
1131 ZYPP_THROW(RepoException("Invalid repo file name at " + url.asString() ));
1133 // assert the directory exists
1134 filesystem::assert_dir(_pimpl->options.knownReposPath);
1136 Pathname repofile = _pimpl->generateNonExistingName(_pimpl->options.knownReposPath, filename);
1137 // now we have a filename that does not exists
1138 MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1140 std::ofstream file(repofile.c_str());
1142 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1145 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1149 MIL << "Saving " << (*it).alias() << endl;
1150 (*it).dumpRepoOn(file);
1152 MIL << "done" << endl;
1155 ////////////////////////////////////////////////////////////////////////////
1157 void RepoManager::removeRepository( const RepoInfo & info,
1158 const ProgressData::ReceiverFnc & progressrcv)
1160 ProgressData progress;
1161 callback::SendReport<ProgressReport> report;
1162 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1163 progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
1165 MIL << "Going to delete repo " << info.alias() << endl;
1167 std::list<RepoInfo> repos = knownRepositories();
1168 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1172 // they can be the same only if the provided is empty, that means
1173 // the provided repo has no alias
1175 if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1178 // TODO match by url
1180 // we have a matcing repository, now we need to know
1181 // where it does come from.
1182 RepoInfo todelete = *it;
1183 if (todelete.filepath().empty())
1185 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1189 // figure how many repos are there in the file:
1190 std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1191 if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
1193 // easy, only this one, just delete the file
1194 if ( filesystem::unlink(todelete.filepath()) != 0 )
1196 ZYPP_THROW(RepoException("Can't delete " + todelete.filepath().asString()));
1198 MIL << todelete.alias() << " sucessfully deleted." << endl;
1202 // there are more repos in the same file
1203 // write them back except the deleted one.
1205 //std::ofstream file(tmp.path().c_str());
1207 // assert the directory exists
1208 filesystem::assert_dir(todelete.filepath().dirname());
1210 std::ofstream file(todelete.filepath().c_str());
1212 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1213 ZYPP_THROW (Exception( "Can't open " + todelete.filepath().asString() ) );
1215 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1216 fit != filerepos.end();
1219 if ( (*fit).alias() != todelete.alias() )
1220 (*fit).dumpRepoOn(file);
1224 CombinedProgressData subprogrcv(progress, 70);
1225 CombinedProgressData cleansubprogrcv(progress, 30);
1226 // now delete it from cache
1227 if ( isCached(todelete) )
1228 cleanCache( todelete, subprogrcv);
1229 // now delete metadata (#301037)
1230 cleanMetadata( todelete, cleansubprogrcv);
1231 MIL << todelete.alias() << " sucessfully deleted." << endl;
1233 } // else filepath is empty
1236 // should not be reached on a sucess workflow
1237 ZYPP_THROW(RepoNotFoundException(info));
1240 ////////////////////////////////////////////////////////////////////////////
1242 void RepoManager::modifyRepository( const std::string &alias,
1243 const RepoInfo & newinfo,
1244 const ProgressData::ReceiverFnc & progressrcv )
1246 RepoInfo toedit = getRepositoryInfo(alias);
1248 // check if the new alias already exists when renaming the repo
1249 if (alias != newinfo.alias())
1251 std::list<RepoInfo> repos = knownRepositories();
1252 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
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);
1297 ////////////////////////////////////////////////////////////////////////////
1299 RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
1300 const ProgressData::ReceiverFnc & progressrcv )
1302 std::list<RepoInfo> repos = knownRepositories();
1303 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1307 if ( (*it).alias() == alias )
1311 info.setAlias(info.alias());
1312 ZYPP_THROW(RepoNotFoundException(info));
1315 ////////////////////////////////////////////////////////////////////////////
1317 RepoInfo RepoManager::getRepositoryInfo( const Url & url,
1318 const url::ViewOption & urlview,
1319 const ProgressData::ReceiverFnc & progressrcv )
1321 std::list<RepoInfo> repos = knownRepositories();
1322 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1326 for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1327 urlit != (*it).baseUrlsEnd();
1330 if ((*urlit).asString(urlview) == url.asString(urlview))
1335 info.setAlias(info.alias());
1336 info.setBaseUrl(url);
1337 ZYPP_THROW(RepoNotFoundException(info));
1340 void RepoManager::addService( const std::string& name, const Url& url )
1342 Service service(name, url);
1344 //check if service isn't already exist
1345 if( _pimpl->services.find(service)!= _pimpl->services.end() )
1346 return; //TODO throw exception
1348 _pimpl->services.insert( service );
1350 _pimpl->saveService( service );
1353 void RepoManager::removeService( const string& name)
1355 MIL << "Going to delete repo " << name << endl;
1357 const Service& service = getService( name );
1359 Pathname location = service.location();
1360 if( location.empty() )
1362 ZYPP_THROW(RepoException("Can't figure where the service is stored"));
1366 Impl::ServiceCollector collector(tmpSet);
1368 parser::ServiceFileReader reader( location,
1369 bind(&Impl::ServiceCollector::collect,collector,_1) );
1371 if ( tmpSet.size() == 1 ) //only one record
1373 if ( filesystem::unlink(location) != 0 )
1375 ZYPP_THROW(RepoException("Can't delete " + location.asString()));
1377 MIL << name << " sucessfully deleted." << endl;
1381 filesystem::assert_dir(location.dirname());
1383 std::ofstream file(location.c_str());
1385 for_(it, tmpSet.begin(), tmpSet.end())
1387 if( it->name() != name )
1388 it->dumpServiceOn(file);
1392 //now remove all repositories added by this service
1393 for_( it, service.begin(), service.end() )
1394 removeRepository(getRepositoryInfo(*it));
1398 void RepoManager::Impl::saveService( const Service& service )
1400 filesystem::assert_dir( options.knownServicesPath );
1402 Pathname servfile = generateNonExistingName( options.knownServicesPath,
1403 generateFilename( service ) );
1405 MIL << "saving service in " << servfile << endl;
1407 std::ofstream file(servfile.c_str());
1409 ZYPP_THROW (Exception( "Can't open " + servfile.asString() ) );
1412 service.dumpServiceOn( file );
1414 service.setLocation( servfile );
1415 MIL << "done" << endl;
1418 const Service& RepoManager::getService( const std::string& name ) const
1420 //little trick for Set, because it is sorted by Service name
1421 Service tmpServ(name);
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)
1457 void RepoManager::modifyService(const Service& service) const
1459 MIL << "Going to modify service " << service.name() << endl;
1461 Pathname location = service.location();
1462 if( location.empty() )
1464 ZYPP_THROW(RepoException("Can't figure where the service is stored"));
1468 Impl::ServiceCollector collector(tmpSet);
1470 parser::ServiceFileReader reader( location,
1471 bind(&Impl::ServiceCollector::collect,collector,_1) );
1473 filesystem::assert_dir(location.dirname());
1475 std::ofstream file(location.c_str());
1477 for_(it, tmpSet.begin(), tmpSet.end())
1479 if( *it != service )
1480 it->dumpServiceOn(file);
1483 service.dumpServiceOn(file);
1488 void RepoManager::Impl::knownServices()
1490 ServiceCollector collector(services);
1491 Pathname dir = options.knownServicesPath;
1492 list<Pathname> entries;
1494 if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
1495 ZYPP_THROW(Exception("failed to read directory"));
1497 str::regex allowedServiceExt("^\\.service(_[0-9]+)?$");
1498 for_(it, entries.begin(), entries.end() )
1500 parser::ServiceFileReader reader(*it,
1501 bind(&ServiceCollector::collect, collector, _1) );
1505 ////////////////////////////////////////////////////////////////////////////
1507 std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
1509 return str << *obj._pimpl;
1512 /////////////////////////////////////////////////////////////////
1514 ///////////////////////////////////////////////////////////////////