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"
21 #include "zypp/sat/detail/PoolImpl.h"
22 #include "zypp/Repository.h"
23 #include "zypp/sat/Pool.h"
27 ///////////////////////////////////////////////////////////////////
29 { /////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////
32 const Repository Repository::noRepository;
34 /////////////////////////////////////////////////////////////////
36 ::_Repo * Repository::get() const
37 { return myPool().getRepo( _id ); }
39 #define NO_REPOSITORY_RETURN( VAL ) \
40 ::_Repo * _repo( get() ); \
41 if ( ! _repo ) return VAL
43 #define NO_REPOSITORY_THROW( VAL ) \
44 ::_Repo * _repo( get() ); \
45 if ( ! _repo ) ZYPP_THROW( VAL )
47 bool Repository::isSystemRepo() const
49 NO_REPOSITORY_RETURN( false );
50 return( sat::Pool::systemRepoAlias() == _repo->name );
53 std::string Repository::alias() const
55 NO_REPOSITORY_RETURN( std::string() );
61 #warning FIX to iterator
62 zypp::Date Repository::generatedTimestamp() const
65 ::dataiterator_init(&di, get(), SOLVID_META, REPOSITORY_TIMESTAMP, 0, 0);
66 if (::dataiterator_step(&di))
72 case REPOSITORY_TIMESTAMP:
79 while (::dataiterator_step(&di));
85 ERR << "the attribute generated timestamp does not exist in the repo" << endl;
92 zypp::Date Repository::suggestedExpirationTimestamp() const
95 ::dataiterator_init(&di, get(), SOLVID_META, REPOSITORY_EXPIRE, 0, 0);
96 Date generated = generatedTimestamp();
97 // do not calculate over a missing generated
99 if ( generated == Date() )
102 if (::dataiterator_step(&di))
106 switch (di.key->name)
108 case REPOSITORY_EXPIRE:
110 return generated + di.kv.num;
115 while (::dataiterator_step(&di));
119 if ( isSystemRepo() )
121 ERR << "the attribute suggested expiration timestamp does not exist in the repo" << endl;
127 Repository::Keywords Repository::keywords() const
128 { return Keywords(sat::SolvAttr::repositoryKeywords); }
130 Repository::Products Repository::products() const
131 { return Products(sat::SolvAttr::repositoryProducts); }
133 bool Repository::maybeOutdated() const
135 // system repo is not mirrored
136 if ( isSystemRepo() )
139 Date suggested = suggestedExpirationTimestamp();
141 // if no data, don't suggest
142 if ( suggested == Date() )
145 return suggestedExpirationTimestamp() < Date::now();
148 bool Repository::providesUpdatesFor( const std::string &key ) const
153 bool Repository::isUpdateRepo() const
158 bool Repository::solvablesEmpty() const
160 NO_REPOSITORY_RETURN( true );
161 return !_repo->nsolvables;
164 Repository::size_type Repository::solvablesSize() const
166 NO_REPOSITORY_RETURN( 0 );
167 return _repo->nsolvables;
170 Repository::SolvableIterator Repository::solvablesBegin() const
172 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
173 sat::detail::SolvableIterator(),
174 sat::detail::SolvableIterator() ) );
175 return make_filter_iterator( detail::ByRepository( *this ),
176 sat::detail::SolvableIterator(_repo->start),
177 sat::detail::SolvableIterator(_repo->end) );
180 Repository::SolvableIterator Repository::solvablesEnd() 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->end),
187 sat::detail::SolvableIterator(_repo->end) );
190 RepoInfo Repository::info() const
192 NO_REPOSITORY_RETURN( RepoInfo() );
193 return myPool().repoInfo( _repo );
196 void Repository::setInfo( const RepoInfo & info_r )
198 NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
199 if ( info_r.alias() != alias() )
201 ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
202 info_r.alias().c_str(), alias().c_str() ) ) );
204 myPool().setRepoInfo( _repo, info_r );
206 // satsolver priority is based on '<', while yum's repoinfo
207 // uses 1(highest)->99(lowest). Thus we use -info_r.priority.
208 _repo->priority = -info_r.priority();
211 void Repository::clearInfo()
213 NO_REPOSITORY_RETURN();
214 myPool().setRepoInfo( _repo, RepoInfo() );
217 void Repository::eraseFromPool()
219 NO_REPOSITORY_RETURN();
220 myPool()._deleteRepo( _repo );
221 _id = sat::detail::noRepoId;
224 Repository Repository::nextInPool() const
226 NO_REPOSITORY_RETURN( noRepository );
227 for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
231 if ( ++it != _for_end )
239 #warning NEED POOL MANIP EXEPTIONS
240 void Repository::addSolv( const Pathname & file_r )
242 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
244 AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "r" ), ::fclose );
248 ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ));
251 if ( myPool()._addSolv( _repo, file, isSystemRepo() ) != 0 )
253 ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ));
257 sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
259 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
260 return myPool()._addSolvables( _repo, count_r );
263 /******************************************************************
265 ** FUNCTION NAME : operator<<
266 ** FUNCTION TYPE : std::ostream &
268 std::ostream & operator<<( std::ostream & str, const Repository & obj )
271 return str << "noRepository";
273 return str << "sat::repo(" << obj.alias() << ")"
275 << "prio " << obj.get()->priority
276 << ", size " << obj.solvablesSize()
281 /////////////////////////////////////////////////////////////////
283 ///////////////////////////////////////////////////////////////////