nspkg / tests / coracle / legacy_md5_reference.h
44 lines · 43 sloc · 1.61 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1/* package.c and util.c immediately before pacman bdedf621 (GPL-2.0-or-later).
2 * the removed package fields are parameters; public accessors replace private
3 * handle access. hashing remains the installed C library's implementation. */
4#ifndef V_ALPM_LEGACY_MD5_REFERENCE_H
5#define V_ALPM_LEGACY_MD5_REFERENCE_H
6#include <alpm.h>
7#include <limits.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/stat.h>
12
13static char *v_alpm_legacy_cache_find(alpm_handle_t *handle, const char *filename) {
14 char path[PATH_MAX];
15 struct stat buf;
16 alpm_list_t *i;
17 for(i = alpm_option_get_cachedirs(handle); i; i = i->next) {
18 snprintf(path, PATH_MAX, "%s%s", (char *)i->data, filename);
19 if(stat(path, &buf) == 0 && S_ISREG(buf.st_mode)) { return strdup(path); }
20 }
21 return NULL;
22}
23static int v_alpm_legacy_test_md5(const char *filepath, const char *expected) {
24 char *computed = alpm_compute_md5sum(filepath);
25 int ret;
26 if(expected == NULL || computed == NULL) { ret = -1; }
27 else if(strcmp(expected, computed) != 0) { ret = 1; }
28 else { ret = 0; }
29 free(computed);
30 return ret;
31}
32static int v_alpm_legacy_checkmd5sum(alpm_handle_t *handle, int origin,
33 const char *filename, const char *md5sum, int *error) {
34 char *fpath;
35 int retval;
36 *error = ALPM_ERR_OK;
37 if(origin != ALPM_PKG_FROM_SYNCDB) { *error = ALPM_ERR_WRONG_ARGS; return -1; }
38 fpath = v_alpm_legacy_cache_find(handle, filename);
39 retval = v_alpm_legacy_test_md5(fpath, md5sum);
40 free(fpath);
41 if(retval == 1) { *error = ALPM_ERR_PKG_INVALID; retval = -1; }
42 return retval;
43}
44#endif
45