1*c3a13cfdSriastradh /* $NetBSD: dev_mkdb.c,v 1.31 2023/08/08 10:35:37 riastradh Exp $ */
2fdb51915Senami
361f28255Scgd /*-
4bbfccb8fSmycroft * Copyright (c) 1990, 1993
5bbfccb8fSmycroft * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15326b2259Sagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
320a469c56Slukem #include <sys/cdefs.h>
33*c3a13cfdSriastradh __RCSID("$NetBSD: dev_mkdb.c,v 1.31 2023/08/08 10:35:37 riastradh Exp $");
3461f28255Scgd
351907ec3aSjoerg #include <sys/queue.h>
3661f28255Scgd #include <sys/stat.h>
37bbfccb8fSmycroft
381907ec3aSjoerg #include <cdbw.h>
3961f28255Scgd #include <db.h>
40bbfccb8fSmycroft #include <err.h>
4161f28255Scgd #include <errno.h>
42bbfccb8fSmycroft #include <fcntl.h>
43fdb51915Senami #include <fts.h>
4461f28255Scgd #include <paths.h>
451907ec3aSjoerg #include <search.h>
461907ec3aSjoerg #include <stdint.h>
47bbfccb8fSmycroft #include <stdio.h>
4861f28255Scgd #include <stdlib.h>
4961f28255Scgd #include <string.h>
50bbfccb8fSmycroft #include <unistd.h>
511907ec3aSjoerg #include <util.h>
521907ec3aSjoerg
531907ec3aSjoerg #define HASH_SIZE 65536
541907ec3aSjoerg #define FILE_PERMISSION S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
551907ec3aSjoerg
561907ec3aSjoerg static struct cdbw *db;
571907ec3aSjoerg static DB *db_compat;
581907ec3aSjoerg static const char *db_name;
591907ec3aSjoerg static char *db_name_tmp;
6061f28255Scgd
613bb0bce3Sjoerg static void usage(void) __dead;
6261f28255Scgd
631907ec3aSjoerg static void
cdb_open(void)641907ec3aSjoerg cdb_open(void)
651907ec3aSjoerg {
661907ec3aSjoerg db = cdbw_open();
671907ec3aSjoerg if (db == NULL)
681907ec3aSjoerg err(1, "opening cdb writer failed");
691907ec3aSjoerg }
701907ec3aSjoerg
711907ec3aSjoerg static void
cdb_close(void)721907ec3aSjoerg cdb_close(void)
731907ec3aSjoerg {
741907ec3aSjoerg int fd;
751907ec3aSjoerg
761907ec3aSjoerg fd = open(db_name_tmp, O_CREAT|O_EXCL|O_WRONLY, FILE_PERMISSION);
771907ec3aSjoerg if (fd == -1)
781907ec3aSjoerg err(1, "opening %s failed", db_name_tmp);
79*c3a13cfdSriastradh if (cdbw_output(db, fd, "NetBSD6 devdb", NULL))
801907ec3aSjoerg err(1, "failed to write temporary database %s", db_name_tmp);
811907ec3aSjoerg cdbw_close(db);
821907ec3aSjoerg db = NULL;
831907ec3aSjoerg if (close(fd))
841907ec3aSjoerg err(1, "failed to write temporary database %s", db_name_tmp);
851907ec3aSjoerg }
861907ec3aSjoerg
871907ec3aSjoerg static void
cdb_add_entry(dev_t dev,mode_t type,const char * relpath)881907ec3aSjoerg cdb_add_entry(dev_t dev, mode_t type, const char *relpath)
891907ec3aSjoerg {
901907ec3aSjoerg uint8_t *buf;
911907ec3aSjoerg size_t len;
921907ec3aSjoerg
931907ec3aSjoerg len = strlen(relpath) + 1;
941907ec3aSjoerg buf = malloc(len + 10);
951907ec3aSjoerg le64enc(buf, dev);
961907ec3aSjoerg le16enc(buf + 8, type);
971907ec3aSjoerg memcpy(buf + 10, relpath, len);
981907ec3aSjoerg cdbw_put(db, buf, 10, buf, len + 10);
991907ec3aSjoerg free(buf);
1001907ec3aSjoerg }
1011907ec3aSjoerg
1021907ec3aSjoerg static void
compat_open(void)1031907ec3aSjoerg compat_open(void)
1041907ec3aSjoerg {
1053bb0bce3Sjoerg static HASHINFO openinfo = {
106487a4fb8Shannken 4096, /* bsize */
107487a4fb8Shannken 128, /* ffactor */
108487a4fb8Shannken 1024, /* nelem */
109487a4fb8Shannken 2048 * 1024, /* cachesize */
110487a4fb8Shannken NULL, /* hash() */
111487a4fb8Shannken 0 /* lorder */
112487a4fb8Shannken };
113487a4fb8Shannken
1141907ec3aSjoerg db_compat = dbopen(db_name_tmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC,
1151907ec3aSjoerg FILE_PERMISSION, DB_HASH, &openinfo);
1161907ec3aSjoerg
1171907ec3aSjoerg if (db_compat == NULL)
1181907ec3aSjoerg err(1, "failed to create temporary database %s",
1191907ec3aSjoerg db_name_tmp);
1201907ec3aSjoerg }
1211907ec3aSjoerg
1221907ec3aSjoerg static void
compat_close(void)1231907ec3aSjoerg compat_close(void)
12461f28255Scgd {
1251907ec3aSjoerg if ((*db_compat->close)(db_compat))
1261907ec3aSjoerg err(1, "failed to write temporary database %s", db_name_tmp);
12761f28255Scgd }
12861f28255Scgd
1291907ec3aSjoerg static void
compat_add_entry(dev_t dev,mode_t type,const char * relpath)1301907ec3aSjoerg compat_add_entry(dev_t dev, mode_t type, const char *relpath)
1311907ec3aSjoerg {
13261f28255Scgd /*
13361f28255Scgd * Keys are a mode_t followed by a dev_t. The former is the type of
134bbfccb8fSmycroft * the file (mode & S_IFMT), the latter is the st_rdev field. Note
135bbfccb8fSmycroft * that the structure may contain padding, so we have to clear it
136bbfccb8fSmycroft * out here.
13761f28255Scgd */
1381907ec3aSjoerg struct {
1391907ec3aSjoerg mode_t type;
1401907ec3aSjoerg dev_t dev;
1411907ec3aSjoerg } bkey;
1421907ec3aSjoerg struct {
1431907ec3aSjoerg mode_t type;
1441907ec3aSjoerg int32_t dev;
1451907ec3aSjoerg } obkey;
1461907ec3aSjoerg DBT data, key;
1471907ec3aSjoerg
148f3c42fa4Schristos (void)memset(&bkey, 0, sizeof(bkey));
14961f28255Scgd key.data = &bkey;
15061f28255Scgd key.size = sizeof(bkey);
1511907ec3aSjoerg data.data = __UNCONST(relpath);
1521907ec3aSjoerg data.size = strlen(relpath) + 1;
1531907ec3aSjoerg bkey.type = type;
1541907ec3aSjoerg bkey.dev = dev;
1551907ec3aSjoerg if ((*db_compat->put)(db_compat, &key, &data, 0))
1561907ec3aSjoerg err(1, "failed to write temporary database %s", db_name_tmp);
15761f28255Scgd
15861f28255Scgd /*
1591907ec3aSjoerg * If the device fits into the old 32bit format, add compat entry
1601907ec3aSjoerg * for pre-NetBSD6 libc.
16161f28255Scgd */
1621907ec3aSjoerg
1631907ec3aSjoerg if ((dev_t)(int32_t)dev != dev)
1641907ec3aSjoerg return;
1651907ec3aSjoerg
1661907ec3aSjoerg (void)memset(&obkey, 0, sizeof(obkey));
1671907ec3aSjoerg key.data = &obkey;
1681907ec3aSjoerg key.size = sizeof(obkey);
1691907ec3aSjoerg data.data = __UNCONST(relpath);
1701907ec3aSjoerg data.size = strlen(relpath) + 1;
1711907ec3aSjoerg obkey.type = type;
1721907ec3aSjoerg obkey.dev = (int32_t)dev;
1731907ec3aSjoerg if ((*db_compat->put)(db_compat, &key, &data, 0))
1741907ec3aSjoerg err(1, "failed to write temporary database %s", db_name_tmp);
17561f28255Scgd }
1761907ec3aSjoerg
1771907ec3aSjoerg int
main(int argc,char ** argv)1781907ec3aSjoerg main(int argc, char **argv)
1791907ec3aSjoerg {
1801907ec3aSjoerg struct stat *st;
1811907ec3aSjoerg FTS *ftsp;
1821907ec3aSjoerg FTSENT *p;
1831907ec3aSjoerg int ch;
1841907ec3aSjoerg char *pathv[2];
1851907ec3aSjoerg size_t dlen;
1861907ec3aSjoerg int compat_mode;
1871907ec3aSjoerg
1881907ec3aSjoerg setprogname(argv[0]);
1891907ec3aSjoerg compat_mode = 0;
1901907ec3aSjoerg
1911907ec3aSjoerg while ((ch = getopt(argc, argv, "co:")) != -1)
1921907ec3aSjoerg switch (ch) {
1931907ec3aSjoerg case 'c':
1941907ec3aSjoerg compat_mode = 1;
1951907ec3aSjoerg break;
1961907ec3aSjoerg case 'o':
1971907ec3aSjoerg db_name = optarg;
1981907ec3aSjoerg break;
1991907ec3aSjoerg default:
2001907ec3aSjoerg usage();
2011907ec3aSjoerg }
2021907ec3aSjoerg argc -= optind;
2031907ec3aSjoerg argv += optind;
2041907ec3aSjoerg
2051907ec3aSjoerg if (argc > 1)
2061907ec3aSjoerg usage();
2071907ec3aSjoerg
2081907ec3aSjoerg pathv[1] = NULL;
2091907ec3aSjoerg if (argc == 1)
2101907ec3aSjoerg pathv[0] = argv[0];
2111907ec3aSjoerg else
2121907ec3aSjoerg pathv[0] = __UNCONST(_PATH_DEV);
2131907ec3aSjoerg
2141907ec3aSjoerg ftsp = fts_open(pathv, FTS_NOCHDIR | FTS_PHYSICAL, NULL);
2151907ec3aSjoerg if (ftsp == NULL)
2161907ec3aSjoerg err(1, "fts_open: %s", pathv[0]);
2171907ec3aSjoerg
2181907ec3aSjoerg if (db_name == NULL) {
2191907ec3aSjoerg if (compat_mode)
2201907ec3aSjoerg db_name = _PATH_DEVDB;
2211907ec3aSjoerg else
2221907ec3aSjoerg db_name = _PATH_DEVCDB;
2231907ec3aSjoerg }
2241907ec3aSjoerg easprintf(&db_name_tmp, "%s.XXXXXXX", db_name);
2251907ec3aSjoerg mktemp(db_name_tmp);
2261907ec3aSjoerg
2271907ec3aSjoerg if (compat_mode)
2281907ec3aSjoerg compat_open();
2291907ec3aSjoerg else
2301907ec3aSjoerg cdb_open();
2311907ec3aSjoerg
2321907ec3aSjoerg while ((p = fts_read(ftsp)) != NULL) {
2331907ec3aSjoerg if (p->fts_info != FTS_DEFAULT)
2341907ec3aSjoerg continue;
2351907ec3aSjoerg
2361907ec3aSjoerg st = p->fts_statp;
2371907ec3aSjoerg if (!S_ISCHR(st->st_mode) && !S_ISBLK(st->st_mode))
2381907ec3aSjoerg continue;
2391907ec3aSjoerg dlen = strlen(pathv[0]);
2401907ec3aSjoerg while (pathv[0][dlen] == '/')
2411907ec3aSjoerg ++dlen;
2421907ec3aSjoerg if (compat_mode)
2431907ec3aSjoerg compat_add_entry(st->st_rdev, st->st_mode & S_IFMT,
2441907ec3aSjoerg p->fts_path + dlen);
2451907ec3aSjoerg else
2461907ec3aSjoerg cdb_add_entry(st->st_rdev, st->st_mode & S_IFMT,
2471907ec3aSjoerg p->fts_path + dlen);
2481907ec3aSjoerg }
249f3c42fa4Schristos (void)fts_close(ftsp);
2503ac4f70dSmanu
2511907ec3aSjoerg if (compat_mode)
2521907ec3aSjoerg compat_close();
2531907ec3aSjoerg else
2541907ec3aSjoerg cdb_close();
2551907ec3aSjoerg
2561907ec3aSjoerg if (rename(db_name_tmp, db_name) == -1)
2571907ec3aSjoerg err(1, "rename %s to %s", db_name_tmp, db_name);
258f3c42fa4Schristos return 0;
25961f28255Scgd }
26061f28255Scgd
261f3c42fa4Schristos static void
usage(void)262cf66608cSxtraeme usage(void)
26361f28255Scgd {
264bbfccb8fSmycroft
2651907ec3aSjoerg (void)fprintf(stderr, "Usage: %s [-c] [-o database] [directory]\n",
266f3c42fa4Schristos getprogname());
26761f28255Scgd exit(1);
26861f28255Scgd }
269