2001-03-15 10:47:00 +00:00
|
|
|
/*
|
|
|
|
* FreeBSD install - a package for the installation and maintainance
|
|
|
|
* of non-core utilities.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Maxim Sobolev
|
|
|
|
* 14 March 2001
|
|
|
|
*
|
|
|
|
* Routines used to do various operations with dependencies
|
|
|
|
* among installed packages.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-04-01 09:39:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-03-15 10:47:00 +00:00
|
|
|
#include "lib.h"
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2007-06-18 22:49:13 +00:00
|
|
|
void list_deps(const char *pkgname, char **pkgs, char *listed,
|
|
|
|
char *check_loop, char **newpkgs, int *nrnewpkgs,
|
|
|
|
int *err_cnt);
|
|
|
|
|
2001-03-15 10:47:00 +00:00
|
|
|
/*
|
|
|
|
* Sort given NULL-terminated list of installed packages (pkgs) in
|
|
|
|
* such a way that if package A depends on package B then after
|
|
|
|
* sorting A will be listed before B no matter how they were
|
|
|
|
* originally positioned in the list.
|
2007-06-18 22:49:13 +00:00
|
|
|
*
|
|
|
|
* Works by performing a recursive depth-first search on the
|
|
|
|
* required-by lists.
|
2001-03-15 10:47:00 +00:00
|
|
|
*/
|
2007-06-18 22:49:13 +00:00
|
|
|
|
2001-03-15 10:47:00 +00:00
|
|
|
int
|
|
|
|
sortdeps(char **pkgs)
|
|
|
|
{
|
2007-06-18 22:49:13 +00:00
|
|
|
int i, err_cnt=0;
|
|
|
|
int nrpkgs, nrnewpkgs;
|
|
|
|
char *listed, *check_loop, **newpkgs;
|
|
|
|
char *cp;
|
2001-03-15 10:47:00 +00:00
|
|
|
|
2002-02-20 17:15:17 +00:00
|
|
|
if (pkgs[0] == NULL || pkgs[1] == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2007-06-18 22:49:13 +00:00
|
|
|
nrpkgs = 0;
|
|
|
|
while (pkgs[nrpkgs]) nrpkgs++;
|
|
|
|
listed = alloca(nrpkgs);
|
|
|
|
if (listed == NULL) {
|
|
|
|
warnx("%s(): alloca() failed", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
bzero(listed,nrpkgs);
|
|
|
|
check_loop = alloca(nrpkgs);
|
|
|
|
if (check_loop == NULL) {
|
|
|
|
warnx("%s(): alloca() failed", __func__);
|
|
|
|
return 1;
|
2001-03-15 10:47:00 +00:00
|
|
|
}
|
2007-06-18 22:49:13 +00:00
|
|
|
bzero(check_loop,nrpkgs);
|
|
|
|
newpkgs = alloca(nrpkgs*sizeof(char*));
|
|
|
|
if (newpkgs == NULL) {
|
|
|
|
warnx("%s(): alloca() failed", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
nrnewpkgs = 0;
|
|
|
|
|
|
|
|
for (i = 0; pkgs[i]; i++) if (!listed[i]) {
|
|
|
|
check_loop[i] = 1;
|
|
|
|
cp = strchr(pkgs[i], ':');
|
|
|
|
if (cp != NULL)
|
|
|
|
*cp = '\0';
|
|
|
|
list_deps(pkgs[i],pkgs,listed,check_loop,newpkgs,&nrnewpkgs,&err_cnt);
|
|
|
|
if (cp != NULL)
|
|
|
|
*cp = ':';
|
|
|
|
listed[i] = 1;
|
|
|
|
newpkgs[nrnewpkgs] = pkgs[i];
|
|
|
|
nrnewpkgs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nrnewpkgs != nrpkgs) {
|
|
|
|
fprintf(stderr,"This shouldn't happen, and indicates a huge error in the code.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for (i = 0; i < nrnewpkgs; i++) pkgs[i] = newpkgs[i];
|
|
|
|
|
2001-03-15 10:47:00 +00:00
|
|
|
return err_cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-06-18 22:49:13 +00:00
|
|
|
* This recursive function lists the dependencies (that is, the
|
|
|
|
* "required-by"s) for pkgname, putting them into newpkgs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void list_deps(const char *pkgname, char **pkgs, char *listed,
|
|
|
|
char *check_loop, char **newpkgs, int *nrnewpkgs,
|
|
|
|
int *err_cnt) {
|
|
|
|
char **rb, **rbtmp;
|
|
|
|
char *cp;
|
|
|
|
int errcode, i, j;
|
2001-09-19 08:06:48 +00:00
|
|
|
struct reqr_by_entry *rb_entry;
|
|
|
|
struct reqr_by_head *rb_list;
|
|
|
|
|
2007-06-18 22:49:13 +00:00
|
|
|
if (isinstalledpkg(pkgname) <= 0)
|
|
|
|
return;
|
2001-09-19 08:06:48 +00:00
|
|
|
|
2007-06-18 22:49:13 +00:00
|
|
|
errcode = requiredby(pkgname, &rb_list, FALSE, TRUE);
|
2001-09-19 08:06:48 +00:00
|
|
|
if (errcode < 0)
|
2007-06-18 22:49:13 +00:00
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* We put rb_list into an argv style NULL terminated list,
|
|
|
|
* because requiredby uses some static storage, and list_deps
|
|
|
|
* is a recursive function.
|
|
|
|
*/
|
2001-09-19 08:06:48 +00:00
|
|
|
|
2007-06-18 22:49:13 +00:00
|
|
|
rbtmp = rb = alloca((errcode + 1) * sizeof(*rb));
|
|
|
|
if (rb == NULL) {
|
|
|
|
warnx("%s(): alloca() failed", __func__);
|
|
|
|
(*err_cnt)++;
|
|
|
|
return;
|
|
|
|
}
|
2002-05-05 21:03:25 +00:00
|
|
|
STAILQ_FOREACH(rb_entry, rb_list, link) {
|
2007-06-18 22:49:13 +00:00
|
|
|
*rbtmp = alloca(strlen(rb_entry->pkgname) + 1);
|
|
|
|
if (*rbtmp == NULL) {
|
|
|
|
warnx("%s(): alloca() failed", __func__);
|
|
|
|
(*err_cnt)++;
|
|
|
|
return;
|
2002-05-05 21:03:25 +00:00
|
|
|
}
|
2007-06-18 22:49:13 +00:00
|
|
|
strcpy(*rbtmp, rb_entry->pkgname);
|
|
|
|
rbtmp++;
|
2002-05-05 21:03:25 +00:00
|
|
|
}
|
2007-06-18 22:49:13 +00:00
|
|
|
*rbtmp = NULL;
|
2001-09-19 08:06:48 +00:00
|
|
|
|
2007-06-18 22:49:13 +00:00
|
|
|
for (i = 0; rb[i]; i++)
|
|
|
|
for (j = 0; pkgs[j]; j++) if (!listed[j]) {
|
|
|
|
cp = strchr(pkgs[j], ':');
|
|
|
|
if (cp != NULL)
|
|
|
|
*cp = '\0';
|
|
|
|
if (strcmp(rb[i], pkgs[j]) == 0) { /*match */
|
|
|
|
/*
|
|
|
|
* Try to avoid deadlock if package A depends on B which in
|
|
|
|
* turn depends on C and C due to an error depends on A.
|
|
|
|
* It Should Never Happen[tm] in real life.
|
|
|
|
*/
|
|
|
|
if (check_loop[j]) {
|
|
|
|
warnx("dependency loop detected for package %s", pkgs[j]);
|
|
|
|
(*err_cnt)++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
check_loop[j] = 1;
|
|
|
|
list_deps(pkgs[j],pkgs,listed,check_loop,newpkgs,nrnewpkgs,err_cnt);
|
|
|
|
listed[j] = 1;
|
|
|
|
newpkgs[*nrnewpkgs] = pkgs[j];
|
|
|
|
(*nrnewpkgs)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cp != NULL)
|
|
|
|
*cp = ':';
|
|
|
|
}
|
2001-09-19 08:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2001-03-15 10:47:00 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2002-05-14 21:42:37 +00:00
|
|
|
char fbuf[FILENAME_MAX], fname[FILENAME_MAX];
|
2001-03-15 10:47:00 +00:00
|
|
|
int retval;
|
2001-09-19 08:06:48 +00:00
|
|
|
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);
|
|
|
|
}
|
2001-03-15 10:47:00 +00:00
|
|
|
|
2004-06-29 18:59:19 +00:00
|
|
|
if (isinstalledpkg(pkgname) <= 0) {
|
2001-09-19 08:06:48 +00:00
|
|
|
if (strict == TRUE)
|
|
|
|
warnx("no such package '%s' installed", pkgname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-05-14 21:42:37 +00:00
|
|
|
snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, pkgname,
|
|
|
|
REQUIRED_BY_FNAME);
|
2001-03-15 10:47:00 +00:00
|
|
|
fp = fopen(fname, "r");
|
|
|
|
if (fp == NULL) {
|
2001-09-19 08:06:48 +00:00
|
|
|
/* Probably pkgname doesn't have any packages that depend on it */
|
|
|
|
if (strict == TRUE)
|
|
|
|
warnx("couldn't open dependency file '%s'", fname);
|
2001-03-15 10:47:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = 0;
|
|
|
|
while (fgets(fbuf, sizeof(fbuf), fp) != NULL) {
|
2001-09-19 08:06:48 +00:00
|
|
|
if (fbuf[strlen(fbuf) - 1] == '\n')
|
|
|
|
fbuf[strlen(fbuf) - 1] = '\0';
|
2004-06-29 18:59:19 +00:00
|
|
|
if (filter == TRUE && isinstalledpkg(fbuf) <= 0) {
|
2001-09-19 08:06:48 +00:00
|
|
|
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) {
|
2002-05-11 04:17:55 +00:00
|
|
|
warnx("%s(): malloc() failed", __func__);
|
2001-09-19 08:06:48 +00:00
|
|
|
retval = -1;
|
2001-03-15 10:47:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-09-19 08:06:48 +00:00
|
|
|
strlcpy(rb_entry->pkgname, fbuf, sizeof(rb_entry->pkgname));
|
|
|
|
STAILQ_INSERT_TAIL(&rb_list, rb_entry, link);
|
2001-03-15 10:47:00 +00:00
|
|
|
}
|
|
|
|
fclose(fp);
|
2001-09-19 08:06:48 +00:00
|
|
|
|
2001-03-15 10:47:00 +00:00
|
|
|
return retval;
|
|
|
|
}
|