libzypp: zypp/repo/Applydeltarpm.cc Source File

libzypp 17.38.5

Applydeltarpm.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13
14#include <zypp-core/base/Logger.h>
15#include <zypp-core/base/String.h>
16#include <zypp-core/base/Regex.h>
17#include <zypp/repo/Applydeltarpm.h>
18#include <zypp-core/ExternalProgram.h>
19#include <zypp-core/AutoDispose.h>
20#include <zypp/PathInfo.h>
21#include <zypp-core/TriBool.h>
22
23using std::endl;
24
26namespace zypp
27{
29 namespace applydeltarpm
30 {
32 namespace
33 {
34
35 const Pathname applydeltarpm_prog( "/usr/bin/applydeltarpm" );
36 const str::regex applydeltarpm_tick ( "([0-9]+) percent finished" );
37
38 /******************************************************************
39 **
40 ** FUNCTION NAME : applydeltarpm
41 ** FUNCTION TYPE : bool
42 */
43 bool applydeltarpm( const char *const argv_r[],
44 const Progress & report_r = Progress() )
45 {
46 ExternalProgram prog( argv_r, ExternalProgram::Stderr_To_Stdout );
47 str::smatch what;
48 for ( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
49 {
50 if ( report_r && str::regex_match( line, what, applydeltarpm_tick ) )
51 {
52 report_r( str::strtonum<unsigned>( what[1] ) );
53 }
54 else
55 DBG << "Applydeltarpm : " << line;
56 }
57 return( prog.close() == 0 );
58 }
59
61 } // namespace
63
64 /******************************************************************
65 **
66 ** FUNCTION NAME : haveApplydeltarpm
67 ** FUNCTION TYPE : bool
68 */
69 bool haveApplydeltarpm()
70 {
71 // To track changes in availability of applydeltarpm.
72 static TriBool _last = indeterminate;
73 PathInfo prog( applydeltarpm_prog );
74 bool have = prog.isX();
75 if ( _last == have )
76 ; // TriBool! 'else' is not '_last != have'
77 else
78 {
79 // _last is 'indeterminate' or '!have'
80 if ( (_last = have) )
81 MIL << "Found executable " << prog << endl;
82 else
83 WAR << "No executable " << prog << endl;
84 }
85 return ( bool ) _last;
86 }
87
88 /******************************************************************
89 **
90 ** FUNCTION NAME : check
91 ** FUNCTION TYPE : bool
92 */
93 bool check( const std::string & sequenceinfo_r, bool quick_r )
94 {
95 if ( ! haveApplydeltarpm() )
96 return false;
97
98 const char *const argv[] = {
99 "/usr/bin/applydeltarpm",
100 ( quick_r ? "-C" : "-c" ),
101 "-s", sequenceinfo_r.c_str(),
102 NULL
103 };
104
105 return( applydeltarpm( argv ) );
106 }
107
108 /******************************************************************
109 **
110 ** FUNCTION NAME : check
111 ** FUNCTION TYPE : bool
112 */
113 bool check( const Pathname & delta_r, bool quick_r )
114 {
115 if ( ! haveApplydeltarpm() )
116 return false;
117
118 const char *const argv[] = {
119 "/usr/bin/applydeltarpm",
120 ( quick_r ? "-C" : "-c" ),
121 delta_r.asString().c_str(),
122 NULL
123 };
124
125 return( applydeltarpm( argv ) );
126 }
127
128 /******************************************************************
129 **
130 ** FUNCTION NAME : provide
131 ** FUNCTION TYPE : bool
132 */
133 bool provide( const Pathname & delta_r, const Pathname & new_r,
134 const Progress & report_r )
135 {
136 // cleanup on error
137 AutoDispose<const Pathname> guard( new_r, filesystem::unlink );
138
139 if ( ! haveApplydeltarpm() )
140 return false;
141
142 const char *const argv[] = {
143 "/usr/bin/applydeltarpm",
144 "-p", "-p", // twice to get percent output one per line
145 delta_r.asString().c_str(),
146 new_r.asString().c_str(),
147 NULL
148 };
149
150 filesystem::assert_dir( new_r.dirname() ); // bsc#1219442: in case .rpm and .drpm are not in the same directory
151 if ( ! applydeltarpm( argv, report_r ) )
152 return false;
153
154 guard.resetDispose(); // no cleanup on success
155 return true;
156 }
157
158 /******************************************************************
159 **
160 ** FUNCTION NAME : provide
161 ** FUNCTION TYPE : bool
162 */
163 bool provide( const Pathname & old_r, const Pathname & delta_r,
164 const Pathname & new_r,
165 const Progress & report_r )
166 {
167 // cleanup on error
168 AutoDispose<const Pathname> guard( new_r, filesystem::unlink );
169
170 if ( ! haveApplydeltarpm() )
171 return false;
172
173 const char *const argv[] = {
174 "/usr/bin/applydeltarpm",
175 "-p", "-p", // twice to get percent output one per line
176 "-r", old_r.asString().c_str(),
177 delta_r.asString().c_str(),
178 new_r.asString().c_str(),
179 NULL
180 };
181
182 filesystem::assert_dir( new_r.dirname() ); // bsc#1219442: in case .rpm and .drpm are not in the same directory
183 if ( ! applydeltarpm( argv, report_r ) )
184 return false;
185
186 guard.resetDispose(); // no cleanup on success
187 return true;
188 }
189
191 } // namespace applydeltarpm
192
194} // namespace zypp
Applydeltarpm.h
AutoDispose.h
ExternalProgram.h
Logger.h
DBG
#define DBG
Definition Logger.h:99
MIL
#define MIL
Definition Logger.h:100
WAR
#define WAR
Definition Logger.h:101
Regex.h
String.h
TriBool.h
zypp::AutoDispose
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
zypp::AutoDispose::resetDispose
void resetDispose()
Set no dispose function.
Definition AutoDispose.h:171
zypp::ExternalProgram
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
Definition ExternalProgram.h:64
zypp::ExternalProgram::close
int close() override
Wait for the progamm to complete.
Definition ExternalProgram.cc:357
zypp::ExternalProgram::Stderr_To_Stdout
@ Stderr_To_Stdout
Definition ExternalProgram.h:77
zypp::externalprogram::ExternalDataSource::receiveLine
std::string receiveLine()
Read one line from the input stream.
Definition ExternalDataSource.cc:115
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::PathInfo::isX
bool isX() const
Definition PathInfo.h:310
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::dirname
Pathname dirname() const
Return all but the last component od this path.
Definition Pathname.h:133
zypp::filesystem::Pathname::asString
const std::string & asString() const
String representation.
Definition Pathname.h:94
zypp::str::regex
Regular expression.
Definition Regex.h:95
zypp::str::smatch
Regular expression match result.
Definition Regex.h:168
zypp::TriBool
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition String.h:31
zypp::applydeltarpm
Namespace wrapping invocations of /usr/bin/applydeltarpm.
Definition Applydeltarpm.cc:30
zypp::applydeltarpm::haveApplydeltarpm
bool haveApplydeltarpm()
Test whether an execuatble applydeltarpm program is available.
Definition Applydeltarpm.cc:69
zypp::applydeltarpm::Progress
function< void(unsigned int)> Progress
progress reporting
Definition Applydeltarpm.h:61
zypp::applydeltarpm::provide
bool provide(const Pathname &delta_r, const Pathname &new_r, const Progress &report_r)
Apply a binary delta to on-disk data to re-create a new rpm.
Definition Applydeltarpm.cc:133
zypp::applydeltarpm::check
bool check(const std::string &sequenceinfo_r, bool quick_r)
Check via sequence info.
Definition Applydeltarpm.cc:93
zypp::filesystem::unlink
int unlink(const Pathname &path)
Like 'unlink'.
Definition PathInfo.cc:719
zypp::filesystem::assert_dir
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition PathInfo.cc:338
zypp::str::regex_match
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
zypp::str::strtonum
TInt strtonum(const C_Str &str)
Parsing numbers from string.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
PathInfo.h