1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/sat/Solvable.cc
14 #include "zypp/base/Logger.h"
15 #include "zypp/base/Gettext.h"
16 #include "zypp/base/Exception.h"
17 #include "zypp/base/Functional.h"
18 #include "zypp/base/Collector.h"
20 #include "zypp/sat/detail/PoolImpl.h"
21 #include "zypp/sat/Solvable.h"
22 #include "zypp/sat/Pool.h"
23 #include "zypp/sat/LookupAttr.h"
25 #include "zypp/Repository.h"
26 #include "zypp/OnMediaLocation.h"
27 #include "zypp/ZConfig.h"
31 ///////////////////////////////////////////////////////////////////
33 { /////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////
36 { /////////////////////////////////////////////////////////////////
40 void _doSplit( IdString & _ident, ResKind & _kind, IdString & _name )
45 const char * ident = _ident.c_str();
46 const char * sep = ::strchr( ident, ':' );
48 // no ':' in package names (hopefully)
51 _kind = ResKind::package;
57 _name = IdString( sep+1 );
59 // Quick check for well known kinds.
60 // NOTE: kind package and srcpackage do not
61 // have namespaced ident!
66 #define OUTS(K,S) if ( !::strncmp( ident, ResKind::K.c_str(), S ) ) _kind = ResKind::K
68 case 'c': OUTS( patch, 5 ); return; break;
69 case 'd': OUTS( product, 7 ); return; break;
70 case 'k': OUTS( package, 7 ); _ident = _name; return; break;
71 case 'p': OUTS( srcpackage, 10 ); _ident = _name; return; break;
72 case 't': OUTS( pattern, 7 ); return; break;
78 _kind = ResKind( std::string( ident, sep-ident ) );
82 Solvable::SplitIdent::SplitIdent( IdString ident_r )
84 { _doSplit( _ident, _kind, _name ); }
86 Solvable::SplitIdent::SplitIdent( const char * ident_r )
88 { _doSplit( _ident, _kind, _name ); }
90 Solvable::SplitIdent::SplitIdent( const std::string & ident_r )
92 { _doSplit( _ident, _kind, _name ); }
94 Solvable::SplitIdent::SplitIdent( ResKind kind_r, IdString name_r )
98 if ( kind_r == ResKind::package || kind_r == ResKind::srcpackage )
101 _ident = IdString( str::form( "%s:%s", kind_r.c_str(), name_r.c_str() ) );
104 Solvable::SplitIdent::SplitIdent( ResKind kind_r, const C_Str & name_r )
108 if ( kind_r == ResKind::package || kind_r == ResKind::srcpackage )
111 _ident = IdString( str::form( "%s:%s", kind_r.c_str(), name_r.c_str() ) );
114 /////////////////////////////////////////////////////////////////
116 const Solvable Solvable::noSolvable;
118 /////////////////////////////////////////////////////////////////
120 ::_Solvable * Solvable::get() const
121 { return myPool().getSolvable( _id ); }
123 #define NO_SOLVABLE_RETURN( VAL ) \
124 ::_Solvable * _solvable( get() ); \
125 if ( ! _solvable ) return VAL
127 Solvable Solvable::nextInPool() const
128 { return Solvable( myPool().getNextId( _id ) ); }
130 Solvable Solvable::nextInRepo() const
132 NO_SOLVABLE_RETURN( noSolvable );
133 for ( detail::SolvableIdType next = _id+1; next < unsigned(_solvable->repo->end); ++next )
135 ::_Solvable * nextS( myPool().getSolvable( next ) );
136 if ( nextS && nextS->repo == _solvable->repo )
138 return Solvable( next );
144 Repository Solvable::repository() const
146 NO_SOLVABLE_RETURN( Repository::noRepository );
147 return Repository( _solvable->repo );
150 bool Solvable::isSystem() const
152 NO_SOLVABLE_RETURN( _id == detail::systemSolvableId );
153 return myPool().isSystemRepo( _solvable->repo );
156 IdString Solvable::ident() const
158 NO_SOLVABLE_RETURN( IdString() );
159 return IdString( _solvable->name );
162 std::string Solvable::lookupStrAttribute( const SolvAttr & attr ) const
164 NO_SOLVABLE_RETURN( std::string() );
165 const char * s = ::solvable_lookup_str( _solvable, attr.id() );
166 return s ? s : std::string();
169 std::string Solvable::lookupStrAttribute( const SolvAttr & attr, const Locale & lang_r ) const
171 NO_SOLVABLE_RETURN( std::string() );
173 if ( lang_r == Locale::noCode )
175 s = ::solvable_lookup_str_poollang( _solvable, attr.id() );
179 s = ::solvable_lookup_str_lang( _solvable, attr.id(), lang_r.code().c_str() );
181 return s ? s : std::string();
184 unsigned Solvable::lookupNumAttribute( const SolvAttr & attr ) const
186 NO_SOLVABLE_RETURN( 0 );
187 return ::solvable_lookup_num( _solvable, attr.id(), 0 );
190 bool Solvable::lookupBoolAttribute( const SolvAttr & attr ) const
192 NO_SOLVABLE_RETURN( false );
193 return ::solvable_lookup_bool( _solvable, attr.id() );
196 detail::IdType Solvable::lookupIdAttribute( const SolvAttr & attr ) const
198 NO_SOLVABLE_RETURN( detail::noId );
199 return ::solvable_lookup_id( _solvable, attr.id() );
202 CheckSum Solvable::lookupCheckSumAttribute( const SolvAttr & attr ) const
204 NO_SOLVABLE_RETURN( CheckSum() );
205 detail::IdType chksumtype = 0;
206 const char * s = ::solvable_lookup_checksum( _solvable, attr.id(), &chksumtype );
209 switch ( chksumtype )
211 case REPOKEY_TYPE_MD5: return CheckSum::md5( s );
212 case REPOKEY_TYPE_SHA1: return CheckSum::sha1( s );
213 case REPOKEY_TYPE_SHA256: return CheckSum::sha256( s );
215 return CheckSum( std::string(), s ); // try to autodetect
218 ///////////////////////////////////////////////////////////////////
221 inline Pathname lookupDatadirIn( Repository repor_r )
223 static const sat::SolvAttr susetagsDatadir( "susetags:datadir" );
225 // First look for repo attribute "susetags:datadir". If not found,
226 // look into the solvables as Code11 satsolver placed it there.
227 sat::LookupRepoAttr datadir( susetagsDatadir, repor_r );
228 if ( ! datadir.empty() )
229 ret = datadir.begin().asString();
232 sat::LookupAttr datadir( susetagsDatadir, repor_r );
233 if ( ! datadir.empty() )
234 ret = datadir.begin().asString();
239 ///////////////////////////////////////////////////////////////////
241 OnMediaLocation Solvable::lookupLocation() const
243 NO_SOLVABLE_RETURN( OnMediaLocation() );
244 // medianumber and path
246 char * file = ::solvable_get_location( _solvable, &medianr );
248 return OnMediaLocation();
253 switch ( repository().info().type().toEnum() )
255 case repo::RepoType::NONE_e:
257 path = lookupDatadirIn( repository() );
258 if ( ! path.empty() )
259 repository().info().setProbedType( repo::RepoType::YAST2_e );
263 case repo::RepoType::YAST2_e:
265 path = lookupDatadirIn( repository() );
274 ret.setLocation ( path/file, medianr );
275 ret.setDownloadSize( ByteCount( lookupNumAttribute( SolvAttr::downloadsize ), ByteCount::K ) );
276 ret.setChecksum ( lookupCheckSumAttribute( SolvAttr::checksum ) );
277 // Not needed/available for solvables?
278 //ret.setOpenSize ( ByteCount( lookupNumAttribute( SolvAttr::opensize ), ByteCount::K ) );
279 //ret.setOpenChecksum( lookupCheckSumAttribute( SolvAttr::openchecksum ) );
283 ResKind Solvable::kind() const
285 NO_SOLVABLE_RETURN( ResKind() );
286 // detect srcpackages by 'arch'
287 switch ( _solvable->arch )
291 return ResKind::srcpackage;
295 const char * ident = IdString( _solvable->name ).c_str();
296 const char * sep = ::strchr( ident, ':' );
298 // no ':' in package names (hopefully)
300 return ResKind::package;
302 // quick check for well known kinds
303 if ( sep-ident >= 4 )
307 #define OUTS(K,S) if ( !::strncmp( ident, ResKind::K.c_str(), S ) ) return ResKind::K
309 case 'c': OUTS( patch, 5 ); break;
310 case 'd': OUTS( product, 7 ); break;
311 case 'k': OUTS( package, 7 ); break;
312 case 'p': OUTS( srcpackage, 10 ); break;
313 case 't': OUTS( pattern, 7 ); break;
319 return ResKind( std::string( ident, sep-ident ) );
322 bool Solvable::isKind( const ResKind & kind_r ) const
324 NO_SOLVABLE_RETURN( false );
326 // detect srcpackages by 'arch'
327 switch ( _solvable->arch )
331 return( kind_r == ResKind::srcpackage );
335 // no ':' in package names (hopefully)
336 const char * ident = IdString( _solvable->name ).c_str();
337 if ( kind_r == ResKind::package )
339 return( ::strchr( ident, ':' ) == 0 );
342 // look for a 'kind:' prefix
343 const char * kind = kind_r.c_str();
344 unsigned ksize = ::strlen( kind );
345 return( ::strncmp( ident, kind, ksize ) == 0
346 && ident[ksize] == ':' );
349 std::string Solvable::name() const
351 NO_SOLVABLE_RETURN( std::string() );
352 const char * ident = IdString( _solvable->name ).c_str();
353 const char * sep = ::strchr( ident, ':' );
354 return( sep ? sep+1 : ident );
357 Edition Solvable::edition() const
359 NO_SOLVABLE_RETURN( Edition() );
360 return Edition( _solvable->evr );
363 Arch Solvable::arch() const
365 NO_SOLVABLE_RETURN( Arch_noarch ); //ArchId() );
366 switch ( _solvable->arch )
370 return Arch_noarch; //ArchId( ARCH_NOARCH );
373 return Arch( IdString(_solvable->arch).asString() );
374 //return ArchId( _solvable->arch );
377 bool Solvable::multiversionInstall() const
379 return myPool().isMultiversion( ident() );
382 bool Solvable::installOnly() const { return multiversionInstall(); }
384 IdString Solvable::vendor() const
386 NO_SOLVABLE_RETURN( IdString() );
387 return IdString( _solvable->vendor );
390 Capabilities Solvable::operator[]( Dep which_r ) const
392 switch( which_r.inSwitch() )
394 case Dep::PROVIDES_e: return provides(); break;
395 case Dep::REQUIRES_e: return requires(); break;
396 case Dep::CONFLICTS_e: return conflicts(); break;
397 case Dep::OBSOLETES_e: return obsoletes(); break;
398 case Dep::RECOMMENDS_e: return recommends(); break;
399 case Dep::SUGGESTS_e: return suggests(); break;
400 case Dep::ENHANCES_e: return enhances(); break;
401 case Dep::SUPPLEMENTS_e: return supplements(); break;
402 case Dep::PREREQUIRES_e: return prerequires(); break;
404 return Capabilities();
407 inline Capabilities _getCapabilities( detail::IdType * idarraydata_r, ::Offset offs_r )
409 return offs_r ? Capabilities( idarraydata_r + offs_r ) : Capabilities();
411 Capabilities Solvable::provides() const
413 NO_SOLVABLE_RETURN( Capabilities() );
414 return _getCapabilities( _solvable->repo->idarraydata, _solvable->provides );
416 Capabilities Solvable::requires() const
418 NO_SOLVABLE_RETURN( Capabilities() );
419 return _getCapabilities( _solvable->repo->idarraydata, _solvable->requires );
421 Capabilities Solvable::conflicts() const
423 NO_SOLVABLE_RETURN( Capabilities() );
424 return _getCapabilities( _solvable->repo->idarraydata, _solvable->conflicts );
426 Capabilities Solvable::obsoletes() const
428 NO_SOLVABLE_RETURN( Capabilities() );
429 return _getCapabilities( _solvable->repo->idarraydata, _solvable->obsoletes );
431 Capabilities Solvable::recommends() const
433 NO_SOLVABLE_RETURN( Capabilities() );
434 return _getCapabilities( _solvable->repo->idarraydata, _solvable->recommends );
436 Capabilities Solvable::suggests() const
438 NO_SOLVABLE_RETURN( Capabilities() );
439 return _getCapabilities( _solvable->repo->idarraydata, _solvable->suggests );
441 Capabilities Solvable::enhances() const
443 NO_SOLVABLE_RETURN( Capabilities() );
444 return _getCapabilities( _solvable->repo->idarraydata, _solvable->enhances );
446 Capabilities Solvable::supplements() const
448 NO_SOLVABLE_RETURN( Capabilities() );
449 return _getCapabilities( _solvable->repo->idarraydata, _solvable->supplements );
451 Capabilities Solvable::prerequires() const
453 NO_SOLVABLE_RETURN( Capabilities() );
454 // prerequires are a subset of requires
455 ::Offset offs = _solvable->requires;
456 return offs ? Capabilities( _solvable->repo->idarraydata + offs, detail::solvablePrereqMarker )
460 CapabilitySet Solvable::providesNamespace( const std::string & namespace_r ) const
462 NO_SOLVABLE_RETURN( CapabilitySet() );
464 Capabilities caps( provides() );
465 for_( it, caps.begin(), caps.end() )
467 CapDetail caprep( it->detail() );
468 if ( str::hasPrefix( caprep.name().c_str(), namespace_r ) && *(caprep.name().c_str()+namespace_r.size()) == '(' )
474 CapabilitySet Solvable::valuesOfNamespace( const std::string & namespace_r ) const
476 NO_SOLVABLE_RETURN( CapabilitySet() );
478 Capabilities caps( provides() );
479 for_( it, caps.begin(), caps.end() )
481 CapDetail caprep( it->detail() );
482 if ( str::hasPrefix( caprep.name().c_str(), namespace_r ) && *(caprep.name().c_str()+namespace_r.size()) == '(' )
484 std::string value( caprep.name().c_str()+namespace_r.size()+1 );
485 value[value.size()-1] = '\0'; // erase the trailing ')'
486 ret.insert( Capability( value, caprep.op(), caprep.ed() ) );
493 std::string Solvable::asString() const
495 NO_SOLVABLE_RETURN( (_id == detail::systemSolvableId ? "systemSolvable" : "noSolvable") );
496 return str::form( "%s-%s.%s",
497 IdString( _solvable->name ).c_str(),
498 IdString( _solvable->evr ).c_str(),
499 IdString( _solvable->arch ).c_str() );
502 bool Solvable::identical( Solvable rhs ) const
504 NO_SOLVABLE_RETURN( ! rhs.get() );
505 ::_Solvable * rhssolvable( rhs.get() );
506 return rhssolvable && ::solvable_identical( _solvable, rhssolvable );
509 ///////////////////////////////////////////////////////////////////
511 { /////////////////////////////////////////////////////////////////
512 /** Expand \ref Capability and call \c fnc_r for each namescpace:language
513 * dependency. Return #invocations of fnc_r, negative if fnc_r returned
514 * false to indicate abort.
516 int invokeOnEachSupportedLocale( Capability cap_r, function<bool (const Locale &)> fnc_r )
518 CapDetail detail( cap_r );
519 if ( detail.kind() == CapDetail::EXPRESSION )
521 switch ( detail.capRel() )
523 case CapDetail::CAP_AND:
524 case CapDetail::CAP_OR:
527 int res = invokeOnEachSupportedLocale( detail.lhs(), fnc_r );
529 return res; // negative on abort.
530 int res2 = invokeOnEachSupportedLocale( detail.rhs(), fnc_r );
532 return -res + res2; // negative on abort.
537 case CapDetail::CAP_NAMESPACE:
538 if ( detail.lhs().id() == NAMESPACE_LANGUAGE )
540 return ( !fnc_r || fnc_r( Locale( IdString(detail.rhs().id()) ) ) ) ? 1 : -1; // negative on abort.
544 case CapDetail::REL_NONE:
545 case CapDetail::CAP_WITH:
546 case CapDetail::CAP_ARCH:
553 /** Expand \ref Capability and call \c fnc_r for each namescpace:language
554 * dependency. Return #invocations of fnc_r, negative if fnc_r returned
555 * false to indicate abort.
557 inline int invokeOnEachSupportedLocale( Capabilities cap_r, function<bool (const Locale &)> fnc_r )
560 for_( cit, cap_r.begin(), cap_r.end() )
562 int res = invokeOnEachSupportedLocale( *cit, fnc_r );
564 return -cnt + res; // negative on abort.
571 // Functor returning false if a Locale is in the set.
574 NoMatchIn( const LocaleSet & locales_r ) : _locales( locales_r ) {}
576 bool operator()( const Locale & locale_r ) const
578 return _locales.find( locale_r ) == _locales.end();
581 const LocaleSet & _locales;
584 } /////////////////////////////////////////////////////////////////
586 bool Solvable::supportsLocales() const
588 // false_c stops on 1st Locale.
589 return invokeOnEachSupportedLocale( supplements(), functor::false_c() ) < 0;
592 bool Solvable::supportsLocale( const Locale & locale_r ) const
594 // not_equal_to stops on == Locale.
595 return invokeOnEachSupportedLocale( supplements(), bind( std::not_equal_to<Locale>(), locale_r, _1 ) ) < 0;
598 bool Solvable::supportsLocale( const LocaleSet & locales_r ) const
600 if ( locales_r.empty() )
602 // NoMatchIn stops if Locale is included.
603 return invokeOnEachSupportedLocale( supplements(), NoMatchIn(locales_r) ) < 0;
606 bool Solvable::supportsRequestedLocales() const
607 { return supportsLocale( myPool().getRequestedLocales() ); }
609 void Solvable::getSupportedLocales( LocaleSet & locales_r ) const
611 invokeOnEachSupportedLocale( supplements(),
612 functor::Collector( std::inserter( locales_r, locales_r.begin() ) ) );
615 /******************************************************************
617 ** FUNCTION NAME : operator<<
618 ** FUNCTION TYPE : std::ostream &
620 std::ostream & operator<<( std::ostream & str, const Solvable & obj )
623 return str << (obj.isSystem() ? "systemSolvable" : "noSolvable" );
625 return str << "(" << obj.id() << ")"
626 << ( obj.isKind( ResKind::srcpackage ) ? "srcpackage:" : "" ) << obj.ident()
627 << '-' << obj.edition() << '.' << obj.arch() << "("
628 << obj.repository().alias() << ")";
631 /******************************************************************
633 ** FUNCTION NAME : dumpOn
634 ** FUNCTION TYPE : std::ostream &
636 std::ostream & dumpOn( std::ostream & str, const Solvable & obj )
641 #define OUTS(X) if ( ! obj[Dep::X].empty() ) str << endl << " " #X " " << obj[Dep::X]
656 /////////////////////////////////////////////////////////////////
658 ///////////////////////////////////////////////////////////////////
659 /////////////////////////////////////////////////////////////////
661 ///////////////////////////////////////////////////////////////////