commit 6ec4de00a7557cab85281761a2f5221686a429a8
parent eb63651200bb9dc67b704fda62fca8dbad467991
Author: aabacchus <ben@bvnf.space>
Date: Sun, 23 Apr 2023 23:12:41 +0100
bad update
Diffstat:
7 files changed, 76 insertions(+), 2 deletions(-)
diff --git a/README b/README
@@ -15,7 +15,7 @@ An implementation of the kiss package manager in C.
[x] list
[ ] remove
[x] search
-[ ] update
+[.] update
[ ] upgrade
[x] version
[ ] external tools
diff --git a/src/Makefile b/src/Makefile
@@ -2,7 +2,7 @@
XCFLAGS = $(CFLAGS) -Wall -Wextra -Wshadow -pedantic -D_XOPEN_SOURCE=700 -Og -g
-OBJS = utils.o list.o search.o array.o checksum.o checksums.o pkg.o
+OBJS = utils.o list.o search.o array.o checksum.o checksums.o pkg.o update.o
LIBS = -lblake3
all: ckiss
@@ -35,4 +35,5 @@ array.o: array.h utils.h
checksum.o: actions.h pkg.h utils.h checksums.h
checksums.o: utils.h checksums.h
pkg.o: pkg.h utils.h
+update.o: actions.h utils.h
main.o: actions.h utils.h
diff --git a/src/actions.h b/src/actions.h
@@ -5,4 +5,5 @@
int list(int argc, char **argv, struct env *e);
int search(int argc, char **argv, struct env *e);
int checksum(int argc, char **argv, struct env *e);
+int update(struct env *e);
#endif
diff --git a/src/main.c b/src/main.c
@@ -12,6 +12,7 @@ usage(int r) {
mylog("checksum Generate checksums");
mylog("list List installed packages");
mylog("search Search for packages");
+ mylog("update Update the repositories");
mylog("version Package manager version");
exit(r);
}
@@ -34,6 +35,9 @@ main(int argc, char **argv) {
case 's':
ret = search(argc - 1, argv + 1, e);
break;
+ case 'u':
+ ret = update(e);
+ break;
case 'v':
printf("%s\n", KISS_VERSION);
break;
diff --git a/src/update.c b/src/update.c
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+
+#include "utils.h"
+#include "actions.h"
+
+/* TODO: use libgit2 */
+
+void
+repo_try_print_motd(char *repo) {
+ char *fn = concat(repo, "/MOTD", NULL);
+ FILE *f = fopen(fn, "r");
+ if (f != NULL) {
+ int c;
+ while ((c = fgetc(f)) != EOF)
+ putchar(c);
+ fclose(f);
+ }
+ free(fn);
+}
+
+int
+update(struct env *e) {
+ for (int i = 0; e->kiss_path[i] != NULL; i++) {
+ char *repo = e->kiss_path[i];
+ mylog(repo);
+ /* TODO need to get root of git repo, ideally only pull once each, and use that for motd. */
+
+ /*
+ char *buf;
+ int n = snprintf(NULL, 0, "git -C \"%s\" pull", repo);
+ buf = malloc(n+1);
+ if (buf == NULL)
+ die_perror("malloc");
+ snprintf(buf, n+1, "git -C \"%s\" pull", repo);
+ */
+
+ char *cmd[] = {"git", "-C", repo, "pull", NULL};
+ if (run(cmd) != 0)
+ die_perror(repo);
+
+ repo_try_print_motd(repo);
+ }
+ return 0;
+}
diff --git a/src/utils.c b/src/utils.c
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
@@ -356,3 +357,23 @@ destroy_env(struct env *e) {
free(e->pid);
free(e);
}
+
+int
+run(char *argv[]) {
+ pid_t pid = fork();
+ int stat_loc;
+
+ switch (pid) {
+ case -1:
+ die_perror("fork");
+ return 1;
+ case 0:
+ execvp(argv[0], argv);
+
+ break;
+ default:
+ waitpid(pid, &stat_loc, 0);
+ return WEXITSTATUS(stat_loc);
+ }
+ return 0;
+}
diff --git a/src/utils.h b/src/utils.h
@@ -65,4 +65,5 @@ struct env *setup_env(void);
/* correctly frees a struct env. */
void destroy_env(struct env *e);
+int run(char *argv[]);
#endif