1 /* $NetBSD: misc.c,v 1.34 2012/12/20 19:09:25 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1991, 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 * @(#)misc.c 8.1 (Berkeley) 6/6/93 32 */ 33 34 #if HAVE_NBTOOL_CONFIG_H 35 #include "nbtool_config.h" 36 #endif 37 38 #include <sys/cdefs.h> 39 #if defined(__RCSID) && !defined(lint) 40 __RCSID("$NetBSD: misc.c,v 1.34 2012/12/20 19:09:25 christos Exp $"); 41 #endif /* not lint */ 42 43 #include <sys/types.h> 44 #include <sys/stat.h> 45 46 #include <stdarg.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "extern.h" 52 53 #if 1 /* defined(__minix): LSC: our mkfs doesn't understand escaped names, 54 so always default to netbsd6 escaping mode. */ 55 enum flavor flavor = F_NETBSD6; 56 #else 57 enum flavor flavor = F_MTREE; 58 #endif /* defined(__minix) */ 59 60 typedef struct _key { 61 const char *name; /* key name */ 62 u_int val; /* value */ 63 64 #define NEEDVALUE 0x01 65 u_int flags; 66 } KEY; 67 68 /* NB: the following tables must be sorted lexically. */ 69 static KEY keylist[] = { 70 {"cksum", F_CKSUM, NEEDVALUE}, 71 {"device", F_DEV, NEEDVALUE}, 72 {"flags", F_FLAGS, NEEDVALUE}, 73 {"gid", F_GID, NEEDVALUE}, 74 {"gname", F_GNAME, NEEDVALUE}, 75 {"ignore", F_IGN, 0}, 76 {"link", F_SLINK, NEEDVALUE}, 77 {"md5", F_MD5, NEEDVALUE}, 78 {"md5digest", F_MD5, NEEDVALUE}, 79 {"mode", F_MODE, NEEDVALUE}, 80 {"nlink", F_NLINK, NEEDVALUE}, 81 {"nochange", F_NOCHANGE, 0}, 82 {"optional", F_OPT, 0}, 83 {"ripemd160digest", F_RMD160, NEEDVALUE}, 84 {"rmd160", F_RMD160, NEEDVALUE}, 85 {"rmd160digest",F_RMD160, NEEDVALUE}, 86 {"sha1", F_SHA1, NEEDVALUE}, 87 {"sha1digest", F_SHA1, NEEDVALUE}, 88 {"sha256", F_SHA256, NEEDVALUE}, 89 {"sha256digest",F_SHA256, NEEDVALUE}, 90 {"sha384", F_SHA384, NEEDVALUE}, 91 {"sha384digest",F_SHA384, NEEDVALUE}, 92 {"sha512", F_SHA512, NEEDVALUE}, 93 {"sha512digest",F_SHA512, NEEDVALUE}, 94 {"size", F_SIZE, NEEDVALUE}, 95 {"tags", F_TAGS, NEEDVALUE}, 96 {"time", F_TIME, NEEDVALUE}, 97 {"type", F_TYPE, NEEDVALUE}, 98 {"uid", F_UID, NEEDVALUE}, 99 {"uname", F_UNAME, NEEDVALUE} 100 }; 101 102 static KEY typelist[] = { 103 {"block", F_BLOCK, 0}, 104 {"char", F_CHAR, 0}, 105 {"dir", F_DIR, 0}, 106 #ifdef S_IFDOOR 107 {"door", F_DOOR, 0}, 108 #endif 109 {"fifo", F_FIFO, 0}, 110 {"file", F_FILE, 0}, 111 {"link", F_LINK, 0}, 112 {"socket", F_SOCK, 0}, 113 }; 114 115 slist_t excludetags, includetags; 116 int keys = KEYDEFAULT; 117 118 119 int keycompare(const void *, const void *); 120 121 u_int 122 parsekey(const char *name, int *needvaluep) 123 { 124 static int allbits; 125 KEY *k, tmp; 126 127 if (allbits == 0) { 128 size_t i; 129 130 for (i = 0; i < sizeof(keylist) / sizeof(KEY); i++) 131 allbits |= keylist[i].val; 132 } 133 tmp.name = name; 134 if (strcmp(name, "all") == 0) 135 return (allbits); 136 k = (KEY *)bsearch(&tmp, keylist, sizeof(keylist) / sizeof(KEY), 137 sizeof(KEY), keycompare); 138 if (k == NULL) 139 mtree_err("unknown keyword `%s'", name); 140 141 if (needvaluep) 142 *needvaluep = k->flags & NEEDVALUE ? 1 : 0; 143 144 return (k->val); 145 } 146 147 u_int 148 parsetype(const char *name) 149 { 150 KEY *k, tmp; 151 152 tmp.name = name; 153 k = (KEY *)bsearch(&tmp, typelist, sizeof(typelist) / sizeof(KEY), 154 sizeof(KEY), keycompare); 155 if (k == NULL) 156 mtree_err("unknown file type `%s'", name); 157 158 return (k->val); 159 } 160 161 int 162 keycompare(const void *a, const void *b) 163 { 164 165 return (strcmp(((const KEY *)a)->name, ((const KEY *)b)->name)); 166 } 167 168 void 169 mtree_err(const char *fmt, ...) 170 { 171 va_list ap; 172 173 va_start(ap, fmt); 174 vwarnx(fmt, ap); 175 va_end(ap); 176 if (mtree_lineno) 177 warnx("failed at line %lu of the specification", 178 (u_long) mtree_lineno); 179 exit(1); 180 /* NOTREACHED */ 181 } 182 183 void 184 addtag(slist_t *list, char *elem) 185 { 186 187 #define TAG_CHUNK 20 188 189 if ((list->count % TAG_CHUNK) == 0) { 190 char **new; 191 192 new = (char **)realloc(list->list, (list->count + TAG_CHUNK) 193 * sizeof(char *)); 194 if (new == NULL) 195 mtree_err("memory allocation error"); 196 list->list = new; 197 } 198 list->list[list->count] = elem; 199 list->count++; 200 } 201 202 void 203 parsetags(slist_t *list, char *args) 204 { 205 char *p, *e; 206 int len; 207 208 if (args == NULL) { 209 addtag(list, NULL); 210 return; 211 } 212 while ((p = strsep(&args, ",")) != NULL) { 213 if (*p == '\0') 214 continue; 215 len = strlen(p) + 3; /* "," + p + ",\0" */ 216 if ((e = malloc(len)) == NULL) 217 mtree_err("memory allocation error"); 218 snprintf(e, len, ",%s,", p); 219 addtag(list, e); 220 } 221 } 222 223 /* 224 * matchtags 225 * returns 0 if there's a match from the exclude list in the node's tags, 226 * or there's an include list and no match. 227 * return 1 otherwise. 228 */ 229 int 230 matchtags(NODE *node) 231 { 232 int i; 233 234 if (node->tags) { 235 for (i = 0; i < excludetags.count; i++) 236 if (strstr(node->tags, excludetags.list[i])) 237 break; 238 if (i < excludetags.count) 239 return (0); 240 241 for (i = 0; i < includetags.count; i++) 242 if (strstr(node->tags, includetags.list[i])) 243 break; 244 if (i > 0 && i == includetags.count) 245 return (0); 246 } else if (includetags.count > 0) { 247 return (0); 248 } 249 return (1); 250 } 251 252 u_int 253 nodetoino(u_int type) 254 { 255 256 switch (type) { 257 case F_BLOCK: 258 return S_IFBLK; 259 case F_CHAR: 260 return S_IFCHR; 261 case F_DIR: 262 return S_IFDIR; 263 case F_FIFO: 264 return S_IFIFO; 265 case F_FILE: 266 return S_IFREG; 267 case F_LINK: 268 return S_IFLNK; 269 #ifdef S_IFSOCK 270 case F_SOCK: 271 return S_IFSOCK; 272 #endif 273 default: 274 printf("unknown type %d", type); 275 abort(); 276 } 277 /* NOTREACHED */ 278 } 279 280 const char * 281 nodetype(u_int type) 282 { 283 284 return (inotype(nodetoino(type))); 285 } 286 287 288 const char * 289 inotype(u_int type) 290 { 291 292 switch (type & S_IFMT) { 293 case S_IFBLK: 294 return ("block"); 295 case S_IFCHR: 296 return ("char"); 297 case S_IFDIR: 298 return ("dir"); 299 case S_IFIFO: 300 return ("fifo"); 301 case S_IFREG: 302 return ("file"); 303 case S_IFLNK: 304 return ("link"); 305 #ifdef S_IFSOCK 306 case S_IFSOCK: 307 return ("socket"); 308 #endif 309 #ifdef S_IFDOOR 310 case S_IFDOOR: 311 return ("door"); 312 #endif 313 default: 314 return ("unknown"); 315 } 316 /* NOTREACHED */ 317 } 318