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