]> git.wh0rd.org - dump.git/blame - restore/utilities.c
Version 0.4b7.
[dump.git] / restore / utilities.c
CommitLineData
1227625a
SP
1/*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
b45f51d6
SP
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
1227625a
SP
6 *
7 */
8
9/*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
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.
28 *
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
39 * SUCH DAMAGE.
40 */
41
42#ifndef lint
b45f51d6 43#if 0
1227625a 44static char sccsid[] = "@(#)utilities.c 8.5 (Berkeley) 4/28/95";
b45f51d6
SP
45#endif
46static const char rcsid[] =
8d4197bb 47 "$Id: utilities.c,v 1.4 1999/10/11 13:08:10 stelian Exp $";
1227625a
SP
48#endif /* not lint */
49
50#include <sys/param.h>
51#include <sys/stat.h>
52
53#ifdef __linux__
54#include <sys/time.h>
55#include <linux/ext2_fs.h>
56#include <bsdcompat.h>
57#else /* __linux__ */
58#include <ufs/ufs/dinode.h>
59#include <ufs/ufs/dir.h>
60#endif /* __linux__ */
61
62#include <errno.h>
ddd2ef55 63#include <compaterr.h>
1227625a 64#include <stdio.h>
1227625a
SP
65#include <string.h>
66#include <unistd.h>
67
68#ifdef __linux__
69#include <ext2fs/ext2fs.h>
70#endif
71
72#include "restore.h"
73#include "extern.h"
74
75/*
76 * Insure that all the components of a pathname exist.
77 */
78void
ddd2ef55 79pathcheck(char *name)
1227625a
SP
80{
81 register char *cp;
82 struct entry *ep;
83 char *start;
84
b45f51d6 85 start = strchr(name, '/');
1227625a
SP
86 if (start == 0)
87 return;
88 for (cp = start; *cp != '\0'; cp++) {
89 if (*cp != '/')
90 continue;
91 *cp = '\0';
92 ep = lookupname(name);
93 if (ep == NULL) {
94 /* Safe; we know the pathname exists in the dump. */
95 ep = addentry(name, pathsearch(name)->d_ino, NODE);
96 newnode(ep);
97 }
98 ep->e_flags |= NEW|KEEP;
99 *cp = '/';
100 }
101}
102
103/*
104 * Change a name to a unique temporary name.
105 */
106void
ddd2ef55 107mktempname(struct entry *ep)
1227625a
SP
108{
109 char oldname[MAXPATHLEN];
110
111 if (ep->e_flags & TMPNAME)
112 badentry(ep, "mktempname: called with TMPNAME");
113 ep->e_flags |= TMPNAME;
114 (void) strcpy(oldname, myname(ep));
115 freename(ep->e_name);
116 ep->e_name = savename(gentempname(ep));
117 ep->e_namlen = strlen(ep->e_name);
118 renameit(oldname, myname(ep));
119}
120
121/*
122 * Generate a temporary name for an entry.
123 */
124char *
ddd2ef55 125gentempname(struct entry *ep)
1227625a
SP
126{
127 static char name[MAXPATHLEN];
128 struct entry *np;
129 long i = 0;
130
131 for (np = lookupino(ep->e_ino);
132 np != NULL && np != ep; np = np->e_links)
133 i++;
134 if (np == NULL)
135 badentry(ep, "not on ino list");
ddd2ef55 136 (void) snprintf(name, sizeof(name), "%s%ld%lu", TMPHDR, i, (unsigned long)ep->e_ino);
1227625a
SP
137 return (name);
138}
139
140/*
141 * Rename a file or directory.
142 */
143void
ddd2ef55 144renameit(char *from, char *to)
1227625a
SP
145{
146 if (!Nflag && rename(from, to) < 0) {
ddd2ef55 147 warn("cannot rename %s to %s", from, to);
1227625a
SP
148 return;
149 }
ddd2ef55 150 Vprintf(stdout, "rename %s to %s\n", from, to);
1227625a
SP
151}
152
153/*
154 * Create a new node (directory).
155 */
156void
ddd2ef55 157newnode(struct entry *np)
1227625a
SP
158{
159 char *cp;
1227625a
SP
160 if (np->e_type != NODE)
161 badentry(np, "newnode: not a node");
162 cp = myname(np);
163 if (command == 'C') return;
b45f51d6
SP
164
165 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
1227625a 166 np->e_flags |= EXISTED;
ddd2ef55 167 warn("%s", cp);
1227625a
SP
168 return;
169 }
ddd2ef55 170 Vprintf(stdout, "Make node %s\n", cp);
1227625a
SP
171}
172
173/*
174 * Remove an old node (directory).
175 */
176void
ddd2ef55 177removenode(struct entry *ep)
1227625a
SP
178{
179 char *cp;
180
181 if (ep->e_type != NODE)
182 badentry(ep, "removenode: not a node");
183 if (ep->e_entries != NULL)
184 badentry(ep, "removenode: non-empty directory");
185 ep->e_flags |= REMOVED;
186 ep->e_flags &= ~TMPNAME;
187 cp = myname(ep);
188 if (!Nflag && rmdir(cp) < 0) {
ddd2ef55 189 warn("%s", cp);
1227625a
SP
190 return;
191 }
ddd2ef55 192 Vprintf(stdout, "Remove node %s\n", cp);
1227625a
SP
193}
194
195/*
196 * Remove a leaf.
197 */
198void
ddd2ef55 199removeleaf(struct entry *ep)
1227625a
SP
200{
201 char *cp;
202
203 if (command == 'C') return;
204
205 if (ep->e_type != LEAF)
206 badentry(ep, "removeleaf: not a leaf");
207 ep->e_flags |= REMOVED;
208 ep->e_flags &= ~TMPNAME;
209 cp = myname(ep);
210 if (!Nflag && unlink(cp) < 0) {
ddd2ef55 211 warn("%s", cp);
1227625a
SP
212 return;
213 }
ddd2ef55 214 Vprintf(stdout, "Remove leaf %s\n", cp);
1227625a
SP
215}
216
217/*
218 * Create a link.
219 */
220int
ddd2ef55 221linkit(char *existing, char *new, int type)
1227625a
SP
222{
223
b45f51d6
SP
224 /* if we want to unlink first, do it now so *link() won't fail */
225 if (uflag && !Nflag)
226 (void)unlink(new);
227
1227625a
SP
228 if (type == SYMLINK) {
229 if (!Nflag && symlink(existing, new) < 0) {
ddd2ef55
SP
230 warn("cannot create symbolic link %s->%s",
231 new, existing);
1227625a
SP
232 return (FAIL);
233 }
234 } else if (type == HARDLINK) {
b45f51d6
SP
235 int ret;
236
237 if (!Nflag && (ret = link(existing, new)) < 0) {
238
239#ifndef __linux__
240 struct stat s;
241
242 /*
243 * Most likely, the schg flag is set. Clear the
244 * flags and try again.
245 */
246 if (stat(existing, &s) == 0 && s.st_flags != 0 &&
247 chflags(existing, 0) == 0) {
248 ret = link(existing, new);
249 chflags(existing, s.st_flags);
250 }
251#endif
252 if (ret < 0) {
ddd2ef55
SP
253 warn("warning: cannot create hard link %s->%s",
254 new, existing);
b45f51d6
SP
255 return (FAIL);
256 }
1227625a
SP
257 }
258 } else {
259 panic("linkit: unknown type %d\n", type);
260 return (FAIL);
261 }
ddd2ef55 262 Vprintf(stdout, "Create %s link %s->%s\n",
1227625a
SP
263 type == SYMLINK ? "symbolic" : "hard", new, existing);
264 return (GOOD);
265}
266
267#ifndef __linux__
268/*
b45f51d6 269 * Create a whiteout.
1227625a
SP
270 */
271int
ddd2ef55 272addwhiteout(char *name)
1227625a
SP
273{
274
275 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
ddd2ef55 276 warn("cannot create whiteout %s", name);
1227625a
SP
277 return (FAIL);
278 }
ddd2ef55 279 Vprintf(stdout, "Create whiteout %s\n", name);
1227625a
SP
280 return (GOOD);
281}
282
283/*
284 * Delete a whiteout.
285 */
286void
ddd2ef55 287delwhiteout(struct entry *ep)
1227625a
SP
288{
289 char *name;
290
291 if (ep->e_type != LEAF)
292 badentry(ep, "delwhiteout: not a leaf");
293 ep->e_flags |= REMOVED;
294 ep->e_flags &= ~TMPNAME;
295 name = myname(ep);
296 if (!Nflag && undelete(name) < 0) {
ddd2ef55 297 warn("cannot delete whiteout %s", name);
1227625a
SP
298 return;
299 }
ddd2ef55 300 Vprintf(stdout, "Delete whiteout %s\n", name);
1227625a
SP
301}
302#endif
303
304/*
305 * find lowest number file (above "start") that needs to be extracted
306 */
307ino_t
ddd2ef55 308lowerbnd(ino_t start)
1227625a
SP
309{
310 register struct entry *ep;
311
312 for ( ; start < maxino; start++) {
313 ep = lookupino(start);
314 if (ep == NULL || ep->e_type == NODE)
315 continue;
316 if (ep->e_flags & (NEW|EXTRACT))
317 return (start);
318 }
319 return (start);
320}
321
322/*
323 * find highest number file (below "start") that needs to be extracted
324 */
325ino_t
ddd2ef55 326upperbnd(ino_t start)
1227625a
SP
327{
328 register struct entry *ep;
329
330 for ( ; start > ROOTINO; start--) {
331 ep = lookupino(start);
332 if (ep == NULL || ep->e_type == NODE)
333 continue;
334 if (ep->e_flags & (NEW|EXTRACT))
335 return (start);
336 }
337 return (start);
338}
339
340/*
341 * report on a badly formed entry
342 */
343void
ddd2ef55 344badentry(struct entry *ep, const char *msg)
1227625a
SP
345{
346
347 fprintf(stderr, "bad entry: %s\n", msg);
348 fprintf(stderr, "name: %s\n", myname(ep));
349 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
350 if (ep->e_sibling != NULL)
351 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
352 if (ep->e_entries != NULL)
353 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
354 if (ep->e_links != NULL)
355 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
356 if (ep->e_next != NULL)
357 fprintf(stderr,
358 "next hashchain name: %s\n", myname(ep->e_next));
359 fprintf(stderr, "entry type: %s\n",
360 ep->e_type == NODE ? "NODE" : "LEAF");
ddd2ef55 361 fprintf(stderr, "inode number: %lu\n", (unsigned long)ep->e_ino);
1227625a
SP
362 panic("flags: %s\n", flagvalues(ep));
363}
364
365/*
366 * Construct a string indicating the active flag bits of an entry.
367 */
368char *
ddd2ef55 369flagvalues(struct entry *ep)
1227625a
SP
370{
371 static char flagbuf[BUFSIZ];
372
373 (void) strcpy(flagbuf, "|NIL");
374 flagbuf[0] = '\0';
375 if (ep->e_flags & REMOVED)
376 (void) strcat(flagbuf, "|REMOVED");
377 if (ep->e_flags & TMPNAME)
378 (void) strcat(flagbuf, "|TMPNAME");
379 if (ep->e_flags & EXTRACT)
380 (void) strcat(flagbuf, "|EXTRACT");
381 if (ep->e_flags & NEW)
382 (void) strcat(flagbuf, "|NEW");
383 if (ep->e_flags & KEEP)
384 (void) strcat(flagbuf, "|KEEP");
385 if (ep->e_flags & EXISTED)
386 (void) strcat(flagbuf, "|EXISTED");
387 return (&flagbuf[1]);
388}
389
390/*
391 * Check to see if a name is on a dump tape.
392 */
393ino_t
ddd2ef55 394dirlookup(const char *name)
1227625a
SP
395{
396 struct direct *dp;
397 ino_t ino;
b45f51d6 398
1227625a
SP
399 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
400
401 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
402 fprintf(stderr, "%s is not on the tape\n", name);
403 return (ino);
404}
405
406/*
407 * Elicit a reply.
408 */
409int
ddd2ef55 410reply(const char *question)
1227625a
SP
411{
412 char c;
413
414 do {
415 fprintf(stderr, "%s? [yn] ", question);
416 (void) fflush(stderr);
417 c = getc(terminal);
418 while (c != '\n' && getc(terminal) != '\n')
419 if (feof(terminal))
420 return (FAIL);
421 } while (c != 'y' && c != 'n');
422 if (c == 'y')
423 return (GOOD);
424 return (FAIL);
425}
426
427/*
428 * handle unexpected inconsistencies
429 */
ddd2ef55 430#ifdef __STDC__
1227625a
SP
431#include <stdarg.h>
432#else
433#include <varargs.h>
434#endif
435
436void
ddd2ef55 437#ifdef __STDC__
1227625a
SP
438panic(const char *fmt, ...)
439#else
440panic(fmt, va_alist)
441 char *fmt;
442 va_dcl
443#endif
444{
445 va_list ap;
ddd2ef55 446#ifdef __STDC__
1227625a
SP
447 va_start(ap, fmt);
448#else
449 va_start(ap);
450#endif
451
452 vfprintf(stderr, fmt, ap);
453 if (yflag)
454 return;
455 if (reply("abort") == GOOD) {
456 if (reply("dump core") == GOOD)
457 abort();
ddd2ef55 458 exit(1);
1227625a
SP
459 }
460}