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 Repository::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 CpeId & cpeid_r ) const
147 NO_REPOSITORY_RETURN( false );
148 for_( it, updatesProductBegin(), updatesProductEnd() )
150 if ( cpeid_r.match( it.cpeId() ) == CpeId::Match::subset )
156 bool Repository::isUpdateRepo() const
158 NO_REPOSITORY_RETURN( false );
159 return ( updatesProductBegin() != updatesProductEnd() );
162 bool Repository::solvablesEmpty() const
164 NO_REPOSITORY_RETURN( true );
165 return !_repo->nsolvables;
168 Repository::size_type Repository::solvablesSize() const
170 NO_REPOSITORY_RETURN( 0 );
171 return _repo->nsolvables;
174 Repository::SolvableIterator Repository::solvablesBegin() const
176 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
177 sat::detail::SolvableIterator(),
178 sat::detail::SolvableIterator() ) );
179 return make_filter_iterator( detail::ByRepository( *this ),
180 sat::detail::SolvableIterator(_repo->start),
181 sat::detail::SolvableIterator(_repo->end) );
184 Repository::SolvableIterator Repository::solvablesEnd() const
186 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
187 sat::detail::SolvableIterator(),
188 sat::detail::SolvableIterator() ) );
189 return make_filter_iterator(detail::ByRepository( *this ),
190 sat::detail::SolvableIterator(_repo->end),
191 sat::detail::SolvableIterator(_repo->end) );
194 Repository::ProductInfoIterator Repository::compatibleWithProductBegin() const
196 NO_REPOSITORY_RETURN( ProductInfoIterator() );
197 return ProductInfoIterator( sat::SolvAttr::repositoryDistros, *this );
200 Repository::ProductInfoIterator Repository::compatibleWithProductEnd() const
202 return ProductInfoIterator();
205 Repository::ProductInfoIterator Repository::updatesProductBegin() const
207 NO_REPOSITORY_RETURN( ProductInfoIterator() );
208 return ProductInfoIterator( sat::SolvAttr::repositoryUpdates, *this );
211 Repository::ProductInfoIterator Repository::updatesProductEnd() const
213 return ProductInfoIterator();
216 RepoInfo Repository::info() const
218 NO_REPOSITORY_RETURN( RepoInfo() );
219 return myPool().repoInfo( _repo );
222 void Repository::setInfo( const RepoInfo & info_r )
224 NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
225 if ( info_r.alias() != alias() )
227 ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
228 info_r.alias().c_str(), alias().c_str() ) ) );
230 myPool().setRepoInfo( _repo, info_r );
231 MIL << *this << endl;
234 void Repository::clearInfo()
236 NO_REPOSITORY_RETURN();
237 myPool().setRepoInfo( _repo, RepoInfo() );
240 void Repository::eraseFromPool()
242 NO_REPOSITORY_RETURN();
243 MIL << *this << " removed from pool" << endl;
244 myPool()._deleteRepo( _repo );
245 _id = sat::detail::noRepoId;
248 Repository Repository::nextInPool() const
250 NO_REPOSITORY_RETURN( noRepository );
251 for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
255 if ( ++it != _for_end )
263 void Repository::addSolv( const Pathname & file_r )
265 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
267 AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "re" ), ::fclose );
271 ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
274 if ( myPool()._addSolv( _repo, file ) != 0 )
276 ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
279 MIL << *this << " after adding " << file_r << endl;
282 void Repository::addHelix( const Pathname & file_r )
284 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
286 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
287 command += file_r.asString();
289 AutoDispose<FILE*> file( ::popen( command.c_str(), "re" ), ::pclose );
293 ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
296 if ( myPool()._addHelix( _repo, file ) != 0 )
298 ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
301 MIL << *this << " after adding " << file_r << endl;
304 sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
306 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
307 return myPool()._addSolvables( _repo, count_r );
310 /******************************************************************
312 ** FUNCTION NAME : operator<<
313 ** FUNCTION TYPE : std::ostream &
315 std::ostream & operator<<( std::ostream & str, const Repository & obj )
318 return str << "noRepository";
320 return str << "sat::repo(" << obj.alias() << ")"
322 << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
323 << ", size " << obj.solvablesSize()
327 std::ostream & dumpAsXmlOn( std::ostream & str, const Repository & obj )
329 return xmlout::node( str, "repository", {
330 { "alias", obj.name() },
331 { "name", obj.alias() }
335 //////////////////////////////////////////////////////////////////
338 void RepositoryIterator::increment()
342 ::_Pool * satpool = sat::Pool::instance().get();
345 } while ( base() < satpool->repos+satpool->nrepos && !*base() );
348 } // namespace detail
349 //////////////////////////////////////////////////////////////////
351 ///////////////////////////////////////////////////////////////////
353 // Repository::ProductInfoIterator
355 ///////////////////////////////////////////////////////////////////
357 Repository::ProductInfoIterator::ProductInfoIterator( sat::SolvAttr attr_r, Repository repo_r )
358 { base_reference() = sat::LookupRepoAttr( attr_r, repo_r ).begin(); }
360 std::string Repository::ProductInfoIterator::label() const
361 { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
363 CpeId Repository::ProductInfoIterator::cpeId() const
364 { return CpeId( base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(), CpeId::noThrow ); }
366 /////////////////////////////////////////////////////////////////
368 ///////////////////////////////////////////////////////////////////