]> git.wh0rd.org - dump.git/blob - restore/utilities.c
From Uwe Gohlke:
[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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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.20 2002/02/04 11:18:46 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 <time.h>
60 #ifdef HAVE_EXT2FS_EXT2_FS_H
61 #include <ext2fs/ext2_fs.h>
62 #else
63 #include <linux/ext2_fs.h>
64 #endif
65 #include <ext2fs/ext2fs.h>
66 #include <bsdcompat.h>
67 #else /* __linux__ */
68 #include <ufs/ufs/dinode.h>
69 #include <ufs/ufs/dir.h>
70 #endif /* __linux__ */
71
72 #include "restore.h"
73 #include "extern.h"
74
75 /*
76 * Insure that all the components of a pathname exist.
77 */
78 void
79 pathcheck(char *name)
80 {
81 char *cp;
82 struct entry *ep;
83 char *start;
84
85 start = strchr(name, '/');
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 */
106 void
107 mktempname(struct entry *ep)
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 */
124 char *
125 gentempname(struct entry *ep)
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");
136 (void) snprintf(name, sizeof(name), "%s%ld%lu", TMPHDR, i, (unsigned long)ep->e_ino);
137 return (name);
138 }
139
140 /*
141 * Rename a file or directory.
142 */
143 void
144 renameit(char *from, char *to)
145 {
146 if (!Nflag && rename(from, to) < 0) {
147 warn("cannot rename %s to %s", from, to);
148 return;
149 }
150 Vprintf(stdout, "rename %s to %s\n", from, to);
151 }
152
153 /*
154 * Create a new node (directory).
155 */
156 void
157 newnode(struct entry *np)
158 {
159 char *cp;
160 if (np->e_type != NODE)
161 badentry(np, "newnode: not a node");
162 cp = myname(np);
163 if (command == 'C') return;
164
165 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
166 np->e_flags |= EXISTED;
167 warn("%s", cp);
168 return;
169 }
170 Vprintf(stdout, "Make node %s\n", cp);
171 }
172
173 /*
174 * Remove an old node (directory).
175 */
176 void
177 removenode(struct entry *ep)
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) {
189 warn("%s", cp);
190 return;
191 }
192 Vprintf(stdout, "Remove node %s\n", cp);
193 }
194
195 /*
196 * Remove a leaf.
197 */
198 void
199 removeleaf(struct entry *ep)
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) {
211 warn("%s", cp);
212 return;
213 }
214 Vprintf(stdout, "Remove leaf %s\n", cp);
215 }
216
217 /*
218 * Create a link.
219 */
220 int
221 linkit(char *existing, char *new, int type)
222 {
223
224 /* if we want to unlink first, do it now so *link() won't fail */
225 if (uflag && !Nflag)
226 (void)unlink(new);
227
228 if (type == SYMLINK) {
229 if (!Nflag && symlink(existing, new) < 0) {
230 warn("cannot create symbolic link %s->%s",
231 new, existing);
232 return (FAIL);
233 }
234 } else if (type == HARDLINK) {
235 int ret;
236
237 if (!Nflag && (ret = link(existing, new)) < 0) {
238
239 #if !defined(__linux__) && !defined(sunos)
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 #else
252 unsigned long s;
253
254 /*
255 * Most likely, the immutable or append-only attribute
256 * is set. Clear the attributes and try again.
257 */
258 if (fgetflags (existing, &s) != -1 &&
259 fsetflags (existing, 0) != -1) {
260 ret = link(existing, new);
261 fsetflags(existing, s);
262 }
263 #endif
264 if (ret < 0) {
265 warn("warning: cannot create hard link %s->%s",
266 new, existing);
267 return (FAIL);
268 }
269 }
270 } else {
271 panic("linkit: unknown type %d\n", type);
272 return (FAIL);
273 }
274 Vprintf(stdout, "Create %s link %s->%s\n",
275 type == SYMLINK ? "symbolic" : "hard", new, existing);
276 return (GOOD);
277 }
278
279 #if !defined(__linux__) && !defined(sunos)
280 /*
281 * Create a whiteout.
282 */
283 int
284 addwhiteout(char *name)
285 {
286
287 if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
288 warn("cannot create whiteout %s", name);
289 return (FAIL);
290 }
291 Vprintf(stdout, "Create whiteout %s\n", name);
292 return (GOOD);
293 }
294
295 /*
296 * Delete a whiteout.
297 */
298 void
299 delwhiteout(struct entry *ep)
300 {
301 char *name;
302
303 if (ep->e_type != LEAF)
304 badentry(ep, "delwhiteout: not a leaf");
305 ep->e_flags |= REMOVED;
306 ep->e_flags &= ~TMPNAME;
307 name = myname(ep);
308 if (!Nflag && undelete(name) < 0) {
309 warn("cannot delete whiteout %s", name);
310 return;
311 }
312 Vprintf(stdout, "Delete whiteout %s\n", name);
313 }
314 #endif
315
316 /*
317 * find lowest number file (above "start") that needs to be extracted
318 */
319 dump_ino_t
320 lowerbnd(dump_ino_t start)
321 {
322 struct entry *ep;
323
324 for ( ; start < maxino; start++) {
325 ep = lookupino(start);
326 if (ep == NULL || ep->e_type == NODE)
327 continue;
328 if (ep->e_flags & (NEW|EXTRACT))
329 return (start);
330 }
331 return (start);
332 }
333
334 /*
335 * find highest number file (below "start") that needs to be extracted
336 */
337 dump_ino_t
338 upperbnd(dump_ino_t start)
339 {
340 struct entry *ep;
341
342 for ( ; start > ROOTINO; start--) {
343 ep = lookupino(start);
344 if (ep == NULL || ep->e_type == NODE)
345 continue;
346 if (ep->e_flags & (NEW|EXTRACT))
347 return (start);
348 }
349 return (start);
350 }
351
352 /*
353 * report on a badly formed entry
354 */
355 void
356 badentry(struct entry *ep, const char *msg)
357 {
358
359 fprintf(stderr, "bad entry: %s\n", msg);
360 fprintf(stderr, "name: %s\n", myname(ep));
361 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
362 if (ep->e_sibling != NULL)
363 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
364 if (ep->e_entries != NULL)
365 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
366 if (ep->e_links != NULL)
367 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
368 if (ep->e_next != NULL)
369 fprintf(stderr,
370 "next hashchain name: %s\n", myname(ep->e_next));
371 fprintf(stderr, "entry type: %s\n",
372 ep->e_type == NODE ? "NODE" : "LEAF");
373 fprintf(stderr, "inode number: %lu\n", (unsigned long)ep->e_ino);
374 panic("flags: %s\n", flagvalues(ep));
375 }
376
377 /*
378 * Construct a string indicating the active flag bits of an entry.
379 */
380 char *
381 flagvalues(struct entry *ep)
382 {
383 static char flagbuf[BUFSIZ];
384
385 (void) strcpy(flagbuf, "|NIL");
386 flagbuf[0] = '\0';
387 if (ep->e_flags & REMOVED)
388 (void) strcat(flagbuf, "|REMOVED");
389 if (ep->e_flags & TMPNAME)
390 (void) strcat(flagbuf, "|TMPNAME");
391 if (ep->e_flags & EXTRACT)
392 (void) strcat(flagbuf, "|EXTRACT");
393 if (ep->e_flags & NEW)
394 (void) strcat(flagbuf, "|NEW");
395 if (ep->e_flags & KEEP)
396 (void) strcat(flagbuf, "|KEEP");
397 if (ep->e_flags & EXISTED)
398 (void) strcat(flagbuf, "|EXISTED");
399 return (&flagbuf[1]);
400 }
401
402 /*
403 * Check to see if a name is on a dump tape.
404 */
405 dump_ino_t
406 dirlookup(const char *name)
407 {
408 struct direct *dp;
409 dump_ino_t ino;
410
411 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
412
413 if (ino == 0 || TSTINO(ino, dumpmap) == 0)
414 fprintf(stderr, "%s is not on the tape\n", name);
415 return (ino);
416 }
417
418 /*
419 * Elicit a reply.
420 */
421 int
422 reply(const char *question)
423 {
424 char c;
425
426 do {
427 fprintf(stderr, "%s? [yn] ", question);
428 (void) fflush(stderr);
429 c = getc(terminal);
430 while (c != '\n' && getc(terminal) != '\n')
431 if (feof(terminal))
432 return (FAIL);
433 } while (c != 'y' && c != 'n');
434 if (c == 'y')
435 return (GOOD);
436 return (FAIL);
437 }
438
439 /*
440 * handle unexpected inconsistencies
441 */
442 #ifdef __STDC__
443 #include <stdarg.h>
444 #else
445 #include <varargs.h>
446 #endif
447
448 void
449 #ifdef __STDC__
450 panic(const char *fmt, ...)
451 #else
452 panic(fmt, va_alist)
453 char *fmt;
454 va_dcl
455 #endif
456 {
457 va_list ap;
458 #ifdef __STDC__
459 va_start(ap, fmt);
460 #else
461 va_start(ap);
462 #endif
463
464 vfprintf(stderr, fmt, ap);
465 if (yflag)
466 return;
467 if (reply("abort") == GOOD) {
468 if (reply("dump core") == GOOD)
469 abort();
470 exit(1);
471 }
472 }
473
474 #ifdef USE_QFA
475 /*
476 * search for ino in QFA file
477 *
478 * if exactmatch:
479 * if ino found return tape number and tape position
480 * if ino not found return tnum=0 and tpos=0
481 *
482 * if not exactmatch:
483 * if ino found return tape number and tape position
484 * if ino not found return tape number and tape position of last smaller ino
485 * if no smaller inode found return tnum=0 and tpos=0
486 */
487 int
488 Inode2Tapepos(dump_ino_t ino, long *tnum, long long *tpos, int exactmatch)
489 {
490 char *p, *pp;
491 char numbuff[32];
492 unsigned long tmpino;
493 long tmptnum;
494 long long tmptpos;
495
496 *tpos = 0;
497 *tnum = 0;
498 if (fseek(gTapeposfp, gSeekstart, SEEK_SET) == -1)
499 return errno;
500 while (fgets(gTps, sizeof(gTps), gTapeposfp) != NULL) {
501 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
502 p = gTps;
503 bzero(numbuff, sizeof(numbuff));
504 pp = numbuff;
505 /* read inode */
506 while ((*p != 0) && (*p != '\t'))
507 *pp++ = *p++;
508 tmpino = atol(numbuff);
509 if (*p == 0)
510 return 1; /* may NOT happen */
511 p++;
512 bzero(numbuff, sizeof(numbuff));
513 pp = numbuff;
514 /* read tapenum */
515 while ((*p != 0) && (*p != '\t'))
516 *pp++ = *p++;
517 if (*p == 0)
518 return 1; /* may NOT happen */
519 tmptnum = atol(numbuff);
520 p++;
521 bzero(numbuff, sizeof(numbuff));
522 pp = numbuff;
523 /* read tapepos */
524 while ((*p != 0) && (*p != '\t'))
525 *pp++ = *p++;
526 tmptpos = atoll(numbuff);
527
528 if (exactmatch) {
529 if (tmpino == ino) {
530 *tnum = tmptnum;
531 *tpos = tmptpos;
532 return 0;
533 }
534 } else {
535 if (tmpino > ino) {
536 return 0;
537 } else {
538 *tnum = tmptnum;
539 *tpos = tmptpos;
540 }
541 }
542 }
543 return 0;
544 }
545 #endif /* USE_QFA */