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 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]);
93 Boolean installed = FALSE, isTMP = FALSE;
94 char log_dir[FILENAME_MAX];
95 char fname[FILENAME_MAX];
103 if ((cp = fileGetURL(NULL, pkg, KeepPackage)) != NULL) {
104 if (!getcwd(fname, FILENAME_MAX))
111 else if (fexists(pkg) && isfile(pkg)) {
115 if (!getcwd(fname, FILENAME_MAX))
118 snprintf(&fname[len], FILENAME_MAX - len, "/%s", pkg);
125 if ((cp = fileFindByPath(NULL, pkg)) != NULL)
126 strncpy(fname, cp, FILENAME_MAX);
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.
136 if (stat(fname, &sb) == FAIL) {
137 warnx("can't stat package file '%s'", fname);
141 Home = make_playpen(PlayPen, sb.st_size / 2);
142 if (unpack(fname, "'+*'")) {
143 warnx("error during unpacking, no info for '%s' available", pkg);
149 /* It's not an uninstalled package, try and find it among the installed */
151 int isinstalled = isinstalledpkg(pkg);
152 if (isinstalled < 0) {
153 warnx("the package info for package '%s' is corrupt", pkg);
155 } else if (isinstalled == 0) {
156 warnx("can't find package '%s' installed or in a file!", pkg);
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);
167 /* Suck in the contents list */
168 plist.head = plist.tail = NULL;
169 fp = fopen(CONTENTS_FNAME, "r");
171 warnx("unable to open %s file", CONTENTS_FNAME);
175 /* If we have a prefix, add it now */
176 read_plist(&plist, fp);
180 * Index is special info type that has to override all others to make
183 if (Flags & SHOW_INDEX) {
184 char tmp[FILENAME_MAX];
186 snprintf(tmp, FILENAME_MAX, "%-19s ", pkg);
187 show_index(tmp, COMMENT_FNAME);
190 /* Start showing the package contents */
192 printf("%sInformation for %s:\n\n", InfoPrefix, pkg);
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);
245 static int in_cleanup = 0;
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.
261 abspath(const char *pathname)
263 char *tmp, *tmp1, *resolved_path;
267 if (pathname[0] != '/') {
268 cwd = getcwd(NULL, MAXPATHLEN);
269 asprintf(&resolved_path, "%s/%s/", cwd, pathname);
271 asprintf(&resolved_path, "%s/", pathname);
273 if (resolved_path == NULL)
279 while ((tmp = strstr(resolved_path, "//")) != NULL)
280 strcpy(tmp, tmp + 1);
282 while ((tmp = strstr(resolved_path, "/./")) != NULL)
283 strcpy(tmp, tmp + 2);
285 while ((tmp = strstr(resolved_path, "/../")) != NULL) {
287 if ((tmp1 = strrchr(resolved_path, '/')) == NULL)
288 tmp1 = resolved_path;
289 strcpy(tmp1, tmp + 3);
292 len = strlen(resolved_path);
293 if (len > 1 && resolved_path[len - 1] == '/')
294 resolved_path[len - 1] = '\0';
296 return resolved_path;
300 * Comparison to see if the path we're on matches the
301 * one we are looking for.
304 cmp_path(const char *target, const char *current, const char *cwd)
306 char *resolved, *temp;
309 asprintf(&temp, "%s/%s", cwd, current);
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.
317 resolved = abspath(temp);
319 if (strcmp(target, resolved) == 0)
330 * Look through package dbs in LOG_DIR and find which
331 * packages installed the files in which_list.
334 find_pkg(struct which_head *which_list)
338 struct which_entry *wp;
340 TAILQ_FOREACH(wp, which_list, next) {
341 const char *msg = "file cannot be found";
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);
350 strlcpy(wp->file, tmp, PATH_MAX);
354 msg = "file is not in PATH";
357 tmp = abspath(wp->file);
359 strlcpy(wp->file, tmp, PATH_MAX);
364 if (wp->skip == TRUE)
365 warnx("%s: %s", wp->file, msg);
368 installed = matchinstalled(MATCH_ALL, NULL, &errcode);
369 if (installed == NULL)
372 for (i = 0; installed[i] != NULL; i++) {
379 snprintf(tmp, PATH_MAX, "%s/%s/%s", LOG_DIR, installed[i],
381 fp = fopen(tmp, "r");
387 pkg.head = pkg.tail = NULL;
388 read_plist(&pkg, fp);
390 for (itr = pkg.head; itr != pkg.tail; itr = itr->next) {
391 if (itr->type == PLIST_CWD) {
393 } else if (itr->type == PLIST_FILE) {
394 TAILQ_FOREACH(wp, which_list, next) {
395 if (wp->skip == TRUE)
397 if (!cmp_path(wp->file, itr->name, cwd))
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);
403 strlcpy(wp->package, installed[i], PATH_MAX);
411 TAILQ_FOREACH(wp, which_list, next) {
412 if (wp->package[0] != '\0') {
416 printf("%s was installed by package %s\n", \
417 wp->file, wp->package);
420 while (!TAILQ_EMPTY(which_list)) {
421 wp = TAILQ_FIRST(which_list);
422 TAILQ_REMOVE(which_list, wp, next);
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
438 find_pkgs_by_origin(const char *origin)
444 printf("The following installed package(s) has %s origin:\n", origin);
446 matched = matchbyorigin(origin, &errcode);
450 for (i = 0; matched[i] != NULL; i++)
457 * List only the matching package names.
458 * Mainly intended for scripts.
461 matched_packages(char **pkgs)
466 matched = matchinstalled(MatchType == MATCH_GLOB ? MATCH_NGLOB : MatchType, pkgs, &errcode);
468 if (errcode != 0 || matched == NULL)
471 for (i = 0; matched[i]; i++)
473 printf("%s\n", matched[i]);
475 printf("%s%s\n", InfoPrefix, matched[i]);