xref: /netbsd/sbin/fsck/fsutil.c (revision 6550d01e)
1 /*	$NetBSD: fsutil.c,v 1.19 2010/02/04 23:55:42 christos 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/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: fsutil.c,v 1.19 2010/02/04 23:55:42 christos Exp $");
35 #endif /* not lint */
36 
37 #include <sys/param.h>
38 
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <stdarg.h>
43 #include <errno.h>
44 #include <fstab.h>
45 #include <err.h>
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 
50 #include "fsutil.h"
51 #include "exitvalues.h"
52 
53 static const char *dev = NULL;
54 static int hot = 0;
55 static int preen = 0;
56 int quiet;
57 #define F_ERROR	0x80000000
58 
59 void
60 setcdevname(const char *cd, int pr)
61 {
62 
63 	dev = cd;
64 	preen = pr;
65 }
66 
67 const char *
68 cdevname(void)
69 {
70 
71 	return dev;
72 }
73 
74 int
75 hotroot(void)
76 {
77 
78 	return hot;
79 }
80 
81 /*VARARGS*/
82 void
83 errexit(const char *fmt, ...)
84 {
85 	va_list ap;
86 
87 	va_start(ap, fmt);
88 	(void) vfprintf(stderr, fmt, ap);
89 	va_end(ap);
90 	(void)fprintf(stderr, "\n");
91 	exit(FSCK_EXIT_CHECK_FAILED);
92 }
93 
94 void
95 vmsg(int fatal, const char *fmt, va_list ap)
96 {
97 	int serr = fatal & F_ERROR;
98 	int serrno = errno;
99 	fatal &= ~F_ERROR;
100 
101 	if (!fatal && preen)
102 		(void)printf("%s: ", dev);
103 	if (quiet && !preen) {
104 		(void)printf("** %s (vmsg)\n", dev);
105 		quiet = 0;
106 	}
107 
108 	(void) vprintf(fmt, ap);
109 	if (serr)
110 		printf(" (%s)", strerror(serrno));
111 
112 	if (fatal && preen)
113 		(void) printf("\n");
114 
115 	if (fatal && preen) {
116 		(void) printf(
117 		    "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
118 		    dev, getprogname());
119 		exit(FSCK_EXIT_CHECK_FAILED);
120 	}
121 }
122 
123 /*VARARGS*/
124 void
125 pfatal(const char *fmt, ...)
126 {
127 	va_list ap;
128 
129 	va_start(ap, fmt);
130 	vmsg(1, fmt, ap);
131 	va_end(ap);
132 }
133 
134 /*VARARGS*/
135 void
136 pwarn(const char *fmt, ...)
137 {
138 	va_list ap;
139 
140 	va_start(ap, fmt);
141 	vmsg(0, fmt, ap);
142 	va_end(ap);
143 }
144 
145 void
146 perr(const char *fmt, ...)
147 {
148 	va_list ap;
149 
150 	va_start(ap, fmt);
151 	vmsg(1 | F_ERROR, fmt, ap);
152 	va_end(ap);
153 }
154 
155 void
156 panic(const char *fmt, ...)
157 {
158 	va_list ap;
159 
160 	va_start(ap, fmt);
161 	vmsg(1, fmt, ap);
162 	va_end(ap);
163 	exit(FSCK_EXIT_CHECK_FAILED);
164 }
165 
166 const char *
167 unrawname(const char *name)
168 {
169 	static char unrawbuf[MAXPATHLEN];
170 	const char *dp;
171 	struct stat stb;
172 
173 	if ((dp = strrchr(name, '/')) == 0)
174 		return (name);
175 	if (stat(name, &stb) < 0)
176 		return (name);
177 	if (!S_ISCHR(stb.st_mode))
178 		return (name);
179 	if (dp[1] != 'r')
180 		return (name);
181 	(void)snprintf(unrawbuf, sizeof(unrawbuf), "%.*s/%s",
182 	    (int)(dp - name), name, dp + 2);
183 	return (unrawbuf);
184 }
185 
186 const char *
187 rawname(const char *name)
188 {
189 	static char rawbuf[MAXPATHLEN];
190 	const char *dp;
191 
192 	if ((dp = strrchr(name, '/')) == 0)
193 		return (0);
194 	(void)snprintf(rawbuf, sizeof(rawbuf), "%.*s/r%s",
195 	    (int)(dp - name), name, dp + 1);
196 	return (rawbuf);
197 }
198 
199 const char *
200 blockcheck(const char *origname)
201 {
202 	struct stat stslash, stblock, stchar;
203 	const char *newname, *raw;
204 	struct fstab *fsp;
205 	int retried = 0;
206 
207 	hot = 0;
208 	if (stat("/", &stslash) < 0) {
209 		perr("Can't stat `/'");
210 		return (origname);
211 	}
212 	newname = origname;
213 retry:
214 	if (stat(newname, &stblock) < 0) {
215 		perr("Can't stat `%s'", newname);
216 		return (origname);
217 	}
218 	if (S_ISBLK(stblock.st_mode)) {
219 		if (stslash.st_dev == stblock.st_rdev)
220 			hot++;
221 		raw = rawname(newname);
222 		if (stat(raw, &stchar) < 0) {
223 			perr("Can't stat `%s'", raw);
224 			return (origname);
225 		}
226 		if (S_ISCHR(stchar.st_mode)) {
227 			return (raw);
228 		} else {
229 			printf("%s is not a character device\n", raw);
230 			return (origname);
231 		}
232 	} else if (S_ISCHR(stblock.st_mode) && !retried) {
233 		newname = unrawname(newname);
234 		retried++;
235 		goto retry;
236 	} else if ((fsp = getfsfile(newname)) != 0 && !retried) {
237 		newname = fsp->fs_spec;
238 		retried++;
239 		goto retry;
240 	}
241 	/*
242 	 * Not a block or character device, just return name and
243 	 * let the user decide whether to use it.
244 	 */
245 	return (origname);
246 }
247 
248 const char *
249 print_mtime(time_t t)
250 {
251 	static char b[128];
252 	char *p = ctime(&t);
253 	if (p != NULL)
254 		(void)snprintf(b, sizeof(b), "%12.12s %4.4s ", &p[4], &p[20]);
255 	else
256 		(void)snprintf(b, sizeof(b), "%lld ", (long long)t);
257 	return b;
258 }
259