1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/RepoManager.cc
17 #include "zypp/base/InputStream.h"
18 #include "zypp/base/Logger.h"
19 #include "zypp/base/Gettext.h"
20 #include "zypp/base/Function.h"
21 #include "zypp/PathInfo.h"
22 #include "zypp/TmpPath.h"
24 #include "zypp/repo/RepoException.h"
25 #include "zypp/RepoManager.h"
27 #include "zypp/cache/CacheStore.h"
28 #include "zypp/repo/cached/RepoImpl.h"
29 #include "zypp/media/MediaManager.h"
30 #include "zypp/MediaSetAccess.h"
32 #include "zypp/parser/RepoFileReader.h"
33 #include "zypp/repo/yum/Downloader.h"
34 #include "zypp/parser/yum/RepoParser.h"
35 #include "zypp/parser/plaindir/RepoParser.h"
36 #include "zypp/repo/susetags/Downloader.h"
37 #include "zypp/parser/susetags/RepoParser.h"
39 #include "zypp/ZYppCallbacks.h"
43 using namespace zypp::repo;
44 using namespace zypp::filesystem;
46 using namespace zypp::repo;
48 ///////////////////////////////////////////////////////////////////
50 { /////////////////////////////////////////////////////////////////
52 ///////////////////////////////////////////////////////////////////
54 // CLASS NAME : RepoManagerOptions
56 ///////////////////////////////////////////////////////////////////
58 RepoManagerOptions::RepoManagerOptions()
60 repoCachePath = ZConfig::instance().repoCachePath();
61 repoRawCachePath = ZConfig::instance().repoMetadataPath();
62 knownReposPath = ZConfig::instance().knownReposPath();
65 ////////////////////////////////////////////////////////////////////////////
68 * \short Simple callback to collect the results
70 * Classes like RepoFileParser call the callback
71 * once per each repo in a file.
73 * Passing this functor as callback, you can collect
74 * all resuls at the end, without dealing with async
89 bool collect( const RepoInfo &repo )
91 //MIL << "here in collector: " << repo.alias() << endl;
92 repos.push_back(repo);
93 //MIL << "added: " << repo.alias() << endl;
100 ////////////////////////////////////////////////////////////////////////////
103 * \short Internal version of clean cache
105 * Takes an extra CacheStore reference, so we avoid internally
106 * having 2 CacheStores writing to the same database.
108 static void cleanCacheInternal( cache::CacheStore &store,
109 const RepoInfo &info,
110 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() )
112 ProgressData progress;
113 callback::SendReport<ProgressReport> report;
114 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
115 progress.name(str::form(_("Cleaning repository '%s' cache"), info.name().c_str()));
117 if ( !store.isCached(info.alias()) )
120 MIL << info.alias() << " cleaning cache..." << endl;
121 data::RecordId id = store.lookupRepository(info.alias());
123 CombinedProgressData subprogrcv(progress);
125 store.cleanRepository(id, subprogrcv);
128 ////////////////////////////////////////////////////////////////////////////
131 * Reads RepoInfo's from a repo file.
133 * \param file pathname of the file to read.
135 static std::list<RepoInfo> repositories_in_file( const Pathname & file )
137 MIL << "repo file: " << file << endl;
138 RepoCollector collector;
139 parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
140 return collector.repos;
143 ////////////////////////////////////////////////////////////////////////////
145 std::list<RepoInfo> readRepoFile(const Url & repo_file)
147 // no interface to download a specific file, using workaround:
148 //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
150 Pathname path(url.getPathName());
151 url.setPathName ("/");
152 MediaSetAccess access(url);
153 Pathname local = access.provideFile(path);
155 DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
157 return repositories_in_file(local);
160 ////////////////////////////////////////////////////////////////////////////
163 * \short List of RepoInfo's from a directory
165 * Goes trough every file in a directory and adds all
166 * RepoInfo's contained in that file.
168 * \param dir pathname of the directory to read.
170 static std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
172 MIL << "directory " << dir << endl;
173 list<RepoInfo> repos;
174 list<Pathname> entries;
175 if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
176 ZYPP_THROW(Exception("failed to read directory"));
178 for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
180 list<RepoInfo> tmp = repositories_in_file( *it );
181 repos.insert( repos.end(), tmp.begin(), tmp.end() );
183 //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
184 //MIL << "ok" << endl;
189 ////////////////////////////////////////////////////////////////////////////
191 static void assert_alias( const RepoInfo &info )
193 if (info.alias().empty())
194 ZYPP_THROW(RepoNoAliasException());
197 ////////////////////////////////////////////////////////////////////////////
199 static void assert_urls( const RepoInfo &info )
201 if (info.baseUrlsEmpty())
202 ZYPP_THROW(RepoNoUrlException());
205 ////////////////////////////////////////////////////////////////////////////
208 * \short Calculates the raw cache path for a repository
210 static Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
213 return opt.repoRawCachePath + info.alias();
216 ///////////////////////////////////////////////////////////////////
218 // CLASS NAME : RepoManager::Impl
220 ///////////////////////////////////////////////////////////////////
223 * \short RepoManager implementation.
225 struct RepoManager::Impl
227 Impl( const RepoManagerOptions &opt )
238 RepoManagerOptions options;
241 /** Offer default Impl. */
242 static shared_ptr<Impl> nullimpl()
244 static shared_ptr<Impl> _nullimpl( new Impl );
249 friend Impl * rwcowClone<Impl>( const Impl * rhs );
250 /** clone for RWCOW_pointer */
252 { return new Impl( *this ); }
254 ///////////////////////////////////////////////////////////////////
256 /** \relates RepoManager::Impl Stream output */
257 inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
259 return str << "RepoManager::Impl";
262 ///////////////////////////////////////////////////////////////////
264 // CLASS NAME : RepoManager
266 ///////////////////////////////////////////////////////////////////
268 RepoManager::RepoManager( const RepoManagerOptions &opt )
269 : _pimpl( new Impl(opt) )
272 ////////////////////////////////////////////////////////////////////////////
274 RepoManager::~RepoManager()
277 ////////////////////////////////////////////////////////////////////////////
279 std::list<RepoInfo> RepoManager::knownRepositories() const
283 if ( PathInfo(_pimpl->options.knownReposPath).isExist() )
285 RepoInfoList repos = repositories_in_dir(_pimpl->options.knownReposPath);
286 for ( RepoInfoList::iterator it = repos.begin();
290 // set the metadata path for the repo
291 Pathname metadata_path = rawcache_path_for_repoinfo(_pimpl->options, (*it));
292 (*it).setMetadataPath(metadata_path);
297 return std::list<RepoInfo>();
302 ////////////////////////////////////////////////////////////////////////////
304 RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
306 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
307 RepoType repokind = info.type();
310 switch ( repokind.toEnum() )
312 case RepoType::NONE_e:
313 // unknown, probe the local metadata
314 repokind = probe(rawpath.asUrl());
320 switch ( repokind.toEnum() )
322 case RepoType::RPMMD_e :
324 status = RepoStatus( rawpath + "/repodata/repomd.xml");
328 case RepoType::YAST2_e :
330 status = RepoStatus( rawpath + "/content");
334 case RepoType::RPMPLAINDIR_e :
336 if ( PathInfo(Pathname(rawpath + "/cookie")).isExist() )
337 status = RepoStatus( rawpath + "/cookie");
341 case RepoType::NONE_e :
342 // Return default RepoStatus in case of RepoType::NONE
343 // indicating it should be created?
344 // ZYPP_THROW(RepoUnknownTypeException());
350 bool RepoManager::checkIfToRefreshMetadata( const RepoInfo &info,
352 RawMetadataRefreshPolicy policy )
356 RepoStatus oldstatus;
357 RepoStatus newstatus;
361 MIL << "Going to try to check whether refresh is needed for " << url << endl;
363 repo::RepoType repokind = info.type();
365 // if the type is unknown, try probing.
366 switch ( repokind.toEnum() )
368 case RepoType::NONE_e:
370 repokind = probe(url);
376 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
377 filesystem::assert_dir(rawpath);
378 oldstatus = metadataStatus(info);
380 // create temp dir as sibling of rawpath
381 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
383 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
384 ( repokind.toEnum() == RepoType::YAST2_e ) )
386 MediaSetAccess media(url);
387 shared_ptr<repo::Downloader> downloader_ptr;
389 if ( repokind.toEnum() == RepoType::RPMMD_e )
390 downloader_ptr.reset(new yum::Downloader(info.path()));
392 downloader_ptr.reset( new susetags::Downloader(info.path()));
394 RepoStatus newstatus = downloader_ptr->status(media);
395 bool refresh = false;
396 if ( oldstatus.checksum() == newstatus.checksum() )
398 MIL << "repo has not changed" << endl;
399 if ( policy == RefreshForced )
401 MIL << "refresh set to forced" << endl;
407 MIL << "repo has changed, going to refresh" << endl;
413 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
415 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
416 bool refresh = false;
417 if ( oldstatus.checksum() == newstatus.checksum() )
419 MIL << "repo has not changed" << endl;
420 if ( policy == RefreshForced )
422 MIL << "refresh set to forced" << endl;
428 MIL << "repo has changed, going to refresh" << endl;
436 ZYPP_THROW(RepoUnknownTypeException());
439 catch ( const Exception &e )
442 ERR << "refresh check failed for " << url << endl;
446 return true; // default
449 void RepoManager::refreshMetadata( const RepoInfo &info,
450 RawMetadataRefreshPolicy policy,
451 const ProgressData::ReceiverFnc & progress )
456 // try urls one by one
457 for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
463 // check whether to refresh metadata
464 // if the check fails for this url, it throws, so another url will be checked
465 if (!checkIfToRefreshMetadata(info, url, policy))
468 MIL << "Going to refresh metadata from " << url << endl;
470 repo::RepoType repokind = info.type();
472 // if the type is unknown, try probing.
473 switch ( repokind.toEnum() )
475 case RepoType::NONE_e:
477 repokind = probe(*it);
483 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
484 filesystem::assert_dir(rawpath);
486 // create temp dir as sibling of rawpath
487 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
489 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
490 ( repokind.toEnum() == RepoType::YAST2_e ) )
492 MediaSetAccess media(url);
493 shared_ptr<repo::Downloader> downloader_ptr;
495 if ( repokind.toEnum() == RepoType::RPMMD_e )
496 downloader_ptr.reset(new yum::Downloader(info.path()));
498 downloader_ptr.reset( new susetags::Downloader(info.path()));
501 * Given a downloader, sets the other repos raw metadata
502 * path as cache paths for the fetcher, so if another
503 * repo has the same file, it will not download it
504 * but copy it from the other repository
506 std::list<RepoInfo> repos = knownRepositories();
507 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
511 downloader_ptr->addCachePath(rawcache_path_for_repoinfo( _pimpl->options, *it ));
514 downloader_ptr->download( media, tmpdir.path());
516 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
518 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
520 std::ofstream file(( tmpdir.path() + "/cookie").c_str());
522 ZYPP_THROW (Exception( "Can't open " + tmpdir.path().asString() + "/cookie" ) );
525 file << newstatus.checksum() << endl;
531 ZYPP_THROW(RepoUnknownTypeException());
534 // ok we have the metadata, now exchange
536 TmpDir oldmetadata( TmpDir::makeSibling( rawpath ) );
537 filesystem::rename( rawpath, oldmetadata.path() );
538 // move the just downloaded there
539 filesystem::rename( tmpdir.path(), rawpath );
543 catch ( const Exception &e )
546 ERR << "Trying another url..." << endl;
549 ERR << "No more urls..." << endl;
550 ZYPP_THROW(RepoException(_("Valid metadata not found at specified URL(s)")));
553 ////////////////////////////////////////////////////////////////////////////
555 void RepoManager::cleanMetadata( const RepoInfo &info,
556 const ProgressData::ReceiverFnc & progress )
558 filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_pimpl->options, info));
561 void RepoManager::buildCache( const RepoInfo &info,
562 CacheBuildPolicy policy,
563 const ProgressData::ReceiverFnc & progressrcv )
566 Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
568 cache::CacheStore store(_pimpl->options.repoCachePath);
570 RepoStatus raw_metadata_status = metadataStatus(info);
571 if ( raw_metadata_status.empty() )
573 ZYPP_THROW(RepoMetadataException(info));
576 bool needs_cleaning = false;
577 if ( store.isCached( info.alias() ) )
579 MIL << info.alias() << " is already cached." << endl;
580 data::RecordId id = store.lookupRepository(info.alias());
581 RepoStatus cache_status = store.repositoryStatus(id);
583 if ( cache_status.checksum() == raw_metadata_status.checksum() )
585 MIL << info.alias() << " cache is up to date with metadata." << endl;
586 if ( policy == BuildIfNeeded ) {
590 MIL << info.alias() << " cache rebuild is forced" << endl;
594 needs_cleaning = true;
597 ProgressData progress(100);
598 callback::SendReport<ProgressReport> report;
599 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
600 progress.name(str::form(_("Building repository '%s' cache"), info.name().c_str()));
604 cleanCacheInternal( store, info);
606 MIL << info.alias() << " building cache..." << endl;
607 data::RecordId id = store.lookupOrAppendRepository(info.alias());
609 repo::RepoType repokind = info.type();
611 // if the type is unknown, try probing.
612 switch ( repokind.toEnum() )
614 case RepoType::NONE_e:
615 // unknown, probe the local metadata
616 repokind = probe(rawpath.asUrl());
623 switch ( repokind.toEnum() )
625 case RepoType::RPMMD_e :
627 CombinedProgressData subprogrcv( progress, 100);
628 parser::yum::RepoParser parser(id, store, parser::yum::RepoParserOpts(), subprogrcv);
629 parser.parse(rawpath);
633 case RepoType::YAST2_e :
635 CombinedProgressData subprogrcv( progress, 100);
636 parser::susetags::RepoParser parser(id, store, subprogrcv);
637 parser.parse(rawpath);
641 case RepoType::RPMPLAINDIR_e :
643 CombinedProgressData subprogrcv( progress, 100);
644 InputStream is(rawpath + "cookie");
646 getline( is.stream(), buffer);
648 parser::plaindir::RepoParser parser(id, store, subprogrcv);
649 parser.parse(url.getPathName());
653 ZYPP_THROW(RepoUnknownTypeException());
656 // update timestamp and checksum
657 store.updateRepositoryStatus(id, raw_metadata_status);
659 MIL << "Commit cache.." << endl;
664 ////////////////////////////////////////////////////////////////////////////
666 repo::RepoType RepoManager::probe( const Url &url ) const
668 if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName() ).isDir() )
670 // Handle non existing local directory in advance, as
671 // MediaSetAccess does not support it.
672 return repo::RepoType::NONE;
677 MediaSetAccess access(url);
678 if ( access.doesFileExist("/repodata/repomd.xml") )
679 return repo::RepoType::RPMMD;
680 if ( access.doesFileExist("/content") )
681 return repo::RepoType::YAST2;
683 // if it is a local url of type dir
684 if ( (! media::MediaManager::downloads(url)) && ( url.getScheme() == "dir" ) )
686 Pathname path = Pathname(url.getPathName());
687 if ( PathInfo(path).isDir() )
689 // allow empty dirs for now
690 return repo::RepoType::RPMPLAINDIR;
694 catch ( const media::MediaException &e )
697 RepoException enew("Error trying to read from " + url.asString());
701 catch ( const Exception &e )
704 Exception enew("Unknown error reading from " + url.asString());
709 return repo::RepoType::NONE;
712 ////////////////////////////////////////////////////////////////////////////
714 void RepoManager::cleanCache( const RepoInfo &info,
715 const ProgressData::ReceiverFnc & progressrcv )
717 cache::CacheStore store(_pimpl->options.repoCachePath);
718 cleanCacheInternal( store, info, progressrcv );
722 ////////////////////////////////////////////////////////////////////////////
724 bool RepoManager::isCached( const RepoInfo &info ) const
726 cache::CacheStore store(_pimpl->options.repoCachePath);
727 return store.isCached(info.alias());
730 RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
732 cache::CacheStore store(_pimpl->options.repoCachePath);
733 data::RecordId id = store.lookupRepository(info.alias());
734 RepoStatus cache_status = store.repositoryStatus(id);
738 Repository RepoManager::createFromCache( const RepoInfo &info,
739 const ProgressData::ReceiverFnc & progressrcv )
741 callback::SendReport<ProgressReport> report;
742 ProgressData progress;
743 progress.sendTo(ProgressReportAdaptor( progressrcv, report ));
744 //progress.sendTo( progressrcv );
745 progress.name(str::form(_("Reading repository '%s' cache"), info.name().c_str()));
747 cache::CacheStore store(_pimpl->options.repoCachePath);
749 if ( ! store.isCached( info.alias() ) )
750 ZYPP_THROW(RepoNotCachedException());
752 MIL << "Repository " << info.alias() << " is cached" << endl;
754 data::RecordId id = store.lookupRepository(info.alias());
756 CombinedProgressData subprogrcv(progress);
758 repo::cached::RepoOptions opts( info, _pimpl->options.repoCachePath, id );
759 opts.readingResolvablesProgress = subprogrcv;
760 repo::cached::RepoImpl::Ptr repoimpl =
761 new repo::cached::RepoImpl( opts );
763 repoimpl->resolvables();
764 // read the resolvables from cache
765 return Repository(repoimpl);
768 ////////////////////////////////////////////////////////////////////////////
771 * Generate a non existing filename in a directory, using a base
772 * name. For example if a directory contains 3 files
778 * If you try to generate a unique filename for this directory,
779 * based on "ruu" you will get "ruu", but if you use the base
780 * "foo" you will get "foo_1"
782 * \param dir Directory where the file needs to be unique
783 * \param basefilename string to base the filename on.
785 static Pathname generate_non_existing_name( const Pathname &dir,
786 const std::string &basefilename )
788 string final_filename = basefilename;
790 while ( PathInfo(dir + final_filename).isExist() )
792 final_filename = basefilename + "_" + str::numstring(counter);
795 return dir + Pathname(final_filename);
798 ////////////////////////////////////////////////////////////////////////////
801 * \short Generate a related filename from a repo info
803 * From a repo info, it will try to use the alias as a filename
804 * escaping it if necessary. Other fallbacks can be added to
805 * this function in case there is no way to use the alias
807 static std::string generate_filename( const RepoInfo &info )
811 std::string filename = info.alias();
812 // replace slashes with underscores
813 size_t pos = filename.find(fnd);
814 while(pos!=string::npos)
816 filename.replace(pos,fnd.length(),rep);
817 pos = filename.find(fnd,pos+rep.length());
819 filename = Pathname(filename).extend(".repo").asString();
820 MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
825 ////////////////////////////////////////////////////////////////////////////
827 void RepoManager::addRepository( const RepoInfo &info,
828 const ProgressData::ReceiverFnc & progressrcv )
832 ProgressData progress(100);
833 callback::SendReport<ProgressReport> report;
834 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
835 progress.name(str::form(_("Adding repository '%s'"), info.name().c_str()));
838 std::list<RepoInfo> repos = knownRepositories();
839 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
843 if ( info.alias() == (*it).alias() )
844 ZYPP_THROW(RepoAlreadyExistsException(info.alias()));
847 RepoInfo tosave = info;
849 // check the first url for now
850 if ( ZConfig::instance().repo_add_probe() || ( tosave.type() == RepoType::NONE ) )
853 probedtype = probe(*tosave.baseUrlsBegin());
854 if ( tosave.baseUrlsSize() > 0 )
856 if ( probedtype == RepoType::NONE )
857 ZYPP_THROW(RepoUnknownTypeException());
859 tosave.setType(probedtype);
865 // assert the directory exists
866 filesystem::assert_dir(_pimpl->options.knownReposPath);
868 Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath,
869 generate_filename(tosave));
870 // now we have a filename that does not exists
871 MIL << "Saving repo in " << repofile << endl;
873 std::ofstream file(repofile.c_str());
875 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
878 tosave.dumpRepoOn(file);
880 MIL << "done" << endl;
883 void RepoManager::addRepositories( const Url &url,
884 const ProgressData::ReceiverFnc & progressrcv )
886 std::list<RepoInfo> knownrepos = knownRepositories();
887 std::list<RepoInfo> repos = readRepoFile(url);
888 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
892 // look if the alias is in the known repos.
893 for ( std::list<RepoInfo>::const_iterator kit = knownrepos.begin();
894 kit != knownrepos.end();
897 if ( (*it).alias() == (*kit).alias() )
899 ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
900 ZYPP_THROW(RepoAlreadyExistsException((*it).alias()));
905 string filename = Pathname(url.getPathName()).basename();
907 if ( filename == Pathname() )
908 ZYPP_THROW(RepoException("Invalid repo file name at " + url.asString() ));
910 // assert the directory exists
911 filesystem::assert_dir(_pimpl->options.knownReposPath);
913 Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath, filename);
914 // now we have a filename that does not exists
915 MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
917 std::ofstream file(repofile.c_str());
919 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
922 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
926 MIL << "Saving " << (*it).alias() << endl;
927 (*it).dumpRepoOn(file);
929 MIL << "done" << endl;
932 ////////////////////////////////////////////////////////////////////////////
934 void RepoManager::removeRepository( const RepoInfo & info,
935 const ProgressData::ReceiverFnc & progressrcv)
937 ProgressData progress;
938 callback::SendReport<ProgressReport> report;
939 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
940 progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
942 MIL << "Going to delete repo " << info.alias() << endl;
944 std::list<RepoInfo> repos = knownRepositories();
945 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
949 // they can be the same only if the provided is empty, that means
950 // the provided repo has no alias
952 if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
957 // we have a matcing repository, now we need to know
958 // where it does come from.
959 RepoInfo todelete = *it;
960 if (todelete.filepath().empty())
962 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
966 // figure how many repos are there in the file:
967 std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
968 if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
970 // easy, only this one, just delete the file
971 if ( filesystem::unlink(todelete.filepath()) != 0 )
973 ZYPP_THROW(RepoException("Can't delete " + todelete.filepath().asString()));
975 MIL << todelete.alias() << " sucessfully deleted." << endl;
979 // there are more repos in the same file
980 // write them back except the deleted one.
982 //std::ofstream file(tmp.path().c_str());
984 // assert the directory exists
985 filesystem::assert_dir(todelete.filepath().dirname());
987 std::ofstream file(todelete.filepath().c_str());
989 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
990 ZYPP_THROW (Exception( "Can't open " + todelete.filepath().asString() ) );
992 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
993 fit != filerepos.end();
996 if ( (*fit).alias() != todelete.alias() )
997 (*fit).dumpRepoOn(file);
1001 CombinedProgressData subprogrcv(progress);
1003 // now delete it from cache
1004 cleanCache( todelete, subprogrcv);
1006 MIL << todelete.alias() << " sucessfully deleted." << endl;
1008 } // else filepath is empty
1011 // should not be reached on a sucess workflow
1012 ZYPP_THROW(RepoNotFoundException(info));
1015 ////////////////////////////////////////////////////////////////////////////
1017 void RepoManager::modifyRepository( const std::string &alias,
1018 const RepoInfo & newinfo,
1019 const ProgressData::ReceiverFnc & progressrcv )
1021 RepoInfo toedit = getRepositoryInfo(alias);
1023 if (toedit.filepath().empty())
1025 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1029 // figure how many repos are there in the file:
1030 std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1032 // there are more repos in the same file
1033 // write them back except the deleted one.
1035 //std::ofstream file(tmp.path().c_str());
1037 // assert the directory exists
1038 filesystem::assert_dir(toedit.filepath().dirname());
1040 std::ofstream file(toedit.filepath().c_str());
1042 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1043 ZYPP_THROW (Exception( "Can't open " + toedit.filepath().asString() ) );
1045 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1046 fit != filerepos.end();
1049 // if the alias is different, dump the original
1050 // if it is the same, dump the provided one
1051 if ( (*fit).alias() != toedit.alias() )
1052 (*fit).dumpRepoOn(file);
1054 newinfo.dumpRepoOn(file);
1059 ////////////////////////////////////////////////////////////////////////////
1061 RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
1062 const ProgressData::ReceiverFnc & progressrcv )
1064 std::list<RepoInfo> repos = knownRepositories();
1065 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1069 if ( (*it).alias() == alias )
1073 info.setAlias(info.alias());
1074 ZYPP_THROW(RepoNotFoundException(info));
1077 ////////////////////////////////////////////////////////////////////////////
1079 RepoInfo RepoManager::getRepositoryInfo( const Url & url,
1080 const url::ViewOption & urlview,
1081 const ProgressData::ReceiverFnc & progressrcv )
1083 std::list<RepoInfo> repos = knownRepositories();
1084 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1088 for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1089 urlit != (*it).baseUrlsEnd();
1092 if ((*urlit).asString(urlview) == url.asString(urlview))
1097 info.setAlias(info.alias());
1098 info.setBaseUrl(url);
1099 ZYPP_THROW(RepoNotFoundException(info));
1102 ////////////////////////////////////////////////////////////////////////////
1104 std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
1106 return str << *obj._pimpl;
1109 /////////////////////////////////////////////////////////////////
1111 ///////////////////////////////////////////////////////////////////