1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/*
* 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <err.h>
#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, **rb, **rbtmp;
int errcode, i, j;
int err_cnt = 0;
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;
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, "%s: unable to get current working directory!", __FUNCTION__);
}
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 (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, "%s: unable to return to working directory %s!", __FUNCTION__,
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, "%s: unable to return to working directory %s!", __FUNCTION__,
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, "%s: installed package %s has no %s file!", __FUNCTION__,
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];
FILE *fpwr;
int s;
struct reqr_by_entry *rb_entry;
struct reqr_by_head *rb_list;
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;
}
|