Imported Upstream version 15.0.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     // bnc #497696: Update repos may have multiple release package versions
83     // providing the same product. As a workaround we link to the one with
84     // the highest version.
85     Capability identCap( str::form( "product(%s) = %s", name().c_str(), edition().c_str() ) );
86
87     sat::Solvable found;
88     sat::WhatProvides providers( identCap );
89     for_( it, providers.begin(), providers.end() )
90     {
91       if ( it->repository() == repository()
92            && it->arch() == arch() )
93       {
94         if ( ! found || found.edition() < it->edition() )
95           found = *it;
96       }
97     }
98
99     if ( ! found && isSystem() )
100     {
101       // bnc#784900: for installed products check whether the file is owned by
102       // some package. If so, ust this as buddy.
103       sat::LookupAttr q( sat::SolvAttr::filelist, repository() );
104       std::string refFile( referenceFilename() );
105       if ( ! refFile.empty() )
106       {
107         StrMatcher matcher( referenceFilename() );
108         q.setStrMatcher( matcher );
109         if ( ! q.empty() )
110           found = q.begin().inSolvable();
111       }
112       else
113         INT << "Product referenceFilename unexpectedly empty!" << endl;
114     }
115
116     if ( ! found )
117       WAR << *this << ": no reference package found: " << identCap << endl;
118     return found;
119   }
120
121   std::string Product::referenceFilename() const
122   { return lookupStrAttribute( sat::SolvAttr::productReferenceFile ); }
123
124   Product::ReplacedProducts Product::replacedProducts() const
125   {
126     std::vector<constPtr> ret;
127     // By now we simply collect what is obsoleted by the Product,
128     // or by the products buddy (release-package).
129
130     // Check our own dependencies. We should not have any,
131     // but just to be shure.
132     sat::WhatObsoletes obsoleting( satSolvable() );
133     for_( it, obsoleting.begin(), obsoleting.end() )
134     {
135       if ( it->isKind( ResKind::product ) )
136         ret.push_back( make<Product>( *it ) );
137     }
138
139     // If we have a buddy, we check what product buddies the
140     // buddy replaces.
141     obsoleting = sat::WhatObsoletes( poolItem().buddy() );
142     for_( it, obsoleting.poolItemBegin(), obsoleting.poolItemEnd() )
143     {
144       if ( (*it).buddy().isKind( ResKind::product ) )
145         ret.push_back( make<Product>( (*it).buddy() ) );
146     }
147     return ret;
148   }
149
150   CapabilitySet Product::droplist() const
151   { return poolItem().buddy().valuesOfNamespace( "weakremover" ); }
152
153   std::string Product::productLine() const
154   { return lookupStrAttribute( sat::SolvAttr::productProductLine ); }
155
156   ///////////////////////////////////////////////////////////////////
157
158   std::string Product::shortName() const
159   {
160     std::string ret( lookupStrAttribute( sat::SolvAttr::productShortlabel ) );
161     if ( ret.empty() ) ret = name();
162     return ret;
163
164   }
165
166   std::string Product::flavor() const
167   {
168     // Look for a  provider of 'product_flavor(name) = version'
169     // within the same repo. Unlike the reference package, we
170     // can be relaxed and ignore the architecture.
171     Capability identCap( str::form( "product_flavor(%s) = %s", name().c_str(), edition().c_str() ) );
172
173     sat::WhatProvides providers( identCap );
174     for_( it, providers.begin(), providers.end() )
175     {
176       if ( it->repository() == repository() )
177       {
178         // Got the package now try to get the provided 'flavor(...)'
179         Capabilities provides( it->provides() );
180         for_( cap, provides.begin(), provides.end() )
181         {
182           std::string capstr( cap->asString() );
183           if ( str::hasPrefix( capstr, "flavor(" ) )
184           {
185             capstr = str::stripPrefix( capstr, "flavor(" );
186             capstr.erase( capstr.size()-1 ); // trailing ')'
187             return capstr;
188           }
189         }
190       }
191     }
192     return std::string();
193   }
194
195   std::string Product::type() const
196   { return lookupStrAttribute( sat::SolvAttr::productType ); }
197
198   std::list<std::string> Product::flags() const
199   {
200     std::list<std::string> ret;
201     fillList( ret, satSolvable(), sat::SolvAttr::productFlags );
202     return ret;
203   }
204
205   Date Product::endOfLife() const
206   { return Date( lookupNumAttribute( sat::SolvAttr::productEndOfLife ) );}
207
208   std::vector<Repository::ContentIdentifier> Product::updateContentIdentifier() const
209   {
210     std::vector<Repository::ContentIdentifier> ret;
211     sat::LookupAttr q( sat::SolvAttr::productUpdatesRepoid, sat::SolvAttr::productUpdates, *this );
212     if ( ! q.empty() )
213     {
214       ret.reserve( 2 );
215       for_( it, q.begin(), q.end() )
216         ret.push_back( it.asString() );
217     }
218     return ret;
219   }
220
221   bool Product::hasUpdateContentIdentifier( const Repository::ContentIdentifier & cident_r ) const
222   {
223     sat::LookupAttr q( sat::SolvAttr::productUpdatesRepoid, sat::SolvAttr::productUpdates, *this );
224     for_( it, q.begin(), q.end() )
225     {
226       if ( it.asString() == cident_r )
227         return true;
228     }
229     return false;
230   }
231
232   bool Product::isTargetDistribution() const
233   { return isSystem() && lookupStrAttribute( sat::SolvAttr::productType ) == "base"; }
234
235   std::string Product::registerTarget() const
236   { return lookupStrAttribute( sat::SolvAttr::productRegisterTarget ); }
237
238   std::string Product::registerRelease() const
239   { return lookupStrAttribute( sat::SolvAttr::productRegisterRelease ); }
240
241   std::string Product::registerFlavor() const
242   { return lookupStrAttribute( sat::SolvAttr::productRegisterFlavor ); }
243   
244   /////////////////////////////////////////////////////////////////
245
246   Product::UrlList Product::urls( const std::string & key_r ) const
247   {
248     UrlList ret;
249
250     sat::LookupAttr url( sat::SolvAttr::productUrl, *this );
251     sat::LookupAttr url_type( sat::SolvAttr::productUrlType, *this );
252
253     sat::LookupAttr::iterator url_it(url.begin());
254     sat::LookupAttr::iterator url_type_it(url_type.begin());
255
256     for (;url_it != url.end(); ++url_it, ++url_type_it)
257     {
258         /* safety checks, shouldn't happen (tm) */
259         if (url_type_it == url_type.end())
260         {
261             ERR << *this << " : The thing that should not happen, happened." << endl;
262             break;
263         }
264
265         if ( url_type_it.asString() == key_r )
266         {
267             ret._list.push_back(url_it.asString());
268         }
269     } /* while (attribute array) */
270
271     return ret;
272   }
273
274   Product::UrlList Product::releaseNotesUrls() const { return urls( "releasenotes" ); }
275   Product::UrlList Product::registerUrls()     const { return urls( "register" ); }
276   Product::UrlList Product::smoltUrls()        const { return urls( "smolt" ); }
277   Product::UrlList Product::updateUrls()       const { return urls( "update" ); }
278   Product::UrlList Product::extraUrls()        const { return urls( "extra" ); }
279   Product::UrlList Product::optionalUrls()     const { return urls( "optional" ); }
280
281   std::ostream & operator<<( std::ostream & str, const Product::UrlList & obj )
282   { return dumpRange( str << obj.key() << ' ', obj.begin(), obj.end() ); }
283
284   /////////////////////////////////////////////////////////////////
285 } // namespace zypp
286 ///////////////////////////////////////////////////////////////////