aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_install/lib
diff options
context:
space:
mode:
authorMaxim Sobolev <sobomax@FreeBSD.org>2001-09-19 08:06:48 +0000
committerMaxim Sobolev <sobomax@FreeBSD.org>2001-09-19 08:06:48 +0000
commita09c8e438beefd4171416a7f0deb0f7ccef88d1b (patch)
tree983c446512bc26a2723c915739303dcac0da1570 /usr.sbin/pkg_install/lib
parent096f9ef53b08c9e982d7026efbd4567a5ba0fd03 (diff)
Notes
Diffstat (limited to 'usr.sbin/pkg_install/lib')
-rw-r--r--usr.sbin/pkg_install/lib/deps.c89
-rw-r--r--usr.sbin/pkg_install/lib/lib.h8
2 files changed, 87 insertions, 10 deletions
diff --git a/usr.sbin/pkg_install/lib/deps.c b/usr.sbin/pkg_install/lib/deps.c
index 802d6c8de790..cd5220957ee1 100644
--- a/usr.sbin/pkg_install/lib/deps.c
+++ b/usr.sbin/pkg_install/lib/deps.c
@@ -82,29 +82,98 @@ sortdeps(char **pkgs)
int
chkifdepends(char *pkgname1, char *pkgname2)
{
+ char pkgdir[FILENAME_MAX];
+ int errcode;
+ struct reqr_by_entry *rb_entry;
+ struct reqr_by_head *rb_list;
+
+ /* Check that pkgname2 is actually installed */
+ snprintf(pkgdir, sizeof(pkgdir), "%s/%s", LOG_DIR, pkgname2);
+ if (!isdir(pkgdir))
+ return 0;
+
+ errcode = requiredby(pkgname2, &rb_list, FALSE, TRUE);
+ if (errcode < 0)
+ return errcode;
+
+ STAILQ_FOREACH(rb_entry, rb_list, link)
+ if (strcmp(rb_entry->pkgname, pkgname1) == 0) /* match */
+ return 1;
+
+ return 0;
+}
+
+/*
+ * Load +REQUIRED_BY file and return a list with names of
+ * packages that require package reffered to by `pkgname'.
+ *
+ * Optionally check that packages listed there are actually
+ * installed and filter out those that don't (filter == TRUE).
+ *
+ * strict argument controls whether the caller want warnings
+ * to be emitted when there are some non-fatal conditions,
+ * i.e. package doesn't have +REQUIRED_BY file or some packages
+ * listed in +REQUIRED_BY don't exist.
+ *
+ * Result returned in the **list, while return value is equal
+ * to the number of entries in the resulting list. Print error
+ * message and return -1 on error.
+ */
+int
+requiredby(const char *pkgname, struct reqr_by_head **list, Boolean strict, Boolean filter)
+{
FILE *fp;
- char fname[FILENAME_MAX];
- char fbuf[FILENAME_MAX];
- char *tmp;
+ char fbuf[FILENAME_MAX], fname[FILENAME_MAX], pkgdir[FILENAME_MAX];
int retval;
+ struct reqr_by_entry *rb_entry;
+ static struct reqr_by_head rb_list = STAILQ_HEAD_INITIALIZER(rb_list);
+
+ *list = &rb_list;
+ /* Deallocate any previously allocated space */
+ while (!STAILQ_EMPTY(&rb_list)) {
+ rb_entry = STAILQ_FIRST(&rb_list);
+ STAILQ_REMOVE_HEAD(&rb_list, link);
+ free(rb_entry);
+ }
- sprintf(fname, "%s/%s/%s", LOG_DIR, pkgname2, REQUIRED_BY_FNAME);
+ snprintf(fname, sizeof(fname), "%s/%s", LOG_DIR, pkgname);
+ if (!isdir(fname)) {
+ if (strict == TRUE)
+ warnx("no such package '%s' installed", pkgname);
+ return -1;
+ }
+
+ snprintf(fname, sizeof(fname), "%s/%s", fname, REQUIRED_BY_FNAME);
fp = fopen(fname, "r");
if (fp == NULL) {
- /* Probably pkgname2 doesn't have any packages that depend on it */
+ /* Probably pkgname doesn't have any packages that depend on it */
+ if (strict == TRUE)
+ warnx("couldn't open dependency file '%s'", fname);
return 0;
}
retval = 0;
while (fgets(fbuf, sizeof(fbuf), fp) != NULL) {
- if (fbuf[strlen(fbuf)-1] == '\n')
- fbuf[strlen(fbuf)-1] = '\0';
- if (strcmp(fbuf, pkgname1) == 0) { /* match */
- retval = 1;
+ if (fbuf[strlen(fbuf) - 1] == '\n')
+ fbuf[strlen(fbuf) - 1] = '\0';
+ snprintf(pkgdir, sizeof(pkgdir), "%s/%s", LOG_DIR, fbuf);
+ if (filter == TRUE && !isdir(pkgdir)) {
+ if (strict == TRUE)
+ warnx("package '%s' is recorded in the '%s' but isn't "
+ "actually installed", fbuf, fname);
+ continue;
+ }
+ retval++;
+ rb_entry = malloc(sizeof(*rb_entry));
+ if (rb_entry == NULL) {
+ warnx("%s(): malloc() failed", __FUNCTION__);
+ retval = -1;
break;
}
+ strlcpy(rb_entry->pkgname, fbuf, sizeof(rb_entry->pkgname));
+ STAILQ_INSERT_TAIL(&rb_list, rb_entry, link);
}
-
fclose(fp);
+
return retval;
}
diff --git a/usr.sbin/pkg_install/lib/lib.h b/usr.sbin/pkg_install/lib/lib.h
index fc41c66fdfbc..ee709cd4a7b2 100644
--- a/usr.sbin/pkg_install/lib/lib.h
+++ b/usr.sbin/pkg_install/lib/lib.h
@@ -27,6 +27,7 @@
#include <sys/param.h>
#include <sys/file.h>
#include <sys/stat.h>
+#include <sys/queue.h>
#include <ctype.h>
#include <dirent.h>
#include <stdarg.h>
@@ -111,6 +112,12 @@ struct _pack {
};
typedef struct _pack Package;
+struct reqr_by_entry {
+ STAILQ_ENTRY(reqr_by_entry) link;
+ char pkgname[PATH_MAX];
+};
+STAILQ_HEAD(reqr_by_head, reqr_by_entry);
+
/* Prototypes */
/* Misc */
int vsystem(const char *, ...);
@@ -183,6 +190,7 @@ char **matchinstalled(match_t, char **, int *);
/* Dependencies */
int sortdeps(char **);
int chkifdepends(char *, char *);
+int requiredby(const char *, struct reqr_by_head **, Boolean, Boolean);
/* Externs */
extern Boolean Verbose;