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"
19 #include "zypp/AutoDispose.h"
20 #include "zypp/Pathname.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 int Repository::satInternalPriority() const
69 NO_REPOSITORY_RETURN( INT_MIN );
70 return _repo->priority;
73 int Repository::satInternalSubPriority() const
75 NO_REPOSITORY_RETURN( INT_MIN );
76 return _repo->subpriority;
80 zypp::Date Repository::generatedTimestamp() const
82 NO_REPOSITORY_RETURN( 0 );
83 sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
84 return( q.empty() ? 0 : q.begin().asUnsigned() );
87 zypp::Date Repository::suggestedExpirationTimestamp() const
89 NO_REPOSITORY_RETURN( 0 );
90 Date generated = generatedTimestamp();
92 return 0; // do not calculate over a missing generated timestamp
94 sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
98 return generated + q.begin().asUnsigned();
101 Repository::Keywords Repository::keywords() const
103 NO_REPOSITORY_RETURN( Keywords() );
104 return Keywords( sat::SolvAttr::repositoryKeywords, *this, sat::LookupAttr::REPO_ATTR );
107 bool Repository::maybeOutdated() const
109 NO_REPOSITORY_RETURN( false );
110 // system repo is not mirrored
111 if ( isSystemRepo() )
114 Date suggested = suggestedExpirationTimestamp();
116 // if no data, don't suggest
120 return suggestedExpirationTimestamp() < Date::now();
123 bool Repository::providesUpdatesFor( const std::string &key ) const
125 NO_REPOSITORY_RETURN( false );
128 updatesProductBegin(),
129 updatesProductEnd() )
131 // FIXME implement real CPE matching here
133 if ( key == it.cpeId() )
140 bool Repository::isUpdateRepo() const
142 NO_REPOSITORY_RETURN( false );
143 return ( updatesProductBegin() != updatesProductEnd() );
146 bool Repository::solvablesEmpty() const
148 NO_REPOSITORY_RETURN( true );
149 return !_repo->nsolvables;
152 Repository::size_type Repository::solvablesSize() const
154 NO_REPOSITORY_RETURN( 0 );
155 return _repo->nsolvables;
158 Repository::SolvableIterator Repository::solvablesBegin() const
160 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
161 sat::detail::SolvableIterator(),
162 sat::detail::SolvableIterator() ) );
163 return make_filter_iterator( detail::ByRepository( *this ),
164 sat::detail::SolvableIterator(_repo->start),
165 sat::detail::SolvableIterator(_repo->end) );
168 Repository::SolvableIterator Repository::solvablesEnd() const
170 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
171 sat::detail::SolvableIterator(),
172 sat::detail::SolvableIterator() ) );
173 return make_filter_iterator(detail::ByRepository( *this ),
174 sat::detail::SolvableIterator(_repo->end),
175 sat::detail::SolvableIterator(_repo->end) );
178 Repository::ProductInfoIterator Repository::compatibleWithProductBegin() const
180 NO_REPOSITORY_RETURN( ProductInfoIterator() );
181 return ProductInfoIterator( sat::SolvAttr::repositoryDistros, *this );
184 Repository::ProductInfoIterator Repository::compatibleWithProductEnd() const
186 return ProductInfoIterator();
189 Repository::ProductInfoIterator Repository::updatesProductBegin() const
191 NO_REPOSITORY_RETURN( ProductInfoIterator() );
192 return ProductInfoIterator( sat::SolvAttr::repositoryUpdates, *this );
195 Repository::ProductInfoIterator Repository::updatesProductEnd() const
197 return ProductInfoIterator();
200 RepoInfo Repository::info() const
202 NO_REPOSITORY_RETURN( RepoInfo() );
203 return myPool().repoInfo( _repo );
206 void Repository::setInfo( const RepoInfo & info_r )
208 NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
209 if ( info_r.alias() != alias() )
211 ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
212 info_r.alias().c_str(), alias().c_str() ) ) );
214 myPool().setRepoInfo( _repo, info_r );
215 MIL << *this << endl;
218 void Repository::clearInfo()
220 NO_REPOSITORY_RETURN();
221 myPool().setRepoInfo( _repo, RepoInfo() );
224 void Repository::eraseFromPool()
226 NO_REPOSITORY_RETURN();
227 MIL << *this << " removed from pool" << endl;
228 myPool()._deleteRepo( _repo );
229 _id = sat::detail::noRepoId;
232 Repository Repository::nextInPool() const
234 NO_REPOSITORY_RETURN( noRepository );
235 for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
239 if ( ++it != _for_end )
247 void Repository::addSolv( const Pathname & file_r )
249 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
251 AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "r" ), ::fclose );
255 ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
258 if ( myPool()._addSolv( _repo, file ) != 0 )
260 ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
263 MIL << *this << " after adding " << file_r << endl;
266 void Repository::addHelix( const Pathname & file_r )
268 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
270 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
271 command += file_r.asString();
273 AutoDispose<FILE*> file( ::popen( command.c_str(), "r" ), ::pclose );
277 ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
280 if ( myPool()._addHelix( _repo, file ) != 0 )
282 ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
285 MIL << *this << " after adding " << file_r << endl;
288 sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
290 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
291 return myPool()._addSolvables( _repo, count_r );
294 /******************************************************************
296 ** FUNCTION NAME : operator<<
297 ** FUNCTION TYPE : std::ostream &
299 std::ostream & operator<<( std::ostream & str, const Repository & obj )
302 return str << "noRepository";
304 return str << "sat::repo(" << obj.alias() << ")"
306 << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
307 << ", size " << obj.solvablesSize()
311 ///////////////////////////////////////////////////////////////////
313 // Repository::ProductInfoIterator
315 ///////////////////////////////////////////////////////////////////
317 Repository::ProductInfoIterator::ProductInfoIterator( sat::SolvAttr attr_r, Repository repo_r )
318 { base_reference() = sat::LookupRepoAttr( attr_r, repo_r ).begin(); }
320 std::string Repository::ProductInfoIterator::label() const
321 { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
323 std::string Repository::ProductInfoIterator::cpeId() const
324 { return base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(); }
326 /////////////////////////////////////////////////////////////////
328 ///////////////////////////////////////////////////////////////////