commit a5b7f756594e4d1e039a594f171d53a8eb851b87
parent 3b49ce26d9debcf435f7f1ed26ea6bdadbe24d5e
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Wed, 17 Feb 2021 14:04:03 -0600
pkg.*: add pkg_exists to check whether a package exists in a repo
Diffstat:
M | pkg.c | | | 28 | ++++++++++++++++++++++++++++ |
M | pkg.h | | | 2 | ++ |
2 files changed, 30 insertions(+), 0 deletions(-)
diff --git a/pkg.c b/pkg.c
@@ -1,4 +1,5 @@
#include <dirent.h>
+#include <errno.h>
#include <fnmatch.h>
#include <stdlib.h>
#include <stdio.h>
@@ -64,3 +65,30 @@ pkg_match(char *repo, char pkgs[][PKG_NAME_MAX], const char *pattern)
return index;
}
+
+/* check if package name exists in repository
+ * path must be at least PATH_LEN length */
+int
+pkg_exists(const char *repo, const char *name, char *path)
+{
+ DIR *pkg;
+ int found = 0;
+
+ if (strlen(repo) + strlen(name) > PATH_LEN)
+ die("%s: repo path length + package name > max path length", __func__);
+
+ strcpy(path, repo);
+ strcat(path, "/");
+ strcat(path, name);
+
+ if ((pkg = opendir(path)) == NULL) {
+ if (errno == ENOENT) {
+ path[0] = '\0';
+ return 0;
+ } else
+ die("%s:", __func__);
+ }
+
+ closedir(pkg);
+ return 1;
+}
diff --git a/pkg.h b/pkg.h
@@ -2,6 +2,8 @@
#define PKG_VERSION_MAX 256
#define PKG_COUNT_MAX 1024
#define PKG_NAME_MAX 64
+#define PATH_LEN 512
void pkg_version(char *repo, char *name, char *ver);
int pkg_match(char *repo, char pkgs[][64], const char *pattern);
+int pkg_exists(const char *repo, const char *name, char *path);