]>
git.wh0rd.org - dump.git/blob - restore/utilities.c
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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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
43 static const char rcsid
[] =
44 "$Id: utilities.c,v 1.13 2001/03/20 10:02:48 stelian Exp $";
49 #include <compaterr.h>
54 #include <sys/param.h>
59 #include <linux/ext2_fs.h>
60 #include <ext2fs/ext2fs.h>
61 #include <bsdcompat.h>
63 #include <ufs/ufs/dinode.h>
64 #include <ufs/ufs/dir.h>
65 #endif /* __linux__ */
71 * Insure that all the components of a pathname exist.
80 start
= strchr(name
, '/');
83 for (cp
= start
; *cp
!= '\0'; cp
++) {
87 ep
= lookupname(name
);
89 /* Safe; we know the pathname exists in the dump. */
90 ep
= addentry(name
, pathsearch(name
)->d_ino
, NODE
);
93 ep
->e_flags
|= NEW
|KEEP
;
99 * Change a name to a unique temporary name.
102 mktempname(struct entry
*ep
)
104 char oldname
[MAXPATHLEN
];
106 if (ep
->e_flags
& TMPNAME
)
107 badentry(ep
, "mktempname: called with TMPNAME");
108 ep
->e_flags
|= TMPNAME
;
109 (void) strcpy(oldname
, myname(ep
));
110 freename(ep
->e_name
);
111 ep
->e_name
= savename(gentempname(ep
));
112 ep
->e_namlen
= strlen(ep
->e_name
);
113 renameit(oldname
, myname(ep
));
117 * Generate a temporary name for an entry.
120 gentempname(struct entry
*ep
)
122 static char name
[MAXPATHLEN
];
126 for (np
= lookupino(ep
->e_ino
);
127 np
!= NULL
&& np
!= ep
; np
= np
->e_links
)
130 badentry(ep
, "not on ino list");
131 (void) snprintf(name
, sizeof(name
), "%s%ld%lu", TMPHDR
, i
, (unsigned long)ep
->e_ino
);
136 * Rename a file or directory.
139 renameit(char *from
, char *to
)
141 if (!Nflag
&& rename(from
, to
) < 0) {
142 warn("cannot rename %s to %s", from
, to
);
145 Vprintf(stdout
, "rename %s to %s\n", from
, to
);
149 * Create a new node (directory).
152 newnode(struct entry
*np
)
155 if (np
->e_type
!= NODE
)
156 badentry(np
, "newnode: not a node");
158 if (command
== 'C') return;
160 if (!Nflag
&& mkdir(cp
, 0777) < 0 && !uflag
) {
161 np
->e_flags
|= EXISTED
;
165 Vprintf(stdout
, "Make node %s\n", cp
);
169 * Remove an old node (directory).
172 removenode(struct entry
*ep
)
176 if (ep
->e_type
!= NODE
)
177 badentry(ep
, "removenode: not a node");
178 if (ep
->e_entries
!= NULL
)
179 badentry(ep
, "removenode: non-empty directory");
180 ep
->e_flags
|= REMOVED
;
181 ep
->e_flags
&= ~TMPNAME
;
183 if (!Nflag
&& rmdir(cp
) < 0) {
187 Vprintf(stdout
, "Remove node %s\n", cp
);
194 removeleaf(struct entry
*ep
)
198 if (command
== 'C') return;
200 if (ep
->e_type
!= LEAF
)
201 badentry(ep
, "removeleaf: not a leaf");
202 ep
->e_flags
|= REMOVED
;
203 ep
->e_flags
&= ~TMPNAME
;
205 if (!Nflag
&& unlink(cp
) < 0) {
209 Vprintf(stdout
, "Remove leaf %s\n", cp
);
216 linkit(char *existing
, char *new, int type
)
219 /* if we want to unlink first, do it now so *link() won't fail */
223 if (type
== SYMLINK
) {
224 if (!Nflag
&& symlink(existing
, new) < 0) {
225 warn("cannot create symbolic link %s->%s",
229 } else if (type
== HARDLINK
) {
232 if (!Nflag
&& (ret
= link(existing
, new)) < 0) {
238 * Most likely, the schg flag is set. Clear the
239 * flags and try again.
241 if (stat(existing
, &s
) == 0 && s
.st_flags
!= 0 &&
242 chflags(existing
, 0) == 0) {
243 ret
= link(existing
, new);
244 chflags(existing
, s
.st_flags
);
250 * Most likely, the immutable or append-only attribute
251 * is set. Clear the attributes and try again.
253 if (fgetflags (existing
, &s
) != -1 &&
254 fsetflags (existing
, 0) != -1) {
255 ret
= link(existing
, new);
256 fsetflags(existing
, s
);
260 warn("warning: cannot create hard link %s->%s",
266 panic("linkit: unknown type %d\n", type
);
269 Vprintf(stdout
, "Create %s link %s->%s\n",
270 type
== SYMLINK
? "symbolic" : "hard", new, existing
);
279 addwhiteout(char *name
)
282 if (!Nflag
&& mknod(name
, S_IFWHT
, 0) < 0) {
283 warn("cannot create whiteout %s", name
);
286 Vprintf(stdout
, "Create whiteout %s\n", name
);
294 delwhiteout(struct entry
*ep
)
298 if (ep
->e_type
!= LEAF
)
299 badentry(ep
, "delwhiteout: not a leaf");
300 ep
->e_flags
|= REMOVED
;
301 ep
->e_flags
&= ~TMPNAME
;
303 if (!Nflag
&& undelete(name
) < 0) {
304 warn("cannot delete whiteout %s", name
);
307 Vprintf(stdout
, "Delete whiteout %s\n", name
);
312 * find lowest number file (above "start") that needs to be extracted
315 lowerbnd(dump_ino_t start
)
317 register struct entry
*ep
;
319 for ( ; start
< maxino
; start
++) {
320 ep
= lookupino(start
);
321 if (ep
== NULL
|| ep
->e_type
== NODE
)
323 if (ep
->e_flags
& (NEW
|EXTRACT
))
330 * find highest number file (below "start") that needs to be extracted
333 upperbnd(dump_ino_t start
)
335 register struct entry
*ep
;
337 for ( ; start
> ROOTINO
; start
--) {
338 ep
= lookupino(start
);
339 if (ep
== NULL
|| ep
->e_type
== NODE
)
341 if (ep
->e_flags
& (NEW
|EXTRACT
))
348 * report on a badly formed entry
351 badentry(struct entry
*ep
, const char *msg
)
354 fprintf(stderr
, "bad entry: %s\n", msg
);
355 fprintf(stderr
, "name: %s\n", myname(ep
));
356 fprintf(stderr
, "parent name %s\n", myname(ep
->e_parent
));
357 if (ep
->e_sibling
!= NULL
)
358 fprintf(stderr
, "sibling name: %s\n", myname(ep
->e_sibling
));
359 if (ep
->e_entries
!= NULL
)
360 fprintf(stderr
, "next entry name: %s\n", myname(ep
->e_entries
));
361 if (ep
->e_links
!= NULL
)
362 fprintf(stderr
, "next link name: %s\n", myname(ep
->e_links
));
363 if (ep
->e_next
!= NULL
)
365 "next hashchain name: %s\n", myname(ep
->e_next
));
366 fprintf(stderr
, "entry type: %s\n",
367 ep
->e_type
== NODE
? "NODE" : "LEAF");
368 fprintf(stderr
, "inode number: %lu\n", (unsigned long)ep
->e_ino
);
369 panic("flags: %s\n", flagvalues(ep
));
373 * Construct a string indicating the active flag bits of an entry.
376 flagvalues(struct entry
*ep
)
378 static char flagbuf
[BUFSIZ
];
380 (void) strcpy(flagbuf
, "|NIL");
382 if (ep
->e_flags
& REMOVED
)
383 (void) strcat(flagbuf
, "|REMOVED");
384 if (ep
->e_flags
& TMPNAME
)
385 (void) strcat(flagbuf
, "|TMPNAME");
386 if (ep
->e_flags
& EXTRACT
)
387 (void) strcat(flagbuf
, "|EXTRACT");
388 if (ep
->e_flags
& NEW
)
389 (void) strcat(flagbuf
, "|NEW");
390 if (ep
->e_flags
& KEEP
)
391 (void) strcat(flagbuf
, "|KEEP");
392 if (ep
->e_flags
& EXISTED
)
393 (void) strcat(flagbuf
, "|EXISTED");
394 return (&flagbuf
[1]);
398 * Check to see if a name is on a dump tape.
401 dirlookup(const char *name
)
406 ino
= ((dp
= pathsearch(name
)) == NULL
) ? 0 : dp
->d_ino
;
408 if (ino
== 0 || TSTINO(ino
, dumpmap
) == 0)
409 fprintf(stderr
, "%s is not on the tape\n", name
);
417 reply(const char *question
)
422 fprintf(stderr
, "%s? [yn] ", question
);
423 (void) fflush(stderr
);
425 while (c
!= '\n' && getc(terminal
) != '\n')
428 } while (c
!= 'y' && c
!= 'n');
435 * handle unexpected inconsistencies
445 panic(const char *fmt
, ...)
459 vfprintf(stderr
, fmt
, ap
);
462 if (reply("abort") == GOOD
) {
463 if (reply("dump core") == GOOD
)