summaryrefslogtreecommitdiff
path: root/src/pkgman.c
blob: 7f5912ea005eceea09d5f54a9e98d0325f99eb43 (plain)
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
#include <pkgman.h>

#include <net.h>
#include <parser.h>
#include <um.h>
#include <lib/sv.h>
#include <err.h>
#include <lib/url.h>
#include <lib/archive.h>
#include <cookbook.h>
#include <crypto.h>

#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <gpgme.h>
#include <archive.h>
#include <archive_entry.h>
#include <errno.h>

#define PKGMAN_CONFIG_CHECK(x)							\
	do {									\
		TRY(pc->dir_tmp != NULL, PKGCONFERR);				\
		TRY(pc->dir_staging != NULL, PKGCONFERR);			\
		TRY(pc->upstream != NULL, PKGCONFERR);				\
	} while(0)								

int pkgman_upstream_check(struct pkgman_config *pc, const char *pkg)
{
	assert(pkg != NULL);
	PKGMAN_CONFIG_CHECK(pc);

	int ret = -ERR;

	struct url upstream;
	url_init(&upstream, pc->upstream);
	url_append_path(&upstream, "list");
	
	struct net_write_data mem = { 0 };
	ret = net_send_request(upstream.buffer,
			       WRITE_OPT_MEMORY,
			       (void*)&mem);
	
	if (ret != SUCCESS)
		goto cleanup;
	
	struct um_user_data userdata = { 0 };
	struct parser parser;
	struct parser_backend backend = um_backend();
	struct string_view parser_src = {0};
	parser_src.buf = mem.buffer;
	parser_src.len = mem.size;
	
	parser_init(&parser, &parser_src, &backend, (void*)&userdata);
	parser_parse(&parser);
	
	struct string_view sv_pkg = sv_create(pkg, strlen(pkg)); 
	
	int found = 0;
	
	LL_FOREACH(manifest, &userdata.manifest) {
		if (!current->data.key.buf)
			continue;
		
		if (sv_equal(&sv_pkg, &current->data.key)) {
			found = 1;
			printf("Package '%s' found on upstream!\n", pkg);
		        ret = SUCCESS;
		}
	}
	
	if (!found) {
		printf("Package '%s' not found!\n", pkg);
		ret =  -PKGNOTFND;
	}
	

	LL_FOREACH(manifest, &userdata.manifest) {
		if (current->data.key.buf == NULL)
		    continue;
		
		free(current->data.key.buf);
		free(current->data.value.buf);
	}
	
	ll_manifest_free(&userdata.manifest);

 cleanup:	
	free(mem.buffer);
	url_free(&upstream);

	return ret;
}

int pkgman_install_pkg(struct pkgman_config *pc, const char *pkg)
{
	assert(pc != NULL);
	PKGMAN_CONFIG_CHECK(pc);
	
	int ret = -ERR;
	
	struct url path = {0};
	url_init(&path, pc->dir_tmp);
	url_append_path(&path, pkg);
	
	struct url dst_path = {0};
	url_init(&dst_path, pc->dir_tmp);
	url_append_path(&dst_path, pkg);
	url_append(&dst_path, "-extract/");

	if (mkdir(dst_path.buffer, 0777) != 0 && errno != EEXIST) {
		ret = -ERR;
		goto cleanup;			
	}

        pkg_extract(path.buffer, &dst_path);

	struct string_view recipe_out = {0};
	sv_init(&recipe_out, "");

	int cret = cookbook_recipe_run(&dst_path, "artifacts", &recipe_out);
	
	if (cret != 0) {
		printf("Artifacts recipe failed with error code: %d", cret);
		ret = -ERR;
		goto cleanup;
	}

	printf("Not displaying artifacts...\n");

	sv_free(&recipe_out);
	sv_init(&recipe_out, "");

        cret = cookbook_recipe_run(&dst_path, "install", &recipe_out);
	
	if (cret != 0) {
		printf("Install recipe failed with error code: %d", cret);
		ret = -ERR;
		goto cleanup;
	}

	printf("Build script response: %s\n", recipe_out.buf);

	ret = SUCCESS;
	
 cleanup:      
        sv_free(&recipe_out);
        url_free(&dst_path);
	url_free(&path);
	return ret;
}

int pkgman_upstream_integrity_download(struct pkgman_config *pc,
				       const char *pkg)
{
	assert(pc != NULL);
	PKGMAN_CONFIG_CHECK(pc);

	int ret = -ERR;
	struct url url_pkg = {0};
	struct url url_sig = {0};

	url_init(&url_pkg, pc->upstream);
	url_append_path(&url_pkg, pkg);
	url_append(&url_pkg, ".tar.zstd");

	url_copy(&url_pkg, &url_sig);
	url_append(&url_sig, ".sig");

	struct url path_pkg = {0};
	struct url path_sig = {0};

	url_init(&path_pkg, "/tmp/pkgman");
	url_append_path(&path_pkg, pkg);

	url_copy(&path_pkg, &path_sig);
	url_append(&path_sig, ".sig");
	
	///// I need to abstract away the cache filemgmt feature in the future.

	mkdir("/tmp/pkgman/", 0777);
	
	////

	if(net_download(url_pkg.buffer, path_pkg.buffer) != SUCCESS) {
		ret = -PKGNOTFND;
		goto cleanup;
	}
	
	printf("Package '%s' is downloaded from upstream.\n", pkg);
	
	// Download signature file
	if(net_download(url_sig.buffer, path_sig.buffer) != SUCCESS) {
		ret = -PKGNOTFND;
		goto cleanup;
	}
	
	printf("Signature for '%s' downloaded.\n", url_sig.buffer);
	
	if(crypto_verify_integrity(path_sig.buffer, path_pkg.buffer)
	   != SUCCESS) {
		ret = -INTEGRITYERR;
		goto cleanup;
	}

	ret = SUCCESS;

 cleanup:
	url_free(&url_pkg);
	url_free(&url_sig);

	url_free(&path_pkg);
	url_free(&path_sig);
	
	return ret;
}