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 zypp::Date Repository::generatedTimestamp() const
64 ::dataiterator_init(&di, get(), -1, 0, 0, SEARCH_EXTRA | SEARCH_NO_STORAGE_SOLVABLE);
65 if (::dataiterator_step(&di))
71 case REPOSITORY_TIMESTAMP:
78 while (::dataiterator_step(&di));
84 ERR << "the attribute generated timestamp does not exist in the repo" << endl;
91 zypp::Date Repository::suggestedExpirationTimestamp() const
94 ::dataiterator_init(&di, get(), -1, 0, 0, SEARCH_EXTRA | SEARCH_NO_STORAGE_SOLVABLE);
95 Date generated = generatedTimestamp();
96 // do not calculate over a missing generated
98 if ( generated == Date() )
101 if (::dataiterator_step(&di))
105 switch (di.key->name)
107 case REPOSITORY_EXPIRE:
109 return generated + di.kv.num;
114 while (::dataiterator_step(&di));
118 if ( isSystemRepo() )
120 ERR << "the attribute suggested expiration timestamp does not exist in the repo" << endl;
126 bool Repository::maybeOutdated() const
128 // system repo is not mirrored
129 if ( isSystemRepo() )
132 Date suggested = suggestedExpirationTimestamp();
134 // if no data, don't suggest
135 if ( suggested == Date() )
138 return suggestedExpirationTimestamp() < Date::now();
141 Repository::UpdateKeys Repository::updateKeys() const
142 { return UpdateKeys( sat::SolvAttr::repositoryUpdates, *this ); }
144 bool Repository::providesUpdatesForKey( const std::string &key ) const
146 UpdateKeys keys(updateKeys());
147 return ( keys.find(key) != keys.end() );
150 bool Repository::isUpdateRepo() const
152 return ( ! updateKeys().empty() );
155 bool Repository::solvablesEmpty() const
157 NO_REPOSITORY_RETURN( true );
158 return !_repo->nsolvables;
161 Repository::size_type Repository::solvablesSize() const
163 NO_REPOSITORY_RETURN( 0 );
164 return _repo->nsolvables;
167 Repository::SolvableIterator Repository::solvablesBegin() const
169 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
170 sat::detail::SolvableIterator(),
171 sat::detail::SolvableIterator() ) );
172 return make_filter_iterator( detail::ByRepository( *this ),
173 sat::detail::SolvableIterator(_repo->start),
174 sat::detail::SolvableIterator(_repo->end) );
177 Repository::SolvableIterator Repository::solvablesEnd() const
179 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
180 sat::detail::SolvableIterator(),
181 sat::detail::SolvableIterator() ) );
182 return make_filter_iterator(detail::ByRepository( *this ),
183 sat::detail::SolvableIterator(_repo->end),
184 sat::detail::SolvableIterator(_repo->end) );
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();
208 void Repository::clearInfo()
210 NO_REPOSITORY_RETURN();
211 myPool().setRepoInfo( _repo, RepoInfo() );
214 void Repository::eraseFromPool()
216 NO_REPOSITORY_RETURN();
217 myPool()._deleteRepo( _repo );
218 _id = sat::detail::noRepoId;
221 Repository Repository::nextInPool() const
223 NO_REPOSITORY_RETURN( noRepository );
224 for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
228 if ( ++it != _for_end )
236 #warning NEED POOL MANIP EXEPTIONS
237 void Repository::addSolv( const Pathname & file_r )
239 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
241 AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "r" ), ::fclose );
245 ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ));
248 if ( myPool()._addSolv( _repo, file, isSystemRepo() ) != 0 )
250 ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ));
254 sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
256 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
257 return myPool()._addSolvables( _repo, count_r );
260 /******************************************************************
262 ** FUNCTION NAME : operator<<
263 ** FUNCTION TYPE : std::ostream &
265 std::ostream & operator<<( std::ostream & str, const Repository & obj )
268 return str << "noRepository";
270 return str << "sat::repo(" << obj.alias() << ")"
272 << "prio " << obj.get()->priority
273 << ", size " << obj.solvablesSize()
278 /////////////////////////////////////////////////////////////////
280 ///////////////////////////////////////////////////////////////////