1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/Repository.cc
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/Exception.h"
18 #include "zypp/base/Xml.h"
20 #include "zypp/AutoDispose.h"
21 #include "zypp/Pathname.h"
23 #include "zypp/sat/detail/PoolImpl.h"
24 #include "zypp/Repository.h"
25 #include "zypp/sat/Pool.h"
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
33 const Repository Repository::noRepository;
35 const std::string & Repository::systemRepoAlias()
36 { return sat::detail::PoolImpl::systemRepoAlias(); }
38 /////////////////////////////////////////////////////////////////
40 ::_Repo * Repository::get() const
41 { return myPool().getRepo( _id ); }
43 #define NO_REPOSITORY_RETURN( VAL ) \
44 ::_Repo * _repo( get() ); \
45 if ( ! _repo ) return VAL
47 #define NO_REPOSITORY_THROW( VAL ) \
48 ::_Repo * _repo( get() ); \
49 if ( ! _repo ) ZYPP_THROW( VAL )
51 bool Repository::isSystemRepo() const
53 NO_REPOSITORY_RETURN( false );
54 return myPool().isSystemRepo( _repo );
57 std::string Repository::alias() const
59 NO_REPOSITORY_RETURN( std::string() );
65 std::string Repository::name() const
66 { return info().name(); }
68 int Repository::satInternalPriority() const
70 NO_REPOSITORY_RETURN( INT_MIN );
71 return _repo->priority;
74 int Repository::satInternalSubPriority() const
76 NO_REPOSITORY_RETURN( INT_MIN );
77 return _repo->subpriority;
80 Repository::ContentRevision contentRevision() const
82 NO_REPOSITORY_RETURN( ContentRevision() );
83 sat::LookupRepoAttr q( sat::SolvAttr::repositoryRevision, *this );
84 return q.empty() ? std::string() : q.begin().asString();
87 Repository::ContentIdentifier Repository::contentIdentifier() const
89 NO_REPOSITORY_RETURN( ContentIdentifier() );
90 sat::LookupRepoAttr q( sat::SolvAttr::repositoryRepoid, *this );
91 return q.empty() ? std::string() : q.begin().asString();
94 zypp::Date Repository::generatedTimestamp() const
96 NO_REPOSITORY_RETURN( 0 );
97 sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
98 return( q.empty() ? 0 : q.begin().asUnsigned() );
101 zypp::Date Repository::suggestedExpirationTimestamp() const
103 NO_REPOSITORY_RETURN( 0 );
104 Date generated = generatedTimestamp();
106 return 0; // do not calculate over a missing generated timestamp
108 sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
112 return generated + Date(q.begin().asUnsigned());
115 Repository::Keywords Repository::keywords() const
117 NO_REPOSITORY_RETURN( Keywords() );
118 return Keywords( sat::SolvAttr::repositoryKeywords, *this, sat::LookupAttr::REPO_ATTR );
121 bool Repository::hasKeyword( const std::string & val_r ) const
123 for ( const auto & val : keywords() )
129 bool Repository::maybeOutdated() const
131 NO_REPOSITORY_RETURN( false );
132 // system repo is not mirrored
133 if ( isSystemRepo() )
136 Date suggested = suggestedExpirationTimestamp();
138 // if no data, don't suggest
142 return suggestedExpirationTimestamp() < Date::now();
145 bool Repository::providesUpdatesFor( const std::string &key ) const
147 NO_REPOSITORY_RETURN( false );
150 updatesProductBegin(),
151 updatesProductEnd() )
153 // FIXME implement real CPE matching here
155 if ( key == it.cpeId() )
162 bool Repository::isUpdateRepo() const
164 NO_REPOSITORY_RETURN( false );
165 return ( updatesProductBegin() != updatesProductEnd() );
168 bool Repository::solvablesEmpty() const
170 NO_REPOSITORY_RETURN( true );
171 return !_repo->nsolvables;
174 Repository::size_type Repository::solvablesSize() const
176 NO_REPOSITORY_RETURN( 0 );
177 return _repo->nsolvables;
180 Repository::SolvableIterator Repository::solvablesBegin() const
182 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
183 sat::detail::SolvableIterator(),
184 sat::detail::SolvableIterator() ) );
185 return make_filter_iterator( detail::ByRepository( *this ),
186 sat::detail::SolvableIterator(_repo->start),
187 sat::detail::SolvableIterator(_repo->end) );
190 Repository::SolvableIterator Repository::solvablesEnd() const
192 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
193 sat::detail::SolvableIterator(),
194 sat::detail::SolvableIterator() ) );
195 return make_filter_iterator(detail::ByRepository( *this ),
196 sat::detail::SolvableIterator(_repo->end),
197 sat::detail::SolvableIterator(_repo->end) );
200 Repository::ProductInfoIterator Repository::compatibleWithProductBegin() const
202 NO_REPOSITORY_RETURN( ProductInfoIterator() );
203 return ProductInfoIterator( sat::SolvAttr::repositoryDistros, *this );
206 Repository::ProductInfoIterator Repository::compatibleWithProductEnd() const
208 return ProductInfoIterator();
211 Repository::ProductInfoIterator Repository::updatesProductBegin() const
213 NO_REPOSITORY_RETURN( ProductInfoIterator() );
214 return ProductInfoIterator( sat::SolvAttr::repositoryUpdates, *this );
217 Repository::ProductInfoIterator Repository::updatesProductEnd() const
219 return ProductInfoIterator();
222 RepoInfo Repository::info() const
224 NO_REPOSITORY_RETURN( RepoInfo() );
225 return myPool().repoInfo( _repo );
228 void Repository::setInfo( const RepoInfo & info_r )
230 NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
231 if ( info_r.alias() != alias() )
233 ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
234 info_r.alias().c_str(), alias().c_str() ) ) );
236 myPool().setRepoInfo( _repo, info_r );
237 MIL << *this << endl;
240 void Repository::clearInfo()
242 NO_REPOSITORY_RETURN();
243 myPool().setRepoInfo( _repo, RepoInfo() );
246 void Repository::eraseFromPool()
248 NO_REPOSITORY_RETURN();
249 MIL << *this << " removed from pool" << endl;
250 myPool()._deleteRepo( _repo );
251 _id = sat::detail::noRepoId;
254 Repository Repository::nextInPool() const
256 NO_REPOSITORY_RETURN( noRepository );
257 for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
261 if ( ++it != _for_end )
269 void Repository::addSolv( const Pathname & file_r )
271 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
273 AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
277 ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
280 if ( myPool()._addSolv( _repo, file ) != 0 )
282 ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
285 MIL << *this << " after adding " << file_r << endl;
288 void Repository::addHelix( const Pathname & file_r )
290 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
292 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
293 command += file_r.asString();
295 AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
299 ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
302 if ( myPool()._addHelix( _repo, file ) != 0 )
304 ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
307 MIL << *this << " after adding " << file_r << endl;
310 sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
312 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
313 return myPool()._addSolvables( _repo, count_r );
316 /******************************************************************
318 ** FUNCTION NAME : operator<<
319 ** FUNCTION TYPE : std::ostream &
321 std::ostream & operator<<( std::ostream & str, const Repository & obj )
324 return str << "noRepository";
326 return str << "sat::repo(" << obj.alias() << ")"
328 << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
329 << ", size " << obj.solvablesSize()
333 std::ostream & dumpAsXmlOn( std::ostream & str, const Repository & obj )
335 return xmlout::node( str, "repository", {
336 { "alias", obj.name() },
337 { "name", obj.alias() }
341 //////////////////////////////////////////////////////////////////
344 void RepositoryIterator::increment()
348 ::_Pool * satpool = sat::Pool::instance().get();
351 } while ( base() < satpool->repos+satpool->nrepos && !*base() );
354 } // namespace detail
355 //////////////////////////////////////////////////////////////////
357 ///////////////////////////////////////////////////////////////////
359 // Repository::ProductInfoIterator
361 ///////////////////////////////////////////////////////////////////
363 Repository::ProductInfoIterator::ProductInfoIterator( sat::SolvAttr attr_r, Repository repo_r )
364 { base_reference() = sat::LookupRepoAttr( attr_r, repo_r ).begin(); }
366 std::string Repository::ProductInfoIterator::label() const
367 { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
369 std::string Repository::ProductInfoIterator::cpeId() const
370 { return base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(); }
372 /////////////////////////////////////////////////////////////////
374 ///////////////////////////////////////////////////////////////////