2003-09-19 21:44:53 +04:00
|
|
|
/* $NetBSD: main.c,v 1.42 2003/09/19 17:44:53 wiz Exp $ */
|
1999-01-19 20:01:56 +03:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2003-09-19 21:44:53 +04:00
|
|
|
__RCSID("$NetBSD: main.c,v 1.42 2003/09/19 17:44:53 wiz Exp $");
|
1999-01-19 20:01:56 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1999 Hubert Feyrer. 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:
|
2000-02-22 04:24:26 +03:00
|
|
|
* This product includes software developed by Hubert Feyrer for
|
|
|
|
* the NetBSD Project.
|
1999-01-19 20:01:56 +03:00
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <md5.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "lib.h"
|
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
#define DEFAULT_SFX ".t[bg]z" /* default suffix for ls{all,best} */
|
|
|
|
|
2003-09-18 13:56:20 +04:00
|
|
|
static const char Options[] = "K:SVbd:s:";
|
1999-01-19 20:01:56 +03:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
int filecnt;
|
2000-03-19 20:24:27 +03:00
|
|
|
int pkgcnt;
|
1999-03-22 08:02:39 +03:00
|
|
|
|
2002-06-09 17:23:44 +04:00
|
|
|
static int checkpattern_fn(const char *, void *);
|
|
|
|
|
2003-09-18 13:56:20 +04:00
|
|
|
/* print usage message and exit */
|
|
|
|
static void
|
|
|
|
usage(const char *prog)
|
|
|
|
{
|
|
|
|
(void) fprintf(stderr, "usage: %s [-b] [-d lsdir] [-V] [-s sfx] command args ...\n"
|
|
|
|
"Where 'commands' and 'args' are:\n"
|
|
|
|
" rebuild - rebuild pkgdb from +CONTENTS files\n"
|
|
|
|
" check [pkg ...] - check md5 checksum of installed files\n"
|
|
|
|
" add pkg ... - add pkg files to database\n"
|
|
|
|
" delete pkg ... - delete file entries for pkg in database\n"
|
|
|
|
#ifdef PKGDB_DEBUG
|
|
|
|
" addkey key value - add key and value\n"
|
|
|
|
" delkey key - delete reference to key\n"
|
|
|
|
#endif
|
|
|
|
" lsall /path/to/pkgpattern - list all pkgs matching the pattern\n"
|
|
|
|
" lsbest /path/to/pkgpattern - list pkgs matching the pattern best\n"
|
|
|
|
" dump - dump database\n"
|
|
|
|
" pmatch pattern pkg - returns true if pkg matches pattern, otherwise false\n",
|
|
|
|
prog);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
1999-01-19 20:01:56 +03:00
|
|
|
/*
|
1999-08-24 04:48:37 +04:00
|
|
|
* Assumes CWD is in /var/db/pkg/<pkg>!
|
1999-01-19 20:01:56 +03:00
|
|
|
*/
|
1999-08-24 04:48:37 +04:00
|
|
|
static void
|
|
|
|
check1pkg(const char *pkgdir)
|
1999-01-19 20:01:56 +03:00
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
FILE *f;
|
|
|
|
plist_t *p;
|
|
|
|
package_t Plist;
|
|
|
|
char *PkgName, *dirp = NULL, *md5file;
|
|
|
|
char file[FILENAME_MAX];
|
|
|
|
char dir[FILENAME_MAX];
|
|
|
|
|
|
|
|
f = fopen(CONTENTS_FNAME, "r");
|
|
|
|
if (f == NULL)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "can't open %s/%s/%s", _pkgdb_getPKGDB_DIR(), pkgdir, CONTENTS_FNAME);
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
Plist.head = Plist.tail = NULL;
|
|
|
|
read_plist(&Plist, f);
|
|
|
|
p = find_plist(&Plist, PLIST_NAME);
|
|
|
|
if (p == NULL)
|
2003-01-06 00:27:20 +03:00
|
|
|
errx(EXIT_FAILURE, "Package %s has no @name, aborting.",
|
1999-08-24 04:48:37 +04:00
|
|
|
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);
|
|
|
|
|
1999-09-13 04:32:14 +04:00
|
|
|
if (!(isfile(file) || islinktodir(file)))
|
1999-08-24 04:48:37 +04:00
|
|
|
warnx("%s: File %s is in %s but not on filesystem!", PkgName, file, CONTENTS_FNAME);
|
|
|
|
else {
|
|
|
|
if (p->next &&
|
|
|
|
p->next->type == PLIST_COMMENT &&
|
|
|
|
strncmp(p->next->name, CHECKSUM_HEADER, ChecksumHeaderLen) == 0) { /* || PLIST_MD5 - HF */
|
|
|
|
if ((md5file = MD5File(file, NULL)) != NULL) {
|
|
|
|
/* Mismatch? */
|
1999-01-19 20:01:56 +03:00
|
|
|
#ifdef PKGDB_DEBUG
|
1999-08-24 04:48:37 +04:00
|
|
|
printf("%s: md5 should=<%s>, is=<%s>\n",
|
|
|
|
file, p->next->name + ChecksumHeaderLen, md5file);
|
1999-01-19 20:01:56 +03:00
|
|
|
#endif
|
1999-08-24 04:48:37 +04:00
|
|
|
if (strcmp(md5file, p->next->name + ChecksumHeaderLen) != 0)
|
|
|
|
printf("%s fails MD5 checksum\n", file);
|
|
|
|
|
|
|
|
free(md5file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filecnt++;
|
|
|
|
}
|
|
|
|
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:
|
2001-04-17 14:42:11 +04:00
|
|
|
case PLIST_SRC:
|
1999-08-24 04:48:37 +04:00
|
|
|
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:
|
2002-03-05 16:01:19 +03:00
|
|
|
case PLIST_BLDDEP:
|
1999-08-24 04:48:37 +04:00
|
|
|
break;
|
1999-01-19 20:01:56 +03:00
|
|
|
}
|
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
free_plist(&Plist);
|
|
|
|
fclose(f);
|
2000-11-28 08:23:35 +03:00
|
|
|
pkgcnt++;
|
1999-01-19 20:01:56 +03:00
|
|
|
}
|
|
|
|
|
2003-09-08 11:04:40 +04:00
|
|
|
/*
|
|
|
|
* add1pkg(<pkg>)
|
|
|
|
* adds the files listed in the +CONTENTS of <pkg> into the
|
|
|
|
* pkgdb.byfile.db database file in the current package dbdir. It
|
|
|
|
* returns the number of files added to the database file.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
add1pkg(const char *pkgdir)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
plist_t *p;
|
|
|
|
package_t Plist;
|
|
|
|
char contents[FILENAME_MAX];
|
|
|
|
char *PkgDBDir, *PkgName, *dirp;
|
|
|
|
char file[FILENAME_MAX];
|
|
|
|
char dir[FILENAME_MAX];
|
2003-09-19 21:44:53 +04:00
|
|
|
int cnt = 0;
|
2003-09-08 11:04:40 +04:00
|
|
|
|
|
|
|
if (!pkgdb_open(ReadWrite))
|
|
|
|
err(EXIT_FAILURE, "cannot open pkgdb");
|
|
|
|
|
|
|
|
PkgDBDir = _pkgdb_getPKGDB_DIR();
|
|
|
|
(void) snprintf(contents, sizeof(contents), "%s/%s", PkgDBDir, pkgdir);
|
2003-09-09 02:11:12 +04:00
|
|
|
if (!(isdir(contents) || islinktodir(contents)))
|
2003-09-08 11:04:40 +04:00
|
|
|
errx(EXIT_FAILURE, "`%s' does not exist.", contents);
|
|
|
|
|
|
|
|
(void) strlcat(contents, "/", sizeof(contents));
|
|
|
|
(void) strlcat(contents, CONTENTS_FNAME, sizeof(contents));
|
|
|
|
if ((f = fopen(contents, "r")) == NULL)
|
|
|
|
errx(EXIT_FAILURE, "%s: can't open `%s'", pkgdir, CONTENTS_FNAME);
|
|
|
|
|
|
|
|
Plist.head = Plist.tail = NULL;
|
|
|
|
read_plist(&Plist, f);
|
|
|
|
if ((p = find_plist(&Plist, PLIST_NAME)) == NULL) {
|
|
|
|
errx(EXIT_FAILURE, "Package `%s' has no @name, aborting.", pkgdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
PkgName = p->name;
|
|
|
|
dirp = NULL;
|
|
|
|
for (p = Plist.head; p; p = p->next) {
|
|
|
|
switch(p->type) {
|
|
|
|
case PLIST_FILE:
|
|
|
|
if (dirp == NULL) {
|
|
|
|
errx(EXIT_FAILURE, "@cwd not yet found, please send-pr!");
|
|
|
|
}
|
|
|
|
(void) snprintf(file, sizeof(file), "%s/%s", dirp, p->name);
|
|
|
|
if (!(isfile(file) || islinktodir(file))) {
|
|
|
|
warnx("%s: File `%s' is in %s but not on filesystem!",
|
|
|
|
PkgName, file, CONTENTS_FNAME);
|
|
|
|
} else {
|
|
|
|
pkgdb_store(file, PkgName);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PLIST_CWD:
|
|
|
|
if (strcmp(p->name, ".") != 0) {
|
|
|
|
dirp = p->name;
|
|
|
|
} else {
|
|
|
|
(void) snprintf(dir, sizeof(dir), "%s/%s", PkgDBDir, 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);
|
|
|
|
pkgdb_close();
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
delete1pkg(const char *pkgdir)
|
|
|
|
{
|
|
|
|
if (!pkgdb_open(ReadWrite))
|
|
|
|
err(EXIT_FAILURE, "cannot open pkgdb");
|
|
|
|
(void) pkgdb_remove_pkg(pkgdir);
|
|
|
|
pkgdb_close();
|
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
static void
|
|
|
|
rebuild(void)
|
1999-01-19 20:01:56 +03:00
|
|
|
{
|
2003-09-08 11:04:40 +04:00
|
|
|
DIR *dp;
|
|
|
|
struct dirent *de;
|
|
|
|
char *PkgDBDir;
|
|
|
|
char cachename[FILENAME_MAX];
|
1999-08-24 04:48:37 +04:00
|
|
|
|
2000-03-19 20:24:27 +03:00
|
|
|
pkgcnt = 0;
|
1999-08-24 04:48:37 +04:00
|
|
|
filecnt = 0;
|
|
|
|
|
2003-09-08 11:04:40 +04:00
|
|
|
(void) _pkgdb_getPKGDB_FILE(cachename, sizeof(cachename));
|
|
|
|
if (unlink(cachename) != 0 && errno != ENOENT)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "unlink %s", cachename);
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
PkgDBDir = _pkgdb_getPKGDB_DIR();
|
|
|
|
chdir(PkgDBDir);
|
1999-01-19 20:01:56 +03:00
|
|
|
#ifdef PKGDB_DEBUG
|
1999-08-24 04:48:37 +04:00
|
|
|
printf("PkgDBDir='%s'\n", PkgDBDir);
|
1999-01-19 20:01:56 +03:00
|
|
|
#endif
|
1999-08-24 04:48:37 +04:00
|
|
|
dp = opendir(".");
|
|
|
|
if (dp == NULL)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "opendir failed");
|
1999-08-24 04:48:37 +04:00
|
|
|
while ((de = readdir(dp))) {
|
2003-09-09 02:11:12 +04:00
|
|
|
if (!(isdir(de->d_name) || islinktodir(de->d_name)))
|
1999-08-24 04:48:37 +04:00
|
|
|
continue;
|
1999-01-19 20:01:56 +03:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
if (strcmp(de->d_name, ".") == 0 ||
|
|
|
|
strcmp(de->d_name, "..") == 0)
|
|
|
|
continue;
|
1999-01-19 20:01:56 +03:00
|
|
|
|
|
|
|
#ifdef PKGDB_DEBUG
|
1999-08-24 04:48:37 +04:00
|
|
|
printf("%s\n", de->d_name);
|
1999-01-19 20:01:56 +03:00
|
|
|
#else
|
1999-08-24 04:48:37 +04:00
|
|
|
printf(".");
|
1999-01-19 20:01:56 +03:00
|
|
|
#endif
|
|
|
|
|
2003-09-08 11:04:40 +04:00
|
|
|
filecnt += add1pkg(de->d_name);
|
2000-11-28 08:23:35 +03:00
|
|
|
pkgcnt++;
|
1999-08-24 04:48:37 +04:00
|
|
|
}
|
2003-09-08 11:04:40 +04:00
|
|
|
chdir("..");
|
1999-08-24 04:48:37 +04:00
|
|
|
closedir(dp);
|
1999-01-19 20:01:56 +03:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
printf("\n");
|
|
|
|
printf("Stored %d file%s from %d package%s in %s.\n",
|
|
|
|
filecnt, filecnt == 1 ? "" : "s",
|
|
|
|
pkgcnt, pkgcnt == 1 ? "" : "s",
|
2003-01-06 00:27:20 +03:00
|
|
|
cachename);
|
1999-08-24 04:48:37 +04:00
|
|
|
}
|
1999-01-19 20:01:56 +03:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
static void
|
|
|
|
checkall(void)
|
1999-01-19 20:01:56 +03:00
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
DIR *dp;
|
|
|
|
struct dirent *de;
|
|
|
|
|
2000-03-19 20:24:27 +03:00
|
|
|
pkgcnt = 0;
|
1999-08-24 04:48:37 +04:00
|
|
|
filecnt = 0;
|
|
|
|
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
chdir(_pkgdb_getPKGDB_DIR());
|
|
|
|
|
|
|
|
dp = opendir(".");
|
|
|
|
if (dp == NULL)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "opendir failed");
|
1999-08-24 04:48:37 +04:00
|
|
|
while ((de = readdir(dp))) {
|
2003-09-09 02:11:12 +04:00
|
|
|
if (!(isdir(de->d_name) || islinktodir(de->d_name)))
|
1999-08-24 04:48:37 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(de->d_name, ".") == 0 ||
|
|
|
|
strcmp(de->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
chdir(de->d_name);
|
|
|
|
|
|
|
|
check1pkg(de->d_name);
|
|
|
|
printf(".");
|
|
|
|
|
|
|
|
chdir("..");
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
pkgdb_close();
|
1999-01-19 20:01:56 +03:00
|
|
|
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
printf("\n");
|
|
|
|
printf("Checked %d file%s from %d package%s.\n",
|
|
|
|
filecnt, (filecnt == 1) ? "" : "s",
|
|
|
|
pkgcnt, (pkgcnt == 1) ? "" : "s");
|
1999-01-19 20:01:56 +03:00
|
|
|
}
|
|
|
|
|
1999-03-22 08:02:39 +03:00
|
|
|
static int
|
2002-06-09 17:23:44 +04:00
|
|
|
checkpattern_fn(const char *pkg, void *vp)
|
1999-03-22 08:02:39 +03:00
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = chdir(pkg);
|
|
|
|
if (rc == -1)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "Cannot chdir to %s/%s", _pkgdb_getPKGDB_DIR(), pkg);
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
check1pkg(pkg);
|
|
|
|
printf(".");
|
|
|
|
|
|
|
|
chdir("..");
|
|
|
|
|
|
|
|
return 0;
|
1999-03-22 08:02:39 +03:00
|
|
|
}
|
1999-01-19 20:01:56 +03:00
|
|
|
|
2000-05-09 02:48:42 +04:00
|
|
|
static int
|
2002-06-09 17:23:44 +04:00
|
|
|
lspattern_fn(const char *pkg, void *vp)
|
2000-05-09 02:48:42 +04:00
|
|
|
{
|
2002-06-09 17:23:44 +04:00
|
|
|
char *data = vp;
|
2000-05-09 02:48:42 +04:00
|
|
|
printf("%s/%s\n", data, pkg);
|
2003-09-13 09:48:50 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lsbasepattern_fn(const char *pkg, void *vp)
|
|
|
|
{
|
|
|
|
printf("%s\n", pkg);
|
2000-05-09 02:48:42 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
1999-01-19 20:01:56 +03:00
|
|
|
{
|
2003-09-18 13:56:20 +04:00
|
|
|
const char *prog;
|
|
|
|
Boolean use_default_sfx = TRUE;
|
|
|
|
Boolean show_basename_only = FALSE;
|
|
|
|
char lsdir[FILENAME_MAX];
|
|
|
|
char sfx[FILENAME_MAX];
|
|
|
|
char *lsdirp = NULL;
|
|
|
|
int ch;
|
2003-09-02 11:34:47 +04:00
|
|
|
|
2003-09-18 13:56:20 +04:00
|
|
|
setprogname(prog = argv[0]);
|
2003-06-05 17:15:43 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
if (argc < 2)
|
2003-09-18 13:56:20 +04:00
|
|
|
usage(prog);
|
1999-08-24 04:48:37 +04:00
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
while ((ch = getopt(argc, argv, Options)) != -1)
|
|
|
|
switch (ch) {
|
2003-09-18 13:56:20 +04:00
|
|
|
case 'K':
|
|
|
|
_pkgdb_setPKGDB_DIR(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
sfx[0] = 0x0;
|
|
|
|
use_default_sfx = FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V':
|
|
|
|
show_version();
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
2003-09-13 09:48:50 +04:00
|
|
|
case 'b':
|
|
|
|
show_basename_only = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
(void) strlcpy(lsdir, optarg, sizeof(lsdir));
|
|
|
|
lsdirp = lsdir;
|
|
|
|
break;
|
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
case 's':
|
|
|
|
(void) strlcpy(sfx, optarg, sizeof(sfx));
|
|
|
|
use_default_sfx = FALSE;
|
|
|
|
break;
|
2001-03-05 19:53:12 +03:00
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
default:
|
2003-09-18 13:56:20 +04:00
|
|
|
usage(prog);
|
2003-09-02 11:34:47 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2003-09-18 13:56:20 +04:00
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2003-09-18 13:56:20 +04:00
|
|
|
if (argc <= 0) {
|
|
|
|
usage(prog);
|
|
|
|
}
|
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
if (use_default_sfx)
|
|
|
|
(void) snprintf(sfx, sizeof(sfx), "%s", DEFAULT_SFX);
|
|
|
|
|
|
|
|
if (strcasecmp(argv[0], "pmatch") == 0) {
|
2002-08-05 00:50:55 +04:00
|
|
|
|
|
|
|
char *pattern, *pkg;
|
|
|
|
|
|
|
|
argv++; /* "pmatch" */
|
|
|
|
|
|
|
|
pattern = argv[0];
|
|
|
|
pkg = argv[1];
|
|
|
|
|
|
|
|
if (pattern == NULL || pkg == NULL) {
|
2003-09-18 13:56:20 +04:00
|
|
|
usage(prog);
|
2002-08-05 00:50:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pmatch(pattern, pkg)){
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
} else if (strcasecmp(argv[0], "rebuild") == 0) {
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
rebuild();
|
2000-05-09 02:48:42 +04:00
|
|
|
printf("Done.\n");
|
1999-08-24 04:48:37 +04:00
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
} else if (strcasecmp(argv[0], "check") == 0) {
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
argv++; /* "check" */
|
|
|
|
|
|
|
|
if (*argv != NULL) {
|
|
|
|
/* args specified */
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
filecnt = 0;
|
|
|
|
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
|
|
|
|
rc = chdir(_pkgdb_getPKGDB_DIR());
|
|
|
|
if (rc == -1)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "Cannot chdir to %s", _pkgdb_getPKGDB_DIR());
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
while (*argv != NULL) {
|
|
|
|
if (ispkgpattern(*argv)) {
|
2003-03-25 00:44:45 +03:00
|
|
|
if (findmatchingname(_pkgdb_getPKGDB_DIR(), *argv, checkpattern_fn, NULL) <= 0)
|
2003-01-06 00:27:20 +03:00
|
|
|
errx(EXIT_FAILURE, "No matching pkg for %s.", *argv);
|
1999-08-24 04:48:37 +04:00
|
|
|
} else {
|
|
|
|
rc = chdir(*argv);
|
2000-03-19 20:24:27 +03:00
|
|
|
if (rc == -1) {
|
|
|
|
/* found nothing - try 'pkg-[0-9]*' */
|
|
|
|
char try[FILENAME_MAX];
|
|
|
|
|
|
|
|
snprintf(try, sizeof(try), "%s-[0-9]*", *argv);
|
|
|
|
if (findmatchingname(_pkgdb_getPKGDB_DIR(), try,
|
|
|
|
checkpattern_fn, NULL) <= 0) {
|
|
|
|
|
2003-01-06 00:27:20 +03:00
|
|
|
errx(EXIT_FAILURE, "cannot find package %s", *argv);
|
2000-03-19 20:24:27 +03:00
|
|
|
} else {
|
|
|
|
/* nothing to do - all the work is/was
|
|
|
|
* done in checkpattern_fn() */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
check1pkg(*argv);
|
|
|
|
printf(".");
|
|
|
|
|
|
|
|
chdir("..");
|
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("Checked %d file%s from %d package%s.\n",
|
|
|
|
filecnt, (filecnt == 1) ? "" : "s",
|
|
|
|
pkgcnt, (pkgcnt == 1) ? "" : "s");
|
|
|
|
} else {
|
|
|
|
checkall();
|
1999-03-22 08:02:39 +03:00
|
|
|
}
|
2000-05-09 02:48:42 +04:00
|
|
|
printf("Done.\n");
|
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
} else if (strcasecmp(argv[0], "lsall") == 0) {
|
2000-05-09 02:48:42 +04:00
|
|
|
int saved_wd;
|
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
argv++; /* "lsall" */
|
2000-05-09 02:48:42 +04:00
|
|
|
|
|
|
|
/* preserve cwd */
|
|
|
|
saved_wd=open(".", O_RDONLY);
|
|
|
|
if (saved_wd == -1)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "Cannot save working dir");
|
2000-05-09 02:48:42 +04:00
|
|
|
|
|
|
|
while (*argv != NULL) {
|
|
|
|
/* args specified */
|
|
|
|
int rc;
|
2002-07-19 23:04:33 +04:00
|
|
|
const char *basep, *dir;
|
2003-04-17 17:32:21 +04:00
|
|
|
char cwd[MAXPATHLEN];
|
2000-05-09 02:48:42 +04:00
|
|
|
char base[FILENAME_MAX];
|
|
|
|
|
2003-09-13 09:48:50 +04:00
|
|
|
dir = lsdirp ? lsdirp : dirname_of(*argv);
|
2000-05-09 02:48:42 +04:00
|
|
|
basep = basename_of(*argv);
|
2003-09-02 11:34:47 +04:00
|
|
|
snprintf(base, sizeof(base), "%s%s", basep, sfx);
|
2000-05-09 02:48:42 +04:00
|
|
|
|
|
|
|
fchdir(saved_wd);
|
|
|
|
rc = chdir(dir);
|
|
|
|
if (rc == -1)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "Cannot chdir to %s", _pkgdb_getPKGDB_DIR());
|
2000-05-09 02:48:42 +04:00
|
|
|
|
2003-04-17 17:32:21 +04:00
|
|
|
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
|
|
|
err(EXIT_FAILURE, "getcwd");
|
2003-09-13 09:48:50 +04:00
|
|
|
|
|
|
|
if (show_basename_only)
|
|
|
|
rc = findmatchingname(cwd, base, lsbasepattern_fn, cwd);
|
|
|
|
else
|
|
|
|
rc = findmatchingname(cwd, base, lspattern_fn, cwd);
|
|
|
|
if (rc == -1)
|
2003-01-06 00:27:20 +03:00
|
|
|
errx(EXIT_FAILURE, "Error in findmatchingname(\"%s\", \"%s\", ...)",
|
2000-05-09 02:48:42 +04:00
|
|
|
cwd, base);
|
2003-04-17 17:32:21 +04:00
|
|
|
|
2000-05-09 02:48:42 +04:00
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(saved_wd);
|
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
} else if (strcasecmp(argv[0], "lsbest") == 0) {
|
2000-05-09 02:48:42 +04:00
|
|
|
int saved_wd;
|
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
argv++; /* "lsbest" */
|
2000-05-09 02:48:42 +04:00
|
|
|
|
|
|
|
/* preserve cwd */
|
|
|
|
saved_wd=open(".", O_RDONLY);
|
|
|
|
if (saved_wd == -1)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "Cannot save working dir");
|
2000-05-09 02:48:42 +04:00
|
|
|
|
|
|
|
while (*argv != NULL) {
|
|
|
|
/* args specified */
|
|
|
|
int rc;
|
2002-07-19 23:04:33 +04:00
|
|
|
const char *basep, *dir;
|
2003-04-17 17:32:21 +04:00
|
|
|
char cwd[MAXPATHLEN];
|
2000-05-09 02:48:42 +04:00
|
|
|
char base[FILENAME_MAX];
|
|
|
|
char *p;
|
|
|
|
|
2003-09-13 09:48:50 +04:00
|
|
|
dir = lsdirp ? lsdirp : dirname_of(*argv);
|
2000-05-09 02:48:42 +04:00
|
|
|
basep = basename_of(*argv);
|
2003-09-02 11:34:47 +04:00
|
|
|
snprintf(base, sizeof(base), "%s%s", basep, sfx);
|
2000-05-09 02:48:42 +04:00
|
|
|
|
|
|
|
fchdir(saved_wd);
|
|
|
|
rc = chdir(dir);
|
|
|
|
if (rc == -1)
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "Cannot chdir to %s", _pkgdb_getPKGDB_DIR());
|
2000-05-09 02:48:42 +04:00
|
|
|
|
2003-04-17 17:32:21 +04:00
|
|
|
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
|
|
|
err(EXIT_FAILURE, "getcwd");
|
2000-05-09 02:48:42 +04:00
|
|
|
p = findbestmatchingname(cwd, base);
|
2002-06-09 15:56:59 +04:00
|
|
|
if (p) {
|
2003-09-13 09:48:50 +04:00
|
|
|
if (show_basename_only)
|
|
|
|
printf("%s\n", p);
|
|
|
|
else
|
|
|
|
printf("%s/%s\n", cwd, p);
|
2002-06-09 15:56:59 +04:00
|
|
|
free(p);
|
|
|
|
}
|
2000-05-09 02:48:42 +04:00
|
|
|
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(saved_wd);
|
1999-01-19 20:01:56 +03:00
|
|
|
|
2003-09-02 11:34:47 +04:00
|
|
|
} else if (strcasecmp(argv[0], "list") == 0 ||
|
|
|
|
strcasecmp(argv[0], "dump") == 0) {
|
1999-01-19 20:01:56 +03:00
|
|
|
|
2003-03-15 23:49:25 +03:00
|
|
|
pkgdb_dump();
|
1999-01-19 20:01:56 +03:00
|
|
|
|
2003-09-08 11:04:40 +04:00
|
|
|
} else if (strcasecmp(argv[0], "add") == 0) {
|
|
|
|
argv++; /* "add" */
|
|
|
|
while (*argv != NULL) {
|
|
|
|
add1pkg(*argv);
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
} else if (strcasecmp(argv[0], "delete") == 0) {
|
|
|
|
argv++; /* "delete" */
|
|
|
|
while (*argv != NULL) {
|
|
|
|
delete1pkg(*argv);
|
|
|
|
argv++;
|
|
|
|
}
|
1999-01-19 20:01:56 +03:00
|
|
|
}
|
|
|
|
#ifdef PKGDB_DEBUG
|
2003-09-08 11:04:40 +04:00
|
|
|
else if (strcasecmp(argv[0], "delkey") == 0) {
|
1999-08-24 04:48:37 +04:00
|
|
|
int rc;
|
|
|
|
|
2003-01-06 00:49:51 +03:00
|
|
|
if (!pkgdb_open(ReadWrite))
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "cannot open pkgdb");
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
rc = pkgdb_remove(argv[2]);
|
|
|
|
if (rc) {
|
|
|
|
if (errno)
|
|
|
|
perror("pkgdb_remove");
|
|
|
|
else
|
|
|
|
printf("Key not present in pkgdb.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgdb_close();
|
|
|
|
|
2003-09-08 11:04:40 +04:00
|
|
|
} else if (strcasecmp(argv[0], "addkey") == 0) {
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
int rc;
|
|
|
|
|
2003-01-06 00:49:51 +03:00
|
|
|
if (!pkgdb_open(ReadWrite)) {
|
2003-01-06 00:27:20 +03:00
|
|
|
err(EXIT_FAILURE, "cannot open pkgdb");
|
1999-08-24 04:48:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = pkgdb_store(argv[2], argv[3]);
|
|
|
|
switch (rc) {
|
|
|
|
case -1:
|
|
|
|
perror("pkgdb_store");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
printf("Key already present.\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* 0: everything ok */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgdb_close();
|
1999-01-19 20:01:56 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
1999-08-24 04:48:37 +04:00
|
|
|
else {
|
2003-09-18 13:56:20 +04:00
|
|
|
usage(prog);
|
1999-08-24 04:48:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
1999-01-19 20:01:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup(int signo)
|
|
|
|
{
|
|
|
|
}
|