libzypp  17.14.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<std::string> & 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  ret_r.push_back( it.asString() );
40  }
41  }
42  }
43 
45  //
46  // METHOD NAME : Product::Product
47  // METHOD TYPE : Ctor
48  //
49  Product::Product( const sat::Solvable & solvable_r )
50  : ResObject( solvable_r )
51  {}
52 
54  //
55  // METHOD NAME : Product::~Product
56  // METHOD TYPE : Dtor
57  //
59  {}
60 
62 
64  {
65  // Look for a provider of 'product(name) = version' of same
66  // architecture and within the same repo.
67  //
68  // Code12: Update repos may have multiple release package versions
69  // providing the same product. Prefer the one matching the buildtime,
70  // as the product buildtime is derived from the -release package.
71  Capability identCap( str::form( "product(%s) = %s", name().c_str(), edition().c_str() ) );
72 
73  sat::Solvable found;
74  bool foundBuildTime = false;
75  sat::WhatProvides providers( identCap );
76  for_( it, providers.begin(), providers.end() )
77  {
78  if ( it->repository() == repository() && it->arch() == arch() )
79  {
80  bool fitsBuildtime = ( it->buildtime() == buildtime() );
81  if ( found )
82  {
83  bool lowerEdition = ( it->edition() <= found.edition() );
84  if ( ( foundBuildTime && ( !fitsBuildtime || lowerEdition ) )
85  || ( !foundBuildTime && ( !fitsBuildtime && lowerEdition ) ) )
86  continue;
87  }
88  found = *it;
89  if ( fitsBuildtime )
90  foundBuildTime = true;
91  }
92  }
93 
94  if ( ! found && isSystem() )
95  {
96  // bnc#784900: for installed products check whether the file is owned by
97  // some package. If so, ust this as buddy.
99  std::string refFile( referenceFilename() ); // the basename only!
100  if ( ! refFile.empty() )
101  {
102  StrMatcher matcher( "/etc/products.d/"+refFile, Match::STRING | Match::FILES );
103  q.setStrMatcher( matcher );
104  if ( ! q.empty() )
105  found = q.begin().inSolvable();
106  }
107  else
108  INT << "Product referenceFilename unexpectedly empty!" << endl;
109  }
110 
111  if ( ! found )
112  WAR << *this << ": no reference package found: " << identCap << endl;
113  return found;
114  }
115 
116  std::string Product::referenceFilename() const
118 
120  {
121  std::vector<constPtr> ret;
122  // By now we simply collect what is obsoleted by the Product,
123  // or by the products buddy (release-package).
124 
125  // Check our own dependencies. We should not have any,
126  // but just to be shure.
127  sat::WhatObsoletes obsoleting( satSolvable() );
128  for_( it, obsoleting.begin(), obsoleting.end() )
129  {
130  if ( it->isKind( ResKind::product ) )
131  ret.push_back( make<Product>( *it ) );
132  }
133 
134  // If we have a buddy, we check what product buddies the
135  // buddy replaces.
136  obsoleting = sat::WhatObsoletes( poolItem().buddy() );
137  for_( it, obsoleting.poolItemBegin(), obsoleting.poolItemEnd() )
138  {
139  if ( (*it).buddy().isKind( ResKind::product ) )
140  ret.push_back( make<Product>( (*it).buddy() ) );
141  }
142  return ret;
143  }
144 
146  { return poolItem().buddy().valuesOfNamespace( "weakremover" ); }
147 
148  std::string Product::productLine() const
150 
152 
153  std::string Product::shortName() const
154  {
156  if ( ret.empty() ) ret = name();
157  return ret;
158 
159  }
160 
161  std::string Product::flavor() const
162  {
163  // Look for a provider of 'product_flavor(name) = version'
164  // within the same repo. Unlike the reference package, we
165  // can be relaxed and ignore the architecture.
166  Capability identCap( str::form( "product_flavor(%s) = %s", name().c_str(), edition().c_str() ) );
167 
168  sat::WhatProvides providers( identCap );
169  for_( it, providers.begin(), providers.end() )
170  {
171  if ( it->repository() == repository() )
172  {
173  // Got the package now try to get the provided 'flavor(...)'
174  Capabilities provides( it->provides() );
175  for_( cap, provides.begin(), provides.end() )
176  {
177  std::string capstr( cap->asString() );
178  if ( str::hasPrefix( capstr, "flavor(" ) )
179  {
180  capstr = str::stripPrefix( capstr, "flavor(" );
181  capstr.erase( capstr.size()-1 ); // trailing ')'
182  return capstr;
183  }
184  }
185  }
186  }
187  return std::string();
188  }
189 
190  std::string Product::type() const
192 
193  std::list<std::string> Product::flags() const
194  {
195  std::list<std::string> ret;
196  fillList( ret, satSolvable(), sat::SolvAttr::productFlags );
197  return ret;
198  }
199 
202 
204  { return( lookupNumAttribute( sat::SolvAttr::productEndOfLife, -1 ) != (unsigned long long)(-1) ); }
205 
206  bool Product::hasEndOfLife( Date & value ) const
207  {
209  if ( res == -1 )
210  return false;
211  // else:
212  value = res;
213  return true;
214  }
215 
216  std::vector<Repository::ContentIdentifier> Product::updateContentIdentifier() const
217  {
218  std::vector<Repository::ContentIdentifier> ret;
220  if ( ! q.empty() )
221  {
222  ret.reserve( 2 );
223  for_( it, q.begin(), q.end() )
224  ret.push_back( it.asString() );
225  }
226  return ret;
227  }
228 
230  {
232  for_( it, q.begin(), q.end() )
233  {
234  if ( it.asString() == cident_r )
235  return true;
236  }
237  return false;
238  }
239 
241  { return isSystem() && lookupStrAttribute( sat::SolvAttr::productType ) == "base"; }
242 
243  std::string Product::registerTarget() const
245 
246  std::string Product::registerRelease() const
248 
249  std::string Product::registerFlavor() const
251 
253 
254  Product::UrlList Product::urls( const std::string & key_r ) const
255  {
256  UrlList ret;
257 
260 
261  sat::LookupAttr::iterator url_it(url.begin());
262  sat::LookupAttr::iterator url_type_it(url_type.begin());
263 
264  for (;url_it != url.end(); ++url_it, ++url_type_it)
265  {
266  /* safety checks, shouldn't happen (tm) */
267  if (url_type_it == url_type.end())
268  {
269  ERR << *this << " : The thing that should not happen, happened." << endl;
270  break;
271  }
272 
273  if ( url_type_it.asString() == key_r )
274  {
275  ret._list.push_back(url_it.asString());
276  }
277  } /* while (attribute array) */
278 
279  return ret;
280  }
281 
282  Product::UrlList Product::releaseNotesUrls() const { return urls( "releasenotes" ); }
283  Product::UrlList Product::registerUrls() const { return urls( "register" ); }
284  Product::UrlList Product::smoltUrls() const { return urls( "smolt" ); }
285  Product::UrlList Product::updateUrls() const { return urls( "update" ); }
286  Product::UrlList Product::extraUrls() const { return urls( "extra" ); }
287  Product::UrlList Product::optionalUrls() const { return urls( "optional" ); }
288 
289  std::ostream & operator<<( std::ostream & str, const Product::UrlList & obj )
290  { return dumpRange( str << obj.key() << ' ', obj.begin(), obj.end() ); }
291 
293 } // namespace zypp