Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / Product.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Product.cc
10  *
11 */
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
26 ///////////////////////////////////////////////////////////////////
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
58   ///////////////////////////////////////////////////////////////////
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
67   ///////////////////////////////////////////////////////////////////
68   //
69   //    METHOD NAME : Product::~Product
70   //    METHOD TYPE : Dtor
71   //
72   Product::~Product()
73   {}
74
75   ///////////////////////////////////////////////////////////////////
76
77   sat::Solvable Product::referencePackage() const
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 = ( PoolItem(*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.
112       sat::LookupAttr q( sat::SolvAttr::filelist, repository() );
113       std::string refFile( referenceFilename() );
114       if ( ! refFile.empty() )
115       {
116         StrMatcher matcher( referenceFilename() );
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
131   { return lookupStrAttribute( sat::SolvAttr::productReferenceFile ); }
132
133   Product::ReplacedProducts Product::replacedProducts() const
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
159   CapabilitySet Product::droplist() const
160   { return poolItem().buddy().valuesOfNamespace( "weakremover" ); }
161
162   std::string Product::productLine() const
163   { return lookupStrAttribute( sat::SolvAttr::productProductLine ); }
164
165   ///////////////////////////////////////////////////////////////////
166
167   std::string Product::shortName() const
168   {
169     std::string ret( lookupStrAttribute( sat::SolvAttr::productShortlabel ) );
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
205   { return lookupStrAttribute( sat::SolvAttr::productType ); }
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
214   Date Product::endOfLife() const
215   { return Date( lookupNumAttribute( sat::SolvAttr::productEndOfLife ) );}
216
217   std::vector<Repository::ContentIdentifier> Product::updateContentIdentifier() const
218   {
219     std::vector<Repository::ContentIdentifier> ret;
220     sat::LookupAttr q( sat::SolvAttr::productUpdatesRepoid, sat::SolvAttr::productUpdates, *this );
221     if ( ! q.empty() )
222     {
223       ret.reserve( 2 );
224       for_( it, q.begin(), q.end() )
225         ret.push_back( it.asString() );
226     }
227     return ret;
228   }
229
230   bool Product::hasUpdateContentIdentifier( const Repository::ContentIdentifier & cident_r ) const
231   {
232     sat::LookupAttr q( sat::SolvAttr::productUpdatesRepoid, sat::SolvAttr::productUpdates, *this );
233     for_( it, q.begin(), q.end() )
234     {
235       if ( it.asString() == cident_r )
236         return true;
237     }
238     return false;
239   }
240
241   bool Product::isTargetDistribution() const
242   { return isSystem() && lookupStrAttribute( sat::SolvAttr::productType ) == "base"; }
243
244   std::string Product::registerTarget() const
245   { return lookupStrAttribute( sat::SolvAttr::productRegisterTarget ); }
246
247   std::string Product::registerRelease() const
248   { return lookupStrAttribute( sat::SolvAttr::productRegisterRelease ); }
249
250   std::string Product::registerFlavor() const
251   { return lookupStrAttribute( sat::SolvAttr::productRegisterFlavor ); }
252   
253   /////////////////////////////////////////////////////////////////
254
255   Product::UrlList Product::urls( const std::string & key_r ) const
256   {
257     UrlList ret;
258
259     sat::LookupAttr url( sat::SolvAttr::productUrl, *this );
260     sat::LookupAttr url_type( sat::SolvAttr::productUrlType, *this );
261
262     sat::LookupAttr::iterator url_it(url.begin());
263     sat::LookupAttr::iterator url_type_it(url_type.begin());
264
265     for (;url_it != url.end(); ++url_it, ++url_type_it)
266     {
267         /* safety checks, shouldn't happen (tm) */
268         if (url_type_it == url_type.end())
269         {
270             ERR << *this << " : The thing that should not happen, happened." << endl;
271             break;
272         }
273
274         if ( url_type_it.asString() == key_r )
275         {
276             ret._list.push_back(url_it.asString());
277         }
278     } /* while (attribute array) */
279
280     return ret;
281   }
282
283   Product::UrlList Product::releaseNotesUrls() const { return urls( "releasenotes" ); }
284   Product::UrlList Product::registerUrls()     const { return urls( "register" ); }
285   Product::UrlList Product::smoltUrls()        const { return urls( "smolt" ); }
286   Product::UrlList Product::updateUrls()       const { return urls( "update" ); }
287   Product::UrlList Product::extraUrls()        const { return urls( "extra" ); }
288   Product::UrlList Product::optionalUrls()     const { return urls( "optional" ); }
289
290   std::ostream & operator<<( std::ostream & str, const Product::UrlList & obj )
291   { return dumpRange( str << obj.key() << ' ', obj.begin(), obj.end() ); }
292
293   /////////////////////////////////////////////////////////////////
294 } // namespace zypp
295 ///////////////////////////////////////////////////////////////////