-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.c
383 lines (305 loc) · 11.7 KB
/
install.c
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
/*
* This file is part of aurinstall.
*
* aurinstall is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* aurinstall is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with aurinstall. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2023 Hasan Zahra
* https://github.com/prismz/aurinstall
*/
#include "install.h"
#include "requests.h"
#include "output.h"
#include "alloc.h"
#include "util.h"
#include "rpc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <ctype.h>
int import_pgp_keys(char *path)
{
char *full_path = safe_malloc(sizeof(char) * PATH_MAX);
snprintf(full_path, PATH_MAX, "%s/.SRCINFO", path);
FILE *fp = fopen(full_path, "rb");
if (!fp) {
free(full_path);
return 1;
}
char buf[1024];
while ((fgets(buf, 1024, fp)) != NULL) {
size_t len = strlen(buf);
size_t i;
for (i = 0; i < len; i++) {
if (!isspace(buf[i]))
break;
}
char *searchterm = "validpgpkeys";
if (strncmp(searchterm, buf + i, strlen(searchterm)) != 0)
continue;
char *stripped = buf + i;
for (i = 0; i < strlen(stripped); i++) {
char c = stripped[i];
if (c == '=') {
i++;
break;
}
}
char *key = stripped + i;
if (snsystem("gpg --recv-keys %s",
32 + strlen(key), key) != 0) {
fprintf(stderr, "failed to import key: %s\n", key);
return 1;
}
}
free(full_path);
fclose(fp);
return 0;
}
int install_package(char *name, char *cache_path)
{
int rc = 0;
struct rpc_data *api_json_result = NULL;
struct json *pkg_json = NULL;
struct package *pkg_info = NULL;
char *dest_path = NULL;
size_t api_info_str_len = strlen(name) + 128;
char *api_info_str = safe_malloc(sizeof(char) * api_info_str_len);
snprintf(api_info_str, api_info_str_len,
"https://aur.archlinux.org/rpc/?v=5&type=info&arg=%s",
name
);
api_json_result = make_rpc_request(api_info_str);
free(api_info_str);
if (api_json_result->resultcount != 1) {
fprintf(stderr, "error installing package %s.\n", name);
rc = 1;
goto end;
}
pkg_json = json_get_array_item(api_json_result->results, 0);
pkg_info = parse_package_json(pkg_json);
if (pkg_info->outofdate) {
bool shouldcontinue = yesno_prompt(
"Package is out of date. Continue?", false);
if (!shouldcontinue)
goto end;
}
dest_path = safe_malloc(sizeof(char) * PATH_MAX);
snprintf(dest_path, PATH_MAX, "%s/%s", cache_path, name);
printf("installing to directory %s\n", dest_path);
int should_clone = 1;
if (!dir_is_empty(dest_path)) {
char *prompt = "Existing package data found. Rebuild?";
should_clone = yesno_prompt(prompt, false);
if (should_clone)
snsystem("rm -rfv %s", PATH_MAX + 16, dest_path);
}
if (should_clone) {
char *fmt = "git clone https://aur.archlinux.org/%s.git %s";
int git_clone_r = snsystem(fmt,
strlen(pkg_info->name) + strlen(dest_path) + 64,
pkg_info->name, dest_path
);
if (git_clone_r) {
fprintf(stderr, "Failed to clone git repository.\n");
rc = 1;
goto end;
}
}
if (import_pgp_keys(dest_path)) {
fprintf(stderr, "failed to import PGP keys.\n");
rc = 1;
goto end;
}
char *makepkg_fmt = "cd %s && makepkg --noconfirm -si";
int makepkg_r = snsystem(makepkg_fmt, PATH_MAX + 128, dest_path);
if (makepkg_r) {
fprintf(stderr, "makepkg failed.\n");
rc = 1;
}
end:
free(dest_path);
free_rpc_data(api_json_result);
free_package_data(pkg_info);
return rc;
}
/* returns a hashmap with the package names and values */
HashMap *get_installed_packages(void)
{
HashMap *installed_packages = new_hashmap(16);
if (installed_packages == NULL)
return NULL;
FILE *fp = popen("pacman -Qm", "r");
char *line_buf = safe_malloc(sizeof(char) * 1024);
while (fgets(line_buf, 1024, fp) != NULL) {
size_t len = strlen(line_buf);
line_buf[len - 1] = '\0';
len--;
int splitspace_idx = -1;
for (size_t i = 0; i < len; i++) {
if (line_buf[i] == ' ') {
splitspace_idx = (int)i;
break;
}
}
if (splitspace_idx == -1) {
fprintf(stderr, "invalid package atom: %s\n", line_buf);
free_hashmap(installed_packages);
pclose(fp);
free(line_buf);
return NULL;
}
line_buf[splitspace_idx] = '\0';
char *version_ro = line_buf + splitspace_idx + 1;
char *name_ro = line_buf;
char *name = safe_strdup(name_ro);
char *version = safe_strdup(version_ro);
HMItem *item = new_item(name, version, free, free);
if (hashmap_set(installed_packages, item) != 0) {
fprintf(stderr, "failed to set hashmap item.\n");
free_hashmap(installed_packages);
pclose(fp);
free(name);
free(version);
free(line_buf);
return NULL;
}
}
if (installed_packages->stored == 0) {
free_hashmap(installed_packages);
return NULL;
}
free(line_buf);
pclose(fp);
return installed_packages;
}
int update_packages(char *cache_path)
{
int repeat = 0; /* multiple updates needed */
HashMap *installed_packages = get_installed_packages();
if (installed_packages == NULL)
return 1;
/* no installed AUR packages */
if (installed_packages->stored == 0)
return 0;
/*
* build one long api request - this is tedious, but works better for
* not DDoS'ing the AUR.
*/
char *fmt = safe_calloc(1, sizeof(char) * 4443);
strcpy(fmt, "https://aur.archlinux.org/rpc/?v=5&type=info");
/* keep track of the longest package for nicer formatting below */
size_t largest_package_name_length = 0;
for (size_t i = 0; i < installed_packages->can_store; i++) {
HMItem *item = installed_packages->items[i];
if (item == NULL)
continue;
char *key = item->key;
size_t klen = strlen(key);
if (klen > largest_package_name_length)
largest_package_name_length = klen;
char *delim = "&arg[]=";
size_t added = strlen(delim) + klen;
if (added + strlen(fmt) >= 4443) {
repeat = 1;
break;
}
strcat(fmt, "&arg[]=");
strcat(fmt, key);
}
struct rpc_data *data = make_rpc_request(fmt);
if (data == NULL) {
free(fmt);
free_hashmap(installed_packages);
return 1;
}
size_t update_queue_i = 0;
char *update_queue[1024];
printf("The following have updates:\n");
/* parse the results */
struct json *results = data->results;
for (size_t i = 0; i < data->resultcount; i++) {
struct json *pkg_j = json_get_array_item(results, i);
struct package *pkg = parse_package_json(pkg_j);
char *name = pkg->name;
char *new_version = pkg->version;
char *installed_version = hashmap_index(installed_packages,
name);
/* add to list of old packages */
if (strcmp(new_version, installed_version) != 0) {
update_queue[update_queue_i++] = safe_strdup(name);
char name_fmt[1024];
snprintf(name_fmt, 1024, "%s%s%%-%zus%s: ",
BLUE, BOLD,
largest_package_name_length + 3, ENDC);
printf(name_fmt, name);
print_diff(installed_version, new_version);
printf("\n");
if (update_queue_i + 1 > 1024) {
repeat = 1;
free_package_data(pkg);
break;
}
}
free_package_data(pkg);
}
if (update_queue_i == 0) {
printf("no updates. exiting.\n");
goto end;
}
bool should_update = yesno_prompt("update?", true);
if (!should_update) {
repeat = 0;
goto end;
}
for (size_t i = 0; i < update_queue_i; i++) {
char *name = update_queue[i];
snsystem("rm -rf %s/%s", 2048, cache_path, name);
install_package(name, cache_path);
free(update_queue[i]);
}
end:
free(fmt);
free_rpc_data(data);
free_hashmap(installed_packages);
if (repeat) {
printf("Multiple updates needed. Re-running.\n");
return update_packages(cache_path);
}
return 0;
}
int clean_cache(char *cache_path)
{
return snsystem("rm -rfv %s", PATH_MAX + 32, cache_path);
}
int remove_packages(int n, char **pkgs)
{
size_t curr_len = 0;
size_t capacity = 4096;
char *str = safe_calloc(1, sizeof(char) * capacity);
for (int i = 0; i < n; i++) {
char *pkg = pkgs[i];
size_t len = strlen(pkg);
if (curr_len + len + 3 > capacity) {
int extra = (len > 32) ? len : 32;
capacity += extra;
str = safe_realloc(str, sizeof(char) * capacity);
}
strcat(str, pkg);
strcat(str, " ");
curr_len += len;
}
return snsystem("pacman -R %s", 32 + strlen(str), str);
}