Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / pkg_install / dist / admin / check.c
blob82f77e1bcb5d737fbc393a2927d3fc47de1976c9
1 /* $NetBSD: check.c,v 1.9 2009/04/24 14:00:25 joerg Exp $ */
3 #if HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 #include <nbcompat.h>
7 #if HAVE_SYS_CDEFS_H
8 #include <sys/cdefs.h>
9 #endif
10 __RCSID("$NetBSD: check.c,v 1.9 2009/04/24 14:00:25 joerg Exp $");
12 /*-
13 * Copyright (c) 1999-2008 The NetBSD Foundation, Inc.
14 * All rights reserved.
16 * This code is derived from software contributed to The NetBSD Foundation
17 * by Hubert Feyrer <hubert@feyrer.de>.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #if HAVE_SYS_TYPES_H
42 #include <sys/types.h>
43 #endif
44 #if HAVE_SYS_STAT_H
45 #include <sys/stat.h>
46 #endif
47 #if HAVE_DIRENT_H
48 #include <dirent.h>
49 #endif
50 #if HAVE_ERR_H
51 #include <err.h>
52 #endif
53 #if HAVE_ERRNO_H
54 #include <errno.h>
55 #endif
56 #if HAVE_FCNTL_H
57 #include <fcntl.h>
58 #endif
59 #ifndef NETBSD
60 #include <nbcompat/md5.h>
61 #else
62 #include <md5.h>
63 #endif
64 #if HAVE_LIMITS_H
65 #include <limits.h>
66 #endif
67 #if HAVE_STDIO_H
68 #include <stdio.h>
69 #endif
70 #if HAVE_STRING_H
71 #include <string.h>
72 #endif
74 #include "admin.h"
75 #include "lib.h"
77 static int checkpattern_fn(const char *, void *);
80 * Assumes CWD is in /var/db/pkg/<pkg>!
82 static void
83 check1pkg(const char *pkgdir, int *filecnt, int *pkgcnt)
85 FILE *f;
86 plist_t *p;
87 package_t Plist;
88 char *PkgName, *dirp = NULL, *md5file;
89 char file[MaxPathSize];
90 char dir[MaxPathSize];
91 char *content;
93 content = pkgdb_pkg_file(pkgdir, CONTENTS_FNAME);
94 f = fopen(content, "r");
95 if (f == NULL)
96 err(EXIT_FAILURE, "can't open %s", content);
97 free(content);
99 read_plist(&Plist, f);
100 p = find_plist(&Plist, PLIST_NAME);
101 if (p == NULL)
102 errx(EXIT_FAILURE, "Package %s has no @name, aborting.",
103 pkgdir);
104 PkgName = p->name;
105 for (p = Plist.head; p; p = p->next) {
106 switch (p->type) {
107 case PLIST_FILE:
108 if (dirp == NULL) {
109 warnx("dirp not initialized, please send-pr!");
110 abort();
113 (void) snprintf(file, sizeof(file), "%s/%s", dirp, p->name);
115 if (isfile(file) || islinktodir(file)) {
116 if (p->next && p->next->type == PLIST_COMMENT) {
117 if (strncmp(p->next->name, CHECKSUM_HEADER, ChecksumHeaderLen) == 0) {
118 if ((md5file = MD5File(file, NULL)) != NULL) {
119 /* Mismatch? */
120 if (strcmp(md5file, p->next->name + ChecksumHeaderLen) != 0)
121 printf("%s fails MD5 checksum\n", file);
123 free(md5file);
125 } else if (strncmp(p->next->name, SYMLINK_HEADER, SymlinkHeaderLen) == 0) {
126 char buf[MaxPathSize + SymlinkHeaderLen];
127 int cc;
129 (void) strlcpy(buf, SYMLINK_HEADER, sizeof(buf));
130 if ((cc = readlink(file, &buf[SymlinkHeaderLen],
131 sizeof(buf) - SymlinkHeaderLen - 1)) < 0) {
132 warnx("can't readlink `%s'", file);
133 } else {
134 buf[SymlinkHeaderLen + cc] = 0x0;
135 if (strcmp(buf, p->next->name) != 0) {
136 printf("symlink (%s) is not same as recorded value, %s: %s\n",
137 file, buf, p->next->name);
143 (*filecnt)++;
144 } else if (isbrokenlink(file)) {
145 warnx("%s: Symlink `%s' exists and is in %s but target does not exist!", PkgName, file, CONTENTS_FNAME);
146 } else {
147 warnx("%s: File `%s' is in %s but not on filesystem!", PkgName, file, CONTENTS_FNAME);
149 break;
150 case PLIST_CWD:
151 if (strcmp(p->name, ".") != 0)
152 dirp = p->name;
153 else {
154 (void) snprintf(dir, sizeof(dir), "%s/%s", _pkgdb_getPKGDB_DIR(), pkgdir);
155 dirp = dir;
157 break;
158 case PLIST_IGNORE:
159 p = p->next;
160 break;
161 case PLIST_SHOW_ALL:
162 case PLIST_SRC:
163 case PLIST_CMD:
164 case PLIST_CHMOD:
165 case PLIST_CHOWN:
166 case PLIST_CHGRP:
167 case PLIST_COMMENT:
168 case PLIST_NAME:
169 case PLIST_UNEXEC:
170 case PLIST_DISPLAY:
171 case PLIST_PKGDEP:
172 case PLIST_DIR_RM:
173 case PLIST_OPTION:
174 case PLIST_PKGCFL:
175 case PLIST_BLDDEP:
176 case PLIST_PKGDIR:
177 break;
180 free_plist(&Plist);
181 fclose(f);
182 (*pkgcnt)++;
185 struct checkpattern_arg {
186 int filecnt;
187 int pkgcnt;
188 int got_match;
191 static int
192 checkpattern_fn(const char *pkg, void *vp)
194 struct checkpattern_arg *arg = vp;
196 check1pkg(pkg, &arg->filecnt, &arg->pkgcnt);
197 if (!quiet)
198 printf(".");
200 arg->got_match = 1;
202 return 0;
205 static void
206 check_pkg(const char *pkg, int *filecnt, int *pkgcnt, int allow_unmatched)
208 struct checkpattern_arg arg;
209 char *pattern;
211 arg.filecnt = *filecnt;
212 arg.pkgcnt = *pkgcnt;
213 arg.got_match = 0;
215 if (match_installed_pkgs(pkg, checkpattern_fn, &arg) == -1)
216 errx(EXIT_FAILURE, "Cannot process pkdbdb");
217 if (arg.got_match != 0) {
218 *filecnt = arg.filecnt;
219 *pkgcnt = arg.pkgcnt;
220 return;
223 if (ispkgpattern(pkg)) {
224 if (allow_unmatched)
225 return;
226 errx(EXIT_FAILURE, "No matching pkg for %s.", pkg);
229 pattern = xasprintf("%s-[0-9]*", pkg);
231 if (match_installed_pkgs(pattern, checkpattern_fn, &arg) == -1)
232 errx(EXIT_FAILURE, "Cannot process pkdbdb");
234 if (arg.got_match == 0)
235 errx(EXIT_FAILURE, "cannot find package %s", pkg);
236 free(pattern);
238 *filecnt = arg.filecnt;
239 *pkgcnt = arg.pkgcnt;
242 void
243 check(char **argv)
245 int filecnt, pkgcnt;
247 filecnt = 0;
248 pkgcnt = 0;
249 setbuf(stdout, NULL);
251 if (*argv == NULL) {
252 check_pkg("*", &filecnt, &pkgcnt, 1);
253 } else {
254 for (; *argv != NULL; ++argv)
255 check_pkg(*argv, &filecnt, &pkgcnt, 0);
258 printf("\n");
259 printf("Checked %d file%s from %d package%s.\n",
260 filecnt, (filecnt == 1) ? "" : "s",
261 pkgcnt, (pkgcnt == 1) ? "" : "s");