perform.c revision 154145
1/*
2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
14 * Jordan K. Hubbard
15 * 23 Aug 1993
16 *
17 * This is the main body of the info module.
18 *
19 */
20
21#include <sys/cdefs.h>
22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/info/perform.c 154145 2006-01-09 18:27:21Z flz $");
23
24#include "lib.h"
25#include "info.h"
26#include <err.h>
27#include <signal.h>
28
29static int pkg_do(char *);
30static int find_pkg(struct which_head *);
31static int cmp_path(const char *, const char *, const char *);
32static char *abspath(const char *);
33static int find_pkgs_by_origin(const char *);
34static int matched_packages(char **pkgs);
35
36int
37pkg_perform(char **pkgs)
38{
39    char **matched;
40    int err_cnt = 0;
41    int i, errcode;
42
43    signal(SIGINT, cleanup);
44
45    /* Overriding action? */
46    if (Flags & SHOW_PKGNAME) {
47	return matched_packages(pkgs);
48    } else if (CheckPkg) {
49	return isinstalledpkg(CheckPkg) > 0 ? 0 : 1;
50	/* Not reached */
51    } else if (!TAILQ_EMPTY(whead)) {
52	return find_pkg(whead);
53    } else if (LookUpOrigin != NULL) {
54	return find_pkgs_by_origin(LookUpOrigin);
55    }
56
57    if (MatchType != MATCH_EXACT) {
58	matched = matchinstalled(MatchType, pkgs, &errcode);
59	if (errcode != 0)
60	    return 1;
61	    /* Not reached */
62
63	if (matched != NULL)
64	    pkgs = matched;
65	else switch (MatchType) {
66	    case MATCH_GLOB:
67		break;
68	    case MATCH_ALL:
69		warnx("no packages installed");
70		return 0;
71		/* Not reached */
72	    case MATCH_REGEX:
73	    case MATCH_EREGEX:
74		warnx("no packages match pattern(s)");
75		return 1;
76		/* Not reached */
77	    default:
78		break;
79	}
80    }
81
82    for (i = 0; pkgs[i]; i++)
83	err_cnt += pkg_do(pkgs[i]);
84
85    return err_cnt;
86}
87
88static char *Home;
89
90static int
91pkg_do(char *pkg)
92{
93    Boolean installed = FALSE, isTMP = FALSE;
94    char log_dir[FILENAME_MAX];
95    char fname[FILENAME_MAX];
96    Package plist;
97    FILE *fp;
98    struct stat sb;
99    char *cp = NULL;
100    int code = 0;
101
102    if (isURL(pkg)) {
103	if ((cp = fileGetURL(NULL, pkg, KeepPackage)) != NULL) {
104	    strcpy(fname, cp);
105	    isTMP = TRUE;
106	}
107    }
108    else if (fexists(pkg) && isfile(pkg)) {
109	int len;
110
111	if (*pkg != '/') {
112	    if (!getcwd(fname, FILENAME_MAX))
113		upchuck("getcwd");
114	    len = strlen(fname);
115	    snprintf(&fname[len], FILENAME_MAX - len, "/%s", pkg);
116	}
117	else
118	    strcpy(fname, pkg);
119	cp = fname;
120    }
121    else {
122	if ((cp = fileFindByPath(NULL, pkg)) != NULL)
123	    strncpy(fname, cp, FILENAME_MAX);
124    }
125    if (cp) {
126	/*
127	 * Apply a crude heuristic to see how much space the package will
128	 * take up once it's unpacked.  I've noticed that most packages
129	 * compress an average of 75%, but we're only unpacking the + files so
130	 * be very optimistic.
131	 */
132	if (stat(fname, &sb) == FAIL) {
133	    warnx("can't stat package file '%s'", fname);
134	    code = 1;
135	    goto bail;
136	}
137	Home = make_playpen(PlayPen, sb.st_size / 2);
138	if (unpack(fname, "'+*'")) {
139	    warnx("error during unpacking, no info for '%s' available", pkg);
140	    code = 1;
141	    goto bail;
142	}
143    }
144    /* It's not an uninstalled package, try and find it among the installed */
145    else {
146	int isinstalled = isinstalledpkg(pkg);
147	if (isinstalled < 0) {
148	    warnx("the package info for package '%s' is corrupt", pkg);
149	    return 1;
150	} else if (isinstalled == 0) {
151	    warnx("can't find package '%s' installed or in a file!", pkg);
152	    return 1;
153	}
154	sprintf(log_dir, "%s/%s", LOG_DIR, pkg);
155	if (chdir(log_dir) == FAIL) {
156	    warnx("can't change directory to '%s'!", log_dir);
157	    return 1;
158	}
159	installed = TRUE;
160    }
161
162    /* Suck in the contents list */
163    plist.head = plist.tail = NULL;
164    fp = fopen(CONTENTS_FNAME, "r");
165    if (!fp) {
166	warnx("unable to open %s file", CONTENTS_FNAME);
167	code = 1;
168	goto bail;
169    }
170    /* If we have a prefix, add it now */
171    read_plist(&plist, fp);
172    fclose(fp);
173
174    /*
175     * Index is special info type that has to override all others to make
176     * any sense.
177     */
178    if (Flags & SHOW_INDEX) {
179	char tmp[FILENAME_MAX];
180
181	snprintf(tmp, FILENAME_MAX, "%-19s ", pkg);
182	show_index(tmp, COMMENT_FNAME);
183    }
184    else {
185	/* Start showing the package contents */
186	if (!Quiet)
187	    printf("%sInformation for %s:\n\n", InfoPrefix, pkg);
188	else if (QUIET)
189	    printf("%s%s:", InfoPrefix, pkg);
190	if (Flags & SHOW_COMMENT)
191	    show_file("Comment:\n", COMMENT_FNAME);
192	if (Flags & SHOW_DEPEND)
193	    show_plist("Depends on:\n", &plist, PLIST_PKGDEP, FALSE);
194	if ((Flags & SHOW_REQBY) && !isemptyfile(REQUIRED_BY_FNAME))
195	    show_file("Required by:\n", REQUIRED_BY_FNAME);
196	if (Flags & SHOW_DESC)
197	    show_file("Description:\n", DESC_FNAME);
198	if ((Flags & SHOW_DISPLAY) && fexists(DISPLAY_FNAME))
199	    show_file("Install notice:\n", DISPLAY_FNAME);
200	if (Flags & SHOW_PLIST)
201	    show_plist("Packing list:\n", &plist, (plist_t)0, TRUE);
202	if (Flags & SHOW_REQUIRE && fexists(REQUIRE_FNAME))
203	    show_file("Requirements script:\n", REQUIRE_FNAME);
204	if ((Flags & SHOW_INSTALL) && fexists(INSTALL_FNAME))
205	    show_file("Install script:\n", INSTALL_FNAME);
206	if ((Flags & SHOW_INSTALL) && fexists(POST_INSTALL_FNAME))
207	    show_file("Post-Install script:\n", POST_INSTALL_FNAME);
208	if ((Flags & SHOW_DEINSTALL) && fexists(DEINSTALL_FNAME))
209	    show_file("De-Install script:\n", DEINSTALL_FNAME);
210	if ((Flags & SHOW_DEINSTALL) && fexists(POST_DEINSTALL_FNAME))
211	    show_file("Post-DeInstall script:\n", POST_DEINSTALL_FNAME);
212	if ((Flags & SHOW_MTREE) && fexists(MTREE_FNAME))
213	    show_file("mtree file:\n", MTREE_FNAME);
214	if (Flags & SHOW_PREFIX)
215	    show_plist("Prefix(s):\n", &plist, PLIST_CWD, FALSE);
216	if (Flags & SHOW_FILES)
217	    show_files("Files:\n", &plist);
218	if ((Flags & SHOW_SIZE) && installed)
219	    show_size("Package Size:\n", &plist);
220	if ((Flags & SHOW_CKSUM) && installed)
221	    show_cksum("Mismatched Checksums:\n", &plist);
222	if (Flags & SHOW_ORIGIN)
223	    show_origin("Origin:\n", &plist);
224	if (Flags & SHOW_FMTREV)
225	    show_fmtrev("Packing list format revision:\n", &plist);
226	if (!Quiet)
227	    puts(InfoPrefix);
228    }
229    free_plist(&plist);
230 bail:
231    leave_playpen();
232    if (isTMP)
233	unlink(fname);
234    return code;
235}
236
237void
238cleanup(int sig)
239{
240    static int in_cleanup = 0;
241
242    if (!in_cleanup) {
243	in_cleanup = 1;
244	leave_playpen();
245    }
246    if (sig)
247	exit(1);
248}
249
250/*
251 * Return an absolute path, additionally removing all .'s, ..'s, and extraneous
252 * /'s, as realpath() would, but without resolving symlinks, because that can
253 * potentially screw up our comparisons later.
254 */
255static char *
256abspath(const char *pathname)
257{
258    char *tmp, *tmp1, *resolved_path;
259    char *cwd = NULL;
260    int len;
261
262    if (pathname[0] != '/') {
263	cwd = getcwd(NULL, MAXPATHLEN);
264	asprintf(&resolved_path, "%s/%s/", cwd, pathname);
265    } else
266	asprintf(&resolved_path, "%s/", pathname);
267
268    if (resolved_path == NULL)
269	errx(2, NULL);
270
271    if (cwd != NULL)
272	free(cwd);
273
274    while ((tmp = strstr(resolved_path, "//")) != NULL)
275	strcpy(tmp, tmp + 1);
276
277    while ((tmp = strstr(resolved_path, "/./")) != NULL)
278	strcpy(tmp, tmp + 2);
279
280    while ((tmp = strstr(resolved_path, "/../")) != NULL) {
281	*tmp = '\0';
282	if ((tmp1 = strrchr(resolved_path, '/')) == NULL)
283	   tmp1 = resolved_path;
284	strcpy(tmp1, tmp + 3);
285    }
286
287    len = strlen(resolved_path);
288    if (len > 1 && resolved_path[len - 1] == '/')
289	resolved_path[len - 1] = '\0';
290
291    return resolved_path;
292}
293
294/*
295 * Comparison to see if the path we're on matches the
296 * one we are looking for.
297 */
298static int
299cmp_path(const char *target, const char *current, const char *cwd)
300{
301    char *resolved, *temp;
302    int rval;
303
304    asprintf(&temp, "%s/%s", cwd, current);
305    if (temp == NULL)
306        errx(2, NULL);
307
308    /*
309     * Make sure there's no multiple /'s or other weird things in the PLIST,
310     * since some plists seem to have them and it could screw up our strncmp.
311     */
312    resolved = abspath(temp);
313
314    if (strcmp(target, resolved) == 0)
315	rval = 1;
316    else
317	rval = 0;
318
319    free(temp);
320    free(resolved);
321    return rval;
322}
323
324/*
325 * Look through package dbs in LOG_DIR and find which
326 * packages installed the files in which_list.
327 */
328static int
329find_pkg(struct which_head *which_list)
330{
331    char **installed;
332    int errcode, i;
333    struct which_entry *wp;
334
335    TAILQ_FOREACH(wp, which_list, next) {
336	const char *msg = "file cannot be found";
337	char *tmp;
338
339	wp->skip = TRUE;
340	/* If it's not a file, we'll see if it's an executable. */
341	if (isfile(wp->file) == FALSE) {
342	    if (strchr(wp->file, '/') == NULL) {
343		tmp = vpipe("/usr/bin/which %s", wp->file);
344		if (tmp != NULL) {
345		    strlcpy(wp->file, tmp, PATH_MAX);
346		    wp->skip = FALSE;
347		    free(tmp);
348		} else
349		    msg = "file is not in PATH";
350	    }
351	} else {
352	    tmp = abspath(wp->file);
353	    if (isfile(tmp)) {
354	    	strlcpy(wp->file, tmp, PATH_MAX);
355	    	wp->skip = FALSE;
356	    }
357	    free(tmp);
358	}
359	if (wp->skip == TRUE)
360	    warnx("%s: %s", wp->file, msg);
361    }
362
363    installed = matchinstalled(MATCH_ALL, NULL, &errcode);
364    if (installed == NULL)
365        return errcode;
366
367    for (i = 0; installed[i] != NULL; i++) {
368     	FILE *fp;
369     	Package pkg;
370     	PackingList itr;
371     	char *cwd = NULL;
372     	char tmp[PATH_MAX];
373
374	snprintf(tmp, PATH_MAX, "%s/%s/%s", LOG_DIR, installed[i],
375		 CONTENTS_FNAME);
376	fp = fopen(tmp, "r");
377	if (fp == NULL) {
378	    warn("%s", tmp);
379	    return 1;
380	}
381
382	pkg.head = pkg.tail = NULL;
383	read_plist(&pkg, fp);
384	fclose(fp);
385	for (itr = pkg.head; itr != pkg.tail; itr = itr->next) {
386	    if (itr->type == PLIST_CWD) {
387		cwd = itr->name;
388	    } else if (itr->type == PLIST_FILE) {
389		TAILQ_FOREACH(wp, which_list, next) {
390		    if (wp->skip == TRUE)
391			continue;
392		    if (!cmp_path(wp->file, itr->name, cwd))
393			continue;
394		    if (wp->package[0] != '\0') {
395			warnx("both %s and %s claim to have installed %s\n",
396			      wp->package, installed[i], wp->file);
397		    } else {
398			strlcpy(wp->package, installed[i], PATH_MAX);
399		    }
400		}
401	    }
402	}
403	free_plist(&pkg);
404    }
405
406    TAILQ_FOREACH(wp, which_list, next) {
407	if (wp->package[0] != '\0') {
408	    if (Quiet)
409		puts(wp->package);
410	    else
411		printf("%s was installed by package %s\n", \
412		       wp->file, wp->package);
413	}
414    }
415    while (!TAILQ_EMPTY(which_list)) {
416	wp = TAILQ_FIRST(which_list);
417	TAILQ_REMOVE(which_list, wp, next);
418	free(wp);
419    }
420
421    free(which_list);
422    return 0;
423}
424
425/*
426 * Look through package dbs in LOG_DIR and find which
427 * packages have the given origin. Don't use read_plist()
428 * because this increases time necessary for lookup by 40
429 * times, as we don't really have to parse all plist to
430 * get origin.
431 */
432static int
433find_pkgs_by_origin(const char *origin)
434{
435    char **matched;
436    int errcode, i;
437
438    if (!Quiet)
439	printf("The following installed package(s) has %s origin:\n", origin);
440
441    matched = matchbyorigin(origin, &errcode);
442    if (matched == NULL)
443	return errcode;
444
445    for (i = 0; matched[i] != NULL; i++)
446	puts(matched[i]);
447
448    return 0;
449}
450
451/*
452 * List only the matching package names.
453 * Mainly intended for scripts.
454 */
455static int
456matched_packages(char **pkgs)
457{
458    char **matched;
459    int i, errcode;
460
461    matched = matchinstalled(MatchType == MATCH_GLOB ? MATCH_NGLOB : MatchType, pkgs, &errcode);
462
463    if (errcode != 0 || matched == NULL)
464	return 1;
465
466    for (i = 0; matched[i]; i++)
467	if (!Quiet)
468	    printf("%s\n", matched[i]);
469	else if (QUIET)
470	    printf("%s%s\n", InfoPrefix, matched[i]);
471
472    return 0;
473}
474