-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rpm.cpp
668 lines (623 loc) · 23.1 KB
/
Rpm.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// SPDX-License-Identifier: AGPL-3.0-or-later
// (C) 2023 Bernhard Rosenkränzer <[email protected]>
#include "Rpm.h"
#include "DesktopFile.h"
#include "Archive.h"
#include <QFile>
#include <QCryptographicHash>
#include <QDomDocument>
#include <QHash>
#include <QImage>
#include <QBuffer>
#include <iostream>
#include <cstring>
extern "C" {
#include <archive.h>
#include <archive_entry.h>
#include <fcntl.h>
#include <arpa/inet.h>
}
rpmts Rpm::_ts = nullptr;
void Rpm::initRpm() {
rpmReadConfigFiles(NULL, NULL);
_ts = rpmtsCreate();
rpmtsSetVSFlags(_ts, _RPMVSF_NODIGESTS | _RPMVSF_NOSIGNATURES | RPMVSF_NOHDRCHK);
}
Rpm::Rpm(FileName const &filename):_filename(filename) {
if(!_ts)
initRpm();
FD_t rpmFd = Fopen(filename, "r");
int rc = rpmReadPackageFile(_ts, rpmFd, NULL, &_hdr);
if(rc == RPMRC_NOKEY || rc == RPMRC_NOTTRUSTED) {
std::cerr << filename << ": signature problem " << rc << std::endl;
} else if(rc != RPMRC_OK) {
std::cerr << "Can't open " << filename << ": " << rc << std::endl;
return;
}
// Let's get file data and the start and end of headers in the file
// while it's open anyway... repoMd needs it unconditionally
int fd=Fileno(rpmFd);
struct stat s;
fstat(fd, &s);
_fileSize = s.st_size;
_fileMtime = s.st_mtime;
lseek(fd, 104, SEEK_SET);
uint32_t sigindex, sigdata;
read(fd, &sigindex, 4);
sigindex = htonl(sigindex);
read(fd, &sigdata, 4);
sigdata = htonl(sigdata);
uint32_t sigindexsize = sigindex * 16;
uint32_t sigsize = sigdata + sigindexsize;
uint32_t disttoboundary = sigsize % 8;
if(disttoboundary)
disttoboundary = 8-disttoboundary;
_headersStart = 112 + sigsize + disttoboundary;
lseek(fd, _headersStart+8, SEEK_SET);
uint32_t hdrindex, hdrdata;
read(fd, &hdrindex, 4);
hdrindex = htonl(hdrindex);
read(fd, &hdrdata, 4);
hdrdata = htonl(hdrdata);
uint32_t hdrindexsize = hdrindex * 16;
uint32_t hdrsize = hdrdata + hdrindexsize + 16;
_headersEnd = _headersStart + hdrsize;
Fclose(rpmFd);
}
Rpm::~Rpm() {
}
static constexpr struct {
char const * const mdTag;
enum rpmTag_e const rpmTag;
} md2rpm[] = {
{ "license", RPMTAG_LICENSE },
{ "vendor", RPMTAG_VENDOR },
{ "group", RPMTAG_GROUP },
{ "buildhost", RPMTAG_BUILDHOST },
{ "sourcerpm", RPMTAG_SOURCERPM },
};
String Dependency::repoMdFlags() const {
switch(_flags&0xf) {
case 0:
return String();
case 2:
return "LT";
case 4:
return "GT";
case 8:
return "EQ";
case 8|2:
return "LE";
case 8|4:
return "GE";
}
return String();
}
String Dependency::repoMdVersion() const {
if(!_version)
return String();
String v, ret;
int colon = _version.indexOf(':');
if(colon > 0)
ret = "epoch=\"" + _version.first(colon) + "\" ";
int dash = _version.lastIndexOf('-');
ret += "ver=\"" + _version.mid(colon+1, dash-colon-1) + "\"";
if(dash > 0)
ret += " rel=\"" + _version.mid(dash+1) + "\"";
return ret;
}
String Rpm::repoMdVersion() const {
return "epoch=\"" + String::number(epoch()) + "\" ver=\"" + version() + "\" rel=\"" + release() + "\"";
}
static constexpr struct {
char const * const repoMdTag;
int const nameTag;
int const flagTag;
int const versionTag;
} depType[] = {
{ "provides", RPMTAG_PROVIDES, RPMTAG_PROVIDEFLAGS, RPMTAG_PROVIDEVERSION },
{ "requires", RPMTAG_REQUIRES, RPMTAG_REQUIREFLAGS, RPMTAG_REQUIREVERSION },
{ "conflicts", RPMTAG_CONFLICTS, RPMTAG_CONFLICTFLAGS, RPMTAG_CONFLICTVERSION },
{ "obsoletes", RPMTAG_OBSOLETES, RPMTAG_OBSOLETEFLAGS, RPMTAG_OBSOLETEVERSION },
{ "recommends", RPMTAG_RECOMMENDS, RPMTAG_RECOMMENDFLAGS, RPMTAG_RECOMMENDVERSION },
{ "suggests", RPMTAG_SUGGESTS, RPMTAG_SUGGESTFLAGS, RPMTAG_SUGGESTVERSION },
{ "supplements", RPMTAG_SUPPLEMENTS, RPMTAG_SUPPLEMENTFLAGS, RPMTAG_SUPPLEMENTVERSION },
{ "enhances", RPMTAG_ENHANCES, RPMTAG_ENHANCEFLAGS, RPMTAG_ENHANCEVERSION },
};
QList<Dependency> Rpm::dependencies(enum DepType type) const {
QList<Dependency> ret;
rpmtd deps = rpmtdNew();
rpmtd depFlags = rpmtdNew();
rpmtd depVersion = rpmtdNew();
rpmtdInit(deps);
rpmtdInit(depFlags);
rpmtdInit(depVersion);
if(headerGet(_hdr, depType[static_cast<uint8_t>(type)].nameTag, deps, HEADERGET_MINMEM|HEADERGET_EXT) &&
headerGet(_hdr, depType[static_cast<uint8_t>(type)].flagTag, depFlags, HEADERGET_MINMEM|HEADERGET_EXT) &&
headerGet(_hdr, depType[static_cast<uint8_t>(type)].versionTag, depVersion, HEADERGET_MINMEM|HEADERGET_EXT)
) {
while((rpmtdNext(deps) != -1) &&
(rpmtdNext(depFlags) != -1) &&
(rpmtdNext(depVersion) != -1)
) {
ret.append(Dependency(rpmtdGetString(deps), rpmtdGetNumber(depFlags), rpmtdGetString(depVersion)));
}
}
rpmtdFreeData(deps);
rpmtdFreeData(depFlags);
rpmtdFreeData(depVersion);
rpmtdFree(deps);
rpmtdFree(depFlags);
rpmtdFree(depVersion);
return ret;
}
String Dependency::repoMd() const {
String ret = "<rpm:entry name=\"" + name().xmlEncode() + "\"";
String s = repoMdFlags();
if(s)
ret += " flags=\"" + s + "\"";
s = repoMdVersion();
if(s)
ret += " " + s;
ret += "/>";
return ret;
}
String Rpm::dependenciesMd(enum DepType type) const {
QList<Dependency> deps = dependencies(type);
if(!deps.size())
return String();
String ret = String(" <rpm:") + depType[static_cast<uint8_t>(type)].repoMdTag + ">\n";
for(Dependency const &d : dependencies(type))
ret += " " + d.repoMd() + "\n";
ret += String(" </rpm:") + depType[static_cast<uint8_t>(type)].repoMdTag + ">\n";
return ret;
}
String Rpm::dependenciesMd() const {
return dependenciesMd(DepType::Provides) +
dependenciesMd(DepType::Requires) +
dependenciesMd(DepType::Conflicts) +
dependenciesMd(DepType::Obsoletes) +
dependenciesMd(DepType::Suggests) +
dependenciesMd(DepType::Recommends) +
dependenciesMd(DepType::Supplements) +
dependenciesMd(DepType::Enhances);
}
String Rpm::sha256() {
if(_sha256.isEmpty()) {
QFile rpm;
int fd = open(_filename, O_RDONLY);
lseek(fd, 0, SEEK_SET);
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED);
rpm.open(fd, QFile::ReadOnly, QFile::AutoCloseHandle);
QCryptographicHash hash(QCryptographicHash::Sha256);
hash.addData(&rpm);
rpm.close();
_sha256=hash.result().toHex();
}
return _sha256;
}
String Rpm::appstreamMd(QHash<String,QByteArray> *icons) const {
if(icons)
icons->clear();
String ret;
QList<String> appstreamFiles;
QList<String> desktopFiles;
QList<String> iconFiles;
for(FileInfo const &fi : fileList(false)) {
if(fi.name().startsWith("/usr/share/metainfo/") || fi.name().startsWith("/usr/share/appdata/"))
appstreamFiles.append(fi.name());
else if(fi.name().startsWith("/usr/share/applications/"))
desktopFiles.append(fi.name());
else if(fi.name().startsWith("/usr/share/icons/") || fi.name().startsWith("/usr/share/pixmaps"))
iconFiles.append(fi.name());
}
QHash<String,QByteArray> appstreams = extractFiles(appstreamFiles + desktopFiles);
if(appstreamFiles.count()) {
for(auto it = appstreams.cbegin(), end = appstreams.cend(); it != end; ++it) {
// We don't need to try to build metadata from desktop
// files if we have appstream files (but we need to read
// them anyway to supplement the appstream files)
if(it.key().startsWith("/usr/share/applications/"))
continue;
// Here, we actually have to use a real XML parser instead of the simplistic
// assumptions we use elsewhere in the code: since we don't control the
// input files, they may not be indented reasonably and they may not even
// be valid.
QDomDocument dom;
// We use trimmed() here because Qt's XML parser is strict about
// things like <?xml ... having to be at the beginning of the file,
// without a linebreak or anything in front of it.
// A few metadata files (e.g. flightgear) have a leading newline.
dom.setContent(it.value().trimmed());
QDomElement root = dom.documentElement();
if(root.tagName() == "application") {
// Seems to be an old version of the standard, spotted in
// brasero-3.12.3, Clementine-1.4.0-rc2, empathy-3.12.14
root.setTagName("component");
root.setAttribute("type", "desktop-application");
}
if(root.tagName() != "component") {
std::cerr << "Appstream metadata with document element \"" << root.tagName() << "\" rather than \"component\" found: " << it.key() << " in " << _filename << std::endl;
continue;
}
// This is not strictly correct according to the standard, but a forgotten
// type="desktop" seems to be far more common than a legitimately untyped
// metainfo file.
if(!root.hasAttribute("type"))
root.setAttribute("type", "desktop-application");
// This is extremely common (in fact, more so than desktop-application),
// but seems to be wrong according to the spec
if(root.attribute("type") == "desktop")
root.setAttribute("type", "desktop-application");
QDomElement id = root.firstChildElement("id");
if(id.isNull()) {
// No id -- so let's create one from the filename instead
id = dom.createElement("id");
String fakeId = FileName(it.key()).basename(".metainfo.xml");
// Since there is no consensus about *.metainfo.xml vs. *.appdata.xml,
// strip that off too
if(fakeId.endsWith(".appdata.xml"))
fakeId = fakeId.sliced(0, fakeId.length()-12);
id.appendChild(dom.createTextNode(fakeId));
if(root.firstChild().isNull())
root.appendChild(id);
else
root.insertBefore(id, root.firstChild());
}
if(root.firstChildElement("source_pkgname").isNull()) {
QDomElement pkgname = dom.createElement("source_pkgname");
String srpmName=sourceRpm();
// strip off -VERSION-RELEASE.src.rpm
if(srpmName.contains('-'))
srpmName=srpmName.sliced(0, srpmName.lastIndexOf('-'));
if(srpmName.contains('-'))
srpmName=srpmName.sliced(0, srpmName.lastIndexOf('-'));
pkgname.appendChild(dom.createTextNode(srpmName));
root.insertAfter(pkgname, id);
}
if(root.firstChildElement("pkgname").isNull()) {
QDomElement pkgname = dom.createElement("pkgname");
pkgname.appendChild(dom.createTextNode(name()));
root.insertAfter(pkgname, id);
}
// spec says update_contact must not be exposed to the end user
while(!root.firstChildElement("update_contact").isNull())
root.removeChild(root.firstChildElement("update_contact"));
// updatecontent is wrong, but relatively common especially in
// GNOME stuff. Let's remote it too.
while(!root.firstChildElement("updatecontact").isNull())
root.removeChild(root.firstChildElement("updatecontact"));
// If we have a matching desktop file, we can supplement the
// metainfo with it metainfo files frequently "forget" the
// icon as well as categories.
String desktopFile;
QDomElement launchable = root.firstChildElement("launchable");
while(!launchable.isNull()) {
if(launchable.attribute("type") == "desktop-id") {
String d = "/usr/share/applications/" + launchable.text();
if(desktopFiles.contains(d)) {
desktopFile = d;
break;
} else if(desktopFiles.contains(d + ".desktop")) {
// Just to make sure. There's no known cases
// of this, but it seems easy to "forget" to
// append .desktop to the ID...
desktopFile = d + ".desktop";
break;
}
}
launchable = launchable.nextSiblingElement("launchable");
}
// The desktop file *should* be referenced with a
// <launchable type="desktop-id"> tag, but frequently isn't,
// so we'll also look for a desktop file matching the ID.
if(!desktopFile) {
String d = "/usr/share/applications/" + id.text() + ".desktop";
if(desktopFiles.contains(d))
desktopFile = d;
if(!desktopFile) {
// A few bogus appdata files (e.g. konsole and falkon)
// already list ".desktop" as part of their id
// (a desktop to appdata converter gone wrong?)
d = "/usr/share/applications/" + id.text();
if(desktopFiles.contains(d))
desktopFile = d;
// Lastly, let's try just the name
if(!desktopFile) {
d = "/usr/share/applications/" + name() + ".desktop";
if(desktopFiles.contains(d))
desktopFile = d;
}
}
}
// If we still haven't found a desktop file, but there's
// only one in the package, there's a good chance it's what
// we want...
if(!desktopFile && desktopFiles.count() == 1)
desktopFile = desktopFiles.at(0);
String fancyName = name();
if(desktopFile) {
// We found a matching desktop file -- so let's make
// sure it's listed as launchable too...
launchable = root.firstChildElement("launchable");
if(launchable.isNull()) {
launchable = dom.createElement("launchable");
launchable.setAttribute("type", "desktop-id");
launchable.appendChild(dom.createTextNode(FileName(desktopFile).basename()));
root.appendChild(launchable);
}
DesktopFile df(appstreams[desktopFile]);
QDomElement icon = root.firstChildElement("icon");
if(icon.isNull() && df.hasKey("Icon")) {
String iconName = df.value("Icon");
#ifdef DO_WHAT_IS_SANE_AND_NOT_WHAT_APPSTREAM_DOES
icon = dom.createElement("icon");
icon.setAttribute("type", "stock");
icon.appendChild(dom.createTextNode(iconName));
root.appendChild(icon);
#endif
if(icons) {
QList<String> relevantIcons;
for(String const &i : iconFiles) {
if(i.startsWith("/usr/share/icons/") && (i.endsWith("/64x64/apps/" + iconName + ".png") || i.endsWith("/128x128/apps/" + iconName + ".png")))
relevantIcons.append(i);
}
if(!relevantIcons.count()) {
// the spec says png icons are preferred, but vector is
// allowed, so if we can't find the PNGs, fall back
// to SVGs
for(String const &i : iconFiles) {
if(i.startsWith("/usr/share/icons/") && (i.endsWith("/scalable/apps/" + iconName + ".svg") || i.endsWith("/scalable/apps/" + iconName + ".svgz")))
relevantIcons.append(i);
}
}
if(relevantIcons.count()) {
QHash<String,QByteArray> iconData = extractFiles(relevantIcons);
for(auto i = iconData.cbegin(), e = iconData.cend(); i != e; ++i) {
QList<QByteArray> n=i.key().split('/');
String size=n.at(n.length()-3);
String name;
#ifndef DO_WHAT_IS_SANE_AND_NOT_WHAT_APPSTREAM_DOES
if(size == "scalable") {
size = "64x64";
name = size + "/" + iconName + ".png";
QImage original;
original.loadFromData(i.value());
QBuffer converted;
original.save(&converted, "PNG");
icons->insert(name, converted.data());
} else {
#endif
name = size + "/" + iconName + "." + n.at(n.length()-1).split('.').last();
icons->insert(name, i.value());
#ifndef DO_WHAT_IS_SANE_AND_NOT_WHAT_APPSTREAM_DOES
}
#endif
String simpleSize=size.split('x').at(0);
icon = dom.createElement("icon");
icon.setAttribute("type", "cached");
icon.setAttribute("width", QString(simpleSize));
icon.setAttribute("height", QString(simpleSize));
icon.appendChild(dom.createTextNode(name.split('/').last()));
root.appendChild(icon);
}
}
}
}
if (df.hasKey("Name"))
fancyName = df.value("Name");
QDomElement categories = root.firstChildElement("categories");
if(categories.isNull() && df.hasKey("Categories")) {
categories = dom.createElement("categories");
for(QByteArray const &s : df.value("Categories").split(';')) {
if(s.isEmpty())
continue;
QDomElement category = dom.createElement("category");
category.appendChild(dom.createTextNode(s));
categories.appendChild(category);
}
root.appendChild(categories);
}
} else
fancyName = name();
// Some badly done appdata files miss name and summary
if(root.firstChildElement("name").isNull()) {
QDomElement appname = dom.createElement("name");
appname.appendChild(dom.createTextNode(fancyName));
root.insertAfter(appname, id);
}
if(root.firstChildElement("summary").isNull()) {
QDomElement appsummary = dom.createElement("summary");
appsummary.appendChild(dom.createTextNode(summary()));
root.insertAfter(appsummary, id);
}
String md(dom.toByteArray());
// Strip XML header, repeating <?xml version ..... is harmful
while(!md.startsWith("<component") && md.contains('\n'))
md=md.sliced(md.indexOf('\n')+1);
ret += md.trimmed() + "\n";
}
} else if(desktopFiles.count()) {
// No appstream files, but we can get much of the same content from desktop files...
QHash<String,QByteArray> desktops = extractFiles(desktopFiles);
for(auto i=desktops.cbegin(), end=desktops.cend(); i!=end; ++i) {
String md;
String desktopName = FileName(i.key()).basename(".desktop");
String id = desktopName;
// IDs can't contain special characters, but we must
// leave desktopName unmodified...
id.replace(' ', '_').replace('-','_');
md += "<component type=\"desktop\">\n"
" <id>" + id + "</id>\n"
" <pkgname>" + name() + "</pkgname>\n";
String srpmName=sourceRpm();
// strip off -VERSION-RELEASE.src.rpm
if(srpmName.contains('-'))
srpmName=srpmName.sliced(0, srpmName.lastIndexOf('-'));
if(srpmName.contains('-'))
srpmName=srpmName.sliced(0, srpmName.lastIndexOf('-'));
md += " <source_pkgname>" + srpmName + "</source_pkgname>\n"
" <launchable type=\"desktop-id\">" + desktopName + ".desktop</launchable>\n"
" <description><p>" + description().xmlEncode() + "</p></description>\n";
DesktopFile df(i.value());
QHash<String, String> entries = df["Desktop Entry"];
for(auto dfe=entries.cbegin(), dfend=entries.cend(); dfe != dfend; ++dfe) {
if(dfe.key() == "Icon") {
String iconName = dfe.value();
#ifdef DO_WHAT_IS_SANE_AND_NOT_WHAT_APPSTREAM_DOES
md += " <icon type=\"stock\">" + iconName + "</icon>\n";
#endif
if(icons) {
QList<String> relevantIcons;
for(String const &i : iconFiles) {
if(i.startsWith("/usr/share/icons/") && (i.endsWith("/64x64/apps/" + iconName + ".png") || i.endsWith("/128x128/apps/" + iconName + ".png")))
relevantIcons.append(i);
}
if(!relevantIcons.count()) {
// the spec says png icons are preferred, but vector is
// allowed, so if we can't find the PNGs, fall back
// to SVGs
for(String const &i : iconFiles) {
if(i.startsWith("/usr/share/icons/") && (i.endsWith("/scalable/apps/" + iconName + ".svg") || i.endsWith("/scalable/apps/" + iconName + ".svgz")))
relevantIcons.append(i);
}
}
if(relevantIcons.count()) {
QHash<String,QByteArray> iconData = extractFiles(relevantIcons);
for(auto i = iconData.cbegin(), e = iconData.cend(); i != e; ++i) {
QList<QByteArray> n=i.key().split('/');
String size=n.at(n.length()-3);
String name;
#ifndef DO_WHAT_IS_SANE_AND_NOT_WHAT_APPSTREAM_DOES
if(size == "scalable") {
size = "64x64";
name = size + "/" + iconName + ".png";
QImage original;
original.loadFromData(i.value());
QBuffer converted;
original.save(&converted, "PNG");
icons->insert(name, converted.data());
} else {
#endif
name = size + "/" + iconName + "." + n.at(n.length()-1).split('.').last();
icons->insert(name, i.value());
#ifndef DO_WHAT_IS_SANE_AND_NOT_WHAT_APPSTREAM_DOES
}
#endif
if(size == "scalable") {
md += " <icon type=\"cached\" width=\"64\" height=\"64\">" + name.split('/').last() + "</icon>\n";
} else {
String simpleSize=size.split('x').at(0);
md += " <icon type=\"cached\" width=\"" + simpleSize + "\" height=\"" + simpleSize + "\">" + name.split('/').last() + "</icon>\n";
}
}
}
}
} else if(dfe.key() == "Name") {
md += " <name>" + dfe.value().xmlEncode() + "</name>\n";
} else if(dfe.key() == "GenericName") {
md += " <summary>" + dfe.value().xmlEncode() + "</summary>\n";
} else if(dfe.key() == "Categories") {
md += " <categories>\n";
for(QByteArray c : dfe.value().split(';')) {
if(c.length())
md += " <category>" + c + "</category>\n";
}
md += " </categories>\n";
}
}
md += "</component>\n";
ret += md;
}
}
return ret;
}
QHash<String,QByteArray> Rpm::extractFiles(QList<String> const &filenames) const {
QHash<String,QByteArray> ret;
archive *a = archive_read_new();
archive_read_support_filter_all(a);
archive_read_support_format_all(a);
int r = archive_read_open_filename(a, _filename, 16384);
if(r != ARCHIVE_OK) {
return ret;
}
ret.reserve(filenames.count());
archive_entry *e;
while(archive_read_next_header(a, &e) == ARCHIVE_OK) {
char const * fn = archive_entry_pathname(e);
if(*fn == '.') // rpm seems to store filenames with a leading dot
fn++;
if(filenames.contains(fn)) {
size_t size=archive_entry_size(e);
char buf[size];
int r = archive_read_data(a, &buf, size);
ret.insert(fn, QByteArray(buf, size));
if(ret.count() == filenames.count()) {
// No need to keep reading the archive...
break;
}
} else
archive_read_data_skip(a);
}
archive_read_free(a);
return ret;
}
Files Rpm::fileList(bool onlyPrimary) const {
// Also potentially of interest:
// RPMTAG_DIRINDEXES seems to hold a number associated with the directory the file is in
// RPMTAG_BASENAMES holds the basename of every file
// RPMTAG_FILEDIGESTS holds the SHA256 checksum of every file (in string format; empty for symlinks and directories)
Files fn;
// Filenames
rpmtd filenames = rpmtdNew();
// RPMTAG_FILEFLAGS attributes -- see enum rpmfileAttrs_e in <rpm/rpmfiles.h>
// probably most important: RPMFILE_GHOST, RPMFILE_CONFIG, RPMFILE_MISSINGOK,
// RPMFILE_NOREPLACE, RPMFILE_DOC, RPMFILE_LICENSE, RPMFILE_PUBKEY
rpmtd fileflags = rpmtdNew();
// File modes, same as st_mode in struct stat
rpmtd filemodes = rpmtdNew();
rpmtdInit(filenames);
rpmtdInit(fileflags);
rpmtdInit(filemodes);
constexpr headerGetFlags flags = HEADERGET_MINMEM|HEADERGET_EXT;
if(headerGet(_hdr, RPMTAG_FILENAMES, filenames, flags) &&
headerGet(_hdr, RPMTAG_FILEFLAGS, fileflags, flags) &&
headerGet(_hdr, RPMTAG_FILEMODES, filemodes, flags)
) {
while((rpmtdNext(filenames) != -1) &&
(rpmtdNext(fileflags) != -1) &&
(rpmtdNext(filemodes) != -1)
) {
FileInfo fi(rpmtdGetString(filenames), static_cast<rpmfileAttrs_e>(rpmtdGetNumber(fileflags)), rpmtdGetNumber(filemodes));
// The definition of what is "primary" and what isn't is very vague.
// According to https://createrepo.baseurl.org/:
// "CERTAIN files - specifically files matching: /etc*,
// *bin/*, /usr/lib/sendmail"
// So we'll take anything in /etc and anything that's
// executable and not a shared library (seems to make more
// sense than *bin/*, given there's such things as /opt)
if(!onlyPrimary ||
((S_ISREG(fi.mode()) && (fi.mode() & 0111) && !fi.name().contains(".so")) ||
fi.name().startsWith("/etc/"))
)
fn.append(fi);
}
}
rpmtdFreeData(filenames);
rpmtdFree(filenames);
return fn;
}
String Rpm::fileListMd(bool onlyPrimary) const {
String ret;
String indent = onlyPrimary ? " " : " ";
for(FileInfo const &f : fileList(onlyPrimary)) {
ret += indent + "<file";
if(S_ISDIR(f.mode()))
ret += " type=\"dir\"";
else if(f.attributes() & RPMFILE_GHOST)
ret += " type=\"ghost\"";
ret += ">" + f.name().xmlEncode() + "</file>\n";
}
return ret;
}