]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pkg_install/version/perform.c
This commit was generated by cvs2svn to compensate for changes in r151497,
[FreeBSD/FreeBSD.git] / usr.sbin / pkg_install / version / perform.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
3  * of non-core utilities.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
14  * Jeremy D. Lea.
15  * 11 May 2002
16  *
17  * This is the version module. Based on pkg_version.pl by Bruce A. Mah.
18  *
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include "lib.h"
25 #include "version.h"
26 #include <err.h>
27 #include <fetch.h>
28 #include <signal.h>
29
30 FILE *IndexFile;
31 struct index_head Index = SLIST_HEAD_INITIALIZER(Index);
32
33 static int pkg_do(char *);
34 static void show_version(Package, const char *, const char *);
35
36 /*
37  * This is the traditional pkg_perform, except that the argument is _not_
38  * a list of packages. It is the index file from the command line.
39  *
40  * We loop over the installed packages, matching them with the -s flag
41  * if needed and calling pkg_do(). Before hand we set up a few things,
42  * and after we tear them down...
43  */
44 int
45 pkg_perform(char **indexarg)
46 {
47     char tmp[PATH_MAX], **pkgs, *pat[2], **patterns;
48     struct index_entry *ie;
49     int i, err_cnt = 0;
50     int MatchType;
51
52     /*
53      * Try to find and open the INDEX. We only check IndexFile != NULL
54      * later, if we actually need the INDEX.
55      */
56     if (*indexarg == NULL)
57         snprintf(tmp, PATH_MAX, "%s/%s", PORTS_DIR, INDEX_FNAME);
58     else
59         strlcpy(tmp, *indexarg, PATH_MAX);
60     if (isURL(tmp))
61         IndexFile = fetchGetURL(tmp, "");
62     else
63         IndexFile = fopen(tmp, "r");
64
65     /* Get either a list of matching or all packages */
66     if (MatchName != NULL) {
67         pat[0] = MatchName;
68         pat[1] = NULL;
69         MatchType = RegexExtended ? MATCH_EREGEX : MATCH_REGEX;
70         patterns = pat;
71      } else {
72         MatchType = MATCH_ALL;
73         patterns = NULL;
74      }
75
76     if (LookUpOrigin != NULL)
77         pkgs = matchbyorigin(LookUpOrigin, &err_cnt);
78     else
79         pkgs = matchinstalled(MatchType, patterns, &err_cnt);
80
81     if (err_cnt != 0)
82         errx(2, "Unable to find package database directory!");
83     if (pkgs == NULL) {
84         if (LookUpOrigin != NULL) {
85             warnx("no packages recorded with this origin");
86             return (1);
87         } else {
88             switch (MatchType) {
89             case MATCH_ALL:
90                 warnx("no packages installed");
91                 return (0);
92             case MATCH_EREGEX:
93             case MATCH_REGEX:
94                 warnx("no packages match pattern");
95                 return (1);
96             default:
97                 break;
98             }
99         }
100     }
101
102     for (i = 0; pkgs[i] != NULL; i++)
103         err_cnt += pkg_do(pkgs[i]);
104
105     /* If we opened the INDEX in pkg_do(), clean up. */
106     while (!SLIST_EMPTY(&Index)) {
107         ie = SLIST_FIRST(&Index);
108         SLIST_REMOVE_HEAD(&Index, next);
109         if (ie->name != NULL)
110             free(ie->name);
111         if (ie->origin != NULL)
112             free(ie->origin);
113         free(ie);
114     }
115     if (IndexFile != NULL)
116         fclose(IndexFile);
117
118     return err_cnt;
119 }
120
121 /*
122  * Traditional pkg_do(). We take the package name we are passed and
123  * first slurp in the CONTENTS file, getting name and origin, then
124  * we look for it's corresponding Makefile. If that fails we pull in
125  * the INDEX, and check there.
126  */
127 static int
128 pkg_do(char *pkg)
129 {
130     char *ch, tmp[PATH_MAX], tmp2[PATH_MAX], *latest = NULL;
131     Package plist;
132     struct index_entry *ie;
133     FILE *fp;
134     size_t len;
135
136     /* Suck in the contents list. */
137     plist.head = plist.tail = NULL;
138     plist.name = plist.origin = NULL;
139     snprintf(tmp, PATH_MAX, "%s/%s/%s", LOG_DIR, pkg, CONTENTS_FNAME);
140     fp = fopen(tmp, "r");
141     if (!fp) {
142         warnx("the package info for package '%s' is corrupt", pkg);
143         return 1;
144     }
145     read_plist(&plist, fp);
146     fclose(fp);
147     if (plist.name == NULL) {
148         warnx("%s does not appear to be a valid package!", pkg);
149         return 1;
150     }
151
152     /*
153      * First we check if the installed package has an origin, and try
154      * looking for it's Makefile. If we find the Makefile we get the
155      * latest version from there. If we fail, we start looking in the
156      * INDEX, first matching the origin and then the package name.
157      */
158     if (plist.origin != NULL && !UseINDEXOnly) {
159         snprintf(tmp, PATH_MAX, "%s/%s", PORTS_DIR, plist.origin);
160         if (isdir(tmp) && chdir(tmp) != FAIL && isfile("Makefile")) {
161             if ((latest = vpipe("/usr/bin/make -V PKGNAME", tmp)) == NULL)
162                 warnx("Failed to get PKGNAME from %s/Makefile!", tmp);
163             else
164                 show_version(plist, latest, "port");
165         }
166     }
167     if (latest == NULL) {
168         /* Report package as not found in INDEX if the INDEX is not required. */
169         if (IndexFile == NULL && !UseINDEXOnly)
170                 show_version(plist, NULL, plist.origin);
171         else {
172         /* We only pull in the INDEX once, if needed. */
173         if (SLIST_EMPTY(&Index)) {
174             if (!IndexFile)
175                 errx(2, "Unable to open INDEX in %s.", __func__);
176             while ((ch = fgetln(IndexFile, &len)) != NULL) {
177                 /*
178                  * Don't use strlcpy() because fgetln() doesn't
179                  * return a valid C string.
180                  */
181                 strncpy(tmp, ch, MIN(len, PATH_MAX));
182                 tmp[PATH_MAX-1] = '\0';
183                 /* The INDEX has pkgname|portdir|... */
184                 if ((ch = strchr(tmp, '|')) != NULL)
185                     ch[0] = '\0';
186                 if (ch != NULL && (ch = strchr(&ch[1], '|')) != NULL)
187                     ch[0] = '\0';
188                 /* Look backwards for the last two dirs = origin */
189                 while (ch != NULL && *--ch != '/')
190                     if (ch[0] == '\0')
191                         ch = NULL;
192                 while (ch != NULL && *--ch != '/')
193                     if (ch[0] == '\0')
194                         ch = NULL;
195                 if (ch == NULL)
196                     errx(2, "The INDEX does not appear to be valid!");
197                 if ((ie = malloc(sizeof(struct index_entry))) == NULL)
198                     errx(2, "Unable to allocate memory in %s.", __func__);
199                 bzero(ie, sizeof(struct index_entry));
200                 ie->name = strdup(tmp);
201                 ie->origin = strdup(&ch[1]);
202                 /* Who really cares if we reverse the index... */
203                 SLIST_INSERT_HEAD(&Index, ie, next);
204             }
205         }
206         /* Now that we've slurped in the INDEX... */
207         SLIST_FOREACH(ie, &Index, next) {
208             if (plist.origin != NULL) {
209                 if (strcmp(plist.origin, ie->origin) == 0)
210                     latest = strdup(ie->name);
211             } else {
212                 strlcpy(tmp, ie->name, PATH_MAX);
213                 strlcpy(tmp2, plist.name, PATH_MAX);
214                 /* Chop off the versions and compare. */
215                 if ((ch = strrchr(tmp, '-')) == NULL)
216                     errx(2, "The INDEX does not appear to be valid!");
217                 ch[0] = '\0';
218                 if ((ch = strrchr(tmp2, '-')) == NULL)
219                     warnx("%s is not a valid package!", plist.name);
220                 else
221                     ch[0] = '\0';
222                 if (strcmp(tmp2, tmp) == 0) {
223                     if (latest != NULL) {
224                         /* Multiple matches */
225                         snprintf(tmp, PATH_MAX, "%s|%s", latest, ie->name);
226                         free(latest);
227                         latest = strdup(tmp);
228                     } else
229                         latest = strdup(ie->name);
230                 }
231             }
232         }
233         if (latest == NULL)
234             show_version(plist, NULL, NULL);
235         else
236             show_version(plist, latest, "index");
237         }
238     }
239     if (latest != NULL)
240         free(latest);
241     free_plist(&plist);
242     return 0;
243 }
244
245 #define OUTPUT(c) ((PreventChars != NULL && !strchr(PreventChars, (c))) || \
246                         (LimitChars != NULL && strchr(LimitChars, (c))) || \
247                         (PreventChars == NULL && LimitChars == NULL))
248
249 /*
250  * Do the work of comparing and outputing. Ugly, but well that's what
251  * You get when you try to match perl output in C ;-).
252  */
253 void
254 show_version(Package plist, const char *latest, const char *source)
255 {
256     char *ch, tmp[PATH_MAX];
257     const char *ver;
258     int cmp = 0;
259
260     if (!plist.name || strlen(plist.name) == 0)
261         return;
262     if (ShowOrigin != FALSE)
263         strlcpy(tmp, plist.origin, PATH_MAX);
264     else
265         strlcpy(tmp, plist.name, PATH_MAX);
266     if (!Verbose) {
267         if ((ch = strrchr(tmp, '-')) != NULL)
268             ch[0] = '\0';
269     }
270     if (latest == NULL) {
271         if (source == NULL && OUTPUT('!')) {
272             printf("%-34s  !", tmp);
273             if (Verbose)
274                 printf("   Comparison failed");
275             printf("\n");
276         } else if (OUTPUT('?')) {
277             printf("%-34s  ?", tmp);
278             if (Verbose)
279                 printf("   orphaned: %s", plist.origin);
280             printf("\n");
281         }
282     } else if (strchr(latest,'|') != NULL) {
283         if (OUTPUT('*')) {
284             printf("%-34s  *", tmp);
285             if (Verbose) {
286                 strlcpy(tmp, latest, PATH_MAX);
287                 ch = strchr(tmp, '|');
288                 ch[0] = '\0';
289
290                 ver = strrchr(tmp, '-');
291                 ver = ver ? &ver[1] : tmp;
292                 printf("   multiple versions (index has %s", ver);
293                 do {
294                     ver = strrchr(&ch[1], '-');
295                     ver = ver ? &ver[1] : &ch[1];
296                     if ((ch = strchr(&ch[1], '|')) != NULL)
297                             ch[0] = '\0';
298                     printf(", %s", ver);
299                 } while (ch != NULL);
300                 printf(")");
301             }
302             printf("\n");
303         }
304     } else {
305         cmp = version_cmp(plist.name, latest);
306         ver = strrchr(latest, '-');
307         ver = ver ? &ver[1] : latest;
308         if (cmp < 0 && OUTPUT('<')) {
309             printf("%-34s  %c", tmp, Quiet ? '\0' : '<');
310             if (Verbose)
311                 printf("   needs updating (%s has %s)", source, ver);
312             printf("\n");
313         } else if (cmp == 0 && OUTPUT('=')) {
314             printf("%-34s  %c", tmp, Quiet ? '\0' : '=');
315             if (Verbose)
316                 printf("   up-to-date with %s", source);
317             printf("\n");
318         } else if (cmp > 0 && OUTPUT('>')) {
319             printf("%-34s  %c", tmp, Quiet ? '\0' : '>');
320             if (Verbose)
321                 printf("   succeeds %s (%s has %s)", source, source, ver);
322             printf("\n");
323         }
324     }
325 }
326
327 int
328 version_match(char *pattern, const char *pkgname)
329 {
330     int ret = 0;
331     int matchstream = 0;
332     FILE *fp = NULL;
333     Boolean isTMP = FALSE;
334
335     if (isURL(pkgname)) {
336         fp = fetchGetURL(pkgname, "");
337         isTMP = TRUE;
338         matchstream = 1;
339         if (fp == NULL)
340             errx(2, "Unable to open %s.", pkgname);
341     } else if (pkgname[0] == '/') {
342         fp = fopen(pkgname, "r");
343         isTMP = TRUE;
344         matchstream = 1;
345         if (fp == NULL)
346             errx(2, "Unable to open %s.", pkgname);
347     } else if (strcmp(pkgname, "-") == 0) {
348         fp = stdin;
349         matchstream = 1;
350     } else if (isURL(pattern)) {
351         fp = fetchGetURL(pattern, "");
352         isTMP = TRUE;
353         matchstream = -1;
354         if (fp == NULL)
355             errx(2, "Unable to open %s.", pattern);
356     } else if (pattern[0] == '/') {
357         fp = fopen(pattern, "r");
358         isTMP = TRUE;
359         matchstream = -1;
360         if (fp == NULL)
361             errx(2, "Unable to open %s.", pattern);
362     } else if (strcmp(pattern, "-") == 0) {
363         fp = stdin;
364         matchstream = -1;
365     } else {
366         ret = pattern_match(MATCH_GLOB, pattern, pkgname);
367     }
368
369     if (fp != NULL) {
370         size_t len;
371         char *line;
372         while ((line = fgetln(fp, &len)) != NULL) {
373             int match;
374             char *ch, ln[2048];
375             size_t lnlen;
376             if (len > 0 && line[len-1] == '\n')
377                 len --;
378             lnlen = len;
379             if (lnlen > sizeof(ln)-1)
380                 lnlen = sizeof(ln)-1;
381             memcpy(ln, line, lnlen);
382             ln[lnlen] = '\0';
383             if ((ch = strchr(ln, '|')) != NULL)
384                 ch[0] = '\0';
385             if (matchstream > 0)
386                 match = pattern_match(MATCH_GLOB, pattern, ln);
387             else
388                 match = pattern_match(MATCH_GLOB, ln, pkgname);
389             if (match == 1) {
390                 ret = 1;
391                 printf("%.*s\n", (int)len, line);
392             }
393         }
394         if (isTMP)
395             fclose(fp);
396     }
397
398     return ret;
399 }
400
401 void
402 cleanup(int sig)
403 {
404     if (sig)
405         exit(1);
406 }