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
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
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.2 2003/06/17 04:27:34 dillon Exp $
38 #include <sys/param.h>
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ufs/dir.h>
54 * Insure that all the components of a pathname exist.
64 start = strchr(name, '/');
67 for (cp = start; *cp != '\0'; cp++) {
71 ep = lookupname(name);
73 /* Safe; we know the pathname exists in the dump. */
74 ep = addentry(name, pathsearch(name)->d_ino, NODE);
77 ep->e_flags |= NEW|KEEP;
83 * Change a name to a unique temporary name.
87 register struct entry *ep;
89 char oldname[MAXPATHLEN];
91 if (ep->e_flags & TMPNAME)
92 badentry(ep, "mktempname: called with TMPNAME");
93 ep->e_flags |= TMPNAME;
94 (void) strcpy(oldname, myname(ep));
96 ep->e_name = savename(gentempname(ep));
97 ep->e_namlen = strlen(ep->e_name);
98 renameit(oldname, myname(ep));
102 * Generate a temporary name for an entry.
108 static char name[MAXPATHLEN];
112 for (np = lookupino(ep->e_ino);
113 np != NULL && np != ep; np = np->e_links)
116 badentry(ep, "not on ino list");
117 (void) sprintf(name, "%s%ld%lu", TMPHDR, i, (u_long)ep->e_ino);
122 * Rename a file or directory.
128 if (!Nflag && rename(from, to) < 0) {
129 fprintf(stderr, "warning: cannot rename %s to %s: %s\n",
130 from, to, strerror(errno));
133 vprintf(stdout, "rename %s to %s\n", from, to);
137 * Create a new node (directory).
145 if (np->e_type != NODE)
146 badentry(np, "newnode: not a node");
148 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
149 np->e_flags |= EXISTED;
150 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
153 vprintf(stdout, "Make node %s\n", cp);
157 * Remove an old node (directory).
161 register struct entry *ep;
165 if (ep->e_type != NODE)
166 badentry(ep, "removenode: not a node");
167 if (ep->e_entries != NULL)
168 badentry(ep, "removenode: non-empty directory");
169 ep->e_flags |= REMOVED;
170 ep->e_flags &= ~TMPNAME;
172 if (!Nflag && rmdir(cp) < 0) {
173 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
176 vprintf(stdout, "Remove node %s\n", cp);
184 register struct entry *ep;
188 if (ep->e_type != LEAF)
189 badentry(ep, "removeleaf: not a leaf");
190 ep->e_flags |= REMOVED;
191 ep->e_flags &= ~TMPNAME;
193 if (!Nflag && unlink(cp) < 0) {
194 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
197 vprintf(stdout, "Remove leaf %s\n", cp);
204 linkit(existing, new, type)
205 char *existing, *new;
209 /* if we want to unlink first, do it now so *link() won't fail */
213 if (type == SYMLINK) {
214 if (!Nflag && symlink(existing, new) < 0) {
216 "warning: cannot create symbolic link %s->%s: %s\n",
217 new, existing, strerror(errno));
220 } else if (type == HARDLINK) {
223 if (!Nflag && (ret = link(existing, new)) < 0) {
227 * Most likely, the schg flag is set. Clear the
228 * flags and try again.
230 if (stat(existing, &s) == 0 && s.st_flags != 0 &&
231 chflags(existing, 0) == 0) {
232 ret = link(existing, new);
233 chflags(existing, s.st_flags);
236 fprintf(stderr, "warning: cannot create "
237 "hard link %s->%s: %s\n",
238 new, existing, strerror(errno));
243 panic("linkit: unknown type %d\n", type);
246 vprintf(stdout, "Create %s link %s->%s\n",
247 type == SYMLINK ? "symbolic" : "hard", new, existing);
259 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
260 fprintf(stderr, "warning: cannot create whiteout %s: %s\n",
261 name, strerror(errno));
264 vprintf(stdout, "Create whiteout %s\n", name);
273 register struct entry *ep;
277 if (ep->e_type != LEAF)
278 badentry(ep, "delwhiteout: not a leaf");
279 ep->e_flags |= REMOVED;
280 ep->e_flags &= ~TMPNAME;
282 if (!Nflag && undelete(name) < 0) {
283 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
284 name, strerror(errno));
287 vprintf(stdout, "Delete whiteout %s\n", name);
291 * find lowest number file (above "start") that needs to be extracted
297 register struct entry *ep;
299 for ( ; start < maxino; start++) {
300 ep = lookupino(start);
301 if (ep == NULL || ep->e_type == NODE)
303 if (ep->e_flags & (NEW|EXTRACT))
310 * find highest number file (below "start") that needs to be extracted
316 register struct entry *ep;
318 for ( ; start > ROOTINO; start--) {
319 ep = lookupino(start);
320 if (ep == NULL || ep->e_type == NODE)
322 if (ep->e_flags & (NEW|EXTRACT))
329 * report on a badly formed entry
333 register struct entry *ep;
337 fprintf(stderr, "bad entry: %s\n", msg);
338 fprintf(stderr, "name: %s\n", myname(ep));
339 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
340 if (ep->e_sibling != NULL)
341 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
342 if (ep->e_entries != NULL)
343 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
344 if (ep->e_links != NULL)
345 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
346 if (ep->e_next != NULL)
348 "next hashchain name: %s\n", myname(ep->e_next));
349 fprintf(stderr, "entry type: %s\n",
350 ep->e_type == NODE ? "NODE" : "LEAF");
351 fprintf(stderr, "inode number: %lu\n", (u_long)ep->e_ino);
352 panic("flags: %s\n", flagvalues(ep));
356 * Construct a string indicating the active flag bits of an entry.
360 register struct entry *ep;
362 static char flagbuf[BUFSIZ];
364 (void) strcpy(flagbuf, "|NIL");
366 if (ep->e_flags & REMOVED)
367 (void) strcat(flagbuf, "|REMOVED");
368 if (ep->e_flags & TMPNAME)
369 (void) strcat(flagbuf, "|TMPNAME");
370 if (ep->e_flags & EXTRACT)
371 (void) strcat(flagbuf, "|EXTRACT");
372 if (ep->e_flags & NEW)
373 (void) strcat(flagbuf, "|NEW");
374 if (ep->e_flags & KEEP)
375 (void) strcat(flagbuf, "|KEEP");
376 if (ep->e_flags & EXISTED)
377 (void) strcat(flagbuf, "|EXISTED");
378 return (&flagbuf[1]);
382 * Check to see if a name is on a dump tape.
391 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
393 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
394 fprintf(stderr, "%s is not on the tape\n", name);
408 fprintf(stderr, "%s? [yn] ", question);
409 (void) fflush(stderr);
411 while (c != '\n' && getc(terminal) != '\n')
414 } while (c != 'y' && c != 'n');
421 * handle unexpected inconsistencies
431 panic(const char *fmt, ...)
445 vfprintf(stderr, fmt, ap);
448 if (reply("abort") == GOOD) {
449 if (reply("dump core") == GOOD)