-
Notifications
You must be signed in to change notification settings - Fork 0
/
createmd-perfile.cpp
464 lines (422 loc) · 17.9 KB
/
createmd-perfile.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
// SPDX-License-Identifier: AGPL-3.0-or-later
// (C) 2023 Bernhard Rosenkränzer <[email protected]>
#include "Rpm.h"
#include "Sha256.h"
#include "Compression.h"
#include "Archive.h"
#include <QGuiApplication>
#include <QCommandLineParser>
#include <QFile>
#include <QDir>
#include <QDomDocument>
#include <QTextStream>
#include <iostream>
extern "C" {
#include <time.h>
#include <archive_entry.h>
}
static bool verbose;
/**
* Extract metadata from a package
* @param d Directory containing the package
* @param rpm rpm filename
*/
static bool extractMetadata(QDir &d, QString const &rpm) {
QDir rd(d.absolutePath() + "/repodata/perfile");
if(!rd.exists()) {
d.mkdir("repodata", QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther);
d.mkdir("repodata/perfile", QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther);
if(!rd.exists()) {
std::cerr << "Can't create/use repodata directory in " << qPrintable(rd.absolutePath()) << ", ignoring" << std::endl;
return false;
}
}
QFile primary(rd.filePath(rpm + ".primary.xml"));
if(!primary.open(QFile::WriteOnly|QFile::Truncate)) {
std::cerr << "Can't write to " << rd.filePath(rpm + ".primary.xml") << std::endl;
return false;
}
QFile filelists(rd.filePath(rpm + ".filelists.xml"));
if(!filelists.open(QFile::WriteOnly|QFile::Truncate)) {
std::cerr << "Can't write to " << rd.filePath(rpm + ".filelists.xml") << std::endl;
return false;
}
QFile other(rd.filePath(rpm + ".other.xml"));
if(!other.open(QFile::WriteOnly|QFile::Truncate)) {
std::cerr << "Can't write to " << rd.filePath(rpm + ".other.xml") << std::endl;
return false;
}
Rpm r(d.filePath(rpm));
QTextStream primaryTs(&primary);
primaryTs << "<package type=\"rpm\">" << Qt::endl
<< " <name>" << r.name() << "</name>" << Qt::endl
<< " <arch>" << r.arch() << "</arch>" << Qt::endl
<< " <version epoch=\"" << r.epoch() << "\" ver=\"" << r.version() << "\" rel=\"" << r.release() << "\"/>" << Qt::endl
<< " <checksum type=\"sha256\" pkgid=\"YES\">" << r.sha256() << "</checksum>" << Qt::endl
<< " <summary>" << r.summary().xmlEncode() << "</summary>" << Qt::endl
<< " <description>" << r.description().xmlEncode() << "</description>" << Qt::endl
<< " <packager>" << r.packager().xmlEncode() << "</packager>" << Qt::endl
<< " <url>" << r.url().xmlEncode() << "</url>" << Qt::endl
<< " <time file=\"" << r.time() << "\" build=\"" << r.buildTime() << "\"/>" << Qt::endl
<< " <size package=\"" << r.size() << "\" installed=\"" << r.installedSize() << "\" archive=\"" << r.archiveSize() << "\"/>" << Qt::endl
<< " <location href=\"" << rpm << "\"/>" << Qt::endl
<< " <format>" << Qt::endl
<< " <rpm:license>" << r.license().xmlEncode() << "</rpm:license>" << Qt::endl
<< " <rpm:vendor>" << r.vendor().xmlEncode() << "</rpm:vendor>" << Qt::endl
<< " <rpm:group>" << r.group().xmlEncode() << "</rpm:group>" << Qt::endl
<< " <rpm:buildhost>" << r.buildHost() << "</rpm:buildhost>" << Qt::endl
<< " <rpm:sourcerpm>" << r.sourceRpm() << "</rpm:sourcerpm>" << Qt::endl
<< " <rpm:header-range start=\"" << r.headersStart() << "\" end=\"" << r.headersEnd() << "\"/>" << Qt::endl
<< r.dependenciesMd()
<< r.fileListMd(true)
<< " </format>" << Qt::endl
<< "</package>" << Qt::endl;
QTextStream filelistsTs(&filelists);
filelistsTs << "<package pkgid=\"" << r.sha256() << "\" name=\"" << r.name() << "\" arch=\"" << r.arch() << "\">" << Qt::endl
<< " <version " << r.repoMdVersion() << "/>" << Qt::endl
<< r.fileListMd()
<< "</package>" << Qt::endl;
QTextStream otherTs(&other);
otherTs << "<package pkgid=\"" << r.sha256() << "\" name=\"" << r.name() << "\" arch=\"" << r.arch() << "\">" << Qt::endl
<< " <version " << r.repoMdVersion() << "/>" << Qt::endl
<< "</package>" << Qt::endl;
QHash<String,QByteArray> icons;
String appstreamMd = r.appstreamMd(&icons);
if(!appstreamMd.isEmpty()) {
QFile appstream(rd.filePath(rpm + ".appstream.xml"));
if(!appstream.open(QFile::WriteOnly|QFile::Truncate)) {
std::cerr << "Can't write to " << rd.filePath(rpm + ".appstream.xml") << std::endl;
return false;
}
appstream.write(appstreamMd);
appstream.close();
QDir appstreamIcons(rd.filePath(rpm + ".appstream-icons"));
if(appstreamIcons.exists())
appstreamIcons.removeRecursively();
for(auto icon=icons.cbegin(), iend=icons.cend(); icon != iend; ++icon) {
FileName fn=rpm + ".appstream-icons/" + icon.key();
rd.mkpath(fn.dirname());
QFile iconFile(rd.filePath(fn));
if(!iconFile.open(QFile::WriteOnly|QFile::Truncate)) {
std::cerr << "Can't write to " << iconFile.fileName() << std::endl;
continue;
}
iconFile.write(icon.value());
}
}
primary.close();
filelists.close();
other.close();
return true;
}
static bool createMetadata(String const &path) {
QDir d(path);
if(!d.exists()) {
std::cerr << path << " not found, ignoring" << std::endl;
return false;
}
QStringList rpms = d.entryList(QStringList() << "*.rpm", QDir::Files|QDir::Readable);
if(rpms.isEmpty()) {
std::cerr << "No rpms found in " << qPrintable(path) << ", ignoring" << std::endl;
return false;
}
for(QString const &rpm : rpms) {
extractMetadata(d, rpm);
}
return true;
}
static bool cleanup(QDir &d) {
QStringList const rpms = d.entryList(QStringList() << "*.rpm", QDir::Files|QDir::Readable);
QDir rd(d.absolutePath() + "/repodata/perfile");
QStringList const mdFiles = rd.entryList();
for(QString const &file : mdFiles) {
if(!file.contains(".rpm.")) {
if(file != "." && file != "..")
std::cerr << "Non-metadata file in metadata directory: " << qPrintable(file) << std::endl;
continue;
}
QString rpm = file.left(file.lastIndexOf(".rpm.")+4);
if(!rpms.contains(rpm)) {
if(verbose)
std::cerr << "Stale metadata for: " << qPrintable(rpm) << std::endl;
if(file.endsWith(".appstream-icons")) {
QDir icon(rd.filePath(file));
icon.removeRecursively();
} else {
QFile::remove(rd.filePath(file));
}
}
}
return true;
}
static QStringList newFiles(QDir &d) {
QStringList ret;
QStringList const rpms = d.entryList(QStringList() << "*.rpm", QDir::Files|QDir::Readable);
QDir rd(d.absolutePath() + "/repodata/perfile");
QStringList const mdFiles = rd.entryList();
for(QString const &rpm : rpms) {
if(!mdFiles.contains(rpm + ".primary.xml")) {
if(verbose)
std::cerr << "New file: " << qPrintable(rpm) << std::endl;
ret << rpm;
}
}
return ret;
}
static QStringList modifiedFiles(QDir &d) {
QStringList ret;
QFileInfoList const rpms = d.entryInfoList(QStringList() << "*.rpm", QDir::Files|QDir::Readable);
for(QFileInfo const &rpm : rpms) {
QString md=d.absolutePath() + "/repodata/perfile/" + rpm.fileName() + ".primary.xml";
QFileInfo mdInfo(md);
if(!mdInfo.exists()) {
std::cerr << "No metadata found for " << qPrintable(rpm.fileName()) << std::endl;
continue;
}
if(mdInfo.lastModified() < rpm.lastModified()) {
if(verbose)
std::cerr << "Modified file: " << qPrintable(rpm.fileName()) << std::endl;
ret << rpm.fileName();
}
}
return ret;
}
static QStringList recursiveEntryList(QDir const &d, QString const &prefix=QString()) {
QStringList ret;
QStringList files = d.entryList(QDir::Files);
if(prefix.isEmpty())
ret = files;
else {
for(QString const &f : files)
ret << (prefix + "/" + f);
}
QStringList subdirs = d.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
if(!subdirs.isEmpty()) {
for(QString const &sd : subdirs) {
ret << recursiveEntryList(d.absoluteFilePath(sd), prefix + (prefix.isEmpty() ? "" : "/") + sd);
}
}
return ret;
}
/**
* Finalize the metadata
*
* This compresses the metadata files, renames them to
* their final names (checksum included in filename),
* and creates the corresponding repomd.xml file.
*
* @param d directory containing the metadata
* @return \c true on success
*/
// TODO add some error checking
static bool finalizeMetadata(QDir const &d) {
QStringList oldMetadata = d.entryList(QStringList() << "*.?z", QDir::Files);
Compression::CompressFile(d.absoluteFilePath("primary.xml"));
Compression::CompressFile(d.absoluteFilePath("filelists.xml"));
Compression::CompressFile(d.absoluteFilePath("other.xml"));
Compression::CompressFile(d.absoluteFilePath("appstream.xml"), Compression::Format::GZip);
Compression::CompressFile(d.absoluteFilePath("appstream-icons.tar"), Compression::Format::GZip);
QHash<String,String> checksum{
{"primary", Sha256::checksum(d.absoluteFilePath("primary.xml"))},
{"filelists", Sha256::checksum(d.absoluteFilePath("filelists.xml"))},
{"other", Sha256::checksum(d.absoluteFilePath("other.xml"))},
{"appstream", Sha256::checksum(d.absoluteFilePath("appstream.xml"))},
{"appstream-icons", Sha256::checksum(d.absoluteFilePath("appstream-icons.tar"))},
{"primaryXZ", Sha256::checksum(d.absoluteFilePath("primary.xml.xz"))},
{"filelistsXZ", Sha256::checksum(d.absoluteFilePath("filelists.xml.xz"))},
{"otherXZ", Sha256::checksum(d.absoluteFilePath("other.xml.xz"))},
{"appstreamGZ", Sha256::checksum(d.absoluteFilePath("appstream.xml.gz"))},
{"appstream-iconsGZ", Sha256::checksum(d.absoluteFilePath("appstream-icons.tar.gz"))}
};
std::cerr << "mv " << qPrintable(d.absoluteFilePath("primary.xml.xz")) << " " << qPrintable(d.absoluteFilePath(checksum["primaryXZ"] + "-primary.xml.xz")) << std::endl;
QFile::rename(d.absoluteFilePath("primary.xml.xz"), d.absoluteFilePath(checksum["primaryXZ"] + "-primary.xml.xz"));
QFile::rename(d.absoluteFilePath("filelists.xml.xz"), d.absoluteFilePath(checksum["filelistsXZ"] + "-filelists.xml.xz"));
QFile::rename(d.absoluteFilePath("other.xml.xz"), d.absoluteFilePath(checksum["otherXZ"] + "-other.xml.xz"));
QFile::rename(d.absoluteFilePath("appstream.xml.gz"), d.absoluteFilePath(checksum["appstreamGZ"] + "-appstream.xml.gz"));
QFile::rename(d.absoluteFilePath("appstream-icons.tar.gz"), d.absoluteFilePath(checksum["appstream-iconsGZ"] + "-appstream-icons.tar.gz"));
QFile repomd(d.absoluteFilePath("repomd.xml"));
repomd.open(QFile::WriteOnly|QFile::Truncate);
QTextStream repomdTs(&repomd);
time_t timestamp = time(0);
repomdTs << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << Qt::endl
<< "<repomd xmlns=\"http://linux.duke.edu/metadata/repo\" xmlns:rpm=\"http://linux.duke.edu/metadata/rpm\">" << Qt::endl
<< " <revision>" << timestamp << "</revision>" << Qt::endl;
for(String const &file : QList<String>{"primary", "filelists", "other", "appstream", "appstream-icons"}) {
String compressedFile = (file.startsWith("appstream")) ? file + "GZ" : file + "XZ";
String compressExtension = (file.startsWith("appstream")) ? ".gz" : ".xz";
String extension = (file == "appstream-icons") ? ".tar" : ".xml";
struct stat s, uncompressed;
stat(d.absoluteFilePath(checksum[compressedFile] + "-" + file + extension + compressExtension).toUtf8(), &s);
stat(d.absoluteFilePath(file + ".xml").toUtf8(), &uncompressed);
repomdTs << " <data type=\"" << file << "\">" << Qt::endl
<< " <checksum type=\"sha256\">" << checksum[compressedFile] << "</checksum>" << Qt::endl
<< " <open-checksum type=\"sha256\">" << checksum[file] << "</open-checksum>" << Qt::endl
<< " <location href=\"repodata/" << checksum[compressedFile] << "-" << file << extension + compressExtension + "\"/>" << Qt::endl
<< " <timestamp>" << s.st_mtime << "</timestamp>" << Qt::endl
<< " <size>" << s.st_size << "</size>" << Qt::endl
<< " <open-size>" << uncompressed.st_size << "</open-size>" << Qt::endl
<< " </data>" << Qt::endl;
QFile::remove(d.absoluteFilePath(file + extension));
}
repomdTs << "</repomd>" << Qt::endl;
repomd.close();
for(QString const &file : oldMetadata) {
QFile::remove(d.absoluteFilePath(file));
}
return true;
}
static bool mergeMetadata(QDir &d, String const &origin="openmandriva") {
QDir rd(d.absolutePath() + "/repodata");
QDir pf(d.absolutePath() + "/repodata/perfile");
if(!pf.exists()) {
// The directory doesn't contain any rpms or metadata. The right thing to do
// would be to throw an error, but createrepo_c just creates empty but valid
// XML files (containing the header with 0 packages), and dnf expects this
// behavior, so we have to copy it even if it's stupid.
d.mkdir("repodata", QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther);
d.mkdir("repodata/perfile", QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner|QFile::ReadGroup|QFile::ExeGroup|QFile::ReadOther|QFile::ExeOther);
}
QFile primary(rd.absoluteFilePath("primary.xml"));
if(!primary.open(QFile::WriteOnly|QFile::Truncate)) {
std::cerr << "Can't open " << qPrintable(primary.fileName()) << std::endl;
return false;
} else {
QStringList const primaryFiles = pf.entryList(QStringList() << "*.primary.xml", QDir::Files|QDir::Readable, QDir::Name);
primary.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<metadata xmlns=\"http://linux.duke.edu/metadata/common\" xmlns:rpm=\"http://linux.duke.edu/metadata/rpm\" packages=\"");
primary.write(QString::number(primaryFiles.count()).toLocal8Bit() );
primary.write("\">\n");
for(QString const &p : primaryFiles) {
QFile f(pf.absoluteFilePath(p));
if(!f.open(QFile::ReadOnly)) {
std::cerr << "Can't open " << qPrintable(p) << std::endl;
continue;
}
primary.write(f.readAll());
f.close();
}
primary.write("</metadata>");
primary.close();
}
QFile filelists(rd.absoluteFilePath("filelists.xml"));
if(!filelists.open(QFile::WriteOnly|QFile::Truncate)) {
std::cerr << "Can't open " << qPrintable(filelists.fileName()) << std::endl;
return false;
} else {
QStringList const filelistsFiles = pf.entryList(QStringList() << "*.filelists.xml", QDir::Files|QDir::Readable, QDir::Name);
filelists.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<filelists xmlns=\"http://linux.duke.edu/metadata/filelists\" xmlns:rpm=\"http://linux.duke.edu/metadata/rpm\" packages=\"");
filelists.write(QString::number(filelistsFiles.count()).toLocal8Bit() );
filelists.write("\">\n");
for(QString const &p : filelistsFiles) {
QFile f(pf.absoluteFilePath(p));
if(!f.open(QFile::ReadOnly)) {
std::cerr << "Can't open " << qPrintable(p) << std::endl;
continue;
}
filelists.write(f.readAll());
f.close();
}
filelists.write("</filelists>");
filelists.close();
}
QFile other(rd.absoluteFilePath("other.xml"));
if(!other.open(QFile::WriteOnly|QFile::Truncate)) {
std::cerr << "Can't open " << qPrintable(other.fileName()) << std::endl;
return false;
} else {
QStringList const otherFiles = pf.entryList(QStringList() << "*.other.xml", QDir::Files|QDir::Readable, QDir::Name);
other.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<otherdata xmlns=\"http://linux.duke.edu/metadata/other\" xmlns:rpm=\"http://linux.duke.edu/metadata/rpm\" packages=\"");
other.write(QString::number(otherFiles.count()).toLocal8Bit() );
other.write("\">\n");
for(QString const &p : otherFiles) {
QFile f(pf.absoluteFilePath(p));
if(!f.open(QFile::ReadOnly)) {
std::cerr << "Can't open " << qPrintable(p) << std::endl;
continue;
}
other.write(f.readAll());
f.close();
}
other.write("</otherdata>");
other.close();
}
QFile appstream(rd.absoluteFilePath("appstream.xml"));
if(!appstream.open(QFile::WriteOnly|QFile::Truncate)) {
std::cerr << "Can't open " << qPrintable(appstream.fileName()) << std::endl;
return false;
} else {
QStringList const appstreamFiles = pf.entryList(QStringList() << "*.appstream.xml", QDir::Files|QDir::Readable, QDir::Name);
appstream.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<components origin=\"");
appstream.write(origin);
appstream.write("\" version=\"0.14\">\n");
for(QString const &p : appstreamFiles) {
QFile f(pf.absoluteFilePath(p));
if(!f.open(QFile::ReadOnly)) {
std::cerr << "Can't open " << qPrintable(p) << std::endl;
continue;
}
appstream.write(f.readAll());
f.close();
}
appstream.write("</components>");
appstream.close();
}
QStringList const appstreamIcons = pf.entryList(QStringList() << "*.appstream-icons", QDir::Dirs|QDir::Readable, QDir::Name);
Archive icons(rd.absoluteFilePath("appstream-icons.tar"));
for(QString const &iconDir : appstreamIcons) {
QDir d(pf.absoluteFilePath(iconDir));
QStringList iconFiles = recursiveEntryList(d);
for(QString const &file : iconFiles) {
QFile f(d.absoluteFilePath(file));
if(f.open(QFile::ReadOnly)) {
icons.addFile(file, f.readAll());
f.close();
}
}
}
icons.close();
return true;
}
int main(int argc, char **argv) {
setenv("QT_QPA_PLATFORM", "offscreen", 1);
QGuiApplication app(argc, argv);
QGuiApplication::setApplicationName("createmd-perfile");
QGuiApplication::setApplicationVersion("0.0.1");
QCommandLineParser cp;
cp.setApplicationDescription("RPM repository metadata creator");
cp.addOptions({
{{"c", "cleanup"}, QGuiApplication::translate("main", "Clean up [remove stale metadata files] only")},
{{"o", "origin"}, QGuiApplication::translate("main", "Origin identifier to be used (only while generating from scratch)"), "origin"},
{{"V", "verbose"}, QGuiApplication::translate("main", "Verbose debugging output")},
});
cp.addHelpOption();
cp.addVersionOption();
cp.addPositionalArgument("path", QGuiApplication::translate("main", "Directory containing the RPM files"), "[path...]");
cp.process(app);
if(cp.positionalArguments().isEmpty()) {
std::cerr << "Usage: " << argv[0] << "/path/to/rpm/files" << std::endl;
return 1;
}
bool const cleanupOnly = cp.isSet("c");
verbose = cp.isSet("V");
String origin = cp.value("o");
if(!origin)
origin = "openmandriva";
for(QString const &path : cp.positionalArguments()) {
QDir d(path);
cleanup(d);
if(cleanupOnly)
continue;
QStringList files = newFiles(d);
for(QString const &f : files)
extractMetadata(d, f);
files = modifiedFiles(d);
for(QString const &f : files)
extractMetadata(d, f);
mergeMetadata(d, origin);
finalizeMetadata(d.absoluteFilePath("repodata"));
}
}