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"
35 #include "zypp/parser/RepoFileReader.h"
36 #include "zypp/repo/yum/Downloader.h"
37 #include "zypp/parser/yum/RepoParser.h"
38 //#include "zypp/parser/plaindir/RepoParser.h"
39 #include "zypp/repo/susetags/Downloader.h"
40 #include "zypp/parser/susetags/RepoParser.h"
42 #include "zypp/ZYppCallbacks.h"
44 #include "satsolver/pool.h"
45 #include "satsolver/repo.h"
46 #include "satsolver/repo_solv.h"
50 using namespace zypp::repo;
51 using namespace zypp::filesystem;
53 using namespace zypp::repo;
55 ///////////////////////////////////////////////////////////////////
57 { /////////////////////////////////////////////////////////////////
59 ///////////////////////////////////////////////////////////////////
61 // CLASS NAME : RepoManagerOptions
63 ///////////////////////////////////////////////////////////////////
65 RepoManagerOptions::RepoManagerOptions()
67 repoCachePath = ZConfig::instance().repoCachePath();
68 repoRawCachePath = ZConfig::instance().repoMetadataPath();
69 knownReposPath = ZConfig::instance().knownReposPath();
72 ////////////////////////////////////////////////////////////////////////////
75 * \short Simple callback to collect the results
77 * Classes like RepoFileParser call the callback
78 * once per each repo in a file.
80 * Passing this functor as callback, you can collect
81 * all resuls at the end, without dealing with async
96 bool collect( const RepoInfo &repo )
98 //MIL << "here in collector: " << repo.alias() << endl;
99 repos.push_back(repo);
100 //MIL << "added: " << repo.alias() << endl;
107 ////////////////////////////////////////////////////////////////////////////
110 * \short Internal version of clean cache
112 * Takes an extra SolvStore reference, so we avoid internally
113 * having 2 SolvStores writing to the same database.
115 static void cleanCacheInternal( cache::SolvStore &store,
116 const RepoInfo &info,
117 const ProgressData::ReceiverFnc & progressrcv = ProgressData::ReceiverFnc() )
119 ProgressData progress;
120 callback::SendReport<ProgressReport> report;
121 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
122 progress.name(str::form(_("Cleaning repository '%s' cache"), info.name().c_str()));
124 if ( !store.isCached(info.alias()) )
127 MIL << info.alias() << " cleaning cache..." << endl;
128 data::RecordId id = store.lookupRepository(info.alias());
130 CombinedProgressData subprogrcv(progress);
132 store.cleanRepository(id, subprogrcv);
135 ////////////////////////////////////////////////////////////////////////////
138 * Reads RepoInfo's from a repo file.
140 * \param file pathname of the file to read.
142 static std::list<RepoInfo> repositories_in_file( const Pathname & file )
144 MIL << "repo file: " << file << endl;
145 RepoCollector collector;
146 parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
147 return collector.repos;
150 ////////////////////////////////////////////////////////////////////////////
152 std::list<RepoInfo> readRepoFile(const Url & repo_file)
154 // no interface to download a specific file, using workaround:
155 //! \todo add MediaManager::provideFile(Url file_url) to easily access any file URLs? (no need for media access id or media_nr)
157 Pathname path(url.getPathName());
158 url.setPathName ("/");
159 MediaSetAccess access(url);
160 Pathname local = access.provideFile(path);
162 DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
164 return repositories_in_file(local);
167 ////////////////////////////////////////////////////////////////////////////
170 * \short List of RepoInfo's from a directory
172 * Goes trough every file ending with ".repo" in a directory and adds all
173 * RepoInfo's contained in that file.
175 * \param dir pathname of the directory to read.
177 static std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
179 MIL << "directory " << dir << endl;
180 list<RepoInfo> repos;
181 list<Pathname> entries;
182 if ( filesystem::readdir( entries, Pathname(dir), false ) != 0 )
183 ZYPP_THROW(Exception("failed to read directory"));
185 str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
186 for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
188 if (str::regex_match(it->extension(), allowedRepoExt))
190 list<RepoInfo> tmp = repositories_in_file( *it );
191 repos.insert( repos.end(), tmp.begin(), tmp.end() );
193 //std::copy( collector.repos.begin(), collector.repos.end(), std::back_inserter(repos));
194 //MIL << "ok" << endl;
200 ////////////////////////////////////////////////////////////////////////////
202 static void assert_alias( const RepoInfo &info )
204 if (info.alias().empty())
205 ZYPP_THROW(RepoNoAliasException());
208 ////////////////////////////////////////////////////////////////////////////
210 static void assert_urls( const RepoInfo &info )
212 if (info.baseUrlsEmpty())
213 ZYPP_THROW(RepoNoUrlException());
216 ////////////////////////////////////////////////////////////////////////////
219 * \short Calculates the raw cache path for a repository
221 static Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
224 return opt.repoRawCachePath + info.alias();
227 ///////////////////////////////////////////////////////////////////
229 // CLASS NAME : RepoManager::Impl
231 ///////////////////////////////////////////////////////////////////
234 * \short RepoManager implementation.
236 struct RepoManager::Impl
238 Impl( const RepoManagerOptions &opt )
249 RepoManagerOptions options;
252 /** Offer default Impl. */
253 static shared_ptr<Impl> nullimpl()
255 static shared_ptr<Impl> _nullimpl( new Impl );
260 friend Impl * rwcowClone<Impl>( const Impl * rhs );
261 /** clone for RWCOW_pointer */
263 { return new Impl( *this ); }
266 ///////////////////////////////////////////////////////////////////
268 /** \relates RepoManager::Impl Stream output */
269 inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
271 return str << "RepoManager::Impl";
274 ///////////////////////////////////////////////////////////////////
276 // CLASS NAME : RepoManager
278 ///////////////////////////////////////////////////////////////////
280 RepoManager::RepoManager( const RepoManagerOptions &opt )
281 : _pimpl( new Impl(opt) )
284 ////////////////////////////////////////////////////////////////////////////
286 RepoManager::~RepoManager()
289 ////////////////////////////////////////////////////////////////////////////
291 std::list<RepoInfo> RepoManager::knownRepositories() const
295 if ( PathInfo(_pimpl->options.knownReposPath).isExist() )
297 RepoInfoList repos = repositories_in_dir(_pimpl->options.knownReposPath);
298 for ( RepoInfoList::iterator it = repos.begin();
302 // set the metadata path for the repo
303 Pathname metadata_path = rawcache_path_for_repoinfo(_pimpl->options, (*it));
304 (*it).setMetadataPath(metadata_path);
309 return std::list<RepoInfo>();
314 ////////////////////////////////////////////////////////////////////////////
316 Pathname RepoManager::metadataPath( const RepoInfo &info ) const
318 return rawcache_path_for_repoinfo(_pimpl->options, info );
321 ////////////////////////////////////////////////////////////////////////////
323 RepoStatus RepoManager::metadataStatus( const RepoInfo &info ) const
325 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
326 RepoType repokind = info.type();
329 switch ( repokind.toEnum() )
331 case RepoType::NONE_e:
332 // unknown, probe the local metadata
333 repokind = probe(rawpath.asUrl());
339 switch ( repokind.toEnum() )
341 case RepoType::RPMMD_e :
343 status = RepoStatus( rawpath + "/repodata/repomd.xml");
347 case RepoType::YAST2_e :
349 // the order of RepoStatus && RepoStatus matters! (#304310)
350 status = RepoStatus( rawpath + "/content") && (RepoStatus( rawpath + "/media.1/media"));
354 case RepoType::RPMPLAINDIR_e :
356 if ( PathInfo(Pathname(rawpath + "/cookie")).isExist() )
357 status = RepoStatus( rawpath + "/cookie");
361 case RepoType::NONE_e :
362 // Return default RepoStatus in case of RepoType::NONE
363 // indicating it should be created?
364 // ZYPP_THROW(RepoUnknownTypeException());
370 void RepoManager::touchIndexFile(const RepoInfo & info)
372 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
374 RepoType repokind = info.type();
375 if ( repokind.toEnum() == RepoType::NONE_e )
376 // unknown, probe the local metadata
377 repokind = probe(rawpath.asUrl());
378 // if still unknown, just return
379 if (repokind == RepoType::NONE_e)
383 switch ( repokind.toEnum() )
385 case RepoType::RPMMD_e :
386 p = Pathname(rawpath + "/repodata/repomd.xml");
389 case RepoType::YAST2_e :
390 p = Pathname(rawpath + "/content");
393 case RepoType::RPMPLAINDIR_e :
394 p = Pathname(rawpath + "/cookie");
397 case RepoType::NONE_e :
402 // touch the file, ignore error (they are logged anyway)
403 filesystem::touch(p);
406 bool RepoManager::checkIfToRefreshMetadata( const RepoInfo &info,
408 RawMetadataRefreshPolicy policy )
412 RepoStatus oldstatus;
413 RepoStatus newstatus;
417 MIL << "Going to try to check whether refresh is needed for " << url << endl;
419 repo::RepoType repokind = info.type();
421 // if the type is unknown, try probing.
422 switch ( repokind.toEnum() )
424 case RepoType::NONE_e:
426 repokind = probe(url);
432 Pathname rawpath = rawcache_path_for_repoinfo( _pimpl->options, info );
433 filesystem::assert_dir(rawpath);
434 oldstatus = metadataStatus(info);
436 // now we've got the old (cached) status, we can decide repo.refresh.delay
437 if (policy != RefreshForced)
439 // difference in seconds
440 double diff = difftime(
441 (Date::ValueType)Date::now(),
442 (Date::ValueType)oldstatus.timestamp()) / 60;
444 DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
445 DBG << "current time: " << (Date::ValueType)Date::now() << endl;
446 DBG << "last refresh = " << diff << " minutes ago" << endl;
448 if (diff < ZConfig::instance().repo_refresh_delay())
450 MIL << "Repository '" << info.alias()
451 << "' has been refreshed less than repo.refresh.delay ("
452 << ZConfig::instance().repo_refresh_delay()
453 << ") minutes ago. Advising to skip refresh" << endl;
458 // create temp dir as sibling of rawpath
459 filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( rawpath ) );
461 if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
462 ( repokind.toEnum() == RepoType::YAST2_e ) )
464 MediaSetAccess media(url);
465 shared_ptr<repo::Downloader> downloader_ptr;
467 if ( repokind.toEnum() == RepoType::RPMMD_e )
468 downloader_ptr.reset(new yum::Downloader(info.path()));
470 downloader_ptr.reset( new susetags::Downloader(info.path()));
472 RepoStatus newstatus = downloader_ptr->status(media);
473 bool refresh = false;
474 if ( oldstatus.checksum() == newstatus.checksum() )
476 MIL << "repo has not changed" << endl;
477 if ( policy == RefreshForced )
479 MIL << "refresh set to forced" << endl;
485 MIL << "repo has changed, going to refresh" << endl;
490 touchIndexFile(info);
495 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
497 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
498 bool refresh = false;
499 if ( oldstatus.checksum() == newstatus.checksum() )
501 MIL << "repo has not changed" << endl;
502 if ( policy == RefreshForced )
504 MIL << "refresh set to forced" << endl;
510 MIL << "repo has changed, going to refresh" << endl;
515 touchIndexFile(info);
522 ZYPP_THROW(RepoUnknownTypeException());
525 catch ( const Exception &e )
528 ERR << "refresh check failed for " << url << endl;
532 return true; // default
535 void RepoManager::refreshMetadata( const RepoInfo &info,
536 RawMetadataRefreshPolicy policy,
537 const ProgressData::ReceiverFnc & progress )
542 // we will throw this later if no URL checks out fine
543 RepoException rexception(_("Valid metadata not found at specified URL(s)"));
545 // try urls one by one
546 for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
552 // check whether to refresh metadata
553 // if the check fails for this url, it throws, so another url will be checked
554 if (!checkIfToRefreshMetadata(info, url, policy))
557 MIL << "Going to refresh metadata from " << url << endl;
559 repo::RepoType repokind = info.type();
561 // if the type is unknown, try probing.
562 switch ( repokind.toEnum() )
564 case RepoType::NONE_e:
566 repokind = probe(*it);
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 downloader_ptr->addCachePath(rawcache_path_for_repoinfo( _pimpl->options, *it ));
603 downloader_ptr->download( media, tmpdir.path());
606 else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
608 RepoStatus newstatus = parser::plaindir::dirStatus(url.getPathName());
610 std::ofstream file(( tmpdir.path() + "/cookie").c_str());
612 ZYPP_THROW (Exception( "Can't open " + tmpdir.path().asString() + "/cookie" ) );
615 file << newstatus.checksum() << endl;
622 ZYPP_THROW(RepoUnknownTypeException());
625 // ok we have the metadata, now exchange
627 TmpDir oldmetadata( TmpDir::makeSibling( rawpath ) );
628 filesystem::rename( rawpath, oldmetadata.path() );
629 // move the just downloaded there
630 filesystem::rename( tmpdir.path(), rawpath );
634 catch ( const Exception &e )
637 ERR << "Trying another url..." << endl;
639 // remember the exception caught for the *first URL*
640 // if all other URLs fail, the rexception will be thrown with the
641 // cause of the problem of the first URL remembered
642 if (it == info.baseUrlsBegin())
643 rexception.remember(e);
646 ERR << "No more urls..." << endl;
647 ZYPP_THROW(rexception);
650 ////////////////////////////////////////////////////////////////////////////
652 void RepoManager::cleanMetadata( const RepoInfo &info,
653 const ProgressData::ReceiverFnc & progressfnc )
655 ProgressData progress(100);
656 progress.sendTo(progressfnc);
658 filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_pimpl->options, info));
662 void RepoManager::buildCache( const RepoInfo &info,
663 CacheBuildPolicy policy,
664 const ProgressData::ReceiverFnc & progressrcv )
667 Pathname rawpath = rawcache_path_for_repoinfo(_pimpl->options, info);
669 Pathname base = _pimpl->options.repoCachePath + Pathname(info.alias());
670 Pathname solvfile = base.extend(".solv");
672 //cache::SolvStore store(_pimpl->options.repoCachePath);
674 RepoStatus raw_metadata_status = metadataStatus(info);
675 if ( raw_metadata_status.empty() )
677 ZYPP_THROW(RepoMetadataException(info));
680 bool needs_cleaning = false;
681 if ( isCached( info ) )
683 MIL << info.alias() << " is already cached." << endl;
684 //data::RecordId id = store.lookupRepository(info.alias());
685 RepoStatus cache_status = cacheStatus(info);
687 if ( cache_status.checksum() == raw_metadata_status.checksum() )
689 MIL << info.alias() << " cache is up to date with metadata." << endl;
690 if ( policy == BuildIfNeeded ) {
694 MIL << info.alias() << " cache rebuild is forced" << endl;
698 needs_cleaning = true;
701 ProgressData progress(100);
702 callback::SendReport<ProgressReport> report;
703 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
704 progress.name(str::form(_("Building repository '%s' cache"), info.name().c_str()));
709 // Pathname name = _pimpl->options.repoCachePath;
710 // //data::RecordId id = store.lookupRepository(info.alias());
713 // name += os.str() + ".solv";
715 // cleanCacheInternal( store, info);
719 MIL << info.alias() << " building cache..." << endl;
720 //data::RecordId id = store.lookupOrAppendRepository(info.alias());
722 repo::RepoType repokind = info.type();
724 // if the type is unknown, try probing.
725 switch ( repokind.toEnum() )
727 case RepoType::NONE_e:
728 // unknown, probe the local metadata
729 repokind = probe(rawpath.asUrl());
736 switch ( repokind.toEnum() )
738 case RepoType::RPMMD_e :
739 case RepoType::YAST2_e :
741 string cmd = "repo2solv.sh \"";
742 cmd += rawpath.asString() + "\" > " + solvfile.asString();
743 int ret = system (cmd.c_str());
744 if (WIFEXITED (ret) && WEXITSTATUS (ret) != 0)
745 ZYPP_THROW(RepoUnknownTypeException());
752 switch ( repokind.toEnum() )
754 case RepoType::RPMMD_e :
757 CombinedProgressData subprogrcv( progress, 100);
758 parser::yum::RepoParser parser(id, store, parser::yum::RepoParserOpts(), subprogrcv);
759 parser.parse(rawpath);
763 case RepoType::YAST2_e :
766 CombinedProgressData subprogrcv( progress, 100);
767 parser::susetags::RepoParser parser(id, store, subprogrcv);
768 parser.parse(rawpath);
774 case RepoType::RPMPLAINDIR_e :
776 CombinedProgressData subprogrcv( progress, 100);
777 InputStream is(rawpath + "cookie");
779 getline( is.stream(), buffer);
781 parser::plaindir::RepoParser parser(id, store, subprogrcv);
782 parser.parse(url.getPathName());
787 ZYPP_THROW(RepoUnknownTypeException());
790 // update timestamp and checksum
791 //store.updateRepositoryStatus(id, raw_metadata_status);
792 setCacheStatus(info.alias(), raw_metadata_status);
793 MIL << "Commit cache.." << endl;
798 ////////////////////////////////////////////////////////////////////////////
800 repo::RepoType RepoManager::probe( const Url &url ) const
802 if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName() ).isDir() )
804 // Handle non existing local directory in advance, as
805 // MediaSetAccess does not support it.
806 return repo::RepoType::NONE;
811 MediaSetAccess access(url);
812 if ( access.doesFileExist("/repodata/repomd.xml") )
813 return repo::RepoType::RPMMD;
814 if ( access.doesFileExist("/content") )
815 return repo::RepoType::YAST2;
817 // if it is a local url of type dir
818 if ( (! media::MediaManager::downloads(url)) && ( url.getScheme() == "dir" ) )
820 Pathname path = Pathname(url.getPathName());
821 if ( PathInfo(path).isDir() )
823 // allow empty dirs for now
824 return repo::RepoType::RPMPLAINDIR;
828 catch ( const media::MediaException &e )
831 RepoException enew("Error trying to read from " + url.asString());
835 catch ( const Exception &e )
838 Exception enew("Unknown error reading from " + url.asString());
843 return repo::RepoType::NONE;
846 ////////////////////////////////////////////////////////////////////////////
848 void RepoManager::cleanCache( const RepoInfo &info,
849 const ProgressData::ReceiverFnc & progressrcv )
851 Pathname name = _pimpl->options.repoCachePath;
852 name += info.alias() + ".solv";
856 ////////////////////////////////////////////////////////////////////////////
858 bool RepoManager::isCached( const RepoInfo &info ) const
860 Pathname name = _pimpl->options.repoCachePath;
861 return PathInfo(name + Pathname(info.alias()).extend(".solv")).isExist();
864 RepoStatus RepoManager::cacheStatus( const RepoInfo &info ) const
866 Pathname name = _pimpl->options.repoCachePath;
868 Pathname base = _pimpl->options.repoCachePath + Pathname(info.alias());
869 Pathname solvfile = base.extend(".solv");
870 Pathname cookiefile = base.extend(".cookie");
872 std::ifstream file(cookiefile.c_str());
874 ZYPP_THROW (Exception( "Can't open " + cookiefile.asString() ) );
878 while(file && !file.eof()) {
879 getline(file, buffer);
882 std::vector<std::string> words;
883 if ( str::split( buffer, std::back_inserter(words) ) != 2 )
884 ZYPP_THROW (Exception( "corrupt file " + cookiefile.asString() ) );
886 status.setTimestamp(Date(str::strtonum<time_t>(words[1])));
887 status.setChecksum(words[0]);
891 void RepoManager::setCacheStatus( const string &alias, const RepoStatus &status )
893 Pathname base = _pimpl->options.repoCachePath + alias;
894 Pathname cookiefile = base.extend(".cookie");
896 std::ofstream file(cookiefile.c_str());
898 ZYPP_THROW (Exception( "Can't open " + cookiefile.asString() ) );
904 map<data::RecordId, Repo *> repo2solv;
906 Repository RepoManager::createFromCache( const RepoInfo &info,
907 const ProgressData::ReceiverFnc & progressrcv )
909 callback::SendReport<ProgressReport> report;
910 ProgressData progress;
911 progress.sendTo(ProgressReportAdaptor( progressrcv, report ));
912 //progress.sendTo( progressrcv );
913 progress.name(str::form(_("Reading repository '%s' cache"), info.name().c_str()));
915 //_pimpl->options.repoCachePath
916 if ( ! isCached( info ) )
917 ZYPP_THROW(RepoNotCachedException());
919 MIL << "Repository " << info.alias() << " is cached" << endl;
921 //sat::Pool satpool( sat::Pool::instance() );
923 //Pathname name = _pimpl->options.repoCachePath + Pathname(info.alias()).extend(".solv");
927 //satpool.addRepoSolv(name);
929 catch ( const Exception &e )
934 CombinedProgressData subprogrcv(progress);
936 // repo::cached::RepoOptions opts( info, _pimpl->options.repoCachePath, id );
937 // opts.readingResolvablesProgress = subprogrcv;
938 // //opts.repo = repo;
939 // repo::cached::RepoImpl::Ptr repoimpl =
940 // new repo::cached::RepoImpl( opts );
942 // repoimpl->resolvables();
943 // read the resolvables from cache
944 return Repository::noRepository;
947 ////////////////////////////////////////////////////////////////////////////
950 * Generate a non existing filename in a directory, using a base
951 * name. For example if a directory contains 3 files
957 * If you try to generate a unique filename for this directory,
958 * based on "ruu" you will get "ruu", but if you use the base
959 * "foo" you will get "foo_1"
961 * \param dir Directory where the file needs to be unique
962 * \param basefilename string to base the filename on.
964 static Pathname generate_non_existing_name( const Pathname &dir,
965 const std::string &basefilename )
967 string final_filename = basefilename;
969 while ( PathInfo(dir + final_filename).isExist() )
971 final_filename = basefilename + "_" + str::numstring(counter);
974 return dir + Pathname(final_filename);
977 ////////////////////////////////////////////////////////////////////////////
980 * \short Generate a related filename from a repo info
982 * From a repo info, it will try to use the alias as a filename
983 * escaping it if necessary. Other fallbacks can be added to
984 * this function in case there is no way to use the alias
986 static std::string generate_filename( const RepoInfo &info )
990 std::string filename = info.alias();
991 // replace slashes with underscores
992 size_t pos = filename.find(fnd);
993 while(pos!=string::npos)
995 filename.replace(pos,fnd.length(),rep);
996 pos = filename.find(fnd,pos+rep.length());
998 filename = Pathname(filename).extend(".repo").asString();
999 MIL << "generating filename for repo [" << info.alias() << "] : '" << filename << "'" << endl;
1004 ////////////////////////////////////////////////////////////////////////////
1006 void RepoManager::addRepository( const RepoInfo &info,
1007 const ProgressData::ReceiverFnc & progressrcv )
1011 ProgressData progress(100);
1012 callback::SendReport<ProgressReport> report;
1013 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1014 progress.name(str::form(_("Adding repository '%s'"), info.name().c_str()));
1017 std::list<RepoInfo> repos = knownRepositories();
1018 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1022 if ( info.alias() == (*it).alias() )
1023 ZYPP_THROW(RepoAlreadyExistsException(info.alias()));
1026 RepoInfo tosave = info;
1028 // check the first url for now
1029 if ( ZConfig::instance().repo_add_probe()
1030 || ( tosave.type() == RepoType::NONE && tosave.enabled()) )
1032 DBG << "unknown repository type, probing" << endl;
1034 RepoType probedtype;
1035 probedtype = probe(*tosave.baseUrlsBegin());
1036 if ( tosave.baseUrlsSize() > 0 )
1038 if ( probedtype == RepoType::NONE )
1039 ZYPP_THROW(RepoUnknownTypeException());
1041 tosave.setType(probedtype);
1047 // assert the directory exists
1048 filesystem::assert_dir(_pimpl->options.knownReposPath);
1050 Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath,
1051 generate_filename(tosave));
1052 // now we have a filename that does not exists
1053 MIL << "Saving repo in " << repofile << endl;
1055 std::ofstream file(repofile.c_str());
1057 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1060 tosave.dumpRepoOn(file);
1062 MIL << "done" << endl;
1065 void RepoManager::addRepositories( const Url &url,
1066 const ProgressData::ReceiverFnc & progressrcv )
1068 std::list<RepoInfo> knownrepos = knownRepositories();
1069 std::list<RepoInfo> repos = readRepoFile(url);
1070 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1074 // look if the alias is in the known repos.
1075 for ( std::list<RepoInfo>::const_iterator kit = knownrepos.begin();
1076 kit != knownrepos.end();
1079 if ( (*it).alias() == (*kit).alias() )
1081 ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1082 ZYPP_THROW(RepoAlreadyExistsException((*it).alias()));
1087 string filename = Pathname(url.getPathName()).basename();
1089 if ( filename == Pathname() )
1090 ZYPP_THROW(RepoException("Invalid repo file name at " + url.asString() ));
1092 // assert the directory exists
1093 filesystem::assert_dir(_pimpl->options.knownReposPath);
1095 Pathname repofile = generate_non_existing_name(_pimpl->options.knownReposPath, filename);
1096 // now we have a filename that does not exists
1097 MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1099 std::ofstream file(repofile.c_str());
1101 ZYPP_THROW (Exception( "Can't open " + repofile.asString() ) );
1104 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1108 MIL << "Saving " << (*it).alias() << endl;
1109 (*it).dumpRepoOn(file);
1111 MIL << "done" << endl;
1114 ////////////////////////////////////////////////////////////////////////////
1116 void RepoManager::removeRepository( const RepoInfo & info,
1117 const ProgressData::ReceiverFnc & progressrcv)
1119 ProgressData progress;
1120 callback::SendReport<ProgressReport> report;
1121 progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1122 progress.name(str::form(_("Removing repository '%s'"), info.name().c_str()));
1124 MIL << "Going to delete repo " << info.alias() << endl;
1126 std::list<RepoInfo> repos = knownRepositories();
1127 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1131 // they can be the same only if the provided is empty, that means
1132 // the provided repo has no alias
1134 if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1137 // TODO match by url
1139 // we have a matcing repository, now we need to know
1140 // where it does come from.
1141 RepoInfo todelete = *it;
1142 if (todelete.filepath().empty())
1144 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1148 // figure how many repos are there in the file:
1149 std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1150 if ( (filerepos.size() == 1) && ( filerepos.front().alias() == todelete.alias() ) )
1152 // easy, only this one, just delete the file
1153 if ( filesystem::unlink(todelete.filepath()) != 0 )
1155 ZYPP_THROW(RepoException("Can't delete " + todelete.filepath().asString()));
1157 MIL << todelete.alias() << " sucessfully deleted." << endl;
1161 // there are more repos in the same file
1162 // write them back except the deleted one.
1164 //std::ofstream file(tmp.path().c_str());
1166 // assert the directory exists
1167 filesystem::assert_dir(todelete.filepath().dirname());
1169 std::ofstream file(todelete.filepath().c_str());
1171 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1172 ZYPP_THROW (Exception( "Can't open " + todelete.filepath().asString() ) );
1174 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1175 fit != filerepos.end();
1178 if ( (*fit).alias() != todelete.alias() )
1179 (*fit).dumpRepoOn(file);
1183 CombinedProgressData subprogrcv(progress, 70);
1184 CombinedProgressData cleansubprogrcv(progress, 30);
1185 // now delete it from cache
1186 cleanCache( todelete, subprogrcv);
1187 // now delete metadata (#301037)
1188 cleanMetadata( todelete, cleansubprogrcv);
1189 MIL << todelete.alias() << " sucessfully deleted." << endl;
1191 } // else filepath is empty
1194 // should not be reached on a sucess workflow
1195 ZYPP_THROW(RepoNotFoundException(info));
1198 ////////////////////////////////////////////////////////////////////////////
1200 void RepoManager::modifyRepository( const std::string &alias,
1201 const RepoInfo & newinfo,
1202 const ProgressData::ReceiverFnc & progressrcv )
1204 RepoInfo toedit = getRepositoryInfo(alias);
1206 if (toedit.filepath().empty())
1208 ZYPP_THROW(RepoException("Can't figure where the repo is stored"));
1212 // figure how many repos are there in the file:
1213 std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1215 // there are more repos in the same file
1216 // write them back except the deleted one.
1218 //std::ofstream file(tmp.path().c_str());
1220 // assert the directory exists
1221 filesystem::assert_dir(toedit.filepath().dirname());
1223 std::ofstream file(toedit.filepath().c_str());
1225 //ZYPP_THROW (Exception( "Can't open " + tmp.path().asString() ) );
1226 ZYPP_THROW (Exception( "Can't open " + toedit.filepath().asString() ) );
1228 for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1229 fit != filerepos.end();
1232 // if the alias is different, dump the original
1233 // if it is the same, dump the provided one
1234 if ( (*fit).alias() != toedit.alias() )
1235 (*fit).dumpRepoOn(file);
1237 newinfo.dumpRepoOn(file);
1242 ////////////////////////////////////////////////////////////////////////////
1244 RepoInfo RepoManager::getRepositoryInfo( const std::string &alias,
1245 const ProgressData::ReceiverFnc & progressrcv )
1247 std::list<RepoInfo> repos = knownRepositories();
1248 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1252 if ( (*it).alias() == alias )
1256 info.setAlias(info.alias());
1257 ZYPP_THROW(RepoNotFoundException(info));
1260 ////////////////////////////////////////////////////////////////////////////
1262 RepoInfo RepoManager::getRepositoryInfo( const Url & url,
1263 const url::ViewOption & urlview,
1264 const ProgressData::ReceiverFnc & progressrcv )
1266 std::list<RepoInfo> repos = knownRepositories();
1267 for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1271 for(RepoInfo::urls_const_iterator urlit = (*it).baseUrlsBegin();
1272 urlit != (*it).baseUrlsEnd();
1275 if ((*urlit).asString(urlview) == url.asString(urlview))
1280 info.setAlias(info.alias());
1281 info.setBaseUrl(url);
1282 ZYPP_THROW(RepoNotFoundException(info));
1285 ////////////////////////////////////////////////////////////////////////////
1287 std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
1289 return str << *obj._pimpl;
1292 /////////////////////////////////////////////////////////////////
1294 ///////////////////////////////////////////////////////////////////