commit 458b1f097096a8e28e998eb4038d0e2160bd27d8
parent 00e23c6d2b854497697af0a86b5151ef66afd2f4
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Wed, 10 Mar 2021 23:32:35 -0600
pkg.c: use manifest_* for pkg_remove
Diffstat:
M | Makefile | | | 2 | +- |
M | pkg.c | | | 41 | ++++++++++++++++++----------------------- |
2 files changed, 19 insertions(+), 24 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,2 +1,2 @@
all:
- c99 -Wall -Werror alt.c http.c kiss.c pkg.c sha256.c util.c -ltls
+ c99 -Wall -Werror alt.c http.c kiss.c manifest.c pkg.c sha256.c util.c -ltls
diff --git a/pkg.c b/pkg.c
@@ -10,6 +10,7 @@
#include "common.h"
#include "http.h"
+#include "manifest.h"
#include "pkg.h"
#include "sha256.h"
#include "util.h"
@@ -17,10 +18,11 @@
void
pkg_remove(char *name)
{
- char manifestpath[PATH_LEN], etcsumspath[PATH_LEN], line[PATH_LEN],
+ char manifestpath[PATH_LEN], etcsumspath[PATH_LEN],
etcsumstr[1024], etcsumhash[HASH_LEN], filehash[HASH_LEN];
- FILE *manifest, *etcsums = NULL;
- char *p;
+ FILE *etcsums = NULL;
+ struct manifest_t manifest;
+ char **line;
if (snprintf(manifestpath, PATH_LEN, "%s/%s/manifest",
KISS_INSTALLED, name) >= PATH_LEN) {
@@ -30,34 +32,27 @@ pkg_remove(char *name)
/* no length check needed: "etcsums" is shorter than "manifest" */
snprintf(etcsumspath, PATH_LEN, "%s/%s/etcsums", KISS_INSTALLED, name);
- if ((manifest = fopen(manifestpath, "r")) == NULL) {
+ if (manifest_open(&manifest, manifestpath) == -1) {
die("%s: failed to open manifest for %s", __func__, name);
}
if (access(etcsumspath, F_OK) == 0 && !(etcsums = fopen(etcsumspath, "r"))) {
- fclose(manifest);
+ manifest_close(&manifest);
die("%s: failed to open etcsums for %s", name);
}
- while(fgets(line, PATH_LEN, manifest)) {
- if ((p = strchr(line, '\n')) == NULL) {
- fclose(manifest);
- if (etcsums)
- fclose(etcsums);
- die("%s: path too long in manifest for %s", __func__, name);
- }
- *p = '\0';
-
- /* /etc files are dealt with specially */
- if (strncmp(line, "/etc/", sizeof("/etc/") - 1) == 0 && etcsums) {
+ for (line = manifest.lines; *line != NULL; line++) {
+ /* /etc files are dealt with specially
+ * we use "/etc/" to avoid matching the "/etc" directory */
+ if (etcsums && strncmp(*line, "/etc/", sizeof("/etc/") - 1) == 0) {
if (!fgets(etcsumstr, 1024, etcsums)) {
- fclose(manifest);
+ manifest_close(&manifest);
fclose(etcsums);
die("%s: bad etcsums for %s", __func__, name);
}
- if (sha256_file(line, filehash) == -1)
- die("%s: failed to hash %s", __func__, line);
+ if (sha256_file(*line, filehash) == -1)
+ die("%s: failed to hash %s", __func__, *line);
sha256_fromstr(etcsumhash, etcsumstr);
@@ -68,16 +63,16 @@ pkg_remove(char *name)
/* we do rmdir first, as unlink won't tell us whether a file cannot be
* removed because it's a directory. */
- if (rmdir(line) == -1) {
+ if (rmdir(*line) == -1) {
if (errno == ENOTEMPTY)
continue;
- if (errno == ENOTDIR && unlink(line) == -1)
- warn("%s: failed to remove %s:", __func__, line);
+ if (errno == ENOTDIR && unlink(*line) == -1)
+ warn("%s: failed to remove %s:", __func__, *line);
}
}
- fclose(manifest);
+ manifest_close(&manifest);
if (etcsums)
fclose(etcsums);
}