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 { /////////////////////////////////////////////////////////////////
31 const Repository Repository::noRepository;
33 const std::string & Repository::systemRepoAlias()
34 { return sat::detail::PoolImpl::systemRepoAlias(); }
36 /////////////////////////////////////////////////////////////////
38 ::_Repo * Repository::get() const
39 { return myPool().getRepo( _id ); }
41 #define NO_REPOSITORY_RETURN( VAL ) \
42 ::_Repo * _repo( get() ); \
43 if ( ! _repo ) return VAL
45 #define NO_REPOSITORY_THROW( VAL ) \
46 ::_Repo * _repo( get() ); \
47 if ( ! _repo ) ZYPP_THROW( VAL )
49 bool Repository::isSystemRepo() const
51 NO_REPOSITORY_RETURN( false );
52 return myPool().isSystemRepo( _repo );
55 std::string Repository::alias() const
57 NO_REPOSITORY_RETURN( std::string() );
63 zypp::Date Repository::generatedTimestamp() const
65 NO_REPOSITORY_RETURN( 0 );
66 sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
67 return( q.empty() ? 0 : q.begin().asUnsigned() );
70 zypp::Date Repository::suggestedExpirationTimestamp() const
72 NO_REPOSITORY_RETURN( 0 );
73 Date generated = generatedTimestamp();
75 return 0; // do not calculate over a missing generated timestamp
77 sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
81 return generated + q.begin().asUnsigned();
84 Repository::Keywords Repository::keywords() const
86 NO_REPOSITORY_RETURN( Keywords() );
87 return Keywords( sat::SolvAttr::repositoryKeywords, *this, sat::LookupAttr::REPO_ATTR );
90 bool Repository::maybeOutdated() const
92 NO_REPOSITORY_RETURN( false );
93 // system repo is not mirrored
97 Date suggested = suggestedExpirationTimestamp();
99 // if no data, don't suggest
103 return suggestedExpirationTimestamp() < Date::now();
106 bool Repository::providesUpdatesFor( const std::string &key ) const
108 NO_REPOSITORY_RETURN( false );
111 updatesProductBegin(),
112 updatesProductEnd() )
114 // FIXME implement real CPE matching here
116 if ( key == it.cpeId() )
123 bool Repository::isUpdateRepo() const
125 NO_REPOSITORY_RETURN( false );
126 return ( updatesProductBegin() != updatesProductEnd() );
129 bool Repository::solvablesEmpty() const
131 NO_REPOSITORY_RETURN( true );
132 return !_repo->nsolvables;
135 Repository::size_type Repository::solvablesSize() const
137 NO_REPOSITORY_RETURN( 0 );
138 return _repo->nsolvables;
141 Repository::SolvableIterator Repository::solvablesBegin() const
143 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
144 sat::detail::SolvableIterator(),
145 sat::detail::SolvableIterator() ) );
146 return make_filter_iterator( detail::ByRepository( *this ),
147 sat::detail::SolvableIterator(_repo->start),
148 sat::detail::SolvableIterator(_repo->end) );
151 Repository::SolvableIterator Repository::solvablesEnd() const
153 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
154 sat::detail::SolvableIterator(),
155 sat::detail::SolvableIterator() ) );
156 return make_filter_iterator(detail::ByRepository( *this ),
157 sat::detail::SolvableIterator(_repo->end),
158 sat::detail::SolvableIterator(_repo->end) );
161 Repository::ProductInfoIterator Repository::compatibleWithProductBegin() const
163 NO_REPOSITORY_RETURN( ProductInfoIterator() );
164 return ProductInfoIterator( sat::SolvAttr::repositoryDistros, *this );
167 Repository::ProductInfoIterator Repository::compatibleWithProductEnd() const
169 return ProductInfoIterator();
172 Repository::ProductInfoIterator Repository::updatesProductBegin() const
174 NO_REPOSITORY_RETURN( ProductInfoIterator() );
175 return ProductInfoIterator( sat::SolvAttr::repositoryUpdates, *this );
178 Repository::ProductInfoIterator Repository::updatesProductEnd() const
180 return ProductInfoIterator();
183 RepoInfo Repository::info() const
185 NO_REPOSITORY_RETURN( RepoInfo() );
186 return myPool().repoInfo( _repo );
189 void Repository::setInfo( const RepoInfo & info_r )
191 NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
192 if ( info_r.alias() != alias() )
194 ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
195 info_r.alias().c_str(), alias().c_str() ) ) );
197 myPool().setRepoInfo( _repo, info_r );
199 // satsolver priority is based on '<', while yum's repoinfo
200 // uses 1(highest)->99(lowest). Thus we use -info_r.priority.
201 _repo->priority = -info_r.priority();
202 // subpriority could be used to e.g. prefer http over dvd iff both have same priority.
203 // _repo->subpriority = 0;
206 void Repository::clearInfo()
208 NO_REPOSITORY_RETURN();
209 myPool().setRepoInfo( _repo, RepoInfo() );
212 void Repository::eraseFromPool()
214 NO_REPOSITORY_RETURN();
215 MIL << *this << " removed from pool" << endl;
216 myPool()._deleteRepo( _repo );
217 _id = sat::detail::noRepoId;
220 Repository Repository::nextInPool() const
222 NO_REPOSITORY_RETURN( noRepository );
223 for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
227 if ( ++it != _for_end )
235 void Repository::addSolv( const Pathname & file_r )
237 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
239 AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "r" ), ::fclose );
243 ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
246 if ( myPool()._addSolv( _repo, file ) != 0 )
248 ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
251 MIL << *this << " after adding " << file_r << endl;
254 void Repository::addHelix( const Pathname & file_r )
256 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
258 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
259 command += file_r.asString();
261 AutoDispose<FILE*> file( ::popen( command.c_str(), "r" ), ::pclose );
265 ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
268 if ( myPool()._addHelix( _repo, file ) != 0 )
270 ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
273 MIL << *this << " after adding " << file_r << endl;
276 sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
278 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
279 return myPool()._addSolvables( _repo, count_r );
282 /******************************************************************
284 ** FUNCTION NAME : operator<<
285 ** FUNCTION TYPE : std::ostream &
287 std::ostream & operator<<( std::ostream & str, const Repository & obj )
290 return str << "noRepository";
292 return str << "sat::repo(" << obj.alias() << ")"
294 << "prio " << obj.get()->priority
295 << ", size " << obj.solvablesSize()
299 ///////////////////////////////////////////////////////////////////
301 // Repository::ProductInfoIterator
303 ///////////////////////////////////////////////////////////////////
305 Repository::ProductInfoIterator::ProductInfoIterator( sat::SolvAttr attr_r, Repository repo_r )
306 { base_reference() = sat::LookupRepoAttr( attr_r, repo_r ).begin(); }
308 std::string Repository::ProductInfoIterator::label() const
309 { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
311 std::string Repository::ProductInfoIterator::cpeId() const
312 { return base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(); }
314 /////////////////////////////////////////////////////////////////
316 ///////////////////////////////////////////////////////////////////