libzypp 17.35.12
RpmPostTransCollector.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
11#include <iostream>
12#include <fstream>
13#include <optional>
14#include <utility>
15#include <zypp/base/LogTools.h>
17#include <zypp/base/Gettext.h>
18#include <zypp/base/Regex.h>
19#include <zypp/base/IOStream.h>
22
23#include <zypp/TmpPath.h>
24#include <zypp/PathInfo.h>
25#include <zypp/HistoryLog.h>
26#include <zypp/ZYppCallbacks.h>
30#include <zypp/ZConfig.h>
31#include <zypp/ZYppCallbacks.h>
32
33using std::endl;
34#undef ZYPP_BASE_LOGGER_LOGGROUP
35#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::posttrans"
36
38namespace zypp
39{
41 namespace target
42 {
48 {
49 friend std::ostream & operator<<( std::ostream & str, const Impl & obj );
50 friend std::ostream & dumpOn( std::ostream & str, const Impl & obj );
51
53 using ScriptList = std::list< std::pair<std::string,std::string> >;
54
56 struct Dumpfile
57 {
58 Dumpfile( Pathname dumpfile_r )
59 : _dumpfile { std::move(dumpfile_r) }
60 {}
61
63 size_t _numscripts = 0;
64 bool _runposttrans = true;
65 };
66
67 public:
68 Impl( Pathname &&root_r )
69 : _root(std::move( root_r ))
70 , _myJobReport { "cmdout", "%posttrans" }
71 {}
72
73 Impl(const Impl &) = delete;
74 Impl(Impl &&) = delete;
75 Impl &operator=(const Impl &) = delete;
76 Impl &operator=(Impl &&) = delete;
77
79 {}
80
81 bool hasPosttransScript( const Pathname & rpmPackage_r )
82 { return bool(getHeaderIfPosttrans( rpmPackage_r )); }
83
84 void collectPosttransInfo( const Pathname & rpmPackage_r, const std::vector<std::string> & runposttrans_r )
85 { if ( not collectDumpPosttransLines( runposttrans_r ) ) collectScriptForPackage( rpmPackage_r ); }
86
87 void collectPosttransInfo( const std::vector<std::string> & runposttrans_r )
88 { collectDumpPosttransLines( runposttrans_r ); }
89
91 {
92 if ( pkg ) {
93 if ( not _scripts ) {
95 }
96
97 filesystem::TmpFile script( tmpDir(), pkg->ident() );
98 filesystem::addmod( script.path(), 0500 ); // script must be executable
99 script.autoCleanup( false ); // no autodelete; within a tmpdir
100 {
101 std::ofstream out( script.path().c_str() );
102 out << "#! " << pkg->tag_posttransprog() << endl
103 << pkg->tag_posttrans() << endl;
104 }
105
106 _scripts->push_back( std::make_pair( script.path().basename(), pkg->tag_name() ) );
107 MIL << "COLLECT posttrans: '" << PathInfo( script.path() ) << "' for package: '" << pkg->tag_name() << "'" << endl;
108 }
109 }
110
111 void collectScriptForPackage( const Pathname & rpmPackage_r )
112 { collectScriptFromHeader( getHeaderIfPosttrans( rpmPackage_r ) ); }
113
119 bool collectDumpPosttransLines( const std::vector<std::string> & runposttrans_r )
120 {
121 if ( runposttrans_r.empty() ) {
122 if ( _dumpfile and _dumpfile->_runposttrans ) {
123 MIL << "LOST dump_posttrans support" << endl;
124 _dumpfile->_runposttrans = false; // rpm was downgraded to a version not supporing --runposttrans
125 }
126 return false;
127 }
128
129 if ( not _dumpfile ) {
130 filesystem::TmpFile dumpfile( tmpDir(), "dumpfile" );
131 filesystem::addmod( dumpfile.path(), 0400 ); // dumpfile must be readable
132 dumpfile.autoCleanup( false ); // no autodelete; within a tmpdir
133 _dumpfile = Dumpfile( dumpfile.path() );
134 MIL << "COLLECT dump_posttrans to '" << _dumpfile->_dumpfile << endl;
135 }
136
137 std::ofstream out( _dumpfile->_dumpfile.c_str(), std::ios_base::app );
138 for ( const auto & s : runposttrans_r ) {
139 out << s << endl;
140 }
141 _dumpfile->_numscripts += runposttrans_r.size();
142 MIL << "COLLECT " << runposttrans_r.size() << " dump_posttrans lines" << endl;
143 return true;
144 }
145
155 {
156 if ( _dumpfile && not _dumpfile->_runposttrans ) {
157 // Here a downgraded rpm lost the ability to --runposttrans. Extract at least any
158 // missing %posttrans scripts collected in _dumpfile and prepend them to the _scripts.
159 MIL << "Extract missing %posttrans scripts and prepend them to the scripts." << endl;
160
161 // collectScriptFromHeader appends to _scripts, so we save here and append again later
162 std::optional<ScriptList> savedscripts;
163 if ( _scripts ) {
164 savedscripts = std::move(*_scripts);
165 _scripts = std::nullopt;
166 }
167
169 recallFromDumpfile( _dumpfile->_dumpfile, [&]( const std::string& n_r, const std::string& v_r, const std::string& r_r, const std::string& a_r ) -> void {
170 if ( it.findPackage( n_r, Edition( v_r, r_r ) ) && headerHasPosttrans( *it ) )
171 collectScriptFromHeader( *it );
172 } );
173
174 // append any savedscripts
175 if ( savedscripts ) {
176 if ( _scripts ) {
177 _scripts->splice( _scripts->end(), *savedscripts );
178 } else {
179 _scripts = std::move(*savedscripts);
180 }
181 }
182 _dumpfile = std::nullopt;
183 }
184
185 if ( not ( _scripts || _dumpfile ) )
186 return; // Nothing todo
187
188 // ProgressReport counting the scripts ( 0:preparation, 1->n:for n scripts, n+1: indicate success)
190 ProgressData scriptProgress( [&]() -> ProgressData::value_type {
192 if ( _scripts )
193 ret += _scripts->size();
194 if ( _dumpfile )
195 ret += _dumpfile->_numscripts;
196 return ret;
197 }() );
198 scriptProgress.sendTo( ProgressReportAdaptor( ProgressData::ReceiverFnc(), report ) );
199 // Translator: progress bar label
200 std::string scriptProgressName { _("Running post-transaction scripts") };
201 // Translator: progress bar label; %1% is a script identifier like '%posttrans(mypackage-2-0.noarch)'
202 str::Format fmtScriptProgressRun { _("Running %1% script") };
203 // Translator: headline; %1% is a script identifier like '%posttrans(mypackage-2-0.noarch)'
204 str::Format fmtRipoff { _("%1% script output:") };
205 std::string sendRipoff;
206
207 HistoryLog historylog;
208
209 // lambda to prepare reports for a new script
210 auto startNewScript = [&] ( const std::string & scriptident_r ) -> void {
211 // scriptident_r : script identifier like "%transfiletriggerpostun(istrigger-2-0.noarch)"
212 sendRipoff = fmtRipoff % scriptident_r;
213 scriptProgress.name( fmtScriptProgressRun % scriptident_r );
214 scriptProgress.incr();
215 };
216
217 // lambda to send script output to reports
218 auto sendScriptOutput = [&] ( const std::string & line_r ) -> void {
219 OnScopeExit cleanup; // in case we need it
220 if ( not sendRipoff.empty() ) {
221 historylog.comment( sendRipoff, true /*timestamp*/);
222 _myJobReport.set( "ripoff", std::cref(sendRipoff) );
223 cleanup.setDispose( [&]() -> void {
224 _myJobReport.erase( "ripoff" );
225 sendRipoff.clear();
226 } );
227 }
228 historylog.comment( line_r );
229 _myJobReport.info( line_r );
230 };
231
232 // send the initial progress report
233 scriptProgress.name( scriptProgressName );
234 scriptProgress.toMin();
235
236 // Scripts first...
237 if ( _scripts ) {
238 Pathname noRootScriptDir( ZConfig::instance().update_scriptsPath() / tmpDir().basename() );
239 // like rpm would report it (intentionally not translated and NL-terminated):
240 str::Format fmtScriptFailedMsg { "warning: %%posttrans(%1%) scriptlet failed, exit status %2%\n" };
241 str::Format fmtPosttrans { "%%posttrans(%1%)" };
242
243 rpm::RpmDb::db_const_iterator it { rpm_r.dbConstIterator() }; // Open DB only once
244 while ( ! _scripts->empty() )
245 {
246 const auto &scriptPair = _scripts->front();
247 const std::string & script = scriptPair.first;
248 const std::string & pkgident( script.substr( 0, script.size()-6 ) ); // strip tmp file suffix[6]
249 startNewScript( fmtPosttrans % pkgident );
250
251 int npkgs = 0;
252 for ( it.findByName( scriptPair.second ); *it; ++it )
253 ++npkgs;
254
255 MIL << "EXECUTE posttrans: " << script << " with argument: " << npkgs << endl;
257 "/bin/sh",
258 (noRootScriptDir/script).asString(),
259 str::numstring( npkgs )
260 };
261 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout, false, -1, true, _root );
262
263 for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() ) {
264 sendScriptOutput( line );
265 }
266 //script was executed, remove it from the list
267 _scripts->pop_front();
268
269 int ret = prog.close();
270 if ( ret != 0 )
271 {
272 std::string msg { fmtScriptFailedMsg % pkgident % ret };
273 WAR << msg;
274 sendScriptOutput( msg ); // info!, as rpm would have reported it.
275 }
276 }
277 _scripts = std::nullopt;
278 }
279
280 // ...then 'rpm --runposttrans'
281 int res = 0; // Indicate a failed call to rpm itself! (a failed script is just a warning)
282 if ( _dumpfile ) {
283 res = rpm_r.runposttrans( _dumpfile->_dumpfile, [&] ( const std::string & line_r ) ->void {
284 if ( str::startsWith( line_r, "RIPOFF:" ) )
285 startNewScript( line_r.substr( 7 ) ); // new scripts ident sent by rpm
286 else
287 sendScriptOutput( line_r );
288 } );
289 if ( res != 0 )
290 _myJobReport.error( str::Format("rpm --runposttrans returned %1%.") % res );
291
292 _dumpfile = std::nullopt;
293 }
294
295 // send a final progress report
296 scriptProgress.name( scriptProgressName );
297 if ( res == 0 )
298 scriptProgress.toMax(); // Indicate 100%, in case Dumpfile::_numscripts estimation was off
299 return;
300 }
301
307 {
308 if ( not ( _scripts || _dumpfile ) )
309 return; // Nothing todo
310
311 str::Str msg;
312
313 if ( _scripts ) {
314 // Legacy format logs all collected %posttrans
315 msg << "%posttrans scripts skipped while aborting:" << endl;
316 for ( const auto & script : *_scripts )
317 {
318 WAR << "UNEXECUTED posttrans: " << script.first << endl;
319 const std::string & pkgident( script.first.substr( 0, script.first.size()-6 ) ); // strip tmp file suffix[6]
320 msg << " " << pkgident << "\n";
321 }
322 _scripts = std::nullopt;
323 }
324
325 if ( _dumpfile ) {
326 msg << "%posttrans and %transfiletrigger scripts are not executed when aborting!" << endl;
327 _dumpfile = std::nullopt;
328 }
329
330 HistoryLog historylog;
331 historylog.comment( msg, true /*timestamp*/);
332 _myJobReport.warning( msg );
333 }
334
335 private:
338 {
339 if ( !_ptrTmpdir ) _ptrTmpdir.reset( new filesystem::TmpDir( _root / ZConfig::instance().update_scriptsPath(), "posttrans" ) );
340 DBG << _ptrTmpdir->path() << endl;
341 return _ptrTmpdir->path();
342 }
343
346 {
347 bool ret = false;
348 if ( pkg_r ) {
349 std::string prog( pkg_r->tag_posttransprog() );
350 if ( not prog.empty() && prog != "<lua>" ) // by now leave lua to rpm
351 ret = true;
352 }
353 return ret;
354 }
355
360 {
361 if ( _headercache.first == rpmPackage_r )
362 return _headercache.second;
363
365 if ( ret ) {
366 if ( not headerHasPosttrans( ret ) )
367 ret = nullptr;
368 } else {
369 WAR << "Unexpectedly this is no package: " << rpmPackage_r << endl;
370 }
371 _headercache = std::make_pair( rpmPackage_r, ret );
372 return ret;
373 }
374
376 void recallFromDumpfile( const Pathname & dumpfile_r, std::function<void(std::string,std::string,std::string,std::string)> consume_r )
377 {
378 // dump_posttrans: install 10 terminfo-base-6.4.20230819-19.1.x86_64
379 static const str::regex rxInstalled { "^dump_posttrans: +install +[0-9]+ +(.+)-([^-]+)-([^-]+)\\.([^.]+)" };
380 str::smatch what;
381 iostr::forEachLine( InputStream( dumpfile_r ), [&]( int num_r, const std::string& line_r ) -> bool {
382 if( str::regex_match( line_r, what, rxInstalled ) )
383 consume_r( what[1], what[2], what[3], what[4] );
384 return true; // continue iostr::forEachLine
385 } );
386 }
387
388 private:
390 std::optional<ScriptList> _scripts;
391 std::optional<Dumpfile> _dumpfile;
392 boost::scoped_ptr<filesystem::TmpDir> _ptrTmpdir;
393
395
396 std::pair<Pathname,rpm::RpmHeader::constPtr> _headercache;
397 };
398
400 inline std::ostream & operator<<( std::ostream & str, const RpmPostTransCollector::Impl & obj )
401 { return str << "RpmPostTransCollector::Impl"; }
402
404 inline std::ostream & dumpOn( std::ostream & str, const RpmPostTransCollector::Impl & obj )
405 { return str << obj; }
406
408 //
409 // CLASS NAME : RpmPostTransCollector
410 //
412
414 : _pimpl( new Impl( std::move(root_r) ) )
415 {}
416
419
421 { return _pimpl->hasPosttransScript( rpmPackage_r ); }
422
423 void RpmPostTransCollector::collectPosttransInfo( const Pathname & rpmPackage_r, const std::vector<std::string> & runposttrans_r )
424 { _pimpl->collectPosttransInfo( rpmPackage_r, runposttrans_r ); }
425
426 void RpmPostTransCollector::collectPosttransInfo( const std::vector<std::string> & runposttrans_r )
427 { _pimpl->collectPosttransInfo( runposttrans_r ); }
428
431
434
435 std::ostream & operator<<( std::ostream & str, const RpmPostTransCollector & obj )
436 { return str << *obj._pimpl; }
437
438 std::ostream & dumpOn( std::ostream & str, const RpmPostTransCollector & obj )
439 { return dumpOn( str, *obj._pimpl ); }
440
441 } // namespace target
443} // namespace zypp
void setDispose(const Dispose &dispose_r)
Set a new dispose function.
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
int close() override
Wait for the progamm to complete.
std::vector< std::string > Arguments
Writing the zypp history file.
Definition HistoryLog.h:57
void comment(const std::string &comment, bool timestamp=false)
Log a comment (even multiline).
Helper to create and pass std::istream.
Definition inputstream.h:57
Maintain [min,max] and counter (value) for progress counting.
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
static ZConfig & instance()
Singleton ctor.
Definition ZConfig.cc:925
void erase(const std::string &key_r)
Remove key from data.
Definition UserData.h:142
bool set(const std::string &key_r, AnyType val_r)
Set the value for key (nonconst version always returns true).
Definition UserData.h:119
std::string receiveLine()
Read one line from the input stream.
Wrapper class for stat/lstat.
Definition PathInfo.h:222
const char * c_str() const
String representation.
Definition Pathname.h:112
std::string basename() const
Return the last component of this path.
Definition Pathname.h:130
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition TmpPath.h:182
Provide a new empty temporary file and delete it when no longer needed.
Definition TmpPath.h:128
Pathname path() const
Definition TmpPath.cc:150
bool autoCleanup() const
Whether path is valid and deleted when the last reference drops.
Definition TmpPath.cc:167
Regular expression.
Definition Regex.h:95
Regular expression match result.
Definition Regex.h:168
RpmPostTransCollector implementation.
UserDataJobReport _myJobReport
JobReport with ContentType "cmdout/%posttrans".
void discardScripts()
Discard all remembered scrips.
Impl & operator=(const Impl &)=delete
void collectPosttransInfo(const std::vector< std::string > &runposttrans_r)
void recallFromDumpfile(const Pathname &dumpfile_r, std::function< void(std::string, std::string, std::string, std::string)> consume_r)
Retrieve "dump_posttrans: install" lines from dumpfile_r and pass n,v,r,a to the consumer_r.
rpm::RpmHeader::constPtr getHeaderIfPosttrans(const Pathname &rpmPackage_r)
Cache RpmHeader for consecutive hasPosttransScript / collectScriptForPackage calls.
boost::scoped_ptr< filesystem::TmpDir > _ptrTmpdir
Pathname tmpDir()
Lazy create tmpdir on demand.
bool collectDumpPosttransLines(const std::vector< std::string > &runposttrans_r)
Return whether runposttrans lines were collected.
void collectPosttransInfo(const Pathname &rpmPackage_r, const std::vector< std::string > &runposttrans_r)
friend std::ostream & operator<<(std::ostream &str, const Impl &obj)
std::list< std::pair< std::string, std::string > > ScriptList
<posttrans script basename, pkgname> pairs.
bool hasPosttransScript(const Pathname &rpmPackage_r)
void executeScripts(rpm::RpmDb &rpm_r)
Execute the remembered scripts.
std::ostream & dumpOn(std::ostream &str, const RpmPostTransCollector::Impl &obj)
Verbose stream output.
void collectScriptFromHeader(const rpm::RpmHeader::constPtr &pkg)
std::pair< Pathname, rpm::RpmHeader::constPtr > _headercache
std::ostream & operator<<(std::ostream &str, const RpmPostTransCollector::Impl &obj)
Stream output.
friend std::ostream & dumpOn(std::ostream &str, const Impl &obj)
void collectScriptForPackage(const Pathname &rpmPackage_r)
bool headerHasPosttrans(const rpm::RpmHeader::constPtr &pkg_r) const
Return whether RpmHeader has a posttrans.
Extract and remember posttrans scripts for later execution.
void collectPosttransInfo(const Pathname &rpmPackage_r, const std::vector< std::string > &runposttrans_r)
Extract and remember a packages posttrans script or dump_posttrans lines for later execution.
void executeScripts(rpm::RpmDb &rpm_r)
Execute the remembered scripts and/or or dump_posttrans lines.
RW_pointer< Impl > _pimpl
Implementation class.
RpmPostTransCollector(Pathname root_r)
Default ctor.
bool hasPosttransScript(const Pathname &rpmPackage_r)
Test whether a package defines a posttrans script.
void discardScripts()
Discard all remembered scripts and/or or dump_posttrans lines.
Interface to the rpm program.
Definition RpmDb.h:51
int runposttrans(const Pathname &filename_r, const std::function< void(const std::string &)> &output_r)
Run collected posttrans and transfiletrigger(postun|in) if rpm --runposttrans is supported.
Definition RpmDb.cc:2010
db_const_iterator dbConstIterator() const
Definition RpmDb.cc:251
intrusive_ptr< const RpmHeader > constPtr
Definition RpmHeader.h:65
static RpmHeader::constPtr readPackage(const Pathname &path, VERIFICATION verification=VERIFY)
Get an accessible packages data from disk.
Definition RpmHeader.cc:212
Definition Arch.h:364
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
Definition PathInfo.cc:1109
int forEachLine(std::istream &str_r, const function< bool(int, std::string)> &consume_r)
Simple lineparser: Call functor consume_r for each line.
Definition IOStream.cc:100
std::string numstring(char n, int w=0)
Definition String.h:289
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
\relates regex \ingroup ZYPP_STR_REGEX \relates regex \ingroup ZYPP_STR_REGEX
Definition Regex.h:70
std::ostream & dumpOn(std::ostream &str, const RpmPostTransCollector &obj)
std::ostream & operator<<(std::ostream &str, const CommitPackageCache &obj)
Easy-to use interface to the ZYPP dependency resolver.
std::string asString(const Patch::Category &obj)
Definition Patch.cc:122
JobReport convenience sending this instance of UserData with each message.
bool error(const std::string &msg_r)
bool info(const std::string &msg_r)
bool warning(const std::string &msg_r)
Convenient building of std::string with boost::format.
Definition String.h:253
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:212
Data regarding the dumpfile used if rpm --runposttrans is supported.
bool _runposttrans
Set to false if rpm lost –runposttrans support during transaction.
Pathname _dumpfile
The file holding the collected dump_posttrans: lines.
size_t _numscripts
Number of scripts we collected (roughly estimated)
Wrapper providing a librpmDb::db_const_iterator for this RpmDb.
Definition RpmDb.h:65
#define _(MSG)
Definition Gettext.h:39
#define DBG
Definition Logger.h:97
#define MIL
Definition Logger.h:98
#define WAR
Definition Logger.h:99
Interface to gettext.