1 /* $OpenBSD: dev_mkdb.c,v 1.5 2000/10/03 18:07:51 mickey 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 #ifndef lint 37 static char copyright[] = 38 "@(#) 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 /*static char sccsid[] = "from: @(#)dev_mkdb.c 8.1 (Berkeley) 6/6/93";*/ 44 static char rcsid[] = "$Id: dev_mkdb.c,v 1.5 2000/10/03 18:07:51 mickey Exp $"; 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/stat.h> 49 50 #include <db.h> 51 #include <dirent.h> 52 #include <err.h> 53 #include <errno.h> 54 #include <fcntl.h> 55 #include <kvm.h> 56 #include <nlist.h> 57 #include <paths.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 63 void usage __P((void)); 64 65 int 66 main(argc, argv) 67 int argc; 68 char *argv[]; 69 { 70 register DIR *dirp; 71 register struct dirent *dp; 72 struct stat sb; 73 struct { 74 mode_t type; 75 dev_t dev; 76 } bkey; 77 DB *db; 78 DBT data, key; 79 HASHINFO info; 80 int ch; 81 u_char buf[MAXNAMLEN + 1]; 82 char dbtmp[MAXPATHLEN], dbname[MAXPATHLEN]; 83 84 while ((ch = getopt(argc, argv, "")) != -1) 85 switch((char)ch) { 86 case '?': 87 default: 88 usage(); 89 } 90 argc -= optind; 91 argv += optind; 92 93 if (argc > 0) 94 usage(); 95 96 if (chdir(_PATH_DEV)) 97 err(1, "%s", _PATH_DEV); 98 99 dirp = opendir("."); 100 101 (void)snprintf(dbtmp, sizeof(dbtmp), "%sdev.tmp", _PATH_VARRUN); 102 (void)snprintf(dbname, sizeof(dbtmp), "%sdev.db", _PATH_VARRUN); 103 bzero(&info, sizeof(info)); 104 info.bsize = 8192; 105 db = dbopen(dbtmp, O_CREAT|O_EXLOCK|O_RDWR|O_TRUNC, 106 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &info); 107 if (db == NULL) 108 err(1, "%s", dbtmp); 109 110 /* 111 * Keys are a mode_t followed by a dev_t. The former is the type of 112 * the file (mode & S_IFMT), the latter is the st_rdev field. Note 113 * that the structure may contain padding, so we have to clear it 114 * out here. 115 */ 116 bzero(&bkey, sizeof(bkey)); 117 key.data = &bkey; 118 key.size = sizeof(bkey); 119 data.data = buf; 120 while ((dp = readdir(dirp))) { 121 if (lstat(dp->d_name, &sb)) { 122 warn("%s", dp->d_name); 123 continue; 124 } 125 126 /* Create the key. */ 127 if (S_ISCHR(sb.st_mode)) 128 bkey.type = S_IFCHR; 129 else if (S_ISBLK(sb.st_mode)) 130 bkey.type = S_IFBLK; 131 else 132 continue; 133 bkey.dev = sb.st_rdev; 134 135 /* 136 * Create the data; nul terminate the name so caller doesn't 137 * have to. 138 */ 139 bcopy(dp->d_name, buf, dp->d_namlen); 140 buf[dp->d_namlen] = '\0'; 141 data.size = dp->d_namlen + 1; 142 if ((db->put)(db, &key, &data, 0)) 143 err(1, "dbput %s", dbtmp); 144 } 145 (void)(db->close)(db); 146 if (rename(dbtmp, dbname)) 147 err(1, "rename %s to %s", dbtmp, dbname); 148 149 return (0); 150 } 151 152 void 153 usage() 154 { 155 156 (void)fprintf(stderr, "usage: dev_mkdb\n"); 157 exit(1); 158 } 159