Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / Package.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Package.cc
10  *
11 */
12 #include <iostream>
13 #include <fstream>
14
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/String.h"
17 #include "zypp/Package.h"
18 #include "zypp/sat/LookupAttr.h"
19 #include "zypp/ZYppFactory.h"
20 #include "zypp/target/rpm/RpmDb.h"
21 #include "zypp/target/rpm/RpmHeader.h"
22
23 using namespace std;
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28
29   IMPL_PTR_TYPE(Package);
30
31   ///////////////////////////////////////////////////////////////////
32   //
33   //    METHOD NAME : Package::Package
34   //    METHOD TYPE : Ctor
35   //
36   Package::Package( const sat::Solvable & solvable_r )
37   : ResObject( solvable_r )
38   {}
39
40   ///////////////////////////////////////////////////////////////////
41   //
42   //    METHOD NAME : Package::~Package
43   //    METHOD TYPE : Dtor
44   //
45   Package::~Package()
46   {}
47
48   VendorSupportOption Package::vendorSupport() const
49   {
50     static const IdString support_unsupported( "support_unsupported" );
51     static const IdString support_acc( "support_acc" );
52     static const IdString support_l1( "support_l1" );
53     static const IdString support_l2( "support_l2" );
54     static const IdString support_l3( "support_l3" );
55
56     VendorSupportOption ret( VendorSupportUnknown );
57     // max over all identical packages
58     for ( const auto & solv : sat::WhatProvides( (Capability(ident().id())) ) )
59     {
60       if ( solv.edition() == edition()
61         && solv.ident() == ident()
62         && identical( solv ) )
63       {
64         for ( PackageKeyword kw : Keywords( sat::SolvAttr::keywords, solv ) )
65         {
66           switch ( ret )
67           {
68             case VendorSupportUnknown:
69               if ( kw == support_unsupported )  { ret = VendorSupportUnsupported; break; }
70             case VendorSupportUnsupported:
71               if ( kw == support_acc )  { ret = VendorSupportACC; break; }
72             case VendorSupportACC:
73               if ( kw == support_l1 )   { ret = VendorSupportLevel1; break; }
74             case VendorSupportLevel1:
75               if ( kw == support_l2 )   { ret = VendorSupportLevel2; break; }
76             case VendorSupportLevel2:
77               if ( kw == support_l3 )   { return VendorSupportLevel3; break; }
78             case VendorSupportLevel3:
79               /* make gcc happy */ break;
80           }
81         }
82       }
83     }
84     return ret;
85   }
86
87   bool Package::maybeUnsupported() const
88   {
89     static const VendorSupportOptions unsupportedOpts( VendorSupportUnknown
90                                                      | VendorSupportUnsupported
91                                                      | VendorSupportACC );
92     return unsupportedOpts.testFlag( vendorSupport() );
93   }
94
95   Changelog Package::changelog() const
96   {
97       Target_Ptr target( getZYpp()->getTarget() );
98       if ( ! target )
99       {
100         ERR << "Target not initialized. Changelog is not available." << std::endl;
101         return Changelog();
102       }
103
104       if ( repository().isSystemRepo() )
105       {
106           target::rpm::RpmHeader::constPtr header;
107           target->rpmDb().getData(name(), header);
108           return header ? header->tag_changelog() : Changelog(); // might be deleted behind our back (bnc #530595)
109       }
110       WAR << "changelog is not available for uninstalled packages" << std::endl;
111       return Changelog();
112   }
113
114   std::string Package::buildhost() const
115   { return lookupStrAttribute( sat::SolvAttr::buildhost ); }
116
117   std::string Package::distribution() const
118   { return lookupStrAttribute( sat::SolvAttr::distribution ); }
119
120   std::string Package::license() const
121   { return lookupStrAttribute( sat::SolvAttr::license ); }
122
123   std::string Package::packager() const
124   { return lookupStrAttribute( sat::SolvAttr::packager ); }
125
126   std::string Package::group() const
127   { return lookupStrAttribute( sat::SolvAttr::group ); }
128
129   Package::Keywords Package::keywords() const
130   { return Keywords( sat::SolvAttr::keywords, satSolvable() ); }
131
132   std::string Package::url() const
133   { return lookupStrAttribute( sat::SolvAttr::url ); }
134
135   ByteCount Package::sourcesize() const
136   { return lookupNumAttribute( sat::SolvAttr::sourcesize ); }
137
138   std::list<std::string> Package::authors() const
139   {
140     std::list<std::string> ret;
141     str::split( lookupStrAttribute( sat::SolvAttr::authors ), std::back_inserter(ret), "\n" );
142     return ret;
143   }
144
145   Package::FileList Package::filelist() const
146   { return FileList( sat::SolvAttr::filelist, satSolvable() ); }
147
148   CheckSum Package::checksum() const
149   { return lookupCheckSumAttribute( sat::SolvAttr::checksum ); }
150
151   OnMediaLocation Package::location() const
152   { return lookupLocation(); }
153
154   namespace
155   {
156     bool schemeIsLocalDir( const Url & url_r )
157     {
158       std::string s( url_r.getScheme() );
159       return s == "dir" || s == "file";
160     }
161   }
162
163   Pathname Package::cachedLocation() const
164   {
165     OnMediaLocation loc( location() );
166     PathInfo pi( repoInfo().packagesPath() / loc.filename() );
167
168     if ( ! pi.isExist() )
169       return Pathname();        // no file in cache
170
171     if ( loc.checksum().empty() )
172     {
173       Url url( repoInfo().url() );
174       if ( ! schemeIsLocalDir( url ) )
175         return Pathname();      // same name but no checksum to verify
176
177       // for local repos compare with the checksum in repo
178       if ( CheckSum( CheckSum::md5Type(), std::ifstream( (url.getPathName() / loc.filename()).c_str() ) )
179         != CheckSum( CheckSum::md5Type(), std::ifstream( pi.c_str() ) ) )
180         return Pathname();      // same name but wrong checksum
181     }
182     else
183     {
184       if ( loc.checksum() != CheckSum( loc.checksum().type(), std::ifstream( pi.c_str() ) ) )
185         return Pathname();      // same name but wrong checksum
186     }
187
188     return pi.path();           // the right one
189   }
190
191   std::string Package::sourcePkgName() const
192   {
193     // no id means same as package
194     sat::detail::IdType id( lookupIdAttribute( sat::SolvAttr::sourcename ) );
195     return id ? IdString( id ).asString() : name();
196   }
197
198   Edition Package::sourcePkgEdition() const
199   {
200    // no id means same as package
201     sat::detail::IdType id( lookupIdAttribute( sat::SolvAttr::sourceevr ) );
202     return id ? Edition( id ) : edition();
203   }
204
205   std::string Package::sourcePkgType() const
206   { return lookupStrAttribute( sat::SolvAttr::sourcearch ); }
207
208   std::string Package::sourcePkgLongName() const
209   { return str::form( "%s-%s.%s", sourcePkgName().c_str(), sourcePkgEdition().c_str(), sourcePkgType().c_str() ); }
210
211
212   /////////////////////////////////////////////////////////////////
213 } // namespace zypp
214 ///////////////////////////////////////////////////////////////////