libzypp  17.38.8
repository.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <climits>
13 #include <iostream>
14 #include <utility>
15 
17 #include <zypp-core/base/Gettext.h>
19 
20 #include <zypp-core/AutoDispose.h>
21 #include <zypp-core/Pathname.h>
22 
25 #include <zypp/ng/sat/pool.h>
26 #include <zypp/ng/sat/repository.h>
27 #include <zypp/ng/sat/lookupattr.h>
28 
29 using std::endl;
30 
32 namespace zyppng
33 {
34  namespace sat
35  {
36  namespace detail {
37  template<> Pool & poolFromType( Repository & r )
38  {
39  detail::CRepo * repo = r.get();
40  ZYPP_PRECONDITION( repo && repo->pool && repo->pool->appdata );
41  return *static_cast<Pool *>( repo->pool->appdata );
42  }
43  template<> const Pool & poolFromType( const Repository & r )
44  {
45  const detail::CRepo * repo = r.get();
46  ZYPP_PRECONDITION( repo && repo->pool && repo->pool->appdata );
47  return *static_cast<const Pool *>( repo->pool->appdata );
48  }
49  }
50 
52 
53  const std::string & Repository::systemRepoAlias()
54  { return Pool::systemRepoAlias(); }
55 
57 
59  { return _id; }
60 
61 #define NO_REPOSITORY_RETURN( VAL ) \
62  detail::CRepo * _repo( get() ); \
63  if ( ! _repo ) return VAL
64 
65 #define NO_REPOSITORY_THROW( VAL ) \
66  detail::CRepo * _repo( get() ); \
67  if ( ! _repo ) ZYPP_THROW( VAL )
68 
70  {
71  NO_REPOSITORY_RETURN( false );
72  return pool().isSystemRepo( _repo );
73  }
74 
75  std::string Repository::alias() const
76  {
77  NO_REPOSITORY_RETURN( std::string() );
78  if ( ! _repo->name )
79  return std::string();
80  return _repo->name;
81  }
82 
84  {
85  NO_REPOSITORY_RETURN( INT_MIN );
86  return _repo->priority;
87  }
88 
90  {
91  NO_REPOSITORY_RETURN( INT_MIN );
92  return _repo->subpriority;
93  }
94 
96  {
98  sat::LookupRepoAttr q( sat::SolvAttr::repositoryRevision, *this );
99  return q.empty() ? std::string() : (*q.begin()).asString();
100  }
101 
103  {
105  sat::LookupRepoAttr q( sat::SolvAttr::repositoryRepoid, *this );
106  return q.empty() ? std::string() : (*q.begin()).asString();
107  }
108 
110  {
111  NO_REPOSITORY_RETURN( false );
112 
113  sat::LookupRepoAttr q( sat::SolvAttr::repositoryRepoid, *this );
114  return ranges::any_of( q, [&id_r]( const sat::LookupAttrValue v ){
115  return v.asString() == id_r;
116  });
117  }
118 
120  {
122  sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
123  return( q.empty() ? 0 : (*q.begin()).asUnsigned() );
124 
125  }
126 
128  {
130  zypp::Date generated = generatedTimestamp();
131  if ( ! generated )
132  return 0; // do not calculate over a missing generated timestamp
133 
134  sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
135  if ( q.empty() )
136  return 0;
137 
138  return generated + zypp::Date((*q.begin()).asUnsigned());
139 
140  }
141 
143  {
144  NO_REPOSITORY_RETURN( false );
145  // system repo is not mirrored
146  if ( isSystemRepo() )
147  return false;
148 
150 
151  // if no data, don't suggest
152  if ( ! suggested )
153  return false;
154 
155  return suggested < zypp::Date::now();
156  }
157 
159  {
160  NO_REPOSITORY_RETURN( true );
161  return !_repo->nsolvables;
162  }
163 
165  {
167  return _repo->nsolvables;
168  }
169 
171  {
176  sat::detail::SolvableIterator(_repo->start),
177  sat::detail::SolvableIterator(_repo->end) );
178  }
179 
181  {
186  sat::detail::SolvableIterator(_repo->end),
187  sat::detail::SolvableIterator(_repo->end) );
188  }
189 
191  {
193  //MIL << *this << " removed from pool" << endl;
194  pool()._deleteRepo( _repo );
196  }
197 
199  {
201  auto iterable = pool().repos();
202  for( auto it = iterable.begin(); it != iterable.end(); ++it )
203  {
204  if ( *it == *this )
205  {
206  if ( ++it != iterable.end() )
207  return *it;
208  break;
209  }
210  }
211  return noRepository;
212  }
213 
214  void Repository::addSolv( const zypp::Pathname & file_r )
215  {
216  NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
217 
218  zypp::AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
219  if ( file == NULL )
220  {
221  file.resetDispose();
222  ZYPP_THROW( zypp::Exception( "Can't open solv-file: "+file_r.asString() ) );
223  }
224 
225  if ( pool()._addSolv( _repo, file ) != 0 )
226  {
227  ZYPP_THROW( zypp::Exception( "Error reading solv-file: "+file_r.asString() ) );
228  }
229 
230  //MIL << *this << " after adding " << file_r << endl;
231  }
232 
233  void Repository::addHelix( const zypp::Pathname & file_r )
234  {
235  NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
236 
237  std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
238  command += "'";
239  command += file_r.asString();
240  command += "'";
241 
242  zypp::AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
243  if ( file == NULL )
244  {
245  file.resetDispose();
246  ZYPP_THROW( zypp::Exception( "Can't open helix-file: "+file_r.asString() ) );
247  }
248 
249  if ( pool()._addHelix( _repo, file ) != 0 )
250  {
251  ZYPP_THROW( zypp::Exception( "Error reading helix-file: "+file_r.asString() ) );
252  }
253 
254  //MIL << *this << " after adding " << file_r << endl;
255  }
256 
258  {
259  NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
260 
261  std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
262  command += "'";
263  command += file_r.asString();
264  command += "'";
265 
266  zypp::AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
267  if ( file == NULL )
268  {
269  file.resetDispose();
270  ZYPP_THROW( zypp::Exception( "Can't open testtags-file: "+file_r.asString() ) );
271  }
272 
273  if ( pool()._addTesttags( _repo, file ) != 0 )
274  {
275  ZYPP_THROW( zypp::Exception( "Error reading testtags-file: "+file_r.asString() ) );
276  }
277 
278  //MIL << *this << " after adding " << file_r << endl;
279  }
280 
282  {
283  NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo.") );
284  return pool()._addSolvables( _repo, count_r );
285  }
286 
287  namespace detail
288  {
290  {
291  if ( _base )
292  {
293  sat::detail::CPool * satpool = (*_base)->pool;
294  do {
295  ++_base;
296  } while ( _base < satpool->repos+satpool->nrepos && !*_base );
297  }
298  }
299  }
300 
301  } // namespace sat
302 } // namespace zyppng
RepositoryIterable repos() const
Iteratable to the repositories.
Definition: pool.cc:208
std::string alias() const
Short unique string to identify a repo.
Definition: repository.cc:75
ContentIdentifier contentIdentifier() const
Unique string identifying a repositories content.
Definition: repository.cc:102
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:459
Pool & poolFromType(T &)
Repository nextInPool() const
Return next Repository in Pool (or noRepository).
Definition: repository.cc:198
Lightweight repository attribute value lookup.
Definition: lookupattr.h:258
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition: Patch.cc:122
bool solvablesEmpty() const
Hint whether the Repo may provide updates for a product.
Definition: repository.cc:158
#define NO_REPOSITORY_RETURN(VAL)
Definition: repository.cc:61
A simple forward iterator that filters a base range according to a predicate.
Definition: iterators.h:39
static const RepoIdType noRepoId(0)
Id to denote Repo::noRepository.
bool isSystemRepo(detail::CRepo *repo_r) const
Definition: pool.h:132
const char * c_str() const
String representation.
Definition: Pathname.h:113
Value proxy returned by LookupAttr::iterator::operator*().
Definition: lookupattr.h:453
Orchestrator for a libsolv pool instance.
Definition: pool.h:36
void addSolv(const zypp::Pathname &file_r)
Load Solvables from a solv-file.
Definition: repository.cc:214
std::string ContentRevision
Definition: repository.h:60
zypp::sat::detail::CPool CPool
Definition: poolconstants.h:36
void addHelix(const zypp::Pathname &file_r)
Load Solvables from a helix-file.
Definition: repository.cc:233
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: repository.cc:53
detail::SolvableIdType _addSolvables(detail::CRepo *repo_r, unsigned count_r)
Adding Solvables to a repo.
Definition: pool.cc:318
bool empty() const
Whether the query is empty.
Definition: lookupattr.cc:246
Store and operate on date (time_t).
Definition: Date.h:32
int satInternalPriority() const
libsolv internal priorities.
Definition: repository.cc:83
std::string asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: lookupattr.cc:575
Iterate over valid Solvables in the pool.
Definition: solvable.h:414
const std::string & asString() const
String representation.
Definition: Pathname.h:94
detail::CRepo * get() const
Expert backdoor.
Definition: repository.cc:58
#define NO_REPOSITORY_THROW(VAL)
Definition: repository.cc:65
static const Repository noRepository
Represents no Repository.
Definition: repository.h:79
void addTesttags(const zypp::Pathname &file_r)
Load Solvables from a libsolv testtags-file.
Definition: repository.cc:257
SolvableIterator solvablesEnd() const
Iterator behind the last Solvable.
Definition: repository.cc:180
iterator begin() const
Iterator to the begin of query results.
Definition: lookupattr.cc:240
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:171
void eraseFromPool()
Query class for Repository related products.
Definition: repository.cc:190
Namespace routing for C++20 ranges and C++23 ranges::to<T>() backport.
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
Definition: repository.cc:170
zypp::sat::detail::CRepo CRepo
Definition: poolconstants.h:38
ContentRevision contentRevision() const
Label to display for this repo.
Definition: repository.cc:95
zypp::Date suggestedExpirationTimestamp() const
Suggested expiration timestamp.
Definition: repository.cc:127
unsigned int size_type
Definition: repository.h:55
Base class for Exception.
Definition: Exception.h:152
FilterIterator< Pred, Base > make_filter_iterator(Pred p, Base it, Base end)
Helper function to deduce types and construct a FilterIterator.
Definition: iterators.h:116
bool any_of(const Container &c, Fnc &&cb)
Definition: Algorithm.h:76
int satInternalSubPriority() const
Definition: repository.cc:89
static Date now()
Return the current time.
Definition: Date.h:78
detail::SolvableIdType IdType
Definition: solvable.h:68
zypp::Date generatedTimestamp() const
Timestamp when this repository was generated.
Definition: repository.cc:119
size_type solvablesSize() const
Number of solvables in Repository.
Definition: repository.cc:164
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:94
#define ZYPP_PRECONDITION(EXPR,...)
Always-on precondition check — fires in debug AND release builds.
Definition: precondition.h:42
sat::Solvable::IdType addSolvables(unsigned count_r)
Add count_r new empty Solvable to this Repository.
Definition: repository.cc:281
bool maybeOutdated() const
repository keywords (tags)
Definition: repository.cc:142
void _deleteRepo(detail::CRepo *repo_r)
Delete repo repo_r from pool.
Definition: pool.cc:277
std::string ContentIdentifier
Definition: repository.h:61
bool isSystemRepo() const
Return whether this is the system repository.
Definition: repository.cc:69
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: pool.cc:178
bool hasContentIdentifier(const ContentIdentifier &id_r) const
Whether id_r matches this repos content identifier.
Definition: repository.cc:109
Always-on precondition checking for NG code.
Functor filtering Solvable by Repository.
Definition: repository.h:522
std::string extension() const
Return all of the characters in name after and including the last dot in the last element of name...
Definition: Pathname.h:144