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 std::string Repository::name() const
64 { return info().name(); }
66 zypp::Date Repository::generatedTimestamp() const
68 NO_REPOSITORY_RETURN( 0 );
69 sat::LookupRepoAttr q( sat::SolvAttr::repositoryTimestamp, *this );
70 return( q.empty() ? 0 : q.begin().asUnsigned() );
73 zypp::Date Repository::suggestedExpirationTimestamp() const
75 NO_REPOSITORY_RETURN( 0 );
76 Date generated = generatedTimestamp();
78 return 0; // do not calculate over a missing generated timestamp
80 sat::LookupRepoAttr q( sat::SolvAttr::repositoryExpire, *this );
84 return generated + q.begin().asUnsigned();
87 Repository::Keywords Repository::keywords() const
89 NO_REPOSITORY_RETURN( Keywords() );
90 return Keywords( sat::SolvAttr::repositoryKeywords, *this, sat::LookupAttr::REPO_ATTR );
93 bool Repository::maybeOutdated() const
95 NO_REPOSITORY_RETURN( false );
96 // system repo is not mirrored
100 Date suggested = suggestedExpirationTimestamp();
102 // if no data, don't suggest
106 return suggestedExpirationTimestamp() < Date::now();
109 bool Repository::providesUpdatesFor( const std::string &key ) const
111 NO_REPOSITORY_RETURN( false );
114 updatesProductBegin(),
115 updatesProductEnd() )
117 // FIXME implement real CPE matching here
119 if ( key == it.cpeId() )
126 bool Repository::isUpdateRepo() const
128 NO_REPOSITORY_RETURN( false );
129 return ( updatesProductBegin() != updatesProductEnd() );
132 bool Repository::solvablesEmpty() const
134 NO_REPOSITORY_RETURN( true );
135 return !_repo->nsolvables;
138 Repository::size_type Repository::solvablesSize() const
140 NO_REPOSITORY_RETURN( 0 );
141 return _repo->nsolvables;
144 Repository::SolvableIterator Repository::solvablesBegin() const
146 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
147 sat::detail::SolvableIterator(),
148 sat::detail::SolvableIterator() ) );
149 return make_filter_iterator( detail::ByRepository( *this ),
150 sat::detail::SolvableIterator(_repo->start),
151 sat::detail::SolvableIterator(_repo->end) );
154 Repository::SolvableIterator Repository::solvablesEnd() const
156 NO_REPOSITORY_RETURN( make_filter_iterator( detail::ByRepository( *this ),
157 sat::detail::SolvableIterator(),
158 sat::detail::SolvableIterator() ) );
159 return make_filter_iterator(detail::ByRepository( *this ),
160 sat::detail::SolvableIterator(_repo->end),
161 sat::detail::SolvableIterator(_repo->end) );
164 Repository::ProductInfoIterator Repository::compatibleWithProductBegin() const
166 NO_REPOSITORY_RETURN( ProductInfoIterator() );
167 return ProductInfoIterator( sat::SolvAttr::repositoryDistros, *this );
170 Repository::ProductInfoIterator Repository::compatibleWithProductEnd() const
172 return ProductInfoIterator();
175 Repository::ProductInfoIterator Repository::updatesProductBegin() const
177 NO_REPOSITORY_RETURN( ProductInfoIterator() );
178 return ProductInfoIterator( sat::SolvAttr::repositoryUpdates, *this );
181 Repository::ProductInfoIterator Repository::updatesProductEnd() const
183 return ProductInfoIterator();
186 RepoInfo Repository::info() const
188 NO_REPOSITORY_RETURN( RepoInfo() );
189 return myPool().repoInfo( _repo );
192 void Repository::setInfo( const RepoInfo & info_r )
194 NO_REPOSITORY_THROW( Exception( "Can't set RepoInfo for norepo." ) );
195 if ( info_r.alias() != alias() )
197 ZYPP_THROW( Exception( str::form( "RepoInfo alias (%s) does not match repository alias (%s)",
198 info_r.alias().c_str(), alias().c_str() ) ) );
200 myPool().setRepoInfo( _repo, info_r );
201 MIL << *this << endl;
204 void Repository::clearInfo()
206 NO_REPOSITORY_RETURN();
207 myPool().setRepoInfo( _repo, RepoInfo() );
210 void Repository::eraseFromPool()
212 NO_REPOSITORY_RETURN();
213 MIL << *this << " removed from pool" << endl;
214 myPool()._deleteRepo( _repo );
215 _id = sat::detail::noRepoId;
218 Repository Repository::nextInPool() const
220 NO_REPOSITORY_RETURN( noRepository );
221 for_( it, sat::Pool::instance().reposBegin(), sat::Pool::instance().reposEnd() )
225 if ( ++it != _for_end )
233 void Repository::addSolv( const Pathname & file_r )
235 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
237 AutoDispose<FILE*> file( ::fopen( file_r.c_str(), "r" ), ::fclose );
241 ZYPP_THROW( Exception( "Can't open solv-file: "+file_r.asString() ) );
244 if ( myPool()._addSolv( _repo, file ) != 0 )
246 ZYPP_THROW( Exception( "Error reading solv-file: "+file_r.asString() ) );
249 MIL << *this << " after adding " << file_r << endl;
252 void Repository::addHelix( const Pathname & file_r )
254 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo." ) );
256 std::string command( file_r.extension() == ".gz" ? "zcat " : "cat " );
257 command += file_r.asString();
259 AutoDispose<FILE*> file( ::popen( command.c_str(), "r" ), ::pclose );
263 ZYPP_THROW( Exception( "Can't open helix-file: "+file_r.asString() ) );
266 if ( myPool()._addHelix( _repo, file ) != 0 )
268 ZYPP_THROW( Exception( "Error reading helix-file: "+file_r.asString() ) );
271 MIL << *this << " after adding " << file_r << endl;
274 sat::detail::SolvableIdType Repository::addSolvables( unsigned count_r )
276 NO_REPOSITORY_THROW( Exception( "Can't add solvables to norepo.") );
277 return myPool()._addSolvables( _repo, count_r );
280 /******************************************************************
282 ** FUNCTION NAME : operator<<
283 ** FUNCTION TYPE : std::ostream &
285 std::ostream & operator<<( std::ostream & str, const Repository & obj )
288 return str << "noRepository";
290 return str << "sat::repo(" << obj.alias() << ")"
292 << "prio " << obj.get()->priority << '.' << obj.get()->subpriority
293 << ", size " << obj.solvablesSize()
297 ///////////////////////////////////////////////////////////////////
299 // Repository::ProductInfoIterator
301 ///////////////////////////////////////////////////////////////////
303 Repository::ProductInfoIterator::ProductInfoIterator( sat::SolvAttr attr_r, Repository repo_r )
304 { base_reference() = sat::LookupRepoAttr( attr_r, repo_r ).begin(); }
306 std::string Repository::ProductInfoIterator::label() const
307 { return base_reference().subFind( sat::SolvAttr::repositoryProductLabel ).asString(); }
309 std::string Repository::ProductInfoIterator::cpeId() const
310 { return base_reference().subFind( sat::SolvAttr::repositoryProductCpeid ).asString(); }
312 /////////////////////////////////////////////////////////////////
314 ///////////////////////////////////////////////////////////////////