Split check functions into a separate file.

Drop checkall and implement it using "*" as wildcard match.
This commit is contained in:
joerg 2008-03-09 19:02:27 +00:00
parent 7065ab1326
commit 4aec8613d2
4 changed files with 328 additions and 224 deletions

View file

@ -1,4 +1,4 @@
# $NetBSD: Makefile.in,v 1.11 2007/07/16 09:57:57 joerg Exp $
# $NetBSD: Makefile.in,v 1.12 2008/03/09 19:02:27 joerg Exp $
srcdir= @srcdir@
@ -22,7 +22,7 @@ INSTALL= @INSTALL@
PROG= pkg_admin
OBJS= main.o
OBJS= check.o main.o
all: $(PROG)

View file

@ -0,0 +1,36 @@
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
extern int quiet;
void check(char **);

View file

@ -0,0 +1,285 @@
/* $NetBSD: check.c,v 1.1 2008/03/09 19:02:27 joerg Exp $ */
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <nbcompat.h>
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#ifndef lint
__RCSID("$NetBSD: check.c,v 1.1 2008/03/09 19:02:27 joerg Exp $");
#endif
/*-
* Copyright (c) 1999-2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Hubert Feyrer <hubert@feyrer.de>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_DIRENT_H
#include <dirent.h>
#endif
#if HAVE_ERR_H
#include <err.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_MD5_H
#include <md5.h>
#endif
#if HAVE_LIMITS_H
#include <limits.h>
#endif
#if HAVE_STDIO_H
#include <stdio.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#include "admin.h"
#include "lib.h"
static int checkpattern_fn(const char *, void *);
/*
* Assumes CWD is in /var/db/pkg/<pkg>!
*/
static void
check1pkg(const char *pkgdir, int *filecnt, int *pkgcnt)
{
FILE *f;
plist_t *p;
package_t Plist;
char *PkgName, *dirp = NULL, *md5file;
char file[MaxPathSize];
char dir[MaxPathSize];
f = fopen(CONTENTS_FNAME, "r");
if (f == NULL)
err(EXIT_FAILURE, "can't open %s/%s/%s", _pkgdb_getPKGDB_DIR(), pkgdir, CONTENTS_FNAME);
Plist.head = Plist.tail = NULL;
read_plist(&Plist, f);
p = find_plist(&Plist, PLIST_NAME);
if (p == NULL)
errx(EXIT_FAILURE, "Package %s has no @name, aborting.",
pkgdir);
PkgName = p->name;
for (p = Plist.head; p; p = p->next) {
switch (p->type) {
case PLIST_FILE:
if (dirp == NULL) {
warnx("dirp not initialized, please send-pr!");
abort();
}
(void) snprintf(file, sizeof(file), "%s/%s", dirp, p->name);
if (isfile(file) || islinktodir(file)) {
if (p->next && p->next->type == PLIST_COMMENT) {
if (strncmp(p->next->name, CHECKSUM_HEADER, ChecksumHeaderLen) == 0) {
if ((md5file = MD5File(file, NULL)) != NULL) {
/* Mismatch? */
#ifdef PKGDB_DEBUG
printf("%s: md5 should=<%s>, is=<%s>\n",
file, p->next->name + ChecksumHeaderLen, md5file);
#endif
if (strcmp(md5file, p->next->name + ChecksumHeaderLen) != 0)
printf("%s fails MD5 checksum\n", file);
free(md5file);
}
} else if (strncmp(p->next->name, SYMLINK_HEADER, SymlinkHeaderLen) == 0) {
char buf[MaxPathSize + SymlinkHeaderLen];
int cc;
(void) strlcpy(buf, SYMLINK_HEADER, sizeof(buf));
if ((cc = readlink(file, &buf[SymlinkHeaderLen],
sizeof(buf) - SymlinkHeaderLen - 1)) < 0) {
warnx("can't readlink `%s'", file);
} else {
buf[SymlinkHeaderLen + cc] = 0x0;
if (strcmp(buf, p->next->name) != 0) {
printf("symlink (%s) is not same as recorded value, %s: %s\n",
file, buf, p->next->name);
}
}
}
}
(*filecnt)++;
} else if (isbrokenlink(file)) {
warnx("%s: Symlink `%s' exists and is in %s but target does not exist!", PkgName, file, CONTENTS_FNAME);
} else {
warnx("%s: File `%s' is in %s but not on filesystem!", PkgName, file, CONTENTS_FNAME);
}
break;
case PLIST_CWD:
if (strcmp(p->name, ".") != 0)
dirp = p->name;
else {
(void) snprintf(dir, sizeof(dir), "%s/%s", _pkgdb_getPKGDB_DIR(), pkgdir);
dirp = dir;
}
break;
case PLIST_IGNORE:
p = p->next;
break;
case PLIST_SHOW_ALL:
case PLIST_SRC:
case PLIST_CMD:
case PLIST_CHMOD:
case PLIST_CHOWN:
case PLIST_CHGRP:
case PLIST_COMMENT:
case PLIST_NAME:
case PLIST_UNEXEC:
case PLIST_DISPLAY:
case PLIST_PKGDEP:
case PLIST_MTREE:
case PLIST_DIR_RM:
case PLIST_IGNORE_INST:
case PLIST_OPTION:
case PLIST_PKGCFL:
case PLIST_BLDDEP:
break;
}
}
free_plist(&Plist);
fclose(f);
(*pkgcnt)++;
}
struct checkpattern_arg {
int filecnt;
int pkgcnt;
int got_match;
};
static int
checkpattern_fn(const char *pkg, void *vp)
{
struct checkpattern_arg *arg = vp;
int rc;
rc = chdir(pkg);
if (rc == -1)
err(EXIT_FAILURE, "Cannot chdir to %s/%s", _pkgdb_getPKGDB_DIR(), pkg);
check1pkg(pkg, &arg->filecnt, &arg->pkgcnt);
if (!quiet) {
printf(".");
}
chdir("..");
arg->got_match = 1;
return 0;
}
static void
check_pkg(const char *pkg, int *filecnt, int *pkgcnt, int allow_unmatched)
{
struct checkpattern_arg arg;
char *pattern;
arg.filecnt = *filecnt;
arg.pkgcnt = *pkgcnt;
arg.got_match = 0;
if (match_installed_pkgs(pkg, checkpattern_fn, &arg) == -1)
errx(EXIT_FAILURE, "Cannot process pkdbdb");
if (arg.got_match != 0) {
*filecnt = arg.filecnt;
*pkgcnt = arg.pkgcnt;
return;
}
if (ispkgpattern(pkg)) {
if (allow_unmatched)
return;
errx(EXIT_FAILURE, "No matching pkg for %s.", pkg);
}
if (asprintf(&pattern, "%s-[0-9]*", pkg) == -1)
errx(EXIT_FAILURE, "asprintf failed");
if (match_installed_pkgs(pattern, checkpattern_fn, &arg) == -1)
errx(EXIT_FAILURE, "Cannot process pkdbdb");
if (arg.got_match == 0)
errx(EXIT_FAILURE, "cannot find package %s", pkg);
free(pattern);
*filecnt = arg.filecnt;
*pkgcnt = arg.pkgcnt;
}
void
check(char **argv)
{
int filecnt, pkgcnt, rc;
filecnt = 0;
pkgcnt = 0;
setbuf(stdout, NULL);
rc = chdir(_pkgdb_getPKGDB_DIR());
if (rc == -1)
err(EXIT_FAILURE, "Cannot chdir to %s", _pkgdb_getPKGDB_DIR());
if (*argv == NULL) {
check_pkg("*", &filecnt, &pkgcnt, 1);
} else {
for (; *argv != NULL; ++argv)
check_pkg(*argv, &filecnt, &pkgcnt, 0);
}
printf("\n");
printf("Checked %d file%s from %d package%s.\n",
filecnt, (filecnt == 1) ? "" : "s",
pkgcnt, (pkgcnt == 1) ? "" : "s");
}

