fsutil.c revision 1.14 1 1.14 dsl /* $NetBSD: fsutil.c,v 1.14 2003/10/20 12:04:38 dsl 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.14 dsl __RCSID("$NetBSD: fsutil.c,v 1.14 2003/10/20 12:04:38 dsl 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.1 christos
57 1.1 christos void
58 1.10 lukem setcdevname(const char *cd, int pr)
59 1.1 christos {
60 1.10 lukem
61 1.1 christos dev = cd;
62 1.1 christos preen = pr;
63 1.1 christos }
64 1.1 christos
65 1.1 christos const char *
66 1.10 lukem cdevname(void)
67 1.1 christos {
68 1.10 lukem
69 1.1 christos return dev;
70 1.1 christos }
71 1.1 christos
72 1.1 christos int
73 1.10 lukem hotroot(void)
74 1.1 christos {
75 1.10 lukem
76 1.1 christos return hot;
77 1.1 christos }
78 1.1 christos
79 1.1 christos /*VARARGS*/
80 1.1 christos void
81 1.1 christos errexit(const char *fmt, ...)
82 1.1 christos {
83 1.1 christos va_list ap;
84 1.3 christos
85 1.1 christos va_start(ap, fmt);
86 1.1 christos (void) vfprintf(stderr, fmt, ap);
87 1.1 christos va_end(ap);
88 1.1 christos exit(8);
89 1.1 christos }
90 1.1 christos
91 1.12 perseant void
92 1.10 lukem vmsg(int fatal, const char *fmt, va_list ap)
93 1.1 christos {
94 1.10 lukem
95 1.1 christos if (!fatal && preen)
96 1.14 dsl (void)printf("%s: ", dev);
97 1.14 dsl if (quiet && !preen) {
98 1.14 dsl (void)printf("** %s (vmsg)\n", dev);
99 1.14 dsl quiet = 0;
100 1.14 dsl }
101 1.1 christos
102 1.1 christos (void) vprintf(fmt, ap);
103 1.1 christos
104 1.1 christos if (fatal && preen)
105 1.1 christos (void) printf("\n");
106 1.1 christos
107 1.1 christos if (fatal && preen) {
108 1.1 christos (void) printf(
109 1.1 christos "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
110 1.9 cgd dev, getprogname());
111 1.1 christos exit(8);
112 1.1 christos }
113 1.1 christos }
114 1.1 christos
115 1.1 christos /*VARARGS*/
116 1.1 christos void
117 1.1 christos pfatal(const char *fmt, ...)
118 1.1 christos {
119 1.1 christos va_list ap;
120 1.3 christos
121 1.1 christos va_start(ap, fmt);
122 1.1 christos vmsg(1, fmt, ap);
123 1.1 christos va_end(ap);
124 1.1 christos }
125 1.1 christos
126 1.1 christos /*VARARGS*/
127 1.1 christos void
128 1.1 christos pwarn(const char *fmt, ...)
129 1.1 christos {
130 1.1 christos va_list ap;
131 1.10 lukem
132 1.1 christos va_start(ap, fmt);
133 1.1 christos vmsg(0, fmt, ap);
134 1.1 christos va_end(ap);
135 1.1 christos }
136 1.1 christos
137 1.1 christos void
138 1.10 lukem perror(const char *s)
139 1.1 christos {
140 1.10 lukem
141 1.1 christos pfatal("%s (%s)", s, strerror(errno));
142 1.1 christos }
143 1.1 christos
144 1.1 christos void
145 1.1 christos panic(const char *fmt, ...)
146 1.1 christos {
147 1.1 christos va_list ap;
148 1.3 christos
149 1.1 christos va_start(ap, fmt);
150 1.1 christos vmsg(1, fmt, ap);
151 1.1 christos va_end(ap);
152 1.1 christos exit(8);
153 1.1 christos }
154 1.1 christos
155 1.6 mycroft const char *
156 1.10 lukem unrawname(const char *name)
157 1.1 christos {
158 1.11 lukem static char unrawbuf[MAXPATHLEN];
159 1.6 mycroft const char *dp;
160 1.1 christos struct stat stb;
161 1.1 christos
162 1.1 christos if ((dp = strrchr(name, '/')) == 0)
163 1.1 christos return (name);
164 1.1 christos if (stat(name, &stb) < 0)
165 1.1 christos return (name);
166 1.1 christos if (!S_ISCHR(stb.st_mode))
167 1.1 christos return (name);
168 1.1 christos if (dp[1] != 'r')
169 1.1 christos return (name);
170 1.11 lukem (void)snprintf(unrawbuf, sizeof(unrawbuf), "%.*s/%s",
171 1.11 lukem (int)(dp - name), name, dp + 2);
172 1.6 mycroft return (unrawbuf);
173 1.1 christos }
174 1.1 christos
175 1.6 mycroft const char *
176 1.10 lukem rawname(const char *name)
177 1.1 christos {
178 1.11 lukem static char rawbuf[MAXPATHLEN];
179 1.6 mycroft const char *dp;
180 1.1 christos
181 1.1 christos if ((dp = strrchr(name, '/')) == 0)
182 1.1 christos return (0);
183 1.11 lukem (void)snprintf(rawbuf, sizeof(rawbuf), "%.*s/r%s",
184 1.11 lukem (int)(dp - name), name, dp + 1);
185 1.1 christos return (rawbuf);
186 1.1 christos }
187 1.1 christos
188 1.6 mycroft const char *
189 1.10 lukem blockcheck(const char *origname)
190 1.1 christos {
191 1.1 christos struct stat stslash, stblock, stchar;
192 1.6 mycroft const char *newname, *raw;
193 1.1 christos struct fstab *fsp;
194 1.1 christos int retried = 0;
195 1.1 christos
196 1.1 christos hot = 0;
197 1.1 christos if (stat("/", &stslash) < 0) {
198 1.1 christos perror("/");
199 1.1 christos printf("Can't stat root\n");
200 1.1 christos return (origname);
201 1.1 christos }
202 1.1 christos newname = origname;
203 1.1 christos retry:
204 1.1 christos if (stat(newname, &stblock) < 0) {
205 1.1 christos perror(newname);
206 1.1 christos printf("Can't stat %s\n", newname);
207 1.1 christos return (origname);
208 1.1 christos }
209 1.1 christos if (S_ISBLK(stblock.st_mode)) {
210 1.1 christos if (stslash.st_dev == stblock.st_rdev)
211 1.1 christos hot++;
212 1.1 christos raw = rawname(newname);
213 1.1 christos if (stat(raw, &stchar) < 0) {
214 1.1 christos perror(raw);
215 1.1 christos printf("Can't stat %s\n", raw);
216 1.1 christos return (origname);
217 1.1 christos }
218 1.1 christos if (S_ISCHR(stchar.st_mode)) {
219 1.1 christos return (raw);
220 1.1 christos } else {
221 1.1 christos printf("%s is not a character device\n", raw);
222 1.1 christos return (origname);
223 1.1 christos }
224 1.1 christos } else if (S_ISCHR(stblock.st_mode) && !retried) {
225 1.1 christos newname = unrawname(newname);
226 1.1 christos retried++;
227 1.1 christos goto retry;
228 1.1 christos } else if ((fsp = getfsfile(newname)) != 0 && !retried) {
229 1.1 christos newname = fsp->fs_spec;
230 1.1 christos retried++;
231 1.1 christos goto retry;
232 1.1 christos }
233 1.1 christos /*
234 1.1 christos * Not a block or character device, just return name and
235 1.1 christos * let the user decide whether to use it.
236 1.1 christos */
237 1.1 christos return (origname);
238 1.1 christos }
239 1.1 christos
240 1.1 christos
241 1.1 christos void *
242 1.10 lukem emalloc(size_t s)
243 1.1 christos {
244 1.5 mycroft void *p;
245 1.5 mycroft
246 1.5 mycroft p = malloc(s);
247 1.1 christos if (p == NULL)
248 1.1 christos err(1, "malloc failed");
249 1.5 mycroft return (p);
250 1.2 christos }
251 1.2 christos
252 1.2 christos
253 1.2 christos void *
254 1.10 lukem erealloc(void *p, size_t s)
255 1.2 christos {
256 1.5 mycroft void *q;
257 1.5 mycroft
258 1.5 mycroft q = realloc(p, s);
259 1.5 mycroft if (q == NULL)
260 1.2 christos err(1, "realloc failed");
261 1.5 mycroft return (q);
262 1.1 christos }
263 1.1 christos
264 1.1 christos
265 1.1 christos char *
266 1.10 lukem estrdup(const char *s)
267 1.1 christos {
268 1.5 mycroft char *p;
269 1.5 mycroft
270 1.5 mycroft p = strdup(s);
271 1.1 christos if (p == NULL)
272 1.1 christos err(1, "strdup failed");
273 1.5 mycroft return (p);
274 1.1 christos }
275