libzypp 17.37.10

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-core/APIConfig.h>
20#include <zypp/ZYppCommitPolicy.h>
21#include <zypp-core/base/LogControl.h>
22#include <zypp-core/TriBool.h>
23#include <zypp/PathInfo.h>
24#include <zypp/ZYppCallbacks.h>
25#include <zypp/ZYppFactory.h>
26
28 namespace zypp
29{
30
32#ifdef SUSE
33
34#if APIConfig(LIBZYPP_CONFIG_USE_CLASSIC_RPMTRANS_BY_DEFAULT)
35 // In old codebases it can be explicitly enabled for zypper only via ZYPP_SINGLE_RPMTRANS.
36 inline bool ImZYPPER()
37 {
38 static bool ret = filesystem::readlink( "/proc/self/exe" ).basename() == "zypper";
39 return ret;
40 }
41 inline bool singleTransInEnv()
42 {
43 const char *val = ::getenv("ZYPP_SINGLE_RPMTRANS");
44 return ( val && str::strToFalse( val ) );
45 }
46 inline bool singleTransEnabled()
47 { return ImZYPPER() && singleTransInEnv(); }
48#else
49 // SUSE using singletrans as default may allow to explicitly disable it via ZYPP_SINGLE_RPMTRANS.
50 // NOTE: singleTransInEnv() here is no real enablement because it defaults to false
51 // if ZYPP_SINGLE_RPMTRANS is undefined. By now it just allows using singletrans
52 // with all applications, not just zypper. In case it actually becomes the
53 // default on SUSE, we may just want a method to explicitly disable it.
54 inline bool singleTransInEnv()
55 {
56 const char *val = ::getenv("ZYPP_SINGLE_RPMTRANS");
57 return ( val && str::strToFalse( val ) );
58 }
59 inline bool singleTransEnabled()
60 { return singleTransInEnv(); }
61#endif
62
63#else // not SUSE
64 inline bool singleTransEnabled()
65 { return true; }
66#endif
68
69 inline bool isPreUsrmerge( const Pathname & root_r )
70 {
71 // If systems /lib is a directory and not a symlink.
72 return PathInfo( root_r / "lib", PathInfo::LSTAT ).isDir();
73 }
74
75 inline bool transactionWillUsrmerge()
76 {
77 // A package providing "may-perform-usrmerge" is going to be installed.
78 const sat::WhatProvides q { Capability("may-perform-usrmerge") };
79 for ( const auto & pi : q.poolItem() ) {
80 if ( pi.status().isToBeInstalled() )
81 return true;
82 }
83 return false;
84 }
85
86 inline bool pendingUsrmerge()
87 {
88#if 1
89 // introduced solely for the use in QA test for Code16
90 const char *val = ::getenv("ZYPP_NO_USRMERGE_PROTECT");
91 if ( val && str::strToTrue( val ) )
92 return false;
93#endif
94
95 // NOTE: Bug 1189788 - UsrMerge: filesystem package breaks system when
96 // upgraded in a single rpm transaction. Target::Commit must amend this
97 // request depending on whether a UsrMerge may be performed by the
98 // transaction.
99 Target_Ptr target { getZYpp()->getTarget() };
100 bool ret = target && isPreUsrmerge( target->root() ) && transactionWillUsrmerge();
101 return ret;
102 }
103
105 //
106 // CLASS NAME : ZYppCommitPolicy::Impl
107 //
109
110 class ZYppCommitPolicy::Impl
111 {
112 public:
113 Impl()
114 : _restrictToMedia ( 0 )
115 , _downloadMode ( ZConfig::instance().commit_downloadMode() )
116 , _rpmInstFlags ( ZConfig::instance().rpmInstallFlags() )
117 , _syncPoolAfterCommit ( true )
118 , _singleTransMode ( singleTransEnabled() )
119 {}
120
121 public:
122 unsigned _restrictToMedia;
123 DownloadMode _downloadMode;
124 target::rpm::RpmInstFlags _rpmInstFlags;
125 bool _syncPoolAfterCommit;
126 bool _singleTransMode;
127
128 mutable bool _notifyBSC1189788 = true; // mutable: send notification just once
129
130 private:
131 friend Impl * rwcowClone<Impl>( const Impl * rhs );
133 Impl * clone() const { return new Impl( *this ); }
134 };
135
137 //
138 // CLASS NAME : ZYppCommitPolicy
139 //
141
142 ZYppCommitPolicy::ZYppCommitPolicy()
143 : _pimpl( new Impl )
144 {}
145
146
147 ZYppCommitPolicy & ZYppCommitPolicy::restrictToMedia( unsigned mediaNr_r )
148 { _pimpl->_restrictToMedia = ( mediaNr_r == 1 ) ? 1 : 0; return *this; }
149
150 unsigned ZYppCommitPolicy::restrictToMedia() const
151 { return _pimpl->_restrictToMedia; }
152
153
154 ZYppCommitPolicy & ZYppCommitPolicy::dryRun( bool yesNo_r )
155 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_TEST, yesNo_r ); return *this; }
156
157 bool ZYppCommitPolicy::dryRun() const
158 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_TEST );}
159
160 ZYppCommitPolicy & ZYppCommitPolicy::downloadMode( DownloadMode val_r )
161 { _pimpl->_downloadMode = val_r; return *this; }
162
163 DownloadMode ZYppCommitPolicy::downloadMode() const
164 { return _pimpl->_downloadMode; }
165
166 ZYppCommitPolicy & ZYppCommitPolicy::rpmInstFlags( target::rpm::RpmInstFlags newFlags_r )
167 { _pimpl->_rpmInstFlags = newFlags_r; return *this; }
168
169 ZYppCommitPolicy & ZYppCommitPolicy::rpmNoSignature( bool yesNo_r )
170 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_NOSIGNATURE, yesNo_r ); return *this; }
171
172 ZYppCommitPolicy & ZYppCommitPolicy::rpmExcludeDocs( bool yesNo_r )
173 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_EXCLUDEDOCS, yesNo_r ); return *this; }
174
175 target::rpm::RpmInstFlags ZYppCommitPolicy::rpmInstFlags() const
176 { return _pimpl->_rpmInstFlags; }
177
178 bool ZYppCommitPolicy::rpmNoSignature() const
179 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_NOSIGNATURE ); }
180
181 bool ZYppCommitPolicy::rpmExcludeDocs() const
182 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_EXCLUDEDOCS ); }
183
184 ZYppCommitPolicy &ZYppCommitPolicy::allowDowngrade(bool yesNo_r)
185 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_ALLOWDOWNGRADE, yesNo_r ); return *this; }
186
187 bool ZYppCommitPolicy::allowDowngrade() const
188 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_ALLOWDOWNGRADE ); }
189
190 ZYppCommitPolicy &ZYppCommitPolicy::replaceFiles( bool yesNo_r )
191 { _pimpl->_rpmInstFlags.setFlag( target::rpm::RPMINST_REPLACEFILES, yesNo_r ); return *this; }
192
193 bool ZYppCommitPolicy::replaceFiles( ) const
194 { return _pimpl->_rpmInstFlags.testFlag( target::rpm::RPMINST_REPLACEFILES ); }
195
196 ZYppCommitPolicy & ZYppCommitPolicy::syncPoolAfterCommit( bool yesNo_r )
197 { _pimpl->_syncPoolAfterCommit = yesNo_r; return *this; }
198
199 bool ZYppCommitPolicy::syncPoolAfterCommit() const
200 { return _pimpl->_syncPoolAfterCommit; }
201
202 bool ZYppCommitPolicy::singleTransModeEnabled() const
203 {
204 if ( _pimpl->_singleTransMode ) {
205 // Explicitly selecting DownloadAsNeeded also selects the classic_rpmtrans backend.
206 // NOTE: This behavior was negotiated with YAST/Agama and is required for
207 // certain install scenarios. Don't change it without checking with the teams.
208 if ( _pimpl->_downloadMode == DownloadAsNeeded ) {
209 MIL << "DownloadAsNeeded enforces the classic_rpmtrans backend!" << std::endl;
210 return false;
211 }
212
213 if ( pendingUsrmerge() ) {
214 WAR << "Ignore $ZYPP_SINGLE_RPMTRANS=1: Bug 1189788 - UsrMerge: filesystem package breaks system when upgraded in a single rpm transaction" << std::endl;
215 if ( _pimpl->_notifyBSC1189788 ) {
216 _pimpl->_notifyBSC1189788 = false;
217 JobReport::info(
218 "[bsc#1189788] The filesystem package seems to be unable to perform the pending\n"
219 " UsrMerge reliably in a single transaction. The single_rpmtrans\n"
220 " backend will therefore be IGNORED and the transaction is performed\n"
221 " by the classic_rpmtrans backend."
222 , JobReport::UserData( "cmdout", "[bsc#1189788]" ) );
223 }
224 return false;
225 }
226 }
227 return _pimpl->_singleTransMode;
228 }
229
230 std::ostream & operator<<( std::ostream & str, const ZYppCommitPolicy & obj )
231 {
232 str << "CommitPolicy(";
233 if ( obj.restrictToMedia() )
234 str << " restrictToMedia:" << obj.restrictToMedia();
235 if ( obj.dryRun() )
236 str << " dryRun";
237 str << " " << obj.downloadMode();
238 if ( obj.syncPoolAfterCommit() )
239 str << " syncPoolAfterCommit";
240 if ( obj.rpmInstFlags() )
241 str << " rpmInstFlags{" << str::hexstring(obj.rpmInstFlags()) << "}";
242 return str << " )";
243 }
244
246} // 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:111
zypp::ZYppCommitPolicy::Impl::clone
Impl * clone() const
clone for RWCOW_pointer
Definition ZYppCommitPolicy.cc:133
zypp::ZYppCommitPolicy::Impl::rwcowClone
friend Impl * rwcowClone(const Impl *rhs)
zypp::ZYppCommitPolicy::Impl::_notifyBSC1189788
bool _notifyBSC1189788
Definition ZYppCommitPolicy.cc:128
zypp::ZYppCommitPolicy::Impl::_syncPoolAfterCommit
bool _syncPoolAfterCommit
Definition ZYppCommitPolicy.cc:125
zypp::ZYppCommitPolicy::Impl::Impl
Impl()
Definition ZYppCommitPolicy.cc:113
zypp::ZYppCommitPolicy::Impl::_downloadMode
DownloadMode _downloadMode
Definition ZYppCommitPolicy.cc:123
zypp::ZYppCommitPolicy::Impl::_singleTransMode
bool _singleTransMode
Definition ZYppCommitPolicy.cc:126
zypp::ZYppCommitPolicy::Impl::_rpmInstFlags
target::rpm::RpmInstFlags _rpmInstFlags
Definition ZYppCommitPolicy.cc:124
zypp::ZYppCommitPolicy::Impl::_restrictToMedia
unsigned _restrictToMedia
Definition ZYppCommitPolicy.cc:122
zypp::ZYppCommitPolicy::allowDowngrade
bool allowDowngrade() const
Definition ZYppCommitPolicy.cc:187
zypp::ZYppCommitPolicy::rpmInstFlags
ZYppCommitPolicy & rpmInstFlags(target::rpm::RpmInstFlags newFlags_r)
The default target::rpm::RpmInstFlags.
Definition ZYppCommitPolicy.cc:166
zypp::ZYppCommitPolicy::singleTransModeEnabled
bool singleTransModeEnabled() const
Whether the single_rpmtrans backend is enabled (or the classic_rpmtrans)
Definition ZYppCommitPolicy.cc:202
zypp::ZYppCommitPolicy::syncPoolAfterCommit
ZYppCommitPolicy & syncPoolAfterCommit(bool yesNo_r)
Kepp pool in sync with the Target databases after commit (default: true)
Definition ZYppCommitPolicy.cc:196
zypp::ZYppCommitPolicy::dryRun
ZYppCommitPolicy & dryRun(bool yesNo_r)
Set dry run (default: false).
Definition ZYppCommitPolicy.cc:154
zypp::ZYppCommitPolicy::dryRun
bool dryRun() const
Definition ZYppCommitPolicy.cc:157
zypp::ZYppCommitPolicy::restrictToMedia
ZYppCommitPolicy & restrictToMedia(unsigned mediaNr_r)
Restrict commit to media 1.
Definition ZYppCommitPolicy.cc:147
zypp::ZYppCommitPolicy::ZYppCommitPolicy
ZYppCommitPolicy()
Definition ZYppCommitPolicy.cc:142
zypp::ZYppCommitPolicy::downloadMode
DownloadMode downloadMode() const
Definition ZYppCommitPolicy.cc:163
zypp::ZYppCommitPolicy::rpmNoSignature
bool rpmNoSignature() const
Definition ZYppCommitPolicy.cc:178
zypp::ZYppCommitPolicy::rpmInstFlags
target::rpm::RpmInstFlags rpmInstFlags() const
Definition ZYppCommitPolicy.cc:175
zypp::ZYppCommitPolicy::downloadMode
ZYppCommitPolicy & downloadMode(DownloadMode val_r)
Commit download policy to use.
Definition ZYppCommitPolicy.cc:160
zypp::ZYppCommitPolicy::restrictToMedia
unsigned restrictToMedia() const
Definition ZYppCommitPolicy.cc:150
zypp::ZYppCommitPolicy::rpmExcludeDocs
bool rpmExcludeDocs() const
Definition ZYppCommitPolicy.cc:181
zypp::ZYppCommitPolicy::syncPoolAfterCommit
bool syncPoolAfterCommit() const
Definition ZYppCommitPolicy.cc:199
zypp::ZYppCommitPolicy::_pimpl
RWCOW_pointer< Impl > _pimpl
Pointer to data.
Definition ZYppCommitPolicy.h:112
zypp::ZYppCommitPolicy::replaceFiles
bool replaceFiles() const
Definition ZYppCommitPolicy.cc:193
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::SolvIterMixin::poolItem
Iterable< PoolItem_iterator > poolItem() const
Definition SolvIterMixin.h:166
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::strToTrue
bool strToTrue(const C_Str &str)
Parsing boolean from string.
Definition String.cc:66
zypp::str::hexstring
std::string hexstring(char n, int w=4)
Definition String.h:325
zypp::str::strToFalse
bool strToFalse(const C_Str &str)
Return false if str is 0, false, no, off, never.
Definition String.cc:84
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:86
zypp::isPreUsrmerge
bool isPreUsrmerge(const Pathname &root_r)
Definition ZYppCommitPolicy.cc:69
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition SerialNumber.cc:52
zypp::singleTransEnabled
bool singleTransEnabled()
Definition ZYppCommitPolicy.cc:64
zypp::transactionWillUsrmerge
bool transactionWillUsrmerge()
Definition ZYppCommitPolicy.cc:75
zypp::DownloadMode
DownloadMode
Supported commit download policies.
Definition DownloadMode.h:25
zypp::DownloadAsNeeded
@ DownloadAsNeeded
Definition DownloadMode.h:34
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:1099
TriBool.h
LogControl.h
MIL
#define MIL
Definition Logger.h:100
WAR
#define WAR
Definition Logger.h:101
StringV.h
String.h
PathInfo.h