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.
19 * $FreeBSD: src/usr.sbin/pkg_install/info/perform.c,v 1.29.2.14 2002/10/11 14:23:43 sobomax Exp $
20 * $DragonFly: src/usr.sbin/pkg_install/info/Attic/perform.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
28 static int pkg_do(char *);
29 static int find_pkg(struct which_head *);
30 static int cmp_path(const char *, const char *, const char *);
31 static char *abspath(const char *);
32 static int find_pkgs_by_origin(const char *);
35 pkg_perform(char **pkgs)
41 signal(SIGINT, cleanup);
43 /* Overriding action? */
45 return isinstalledpkg(CheckPkg) == TRUE ? 0 : 1;
47 } else if (!TAILQ_EMPTY(whead)) {
48 return find_pkg(whead);
49 } else if (LookUpOrigin != NULL) {
50 return find_pkgs_by_origin(LookUpOrigin);
53 if (MatchType != MATCH_EXACT) {
54 matched = matchinstalled(MatchType, pkgs, &errcode);
61 else switch (MatchType) {
65 warnx("no packages installed");
69 warnx("no packages match pattern(s)");
77 for (i = 0; pkgs[i]; i++)
78 err_cnt += pkg_do(pkgs[i]);
88 Boolean installed = FALSE, isTMP = FALSE;
89 char log_dir[FILENAME_MAX];
90 char fname[FILENAME_MAX], extrlist[FILENAME_MAX];
98 if ((cp = fileGetURL(NULL, pkg)) != NULL) {
103 else if (fexists(pkg) && isfile(pkg)) {
107 if (!getcwd(fname, FILENAME_MAX))
110 snprintf(&fname[len], FILENAME_MAX - len, "/%s", pkg);
117 if ((cp = fileFindByPath(NULL, pkg)) != NULL)
118 strncpy(fname, cp, FILENAME_MAX);
122 * Apply a crude heuristic to see how much space the package will
123 * take up once it's unpacked. I've noticed that most packages
124 * compress an average of 75%, but we're only unpacking the + files so
125 * be very optimistic.
127 if (stat(fname, &sb) == FAIL) {
128 warnx("can't stat package file '%s'", fname);
132 Home = make_playpen(PlayPen, sb.st_size / 2);
133 snprintf(extrlist, sizeof(extrlist), "--fast-read %s %s %s",
134 CONTENTS_FNAME, COMMENT_FNAME, DESC_FNAME);
135 if (Flags & SHOW_DISPLAY)
136 snprintf(extrlist, sizeof(extrlist), "%s %s", extrlist,
138 if (Flags & SHOW_INSTALL)
139 snprintf(extrlist, sizeof(extrlist), "%s %s %s", extrlist,
140 INSTALL_FNAME, POST_INSTALL_FNAME);
141 if (Flags & SHOW_DEINSTALL)
142 snprintf(extrlist, sizeof(extrlist), "%s %s %s", extrlist,
143 DEINSTALL_FNAME, POST_DEINSTALL_FNAME);
144 if (Flags & SHOW_MTREE)
145 snprintf(extrlist, sizeof(extrlist), "%s %s", extrlist,
147 if (unpack(fname, extrlist)) {
148 warnx("error during unpacking, no info for '%s' available", pkg);
153 /* It's not an ininstalled package, try and find it among the installed */
155 if (!isinstalledpkg(pkg)) {
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);
193 if (Flags & SHOW_COMMENT)
194 show_file("Comment:\n", COMMENT_FNAME);
195 if (Flags & SHOW_REQUIRE)
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_INSTALL) && fexists(INSTALL_FNAME))
206 show_file("Install script:\n", INSTALL_FNAME);
207 if ((Flags & SHOW_INSTALL) && fexists(POST_INSTALL_FNAME))
208 show_file("Post-Install script:\n", POST_INSTALL_FNAME);
209 if ((Flags & SHOW_DEINSTALL) && fexists(DEINSTALL_FNAME))
210 show_file("De-Install script:\n", DEINSTALL_FNAME);
211 if ((Flags & SHOW_DEINSTALL) && fexists(POST_DEINSTALL_FNAME))
212 show_file("Post-DeInstall script:\n", POST_DEINSTALL_FNAME);
213 if ((Flags & SHOW_MTREE) && fexists(MTREE_FNAME))
214 show_file("mtree file:\n", MTREE_FNAME);
215 if (Flags & SHOW_PREFIX)
216 show_plist("Prefix(s):\n", &plist, PLIST_CWD, FALSE);
217 if (Flags & SHOW_FILES)
218 show_files("Files:\n", &plist);
219 if ((Flags & SHOW_SIZE) && installed)
220 show_size("Package Size:\n", &plist);
221 if ((Flags & SHOW_CKSUM) && installed)
222 show_cksum("Mismatched Checksums:\n", &plist);
223 if (Flags & SHOW_ORIGIN)
224 show_origin("Origin:\n", &plist);
225 if (Flags & SHOW_FMTREV)
226 show_fmtrev("Packing list format revision:\n", &plist);
241 static int in_cleanup = 0;
252 * Return an absolute path, additionally removing all .'s, ..'s, and extraneous
253 * /'s, as realpath() would, but without resolving symlinks, because that can
254 * potentially screw up our comparisons later.
257 abspath(const char *pathname)
259 char *tmp, *tmp1, *resolved_path;
263 if (pathname[0] != '/') {
264 cwd = getcwd(NULL, MAXPATHLEN);
265 asprintf(&resolved_path, "%s/%s/", cwd, pathname);
267 asprintf(&resolved_path, "%s/", pathname);
269 if (resolved_path == NULL)
275 while ((tmp = strstr(resolved_path, "//")) != NULL)
276 strcpy(tmp, tmp + 1);
278 while ((tmp = strstr(resolved_path, "/./")) != NULL)
279 strcpy(tmp, tmp + 2);
281 while ((tmp = strstr(resolved_path, "/../")) != NULL) {
283 if ((tmp1 = strrchr(resolved_path, '/')) == NULL)
284 tmp1 = resolved_path;
285 strcpy(tmp1, tmp + 3);
288 len = strlen(resolved_path);
289 if (len > 1 && resolved_path[len - 1] == '/')
290 resolved_path[len - 1] = '\0';
292 return resolved_path;
296 * Comparison to see if the path we're on matches the
297 * one we are looking for.
300 cmp_path(const char *target, const char *current, const char *cwd)
302 char *resolved, *temp;
305 asprintf(&temp, "%s/%s", cwd, current);
310 * Make sure there's no multiple /'s or other weird things in the PLIST,
311 * since some plists seem to have them and it could screw up our strncmp.
313 resolved = abspath(temp);
315 if (strcmp(target, resolved) == 0)
326 * Look through package dbs in LOG_DIR and find which
327 * packages installed the files in which_list.
330 find_pkg(struct which_head *which_list)
334 struct which_entry *wp;
336 TAILQ_FOREACH(wp, which_list, next) {
337 const char *msg = "file cannot be found";
341 /* If it's not a file, we'll see if it's an executable. */
342 if (isfile(wp->file) == FALSE) {
343 if (strchr(wp->file, '/') == NULL) {
344 tmp = vpipe("/usr/bin/which %s", wp->file);
346 strlcpy(wp->file, tmp, PATH_MAX);
350 msg = "file is not in PATH";
353 tmp = abspath(wp->file);
355 strlcpy(wp->file, tmp, PATH_MAX);
360 if (wp->skip == TRUE)
361 warnx("%s: %s", wp->file, msg);
364 installed = matchinstalled(MATCH_ALL, NULL, &errcode);
365 if (installed == NULL)
368 for (i = 0; installed[i] != NULL; i++) {
375 snprintf(tmp, PATH_MAX, "%s/%s/%s", LOG_DIR, installed[i],
377 fp = fopen(tmp, "r");
383 pkg.head = pkg.tail = NULL;
384 read_plist(&pkg, fp);
386 for (itr = pkg.head; itr != pkg.tail; itr = itr->next) {
387 if (itr->type == PLIST_CWD) {
389 } else if (itr->type == PLIST_FILE) {
390 TAILQ_FOREACH(wp, which_list, next) {
391 if (wp->skip == TRUE)
393 if (!cmp_path(wp->file, itr->name, cwd))
395 if (wp->package[0] != '\0') {
396 warnx("both %s and %s claim to have installed %s\n",
397 wp->package, installed[i], wp->file);
399 strlcpy(wp->package, installed[i], PATH_MAX);
407 TAILQ_FOREACH(wp, which_list, next) {
408 if (wp->package[0] != '\0') {
412 printf("%s was installed by package %s\n", \
413 wp->file, wp->package);
416 while (!TAILQ_EMPTY(which_list)) {
417 wp = TAILQ_FIRST(which_list);
418 TAILQ_REMOVE(which_list, wp, next);
427 * Look through package dbs in LOG_DIR and find which
428 * packages have the given origin. Don't use read_plist()
429 * because this increases time necessary for lookup by 40
430 * times, as we don't really have to parse all plist to
434 find_pkgs_by_origin(const char *origin)
440 printf("The following installed package(s) has %s origin:\n", origin);
442 matched = matchbyorigin(origin, &errcode);
446 for (i = 0; matched[i] != NULL; i++)