1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoManager.cc
19 #include "zypp/base/InputStream.h"
20 #include "zypp/base/Logger.h"
21 #include "zypp/base/Gettext.h"
22 #include "zypp/base/Function.h"
23 #include "zypp/base/Regex.h"
24 #include "zypp/PathInfo.h"
25 #include "zypp/TmpPath.h"
27 #include "zypp/repo/RepoException.h"
28 #include "zypp/RepoManager.h"
30 #include "zypp/cache/SolvStore.h"
31 #include "zypp/repo/cached/RepoImpl.h"
32 #include "zypp/media/MediaManager.h"
33 #include "zypp/MediaSetAccess.h"
34 #include "zypp/ExternalProgram.h"
36 #include "zypp/parser/RepoFileReader.h"
37 #include "zypp/repo/yum/Downloader.h"
38 #include "zypp/parser/yum/RepoParser.h"
39 //#include "zypp/parser/plaindir/RepoParser.h"
40 #include "zypp/repo/susetags/Downloader.h"
41 #include "zypp/parser/susetags/RepoParser.h"
43 #include "zypp/ZYppCallbacks.h"
46 #include "satsolver/pool.h"
47 #include "satsolver/repo.h"
48 #include "satsolver/repo_solv.h"
52 using namespace zypp::repo;
53 using namespace zypp::filesystem;
55 using namespace zypp::repo;
57 ///////////////////////////////////////////////////////////////////
59 { /////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////////////////
63 // CLASS NAME : RepoManagerOptions
65 ///////////////////////////////////////////////////////////////////
67 RepoManagerOptions::RepoManagerOptions()
69 repoCachePath = ZConfig::instance().repoCachePath();
70 repoRawCachePath = ZConfig::instance().repoMetadataPath();
71 knownReposPath = ZConfig::instance().knownReposPath();
74 ////////////////////////////////////////////////////////////////////////////
77 * \short Simple callback to collect the results
79 * Classes like RepoFileParser call the callback
80 * once per each repo in a file.
82 * Passing this functor as callback, you can collect
83 * all resuls at the end, without dealing with async
98 bool collect( const RepoInfo &repo )
100 //MIL << "here in collector: " << repo.alias() << endl;
101 repos.push_back(repo);
102 //MIL << "added: " << repo.alias() << endl;
109 ////////////////////////////////////////////////////////////////////////////
112 * \short Internal version of clean cache
114 * Takes an extra SolvStore reference, so we avoid internally
115 * having 2 SolvStores writing to the same database.
117 static void cleanCacheInternal( cache::SolvStore &store,
118 const RepoInfo &info,
119 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() )
121 // ProgressData progress;
122 // callback::SendReport<ProgressReport> report;
123 // progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
124 // progress.name(str::form(_("Cleaning repository '%s' cache"), info.name().c_str()));
126 // if ( !store.isCached(info.alias()) )
129 // MIL << info.alias() << " cleaning cache..." << endl;
131 // CombinedProgressData subprogrcv(progress);
133 // store.cleanRepository(info.alias(), subprogrcv);
136 ////////////////////////////////////////////////////////////////////////////
139 * Reads RepoInfo's from a repo file.
141 * \param file pathname of the file to read.
143 static std::list<RepoInfo> repositories_in_file( const Pathname & file )
145 MIL << "repo file: " << file << endl;
146 RepoCollector collector;
147 parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
148 return collector.repos;
151 ////////////////////////////////////////////////////////////////////////////
153 std::list<RepoInfo> readRepoFile(const Url & repo_file)
155 // no interface to download a specific file, using workaround:
156 //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
158 Pathname path(url.getPathName());
159 url.setPathName ("/");
160 MediaSetAccess access(url);
161 Pathname local = access.provideFile(path);
163 DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
165 return repositories_in_file(local);
168 ////////////////////////////////////////////////////////////////////////////
171 * \short List of RepoInfo's from a directory
173 * Goes trough every file ending with ".repo" in a directory and adds all
174 * RepoInfo's contained in that file.
176 * \param dir pathname of the directory to read.
178 static std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
180 MIL << "directory " << dir << endl;
181 list<RepoInfo> repos;
182 list<Pathname> entries;
183 if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
184 ZYPP_THROW(Exception("failed to read directory"));
186 str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
187 for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
189 if (str::regex_match(it->extension(), allowedRepoExt))
191 list<RepoInfo> tmp = repositories_in_file( *it );
192 repos.insert( repos.end(), tmp.begin(), tmp.end() );
194 //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
195 //MIL << "ok" << endl;
201 ////////////////////////////////////////////////////////////////////////////
203 static void assert_alias( const RepoInfo &info )
205 if (info.alias().empty())
206 ZYPP_THROW(RepoNoAliasException());
209 ////////////////////////////////////////////////////////////////////////////
211 static void assert_urls( const RepoInfo &info )
213 if (info.baseUrlsEmpty())
214 ZYPP_THROW(RepoNoUrlException());
217 ////////////////////////////////////////////////////////////////////////////
220 * \short Calculates the raw cache path for a repository
222 static Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
225 return opt.repoRawCachePath + info.alias();
228 ///////////////////////////////////////////////////////////////////
230 // CLASS NAME : RepoManager::Impl
232 ///////////////////////////////////////////////////////////////////
235 * \short RepoManager implementation.
237 struct RepoManager::Impl
239 Impl( const RepoManagerOptions &opt )
250 RepoManagerOptions options;
253 /** Offer default Impl. */
254 static shared_ptr<Impl> nullimpl()
256 static shared_ptr<Impl> _nullimpl( new Impl );
261 friend Impl * rwcowClone<Impl>( const Impl * rhs );
262 /** clone for RWCOW_pointer */
264 { return new Impl( *this ); }
267 ///////////////////////////////////////////////////////////////////
269 /** \relates RepoManager::Impl Stream output */
270 inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
272 return str << "RepoManager::Impl";
275 ///////////////////////////////////////////////////////////////////
277 // CLASS NAME : RepoManager
279 ///////////////////////////////////////////////////////////////////
281 RepoManager::RepoManager( const RepoManagerOptions &opt )
282 : _pimpl( new Impl(opt) )
285 ////////////////////////////////////////////////////////////////////////////
287 RepoManager::~RepoManager()
290 ////////////////////////////////////////////////////////////////////////////
292 std::list<RepoInfo> RepoManager::knownRepositories() const
296 if ( PathInfo(_pimpl->options.knownReposPath).isExist() )
298 RepoInfoList repos = repositories_in_dir(_pimpl->options.knownReposPath);
299 for ( RepoInfoList::iterator it = repos.begin();
303 // set the metadata path for the repo
304 Pathname metadata_path = rawcache_path_for_repoinfo(_pimpl->options, (*it));
305 (*it).setMetadataPath(metadata_path);
310 return std::list<RepoInfo>();
315 ////////////////////////////////////////////////////////////////////////////
317 Pathname RepoManager::metadataPath( const RepoInfo &info ) const
319 return rawcache_path_for_repoinfo(_pimpl->options, info );
322 ////////////////////////////////////////////////////////////////////////////
324 RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
326 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
327 RepoType repokind = info.type();
330 switch ( repokind.toEnum() )
332 case RepoType::NONE_e:
333 // unknown, probe the local metadata
334 repokind = probe(rawpath.asUrl());
340 switch ( repokind.toEnum() )
342 case RepoType::RPMMD_e :
344 status = RepoStatus( rawpath + "/repodata/repomd.xml");
348 case RepoType::YAST2_e :
350 // the order of RepoStatus && RepoStatus matters! (#304310)
351 status = RepoStatus( rawpath + "/content") && (RepoStatus( rawpath + "/media.1/media"));
355 case RepoType::RPMPLAINDIR_e :
357 if ( PathInfo(Pathname(rawpath + "/cookie")).isExist() )
358 status = RepoStatus( rawpath + "/cookie");
362 case RepoType::NONE_e :
363 // Return default RepoStatus in case of RepoType::NONE
364 // indicating it should be created?
365 // ZYPP_THROW(RepoUnknownTypeException());
371 void RepoManager::touchIndexFile(const RepoInfo & info)
373 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
375 RepoType repokind = info.type();
376 if ( repokind.toEnum() == RepoType::NONE_e )
377 // unknown, probe the local metadata
378 repokind = probe(rawpath.asUrl());
379 // if still unknown, just return
380 if (repokind == RepoType::NONE_e)
384 switch ( repokind.toEnum() )
386 case RepoType::RPMMD_e :
387 p = Pathname(rawpath + "/repodata/repomd.xml");
390 case RepoType::YAST2_e :
391 p = Pathname(rawpath + "/content");
394 case RepoType::RPMPLAINDIR_e :
395 p = Pathname(rawpath + "/cookie");
398 case RepoType::NONE_e :
403 // touch the file, ignore error (they are logged anyway)
404 filesystem::touch(p);
407 bool RepoManager::checkIfToRefreshMetadata( const RepoInfo &info,
409 RawMetadataRefreshPolicy policy )
413 RepoStatus oldstatus;
414 RepoStatus newstatus;
418 MIL << "Going to try to check whether refresh is needed for " << url << endl;
420 repo::RepoType repokind = info.type();
422 // if the type is unknown, try probing.
423 switch ( repokind.toEnum() )
425 case RepoType::NONE_e:
427 repokind = probe(url);
433 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
434 filesystem::assert_dir(rawpath);
435 oldstatus = metadataStatus(info);
437 // now we've got the old (cached) status, we can decide repo.refresh.delay
438 if (policy != RefreshForced)
440 // difference in seconds
441 double diff = difftime(
442 (Date::ValueType)Date::now(),
443 (Date::ValueType)oldstatus.timestamp()) / 60;
445 DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
446 DBG << "current time: " << (Date::ValueType)Date::now() << endl;
447 DBG << "last refresh = " << diff << " minutes ago" << endl;
449 if (diff < ZConfig::instance().repo_refresh_delay())
451 MIL << "Repository '" << info.alias()
452 << "' has been refreshed less than repo.refresh.delay ("
453 << ZConfig::instance().repo_refresh_delay()
454 << ") minutes ago. Advising to skip refresh" << endl;
459 // create temp dir as sibling of rawpath
460 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
462 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
463 ( repokind.toEnum() == RepoType::YAST2_e ) )
465 MediaSetAccess media(url);
466 shared_ptr<repo::Downloader> downloader_ptr;
468 if ( repokind.toEnum() == RepoType::RPMMD_e )
469 downloader_ptr.reset(new yum::Downloader(info.path()));
471 downloader_ptr.reset( new susetags::Downloader(info.path()));
473 RepoStatus newstatus = downloader_ptr->status(media);
474 bool refresh = false;
475 if ( oldstatus.checksum() == newstatus.checksum() )
477 MIL << "repo has not changed" << endl;
478 if ( policy == RefreshForced )
480 MIL << "refresh set to forced" << endl;
486 MIL << "repo has changed, going to refresh" << endl;
491 touchIndexFile(info);
496 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
498 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
499 bool refresh = false;
500 if ( oldstatus.checksum() == newstatus.checksum() )
502 MIL << "repo has not changed" << endl;
503 if ( policy == RefreshForced )
505 MIL << "refresh set to forced" << endl;
511 MIL << "repo has changed, going to refresh" << endl;
516 touchIndexFile(info);
523 ZYPP_THROW(RepoUnknownTypeException());
526 catch ( const Exception &e )
529 ERR << "refresh check failed for " << url << endl;
533 return true; // default
536 void RepoManager::refreshMetadata( const RepoInfo &info,
537 RawMetadataRefreshPolicy policy,
538 const ProgressData::ReceiverFnc & progress )
543 // we will throw this later if no URL checks out fine
544 RepoException rexception(_("Valid metadata not found at specified URL(s)"));
546 // try urls one by one
547 for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
553 // check whether to refresh metadata
554 // if the check fails for this url, it throws, so another url will be checked
555 if (!checkIfToRefreshMetadata(info, url, policy))
558 MIL << "Going to refresh metadata from " << url << endl;
560 repo::RepoType repokind = info.type();
562 // if the type is unknown, try probing.
563 switch ( repokind.toEnum() )
565 case RepoType::NONE_e:
567 repokind = probe(*it);
573 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
574 filesystem::assert_dir(rawpath);
576 // create temp dir as sibling of rawpath
577 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
579 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
580 ( repokind.toEnum() == RepoType::YAST2_e ) )
582 MediaSetAccess media(url);
583 shared_ptr<repo::Downloader> downloader_ptr;
585 if ( repokind.toEnum() == RepoType::RPMMD_e )
586 downloader_ptr.reset(new yum::Downloader(info.path()));
588 downloader_ptr.reset( new susetags::Downloader(info.path()));
591 * Given a downloader, sets the other repos raw metadata
592 * path as cache paths for the fetcher, so if another
593 * repo has the same file, it will not download it
594 * but copy it from the other repository
596 std::list<RepoInfo> repos = knownRepositories();
597 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
601 downloader_ptr->addCachePath(rawcache_path_for_repoinfo( _pimpl->options, *it ));
604 downloader_ptr->download( media, tmpdir.path());
607 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
609 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
611 std::ofstream file(( tmpdir.path() + "/cookie").c_str());
613 ZYPP_THROW (Exception( "Can't open " + tmpdir.path().asString() + "/cookie" ) );
616 file << newstatus.checksum() << endl;
623 ZYPP_THROW(RepoUnknownTypeException());
626 // ok we have the metadata, now exchange
628 TmpDir oldmetadata( TmpDir::makeSibling( rawpath ) );
629 filesystem::rename( rawpath, oldmetadata.path() );
630 // move the just downloaded there
631 filesystem::rename( tmpdir.path(), rawpath );
635 catch ( const Exception &e )
638 ERR << "Trying another url..." << endl;
640 // remember the exception caught for the *first URL*
641 // if all other URLs fail, the rexception will be thrown with the
642 // cause of the problem of the first URL remembered
643 if (it == info.baseUrlsBegin())
644 rexception.remember(e);
647 ERR << "No more urls..." << endl;
648 ZYPP_THROW(rexception);
651 ////////////////////////////////////////////////////////////////////////////
653 void RepoManager::cleanMetadata( const RepoInfo &info,
654 const ProgressData::ReceiverFnc & progressfnc )
656 ProgressData progress(100);
657 progress.sendTo(progressfnc);
659 filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_pimpl->options, info));
663 void RepoManager::buildCache( const RepoInfo &info,
664 CacheBuildPolicy policy,
665 const ProgressData::ReceiverFnc & progressrcv )
668 Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
670 Pathname base = _pimpl->options.repoCachePath + info.alias();
671 Pathname solvfile = base.extend(".solv");
673 //cache::SolvStore store(_pimpl->options.repoCachePath);
675 RepoStatus raw_metadata_status = metadataStatus(info);
676 if ( raw_metadata_status.empty() )
678 ZYPP_THROW(RepoMetadataException(info));
681 bool needs_cleaning = false;
682 if ( isCached( info ) )
684 MIL << info.alias() << " is already cached." << endl;
685 //data::RecordId id = store.lookupRepository(info.alias());
686 RepoStatus cache_status = cacheStatus(info);
688 if ( cache_status.checksum() == raw_metadata_status.checksum() )
690 MIL << info.alias() << " cache is up to date with metadata." << endl;
691 if ( policy == BuildIfNeeded ) {
695 MIL << info.alias() << " cache rebuild is forced" << endl;
699 needs_cleaning = true;
702 ProgressData progress(100);
703 callback::SendReport<ProgressReport> report;
704 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
705 progress.name(str::form(_("Building repository '%s' cache"), info.name().c_str()));
710 // Pathname name = _pimpl->options.repoCachePath;
711 // //data::RecordId id = store.lookupRepository(info.alias());
714 // name += os.str() + ".solv";
716 // cleanCacheInternal( store, info);
720 MIL << info.alias() << " building cache..." << endl;
721 //data::RecordId id = store.lookupOrAppendRepository(info.alias());
723 repo::RepoType repokind = info.type();
725 // if the type is unknown, try probing.
726 switch ( repokind.toEnum() )
728 case RepoType::NONE_e:
729 // unknown, probe the local metadata
730 repokind = probe(rawpath.asUrl());
736 MIL << "repo type is " << repokind << endl;
738 switch ( repokind.toEnum() )
740 case RepoType::RPMMD_e :
741 case RepoType::YAST2_e :
743 // string cmd = "repo2solv.sh \"";
744 // cmd += rawpath.asString() + "\" > " + solvfile.asString();
745 // int ret = system (cmd.c_str());
746 // if (WIFEXITED (ret) && WEXITSTATUS (ret) != 0)
747 // ZYPP_THROW(RepoUnknownTypeException());
748 MIL << "Executing solv converter" << endl;
749 string cmd( str::form( "/usr/bin/repo2solv.sh \"%s\" > %s", rawpath.asString().c_str(), solvfile.asString().c_str() ) );
750 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
751 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
752 MIL << " " << output;
754 int ret = prog.close();
758 ZYPP_THROW(Exception("Unhandled repostory type"));
762 switch ( repokind.toEnum() )
764 case RepoType::RPMMD_e :
767 CombinedProgressData subprogrcv( progress, 100);
768 parser::yum::RepoParser parser(id, store, parser::yum::RepoParserOpts(), subprogrcv);
769 parser.parse(rawpath);
773 case RepoType::YAST2_e :
776 CombinedProgressData subprogrcv( progress, 100);
777 parser::susetags::RepoParser parser(id, store, subprogrcv);
778 parser.parse(rawpath);
784 case RepoType::RPMPLAINDIR_e :
786 CombinedProgressData subprogrcv( progress, 100);
787 InputStream is(rawpath + "cookie");
789 getline( is.stream(), buffer);
791 parser::plaindir::RepoParser parser(id, store, subprogrcv);
792 parser.parse(url.getPathName());
797 ZYPP_THROW(RepoUnknownTypeException());
800 // update timestamp and checksum
801 //store.updateRepositoryStatus(id, raw_metadata_status);
802 setCacheStatus(info.alias(), raw_metadata_status);
803 MIL << "Commit cache.." << endl;
808 ////////////////////////////////////////////////////////////////////////////
810 repo::RepoType RepoManager::probe( const Url &url ) const
812 if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName() ).isDir() )
814 // Handle non existing local directory in advance, as
815 // MediaSetAccess does not support it.
816 return repo::RepoType::NONE;
821 MediaSetAccess access(url);
822 if ( access.doesFileExist("/repodata/repomd.xml") )
823 return repo::RepoType::RPMMD;
824 if ( access.doesFileExist("/content") )
825 return repo::RepoType::YAST2;
827 // if it is a local url of type dir
828 if ( (! media::MediaManager::downloads(url)) && ( url.getScheme() == "dir" ) )
830 Pathname path = Pathname(url.getPathName());
831 if ( PathInfo(path).isDir() )
833 // allow empty dirs for now
834 return repo::RepoType::RPMPLAINDIR;
838 catch ( const media::MediaException &e )
841 RepoException enew("Error trying to read from " + url.asString());
845 catch ( const Exception &e )
848 Exception enew("Unknown error reading from " + url.asString());
853 return repo::RepoType::NONE;
856 ////////////////////////////////////////////////////////////////////////////
858 void RepoManager::cleanCache( const RepoInfo &info,
859 const ProgressData::ReceiverFnc & progressrcv )
861 Pathname name = _pimpl->options.repoCachePath;
862 name += info.alias() + ".solv";
866 ////////////////////////////////////////////////////////////////////////////
868 bool RepoManager::isCached( const RepoInfo &info ) const
870 Pathname name = _pimpl->options.repoCachePath;
871 return PathInfo(name + Pathname(info.alias()).extend(".solv")).isExist();
874 RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
877 Pathname base = _pimpl->options.repoCachePath + info.alias();
878 Pathname cookiefile = base.extend(".cookie");
880 return RepoStatus::fromCookieFile(cookiefile);
883 void RepoManager::setCacheStatus( const string &alias, const RepoStatus &status )
885 Pathname base = _pimpl->options.repoCachePath + alias;
886 Pathname cookiefile = base.extend(".cookie");
888 std::ofstream file(cookiefile.c_str());
890 ZYPP_THROW (Exception( "Can't open " + cookiefile.asString() ) );
896 map<data::RecordId, Repo *> repo2solv;
898 Repository RepoManager::createFromCache( const RepoInfo &info,
899 const ProgressData::ReceiverFnc & progressrcv )
901 callback::SendReport<ProgressReport> report;
902 ProgressData progress;
903 progress.sendTo(ProgressReportAdaptor( progressrcv, report ));
904 //progress.sendTo( progressrcv );
905 progress.name(str::form(_("Reading repository '%s' cache"), info.name().c_str()));
907 //_pimpl->options.repoCachePath
908 if ( ! isCached( info ) )
909 ZYPP_THROW(RepoNotCachedException());
911 MIL << "Repository " << info.alias() << " is cached" << endl;
913 CombinedProgressData subprogrcv(progress);
915 repo::cached::RepoOptions opts( info, _pimpl->options.repoCachePath );
916 opts.readingResolvablesProgress = subprogrcv;
918 repo::cached::RepoImpl::Ptr repoimpl =
919 new repo::cached::RepoImpl( opts );
921 //repoimpl->createResolvables();
922 repoimpl->resolvables();
923 // read the resolvables from cache
924 //return Repository::noRepository;
925 return Repository(repoimpl);
928 ////////////////////////////////////////////////////////////////////////////
931 * Generate a non existing filename in a directory, using a base
932 * name. For example if a directory contains 3 files
938 * If you try to generate a unique filename for this directory,
939 * based on "ruu" you will get "ruu", but if you use the base
940 * "foo" you will get "foo_1"
942 * \param dir Directory where the file needs to be unique
943 * \param basefilename string to base the filename on.
945 static Pathname generate_non_existing_name( const Pathname &dir,
946 const std::string &basefilename )
948 string final_filename = basefilename;
950 while ( PathInfo(dir + final_filename).isExist() )
952 final_filename = basefilename + "_" + str::numstring(counter);
955 return dir + Pathname(final_filename);
958 ////////////////////////////////////////////////////////////////////////////
961 * \short Generate a related filename from a repo info
963 * From a repo info, it will try to use the alias as a filename
964 * escaping it if necessary. Other fallbacks can be added to
965 * this function in case there is no way to use the alias
967 static std::string generate_filename( const RepoInfo &info )
971 std::string filename = info.alias();
972 // replace slashes with underscores
973 size_t pos = filename.find(fnd);
974 while(pos!=string::npos)
976 filename.replace(pos,fnd.length(),rep);
977 pos = filename.find(fnd,pos+rep.length());
979 filename = Pathname(filename).extend(".repo").asString();
980 MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
985 ////////////////////////////////////////////////////////////////////////////
987 void RepoManager::addRepository( const RepoInfo &info,
988 const ProgressData::ReceiverFnc & progressrcv )
992 ProgressData progress(100);
993 callback::SendReport<ProgressReport> report;
994 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
995 progress.name(str::form(_("Adding repository '%s'"), info.name().c_str()));
998 std::list<RepoInfo> repos = knownRepositories();
999 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1003 if ( info.alias() == (*it).alias() )
1004 ZYPP_THROW(RepoAlreadyExistsException(info.alias()));
1007 RepoInfo tosave = info;
1009 // check the first url for now
1010 if ( ZConfig::instance().repo_add_probe()
1011 || ( tosave.type() == RepoType::NONE && tosave.enabled()) )
1013 DBG << "unknown repository type, probing" << endl;
1015 RepoType probedtype;
1016 probedtype = probe(*tosave.baseUrlsBegin());
1017 if ( tosave.baseUrlsSize() > 0 )
1019 if ( probedtype == RepoType::NONE )
1020 ZYPP_THROW(RepoUnknownTypeException());
1022 tosave.setType(probedtype);
1028 // assert the directory exists
1029 filesystem::assert_dir(_pimpl->options.knownReposPath);
1031 Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath,
1032 generate_filename(tosave));
1033 // now we have a filename that does not exists
1034 MIL << "Saving repo in " << repofile << endl;
1036 std::ofstream file(repofile.c_str());
1038 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1041 tosave.dumpRepoOn(file);
1043 MIL << "done" << endl;
1046 void RepoManager::addRepositories( const Url &url,
1047 const ProgressData::ReceiverFnc & progressrcv )
1049 std::list<RepoInfo> knownrepos = knownRepositories();
1050 std::list<RepoInfo> repos = readRepoFile(url);
1051 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1055 // look if the alias is in the known repos.
1056 for ( std::list<RepoInfo>::const_iterator kit = knownrepos.begin();
1057 kit != knownrepos.end();
1060 if ( (*it).alias() == (*kit).alias() )
1062 ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1063 ZYPP_THROW(RepoAlreadyExistsException((*it).alias()));
1068 string filename = Pathname(url.getPathName()).basename();
1070 if ( filename == Pathname() )
1071 ZYPP_THROW(RepoException("Invalid repo file name at " + url.asString() ));
1073 // assert the directory exists
1074 filesystem::assert_dir(_pimpl->options.knownReposPath);
1076 Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath, filename);
1077 // now we have a filename that does not exists
1078 MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1080 std::ofstream file(repofile.c_str());
1082 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1085 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1089 MIL << "Saving " << (*it).alias() << endl;
1090 (*it).dumpRepoOn(file);
1092 MIL << "done" << endl;
1095 ////////////////////////////////////////////////////////////////////////////
1097 void RepoManager::removeRepository( const RepoInfo & info,
1098 const ProgressData::ReceiverFnc & progressrcv)
1100 ProgressData progress;
1101 callback::SendReport<ProgressReport> report;
1102 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1103 progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
1105 MIL << "Going to delete repo " << info.alias() << endl;
1107 std::list<RepoInfo> repos = knownRepositories();
1108 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1112 // they can be the same only if the provided is empty, that means
1113 // the provided repo has no alias
1115 if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1118 // TODO match by url
1120 // we have a matcing repository, now we need to know
1121 // where it does come from.
1122 RepoInfo todelete = *it;
1123 if (todelete.filepath().empty())
1125 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1129 // figure how many repos are there in the file:
1130 std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1131 if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
1133 // easy, only this one, just delete the file
1134 if ( filesystem::unlink(todelete.filepath()) != 0 )
1136 ZYPP_THROW(RepoException("Can't delete " + todelete.filepath().asString()));
1138 MIL << todelete.alias() << " sucessfully deleted." << endl;
1142 // there are more repos in the same file
1143 // write them back except the deleted one.
1145 //std::ofstream file(tmp.path().c_str());
1147 // assert the directory exists
1148 filesystem::assert_dir(todelete.filepath().dirname());
1150 std::ofstream file(todelete.filepath().c_str());
1152 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1153 ZYPP_THROW (Exception( "Can't open " + todelete.filepath().asString() ) );
1155 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1156 fit != filerepos.end();
1159 if ( (*fit).alias() != todelete.alias() )
1160 (*fit).dumpRepoOn(file);
1164 CombinedProgressData subprogrcv(progress, 70);
1165 CombinedProgressData cleansubprogrcv(progress, 30);
1166 // now delete it from cache
1167 if ( isCached(todelete) )
1168 cleanCache( todelete, subprogrcv);
1169 // now delete metadata (#301037)
1170 cleanMetadata( todelete, cleansubprogrcv);
1171 MIL << todelete.alias() << " sucessfully deleted." << endl;
1173 } // else filepath is empty
1176 // should not be reached on a sucess workflow
1177 ZYPP_THROW(RepoNotFoundException(info));
1180 ////////////////////////////////////////////////////////////////////////////
1182 void RepoManager::modifyRepository( const std::string &alias,
1183 const RepoInfo & newinfo,
1184 const ProgressData::ReceiverFnc & progressrcv )
1186 RepoInfo toedit = getRepositoryInfo(alias);
1188 if (toedit.filepath().empty())
1190 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1194 // figure how many repos are there in the file:
1195 std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1197 // there are more repos in the same file
1198 // write them back except the deleted one.
1200 //std::ofstream file(tmp.path().c_str());
1202 // assert the directory exists
1203 filesystem::assert_dir(toedit.filepath().dirname());
1205 std::ofstream file(toedit.filepath().c_str());
1207 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1208 ZYPP_THROW (Exception( "Can't open " + toedit.filepath().asString() ) );
1210 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1211 fit != filerepos.end();
1214 // if the alias is different, dump the original
1215 // if it is the same, dump the provided one
1216 if ( (*fit).alias() != toedit.alias() )
1217 (*fit).dumpRepoOn(file);
1219 newinfo.dumpRepoOn(file);
1224 ////////////////////////////////////////////////////////////////////////////
1226 RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
1227 const ProgressData::ReceiverFnc & progressrcv )
1229 std::list<RepoInfo> repos = knownRepositories();
1230 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1234 if ( (*it).alias() == alias )
1238 info.setAlias(info.alias());
1239 ZYPP_THROW(RepoNotFoundException(info));
1242 ////////////////////////////////////////////////////////////////////////////
1244 RepoInfo RepoManager::getRepositoryInfo( const Url & url,
1245 const url::ViewOption & urlview,
1246 const ProgressData::ReceiverFnc & progressrcv )
1248 std::list<RepoInfo> repos = knownRepositories();
1249 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1253 for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1254 urlit != (*it).baseUrlsEnd();
1257 if ((*urlit).asString(urlview) == url.asString(urlview))
1262 info.setAlias(info.alias());
1263 info.setBaseUrl(url);
1264 ZYPP_THROW(RepoNotFoundException(info));
1267 ////////////////////////////////////////////////////////////////////////////
1269 std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
1271 return str << *obj._pimpl;
1274 /////////////////////////////////////////////////////////////////
1276 ///////////////////////////////////////////////////////////////////