summaryrefslogtreecommitdiff
path: root/src/app_adbdump.c
blob: ce7b5d625e37eec0211c14000c1744f1e2f603f9 (plain) (blame)
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
#include <stdio.h>
#include <unistd.h>
#include "apk_adb.h"
#include "apk_applet.h"
#include "apk_print.h"

static const struct adb_db_schema dbschemas[] = {
	{ .magic = ADB_SCHEMA_INDEX,		.root = &schema_index, },
	{ .magic = ADB_SCHEMA_INSTALLED_DB,	.root = &schema_idb, },
	{ .magic = ADB_SCHEMA_PACKAGE,		.root = &schema_package },
	{},
};

static int adbdump_main(void *pctx, struct apk_ctx *ac, struct apk_string_array *args)
{
	struct apk_out *out = &ac->out;
	char **arg;
	int r;

	foreach_array_item(arg, args) {
		struct adb_walk_gentext td = {
			.d.ops = &adb_walk_gentext_ops,
			.d.schemas = dbschemas,
			.out = out->out,
		};

		r = adb_walk_adb(&td.d,
			adb_decompress(apk_istream_from_file_mmap(AT_FDCWD, *arg), 0),
			apk_ctx_get_trust(ac));
		if (r) {
			apk_err(out, "%s: %s", *arg, apk_error_str(r));
			return r;
		}
	}

	return 0;
}

static struct apk_applet apk_adbdump = {
	.name = "adbdump",
	.main = adbdump_main,
};
APK_DEFINE_APPLET(apk_adbdump);


static int adbgen_main(void *pctx, struct apk_ctx *ac, struct apk_string_array *args)
{
	struct apk_out *out = &ac->out;
	char **arg;
	int r;
	struct adb_walk_genadb genadb = {
		.d.ops = &adb_walk_genadb_ops,
		.d.schemas = dbschemas,
	};

	adb_w_init_alloca(&genadb.db, 0, 1000);
	adb_w_init_alloca(&genadb.idb[0], 0, 100);
	foreach_array_item(arg, args) {
		adb_reset(&genadb.db);
		adb_reset(&genadb.idb[0]);
		r = adb_walk_text(&genadb.d, apk_istream_from_file(AT_FDCWD, *arg));
		if (!r) {
			adb_w_root(&genadb.db, genadb.stored_object);
			r = adb_c_create(apk_ostream_to_fd(STDOUT_FILENO), &genadb.db,
				apk_ctx_get_trust(ac));
		}
		adb_free(&genadb.db);
		adb_free(&genadb.idb[0]);
		if (r) apk_err(out, "%s: %s", *arg, apk_error_str(r));
	}

	return 0;
}

static struct apk_applet apk_adbgen = {
	.name = "adbgen",
	.main = adbgen_main,
};
APK_DEFINE_APPLET(apk_adbgen);