libzypp  17.38.7
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 
24 #include <zypp/ng/sat/pool.h>
25 #include <zypp/ng/sat/repository.h>
26 #include <zypp/ng/sat/lookupattr.h>
27 
28 using std::endl;
29 
31 namespace zyppng
32 {
33  namespace sat
34  {
35  namespace detail {
36  template<> Pool & poolFromType( Repository & r )
37  {
38  detail::CRepo * repo = r.get();
39  ZYPP_PRECONDITION( repo && repo->pool && repo->pool->appdata );
40  return *static_cast<Pool *>( repo->pool->appdata );
41  }
42  template<> const Pool & poolFromType( const Repository & r )
43  {
44  const detail::CRepo * repo = r.get();
45  ZYPP_PRECONDITION( repo && repo->pool && repo->pool->appdata );
46  return *static_cast<const Pool *>( repo->pool->appdata );
47  }
48  }
49 
51 
52  const std::string & Repository::systemRepoAlias()
53  { return Pool::systemRepoAlias(); }
54 
56 
58  { return _id; }
59 
60 #define NO_REPOSITORY_RETURN( VAL ) \
61  detail::CRepo * _repo( get() ); \
62  if ( ! _repo ) return VAL
63 
64 #define NO_REPOSITORY_THROW( VAL ) \
65  detail::CRepo * _repo( get() ); \
66  if ( ! _repo ) ZYPP_THROW( VAL )
67 
69  {
70  NO_REPOSITORY_RETURN( false );
71  return pool().isSystemRepo( _repo );
72  }
73 
74  std::string Repository::alias() const
75  {
76  NO_REPOSITORY_RETURN( std::string() );
77  if ( ! _repo->name )
78  return std::string();
79  return _repo->name;
80  }
81 
83  {
84  NO_REPOSITORY_RETURN( INT_MIN );
85  return _repo->priority;
86  }
87 
89  {
90  NO_REPOSITORY_RETURN( INT_MIN );
91  return _repo->subpriority;
92  }
93 
95  {
97  sat::LookupRepoAttr q( sat::SolvAttr::repositoryRevision, *this );
98  return q.empty() ? std::string() : q.begin().asString();
99  }
100 
102  {
104  sat::LookupRepoAttr q( sat::SolvAttr::repositoryRepoid, *this );
105  return q.empty() ? std::string() : q.begin().asString();
106  }
107 
109  {
110  NO_REPOSITORY_RETURN( false );
111 
112  sat::LookupRepoAttr q( sat::SolvAttr::repositoryRepoid, *this );
113  for_( it, q.begin(), q.end() )
114  if ( it.asString() == id_r )
115  return true;
116 
117  return false;
118  }
119 
121  {
123  sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
124  return( q.empty() ? 0 : q.begin().asUnsigned() );
125 
126  }
127 
129  {
131  zypp::Date generated = generatedTimestamp();
132  if ( ! generated )
133  return 0; // do not calculate over a missing generated timestamp
134 
135  sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
136  if ( q.empty() )
137  return 0;
138 
139  return generated + zypp::Date(q.begin().asUnsigned());
140 
141  }
142 
144  {
145  NO_REPOSITORY_RETURN( false );
146  // system repo is not mirrored
147  if ( isSystemRepo() )
148  return false;
149 
151 
152  // if no data, don't suggest
153  if ( ! suggested )
154  return false;
155 
156  return suggested < zypp::Date::now();
157  }
158 
160  {
161  NO_REPOSITORY_RETURN( true );
162  return !_repo->nsolvables;
163  }
164 
166  {
168  return _repo->nsolvables;
169  }
170 
172  {
177  sat::detail::SolvableIterator(_repo->start),
178  sat::detail::SolvableIterator(_repo->end) );
179  }
180 
182  {
187  sat::detail::SolvableIterator(_repo->end),
188  sat::detail::SolvableIterator(_repo->end) );
189  }
190 
192  {
194  //MIL << *this << " removed from pool" << endl;
195  pool()._deleteRepo( _repo );
197  }
198 
200  {
202  auto iterable = pool().repos();
203  for( auto it = iterable.begin(); it != iterable.end(); ++it )
204  {
205  if ( *it == *this )
206  {
207  if ( ++it != iterable.end() )
208  return *it;
209  break;
210  }
211  }
212  return noRepository;
213  }
214 
215  void Repository::addSolv( const zypp::Pathname & file_r )
216  {
217  NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
218 
219  zypp::AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
220  if ( file == NULL )
221  {
222  file.resetDispose();
223  ZYPP_THROW( zypp::Exception( "Can't open solv-file: "+file_r.asString() ) );
224  }
225 
226  if ( pool()._addSolv( _repo, file ) != 0 )
227  {
228  ZYPP_THROW( zypp::Exception( "Error reading solv-file: "+file_r.asString() ) );
229  }
230 
231  //MIL << *this << " after adding " << file_r << endl;
232  }
233 
234  void Repository::addHelix( const zypp::Pathname & file_r )
235  {
236  NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
237 
238  std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
239  command += "'";
240  command += file_r.asString();
241  command += "'";
242 
243  zypp::AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
244  if ( file == NULL )
245  {
246  file.resetDispose();
247  ZYPP_THROW( zypp::Exception( "Can't open helix-file: "+file_r.asString() ) );
248  }
249 
250  if ( pool()._addHelix( _repo, file ) != 0 )
251  {
252  ZYPP_THROW( zypp::Exception( "Error reading helix-file: "+file_r.asString() ) );
253  }
254 
255  //MIL << *this << " after adding " << file_r << endl;
256  }
257 
259  {
260  NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo." ) );
261 
262  std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
263  command += "'";
264  command += file_r.asString();
265  command += "'";
266 
267  zypp::AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
268  if ( file == NULL )
269  {
270  file.resetDispose();
271  ZYPP_THROW( zypp::Exception( "Can't open testtags-file: "+file_r.asString() ) );
272  }
273 
274  if ( pool()._addTesttags( _repo, file ) != 0 )
275  {
276  ZYPP_THROW( zypp::Exception( "Error reading testtags-file: "+file_r.asString() ) );
277  }
278 
279  //MIL << *this << " after adding " << file_r << endl;
280  }
281 
283  {
284  NO_REPOSITORY_THROW( zypp::Exception( "Can't add solvables to norepo.") );
285  return pool()._addSolvables( _repo, count_r );
286  }
287 
288  namespace detail
289  {
291  {
292  if ( _base )
293  {
294  sat::detail::CPool * satpool = (*_base)->pool;
295  do {
296  ++_base;
297  } while ( _base < satpool->repos+satpool->nrepos && !*_base );
298  }
299  }
300  }
301 
302  } // namespace sat
303 } // 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:74
ContentIdentifier contentIdentifier() const
Unique string identifying a repositories content.
Definition: repository.cc:101
#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:199
Lightweight repository attribute value lookup.
Definition: lookupattr.h:256
bool solvablesEmpty() const
Hint whether the Repo may provide updates for a product.
Definition: repository.cc:159
#define NO_REPOSITORY_RETURN(VAL)
Definition: repository.cc:60
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
Orchestrator for a libsolv pool instance.
Definition: pool.h:36
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
void addSolv(const zypp::Pathname &file_r)
Load Solvables from a solv-file.
Definition: repository.cc:215
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:234
static const std::string & systemRepoAlias()
Reserved system repository alias .
Definition: repository.cc:52
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
std::string asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: lookupattr.cc:566
Store and operate on date (time_t).
Definition: Date.h:32
int satInternalPriority() const
libsolv internal priorities.
Definition: repository.cc:82
Iterate over valid Solvables in the pool.
Definition: solvable.h:410
const std::string & asString() const
String representation.
Definition: Pathname.h:94
detail::CRepo * get() const
Expert backdoor.
Definition: repository.cc:57
#define NO_REPOSITORY_THROW(VAL)
Definition: repository.cc:64
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:258
SolvableIterator solvablesEnd() const
Iterator behind the last Solvable.
Definition: repository.cc:181
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:191
SolvableIterator solvablesBegin() const
Iterator to the first Solvable.
Definition: repository.cc:171
zypp::sat::detail::CRepo CRepo
Definition: poolconstants.h:38
ContentRevision contentRevision() const
Label to display for this repo.
Definition: repository.cc:94
zypp::Date suggestedExpirationTimestamp() const
Suggested expiration timestamp.
Definition: repository.cc:128
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
int satInternalSubPriority() const
Definition: repository.cc:88
static Date now()
Return the current time.
Definition: Date.h:78
detail::SolvableIdType IdType
Definition: solvable.h:67
zypp::Date generatedTimestamp() const
Timestamp when this repository was generated.
Definition: repository.cc:120
size_type solvablesSize() const
Number of solvables in Repository.
Definition: repository.cc:165
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:282
iterator end() const
Iterator behind the end of query results.
Definition: lookupattr.cc:243
bool maybeOutdated() const
repository keywords (tags)
Definition: repository.cc:143
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:68
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:108
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