-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.c
350 lines (293 loc) · 12.4 KB
/
database.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
/**
* this file is part of kawa
* Copyright (c) 2020-2021 Emily <[email protected]>
*
* kawa 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.
*
* kawa 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 kawa. If not, see <https://www.gnu.org/licenses/>.
**/
#include "database.h"
struct package package_constructor(char* nameParam, char* descriptionParam, char* versionParam, char* archiveurlParam, char* maintainerParam, char* configurecmdParam, char* typeParam, char* sepbuildParam, char* uninstallcmdParam, char* licenseParam, int repoindexParam) {
// this constructs a struct package with malloc,
// so that it can safely returned, even if a local variable was used
// to initialize some parameter
// TODO: should be strnlen instead of strlen
// TODO: I know that this is horrible spaghetti code, please forgive me
char *name;
if (!(name = malloc(sizeof(char *) + sizeof(char) * strlen(nameParam ) + 1))) malloc_fail();
strcpy(name, nameParam);
char *description;
if (!(description = malloc(sizeof(char *) + sizeof(char) * strlen(descriptionParam ) + 1))) malloc_fail();
strcpy(description, descriptionParam);
char *version;
if (!(version = malloc(sizeof(char *) + sizeof(char) * strlen(versionParam ) + 1))) malloc_fail();
strcpy(version, versionParam);
char *archiveurl;
if (!(archiveurl = malloc(sizeof(char *) + sizeof(char) * strlen(archiveurlParam ) + 1))) malloc_fail();
strcpy(archiveurl, archiveurlParam);
char *maintainer;
if (!(maintainer = malloc(sizeof(char *) + sizeof(char) * strlen(maintainerParam ) + 1))) malloc_fail();
strcpy(maintainer, maintainerParam);
char *configurecmd;
if (!(configurecmd = malloc(sizeof(char *) + sizeof(char) * strlen(configurecmdParam ) + 1))) malloc_fail();
strcpy(configurecmd, configurecmdParam);
char *type;
if (!(type = malloc(sizeof(char *) + sizeof(char) * strlen(typeParam ) + 1))) malloc_fail();
strcpy(type, typeParam);
char *sepbuild;
if (!(sepbuild = malloc(sizeof(char *) + sizeof(char) * strlen(sepbuildParam ) + 1))) malloc_fail();
strcpy(sepbuild, sepbuildParam);
char *uninstallcmd;
if (!(uninstallcmd = malloc(sizeof(char *) + sizeof(char) * strlen(uninstallcmdParam ) + 1))) malloc_fail();
strcpy(uninstallcmd, uninstallcmdParam);
char *license;
if (!(license = malloc(sizeof(char *) + sizeof(char) * strlen(licenseParam ) + 1))) malloc_fail();
strcpy(license, licenseParam);
int *repoindex;
if (!(repoindex = malloc(sizeof(int *) + sizeof(int) ))) malloc_fail();
*repoindex = repoindexParam;
// copy every pointer to struct object
struct package retval = (struct package) {
.name = name ,
.description = description ,
.version = version ,
.archiveurl = archiveurl ,
.maintainer = maintainer ,
.configurecmd = configurecmd ,
.type = type ,
.sepbuild = sepbuild ,
.uninstallcmd = uninstallcmd ,
.license = license ,
.repoindex = repoindex
};
return retval;
}
struct strarr_retval split_space(char to_split[]) {
char *p = to_split;
int spacecount = 0;
// count spaces
for (int i = 0; to_split[i]; i++) {
if (to_split[i] == ' ') {
spacecount++;
}
}
// alloc memory
char **retval;
if (!(retval = malloc(sizeof(char *) * (spacecount + 1)))) malloc_fail();
// end at newline
ssize_t ln = strlen(p) - 1;
if (ln < 0) // underflow protection
ln = 0;
if (p[ln] == '\n' || p[ln] == '\r') // for CRLF
p[ln] = '\0';
// tokenize everything and put it into the array
int i = 0;
while (1) {
char *p2 = strchr(p, ' ');
if (p2 != NULL)
*p2 = '\0';
if (!(retval[i] = malloc(sizeof(char[64])))) malloc_fail();
// important, do not remove
strcpy(retval[i], p);
if (p2 == NULL)
break;
p = p2 + 1;
i++;
}
struct strarr_retval sarv = { .retval = retval, .retc = spacecount+1 };
return sarv;
}
void parse_csv_line(char line[], struct package* retval, int repoindex) {
// see split_space for explanation
char *p = line;
size_t ln = strlen(p) - 1;
char parsed[14][2048];
if (p[ln] == '\n')
p[ln] = '\0';
int index = 0;
while (1) {
char *p2 = strchr(p, ';');
if (p2 != NULL)
*p2 = '\0';
strcpy(parsed[index], p);
if (p2 == NULL)
break;
p = p2 + 1;
index++;
}
// construct the package
*retval = package_constructor(parsed[0],parsed[1],parsed[2],parsed[3],parsed[4],parsed[7],parsed[9],parsed[10],parsed[11],parsed[12],repoindex);
retval->depends = split_space(parsed[5]);
retval->conflicts = split_space(parsed[6]);
retval->configureopts = split_space(parsed[8]);
retval->scripts = split_space(parsed[13]);
}
struct pkglist *get_packages_from_repo(char reponame[], int repoindex) {
// init path variable
char path[strlen(INSTALLPREFIX)+25+strlen(reponame)];
strcpy(path, "");
strcat(path, INSTALLPREFIX);
strcat(path, "/etc/kawa.d/");
strcat(path, reponame);
strcat(path, ".packages.db");
FILE* indexfile = fopen(path, "r");
if (indexfile == NULL) {
perror("fopen");
exit(4);
}
// get file size (for malloc)
fseek(indexfile, 0L, SEEK_END);
int fsize = ftell(indexfile);
fseek(indexfile, 0L, SEEK_SET);
struct package **packages;
if (!(packages = malloc(sizeof(struct package*) + sizeof(char) * fsize))) malloc_fail();
int pkg_count = 0;
// parse every package
char buffer[4096];
while (fgets(buffer, 4096, (FILE*)indexfile) != NULL) {
struct package *newpkg;
if (!(newpkg = malloc(sizeof(struct package*) + (sizeof(char *) * 15) + sizeof(struct strarr_retval *) * 5))) malloc_fail();
parse_csv_line(buffer, newpkg, repoindex);
packages[pkg_count++] = newpkg;
}
// construct return value struct pkglist
struct pkglist *retval;
if (!(retval = malloc(sizeof(struct pkglist*) + sizeof(char) * fsize))) malloc_fail();
retval->pkg_count = pkg_count;
retval->packages = packages;
fclose(indexfile);
return retval;
}
struct pkglist *get_all_packages() {
// all repos together probably won't exceed 16MiB. If they do, I'll just release a kawa update making this limit bigger
// I just checked, 16MiB could cover more than 45,000 packages. Yeah, 16MiB is enough
struct package **packages;
if (!(packages = malloc(sizeof(struct pkglist*) + sizeof(char) * 4096 * 4096))) malloc_fail();
int pkg_count = 0;
long total_size = 0L;
int currepoidx = 0;
struct repository **repos;
if (!(repos = malloc(sizeof(struct repository*) * 8))) malloc_fail();
FILE *fp;
char reponame[127];
char repourl[511];
// init path variable
char path[strlen(INSTALLPREFIX)+23];
strcpy(path, "");
strcat(path, INSTALLPREFIX);
strcat(path, "/etc/kawa.d/repos.conf");
fp = fopen(path, "r");
if (fp == NULL) {
perror("fopen");
exit(4);
}
// go through every repo and parse it using get_packages_from_repo
while (fscanf(fp, "%126s %510s", reponame, repourl) != EOF) {
printf("Reading Repo %s...", reponame);
fflush(stdout);
struct pkglist *currepo = get_packages_from_repo(reponame, currepoidx);
// realloc more space if needed
if (currepoidx % 8 == 0 && currepoidx > 0) {
if (!(repos = realloc(repos, sizeof(struct repository*) * (currepoidx + 8)))) malloc_fail();
}
// set repolist variables
if (!(repos[currepoidx] = malloc(sizeof(struct repository*) + sizeof(struct repository)))) malloc_fail();
char *baseurl = str_replace(repourl, "packages.db", "");
strcpy(repos[currepoidx]->reponame, reponame);
strcpy(repos[currepoidx]->repourl, repourl);
strcpy(repos[currepoidx]->baseurl, baseurl);
free(baseurl);
// append packages to pkglist
printf(" Read %d packages\n", currepo->pkg_count);
for (int i = 0; i < currepo->pkg_count; i++) {
packages[pkg_count++] = currepo->packages[i];
total_size += sizeof(currepo->packages[i]);
}
currepoidx++;
}
fclose(fp);
// construct return value
struct pkglist *retval;
if (!(retval = malloc(sizeof(struct pkglist*) + total_size))) malloc_fail();
retval->pkg_count = pkg_count;
retval->packages = packages;
struct repolist *repolist;
if (!(repolist = malloc(sizeof(struct repolist) + sizeof(char) * (127 + 511) * 127))) malloc_fail();
repolist->repo_count = currepoidx;
repolist->repos = repos;
retval->repos = repolist;
return sort_package_list(retval);
}
struct pkglist *get_installed_packages() {
printf("Querying installed packages...\n");
return sort_package_list(get_packages_from_repo("Installed", 0));
}
int compare_strings(const void* a, const void* b) {
struct package *pkgA = *(struct package**)a;
struct package *pkgB = *(struct package**)b;
return strcmp(pkgA->name, pkgB->name);
}
struct pkglist *sort_package_list(struct pkglist *orig_pkglist) {
// this sorts the list of packages (to make a lot of things involving all pkgs vs installed pkgs a lot easier)
qsort(orig_pkglist->packages, orig_pkglist->pkg_count, sizeof(orig_pkglist->packages[0]), compare_strings);
return orig_pkglist;
}
void pkglist_free(struct pkglist *packages) {
for (int i = 0; i < packages->pkg_count; i++) {
// this should properly free a struct pkglist (i think?)
struct package *currpkg = packages->packages[i];
free(currpkg->name);
free(currpkg->description);
free(currpkg->version);
free(currpkg->archiveurl);
free(currpkg->maintainer);
free(currpkg->configurecmd);
free(currpkg->type);
free(currpkg->sepbuild);
free(currpkg->uninstallcmd);
free(currpkg->license);
free(currpkg->depends.retval);
free(currpkg->conflicts.retval);
free(currpkg->configureopts.retval);
free(currpkg->scripts.retval);
free(currpkg->repoindex);
free(currpkg);
}
}
int write_installed_packages(struct pkglist *installed, struct pkglist *database) {
// no sort, cause already and will be done on read
// speed should only be a problem when you literally install the entire
// chiyoko linux repo (and also I'll optimize this later)
// init path variable
char path[strlen(INSTALLPREFIX)+34];
strcpy(path, "");
strcat(path, INSTALLPREFIX);
strcat(path, "/etc/kawa.d/Installed.packages.db");
FILE* indexfile = fopen(path, "w");
if (indexfile == NULL) {
perror("fopen");
exit(4);
}
int retval = 0;
// iterate through all installed packages
for (int i = 0; i < installed->pkg_count; i++) {
struct package *currpkg = installed->packages[i];
// this could've been done by remove, if so, skip package
// no writing NULL pointers
if (currpkg == NULL)
continue;
// write the package to the index file
fprintf(indexfile, "%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s\n", currpkg->name, currpkg->description, currpkg->version, currpkg->archiveurl, database->repos->repos[*currpkg->repoindex]->reponame, whitespace_join(currpkg->depends), whitespace_join(currpkg->conflicts), currpkg->configurecmd, whitespace_join(currpkg->configureopts), currpkg->type, currpkg->sepbuild, currpkg->uninstallcmd, currpkg->license, whitespace_join(currpkg->scripts));
}
fclose(indexfile);
return retval;
}