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