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) {
108 else if (fexists(pkg) && isfile(pkg)) {
112 if (!getcwd(fname, FILENAME_MAX))
115 snprintf(&fname[len], FILENAME_MAX - len, "/%s", pkg);
122 if ((cp = fileFindByPath(NULL, pkg)) != NULL)
123 strncpy(fname, cp, FILENAME_MAX);
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.
132 if (stat(fname, &sb) == FAIL) {
133 warnx("can't stat package file '%s'", fname);
137 Home = make_playpen(PlayPen, sb.st_size / 2);
138 if (unpack(fname, "'+*'")) {
139 warnx("error during unpacking, no info for '%s' available", pkg);
144 /* It's not an uninstalled package, try and find it among the installed */
146 int isinstalled = isinstalledpkg(pkg);
147 if (isinstalled < 0) {
148 warnx("the package info for package '%s' is corrupt", pkg);
150 } else if (isinstalled == 0) {
151 warnx("can't find package '%s' installed or in a file!", pkg);
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);
162 /* Suck in the contents list */
163 plist.head = plist.tail = NULL;
164 fp = fopen(CONTENTS_FNAME, "r");
166 warnx("unable to open %s file", CONTENTS_FNAME);
170 /* If we have a prefix, add it now */
171 read_plist(&plist, fp);
175 * Index is special info type that has to override all others to make
178 if (Flags & SHOW_INDEX) {
179 char tmp[FILENAME_MAX];
181 snprintf(tmp, FILENAME_MAX, "%-19s ", pkg);
182 show_index(tmp, COMMENT_FNAME);
185 /* Start showing the package contents */
187 printf("%sInformation for %s:\n\n", InfoPrefix, pkg);
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);
240 static int in_cleanup = 0;
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.
256 abspath(const char *pathname)
258 char *tmp, *tmp1, *resolved_path;
262 if (pathname[0] != '/') {
263 cwd = getcwd(NULL, MAXPATHLEN);
264 asprintf(&resolved_path, "%s/%s/", cwd, pathname);
266 asprintf(&resolved_path, "%s/", pathname);
268 if (resolved_path == NULL)
274 while ((tmp = strstr(resolved_path, "//")) != NULL)
275 strcpy(tmp, tmp + 1);
277 while ((tmp = strstr(resolved_path, "/./")) != NULL)
278 strcpy(tmp, tmp + 2);
280 while ((tmp = strstr(resolved_path, "/../")) != NULL) {
282 if ((tmp1 = strrchr(resolved_path, '/')) == NULL)
283 tmp1 = resolved_path;
284 strcpy(tmp1, tmp + 3);
287 len = strlen(resolved_path);
288 if (len > 1 && resolved_path[len - 1] == '/')
289 resolved_path[len - 1] = '\0';
291 return resolved_path;
295 * Comparison to see if the path we're on matches the
296 * one we are looking for.
299 cmp_path(const char *target, const char *current, const char *cwd)
301 char *resolved, *temp;
304 asprintf(&temp, "%s/%s", cwd, current);
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.
312 resolved = abspath(temp);
314 if (strcmp(target, resolved) == 0)
325 * Look through package dbs in LOG_DIR and find which
326 * packages installed the files in which_list.
329 find_pkg(struct which_head *which_list)
333 struct which_entry *wp;
335 TAILQ_FOREACH(wp, which_list, next) {
336 const char *msg = "file cannot be found";
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);
345 strlcpy(wp->file, tmp, PATH_MAX);
349 msg = "file is not in PATH";
352 tmp = abspath(wp->file);
354 strlcpy(wp->file, tmp, PATH_MAX);
359 if (wp->skip == TRUE)
360 warnx("%s: %s", wp->file, msg);
363 installed = matchinstalled(MATCH_ALL, NULL, &errcode);
364 if (installed == NULL)
367 for (i = 0; installed[i] != NULL; i++) {
374 snprintf(tmp, PATH_MAX, "%s/%s/%s", LOG_DIR, installed[i],
376 fp = fopen(tmp, "r");
382 pkg.head = pkg.tail = NULL;
383 read_plist(&pkg, fp);
385 for (itr = pkg.head; itr != pkg.tail; itr = itr->next) {
386 if (itr->type == PLIST_CWD) {
388 } else if (itr->type == PLIST_FILE) {
389 TAILQ_FOREACH(wp, which_list, next) {
390 if (wp->skip == TRUE)
392 if (!cmp_path(wp->file, itr->name, cwd))
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);
398 strlcpy(wp->package, installed[i], PATH_MAX);
406 TAILQ_FOREACH(wp, which_list, next) {
407 if (wp->package[0] != '\0') {
411 printf("%s was installed by package %s\n", \
412 wp->file, wp->package);
415 while (!TAILQ_EMPTY(which_list)) {
416 wp = TAILQ_FIRST(which_list);
417 TAILQ_REMOVE(which_list, wp, next);
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
433 find_pkgs_by_origin(const char *origin)
439 printf("The following installed package(s) has %s origin:\n", origin);
441 matched = matchbyorigin(origin, &errcode);
445 for (i = 0; matched[i] != NULL; i++)
452 * List only the matching package names.
453 * Mainly intended for scripts.
456 matched_packages(char **pkgs)
461 matched = matchinstalled(MatchType == MATCH_GLOB ? MATCH_NGLOB : MatchType, pkgs, &errcode);
463 if (errcode != 0 || matched == NULL)
466 for (i = 0; matched[i]; i++)
468 printf("%s\n", matched[i]);
470 printf("%s%s\n", InfoPrefix, matched[i]);