libzypp 17.31.23
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/base/Logger.h>
15#include <zypp/base/String.h>
16#include <zypp/base/Regex.h>
18#include <zypp/ExternalProgram.h>
19#include <zypp/AutoDispose.h>
20#include <zypp/PathInfo.h>
21#include <zypp/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 {
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 */
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
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 if ( ! applydeltarpm( argv, report_r ) )
151 return false;
152
153 guard.resetDispose(); // no cleanup on success
154 return true;
155 }
156
157 /******************************************************************
158 **
159 ** FUNCTION NAME : provide
160 ** FUNCTION TYPE : bool
161 */
162 bool provide( const Pathname & old_r, const Pathname & delta_r,
163 const Pathname & new_r,
164 const Progress & report_r )
165 {
166 // cleanup on error
168
169 if ( ! haveApplydeltarpm() )
170 return false;
171
172 const char *const argv[] = {
173 "/usr/bin/applydeltarpm",
174 "-p", "-p", // twice to get percent output one per line
175 "-r", old_r.asString().c_str(),
176 delta_r.asString().c_str(),
177 new_r.asString().c_str(),
178 NULL
179 };
180
181 if ( ! applydeltarpm( argv, report_r ) )
182 return false;
183
184 guard.resetDispose(); // no cleanup on success
185 return true;
186 }
187
189 } // namespace applydeltarpm
192} // namespace zypp
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:94
void resetDispose()
Set no dispose function.
Definition: AutoDispose.h:180
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
int close()
Wait for the progamm to complete.
std::string receiveLine()
Read one line from the input stream.
Wrapper class for stat/lstat.
Definition: PathInfo.h:221
const std::string & asString() const
String representation.
Definition: Pathname.h:91
Regular expression.
Definition: Regex.h:95
Regular expression match result.
Definition: Regex.h:168
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition: String.h:30
bool haveApplydeltarpm()
Test whether an execuatble applydeltarpm program is available.
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.
bool check(const std::string &sequenceinfo_r, bool quick_r)
Check via sequence info.
function< void(unsigned)> Progress
progress reporting
Definition: Applydeltarpm.h:61
int unlink(const Pathname &path)
Like 'unlink'.
Definition: PathInfo.cc:700
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
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
#define DBG
Definition: Logger.h:95
#define MIL
Definition: Logger.h:96
#define WAR
Definition: Logger.h:97