Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / sbin / restore / utilities.c
blob6b984e4097495a57f577538dee50b360d5354f75
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)utilities.c 8.5 (Berkeley) 4/28/95";
33 #endif
34 static const char rcsid[] =
35 "$FreeBSD$";
36 #endif /* not lint */
38 #include <sys/param.h>
39 #include <sys/stat.h>
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ufs/dir.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
51 #include "restore.h"
52 #include "extern.h"
55 * Insure that all the components of a pathname exist.
57 void
58 pathcheck(char *name)
60 char *cp;
61 struct entry *ep;
62 char *start;
64 start = strchr(name, '/');
65 if (start == 0)
66 return;
67 for (cp = start; *cp != '\0'; cp++) {
68 if (*cp != '/')
69 continue;
70 *cp = '\0';
71 ep = lookupname(name);
72 if (ep == NULL) {
73 /* Safe; we know the pathname exists in the dump. */
74 ep = addentry(name, pathsearch(name)->d_ino, NODE);
75 newnode(ep);
77 ep->e_flags |= NEW|KEEP;
78 *cp = '/';
83 * Change a name to a unique temporary name.
85 void
86 mktempname(struct entry *ep)
88 char oldname[MAXPATHLEN];
90 if (ep->e_flags & TMPNAME)
91 badentry(ep, "mktempname: called with TMPNAME");
92 ep->e_flags |= TMPNAME;
93 (void) strcpy(oldname, myname(ep));
94 freename(ep->e_name);
95 ep->e_name = savename(gentempname(ep));
96 ep->e_namlen = strlen(ep->e_name);
97 renameit(oldname, myname(ep));
101 * Generate a temporary name for an entry.
103 char *
104 gentempname(struct entry *ep)
106 static char name[MAXPATHLEN];
107 struct entry *np;
108 long i = 0;
110 for (np = lookupino(ep->e_ino);
111 np != NULL && np != ep; np = np->e_links)
112 i++;
113 if (np == NULL)
114 badentry(ep, "not on ino list");
115 (void) sprintf(name, "%s%ld%lu", TMPHDR, i, (u_long)ep->e_ino);
116 return (name);
120 * Rename a file or directory.
122 void
123 renameit(char *from, char *to)
125 if (!Nflag && rename(from, to) < 0) {
126 fprintf(stderr, "warning: cannot rename %s to %s: %s\n",
127 from, to, strerror(errno));
128 return;
130 vprintf(stdout, "rename %s to %s\n", from, to);
134 * Create a new node (directory).
136 void
137 newnode(struct entry *np)
139 char *cp;
141 if (np->e_type != NODE)
142 badentry(np, "newnode: not a node");
143 cp = myname(np);
144 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
145 np->e_flags |= EXISTED;
146 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
147 return;
149 vprintf(stdout, "Make node %s\n", cp);
153 * Remove an old node (directory).
155 void
156 removenode(struct entry *ep)
158 char *cp;
160 if (ep->e_type != NODE)
161 badentry(ep, "removenode: not a node");
162 if (ep->e_entries != NULL)
163 badentry(ep, "removenode: non-empty directory");
164 ep->e_flags |= REMOVED;
165 ep->e_flags &= ~TMPNAME;
166 cp = myname(ep);
167 if (!Nflag && rmdir(cp) < 0) {
168 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
169 return;
171 vprintf(stdout, "Remove node %s\n", cp);
175 * Remove a leaf.
177 void
178 removeleaf(struct entry *ep)
180 char *cp;
182 if (ep->e_type != LEAF)
183 badentry(ep, "removeleaf: not a leaf");
184 ep->e_flags |= REMOVED;
185 ep->e_flags &= ~TMPNAME;
186 cp = myname(ep);
187 if (!Nflag && unlink(cp) < 0) {
188 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
189 return;
191 vprintf(stdout, "Remove leaf %s\n", cp);
195 * Create a link.
198 linkit(char *existing, char *new, int type)
201 /* if we want to unlink first, do it now so *link() won't fail */
202 if (uflag && !Nflag)
203 (void)unlink(new);
205 if (type == SYMLINK) {
206 if (!Nflag && symlink(existing, new) < 0) {
207 fprintf(stderr,
208 "warning: cannot create symbolic link %s->%s: %s\n",
209 new, existing, strerror(errno));
210 return (FAIL);
212 } else if (type == HARDLINK) {
213 int ret;
215 if (!Nflag && (ret = link(existing, new)) < 0) {
216 struct stat s;
219 * Most likely, the schg flag is set. Clear the
220 * flags and try again.
222 if (stat(existing, &s) == 0 && s.st_flags != 0 &&
223 chflags(existing, 0) == 0) {
224 ret = link(existing, new);
225 chflags(existing, s.st_flags);
227 if (ret < 0) {
228 fprintf(stderr, "warning: cannot create "
229 "hard link %s->%s: %s\n",
230 new, existing, strerror(errno));
231 return (FAIL);
234 } else {
235 panic("linkit: unknown type %d\n", type);
236 return (FAIL);
238 vprintf(stdout, "Create %s link %s->%s\n",
239 type == SYMLINK ? "symbolic" : "hard", new, existing);
240 return (GOOD);
244 * Create a whiteout.
247 addwhiteout(char *name)
250 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
251 fprintf(stderr, "warning: cannot create whiteout %s: %s\n",
252 name, strerror(errno));
253 return (FAIL);
255 vprintf(stdout, "Create whiteout %s\n", name);
256 return (GOOD);
260 * Delete a whiteout.
262 void
263 delwhiteout(struct entry *ep)
265 char *name;
267 if (ep->e_type != LEAF)
268 badentry(ep, "delwhiteout: not a leaf");
269 ep->e_flags |= REMOVED;
270 ep->e_flags &= ~TMPNAME;
271 name = myname(ep);
272 if (!Nflag && undelete(name) < 0) {
273 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
274 name, strerror(errno));
275 return;
277 vprintf(stdout, "Delete whiteout %s\n", name);
281 * find lowest number file (above "start") that needs to be extracted
283 ino_t
284 lowerbnd(ino_t start)
286 struct entry *ep;
288 for ( ; start < maxino; start++) {
289 ep = lookupino(start);
290 if (ep == NULL || ep->e_type == NODE)
291 continue;
292 if (ep->e_flags & (NEW|EXTRACT))
293 return (start);
295 return (start);
299 * find highest number file (below "start") that needs to be extracted
301 ino_t
302 upperbnd(ino_t start)
304 struct entry *ep;
306 for ( ; start > ROOTINO; start--) {
307 ep = lookupino(start);
308 if (ep == NULL || ep->e_type == NODE)
309 continue;
310 if (ep->e_flags & (NEW|EXTRACT))
311 return (start);
313 return (start);
317 * report on a badly formed entry
319 void
320 badentry(struct entry *ep, char *msg)
323 fprintf(stderr, "bad entry: %s\n", msg);
324 fprintf(stderr, "name: %s\n", myname(ep));
325 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
326 if (ep->e_sibling != NULL)
327 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
328 if (ep->e_entries != NULL)
329 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
330 if (ep->e_links != NULL)
331 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
332 if (ep->e_next != NULL)
333 fprintf(stderr,
334 "next hashchain name: %s\n", myname(ep->e_next));
335 fprintf(stderr, "entry type: %s\n",
336 ep->e_type == NODE ? "NODE" : "LEAF");
337 fprintf(stderr, "inode number: %lu\n", (u_long)ep->e_ino);
338 panic("flags: %s\n", flagvalues(ep));
342 * Construct a string indicating the active flag bits of an entry.
344 char *
345 flagvalues(struct entry *ep)
347 static char flagbuf[BUFSIZ];
349 (void) strcpy(flagbuf, "|NIL");
350 flagbuf[0] = '\0';
351 if (ep->e_flags & REMOVED)
352 (void) strcat(flagbuf, "|REMOVED");
353 if (ep->e_flags & TMPNAME)
354 (void) strcat(flagbuf, "|TMPNAME");
355 if (ep->e_flags & EXTRACT)
356 (void) strcat(flagbuf, "|EXTRACT");
357 if (ep->e_flags & NEW)
358 (void) strcat(flagbuf, "|NEW");
359 if (ep->e_flags & KEEP)
360 (void) strcat(flagbuf, "|KEEP");
361 if (ep->e_flags & EXISTED)
362 (void) strcat(flagbuf, "|EXISTED");
363 return (&flagbuf[1]);
367 * Check to see if a name is on a dump tape.
369 ino_t
370 dirlookup(const char *name)
372 struct direct *dp;
373 ino_t ino;
375 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
377 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
378 fprintf(stderr, "%s is not on the tape\n", name);
379 return (ino);
383 * Elicit a reply.
386 reply(char *question)
388 int c;
390 do {
391 fprintf(stderr, "%s? [yn] ", question);
392 (void) fflush(stderr);
393 c = getc(terminal);
394 while (c != '\n' && getc(terminal) != '\n')
395 if (c == EOF)
396 return (FAIL);
397 } while (c != 'y' && c != 'n');
398 if (c == 'y')
399 return (GOOD);
400 return (FAIL);
404 * handle unexpected inconsistencies
406 #include <stdarg.h>
408 void
409 panic(const char *fmt, ...)
411 va_list ap;
412 va_start(ap, fmt);
413 vfprintf(stderr, fmt, ap);
414 if (yflag)
415 return;
416 if (reply("abort") == GOOD) {
417 if (reply("dump core") == GOOD)
418 abort();
419 done(1);