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