]> git.wh0rd.org - dump.git/blob - restore/utilities.c
QFA support.
[dump.git] / restore / utilities.c
1 /*
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
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
43 static const char rcsid[] =
44 "$Id: utilities.c,v 1.14 2001/04/10 12:46:53 stelian Exp $";
45 #endif /* not lint */
46
47 #include <config.h>
48 #include <errno.h>
49 #include <compaterr.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include <sys/param.h>
55 #include <sys/stat.h>
56
57 #ifdef __linux__
58 #include <sys/time.h>
59 #include <linux/ext2_fs.h>
60 #include <ext2fs/ext2fs.h>
61 #include <bsdcompat.h>
62 #else /* __linux__ */
63 #include <ufs/ufs/dinode.h>
64 #include <ufs/ufs/dir.h>
65 #endif /* __linux__ */
66
67 #include "restore.h"
68 #include "extern.h"
69
70 /*
71 * Insure that all the components of a pathname exist.
72 */
73 void
74 pathcheck(char *name)
75 {
76 register char *cp;
77 struct entry *ep;
78 char *start;
79
80 start = strchr(name, '/');
81 if (start == 0)
82 return;
83 for (cp = start; *cp != '\0'; cp++) {
84 if (*cp != '/')
85 continue;
86 *cp = '\0';
87 ep = lookupname(name);
88 if (ep == NULL) {
89 /* Safe; we know the pathname exists in the dump. */
90 ep = addentry(name, pathsearch(name)->d_ino, NODE);
91 newnode(ep);
92 }
93 ep->e_flags |= NEW|KEEP;
94 *cp = '/';
95 }
96 }
97
98 /*
99 * Change a name to a unique temporary name.
100 */
101 void
102 mktempname(struct entry *ep)
103 {
104 char oldname[MAXPATHLEN];
105
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));
114 }
115
116 /*
117 * Generate a temporary name for an entry.
118 */
119 char *
120 gentempname(struct entry *ep)
121 {
122 static char name[MAXPATHLEN];
123 struct entry *np;
124 long i = 0;
125
126 for (np = lookupino(ep->e_ino);
127 np != NULL && np != ep; np = np->e_links)
128 i++;
129 if (np == NULL)
130 badentry(ep, "not on ino list");
131 (void) snprintf(name, sizeof(name), "%s%ld%lu", TMPHDR, i, (unsigned long)ep->e_ino);
132 return (name);
133 }
134
135 /*
136 * Rename a file or directory.
137 */
138 void
139 renameit(char *from, char *to)
140 {
141 if (!Nflag && rename(from, to) < 0) {
142 warn("cannot rename %s to %s", from, to);
143 return;
144 }
145 Vprintf(stdout, "rename %s to %s\n", from, to);
146 }
147
148 /*
149 * Create a new node (directory).
150 */
151 void
152 newnode(struct entry *np)
153 {
154 char *cp;
155 if (np->e_type != NODE)
156 badentry(np, "newnode: not a node");
157 cp = myname(np);
158 if (command == 'C') return;
159
160 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
161 np->e_flags |= EXISTED;
162 warn("%s", cp);
163 return;
164 }
165 Vprintf(stdout, "Make node %s\n", cp);
166 }
167
168 /*
169 * Remove an old node (directory).
170 */
171 void
172 removenode(struct entry *ep)
173 {
174 char *cp;
175
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;
182 cp = myname(ep);
183 if (!Nflag && rmdir(cp) < 0) {
184 warn("%s", cp);
185 return;
186 }
187 Vprintf(stdout, "Remove node %s\n", cp);
188 }
189
190 /*
191 * Remove a leaf.
192 */
193 void
194 removeleaf(struct entry *ep)
195 {
196 char *cp;
197
198 if (command == 'C') return;
199
200 if (ep->e_type != LEAF)
201 badentry(ep, "removeleaf: not a leaf");
202 ep->e_flags |= REMOVED;
203 ep->e_flags &= ~TMPNAME;
204 cp = myname(ep);
205 if (!Nflag && unlink(cp) < 0) {
206 warn("%s", cp);
207 return;
208 }
209 Vprintf(stdout, "Remove leaf %s\n", cp);
210 }
211
212 /*
213 * Create a link.
214 */
215 int
216 linkit(char *existing, char *new, int type)
217 {
218
219 /* if we want to unlink first, do it now so *link() won't fail */
220 if (uflag && !Nflag)
221 (void)unlink(new);
222
223 if (type == SYMLINK) {
224 if (!Nflag && symlink(existing, new) < 0) {
225 warn("cannot create symbolic link %s->%s",
226 new, existing);
227 return (FAIL);
228 }
229 } else if (type == HARDLINK) {
230 int ret;
231
232 if (!Nflag && (ret = link(existing, new)) < 0) {
233
234 #ifndef __linux__
235 struct stat s;
236
237 /*
238 * Most likely, the schg flag is set. Clear the
239 * flags and try again.
240 */
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);
245 }
246 #else
247 unsigned long s;
248
249 /*
250 * Most likely, the immutable or append-only attribute
251 * is set. Clear the attributes and try again.
252 */
253 if (fgetflags (existing, &s) != -1 &&
254 fsetflags (existing, 0) != -1) {
255 ret = link(existing, new);
256 fsetflags(existing, s);
257 }
258 #endif
259 if (ret < 0) {
260 warn("warning: cannot create hard link %s->%s",
261 new, existing);
262 return (FAIL);
263 }
264 }
265 } else {
266 panic("linkit: unknown type %d\n", type);
267 return (FAIL);
268 }
269 Vprintf(stdout, "Create %s link %s->%s\n",
270 type == SYMLINK ? "symbolic" : "hard", new, existing);
271 return (GOOD);
272 }
273
274 #ifndef __linux__
275 /*
276 * Create a whiteout.
277 */
278 int
279 addwhiteout(char *name)
280 {
281
282 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
283 warn("cannot create whiteout %s", name);
284 return (FAIL);
285 }
286 Vprintf(stdout, "Create whiteout %s\n", name);
287 return (GOOD);
288 }
289
290 /*
291 * Delete a whiteout.
292 */
293 void
294 delwhiteout(struct entry *ep)
295 {
296 char *name;
297
298 if (ep->e_type != LEAF)
299 badentry(ep, "delwhiteout: not a leaf");
300 ep->e_flags |= REMOVED;
301 ep->e_flags &= ~TMPNAME;
302 name = myname(ep);
303 if (!Nflag && undelete(name) < 0) {
304 warn("cannot delete whiteout %s", name);
305 return;
306 }
307 Vprintf(stdout, "Delete whiteout %s\n", name);
308 }
309 #endif
310
311 /*
312 * find lowest number file (above "start") that needs to be extracted
313 */
314 dump_ino_t
315 lowerbnd(dump_ino_t start)
316 {
317 register struct entry *ep;
318
319 for ( ; start < maxino; start++) {
320 ep = lookupino(start);
321 if (ep == NULL || ep->e_type == NODE)
322 continue;
323 if (ep->e_flags & (NEW|EXTRACT))
324 return (start);
325 }
326 return (start);
327 }
328
329 /*
330 * find highest number file (below "start") that needs to be extracted
331 */
332 dump_ino_t
333 upperbnd(dump_ino_t start)
334 {
335 register struct entry *ep;
336
337 for ( ; start > ROOTINO; start--) {
338 ep = lookupino(start);
339 if (ep == NULL || ep->e_type == NODE)
340 continue;
341 if (ep->e_flags & (NEW|EXTRACT))
342 return (start);
343 }
344 return (start);
345 }
346
347 /*
348 * report on a badly formed entry
349 */
350 void
351 badentry(struct entry *ep, const char *msg)
352 {
353
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)
364 fprintf(stderr,
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));
370 }
371
372 /*
373 * Construct a string indicating the active flag bits of an entry.
374 */
375 char *
376 flagvalues(struct entry *ep)
377 {
378 static char flagbuf[BUFSIZ];
379
380 (void) strcpy(flagbuf, "|NIL");
381 flagbuf[0] = '\0';
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]);
395 }
396
397 /*
398 * Check to see if a name is on a dump tape.
399 */
400 dump_ino_t
401 dirlookup(const char *name)
402 {
403 struct direct *dp;
404 dump_ino_t ino;
405
406 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
407
408 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
409 fprintf(stderr, "%s is not on the tape\n", name);
410 return (ino);
411 }
412
413 /*
414 * Elicit a reply.
415 */
416 int
417 reply(const char *question)
418 {
419 char c;
420
421 do {
422 fprintf(stderr, "%s? [yn] ", question);
423 (void) fflush(stderr);
424 c = getc(terminal);
425 while (c != '\n' && getc(terminal) != '\n')
426 if (feof(terminal))
427 return (FAIL);
428 } while (c != 'y' && c != 'n');
429 if (c == 'y')
430 return (GOOD);
431 return (FAIL);
432 }
433
434 /*
435 * handle unexpected inconsistencies
436 */
437 #ifdef __STDC__
438 #include <stdarg.h>
439 #else
440 #include <varargs.h>
441 #endif
442
443 void
444 #ifdef __STDC__
445 panic(const char *fmt, ...)
446 #else
447 panic(fmt, va_alist)
448 char *fmt;
449 va_dcl
450 #endif
451 {
452 va_list ap;
453 #ifdef __STDC__
454 va_start(ap, fmt);
455 #else
456 va_start(ap);
457 #endif
458
459 vfprintf(stderr, fmt, ap);
460 if (yflag)
461 return;
462 if (reply("abort") == GOOD) {
463 if (reply("dump core") == GOOD)
464 abort();
465 exit(1);
466 }
467 }
468
469 #ifdef USE_QFA
470 /*
471 * search for ino in QFA file
472 *
473 * if exactmatch:
474 * if ino found return tape number and tape position
475 * if ino not found return tnum=0 and tpos=0
476 *
477 * if not exactmatch:
478 * if ino found return tape number and tape position
479 * if ino not found return tape number and tape position of last smaller ino
480 * if no smaller inode found return tnum=0 and tpos=0
481 */
482 int
483 Inode2Tapepos(dump_ino_t ino, long *tnum, long *tpos, int exactmatch)
484 {
485 char *p, *pp;
486 char numbuff[32];
487 unsigned long tmpino;
488 long tmptnum;
489 long tmptpos;
490
491 *tpos = 0;
492 *tnum = 0;
493 if (fseek(gTapeposfp, gSeekstart, SEEK_SET) == -1)
494 return errno;
495 while (fgets(gTps, sizeof(gTps), gTapeposfp) != NULL) {
496 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
497 p = gTps;
498 bzero(numbuff, sizeof(numbuff));
499 pp = numbuff;
500 /* read inode */
501 while ((*p != 0) && (*p != '\t'))
502 *pp++ = *p++;
503 tmpino = atol(numbuff);
504 if (*p == 0)
505 return 1; /* may NOT happen */
506 p++;
507 bzero(numbuff, sizeof(numbuff));
508 pp = numbuff;
509 /* read tapenum */
510 while ((*p != 0) && (*p != '\t'))
511 *pp++ = *p++;
512 if (*p == 0)
513 return 1; /* may NOT happen */
514 tmptnum = atol(numbuff);
515 p++;
516 bzero(numbuff, sizeof(numbuff));
517 pp = numbuff;
518 /* read tapepos */
519 while ((*p != 0) && (*p != '\t'))
520 *pp++ = *p++;
521 tmptpos = atol(numbuff);
522
523 if (exactmatch) {
524 if (tmpino == ino) {
525 *tnum = tmptnum;
526 *tpos = tmptpos;
527 return 0;
528 }
529 } else {
530 if (tmpino > ino) {
531 return 0;
532 } else {
533 *tnum = tmptnum;
534 *tpos = tmptpos;
535 }
536 }
537 }
538 return 0;
539 }
540 #endif /* USE_QFA */