libzypp  17.32.4
repomanagerbase_p.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_ZYPP_DETAIL_REPOMANAGERIMPL_H
13 #define ZYPP_ZYPP_DETAIL_REPOMANAGERIMPL_H
14 
16 #include <zypp/RepoStatus.h>
17 
19 
20 #include <zypp-core/base/Gettext.h>
21 #include <utility>
22 #include <zypp-core/base/DefaultIntegral>
24 #include <zypp-core/fs/PathInfo.h>
26 
27 namespace zypp {
28 
29  #define OPT_PROGRESS const ProgressData::ReceiverFnc & = ProgressData::ReceiverFnc()
30 
32  inline bool isTmpRepo( const RepoInfo & info_r )
33  { return( info_r.filepath().empty() && info_r.usesAutoMetadataPaths() ); }
34 
35  void assert_alias( const RepoInfo & info );
36 
37  inline void assert_alias( const ServiceInfo & info )
38  {
39  if ( info.alias().empty() )
41  // bnc #473834. Maybe we can match the alias against a regex to define
42  // and check for valid aliases
43  if ( info.alias()[0] == '.')
45  info, _("Service alias cannot start with dot.")));
46  }
47 
49  template <class Iterator>
50  inline bool foundAliasIn( const std::string & alias_r, Iterator begin_r, Iterator end_r )
51  {
52  for_( it, begin_r, end_r )
53  if ( it->alias() == alias_r )
54  return true;
55  return false;
56  }
58  template <class Container>
59  inline bool foundAliasIn( const std::string & alias_r, const Container & cont_r )
60  { return foundAliasIn( alias_r, cont_r.begin(), cont_r.end() ); }
61 
63  template <class Iterator>
64  inline Iterator findAlias( const std::string & alias_r, Iterator begin_r, Iterator end_r )
65  {
66  for_( it, begin_r, end_r )
67  if ( it->alias() == alias_r )
68  return it;
69  return end_r;
70  }
72  template <class Container>
73  inline typename Container::iterator findAlias( const std::string & alias_r, Container & cont_r )
74  { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
76  template <class Container>
77  inline typename Container::const_iterator findAlias( const std::string & alias_r, const Container & cont_r )
78  { return findAlias( alias_r, cont_r.begin(), cont_r.end() ); }
79 
80 
82  std::string filenameFromAlias( const std::string & alias_r, const std::string & stem_r );
83 
100  {
102  {}
103 
104  RepoCollector(std::string targetDistro_)
105  : targetDistro(std::move(targetDistro_))
106  {}
107 
108  bool collect( const RepoInfo &repo );
109 
110  RepoInfoList repos;
111  std::string targetDistro;
112  };
114 
120  std::list<RepoInfo> repositories_in_file( const Pathname & file );
121 
123 
132  std::list<RepoInfo> repositories_in_dir( const Pathname &dir );
133 
134  void assert_urls( const RepoInfo & info );
135 
136  inline void assert_url( const ServiceInfo & info )
137  {
138  if ( ! info.url().isValid() )
140  }
141 
147  {
148  assert_alias(info);
149  return isTmpRepo( info ) ? info.metadataPath() : opt.repoRawCachePath / info.escaped_alias();
150  }
151 
161  { return rawcache_path_for_repoinfo( opt, info ) / info.path(); }
162 
167  {
168  assert_alias(info);
169  return isTmpRepo( info ) ? info.packagesPath() : opt.repoPackagesCachePath / info.escaped_alias();
170  }
171 
175  inline Pathname solv_path_for_repoinfo( const RepoManagerOptions &opt, const RepoInfo &info )
176  {
177  assert_alias(info);
178  return isTmpRepo( info ) ? info.metadataPath().dirname() / "%SLV%" : opt.repoSolvCachePath / info.escaped_alias();
179  }
180 
182 
185  {
186  public:
187  using ServiceSet = std::set<ServiceInfo>;
188 
189  ServiceCollector( ServiceSet & services_r )
190  : _services( services_r )
191  {}
192 
193  bool operator()( const ServiceInfo & service_r ) const
194  {
195  _services.insert( service_r );
196  return true;
197  }
198 
199  private:
201  };
203 
205  bool autoPruneInDir( const Pathname & path_r );
206 
207 
214  {
215  public:
216 
221  {
222  public:
223  MatchServiceAlias( std::string alias_ ) : alias(std::move(alias_)) {}
224  bool operator()( const RepoInfo & info ) const
225  { return info.service() == alias; }
226  private:
227  std::string alias;
228  };
229 
231  using ServiceSet = std::set<ServiceInfo>;
232  using ServiceConstIterator = ServiceSet::const_iterator;
234 
236  using RepoSet = std::set<RepoInfo>;
237  using RepoConstIterator = RepoSet::const_iterator;
239 
240 
242  virtual ~RepoManagerBaseImpl();
243 
244  public:
245 
246  bool repoEmpty() const { return repos().empty(); }
247  RepoSizeType repoSize() const { return repos().size(); }
248  RepoConstIterator repoBegin() const { return repos().begin(); }
249  RepoConstIterator repoEnd() const { return repos().end(); }
250 
251  bool hasRepo( const std::string & alias ) const
252  { return foundAliasIn( alias, repos() ); }
253 
254  RepoInfo getRepo( const std::string & alias ) const
255  {
256  RepoConstIterator it( findAlias( alias, repos() ) );
257  return it == repos().end() ? RepoInfo::noRepo : *it;
258  }
259 
260  public:
261  Pathname metadataPath( const RepoInfo & info ) const
262  { return rawcache_path_for_repoinfo( _options, info ); }
263 
264  Pathname packagesPath( const RepoInfo & info ) const
265  { return packagescache_path_for_repoinfo( _options, info ); }
266 
267  static RepoStatus metadataStatus( const RepoInfo & info, const RepoManagerOptions &options );
268  RepoStatus metadataStatus( const RepoInfo & info ) const;
269 
270  void cleanMetadata( const RepoInfo & info, OPT_PROGRESS );
271 
272  void cleanPackages(const RepoInfo & info, OPT_PROGRESS , bool isAutoClean = false);
273 
274  static repo::RepoType probeCache( const Pathname & path_r );
275 
276  void cleanCacheDirGarbage( OPT_PROGRESS );
277 
278  void cleanCache( const RepoInfo & info, OPT_PROGRESS );
279 
280  bool isCached( const RepoInfo & info ) const
281  { return PathInfo(solv_path_for_repoinfo( _options, info ) / "solv").isExist(); }
282 
283  RepoStatus cacheStatus( const RepoInfo & info ) const
284  { return cacheStatus( info, _options ); }
285 
286  static RepoStatus cacheStatus( const RepoInfo & info, const RepoManagerOptions &options )
287  { return RepoStatus::fromCookieFile(solv_path_for_repoinfo(options, info) / "cookie"); }
288 
289  void loadFromCache( const RepoInfo & info, OPT_PROGRESS );
290 
291  void addProbedRepository( const RepoInfo & info, repo::RepoType probedType );
292 
293  virtual void removeRepository ( const RepoInfo & info, OPT_PROGRESS ) = 0;
294 
295  void modifyRepository( const std::string & alias, const RepoInfo & newinfo_r, OPT_PROGRESS );
296 
297  RepoInfo getRepositoryInfo( const std::string & alias );
298  RepoInfo getRepositoryInfo( const Url & url, const url::ViewOption & urlview );
299 
300  public:
301  bool serviceEmpty() const { return _services.empty(); }
302  ServiceSizeType serviceSize() const { return _services.size(); }
303  ServiceConstIterator serviceBegin() const { return _services.begin(); }
304  ServiceConstIterator serviceEnd() const { return _services.end(); }
305 
306  bool hasService( const std::string & alias ) const
307  { return foundAliasIn( alias, _services ); }
308 
309  ServiceInfo getService( const std::string & alias ) const
310  {
311  ServiceConstIterator it( findAlias( alias, _services ) );
312  return it == _services.end() ? ServiceInfo::noService : *it;
313  }
314 
315  public:
316  void addService( const ServiceInfo & service );
317  void addService( const std::string & alias, const Url & url )
318  { addService( ServiceInfo( alias, url ) ); }
319 
320  void removeService( const std::string & alias );
321  void removeService( const ServiceInfo & service )
322  { removeService( service.alias() ); }
323 
324  void modifyService( const std::string & oldAlias, const ServiceInfo & newService );
325 
326  static void touchIndexFile( const RepoInfo & info, const RepoManagerOptions &options );
327 
328  protected:
329  void removeRepositoryImpl( const RepoInfo & info, OPT_PROGRESS );
330  void saveService( ServiceInfo & service ) const;
331 
332  Pathname generateNonExistingName( const Pathname & dir, const std::string & basefilename ) const;
333 
334  std::string generateFilename( const RepoInfo & info ) const
335  { return filenameFromAlias( info.alias(), "repo" ); }
336 
337  std::string generateFilename( const ServiceInfo & info ) const
338  { return filenameFromAlias( info.alias(), "service" ); }
339 
340  void setCacheStatus( const RepoInfo & info, const RepoStatus & status )
341  {
342  Pathname base = solv_path_for_repoinfo( _options, info );
344  status.saveToCookieFile( base / "cookie" );
345  }
346 
347  void touchIndexFile( const RepoInfo & info );
348 
349  template<typename OutputIterator>
350  void getRepositoriesInService( const std::string & alias, OutputIterator out ) const
351  {
352  MatchServiceAlias filter( alias );
353  std::copy( boost::make_filter_iterator( filter, repos().begin(), repos().end() ),
354  boost::make_filter_iterator( filter, repos().end(), repos().end() ),
355  out);
356  }
357 
358  protected:
359  void init_knownServices();
360  void init_knownRepositories();
361 
362  const RepoSet & repos() const { return _reposX; }
363  RepoSet & reposManip() { if ( ! _reposDirty ) _reposDirty = true; return _reposX; }
364 
365  protected:
369 
371  };
372 
373 }
374 
375 #endif
Pathname filepath() const
File where this repo was read from.
ServiceSet::size_type ServiceSizeType
int assert_dir(const Pathname &path, unsigned mode)
Like &#39;mkdir -p&#39;.
Definition: PathInfo.cc:320
Service data.
Definition: ServiceInfo.h:36
Pathname path() const
Repository path.
Definition: RepoInfo.cc:635
Thrown when the repo alias is found to be invalid.
Pathname solv_path_for_repoinfo(const RepoManagerOptions &opt, const RepoInfo &info)
Calculates the solv cache path for a repository.
RepoStatus cacheStatus(const RepoInfo &info) const
#define _(MSG)
Definition: Gettext.h:37
ServiceSet::const_iterator ServiceConstIterator
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:429
std::string generateFilename(const RepoInfo &info) const
std::list< RepoInfo > repositories_in_file(const Pathname &file)
Reads RepoInfo&#39;s from a repo file.
std::string service() const
Gets name of the service to which this repository belongs or empty string if it has been added manual...
Definition: RepoInfo.cc:638
bool collect(const RepoInfo &repo)
ServiceConstIterator serviceEnd() const
bool hasRepo(const std::string &alias) const
RepoCollector(std::string targetDistro_)
ServiceConstIterator serviceBegin() const
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
static RepoStatus cacheStatus(const RepoInfo &info, const RepoManagerOptions &options)
Definition: Arch.h:363
What is known about a repository.
Definition: RepoInfo.h:71
Pathname packagescache_path_for_repoinfo(const RepoManagerOptions &opt, const RepoInfo &info)
Calculates the packages cache path for a repository.
std::string escaped_alias() const
Same as alias(), just escaped in a way to be a valid file name.
RepoInfo getRepo(const std::string &alias) const
std::string generateFilename(const ServiceInfo &info) const
static RepoStatus fromCookieFile(const Pathname &path)
Reads the status from a cookie file.
Definition: RepoStatus.cc:210
Service without alias was used in an operation.
Url::asString() view options.
Definition: UrlBase.h:39
Pathname rawcache_path_for_repoinfo(const RepoManagerOptions &opt, const RepoInfo &info)
Calculates the raw cache path for a repository, this is usually /var/cache/zypp/alias.
const RepoSet & repos() const
Repo manager settings.
bool hasService(const std::string &alias) const
void saveToCookieFile(const Pathname &path_r) const
Save the status information to a cookie file.
Definition: RepoStatus.cc:224
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
void assert_alias(const RepoInfo &info)
Iterator findAlias(const std::string &alias_r, Iterator begin_r, Iterator end_r)
Find alias_r in repo/service container.
std::string alias() const
unique identifier for this source.
bool isExist() const
Return whether valid stat info exists.
Definition: PathInfo.h:282
Pathname rawproductdata_path_for_repoinfo(const RepoManagerOptions &opt, const RepoInfo &info)
Calculates the raw product metadata path for a repository, this is inside the raw cache dir...
Pathname dirname() const
Return all but the last component od this path.
Definition: Pathname.h:124
#define OPT_PROGRESS
RepoManagerOptions _options
ServiceInfo getService(const std::string &alias) const
bool isValid() const
Verifies the Url.
Definition: Url.cc:493
Pathname packagesPath(const RepoInfo &info) const
Functor thats filter RepoInfo by service which it belongs to.
Pathname metadataPath() const
Path where this repo metadata was read from.
Definition: RepoInfo.cc:593
bool isTmpRepo(const RepoInfo &info_r)
Whether repo is not under RM control and provides its own methadata paths.
Url url() const
The service url.
Definition: ServiceInfo.cc:102
bool usesAutoMetadataPaths() const
Whether metadataPath uses AUTO% setup.
Definition: RepoInfo.cc:599
std::ostream & copy(std::istream &from_r, std::ostream &to_r)
Copy istream to ostream.
Definition: IOStream.h:51
Simple callback to collect the results.
void addService(const std::string &alias, const Url &url)
std::set< ServiceInfo > ServiceSet
std::string filenameFromAlias(const std::string &alias_r, const std::string &stem_r)
Generate a related filename from a repo/service infos alias.
void assert_urls(const RepoInfo &info)
void removeService(const ServiceInfo &service)
RepoSizeType repoSize() const
RepoConstIterator repoBegin() const
void assert_url(const ServiceInfo &info)
RepoSet::size_type RepoSizeType
DefaultIntegral< bool, false > _reposDirty
Functor collecting ServiceInfos into a ServiceSet.
std::set< RepoInfo > RepoSet
RepoInfo typedefs.
std::set< ServiceInfo > ServiceSet
ServiceInfo typedefs.
static const ServiceInfo noService
Represents an empty service.
Definition: ServiceInfo.h:61
Pathname packagesPath() const
Path where this repo packages are cached.
Definition: RepoInfo.cc:596
ServiceSizeType serviceSize() const
RepoSet::const_iterator RepoConstIterator
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:221
void getRepositoriesInService(const std::string &alias, OutputIterator out) const
static const RepoInfo noRepo
Represents no Repository (one with an empty alias).
Definition: RepoInfo.h:85
Track changing files or directories.
Definition: RepoStatus.h:40
bool foundAliasIn(const std::string &alias_r, Iterator begin_r, Iterator end_r)
Check if alias_r is present in repo/service container.
std::list< RepoInfo > repositories_in_dir(const Pathname &dir)
List of RepoInfo&#39;s from a directory.
Pathname metadataPath(const RepoInfo &info) const
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
SolvableIdType size_type
Definition: PoolMember.h:126
ServiceCollector(ServiceSet &services_r)
RepoConstIterator repoEnd() const
Service has no or invalid url defined.
bool isCached(const RepoInfo &info) const
void setCacheStatus(const RepoInfo &info, const RepoStatus &status)
Url manipulation class.
Definition: Url.h:91
#define ZYPP_LOCAL
Definition: Globals.h:59
bool autoPruneInDir(const Pathname &path_r)
bsc#1204956: Tweak to prevent auto pruning package caches.
bool operator()(const ServiceInfo &service_r) const
Repository type enumeration.
Definition: RepoType.h:27
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
bool operator()(const RepoInfo &info) const