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.5 2004/12/18 21:43:40 swildner Exp $
38 #include <sys/param.h>
41 #include <vfs/ufs/dinode.h>
42 #include <vfs/ufs/dir.h>
54 * Insure that all the components of a pathname exist.
63 start = strchr(name, '/');
66 for (cp = start; *cp != '\0'; cp++) {
70 ep = lookupname(name);
72 /* Safe; we know the pathname exists in the dump. */
73 ep = addentry(name, pathsearch(name)->d_ino, NODE);
76 ep->e_flags |= NEW|KEEP;
82 * Change a name to a unique temporary name.
85 mktempname(register 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));
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.
103 gentempname(struct entry *ep)
105 static char name[MAXPATHLEN];
109 for (np = lookupino(ep->e_ino);
110 np != NULL && np != ep; np = np->e_links)
113 badentry(ep, "not on ino list");
114 sprintf(name, "%s%ld%lu", TMPHDR, i, (u_long)ep->e_ino);
119 * Rename a file or directory.
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));
129 vprintf(stdout, "rename %s to %s\n", from, to);
133 * Create a new node (directory).
136 newnode(struct entry *np)
140 if (np->e_type != NODE)
141 badentry(np, "newnode: not a node");
143 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
144 np->e_flags |= EXISTED;
145 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
148 vprintf(stdout, "Make node %s\n", cp);
152 * Remove an old node (directory).
155 removenode(register struct entry *ep)
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;
166 if (!Nflag && rmdir(cp) < 0) {
167 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
170 vprintf(stdout, "Remove node %s\n", cp);
177 removeleaf(register struct entry *ep)
181 if (ep->e_type != LEAF)
182 badentry(ep, "removeleaf: not a leaf");
183 ep->e_flags |= REMOVED;
184 ep->e_flags &= ~TMPNAME;
186 if (!Nflag && unlink(cp) < 0) {
187 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
190 vprintf(stdout, "Remove leaf %s\n", cp);
197 linkit(char *existing, char *new, int type)
200 /* if we want to unlink first, do it now so *link() won't fail */
204 if (type == SYMLINK) {
205 if (!Nflag && symlink(existing, new) < 0) {
207 "warning: cannot create symbolic link %s->%s: %s\n",
208 new, existing, strerror(errno));
211 } else if (type == HARDLINK) {
214 if (!Nflag && (ret = link(existing, new)) < 0) {
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);
227 fprintf(stderr, "warning: cannot create "
228 "hard link %s->%s: %s\n",
229 new, existing, strerror(errno));
234 panic("linkit: unknown type %d\n", type);
237 vprintf(stdout, "Create %s link %s->%s\n",
238 type == SYMLINK ? "symbolic" : "hard", new, existing);
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));
254 vprintf(stdout, "Create whiteout %s\n", name);
262 delwhiteout(register struct entry *ep)
266 if (ep->e_type != LEAF)
267 badentry(ep, "delwhiteout: not a leaf");
268 ep->e_flags |= REMOVED;
269 ep->e_flags &= ~TMPNAME;
271 if (!Nflag && undelete(name) < 0) {
272 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
273 name, strerror(errno));
276 vprintf(stdout, "Delete whiteout %s\n", name);
280 * find lowest number file (above "start") that needs to be extracted
283 lowerbnd(ino_t start)
285 register struct entry *ep;
287 for ( ; start < maxino; start++) {
288 ep = lookupino(start);
289 if (ep == NULL || ep->e_type == NODE)
291 if (ep->e_flags & (NEW|EXTRACT))
298 * find highest number file (below "start") that needs to be extracted
301 upperbnd(ino_t start)
303 register struct entry *ep;
305 for ( ; start > ROOTINO; start--) {
306 ep = lookupino(start);
307 if (ep == NULL || ep->e_type == NODE)
309 if (ep->e_flags & (NEW|EXTRACT))
316 * report on a badly formed entry
319 badentry(register 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)
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.
344 flagvalues(register struct entry *ep)
346 static char flagbuf[BUFSIZ];
348 strcpy(flagbuf, "|NIL");
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.
369 dirlookup(const char *name)
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);
385 reply(char *question)
390 fprintf(stderr, "%s? [yn] ", question);
393 while (c != '\n' && getc(terminal) != '\n')
396 } while (c != 'y' && c != 'n');
403 * handle unexpected inconsistencies
408 panic(const char *fmt, ...)
413 vfprintf(stderr, fmt, ap);
416 if (reply("abort") == GOOD) {
417 if (reply("dump core") == GOOD)