View file

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.34 2008/01/29 15:39:30 hubertf Exp $ */
/* $NetBSD: main.c,v 1.35 2008/03/09 19:02:27 joerg Exp $ */
#if HAVE_CONFIG_H
#include "config.h"
@ -8,7 +8,7 @@
#include <sys/cdefs.h>
#endif
#ifndef lint
__RCSID("$NetBSD: main.c,v 1.34 2008/01/29 15:39:30 hubertf Exp $");
__RCSID("$NetBSD: main.c,v 1.35 2008/03/09 19:02:27 joerg Exp $");
#endif
/*-
@ -78,6 +78,7 @@ __RCSID("$NetBSD: main.c,v 1.34 2008/01/29 15:39:30 hubertf Exp $");
#include <string.h>
#endif
#include "admin.h"
#include "lib.h"
#define DEFAULT_SFX ".t[bg]z" /* default suffix for ls{all,best} */
@ -87,9 +88,8 @@ static const char Options[] = "K:SVbd:qs:";
int filecnt;
int pkgcnt;
static int quiet;
int quiet;
static int checkpattern_fn(const char *, void *);
static void set_unset_variable(char **, Boolean);
/* print usage message and exit */
@ -117,115 +117,6 @@ usage(void)
exit(EXIT_FAILURE);
}
/*
* Assumes CWD is in /var/db/pkg/<pkg>!
*/
static void
check1pkg(const char *pkgdir)
{
FILE *f;
plist_t *p;
package_t Plist;
char *PkgName, *dirp = NULL, *md5file;
char file[MaxPathSize];
char dir[MaxPathSize];
f = fopen(CONTENTS_FNAME, "r");
if (f == NULL)
err(EXIT_FAILURE, "can't open %s/%s/%s", _pkgdb_getPKGDB_DIR(), pkgdir, CONTENTS_FNAME);
Plist.head = Plist.tail = NULL;
read_plist(&Plist, f);
p = find_plist(&Plist, PLIST_NAME);
if (p == NULL)
errx(EXIT_FAILURE, "Package %s has no @name, aborting.",
pkgdir);
PkgName = p->name;
for (p = Plist.head; p; p = p->next) {
switch (p->type) {
case PLIST_FILE:
if (dirp == NULL) {
warnx("dirp not initialized, please send-pr!");
abort();
}
(void) snprintf(file, sizeof(file), "%s/%s", dirp, p->name);
if (isfile(file) || islinktodir(file)) {
if (p->next && p->next->type == PLIST_COMMENT) {
if (strncmp(p->next->name, CHECKSUM_HEADER, ChecksumHeaderLen) == 0) {
if ((md5file = MD5File(file, NULL)) != NULL) {
/* Mismatch? */
#ifdef PKGDB_DEBUG
printf("%s: md5 should=<%s>, is=<%s>\n",
file, p->next->name + ChecksumHeaderLen, md5file);
#endif
if (strcmp(md5file, p->next->name + ChecksumHeaderLen) != 0)
printf("%s fails MD5 checksum\n", file);
free(md5file);
}
} else if (strncmp(p->next->name, SYMLINK_HEADER, SymlinkHeaderLen) == 0) {
char buf[MaxPathSize + SymlinkHeaderLen];
int cc;
(void) strlcpy(buf, SYMLINK_HEADER, sizeof(buf));
if ((cc = readlink(file, &buf[SymlinkHeaderLen],
sizeof(buf) - SymlinkHeaderLen - 1)) < 0) {
warnx("can't readlink `%s'", file);
} else {
buf[SymlinkHeaderLen + cc] = 0x0;
if (strcmp(buf, p->next->name) != 0) {
printf("symlink (%s) is not same as recorded value, %s: %s\n",
file, buf, p->next->name);
}
}
}
}
filecnt++;
} else if (isbrokenlink(file)) {
warnx("%s: Symlink `%s' exists and is in %s but target does not exist!", PkgName, file, CONTENTS_FNAME);
} else {
warnx("%s: File `%s' is in %s but not on filesystem!", PkgName, file, CONTENTS_FNAME);
}
break;
case PLIST_CWD:
if (strcmp(p->name, ".") != 0)
dirp = p->name;
else {
(void) snprintf(dir, sizeof(dir), "%s/%s", _pkgdb_getPKGDB_DIR(), pkgdir);
dirp = dir;
}
break;
case PLIST_IGNORE:
p = p->next;
break;
case PLIST_SHOW_ALL:
case PLIST_SRC:
case PLIST_CMD:
case PLIST_CHMOD:
case PLIST_CHOWN:
case PLIST_CHGRP:
case PLIST_COMMENT:
case PLIST_NAME:
case PLIST_UNEXEC:
case PLIST_DISPLAY:
case PLIST_PKGDEP:
case PLIST_MTREE:
case PLIST_DIR_RM:
case PLIST_IGNORE_INST:
case PLIST_OPTION:
case PLIST_PKGCFL:
case PLIST_BLDDEP:
break;
}
}
free_plist(&Plist);
fclose(f);
pkgcnt++;
}
/*
* add1pkg(<pkg>)
* adds the files listed in the +CONTENTS of <pkg> into the
@ -382,70 +273,6 @@ rebuild(void)
cachename);
}
static void
checkall(void)
{
DIR *dp;
struct dirent *de;
pkgcnt = 0;
filecnt = 0;
setbuf(stdout, NULL);
chdir(_pkgdb_getPKGDB_DIR());
dp = opendir(".");
if (dp == NULL)
err(EXIT_FAILURE, "opendir failed");
while ((de = readdir(dp))) {
if (!(isdir(de->d_name) || islinktodir(de->d_name)))
continue;
if (strcmp(de->d_name, ".") == 0 ||
strcmp(de->d_name, "..") == 0)
continue;
chdir(de->d_name);
check1pkg(de->d_name);
if (!quiet) {
printf(".");
}
chdir("..");
}
closedir(dp);
pkgdb_close();
printf("\n");
printf("Checked %d file%s from %d package%s.\n",
filecnt, (filecnt == 1) ? "" : "s",
pkgcnt, (pkgcnt == 1) ? "" : "s");
}
static int
checkpattern_fn(const char *pkg, void *vp)
{
int *got_match, rc;
rc = chdir(pkg);
if (rc == -1)
err(EXIT_FAILURE, "Cannot chdir to %s/%s", _pkgdb_getPKGDB_DIR(), pkg);
check1pkg(pkg);
if (!quiet) {
printf(".");
}
chdir("..");
got_match = vp;
*got_match = 1;
return 0;
}
static int
lspattern(const char *pkg, void *vp)
{
@ -633,54 +460,10 @@ main(int argc, char *argv[])
printf("Done.\n");
} else if (strcasecmp(argv[0], "check") == 0) {
argv++; /* "check" */
if (*argv != NULL) {
/* args specified */
int rc;
check(argv);
filecnt = 0;
setbuf(stdout, NULL);
rc = chdir(_pkgdb_getPKGDB_DIR());
if (rc == -1)
err(EXIT_FAILURE, "Cannot chdir to %s", _pkgdb_getPKGDB_DIR());
while (*argv != NULL) {
int got_match;
got_match = 0;
if (match_installed_pkgs(*argv, checkpattern_fn, &got_match) == -1)
errx(EXIT_FAILURE, "Cannot process pkdbdb");
if (got_match == 0) {
char *pattern;
if (ispkgpattern(*argv))
errx(EXIT_FAILURE, "No matching pkg for %s.", *argv);
if (asprintf(&pattern, "%s-[0-9]*", *argv) == -1)
errx(EXIT_FAILURE, "asprintf failed");
if (match_installed_pkgs(pattern, checkpattern_fn, &got_match) == -1)
errx(EXIT_FAILURE, "Cannot process pkdbdb");
if (got_match == 0)
errx(EXIT_FAILURE, "cannot find package %s", *argv);
free(pattern);
}
argv++;
}
printf("\n");
printf("Checked %d file%s from %d package%s.\n",
filecnt, (filecnt == 1) ? "" : "s",
pkgcnt, (pkgcnt == 1) ? "" : "s");
} else {
checkall();
}
if (!quiet) {
printf("Done.\n");
}