summaryrefslogtreecommitdiff
path: root/src/app_adbdump.c
blob: 5c1b69ce12530044aa9e829c10f3aa7524a716f9 (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
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <unistd.h>
#include <assert.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 mmap_and_dump_adb(struct apk_trust *trust, int fd, struct apk_out *out)
{
	struct adb db;
	struct adb_walk_gentext td = {
		.d.ops = &adb_walk_gentext_ops,
		.d.schemas = dbschemas,
		.out = out->out,
	};
	int r;

	r = adb_m_map(&db, fd, 0, NULL);
	if (r) return r;

	adb_walk_adb(&td.d, &db, trust);
	adb_free(&db);
	return 0;
}

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) {
		r = mmap_and_dump_adb(apk_ctx_get_trust(ac), open(*arg, O_RDONLY), out);
		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_istream(&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);