1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/detail/PoolImpl.cc
14 #include <boost/mpl/int.hpp>
16 #include "zypp/base/Easy.h"
17 #include "zypp/base/LogTools.h"
18 #include "zypp/base/Gettext.h"
19 #include "zypp/base/Exception.h"
20 #include "zypp/base/Measure.h"
21 #include "zypp/base/WatchFile.h"
22 #include "zypp/base/Sysconfig.h"
23 #include "zypp/base/IOStream.h"
25 #include "zypp/ZConfig.h"
27 #include "zypp/sat/detail/PoolImpl.h"
28 #include "zypp/sat/Pool.h"
29 #include "zypp/Capability.h"
30 #include "zypp/Locale.h"
31 #include "zypp/PoolItem.h"
33 #include "zypp/target/modalias/Modalias.h"
34 #include "zypp/media/MediaPriority.h"
38 // Workaround libsolv project not providing a common include
39 // directory. (the -devel package does, but the git repo doesn't).
40 // #include <solv/repo_helix.h>
41 int repo_add_helix( ::Repo *repo, FILE *fp, int flags );
46 #undef ZYPP_BASE_LOGGER_LOGGROUP
47 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::satpool"
49 // ///////////////////////////////////////////////////////////////////
52 /////////////////////////////////////////////////////////////////
56 inline int LIBSOLV_DEBUGMASK()
58 const char * envp = getenv("LIBSOLV_DEBUGMASK");
59 return envp ? str::strtonum<int>( envp ) : 0;
62 ///////////////////////////////////////////////////////////////////
64 { /////////////////////////////////////////////////////////////////
66 ///////////////////////////////////////////////////////////////////
68 { /////////////////////////////////////////////////////////////////
70 // MPL checks for satlib constants we redefine to avoid
71 // includes and defines.
72 BOOST_MPL_ASSERT_RELATION( noId, ==, STRID_NULL );
73 BOOST_MPL_ASSERT_RELATION( emptyId, ==, STRID_EMPTY );
75 BOOST_MPL_ASSERT_RELATION( noSolvableId, ==, ID_NULL );
76 BOOST_MPL_ASSERT_RELATION( systemSolvableId, ==, SYSTEMSOLVABLE );
78 BOOST_MPL_ASSERT_RELATION( solvablePrereqMarker, ==, SOLVABLE_PREREQMARKER );
79 BOOST_MPL_ASSERT_RELATION( solvableFileMarker, ==, SOLVABLE_FILEMARKER );
81 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_AND, ==, REL_AND );
82 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_OR, ==, REL_OR );
83 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_WITH, ==, REL_WITH );
84 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_NAMESPACE, ==, REL_NAMESPACE );
85 BOOST_MPL_ASSERT_RELATION( CapDetail::CAP_ARCH, ==, REL_ARCH );
87 /////////////////////////////////////////////////////////////////
89 const std::string & PoolImpl::systemRepoAlias()
91 static const std::string _val( "@System" );
95 const Pathname & sysconfigStoragePath()
97 static const Pathname _val( "/etc/sysconfig/storage" );
102 /////////////////////////////////////////////////////////////////
104 static void logSat( struct _Pool *, void *data, int type, const char *logString )
106 if ( type & (SOLV_FATAL|SOLV_ERROR) ) {
107 _ERR("libsolv") << logString;
108 } else if ( type & SOLV_DEBUG_STATS ) {
109 _DBG("libsolv") << logString;
111 _MIL("libsolv") << logString;
115 detail::IdType PoolImpl::nsCallback( struct _Pool *, void * data, detail::IdType lhs, detail::IdType rhs )
117 // lhs: the namespace identifier, e.g. NAMESPACE:MODALIAS
118 // rhs: the value, e.g. pci:v0000104Cd0000840[01]sv*sd*bc*sc*i*
119 // return: 0 if not supportded
120 // 1 if supported by the system
121 // -1 AFAIK it's also possible to return a list of solvables that support it, but don't know how.
123 static const detail::IdType RET_unsupported = 0;
124 static const detail::IdType RET_systemProperty = 1;
127 case NAMESPACE_LANGUAGE:
129 static IdString en( "en" );
130 const std::tr1::unordered_set<IdString> & locale2Solver( reinterpret_cast<PoolImpl*>(data)->_locale2Solver );
131 if ( locale2Solver.empty() )
133 return rhs == en.id() ? RET_systemProperty : RET_unsupported;
135 return locale2Solver.find( IdString(rhs) ) != locale2Solver.end() ? RET_systemProperty : RET_unsupported;
139 case NAMESPACE_MODALIAS:
141 // modalias strings in capability may be hexencoded because rpm does not allow
142 // ',', ' ' or other special chars.
143 return target::Modalias::instance().query( str::hexdecode( IdString(rhs).c_str() ) )
149 case NAMESPACE_FILESYSTEM:
151 const std::set<std::string> & requiredFilesystems( reinterpret_cast<PoolImpl*>(data)->requiredFilesystems() );
152 return requiredFilesystems.find( IdString(rhs).asString() ) != requiredFilesystems.end() ? RET_systemProperty : RET_unsupported;
158 WAR << "Unhandled " << Capability( lhs ) << " vs. " << Capability( rhs ) << endl;
159 return RET_unsupported;
162 ///////////////////////////////////////////////////////////////////
164 // METHOD NAME : PoolMember::myPool
165 // METHOD TYPE : PoolImpl
167 PoolImpl & PoolMember::myPool()
169 static PoolImpl _global;
173 ///////////////////////////////////////////////////////////////////
175 // METHOD NAME : PoolImpl::PoolImpl
176 // METHOD TYPE : Ctor
179 : _pool( ::pool_create() )
181 MIL << "Creating sat-pool." << endl;
184 ZYPP_THROW( Exception( _("Can not create sat-pool.") ) );
186 // initialialize logging
187 if ( env::LIBSOLV_DEBUGMASK() )
189 ::pool_setdebugmask(_pool, env::LIBSOLV_DEBUGMASK() );
193 if ( getenv("ZYPP_LIBSOLV_FULLLOG") || getenv("ZYPP_LIBSAT_FULLLOG") )
194 ::pool_setdebuglevel( _pool, 3 );
195 else if ( getenv("ZYPP_FULLLOG") )
196 ::pool_setdebuglevel( _pool, 2 );
198 ::pool_setdebugmask(_pool, SOLV_DEBUG_JOB|SOLV_DEBUG_STATS );
201 ::pool_setdebugcallback( _pool, logSat, NULL );
203 // set namespace callback
204 _pool->nscallback = &nsCallback;
205 _pool->nscallbackdata = (void*)this;
208 ///////////////////////////////////////////////////////////////////
210 // METHOD NAME : PoolImpl::~PoolImpl
211 // METHOD TYPE : Dtor
213 PoolImpl::~PoolImpl()
215 ::pool_free( _pool );
218 ///////////////////////////////////////////////////////////////////
220 void PoolImpl::setDirty( const char * a1, const char * a2, const char * a3 )
224 if ( a3 ) MIL << a1 << " " << a2 << " " << a3 << endl;
225 else if ( a2 ) MIL << a1 << " " << a2 << endl;
226 else MIL << a1 << endl;
228 _serial.setDirty(); // pool content change
229 _availableLocalesPtr.reset(); // available locales may change
230 _multiversionListPtr.reset(); // re-evaluate ZConfig::multiversionSpec.
232 // invaldate dependency/namespace related indices:
236 void PoolImpl::depSetDirty( const char * a1, const char * a2, const char * a3 )
240 if ( a3 ) MIL << a1 << " " << a2 << " " << a3 << endl;
241 else if ( a2 ) MIL << a1 << " " << a2 << endl;
242 else MIL << a1 << endl;
244 ::pool_freewhatprovides( _pool );
247 void PoolImpl::prepare() const
249 if ( _watcher.remember( _serial ) )
251 // After repo/solvable add/remove:
252 // set pool architecture
253 ::pool_setarch( _pool, ZConfig::instance().systemArchitecture().asString().c_str() );
255 if ( ! _pool->whatprovides )
257 MIL << "pool_createwhatprovides..." << endl;
259 ::pool_addfileprovides( _pool );
260 ::pool_createwhatprovides( _pool );
262 if ( ! _pool->languages )
265 const_cast<PoolImpl*>(this)->setTextLocale( ZConfig::instance().textLocale() );
269 void PoolImpl::prepareForSolving() const
271 // additional /etc/sysconfig/storage check:
272 static WatchFile sysconfigFile( sysconfigStoragePath(), WatchFile::NO_INIT );
273 if ( sysconfigFile.hasChanged() )
275 _requiredFilesystemsPtr.reset(); // recreated on demand
276 const_cast<PoolImpl*>(this)->depSetDirty( "/etc/sysconfig/storage change" );
278 // finally prepare as usual:
282 ///////////////////////////////////////////////////////////////////
284 ::_Repo * PoolImpl::_createRepo( const std::string & name_r )
286 setDirty(__FUNCTION__, name_r.c_str() );
287 ::_Repo * ret = ::repo_create( _pool, name_r.c_str() );
288 if ( ret && name_r == systemRepoAlias() )
289 ::pool_set_installed( _pool, ret );
293 void PoolImpl::_deleteRepo( ::_Repo * repo_r )
295 setDirty(__FUNCTION__, repo_r->name );
296 ::repo_free( repo_r, /*reuseids*/false );
297 eraseRepoInfo( repo_r );
298 if ( isSystemRepo( repo_r ) )
301 _onSystemByUserListPtr.reset(); // re-evaluate
305 int PoolImpl::_addSolv( ::_Repo * repo_r, FILE * file_r )
307 setDirty(__FUNCTION__, repo_r->name );
308 int ret = ::repo_add_solv( repo_r, file_r, 0 );
310 _postRepoAdd( repo_r );
314 int PoolImpl::_addHelix( ::_Repo * repo_r, FILE * file_r )
316 setDirty(__FUNCTION__, repo_r->name );
317 int ret = ::repo_add_helix( repo_r, file_r, 0 );
319 _postRepoAdd( repo_r );
323 void PoolImpl::_postRepoAdd( ::_Repo * repo_r )
325 if ( ! isSystemRepo( repo_r ) )
327 // Filter out unwanted archs
328 std::set<detail::IdType> sysids;
330 Arch::CompatSet sysarchs( Arch::compatSet( ZConfig::instance().systemArchitecture() ) );
331 for_( it, sysarchs.begin(), sysarchs.end() )
332 sysids.insert( it->id() );
334 // unfortunately libsolv treats src/nosrc as architecture:
335 sysids.insert( ARCH_SRC );
336 sysids.insert( ARCH_NOSRC );
339 detail::IdType blockBegin = 0;
340 unsigned blockSize = 0;
341 for ( detail::IdType i = repo_r->start; i < repo_r->end; ++i )
343 ::_Solvable * s( _pool->solvables + i );
344 if ( s->repo == repo_r && sysids.find( s->arch ) == sysids.end() )
346 // Remember an unwanted arch entry:
351 else if ( blockSize )
353 // Free remembered entries
354 ::repo_free_solvable_block( repo_r, blockBegin, blockSize, /*reuseids*/false );
355 blockBegin = blockSize = 0;
360 // Free remembered entries
361 ::repo_free_solvable_block( repo_r, blockBegin, blockSize, /*reuseids*/false );
362 blockBegin = blockSize = 0;
368 _onSystemByUserListPtr.reset(); // re-evaluate
372 detail::SolvableIdType PoolImpl::_addSolvables( ::_Repo * repo_r, unsigned count_r )
374 setDirty(__FUNCTION__, repo_r->name );
375 return ::repo_add_solvable_block( repo_r, count_r );
378 void PoolImpl::setRepoInfo( RepoIdType id_r, const RepoInfo & info_r )
380 ::_Repo * repo( getRepo( id_r ) );
385 // libsolv priority is based on '<', while yum's repoinfo
386 // uses 1(highest)->99(lowest). Thus we use -info_r.priority.
387 if ( repo->priority != int(-info_r.priority()) )
389 repo->priority = -info_r.priority();
393 // subpriority is used to e.g. prefer http over dvd iff
394 // both have same priority.
395 int mediaPriority( media::MediaPriority( info_r.url() ) );
396 if ( repo->subpriority != mediaPriority )
398 repo->subpriority = mediaPriority;
403 setDirty(__FUNCTION__, info_r.alias().c_str() );
405 _repoinfos[id_r] = info_r;
408 ///////////////////////////////////////////////////////////////////
410 // need on demand and id based Locale
411 void _locale_hack( const LocaleSet & locales_r,
412 std::tr1::unordered_set<IdString> & locale2Solver )
414 std::tr1::unordered_set<IdString>( 2*locales_r.size() ).swap( locale2Solver );
415 for_( it, locales_r.begin(),locales_r.end() )
417 for ( Locale l( *it ); l != Locale::noCode; l = l.fallback() )
418 locale2Solver.insert( IdString( l.code() ) );
420 MIL << "New Solver Locales: " << locale2Solver << endl;
423 void PoolImpl::setTextLocale( const Locale & locale_r )
425 std::vector<std::string> fallbacklist;
426 for ( Locale l( locale_r ); l != Locale::noCode; l = l.fallback() )
428 fallbacklist.push_back( l.code() );
430 dumpRangeLine( MIL << "pool_set_languages: ", fallbacklist.begin(), fallbacklist.end() ) << endl;
432 std::vector<const char *> fallbacklist_cstr;
433 for_( it, fallbacklist.begin(), fallbacklist.end() )
435 fallbacklist_cstr.push_back( it->c_str() );
437 ::pool_set_languages( _pool, &fallbacklist_cstr.front(), fallbacklist_cstr.size() );
440 void PoolImpl::setRequestedLocales( const LocaleSet & locales_r )
442 depSetDirty( "setRequestedLocales" );
443 _requestedLocales = locales_r;
444 MIL << "New RequestedLocales: " << locales_r << endl;
445 _locale_hack( _requestedLocales, _locale2Solver );
448 bool PoolImpl::addRequestedLocale( const Locale & locale_r )
450 if ( _requestedLocales.insert( locale_r ).second )
452 depSetDirty( "addRequestedLocale", locale_r.code().c_str() );
453 _locale_hack( _requestedLocales, _locale2Solver );
459 bool PoolImpl::eraseRequestedLocale( const Locale & locale_r )
461 if ( _requestedLocales.erase( locale_r ) )
463 depSetDirty( "addRequestedLocale", locale_r.code().c_str() );
464 _locale_hack( _requestedLocales, _locale2Solver );
470 static void _getLocaleDeps( Capability cap_r, std::tr1::unordered_set<sat::detail::IdType> & store_r )
472 // Collect locales from any 'namespace:language(lang)' dependency
473 CapDetail detail( cap_r );
474 if ( detail.kind() == CapDetail::EXPRESSION )
476 switch ( detail.capRel() )
478 case CapDetail::CAP_AND:
479 case CapDetail::CAP_OR:
481 _getLocaleDeps( detail.lhs(), store_r );
482 _getLocaleDeps( detail.rhs(), store_r );
485 case CapDetail::CAP_NAMESPACE:
486 if ( detail.lhs().id() == NAMESPACE_LANGUAGE )
488 store_r.insert( detail.rhs().id() );
492 case CapDetail::REL_NONE:
493 case CapDetail::CAP_WITH:
494 case CapDetail::CAP_ARCH:
500 const LocaleSet & PoolImpl::getAvailableLocales() const
502 if ( !_availableLocalesPtr )
504 // Collect any 'namespace:language(ja)' dependencies
505 std::tr1::unordered_set<sat::detail::IdType> tmp;
506 Pool pool( Pool::instance() );
507 for_( it, pool.solvablesBegin(), pool.solvablesEnd() )
509 Capabilities cap( it->supplements() );
510 for_( cit, cap.begin(), cap.end() )
512 _getLocaleDeps( *cit, tmp );
515 #warning immediately build LocaleSet as soon as Loale is an Id based type
516 _availableLocalesPtr.reset( new LocaleSet(tmp.size()) );
517 for_( it, tmp.begin(), tmp.end() )
519 _availableLocalesPtr->insert( Locale( IdString(*it) ) );
522 return *_availableLocalesPtr;
525 void PoolImpl::multiversionListInit() const
527 _multiversionListPtr.reset( new MultiversionList );
528 MultiversionList & multiversionList( *_multiversionListPtr );
530 const std::set<std::string> & multiversionSpec( ZConfig::instance().multiversionSpec() );
531 for_( it, multiversionSpec.begin(), multiversionSpec.end() )
533 static const std::string prefix( "provides:" );
534 if ( str::hasPrefix( *it, prefix ) )
536 WhatProvides provides( Capability( it->c_str() + prefix.size() ) );
537 if ( provides.empty() )
539 MIL << "Multiversion install not provided (" << *it << ")" << endl;
543 for_( pit, provides.begin(), provides.end() )
545 if ( multiversionList.insert( pit->ident() ).second )
546 MIL << "Multiversion install " << pit->ident() << " (" << *it << ")" << endl;
552 MIL << "Multiversion install " << *it << endl;
553 multiversionList.insert( IdString( *it ) );
558 void PoolImpl::onSystemByUserListInit() const
560 _onSystemByUserListPtr.reset( new OnSystemByUserList );
561 OnSystemByUserList & onSystemByUserList( *_onSystemByUserListPtr );
563 Pathname root( ZConfig::instance().systemRoot() );
566 MIL << "Target not initialized." << endl;
569 PathInfo pi( root / ZConfig::instance().historyLogFile() );
570 MIL << "onSystemByUserList from history: " << pi << endl;
574 // go and parse it: 'who' must constain an '@', then it was installed by user request.
575 // 2009-09-29 07:25:19|install|lirc-remotes|0.8.5-3.2|x86_64|root@opensuse|InstallationImage|a204211eb0...
576 std::ifstream infile( pi.path().c_str() );
577 for( iostr::EachLine in( infile ); in; in.next() )
579 const char * ch( (*in).c_str() );
581 if ( *ch < '1' || '9' < *ch )
583 const char * sep1 = ::strchr( ch, '|' ); // | after date
587 // if logs an install or delete
588 bool installs = true;
589 if ( ::strncmp( sep1, "install|", 8 ) )
591 if ( ::strncmp( sep1, "remove |", 8 ) )
592 continue; // no install and no remove
594 installs = false; // remove
596 sep1 += 8; // | after what
597 // get the package name
598 const char * sep2 = ::strchr( sep1, '|' ); // | after name
599 if ( !sep2 || sep1 == sep2 )
601 (*in)[sep2-ch] = '\0';
602 IdString pkg( sep1 );
603 // we're done, if a delete
606 onSystemByUserList.erase( pkg );
609 // now guess whether user installed or not (3rd next field contains 'user@host')
610 if ( (sep1 = ::strchr( sep2+1, '|' )) // | after version
611 && (sep1 = ::strchr( sep1+1, '|' )) // | after arch
612 && (sep2 = ::strchr( sep1+1, '|' )) ) // | after who
614 (*in)[sep2-ch] = '\0';
615 if ( ::strchr( sep1+1, '@' ) )
618 onSystemByUserList.insert( pkg );
623 MIL << "onSystemByUserList found: " << onSystemByUserList.size() << endl;
626 const std::set<std::string> & PoolImpl::requiredFilesystems() const
628 if ( ! _requiredFilesystemsPtr )
630 _requiredFilesystemsPtr.reset( new std::set<std::string> );
631 std::set<std::string> & requiredFilesystems( *_requiredFilesystemsPtr );
632 str::split( base::sysconfig::read( sysconfigStoragePath() )["USED_FS_LIST"],
633 std::inserter( requiredFilesystems, requiredFilesystems.end() ) );
635 return *_requiredFilesystemsPtr;
638 /////////////////////////////////////////////////////////////////
639 } // namespace detail
640 ///////////////////////////////////////////////////////////////////
641 /////////////////////////////////////////////////////////////////
643 ///////////////////////////////////////////////////////////////////
644 /////////////////////////////////////////////////////////////////
646 ///////////////////////////////////////////////////////////////////