Debian Codegraph @

restore/utilities.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <[email protected]>, 1994-1997
5 * Stelian Pop <[email protected]>, 1999-2000
6 * Stelian Pop <[email protected]> - 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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include "config.h" // IWYU pragma: keep
39 #include "utilities.h"
40
41 #include <errno.h>
42 #include "compat/include/compaterr.h" // IWYU pragma: keep
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <strings.h>
48 #include "compat/include/protocols/dumprestore.h"
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52
53 #include <fcntl.h>
54 #ifdef HAVE_EXT2FS_EXT2_FS_H
55 #include <ext2fs/ext2_fs.h>
56 #else
57 #include <linux/ext2_fs.h>
58 #endif
59 #include "compat/include/bsdcompat.h"
60 #ifdef DUMP_MACOSX
61 #include "compat/include/darwin.h"
62 #endif
63 #include "restore.h"
64 #include "symtab.h"
65 #include "dirs.h"
66 #include "restore_globals.h"
67
68 #define TMPHDR "RSTTMP"
69
70 /* From e2fsprogs */
71 int fsetflags (const char *, unsigned long);
72 int fgetflags (const char *, unsigned long *);
73
74 /*
75 * Insure that all the components of a pathname exist.
76 */
77 void
78 pathcheck(char *name)
79 {
80 char *cp;
81 struct entry *ep;
82 char *start;
83
84 start = strchr(name, '/');
85 if (start == 0)
86 return;
87 for (cp = start; *cp != '\0'; cp++) {
88 if (*cp != '/')
89 continue;
90 *cp = '\0';
91 ep = lookupname(name);
92 if (ep == NULL) {
93 /* Safe; we know the pathname exists in the dump. */
94 ep = addentry(name, pathsearch(name)->d_ino, NODE);
95 newnode(ep);
96 }
97 ep->e_flags |= NEW|KEEP;
98 *cp = '/';
99 }
100 }
101
102 /*
103 * Change a name to a unique temporary name.
104 */
105 void
106 mktempname(struct entry *ep)
107 {
108 char oldname[MAXPATHLEN];
109
110 if (ep->e_flags & TMPNAME)
111 badentry(ep, "mktempname: called with TMPNAME");
112 ep->e_flags |= TMPNAME;
113 (void) strcpy(oldname, myname(ep));
114 freename(ep->e_name);
115 ep->e_name = savename(gentempname(ep));
116 ep->e_namlen = strlen(ep->e_name);
117 renameit(oldname, myname(ep));
118 }
119
120 /*
121 * Generate a temporary name for an entry.
122 */
123 char *
124 gentempname(struct entry *ep)
125 {
126 static char name[MAXPATHLEN];
127 struct entry *np;
128 long i = 0;
129
130 for (np = lookupino(ep->e_ino);
131 np != NULL && np != ep; np = np->e_links)
132 i++;
133 if (np == NULL)
134 badentry(ep, "not on ino list");
135 (void) snprintf(name, sizeof(name), "%s%ld%lu", TMPHDR, i, (unsigned long)ep->e_ino);
136 return (name);
137 }
138
139 /*
140 * Rename a file or directory.
141 */
142 void
143 renameit(const char *from, const char *to)
144 {
145 if (!Nflag && rename(from, to) < 0) {
146 warn("cannot rename %s to %s", from, to);
147 return;
148 }
149 Vmsg("rename %s to %s\n", from, to);
150 }
151
152 /*
153 * Create a new node (directory).
154 */
155 void
156 newnode(struct entry *np)
157 {
158 char *cp;
159 if (np->e_type != NODE)
160 badentry(np, "newnode: not a node");
161 cp = myname(np);
162 if (command == 'C') return;
163
164 if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
165 np->e_flags |= EXISTED;
166 warn("%s", cp);
167 return;
168 }
169 Vmsg("Make node %s\n", cp);
170 }
171
172 /*
173 * Remove an old node (directory).
174 */
175 void
176 removenode(struct entry *ep)
177 {
178 char *cp;
179
180 if (ep->e_type != NODE)
181 badentry(ep, "removenode: not a node");
182 if (ep->e_entries != NULL) {
183 int i;
184 for (i = 0; i < dirhash_size; i++) {
185 if (ep->e_entries[i] != NULL)
186 badentry(ep, "removenode: non-empty directory");
187 }
188 }
189 ep->e_flags |= REMOVED;
190 ep->e_flags &= ~TMPNAME;
191 cp = myname(ep);
192 if (!Nflag && rmdir(cp) < 0) {
193 warn("%s", cp);
194 return;
195 }
196 Vmsg("Remove node %s\n", cp);
197 }
198
199 /*
200 * Remove a leaf.
201 */
202 void
203 removeleaf(struct entry *ep)
204 {
205 char *cp;
206
207 if (command == 'C') return;
208
209 if (ep->e_type != LEAF)
210 badentry(ep, "removeleaf: not a leaf");
211 ep->e_flags |= REMOVED;
212 ep->e_flags &= ~TMPNAME;
213 cp = myname(ep);
214 if (!Nflag && unlink(cp) < 0) {
215 warn("%s", cp);
216 return;
217 }
218 Vmsg("Remove leaf %s\n", cp);
219 }
220
221 /*
222 * Create a link.
223 */
224 int
225 linkit(char *existing, char *new, int type)
226 {
227
228 /* if we want to unlink first, do it now so *link() won't fail */
229 if (uflag && !Nflag)
230 (void)unlink(new);
231
232 if (type == SYMLINK) {
233 if (!Nflag && symlink(existing, new) < 0) {
234 warn("cannot create symbolic link %s->%s",
235 new, existing);
236 return (FAIL);
237 }
238 } else if (type == HARDLINK) {
239 int ret;
240
241 if (!Nflag && (ret = link(existing, new)) < 0) {
242
243 unsigned long s;
244
245 /*
246 * Most likely, the immutable or append-only attribute
247 * is set. Clear the attributes and try again.
248 */
249 if (lgetflags (existing, &s, 0) != -1 &&
250 lsetflags (existing, 0) != -1) {
251 ret = link(existing, new);
252 lsetflags(existing, s);
253 }
254 if (ret < 0) {
255 warn("warning: cannot create hard link %s->%s",
256 new, existing);
257 return (FAIL);
258 }
259 }
260 } else {
261 panic("linkit: unknown type %d\n", type);
262 return (FAIL);
263 }
264 Vmsg("Create %s link %s->%s\n",
265 type == SYMLINK ? "symbolic" : "hard", new, existing);
266 return (GOOD);
267 }
268
269 /*
270 * find lowest number file (above "start") that needs to be extracted
271 */
272 dump_ino_t
273 lowerbnd(dump_ino_t start)
274 {
275 struct entry *ep;
276
277 for ( ; start < maxino; start++) {
278 ep = lookupino(start);
279 if (ep == NULL || ep->e_type == NODE)
280 continue;
281 if (ep->e_flags & (NEW|EXTRACT))
282 return (start);
283 }
284 return (start);
285 }
286
287 /*
288 * find highest number file (below "start") that needs to be extracted
289 */
290 dump_ino_t
291 upperbnd(dump_ino_t start)
292 {
293 struct entry *ep;
294
295 for ( ; start > ROOTINO; start--) {
296 ep = lookupino(start);
297 if (ep == NULL || ep->e_type == NODE)
298 continue;
299 if (ep->e_flags & (NEW|EXTRACT))
300 return (start);
301 }
302 return (start);
303 }
304
305 /*
•306 * report and terminate on a badly formed entry
307 */
308 void
309 badentry(struct entry *ep, const char *msg)
310 {
311
312 fprintf(stderr, "bad entry: %s\n", msg);
313 fprintf(stderr, "name: %s\n", myname(ep));
314 fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
315 if (ep->e_sibling != NULL)
316 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
317 if (ep->e_entries != NULL) {
318 int i;
319 for (i = 0; i < dirhash_size; i++) {
320 if (ep->e_entries[i] != NULL) {
321 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries[i]));
322 break;
323 }
324 }
325 }
326 if (ep->e_links != NULL)
327 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
328 if (ep->e_next != NULL)
329 fprintf(stderr,
330 "next hashchain name: %s\n", myname(ep->e_next));
331 fprintf(stderr, "entry type: %s\n",
332 ep->e_type == NODE ? "NODE" : "LEAF");
333 fprintf(stderr, "inode number: %lu\n", (unsigned long)ep->e_ino);
•334 /* a corrupt symbol table is very unlikely to be recoverable */
335 yflag = 0;
336 panic("flags: %s\n", flagvalues(ep));
337 }
338
339 /*
340 * Construct a string indicating the active flag bits of an entry.
341 */
342 char *
343 flagvalues(struct entry *ep)
344 {
345 static char flagbuf[BUFSIZ];
346
347 (void) strcpy(flagbuf, "|NIL");
348 flagbuf[0] = '\0';
349 if (ep->e_flags & REMOVED)
350 (void) strcat(flagbuf, "|REMOVED");
351 if (ep->e_flags & TMPNAME)
352 (void) strcat(flagbuf, "|TMPNAME");
353 if (ep->e_flags & EXTRACT)
354 (void) strcat(flagbuf, "|EXTRACT");
355 if (ep->e_flags & NEW)
356 (void) strcat(flagbuf, "|NEW");
357 if (ep->e_flags & KEEP)
358 (void) strcat(flagbuf, "|KEEP");
359 if (ep->e_flags & EXISTED)
360 (void) strcat(flagbuf, "|EXISTED");
361 return (&flagbuf[1]);
362 }
363
364 /*
365 * Check to see if a name is on a dump tape.
366 */
367 dump_ino_t
368 dirlookup(const char *name)
369 {
370 struct direct *dp;
371 dump_ino_t ino;
372
373 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
374
375 if (ino == 0 || TSTINO(ino, dumpinomap) == 0)
376 fprintf(stderr, "%s is not on the tape\n", name);
377 return (ino);
378 }
379
380 /*
381 * Elicit a reply.
382 */
383 int
384 reply(const char *question, int yflag)
385 {
386 if (yflag)
387 return (GOOD);
388
389 char c;
390
391 do {
392 fprintf(stderr, "%s? [yn] ", question);
393 (void) fflush(stderr);
394 c = getc(terminal);
395 while (c != '\n' && getc(terminal) != '\n')
396 if (feof(terminal))
397 return (FAIL);
398 } while (c != 'y' && c != 'n');
399 if (c == 'y')
400 return (GOOD);
401 return (FAIL);
402 }
403
404 /*
405 * handle unexpected inconsistencies
406 */
407 #include <stdarg.h>
408
409 void
410 panic(const char *fmt, ...)
411 {
412 va_list ap;
413 va_start(ap, fmt);
414
415 vfprintf(stderr, fmt, ap);
416 if (qflag)
417 exit(1);
418 if (yflag)
419 return;
420 if (reply("abort", 0) == GOOD) {
421 if (reply("dump core", 0) == GOOD) {
422 if (fchdir(wdfd) < 0)
423 warn("fchdir");
424 abort();
425 }
426 exit(1);
427 }
428 }
429
430 void resizemaps(dump_ino_t oldmax, dump_ino_t newmax)
431 {
432 char *map;
433
434 if (usedinomap) {
435 map = calloc((unsigned)1, (unsigned)howmany(newmax, NBBY));
436 if (map == NULL)
437 errx(1, "no memory for active inode map");
438 memcpy(map, usedinomap, howmany(oldmax, NBBY));
439 free(usedinomap);
440 usedinomap = map;
441 }
442 if (dumpinomap) {
443 map = calloc((unsigned)1, (unsigned)howmany(newmax, NBBY));
444 if (map == NULL)
445 errx(1, "no memory for file dump list");
446 memcpy(map, dumpinomap, howmany(oldmax, NBBY));
447 free(dumpinomap);
448 dumpinomap = map;
449 }
450 }
451
452 void
453 GetPathFile(char *source, char *path, char *fname)
454 {
455 char *p, *s;
456
457 *path = 0;
458 *fname = 0;
459 p = s = source;
460 while (*s) {
461 if (*s == '/') {
462 p = s + 1;
463 }
464 s++;
465 }
466 if (p == source) {
467 *path = 0;
468 } else {
469 strncpy(path, source, p - source);
470 path[p - source] = 0;
471 }
472 strcpy(fname, p);
473 }
474
475 #ifdef USE_QFA
476 /*
477 * search for ino in QFA file
478 *
479 * if exactmatch:
480 * if ino found return tape number and tape position
481 * if ino not found return tnum=0 and tpos=0
482 *
483 * if not exactmatch:
484 * if ino found return tape number and tape position
485 * if ino not found return tape number and tape position of last smaller ino
486 * if no smaller inode found return tnum=0 and tpos=0
487 */
488 int
489 Inode2Tapepos(dump_ino_t ino, long *tnum, long long *tpos, int exactmatch)
490 {
491 char *p, *pp;
492 char numbuff[32];
493 dump_ino_t tmpino;
494 long tmptnum;
495 long long tmptpos;
496
497 *tpos = 0;
498 *tnum = 0;
499 if (fseek(gTapeposfp, gSeekstart, SEEK_SET) == -1)
500 return errno;
501 while (1) {
502 gSeekstart = ftell(gTapeposfp); /* remember for later use */
503 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL) {
504 return 0;
505 }
506 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
507 p = gTps;
508 bzero(numbuff, sizeof(numbuff));
509 pp = numbuff;
510 /* read inode */
511 while ((*p != 0) && (*p != '\t'))
512 *pp++ = *p++;
513 tmpino = atol(numbuff);
514 if (*p == 0)
515 return 1; /* may NOT happen */
516 p++;
517 bzero(numbuff, sizeof(numbuff));
518 pp = numbuff;
519 /* read tapenum */
520 while ((*p != 0) && (*p != '\t'))
521 *pp++ = *p++;
522 if (*p == 0)
523 return 1; /* may NOT happen */
524 tmptnum = atol(numbuff);
525 p++;
526 bzero(numbuff, sizeof(numbuff));
527 pp = numbuff;
528 /* read tapepos */
529 while ((*p != 0) && (*p != '\t'))
530 *pp++ = *p++;
531 tmptpos = atoll(numbuff);
532
533 if (tmpino > ino)
534 return 0;
535 if (tmpino == ino || !exactmatch) {
536 *tnum = tmptnum;
537 *tpos = tmptpos;
538 }
539 if (tmpino == ino)
540 return 0;
541 }
542 return 0;
543 }
544
545 #endif /* USE_QFA */
546
547 #ifdef DUMP_MACOSX
548 int
549 CreateAppleDoubleFileRes(char *oFile, FndrFileInfo *finderinfo, mode_t mode, int flags,
550 struct timeval *timep, u_int32_t uid, u_int32_t gid)
551 {
552 int err = 0;
553 int fdout;
554 char *p;
555 char *pp;
556 ASDHeaderPtr hp;
557 ASDEntryPtr ep;
558 long thesize;
559 long n;
560
561
562 n = 1; /* number of entries in double file ._ only finderinfo */
563 /*
564 no data fork
565 n++;
566 currently no resource fork
567 n++;
568 */
569
570 thesize = sizeof(ASDHeader) + (n * sizeof(ASDEntry)) + INFOLEN;
571 if ((pp = p = (char *)malloc(thesize)) == NULL) {
572 err = errno;
573 return err;
574 }
575
576 hp = (ASDHeaderPtr)p;
577 p += sizeof(ASDHeader);
578 ep = (ASDEntryPtr)p;
579 p += sizeof(ASDEntry) * n;
580
581 hp->magic = ADOUBLE_MAGIC;
582 hp->version = ASD_VERSION2;
583
584 bzero(&hp->filler, sizeof(hp->filler));
585 hp->entries = (short)n;
586
587 ep->entryID = EntryFinderInfo;
588 ep->offset = p - pp - CORRECT;
589 ep->len = INFOLEN; /* sizeof(MacFInfo) + sizeof(FXInfo); */
590 bzero(p, ep->len);
591 bcopy(finderinfo, p, sizeof(FndrFileInfo));
592 p += ep->len;
593 ep++;
594
595 if ((fdout = open(oFile, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
596 err = errno;
597 free(pp);
598 return err;
599 }
600
601 /* write the ASDHeader */
602 if (write(fdout, pp, sizeof(ASDHeader) - CORRECT) == -1) {
603 err = errno;
604 close(fdout);
605 free(pp);
606 unlink(oFile);
607 return err;
608 }
609 /* write the ASDEntries */
610 if (write(fdout, pp + sizeof(ASDHeader), thesize - sizeof(ASDHeader)) == -1) {
611 err = errno;
612 close(fdout);
613 free(pp);
614 unlink(oFile);
615 return err;
616 }
617
618 (void)fchown(fdout, uid, gid);
619 (void)fchmod(fdout, mode);
620 close(fdout);
621 (void)lsetflags(oFile, flags);
622 utimes(oFile, timep);
623 free(pp);
624 return err;
625 }
626 #endif /* DUMP_MACOSX */
627
628 int
629 lgetflags(const char *path, unsigned long *flags, unsigned long inode_flags)
630 {
631 int err;
632 struct stat sb;
633
634 err = lstat(path, &sb);
635 if (err < 0)
636 return err;
637
638 if (S_ISLNK(sb.st_mode) || S_ISFIFO(sb.st_mode)) {
639 // no way to get/set flags on a symlink
640 *flags = inode_flags & EXT4_EXTENTS_FL;
641 return 0;
642 }
643 else
644 return fgetflags(path, flags);
645 }
646
647 int
648 lsetflags(const char *path, unsigned long flags)
649 {
650 int err;
651 struct stat sb;
652
653 err = lstat(path, &sb);
654 if (err < 0)
655 return err;
656
657 if (S_ISLNK(sb.st_mode) || S_ISFIFO(sb.st_mode)) {
658 // no way to get/set flags on a symlink
659 return 0;
660 }
661 else
662 return fsetflags(path, flags);
663 }