1 /* $NetBSD: check.c,v 1.1.1.4 2010/01/30 21:33:23 joerg Exp $ */
10 __RCSID("$NetBSD: check.c,v 1.1.1.4 2010/01/30 21:33:23 joerg Exp $");
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
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.
42 #include <sys/types.h>
60 #include <nbcompat/md5.h>
77 static int checkpattern_fn(const char *, void *);
80 * Assumes CWD is in /var/db/pkg/<pkg>!
83 check1pkg(const char *pkgdir
, int *filecnt
, int *pkgcnt
)
88 char *PkgName
, *dirp
= NULL
, *md5file
;
89 char file
[MaxPathSize
];
92 content
= pkgdb_pkg_file(pkgdir
, CONTENTS_FNAME
);
93 f
= fopen(content
, "r");
95 err(EXIT_FAILURE
, "can't open %s", content
);
98 read_plist(&Plist
, f
);
99 p
= find_plist(&Plist
, PLIST_NAME
);
101 errx(EXIT_FAILURE
, "Package %s has no @name, aborting.",
104 for (p
= Plist
.head
; p
; p
= p
->next
) {
108 warnx("dirp not initialized, please send-pr!");
112 (void) snprintf(file
, sizeof(file
), "%s/%s", dirp
, p
->name
);
114 if (isfile(file
) || islinktodir(file
)) {
115 if (p
->next
&& p
->next
->type
== PLIST_COMMENT
) {
116 if (strncmp(p
->next
->name
, CHECKSUM_HEADER
, ChecksumHeaderLen
) == 0) {
117 if ((md5file
= MD5File(file
, NULL
)) != NULL
) {
119 if (strcmp(md5file
, p
->next
->name
+ ChecksumHeaderLen
) != 0)
120 printf("%s fails MD5 checksum\n", file
);
124 } else if (strncmp(p
->next
->name
, SYMLINK_HEADER
, SymlinkHeaderLen
) == 0) {
125 char buf
[MaxPathSize
+ SymlinkHeaderLen
];
128 (void) strlcpy(buf
, SYMLINK_HEADER
, sizeof(buf
));
129 if ((cc
= readlink(file
, &buf
[SymlinkHeaderLen
],
130 sizeof(buf
) - SymlinkHeaderLen
- 1)) < 0) {
131 warnx("can't readlink `%s'", file
);
133 buf
[SymlinkHeaderLen
+ cc
] = 0x0;
134 if (strcmp(buf
, p
->next
->name
) != 0) {
135 printf("symlink (%s) is not same as recorded value, %s: %s\n",
136 file
, buf
, p
->next
->name
);
143 } else if (isbrokenlink(file
)) {
144 warnx("%s: Symlink `%s' exists and is in %s but target does not exist!", PkgName
, file
, CONTENTS_FNAME
);
146 warnx("%s: File `%s' is in %s but not on filesystem!", PkgName
, file
, CONTENTS_FNAME
);
150 if (strcmp(p
->name
, ".") != 0)
153 dirp
= pkgdb_pkg_dir(pkgdir
);
182 struct checkpattern_arg
{
189 checkpattern_fn(const char *pkg
, void *vp
)
191 struct checkpattern_arg
*arg
= vp
;
193 check1pkg(pkg
, &arg
->filecnt
, &arg
->pkgcnt
);
203 check_pkg(const char *pkg
, int *filecnt
, int *pkgcnt
, int allow_unmatched
)
205 struct checkpattern_arg arg
;
208 arg
.filecnt
= *filecnt
;
209 arg
.pkgcnt
= *pkgcnt
;
212 if (match_installed_pkgs(pkg
, checkpattern_fn
, &arg
) == -1)
213 errx(EXIT_FAILURE
, "Cannot process pkdbdb");
214 if (arg
.got_match
!= 0) {
215 *filecnt
= arg
.filecnt
;
216 *pkgcnt
= arg
.pkgcnt
;
220 if (ispkgpattern(pkg
)) {
223 errx(EXIT_FAILURE
, "No matching pkg for %s.", pkg
);
226 pattern
= xasprintf("%s-[0-9]*", pkg
);
228 if (match_installed_pkgs(pattern
, checkpattern_fn
, &arg
) == -1)
229 errx(EXIT_FAILURE
, "Cannot process pkdbdb");
231 if (arg
.got_match
== 0)
232 errx(EXIT_FAILURE
, "cannot find package %s", pkg
);
235 *filecnt
= arg
.filecnt
;
236 *pkgcnt
= arg
.pkgcnt
;
246 setbuf(stdout
, NULL
);
249 check_pkg("*", &filecnt
, &pkgcnt
, 1);
251 for (; *argv
!= NULL
; ++argv
)
252 check_pkg(*argv
, &filecnt
, &pkgcnt
, 0);
256 printf("Checked %d file%s from %d package%s.\n",
257 filecnt
, (filecnt
== 1) ? "" : "s",
258 pkgcnt
, (pkgcnt
== 1) ? "" : "s");