1 /* $OpenBSD: dev_mkdb.c,v 1.11 2009/10/27 23:59:51 deraadt 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/stat.h> 34 35 #include <db.h> 36 #include <dirent.h> 37 #include <err.h> 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <paths.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 void usage(void); 47 48 int 49 main(int argc, char *argv[]) 50 { 51 DIR *dirp; 52 struct dirent *dp; 53 struct stat sb; 54 struct { 55 mode_t type; 56 dev_t dev; 57 } bkey; 58 DB *db; 59 DBT data, key; 60 HASHINFO info; 61 int ch; 62 u_char buf[MAXNAMLEN + 1]; 63 char dbtmp[MAXPATHLEN], dbname[MAXPATHLEN]; 64 65 while ((ch = getopt(argc, argv, "")) != -1) 66 switch((char)ch) { 67 case '?': 68 default: 69 usage(); 70 } 71 argc -= optind; 72 argv += optind; 73 74 if (argc > 0) 75 usage(); 76 77 if (chdir(_PATH_DEV)) 78 err(1, "%s", _PATH_DEV); 79 80 dirp = opendir("."); 81 82 (void)snprintf(dbtmp, sizeof(dbtmp), "%sdev.tmp", _PATH_VARRUN); 83 (void)snprintf(dbname, sizeof(dbtmp), "%sdev.db", _PATH_VARRUN); 84 bzero(&info, sizeof(info)); 85 info.bsize = 8192; 86 db = dbopen(dbtmp, O_CREAT|O_EXLOCK|O_RDWR|O_TRUNC, 87 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &info); 88 if (db == NULL) 89 err(1, "%s", dbtmp); 90 91 /* 92 * Keys are a mode_t followed by a dev_t. The former is the type of 93 * the file (mode & S_IFMT), the latter is the st_rdev field. Note 94 * that the structure may contain padding, so we have to clear it 95 * out here. 96 */ 97 bzero(&bkey, sizeof(bkey)); 98 key.data = &bkey; 99 key.size = sizeof(bkey); 100 data.data = buf; 101 while ((dp = readdir(dirp))) { 102 if (lstat(dp->d_name, &sb)) { 103 warn("%s", dp->d_name); 104 continue; 105 } 106 107 /* Create the key. */ 108 if (S_ISCHR(sb.st_mode)) 109 bkey.type = S_IFCHR; 110 else if (S_ISBLK(sb.st_mode)) 111 bkey.type = S_IFBLK; 112 else 113 continue; 114 bkey.dev = sb.st_rdev; 115 116 /* 117 * Create the data; nul terminate the name so caller doesn't 118 * have to. 119 */ 120 bcopy(dp->d_name, buf, dp->d_namlen); 121 buf[dp->d_namlen] = '\0'; 122 data.size = dp->d_namlen + 1; 123 if ((db->put)(db, &key, &data, 0)) 124 err(1, "dbput %s", dbtmp); 125 } 126 (void)(db->close)(db); 127 if (rename(dbtmp, dbname)) 128 err(1, "rename %s to %s", dbtmp, dbname); 129 130 return (0); 131 } 132 133 void 134 usage(void) 135 { 136 137 (void)fprintf(stderr, "usage: dev_mkdb\n"); 138 exit(1); 139 } 140