Imported Upstream version 16.3.2
[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     switch ( vendorSupport() )
90     {
91       case VendorSupportUnknown:
92       case VendorSupportUnsupported:
93       case VendorSupportACC:
94         return true;
95     }
96     return false;
97   }
98
99   Changelog Package::changelog() const
100   {
101       Target_Ptr target( getZYpp()->getTarget() );
102       if ( ! target )
103       {
104         ERR << "Target not initialized. Changelog is not available." << std::endl;
105         return Changelog();
106       }
107
108       if ( repository().isSystemRepo() )
109       {
110           target::rpm::RpmHeader::constPtr header;
111           target->rpmDb().getData(name(), header);
112           return header ? header->tag_changelog() : Changelog(); // might be deleted behind our back (bnc #530595)
113       }
114       WAR << "changelog is not available for uninstalled packages" << std::endl;
115       return Changelog();
116   }
117
118   std::string Package::buildhost() const
119   { return lookupStrAttribute( sat::SolvAttr::buildhost ); }
120
121   std::string Package::distribution() const
122   { return lookupStrAttribute( sat::SolvAttr::distribution ); }
123
124   std::string Package::license() const
125   { return lookupStrAttribute( sat::SolvAttr::license ); }
126
127   std::string Package::packager() const
128   { return lookupStrAttribute( sat::SolvAttr::packager ); }
129
130   std::string Package::group() const
131   { return lookupStrAttribute( sat::SolvAttr::group ); }
132
133   Package::Keywords Package::keywords() const
134   { return Keywords( sat::SolvAttr::keywords, satSolvable() ); }
135
136   std::string Package::url() const
137   { return lookupStrAttribute( sat::SolvAttr::url ); }
138
139   ByteCount Package::sourcesize() const
140   { return lookupNumAttribute( sat::SolvAttr::sourcesize ); }
141
142   std::list<std::string> Package::authors() const
143   {
144     std::list<std::string> ret;
145     str::split( lookupStrAttribute( sat::SolvAttr::authors ), std::back_inserter(ret), "\n" );
146     return ret;
147   }
148
149   Package::FileList Package::filelist() const
150   { return FileList( sat::SolvAttr::filelist, satSolvable() ); }
151
152   CheckSum Package::checksum() const
153   { return lookupCheckSumAttribute( sat::SolvAttr::checksum ); }
154
155   OnMediaLocation Package::location() const
156   { return lookupLocation(); }
157
158   namespace
159   {
160     bool schemeIsLocalDir( const Url & url_r )
161     {
162       std::string s( url_r.getScheme() );
163       return s == "dir" || s == "file";
164     }
165   }
166
167   Pathname Package::cachedLocation() const
168   {
169     OnMediaLocation loc( location() );
170     PathInfo pi( repoInfo().packagesPath() / loc.filename() );
171
172     if ( ! pi.isExist() )
173       return Pathname();        // no file in cache
174
175     if ( loc.checksum().empty() )
176     {
177       Url url( repoInfo().url() );
178       if ( ! schemeIsLocalDir( url ) )
179         return Pathname();      // same name but no checksum to verify
180
181       // for local repos compare with the checksum in repo
182       if ( CheckSum( CheckSum::md5Type(), std::ifstream( (url.getPathName() / loc.filename()).c_str() ) )
183         != CheckSum( CheckSum::md5Type(), std::ifstream( pi.c_str() ) ) )
184         return Pathname();      // same name but wrong checksum
185     }
186     else
187     {
188       if ( loc.checksum() != CheckSum( loc.checksum().type(), std::ifstream( pi.c_str() ) ) )
189         return Pathname();      // same name but wrong checksum
190     }
191
192     return pi.path();           // the right one
193   }
194
195   std::string Package::sourcePkgName() const
196   {
197     // no id means same as package
198     sat::detail::IdType id( lookupIdAttribute( sat::SolvAttr::sourcename ) );
199     return id ? IdString( id ).asString() : name();
200   }
201
202   Edition Package::sourcePkgEdition() const
203   {
204    // no id means same as package
205     sat::detail::IdType id( lookupIdAttribute( sat::SolvAttr::sourceevr ) );
206     return id ? Edition( id ) : edition();
207   }
208
209   std::string Package::sourcePkgType() const
210   { return lookupStrAttribute( sat::SolvAttr::sourcearch ); }
211
212   std::string Package::sourcePkgLongName() const
213   { return str::form( "%s-%s.%s", sourcePkgName().c_str(), sourcePkgEdition().c_str(), sourcePkgType().c_str() ); }
214
215
216   /////////////////////////////////////////////////////////////////
217 } // namespace zypp
218 ///////////////////////////////////////////////////////////////////