1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/Repository.cc
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/Gettext.h"
16 #include "zypp/base/Exception.h"
18 #include "zypp/AutoDispose.h"
19 #include "zypp/Pathname.h"
20 #include "zypp/media/MediaPriority.h"
22 #include "zypp/sat/detail/PoolImpl.h"
23 #include "zypp/Repository.h"
24 #include "zypp/sat/Pool.h"
28 ///////////////////////////////////////////////////////////////////
30 { /////////////////////////////////////////////////////////////////
32 const Repository Repository::noRepository;
34 const std::string & Repository::systemRepoAlias()
35 { return sat::detail::PoolImpl::systemRepoAlias(); }
37 /////////////////////////////////////////////////////////////////
39 ::_Repo * Repository::get() const
40 { return myPool().getRepo( _id ); }
42 #define NO_REPOSITORY_RETURN( VAL ) \
43 ::_Repo * _repo( get() ); \
44 if ( ! _repo ) return VAL
46 #define NO_REPOSITORY_THROW( VAL ) \
47 ::_Repo * _repo( get() ); \
48 if ( ! _repo ) ZYPP_THROW( VAL )
50 bool Repository::isSystemRepo() const
52 NO_REPOSITORY_RETURN( false );
53 return myPool().isSystemRepo( _repo );
56 std::string Repository::alias() const
58 NO_REPOSITORY_RETURN( std::string() );
64 std::string Repository::name() const
65 { return info().name(); }
67 zypp::Date Repository::generatedTimestamp() const
69 NO_REPOSITORY_RETURN( 0 );
70 sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
71 return( q.empty() ? 0 : q.begin().asUnsigned() );
74 zypp::Date Repository::suggestedExpirationTimestamp() const
76 NO_REPOSITORY_RETURN( 0 );
77 Date generated = generatedTimestamp();
79 return 0; // do not calculate over a missing generated timestamp
81 sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
85 return generated + q.begin().asUnsigned();
88 Repository::Keywords Repository::keywords() const
90 NO_REPOSITORY_RETURN( Keywords() );
91 return Keywords( sat::SolvAttr::repositoryKeywords, *this, sat::LookupAttr::REPO_ATTR );
94 bool Repository::maybeOutdated() const
96 NO_REPOSITORY_RETURN( false );
97 // system repo is not mirrored
101 Date suggested = suggestedExpirationTimestamp();
103 // if no data, don't suggest
107 return suggestedExpirationTimestamp() < Date::now();
110 bool Repository::providesUpdatesFor( const std::string &key ) const
112 NO_REPOSITORY_RETURN( false );
115 updatesProductBegin(),
116 updatesProductEnd() )
118 // FIXME implement real CPE matching here
120 if ( key == it.cpeId() )
127 bool Repository::isUpdateRepo() const
129 NO_REPOSITORY_RETURN( false );
130 return ( updatesProductBegin() != updatesProductEnd() );
133 bool Repository::solvablesEmpty() const
135 NO_REPOSITORY_RETURN( true );
136 return !_repo->nsolvables;
139 Repository::size_type Repository::solvablesSize() const
141 NO_REPOSITORY_RETURN( 0 );
142 return _repo->nsolvables;
145 Repository::SolvableIterator Repository::solvablesBegin() const
147 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
148 sat::detail::SolvableIterator(),
149 sat::detail::SolvableIterator() ) );
150 return make_filter_iterator( detail::ByRepository( *this ),
151 sat::detail::SolvableIterator(_repo->start),
152 sat::detail::SolvableIterator(_repo->end) );
155 Repository::SolvableIterator Repository::solvablesEnd() const
157 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
158 sat::detail::SolvableIterator(),
159 sat::detail::SolvableIterator() ) );
160 return make_filter_iterator(detail::ByRepository( *this ),
161 sat::detail::SolvableIterator(_repo->end),
162 sat::detail::SolvableIterator(_repo->end) );
165 Repository::ProductInfoIterator Repository::compatibleWithProductBegin() const
167 NO_REPOSITORY_RETURN( ProductInfoIterator() );
168 return ProductInfoIterator( sat::SolvAttr::repositoryDistros, *this );
171 Repository::ProductInfoIterator Repository::compatibleWithProductEnd() const
173 return ProductInfoIterator();
176 Repository::ProductInfoIterator Repository::updatesProductBegin() const
178 NO_REPOSITORY_RETURN( ProductInfoIterator() );
179 return ProductInfoIterator( sat::SolvAttr::repositoryUpdates, *this );
182 Repository::ProductInfoIterator Repository::updatesProductEnd() const
184 return ProductInfoIterator();
187 RepoInfo Repository::info() const
189 NO_REPOSITORY_RETURN( RepoInfo() );
190 return myPool().repoInfo( _repo );
193 void Repository::setInfo( const RepoInfo & info_r )
195 NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
196 if ( info_r.alias() != alias() )
198 ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
199 info_r.alias().c_str(), alias().c_str() ) ) );
201 myPool().setRepoInfo( _repo, info_r );
203 // satsolver priority is based on '<', while yum's repoinfo
204 // uses 1(highest)->99(lowest). Thus we use -info_r.priority.
205 _repo->priority = -info_r.priority();
207 // subpriority is used to e.g. prefer http over dvd iff
208 // both have same priority.
209 _repo->subpriority = media::MediaPriority( info_r.url() );
211 MIL << *this << endl;
214 void Repository::clearInfo()
216 NO_REPOSITORY_RETURN();
217 myPool().setRepoInfo( _repo, RepoInfo() );
220 void Repository::eraseFromPool()
222 NO_REPOSITORY_RETURN();
223 MIL << *this << " removed from pool" << endl;
224 myPool()._deleteRepo( _repo );
225 _id = sat::detail::noRepoId;
228 Repository Repository::nextInPool() const
230 NO_REPOSITORY_RETURN( noRepository );
231 for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
235 if ( ++it != _for_end )
243 void Repository::addSolv( const Pathname & file_r )
245 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
247 AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "r" ), ::fclose );
251 ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
254 if ( myPool()._addSolv( _repo, file ) != 0 )
256 ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
259 MIL << *this << " after adding " << file_r << endl;
262 void Repository::addHelix( const Pathname & file_r )
264 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
266 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
267 command += file_r.asString();
269 AutoDispose<FILE*> file( ::popen( command.c_str(), "r" ), ::pclose );
273 ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
276 if ( myPool()._addHelix( _repo, file ) != 0 )
278 ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
281 MIL << *this << " after adding " << file_r << endl;
284 sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
286 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
287 return myPool()._addSolvables( _repo, count_r );
290 /******************************************************************
292 ** FUNCTION NAME : operator<<
293 ** FUNCTION TYPE : std::ostream &
295 std::ostream & operator<<( std::ostream & str, const Repository & obj )
298 return str << "noRepository";
300 return str << "sat::repo(" << obj.alias() << ")"
302 << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
303 << ", size " << obj.solvablesSize()
307 ///////////////////////////////////////////////////////////////////
309 // Repository::ProductInfoIterator
311 ///////////////////////////////////////////////////////////////////
313 Repository::ProductInfoIterator::ProductInfoIterator( sat::SolvAttr attr_r, Repository repo_r )
314 { base_reference() = sat::LookupRepoAttr( attr_r, repo_r ).begin(); }
316 std::string Repository::ProductInfoIterator::label() const
317 { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
319 std::string Repository::ProductInfoIterator::cpeId() const
320 { return base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(); }
322 /////////////////////////////////////////////////////////////////
324 ///////////////////////////////////////////////////////////////////