libzypp: zypp/Package.cc Source File

libzypp 17.38.14

Package.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <fstream>
14
15#include <zypp-core/base/LogTools.h>
16#include <zypp-core/base/String.h>
17#include <zypp-core/base/StringV.h>
18#include <zypp/Package.h>
19#include <zypp/sat/LookupAttr.h>
20#include <zypp/ZYppFactory.h>
21#include <zypp/target/rpm/RpmDb.h>
22#include <zypp/target/rpm/RpmHeader.h>
23#include <zypp/ui/Selectable.h>
24#include <zypp/repo/RepoProvideFile.h>
25
27 namespace zyppintern
28{
29 using namespace zypp;
30
32 using GetVendorSupportOptionReturn = std::pair<VendorSupportOption,std::optional<std::set<std::string_view>>>;
33
35 GetVendorSupportOptionReturn getVendorSupportOption( sat::Solvable solv_r )
36 {
37 static const IdString support_unsupported( "support_unsupported" );
38 static const IdString support_acc( "support_acc" );
39 static const IdString support_l1( "support_l1" );
40 static const IdString support_l2( "support_l2" );
41 static const IdString support_l3( "support_l3" );
42 static const IdString support_superseded( "support_superseded" ); // opt. followed by "(PKGNAME)"
43
44 GetVendorSupportOptionReturn ret { VendorSupportUnknown, std::nullopt };
45
46 // max over all identical packages
47 for ( const auto & solv : sat::WhatProvides( (Capability(solv_r.ident().id())) ) )
48 {
49 if ( solv.edition() == solv_r.edition()
50 && solv.ident() == solv_r.ident()
51 && solv_r.identical( solv ) )
52 {
53 for ( PackageKeyword kw : Package::Keywords( sat::SolvAttr::keywords, solv ) )
54 {
55 switch ( ret.first )
56 {
57 case VendorSupportUnknown:
58 if ( kw == support_unsupported ) { ret.first = VendorSupportUnsupported; break; }
59 case VendorSupportUnsupported:
60 if ( kw == support_acc ) { ret.first = VendorSupportACC; break; }
61 case VendorSupportACC:
62 if ( kw == support_l1 ) { ret.first = VendorSupportLevel1; break; }
63 case VendorSupportLevel1:
64 if ( kw == support_l2 ) { ret.first = VendorSupportLevel2; break; }
65 case VendorSupportLevel2:
66 if ( kw == support_l3 ) { ret.first = VendorSupportLevel3; break; }
67 case VendorSupportLevel3:
68 case VendorSupportSuperseded:
69 // VendorSupportSuperseded is special as the kw may also contain a '(PKGNAME)'.
70 // In fact even multiple (PKGNAMES) derived from multiple keywords.
71 if ( strv::hasPrefix( kw.asStringView(), support_superseded.asStringView() ) ) {
72 ret.first = VendorSupportSuperseded;
73 if ( kw.c_str()[support_superseded.size()] == '(' && kw.c_str()[kw.size()-1] == ')' ) {
74 std::string_view val { kw.c_str()+support_superseded.size()+1, kw.size()-support_superseded.size()-2 };
75 if ( not val.empty() ) {
76 if ( not ret.second )
77 ret.second = std::set<std::string_view>();
78 ret.second->insert( val );
79 } else {
80 WAR << "No superseding package defined in " << kw << endl;
81 }
82 } else {
83 WAR << "No superseding package defined in " << kw << endl;
84 }
85 }
86 }
87 }
88 }
89 }
90 return ret;
91 }
92
93 inline bool schemeIsLocalDir( const Url & url_r )
94 {
95 const std::string & s( url_r.getScheme() );
96 return s == "dir" || s == "file";
97 }
98
99 // here and from SrcPackage.cc
100 Pathname cachedLocation( const OnMediaLocation & loc_r, const RepoInfo & repo_r )
101 {
102 PathInfo pi( repo_r.packagesPath() / repo::RepoMediaAccess::mapToCachePath( repo_r, loc_r ) );
103
104 if ( ! pi.isExist() )
105 return Pathname(); // no file in cache
106
107 if ( loc_r.checksum().empty() )
108 {
109 Url url( repo_r.url() );
110 if ( ! schemeIsLocalDir( url ) )
111 return Pathname(); // same name but no checksum to verify
112
113 // for local repos compare with the checksum in repo
114 if ( CheckSum( CheckSum::md5Type(), std::ifstream( (url.getPathName() / repo::RepoMediaAccess::mapToCachePath( repo_r, loc_r )).c_str() ) )
115 != CheckSum( CheckSum::md5Type(), std::ifstream( pi.c_str() ) ) )
116 return Pathname(); // same name but wrong checksum
117 }
118 else
119 {
120 if ( loc_r.checksum() != CheckSum( loc_r.checksum().type(), std::ifstream( pi.c_str() ) ) )
121 return Pathname(); // same name but wrong checksum
122 }
123
124 return pi.path(); // the right one
125 }
126} // namespace zyppintern
127
128
130namespace zypp
131{
132
133 IMPL_PTR_TYPE(Package);
134
136 //
137 // METHOD NAME : Package::Package
138 // METHOD TYPE : Ctor
139 //
140 Package::Package( const sat::Solvable & solvable_r )
141 : ResObject( solvable_r )
142 {}
143
145 //
146 // METHOD NAME : Package::~Package
147 // METHOD TYPE : Dtor
148 //
149 Package::~Package()
150 {}
151
152 VendorSupportOption Package::vendorSupport() const
153 { return zyppintern::getVendorSupportOption( satSolvable() ).first; }
154
155 bool Package::maybeUnsupported() const
156 {
157 switch ( vendorSupport() )
158 {
159 case VendorSupportUnknown:
160 case VendorSupportUnsupported:
161 case VendorSupportACC:
162 case VendorSupportSuperseded:
163 return true;
164
165 case VendorSupportLevel1:
166 case VendorSupportLevel2:
167 case VendorSupportLevel3:
168 break; // intentionally no default:
169 }
170 return false;
171 }
172
173 std::vector<std::string> Package::supersededBy() const
174 {
175 std::vector<std::string> ret;
176 const auto & res = zyppintern::getVendorSupportOption( satSolvable() );
177 if ( res.second ) {
178 // translate the string_view set into stings
179 for ( const auto & v : *res.second )
180 ret.push_back( std::string( v ) );
181 }
182 return ret;
183 }
184
185
186 std::pair<std::vector<IdString>,std::vector<std::string>> Package::supersededByItems() const
187 {
188 std::pair<std::vector<IdString>,std::vector<std::string>> ret;
189 std::vector<std::string> todo { supersededBy() };
190 if ( not todo.empty() )
191 {
192 std::set<std::string> done; // taken from todo and processed
193 std::set<std::string> unresolved; // supersededBy does not name a package
194 std::set<IdString> resolved; // supersededBy resolved to package (not superseded)
195 while ( not todo.empty() ) {
196 auto doneit { done.insert( std::move(todo.back()) ) };
197 todo.pop_back();
198 if ( not doneit.second )
199 continue; // already processed
200 const std::string & next { *doneit.first };
201
202 if ( auto sel = ui::Selectable::get( next ) ) {
203 const std::vector<std::string> & more { sel->supersededBy() };
204 if ( more.empty() ) {
205 resolved.insert( sel->ident() );
206 } else {
207 todo.insert( todo.end(), more.begin(), more.end() );
208 }
209 } else {
210 unresolved.insert( next );
211 }
212 }
213 if ( not resolved.empty() )
214 ret.first.insert( ret.first.end(), resolved.begin(), resolved.end() );
215 if ( not unresolved.empty() )
216 ret.second.insert( ret.second.end(), unresolved.begin(), unresolved.end() );
217 }
218 return ret;
219 }
220
221 Changelog Package::changelog() const
222 {
223 Target_Ptr target( getZYpp()->getTarget() );
224 if ( ! target )
225 {
226 ERR << "Target not initialized. Changelog is not available." << std::endl;
227 return Changelog();
228 }
229
230 if ( repository().isSystemRepo() )
231 {
232 target::rpm::RpmHeader::constPtr header;
233 target->rpmDb().getData(name(), header);
234 return header ? header->tag_changelog() : Changelog(); // might be deleted behind our back (bnc #530595)
235 }
236 WAR << "changelog is not available for uninstalled packages" << std::endl;
237 return Changelog();
238 }
239
240 std::string Package::buildhost() const
241 { return lookupStrAttribute( sat::SolvAttr::buildhost ); }
242
243 std::string Package::distribution() const
244 { return lookupStrAttribute( sat::SolvAttr::distribution ); }
245
246 std::string Package::license() const
247 { return lookupStrAttribute( sat::SolvAttr::license ); }
248
249 std::string Package::packager() const
250 { return lookupStrAttribute( sat::SolvAttr::packager ); }
251
252 std::string Package::group() const
253 { return lookupStrAttribute( sat::SolvAttr::group ); }
254
255 Package::Keywords Package::keywords() const
256 { return Keywords( sat::SolvAttr::keywords, satSolvable() ); }
257
258 std::string Package::url() const
259 { return lookupStrAttribute( sat::SolvAttr::url ); }
260
261 ByteCount Package::sourcesize() const
262 { return lookupNumAttribute( sat::SolvAttr::sourcesize ); }
263
264 std::list<std::string> Package::authors() const
265 {
266 std::list<std::string> ret;
267 str::split( lookupStrAttribute( sat::SolvAttr::authors ), std::back_inserter(ret), "\n" );
268 return ret;
269 }
270
271 Package::FileList Package::filelist() const
272 { return FileList( sat::SolvAttr::filelist, satSolvable() ); }
273
274 CheckSum Package::checksum() const
275 { return lookupCheckSumAttribute( sat::SolvAttr::checksum ); }
276
277 OnMediaLocation Package::location() const
278 { return lookupLocation(); }
279
280 Pathname Package::cachedLocation() const
281 { return zyppintern::cachedLocation( location(), repoInfo() ); }
282
283 std::string Package::sourcePkgName() const
284 {
285 // no id means same as package
286 sat::detail::IdType id( lookupIdAttribute( sat::SolvAttr::sourcename ) );
287 return id ? IdString( id ).asString() : name();
288 }
289
290 Edition Package::sourcePkgEdition() const
291 {
292 // no id means same as package
293 sat::detail::IdType id( lookupIdAttribute( sat::SolvAttr::sourceevr ) );
294 return id ? Edition( id ) : edition();
295 }
296
297 std::string Package::sourcePkgType() const
298 { return lookupStrAttribute( sat::SolvAttr::sourcearch ); }
299
300 std::string Package::sourcePkgLongName() const
301 { return str::form( "%s-%s.%s", sourcePkgName().c_str(), sourcePkgEdition().c_str(), sourcePkgType().c_str() ); }
302
303
305} // namespace zypp
LogTools.h
ERR
#define ERR
Definition Logger.h:105
WAR
#define WAR
Definition Logger.h:104
LookupAttr.h
Package.h
RepoProvideFile.h
RpmDb.h
RpmHeader.h
Selectable.h
StringV.h
String.h
ZYppFactory.h
Target_Ptr
zypp::ByteCount
Store and operate with byte count.
Definition ByteCount.h:32
zypp::Capability::Capability
Capability()
Default ctor, Empty capability.
Definition Capability.h:69
zypp::CheckSum
Definition CheckSum.h:34
zypp::CheckSum::empty
bool empty() const
Definition CheckSum.cc:173
zypp::CheckSum::md5Type
static const std::string & md5Type()
Definition CheckSum.cc:28
zypp::CheckSum::type
std::string type() const
Definition CheckSum.cc:167
zypp::CheckSum::CheckSum
CheckSum()
Default Ctor: empty checksum.
Definition CheckSum.h:37
zypp::Edition
Edition represents [epoch:]version[-release].
Definition Edition.h:60
zypp::Edition::Edition
Edition()
Default ctor: noedition.
Definition Edition.h:76
zypp::IdString
Access to the sat-pools string space.
Definition IdString.h:55
zypp::IdString::size
unsigned size() const
The strings size.
Definition IdString.cc:48
zypp::IdString::id
IdType id() const
Expert backdoor.
Definition IdString.h:144
zypp::IdString::IdString
constexpr IdString()
Default ctor, empty string.
Definition IdString.h:61
zypp::OnMediaLocation
Describes a resource file located on a medium.
Definition onmedialocation.h:37
zypp::OnMediaLocation::checksum
const CheckSum & checksum() const
The checksum of the resource on the server.
Definition onmedialocation.cc:123
zypp::PackageKeyword
Package keywords.
Definition PackageKeyword.h:29
zypp::Package
Package interface.
Definition Package.h:34
zypp::Package::packager
std::string packager() const
Definition Package.cc:249
zypp::Package::sourcePkgLongName
std::string sourcePkgLongName() const
The source rpms "name-version-release.type".
Definition Package.cc:300
zypp::Package::Keywords
sat::ArrayAttr< PackageKeyword, IdString > Keywords
Definition Package.h:42
zypp::Package::maybeUnsupported
bool maybeUnsupported() const
True if the vendor support for this package is unknown or explicitly unsupported.
Definition Package.cc:155
zypp::Package::keywords
Keywords keywords() const
Definition Package.cc:255
zypp::Package::cachedLocation
Pathname cachedLocation() const
Location of the downloaded package in cache or an empty path.
Definition Package.cc:280
zypp::Package::supersededBy
std::vector< std::string > supersededBy() const
The name(s) of the successor package if vendorSupport is VendorSupportSuperseded.
Definition Package.cc:173
zypp::Package::location
OnMediaLocation location() const
Location of the resolvable in the repository.
Definition Package.cc:277
zypp::Package::filelist
FileList filelist() const
Return the packages filelist (if available).
Definition Package.cc:271
zypp::Package::sourcePkgName
std::string sourcePkgName() const
Name of the source rpm this package was built from.
Definition Package.cc:283
zypp::Package::FileList
sat::ArrayAttr< std::string, std::string > FileList
Definition Package.h:43
zypp::Package::url
std::string url() const
Don't ship it as class Url, because it might be in fact anything but a legal Url.
Definition Package.cc:258
zypp::Package::sourcePkgType
std::string sourcePkgType() const
The type of the source rpm ("src" or "nosrc").
Definition Package.cc:297
zypp::Package::Package
Package(const sat::Solvable &solvable_r)
Ctor.
Definition Package.cc:140
zypp::Package::sourcePkgEdition
Edition sourcePkgEdition() const
Edition of the source rpm this package was built from.
Definition Package.cc:290
zypp::Package::~Package
~Package() override
Dtor.
Definition Package.cc:149
zypp::Package::vendorSupport
VendorSupportOption vendorSupport() const
Returns the level of supportability the vendor gives to this package.
Definition Package.cc:152
zypp::Package::checksum
CheckSum checksum() const
Checksum the source says this package should have.
Definition Package.cc:274
zypp::Package::sourcesize
ByteCount sourcesize() const
Size of corresponding the source package.
Definition Package.cc:261
zypp::Package::authors
std::list< std::string > authors() const
Definition Package.cc:264
zypp::Package::group
std::string group() const
Definition Package.cc:252
zypp::Package::buildhost
std::string buildhost() const
Definition Package.cc:240
zypp::Package::changelog
Changelog changelog() const
Get the package change log.
Definition Package.cc:221
zypp::Package::supersededByItems
std::pair< std::vector< IdString >, std::vector< std::string > > supersededByItems() const
The successor package(s) if vendorSupport is VendorSupportSuperseded.
Definition Package.cc:186
zypp::Package::distribution
std::string distribution() const
Definition Package.cc:243
zypp::Package::license
std::string license() const
Definition Package.cc:246
zypp::RepoInfo
What is known about a repository.
Definition RepoInfo.h:72
zypp::RepoInfo::url
Url url() const
Pars pro toto: The first repository url, this is either baseUrls().front() or if no baseUrl is define...
Definition RepoInfo.cc:909
zypp::RepoInfo::packagesPath
Pathname packagesPath() const
packagesPath Checks if the effective user is allowed to write into the system package cache.
Definition RepoInfo.cc:840
zypp::ResObject::ResObject
ResObject(const sat::Solvable &solvable_r)
Ctor.
Definition ResObject.cc:23
zypp::Url
Url manipulation class.
Definition Url.h:93
zypp::Url::getScheme
std::string getScheme() const
Returns the scheme name of the URL.
Definition Url.cc:560
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::PathInfo::path
const Pathname & path() const
Return current Pathname.
Definition PathInfo.h:251
zypp::filesystem::PathInfo::isExist
bool isExist() const
Return whether valid stat info exists.
Definition PathInfo.h:286
zypp::filesystem::PathInfo::c_str
const char * c_str() const
Return current Pathname as C-string.
Definition PathInfo.h:255
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::Pathname
Pathname()
Default ctor: an empty path.
Definition Pathname.h:51
zypp::repo::RepoMediaAccess::mapToCachePath
static Pathname mapToCachePath(const RepoInfo &repo_r, const OnMediaLocation &resource_r)
Map a resource filename to a local path below a repositories cache.
Definition RepoProvideFile.cc:385
zypp::sat::SolvAttr::filelist
static const SolvAttr filelist
Definition SolvAttr.h:118
zypp::sat::SolvAttr::distribution
static const SolvAttr distribution
Definition SolvAttr.h:111
zypp::sat::SolvAttr::group
static const SolvAttr group
Definition SolvAttr.h:114
zypp::sat::SolvAttr::sourceevr
static const SolvAttr sourceevr
Definition SolvAttr.h:121
zypp::sat::SolvAttr::sourcesize
static const SolvAttr sourcesize
Definition SolvAttr.h:116
zypp::sat::SolvAttr::packager
static const SolvAttr packager
Definition SolvAttr.h:113
zypp::sat::SolvAttr::url
static const SolvAttr url
Definition SolvAttr.h:123
zypp::sat::SolvAttr::sourcename
static const SolvAttr sourcename
Definition SolvAttr.h:120
zypp::sat::SolvAttr::checksum
static const SolvAttr checksum
Definition SolvAttr.h:105
zypp::sat::SolvAttr::sourcearch
static const SolvAttr sourcearch
Definition SolvAttr.h:119
zypp::sat::SolvAttr::keywords
static const SolvAttr keywords
Definition SolvAttr.h:115
zypp::sat::SolvAttr::authors
static const SolvAttr authors
Definition SolvAttr.h:117
zypp::sat::SolvAttr::license
static const SolvAttr license
Definition SolvAttr.h:112
zypp::sat::SolvAttr::buildhost
static const SolvAttr buildhost
Definition SolvAttr.h:110
zypp::sat::Solvable
A Solvable object within the sat Pool.
Definition Solvable.h:54
zypp::sat::Solvable::edition
Edition edition() const
The edition (version-release).
Definition Solvable.cc:341
zypp::sat::Solvable::ident
IdString ident() const
The identifier.
Definition Solvable.cc:273
zypp::sat::Solvable::identical
bool identical(const Solvable &rhs) const
Test whether two Solvables have the same content.
Definition Solvable.cc:471
zypp::sat::WhatProvides
Container of Solvable providing a Capability (read only).
Definition WhatProvides.h:89
zypp::target::rpm::RpmHeader::constPtr
intrusive_ptr< const RpmHeader > constPtr
Definition RpmHeader.h:65
zypp::ui::Selectable::get
static Ptr get(const pool::ByIdent &ident_r)
Get the Selctable.
Definition Selectable.cc:29
zypp::sat::detail::IdType
int IdType
Generic Id type.
Definition PoolDefines.h:42
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
zypp::str::split
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition String.h:602
zypp::target
Definition KeyRing.h:325
zypp::url
Url details namespace.
Definition UrlBase.cc:58
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::getZYpp
ZYpp::Ptr getZYpp()
relates: ZYppFactory Convenience to get the Pointer to the ZYpp instance.
Definition ZYppFactory.h:77
zypp::VendorSupportOption
VendorSupportOption
Definition VendorSupportOptions.h:22
zypp::VendorSupportACC
@ VendorSupportACC
Additional Customer Contract necessary.
Definition VendorSupportOptions.h:34
zypp::VendorSupportLevel3
@ VendorSupportLevel3
Problem resolution, which means technical support designed to resolve complex problems by engaging en...
Definition VendorSupportOptions.h:60
zypp::VendorSupportUnknown
@ VendorSupportUnknown
The support for this package is unknown.
Definition VendorSupportOptions.h:26
zypp::VendorSupportLevel1
@ VendorSupportLevel1
Problem determination, which means technical support designed to provide compatibility information,...
Definition VendorSupportOptions.h:44
zypp::VendorSupportSuperseded
@ VendorSupportSuperseded
The package was discontinued and has been superseded by a new package with a different name.
Definition VendorSupportOptions.h:74
zypp::VendorSupportUnsupported
@ VendorSupportUnsupported
The package is known to be unsupported by the vendor.
Definition VendorSupportOptions.h:30
zypp::VendorSupportLevel2
@ VendorSupportLevel2
Problem isolation, which means technical support designed to duplicate customer problems,...
Definition VendorSupportOptions.h:52
zypp::Changelog
std::list< ChangelogEntry > Changelog
List of ChangelogEntry.
Definition Changelog.h:55
zypp::IMPL_PTR_TYPE
IMPL_PTR_TYPE(Application)
zyppintern
Definition Package.cc:28
zyppintern::GetVendorSupportOptionReturn
std::pair< VendorSupportOption, std::optional< std::set< std::string_view > > > GetVendorSupportOptionReturn
getVendorSupportOption return value
Definition Package.cc:32
zyppintern::getVendorSupportOption
GetVendorSupportOptionReturn getVendorSupportOption(sat::Solvable solv_r)
Get VendorSupportOption and the PKGNAME if VendorSupportSuperseded.
Definition Package.cc:35
zyppintern::cachedLocation
Pathname cachedLocation(const OnMediaLocation &loc_r, const RepoInfo &repo_r)
Definition Package.cc:100
zyppintern::schemeIsLocalDir
bool schemeIsLocalDir(const Url &url_r)
Definition Package.cc:93
zypp::sat::SolvableType< Resolvable >::lookupLocation
OnMediaLocation lookupLocation() const
Definition SolvableType.h:165
zypp::sat::SolvableType< Resolvable >::id
Solvable::IdType id() const
Definition SolvableType.h:166
zypp::sat::SolvableType< Resolvable >::name
std::string name() const
Definition SolvableType.h:70
zypp::sat::SolvableType< Resolvable >::lookupCheckSumAttribute
CheckSum lookupCheckSumAttribute(const SolvAttr &attr) const
Definition SolvableType.h:164
zypp::sat::SolvableType< Resolvable >::lookupStrAttribute
std::string lookupStrAttribute(const SolvAttr &attr) const
Definition SolvableType.h:158
zypp::sat::SolvableType< Resolvable >::repoInfo
RepoInfo repoInfo() const
Definition SolvableType.h:76
zypp::sat::SolvableType< Resolvable >::satSolvable
Solvable satSolvable() const
Definition SolvableType.h:57
zypp::sat::SolvableType< Resolvable >::lookupNumAttribute
unsigned long long lookupNumAttribute(const SolvAttr &attr) const
Definition SolvableType.h:162
zypp::sat::SolvableType< Resolvable >::repository
Repository repository() const
Definition SolvableType.h:75
zypp::sat::SolvableType< Resolvable >::edition
Edition edition() const
Definition SolvableType.h:71
zypp::sat::SolvableType< Resolvable >::lookupIdAttribute
detail::IdType lookupIdAttribute(const SolvAttr &attr) const
Definition SolvableType.h:161