1 /* $NetBSD: check.c,v 1.9 2009/04/24 14:00:25 joerg Exp $ */
10 __RCSID("$NetBSD: check.c,v 1.9 2009/04/24 14:00:25 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
];
90 char dir
[MaxPathSize
];
93 content
= pkgdb_pkg_file(pkgdir
, CONTENTS_FNAME
);
94 f
= fopen(content
, "r");
96 err(EXIT_FAILURE
, "can't open %s", content
);
99 read_plist(&Plist
, f
);
100 p
= find_plist(&Plist
, PLIST_NAME
);
102 errx(EXIT_FAILURE
, "Package %s has no @name, aborting.",
105 for (p
= Plist
.head
; p
; p
= p
->next
) {
109 warnx("dirp not initialized, please send-pr!");
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
) {
120 if (strcmp(md5file
, p
->next
->name
+ ChecksumHeaderLen
) != 0)
121 printf("%s fails MD5 checksum\n", file
);
125 } else if (strncmp(p
->next
->name
, SYMLINK_HEADER
, SymlinkHeaderLen
) == 0) {
126 char buf
[MaxPathSize
+ SymlinkHeaderLen
];
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
);
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
);
144 } else if (isbrokenlink(file
)) {
145 warnx("%s: Symlink `%s' exists and is in %s but target does not exist!", PkgName
, file
, CONTENTS_FNAME
);
147 warnx("%s: File `%s' is in %s but not on filesystem!", PkgName
, file
, CONTENTS_FNAME
);
151 if (strcmp(p
->name
, ".") != 0)
154 (void) snprintf(dir
, sizeof(dir
), "%s/%s", _pkgdb_getPKGDB_DIR(), pkgdir
);
185 struct checkpattern_arg
{
192 checkpattern_fn(const char *pkg
, void *vp
)
194 struct checkpattern_arg
*arg
= vp
;
196 check1pkg(pkg
, &arg
->filecnt
, &arg
->pkgcnt
);
206 check_pkg(const char *pkg
, int *filecnt
, int *pkgcnt
, int allow_unmatched
)
208 struct checkpattern_arg arg
;
211 arg
.filecnt
= *filecnt
;
212 arg
.pkgcnt
= *pkgcnt
;
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
;
223 if (ispkgpattern(pkg
)) {
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
);
238 *filecnt
= arg
.filecnt
;
239 *pkgcnt
= arg
.pkgcnt
;
249 setbuf(stdout
, NULL
);
252 check_pkg("*", &filecnt
, &pkgcnt
, 1);
254 for (; *argv
!= NULL
; ++argv
)
255 check_pkg(*argv
, &filecnt
, &pkgcnt
, 0);
259 printf("Checked %d file%s from %d package%s.\n",
260 filecnt
, (filecnt
== 1) ? "" : "s",
261 pkgcnt
, (pkgcnt
== 1) ? "" : "s");