1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Product.cc
13 #include "zypp/base/LogTools.h"
14 #include "zypp/base/StrMatcher.h"
16 #include "zypp/Product.h"
19 #include "zypp/sat/LookupAttr.h"
20 #include "zypp/sat/WhatProvides.h"
21 #include "zypp/sat/WhatObsoletes.h"
22 #include "zypp/PoolItem.h"
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
30 IMPL_PTR_TYPE(Product);
34 void fillList( std::list<Url> & ret_r, sat::Solvable solv_r, sat::SolvAttr attr_r )
36 sat::LookupAttr query( attr_r, solv_r );
37 for_( it, query.begin(), query.end() )
39 try // ignore malformed urls
41 ret_r.push_back( Url( it.asString() ) );
43 catch( const url::UrlException & )
48 void fillList( std::list<std::string> & ret_r, sat::Solvable solv_r, sat::SolvAttr attr_r )
50 sat::LookupAttr query( attr_r, solv_r );
51 for_( it, query.begin(), query.end() )
53 ret_r.push_back( it.asString() );
58 ///////////////////////////////////////////////////////////////////
60 // METHOD NAME : Product::Product
63 Product::Product( const sat::Solvable & solvable_r )
64 : ResObject( solvable_r )
67 ///////////////////////////////////////////////////////////////////
69 // METHOD NAME : Product::~Product
75 ///////////////////////////////////////////////////////////////////
77 sat::Solvable Product::referencePackage() const
79 // Look for a provider of 'product(name) = version' of same
80 // architecture and within the same repo.
82 // Code12: Update repos may have multiple release package versions
83 // providing the same product. Prefer the one matching the buildtime,
84 // as the product buildtime is derived from the -release package.
85 Capability identCap( str::form( "product(%s) = %s", name().c_str(), edition().c_str() ) );
88 bool foundBuildTime = false;
89 sat::WhatProvides providers( identCap );
90 for_( it, providers.begin(), providers.end() )
92 if ( it->repository() == repository() && it->arch() == arch() )
94 bool fitsBuildtime = ( it->buildtime() == buildtime() );
97 bool lowerEdition = ( it->edition() <= found.edition() );
98 if ( ( foundBuildTime && ( !fitsBuildtime || lowerEdition ) )
99 || ( !foundBuildTime && ( !fitsBuildtime && lowerEdition ) ) )
104 foundBuildTime = true;
108 if ( ! found && isSystem() )
110 // bnc#784900: for installed products check whether the file is owned by
111 // some package. If so, ust this as buddy.
112 sat::LookupAttr q( sat::SolvAttr::filelist, repository() );
113 std::string refFile( referenceFilename() );
114 if ( ! refFile.empty() )
116 StrMatcher matcher( referenceFilename() );
117 q.setStrMatcher( matcher );
119 found = q.begin().inSolvable();
122 INT << "Product referenceFilename unexpectedly empty!" << endl;
126 WAR << *this << ": no reference package found: " << identCap << endl;
130 std::string Product::referenceFilename() const
131 { return lookupStrAttribute( sat::SolvAttr::productReferenceFile ); }
133 Product::ReplacedProducts Product::replacedProducts() const
135 std::vector<constPtr> ret;
136 // By now we simply collect what is obsoleted by the Product,
137 // or by the products buddy (release-package).
139 // Check our own dependencies. We should not have any,
140 // but just to be shure.
141 sat::WhatObsoletes obsoleting( satSolvable() );
142 for_( it, obsoleting.begin(), obsoleting.end() )
144 if ( it->isKind( ResKind::product ) )
145 ret.push_back( make<Product>( *it ) );
148 // If we have a buddy, we check what product buddies the
150 obsoleting = sat::WhatObsoletes( poolItem().buddy() );
151 for_( it, obsoleting.poolItemBegin(), obsoleting.poolItemEnd() )
153 if ( (*it).buddy().isKind( ResKind::product ) )
154 ret.push_back( make<Product>( (*it).buddy() ) );
159 CapabilitySet Product::droplist() const
160 { return poolItem().buddy().valuesOfNamespace( "weakremover" ); }
162 std::string Product::productLine() const
163 { return lookupStrAttribute( sat::SolvAttr::productProductLine ); }
165 ///////////////////////////////////////////////////////////////////
167 std::string Product::shortName() const
169 std::string ret( lookupStrAttribute( sat::SolvAttr::productShortlabel ) );
170 if ( ret.empty() ) ret = name();
175 std::string Product::flavor() const
177 // Look for a provider of 'product_flavor(name) = version'
178 // within the same repo. Unlike the reference package, we
179 // can be relaxed and ignore the architecture.
180 Capability identCap( str::form( "product_flavor(%s) = %s", name().c_str(), edition().c_str() ) );
182 sat::WhatProvides providers( identCap );
183 for_( it, providers.begin(), providers.end() )
185 if ( it->repository() == repository() )
187 // Got the package now try to get the provided 'flavor(...)'
188 Capabilities provides( it->provides() );
189 for_( cap, provides.begin(), provides.end() )
191 std::string capstr( cap->asString() );
192 if ( str::hasPrefix( capstr, "flavor(" ) )
194 capstr = str::stripPrefix( capstr, "flavor(" );
195 capstr.erase( capstr.size()-1 ); // trailing ')'
201 return std::string();
204 std::string Product::type() const
205 { return lookupStrAttribute( sat::SolvAttr::productType ); }
207 std::list<std::string> Product::flags() const
209 std::list<std::string> ret;
210 fillList( ret, satSolvable(), sat::SolvAttr::productFlags );
214 Date Product::endOfLife() const
215 { return Date( lookupNumAttribute( sat::SolvAttr::productEndOfLife ) );}
217 bool Product::hasEndOfLife() const
218 { return( lookupNumAttribute( sat::SolvAttr::productEndOfLife, -1 ) != -1 ); }
220 bool Product::hasEndOfLife( Date & value ) const
222 Date res( lookupNumAttribute( sat::SolvAttr::productEndOfLife, -1 ) );
230 std::vector<Repository::ContentIdentifier> Product::updateContentIdentifier() const
232 std::vector<Repository::ContentIdentifier> ret;
233 sat::LookupAttr q( sat::SolvAttr::productUpdatesRepoid, sat::SolvAttr::productUpdates, *this );
237 for_( it, q.begin(), q.end() )
238 ret.push_back( it.asString() );
243 bool Product::hasUpdateContentIdentifier( const Repository::ContentIdentifier & cident_r ) const
245 sat::LookupAttr q( sat::SolvAttr::productUpdatesRepoid, sat::SolvAttr::productUpdates, *this );
246 for_( it, q.begin(), q.end() )
248 if ( it.asString() == cident_r )
254 bool Product::isTargetDistribution() const
255 { return isSystem() && lookupStrAttribute( sat::SolvAttr::productType ) == "base"; }
257 std::string Product::registerTarget() const
258 { return lookupStrAttribute( sat::SolvAttr::productRegisterTarget ); }
260 std::string Product::registerRelease() const
261 { return lookupStrAttribute( sat::SolvAttr::productRegisterRelease ); }
263 std::string Product::registerFlavor() const
264 { return lookupStrAttribute( sat::SolvAttr::productRegisterFlavor ); }
266 /////////////////////////////////////////////////////////////////
268 Product::UrlList Product::urls( const std::string & key_r ) const
272 sat::LookupAttr url( sat::SolvAttr::productUrl, *this );
273 sat::LookupAttr url_type( sat::SolvAttr::productUrlType, *this );
275 sat::LookupAttr::iterator url_it(url.begin());
276 sat::LookupAttr::iterator url_type_it(url_type.begin());
278 for (;url_it != url.end(); ++url_it, ++url_type_it)
280 /* safety checks, shouldn't happen (tm) */
281 if (url_type_it == url_type.end())
283 ERR << *this << " : The thing that should not happen, happened." << endl;
287 if ( url_type_it.asString() == key_r )
289 ret._list.push_back(url_it.asString());
291 } /* while (attribute array) */
296 Product::UrlList Product::releaseNotesUrls() const { return urls( "releasenotes" ); }
297 Product::UrlList Product::registerUrls() const { return urls( "register" ); }
298 Product::UrlList Product::smoltUrls() const { return urls( "smolt" ); }
299 Product::UrlList Product::updateUrls() const { return urls( "update" ); }
300 Product::UrlList Product::extraUrls() const { return urls( "extra" ); }
301 Product::UrlList Product::optionalUrls() const { return urls( "optional" ); }
303 std::ostream & operator<<( std::ostream & str, const Product::UrlList & obj )
304 { return dumpRange( str << obj.key() << ' ', obj.begin(), obj.end() ); }
306 /////////////////////////////////////////////////////////////////
308 ///////////////////////////////////////////////////////////////////