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 version module. Based on pkg_version.pl by Bruce A. Mah.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
31 char IndexPath[PATH_MAX] = "";
32 struct index_head Index = SLIST_HEAD_INITIALIZER(Index);
34 static int pkg_do(char *);
35 static void show_version(Package, const char *, const char *);
38 * This is the traditional pkg_perform, except that the argument is _not_ a
39 * list of packages. It is the index file from the command line.
41 * We loop over the installed packages, matching them with the -s flag if
42 * needed and calling pkg_do(). Beforehand we set up a few things, and after
43 * we tear them down...
45 * Returns 0 on success, non-zero on failure, corresponding to the number of
46 * failed attempts to access the INDEX.
49 pkg_perform(char **indexarg)
51 char **pkgs, *pat[2], **patterns;
52 struct index_entry *ie;
53 int i, err_cnt = 0, rel_major_ver;
58 if (uname(&u) == -1) {
59 warn("%s(): failed to determine uname information", __func__);
61 } else if ((rel_major_ver = (int) strtol(u.release, NULL, 10)) <= 0) {
66 * Try to find and open the INDEX. We only check IndexFile != NULL
67 * later, if we actually need the INDEX.
69 if (*indexarg == NULL) {
70 snprintf(IndexPath, sizeof(IndexPath), "%s/INDEX-%d", PORTS_DIR,
73 strlcpy(IndexPath, *indexarg, sizeof(IndexPath));
75 IndexFile = fetchGetURL(IndexPath, "");
77 IndexFile = fopen(IndexPath, "r");
79 /* Get either a list of matching or all packages */
80 if (MatchName != NULL) {
83 MatchType = RegexExtended ? MATCH_EREGEX : MATCH_REGEX;
86 MatchType = MATCH_ALL;
90 if (LookUpOrigin != NULL)
91 pkgs = matchbyorigin(LookUpOrigin, &err_cnt);
93 pkgs = matchinstalled(MatchType, patterns, &err_cnt);
96 errx(2, "Unable to find package database directory!");
98 if (LookUpOrigin != NULL) {
99 warnx("no packages recorded with this origin");
104 warnx("no packages installed");
108 warnx("no packages match pattern");
116 for (i = 0; pkgs[i] != NULL; i++)
117 err_cnt += pkg_do(pkgs[i]);
119 /* If we opened the INDEX in pkg_do(), clean up. */
120 while (!SLIST_EMPTY(&Index)) {
121 ie = SLIST_FIRST(&Index);
122 SLIST_REMOVE_HEAD(&Index, next);
123 if (ie->name != NULL)
125 if (ie->origin != NULL)
129 if (IndexFile != NULL)
136 * Traditional pkg_do(). We take the package name we are passed and
137 * first slurp in the CONTENTS file, getting name and origin, then
138 * we look for it's corresponding Makefile. If that fails we pull in
139 * the INDEX, and check there.
144 char *ch, tmp[PATH_MAX], tmp2[PATH_MAX], *latest = NULL;
146 struct index_entry *ie;
150 /* Suck in the contents list. */
151 plist.head = plist.tail = NULL;
152 plist.name = plist.origin = NULL;
153 snprintf(tmp, PATH_MAX, "%s/%s/%s", LOG_DIR, pkg, CONTENTS_FNAME);
154 fp = fopen(tmp, "r");
156 warnx("the package info for package '%s' is corrupt", pkg);
159 read_plist(&plist, fp);
161 if (plist.name == NULL) {
162 warnx("%s does not appear to be a valid package!", pkg);
167 * First we check if the installed package has an origin, and try
168 * looking for it's Makefile. If we find the Makefile we get the
169 * latest version from there. If we fail, we start looking in the
170 * INDEX, first matching the origin and then the package name.
172 if (plist.origin != NULL && !UseINDEXOnly) {
173 snprintf(tmp, PATH_MAX, "%s/%s", PORTS_DIR, plist.origin);
174 if (isdir(tmp) && chdir(tmp) != FAIL && isfile("Makefile")) {
175 if ((latest = vpipe("/usr/bin/make -V PKGNAME", tmp)) == NULL)
176 warnx("Failed to get PKGNAME from %s/Makefile!", tmp);
178 show_version(plist, latest, "port");
181 if (latest == NULL) {
182 /* Report package as not found in INDEX if the INDEX is not required. */
183 if (IndexFile == NULL && !UseINDEXOnly)
184 show_version(plist, NULL, plist.origin);
186 /* We only pull in the INDEX once, if needed. */
187 if (SLIST_EMPTY(&Index)) {
189 errx(2, "Unable to open %s in %s.", IndexPath, __func__);
190 while ((ch = fgetln(IndexFile, &len)) != NULL) {
192 * Don't use strlcpy() because fgetln() doesn't
193 * return a valid C string.
195 strncpy(tmp, ch, MIN(len, PATH_MAX));
196 tmp[PATH_MAX-1] = '\0';
197 /* The INDEX has pkgname|portdir|... */
198 if ((ch = strchr(tmp, '|')) != NULL)
200 if (ch != NULL && (ch = strchr(&ch[1], '|')) != NULL)
202 /* Look backwards for the last two dirs = origin */
203 while (ch != NULL && *--ch != '/')
206 while (ch != NULL && *--ch != '/')
210 errx(2, "The INDEX does not appear to be valid!");
211 if ((ie = malloc(sizeof(struct index_entry))) == NULL)
212 errx(2, "Unable to allocate memory in %s.", __func__);
213 bzero(ie, sizeof(struct index_entry));
214 ie->name = strdup(tmp);
215 ie->origin = strdup(&ch[1]);
216 /* Who really cares if we reverse the index... */
217 SLIST_INSERT_HEAD(&Index, ie, next);
220 /* Now that we've slurped in the INDEX... */
221 SLIST_FOREACH(ie, &Index, next) {
222 if (plist.origin != NULL) {
223 if (strcmp(plist.origin, ie->origin) == 0)
224 latest = strdup(ie->name);
226 strlcpy(tmp, ie->name, PATH_MAX);
227 strlcpy(tmp2, plist.name, PATH_MAX);
228 /* Chop off the versions and compare. */
229 if ((ch = strrchr(tmp, '-')) == NULL)
230 errx(2, "The INDEX does not appear to be valid!");
232 if ((ch = strrchr(tmp2, '-')) == NULL)
233 warnx("%s is not a valid package!", plist.name);
236 if (strcmp(tmp2, tmp) == 0) {
237 if (latest != NULL) {
238 /* Multiple matches */
239 snprintf(tmp, PATH_MAX, "%s|%s", latest, ie->name);
241 latest = strdup(tmp);
243 latest = strdup(ie->name);
248 show_version(plist, NULL, NULL);
250 show_version(plist, latest, "index");
259 #define OUTPUT(c) ((PreventChars != NULL && !strchr(PreventChars, (c))) || \
260 (LimitChars != NULL && strchr(LimitChars, (c))) || \
261 (PreventChars == NULL && LimitChars == NULL))
264 * Do the work of comparing and outputing. Ugly, but well that's what
265 * You get when you try to match perl output in C ;-).
268 show_version(Package plist, const char *latest, const char *source)
270 char *ch, tmp[PATH_MAX];
274 if (!plist.name || strlen(plist.name) == 0)
276 if (ShowOrigin != FALSE && plist.origin != NULL)
277 strlcpy(tmp, plist.origin, PATH_MAX);
279 strlcpy(tmp, plist.name, PATH_MAX);
281 if ((ch = strrchr(tmp, '-')) != NULL)
285 if (latest == NULL) {
286 if (source == NULL && OUTPUT('!')) {
287 printf("%-34s !", tmp);
289 printf(" Comparison failed");
291 } else if (OUTPUT('?')) {
292 printf("%-34s ?", tmp);
294 printf(" orphaned: %s", plist.origin);
297 } else if (strchr(latest,'|') != NULL) {
299 printf("%-34s *", tmp);
301 strlcpy(tmp, latest, PATH_MAX);
302 ch = strchr(tmp, '|');
305 ver = strrchr(tmp, '-');
306 ver = ver ? &ver[1] : tmp;
307 printf(" multiple versions (index has %s", ver);
309 ver = strrchr(&ch[1], '-');
310 ver = ver ? &ver[1] : &ch[1];
311 if ((ch = strchr(&ch[1], '|')) != NULL)
314 } while (ch != NULL);
320 cmp = version_cmp(plist.name, latest);
321 ver = strrchr(latest, '-');
322 ver = ver ? &ver[1] : latest;
323 if (cmp < 0 && OUTPUT('<')) {
324 printf("%-34s %c", tmp, Quiet ? '\0' : '<');
326 printf(" needs updating (%s has %s)", source, ver);
328 } else if (cmp == 0 && OUTPUT('=')) {
329 printf("%-34s %c", tmp, Quiet ? '\0' : '=');
331 printf(" up-to-date with %s", source);
333 } else if (cmp > 0 && OUTPUT('>')) {
334 printf("%-34s %c", tmp, Quiet ? '\0' : '>');
336 printf(" succeeds %s (%s has %s)", source, source, ver);
343 version_match(char *pattern, const char *pkgname)
348 Boolean isTMP = FALSE;
350 if (isURL(pkgname)) {
351 fp = fetchGetURL(pkgname, "");
355 errx(2, "Unable to open %s.", pkgname);
356 } else if (pkgname[0] == '/') {
357 fp = fopen(pkgname, "r");
361 errx(2, "Unable to open %s.", pkgname);
362 } else if (strcmp(pkgname, "-") == 0) {
365 } else if (isURL(pattern)) {
366 fp = fetchGetURL(pattern, "");
370 errx(2, "Unable to open %s.", pattern);
371 } else if (pattern[0] == '/') {
372 fp = fopen(pattern, "r");
376 errx(2, "Unable to open %s.", pattern);
377 } else if (strcmp(pattern, "-") == 0) {
381 ret = pattern_match(MATCH_GLOB, pattern, pkgname);
387 while ((line = fgetln(fp, &len)) != NULL) {
391 if (len > 0 && line[len-1] == '\n')
394 if (lnlen > sizeof(ln)-1)
395 lnlen = sizeof(ln)-1;
396 memcpy(ln, line, lnlen);
398 if ((ch = strchr(ln, '|')) != NULL)
401 match = pattern_match(MATCH_GLOB, pattern, ln);
403 match = pattern_match(MATCH_GLOB, ln, pkgname);
406 printf("%.*s\n", (int)len, line);