-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
RpmPkgManager.cpp
277 lines (203 loc) · 6.52 KB
/
RpmPkgManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* File name: RpmPkgManager.cpp
* Summary: RPM package manager support for QDirStat
* License: GPL V2 - See file LICENSE for details.
*
* Author: Stefan Hundhammer <Stefan.Hundhammer@gmx.de>
*/
#include <iostream> // cerr
#include <QElapsedTimer>
#include <QPointer>
#include "RpmPkgManager.h"
#include "PkgFileListCache.h"
#include "Settings.h"
#include "MessagePanel.h"
#include "PanelMessage.h"
#include "Logger.h"
#include "Exception.h"
#define LONG_CMD_TIMEOUT_SEC 30
using std::cerr;
using std::endl;
using namespace QDirStat;
RpmPkgManager::RpmPkgManager():
_getPkgListWarningSec( 7 )
{
readSettings();
if ( haveCommand( "/usr/bin/rpm" ) )
_rpmCommand = "/usr/bin/rpm";
else
_rpmCommand = "/bin/rpm"; // for old SUSE / Red Hat distros
// Notice that it is not enough to rely on a symlink /bin/rpm ->
// /usr/bin/rpm: While recent SUSE distros have that symlink (and maybe Red
// Hat and Fedora as well?), rpm as a secondary package manager on Ubuntu
// does not have such a link; they only have /usr/bin/rpm.
//
// Also intentionally never leaving _rpmCommand empty if it is not
// available to avoid unpleasant surprises if a caller tries to use any
// other method of this class that refers to it.
}
bool RpmPkgManager::isPrimaryPkgManager()
{
return tryRunCommand( QString( "%1 -qf %1" ).arg( _rpmCommand ),
QRegExp( "^rpm.*" ) );
}
bool RpmPkgManager::isAvailable()
{
return haveCommand( _rpmCommand );
}
QString RpmPkgManager::owningPkg( const QString & path )
{
int exitCode = -1;
QString output = runCommand( _rpmCommand,
QStringList() << "-qf" << "--queryformat" << "%{name}" << path,
&exitCode );
if ( exitCode != 0 || output.contains( "not owned by any package" ) )
return "";
QString pkg = output;
return pkg;
}
PkgInfoList RpmPkgManager::installedPkg()
{
int exitCode = -1;
QElapsedTimer timer;
timer.start();
QString output = runCommand( _rpmCommand,
QStringList()
<< "-qa"
<< "--queryformat"
<< "%{name} | %{version}-%{release} | %{arch}\n",
&exitCode,
LONG_CMD_TIMEOUT_SEC );
if ( timer.hasExpired( _getPkgListWarningSec * 1000 ) )
rebuildRpmDbWarning();
PkgInfoList pkgList;
if ( exitCode == 0 )
pkgList = parsePkgList( output );
return pkgList;
}
PkgInfoList RpmPkgManager::parsePkgList( const QString & output )
{
PkgInfoList pkgList;
foreach ( const QString & line, output.split( "\n" ) )
{
if ( ! line.isEmpty() )
{
QStringList fields = line.split( " | ", QString::KeepEmptyParts );
if ( fields.size() != 3 )
logError() << "Invalid rpm -qa output: " << line << "\n" << endl;
else
{
QString name = fields.takeFirst();
QString version = fields.takeFirst(); // includes release
QString arch = fields.takeFirst();
if ( arch == "(none)" )
arch = "";
PkgInfo * pkg = new PkgInfo( name, version, arch, this );
CHECK_NEW( pkg );
pkgList << pkg;
}
}
}
return pkgList;
}
QString RpmPkgManager::fileListCommand( PkgInfo * pkg )
{
return QString( "%1 -ql %2" )
.arg( _rpmCommand )
.arg( queryName( pkg ) );
}
QStringList RpmPkgManager::parseFileList( const QString & output )
{
QStringList fileList;
fileList = output.split( "\n" );
fileList.removeAll( "(contains no files)" );
return fileList;
}
QString RpmPkgManager::queryName( PkgInfo * pkg )
{
CHECK_PTR( pkg );
QString name = pkg->baseName();
if ( ! pkg->version().isEmpty() )
name += "-" + pkg->version();
if ( ! pkg->arch().isEmpty() )
name += "." + pkg->arch();
return name;
}
PkgFileListCache * RpmPkgManager::createFileListCache( PkgFileListCache::LookupType lookupType )
{
int exitCode = -1;
QString queryFormat = "[%{=NAME}-%{=VERSION}-%{=RELEASE}.%{=ARCH} | %{FILENAMES}\n]";
QString output = runCommand( _rpmCommand,
QStringList() << "-qa" << "--qf" << queryFormat,
&exitCode,
LONG_CMD_TIMEOUT_SEC );
if ( exitCode != 0 )
return 0;
QStringList lines = output.split( "\n" );
output.clear(); // Free all that text ASAP
logDebug() << lines.size() << " output lines" << endl;
PkgFileListCache * cache = new PkgFileListCache( this, lookupType );
CHECK_NEW( cache );
// Sample output:
//
// zsh-5.6-lp151.1.3.x86_64 | /bin/zsh
// zsh-5.6-lp151.1.3.x86_64 | /etc/zprofile
// zsh-5.6-lp151.1.3.x86_64 | /etc/zsh_completion.d
foreach ( const QString & line, lines )
{
if ( line.isEmpty() )
continue;
QStringList fields = line.split( " | " );
if ( fields.size() != 2 )
{
logError() << "Unexpected file list line: \"" << line << "\"" << endl;
}
else
{
QString pkgName = fields.takeFirst();
QString path = fields.takeFirst();
if ( ! pkgName.isEmpty() && ! path.isEmpty() )
cache->add( pkgName, path );
}
}
logDebug() << "file list cache finished." << endl;
return cache;
}
void RpmPkgManager::readSettings()
{
Settings settings;
settings.beginGroup( "Pkg" );
_getPkgListWarningSec = settings.value( "GetRpmPkgListWarningSec", 7 ).toInt();
// Write the value right back to the settings if it isn't there already:
// Since package manager objects are never really destroyed, this can't
// reliably be done in the destructor.
settings.setDefaultValue( "GetRpmPkgListWarningSec", _getPkgListWarningSec );
settings.endGroup();
}
void RpmPkgManager::rebuildRpmDbWarning()
{
static bool issuedWarning = false;
if ( ! issuedWarning )
{
cerr << "WARNING: rpm is very slow. Run sudo rpm --rebuilddb\n" << endl;
logWarning() << "rpm is very slow. Run sudo rpm --rebuilddb" << endl;
}
// Add a panel message so the user is sure to see this message.
//
// This is a bit out of place in this class, but a full-fledged user
// messaging system seemed to be overkill just for this one message.
// Need a guarded pointer because the [x] close button in the PanelMessage
// deletes the instance.
static QPointer<PanelMessage> panelMessage;
if ( MessagePanel::haveInstance() && ! panelMessage )
{
panelMessage = new PanelMessage();
CHECK_NEW( panelMessage );
panelMessage->setHeading( QObject::tr( "RPM is very slow." ) );
panelMessage->setText( QObject::tr( "Open a shell window and run:<br>%1" )
.arg( "<tt>sudo rpm --rebuilddb</tt>" ) );
panelMessage->setIcon( QPixmap( ":/icons/dialog-warning.png" ) );
MessagePanel::firstInstance()->add( panelMessage );
}
issuedWarning = true;
}