libzypp  14.48.5
RepoManager.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include <cstdlib>
14 #include <iostream>
15 #include <fstream>
16 #include <sstream>
17 #include <list>
18 #include <map>
19 #include <algorithm>
20 
21 #include "zypp/base/InputStream.h"
22 #include "zypp/base/LogTools.h"
23 #include "zypp/base/Gettext.h"
25 #include "zypp/base/Function.h"
26 #include "zypp/base/Regex.h"
27 #include "zypp/PathInfo.h"
28 #include "zypp/TmpPath.h"
29 
30 #include "zypp/ServiceInfo.h"
32 #include "zypp/RepoManager.h"
33 
36 #include "zypp/MediaSetAccess.h"
37 #include "zypp/ExternalProgram.h"
38 #include "zypp/ManagedFile.h"
39 
42 #include "zypp/repo/ServiceRepos.h"
46 
47 #include "zypp/Target.h" // for Target::targetDistribution() for repo index services
48 #include "zypp/ZYppFactory.h" // to get the Target from ZYpp instance
49 #include "zypp/HistoryLog.h" // to write history :O)
50 
51 #include "zypp/ZYppCallbacks.h"
52 
53 #include "sat/Pool.h"
54 
55 using std::endl;
56 using std::string;
57 using namespace zypp::repo;
58 
59 #define OPT_PROGRESS const ProgressData::ReceiverFnc & = ProgressData::ReceiverFnc()
60 
62 namespace zypp
63 {
65  namespace
66  {
88  class UrlCredentialExtractor
89  {
90  public:
91  UrlCredentialExtractor( Pathname & root_r )
92  : _root( root_r )
93  {}
94 
95  ~UrlCredentialExtractor()
96  { if ( _cmPtr ) _cmPtr->save(); }
97 
99  bool collect( const Url & url_r )
100  {
101  bool ret = url_r.hasCredentialsInAuthority();
102  if ( ret )
103  {
104  if ( !_cmPtr ) _cmPtr.reset( new media::CredentialManager( _root ) );
105  _cmPtr->addUserCred( url_r );
106  }
107  return ret;
108  }
110  template<class TContainer>
111  bool collect( const TContainer & urls_r )
112  { bool ret = false; for ( const Url & url : urls_r ) { if ( collect( url ) && !ret ) ret = true; } return ret; }
113 
115  bool extract( Url & url_r )
116  {
117  bool ret = collect( url_r );
118  if ( ret )
119  url_r.setPassword( std::string() );
120  return ret;
121  }
123  template<class TContainer>
124  bool extract( TContainer & urls_r )
125  { bool ret = false; for ( Url & url : urls_r ) { if ( extract( url ) && !ret ) ret = true; } return ret; }
126 
127  private:
128  const Pathname & _root;
129  scoped_ptr<media::CredentialManager> _cmPtr;
130  };
131  } // namespace
133 
135  namespace
136  {
140  class MediaMounter
141  {
142  public:
144  MediaMounter( const Url & url_r )
145  {
146  media::MediaManager mediamanager;
147  _mid = mediamanager.open( url_r );
148  mediamanager.attach( _mid );
149  }
150 
152  ~MediaMounter()
153  {
154  media::MediaManager mediamanager;
155  mediamanager.release( _mid );
156  mediamanager.close( _mid );
157  }
158 
163  Pathname getPathName( const Pathname & path_r = Pathname() ) const
164  {
165  media::MediaManager mediamanager;
166  return mediamanager.localPath( _mid, path_r );
167  }
168 
169  private:
171  };
173 
175  template <class Iterator>
176  inline bool foundAliasIn( const std::string & alias_r, Iterator begin_r, Iterator end_r )
177  {
178  for_( it, begin_r, end_r )
179  if ( it->alias() == alias_r )
180  return true;
181  return false;
182  }
184  template <class Container>
185  inline bool foundAliasIn( const std::string & alias_r, const Container & cont_r )
186  { return foundAliasIn( alias_r, cont_r.begin(), cont_r.end() ); }
187 
189  template <class Iterator>
190  inline Iterator findAlias( const std::string & alias_r, Iterator begin_r, Iterator end_r )
191  {
192  for_( it, begin_r, end_r )
193  if ( it->alias() == alias_r )
194  return it;
195  return end_r;
196  }
198  template <class Container>
199  inline typename Container::iterator findAlias( const std::string & alias_r, Container & cont_r )
200  { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
202  template <class Container>
203  inline typename Container::const_iterator findAlias( const std::string & alias_r, const Container & cont_r )
204  { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
205 
206 
208  inline std::string filenameFromAlias( const std::string & alias_r, const std::string & stem_r )
209  {
210  std::string filename( alias_r );
211  // replace slashes with underscores
212  str::replaceAll( filename, "/", "_" );
213 
214  filename = Pathname(filename).extend("."+stem_r).asString();
215  MIL << "generating filename for " << stem_r << " [" << alias_r << "] : '" << filename << "'" << endl;
216  return filename;
217  }
218 
234  struct RepoCollector : private base::NonCopyable
235  {
236  RepoCollector()
237  {}
238 
239  RepoCollector(const std::string & targetDistro_)
240  : targetDistro(targetDistro_)
241  {}
242 
243  bool collect( const RepoInfo &repo )
244  {
245  // skip repositories meant for other distros than specified
246  if (!targetDistro.empty()
247  && !repo.targetDistribution().empty()
248  && repo.targetDistribution() != targetDistro)
249  {
250  MIL
251  << "Skipping repository meant for '" << repo.targetDistribution()
252  << "' distribution (current distro is '"
253  << targetDistro << "')." << endl;
254 
255  return true;
256  }
257 
258  repos.push_back(repo);
259  return true;
260  }
261 
262  RepoInfoList repos;
263  std::string targetDistro;
264  };
266 
272  std::list<RepoInfo> repositories_in_file( const Pathname & file )
273  {
274  MIL << "repo file: " << file << endl;
275  RepoCollector collector;
276  parser::RepoFileReader parser( file, bind( &RepoCollector::collect, &collector, _1 ) );
277  return std::move(collector.repos);
278  }
279 
281 
290  std::list<RepoInfo> repositories_in_dir( const Pathname &dir )
291  {
292  MIL << "directory " << dir << endl;
293  std::list<RepoInfo> repos;
294  bool nonroot( geteuid() != 0 );
295  if ( nonroot && ! PathInfo(dir).userMayRX() )
296  {
297  JobReport::warning( str::FormatNAC(_("Cannot read repo directory '%1%': Permission denied")) % dir );
298  }
299  else
300  {
301  std::list<Pathname> entries;
302  if ( filesystem::readdir( entries, dir, false ) != 0 )
303  {
304  // TranslatorExplanation '%s' is a pathname
305  ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir.c_str())));
306  }
307 
308  str::regex allowedRepoExt("^\\.repo(_[0-9]+)?$");
309  for ( std::list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
310  {
311  if ( str::regex_match(it->extension(), allowedRepoExt) )
312  {
313  if ( nonroot && ! PathInfo(*it).userMayR() )
314  {
315  JobReport::warning( str::FormatNAC(_("Cannot read repo file '%1%': Permission denied")) % *it );
316  }
317  else
318  {
319  const std::list<RepoInfo> & tmp( repositories_in_file( *it ) );
320  repos.insert( repos.end(), tmp.begin(), tmp.end() );
321  }
322  }
323  }
324  }
325  return repos;
326  }
327 
329 
330  inline void assert_alias( const RepoInfo & info )
331  {
332  if ( info.alias().empty() )
333  ZYPP_THROW( RepoNoAliasException( info ) );
334  // bnc #473834. Maybe we can match the alias against a regex to define
335  // and check for valid aliases
336  if ( info.alias()[0] == '.')
338  info, _("Repository alias cannot start with dot.")));
339  }
340 
341  inline void assert_alias( const ServiceInfo & info )
342  {
343  if ( info.alias().empty() )
345  // bnc #473834. Maybe we can match the alias against a regex to define
346  // and check for valid aliases
347  if ( info.alias()[0] == '.')
349  info, _("Service alias cannot start with dot.")));
350  }
351 
353 
354  inline void assert_urls( const RepoInfo & info )
355  {
356  if ( info.baseUrlsEmpty() )
357  ZYPP_THROW( RepoNoUrlException( info ) );
358  }
359 
360  inline void assert_url( const ServiceInfo & info )
361  {
362  if ( ! info.url().isValid() )
364  }
365 
367 
372  inline Pathname rawcache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
373  {
374  assert_alias(info);
375  return opt.repoRawCachePath / info.escaped_alias();
376  }
377 
386  inline Pathname rawproductdata_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
387  {
388  assert_alias(info);
389  return opt.repoRawCachePath / info.escaped_alias() / info.path();
390  }
391 
395  inline Pathname packagescache_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
396  {
397  assert_alias(info);
398  return opt.repoPackagesCachePath / info.escaped_alias();
399  }
400 
404  inline Pathname solv_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info)
405  {
406  assert_alias(info);
407  return opt.repoSolvCachePath / info.escaped_alias();
408  }
409 
411 
413  class ServiceCollector
414  {
415  public:
416  typedef std::set<ServiceInfo> ServiceSet;
417 
418  ServiceCollector( ServiceSet & services_r )
419  : _services( services_r )
420  {}
421 
422  bool operator()( const ServiceInfo & service_r ) const
423  {
424  _services.insert( service_r );
425  return true;
426  }
427 
428  private:
429  ServiceSet & _services;
430  };
432 
433  } // namespace
435 
436  std::list<RepoInfo> readRepoFile( const Url & repo_file )
437  {
438  // no interface to download a specific file, using workaround:
440  Url url(repo_file);
441  Pathname path(url.getPathName());
442  url.setPathName ("/");
443  MediaSetAccess access(url);
444  Pathname local = access.provideFile(path);
445 
446  DBG << "reading repo file " << repo_file << ", local path: " << local << endl;
447 
448  return repositories_in_file(local);
449  }
450 
452  //
453  // class RepoManagerOptions
454  //
456 
457  RepoManagerOptions::RepoManagerOptions( const Pathname & root_r )
458  {
459  repoCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoCachePath() );
460  repoRawCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoMetadataPath() );
461  repoSolvCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoSolvfilesPath() );
462  repoPackagesCachePath = Pathname::assertprefix( root_r, ZConfig::instance().repoPackagesPath() );
463  knownReposPath = Pathname::assertprefix( root_r, ZConfig::instance().knownReposPath() );
464  knownServicesPath = Pathname::assertprefix( root_r, ZConfig::instance().knownServicesPath() );
465  pluginsPath = Pathname::assertprefix( root_r, ZConfig::instance().pluginsPath() );
466  probe = ZConfig::instance().repo_add_probe();
467 
468  rootDir = root_r;
469  }
470 
472  {
473  RepoManagerOptions ret;
474  ret.repoCachePath = root_r;
475  ret.repoRawCachePath = root_r/"raw";
476  ret.repoSolvCachePath = root_r/"solv";
477  ret.repoPackagesCachePath = root_r/"packages";
478  ret.knownReposPath = root_r/"repos.d";
479  ret.knownServicesPath = root_r/"services.d";
480  ret.pluginsPath = root_r/"plugins";
481  ret.rootDir = root_r;
482  return ret;
483  }
484 
485  std:: ostream & operator<<( std::ostream & str, const RepoManagerOptions & obj )
486  {
487 #define OUTS(X) str << " " #X "\t" << obj.X << endl
488  str << "RepoManagerOptions (" << obj.rootDir << ") {" << endl;
489  OUTS( repoRawCachePath );
490  OUTS( repoSolvCachePath );
491  OUTS( repoPackagesCachePath );
492  OUTS( knownReposPath );
493  OUTS( knownServicesPath );
494  OUTS( pluginsPath );
495  str << "}" << endl;
496 #undef OUTS
497  return str;
498  }
499 
506  {
507  public:
508  Impl( const RepoManagerOptions &opt )
509  : _options(opt)
510  {
511  init_knownServices();
512  init_knownRepositories();
513  }
514 
516  {
517  // trigger appdata refresh if some repos change
518  if ( _reposDirty && geteuid() == 0 && ( _options.rootDir.empty() || _options.rootDir == "/" ) )
519  {
520  try {
521  std::list<Pathname> entries;
522  filesystem::readdir( entries, _options.pluginsPath/"appdata", false );
523  if ( ! entries.empty() )
524  {
526  cmd.push_back( "<" ); // discard stdin
527  cmd.push_back( ">" ); // discard stdout
528  cmd.push_back( "PROGRAM" ); // [2] - fix index below if changing!
529  for ( const auto & rinfo : repos() )
530  {
531  if ( ! rinfo.enabled() )
532  continue;
533  cmd.push_back( "-R" );
534  cmd.push_back( rinfo.alias() );
535  cmd.push_back( "-t" );
536  cmd.push_back( rinfo.type().asString() );
537  cmd.push_back( "-p" );
538  cmd.push_back( rinfo.metadataPath().asString() );
539  }
540 
541  for_( it, entries.begin(), entries.end() )
542  {
543  PathInfo pi( *it );
544  //DBG << "/tmp/xx ->" << pi << endl;
545  if ( pi.isFile() && pi.userMayRX() )
546  {
547  // trigger plugin
548  cmd[2] = pi.asString(); // [2] - PROGRAM
550  }
551  }
552  }
553  }
554  catch (...) {} // no throw in dtor
555  }
556  }
557 
558  public:
559  bool repoEmpty() const { return repos().empty(); }
560  RepoSizeType repoSize() const { return repos().size(); }
561  RepoConstIterator repoBegin() const { return repos().begin(); }
562  RepoConstIterator repoEnd() const { return repos().end(); }
563 
564  bool hasRepo( const std::string & alias ) const
565  { return foundAliasIn( alias, repos() ); }
566 
567  RepoInfo getRepo( const std::string & alias ) const
568  {
569  RepoConstIterator it( findAlias( alias, repos() ) );
570  return it == repos().end() ? RepoInfo::noRepo : *it;
571  }
572 
573  public:
574  Pathname metadataPath( const RepoInfo & info ) const
575  { return rawcache_path_for_repoinfo( _options, info ); }
576 
577  Pathname packagesPath( const RepoInfo & info ) const
578  { return packagescache_path_for_repoinfo( _options, info ); }
579 
580  RepoStatus metadataStatus( const RepoInfo & info ) const;
581 
582  RefreshCheckStatus checkIfToRefreshMetadata( const RepoInfo & info, const Url & url, RawMetadataRefreshPolicy policy );
583 
584  void refreshMetadata( const RepoInfo & info, RawMetadataRefreshPolicy policy, OPT_PROGRESS );
585 
586  void cleanMetadata( const RepoInfo & info, OPT_PROGRESS );
587 
588  void cleanPackages( const RepoInfo & info, OPT_PROGRESS );
589 
590  void buildCache( const RepoInfo & info, CacheBuildPolicy policy, OPT_PROGRESS );
591 
592  repo::RepoType probe( const Url & url, const Pathname & path = Pathname() ) const;
593  repo::RepoType probeCache( const Pathname & path_r ) const;
594 
595  void cleanCacheDirGarbage( OPT_PROGRESS );
596 
597  void cleanCache( const RepoInfo & info, OPT_PROGRESS );
598 
599  bool isCached( const RepoInfo & info ) const
600  { return PathInfo(solv_path_for_repoinfo( _options, info ) / "solv").isExist(); }
601 
602  RepoStatus cacheStatus( const RepoInfo & info ) const
603  { return RepoStatus::fromCookieFile(solv_path_for_repoinfo(_options, info) / "cookie"); }
604 
605  void loadFromCache( const RepoInfo & info, OPT_PROGRESS );
606 
607  void addRepository( const RepoInfo & info, OPT_PROGRESS );
608 
609  void addRepositories( const Url & url, OPT_PROGRESS );
610 
611  void removeRepository( const RepoInfo & info, OPT_PROGRESS );
612 
613  void modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, OPT_PROGRESS );
614 
615  RepoInfo getRepositoryInfo( const std::string & alias, OPT_PROGRESS );
616  RepoInfo getRepositoryInfo( const Url & url, const url::ViewOption & urlview, OPT_PROGRESS );
617 
618  public:
619  bool serviceEmpty() const { return _services.empty(); }
620  ServiceSizeType serviceSize() const { return _services.size(); }
621  ServiceConstIterator serviceBegin() const { return _services.begin(); }
622  ServiceConstIterator serviceEnd() const { return _services.end(); }
623 
624  bool hasService( const std::string & alias ) const
625  { return foundAliasIn( alias, _services ); }
626 
627  ServiceInfo getService( const std::string & alias ) const
628  {
629  ServiceConstIterator it( findAlias( alias, _services ) );
630  return it == _services.end() ? ServiceInfo::noService : *it;
631  }
632 
633  public:
634  void addService( const ServiceInfo & service );
635  void addService( const std::string & alias, const Url & url )
636  { addService( ServiceInfo( alias, url ) ); }
637 
638  void removeService( const std::string & alias );
639  void removeService( const ServiceInfo & service )
640  { removeService( service.alias() ); }
641 
642  void refreshServices( const RefreshServiceOptions & options_r );
643 
644  void refreshService( const std::string & alias, const RefreshServiceOptions & options_r );
645  void refreshService( const ServiceInfo & service, const RefreshServiceOptions & options_r )
646  { refreshService( service.alias(), options_r ); }
647 
648  void modifyService( const std::string & oldAlias, const ServiceInfo & newService );
649 
650  repo::ServiceType probeService( const Url & url ) const;
651 
652  private:
653  void saveService( ServiceInfo & service ) const;
654 
655  Pathname generateNonExistingName( const Pathname & dir, const std::string & basefilename ) const;
656 
657  std::string generateFilename( const RepoInfo & info ) const
658  { return filenameFromAlias( info.alias(), "repo" ); }
659 
660  std::string generateFilename( const ServiceInfo & info ) const
661  { return filenameFromAlias( info.alias(), "service" ); }
662 
663  void setCacheStatus( const RepoInfo & info, const RepoStatus & status )
664  {
665  Pathname base = solv_path_for_repoinfo( _options, info );
667  status.saveToCookieFile( base / "cookie" );
668  }
669 
670  void touchIndexFile( const RepoInfo & info );
671 
672  template<typename OutputIterator>
673  void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
674  {
675  MatchServiceAlias filter( alias );
676  std::copy( boost::make_filter_iterator( filter, repos().begin(), repos().end() ),
677  boost::make_filter_iterator( filter, repos().end(), repos().end() ),
678  out);
679  }
680 
681  private:
682  void init_knownServices();
683  void init_knownRepositories();
684 
685  const RepoSet & repos() const { return _reposX; }
686  RepoSet & reposManip() { if ( ! _reposDirty ) _reposDirty = true; return _reposX; }
687 
688  private:
692 
694 
695  private:
696  friend Impl * rwcowClone<Impl>( const Impl * rhs );
698  Impl * clone() const
699  { return new Impl( *this ); }
700  };
702 
704  inline std::ostream & operator<<( std::ostream & str, const RepoManager::Impl & obj )
705  { return str << "RepoManager::Impl"; }
706 
708 
710  {
711  filesystem::assert_dir( _options.knownServicesPath );
712  Pathname servfile = generateNonExistingName( _options.knownServicesPath,
713  generateFilename( service ) );
714  service.setFilepath( servfile );
715 
716  MIL << "saving service in " << servfile << endl;
717 
718  std::ofstream file( servfile.c_str() );
719  if ( !file )
720  {
721  // TranslatorExplanation '%s' is a filename
722  ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), servfile.c_str() )));
723  }
724  service.dumpAsIniOn( file );
725  MIL << "done" << endl;
726  }
727 
743  Pathname RepoManager::Impl::generateNonExistingName( const Pathname & dir,
744  const std::string & basefilename ) const
745  {
746  std::string final_filename = basefilename;
747  int counter = 1;
748  while ( PathInfo(dir + final_filename).isExist() )
749  {
750  final_filename = basefilename + "_" + str::numstring(counter);
751  ++counter;
752  }
753  return dir + Pathname(final_filename);
754  }
755 
757 
759  {
760  Pathname dir = _options.knownServicesPath;
761  std::list<Pathname> entries;
762  if (PathInfo(dir).isExist())
763  {
764  if ( filesystem::readdir( entries, dir, false ) != 0 )
765  {
766  // TranslatorExplanation '%s' is a pathname
767  ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir.c_str())));
768  }
769 
770  //str::regex allowedServiceExt("^\\.service(_[0-9]+)?$");
771  for_(it, entries.begin(), entries.end() )
772  {
773  parser::ServiceFileReader(*it, ServiceCollector(_services));
774  }
775  }
776 
777  repo::PluginServices(_options.pluginsPath/"services", ServiceCollector(_services));
778  }
779 
781  namespace {
787  inline void cleanupNonRepoMetadtaFolders( const Pathname & cachePath_r,
788  const Pathname & defaultCachePath_r,
789  const std::list<std::string> & repoEscAliases_r )
790  {
791  if ( cachePath_r != defaultCachePath_r )
792  return;
793 
794  std::list<std::string> entries;
795  if ( filesystem::readdir( entries, cachePath_r, false ) == 0 )
796  {
797  entries.sort();
798  std::set<std::string> oldfiles;
799  set_difference( entries.begin(), entries.end(), repoEscAliases_r.begin(), repoEscAliases_r.end(),
800  std::inserter( oldfiles, oldfiles.end() ) );
801  for ( const std::string & old : oldfiles )
802  {
803  if ( old == Repository::systemRepoAlias() ) // don't remove the @System solv file
804  continue;
805  filesystem::recursive_rmdir( cachePath_r / old );
806  }
807  }
808  }
809  } // namespace
812  {
813  MIL << "start construct known repos" << endl;
814 
815  if ( PathInfo(_options.knownReposPath).isExist() )
816  {
817  std::list<std::string> repoEscAliases;
818  std::list<RepoInfo> orphanedRepos;
819  for ( RepoInfo & repoInfo : repositories_in_dir(_options.knownReposPath) )
820  {
821  // set the metadata path for the repo
822  repoInfo.setMetadataPath( rawcache_path_for_repoinfo(_options, repoInfo) );
823  // set the downloaded packages path for the repo
824  repoInfo.setPackagesPath( packagescache_path_for_repoinfo(_options, repoInfo) );
825  // remember it
826  _reposX.insert( repoInfo ); // direct access via _reposX in ctor! no reposManip.
827 
828  // detect orphaned repos belonging to a deleted service
829  const std::string & serviceAlias( repoInfo.service() );
830  if ( ! ( serviceAlias.empty() || hasService( serviceAlias ) ) )
831  {
832  WAR << "Schedule orphaned service repo for deletion: " << repoInfo << endl;
833  orphanedRepos.push_back( repoInfo );
834  continue; // don't remember it in repoEscAliases
835  }
836 
837  repoEscAliases.push_back(repoInfo.escaped_alias());
838  }
839 
840  // Cleanup orphanded service repos:
841  if ( ! orphanedRepos.empty() )
842  {
843  for ( auto & repoInfo : orphanedRepos )
844  {
845  MIL << "Delete orphaned service repo " << repoInfo.alias() << endl;
846  // translators: Cleanup a repository previously owned by a meanwhile unknown (deleted) service.
847  // %1% = service name
848  // %2% = repository name
849  JobReport::warning( str::FormatNAC(_("Unknown service '%1%': Removing orphaned service repository '%2%'"))
850  % repoInfo.service()
851  % repoInfo.alias() );
852  try {
853  removeRepository( repoInfo );
854  }
855  catch ( const Exception & caugth )
856  {
857  JobReport::error( caugth.asUserHistory() );
858  }
859  }
860  }
861 
862  // delete metadata folders without corresponding repo (e.g. old tmp directories)
863  //
864  // bnc#891515: Auto-cleanup only zypp.conf default locations. Otherwise
865  // we'd need somemagic file to identify zypp cache directories. Without this
866  // we may easily remove user data (zypper --pkg-cache-dir . download ...)
867  repoEscAliases.sort();
868  RepoManagerOptions defaultCache( _options.rootDir );
869  cleanupNonRepoMetadtaFolders( _options.repoRawCachePath, defaultCache.repoRawCachePath, repoEscAliases );
870  cleanupNonRepoMetadtaFolders( _options.repoSolvCachePath, defaultCache.repoSolvCachePath, repoEscAliases );
871  cleanupNonRepoMetadtaFolders( _options.repoPackagesCachePath, defaultCache.repoPackagesCachePath, repoEscAliases );
872  }
873  MIL << "end construct known repos" << endl;
874  }
875 
877 
879  {
880  Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
881  Pathname productdatapath = rawproductdata_path_for_repoinfo( _options, info );
882 
883  RepoType repokind = info.type();
884  // If unknown, probe the local metadata
885  if ( repokind == RepoType::NONE )
886  repokind = probeCache( productdatapath );
887 
888  RepoStatus status;
889  switch ( repokind.toEnum() )
890  {
891  case RepoType::RPMMD_e :
892  status = RepoStatus( productdatapath/"repodata/repomd.xml");
893  break;
894 
895  case RepoType::YAST2_e :
896  status = RepoStatus( productdatapath/"content" ) && RepoStatus( mediarootpath/"media.1/media" );
897  break;
898 
900  status = RepoStatus::fromCookieFile( productdatapath/"cookie" );
901  break;
902 
903  case RepoType::NONE_e :
904  // Return default RepoStatus in case of RepoType::NONE
905  // indicating it should be created?
906  // ZYPP_THROW(RepoUnknownTypeException());
907  break;
908  }
909  return status;
910  }
911 
912 
914  {
915  Pathname productdatapath = rawproductdata_path_for_repoinfo( _options, info );
916 
917  RepoType repokind = info.type();
918  if ( repokind.toEnum() == RepoType::NONE_e )
919  // unknown, probe the local metadata
920  repokind = probeCache( productdatapath );
921  // if still unknown, just return
922  if (repokind == RepoType::NONE_e)
923  return;
924 
925  Pathname p;
926  switch ( repokind.toEnum() )
927  {
928  case RepoType::RPMMD_e :
929  p = Pathname(productdatapath + "/repodata/repomd.xml");
930  break;
931 
932  case RepoType::YAST2_e :
933  p = Pathname(productdatapath + "/content");
934  break;
935 
937  p = Pathname(productdatapath + "/cookie");
938  break;
939 
940  case RepoType::NONE_e :
941  default:
942  break;
943  }
944 
945  // touch the file, ignore error (they are logged anyway)
947  }
948 
949 
951  {
952  assert_alias(info);
953  try
954  {
955  MIL << "Going to try to check whether refresh is needed for " << url << " (" << info.type() << ")" << endl;
956 
957  // first check old (cached) metadata
958  Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
959  filesystem::assert_dir( mediarootpath );
960  RepoStatus oldstatus = metadataStatus( info );
961 
962  if ( oldstatus.empty() )
963  {
964  MIL << "No cached metadata, going to refresh" << endl;
965  return REFRESH_NEEDED;
966  }
967 
968  {
969  if ( url.schemeIsVolatile() )
970  {
971  MIL << "never refresh CD/DVD" << endl;
972  return REPO_UP_TO_DATE;
973  }
974  if ( url.schemeIsLocal() )
975  {
976  policy = RefreshIfNeededIgnoreDelay;
977  }
978  }
979 
980  // now we've got the old (cached) status, we can decide repo.refresh.delay
981  if (policy != RefreshForced && policy != RefreshIfNeededIgnoreDelay)
982  {
983  // difference in seconds
984  double diff = difftime(
986  (Date::ValueType)oldstatus.timestamp()) / 60;
987 
988  DBG << "oldstatus: " << (Date::ValueType)oldstatus.timestamp() << endl;
989  DBG << "current time: " << (Date::ValueType)Date::now() << endl;
990  DBG << "last refresh = " << diff << " minutes ago" << endl;
991 
992  if ( diff < ZConfig::instance().repo_refresh_delay() )
993  {
994  if ( diff < 0 )
995  {
996  WAR << "Repository '" << info.alias() << "' was refreshed in the future!" << endl;
997  }
998  else
999  {
1000  MIL << "Repository '" << info.alias()
1001  << "' has been refreshed less than repo.refresh.delay ("
1003  << ") minutes ago. Advising to skip refresh" << endl;
1004  return REPO_CHECK_DELAYED;
1005  }
1006  }
1007  }
1008 
1009  repo::RepoType repokind = info.type();
1010  // if unknown: probe it
1011  if ( repokind == RepoType::NONE )
1012  repokind = probe( url, info.path() );
1013 
1014  // retrieve newstatus
1015  RepoStatus newstatus;
1016  switch ( repokind.toEnum() )
1017  {
1018  case RepoType::RPMMD_e:
1019  {
1020  MediaSetAccess media( url );
1021  newstatus = yum::Downloader( info, mediarootpath ).status( media );
1022  }
1023  break;
1024 
1025  case RepoType::YAST2_e:
1026  {
1027  MediaSetAccess media( url );
1028  newstatus = susetags::Downloader( info, mediarootpath ).status( media );
1029  }
1030  break;
1031 
1033  newstatus = RepoStatus( MediaMounter(url).getPathName(info.path()) ); // dir status
1034  break;
1035 
1036  default:
1037  case RepoType::NONE_e:
1039  break;
1040  }
1041 
1042  // check status
1043  bool refresh = false;
1044  if ( oldstatus == newstatus )
1045  {
1046  MIL << "repo has not changed" << endl;
1047  if ( policy == RefreshForced )
1048  {
1049  MIL << "refresh set to forced" << endl;
1050  refresh = true;
1051  }
1052  }
1053  else // includes newstatus.empty() if e.g. repo format changed
1054  {
1055  MIL << "repo has changed, going to refresh" << endl;
1056  refresh = true;
1057  }
1058 
1059  if (!refresh)
1060  touchIndexFile(info);
1061 
1062  return refresh ? REFRESH_NEEDED : REPO_UP_TO_DATE;
1063 
1064  }
1065  catch ( const Exception &e )
1066  {
1067  ZYPP_CAUGHT(e);
1068  ERR << "refresh check failed for " << url << endl;
1069  ZYPP_RETHROW(e);
1070  }
1071 
1072  return REFRESH_NEEDED; // default
1073  }
1074 
1075 
1077  {
1078  assert_alias(info);
1079  assert_urls(info);
1080 
1081  // we will throw this later if no URL checks out fine
1082  RepoException rexception( info, _PL("Valid metadata not found at specified URL",
1083  "Valid metadata not found at specified URLs",
1084  info.baseUrlsSize() ) );
1085 
1086  // Suppress (interactive) media::MediaChangeReport if we in have multiple basurls (>1)
1088  // try urls one by one
1089  for ( RepoInfo::urls_const_iterator it = info.baseUrlsBegin(); it != info.baseUrlsEnd(); ++it )
1090  {
1091  try
1092  {
1093  Url url(*it);
1094 
1095  // check whether to refresh metadata
1096  // if the check fails for this url, it throws, so another url will be checked
1097  if (checkIfToRefreshMetadata(info, url, policy)!=REFRESH_NEEDED)
1098  return;
1099 
1100  MIL << "Going to refresh metadata from " << url << endl;
1101 
1102  // bsc#1048315: Always re-probe in case of repo format change.
1103  // TODO: Would be sufficient to verify the type and re-probe
1104  // if verification failed (or type is RepoType::NONE)
1105  repo::RepoType repokind = info.type();
1106  {
1107  repo::RepoType probed = probe( *it, info.path() );
1108  if ( repokind != probed )
1109  {
1110  repokind = probed;
1111  // Adjust the probed type in RepoInfo
1112  info.setProbedType( repokind ); // lazy init!
1113  //save probed type only for repos in system
1114  for_( it, repoBegin(), repoEnd() )
1115  {
1116  if ( info.alias() == (*it).alias() )
1117  {
1118  RepoInfo modifiedrepo = info;
1119  modifiedrepo.setType( repokind );
1120  modifyRepository( info.alias(), modifiedrepo );
1121  break;
1122  }
1123  }
1124  }
1125  }
1126 
1127  Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
1128  if( filesystem::assert_dir(mediarootpath) )
1129  {
1130  Exception ex(str::form( _("Can't create %s"), mediarootpath.c_str()) );
1131  ZYPP_THROW(ex);
1132  }
1133 
1134  // create temp dir as sibling of mediarootpath
1135  filesystem::TmpDir tmpdir( filesystem::TmpDir::makeSibling( mediarootpath ) );
1136  if( tmpdir.path().empty() )
1137  {
1138  Exception ex(_("Can't create metadata cache directory."));
1139  ZYPP_THROW(ex);
1140  }
1141 
1142  if ( ( repokind.toEnum() == RepoType::RPMMD_e ) ||
1143  ( repokind.toEnum() == RepoType::YAST2_e ) )
1144  {
1145  MediaSetAccess media(url);
1146  shared_ptr<repo::Downloader> downloader_ptr;
1147 
1148  MIL << "Creating downloader for [ " << info.alias() << " ]" << endl;
1149 
1150  if ( repokind.toEnum() == RepoType::RPMMD_e )
1151  downloader_ptr.reset(new yum::Downloader(info, mediarootpath));
1152  else
1153  downloader_ptr.reset( new susetags::Downloader(info, mediarootpath) );
1154 
1161  for_( it, repoBegin(), repoEnd() )
1162  {
1163  Pathname cachepath(rawcache_path_for_repoinfo( _options, *it ));
1164  if ( PathInfo(cachepath).isExist() )
1165  downloader_ptr->addCachePath(cachepath);
1166  }
1167 
1168  downloader_ptr->download( media, tmpdir.path() );
1169  }
1170  else if ( repokind.toEnum() == RepoType::RPMPLAINDIR_e )
1171  {
1172  MediaMounter media( url );
1173  RepoStatus newstatus = RepoStatus( media.getPathName( info.path() ) ); // dir status
1174 
1175  Pathname productpath( tmpdir.path() / info.path() );
1176  filesystem::assert_dir( productpath );
1177  newstatus.saveToCookieFile( productpath/"cookie" );
1178  }
1179  else
1180  {
1182  }
1183 
1184  // ok we have the metadata, now exchange
1185  // the contents
1186  filesystem::exchange( tmpdir.path(), mediarootpath );
1187  reposManip(); // remember to trigger appdata refresh
1188 
1189  // we are done.
1190  return;
1191  }
1192  catch ( const Exception &e )
1193  {
1194  ZYPP_CAUGHT(e);
1195  ERR << "Trying another url..." << endl;
1196 
1197  // remember the exception caught for the *first URL*
1198  // if all other URLs fail, the rexception will be thrown with the
1199  // cause of the problem of the first URL remembered
1200  if (it == info.baseUrlsBegin())
1201  rexception.remember(e);
1202  else
1203  rexception.addHistory( e.asUserString() );
1204 
1205  }
1206  } // for every url
1207  ERR << "No more urls..." << endl;
1208  ZYPP_THROW(rexception);
1209  }
1210 
1212 
1213  void RepoManager::Impl::cleanMetadata( const RepoInfo & info, const ProgressData::ReceiverFnc & progressfnc )
1214  {
1215  ProgressData progress(100);
1216  progress.sendTo(progressfnc);
1217 
1218  filesystem::recursive_rmdir(rawcache_path_for_repoinfo(_options, info));
1219  progress.toMax();
1220  }
1221 
1222 
1223  void RepoManager::Impl::cleanPackages( const RepoInfo & info, const ProgressData::ReceiverFnc & progressfnc )
1224  {
1225  ProgressData progress(100);
1226  progress.sendTo(progressfnc);
1227 
1228  filesystem::recursive_rmdir(packagescache_path_for_repoinfo(_options, info));
1229  progress.toMax();
1230  }
1231 
1232 
1233  void RepoManager::Impl::buildCache( const RepoInfo & info, CacheBuildPolicy policy, const ProgressData::ReceiverFnc & progressrcv )
1234  {
1235  assert_alias(info);
1236  Pathname mediarootpath = rawcache_path_for_repoinfo( _options, info );
1237  Pathname productdatapath = rawproductdata_path_for_repoinfo( _options, info );
1238 
1239  if( filesystem::assert_dir(_options.repoCachePath) )
1240  {
1241  Exception ex(str::form( _("Can't create %s"), _options.repoCachePath.c_str()) );
1242  ZYPP_THROW(ex);
1243  }
1244  RepoStatus raw_metadata_status = metadataStatus(info);
1245  if ( raw_metadata_status.empty() )
1246  {
1247  /* if there is no cache at this point, we refresh the raw
1248  in case this is the first time - if it's !autorefresh,
1249  we may still refresh */
1250  refreshMetadata(info, RefreshIfNeeded, progressrcv );
1251  raw_metadata_status = metadataStatus(info);
1252  }
1253 
1254  bool needs_cleaning = false;
1255  if ( isCached( info ) )
1256  {
1257  MIL << info.alias() << " is already cached." << endl;
1258  RepoStatus cache_status = cacheStatus(info);
1259 
1260  if ( cache_status == raw_metadata_status )
1261  {
1262  MIL << info.alias() << " cache is up to date with metadata." << endl;
1263  if ( policy == BuildIfNeeded ) {
1264  return;
1265  }
1266  else {
1267  MIL << info.alias() << " cache rebuild is forced" << endl;
1268  }
1269  }
1270 
1271  needs_cleaning = true;
1272  }
1273 
1274  ProgressData progress(100);
1276  progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1277  progress.name(str::form(_("Building repository '%s' cache"), info.label().c_str()));
1278  progress.toMin();
1279 
1280  if (needs_cleaning)
1281  {
1282  cleanCache(info);
1283  }
1284 
1285  MIL << info.alias() << " building cache..." << info.type() << endl;
1286 
1287  Pathname base = solv_path_for_repoinfo( _options, info);
1288 
1289  if( filesystem::assert_dir(base) )
1290  {
1291  Exception ex(str::form( _("Can't create %s"), base.c_str()) );
1292  ZYPP_THROW(ex);
1293  }
1294 
1295  if( ! PathInfo(base).userMayW() )
1296  {
1297  Exception ex(str::form( _("Can't create cache at %s - no writing permissions."), base.c_str()) );
1298  ZYPP_THROW(ex);
1299  }
1300  Pathname solvfile = base / "solv";
1301 
1302  // do we have type?
1303  repo::RepoType repokind = info.type();
1304 
1305  // if the type is unknown, try probing.
1306  switch ( repokind.toEnum() )
1307  {
1308  case RepoType::NONE_e:
1309  // unknown, probe the local metadata
1310  repokind = probeCache( productdatapath );
1311  break;
1312  default:
1313  break;
1314  }
1315 
1316  MIL << "repo type is " << repokind << endl;
1317 
1318  switch ( repokind.toEnum() )
1319  {
1320  case RepoType::RPMMD_e :
1321  case RepoType::YAST2_e :
1323  {
1324  // Take care we unlink the solvfile on exception
1325  ManagedFile guard( solvfile, filesystem::unlink );
1326  scoped_ptr<MediaMounter> forPlainDirs;
1327 
1329  cmd.push_back( PathInfo( "/usr/bin/repo2solv" ).isFile() ? "repo2solv" : "repo2solv.sh" );
1330  // repo2solv expects -o as 1st arg!
1331  cmd.push_back( "-o" );
1332  cmd.push_back( solvfile.asString() );
1333  cmd.push_back( "-X" ); // autogenerate pattern from pattern-package
1334  cmd.push_back( "-A" ); // autogenerate application pseudo packages
1335 
1336  if ( repokind == RepoType::RPMPLAINDIR )
1337  {
1338  forPlainDirs.reset( new MediaMounter( info.url() ) );
1339  // recusive for plaindir as 2nd arg!
1340  cmd.push_back( "-R" );
1341  // FIXME this does only work form dir: URLs
1342  cmd.push_back( forPlainDirs->getPathName( info.path() ).c_str() );
1343  }
1344  else
1345  cmd.push_back( productdatapath.asString() );
1346 
1348  std::string errdetail;
1349 
1350  for ( std::string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
1351  WAR << " " << output;
1352  if ( errdetail.empty() ) {
1353  errdetail = prog.command();
1354  errdetail += '\n';
1355  }
1356  errdetail += output;
1357  }
1358 
1359  int ret = prog.close();
1360  if ( ret != 0 )
1361  {
1362  RepoException ex(str::form( _("Failed to cache repo (%d)."), ret ));
1363  ex.remember( errdetail );
1364  ZYPP_THROW(ex);
1365  }
1366 
1367  // We keep it.
1368  guard.resetDispose();
1369  }
1370  break;
1371  default:
1372  ZYPP_THROW(RepoUnknownTypeException( info, _("Unhandled repository type") ));
1373  break;
1374  }
1375  // update timestamp and checksum
1376  setCacheStatus(info, raw_metadata_status);
1377  MIL << "Commit cache.." << endl;
1378  progress.toMax();
1379  }
1380 
1382 
1383 
1390  repo::RepoType RepoManager::Impl::probe( const Url & url, const Pathname & path ) const
1391  {
1392  MIL << "going to probe the repo type at " << url << " (" << path << ")" << endl;
1393 
1394  if ( url.getScheme() == "dir" && ! PathInfo( url.getPathName()/path ).isDir() )
1395  {
1396  // Handle non existing local directory in advance, as
1397  // MediaSetAccess does not support it.
1398  MIL << "Probed type NONE (not exists) at " << url << " (" << path << ")" << endl;
1399  return repo::RepoType::NONE;
1400  }
1401 
1402  // prepare exception to be thrown if the type could not be determined
1403  // due to a media exception. We can't throw right away, because of some
1404  // problems with proxy servers returning an incorrect error
1405  // on ftp file-not-found(bnc #335906). Instead we'll check another types
1406  // before throwing.
1407 
1408  // TranslatorExplanation '%s' is an URL
1409  RepoException enew(str::form( _("Error trying to read from '%s'"), url.asString().c_str() ));
1410  bool gotMediaException = false;
1411  try
1412  {
1413  MediaSetAccess access(url);
1414  try
1415  {
1416  if ( access.doesFileExist(path/"/repodata/repomd.xml") )
1417  {
1418  MIL << "Probed type RPMMD at " << url << " (" << path << ")" << endl;
1419  return repo::RepoType::RPMMD;
1420  }
1421  }
1422  catch ( const media::MediaException &e )
1423  {
1424  ZYPP_CAUGHT(e);
1425  DBG << "problem checking for repodata/repomd.xml file" << endl;
1426  enew.remember(e);
1427  gotMediaException = true;
1428  }
1429 
1430  try
1431  {
1432  if ( access.doesFileExist(path/"/content") )
1433  {
1434  MIL << "Probed type YAST2 at " << url << " (" << path << ")" << endl;
1435  return repo::RepoType::YAST2;
1436  }
1437  }
1438  catch ( const media::MediaException &e )
1439  {
1440  ZYPP_CAUGHT(e);
1441  DBG << "problem checking for content file" << endl;
1442  enew.remember(e);
1443  gotMediaException = true;
1444  }
1445 
1446  // if it is a non-downloading URL denoting a directory
1447  if ( ! url.schemeIsDownloading() )
1448  {
1449  MediaMounter media( url );
1450  if ( PathInfo(media.getPathName()/path).isDir() )
1451  {
1452  // allow empty dirs for now
1453  MIL << "Probed type RPMPLAINDIR at " << url << " (" << path << ")" << endl;
1455  }
1456  }
1457  }
1458  catch ( const Exception &e )
1459  {
1460  ZYPP_CAUGHT(e);
1461  // TranslatorExplanation '%s' is an URL
1462  Exception enew(str::form( _("Unknown error reading from '%s'"), url.asString().c_str() ));
1463  enew.remember(e);
1464  ZYPP_THROW(enew);
1465  }
1466 
1467  if (gotMediaException)
1468  ZYPP_THROW(enew);
1469 
1470  MIL << "Probed type NONE at " << url << " (" << path << ")" << endl;
1471  return repo::RepoType::NONE;
1472  }
1473 
1479  repo::RepoType RepoManager::Impl::probeCache( const Pathname & path_r ) const
1480  {
1481  MIL << "going to probe the cached repo at " << path_r << endl;
1482 
1484 
1485  if ( PathInfo(path_r/"/repodata/repomd.xml").isFile() )
1486  { ret = repo::RepoType::RPMMD; }
1487  else if ( PathInfo(path_r/"/content").isFile() )
1488  { ret = repo::RepoType::YAST2; }
1489  else if ( PathInfo(path_r).isDir() )
1490  { ret = repo::RepoType::RPMPLAINDIR; }
1491 
1492  MIL << "Probed cached type " << ret << " at " << path_r << endl;
1493  return ret;
1494  }
1495 
1497 
1499  {
1500  MIL << "Going to clean up garbage in cache dirs" << endl;
1501 
1502  ProgressData progress(300);
1503  progress.sendTo(progressrcv);
1504  progress.toMin();
1505 
1506  std::list<Pathname> cachedirs;
1507  cachedirs.push_back(_options.repoRawCachePath);
1508  cachedirs.push_back(_options.repoPackagesCachePath);
1509  cachedirs.push_back(_options.repoSolvCachePath);
1510 
1511  for_( dir, cachedirs.begin(), cachedirs.end() )
1512  {
1513  if ( PathInfo(*dir).isExist() )
1514  {
1515  std::list<Pathname> entries;
1516  if ( filesystem::readdir( entries, *dir, false ) != 0 )
1517  // TranslatorExplanation '%s' is a pathname
1518  ZYPP_THROW(Exception(str::form(_("Failed to read directory '%s'"), dir->c_str())));
1519 
1520  unsigned sdircount = entries.size();
1521  unsigned sdircurrent = 1;
1522  for_( subdir, entries.begin(), entries.end() )
1523  {
1524  // if it does not belong known repo, make it disappear
1525  bool found = false;
1526  for_( r, repoBegin(), repoEnd() )
1527  if ( subdir->basename() == r->escaped_alias() )
1528  { found = true; break; }
1529 
1530  if ( ! found && ( Date::now()-PathInfo(*subdir).mtime() > Date::day ) )
1531  filesystem::recursive_rmdir( *subdir );
1532 
1533  progress.set( progress.val() + sdircurrent * 100 / sdircount );
1534  ++sdircurrent;
1535  }
1536  }
1537  else
1538  progress.set( progress.val() + 100 );
1539  }
1540  progress.toMax();
1541  }
1542 
1544 
1545  void RepoManager::Impl::cleanCache( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1546  {
1547  ProgressData progress(100);
1548  progress.sendTo(progressrcv);
1549  progress.toMin();
1550 
1551  MIL << "Removing raw metadata cache for " << info.alias() << endl;
1552  filesystem::recursive_rmdir(solv_path_for_repoinfo(_options, info));
1553 
1554  progress.toMax();
1555  }
1556 
1558 
1559  void RepoManager::Impl::loadFromCache( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1560  {
1561  assert_alias(info);
1562  Pathname solvfile = solv_path_for_repoinfo(_options, info) / "solv";
1563 
1564  if ( ! PathInfo(solvfile).isExist() )
1566 
1567  sat::Pool::instance().reposErase( info.alias() );
1568  try
1569  {
1570  Repository repo = sat::Pool::instance().addRepoSolv( solvfile, info );
1571  // test toolversion in order to rebuild solv file in case
1572  // it was written by an old libsolv-tool parser.
1573  //
1574  // Known version strings used:
1575  // - <no string>
1576  // - "1.0"
1577  //
1579  if ( toolversion.begin().asString().empty() )
1580  {
1581  repo.eraseFromPool();
1582  ZYPP_THROW(Exception("Solv-file was created by old parser."));
1583  }
1584  // else: up-to-date (or even newer).
1585  }
1586  catch ( const Exception & exp )
1587  {
1588  ZYPP_CAUGHT( exp );
1589  MIL << "Try to handle exception by rebuilding the solv-file" << endl;
1590  cleanCache( info, progressrcv );
1591  buildCache( info, BuildIfNeeded, progressrcv );
1592 
1593  sat::Pool::instance().addRepoSolv( solvfile, info );
1594  }
1595  }
1596 
1598 
1599  void RepoManager::Impl::addRepository( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
1600  {
1601  assert_alias(info);
1602 
1603  ProgressData progress(100);
1605  progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1606  progress.name(str::form(_("Adding repository '%s'"), info.label().c_str()));
1607  progress.toMin();
1608 
1609  MIL << "Try adding repo " << info << endl;
1610 
1611  RepoInfo tosave = info;
1612  if ( repos().find(tosave) != repos().end() )
1614 
1615  // check the first url for now
1616  if ( _options.probe )
1617  {
1618  DBG << "unknown repository type, probing" << endl;
1619  assert_urls(tosave);
1620 
1621  RepoType probedtype( probe( tosave.url(), info.path() ) );
1622  if ( probedtype == RepoType::NONE )
1624  else
1625  tosave.setType(probedtype);
1626  }
1627 
1628  progress.set(50);
1629 
1630  // assert the directory exists
1631  filesystem::assert_dir(_options.knownReposPath);
1632 
1633  Pathname repofile = generateNonExistingName(
1634  _options.knownReposPath, generateFilename(tosave));
1635  // now we have a filename that does not exists
1636  MIL << "Saving repo in " << repofile << endl;
1637 
1638  std::ofstream file(repofile.c_str());
1639  if (!file)
1640  {
1641  // TranslatorExplanation '%s' is a filename
1642  ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), repofile.c_str() )));
1643  }
1644 
1645  tosave.dumpAsIniOn(file);
1646  tosave.setFilepath(repofile);
1647  tosave.setMetadataPath( metadataPath( tosave ) );
1648  tosave.setPackagesPath( packagesPath( tosave ) );
1649  {
1650  // We chould fix the API as we must injet those paths
1651  // into the repoinfo in order to keep it usable.
1652  RepoInfo & oinfo( const_cast<RepoInfo &>(info) );
1653  oinfo.setMetadataPath( metadataPath( tosave ) );
1654  oinfo.setPackagesPath( packagesPath( tosave ) );
1655  }
1656  reposManip().insert(tosave);
1657 
1658  progress.set(90);
1659 
1660  // check for credentials in Urls
1661  UrlCredentialExtractor( _options.rootDir ).collect( tosave.baseUrls() );
1662 
1663  HistoryLog(_options.rootDir).addRepository(tosave);
1664 
1665  progress.toMax();
1666  MIL << "done" << endl;
1667  }
1668 
1669 
1671  {
1672  std::list<RepoInfo> repos = readRepoFile(url);
1673  for ( std::list<RepoInfo>::const_iterator it = repos.begin();
1674  it != repos.end();
1675  ++it )
1676  {
1677  // look if the alias is in the known repos.
1678  for_ ( kit, repoBegin(), repoEnd() )
1679  {
1680  if ( (*it).alias() == (*kit).alias() )
1681  {
1682  ERR << "To be added repo " << (*it).alias() << " conflicts with existing repo " << (*kit).alias() << endl;
1684  }
1685  }
1686  }
1687 
1688  std::string filename = Pathname(url.getPathName()).basename();
1689 
1690  if ( filename == Pathname() )
1691  {
1692  // TranslatorExplanation '%s' is an URL
1693  ZYPP_THROW(RepoException(str::form( _("Invalid repo file name at '%s'"), url.asString().c_str() )));
1694  }
1695 
1696  // assert the directory exists
1697  filesystem::assert_dir(_options.knownReposPath);
1698 
1699  Pathname repofile = generateNonExistingName(_options.knownReposPath, filename);
1700  // now we have a filename that does not exists
1701  MIL << "Saving " << repos.size() << " repo" << ( repos.size() ? "s" : "" ) << " in " << repofile << endl;
1702 
1703  std::ofstream file(repofile.c_str());
1704  if (!file)
1705  {
1706  // TranslatorExplanation '%s' is a filename
1707  ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), repofile.c_str() )));
1708  }
1709 
1710  for ( std::list<RepoInfo>::iterator it = repos.begin();
1711  it != repos.end();
1712  ++it )
1713  {
1714  MIL << "Saving " << (*it).alias() << endl;
1715  it->setFilepath(repofile.asString());
1716  it->dumpAsIniOn(file);
1717  reposManip().insert(*it);
1718 
1719  HistoryLog(_options.rootDir).addRepository(*it);
1720  }
1721 
1722  MIL << "done" << endl;
1723  }
1724 
1726 
1728  {
1729  ProgressData progress;
1731  progress.sendTo( ProgressReportAdaptor( progressrcv, report ) );
1732  progress.name(str::form(_("Removing repository '%s'"), info.label().c_str()));
1733 
1734  MIL << "Going to delete repo " << info.alias() << endl;
1735 
1736  for_( it, repoBegin(), repoEnd() )
1737  {
1738  // they can be the same only if the provided is empty, that means
1739  // the provided repo has no alias
1740  // then skip
1741  if ( (!info.alias().empty()) && ( info.alias() != (*it).alias() ) )
1742  continue;
1743 
1744  // TODO match by url
1745 
1746  // we have a matcing repository, now we need to know
1747  // where it does come from.
1748  RepoInfo todelete = *it;
1749  if (todelete.filepath().empty())
1750  {
1751  ZYPP_THROW(RepoException( todelete, _("Can't figure out where the repo is stored.") ));
1752  }
1753  else
1754  {
1755  // figure how many repos are there in the file:
1756  std::list<RepoInfo> filerepos = repositories_in_file(todelete.filepath());
1757  if ( filerepos.size() == 0 // bsc#984494: file may have already been deleted
1758  ||(filerepos.size() == 1 && filerepos.front().alias() == todelete.alias() ) )
1759  {
1760  // easy: file does not exist, contains no or only the repo to delete: delete the file
1761  int ret = filesystem::unlink( todelete.filepath() );
1762  if ( ! ( ret == 0 || ret == ENOENT ) )
1763  {
1764  // TranslatorExplanation '%s' is a filename
1765  ZYPP_THROW(RepoException( todelete, str::form( _("Can't delete '%s'"), todelete.filepath().c_str() )));
1766  }
1767  MIL << todelete.alias() << " successfully deleted." << endl;
1768  }
1769  else
1770  {
1771  // there are more repos in the same file
1772  // write them back except the deleted one.
1773  //TmpFile tmp;
1774  //std::ofstream file(tmp.path().c_str());
1775 
1776  // assert the directory exists
1777  filesystem::assert_dir(todelete.filepath().dirname());
1778 
1779  std::ofstream file(todelete.filepath().c_str());
1780  if (!file)
1781  {
1782  // TranslatorExplanation '%s' is a filename
1783  ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), todelete.filepath().c_str() )));
1784  }
1785  for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1786  fit != filerepos.end();
1787  ++fit )
1788  {
1789  if ( (*fit).alias() != todelete.alias() )
1790  (*fit).dumpAsIniOn(file);
1791  }
1792  }
1793 
1794  CombinedProgressData cSubprogrcv(progress, 20);
1795  CombinedProgressData mSubprogrcv(progress, 40);
1796  CombinedProgressData pSubprogrcv(progress, 40);
1797  // now delete it from cache
1798  if ( isCached(todelete) )
1799  cleanCache( todelete, cSubprogrcv);
1800  // now delete metadata (#301037)
1801  cleanMetadata( todelete, mSubprogrcv );
1802  cleanPackages( todelete, pSubprogrcv );
1803  reposManip().erase(todelete);
1804  MIL << todelete.alias() << " successfully deleted." << endl;
1805  HistoryLog(_options.rootDir).removeRepository(todelete);
1806  return;
1807  } // else filepath is empty
1808 
1809  }
1810  // should not be reached on a sucess workflow
1812  }
1813 
1815 
1816  void RepoManager::Impl::modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, const ProgressData::ReceiverFnc & progressrcv )
1817  {
1818  RepoInfo toedit = getRepositoryInfo(alias);
1819  RepoInfo newinfo( newinfo_r ); // need writable copy to upadte housekeeping data
1820 
1821  // check if the new alias already exists when renaming the repo
1822  if ( alias != newinfo.alias() && hasRepo( newinfo.alias() ) )
1823  {
1825  }
1826 
1827  if (toedit.filepath().empty())
1828  {
1829  ZYPP_THROW(RepoException( toedit, _("Can't figure out where the repo is stored.") ));
1830  }
1831  else
1832  {
1833  // figure how many repos are there in the file:
1834  std::list<RepoInfo> filerepos = repositories_in_file(toedit.filepath());
1835 
1836  // there are more repos in the same file
1837  // write them back except the deleted one.
1838  //TmpFile tmp;
1839  //std::ofstream file(tmp.path().c_str());
1840 
1841  // assert the directory exists
1842  filesystem::assert_dir(toedit.filepath().dirname());
1843 
1844  std::ofstream file(toedit.filepath().c_str());
1845  if (!file)
1846  {
1847  // TranslatorExplanation '%s' is a filename
1848  ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), toedit.filepath().c_str() )));
1849  }
1850  for ( std::list<RepoInfo>::const_iterator fit = filerepos.begin();
1851  fit != filerepos.end();
1852  ++fit )
1853  {
1854  // if the alias is different, dump the original
1855  // if it is the same, dump the provided one
1856  if ( (*fit).alias() != toedit.alias() )
1857  (*fit).dumpAsIniOn(file);
1858  else
1859  newinfo.dumpAsIniOn(file);
1860  }
1861 
1862  newinfo.setFilepath(toedit.filepath());
1863  reposManip().erase(toedit);
1864  reposManip().insert(newinfo);
1865  // check for credentials in Urls
1866  UrlCredentialExtractor( _options.rootDir ).collect( newinfo.baseUrls() );
1867  HistoryLog(_options.rootDir).modifyRepository(toedit, newinfo);
1868  MIL << "repo " << alias << " modified" << endl;
1869  }
1870  }
1871 
1873 
1874  RepoInfo RepoManager::Impl::getRepositoryInfo( const std::string & alias, const ProgressData::ReceiverFnc & progressrcv )
1875  {
1876  RepoConstIterator it( findAlias( alias, repos() ) );
1877  if ( it != repos().end() )
1878  return *it;
1879  RepoInfo info;
1880  info.setAlias( alias );
1882  }
1883 
1884 
1885  RepoInfo RepoManager::Impl::getRepositoryInfo( const Url & url, const url::ViewOption & urlview, const ProgressData::ReceiverFnc & progressrcv )
1886  {
1887  for_( it, repoBegin(), repoEnd() )
1888  {
1889  for_( urlit, (*it).baseUrlsBegin(), (*it).baseUrlsEnd() )
1890  {
1891  if ( (*urlit).asString(urlview) == url.asString(urlview) )
1892  return *it;
1893  }
1894  }
1895  RepoInfo info;
1896  info.setBaseUrl( url );
1898  }
1899 
1901  //
1902  // Services
1903  //
1905 
1907  {
1908  assert_alias( service );
1909 
1910  // check if service already exists
1911  if ( hasService( service.alias() ) )
1913 
1914  // Writable ServiceInfo is needed to save the location
1915  // of the .service file. Finaly insert into the service list.
1916  ServiceInfo toSave( service );
1917  saveService( toSave );
1918  _services.insert( toSave );
1919 
1920  // check for credentials in Url
1921  UrlCredentialExtractor( _options.rootDir ).collect( toSave.url() );
1922 
1923  MIL << "added service " << toSave.alias() << endl;
1924  }
1925 
1927 
1928  void RepoManager::Impl::removeService( const std::string & alias )
1929  {
1930  MIL << "Going to delete service " << alias << endl;
1931 
1932  const ServiceInfo & service = getService( alias );
1933 
1934  Pathname location = service.filepath();
1935  if( location.empty() )
1936  {
1937  ZYPP_THROW(ServiceException( service, _("Can't figure out where the service is stored.") ));
1938  }
1939 
1940  ServiceSet tmpSet;
1941  parser::ServiceFileReader( location, ServiceCollector(tmpSet) );
1942 
1943  // only one service definition in the file
1944  if ( tmpSet.size() == 1 )
1945  {
1946  if ( filesystem::unlink(location) != 0 )
1947  {
1948  // TranslatorExplanation '%s' is a filename
1949  ZYPP_THROW(ServiceException( service, str::form( _("Can't delete '%s'"), location.c_str() ) ));
1950  }
1951  MIL << alias << " successfully deleted." << endl;
1952  }
1953  else
1954  {
1955  filesystem::assert_dir(location.dirname());
1956 
1957  std::ofstream file(location.c_str());
1958  if( !file )
1959  {
1960  // TranslatorExplanation '%s' is a filename
1961  ZYPP_THROW( Exception(str::form( _("Can't open file '%s' for writing."), location.c_str() )));
1962  }
1963 
1964  for_(it, tmpSet.begin(), tmpSet.end())
1965  {
1966  if( it->alias() != alias )
1967  it->dumpAsIniOn(file);
1968  }
1969 
1970  MIL << alias << " successfully deleted from file " << location << endl;
1971  }
1972 
1973  // now remove all repositories added by this service
1974  RepoCollector rcollector;
1975  getRepositoriesInService( alias,
1976  boost::make_function_output_iterator( bind( &RepoCollector::collect, &rcollector, _1 ) ) );
1977  // cannot do this directly in getRepositoriesInService - would invalidate iterators
1978  for_(rit, rcollector.repos.begin(), rcollector.repos.end())
1979  removeRepository(*rit);
1980  }
1981 
1983 
1985  {
1986  // copy the set of services since refreshService
1987  // can eventually invalidate the iterator
1988  ServiceSet services( serviceBegin(), serviceEnd() );
1989  for_( it, services.begin(), services.end() )
1990  {
1991  if ( !it->enabled() )
1992  continue;
1993 
1994  try {
1995  refreshService(*it, options_r);
1996  }
1997  catch ( const repo::ServicePluginInformalException & e )
1998  { ;/* ignore ServicePluginInformalException */ }
1999  }
2000  }
2001 
2002  void RepoManager::Impl::refreshService( const std::string & alias, const RefreshServiceOptions & options_r )
2003  {
2004  ServiceInfo service( getService( alias ) );
2005  assert_alias( service );
2006  assert_url( service );
2007  // NOTE: It might be necessary to modify and rewrite the service info.
2008  // Either when probing the type, or when adjusting the repositories
2009  // enable/disable state.:
2010  bool serviceModified = false;
2011  MIL << "Going to refresh service '" << service.alias() << "', url: "<< service.url() << ", opts: " << options_r << endl;
2012 
2014 
2015  // if the type is unknown, try probing.
2016  if ( service.type() == repo::ServiceType::NONE )
2017  {
2018  repo::ServiceType type = probeService( service.url() );
2019  if ( type != ServiceType::NONE )
2020  {
2021  service.setProbedType( type ); // lazy init!
2022  serviceModified = true;
2023  }
2024  }
2025 
2026  // get target distro identifier
2027  std::string servicesTargetDistro = _options.servicesTargetDistro;
2028  if ( servicesTargetDistro.empty() )
2029  {
2030  servicesTargetDistro = Target::targetDistribution( Pathname() );
2031  }
2032  DBG << "ServicesTargetDistro: " << servicesTargetDistro << endl;
2033 
2034  // parse it
2035  RepoCollector collector(servicesTargetDistro);
2036  // FIXME Ugly hack: ServiceRepos may throw ServicePluginInformalException
2037  // which is actually a notification. Using an exception for this
2038  // instead of signal/callback is bad. Needs to be fixed here, in refreshServices()
2039  // and in zypper.
2040  std::pair<DefaultIntegral<bool,false>, repo::ServicePluginInformalException> uglyHack;
2041  try {
2042  ServiceRepos repos(service, bind( &RepoCollector::collect, &collector, _1 ));
2043  }
2044  catch ( const repo::ServicePluginInformalException & e )
2045  {
2046  /* ignore ServicePluginInformalException and throw later */
2047  uglyHack.first = true;
2048  uglyHack.second = e;
2049  }
2050 
2052  // On the fly remember the new repo states as defined the reopoindex.xml.
2053  // Move into ServiceInfo later.
2054  ServiceInfo::RepoStates newRepoStates;
2055 
2056  // set service alias and base url for all collected repositories
2057  for_( it, collector.repos.begin(), collector.repos.end() )
2058  {
2059  // First of all: Prepend service alias:
2060  it->setAlias( str::form( "%s:%s", service.alias().c_str(), it->alias().c_str() ) );
2061  // set reference to the parent service
2062  it->setService( service.alias() );
2063 
2064  // remember the new parsed repo state
2065  newRepoStates[it->alias()] = *it;
2066 
2067  // - If the repo url was not set by the repoindex parser, set service's url.
2068  // - Libzypp currently has problem with separate url + path handling so just
2069  // append a path, if set, to the baseurls
2070  // - Credentials in the url authority will be extracted later, either if the
2071  // repository is added or if we check for changed urls.
2072  Pathname path;
2073  if ( !it->path().empty() )
2074  {
2075  if ( it->path() != "/" )
2076  path = it->path();
2077  it->setPath("");
2078  }
2079 
2080  if ( it->baseUrlsEmpty() )
2081  {
2082  Url url( service.rawUrl() );
2083  if ( !path.empty() )
2084  url.setPathName( url.getPathName() / path );
2085  it->setBaseUrl( std::move(url) );
2086  }
2087  else if ( !path.empty() )
2088  {
2089  RepoInfo::url_set urls( it->rawBaseUrls() );
2090  for ( Url & url : urls )
2091  {
2092  url.setPathName( url.getPathName() / path );
2093  }
2094  it->setBaseUrls( std::move(urls) );
2095  }
2096  }
2097 
2099  // Now compare collected repos with the ones in the system...
2100  //
2101  RepoInfoList oldRepos;
2102  getRepositoriesInService( service.alias(), std::back_inserter( oldRepos ) );
2103 
2105  // find old repositories to remove...
2106  for_( oldRepo, oldRepos.begin(), oldRepos.end() )
2107  {
2108  if ( ! foundAliasIn( oldRepo->alias(), collector.repos ) )
2109  {
2110  if ( oldRepo->enabled() )
2111  {
2112  // Currently enabled. If this was a user modification remember the state.
2113  const auto & last = service.repoStates().find( oldRepo->alias() );
2114  if ( last != service.repoStates().end() && ! last->second.enabled )
2115  {
2116  DBG << "Service removes user enabled repo " << oldRepo->alias() << endl;
2117  service.addRepoToEnable( oldRepo->alias() );
2118  serviceModified = true;
2119  }
2120  else
2121  DBG << "Service removes enabled repo " << oldRepo->alias() << endl;
2122  }
2123  else
2124  DBG << "Service removes disabled repo " << oldRepo->alias() << endl;
2125 
2126  removeRepository( *oldRepo );
2127  }
2128  }
2129 
2131  // create missing repositories and modify existing ones if needed...
2132  UrlCredentialExtractor urlCredentialExtractor( _options.rootDir ); // To collect any credentials stored in repo URLs
2133  for_( it, collector.repos.begin(), collector.repos.end() )
2134  {
2135  // User explicitly requested the repo being enabled?
2136  // User explicitly requested the repo being disabled?
2137  // And hopefully not both ;) If so, enable wins.
2138 
2139  TriBool toBeEnabled( indeterminate ); // indeterminate - follow the service request
2140  DBG << "Service request to " << (it->enabled()?"enable":"disable") << " service repo " << it->alias() << endl;
2141 
2142  if ( options_r.testFlag( RefreshService_restoreStatus ) )
2143  {
2144  DBG << "Opt RefreshService_restoreStatus " << it->alias() << endl;
2145  // this overrides any pending request!
2146  // Remove from enable request list.
2147  // NOTE: repoToDisable is handled differently.
2148  // It gets cleared on each refresh.
2149  service.delRepoToEnable( it->alias() );
2150  // toBeEnabled stays indeterminate!
2151  }
2152  else
2153  {
2154  if ( service.repoToEnableFind( it->alias() ) )
2155  {
2156  DBG << "User request to enable service repo " << it->alias() << endl;
2157  toBeEnabled = true;
2158  // Remove from enable request list.
2159  // NOTE: repoToDisable is handled differently.
2160  // It gets cleared on each refresh.
2161  service.delRepoToEnable( it->alias() );
2162  serviceModified = true;
2163  }
2164  else if ( service.repoToDisableFind( it->alias() ) )
2165  {
2166  DBG << "User request to disable service repo " << it->alias() << endl;
2167  toBeEnabled = false;
2168  }
2169  }
2170 
2171  RepoInfoList::iterator oldRepo( findAlias( it->alias(), oldRepos ) );
2172  if ( oldRepo == oldRepos.end() )
2173  {
2174  // Not found in oldRepos ==> a new repo to add
2175 
2176  // Make sure the service repo is created with the appropriate enablement
2177  if ( ! indeterminate(toBeEnabled) )
2178  it->setEnabled( toBeEnabled );
2179 
2180  DBG << "Service adds repo " << it->alias() << " " << (it->enabled()?"enabled":"disabled") << endl;
2181  addRepository( *it );
2182  }
2183  else
2184  {
2185  // ==> an exising repo to check
2186  bool oldRepoModified = false;
2187 
2188  if ( indeterminate(toBeEnabled) )
2189  {
2190  // No user request: check for an old user modificaton otherwise follow service request.
2191  // NOTE: Assert toBeEnabled is boolean afterwards!
2192  if ( oldRepo->enabled() == it->enabled() )
2193  toBeEnabled = it->enabled(); // service requests no change to the system
2194  else if (options_r.testFlag( RefreshService_restoreStatus ) )
2195  {
2196  toBeEnabled = it->enabled(); // RefreshService_restoreStatus forced
2197  DBG << "Opt RefreshService_restoreStatus " << it->alias() << " forces " << (toBeEnabled?"enabled":"disabled") << endl;
2198  }
2199  else
2200  {
2201  const auto & last = service.repoStates().find( oldRepo->alias() );
2202  if ( last == service.repoStates().end() || last->second.enabled != it->enabled() )
2203  toBeEnabled = it->enabled(); // service request has changed since last refresh -> follow
2204  else
2205  {
2206  toBeEnabled = oldRepo->enabled(); // service request unchaned since last refresh -> keep user modification
2207  DBG << "User modified service repo " << it->alias() << " may stay " << (toBeEnabled?"enabled":"disabled") << endl;
2208  }
2209  }
2210  }
2211 
2212  // changed enable?
2213  if ( toBeEnabled == oldRepo->enabled() )
2214  {
2215  DBG << "Service repo " << it->alias() << " stays " << (oldRepo->enabled()?"enabled":"disabled") << endl;
2216  }
2217  else if ( toBeEnabled )
2218  {
2219  DBG << "Service repo " << it->alias() << " gets enabled" << endl;
2220  oldRepo->setEnabled( true );
2221  oldRepoModified = true;
2222  }
2223  else
2224  {
2225  DBG << "Service repo " << it->alias() << " gets disabled" << endl;
2226  oldRepo->setEnabled( false );
2227  oldRepoModified = true;
2228  }
2229 
2230  // all other attributes follow the service request:
2231 
2232  // changed name (raw!)
2233  if ( oldRepo->rawName() != it->rawName() )
2234  {
2235  DBG << "Service repo " << it->alias() << " gets new NAME " << it->rawName() << endl;
2236  oldRepo->setName( it->rawName() );
2237  oldRepoModified = true;
2238  }
2239 
2240  // changed autorefresh
2241  if ( oldRepo->autorefresh() != it->autorefresh() )
2242  {
2243  DBG << "Service repo " << it->alias() << " gets new AUTOREFRESH " << it->autorefresh() << endl;
2244  oldRepo->setAutorefresh( it->autorefresh() );
2245  oldRepoModified = true;
2246  }
2247 
2248  // changed priority?
2249  if ( oldRepo->priority() != it->priority() )
2250  {
2251  DBG << "Service repo " << it->alias() << " gets new PRIORITY " << it->priority() << endl;
2252  oldRepo->setPriority( it->priority() );
2253  oldRepoModified = true;
2254  }
2255 
2256  // changed url?
2257  {
2258  RepoInfo::url_set newUrls( it->rawBaseUrls() );
2259  urlCredentialExtractor.extract( newUrls ); // Extract! to prevent passwds from disturbing the comparison below
2260  if ( oldRepo->rawBaseUrls() != newUrls )
2261  {
2262  DBG << "Service repo " << it->alias() << " gets new URLs " << newUrls << endl;
2263  oldRepo->setBaseUrls( std::move(newUrls) );
2264  oldRepoModified = true;
2265  }
2266  }
2267 
2268  // changed gpg check settings?
2269  // ATM only plugin services can set GPG values.
2270  if ( service.type() == ServiceType::PLUGIN )
2271  {
2272  TriBool ogpg[3]; // Gpg RepoGpg PkgGpg
2273  TriBool ngpg[3];
2274  oldRepo->getRawGpgChecks( ogpg[0], ogpg[1], ogpg[2] );
2275  it-> getRawGpgChecks( ngpg[0], ngpg[1], ngpg[2] );
2276 #define Z_CHKGPG(I,N) \
2277  if ( ! sameTriboolState( ogpg[I], ngpg[I] ) ) \
2278  { \
2279  DBG << "Service repo " << it->alias() << " gets new "#N"Check " << ngpg[I] << endl; \
2280  oldRepo->set##N##Check( ngpg[I] ); \
2281  oldRepoModified = true; \
2282  }
2283  Z_CHKGPG( 0, Gpg );
2284  Z_CHKGPG( 1, RepoGpg );
2285  Z_CHKGPG( 2, PkgGpg );
2286 #undef Z_CHKGPG
2287  }
2288 
2289  // save if modified:
2290  if ( oldRepoModified )
2291  {
2292  modifyRepository( oldRepo->alias(), *oldRepo );
2293  }
2294  }
2295  }
2296 
2297  // Unlike reposToEnable, reposToDisable is always cleared after refresh.
2298  if ( ! service.reposToDisableEmpty() )
2299  {
2300  service.clearReposToDisable();
2301  serviceModified = true;
2302  }
2303 
2304  // Remember original service request for next refresh
2305  if ( service.repoStates() != newRepoStates )
2306  {
2307  service.setRepoStates( std::move(newRepoStates) );
2308  serviceModified = true;
2309  }
2310 
2312  // save service if modified: (unless a plugin service)
2313  if ( serviceModified && service.type() != ServiceType::PLUGIN )
2314  {
2315  // write out modified service file.
2316  modifyService( service.alias(), service );
2317  }
2318 
2319  if ( uglyHack.first )
2320  {
2321  throw( uglyHack.second ); // intentionally not ZYPP_THROW
2322  }
2323  }
2324 
2326 
2327  void RepoManager::Impl::modifyService( const std::string & oldAlias, const ServiceInfo & newService )
2328  {
2329  MIL << "Going to modify service " << oldAlias << endl;
2330 
2331  // we need a writable copy to link it to the file where
2332  // it is saved if we modify it
2333  ServiceInfo service(newService);
2334 
2335  if ( service.type() == ServiceType::PLUGIN )
2336  {
2338  }
2339 
2340  const ServiceInfo & oldService = getService(oldAlias);
2341 
2342  Pathname location = oldService.filepath();
2343  if( location.empty() )
2344  {
2345  ZYPP_THROW(ServiceException( oldService, _("Can't figure out where the service is stored.") ));
2346  }
2347 
2348  // remember: there may multiple services being defined in one file:
2349  ServiceSet tmpSet;
2350  parser::ServiceFileReader( location, ServiceCollector(tmpSet) );
2351 
2352  filesystem::assert_dir(location.dirname());
2353  std::ofstream file(location.c_str());
2354  for_(it, tmpSet.begin(), tmpSet.end())
2355  {
2356  if( *it != oldAlias )
2357  it->dumpAsIniOn(file);
2358  }
2359  service.dumpAsIniOn(file);
2360  file.close();
2361  service.setFilepath(location);
2362 
2363  _services.erase(oldAlias);
2364  _services.insert(service);
2365  // check for credentials in Urls
2366  UrlCredentialExtractor( _options.rootDir ).collect( service.url() );
2367 
2368 
2369  // changed properties affecting also repositories
2370  if ( oldAlias != service.alias() // changed alias
2371  || oldService.enabled() != service.enabled() ) // changed enabled status
2372  {
2373  std::vector<RepoInfo> toModify;
2374  getRepositoriesInService(oldAlias, std::back_inserter(toModify));
2375  for_( it, toModify.begin(), toModify.end() )
2376  {
2377  if ( oldService.enabled() != service.enabled() )
2378  {
2379  if ( service.enabled() )
2380  {
2381  // reset to last refreshs state
2382  const auto & last = service.repoStates().find( it->alias() );
2383  if ( last != service.repoStates().end() )
2384  it->setEnabled( last->second.enabled );
2385  }
2386  else
2387  it->setEnabled( false );
2388  }
2389 
2390  if ( oldAlias != service.alias() )
2391  it->setService(service.alias());
2392 
2393  modifyRepository(it->alias(), *it);
2394  }
2395  }
2396 
2398  }
2399 
2401 
2403  {
2404  try
2405  {
2406  MediaSetAccess access(url);
2407  if ( access.doesFileExist("/repo/repoindex.xml") )
2408  return repo::ServiceType::RIS;
2409  }
2410  catch ( const media::MediaException &e )
2411  {
2412  ZYPP_CAUGHT(e);
2413  // TranslatorExplanation '%s' is an URL
2414  RepoException enew(str::form( _("Error trying to read from '%s'"), url.asString().c_str() ));
2415  enew.remember(e);
2416  ZYPP_THROW(enew);
2417  }
2418  catch ( const Exception &e )
2419  {
2420  ZYPP_CAUGHT(e);
2421  // TranslatorExplanation '%s' is an URL
2422  Exception enew(str::form( _("Unknown error reading from '%s'"), url.asString().c_str() ));
2423  enew.remember(e);
2424  ZYPP_THROW(enew);
2425  }
2426 
2427  return repo::ServiceType::NONE;
2428  }
2429 
2431  //
2432  // CLASS NAME : RepoManager
2433  //
2435 
2437  : _pimpl( new Impl(opt) )
2438  {}
2439 
2441  {}
2442 
2444  { return _pimpl->repoEmpty(); }
2445 
2447  { return _pimpl->repoSize(); }
2448 
2450  { return _pimpl->repoBegin(); }
2451 
2453  { return _pimpl->repoEnd(); }
2454 
2455  RepoInfo RepoManager::getRepo( const std::string & alias ) const
2456  { return _pimpl->getRepo( alias ); }
2457 
2458  bool RepoManager::hasRepo( const std::string & alias ) const
2459  { return _pimpl->hasRepo( alias ); }
2460 
2461  std::string RepoManager::makeStupidAlias( const Url & url_r )
2462  {
2463  std::string ret( url_r.getScheme() );
2464  if ( ret.empty() )
2465  ret = "repo-";
2466  else
2467  ret += "-";
2468 
2469  std::string host( url_r.getHost() );
2470  if ( ! host.empty() )
2471  {
2472  ret += host;
2473  ret += "-";
2474  }
2475 
2476  static Date::ValueType serial = Date::now();
2477  ret += Digest::digest( Digest::sha1(), str::hexstring( ++serial ) +url_r.asCompleteString() ).substr(0,8);
2478  return ret;
2479  }
2480 
2482  { return _pimpl->metadataStatus( info ); }
2483 
2485  { return _pimpl->checkIfToRefreshMetadata( info, url, policy ); }
2486 
2487  Pathname RepoManager::metadataPath( const RepoInfo &info ) const
2488  { return _pimpl->metadataPath( info ); }
2489 
2490  Pathname RepoManager::packagesPath( const RepoInfo &info ) const
2491  { return _pimpl->packagesPath( info ); }
2492 
2494  { return _pimpl->refreshMetadata( info, policy, progressrcv ); }
2495 
2496  void RepoManager::cleanMetadata( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2497  { return _pimpl->cleanMetadata( info, progressrcv ); }
2498 
2499  void RepoManager::cleanPackages( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2500  { return _pimpl->cleanPackages( info, progressrcv ); }
2501 
2503  { return _pimpl->cacheStatus( info ); }
2504 
2505  void RepoManager::buildCache( const RepoInfo &info, CacheBuildPolicy policy, const ProgressData::ReceiverFnc & progressrcv )
2506  { return _pimpl->buildCache( info, policy, progressrcv ); }
2507 
2508  void RepoManager::cleanCache( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2509  { return _pimpl->cleanCache( info, progressrcv ); }
2510 
2511  bool RepoManager::isCached( const RepoInfo &info ) const
2512  { return _pimpl->isCached( info ); }
2513 
2514  void RepoManager::loadFromCache( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2515  { return _pimpl->loadFromCache( info, progressrcv ); }
2516 
2518  { return _pimpl->cleanCacheDirGarbage( progressrcv ); }
2519 
2520  repo::RepoType RepoManager::probe( const Url & url, const Pathname & path ) const
2521  { return _pimpl->probe( url, path ); }
2522 
2524  { return _pimpl->probe( url ); }
2525 
2526  void RepoManager::addRepository( const RepoInfo &info, const ProgressData::ReceiverFnc & progressrcv )
2527  { return _pimpl->addRepository( info, progressrcv ); }
2528 
2529  void RepoManager::addRepositories( const Url &url, const ProgressData::ReceiverFnc & progressrcv )
2530  { return _pimpl->addRepositories( url, progressrcv ); }
2531 
2532  void RepoManager::removeRepository( const RepoInfo & info, const ProgressData::ReceiverFnc & progressrcv )
2533  { return _pimpl->removeRepository( info, progressrcv ); }
2534 
2535  void RepoManager::modifyRepository( const std::string &alias, const RepoInfo & newinfo, const ProgressData::ReceiverFnc & progressrcv )
2536  { return _pimpl->modifyRepository( alias, newinfo, progressrcv ); }
2537 
2538  RepoInfo RepoManager::getRepositoryInfo( const std::string &alias, const ProgressData::ReceiverFnc & progressrcv )
2539  { return _pimpl->getRepositoryInfo( alias, progressrcv ); }
2540 
2541  RepoInfo RepoManager::getRepositoryInfo( const Url & url, const url::ViewOption & urlview, const ProgressData::ReceiverFnc & progressrcv )
2542  { return _pimpl->getRepositoryInfo( url, urlview, progressrcv ); }
2543 
2545  { return _pimpl->serviceEmpty(); }
2546 
2548  { return _pimpl->serviceSize(); }
2549 
2551  { return _pimpl->serviceBegin(); }
2552 
2554  { return _pimpl->serviceEnd(); }
2555 
2556  ServiceInfo RepoManager::getService( const std::string & alias ) const
2557  { return _pimpl->getService( alias ); }
2558 
2559  bool RepoManager::hasService( const std::string & alias ) const
2560  { return _pimpl->hasService( alias ); }
2561 
2563  { return _pimpl->probeService( url ); }
2564 
2565  void RepoManager::addService( const std::string & alias, const Url& url )
2566  { return _pimpl->addService( alias, url ); }
2567 
2568  void RepoManager::addService( const ServiceInfo & service )
2569  { return _pimpl->addService( service ); }
2570 
2571  void RepoManager::removeService( const std::string & alias )
2572  { return _pimpl->removeService( alias ); }
2573 
2574  void RepoManager::removeService( const ServiceInfo & service )
2575  { return _pimpl->removeService( service ); }
2576 
2578  { return _pimpl->refreshServices( options_r ); }
2579 
2580  void RepoManager::refreshService( const std::string & alias, const RefreshServiceOptions & options_r )
2581  { return _pimpl->refreshService( alias, options_r ); }
2582 
2583  void RepoManager::refreshService( const ServiceInfo & service, const RefreshServiceOptions & options_r )
2584  { return _pimpl->refreshService( service, options_r ); }
2585 
2586  void RepoManager::modifyService( const std::string & oldAlias, const ServiceInfo & service )
2587  { return _pimpl->modifyService( oldAlias, service ); }
2588 
2590 
2591  std::ostream & operator<<( std::ostream & str, const RepoManager & obj )
2592  { return str << *obj._pimpl; }
2593 
2595 } // namespace zypp
void saveToCookieFile(const Pathname &path_r) const
Save the status information to a cookie file.
Definition: RepoStatus.cc:126
Pathname packagesPath(const RepoInfo &info) const
Definition: RepoManager.cc:577
RepoManager(const RepoManagerOptions &options=RepoManagerOptions())
static const ValueType day
Definition: Date.h:43
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition: PathInfo.cc:329
void removeService(const std::string &alias)
Removes service specified by its name.
Service data.
Definition: ServiceInfo.h:35
thrown when it was impossible to match a repository
Thrown when the repo alias is found to be invalid.
Interface to gettext.
RepoManagerOptions(const Pathname &root_r=Pathname())
Default ctor following ZConfig global settings.
Definition: RepoManager.cc:457
#define MIL
Definition: Logger.h:47
bool hasService(const std::string &alias) const
Definition: RepoManager.cc:624
std::string alias() const
unique identifier for this source.
static const std::string & sha1()
sha1
Definition: Digest.cc:46
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition: PathInfo.cc:696
RepoStatus status(MediaSetAccess &media)
Status of the remote repository.
Definition: Downloader.cc:39
void setCacheStatus(const RepoInfo &info, const RepoStatus &status)
Definition: RepoManager.cc:663
std::string generateFilename(const ServiceInfo &info) const
Definition: RepoManager.cc:660
thrown when it was impossible to determine this repo type.
std::string digest()
get hex string representation of the digest
Definition: Digest.cc:183
Retrieval of repository list for a service.
Definition: ServiceRepos.h:26
virtual std::ostream & dumpAsIniOn(std::ostream &str) const
Write this RepoInfo object into str in a .repo file format.
Definition: RepoInfo.cc:709
void refreshServices(const RefreshServiceOptions &options_r)
bool serviceEmpty() const
Gets true if no service is in RepoManager (so no one in specified location)
void modifyService(const std::string &oldAlias, const ServiceInfo &service)
Modifies service file (rewrites it with new values) and underlying repositories if needed...
Read service data from a .service file.
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: ProgressData.h:226
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
Date timestamp() const
The time the data were changed the last time.
Definition: RepoStatus.cc:139
ServiceConstIterator serviceBegin() const
Definition: RepoManager.cc:621
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:674
Pathname path() const
Definition: TmpPath.cc:146
static TmpDir makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition: TmpPath.cc:287
#define OPT_PROGRESS
Definition: RepoManager.cc:59
void refreshService(const std::string &alias, const RefreshServiceOptions &options_r)
scoped_ptr< media::CredentialManager > _cmPtr
Definition: RepoManager.cc:129
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: RepoManager.h:693
void cleanCacheDirGarbage(const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Remove any subdirectories of cache directories which no longer belong to any of known repositories...
RepoConstIterator repoBegin() const
Definition: RepoManager.cc:561
Pathname filepath() const
File where this repo was read from.
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:162
bool isCached(const RepoInfo &info) const
Definition: RepoManager.cc:599
void refreshServices(const RefreshServiceOptions &options_r=RefreshServiceOptions())
Refreshes all enabled services.
RepoStatus metadataStatus(const RepoInfo &info) const
Status of local metadata.
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
Definition: Url.cc:598
#define _PL(MSG1, MSG2, N)
Definition: Gettext.h:30
bool empty() const
Test for an empty path.
Definition: Pathname.h:113
std::string getHost(EEncoding eflag=zypp::url::E_DECODED) const
Returns the hostname or IP from the URL authority.
Definition: Url.cc:582
RefreshCheckStatus
Possibly return state of checkIfRefreshMEtadata function.
Definition: RepoManager.h:194
Pathname metadataPath(const RepoInfo &info) const
Path where the metadata is downloaded and kept.
const std::string & command() const
The command we're executing.
urls_const_iterator baseUrlsBegin() const
iterator that points at begin of repository urls
Definition: RepoInfo.cc:525
RepoSet::size_type RepoSizeType
Definition: RepoManager.h:121
bool empty() const
Whether the status is empty (default constucted)
Definition: RepoStatus.cc:136
void loadFromCache(const RepoInfo &info, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Load resolvables into the pool.
ServiceConstIterator serviceEnd() const
Iterator to place behind last service in internal storage.
repo::RepoType probe(const Url &url, const Pathname &path) const
Probe repo metadata type.
std::string generateFilename(const RepoInfo &info) const
Definition: RepoManager.cc:657
RepoConstIterator repoBegin() const
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:99
void refreshMetadata(const RepoInfo &info, RawMetadataRefreshPolicy policy=RefreshIfNeeded, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Refresh local raw cache.
Pathname packagesPath(const RepoInfo &info) const
Path where the rpm packages are downloaded and kept.
void addService(const std::string &alias, const Url &url)
Definition: RepoManager.cc:635
void touchIndexFile(const RepoInfo &info)
Definition: RepoManager.cc:913
void setAlias(const std::string &alias)
set the repository alias
Definition: RepoInfoBase.cc:94
void addRepoToEnable(const std::string &alias_r)
Add alias_r to the set of ReposToEnable.
Definition: ServiceInfo.cc:131
void removeRepository(const RepoInfo &info, OPT_PROGRESS)
RefreshServiceFlags RefreshServiceOptions
Options tuning RefreshService.
Definition: RepoManager.h:149
std::list< Url > url_set
Definition: RepoInfo.h:100
void modifyService(const std::string &oldAlias, const ServiceInfo &newService)
bool toMax()
Set counter value to current max value (unless no range).
Definition: ProgressData.h:273
void setProbedType(const repo::RepoType &t) const
This allows to adjust the RepoType lazy, from NONE to some probed value, even for const objects...
Definition: RepoInfo.cc:452
void refreshService(const std::string &alias, const RefreshServiceOptions &options_r=RefreshServiceOptions())
Refresh specific service.
bool doesFileExist(const Pathname &file, unsigned media_nr=1)
Checks if a file exists on the specified media, with user callbacks.
void setFilepath(const Pathname &filename)
set the path to the .repo file
What is known about a repository.
Definition: RepoInfo.h:72
Service plugin has trouble providing the metadata but this should not be treated as error...
void removeRepository(const RepoInfo &info, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Remove the best matching repository from known repos list.
Url url
Definition: MediaCurl.cc:180
const RepoSet & repos() const
Definition: RepoManager.cc:685
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
const RepoStates & repoStates() const
Access the remembered repository states.
Definition: ServiceInfo.cc:171
void setBaseUrl(const Url &url)
Clears current base URL list and adds url.
Definition: RepoInfo.cc:437
bool enabled() const
If enabled is false, then this repository must be ignored as if does not exists, except when checking...
std::string targetDistro
Definition: RepoManager.cc:263
void reposErase(const std::string &alias_r)
Remove a Repository named alias_r.
Definition: Pool.h:105
Service already exists and some unique attribute can't be duplicated.
void refreshService(const ServiceInfo &service, const RefreshServiceOptions &options_r)
Definition: RepoManager.cc:645
bool repo_add_probe() const
Whether repository urls should be probed.
Definition: ZConfig.cc:840
urls_const_iterator baseUrlsEnd() const
iterator that points at end of repository urls
Definition: RepoInfo.cc:528
std::string targetDistribution() const
This is register.target attribute of the installed base product.
Definition: Target.cc:114
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
static RepoStatus fromCookieFile(const Pathname &path)
Reads the status from a cookie file.
Definition: RepoStatus.cc:108
Service without alias was used in an operation.
RepoStatus metadataStatus(const RepoInfo &info) const
Definition: RepoManager.cc:878
RepoSet::const_iterator RepoConstIterator
Definition: RepoManager.h:120
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: ProgressData.h:139
Url::asString() view options.
Definition: UrlBase.h:39
void cleanMetadata(const RepoInfo &info, OPT_PROGRESS)
#define ERR
Definition: Logger.h:49
unsigned int MediaAccessId
Media manager access Id type.
Definition: MediaSource.h:29
repo::RepoType probeCache(const Pathname &path_r) const
Probe Metadata in a local cache directory.
void modifyRepository(const std::string &alias, const RepoInfo &newinfo, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Modify repository attributes.
std::vector< std::string > Arguments
RepoManagerOptions _options
Definition: RepoManager.cc:689
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:491
ServiceInfo getService(const std::string &alias) const
Definition: RepoManager.cc:627
RepoSizeType repoSize() const
Repo manager settings.
Definition: RepoManager.h:53
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition: String.h:29
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:89
std::string & replaceAll(std::string &str_r, const std::string &from_r, const std::string &to_r)
Replace all occurrences of from_r with to_r in str_r (inplace).
Definition: String.cc:313
void removeService(const ServiceInfo &service)
Definition: RepoManager.cc:639
transform_iterator< repo::RepoVariablesUrlReplacer, url_set::const_iterator > urls_const_iterator
Definition: RepoInfo.h:102
Progress callback from another progress.
Definition: ProgressData.h:390
std::map< std::string, RepoState > RepoStates
Definition: ServiceInfo.h:158
std::string label() const
Label for use in messages for the user interface.
void addRepository(const RepoInfo &info, OPT_PROGRESS)
static const ServiceType RIS
Repository Index Service (RIS) (formerly known as 'Novell Update' (NU) service)
Definition: ServiceType.h:32
RepoManager implementation.
Definition: RepoManager.cc:505
#define ZYPP_RETHROW(EXCPT)
Drops a logline and rethrows, updating the CodeLocation.
Definition: Exception.h:328
void setPathName(const std::string &path, EEncoding eflag=zypp::url::E_DECODED)
Set the path name.
Definition: Url.cc:758
std::set< RepoInfo > RepoSet
RepoInfo typedefs.
Definition: RepoManager.h:119
bool toMin()
Set counter value to current min value.
Definition: ProgressData.h:269
RepoInfo getRepositoryInfo(const std::string &alias, OPT_PROGRESS)
Downloader for SUSETags (YaST2) repositories Encapsulates all the knowledge of which files have to be...
Definition: Downloader.h:34
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
static Pool instance()
Singleton ctor.
Definition: Pool.h:52
bool serviceEmpty() const
Definition: RepoManager.cc:619
static RepoManagerOptions makeTestSetup(const Pathname &root_r)
Test setup adjusting all paths to be located below one root_r directory.
Definition: RepoManager.cc:471
Pathname rootDir
remembers root_r value for later use
Definition: RepoManager.h:96
void removeRepository(const RepoInfo &repo)
Log recently removed repository.
Definition: HistoryLog.cc:257
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition: TmpPath.h:170
void clearReposToDisable()
Clear the set of ReposToDisable.
Definition: ServiceInfo.cc:168
Lightweight repository attribute value lookup.
Definition: LookupAttr.h:256
std::string asCompleteString() const
Returns a complete string representation of the Url object.
Definition: Url.cc:499
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
RepoConstIterator repoEnd() const
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
void cleanCacheDirGarbage(OPT_PROGRESS)
int unlink(const Pathname &path)
Like 'unlink'.
Definition: PathInfo.cc:668
thrown when it was impossible to determine one url for this repo.
Definition: RepoException.h:78
Just inherits Exception to separate media exceptions.
static const ServiceType NONE
No service set.
Definition: ServiceType.h:34
static const SolvAttr repositoryToolVersion
Definition: SolvAttr.h:173
Service type enumeration.
Definition: ServiceType.h:26
void modifyRepository(const std::string &alias, const RepoInfo &newinfo_r, OPT_PROGRESS)
ServiceSet::const_iterator ServiceConstIterator
Definition: RepoManager.h:115
void setRepoStates(RepoStates newStates_r)
Remember a new set of repository states.
Definition: ServiceInfo.cc:174
std::ostream & operator<<(std::ostream &str, const DeltaCandidates &obj)
repo::ServiceType probeService(const Url &url) const
Probe the type or the service.
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
Definition: PathInfo.cc:422
#define WAR
Definition: Logger.h:48
#define OUTS(X)
void setMetadataPath(const Pathname &path)
set the path where the local metadata is stored
Definition: RepoInfo.cc:456
void setType(const repo::RepoType &t)
set the repository type
Definition: RepoInfo.cc:449
Maintain [min,max] and counter (value) for progress counting.
Definition: ProgressData.h:130
RepoInfoList repos
Definition: RepoManager.cc:262
RepoStatus cacheStatus(const RepoInfo &info) const
Definition: RepoManager.cc:602
static bool error(const MessageString &msg_r, const UserData &userData_r=UserData())
send error text
Pathname generateNonExistingName(const Pathname &dir, const std::string &basefilename) const
Generate a non existing filename in a directory, using a base name.
Definition: RepoManager.cc:743
void addRepository(const RepoInfo &repo)
Log a newly added repository.
Definition: HistoryLog.cc:245
RepoInfo getRepo(const std::string &alias) const
Definition: RepoManager.cc:567
Writing the zypp history fileReference counted signleton for writhing the zypp history file...
Definition: HistoryLog.h:55
static bool schemeIsVolatile(const std::string &scheme_r)
cd dvd
Definition: Url.cc:468
void addRepository(const RepoInfo &info, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Adds a repository to the list of known repositories.
RepoInfo getRepositoryInfo(const std::string &alias, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Find a matching repository info.
#define _(MSG)
Definition: Gettext.h:29
static const ServiceType PLUGIN
Plugin services are scripts installed on your system that provide the package manager with repositori...
Definition: ServiceType.h:43
Base Exception for service handling.
std::string receiveLine()
Read one line from the input stream.
const Pathname & _root
Definition: RepoManager.cc:128
void delRepoToEnable(const std::string &alias_r)
Remove alias_r from the set of ReposToEnable.
Definition: ServiceInfo.cc:137
static std::string makeStupidAlias(const Url &url_r=Url())
Some stupid string but suitable as alias for your url if nothing better is available.
RefreshCheckStatus checkIfToRefreshMetadata(const RepoInfo &info, const Url &url, RawMetadataRefreshPolicy policy=RefreshIfNeeded)
Checks whether to refresh metadata for specified repository and url.
void cleanCache(const RepoInfo &info, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
clean local cache
void cleanCache(const RepoInfo &info, OPT_PROGRESS)
std::string numstring(char n, int w=0)
Definition: String.h:311
ServiceSet::size_type ServiceSizeType
Definition: RepoManager.h:116
bool reposToDisableEmpty() const
Definition: ServiceInfo.cc:144
static const RepoType NONE
Definition: RepoType.h:32
int touch(const Pathname &path)
Change file's modification and access times.
Definition: PathInfo.cc:1169
ServiceInfo getService(const std::string &alias) const
Finds ServiceInfo by alias or return ServiceInfo::noService.
void getRepositoriesInService(const std::string &alias, OutputIterator out) const
Definition: RepoManager.cc:673
void setPackagesPath(const Pathname &path)
set the path where the local packages are stored
Definition: RepoInfo.cc:459
bool repoEmpty() const
Definition: RepoManager.cc:559
url_set baseUrls() const
The complete set of repository urls.
Definition: RepoInfo.cc:507
std::ostream & copy(std::istream &from_r, std::ostream &to_r)
Copy istream to ostream.
Definition: IOStream.h:50
Temporarily disable MediaChangeReport Sometimes helpful to suppress interactive messages connected to...
int close()
Wait for the progamm to complete.
bool hasRepo(const std::string &alias) const
Return whether there is a known repository for alias.
static const RepoType RPMMD
Definition: RepoType.h:29
creates and provides information about known sources.
Definition: RepoManager.h:105
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:324
RepoStatus cacheStatus(const RepoInfo &info) const
Status of metadata cache.
repo::RepoType type() const
Type of repository,.
Definition: RepoInfo.cc:480
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition: PathInfo.cc:604
RepoSizeType repoSize() const
Definition: RepoManager.cc:560
void addService(const ServiceInfo &service)
std::list< RepoInfo > readRepoFile(const Url &repo_file)
Parses repo_file and returns a list of RepoInfo objects corresponding to repositories found within th...
Definition: RepoManager.cc:436
RepoInfo getRepo(const std::string &alias) const
Find RepoInfo by alias or return RepoInfo::noRepo.
Url rawUrl() const
The service raw url (no variables replaced)
Definition: ServiceInfo.cc:102
static const RepoType YAST2
Definition: RepoType.h:30
ServiceSet & _services
Definition: RepoManager.cc:429
thrown when it was impossible to determine an alias for this repo.
Definition: RepoException.h:91
RepoStatus status(MediaSetAccess &media)
Status of the remote repository.
Definition: Downloader.cc:36
void buildCache(const RepoInfo &info, CacheBuildPolicy policy=BuildIfNeeded, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Refresh local cache.
Base class for Exception.
Definition: Exception.h:143
void addRepositories(const Url &url, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Adds repositores from a repo file to the list of known repositories.
std::set< ServiceInfo > ServiceSet
ServiceInfo typedefs.
Definition: RepoManager.h:111
Type toEnum() const
Definition: RepoType.h:48
Exception for repository handling.
Definition: RepoException.h:37
void saveService(ServiceInfo &service) const
Definition: RepoManager.cc:709
Impl(const RepoManagerOptions &opt)
Definition: RepoManager.cc:508
media::MediaAccessId _mid
Definition: RepoManager.cc:170
static Date now()
Return the current time.
Definition: Date.h:77
repo::RepoType probe(const Url &url, const Pathname &path=Pathname()) const
Probe the metadata type of a repository located at url.
callback::SendReport< DownloadProgressReport > * report
Definition: MediaCurl.cc:184
DefaultIntegral< bool, false > _reposDirty
Definition: RepoManager.cc:693
value_type val() const
Definition: ProgressData.h:295
ServiceConstIterator serviceEnd() const
Definition: RepoManager.cc:622
Functor thats filter RepoInfo by service which it belongs to.
Definition: RepoManager.h:636
bool isCached(const RepoInfo &info) const
Whether a repository exists in cache.
bool hasRepo(const std::string &alias) const
Definition: RepoManager.cc:564
Reference counted access to a _Tp object calling a custom Dispose function when the last AutoDispose ...
Definition: AutoDispose.h:92
The repository cache is not built yet so you can't create the repostories from the cache...
Definition: RepoException.h:65
Url url() const
Pars pro toto: The first repository url.
Definition: RepoInfo.h:128
time_t ValueType
Definition: Date.h:38
void eraseFromPool()
Remove this Repository from it's Pool.
Definition: Repository.cc:297
Pathname repoPackagesCachePath
Definition: RepoManager.h:82
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:75
static const ServiceInfo noService
Represents an empty service.
Definition: ServiceInfo.h:60
RepoConstIterator repoEnd() const
Definition: RepoManager.cc:562
bool hasService(const std::string &alias) const
Return whether there is a known service for alias.
void removeService(const std::string &alias)
void buildCache(const RepoInfo &info, CacheBuildPolicy policy, OPT_PROGRESS)
bool repoToDisableFind(const std::string &alias_r) const
Whether alias_r is mentioned in ReposToDisable.
Definition: ServiceInfo.cc:156
static const RepoInfo noRepo
Represents no Repository (one with an empty alias).
Definition: RepoInfo.h:81
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Definition: Regex.h:70
Thrown when the repo alias is found to be invalid.
ServiceSizeType serviceSize() const
Gets count of service in RepoManager (in specified location)
static const RepoType RPMPLAINDIR
Definition: RepoType.h:31
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: Repository.cc:37
bool repoToEnableFind(const std::string &alias_r) const
Whether alias_r is mentioned in ReposToEnable.
Definition: ServiceInfo.cc:128
ServiceSizeType serviceSize() const
Definition: RepoManager.cc:620
Track changing files or directories.
Definition: RepoStatus.h:38
void cleanPackages(const RepoInfo &info, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Clean local package cache.
unsigned repo_refresh_delay() const
Amount of time in minutes that must pass before another refresh.
Definition: ZConfig.cc:843
Repository already exists and some unique attribute can't be duplicated.
ServiceConstIterator serviceBegin() const
Iterator to first service in internal storage.
bool set(value_type val_r)
Set new counter value.
Definition: ProgressData.h:246
std::string getScheme() const
Returns the scheme name of the URL.
Definition: Url.cc:527
Url url() const
The service url.
Definition: ServiceInfo.cc:99
static bool schemeIsDownloading(const std::string &scheme_r)
http https ftp sftp tftp
Definition: Url.cc:474
void modifyRepository(const RepoInfo &oldrepo, const RepoInfo &newrepo)
Log certain modifications to a repository.
Definition: HistoryLog.cc:268
std::ostream & operator<<(std::ostream &str, const RepoManager::Impl &obj)
Definition: RepoManager.cc:704
Impl * clone() const
clone for RWCOW_pointer
Definition: RepoManager.cc:698
urls_size_type baseUrlsSize() const
number of repository urls
Definition: RepoInfo.cc:531
static bool warning(const MessageString &msg_r, const UserData &userData_r=UserData())
send warning text
Repository addRepoSolv(const Pathname &file_r, const std::string &name_r)
Load Solvables from a solv-file into a Repository named name_r.
Definition: Pool.cc:151
std::string asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: LookupAttr.cc:610
void name(const std::string &name_r)
Set counter name.
Definition: ProgressData.h:222
Downloader for YUM (rpm-nmd) repositories Encapsulates all the knowledge of which files have to be do...
Definition: Downloader.h:41
Pathname metadataPath(const RepoInfo &info) const
Definition: RepoManager.cc:574
void setProbedType(const repo::ServiceType &t) const
Lazy init service type.
Definition: ServiceInfo.cc:113
void cleanPackages(const RepoInfo &info, OPT_PROGRESS)
Pathname provideFile(const OnMediaLocation &resource, ProvideFileOptions options=PROVIDE_DEFAULT, const Pathname &deltafile=Pathname())
Provides a file from a media location.
bool repoEmpty() const
void loadFromCache(const RepoInfo &info, OPT_PROGRESS)
Format with (N)o (A)rgument (C)heck.
Definition: String.h:285
std::string hexstring(char n, int w=4)
Definition: String.h:346
std::string asUserString() const
Translated error message as string suitable for the user.
Definition: Exception.cc:66
void addService(const std::string &alias, const Url &url)
Adds new service by it's alias and url.
void refreshMetadata(const RepoInfo &info, RawMetadataRefreshPolicy policy, OPT_PROGRESS)
Service has no or invalid url defined.
static bool schemeIsLocal(const std::string &scheme_r)
hd cd dvd dir file iso
Definition: Url.cc:456
Url manipulation class.
Definition: Url.h:87
void addRepositories(const Url &url, OPT_PROGRESS)
Media access layer responsible for handling files distributed on a set of media with media change and...
void cleanMetadata(const RepoInfo &info, const ProgressData::ReceiverFnc &progressrcv=ProgressData::ReceiverFnc())
Clean local metadata.
Pathname path() const
Repository path.
Definition: RepoInfo.cc:513
#define Z_CHKGPG(I, N)
#define DBG
Definition: Logger.h:46
virtual std::ostream & dumpAsIniOn(std::ostream &str) const
Writes ServiceInfo to stream in ".service" format.
Definition: ServiceInfo.cc:185
repo::ServiceType type() const
Service type.
Definition: ServiceInfo.cc:108
iterator begin() const
Iterator to the begin of query results.
Definition: LookupAttr.cc:236
Repository type enumeration.
Definition: RepoType.h:27
RefreshCheckStatus checkIfToRefreshMetadata(const RepoInfo &info, const Url &url, RawMetadataRefreshPolicy policy)
Definition: RepoManager.cc:950
repo::ServiceType probeService(const Url &url) const