1 /* $NetBSD: only.c,v 1.3 2017/09/07 04:04:13 nakayama Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of The NetBSD Foundation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <stdbool.h> 41 #include <time.h> 42 #include <err.h> 43 #include <util.h> 44 45 #include "extern.h" 46 47 struct hentry { 48 char *str; 49 uint32_t hash; 50 struct hentry *next; 51 }; 52 53 static struct hentry *table[1024]; 54 static bool loaded; 55 56 static uint32_t 57 hash_str(const char *str) 58 { 59 const uint8_t *s = (const uint8_t *)str; 60 uint8_t c; 61 uint32_t hash = 0; 62 while ((c = *s++) != '\0') 63 hash = hash * 33 + c; /* "perl": k=33, r=r+r/32 */ 64 return hash + (hash >> 5); 65 } 66 67 static bool 68 hash_find(const char *str, uint32_t *h) 69 { 70 struct hentry *e; 71 *h = hash_str(str) % NELEM(table); 72 73 for (e = table[*h]; e; e = e->next) 74 if (e->hash == *h && strcmp(e->str, str) == 0) 75 return true; 76 return false; 77 } 78 79 static void 80 hash_insert(char *str, uint32_t h) 81 { 82 struct hentry *e; 83 char *x; 84 85 if ((e = malloc(sizeof(*e))) == NULL) 86 mtree_err("memory allocation error"); 87 if ((x = strdup(str)) == NULL) 88 mtree_err("memory allocation error"); 89 90 e->str = x; 91 e->hash = h; 92 e->next = table[h]; 93 table[h] = e; 94 } 95 96 static void 97 fill(char *str) 98 { 99 uint32_t h; 100 char *ptr = strrchr(str, '/'); 101 102 if (ptr == NULL) 103 return; 104 105 *ptr = '\0'; 106 if (!hash_find(str, &h)) { 107 hash_insert(str, h); 108 fill(str); 109 } 110 *ptr = '/'; 111 } 112 113 void 114 load_only(const char *fname) 115 { 116 FILE *fp; 117 char *line; 118 size_t len, lineno; 119 120 if ((fp = fopen(fname, "r")) == NULL) 121 err(1, "Cannot open `%s'", fname); 122 123 while ((line = fparseln(fp, &len, &lineno, NULL, FPARSELN_UNESCALL))) { 124 uint32_t h; 125 if (hash_find(line, &h)) 126 err(1, "Duplicate entry %s", line); 127 hash_insert(line, h); 128 fill(line); 129 free(line); 130 } 131 132 fclose(fp); 133 loaded = true; 134 } 135 136 bool 137 find_only(const char *path) 138 { 139 uint32_t h; 140 141 if (!loaded) 142 return true; 143 return hash_find(path, &h); 144 } 145