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"
28 #include "zypp/repo/RepoException.h"
29 #include "zypp/RepoManager.h"
31 #include "zypp/media/MediaManager.h"
32 #include "zypp/MediaSetAccess.h"
33 #include "zypp/ExternalProgram.h"
34 #include "zypp/ManagedFile.h"
36 #include "zypp/parser/RepoFileReader.h"
37 #include "zypp/repo/yum/Downloader.h"
38 #include "zypp/parser/yum/RepoParser.h"
39 #include "zypp/repo/susetags/Downloader.h"
40 #include "zypp/parser/susetags/RepoParser.h"
42 #include "zypp/ZYppCallbacks.h"
45 #include "satsolver/pool.h"
46 #include "satsolver/repo.h"
47 #include "satsolver/repo_solv.h"
51 using namespace zypp::repo;
52 using namespace zypp::filesystem;
54 using namespace zypp::repo;
56 ///////////////////////////////////////////////////////////////////
58 { /////////////////////////////////////////////////////////////////
60 ///////////////////////////////////////////////////////////////////
62 // CLASS NAME : RepoManagerOptions
64 ///////////////////////////////////////////////////////////////////
66 RepoManagerOptions::RepoManagerOptions()
68 repoCachePath = ZConfig::instance().repoCachePath();
69 repoRawCachePath = ZConfig::instance().repoMetadataPath();
70 repoPackagesCachePath = ZConfig::instance().repoPackagesPath();
71 knownReposPath = ZConfig::instance().knownReposPath();
72 probe = ZConfig::instance().repo_add_probe();
75 ////////////////////////////////////////////////////////////////////////////
78 * \short Simple callback to collect the results
80 * Classes like RepoFileParser call the callback
81 * once per each repo in a file.
83 * Passing this functor as callback, you can collect
84 * all resuls at the end, without dealing with async
95 bool collect( const RepoInfo &repo )
97 repos.push_back(repo);
104 ////////////////////////////////////////////////////////////////////////////
107 * Reads RepoInfo's from a repo file.
109 * \param file pathname of the file to read.
111 static std::list<RepoInfo> repositories_in_file( const Pathname & file )
113 MIL << "repo file: " << file << endl;
114 RepoCollector collector;
115 parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
116 return collector.repos;
119 ////////////////////////////////////////////////////////////////////////////
121 std::list<RepoInfo> readRepoFile(const Url & repo_file)
123 // no interface to download a specific file, using workaround:
124 //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
126 Pathname path(url.getPathName());
127 url.setPathName ("/");
128 MediaSetAccess access(url);
129 Pathname local = access.provideFile(path);
131 DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
133 return repositories_in_file(local);
136 ////////////////////////////////////////////////////////////////////////////
139 * \short List of RepoInfo's from a directory
141 * Goes trough every file ending with ".repo" in a directory and adds all
142 * RepoInfo's contained in that file.
144 * \param dir pathname of the directory to read.
146 static std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
148 MIL << "directory " << dir << endl;
149 list<RepoInfo> repos;
150 list<Pathname> entries;
151 if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
152 ZYPP_THROW(Exception("failed to read directory"));
154 str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
155 for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
157 if (str::regex_match(it->extension(), allowedRepoExt))
159 list<RepoInfo> tmp = repositories_in_file( *it );
160 repos.insert( repos.end(), tmp.begin(), tmp.end() );
162 //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
163 //MIL << "ok" << endl;
169 ////////////////////////////////////////////////////////////////////////////
171 static void assert_alias( const RepoInfo &info )
173 if (info.alias().empty())
174 ZYPP_THROW(RepoNoAliasException());
177 ////////////////////////////////////////////////////////////////////////////
179 static void assert_urls( const RepoInfo &info )
181 if (info.baseUrlsEmpty())
182 ZYPP_THROW(RepoNoUrlException());
185 ////////////////////////////////////////////////////////////////////////////
188 * \short Calculates the raw cache path for a repository
190 static Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
193 return opt.repoRawCachePath / info.escaped_alias();
197 * \short Calculates the packages cache path for a repository
199 static Pathname packagescache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
202 return opt.repoPackagesCachePath / info.escaped_alias();
205 ///////////////////////////////////////////////////////////////////
207 // CLASS NAME : RepoManager::Impl
209 ///////////////////////////////////////////////////////////////////
212 * \short RepoManager implementation.
214 struct RepoManager::Impl
216 Impl( const RepoManagerOptions &opt )
227 RepoManagerOptions options;
229 map<string, RepoInfo> _repoinfos;
232 /** Offer default Impl. */
233 static shared_ptr<Impl> nullimpl()
235 static shared_ptr<Impl> _nullimpl( new Impl );
240 friend Impl * rwcowClone<Impl>( const Impl * rhs );
241 /** clone for RWCOW_pointer */
243 { return new Impl( *this ); }
246 ///////////////////////////////////////////////////////////////////
248 /** \relates RepoManager::Impl Stream output */
249 inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
251 return str << "RepoManager::Impl";
254 ///////////////////////////////////////////////////////////////////
256 // CLASS NAME : RepoManager
258 ///////////////////////////////////////////////////////////////////
260 RepoManager::RepoManager( const RepoManagerOptions &opt )
261 : _pimpl( new Impl(opt) )
264 ////////////////////////////////////////////////////////////////////////////
266 RepoManager::~RepoManager()
269 ////////////////////////////////////////////////////////////////////////////
271 std::list<RepoInfo> RepoManager::knownRepositories() const
275 if ( PathInfo(_pimpl->options.knownReposPath).isExist() )
277 RepoInfoList repos = repositories_in_dir(_pimpl->options.knownReposPath);
278 for ( RepoInfoList::iterator it = repos.begin();
282 // set the metadata path for the repo
283 Pathname metadata_path = rawcache_path_for_repoinfo(_pimpl->options, (*it));
284 (*it).setMetadataPath(metadata_path);
286 // set the downloaded packages path for the repo
287 Pathname packages_path = packagescache_path_for_repoinfo(_pimpl->options, (*it));
288 (*it).setPackagesPath(packages_path);
293 return std::list<RepoInfo>();
298 ////////////////////////////////////////////////////////////////////////////
300 Pathname RepoManager::metadataPath( const RepoInfo &info ) const
302 return rawcache_path_for_repoinfo(_pimpl->options, info );
305 ////////////////////////////////////////////////////////////////////////////
307 RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
309 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
310 RepoType repokind = info.type();
313 switch ( repokind.toEnum() )
315 case RepoType::NONE_e:
316 // unknown, probe the local metadata
317 repokind = probe(rawpath.asUrl());
323 switch ( repokind.toEnum() )
325 case RepoType::RPMMD_e :
327 status = RepoStatus( rawpath + "/repodata/repomd.xml");
331 case RepoType::YAST2_e :
333 status = RepoStatus( rawpath + "/content") && (RepoStatus( rawpath + "/media.1/media"));
337 case RepoType::RPMPLAINDIR_e :
339 if ( PathInfo(Pathname(rawpath + "/cookie")).isExist() )
340 status = RepoStatus( rawpath + "/cookie");
344 case RepoType::NONE_e :
345 // Return default RepoStatus in case of RepoType::NONE
346 // indicating it should be created?
347 // ZYPP_THROW(RepoUnknownTypeException());
353 void RepoManager::touchIndexFile(const RepoInfo & info)
355 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
357 RepoType repokind = info.type();
358 if ( repokind.toEnum() == RepoType::NONE_e )
359 // unknown, probe the local metadata
360 repokind = probe(rawpath.asUrl());
361 // if still unknown, just return
362 if (repokind == RepoType::NONE_e)
366 switch ( repokind.toEnum() )
368 case RepoType::RPMMD_e :
369 p = Pathname(rawpath + "/repodata/repomd.xml");
372 case RepoType::YAST2_e :
373 p = Pathname(rawpath + "/content");
376 case RepoType::RPMPLAINDIR_e :
377 p = Pathname(rawpath + "/cookie");
380 case RepoType::NONE_e :
385 // touch the file, ignore error (they are logged anyway)
386 filesystem::touch(p);
389 RepoManager::RefreshCheckStatus RepoManager::checkIfToRefreshMetadata(
390 const RepoInfo &info,
392 RawMetadataRefreshPolicy policy )
396 RepoStatus oldstatus;
397 RepoStatus newstatus;
401 MIL << "Going to try to check whether refresh is needed for " << url << endl;
403 repo::RepoType repokind = info.type();
405 // if the type is unknown, try probing.
406 switch ( repokind.toEnum() )
408 case RepoType::NONE_e:
410 repokind = probe(url);
416 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
417 filesystem::assert_dir(rawpath);
418 oldstatus = metadataStatus(info);
420 // now we've got the old (cached) status, we can decide repo.refresh.delay
421 if (policy != RefreshForced)
423 // difference in seconds
424 double diff = difftime(
425 (Date::ValueType)Date::now(),
426 (Date::ValueType)oldstatus.timestamp()) / 60;
428 DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
429 DBG << "current time: " << (Date::ValueType)Date::now() << endl;
430 DBG << "last refresh = " << diff << " minutes ago" << endl;
432 if (diff < ZConfig::instance().repo_refresh_delay())
434 MIL << "Repository '" << info.alias()
435 << "' has been refreshed less than repo.refresh.delay ("
436 << ZConfig::instance().repo_refresh_delay()
437 << ") minutes ago. Advising to skip refresh" << endl;
438 return REPO_CHECK_DELAYED;
442 // create temp dir as sibling of rawpath
443 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
445 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
446 ( repokind.toEnum() == RepoType::YAST2_e ) )
448 MediaSetAccess media(url);
449 shared_ptr<repo::Downloader> downloader_ptr;
451 if ( repokind.toEnum() == RepoType::RPMMD_e )
452 downloader_ptr.reset(new yum::Downloader(info.path()));
454 downloader_ptr.reset( new susetags::Downloader(info.path()));
456 RepoStatus newstatus = downloader_ptr->status(media);
457 bool refresh = false;
458 if ( oldstatus.checksum() == newstatus.checksum() )
460 MIL << "repo has not changed" << endl;
461 if ( policy == RefreshForced )
463 MIL << "refresh set to forced" << endl;
469 MIL << "repo has changed, going to refresh" << endl;
474 touchIndexFile(info);
476 return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
479 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
481 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
482 bool refresh = false;
483 if ( oldstatus.checksum() == newstatus.checksum() )
485 MIL << "repo has not changed" << endl;
486 if ( policy == RefreshForced )
488 MIL << "refresh set to forced" << endl;
494 MIL << "repo has changed, going to refresh" << endl;
499 touchIndexFile(info);
506 ZYPP_THROW(RepoUnknownTypeException());
509 catch ( const Exception &e )
512 ERR << "refresh check failed for " << url << endl;
516 return REFRESH_NEEDED; // default
519 void RepoManager::refreshMetadata( const RepoInfo &info,
520 RawMetadataRefreshPolicy policy,
521 const ProgressData::ReceiverFnc & progress )
526 // we will throw this later if no URL checks out fine
527 RepoException rexception(_("Valid metadata not found at specified URL(s)"));
529 // try urls one by one
530 for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
536 // check whether to refresh metadata
537 // if the check fails for this url, it throws, so another url will be checked
538 if (checkIfToRefreshMetadata(info, url, policy)!=REFRESH_NEEDED)
541 MIL << "Going to refresh metadata from " << url << endl;
543 repo::RepoType repokind = info.type();
545 // if the type is unknown, try probing.
546 switch ( repokind.toEnum() )
548 case RepoType::NONE_e:
550 repokind = probe(*it);
552 if (repokind.toEnum() != RepoType::NONE_e)
554 //save probed type only for repos in system
555 std::list<RepoInfo> repos = knownRepositories();
556 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
557 it != repos.end(); ++it )
559 if ( info.alias() == (*it).alias() )
561 RepoInfo modifiedrepo = info;
562 modifiedrepo.setType(repokind);
563 modifyRepository(info.alias(),modifiedrepo);
572 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
573 filesystem::assert_dir(rawpath);
575 // create temp dir as sibling of rawpath
576 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
578 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
579 ( repokind.toEnum() == RepoType::YAST2_e ) )
581 MediaSetAccess media(url);
582 shared_ptr<repo::Downloader> downloader_ptr;
584 if ( repokind.toEnum() == RepoType::RPMMD_e )
585 downloader_ptr.reset(new yum::Downloader(info.path()));
587 downloader_ptr.reset( new susetags::Downloader(info.path()));
590 * Given a downloader, sets the other repos raw metadata
591 * path as cache paths for the fetcher, so if another
592 * repo has the same file, it will not download it
593 * but copy it from the other repository
595 std::list<RepoInfo> repos = knownRepositories();
596 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
600 Pathname cachepath(rawcache_path_for_repoinfo( _pimpl->options, *it ));
601 if ( PathInfo(cachepath).isExist() )
602 downloader_ptr->addCachePath(cachepath);
605 downloader_ptr->download( media, tmpdir.path());
608 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
610 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
612 std::ofstream file(( tmpdir.path() + "/cookie").c_str());
614 ZYPP_THROW (Exception( "Can't open " + tmpdir.path().asString() + "/cookie" ) );
617 file << newstatus.checksum() << endl;
624 ZYPP_THROW(RepoUnknownTypeException());
627 // ok we have the metadata, now exchange
630 TmpDir oldmetadata( TmpDir::makeSibling( rawpath ) );
631 filesystem::rename( rawpath, oldmetadata.path() );
632 // move the just downloaded there
633 filesystem::rename( tmpdir.path(), rawpath );
638 catch ( const Exception &e )
641 ERR << "Trying another url..." << endl;
643 // remember the exception caught for the *first URL*
644 // if all other URLs fail, the rexception will be thrown with the
645 // cause of the problem of the first URL remembered
646 if (it == info.baseUrlsBegin())
647 rexception.remember(e);
650 ERR << "No more urls..." << endl;
651 ZYPP_THROW(rexception);
654 ////////////////////////////////////////////////////////////////////////////
656 void RepoManager::cleanMetadata( const RepoInfo &info,
657 const ProgressData::ReceiverFnc & progressfnc )
659 ProgressData progress(100);
660 progress.sendTo(progressfnc);
662 filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_pimpl->options, info));
666 void RepoManager::cleanPackages( const RepoInfo &info,
667 const ProgressData::ReceiverFnc & progressfnc )
669 ProgressData progress(100);
670 progress.sendTo(progressfnc);
672 filesystem::recursive_rmdir(packagescache_path_for_repoinfo(_pimpl->options, info));
676 void RepoManager::buildCache( const RepoInfo &info,
677 CacheBuildPolicy policy,
678 const ProgressData::ReceiverFnc & progressrcv )
681 Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
683 Pathname base = _pimpl->options.repoCachePath + info.escaped_alias();
684 Pathname solvfile = base.extend(".solv");
687 RepoStatus raw_metadata_status = metadataStatus(info);
688 if ( raw_metadata_status.empty() )
690 /* if there is no cache at this point, we refresh the raw
691 in case this is the first time - if it's !autorefresh,
692 we may still refresh */
693 refreshMetadata(info, RefreshIfNeeded, progressrcv );
694 raw_metadata_status = metadataStatus(info);
697 bool needs_cleaning = false;
698 if ( isCached( info ) )
700 MIL << info.alias() << " is already cached." << endl;
701 //data::RecordId id = store.lookupRepository(info.alias());
702 RepoStatus cache_status = cacheStatus(info);
704 if ( cache_status.checksum() == raw_metadata_status.checksum() )
706 MIL << info.alias() << " cache is up to date with metadata." << endl;
707 if ( policy == BuildIfNeeded ) {
711 MIL << info.alias() << " cache rebuild is forced" << endl;
715 needs_cleaning = true;
718 ProgressData progress(100);
719 callback::SendReport<ProgressReport> report;
720 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
721 progress.name(str::form(_("Building repository '%s' cache"), info.name().c_str()));
729 MIL << info.alias() << " building cache..." << endl;
731 repo::RepoType repokind = info.type();
733 // if the type is unknown, try probing.
734 switch ( repokind.toEnum() )
736 case RepoType::NONE_e:
737 // unknown, probe the local metadata
738 repokind = probe(rawpath.asUrl());
744 MIL << "repo type is " << repokind << endl;
746 switch ( repokind.toEnum() )
748 case RepoType::RPMMD_e :
749 case RepoType::YAST2_e :
751 // Take care we unlink the solvfile on exception
752 ManagedFile guard( solvfile, filesystem::unlink );
755 cmd << str::form( "repo2solv.sh \"%s\" > \"%s\"", rawpath.c_str(), solvfile.c_str() );
757 MIL << "Executing: " << cmd.str() << endl;
758 ExternalProgram prog( cmd.str(), ExternalProgram::Stderr_To_Stdout );
761 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
762 WAR << " " << output;
763 cmd << " " << output;
766 int ret = prog.close();
769 RepoException ex(str::form("Failed to cache repo (%d).", ret));
770 ex.remember( cmd.str() );
775 guard.resetDispose();
779 ZYPP_THROW(RepoUnknownTypeException("Unhandled repository type"));
783 switch ( repokind.toEnum() )
785 case RepoType::RPMMD_e :
788 CombinedProgressData subprogrcv( progress, 100);
789 parser::yum::RepoParser parser(id, store, parser::yum::RepoParserOpts(), subprogrcv);
790 parser.parse(rawpath);
794 case RepoType::YAST2_e :
797 CombinedProgressData subprogrcv( progress, 100);
798 parser::susetags::RepoParser parser(id, store, subprogrcv);
799 parser.parse(rawpath);
805 case RepoType::RPMPLAINDIR_e :
807 CombinedProgressData subprogrcv( progress, 100);
808 InputStream is(rawpath + "cookie");
810 getline( is.stream(), buffer);
812 parser::plaindir::RepoParser parser(id, store, subprogrcv);
813 parser.parse(url.getPathName());
818 ZYPP_THROW(RepoUnknownTypeException());
821 // update timestamp and checksum
822 //store.updateRepositoryStatus(id, raw_metadata_status);
823 setCacheStatus(info.escaped_alias(), raw_metadata_status);
824 MIL << "Commit cache.." << endl;
829 ////////////////////////////////////////////////////////////////////////////
831 repo::RepoType RepoManager::probe( const Url &url ) const
833 if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName() ).isDir() )
835 // Handle non existing local directory in advance, as
836 // MediaSetAccess does not support it.
837 return repo::RepoType::NONE;
842 MediaSetAccess access(url);
843 if ( access.doesFileExist("/repodata/repomd.xml") )
844 return repo::RepoType::RPMMD;
845 if ( access.doesFileExist("/content") )
846 return repo::RepoType::YAST2;
848 // if it is a local url of type dir
849 if ( (! media::MediaManager::downloads(url)) && ( url.getScheme() == "dir" ) )
851 Pathname path = Pathname(url.getPathName());
852 if ( PathInfo(path).isDir() )
854 // allow empty dirs for now
855 return repo::RepoType::RPMPLAINDIR;
859 catch ( const media::MediaException &e )
862 RepoException enew("Error trying to read from " + url.asString());
866 catch ( const Exception &e )
869 Exception enew("Unknown error reading from " + url.asString());
874 return repo::RepoType::NONE;
877 ////////////////////////////////////////////////////////////////////////////
879 void RepoManager::cleanCache( const RepoInfo &info,
880 const ProgressData::ReceiverFnc & progressrcv )
882 ProgressData progress(100);
883 progress.sendTo(progressrcv);
886 unlink (_pimpl->options.repoCachePath / (info.escaped_alias() + ".solv"));
888 unlink (_pimpl->options.repoCachePath / (info.escaped_alias() + ".cookie"));
893 void RepoManager::cleanTargetCache(const ProgressData::ReceiverFnc & progressrcv)
895 unlink (_pimpl->options.repoCachePath / (sat::Pool::systemRepoName() + ".solv"));
896 unlink (_pimpl->options.repoCachePath / (sat::Pool::systemRepoName() + ".cookie"));
899 ////////////////////////////////////////////////////////////////////////////
901 bool RepoManager::isCached( const RepoInfo &info ) const
903 Pathname name = _pimpl->options.repoCachePath;
904 return PathInfo(name + Pathname(info.escaped_alias()).extend(".solv")).isExist();
907 RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
910 Pathname base = _pimpl->options.repoCachePath + info.escaped_alias();
911 Pathname cookiefile = base.extend(".cookie");
913 return RepoStatus::fromCookieFile(cookiefile);
916 void RepoManager::setCacheStatus( const string &alias, const RepoStatus &status )
918 Pathname base = _pimpl->options.repoCachePath + alias;
919 Pathname cookiefile = base.extend(".cookie");
921 status.saveToCookieFile(cookiefile);
924 void RepoManager::loadFromCache( const RepoInfo & info,
925 const ProgressData::ReceiverFnc & progressrcv )
928 string alias = info.escaped_alias();
929 Pathname solvfile = (_pimpl->options.repoCachePath / alias).extend(".solv");
931 if ( ! PathInfo(solvfile).isExist() )
932 ZYPP_THROW(RepoNotCachedException());
936 sat::Pool::instance().addRepoSolv( solvfile, info );
938 catch ( const Exception & exp )
941 MIL << "Try to handle exception by rebuilding the solv-file" << endl;
942 cleanCache( info, progressrcv );
943 buildCache( info, BuildIfNeeded, progressrcv );
945 sat::Pool::instance().addRepoSolv( solvfile, info );
949 ////////////////////////////////////////////////////////////////////////////
952 * Generate a non existing filename in a directory, using a base
953 * name. For example if a directory contains 3 files
959 * If you try to generate a unique filename for this directory,
960 * based on "ruu" you will get "ruu", but if you use the base
961 * "foo" you will get "foo_1"
963 * \param dir Directory where the file needs to be unique
964 * \param basefilename string to base the filename on.
966 static Pathname generate_non_existing_name( const Pathname &dir,
967 const std::string &basefilename )
969 string final_filename = basefilename;
971 while ( PathInfo(dir + final_filename).isExist() )
973 final_filename = basefilename + "_" + str::numstring(counter);
976 return dir + Pathname(final_filename);
979 ////////////////////////////////////////////////////////////////////////////
982 * \short Generate a related filename from a repo info
984 * From a repo info, it will try to use the alias as a filename
985 * escaping it if necessary. Other fallbacks can be added to
986 * this function in case there is no way to use the alias
988 static std::string generate_filename( const RepoInfo &info )
992 std::string filename = info.alias();
993 // replace slashes with underscores
994 size_t pos = filename.find(fnd);
995 while(pos!=string::npos)
997 filename.replace(pos,fnd.length(),rep);
998 pos = filename.find(fnd,pos+rep.length());
1000 filename = Pathname(filename).extend(".repo").asString();
1001 MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
1006 ////////////////////////////////////////////////////////////////////////////
1008 void RepoManager::addRepository( const RepoInfo &info,
1009 const ProgressData::ReceiverFnc & progressrcv )
1013 ProgressData progress(100);
1014 callback::SendReport<ProgressReport> report;
1015 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1016 progress.name(str::form(_("Adding repository '%s'"), info.name().c_str()));
1019 std::list<RepoInfo> repos = knownRepositories();
1020 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1024 if ( info.alias() == (*it).alias() )
1025 ZYPP_THROW(RepoAlreadyExistsException(info.alias()));
1028 RepoInfo tosave = info;
1030 // check the first url for now
1031 if ( _pimpl->options.probe )
1033 DBG << "unknown repository type, probing" << endl;
1035 RepoType probedtype;
1036 probedtype = probe(*tosave.baseUrlsBegin());
1037 if ( tosave.baseUrlsSize() > 0 )
1039 if ( probedtype == RepoType::NONE )
1040 ZYPP_THROW(RepoUnknownTypeException());
1042 tosave.setType(probedtype);
1048 // assert the directory exists
1049 filesystem::assert_dir(_pimpl->options.knownReposPath);
1051 Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath,
1052 generate_filename(tosave));
1053 // now we have a filename that does not exists
1054 MIL << "Saving repo in " << repofile << endl;
1056 std::ofstream file(repofile.c_str());
1058 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1061 tosave.dumpRepoOn(file);
1063 MIL << "done" << endl;
1066 void RepoManager::addRepositories( const Url &url,
1067 const ProgressData::ReceiverFnc & progressrcv )
1069 std::list<RepoInfo> knownrepos = knownRepositories();
1070 std::list<RepoInfo> repos = readRepoFile(url);
1071 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1075 // look if the alias is in the known repos.
1076 for ( std::list<RepoInfo>::const_iterator kit = knownrepos.begin();
1077 kit != knownrepos.end();
1080 if ( (*it).alias() == (*kit).alias() )
1082 ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1083 ZYPP_THROW(RepoAlreadyExistsException((*it).alias()));
1088 string filename = Pathname(url.getPathName()).basename();
1090 if ( filename == Pathname() )
1091 ZYPP_THROW(RepoException("Invalid repo file name at " + url.asString() ));
1093 // assert the directory exists
1094 filesystem::assert_dir(_pimpl->options.knownReposPath);
1096 Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath, filename);
1097 // now we have a filename that does not exists
1098 MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1100 std::ofstream file(repofile.c_str());
1102 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1105 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1109 MIL << "Saving " << (*it).alias() << endl;
1110 (*it).dumpRepoOn(file);
1112 MIL << "done" << endl;
1115 ////////////////////////////////////////////////////////////////////////////
1117 void RepoManager::removeRepository( const RepoInfo & info,
1118 const ProgressData::ReceiverFnc & progressrcv)
1120 ProgressData progress;
1121 callback::SendReport<ProgressReport> report;
1122 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1123 progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
1125 MIL << "Going to delete repo " << info.alias() << endl;
1127 std::list<RepoInfo> repos = knownRepositories();
1128 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1132 // they can be the same only if the provided is empty, that means
1133 // the provided repo has no alias
1135 if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1138 // TODO match by url
1140 // we have a matcing repository, now we need to know
1141 // where it does come from.
1142 RepoInfo todelete = *it;
1143 if (todelete.filepath().empty())
1145 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1149 // figure how many repos are there in the file:
1150 std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1151 if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
1153 // easy, only this one, just delete the file
1154 if ( filesystem::unlink(todelete.filepath()) != 0 )
1156 ZYPP_THROW(RepoException("Can't delete " + todelete.filepath().asString()));
1158 MIL << todelete.alias() << " sucessfully deleted." << endl;
1162 // there are more repos in the same file
1163 // write them back except the deleted one.
1165 //std::ofstream file(tmp.path().c_str());
1167 // assert the directory exists
1168 filesystem::assert_dir(todelete.filepath().dirname());
1170 std::ofstream file(todelete.filepath().c_str());
1172 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1173 ZYPP_THROW (Exception( "Can't open " + todelete.filepath().asString() ) );
1175 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1176 fit != filerepos.end();
1179 if ( (*fit).alias() != todelete.alias() )
1180 (*fit).dumpRepoOn(file);
1184 CombinedProgressData subprogrcv(progress, 70);
1185 CombinedProgressData cleansubprogrcv(progress, 30);
1186 // now delete it from cache
1187 if ( isCached(todelete) )
1188 cleanCache( todelete, subprogrcv);
1189 // now delete metadata (#301037)
1190 cleanMetadata( todelete, cleansubprogrcv);
1191 MIL << todelete.alias() << " sucessfully deleted." << endl;
1193 } // else filepath is empty
1196 // should not be reached on a sucess workflow
1197 ZYPP_THROW(RepoNotFoundException(info));
1200 ////////////////////////////////////////////////////////////////////////////
1202 void RepoManager::modifyRepository( const std::string &alias,
1203 const RepoInfo & newinfo,
1204 const ProgressData::ReceiverFnc & progressrcv )
1206 RepoInfo toedit = getRepositoryInfo(alias);
1208 // check if the new alias already exists when renaming the repo
1209 if (alias != newinfo.alias())
1211 std::list<RepoInfo> repos = knownRepositories();
1212 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1216 if ( newinfo.alias() == (*it).alias() )
1217 ZYPP_THROW(RepoAlreadyExistsException(newinfo.alias()));
1221 if (toedit.filepath().empty())
1223 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1227 // figure how many repos are there in the file:
1228 std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1230 // there are more repos in the same file
1231 // write them back except the deleted one.
1233 //std::ofstream file(tmp.path().c_str());
1235 // assert the directory exists
1236 filesystem::assert_dir(toedit.filepath().dirname());
1238 std::ofstream file(toedit.filepath().c_str());
1240 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1241 ZYPP_THROW (Exception( "Can't open " + toedit.filepath().asString() ) );
1243 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1244 fit != filerepos.end();
1247 // if the alias is different, dump the original
1248 // if it is the same, dump the provided one
1249 if ( (*fit).alias() != toedit.alias() )
1250 (*fit).dumpRepoOn(file);
1252 newinfo.dumpRepoOn(file);
1257 ////////////////////////////////////////////////////////////////////////////
1259 RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
1260 const ProgressData::ReceiverFnc & progressrcv )
1262 std::list<RepoInfo> repos = knownRepositories();
1263 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1267 if ( (*it).alias() == alias )
1271 info.setAlias(info.alias());
1272 ZYPP_THROW(RepoNotFoundException(info));
1275 ////////////////////////////////////////////////////////////////////////////
1277 RepoInfo RepoManager::getRepositoryInfo( const Url & url,
1278 const url::ViewOption & urlview,
1279 const ProgressData::ReceiverFnc & progressrcv )
1281 std::list<RepoInfo> repos = knownRepositories();
1282 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1286 for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1287 urlit != (*it).baseUrlsEnd();
1290 if ((*urlit).asString(urlview) == url.asString(urlview))
1295 info.setAlias(info.alias());
1296 info.setBaseUrl(url);
1297 ZYPP_THROW(RepoNotFoundException(info));
1300 ////////////////////////////////////////////////////////////////////////////
1302 std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
1304 return str << *obj._pimpl;
1307 /////////////////////////////////////////////////////////////////
1309 ///////////////////////////////////////////////////////////////////