2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
17 * This is the main body of the delete module.
19 * $FreeBSD: src/usr.sbin/pkg_install/delete/perform.c,v 1.22.2.12 2003/02/28 13:58:14 des Exp $
20 * $DragonFly: src/usr.sbin/pkg_install/delete/Attic/perform.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
27 static int pkg_do(char *);
28 static void sanity_check(char *);
29 static void undepend(char *, char *);
30 static char LogDir[FILENAME_MAX];
34 pkg_perform(char **pkgs)
36 char **matched, **rb, **rbtmp;
39 struct reqr_by_entry *rb_entry;
40 struct reqr_by_head *rb_list;
42 if (MatchType != MATCH_EXACT) {
43 matched = matchinstalled(MatchType, pkgs, &errcode);
49 * Copy matched[] into pkgs[], because we'll need to use
50 * matchinstalled() later on.
52 if (matched != NULL) {
54 for (i = 0; matched[i] != NULL; i++) {
55 pkgs = realloc(pkgs, sizeof(*pkgs) * (i + 2));
56 pkgs[i] = strdup(matched[i]);
60 else switch (MatchType) {
64 warnx("no packages installed");
67 warnx("no packages match pattern(s)");
74 err_cnt += sortdeps(pkgs);
75 for (i = 0; pkgs[i]; i++) {
76 if (Recursive == TRUE) {
77 errcode = requiredby(pkgs[i], &rb_list, FALSE, TRUE);
80 } else if (errcode > 0) {
82 * Copy values from the rb_list queue into argv-like NULL
83 * terminated list because requiredby() uses some static
84 * storage, while pkg_do() below will call this function,
85 * thus blowing our rb_list away.
87 rbtmp = rb = alloca((errcode + 1) * sizeof(*rb));
89 warnx("%s(): alloca() failed", __func__);
93 STAILQ_FOREACH(rb_entry, rb_list, link) {
94 *rbtmp = alloca(strlen(rb_entry->pkgname) + 1);
96 warnx("%s(): alloca() failed", __func__);
100 strcpy(*rbtmp, rb_entry->pkgname);
105 err_cnt += sortdeps(rb);
106 for (j = 0; rb[j]; j++)
107 err_cnt += pkg_do(rb[j]);
110 err_cnt += pkg_do(pkgs[i]);
116 static Package Plist;
118 /* This is seriously ugly code following. Written very fast! */
123 char *deporigin, **depnames, home[FILENAME_MAX];
126 /* support for separate pre/post install scripts */
128 const char *pre_script = DEINSTALL_FNAME;
129 const char *post_script, *pre_arg, *post_arg;
130 struct reqr_by_entry *rb_entry;
131 struct reqr_by_head *rb_list;
133 if (!pkg || !(len = strlen(pkg)))
135 if (pkg[len - 1] == '/')
138 /* Reset some state */
142 if (!isinstalledpkg(pkg)) {
143 warnx("no such package '%s' installed", pkg);
147 if (!getcwd(home, FILENAME_MAX)) {
149 errx(2, "%s: unable to get current working directory!", __func__);
152 sprintf(LogDir, "%s/%s", LOG_DIR, pkg);
154 if (chdir(LogDir) == FAIL) {
155 warnx("unable to change directory to %s! deinstall failed", LogDir);
159 if (Interactive == TRUE) {
162 (void)fprintf(stderr, "delete %s? ", pkg);
163 (void)fflush(stderr);
164 first = ch = getchar();
165 while (ch != '\n' && ch != EOF)
167 if (first != 'y' && first != 'Y')
172 if (requiredby(pkg, &rb_list, FALSE, TRUE) < 0)
174 if (!STAILQ_EMPTY(rb_list)) {
175 warnx("package '%s' is required by these other packages\n"
176 "and may not be deinstalled%s:",
177 pkg, Force ? " (but I'll delete it anyway)" : "");
178 STAILQ_FOREACH(rb_entry, rb_list, link)
179 fprintf(stderr, "%s\n", rb_entry->pkgname);
184 sanity_check(LogDir);
185 cfile = fopen(CONTENTS_FNAME, "r");
188 warnx("unable to open '%s' file", CONTENTS_FNAME);
192 /* If we have a prefix, add it now */
194 add_plist(&Plist, PLIST_CWD, Prefix);
195 read_plist(&Plist, cfile);
197 p = find_plist(&Plist, PLIST_CWD);
200 warnx("package '%s' doesn't have a prefix", pkg);
204 setenv(PKG_PREFIX_VNAME, p->name, 1);
206 if (fexists(REQUIRE_FNAME)) {
208 printf("Executing 'require' script.\n");
209 vsystem("chmod +x %s", REQUIRE_FNAME); /* be sure */
210 if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) {
211 warnx("package %s fails requirements %s", pkg,
212 Force ? "" : "- not deleted");
219 * Test whether to use the old method of passing tokens to deinstallation
220 * scripts, and set appropriate variables..
223 if (fexists(POST_DEINSTALL_FNAME)) {
225 post_script = POST_DEINSTALL_FNAME;
226 pre_arg = post_arg = "";
227 } else if (fexists(DEINSTALL_FNAME)) {
228 post_script = DEINSTALL_FNAME;
229 pre_arg = "DEINSTALL";
230 post_arg = "POST-DEINSTALL";
232 post_script = pre_arg = post_arg = NULL;
235 if (!NoDeInstall && pre_script != NULL && fexists(pre_script)) {
237 printf("Would execute de-install script at this point.\n");
239 vsystem("chmod +x %s", pre_script); /* make sure */
240 if (vsystem("./%s %s %s", pre_script, pkg, pre_arg)) {
241 warnx("deinstall script returned error status");
248 if (chdir(home) == FAIL) {
250 errx(2, "%s: unable to return to working directory %s!", __func__,
255 * Some packages aren't packed right, so we need to just ignore
256 * delete_package()'s status. Ugh! :-(
258 if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
260 "couldn't entirely delete package (perhaps the packing list is\n"
261 "incorrectly specified?)");
263 if (chdir(LogDir) == FAIL) {
264 warnx("unable to change directory to %s! deinstall failed", LogDir);
268 if (!NoDeInstall && post_script != NULL && fexists(post_script)) {
270 printf("Would execute post-deinstall script at this point.\n");
272 vsystem("chmod +x %s", post_script); /* make sure */
273 if (vsystem("./%s %s %s", post_script, pkg, post_arg)) {
274 warnx("post-deinstall script returned error status");
281 if (chdir(home) == FAIL) {
283 errx(2, "%s: unable to return to working directory %s!", __func__,
288 if (vsystem("%s -r%c %s", REMOVE_CMD, Force ? 'f' : ' ', LogDir)) {
289 warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
295 for (p = Plist.head; p ; p = p->next) {
296 if (p->type != PLIST_PKGDEP)
298 deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
301 printf("Trying to remove dependency on package '%s'", p->name);
302 if (deporigin != NULL)
303 printf(" with '%s' origin", deporigin);
307 depnames = (deporigin != NULL) ? matchbyorigin(deporigin, NULL) :
309 if (depnames == NULL) {
310 depnames = alloca(sizeof(*depnames) * 2);
311 depnames[0] = p->name;
314 for (i = 0; depnames[i] != NULL; i++)
315 undepend(depnames[i], pkg);
322 sanity_check(char *pkg)
324 if (!fexists(CONTENTS_FNAME)) {
326 errx(2, "%s: installed package %s has no %s file!", __func__,
327 pkg, CONTENTS_FNAME);
339 undepend(char *p, char *pkgname)
341 char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
344 struct reqr_by_entry *rb_entry;
345 struct reqr_by_head *rb_list;
348 if (requiredby(p, &rb_list, Verbose, FALSE) <= 0)
350 snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, p, REQUIRED_BY_FNAME);
351 snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
354 warnx("couldn't open temp file '%s'", ftmp);
357 fpwr = fdopen(s, "w");
360 warnx("couldn't fdopen temp file '%s'", ftmp);
363 STAILQ_FOREACH(rb_entry, rb_list, link)
364 if (strcmp(rb_entry->pkgname, pkgname)) /* no match */
365 fputs(rb_entry->pkgname, fpwr), putc('\n', fpwr);
366 if (fchmod(s, 0644) == FAIL) {
367 warnx("error changing permission of temp file '%s'", ftmp);
371 if (fclose(fpwr) == EOF) {
372 warnx("error closing temp file '%s'", ftmp);
375 if (rename(ftmp, fname) == -1)
376 warnx("error renaming '%s' to '%s'", ftmp, fname);