-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
DpkgPkgManager.cpp
241 lines (184 loc) · 5.35 KB
/
DpkgPkgManager.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
/*
* File name: DpkgPkgManager.cpp
* Summary: Dpkg package manager support for QDirStat
* License: GPL V2 - See file LICENSE for details.
*
* Author: Stefan Hundhammer <Stefan.Hundhammer@gmx.de>
*/
#include "DpkgPkgManager.h"
#include "PkgFileListCache.h"
#include "Logger.h"
#include "Exception.h"
#define LOG_COMMANDS true
#define LOG_OUTPUT false
#include "SysUtil.h"
using namespace QDirStat;
using SysUtil::runCommand;
using SysUtil::tryRunCommand;
using SysUtil::haveCommand;
bool DpkgPkgManager::isPrimaryPkgManager()
{
return tryRunCommand( "/usr/bin/dpkg -S /usr/bin/dpkg", QRegExp( "^dpkg:.*" ) );
}
bool DpkgPkgManager::isAvailable()
{
return haveCommand( "/usr/bin/dpkg" );
}
QString DpkgPkgManager::owningPkg( const QString & path )
{
int exitCode = -1;
QString output = runCommand( "/usr/bin/dpkg", QStringList() << "-S" << path, &exitCode );
if ( exitCode != 0 || output.contains( "no path found matching pattern" ) )
return "";
// Normal output: One line
// dpkg -S /usr/bin/gdb -->
//
// gdb: /usr/bin/gdb
//
//
// Pathological cases: File diversion (See man dpkg-divert).
//
// File diversion for the file that is diverted away from: 3 lines
// dpkg -S /bin/sh -->
//
// diversion by dash from: /bin/sh
// diversion by dash to: /bin/sh.distrib
// dash: /bin/sh
//
// I.e. the last line still contains the package that owns the file.
//
// File diversion for a file that was the result of a diversion: 2 lines
// dpkg -S /bin/sh.distrib -->
//
// diversion by dash from: /bin/sh
// diversion by dash to: /bin/sh.distrib
//
// I.e there is no hint WTF this file belongs to. Great job, guys.
// We are NOT going to reimplement that brain-dead diversion stuff here.
QStringList lines;
foreach ( const QString & line, output.trimmed().split( "\n" ) )
{
if ( ! line.startsWith( "diversion by" ) && ! line.isEmpty() )
lines << line;
}
QString pkg;
if ( ! lines.isEmpty() )
pkg = lines.last().split( ": " ).first();
return pkg;
}
PkgInfoList DpkgPkgManager::installedPkg()
{
int exitCode = -1;
QString output = runCommand( "/usr/bin/dpkg-query",
QStringList()
<< "--show"
<< "--showformat=${Package} | ${Version} | ${Architecture} | ${Status}\n",
&exitCode );
PkgInfoList pkgList;
if ( exitCode == 0 )
pkgList = parsePkgList( output );
return pkgList;
}
PkgInfoList DpkgPkgManager::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() != 4 )
logError() << "Invalid dpkg-query output: \"" << line << "\n" << endl;
else
{
QString name = fields.takeFirst();
QString version = fields.takeFirst();
QString arch = fields.takeFirst();
QString status = fields.takeFirst();
if ( status == "install ok installed" ||
status == "hold ok installed" )
{
PkgInfo * pkg = new PkgInfo( name, version, arch, this );
CHECK_NEW( pkg );
pkgList << pkg;
}
else
{
// logDebug() << "Ignoring " << line << endl;
}
}
}
}
return pkgList;
}
QString DpkgPkgManager::fileListCommand( PkgInfo * pkg )
{
return QString( "/usr/bin/dpkg-query --listfiles %1" ).arg( queryName( pkg ) );
}
QStringList DpkgPkgManager::parseFileList( const QString & output )
{
QStringList fileList;
QStringList lines = output.split( "\n" );
foreach ( const QString & line, lines )
{
if ( line != "/." && ! line.startsWith( "package diverts" ) )
fileList << line;
}
return fileList;
}
QString DpkgPkgManager::queryName( PkgInfo * pkg )
{
CHECK_PTR( pkg );
QString name = pkg->baseName();
#if 0
if ( pkg->isMultiVersion() )
name += "_" + pkg->version();
if ( pkg->isMultiArch() )
name += ":" + pkg->arch();
#endif
if ( pkg->arch() != "all" )
name += ":" + pkg->arch();
return name;
}
PkgFileListCache * DpkgPkgManager::createFileListCache( PkgFileListCache::LookupType lookupType )
{
int exitCode = -1;
QString output = runCommand( "/usr/bin/dpkg", QStringList() << "-S" << "*", &exitCode );
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:
//
// zip: /usr/bin/zip
// zlib1g-dev:amd64: /usr/include/zlib.h
// zlib1g:i386, zlib1g:amd64: /usr/share/doc/zlib1g
foreach ( const QString & line, lines )
{
if ( line.isEmpty() || line.startsWith( "diversion" ) )
continue;
QStringList fields = line.split( ": " );
if ( fields.size() != 2 )
{
logError() << "Unexpected file list line: \"" << line << "\"" << endl;
}
else
{
QString packages = fields.takeFirst();
QString path = fields.takeFirst();
if ( path != "/." && ! path.isEmpty() )
{
foreach ( const QString & pkgName, packages.split( ", " ) )
{
if ( ! pkgName.isEmpty() )
cache->add( pkgName, path );
}
}
}
}
logDebug() << "file list cache finished." << endl;
return cache;
}