Home | History | Annotate | Line # | Download | only in fsck
fsutil.c revision 1.12
      1 /*	$NetBSD: fsutil.c,v 1.12 2003/03/28 08:12:38 perseant 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __RCSID("$NetBSD: fsutil.c,v 1.12 2003/03/28 08:12:38 perseant Exp $");
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 
     43 #include <stdio.h>
     44 #include <string.h>
     45 #include <stdlib.h>
     46 #include <stdarg.h>
     47 #include <errno.h>
     48 #include <fstab.h>
     49 #include <err.h>
     50 
     51 #include <sys/types.h>
     52 #include <sys/stat.h>
     53 
     54 #include "fsutil.h"
     55 
     56 static const char *dev = NULL;
     57 static int hot = 0;
     58 static int preen = 0;
     59 
     60 void
     61 setcdevname(const char *cd, int pr)
     62 {
     63 
     64 	dev = cd;
     65 	preen = pr;
     66 }
     67 
     68 const char *
     69 cdevname(void)
     70 {
     71 
     72 	return dev;
     73 }
     74 
     75 int
     76 hotroot(void)
     77 {
     78 
     79 	return hot;
     80 }
     81 
     82 /*VARARGS*/
     83 void
     84 errexit(const char *fmt, ...)
     85 {
     86 	va_list ap;
     87 
     88 	va_start(ap, fmt);
     89 	(void) vfprintf(stderr, fmt, ap);
     90 	va_end(ap);
     91 	exit(8);
     92 }
     93 
     94 void
     95 vmsg(int fatal, const char *fmt, va_list ap)
     96 {
     97 
     98 	if (!fatal && preen)
     99 		(void) printf("%s: ", dev);
    100 
    101 	(void) vprintf(fmt, ap);
    102 
    103 	if (fatal && preen)
    104 		(void) printf("\n");
    105 
    106 	if (fatal && preen) {
    107 		(void) printf(
    108 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
    109 		    dev, getprogname());
    110 		exit(8);
    111 	}
    112 }
    113 
    114 /*VARARGS*/
    115 void
    116 pfatal(const char *fmt, ...)
    117 {
    118 	va_list ap;
    119 
    120 	va_start(ap, fmt);
    121 	vmsg(1, fmt, ap);
    122 	va_end(ap);
    123 }
    124 
    125 /*VARARGS*/
    126 void
    127 pwarn(const char *fmt, ...)
    128 {
    129 	va_list ap;
    130 
    131 	va_start(ap, fmt);
    132 	vmsg(0, fmt, ap);
    133 	va_end(ap);
    134 }
    135 
    136 void
    137 perror(const char *s)
    138 {
    139 
    140 	pfatal("%s (%s)", s, strerror(errno));
    141 }
    142 
    143 void
    144 panic(const char *fmt, ...)
    145 {
    146 	va_list ap;
    147 
    148 	va_start(ap, fmt);
    149 	vmsg(1, fmt, ap);
    150 	va_end(ap);
    151 	exit(8);
    152 }
    153 
    154 const char *
    155 unrawname(const char *name)
    156 {
    157 	static char unrawbuf[MAXPATHLEN];
    158 	const char *dp;
    159 	struct stat stb;
    160 
    161 	if ((dp = strrchr(name, '/')) == 0)
    162 		return (name);
    163 	if (stat(name, &stb) < 0)
    164 		return (name);
    165 	if (!S_ISCHR(stb.st_mode))
    166 		return (name);
    167 	if (dp[1] != 'r')
    168 		return (name);
    169 	(void)snprintf(unrawbuf, sizeof(unrawbuf), "%.*s/%s",
    170 	    (int)(dp - name), name, dp + 2);
    171 	return (unrawbuf);
    172 }
    173 
    174 const char *
    175 rawname(const char *name)
    176 {
    177 	static char rawbuf[MAXPATHLEN];
    178 	const char *dp;
    179 
    180 	if ((dp = strrchr(name, '/')) == 0)
    181 		return (0);
    182 	(void)snprintf(rawbuf, sizeof(rawbuf), "%.*s/r%s",
    183 	    (int)(dp - name), name, dp + 1);
    184 	return (rawbuf);
    185 }
    186 
    187 const char *
    188 blockcheck(const char *origname)
    189 {
    190 	struct stat stslash, stblock, stchar;
    191 	const char *newname, *raw;
    192 	struct fstab *fsp;
    193 	int retried = 0;
    194 
    195 	hot = 0;
    196 	if (stat("/", &stslash) < 0) {
    197 		perror("/");
    198 		printf("Can't stat root\n");
    199 		return (origname);
    200 	}
    201 	newname = origname;
    202 retry:
    203 	if (stat(newname, &stblock) < 0) {
    204 		perror(newname);
    205 		printf("Can't stat %s\n", newname);
    206 		return (origname);
    207 	}
    208 	if (S_ISBLK(stblock.st_mode)) {
    209 		if (stslash.st_dev == stblock.st_rdev)
    210 			hot++;
    211 		raw = rawname(newname);
    212 		if (stat(raw, &stchar) < 0) {
    213 			perror(raw);
    214 			printf("Can't stat %s\n", raw);
    215 			return (origname);
    216 		}
    217 		if (S_ISCHR(stchar.st_mode)) {
    218 			return (raw);
    219 		} else {
    220 			printf("%s is not a character device\n", raw);
    221 			return (origname);
    222 		}
    223 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
    224 		newname = unrawname(newname);
    225 		retried++;
    226 		goto retry;
    227 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
    228 		newname = fsp->fs_spec;
    229 		retried++;
    230 		goto retry;
    231 	}
    232 	/*
    233 	 * Not a block or character device, just return name and
    234 	 * let the user decide whether to use it.
    235 	 */
    236 	return (origname);
    237 }
    238 
    239 
    240 void *
    241 emalloc(size_t s)
    242 {
    243 	void *p;
    244 
    245 	p = malloc(s);
    246 	if (p == NULL)
    247 		err(1, "malloc failed");
    248 	return (p);
    249 }
    250 
    251 
    252 void *
    253 erealloc(void *p, size_t s)
    254 {
    255 	void *q;
    256 
    257 	q = realloc(p, s);
    258 	if (q == NULL)
    259 		err(1, "realloc failed");
    260 	return (q);
    261 }
    262 
    263 
    264 char *
    265 estrdup(const char *s)
    266 {
    267 	char *p;
    268 
    269 	p = strdup(s);
    270 	if (p == NULL)
    271 		err(1, "strdup failed");
    272 	return (p);
    273 }
    274