libzypp: zypp/ZYppCommitPolicy.cc Source File

libzypp 17.36.6

ZYppCommitPolicy.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include <iostream>
14
15#include <zypp-core/base/String.h>
16#include <zypp-core/base/StringV.h>
17
18#include <zypp/ZConfig.h>
19#include <zypp/ZYppCommitPolicy.h>
20#include <zypp-core/base/LogControl.h>
21#include <zypp-core/TriBool.h>
22#include <zypp/PathInfo.h>
23#include <zypp/ZYppCallbacks.h>
24#include <zypp/ZYppFactory.h>
25
27namespace zypp
28{
29
30 inline bool ImZYPPER()
31 {
32 static bool ret = filesystem::readlink( "/proc/self/exe" ).basename() == "zypper";
33 return ret;
34 }
35
36 inline bool singleTransInEnv()
37 {
38 const char *val = ::getenv("ZYPP_SINGLE_RPMTRANS");
39 return ( val && std::string_view( val ) == "1" );
40 }
41
42 inline bool singleTransEnabled()
43 {
44#ifdef SINGLE_RPMTRANS_AS_DEFAULT_FOR_ZYPPER
45 return ImZYPPER();
46#else // SINGLE_RPMTRANS_AS_DEFAULT_FOR_ZYPPER
47 return ImZYPPER() && singleTransInEnv();
48#endif // SINGLE_RPMTRANS_AS_DEFAULT_FOR_ZYPPER
49 }
50
51
52 inline bool isPreUsrmerge( const Pathname & root_r )
53 {
54 // If systems /lib is a directory and not a symlink.
55 return PathInfo( root_r / "lib", PathInfo::LSTAT ).isDir();
56 }
57
58 inline bool transactionWillUsrmerge()
59 {
60 // A package providing "may-perform-usrmerge" is going to be installed.
61 const sat::WhatProvides q { Capability("may-perform-usrmerge") };
62 for ( const auto & pi : q.poolItem() ) {
63 if ( pi.status().isToBeInstalled() )
64 return true;
65 }
66 return false;
67 }
68
69 inline bool pendingUsrmerge()
70 {
71 // NOTE: Bug 1189788 - UsrMerge: filesystem package breaks system when
72 // upgraded in a single rpm transaction. Target::Commit must amend this
73 // request depending on whether a UsrMerge may be performed by the
74 // transaction.
75 Target_Ptr target { getZYpp()->getTarget() };
76 bool ret = target && isPreUsrmerge( target->root() ) && transactionWillUsrmerge();
77 return ret;
78 }
79
81 //
82 // CLASS NAME : ZYppCommitPolicy::Impl
83 //
85
86 class ZYppCommitPolicy::Impl
87 {
88 public:
89 Impl()
90 : _restrictToMedia ( 0 )
91 , _downloadMode ( ZConfig::instance().commit_downloadMode() )
92 , _rpmInstFlags ( ZConfig::instance().rpmInstallFlags() )
93 , _syncPoolAfterCommit ( true )
94 , _singleTransMode ( singleTransEnabled() )
95 {}
96
97 public:
98 unsigned _restrictToMedia;
99 DownloadMode _downloadMode;
100 target::rpm::RpmInstFlags _rpmInstFlags;
101 bool _syncPoolAfterCommit;
102 mutable bool _singleTransMode; // mutable: [bsc#1189788] pending usrmerge must disable singletrans
103
104 private:
105 friend Impl * rwcowClone<Impl>( const Impl * rhs );
107 Impl * clone() const { return new Impl( *this ); }
108 };
109
111 //
112 // CLASS NAME : ZYppCommitPolicy
113 //
115
116 ZYppCommitPolicy::ZYppCommitPolicy()
117 : _pimpl( new Impl )
118 {}
119
120
121 ZYppCommitPolicy & ZYppCommitPolicy::restrictToMedia( unsigned mediaNr_r )
122 { _pimpl->_restrictToMedia = ( mediaNr_r == 1 ) ? 1 : 0; return *this; }
123
124 unsigned ZYppCommitPolicy::restrictToMedia() const
125 { return _pimpl->_restrictToMedia; }
126
127
128 ZYppCommitPolicy & ZYppCommitPolicy::dryRun( bool yesNo_r )
129 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_TEST, yesNo_r ); return *this; }
130
131 bool ZYppCommitPolicy::dryRun() const
132 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_TEST );}
133
134 ZYppCommitPolicy & ZYppCommitPolicy::downloadMode( DownloadMode val_r )
135 { _pimpl->_downloadMode = val_r; return *this; }
136
137 DownloadMode ZYppCommitPolicy::downloadMode() const
138 {
139 if ( singleTransModeEnabled() && _pimpl->_downloadMode == DownloadAsNeeded ) {
140 DBG << _pimpl->_downloadMode << " is not compatible with singleTransMode, falling back to " << DownloadInAdvance << std::endl;
141 return DownloadInAdvance;
142 }
143 return _pimpl->_downloadMode;
144 }
145
146 ZYppCommitPolicy & ZYppCommitPolicy::rpmInstFlags( target::rpm::RpmInstFlags newFlags_r )
147 { _pimpl->_rpmInstFlags = newFlags_r; return *this; }
148
149 ZYppCommitPolicy & ZYppCommitPolicy::rpmNoSignature( bool yesNo_r )
150 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_NOSIGNATURE, yesNo_r ); return *this; }
151
152 ZYppCommitPolicy & ZYppCommitPolicy::rpmExcludeDocs( bool yesNo_r )
153 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_EXCLUDEDOCS, yesNo_r ); return *this; }
154
155 target::rpm::RpmInstFlags ZYppCommitPolicy::rpmInstFlags() const
156 { return _pimpl->_rpmInstFlags; }
157
158 bool ZYppCommitPolicy::rpmNoSignature() const
159 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_NOSIGNATURE ); }
160
161 bool ZYppCommitPolicy::rpmExcludeDocs() const
162 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_EXCLUDEDOCS ); }
163
164 ZYppCommitPolicy &ZYppCommitPolicy::allowDowngrade(bool yesNo_r)
165 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_ALLOWDOWNGRADE, yesNo_r ); return *this; }
166
167 bool ZYppCommitPolicy::allowDowngrade() const
168 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_ALLOWDOWNGRADE ); }
169
170 ZYppCommitPolicy &ZYppCommitPolicy::replaceFiles( bool yesNo_r )
171 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_REPLACEFILES, yesNo_r ); return *this; }
172
173 bool ZYppCommitPolicy::replaceFiles( ) const
174 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_REPLACEFILES ); }
175
176 ZYppCommitPolicy & ZYppCommitPolicy::syncPoolAfterCommit( bool yesNo_r )
177 { _pimpl->_syncPoolAfterCommit = yesNo_r; return *this; }
178
179 bool ZYppCommitPolicy::syncPoolAfterCommit() const
180 { return _pimpl->_syncPoolAfterCommit; }
181
182 bool ZYppCommitPolicy::singleTransModeEnabled() const
183 {
184 if ( _pimpl->_singleTransMode and pendingUsrmerge() ) {
185 WAR << "Ignore $ZYPP_SINGLE_RPMTRANS=1: Bug 1189788 - UsrMerge: filesystem package breaks system when upgraded in a single rpm transaction" << std::endl;
186 JobReport::info(
187 "[bsc#1189788] The filesystem package seems to be unable to perform the pending\n"
188 " UsrMerge reliably in a single transaction. The single_rpmtrans\n"
189 " backend will therefore be IGNORED and the transaction is performed\n"
190 " by the classic_rpmtrans backend."
191 , JobReport::UserData( "cmdout", "[bsc#1189788]" ) );
192 _pimpl->_singleTransMode = false;
193 }
194 return _pimpl->_singleTransMode;
195 }
196
197 std::ostream & operator<<( std::ostream & str, const ZYppCommitPolicy & obj )
198 {
199 str << "CommitPolicy(";
200 if ( obj.restrictToMedia() )
201 str << " restrictToMedia:" << obj.restrictToMedia();
202 if ( obj.dryRun() )
203 str << " dryRun";
204 str << " " << obj.downloadMode();
205 if ( obj.syncPoolAfterCommit() )
206 str << " syncPoolAfterCommit";
207 if ( obj.rpmInstFlags() )
208 str << " rpmInstFlags{" << str::hexstring(obj.rpmInstFlags()) << "}";
209 return str << " )";
210 }
211
213} // namespace zypp
ZConfig.h
ZYppCallbacks.h
ZYppCommitPolicy.h
ZYppFactory.h
Target_Ptr
zypp::Capability
A sat capability.
Definition Capability.h:63
zypp::PathInfo::LSTAT
@ LSTAT
Definition PathInfo.h:231
zypp::ZConfig
Interim helper class to collect global options and settings.
Definition ZConfig.h:69
zypp::ZYppCommitPolicy::Impl
Definition ZYppCommitPolicy.cc:87
zypp::ZYppCommitPolicy::Impl::clone
Impl * clone() const
clone for RWCOW_pointer
Definition ZYppCommitPolicy.cc:107
zypp::ZYppCommitPolicy::Impl::rwcowClone
friend Impl * rwcowClone(const Impl *rhs)
zypp::ZYppCommitPolicy::Impl::_syncPoolAfterCommit
bool _syncPoolAfterCommit
Definition ZYppCommitPolicy.cc:101
zypp::ZYppCommitPolicy::Impl::Impl
Impl()
Definition ZYppCommitPolicy.cc:89
zypp::ZYppCommitPolicy::Impl::_downloadMode
DownloadMode _downloadMode
Definition ZYppCommitPolicy.cc:99
zypp::ZYppCommitPolicy::Impl::_singleTransMode
bool _singleTransMode
Definition ZYppCommitPolicy.cc:102
zypp::ZYppCommitPolicy::Impl::_rpmInstFlags
target::rpm::RpmInstFlags _rpmInstFlags
Definition ZYppCommitPolicy.cc:100
zypp::ZYppCommitPolicy::Impl::_restrictToMedia
unsigned _restrictToMedia
Definition ZYppCommitPolicy.cc:98
zypp::ZYppCommitPolicy::allowDowngrade
bool allowDowngrade() const
Definition ZYppCommitPolicy.cc:167
zypp::ZYppCommitPolicy::rpmInstFlags
ZYppCommitPolicy & rpmInstFlags(target::rpm::RpmInstFlags newFlags_r)
The default target::rpm::RpmInstFlags.
Definition ZYppCommitPolicy.cc:146
zypp::ZYppCommitPolicy::singleTransModeEnabled
bool singleTransModeEnabled() const
Definition ZYppCommitPolicy.cc:182
zypp::ZYppCommitPolicy::syncPoolAfterCommit
ZYppCommitPolicy & syncPoolAfterCommit(bool yesNo_r)
Kepp pool in sync with the Target databases after commit (default: true)
Definition ZYppCommitPolicy.cc:176
zypp::ZYppCommitPolicy::dryRun
ZYppCommitPolicy & dryRun(bool yesNo_r)
Set dry run (default: false).
Definition ZYppCommitPolicy.cc:128
zypp::ZYppCommitPolicy::dryRun
bool dryRun() const
Definition ZYppCommitPolicy.cc:131
zypp::ZYppCommitPolicy::restrictToMedia
ZYppCommitPolicy & restrictToMedia(unsigned mediaNr_r)
Restrict commit to media 1.
Definition ZYppCommitPolicy.cc:121
zypp::ZYppCommitPolicy::ZYppCommitPolicy
ZYppCommitPolicy()
Definition ZYppCommitPolicy.cc:116
zypp::ZYppCommitPolicy::downloadMode
DownloadMode downloadMode() const
Definition ZYppCommitPolicy.cc:137
zypp::ZYppCommitPolicy::rpmNoSignature
bool rpmNoSignature() const
Definition ZYppCommitPolicy.cc:158
zypp::ZYppCommitPolicy::rpmInstFlags
target::rpm::RpmInstFlags rpmInstFlags() const
Definition ZYppCommitPolicy.cc:155
zypp::ZYppCommitPolicy::downloadMode
ZYppCommitPolicy & downloadMode(DownloadMode val_r)
Commit download policy to use.
Definition ZYppCommitPolicy.cc:134
zypp::ZYppCommitPolicy::restrictToMedia
unsigned restrictToMedia() const
Definition ZYppCommitPolicy.cc:124
zypp::ZYppCommitPolicy::rpmExcludeDocs
bool rpmExcludeDocs() const
Definition ZYppCommitPolicy.cc:161
zypp::ZYppCommitPolicy::syncPoolAfterCommit
bool syncPoolAfterCommit() const
Definition ZYppCommitPolicy.cc:179
zypp::ZYppCommitPolicy::_pimpl
RWCOW_pointer< Impl > _pimpl
Pointer to data.
Definition ZYppCommitPolicy.h:106
zypp::ZYppCommitPolicy::replaceFiles
bool replaceFiles() const
Definition ZYppCommitPolicy.cc:173
zypp::ZYppFactory::getZYpp
ZYpp::Ptr getZYpp()
Convenience to get the Pointer to the ZYpp instance.
Definition ZYppFactory.h:77
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::PathInfo::isDir
bool isDir() const
Definition PathInfo.h:296
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::sat::WhatProvides
Container of Solvable providing a Capability (read only).
Definition WhatProvides.h:89
str
String related utilities and Regular expression matching.
zypp::filesystem::readlink
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
Definition PathInfo.cc:929
zypp::str::hexstring
std::string hexstring(char n, int w=4)
Definition String.h:324
zypp::target::rpm::RPMINST_NOSIGNATURE
@ RPMINST_NOSIGNATURE
Definition RpmFlags.h:47
zypp::target::rpm::RPMINST_REPLACEFILES
@ RPMINST_REPLACEFILES
Definition RpmFlags.h:52
zypp::target::rpm::RPMINST_TEST
@ RPMINST_TEST
Definition RpmFlags.h:49
zypp::target::rpm::RPMINST_ALLOWDOWNGRADE
@ RPMINST_ALLOWDOWNGRADE
Definition RpmFlags.h:51
zypp::target::rpm::RPMINST_EXCLUDEDOCS
@ RPMINST_EXCLUDEDOCS
Definition RpmFlags.h:40
zypp::target
Definition KeyRing.h:325
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::pendingUsrmerge
bool pendingUsrmerge()
Definition ZYppCommitPolicy.cc:69
zypp::isPreUsrmerge
bool isPreUsrmerge(const Pathname &root_r)
Definition ZYppCommitPolicy.cc:52
zypp::ImZYPPER
bool ImZYPPER()
Definition ZYppCommitPolicy.cc:30
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition SerialNumber.cc:52
zypp::singleTransEnabled
bool singleTransEnabled()
Definition ZYppCommitPolicy.cc:42
zypp::singleTransInEnv
bool singleTransInEnv()
Definition ZYppCommitPolicy.cc:36
zypp::transactionWillUsrmerge
bool transactionWillUsrmerge()
Definition ZYppCommitPolicy.cc:58
zypp::DownloadMode
DownloadMode
Supported commit download policies.
Definition DownloadMode.h:25
zypp::DownloadAsNeeded
@ DownloadAsNeeded
Alternating download and install.
Definition DownloadMode.h:34
zypp::DownloadInAdvance
@ DownloadInAdvance
First download all packages to the local cache.
Definition DownloadMode.h:29
zypp::JobReport::UserData
callback::UserData UserData
typsafe map of userdata
Definition ZYppCallbacks.h:1079
zypp::JobReport::info
static bool info(const std::string &msg_r, const UserData &userData_r=UserData())
send message text
Definition ZYppCallbacks.h:1097
TriBool.h
LogControl.h
DBG
#define DBG
Definition Logger.h:99
WAR
#define WAR
Definition Logger.h:101
StringV.h
String.h
PathInfo.h