2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static char sccsid[] = "@(#)utilities.c 8.5 (Berkeley) 4/28/95";
46 static const char rcsid[] =
47 "$Id: utilities.c,v 1.2 1999/10/11 12:53:25 stelian Exp $";
50 #include <sys/param.h>
55 #include <linux/ext2_fs.h>
56 #include <bsdcompat.h>
58 #include <ufs/ufs/dinode.h>
59 #include <ufs/ufs/dir.h>
60 #endif /* __linux__ */
68 #include <ext2fs/ext2fs.h>
75 * Insure that all the components of a pathname exist.
85 start = strchr(name, '/');
88 for (cp = start; *cp != '\0'; cp++) {
92 ep = lookupname(name);
94 /* Safe; we know the pathname exists in the dump. */
95 ep = addentry(name, pathsearch(name)->d_ino, NODE);
98 ep->e_flags |= NEW|KEEP;
104 * Change a name to a unique temporary name.
108 register struct entry *ep;
110 char oldname[MAXPATHLEN];
112 if (ep->e_flags & TMPNAME)
113 badentry(ep, "mktempname: called with TMPNAME");
114 ep->e_flags |= TMPNAME;
115 (void) strcpy(oldname, myname(ep));
116 freename(ep->e_name);
117 ep->e_name = savename(gentempname(ep));
118 ep->e_namlen = strlen(ep->e_name);
119 renameit(oldname, myname(ep));
123 * Generate a temporary name for an entry.
129 static char name[MAXPATHLEN];
133 for (np = lookupino(ep->e_ino);
134 np != NULL && np != ep; np = np->e_links)
137 badentry(ep, "not on ino list");
138 (void) sprintf(name, "%s%ld%lu", TMPHDR, i, (u_long)ep->e_ino);
143 * Rename a file or directory.
149 if (!Nflag && rename(from, to) < 0) {
150 fprintf(stderr, "warning: cannot rename %s to %s: %s\n",
151 from, to, strerror(errno));
154 vprintf(stdout, "rename %s to %s\n", from, to);
158 * Create a new node (directory).
166 if (np->e_type != NODE)
167 badentry(np, "newnode: not a node");
169 if (command == 'C') return;
171 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
172 np->e_flags |= EXISTED;
173 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
176 vprintf(stdout, "Make node %s\n", cp);
180 * Remove an old node (directory).
184 register struct entry *ep;
188 if (ep->e_type != NODE)
189 badentry(ep, "removenode: not a node");
190 if (ep->e_entries != NULL)
191 badentry(ep, "removenode: non-empty directory");
192 ep->e_flags |= REMOVED;
193 ep->e_flags &= ~TMPNAME;
195 if (!Nflag && rmdir(cp) < 0) {
196 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
199 vprintf(stdout, "Remove node %s\n", cp);
207 register struct entry *ep;
211 if (command == 'C') return;
213 if (ep->e_type != LEAF)
214 badentry(ep, "removeleaf: not a leaf");
215 ep->e_flags |= REMOVED;
216 ep->e_flags &= ~TMPNAME;
218 if (!Nflag && unlink(cp) < 0) {
219 fprintf(stderr, "warning: %s: %s\n", cp, strerror(errno));
222 vprintf(stdout, "Remove leaf %s\n", cp);
229 linkit(existing, new, type)
230 char *existing, *new;
234 /* if we want to unlink first, do it now so *link() won't fail */
238 if (type == SYMLINK) {
239 if (!Nflag && symlink(existing, new) < 0) {
241 "warning: cannot create symbolic link %s->%s: %s\n",
242 new, existing, strerror(errno));
245 } else if (type == HARDLINK) {
248 if (!Nflag && (ret = link(existing, new)) < 0) {
254 * Most likely, the schg flag is set. Clear the
255 * flags and try again.
257 if (stat(existing, &s) == 0 && s.st_flags != 0 &&
258 chflags(existing, 0) == 0) {
259 ret = link(existing, new);
260 chflags(existing, s.st_flags);
264 fprintf(stderr, "warning: cannot create "
265 "hard link %s->%s: %s\n",
266 new, existing, strerror(errno));
271 panic("linkit: unknown type %d\n", type);
274 vprintf(stdout, "Create %s link %s->%s\n",
275 type == SYMLINK ? "symbolic" : "hard", new, existing);
288 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
289 fprintf(stderr, "warning: cannot create whiteout %s: %s\n",
290 name, strerror(errno));
293 vprintf(stdout, "Create whiteout %s\n", name);
302 register struct entry *ep;
306 if (ep->e_type != LEAF)
307 badentry(ep, "delwhiteout: not a leaf");
308 ep->e_flags |= REMOVED;
309 ep->e_flags &= ~TMPNAME;
311 if (!Nflag && undelete(name) < 0) {
312 fprintf(stderr, "warning: cannot delete whiteout %s: %s\n",
313 name, strerror(errno));
316 vprintf(stdout, "Delete whiteout %s\n", name);
321 * find lowest number file (above "start") that needs to be extracted
327 register struct entry *ep;
329 for ( ; start < maxino; start++) {
330 ep = lookupino(start);
331 if (ep == NULL || ep->e_type == NODE)
333 if (ep->e_flags & (NEW|EXTRACT))
340 * find highest number file (below "start") that needs to be extracted
346 register struct entry *ep;
348 for ( ; start > ROOTINO; start--) {
349 ep = lookupino(start);
350 if (ep == NULL || ep->e_type == NODE)
352 if (ep->e_flags & (NEW|EXTRACT))
359 * report on a badly formed entry
363 register struct entry *ep;
367 fprintf(stderr, "bad entry: %s\n", msg);
368 fprintf(stderr, "name: %s\n", myname(ep));
369 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
370 if (ep->e_sibling != NULL)
371 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
372 if (ep->e_entries != NULL)
373 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
374 if (ep->e_links != NULL)
375 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
376 if (ep->e_next != NULL)
378 "next hashchain name: %s\n", myname(ep->e_next));
379 fprintf(stderr, "entry type: %s\n",
380 ep->e_type == NODE ? "NODE" : "LEAF");
381 fprintf(stderr, "inode number: %lu\n", (u_long)ep->e_ino);
382 panic("flags: %s\n", flagvalues(ep));
386 * Construct a string indicating the active flag bits of an entry.
390 register struct entry *ep;
392 static char flagbuf[BUFSIZ];
394 (void) strcpy(flagbuf, "|NIL");
396 if (ep->e_flags & REMOVED)
397 (void) strcat(flagbuf, "|REMOVED");
398 if (ep->e_flags & TMPNAME)
399 (void) strcat(flagbuf, "|TMPNAME");
400 if (ep->e_flags & EXTRACT)
401 (void) strcat(flagbuf, "|EXTRACT");
402 if (ep->e_flags & NEW)
403 (void) strcat(flagbuf, "|NEW");
404 if (ep->e_flags & KEEP)
405 (void) strcat(flagbuf, "|KEEP");
406 if (ep->e_flags & EXISTED)
407 (void) strcat(flagbuf, "|EXISTED");
408 return (&flagbuf[1]);
412 * Check to see if a name is on a dump tape.
421 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
423 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
424 fprintf(stderr, "%s is not on the tape\n", name);
438 fprintf(stderr, "%s? [yn] ", question);
439 (void) fflush(stderr);
441 while (c != '\n' && getc(terminal) != '\n')
444 } while (c != 'y' && c != 'n');
451 * handle unexpected inconsistencies
461 panic(const char *fmt, ...)
475 vfprintf(stderr, fmt, ap);
478 if (reply("abort") == GOOD) {
479 if (reply("dump core") == GOOD)