libzypp  17.3.0
Product.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include "zypp/base/LogTools.h"
14 #include "zypp/base/StrMatcher.h"
15 
16 #include "zypp/Product.h"
17 #include "zypp/Url.h"
18 
19 #include "zypp/sat/LookupAttr.h"
20 #include "zypp/sat/WhatProvides.h"
21 #include "zypp/sat/WhatObsoletes.h"
22 #include "zypp/PoolItem.h"
23 
24 using std::endl;
25 
27 namespace zypp
28 {
29 
30  IMPL_PTR_TYPE(Product);
31 
32  namespace
33  {
34  void fillList( std::list<Url> & ret_r, sat::Solvable solv_r, sat::SolvAttr attr_r )
35  {
36  sat::LookupAttr query( attr_r, solv_r );
37  for_( it, query.begin(), query.end() )
38  {
39  try // ignore malformed urls
40  {
41  ret_r.push_back( Url( it.asString() ) );
42  }
43  catch( const url::UrlException & )
44  {}
45  }
46  }
47 
48  void fillList( std::list<std::string> & ret_r, sat::Solvable solv_r, sat::SolvAttr attr_r )
49  {
50  sat::LookupAttr query( attr_r, solv_r );
51  for_( it, query.begin(), query.end() )
52  {
53  ret_r.push_back( it.asString() );
54  }
55  }
56  }
57 
59  //
60  // METHOD NAME : Product::Product
61  // METHOD TYPE : Ctor
62  //
63  Product::Product( const sat::Solvable & solvable_r )
64  : ResObject( solvable_r )
65  {}
66 
68  //
69  // METHOD NAME : Product::~Product
70  // METHOD TYPE : Dtor
71  //
73  {}
74 
76 
78  {
79  // Look for a provider of 'product(name) = version' of same
80  // architecture and within the same repo.
81  //
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() ) );
86 
87  sat::Solvable found;
88  bool foundBuildTime = false;
89  sat::WhatProvides providers( identCap );
90  for_( it, providers.begin(), providers.end() )
91  {
92  if ( it->repository() == repository() && it->arch() == arch() )
93  {
94  bool fitsBuildtime = ( it->buildtime() == buildtime() );
95  if ( found )
96  {
97  bool lowerEdition = ( it->edition() <= found.edition() );
98  if ( ( foundBuildTime && ( !fitsBuildtime || lowerEdition ) )
99  || ( !foundBuildTime && ( !fitsBuildtime && lowerEdition ) ) )
100  continue;
101  }
102  found = *it;
103  if ( fitsBuildtime )
104  foundBuildTime = true;
105  }
106  }
107 
108  if ( ! found && isSystem() )
109  {
110  // bnc#784900: for installed products check whether the file is owned by
111  // some package. If so, ust this as buddy.
113  std::string refFile( referenceFilename() ); // the basename only!
114  if ( ! refFile.empty() )
115  {
116  StrMatcher matcher( "/etc/products.d/"+refFile, Match::STRING | Match::FILES );
117  q.setStrMatcher( matcher );
118  if ( ! q.empty() )
119  found = q.begin().inSolvable();
120  }
121  else
122  INT << "Product referenceFilename unexpectedly empty!" << endl;
123  }
124 
125  if ( ! found )
126  WAR << *this << ": no reference package found: " << identCap << endl;
127  return found;
128  }
129 
130  std::string Product::referenceFilename() const
132 
134  {
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).
138 
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() )
143  {
144  if ( it->isKind( ResKind::product ) )
145  ret.push_back( make<Product>( *it ) );
146  }
147 
148  // If we have a buddy, we check what product buddies the
149  // buddy replaces.
150  obsoleting = sat::WhatObsoletes( poolItem().buddy() );
151  for_( it, obsoleting.poolItemBegin(), obsoleting.poolItemEnd() )
152  {
153  if ( (*it).buddy().isKind( ResKind::product ) )
154  ret.push_back( make<Product>( (*it).buddy() ) );
155  }
156  return ret;
157  }
158 
160  { return poolItem().buddy().valuesOfNamespace( "weakremover" ); }
161 
162  std::string Product::productLine() const
164 
166 
167  std::string Product::shortName() const
168  {
170  if ( ret.empty() ) ret = name();
171  return ret;
172 
173  }
174 
175  std::string Product::flavor() const
176  {
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() ) );
181 
182  sat::WhatProvides providers( identCap );
183  for_( it, providers.begin(), providers.end() )
184  {
185  if ( it->repository() == repository() )
186  {
187  // Got the package now try to get the provided 'flavor(...)'
188  Capabilities provides( it->provides() );
189  for_( cap, provides.begin(), provides.end() )
190  {
191  std::string capstr( cap->asString() );
192  if ( str::hasPrefix( capstr, "flavor(" ) )
193  {
194  capstr = str::stripPrefix( capstr, "flavor(" );
195  capstr.erase( capstr.size()-1 ); // trailing ')'
196  return capstr;
197  }
198  }
199  }
200  }
201  return std::string();
202  }
203 
204  std::string Product::type() const
206 
207  std::list<std::string> Product::flags() const
208  {
209  std::list<std::string> ret;
210  fillList( ret, satSolvable(), sat::SolvAttr::productFlags );
211  return ret;
212  }
213 
216 
218  { return( lookupNumAttribute( sat::SolvAttr::productEndOfLife, -1 ) != (unsigned long long)(-1) ); }
219 
220  bool Product::hasEndOfLife( Date & value ) const
221  {
223  if ( res == -1 )
224  return false;
225  // else:
226  value = res;
227  return true;
228  }
229 
230  std::vector<Repository::ContentIdentifier> Product::updateContentIdentifier() const
231  {
232  std::vector<Repository::ContentIdentifier> ret;
234  if ( ! q.empty() )
235  {
236  ret.reserve( 2 );
237  for_( it, q.begin(), q.end() )
238  ret.push_back( it.asString() );
239  }
240  return ret;
241  }
242 
244  {
246  for_( it, q.begin(), q.end() )
247  {
248  if ( it.asString() == cident_r )
249  return true;
250  }
251  return false;
252  }
253 
255  { return isSystem() && lookupStrAttribute( sat::SolvAttr::productType ) == "base"; }
256 
257  std::string Product::registerTarget() const
259 
260  std::string Product::registerRelease() const
262 
263  std::string Product::registerFlavor() const
265 
267 
268  Product::UrlList Product::urls( const std::string & key_r ) const
269  {
270  UrlList ret;
271 
274 
275  sat::LookupAttr::iterator url_it(url.begin());
276  sat::LookupAttr::iterator url_type_it(url_type.begin());
277 
278  for (;url_it != url.end(); ++url_it, ++url_type_it)
279  {
280  /* safety checks, shouldn't happen (tm) */
281  if (url_type_it == url_type.end())
282  {
283  ERR << *this << " : The thing that should not happen, happened." << endl;
284  break;
285  }
286 
287  if ( url_type_it.asString() == key_r )
288  {
289  ret._list.push_back(url_it.asString());
290  }
291  } /* while (attribute array) */
292 
293  return ret;
294  }
295 
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" ); }
302 
303  std::ostream & operator<<( std::ostream & str, const Product::UrlList & obj )
304  { return dumpRange( str << obj.key() << ' ', obj.begin(), obj.end() ); }
305 
307 } // namespace zypp
static const SolvAttr productFlags
Definition: SolvAttr.h:148
Product(const sat::Solvable &solvable_r)
Ctor.
Definition: Product.cc:63
static const SolvAttr productUpdates
array of repoids, hopefully label s too
Definition: SolvAttr.h:155
const_iterator begin() const
Definition: Product.h:225
const_iterator begin() const
Iterator pointing to the first Solvable.
Definition: WhatObsoletes.h:83
A Solvable object within the sat Pool.
Definition: Solvable.h:53
Container of Solvable providing a Capability (read only).
Definition: WhatProvides.h:87
UrlList registerUrls() const
The URL for registration.
Definition: Product.cc:297
UrlList updateUrls() const
Online updates for the product.
Definition: Product.cc:299
bool isTargetDistribution() const
This is the installed product that is also targeted by the /etc/products.d/baseproduct symlink...
Definition: Product.cc:254
Container of Capability (currently read only).
Definition: Capabilities.h:35
UrlList optionalUrls() const
Optional software for the product.
Definition: Product.cc:301
std::string productLine() const
Vendor specific string denoting the product line.
Definition: Product.cc:162
Helper to iterate a products URL lists.
Definition: Product.h:208
std::string stripPrefix(const C_Str &str_r, const C_Str &prefix_r)
Strip a prefix_r from str_r and return the resulting string.
Definition: String.h:1018
CapabilitySet droplist() const
List of packages included in older versions of this product and now dropped.
Definition: Product.cc:159
std::string registerRelease() const
This is register.release attribute of an installed product.
Definition: Product.cc:260
UrlList smoltUrls() const
The URL for SMOLT.
Definition: Product.cc:298
std::vector< Repository::ContentIdentifier > updateContentIdentifier() const
ContentIdentifier of required update repositories.
Definition: Product.cc:230
const_iterator end() const
Iterator pointing behind the last Capability.
Definition: Capabilities.h:169
String matching (STRING|SUBSTRING|GLOB|REGEX).
Definition: StrMatcher.h:297
Lightweight attribute value lookup.
Definition: LookupAttr.h:107
std::string key() const
The key used to retrieve this list (for debug)
Definition: Product.h:237
#define INT
Definition: Logger.h:68
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\ ", const std::string &sep="\ ", const std::string &sfx="\, const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition: LogTools.h:91
std::string registerFlavor() const
This is register.flavor attribute of a product.
Definition: Product.cc:263
static const SolvAttr productRegisterRelease
Definition: SolvAttr.h:151
static const SolvAttr productEndOfLife
Definition: SolvAttr.h:149
String related utilities and Regular expression matching.
static const SolvAttr productProductLine
Definition: SolvAttr.h:143
static const SolvAttr productUrlType
Definition: SolvAttr.h:154
sat::Solvable buddy() const
Return the buddy we share our status object with.
Definition: PoolItem.cc:206
UrlList releaseNotesUrls() const
The URL to download the release notes for this product.
Definition: Product.cc:296
Url url
Definition: MediaCurl.cc:196
PoolItem_iterator poolItemBegin() const
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
PoolItem_iterator poolItemEnd() const
#define ERR
Definition: Logger.h:66
std::string type() const
Get the product type Well, in an ideal world there is only one base product.
Definition: Product.cc:204
const_iterator end() const
Iterator pointing behind the last Solvable.
Definition: WhatObsoletes.h:87
CapabilitySet valuesOfNamespace(const std::string &namespace_r) const
Return &#39;value[ op edition]&#39; for namespaced provides &#39;namespace(value)[ op edition]&#39;.
Definition: Solvable.cc:507
static const SolvAttr productRegisterTarget
Definition: SolvAttr.h:150
iterator end() const
Iterator behind the end of query results.
Definition: LookupAttr.cc:239
const_iterator begin() const
Iterator pointing to the first Capability.
Definition: Capabilities.h:166
Store and operate on date (time_t).
Definition: Date.h:32
UrlList extraUrls() const
Additional software for the product They are complementary, not alternatives.
Definition: Product.cc:300
Edition edition() const
The edition (version-release).
Definition: Solvable.cc:333
const_iterator end() const
Definition: Product.h:228
static const SolvAttr productRegisterFlavor
Definition: SolvAttr.h:152
bool hasEndOfLife() const
Return whether an EndOfLife value is actually defined in the metadata.
Definition: Product.cc:217
#define WAR
Definition: Logger.h:65
IMPL_PTR_TYPE(Application)
static const SolvAttr productType
Definition: SolvAttr.h:147
void setStrMatcher(const StrMatcher &matcher_r)
Set the pattern to match.
Definition: LookupAttr.cc:205
Base for resolvable objects.
Definition: ResObject.h:38
Container of installed Solvable which would be obsoleted by the Solvable passed to the ctor...
Definition: WhatObsoletes.h:36
static const SolvAttr productUrl
Definition: SolvAttr.h:153
ReplacedProducts replacedProducts() const
Array of installed Products that would be replaced by installing this one.
Definition: Product.cc:133
std::string ContentIdentifier
Definition: Repository.h:49
friend std::ostream & operator<<(std::ostream &str, const Resolvable &obj)
std::unordered_set< Capability > CapabilitySet
Definition: Capability.h:33
std::vector< constPtr > ReplacedProducts
Definition: Product.h:73
UrlList urls(const std::string &key_r) const
Retrieve URLs flagged with key_r for this product.
Definition: Product.cc:268
static const SolvAttr productUpdatesRepoid
Definition: SolvAttr.h:156
sat::Solvable referencePackage() const
The reference package providing the product metadata, if such a package exists.
Definition: Product.cc:77
Solvable inSolvable() const
The current Solvable.
Definition: LookupAttr.cc:354
std::string shortName() const
Untranslated short name like SLES 10 (fallback: name)
Definition: Product.cc:167
A sat capability.
Definition: Capability.h:59
Excat matching.
Definition: StrMatcher.h:43
unsigned long long lookupNumAttribute(const SolvAttr &attr) const
Definition: SolvableType.h:137
const_iterator begin() const
Iterator pointing to the first Solvable.
static const SolvAttr filelist
Definition: SolvAttr.h:99
virtual ~Product()
Dtor.
Definition: Product.cc:72
bool hasUpdateContentIdentifier(const Repository::ContentIdentifier &cident_r) const
Whether cident_r is listed as required update repository.
Definition: Product.cc:243
std::string flavor() const
The product flavor (LiveCD Demo, FTP edition,...).
Definition: Product.cc:175
Date endOfLife() const
The date when this Product goes out of support as indicated by it&#39;s medadata.
Definition: Product.cc:214
std::string registerTarget() const
This is register.target attribute of a product.
Definition: Product.cc:257
std::string lookupStrAttribute(const SolvAttr &attr) const
Definition: SolvableType.h:133
static const ResKind product
Definition: ResKind.h:43
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
std::string referenceFilename() const
For installed products the name of the corresponding /etc/products.d entry.
Definition: Product.cc:130
std::list< std::string > flags() const
The product flags.
Definition: Product.cc:207
static const SolvAttr productReferenceFile
Definition: SolvAttr.h:142
Solvable satSolvable() const
Return the corresponding sat::Solvable.
Definition: SolvableType.h:57
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:1011
static const Match FILES
LookupAttr: match full path when matching in filelists, otherwise just the basenames.
Definition: StrMatcher.h:75
const_iterator end() const
Iterator pointing behind the last Solvable.
Definition: WhatProvides.h:226
static const SolvAttr productShortlabel
Definition: SolvAttr.h:144
bool empty() const
Whether the query is empty.
Definition: LookupAttr.cc:242
iterator begin() const
Iterator to the begin of query results.
Definition: LookupAttr.cc:236
PoolItem poolItem() const
Access the corresponding PoolItem.
Definition: Resolvable.cc:28