2 * FreeBSD install - a package for the installation and maintenance
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 info module.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
29 static int pkg_do(char *);
30 static int find_pkg(struct which_head *);
31 static int cmp_path(const char *, const char *, const char *);
32 static char *abspath(const char *);
33 static int find_pkgs_by_origin(const char *);
34 static int matched_packages(char **pkgs);
37 pkg_perform(char **pkgs)
43 signal(SIGINT, cleanup);
45 /* Overriding action? */
46 if (Flags & SHOW_PKGNAME) {
47 return matched_packages(pkgs);
48 } else if (CheckPkg) {
49 return isinstalledpkg(CheckPkg) > 0 ? 0 : 1;
51 } else if (!TAILQ_EMPTY(whead)) {
52 return find_pkg(whead);
53 } else if (LookUpOrigin != NULL) {
54 return find_pkgs_by_origin(LookUpOrigin);
57 if (MatchType != MATCH_EXACT) {
58 matched = matchinstalled(MatchType, pkgs, &errcode);
65 else switch (MatchType) {
69 warnx("no packages installed");
74 warnx("no packages match pattern(s)");
82 for (i = 0; pkgs[i]; i++)
83 err_cnt += pkg_do(pkgs[i]);
91 Boolean installed = FALSE, isTMP = FALSE;
92 char log_dir[FILENAME_MAX];
93 char fname[FILENAME_MAX];
97 const char *cp = NULL;
101 if ((cp = fileGetURL(NULL, pkg, KeepPackage)) != NULL) {
102 if (!getcwd(fname, FILENAME_MAX))
109 else if (fexists(pkg) && isfile(pkg)) {
113 if (!getcwd(fname, FILENAME_MAX))
116 snprintf(&fname[len], FILENAME_MAX - len, "/%s", pkg);
123 if ((cp = fileFindByPath(NULL, pkg)) != NULL)
124 strncpy(fname, cp, FILENAME_MAX);
129 * Apply a crude heuristic to see how much space the package will
130 * take up once it's unpacked. I've noticed that most packages
131 * compress an average of 75%, but we're only unpacking the + files so
132 * be very optimistic.
134 if (stat(fname, &sb) == FAIL) {
135 warnx("can't stat package file '%s'", fname);
139 make_playpen(PlayPen, sb.st_size / 2);
140 if (unpack(fname, "'+*'")) {
141 warnx("error during unpacking, no info for '%s' available", pkg);
147 /* It's not an uninstalled package, try and find it among the installed */
149 int isinstalled = isinstalledpkg(pkg);
150 if (isinstalled < 0) {
151 warnx("the package info for package '%s' is corrupt", pkg);
153 } else if (isinstalled == 0) {
154 warnx("can't find package '%s' installed or in a file!", pkg);
157 sprintf(log_dir, "%s/%s", LOG_DIR, pkg);
158 if (chdir(log_dir) == FAIL) {
159 warnx("can't change directory to '%s'!", log_dir);
165 /* Suck in the contents list */
166 plist.head = plist.tail = NULL;
167 fp = fopen(CONTENTS_FNAME, "r");
169 warnx("unable to open %s file", CONTENTS_FNAME);
173 /* If we have a prefix, add it now */
174 read_plist(&plist, fp);
178 * Index is special info type that has to override all others to make
181 if (Flags & SHOW_INDEX) {
182 char tmp[FILENAME_MAX];
184 snprintf(tmp, FILENAME_MAX, "%-19s ", pkg);
185 show_index(tmp, COMMENT_FNAME);
188 /* Start showing the package contents */
190 printf("%sInformation for %s:\n\n", InfoPrefix, pkg);
192 printf("%s%s:", InfoPrefix, pkg);
193 if (Flags & SHOW_COMMENT)
194 show_file("Comment:\n", COMMENT_FNAME);
195 if (Flags & SHOW_DEPEND)
196 show_plist("Depends on:\n", &plist, PLIST_PKGDEP, FALSE);
197 if ((Flags & SHOW_REQBY) && !isemptyfile(REQUIRED_BY_FNAME))
198 show_file("Required by:\n", REQUIRED_BY_FNAME);
199 if (Flags & SHOW_DESC)
200 show_file("Description:\n", DESC_FNAME);
201 if ((Flags & SHOW_DISPLAY) && fexists(DISPLAY_FNAME))
202 show_file("Install notice:\n", DISPLAY_FNAME);
203 if (Flags & SHOW_PLIST)
204 show_plist("Packing list:\n", &plist, (plist_t)0, TRUE);
205 if (Flags & SHOW_REQUIRE && fexists(REQUIRE_FNAME))
206 show_file("Requirements script:\n", REQUIRE_FNAME);
207 if ((Flags & SHOW_INSTALL) && fexists(INSTALL_FNAME))
208 show_file("Install script:\n", INSTALL_FNAME);
209 if ((Flags & SHOW_INSTALL) && fexists(POST_INSTALL_FNAME))
210 show_file("Post-Install script:\n", POST_INSTALL_FNAME);
211 if ((Flags & SHOW_DEINSTALL) && fexists(DEINSTALL_FNAME))
212 show_file("De-Install script:\n", DEINSTALL_FNAME);
213 if ((Flags & SHOW_DEINSTALL) && fexists(POST_DEINSTALL_FNAME))
214 show_file("Post-DeInstall script:\n", POST_DEINSTALL_FNAME);
215 if ((Flags & SHOW_MTREE) && fexists(MTREE_FNAME))
216 show_file("mtree file:\n", MTREE_FNAME);
217 if (Flags & SHOW_PREFIX)
218 show_plist("Prefix(s):\n", &plist, PLIST_CWD, FALSE);
219 if (Flags & SHOW_FILES)
220 show_files("Files:\n", &plist);
221 if ((Flags & SHOW_SIZE) && installed)
222 show_size("Package Size:\n", &plist);
223 if ((Flags & SHOW_CKSUM) && installed)
224 code += show_cksum("Mismatched Checksums:\n", &plist);
225 if (Flags & SHOW_ORIGIN)
226 show_origin("Origin:\n", &plist);
227 if (Flags & SHOW_FMTREV)
228 show_fmtrev("Packing list format revision:\n", &plist);
237 return (code ? 1 : 0);
243 static int in_cleanup = 0;
254 * Return an absolute path, additionally removing all .'s, ..'s, and extraneous
255 * /'s, as realpath() would, but without resolving symlinks, because that can
256 * potentially screw up our comparisons later.
259 abspath(const char *pathname)
261 char *tmp, *tmp1, *resolved_path;
265 if (pathname[0] != '/') {
266 cwd = getcwd(NULL, MAXPATHLEN);
267 asprintf(&resolved_path, "%s/%s/", cwd, pathname);
269 asprintf(&resolved_path, "%s/", pathname);
271 if (resolved_path == NULL)
277 while ((tmp = strstr(resolved_path, "//")) != NULL)
278 strcpy(tmp, tmp + 1);
280 while ((tmp = strstr(resolved_path, "/./")) != NULL)
281 strcpy(tmp, tmp + 2);
283 while ((tmp = strstr(resolved_path, "/../")) != NULL) {
285 if ((tmp1 = strrchr(resolved_path, '/')) == NULL)
286 tmp1 = resolved_path;
287 strcpy(tmp1, tmp + 3);
290 len = strlen(resolved_path);
291 if (len > 1 && resolved_path[len - 1] == '/')
292 resolved_path[len - 1] = '\0';
294 return resolved_path;
298 * Comparison to see if the path we're on matches the
299 * one we are looking for.
302 cmp_path(const char *target, const char *current, const char *cwd)
304 char *resolved, *temp;
307 asprintf(&temp, "%s/%s", cwd, current);
312 * Make sure there's no multiple /'s or other weird things in the PLIST,
313 * since some plists seem to have them and it could screw up our strncmp.
315 resolved = abspath(temp);
317 if (strcmp(target, resolved) == 0)
328 * Look through package dbs in LOG_DIR and find which
329 * packages installed the files in which_list.
332 find_pkg(struct which_head *which_list)
336 struct which_entry *wp;
338 TAILQ_FOREACH(wp, which_list, next) {
339 const char *msg = "file cannot be found";
343 /* If it's not a file, we'll see if it's an executable. */
344 if (isfile(wp->file) == FALSE) {
345 if (strchr(wp->file, '/') == NULL) {
346 tmp = vpipe("/usr/bin/which %s", wp->file);
348 strlcpy(wp->file, tmp, PATH_MAX);
352 msg = "file is not in PATH";
355 tmp = abspath(wp->file);
357 strlcpy(wp->file, tmp, PATH_MAX);
362 if (wp->skip == TRUE)
363 warnx("%s: %s", wp->file, msg);
366 installed = matchinstalled(MATCH_ALL, NULL, &errcode);
367 if (installed == NULL)
370 for (i = 0; installed[i] != NULL; i++) {
377 snprintf(tmp, PATH_MAX, "%s/%s/%s", LOG_DIR, installed[i],
379 fp = fopen(tmp, "r");
385 pkg.head = pkg.tail = NULL;
386 read_plist(&pkg, fp);
388 for (itr = pkg.head; itr != pkg.tail; itr = itr->next) {
389 if (itr->type == PLIST_CWD) {
391 } else if (itr->type == PLIST_FILE) {
392 TAILQ_FOREACH(wp, which_list, next) {
393 if (wp->skip == TRUE)
395 if (!cmp_path(wp->file, itr->name, cwd))
397 if (wp->package[0] != '\0') {
398 warnx("both %s and %s claim to have installed %s\n",
399 wp->package, installed[i], wp->file);
401 strlcpy(wp->package, installed[i], PATH_MAX);
409 TAILQ_FOREACH(wp, which_list, next) {
410 if (wp->package[0] != '\0') {
414 printf("%s was installed by package %s\n", \
415 wp->file, wp->package);
418 while (!TAILQ_EMPTY(which_list)) {
419 wp = TAILQ_FIRST(which_list);
420 TAILQ_REMOVE(which_list, wp, next);
429 * Look through package dbs in LOG_DIR and find which
430 * packages have the given origin. Don't use read_plist()
431 * because this increases time necessary for lookup by 40
432 * times, as we don't really have to parse all plist to
436 find_pkgs_by_origin(const char *origin)
442 printf("The following installed package(s) has %s origin:\n", origin);
444 matched = matchbyorigin(origin, &errcode);
448 for (i = 0; matched[i] != NULL; i++)
455 * List only the matching package names.
456 * Mainly intended for scripts.
459 matched_packages(char **pkgs)
464 matched = matchinstalled(MatchType == MATCH_GLOB ? MATCH_NGLOB : MatchType, pkgs, &errcode);
466 if (errcode != 0 || matched == NULL)
469 for (i = 0; matched[i]; i++)
471 printf("%s\n", matched[i]);
473 printf("%s%s\n", InfoPrefix, matched[i]);