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: src/usr.sbin/pkg_install/info/perform.c,v 1.29.2.14 2002/10/11 14:23:43 sobomax Exp $");
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 *);
36 pkg_perform(char **pkgs)
42 signal(SIGINT, cleanup);
44 /* Overriding action? */
46 return isinstalledpkg(CheckPkg) == TRUE ? 0 : 1;
48 } else if (!TAILQ_EMPTY(whead)) {
49 return find_pkg(whead);
50 } else if (LookUpOrigin != NULL) {
51 return find_pkgs_by_origin(LookUpOrigin);
54 if (MatchType != MATCH_EXACT) {
55 matched = matchinstalled(MatchType, pkgs, &errcode);
62 else switch (MatchType) {
66 warnx("no packages installed");
70 warnx("no packages match pattern(s)");
78 for (i = 0; pkgs[i]; i++)
79 err_cnt += pkg_do(pkgs[i]);
89 Boolean installed = FALSE, isTMP = FALSE;
90 char log_dir[FILENAME_MAX];
91 char fname[FILENAME_MAX], extrlist[FILENAME_MAX];
99 if ((cp = fileGetURL(NULL, pkg)) != NULL) {
104 else if (fexists(pkg) && isfile(pkg)) {
108 if (!getcwd(fname, FILENAME_MAX))
111 snprintf(&fname[len], FILENAME_MAX - len, "/%s", pkg);
118 if ((cp = fileFindByPath(NULL, pkg)) != NULL)
119 strncpy(fname, cp, FILENAME_MAX);
123 * Apply a crude heuristic to see how much space the package will
124 * take up once it's unpacked. I've noticed that most packages
125 * compress an average of 75%, but we're only unpacking the + files so
126 * be very optimistic.
128 if (stat(fname, &sb) == FAIL) {
129 warnx("can't stat package file '%s'", fname);
133 Home = make_playpen(PlayPen, sb.st_size / 2);
134 snprintf(extrlist, sizeof(extrlist), "--fast-read %s %s %s",
135 CONTENTS_FNAME, COMMENT_FNAME, DESC_FNAME);
136 if (Flags & SHOW_DISPLAY)
137 snprintf(extrlist, sizeof(extrlist), "%s %s", extrlist,
139 if (Flags & SHOW_INSTALL)
140 snprintf(extrlist, sizeof(extrlist), "%s %s %s", extrlist,
141 INSTALL_FNAME, POST_INSTALL_FNAME);
142 if (Flags & SHOW_DEINSTALL)
143 snprintf(extrlist, sizeof(extrlist), "%s %s %s", extrlist,
144 DEINSTALL_FNAME, POST_DEINSTALL_FNAME);
145 if (Flags & SHOW_MTREE)
146 snprintf(extrlist, sizeof(extrlist), "%s %s", extrlist,
148 if (unpack(fname, extrlist)) {
149 warnx("error during unpacking, no info for '%s' available", pkg);
154 /* It's not an ininstalled package, try and find it among the installed */
156 if (!isinstalledpkg(pkg)) {
157 warnx("can't find package '%s' installed or in a file!", pkg);
160 sprintf(log_dir, "%s/%s", LOG_DIR, pkg);
161 if (chdir(log_dir) == FAIL) {
162 warnx("can't change directory to '%s'!", log_dir);
168 /* Suck in the contents list */
169 plist.head = plist.tail = NULL;
170 fp = fopen(CONTENTS_FNAME, "r");
172 warnx("unable to open %s file", CONTENTS_FNAME);
176 /* If we have a prefix, add it now */
177 read_plist(&plist, fp);
181 * Index is special info type that has to override all others to make
184 if (Flags & SHOW_INDEX) {
185 char tmp[FILENAME_MAX];
187 snprintf(tmp, FILENAME_MAX, "%-19s ", pkg);
188 show_index(tmp, COMMENT_FNAME);
191 /* Start showing the package contents */
193 printf("%sInformation for %s:\n\n", InfoPrefix, pkg);
194 if (Flags & SHOW_COMMENT)
195 show_file("Comment:\n", COMMENT_FNAME);
196 if (Flags & SHOW_REQUIRE)
197 show_plist("Depends on:\n", &plist, PLIST_PKGDEP, FALSE);
198 if ((Flags & SHOW_REQBY) && !isemptyfile(REQUIRED_BY_FNAME))
199 show_file("Required by:\n", REQUIRED_BY_FNAME);
200 if (Flags & SHOW_DESC)
201 show_file("Description:\n", DESC_FNAME);
202 if ((Flags & SHOW_DISPLAY) && fexists(DISPLAY_FNAME))
203 show_file("Install notice:\n", DISPLAY_FNAME);
204 if (Flags & SHOW_PLIST)
205 show_plist("Packing list:\n", &plist, (plist_t)0, TRUE);
206 if ((Flags & SHOW_INSTALL) && fexists(INSTALL_FNAME))
207 show_file("Install script:\n", INSTALL_FNAME);
208 if ((Flags & SHOW_INSTALL) && fexists(POST_INSTALL_FNAME))
209 show_file("Post-Install script:\n", POST_INSTALL_FNAME);
210 if ((Flags & SHOW_DEINSTALL) && fexists(DEINSTALL_FNAME))
211 show_file("De-Install script:\n", DEINSTALL_FNAME);
212 if ((Flags & SHOW_DEINSTALL) && fexists(POST_DEINSTALL_FNAME))
213 show_file("Post-DeInstall script:\n", POST_DEINSTALL_FNAME);
214 if ((Flags & SHOW_MTREE) && fexists(MTREE_FNAME))
215 show_file("mtree file:\n", MTREE_FNAME);
216 if (Flags & SHOW_PREFIX)
217 show_plist("Prefix(s):\n", &plist, PLIST_CWD, FALSE);
218 if (Flags & SHOW_FILES)
219 show_files("Files:\n", &plist);
220 if ((Flags & SHOW_SIZE) && installed)
221 show_size("Package Size:\n", &plist);
222 if ((Flags & SHOW_CKSUM) && installed)
223 show_cksum("Mismatched Checksums:\n", &plist);
224 if (Flags & SHOW_ORIGIN)
225 show_origin("Origin:\n", &plist);
226 if (Flags & SHOW_FMTREV)
227 show_fmtrev("Packing list format revision:\n", &plist);
242 static int in_cleanup = 0;
253 * Return an absolute path, additionally removing all .'s, ..'s, and extraneous
254 * /'s, as realpath() would, but without resolving symlinks, because that can
255 * potentially screw up our comparisons later.
258 abspath(const char *pathname)
260 char *tmp, *tmp1, *resolved_path;
264 if (pathname[0] != '/') {
265 cwd = getcwd(NULL, MAXPATHLEN);
266 asprintf(&resolved_path, "%s/%s/", cwd, pathname);
268 asprintf(&resolved_path, "%s/", pathname);
270 if (resolved_path == NULL)
276 while ((tmp = strstr(resolved_path, "//")) != NULL)
277 strcpy(tmp, tmp + 1);
279 while ((tmp = strstr(resolved_path, "/./")) != NULL)
280 strcpy(tmp, tmp + 2);
282 while ((tmp = strstr(resolved_path, "/../")) != NULL) {
284 if ((tmp1 = strrchr(resolved_path, '/')) == NULL)
285 tmp1 = resolved_path;
286 strcpy(tmp1, tmp + 3);
289 len = strlen(resolved_path);
290 if (len > 1 && resolved_path[len - 1] == '/')
291 resolved_path[len - 1] = '\0';
293 return resolved_path;
297 * Comparison to see if the path we're on matches the
298 * one we are looking for.
301 cmp_path(const char *target, const char *current, const char *cwd)
303 char *resolved, *temp;
306 asprintf(&temp, "%s/%s", cwd, current);
311 * Make sure there's no multiple /'s or other weird things in the PLIST,
312 * since some plists seem to have them and it could screw up our strncmp.
314 resolved = abspath(temp);
316 if (strcmp(target, resolved) == 0)
327 * Look through package dbs in LOG_DIR and find which
328 * packages installed the files in which_list.
331 find_pkg(struct which_head *which_list)
335 struct which_entry *wp;
337 TAILQ_FOREACH(wp, which_list, next) {
338 const char *msg = "file cannot be found";
342 /* If it's not a file, we'll see if it's an executable. */
343 if (isfile(wp->file) == FALSE) {
344 if (strchr(wp->file, '/') == NULL) {
345 tmp = vpipe("/usr/bin/which %s", wp->file);
347 strlcpy(wp->file, tmp, PATH_MAX);
351 msg = "file is not in PATH";
354 tmp = abspath(wp->file);
356 strlcpy(wp->file, tmp, PATH_MAX);
361 if (wp->skip == TRUE)
362 warnx("%s: %s", wp->file, msg);
365 installed = matchinstalled(MATCH_ALL, NULL, &errcode);
366 if (installed == NULL)
369 for (i = 0; installed[i] != NULL; i++) {
376 snprintf(tmp, PATH_MAX, "%s/%s/%s", LOG_DIR, installed[i],
378 fp = fopen(tmp, "r");
384 pkg.head = pkg.tail = NULL;
385 read_plist(&pkg, fp);
387 for (itr = pkg.head; itr != pkg.tail; itr = itr->next) {
388 if (itr->type == PLIST_CWD) {
390 } else if (itr->type == PLIST_FILE) {
391 TAILQ_FOREACH(wp, which_list, next) {
392 if (wp->skip == TRUE)
394 if (!cmp_path(wp->file, itr->name, cwd))
396 if (wp->package[0] != '\0') {
397 warnx("both %s and %s claim to have installed %s\n",
398 wp->package, installed[i], wp->file);
400 strlcpy(wp->package, installed[i], PATH_MAX);
408 TAILQ_FOREACH(wp, which_list, next) {
409 if (wp->package[0] != '\0') {
413 printf("%s was installed by package %s\n", \
414 wp->file, wp->package);
417 while (!TAILQ_EMPTY(which_list)) {
418 wp = TAILQ_FIRST(which_list);
419 TAILQ_REMOVE(which_list, wp, next);
428 * Look through package dbs in LOG_DIR and find which
429 * packages have the given origin. Don't use read_plist()
430 * because this increases time necessary for lookup by 40
431 * times, as we don't really have to parse all plist to
435 find_pkgs_by_origin(const char *origin)
441 printf("The following installed package(s) has %s origin:\n", origin);
443 matched = matchbyorigin(origin, &errcode);
447 for (i = 0; matched[i] != NULL; i++)