Home | History | Annotate | Line # | Download | only in dev_mkdb
dev_mkdb.c revision 1.12
      1 /*	$NetBSD: dev_mkdb.c,v 1.12 2001/07/05 20:35:33 manu Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "from: @(#)dev_mkdb.c	8.1 (Berkeley) 6/6/93";
     45 #else
     46 __RCSID("$NetBSD: dev_mkdb.c,v 1.12 2001/07/05 20:35:33 manu Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/stat.h>
     52 
     53 #include <db.h>
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <fcntl.h>
     57 #include <fts.h>
     58 #include <kvm.h>
     59 #include <nlist.h>
     60 #include <paths.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 
     66 int	main __P((int, char *[]));
     67 void	usage __P((void));
     68 
     69 int
     70 main(argc, argv)
     71 	int argc;
     72 	char *argv[];
     73 {
     74 	struct stat *st;
     75 	struct {
     76 		mode_t type;
     77 		dev_t dev;
     78 	} bkey;
     79 	DB *db;
     80 	DBT data, key;
     81 	FTS *ftsp;
     82 	FTSENT *p;
     83 	int ch;
     84 	u_char buf[MAXPATHLEN + 1];
     85 	char dbtmp[MAXPATHLEN + 1];
     86 	char dbname[MAXPATHLEN + 1];
     87 	char *dbname_arg = NULL;
     88 	char *pathv[2];
     89 	char path_dev[MAXPATHLEN + 1] = _PATH_DEV;
     90 	char cur_dir[MAXPATHLEN + 1];
     91 
     92 	while ((ch = getopt(argc, argv, "o:")) != -1)
     93 		switch (ch) {
     94 		case 'o':
     95 			if (strlen(optarg) <= MAXPATHLEN)
     96 				dbname_arg = optarg;
     97 			break;
     98 		case '?':
     99 		default:
    100 			usage();
    101 		}
    102 	argc -= optind;
    103 	argv += optind;
    104 
    105 	if ((argc == 1) && (strlen(argv[0]) <= MAXPATHLEN))
    106 		strncpy(path_dev, argv[0], MAXPATHLEN);
    107 
    108 	if (argc > 1)
    109 		usage();
    110 
    111 	if (!getcwd(cur_dir, MAXPATHLEN))
    112 		err(1, "%s", cur_dir);
    113 
    114 	if (chdir(path_dev))
    115 		err(1, "%s", path_dev);
    116 
    117 	pathv[0] = path_dev;
    118 	pathv[1] = NULL;
    119 	ftsp = fts_open(pathv, FTS_PHYSICAL, NULL);
    120 	if (ftsp == NULL)
    121 		err(1, "fts_open: %s", path_dev);
    122 
    123 	(void)snprintf(dbtmp, MAXPATHLEN, "%sdev.tmp", _PATH_TMP);
    124 	if (dbname_arg)
    125 		strncpy(dbname, dbname_arg, MAXPATHLEN);
    126 	else
    127 		(void)snprintf(dbname, MAXPATHLEN, "%sdev.db", _PATH_VARRUN);
    128 	db = dbopen(dbtmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC,
    129 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
    130 	if (db == NULL)
    131 		err(1, "%s", dbtmp);
    132 
    133 	/*
    134 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
    135 	 * the file (mode & S_IFMT), the latter is the st_rdev field.  Note
    136 	 * that the structure may contain padding, so we have to clear it
    137 	 * out here.
    138 	 */
    139 	memset(&bkey, 0, sizeof(bkey));
    140 	key.data = &bkey;
    141 	key.size = sizeof(bkey);
    142 	data.data = buf;
    143 	while ((p = fts_read(ftsp)) != NULL) {
    144 		switch (p->fts_info) {
    145 		case FTS_DEFAULT:
    146 			st = p->fts_statp;
    147 			break;
    148 		default:
    149 			continue;
    150 		}
    151 
    152 		/* Create the key. */
    153 		if (S_ISCHR(st->st_mode))
    154 			bkey.type = S_IFCHR;
    155 		else if (S_ISBLK(st->st_mode))
    156 			bkey.type = S_IFBLK;
    157 		else
    158 			continue;
    159 		bkey.dev = st->st_rdev;
    160 
    161 		/*
    162 		 * Create the data; nul terminate the name so caller doesn't
    163 		 * have to.  Skip path_dev and slash.
    164 		 */
    165 		strlcpy(buf, p->fts_path + strlen(path_dev), sizeof(buf));
    166 		data.size = p->fts_pathlen - strlen(path_dev) + 1;
    167 		if ((*db->put)(db, &key, &data, 0)) {
    168 			err(1, "dbput %s", dbtmp);
    169 		}
    170 	}
    171 	(void)(*db->close)(db);
    172 	fts_close(ftsp);
    173 	if (chdir(cur_dir))
    174 		err(1, "%s", cur_dir);
    175 	if (rename(dbtmp, dbname))
    176 		err(1, "rename %s to %s", dbtmp, dbname);
    177 	exit(0);
    178 }
    179 
    180 void
    181 usage()
    182 {
    183 
    184 	(void)fprintf(stderr, "usage: dev_mkdb [-o database] [directory]\n");
    185 	exit(1);
    186 }
    187