Constify iicbus_write()'s buf argument.
[dragonfly.git] / sbin / restore / utilities.c
blob5d3047e2a79083c5b6082eec30940418fd00a797
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)utilities.c 8.5 (Berkeley) 4/28/95
34 * $FreeBSD: src/sbin/restore/utilities.c,v 1.8.2.2 2001/07/30 10:30:08 dd Exp $
35 * $DragonFly: src/sbin/restore/utilities.c,v 1.7 2005/11/06 12:49:25 swildner Exp $
38 #include <sys/param.h>
39 #include <sys/stat.h>
41 #include <vfs/ufs/dinode.h>
42 #include <vfs/ufs/dir.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
50 #include "restore.h"
51 #include "extern.h"
54 * Insure that all the components of a pathname exist.
56 void
57 pathcheck(char *name)
59 char *cp;
60 struct entry *ep;
61 char *start;
63 start = strchr(name, '/');
64 if (start == 0)
65 return;
66 for (cp = start; *cp != '\0'; cp++) {
67 if (*cp != '/')
68 continue;
69 *cp = '\0';
70 ep = lookupname(name);
71 if (ep == NULL) {
72 /* Safe; we know the pathname exists in the dump. */
73 ep = addentry(name, pathsearch(name)->d_ino, NODE);
74 newnode(ep);
76 ep->e_flags |= NEW|KEEP;
77 *cp = '/';
82 * Change a name to a unique temporary name.
84 void
85 mktempname(struct entry *ep)
87 char oldname[MAXPATHLEN];
89 if (ep->e_flags & TMPNAME)
90 badentry(ep, "mktempname: called with TMPNAME");
91 ep->e_flags |= TMPNAME;
92 strcpy(oldname, myname(ep));
93 freename(ep->e_name);
94 ep->e_name = savename(gentempname(ep));
95 ep->e_namlen = strlen(ep->e_name);
96 renameit(oldname, myname(ep));
100 * Generate a temporary name for an entry.
102 char *
103 gentempname(struct entry *ep)
105 static char name[MAXPATHLEN];
106 struct entry *np;
107 long i = 0;
109 for (np = lookupino(ep->e_ino);
110 np != NULL && np != ep; np = np->e_links)
111 i++;
112 if (np == NULL)
113 badentry(ep, "not on ino list");
114 sprintf(name, "%s%ld%lu", TMPHDR, i, (u_long)ep->e_ino);
115 return (name);
119 * Rename a file or directory.
121 void
122 renameit(char *from, char *to)
124 if (!Nflag && rename(from, to) < 0) {
125 fprintf(stderr, "warning: cannot rename %s to %s: %s\n",
126 from, to, strerror(errno));
127 return;
129 vprintf(stdout, "rename %s to %s\n", from, to);
133 * Create a new node (directory).
135 void
136 newnode(struct entry *np)
138 char *cp;
140 if (np->e_type != NODE)
141 badentry(np, "newnode: not a node");
142 cp = myname(np);
143 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
144 np->e_flags |= EXISTED;
145 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
146 return;
148 vprintf(stdout, "Make node %s\n", cp);
152 * Remove an old node (directory).
154 void
155 removenode(struct entry *ep)
157 char *cp;
159 if (ep->e_type != NODE)
160 badentry(ep, "removenode: not a node");
161 if (ep->e_entries != NULL)
162 badentry(ep, "removenode: non-empty directory");
163 ep->e_flags |= REMOVED;
164 ep->e_flags &= ~TMPNAME;
165 cp = myname(ep);
166 if (!Nflag && rmdir(cp) < 0) {
167 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
168 return;
170 vprintf(stdout, "Remove node %s\n", cp);
174 * Remove a leaf.
176 void
177 removeleaf(struct entry *ep)
179 char *cp;
181 if (ep->e_type != LEAF)
182 badentry(ep, "removeleaf: not a leaf");
183 ep->e_flags |= REMOVED;
184 ep->e_flags &= ~TMPNAME;
185 cp = myname(ep);
186 if (!Nflag && unlink(cp) < 0) {
187 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
188 return;
190 vprintf(stdout, "Remove leaf %s\n", cp);
194 * Create a link.
197 linkit(char *existing, char *new, int type)
200 /* if we want to unlink first, do it now so *link() won't fail */
201 if (uflag && !Nflag)
202 unlink(new);
204 if (type == SYMLINK) {
205 if (!Nflag && symlink(existing, new) < 0) {
206 fprintf(stderr,
207 "warning: cannot create symbolic link %s->%s: %s\n",
208 new, existing, strerror(errno));
209 return (FAIL);
211 } else if (type == HARDLINK) {
212 int ret;
214 if (!Nflag && (ret = link(existing, new)) < 0) {
215 struct stat s;
218 * Most likely, the schg flag is set. Clear the
219 * flags and try again.
221 if (stat(existing, &s) == 0 && s.st_flags != 0 &&
222 chflags(existing, 0) == 0) {
223 ret = link(existing, new);
224 chflags(existing, s.st_flags);
226 if (ret < 0) {
227 fprintf(stderr, "warning: cannot create "
228 "hard link %s->%s: %s\n",
229 new, existing, strerror(errno));
230 return (FAIL);
233 } else {
234 panic("linkit: unknown type %d\n", type);
235 return (FAIL);
237 vprintf(stdout, "Create %s link %s->%s\n",
238 type == SYMLINK ? "symbolic" : "hard", new, existing);
239 return (GOOD);
243 * Create a whiteout.
246 addwhiteout(char *name)
249 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
250 fprintf(stderr, "warning: cannot create whiteout %s: %s\n",
251 name, strerror(errno));
252 return (FAIL);
254 vprintf(stdout, "Create whiteout %s\n", name);
255 return (GOOD);
259 * Delete a whiteout.
261 void
262 delwhiteout(struct entry *ep)
264 char *name;
266 if (ep->e_type != LEAF)
267 badentry(ep, "delwhiteout: not a leaf");
268 ep->e_flags |= REMOVED;
269 ep->e_flags &= ~TMPNAME;
270 name = myname(ep);
271 if (!Nflag && undelete(name) < 0) {
272 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
273 name, strerror(errno));
274 return;
276 vprintf(stdout, "Delete whiteout %s\n", name);
280 * find lowest number file (above "start") that needs to be extracted
282 ufs1_ino_t
283 lowerbnd(ufs1_ino_t start)
285 struct entry *ep;
287 for ( ; start < maxino; start++) {
288 ep = lookupino(start);
289 if (ep == NULL || ep->e_type == NODE)
290 continue;
291 if (ep->e_flags & (NEW|EXTRACT))
292 return (start);
294 return (start);
298 * find highest number file (below "start") that needs to be extracted
300 ufs1_ino_t
301 upperbnd(ufs1_ino_t start)
303 struct entry *ep;
305 for ( ; start > ROOTINO; start--) {
306 ep = lookupino(start);
307 if (ep == NULL || ep->e_type == NODE)
308 continue;
309 if (ep->e_flags & (NEW|EXTRACT))
310 return (start);
312 return (start);
316 * report on a badly formed entry
318 void
319 badentry(struct entry *ep, char *msg)
322 fprintf(stderr, "bad entry: %s\n", msg);
323 fprintf(stderr, "name: %s\n", myname(ep));
324 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
325 if (ep->e_sibling != NULL)
326 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
327 if (ep->e_entries != NULL)
328 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
329 if (ep->e_links != NULL)
330 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
331 if (ep->e_next != NULL)
332 fprintf(stderr,
333 "next hashchain name: %s\n", myname(ep->e_next));
334 fprintf(stderr, "entry type: %s\n",
335 ep->e_type == NODE ? "NODE" : "LEAF");
336 fprintf(stderr, "inode number: %lu\n", (u_long)ep->e_ino);
337 panic("flags: %s\n", flagvalues(ep));
341 * Construct a string indicating the active flag bits of an entry.
343 char *
344 flagvalues(struct entry *ep)
346 static char flagbuf[BUFSIZ];
348 strcpy(flagbuf, "|NIL");
349 flagbuf[0] = '\0';
350 if (ep->e_flags & REMOVED)
351 strcat(flagbuf, "|REMOVED");
352 if (ep->e_flags & TMPNAME)
353 strcat(flagbuf, "|TMPNAME");
354 if (ep->e_flags & EXTRACT)
355 strcat(flagbuf, "|EXTRACT");
356 if (ep->e_flags & NEW)
357 strcat(flagbuf, "|NEW");
358 if (ep->e_flags & KEEP)
359 strcat(flagbuf, "|KEEP");
360 if (ep->e_flags & EXISTED)
361 strcat(flagbuf, "|EXISTED");
362 return (&flagbuf[1]);
366 * Check to see if a name is on a dump tape.
368 ufs1_ino_t
369 dirlookup(const char *name)
371 struct direct *dp;
372 ufs1_ino_t ino;
374 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
376 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
377 fprintf(stderr, "%s is not on the tape\n", name);
378 return (ino);
382 * Elicit a reply.
385 reply(char *question)
387 int c;
389 do {
390 fprintf(stderr, "%s? [yn] ", question);
391 fflush(stderr);
392 c = getc(terminal);
393 while (c != '\n' && getc(terminal) != '\n')
394 if (c == EOF)
395 return (FAIL);
396 } while (c != 'y' && c != 'n');
397 if (c == 'y')
398 return (GOOD);
399 return (FAIL);
403 * handle unexpected inconsistencies
405 #include <stdarg.h>
407 void
408 panic(const char *fmt, ...)
410 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);