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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
28 static int pkg_do(char *);
29 static void sanity_check(char *);
30 static void undepend(char *, char *);
31 static char LogDir
[FILENAME_MAX
];
35 pkg_perform(char **pkgs
)
37 char **matched
, **rb
, **rbtmp
;
40 struct reqr_by_entry
*rb_entry
;
41 struct reqr_by_head
*rb_list
;
43 if (MatchType
!= MATCH_EXACT
) {
44 matched
= matchinstalled(MatchType
, pkgs
, &errcode
);
50 * Copy matched[] into pkgs[], because we'll need to use
51 * matchinstalled() later on.
53 if (matched
!= NULL
) {
55 for (i
= 0; matched
[i
] != NULL
; i
++) {
56 pkgs
= realloc(pkgs
, sizeof(*pkgs
) * (i
+ 2));
57 pkgs
[i
] = strdup(matched
[i
]);
61 else switch (MatchType
) {
65 warnx("no packages installed");
69 warnx("no packages match pattern(s)");
76 err_cnt
+= sortdeps(pkgs
);
77 for (i
= 0; pkgs
[i
]; i
++) {
78 if (Recursive
== TRUE
) {
79 errcode
= requiredby(pkgs
[i
], &rb_list
, FALSE
, TRUE
);
82 } else if (errcode
> 0) {
84 * Copy values from the rb_list queue into argv-like NULL
85 * terminated list because requiredby() uses some static
86 * storage, while pkg_do() below will call this function,
87 * thus blowing our rb_list away.
89 rbtmp
= rb
= alloca((errcode
+ 1) * sizeof(*rb
));
91 warnx("%s(): alloca() failed", __func__
);
95 STAILQ_FOREACH(rb_entry
, rb_list
, link
) {
96 *rbtmp
= alloca(strlen(rb_entry
->pkgname
) + 1);
98 warnx("%s(): alloca() failed", __func__
);
102 strcpy(*rbtmp
, rb_entry
->pkgname
);
107 err_cnt
+= sortdeps(rb
);
108 for (j
= 0; rb
[j
]; j
++)
109 err_cnt
+= pkg_do(rb
[j
]);
112 err_cnt
+= pkg_do(pkgs
[i
]);
118 static Package Plist
;
120 /* This is seriously ugly code following. Written very fast! */
125 char *deporigin
, **deporigins
= NULL
, **depnames
= NULL
, ***depmatches
, home
[FILENAME_MAX
];
129 /* support for separate pre/post install scripts */
130 int new_m
= 0, dep_count
= 0;
131 const char *pre_script
= DEINSTALL_FNAME
;
132 const char *post_script
, *pre_arg
, *post_arg
;
133 struct reqr_by_entry
*rb_entry
;
134 struct reqr_by_head
*rb_list
;
136 if (!pkg
|| !(len
= strlen(pkg
)))
138 if (pkg
[len
- 1] == '/')
141 /* Reset some state */
145 sprintf(LogDir
, "%s/%s", LOG_DIR
, pkg
);
147 isinstalled
= isinstalledpkg(pkg
);
148 if (isinstalled
== 0) {
149 warnx("no such package '%s' installed", pkg
);
151 } else if (isinstalled
< 0) {
152 warnx("the package info for package '%s' is corrupt%s",
153 pkg
, Force
? " (but I'll delete it anyway)" : " (use -f to force removal)");
157 if (vsystem("%s -rf %s", REMOVE_CMD
, LogDir
)) {
158 warnx("couldn't remove log entry in %s, deinstall failed", LogDir
);
160 warnx("couldn't completely deinstall package '%s',\n"
161 "only the log entry in %s was removed", pkg
, LogDir
);
167 if (!getcwd(home
, FILENAME_MAX
)) {
169 errx(2, "%s: unable to get current working directory!", __func__
);
172 if (chdir(LogDir
) == FAIL
) {
173 warnx("unable to change directory to %s! deinstall failed", LogDir
);
177 if (Interactive
== TRUE
) {
180 (void)fprintf(stderr
, "delete %s? ", pkg
);
181 (void)fflush(stderr
);
182 first
= ch
= getchar();
183 while (ch
!= '\n' && ch
!= EOF
)
185 if (first
!= 'y' && first
!= 'Y')
190 if (requiredby(pkg
, &rb_list
, FALSE
, TRUE
) < 0)
192 if (!STAILQ_EMPTY(rb_list
)) {
193 warnx("package '%s' is required by these other packages\n"
194 "and may not be deinstalled%s:",
195 pkg
, Force
? " (but I'll delete it anyway)" : "");
196 STAILQ_FOREACH(rb_entry
, rb_list
, link
)
197 fprintf(stderr
, "%s\n", rb_entry
->pkgname
);
202 sanity_check(LogDir
);
203 cfile
= fopen(CONTENTS_FNAME
, "r");
206 warnx("unable to open '%s' file", CONTENTS_FNAME
);
210 /* If we have a prefix, add it now */
212 add_plist(&Plist
, PLIST_CWD
, Prefix
);
213 read_plist(&Plist
, cfile
);
215 p
= find_plist(&Plist
, PLIST_CWD
);
218 warnx("package '%s' doesn't have a prefix", pkg
);
222 setenv(PKG_PREFIX_VNAME
, p
->name
, 1);
224 if (fexists(REQUIRE_FNAME
)) {
226 printf("Executing 'require' script.\n");
227 vsystem("/bin/chmod +x %s", REQUIRE_FNAME
); /* be sure */
228 if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME
, pkg
)) {
229 warnx("package %s fails requirements %s", pkg
,
230 Force
? "" : "- not deleted");
237 * Test whether to use the old method of passing tokens to deinstallation
238 * scripts, and set appropriate variables..
241 if (fexists(POST_DEINSTALL_FNAME
)) {
243 post_script
= POST_DEINSTALL_FNAME
;
244 pre_arg
= post_arg
= "";
245 } else if (fexists(DEINSTALL_FNAME
)) {
246 post_script
= DEINSTALL_FNAME
;
247 pre_arg
= "DEINSTALL";
248 post_arg
= "POST-DEINSTALL";
250 post_script
= pre_arg
= post_arg
= NULL
;
253 if (!NoDeInstall
&& pre_script
!= NULL
&& fexists(pre_script
)) {
255 printf("Would execute de-install script at this point.\n");
257 vsystem("/bin/chmod +x %s", pre_script
); /* make sure */
258 if (vsystem("./%s %s %s", pre_script
, pkg
, pre_arg
)) {
259 warnx("deinstall script returned error status");
266 for (p
= Plist
.head
; p
; p
= p
->next
) {
267 if (p
->type
!= PLIST_PKGDEP
)
269 deporigin
= (p
->next
!= NULL
&& p
->next
->type
== PLIST_DEPORIGIN
) ? p
->next
->name
:
272 printf("Trying to remove dependency on package '%s'", p
->name
);
273 if (deporigin
!= NULL
)
274 printf(" with '%s' origin", deporigin
);
279 deporigins
= realloc(deporigins
, (dep_count
+ 2) * sizeof(*deporigins
));
280 depnames
= realloc(depnames
, (dep_count
+ 1) * sizeof(*depnames
));
281 deporigins
[dep_count
] = deporigin
;
282 deporigins
[dep_count
+ 1] = NULL
;
283 depnames
[dep_count
] = p
->name
;
286 undepend(p
->name
, pkg
);
292 /* Undepend all the dependencies at once */
293 depmatches
= matchallbyorigin((const char **)deporigins
, NULL
);
296 for (i
= 0; i
< dep_count
; i
++) {
298 char **tmp
= depmatches
[i
];
300 for (j
= 0; tmp
[j
] != NULL
; j
++)
301 undepend(tmp
[j
], pkg
);
302 } else if (depnames
[i
]) {
303 undepend(depnames
[i
], pkg
);
309 if (chdir(home
) == FAIL
) {
311 errx(2, "%s: unable to return to working directory %s!", __func__
,
316 * Some packages aren't packed right, so we need to just ignore
317 * delete_package()'s status. Ugh! :-(
319 if (delete_package(FALSE
, CleanDirs
, &Plist
) == FAIL
)
321 "couldn't entirely delete package (perhaps the packing list is\n"
322 "incorrectly specified?)");
324 if (chdir(LogDir
) == FAIL
) {
325 warnx("unable to change directory to %s! deinstall failed", LogDir
);
329 if (!NoDeInstall
&& post_script
!= NULL
&& fexists(post_script
)) {
331 printf("Would execute post-deinstall script at this point.\n");
333 vsystem("/bin/chmod +x %s", post_script
); /* make sure */
334 if (vsystem("./%s %s %s", post_script
, pkg
, post_arg
)) {
335 warnx("post-deinstall script returned error status");
342 if (chdir(home
) == FAIL
) {
344 errx(2, "%s: unable to return to working directory %s!", __func__
,
349 if (vsystem("%s -r%c %s", REMOVE_CMD
, Force
? 'f' : ' ', LogDir
)) {
350 warnx("couldn't remove log entry in %s, deinstall failed", LogDir
);
359 sanity_check(char *pkg
)
361 if (!fexists(CONTENTS_FNAME
)) {
363 errx(2, "%s: installed package %s has no %s file!", __func__
,
364 pkg
, CONTENTS_FNAME
);
376 undepend(char *p
, char *pkgname
)
378 char fname
[FILENAME_MAX
], ftmp
[FILENAME_MAX
];
381 struct reqr_by_entry
*rb_entry
;
382 struct reqr_by_head
*rb_list
;
385 if (requiredby(p
, &rb_list
, Verbose
, FALSE
) <= 0)
387 snprintf(fname
, sizeof(fname
), "%s/%s/%s", LOG_DIR
, p
, REQUIRED_BY_FNAME
);
388 snprintf(ftmp
, sizeof(ftmp
), "%s.XXXXXX", fname
);
391 warnx("couldn't open temp file '%s'", ftmp
);
394 fpwr
= fdopen(s
, "w");
397 warnx("couldn't fdopen temp file '%s'", ftmp
);
400 STAILQ_FOREACH(rb_entry
, rb_list
, link
)
401 if (strcmp(rb_entry
->pkgname
, pkgname
)) /* no match */
402 fputs(rb_entry
->pkgname
, fpwr
), putc('\n', fpwr
);
403 if (fchmod(s
, 0644) == FAIL
) {
404 warnx("error changing permission of temp file '%s'", ftmp
);
408 if (fclose(fpwr
) == EOF
) {
409 warnx("error closing temp file '%s'", ftmp
);
412 if (rename(ftmp
, fname
) == -1)
413 warnx("error renaming '%s' to '%s'", ftmp
, fname
);