-
Notifications
You must be signed in to change notification settings - Fork 2
/
package.cpp
576 lines (505 loc) · 14.5 KB
/
package.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
#include <memory>
#include <archive.h>
#include <archive_entry.h>
#include "main.h"
#include "elf.h"
#include "package.h"
namespace pkgdepdb {
static bool care_about(struct archive_entry *entry, mode_t mode) {
#if 0
if (AE_IFLNK == (mode & AE_IFLNK)) {
// ignoring symlinks for now
return false;
}
#endif
if (AE_IFREG != (mode & AE_IFREG)) {
// not a regular file
return false;
}
if (!archive_entry_size_is_set(entry)) {
// ignore files which have no size...
return false;
}
return true;
}
// because isspace(3) is locale dependent
static bool c_isspace(const char c) {
return (c == ' ' || c == '\t' ||
c == '\n' || c == '\r' ||
c == '\v' || c == '\f');
}
static bool ends_word(const char c) {
return c_isspace(c) || c == '=';
}
static
void split_depstring(const string &full, string &name, string &constraint) {
size_t opidx = full.find_first_of("=<>!");
if (opidx == string::npos) {
name = full;
constraint.clear();
} else {
name = full.substr(0, opidx);
constraint = full.substr(opidx);
}
}
// NOTE:
// Judgding from pacman/libalpm source code this function
// is way less strict about the formatting, as we skip whitespace
// between every word, whereas pacman matches /^(\w+) = (.*)$/ exactly.
static bool read_info(Package *pkg, struct archive *tar, const size_t size,
const Config& optconfig)
{
vec<char> data(size);
ssize_t rc = archive_read_data(tar, &data[0], size);
if ((size_t)rc != size) {
optconfig.Log(Error, "failed to read .PKGINFO");
return false;
}
string str(&data[0], data.size());
return pkg->ReadInfo(str, size, optconfig);
}
bool Package::ReadInfo(const string& str, const size_t size,
const Config& optconfig)
{
size_t pos = 0;
auto skipwhite = [&]() {
while (pos < size && c_isspace(str[pos]))
++pos;
};
auto skipline = [&]() {
while (pos < size && str[pos] != '\n')
++pos;
};
auto getvalue = [&](const char *entryname, string &out) -> bool {
skipwhite();
if (pos >= size) {
optconfig.Log(Error, "invalid %s entry in .PKGINFO", entryname);
return false;
}
if (str[pos] != '=') {
optconfig.Log(Error, "Error in .PKGINFO");
return false;
}
++pos;
skipwhite();
if (pos >= size) {
optconfig.Log(Error, "invalid %s entry in .PKGINFO", entryname);
return false;
}
size_t to = str.find_first_of("\n\r", pos);
while (to > 0 && (str[to-1] == ' ' || str[to-1] == '\t'))
--to;
out = str.substr(pos, to-pos);
skipline();
return true;
};
auto isentry = [&](const char *what, size_t len) -> bool {
if (size-pos > len &&
str.compare(pos, len, what) == 0 &&
ends_word(str[pos+len]))
{
pos += len;
return true;
}
return false;
};
auto addinfo = [&]() {
if (pos >= size)
return;
auto keypos = pos;
while (pos < size && str[pos] != '=' && str[pos] != c_isspace(str[pos]))
++pos;
auto key = str.substr(keypos, pos-keypos-1);
skipwhite();
if (pos >= size || str[pos] != '=') {
optconfig.Log(Error, "invalid entry in .PKGINFO\n");
if (pos < size)
skipline();
return;
}
++pos;
skipwhite();
auto valuepos = pos;
skipline();
if (valuepos != pos) {
auto value = str.substr(valuepos, pos-valuepos);
this->info_[key].emplace_back(value);
}
};
string es;
string version, constraint;
while (pos < size) {
skipwhite();
if (str[pos] == '#') {
skipline();
continue;
}
if (isentry("pkgname", sizeof("pkgname")-1)) {
if (!getvalue("pkgname", name_))
return false;
continue;
}
if (isentry("pkgver", sizeof("pkgver")-1)) {
if (!getvalue("pkgver", version_))
return false;
continue;
}
if (isentry("pkgbase", sizeof("pkgbase")-1)) {
if (pkgbase_.empty()) {
if (!getvalue("pkgbase", pkgbase_))
return false;
continue;
}
// if we already had a pkgbase line, fall through to the info_ case
}
if (!optconfig.package_depends_ && !optconfig.package_info_) {
skipline();
continue;
}
if (optconfig.package_info_ && isentry("pkgdesc", sizeof("pkgdesc")-1)) {
if (description_.empty()) {
if (!getvalue("pkgdesc", description_))
return false;
continue;
}
// if we already had a pkgdesc line, fall through to the info_ case
}
if (!optconfig.package_depends_) {
addinfo();
continue;
}
if (isentry("depend", sizeof("depend")-1)) {
if (!getvalue("depend", es))
return false;
split_depstring(es, version, constraint);
depends_.emplace_back(version, constraint);
continue;
}
if (isentry("makedepend", sizeof("makedepend")-1)) {
if (!getvalue("makedepend", es))
return false;
split_depstring(es, version, constraint);
makedepends_.emplace_back(version, constraint);
continue;
}
if (isentry("checkdepend", sizeof("checkdepend")-1)) {
if (!getvalue("checkdepend", es))
return false;
split_depstring(es, version, constraint);
checkdepends_.emplace_back(version, constraint);
continue;
}
if (isentry("optdepend", sizeof("optdepend")-1)) {
if (!getvalue("optdepend", es))
return false;
size_t c = es.find_first_of(':');
if (c != string::npos)
es.erase(c);
if (es.length()) {
split_depstring(es, version, constraint);
optdepends_.emplace_back(version, constraint);
}
continue;
}
if (isentry("replaces", sizeof("replaces")-1)) {
if (!getvalue("replaces", es))
return false;
split_depstring(es, version, constraint);
replaces_.emplace_back(version, constraint);
continue;
}
if (isentry("conflict", sizeof("conflict")-1)) {
if (!getvalue("conflict", es))
return false;
split_depstring(es, version, constraint);
conflicts_.emplace_back(version, constraint);
continue;
}
if (isentry("provides", sizeof("provides")-1)) {
if (!getvalue("provides", es))
return false;
split_depstring(es, version, constraint);
provides_.emplace_back(version, constraint);
continue;
}
if (isentry("group", sizeof("group")-1)) {
if (!getvalue("group", es))
return false;
groups_.insert(es);
continue;
}
if (optconfig.package_info_)
addinfo();
else
skipline();
}
return true;
}
static inline
tuple<string, string> splitpath(const string& path)
{
size_t slash = path.find_last_of('/');
if (slash == string::npos)
return make_tuple("/", path);
if (path[0] != '/')
return make_tuple(move(string("/") + path.substr(0, slash)),
path.substr(slash+1));
return make_tuple(path.substr(0, slash), path.substr(slash+1));
}
static bool read_object(Package *pkg,
struct archive *tar,
string &&filename,
size_t size,
const Config &optconfig)
{
vec<char> data;
data.resize(size);
ssize_t rc = archive_read_data(tar, &data[0], size);
if (rc < 0) {
optconfig.Log(Error, "failed to read from archive stream\n");
return false;
}
else if ((size_t)rc != size) {
optconfig.Log(Error, "file was short: %s\n", filename.c_str());
return false;
}
bool err = false;
rptr<Elf> object(Elf::Open(&data[0], data.size(), &err, filename.c_str(),
optconfig));
if (!object.get()) {
if (err)
optconfig.Log(Error, "error in: %s\n", filename.c_str());
return !err;
}
auto split(move(splitpath(filename)));
object->dirname_ = move(std::get<0>(split));
object->basename_ = move(std::get<1>(split));
object->SolvePaths(object->dirname_);
pkg->objects_.push_back(object);
return true;
}
static bool add_entry(Package *pkg,
struct archive *tar,
struct archive_entry *entry,
const Config& optconfig)
{
string filename(archive_entry_pathname(entry));
bool isinfo = filename == ".PKGINFO";
mode_t mode = archive_entry_mode(entry);
// one less string-copy:
guard addfile([&filename,pkg]() {
pkg->filelist_.emplace_back(move(filename));
});
if (!optconfig.package_filelist_ ||
isinfo ||
(AE_IFDIR == (mode & AE_IFDIR)) ||
filename[filename.length()-1] == '/' ||
filename == ".INSTALL" ||
filename == ".MTREE")
{
addfile.release();
}
// for now we only care about files named lib.*\.so(\.|$)
if (!isinfo && !care_about(entry, mode))
{
archive_read_data_skip(tar);
return true;
}
if (AE_IFLNK == (mode & AE_IFLNK)) {
// it's a symlink...
const char *link = archive_entry_symlink(entry);
if (!link) {
optconfig.Log(Error, "error reading symlink");
return false;
}
archive_read_data_skip(tar);
pkg->load_.symlinks[filename] = link;
return true;
}
// Check the size
auto isize = archive_entry_size(entry);
if (isize <= 0)
return true;
auto size = static_cast<size_t>(isize);
if (isinfo)
return read_info(pkg, tar, size, optconfig);
return read_object(pkg, tar, move(filename), size, optconfig);
}
Elf* Package::Find(const string& dirname, const string& basename) const {
for (auto &obj : objects_) {
if (obj->dirname_ == dirname && obj->basename_ == basename)
return const_cast<Elf*>(obj.get());
}
return nullptr;
}
void Package::Guess(const string& path) {
// extract the basename:
size_t at = path.find_last_of('/');
string base(at == string::npos ? path : path.substr(at+1));
// at least N.tgz
if (base.length() < 5)
return;
// ArchLinux scheme:
// ${name}-${pkgver}-${pkgrel}-${CARCH}.pkg.tar.*
// Slackware:
// ${name}-${pkgver}-${CARCH}-${build}.t{gz,bz2,xz}
// so the first part up to the first /-\d/ is part of the name
size_t to = base.find_first_of("-.");
// sanity:
if (!to || to == string::npos)
return;
while (to+1 < base.length() && // gonna check [to+1]
base[to] != '.' && // a dot ends the name
!(base[to+1] >= '0' && base[to+1] <= '9'))
{
// name can have dashes, let's move to the next one
to = base.find_first_of("-.", to+1);
}
name_ = base.substr(0, to);
if (base[to] != '-' || !(base[to+1] >= '0' && base[to+1] <= '9')) {
// no version
return;
}
// version
size_t from = to+1;
to = base.find_first_of('-', from);
if (to == string::npos) {
// we'll take it...
version_ = base.substr(from);
return;
}
bool slack = true;
// check for a pkgrel (Arch scheme)
if (base[to] == '-' &&
to+1 < base.length() &&
(base[to+1] >= '0' && base[to+1] <= '9'))
{
slack = false;
to = base.find_first_of("-.", to+1);
}
version_ = base.substr(from, to-from);
if (!slack || to == string::npos)
return;
// slackware build-name comes right before the extension
to = base.find_last_of('.');
if (!to || to == string::npos)
return;
from = base.find_last_of("-.", to-1);
if (from && from != string::npos) {
version_.append(1, '-');
version_.append(base.substr(from+1, to-from-1));
}
}
Package* Package::Open(const string& path, const Config& optconfig) {
uniq<Package> package(new Package);
struct archive *tar = archive_read_new();
archive_read_support_filter_all(tar);
archive_read_support_format_all(tar);
struct archive_entry *entry;
if (ARCHIVE_OK != archive_read_open_filename(tar, path.c_str(), 10240)) {
return 0;
}
while (ARCHIVE_OK == archive_read_next_header(tar, &entry)) {
if (!add_entry(package.get(), tar, entry, optconfig))
return 0;
}
archive_read_free(tar);
if (!package->name_.length() && !package->version_.length())
package->Guess(path);
bool changed;
do {
changed = false;
for (auto link = package->load_.symlinks.begin(); link != package->load_.symlinks.end();)
{
auto linkfrom = splitpath(link->first);
decltype(linkfrom) linkto;
// handle relative as well as absolute symlinks
if (!link->second.length()) {
// illegal
++link;
continue;
}
if (link->second[0] == '/') // absolute
linkto = splitpath(link->second);
else // relative
{
string fullpath = std::get<0>(linkfrom) + "/" + link->second;
linkto = splitpath(fullpath);
}
Elf *obj = package->Find(std::get<0>(linkto), std::get<1>(linkto));
if (!obj) {
++link;
continue;
}
changed = true;
Elf *copy = new Elf(*obj);
copy->dirname_ = move(std::get<0>(linkfrom));
copy->basename_ = move(std::get<1>(linkfrom));
copy->SolvePaths(obj->dirname_);
package->objects_.push_back(copy);
package->load_.symlinks.erase(link++);
}
} while (changed);
package->load_.symlinks.clear();
return package.release();
}
void Package::ShowNeeded() {
const char *name = this->name_.c_str();
for (auto &obj : objects_) {
string path = obj->dirname_ + "/" + obj->basename_;
const char *objname = path.c_str();
for (auto &need : obj->needed_) {
printf("%s: %s NEEDS %s\n", name, objname, need.c_str());
}
}
}
bool Package::Conflict(const Package& self, const Package &other) {
for (auto &conf : self.conflicts_) {
const string &name = std::get<0>(conf);
#ifdef PKGDEPDB_ENABLE_ALPM
string op, ver;
split_constraint(std::get<1>(conf), op, ver);
if (ver.length()) {
if (package_satisfies(&other, name, op, ver))
return true;
} else {
#endif
if (other.name_ == name)
return true;
for (auto &prov : other.provides_) {
if (std::get<0>(prov) == name)
return true;
}
#ifdef PKGDEPDB_ENABLE_ALPM
}
#endif
}
return false;
}
bool Package::ConflictsWith(const Package &other) const {
return Conflict(*this, other) || Conflict(other, *this);
}
bool Package::Replaces(const Package &other) const {
for (auto &conf : replaces_) {
const string &name = std::get<0>(conf);
#ifdef PKGDEPDB_ENABLE_ALPM
string op, ver;
split_constraint(std::get<1>(conf), op, ver);
if (ver.length()) {
if (package_satisfies(&other, name, op, ver))
return true;
} else {
#endif
if (other.name_ == name)
return true;
for (auto &prov : other.provides_) {
if (std::get<0>(prov) == name)
return true;
}
#ifdef PKGDEPDB_ENABLE_ALPM
}
#endif
}
return false;
}
} // ::pkgdepdb