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