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
|
#include <errno.h>
#include "adb.h"
#include "apk_adb.h"
#include "apk_print.h"
#include "apk_version.h"
#define APK_VERSION_CONFLICT 16
/* Few helpers to map old database to new one */
int apk_dep_split(apk_blob_t *b, apk_blob_t *bdep)
{
extern const apk_spn_match_def apk_spn_dependency_separator;
if (APK_BLOB_IS_NULL(*b)) return 0;
if (apk_blob_cspn(*b, apk_spn_dependency_separator, bdep, b)) {
/* found separator - update b to skip over after all separators */
if (!apk_blob_spn(*b, apk_spn_dependency_separator, NULL, b))
*b = APK_BLOB_NULL;
} else {
/* no separator - return this as the last dependency, signal quit */
*bdep = *b;
*b = APK_BLOB_NULL;
}
return 1;
}
adb_val_t adb_wo_pkginfo(struct adb_obj *obj, unsigned int f, apk_blob_t val)
{
struct apk_checksum csum;
adb_val_t v = ADB_ERROR(EAPKFORMAT);
/* FIXME: get rid of this function, and handle the conversion via schema? */
switch (f) {
case ADBI_PI_UNIQUE_ID:
if (!val.ptr || val.len < 4) break;
apk_blob_pull_csum(&val, &csum);
v = adb_w_int(obj->db, get_unaligned32(csum.data) & ADB_VALUE_MASK);
break;
case ADBI_PI_REPO_COMMIT:
if (val.len < 40) break;
csum.type = 20;
apk_blob_pull_hexdump(&val, APK_BLOB_CSUM(csum));
if (val.ptr) v = adb_w_blob(obj->db, APK_BLOB_CSUM(csum));
break;
default:
return adb_wo_val_fromstring(obj, f, val);
}
if (v != ADB_NULL && !ADB_IS_ERROR(v))
v = adb_wo_val(obj, f, v);
return v;
}
unsigned int adb_pkg_field_index(char f)
{
#define MAP(ch, ndx) [ch - 'A'] = ndx
static unsigned char map[] = {
MAP('C', ADBI_PI_UNIQUE_ID),
MAP('P', ADBI_PI_NAME),
MAP('V', ADBI_PI_VERSION),
MAP('T', ADBI_PI_DESCRIPTION),
MAP('U', ADBI_PI_URL),
MAP('I', ADBI_PI_INSTALLED_SIZE),
MAP('S', ADBI_PI_FILE_SIZE),
MAP('L', ADBI_PI_LICENSE),
MAP('A', ADBI_PI_ARCH),
MAP('D', ADBI_PI_DEPENDS),
MAP('i', ADBI_PI_INSTALL_IF),
MAP('p', ADBI_PI_PROVIDES),
MAP('o', ADBI_PI_ORIGIN),
MAP('m', ADBI_PI_MAINTAINER),
MAP('t', ADBI_PI_BUILD_TIME),
MAP('c', ADBI_PI_REPO_COMMIT),
MAP('r', ADBI_PI_REPLACES),
MAP('k', ADBI_PI_PRIORITY),
};
if (f < 'A' || f-'A' >= ARRAY_SIZE(map)) return 0;
return map[(unsigned char)f - 'A'];
}
/* Schema */
static apk_blob_t string_tostring(struct adb *db, adb_val_t val, char *buf, size_t bufsz)
{
return adb_r_blob(db, val);
}
static adb_val_t string_fromstring(struct adb *db, apk_blob_t val)
{
return adb_w_blob(db, val);
}
static int string_compare(struct adb *db1, adb_val_t v1, struct adb *db2, adb_val_t v2)
{
return apk_blob_sort(adb_r_blob(db1, v1), adb_r_blob(db2, v2));
}
static struct adb_scalar_schema scalar_string = {
.kind = ADB_KIND_BLOB,
.tostring = string_tostring,
.fromstring = string_fromstring,
.compare = string_compare,
};
static struct adb_scalar_schema scalar_mstring = {
.kind = ADB_KIND_BLOB,
.multiline = 1,
.tostring = string_tostring,
.fromstring = string_fromstring,
.compare = string_compare,
};
const struct adb_object_schema schema_string_array = {
.kind = ADB_KIND_ARRAY,
.num_fields = APK_MAX_PKG_TRIGGERS,
.fields = ADB_ARRAY_ITEM(scalar_string),
};
static int version_compare(struct adb *db1, adb_val_t v1, struct adb *db2, adb_val_t v2)
{
switch (apk_version_compare_blob(adb_r_blob(db1, v1), adb_r_blob(db2, v2))) {
case APK_VERSION_LESS: return -1;
case APK_VERSION_GREATER: return 1;
default: return 0;
}
}
static struct adb_scalar_schema scalar_version = {
.kind = ADB_KIND_BLOB,
.tostring = string_tostring,
.fromstring = string_fromstring,
.compare = version_compare,
};
static apk_blob_t hexblob_tostring(struct adb *db, adb_val_t val, char *buf, size_t bufsz)
{
apk_blob_t b = adb_r_blob(db, val), to = APK_BLOB_PTR_LEN(buf, bufsz);
if (APK_BLOB_IS_NULL(b)) return b;
apk_blob_push_hexdump(&to, b);
if (!APK_BLOB_IS_NULL(to))
return APK_BLOB_PTR_PTR(buf, to.ptr-1);
return APK_BLOB_PTR_LEN(buf, snprintf(buf, bufsz, "(%ld bytes)", b.len));
}
static struct adb_scalar_schema scalar_hexblob = {
.kind = ADB_KIND_BLOB,
.tostring = hexblob_tostring,
};
static apk_blob_t int_tostring(struct adb *db, adb_val_t val, char *buf, size_t bufsz)
{
return APK_BLOB_PTR_LEN(buf, snprintf(buf, bufsz, "%u", adb_r_int(db, val)));
}
static adb_val_t int_fromstring(struct adb *db, apk_blob_t val)
{
uint32_t n = apk_blob_pull_uint(&val, 10);
if (val.len) return ADB_ERROR(EINVAL);
return adb_w_int(db, n) ?: ADB_VAL_NULL;
}
static int int_compare(struct adb *db1, adb_val_t v1, struct adb *db2, adb_val_t v2)
{
uint32_t r1 = adb_r_int(db1, v1);
uint32_t r2 = adb_r_int(db1, v2);
if (r1 < r2) return -1;
if (r1 > r2) return 1;
return 0;
}
static struct adb_scalar_schema scalar_int = {
.kind = ADB_KIND_INT,
.tostring = int_tostring,
.fromstring = int_fromstring,
.compare = int_compare,
};
static apk_blob_t oct_tostring(struct adb *db, adb_val_t val, char *buf, size_t bufsz)
{
return APK_BLOB_PTR_LEN(buf, snprintf(buf, bufsz, "%o", adb_r_int(db, val)));
}
static struct adb_scalar_schema scalar_oct = {
.kind = ADB_KIND_INT,
.tostring = oct_tostring,
};
static apk_blob_t hsize_tostring(struct adb *db, adb_val_t val, char *buf, size_t bufsz)
{
off_t v = adb_r_int(db, val);
const char *unit = apk_get_human_size(v, &v);
return APK_BLOB_PTR_LEN(buf, snprintf(buf, bufsz, "%jd %s", (intmax_t)v, unit));
}
static struct adb_scalar_schema scalar_hsize = {
.kind = ADB_KIND_INT,
.tostring = hsize_tostring,
.fromstring = int_fromstring,
.compare = int_compare,
};
static apk_blob_t dependency_tostring(struct adb_obj *obj, char *buf, size_t bufsz)
{
apk_blob_t name, ver;
unsigned int mask;
name = adb_ro_blob(obj, ADBI_DEP_NAME);
ver = adb_ro_blob(obj, ADBI_DEP_VERSION);
if (APK_BLOB_IS_NULL(name)) return APK_BLOB_NULL;
if (APK_BLOB_IS_NULL(ver)) return name;
mask = adb_ro_int(obj, ADBI_DEP_MATCH) ?: APK_VERSION_EQUAL;
return APK_BLOB_PTR_LEN(buf,
snprintf(buf, bufsz, "%s"BLOB_FMT"%s"BLOB_FMT,
(mask & APK_VERSION_CONFLICT) ? "!" : "",
BLOB_PRINTF(name),
apk_version_op_string(mask & ~APK_VERSION_CONFLICT),
BLOB_PRINTF(ver)));
}
static int dependency_fromstring(struct adb_obj *obj, apk_blob_t bdep)
{
extern const apk_spn_match_def apk_spn_dependency_comparer;
extern const apk_spn_match_def apk_spn_repotag_separator;
apk_blob_t bname, bop, bver = APK_BLOB_NULL, btag;
int mask = APK_DEPMASK_ANY;
/* [!]name[<,<=,<~,=,~,>~,>=,>,><]ver */
/* parse the version */
if (bdep.ptr[0] == '!') {
bdep.ptr++;
bdep.len--;
mask |= APK_VERSION_CONFLICT;
}
if (apk_blob_cspn(bdep, apk_spn_dependency_comparer, &bname, &bop)) {
int i;
if (mask == 0)
goto fail;
if (!apk_blob_spn(bop, apk_spn_dependency_comparer, &bop, &bver))
goto fail;
mask = 0;
for (i = 0; i < bop.len; i++) {
switch (bop.ptr[i]) {
case '<':
mask |= APK_VERSION_LESS;
break;
case '>':
mask |= APK_VERSION_GREATER;
break;
case '~':
mask |= APK_VERSION_FUZZY|APK_VERSION_EQUAL;
break;
case '=':
mask |= APK_VERSION_EQUAL;
break;
}
}
if ((mask & APK_DEPMASK_CHECKSUM) != APK_DEPMASK_CHECKSUM &&
!apk_version_validate(bver))
goto fail;
} else {
bname = bdep;
bop = APK_BLOB_NULL;
bver = APK_BLOB_NULL;
}
if (apk_blob_cspn(bname, apk_spn_repotag_separator, &bname, &btag))
; /* tag = repository tag */
adb_wo_blob(obj, ADBI_DEP_NAME, bname);
if (mask != APK_DEPMASK_ANY) {
adb_wo_blob(obj, ADBI_DEP_VERSION, bver);
if (mask != APK_VERSION_EQUAL)
adb_wo_int(obj, ADBI_DEP_MATCH, mask);
}
return 0;
fail:
return -EAPKDEPFORMAT;
}
static int dependency_cmp(const struct adb_obj *o1, const struct adb_obj *o2)
{
return adb_ro_cmp(o1, o2, ADBI_DEP_NAME);
}
const struct adb_object_schema schema_dependency = {
.kind = ADB_KIND_OBJECT,
.num_fields = ADBI_DEP_MAX,
.tostring = dependency_tostring,
.fromstring = dependency_fromstring,
.compare = dependency_cmp,
.fields = {
ADB_FIELD(ADBI_DEP_NAME, "name", scalar_string),
ADB_FIELD(ADBI_DEP_VERSION, "version", scalar_version),
ADB_FIELD(ADBI_DEP_MATCH, "match", scalar_int),
},
};
static int dependencies_fromstring(struct adb_obj *obj, apk_blob_t b)
{
struct adb_obj dep;
apk_blob_t bdep;
adb_wo_alloca(&dep, &schema_dependency, obj->db);
while (apk_dep_split(&b, &bdep)) {
adb_wo_fromstring(&dep, bdep);
adb_wa_append_obj(obj, &dep);
}
return 0;
}
const struct adb_object_schema schema_dependency_array = {
.kind = ADB_KIND_ARRAY,
.fromstring = dependencies_fromstring,
.num_fields = APK_MAX_PKG_DEPENDENCIES,
.pre_commit = adb_wa_sort_unique,
.fields = ADB_ARRAY_ITEM(schema_dependency),
};
static int pkginfo_cmp(const struct adb_obj *o1, const struct adb_obj *o2)
{
int r;
r = adb_ro_cmp(o1, o2, ADBI_PI_NAME);
if (r) return r;
r = adb_ro_cmp(o1, o2, ADBI_PI_VERSION);
if (r) return r;
return adb_ro_cmp(o1, o2, ADBI_PI_UNIQUE_ID);
}
const struct adb_object_schema schema_pkginfo = {
.kind = ADB_KIND_OBJECT,
.num_fields = ADBI_PI_MAX,
.compare = pkginfo_cmp,
.fields = {
ADB_FIELD(ADBI_PI_NAME, "name", scalar_string),
ADB_FIELD(ADBI_PI_VERSION, "version", scalar_version),
ADB_FIELD(ADBI_PI_UNIQUE_ID, "unique-id", scalar_int),
ADB_FIELD(ADBI_PI_DESCRIPTION, "description", scalar_string),
ADB_FIELD(ADBI_PI_ARCH, "arch", scalar_string),
ADB_FIELD(ADBI_PI_LICENSE, "license", scalar_string),
ADB_FIELD(ADBI_PI_ORIGIN, "origin", scalar_string),
ADB_FIELD(ADBI_PI_MAINTAINER, "maintainer", scalar_string),
ADB_FIELD(ADBI_PI_URL, "url", scalar_string),
ADB_FIELD(ADBI_PI_REPO_COMMIT, "repo-commit", scalar_hexblob),
ADB_FIELD(ADBI_PI_BUILD_TIME, "build-time", scalar_int),
ADB_FIELD(ADBI_PI_INSTALLED_SIZE,"installed-size",scalar_hsize),
ADB_FIELD(ADBI_PI_FILE_SIZE, "file-size", scalar_hsize),
ADB_FIELD(ADBI_PI_PRIORITY, "priority", scalar_int),
ADB_FIELD(ADBI_PI_DEPENDS, "depends", schema_dependency_array),
ADB_FIELD(ADBI_PI_PROVIDES, "provides", schema_dependency_array),
ADB_FIELD(ADBI_PI_REPLACES, "replaces", schema_dependency_array),
ADB_FIELD(ADBI_PI_INSTALL_IF, "install-if", schema_dependency_array),
ADB_FIELD(ADBI_PI_RECOMMENDS, "recommends", schema_dependency_array),
},
};
const struct adb_object_schema schema_pkginfo_array = {
.kind = ADB_KIND_ARRAY,
.num_fields = APK_MAX_INDEX_PACKAGES,
.pre_commit = adb_wa_sort,
.fields = ADB_ARRAY_ITEM(schema_pkginfo),
};
const struct adb_object_schema schema_index = {
.kind = ADB_KIND_OBJECT,
.num_fields = ADBI_NDX_MAX,
.fields = {
ADB_FIELD(ADBI_NDX_DESCRIPTION, "description", scalar_string),
ADB_FIELD(ADBI_NDX_PACKAGES, "packages", schema_pkginfo_array),
},
};
static uint32_t file_get_default_int(unsigned i)
{
switch (i) {
case ADBI_FI_UID:
case ADBI_FI_GID:
return 0;
case ADBI_FI_MODE:
return 0644;
}
return -1;
}
static int file_cmp(const struct adb_obj *o1, const struct adb_obj *o2)
{
return adb_ro_cmp(o1, o2, ADBI_FI_NAME);
}
const struct adb_object_schema schema_file = {
.kind = ADB_KIND_OBJECT,
.num_fields = ADBI_FI_MAX,
.get_default_int = file_get_default_int,
.compare = file_cmp,
.fields = {
ADB_FIELD(ADBI_FI_NAME, "name", scalar_string),
ADB_FIELD(ADBI_FI_HASHES, "hash", scalar_hexblob),
ADB_FIELD(ADBI_FI_UID, "uid", scalar_int),
ADB_FIELD(ADBI_FI_GID, "gid", scalar_int),
ADB_FIELD(ADBI_FI_MODE, "mode", scalar_oct),
ADB_FIELD(ADBI_FI_XATTRS, "xattr", scalar_hexblob),
},
};
const struct adb_object_schema schema_file_array = {
.kind = ADB_KIND_ARRAY,
.pre_commit = adb_wa_sort,
.num_fields = APK_MAX_MANIFEST_FILES,
.fields = ADB_ARRAY_ITEM(schema_file),
};
static uint32_t path_get_default_int(unsigned i)
{
switch (i) {
case ADBI_FI_UID:
case ADBI_FI_GID:
return 0;
case ADBI_FI_MODE:
return 0755;
}
return -1;
}
const struct adb_object_schema schema_path = {
.kind = ADB_KIND_OBJECT,
.num_fields = ADBI_FI_MAX,
.get_default_int = path_get_default_int,
.compare = file_cmp,
.fields = {
ADB_FIELD(ADBI_FI_NAME, "name", scalar_string),
ADB_FIELD(ADBI_FI_FILES, "files", schema_file_array),
ADB_FIELD(ADBI_FI_UID, "uid", scalar_int),
ADB_FIELD(ADBI_FI_GID, "gid", scalar_int),
ADB_FIELD(ADBI_FI_MODE, "mode", scalar_oct),
ADB_FIELD(ADBI_FI_XATTRS, "xattr", scalar_hexblob),
},
};
const struct adb_object_schema schema_path_array = {
.kind = ADB_KIND_ARRAY,
.pre_commit = adb_wa_sort,
.num_fields = APK_MAX_MANIFEST_PATHS,
.fields = ADB_ARRAY_ITEM(schema_path),
};
const struct adb_object_schema schema_scripts = {
.kind = ADB_KIND_OBJECT,
.num_fields = ADBI_SCRPT_MAX,
.fields = {
ADB_FIELD(ADBI_SCRPT_TRIGGER, "trigger", scalar_mstring),
ADB_FIELD(ADBI_SCRPT_PREINST, "pre-install", scalar_mstring),
ADB_FIELD(ADBI_SCRPT_POSTINST, "post-install", scalar_mstring),
ADB_FIELD(ADBI_SCRPT_PREDEINST, "pre-deinstall",scalar_mstring),
ADB_FIELD(ADBI_SCRPT_POSTDEINST,"post-deinstall",scalar_mstring),
ADB_FIELD(ADBI_SCRPT_PREUPGRADE,"pre-upgrade", scalar_mstring),
ADB_FIELD(ADBI_SCRPT_POSTUPGRADE,"post-upgrade",scalar_mstring),
},
};
static int package_cmp(const struct adb_obj *o1, const struct adb_obj *o2)
{
return adb_ro_cmp(o1, o2, ADBI_PKG_PKGINFO);
}
const struct adb_object_schema schema_package = {
.kind = ADB_KIND_OBJECT,
.num_fields = ADBI_PKG_MAX,
.compare = package_cmp,
.fields = {
ADB_FIELD(ADBI_PKG_PKGINFO, "info", schema_pkginfo),
ADB_FIELD(ADBI_PKG_PATHS, "paths", schema_path_array),
ADB_FIELD(ADBI_PKG_SCRIPTS, "scripts", schema_scripts),
ADB_FIELD(ADBI_PKG_TRIGGERS, "triggers", schema_string_array),
//ADB_FIELD(ADBI_PKG_PASSWD, "passwd", schema_string_array),
},
};
const struct adb_adb_schema schema_package_adb = {
.kind = ADB_KIND_ADB,
.schema_id = ADB_SCHEMA_PACKAGE,
.schema = &schema_package,
};
const struct adb_object_schema schema_package_adb_array = {
.kind = ADB_KIND_ARRAY,
.pre_commit = adb_wa_sort,
.num_fields = APK_MAX_INDEX_PACKAGES,
.fields = ADB_ARRAY_ITEM(schema_package_adb),
};
const struct adb_object_schema schema_idb = {
.kind = ADB_KIND_OBJECT,
.num_fields = ADBI_IDB_MAX,
.fields = {
ADB_FIELD(ADBI_IDB_PACKAGES, "packages", schema_package_adb_array),
},
};
|