Index: stable/4/usr.sbin/pkg_install/create/perform.c =================================================================== --- stable/4/usr.sbin/pkg_install/create/perform.c (revision 84408) +++ stable/4/usr.sbin/pkg_install/create/perform.c (revision 84409) @@ -1,393 +1,394 @@ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* * 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. * * Jordan K. Hubbard * 18 July 1993 * * This is the main body of the create module. * */ #include "lib.h" #include "create.h" #include +#include #include #include #include #include static void sanity_check(void); static void make_dist(char *, char *, char *, Package *); static char *home; int pkg_perform(char **pkgs) { char *pkg = *pkgs; /* Only one arg to create */ char *cp; FILE *pkg_in, *fp; Package plist; int len; char *suf; int compress = TRUE; /* default is to compress packages */ /* Preliminary setup */ sanity_check(); if (Verbose && !PlistOnly) printf("Creating package %s\n", pkg); get_dash_string(&Comment); get_dash_string(&Desc); if (!strcmp(Contents, "-")) pkg_in = stdin; else { pkg_in = fopen(Contents, "r"); if (!pkg_in) { cleanup(0); errx(2, __FUNCTION__ ": unable to open contents file '%s' for input", Contents); } } plist.head = plist.tail = NULL; /* chop suffix off if already specified, remembering if we want to compress */ len = strlen(pkg); if (len > 4) { if (!strcmp(&pkg[len - 4], ".tgz")) { compress = TRUE; UseBzip2 = FALSE; pkg[len - 4] = '\0'; } else if (!strcmp(&pkg[len - 4], ".tar")) { compress = FALSE; UseBzip2 = FALSE; pkg[len - 4] = '\0'; } else if ((len > 5) && (!strcmp(&pkg[len - 5], ".tbz2"))) { compress = FALSE; UseBzip2 = TRUE; pkg[len - 5] = '\0'; } } if (UseBzip2) suf = "tbz2"; else if (compress) suf = "tgz"; else suf = "tar"; /* Add the origin if asked, at the top */ if (Origin) add_plist(&plist, PLIST_COMMENT, strconcat("ORIGIN:", Origin)); /* Stick the dependencies, if any, at the top */ if (Pkgdeps) { char **deps; int i; int ndeps = 0; if (Verbose && !PlistOnly) printf("Registering depends:"); /* Count number of dependencies */ for (cp = Pkgdeps; cp != NULL && *cp != '\0'; cp = strpbrk(++cp, " \t\n")) { ndeps++; } if (ndeps != 0) { /* Create easy to use NULL-terminated list */ deps = alloca(sizeof(*deps) * ndeps + 1); if (deps == NULL) { errx(2, "%s: alloca() failed", __FUNCTION__); /* Not reached */ } for (i = 0; Pkgdeps; i++) { cp = strsep(&Pkgdeps, " \t\n"); if (*cp) deps[i] = cp; } deps[ndeps] = NULL; sortdeps(deps); for (i = 0; i < ndeps; i++) { add_plist_top(&plist, PLIST_PKGDEP, deps[i]); if (Verbose && !PlistOnly) printf(" %s", deps[i]); } } if (Verbose && !PlistOnly) printf(".\n"); } /* If a SrcDir override is set, add it now */ if (SrcDir) { if (Verbose && !PlistOnly) printf("Using SrcDir value of %s\n", SrcDir); add_plist(&plist, PLIST_SRC, SrcDir); } /* Slurp in the packing list */ read_plist(&plist, pkg_in); /* Prefix should add an @cwd to the packing list */ if (Prefix) add_plist_top(&plist, PLIST_CWD, Prefix); /* * Run down the list and see if we've named it, if not stick in a name * at the top. */ if (find_plist(&plist, PLIST_NAME) == NULL) add_plist_top(&plist, PLIST_NAME, basename_of(pkg)); /* * We're just here for to dump out a revised plist for the FreeBSD ports * hack. It's not a real create in progress. */ if (PlistOnly) { check_list(home, &plist); write_plist(&plist, stdout); exit(0); } /* Make a directory to stomp around in */ home = make_playpen(PlayPen, 0); signal(SIGINT, cleanup); signal(SIGHUP, cleanup); /* Make first "real contents" pass over it */ check_list(home, &plist); (void) umask(022); /* * Make sure gen'ed directories, files don't have * group or other write bits. */ /* copy_plist(home, &plist); */ /* mark_plist(&plist); */ /* Now put the release specific items in */ add_plist(&plist, PLIST_CWD, "."); write_file(COMMENT_FNAME, Comment); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, COMMENT_FNAME); write_file(DESC_FNAME, Desc); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, DESC_FNAME); if (Install) { copy_file(home, Install, INSTALL_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, INSTALL_FNAME); } if (PostInstall) { copy_file(home, PostInstall, POST_INSTALL_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, POST_INSTALL_FNAME); } if (DeInstall) { copy_file(home, DeInstall, DEINSTALL_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, DEINSTALL_FNAME); } if (PostDeInstall) { copy_file(home, PostDeInstall, POST_DEINSTALL_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, POST_DEINSTALL_FNAME); } if (Require) { copy_file(home, Require, REQUIRE_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, REQUIRE_FNAME); } if (Display) { copy_file(home, Display, DISPLAY_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, DISPLAY_FNAME); add_plist(&plist, PLIST_DISPLAY, DISPLAY_FNAME); } if (Mtree) { copy_file(home, Mtree, MTREE_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, MTREE_FNAME); add_plist(&plist, PLIST_MTREE, MTREE_FNAME); } /* Finally, write out the packing list */ fp = fopen(CONTENTS_FNAME, "w"); if (!fp) { cleanup(0); errx(2, __FUNCTION__ ": can't open file %s for writing", CONTENTS_FNAME); } write_plist(&plist, fp); if (fclose(fp)) { cleanup(0); errx(2, __FUNCTION__ ": error while closing %s", CONTENTS_FNAME); } /* And stick it into a tar ball */ make_dist(home, pkg, suf, &plist); /* Cleanup */ free(Comment); free(Desc); free_plist(&plist); leave_playpen(); return TRUE; /* Success */ } static void make_dist(char *home, char *pkg, char *suffix, Package *plist) { char tball[FILENAME_MAX]; PackingList p; int ret; char *args[50]; /* Much more than enough. */ int nargs = 0; int pipefds[2]; FILE *totar; pid_t pid; char *cname; args[nargs++] = "tar"; /* argv[0] */ if (*pkg == '/') snprintf(tball, FILENAME_MAX, "%s.%s", pkg, suffix); else snprintf(tball, FILENAME_MAX, "%s/%s.%s", home, pkg, suffix); args[nargs++] = "-c"; args[nargs++] = "-f"; args[nargs++] = tball; if (strchr(suffix, 'z')) { /* Compress/gzip/bzip2? */ if (UseBzip2) { args[nargs++] = "-y"; cname = "bzip'd "; } else { args[nargs++] = "-z"; cname = "gzip'd "; } } else { cname = ""; } if (Dereference) args[nargs++] = "-h"; if (ExcludeFrom) { args[nargs++] = "-X"; args[nargs++] = ExcludeFrom; } args[nargs++] = "-T"; /* Take filenames from file instead of args. */ args[nargs++] = "-"; /* Use stdin for the file. */ args[nargs] = NULL; if (Verbose) printf("Creating %star ball in '%s'\n", cname, tball); /* Set up a pipe for passing the filenames, and fork off a tar process. */ if (pipe(pipefds) == -1) { cleanup(0); errx(2, __FUNCTION__ ": cannot create pipe"); } if ((pid = fork()) == -1) { cleanup(0); errx(2, __FUNCTION__ ": cannot fork process for tar"); } if (pid == 0) { /* The child */ dup2(pipefds[0], 0); close(pipefds[0]); close(pipefds[1]); execv("/usr/bin/tar", args); cleanup(0); errx(2, __FUNCTION__ ": failed to execute tar command"); } /* Meanwhile, back in the parent process ... */ close(pipefds[0]); if ((totar = fdopen(pipefds[1], "w")) == NULL) { cleanup(0); errx(2, __FUNCTION__ ": fdopen failed"); } fprintf(totar, "%s\n", CONTENTS_FNAME); fprintf(totar, "%s\n", COMMENT_FNAME); fprintf(totar, "%s\n", DESC_FNAME); if (Install) fprintf(totar, "%s\n", INSTALL_FNAME); if (PostInstall) fprintf(totar, "%s\n", POST_INSTALL_FNAME); if (DeInstall) fprintf(totar, "%s\n", DEINSTALL_FNAME); if (PostDeInstall) fprintf(totar, "%s\n", POST_DEINSTALL_FNAME); if (Require) fprintf(totar, "%s\n", REQUIRE_FNAME); if (Display) fprintf(totar, "%s\n", DISPLAY_FNAME); if (Mtree) fprintf(totar, "%s\n", MTREE_FNAME); for (p = plist->head; p; p = p->next) { if (p->type == PLIST_FILE) fprintf(totar, "%s\n", p->name); else if (p->type == PLIST_CWD || p->type == PLIST_SRC) fprintf(totar, "-C\n%s\n", p->name); else if (p->type == PLIST_IGNORE) p = p->next; } fclose(totar); wait(&ret); /* assume either signal or bad exit is enough for us */ if (ret) { cleanup(0); errx(2, __FUNCTION__ ": tar command failed with code %d", ret); } } static void sanity_check() { if (!Comment) { cleanup(0); errx(2, __FUNCTION__ ": required package comment string is missing (-c comment)"); } if (!Desc) { cleanup(0); errx(2, __FUNCTION__ ": required package description string is missing (-d desc)"); } if (!Contents) { cleanup(0); errx(2, __FUNCTION__ ": required package contents list is missing (-f [-]file)"); } } /* Clean up those things that would otherwise hang around */ void cleanup(int sig) { int in_cleanup = 0; if (!in_cleanup) { in_cleanup = 1; leave_playpen(); } if (sig) exit(1); } Index: stable/4/usr.sbin/pkg_install/delete/delete.h =================================================================== --- stable/4/usr.sbin/pkg_install/delete/delete.h (revision 84408) +++ stable/4/usr.sbin/pkg_install/delete/delete.h (revision 84409) @@ -1,35 +1,36 @@ /* $FreeBSD$ */ /* * 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. * * Jordan K. Hubbard * 18 July 1993 * * Include and define various things wanted by the delete command. * */ #ifndef _INST_DELETE_H_INCLUDE #define _INST_DELETE_H_INCLUDE extern char *Prefix; extern Boolean CleanDirs; extern Boolean Interactive; extern Boolean NoDeInstall; extern Boolean Force; +extern Boolean Recursive; extern char *Directory; extern char *PkgName; extern match_t MatchType; #endif /* _INST_DELETE_H_INCLUDE */ Index: stable/4/usr.sbin/pkg_install/delete/main.c =================================================================== --- stable/4/usr.sbin/pkg_install/delete/main.c (revision 84408) +++ stable/4/usr.sbin/pkg_install/delete/main.c (revision 84409) @@ -1,154 +1,159 @@ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* * * 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. * * Jordan K. Hubbard * 18 July 1993 * * This is the delete module. * */ #include #include #include #include "lib.h" #include "delete.h" -static char Options[] = "adDfGhinp:vx"; +static char Options[] = "adDfGhinp:rvx"; char *Prefix = NULL; Boolean CleanDirs = FALSE; Boolean Interactive = FALSE; Boolean NoDeInstall = FALSE; +Boolean Recursive = FALSE; match_t MatchType = MATCH_GLOB; static void usage __P((void)); int main(int argc, char **argv) { int ch, error; char **pkgs, **start; char *pkgs_split; char *tmp; struct stat stat_s; pkgs = start = argv; while ((ch = getopt(argc, argv, Options)) != -1) switch(ch) { case 'v': Verbose = TRUE; break; case 'f': Force = TRUE; break; case 'p': Prefix = optarg; break; case 'D': NoDeInstall = TRUE; break; case 'd': CleanDirs = TRUE; break; case 'n': Fake = TRUE; Verbose = TRUE; break; case 'a': MatchType = MATCH_ALL; break; case 'G': MatchType = MATCH_EXACT; break; case 'x': MatchType = MATCH_REGEX; break; case 'i': Interactive = TRUE; break; + case 'r': + Recursive = TRUE; + break; + case 'h': case '?': default: usage(); break; } argc -= optind; argv += optind; /* Get all the remaining package names, if any */ while (*argv) { /* Don't try to apply heuristics if arguments are regexs */ if (MatchType != MATCH_REGEX) while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) { *pkgs_split++ = '\0'; /* * If character after the '/' is alphanumeric, then we've found the * package name. Otherwise we've come across a trailing '/' and * need to continue our quest. */ if (isalpha(*pkgs_split) || ((MatchType == MATCH_GLOB) && \ strpbrk(pkgs_split, "*?[]") != NULL)) { *argv = pkgs_split; break; } } *pkgs++ = *argv++; } /* If no packages, yelp */ if (pkgs == start && MatchType != MATCH_ALL) warnx("missing package name(s)"), usage(); *pkgs = NULL; tmp = LOG_DIR; (void) stat(tmp, &stat_s); if (!Fake && getuid() && geteuid() != stat_s.st_uid) { if (!Force) errx(1, "you do not own %s, use -f to force", tmp); else warnx("you do not own %s (proceeding anyways)", tmp); } if ((error = pkg_perform(start)) != 0) { if (Verbose) warnx("%d package deletion(s) failed", error); return error; } else return 0; } static void usage() { fprintf(stderr, "%s\n%s\n", - "usage: pkg_delete [-dDfGinvx] [-p prefix] pkg-name ...", + "usage: pkg_delete [-dDfGinrvx] [-p prefix] pkg-name ...", " pkg_delete -a [flags]"); exit(1); } Index: stable/4/usr.sbin/pkg_install/delete/perform.c =================================================================== --- stable/4/usr.sbin/pkg_install/delete/perform.c (revision 84408) +++ stable/4/usr.sbin/pkg_install/delete/perform.c (revision 84409) @@ -1,336 +1,358 @@ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* * 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. * * Jordan K. Hubbard * 18 July 1993 * * This is the main body of the delete module. * */ #include #include "lib.h" #include "delete.h" static int pkg_do(char *); static void sanity_check(char *); static void undepend(PackingList, char *); static char LogDir[FILENAME_MAX]; int pkg_perform(char **pkgs) { - char **matched; - int i; + char **matched, **rb, **rbtmp; + int errcode, i, j; int err_cnt = 0; - int errcode; + struct reqr_by_entry *rb_entry; + struct reqr_by_head *rb_list; if (MatchType != MATCH_EXACT) { matched = matchinstalled(MatchType, pkgs, &errcode); if (errcode != 0) return 1; /* Not reached */ if (matched != NULL) pkgs = matched; else switch (MatchType) { case MATCH_GLOB: break; case MATCH_ALL: warnx("no packages installed"); return 0; case MATCH_REGEX: warnx("no packages match pattern(s)"); return 1; default: break; } } err_cnt += sortdeps(pkgs); for (i = 0; pkgs[i]; i++) { + if (Recursive == TRUE) { + errcode = requiredby(pkgs[i], &rb_list, FALSE, TRUE); + if (errcode < 0) { + err_cnt++; + } else if (errcode > 0) { + /* + * Copy values from the rb_list queue into argv-like NULL + * terminated list because requiredby() uses some static + * storage, while pkg_do() below will call this function, + * thus blowing our rb_list away. + */ + rbtmp = rb = alloca((errcode + 1) * sizeof(*rb)); + if (rb == NULL) { + warnx("%s(): alloca() failed", __FUNCTION__); + err_cnt++; + continue; + } + STAILQ_FOREACH(rb_entry, rb_list, link) { + *rbtmp = alloca(strlen(rb_entry->pkgname) + 1); + if (*rbtmp == NULL) { + warnx("%s(): alloca() failed", __FUNCTION__); + err_cnt++; + continue; + } + strcpy(*rbtmp, rb_entry->pkgname); + rbtmp++; + } + *rbtmp = NULL; + + err_cnt += sortdeps(rb); + for (j = 0; rb[j]; j++) + err_cnt += pkg_do(rb[j]); + } + } err_cnt += pkg_do(pkgs[i]); } return err_cnt; } static Package Plist; /* This is seriously ugly code following. Written very fast! */ static int pkg_do(char *pkg) { FILE *cfile; char home[FILENAME_MAX]; PackingList p; - char *tmp; int len; /* support for separate pre/post install scripts */ int new_m = 0; char pre_script[FILENAME_MAX] = DEINSTALL_FNAME; char post_script[FILENAME_MAX]; char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX]; + struct reqr_by_entry *rb_entry; + struct reqr_by_head *rb_list; if (!pkg || !(len = strlen(pkg))) return 1; if (pkg[len - 1] == '/') pkg[len - 1] = '\0'; /* Reset some state */ if (Plist.head) free_plist(&Plist); sprintf(LogDir, "%s/%s", LOG_DIR, pkg); if (!fexists(LogDir)) { warnx("no such package '%s' installed", pkg); return 1; } if (!getcwd(home, FILENAME_MAX)) { cleanup(0); errx(2, __FUNCTION__ ": unable to get current working directory!"); } if (chdir(LogDir) == FAIL) { warnx("unable to change directory to %s! deinstall failed", LogDir); return 1; } if (Interactive == TRUE) { int first, ch; (void)fprintf(stderr, "delete %s? ", pkg); (void)fflush(stderr); first = ch = getchar(); while (ch != '\n' && ch != EOF) ch = getchar(); if (first != 'y' && first != 'Y') return 0; /* Not reached */ } - if (!isemptyfile(REQUIRED_BY_FNAME)) { - char buf[512]; - warnx("package `%s' is required by these other packages\n" - "and may not be deinstalled%s:", - pkg, Force ? " (but I'll delete it anyway)" : "" ); - cfile = fopen(REQUIRED_BY_FNAME, "r"); - if (cfile) { - while (fgets(buf, sizeof(buf), cfile)) - fprintf(stderr, "%s", buf); - fclose(cfile); - } else - warnx("cannot open requirements file `%s'", REQUIRED_BY_FNAME); + if (requiredby(pkg, &rb_list, FALSE, TRUE) < 0) + return 1; + if (!STAILQ_EMPTY(rb_list)) { + warnx("package '%s' is required by these other packages\n" + "and may not be deinstalled%s:", + pkg, Force ? " (but I'll delete it anyway)" : ""); + STAILQ_FOREACH(rb_entry, rb_list, link) + fprintf(stderr, "%s\n", rb_entry->pkgname); if (!Force) return 1; } sanity_check(LogDir); cfile = fopen(CONTENTS_FNAME, "r"); if (!cfile) { warnx("unable to open '%s' file", CONTENTS_FNAME); return 1; } /* If we have a prefix, add it now */ if (Prefix) add_plist(&Plist, PLIST_CWD, Prefix); read_plist(&Plist, cfile); fclose(cfile); p = find_plist(&Plist, PLIST_CWD); if (!p) { warnx("package '%s' doesn't have a prefix", pkg); return 1; } setenv(PKG_PREFIX_VNAME, p->name, 1); if (fexists(REQUIRE_FNAME)) { if (Verbose) printf("Executing 'require' script.\n"); vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */ if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) { warnx("package %s fails requirements %s", pkg, Force ? "" : "- not deleted"); if (!Force) return 1; } } /* * Test whether to use the old method of passing tokens to deinstallation * scripts, and set appropriate variables.. */ if (fexists(POST_DEINSTALL_FNAME)) { new_m = 1; sprintf(post_script, "%s", POST_DEINSTALL_FNAME); pre_arg[0] = '\0'; post_arg[0] = '\0'; } else { if (fexists(DEINSTALL_FNAME)) { sprintf(post_script, "%s", DEINSTALL_FNAME); sprintf(pre_arg, "DEINSTALL"); sprintf(post_arg, "POST-DEINSTALL"); } } if (!NoDeInstall && fexists(pre_script)) { if (Fake) printf("Would execute de-install script at this point.\n"); else { vsystem("chmod +x %s", pre_script); /* make sure */ if (vsystem("./%s %s %s", pre_script, pkg, pre_arg)) { warnx("deinstall script returned error status"); if (!Force) return 1; } } } if (chdir(home) == FAIL) { cleanup(0); errx(2, __FUNCTION__ ": unable to return to working directory %s!", home); } /* * Some packages aren't packed right, so we need to just ignore * delete_package()'s status. Ugh! :-( */ if (delete_package(FALSE, CleanDirs, &Plist) == FAIL) warnx( "couldn't entirely delete package (perhaps the packing list is\n" "incorrectly specified?)"); if (chdir(LogDir) == FAIL) { warnx("unable to change directory to %s! deinstall failed", LogDir); return 1; } if (!NoDeInstall && fexists(post_script)) { if (Fake) printf("Would execute post-deinstall script at this point.\n"); else { vsystem("chmod +x %s", post_script); /* make sure */ if (vsystem("./%s %s %s", post_script, pkg, post_arg)) { warnx("post-deinstall script returned error status"); if (!Force) return 1; } } } if (chdir(home) == FAIL) { cleanup(0); errx(2, __FUNCTION__ ": unable to return to working directory %s!", home); } if (!Fake) { if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) { warnx("couldn't remove log entry in %s, deinstall failed", LogDir); if (!Force) return 1; } } for (p = Plist.head; p ; p = p->next) { if (p->type != PLIST_PKGDEP) continue; if (Verbose) printf("Attempting to remove dependency on package `%s'\n", p->name); if (!Fake) undepend(p, pkg); } return 0; } static void sanity_check(char *pkg) { if (!fexists(CONTENTS_FNAME)) { cleanup(0); errx(2, __FUNCTION__ ": installed package %s has no %s file!", pkg, CONTENTS_FNAME); } } void cleanup(int sig) { if (sig) exit(1); } static void undepend(PackingList p, char *pkgname) { - char fname[FILENAME_MAX], ftmp[FILENAME_MAX]; - char fbuf[FILENAME_MAX]; - FILE *fp, *fpwr; - char *tmp; - int s; + char fname[FILENAME_MAX], ftmp[FILENAME_MAX]; + FILE *fpwr; + int s; + struct reqr_by_entry *rb_entry; + struct reqr_by_head *rb_list; - sprintf(fname, "%s/%s/%s", LOG_DIR, p->name, REQUIRED_BY_FNAME); - fp = fopen(fname, "r"); - if (fp == NULL) { - warnx("couldn't open dependency file `%s'", fname); - return; - } - sprintf(ftmp, "%s.XXXXXX", fname); - s = mkstemp(ftmp); - if (s == -1) { - fclose(fp); - warnx("couldn't open temp file `%s'", ftmp); - return; - } - fpwr = fdopen(s, "w"); - if (fpwr == NULL) { - close(s); - fclose(fp); - warnx("couldn't fdopen temp file `%s'", ftmp); - remove(ftmp); - return; - } - while (fgets(fbuf, sizeof(fbuf), fp) != NULL) { - if (fbuf[strlen(fbuf)-1] == '\n') - fbuf[strlen(fbuf)-1] = '\0'; - if (strcmp(fbuf, pkgname)) /* no match */ - fputs(fbuf, fpwr), putc('\n', fpwr); - } - (void) fclose(fp); - if (fchmod(s, 0644) == FAIL) { - warnx("error changing permission of temp file `%s'", ftmp); - fclose(fpwr); - remove(ftmp); - return; - } - if (fclose(fpwr) == EOF) { - warnx("error closing temp file `%s'", ftmp); - remove(ftmp); - return; - } - if (rename(ftmp, fname) == -1) - warnx("error renaming `%s' to `%s'", ftmp, fname); - remove(ftmp); /* just in case */ - return; -} + if (requiredby(p->name, &rb_list, Verbose, FALSE) <= 0) + return; + snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p->name, + REQUIRED_BY_FNAME); + snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname); + s = mkstemp(ftmp); + if (s == -1) { + warnx("couldn't open temp file '%s'", ftmp); + return; + } + fpwr = fdopen(s, "w"); + if (fpwr == NULL) { + close(s); + warnx("couldn't fdopen temp file '%s'", ftmp); + goto cleanexit; + } + STAILQ_FOREACH(rb_entry, rb_list, link) + if (strcmp(rb_entry->pkgname, pkgname)) /* no match */ + fputs(rb_entry->pkgname, fpwr), putc('\n', fpwr); + if (fchmod(s, 0644) == FAIL) { + warnx("error changing permission of temp file '%s'", ftmp); + fclose(fpwr); + goto cleanexit; + } + if (fclose(fpwr) == EOF) { + warnx("error closing temp file '%s'", ftmp); + goto cleanexit; + } + if (rename(ftmp, fname) == -1) + warnx("error renaming '%s' to '%s'", ftmp, fname); +cleanexit: + remove(ftmp); + return; +} Index: stable/4/usr.sbin/pkg_install/delete/pkg_delete.1 =================================================================== --- stable/4/usr.sbin/pkg_install/delete/pkg_delete.1 (revision 84408) +++ stable/4/usr.sbin/pkg_install/delete/pkg_delete.1 (revision 84409) @@ -1,271 +1,274 @@ .\" .\" 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. .\" .\" Jordan K. Hubbard .\" .\" .\" @(#)pkg_delete.1 .\" $FreeBSD$ .\" .Dd November 25, 1994 .Dt PKG_DELETE 1 .Os .Sh NAME .Nm pkg_delete .Nd a utility for deleting previously installed software package distributions .Sh SYNOPSIS .Nm -.Op Fl dDfGinvx +.Op Fl dDfGinrvx .Op Fl p Ar prefix .Ar pkg-name ... .Nm .Fl a .Op Ar flags .Sh DESCRIPTION The .Nm command is used to delete packages that have been previously installed with the .Xr pkg_add 1 command. .Sh WARNING .Bf -emphasis Since the .Nm command may execute scripts or programs provided by a package file, your system may be susceptible to .Dq Em trojan horses or other subtle attacks from miscreants who create dangerous package files. .Pp You are advised to verify the competence and identity of those who provide installable package files. For extra protection, examine all the package control files in the package record directory .Pa ( /var/db/pkg// ) . Pay particular attention to any +INSTALL, +POST-INSTALL, +DEINSTALL, +POST-DEINSTALL, +REQUIRE or +MTREE_DIRS files, and inspect the +CONTENTS file for .Cm @cwd , .Cm @mode (check for setuid), .Cm @dirrm , .Cm @exec , and .Cm @unexec directives, and/or use the .Xr pkg_info 1 command to examine the installed package control files. .Ef .Sh OPTIONS The following command line options are supported: .Bl -tag -width indent .It Ar pkg-name ... The named packages are deinstalled. .It Fl a Unconditionally delete all currently installed packages. .It Fl i Request confirmation before attempting to delete each package, regardless whether or not the standard input device is a terminal. .It Fl v Turn on verbose output. .It Fl D If a deinstallation script exists for a given package, do not execute it. .It Fl n Don't actually deinstall a package, just report the steps that would be taken if it were. .It Fl p Ar prefix Set .Ar prefix as the directory in which to delete files from any installed packages which do not explicitly set theirs. For most packages, the prefix will be set automatically to the installed location by .Xr pkg_add 1 . .It Fl d Remove empty directories created by file cleanup. By default, only files/directories explicitly listed in a package's contents (either as normal files/directories or with the .Cm @dirrm directive) will be removed at deinstallation time. This option tells .Nm to also remove any directories that were emptied as a result of removing the package. .It Fl f Force removal of the package, even if a dependency is recorded or the deinstall or require script fails. .It Fl G Do not try to expand shell glob patterns in the .Ar pkg-name when selecting packages to be deleted (by default .Nm automatically expands shell glob patterns in the .Ar pkg-name ) . .It Fl x Treat the .Ar pkg-name as a regular expression and delete all packages whose names match that regular expression. Multiple regular expressions could be provided, in that case .Nm deletes all packages that match at least one regular expression from the list. +.It Fl r +Recursive removal. In addition to specified packages, delete all +packages that depend on those packages as well. .El .Sh TECHNICAL DETAILS .Nm does pretty much what it says. It examines installed package records in .Pa /var/db/pkg/ , deletes the package contents, and finally removes the package records. If the environment variable .Ev PKG_DBDIR is set, this overrides the .Pa /var/db/pkg/ path shown above. .Pp If a package is required by other installed packages, .Nm will list those dependent packages and refuse to delete the package (unless the .Fl f option is given). .Pp If the package contains a .Ar require file (see .Xr pkg_create 1 ) , then this is executed first as .Bd -ragged -offset indent -compact .Cm require .Ar .Ar DEINSTALL .Ed (where .Ar pkg-name is the name of the package in question and .Ar DEINSTALL is a keyword denoting that this is a deinstallation) to see whether or not deinstallation should continue. A non-zero exit status means no, unless the .Fl f option is specified. .Pp If a .Cm deinstall script exists for the package, it is executed before any files are removed. It is this script's responsibility to clean up any additional messy details around the package's installation, since all .Nm knows how to do is delete the files created in the original distribution. The .Nm deinstall script is called as: .Bd -ragged -offset indent -compact .Cm script .Ar .Ar DEINSTALL .Ed where .Ar pkg-name is the name of the package in question and .Ar DEINSTALL is a keyword denoting this as the pre-deinstallation phase. .Pp .Sy Note : The .Ar DEINSTALL keyword will not appear if separate scripts for deinstall and post-deinstall are given during package creation time (using the .Fl k and .Fl K flags to .Xr pkg_create 1 ) . .Pp If a .Cm post-deinstall script exists for the package, it is executed .Cm after all files are removed. It is this script's responsibility to clean up any additional messy details around the package's installation, and leave the system (hopefully) in the same state that it was prior to the installation of the package. .Pp The .Nm post-deinstall script is called as: .Bd -ragged -offset indent -compact .Cm script .Ar .Ar POST-DEINSTALL .Ed where .Ar pkg-name is the name of the package in question and .Ar POST-DEINSTALL is a keyword denoting this as the post-deinstallation phase. .Pp .Sy Note : The .Ar POST-DEINSTALL keyword will not appear if separate scripts for deinstall and post-deinstall are given during package creation time (using the .Fl k and .Fl K flags to .Xr pkg_create 1 ) . .Pp Reasoning behind passing keywords such as .Ar DEINSTALL and .Ar POST-DEINSTALL is that it lets you potentially write only one program/script that handles all aspects of installation and deletion. .Pp But experience has proved that this is a lot more difficult to maintain and is not as advantageous as having separate scripts that handle each aspect of installation and deinstallation. .Pp All scripts are called with the environment variable .Ev PKG_PREFIX set to the installation prefix (see the .Fl p option above). This allows a package author to write a script that reliably performs some action on the directory where the package is installed, even if the user might have changed it by specifying the .Fl p option when running .Nm or .Cm pkg_add . .Sh ENVIRONMENT The environment variable .Ev PKG_DBDIR specifies an alternative location for the installed package database. .Sh FILES .Bl -tag -width /var/db/pkg -compact .It Pa /var/db/pkg Default location of the installed package database. .El .Sh SEE ALSO .Xr pkg_add 1 , .Xr pkg_create 1 , .Xr pkg_info 1 , .Xr pkg_version 1 , .Xr mktemp 3 , .Xr mtree 8 .Sh AUTHORS .An Jordan Hubbard .Sh CONTRIBUTORS .An John Kohl Aq jtk@rational.com .Sh BUGS Sure to be some. Index: stable/4/usr.sbin/pkg_install/info/perform.c =================================================================== --- stable/4/usr.sbin/pkg_install/info/perform.c (revision 84408) +++ stable/4/usr.sbin/pkg_install/info/perform.c (revision 84409) @@ -1,415 +1,413 @@ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* * 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. * * Jordan K. Hubbard * 23 Aug 1993 * * This is the main body of the info module. * */ #include "lib.h" #include "info.h" #include #include static int pkg_do(char *); static int find_pkg(char *, struct which_head *); static int cmp_path(const char *, const char *, const char *); int pkg_perform(char **pkgs) { char **matched; char *tmp; int err_cnt = 0; int i, errcode; signal(SIGINT, cleanup); tmp = LOG_DIR; /* Overriding action? */ if (CheckPkg) { char buf[FILENAME_MAX]; snprintf(buf, FILENAME_MAX, "%s/%s", tmp, CheckPkg); return abs(access(buf, R_OK)); /* Not reached */ } else if (!TAILQ_EMPTY(whead)) { return find_pkg(tmp, whead); } if (MatchType != MATCH_EXACT) { matched = matchinstalled(MatchType, pkgs, &errcode); if (errcode != 0) return 1; /* Not reached */ if (matched != NULL) pkgs = matched; else switch (MatchType) { case MATCH_GLOB: break; case MATCH_ALL: warnx("no packages installed"); return 0; /* Not reached */ case MATCH_REGEX: warnx("no packages match pattern(s)"); return 1; /* Not reached */ default: break; } } for (i = 0; pkgs[i]; i++) err_cnt += pkg_do(pkgs[i]); return err_cnt; } static char *Home; static int pkg_do(char *pkg) { Boolean installed = FALSE, isTMP = FALSE; char log_dir[FILENAME_MAX]; char fname[FILENAME_MAX]; Package plist; FILE *fp; struct stat sb; char *cp = NULL; int code = 0; if (isURL(pkg)) { if ((cp = fileGetURL(NULL, pkg)) != NULL) { strcpy(fname, cp); isTMP = TRUE; } } else if (fexists(pkg) && isfile(pkg)) { int len; if (*pkg != '/') { if (!getcwd(fname, FILENAME_MAX)) upchuck("getcwd"); len = strlen(fname); snprintf(&fname[len], FILENAME_MAX - len, "/%s", pkg); } else strcpy(fname, pkg); cp = fname; } else { if ((cp = fileFindByPath(NULL, pkg)) != NULL) strncpy(fname, cp, FILENAME_MAX); } if (cp) { /* * Apply a crude heuristic to see how much space the package will * take up once it's unpacked. I've noticed that most packages * compress an average of 75%, but we're only unpacking the + files so * be very optimistic. */ if (stat(fname, &sb) == FAIL) { warnx("can't stat package file '%s'", fname); code = 1; goto bail; } Home = make_playpen(PlayPen, sb.st_size / 2); if (unpack(fname, "+*")) { warnx("error during unpacking, no info for '%s' available", pkg); code = 1; goto bail; } } /* It's not an ininstalled package, try and find it among the installed */ else { - char *tmp; - sprintf(log_dir, "%s/%s", LOG_DIR, pkg); if (!fexists(log_dir)) { warnx("can't find package `%s' installed or in a file!", pkg); return 1; } if (chdir(log_dir) == FAIL) { warnx("can't change directory to '%s'!", log_dir); return 1; } installed = TRUE; } /* Suck in the contents list */ plist.head = plist.tail = NULL; fp = fopen(CONTENTS_FNAME, "r"); if (!fp) { warnx("unable to open %s file", CONTENTS_FNAME); code = 1; goto bail; } /* If we have a prefix, add it now */ read_plist(&plist, fp); fclose(fp); /* * Index is special info type that has to override all others to make * any sense. */ if (Flags & SHOW_INDEX) { char tmp[FILENAME_MAX]; snprintf(tmp, FILENAME_MAX, "%-19s ", pkg); show_index(tmp, COMMENT_FNAME); } else { /* Start showing the package contents */ if (!Quiet) printf("%sInformation for %s:\n\n", InfoPrefix, pkg); if (Flags & SHOW_COMMENT) show_file("Comment:\n", COMMENT_FNAME); if (Flags & SHOW_REQUIRE) show_plist("Depends on:\n", &plist, PLIST_PKGDEP); if ((Flags & SHOW_REQBY) && !isemptyfile(REQUIRED_BY_FNAME)) show_file("Required by:\n", REQUIRED_BY_FNAME); if (Flags & SHOW_DESC) show_file("Description:\n", DESC_FNAME); if ((Flags & SHOW_DISPLAY) && fexists(DISPLAY_FNAME)) show_file("Install notice:\n", DISPLAY_FNAME); if (Flags & SHOW_PLIST) show_plist("Packing list:\n", &plist, (plist_t)-1); if ((Flags & SHOW_INSTALL) && fexists(INSTALL_FNAME)) show_file("Install script:\n", INSTALL_FNAME); if ((Flags & SHOW_INSTALL) && fexists(POST_INSTALL_FNAME)) show_file("Post-Install script:\n", POST_INSTALL_FNAME); if ((Flags & SHOW_DEINSTALL) && fexists(DEINSTALL_FNAME)) show_file("De-Install script:\n", DEINSTALL_FNAME); if ((Flags & SHOW_DEINSTALL) && fexists(POST_DEINSTALL_FNAME)) show_file("Post-DeInstall script:\n", POST_DEINSTALL_FNAME); if ((Flags & SHOW_MTREE) && fexists(MTREE_FNAME)) show_file("mtree file:\n", MTREE_FNAME); if (Flags & SHOW_PREFIX) show_plist("Prefix(s):\n", &plist, PLIST_CWD); if (Flags & SHOW_FILES) show_files("Files:\n", &plist); if ((Flags & SHOW_SIZE) && installed) show_size("Package Size:\n", &plist); if ((Flags & SHOW_CKSUM) && installed) show_cksum("Mismatched Checksums:\n", &plist); if (Flags & SHOW_ORIGIN) show_origin("Origin:\n", &plist); if (!Quiet) puts(InfoPrefix); } free_plist(&plist); bail: leave_playpen(); if (isTMP) unlink(fname); return code; } void cleanup(int sig) { static int in_cleanup = 0; if (!in_cleanup) { in_cleanup = 1; leave_playpen(); } if (sig) exit(1); } /* * Return an absolute path, additionally removing all .'s, ..'s, and extraneous * /'s, as realpath() would, but without resolving symlinks, because that can * potentially screw up our comparisons later. */ char * abspath(const char *pathname) { char *tmp, *tmp1, *resolved_path; char *cwd = NULL; int len; if (pathname[0] != '/') { cwd = getcwd(NULL, MAXPATHLEN); asprintf(&resolved_path, "%s/%s/", cwd, pathname); } else asprintf(&resolved_path, "%s/", pathname); if (resolved_path == NULL) errx(2, NULL); if (cwd != NULL) free(cwd); while ((tmp = strstr(resolved_path, "//")) != NULL) strcpy(tmp, tmp + 1); while ((tmp = strstr(resolved_path, "/./")) != NULL) strcpy(tmp, tmp + 2); while ((tmp = strstr(resolved_path, "/../")) != NULL) { *tmp = '\0'; if ((tmp1 = strrchr(resolved_path, '/')) == NULL) tmp1 = resolved_path; strcpy(tmp1, tmp + 3); } len = strlen(resolved_path); if (len > 1 && resolved_path[len - 1] == '/') resolved_path[len - 1] = '\0'; return resolved_path; } /* * Comparison to see if the path we're on matches the * one we are looking for. */ static int cmp_path(const char *target, const char *current, const char *cwd) { char *resolved, *temp; int rval; asprintf(&temp, "%s/%s", cwd, current); if (temp == NULL) errx(2, NULL); /* * Make sure there's no multiple /'s or other weird things in the PLIST, * since some plists seem to have them and it could screw up our strncmp. */ resolved = abspath(temp); if (strcmp(target, resolved) == 0) rval = 1; else rval = 0; free(temp); free(resolved); return rval; } /* * Look through package dbs in db_dir and find which * packages installed the files in which_list. */ static int find_pkg(char *db_dir, struct which_head *which_list) { char **installed; int errcode, i; struct which_entry *wp; TAILQ_FOREACH(wp, which_list, next) { char *msg = "file cannot be found"; char *tmp; wp->skip = TRUE; /* If it's not a file, we'll see if it's an executable. */ if (isfile(wp->file) == FALSE) { if (strchr(wp->file, '/') == NULL) { tmp = vpipe("/usr/bin/which %s", wp->file); if (tmp != NULL) { strlcpy(wp->file, tmp, PATH_MAX); wp->skip = FALSE; free(tmp); } else msg = "file is not in PATH"; } } else { tmp = abspath(wp->file); if (isfile(tmp)) { strlcpy(wp->file, tmp, PATH_MAX); wp->skip = FALSE; } free(tmp); } if (wp->skip == TRUE) warnx("%s: %s", wp->file, msg); } installed = matchinstalled(MATCH_ALL, NULL, &errcode); if (installed == NULL) return errcode; for (i = 0; installed[i] != NULL; i++) { FILE *fp; Package pkg; PackingList itr; char *cwd = NULL; char tmp[PATH_MAX]; snprintf(tmp, PATH_MAX, "%s/%s/%s", db_dir, installed[i], CONTENTS_FNAME); fp = fopen(tmp, "r"); if (fp == NULL) { warn("%s", tmp); return 1; } pkg.head = pkg.tail = NULL; read_plist(&pkg, fp); fclose(fp); for (itr = pkg.head; itr != pkg.tail; itr = itr->next) { if (itr->type == PLIST_CWD) { cwd = itr->name; } else if (itr->type == PLIST_FILE) { TAILQ_FOREACH(wp, which_list, next) { if (wp->skip == TRUE) continue; if (!cmp_path(wp->file, itr->name, cwd)) continue; if (wp->package[0] != '\0') { warnx("both %s and %s claim to have installed %s\n", wp->package, installed[i], wp->file); } else { strlcpy(wp->package, installed[i], PATH_MAX); } } } } free_plist(&pkg); } TAILQ_FOREACH(wp, which_list, next) { if (wp->package[0] != '\0') { if (Quiet) puts(wp->package); else printf("%s was installed by package %s\n", \ wp->file, wp->package); } } while (!TAILQ_EMPTY(which_list)) { wp = TAILQ_FIRST(which_list); TAILQ_REMOVE(which_list, wp, next); free(wp); } free(which_list); return 0; } Index: stable/4/usr.sbin/pkg_install/lib/deps.c =================================================================== --- stable/4/usr.sbin/pkg_install/lib/deps.c (revision 84408) +++ stable/4/usr.sbin/pkg_install/lib/deps.c (revision 84409) @@ -1,110 +1,179 @@ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* * 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. * */ #include "lib.h" #include #include /* * 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. */ int sortdeps(char **pkgs) { char *tmp; int i, j, loop_cnt; int err_cnt = 0; for (i = 0; pkgs[i]; i++) { /* * Check to see if any other package in pkgs[i+1:] depends * on pkgs[i] and swap those two packages if so. */ loop_cnt = 0; for (j = i + 1; pkgs[j]; j++) { if (chkifdepends(pkgs[j], pkgs[i]) == 1) { /* * 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. * Use ugly but simple method, becase it Should Never * Happen[tm] in the real life anyway. */ if (loop_cnt > 4096) { warnx("dependency loop detected for package %s", pkgs[j]); err_cnt++; break; } loop_cnt++; tmp = pkgs[i]; pkgs[i] = pkgs[j]; pkgs[j] = tmp; /* * Another iteration requred to check if new pkgs[i] * itself has any packages that depend on it */ j = i + 1; } } } return err_cnt; } /* * Check to see if pkgname1 depends on pkgname2. * Returns 1 if depends, 0 if not, and -1 if error occured. */ 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); - sprintf(fname, "%s/%s/%s", LOG_DIR, pkgname2, REQUIRED_BY_FNAME); + *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); + } + + 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; } Index: stable/4/usr.sbin/pkg_install/lib/lib.h =================================================================== --- stable/4/usr.sbin/pkg_install/lib/lib.h (revision 84408) +++ stable/4/usr.sbin/pkg_install/lib/lib.h (revision 84409) @@ -1,196 +1,204 @@ /* $FreeBSD$ */ /* * 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. * * Jordan K. Hubbard * 18 July 1993 * * Include and define various things wanted by the library routines. * */ #ifndef _INST_LIB_LIB_H_ #define _INST_LIB_LIB_H_ /* Includes */ #include #include #include +#include #include #include #include #include #include #include #include /* Macros */ #define SUCCESS (0) #define FAIL (-1) #ifndef TRUE #define TRUE (1) #endif #ifndef FALSE #define FALSE (0) #endif #define YES 2 #define NO 1 /* Usually "rm", but often "echo" during debugging! */ #define REMOVE_CMD "rm" /* Usually "rm", but often "echo" during debugging! */ #define RMDIR_CMD "rmdir" /* Where we put logging information by default, else ${PKG_DBDIR} if set */ #define DEF_LOG_DIR "/var/db/pkg" /* just in case we change the environment variable name */ #define PKG_DBDIR "PKG_DBDIR" /* macro to get name of directory where we put logging information */ #define LOG_DIR (getenv(PKG_DBDIR) ? getenv(PKG_DBDIR) : DEF_LOG_DIR) /* The names of our "special" files */ #define CONTENTS_FNAME "+CONTENTS" #define COMMENT_FNAME "+COMMENT" #define DESC_FNAME "+DESC" #define INSTALL_FNAME "+INSTALL" #define POST_INSTALL_FNAME "+POST-INSTALL" #define DEINSTALL_FNAME "+DEINSTALL" #define POST_DEINSTALL_FNAME "+POST-DEINSTALL" #define REQUIRE_FNAME "+REQUIRE" #define REQUIRED_BY_FNAME "+REQUIRED_BY" #define DISPLAY_FNAME "+DISPLAY" #define MTREE_FNAME "+MTREE_DIRS" #define CMD_CHAR '@' /* prefix for extended PLIST cmd */ /* The name of the "prefix" environment variable given to scripts */ #define PKG_PREFIX_VNAME "PKG_PREFIX" enum _plist_t { PLIST_FILE, PLIST_CWD, PLIST_CMD, PLIST_CHMOD, PLIST_CHOWN, PLIST_CHGRP, PLIST_COMMENT, PLIST_IGNORE, PLIST_NAME, PLIST_UNEXEC, PLIST_SRC, PLIST_DISPLAY, PLIST_PKGDEP, PLIST_MTREE, PLIST_DIR_RM, PLIST_IGNORE_INST, PLIST_OPTION }; typedef enum _plist_t plist_t; enum _match_t { MATCH_ALL, MATCH_EXACT, MATCH_GLOB, MATCH_REGEX }; typedef enum _match_t match_t; /* Types */ typedef unsigned int Boolean; struct _plist { struct _plist *prev, *next; char *name; Boolean marked; plist_t type; }; typedef struct _plist *PackingList; struct _pack { struct _plist *head, *tail; }; 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 *, ...); char *vpipe(const char *, ...); void cleanup(int); char *make_playpen(char *, size_t); char *where_playpen(void); void leave_playpen(void); off_t min_free(char *); /* String */ char *get_dash_string(char **); char *copy_string(char *); Boolean suffix(char *, char *); void nuke_suffix(char *); void str_lowercase(char *); char *basename_of(char *); char *strconcat(char *, char *); char *get_string(char *, int, FILE *); int s_strlcpy(char *, const char *, size_t); int s_strlcat(char *, const char *, size_t); /* File */ Boolean fexists(char *); Boolean isdir(char *); Boolean isemptydir(char *fname); Boolean isemptyfile(char *fname); Boolean isfile(char *); Boolean isempty(char *); Boolean issymlink(char *); Boolean isURL(char *); char *fileGetURL(char *, char *); char *fileFindByPath(char *, char *); char *fileGetContents(char *); void write_file(char *, char *); void copy_file(char *, char *, char *); void move_file(char *, char *, char *); void copy_hierarchy(char *, char *, Boolean); int delete_hierarchy(char *, Boolean, Boolean); int unpack(char *, char *); void format_cmd(char *, char *, char *, char *); /* Msg */ void upchuck(const char *); void barf(const char *, ...); void whinge(const char *, ...); Boolean y_or_n(Boolean, const char *, ...); /* Packing list */ PackingList new_plist_entry(void); PackingList last_plist(Package *); PackingList find_plist(Package *, plist_t); char *find_plist_option(Package *, char *name); void plist_delete(Package *, Boolean, plist_t, char *); void free_plist(Package *); void mark_plist(Package *); void csum_plist_entry(char *, PackingList); void add_plist(Package *, plist_t, char *); void add_plist_top(Package *, plist_t, char *); void delete_plist(Package *pkg, Boolean all, plist_t type, char *name); void write_plist(Package *, FILE *); void read_plist(Package *, FILE *); int plist_cmd(char *, char **); int delete_package(Boolean, Boolean, Package *); Boolean make_preserve_name(char *, int, char *, char *); /* For all */ int pkg_perform(char **); /* Query installed packages */ 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; extern Boolean Fake; extern Boolean Force; extern int AutoAnswer; #endif /* _INST_LIB_LIB_H_ */