Debian Codegraph @

restore/utilities.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 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 panic("flags: %s\n", flagvalues(ep));
335 }
336
337 /*
338 * Construct a string indicating the active flag bits of an entry.
339 */
340 char *
341 flagvalues(struct entry *ep)
342 {
343 static char flagbuf[BUFSIZ];
344
345 (void) strcpy(flagbuf, "|NIL");
346 flagbuf[0] = '\0';
347 if (ep->e_flags & REMOVED)
348 (void) strcat(flagbuf, "|REMOVED");
349 if (ep->e_flags & TMPNAME)
350 (void) strcat(flagbuf, "|TMPNAME");
351 if (ep->e_flags & EXTRACT)
352 (void) strcat(flagbuf, "|EXTRACT");
353 if (ep->e_flags & NEW)
354 (void) strcat(flagbuf, "|NEW");
355 if (ep->e_flags & KEEP)
356 (void) strcat(flagbuf, "|KEEP");
357 if (ep->e_flags & EXISTED)
358 (void) strcat(flagbuf, "|EXISTED");
359 return (&flagbuf[1]);
360 }
361
362 /*
363 * Check to see if a name is on a dump tape.
364 */
365 dump_ino_t
366 dirlookup(const char *name)
367 {
368 struct direct *dp;
369 dump_ino_t ino;
370
371 ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
372
373 if (ino == 0 || TSTINO(ino, dumpinomap) == 0)
374 fprintf(stderr, "%s is not on the tape\n", name);
375 return (ino);
376 }
377
378 /*
379 * Elicit a reply.
380 */
381 int
382 reply(const char *question, int yflag)
383 {
384 if (yflag)
385 return (GOOD);
386
387 char c;
388
389 do {
390 fprintf(stderr, "%s? [yn] ", question);
391 (void) fflush(stderr);
392 c = getc(terminal);
393 while (c != '\n' && getc(terminal) != '\n')
394 if (feof(terminal))
395 return (FAIL);
396 } while (c != 'y' && c != 'n');
397 if (c == 'y')
398 return (GOOD);
399 return (FAIL);
400 }
401
402 /*
403 * handle unexpected inconsistencies
404 */
405 #include <stdarg.h>
406
407 void
408 panic(const char *fmt, ...)
409 {
410 va_list ap;
411 va_start(ap, fmt);
412
413 vfprintf(stderr, fmt, ap);
414 if (qflag)
415 exit(1);
416 if (yflag)
417 return;
418 if (reply("abort", 0) == GOOD) {
419 if (reply("dump core", 0) == GOOD) {
420 if (fchdir(wdfd) < 0)
421 warn("fchdir");
422 abort();
423 }
424 exit(1);
425 }
426 }
427
428 void resizemaps(dump_ino_t oldmax, dump_ino_t newmax)
429 {
430 char *map;
431
432 if (usedinomap) {
433 map = calloc((unsigned)1, (unsigned)howmany(newmax, NBBY));
434 if (map == NULL)
435 errx(1, "no memory for active inode map");
436 memcpy(map, usedinomap, howmany(oldmax, NBBY));
437 free(usedinomap);
438 usedinomap = map;
439 }
440 if (dumpinomap) {
441 map = calloc((unsigned)1, (unsigned)howmany(newmax, NBBY));
442 if (map == NULL)
443 errx(1, "no memory for file dump list");
444 memcpy(map, dumpinomap, howmany(oldmax, NBBY));
445 free(dumpinomap);
446 dumpinomap = map;
447 }
448 }
449
450 void
451 GetPathFile(char *source, char *path, char *fname)
452 {
453 char *p, *s;
454
455 *path = 0;
456 *fname = 0;
457 p = s = source;
458 while (*s) {
459 if (*s == '/') {
460 p = s + 1;
461 }
462 s++;
463 }
464 if (p == source) {
465 *path = 0;
466 } else {
467 strncpy(path, source, p - source);
468 path[p - source] = 0;
469 }
470 strcpy(fname, p);
471 }
472
473 #ifdef USE_QFA
474 /*
475 * search for ino in QFA file
476 *
477 * if exactmatch:
478 * if ino found return tape number and tape position
479 * if ino not found return tnum=0 and tpos=0
480 *
481 * if not exactmatch:
482 * if ino found return tape number and tape position
483 * if ino not found return tape number and tape position of last smaller ino
484 * if no smaller inode found return tnum=0 and tpos=0
485 */
486 int
487 Inode2Tapepos(dump_ino_t ino, long *tnum, long long *tpos, int exactmatch)
488 {
489 char *p, *pp;
490 char numbuff[32];
491 dump_ino_t tmpino;
492 long tmptnum;
493 long long tmptpos;
494
495 *tpos = 0;
496 *tnum = 0;
497 if (fseek(gTapeposfp, gSeekstart, SEEK_SET) == -1)
498 return errno;
499 while (1) {
500 gSeekstart = ftell(gTapeposfp); /* remember for later use */
501 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL) {
502 return 0;
503 }
504 gTps[strlen(gTps) - 1] = 0; /* delete end of line */
505 p = gTps;
506 bzero(numbuff, sizeof(numbuff));
507 pp = numbuff;
508 /* read inode */
509 while ((*p != 0) && (*p != '\t'))
510 *pp++ = *p++;
511 tmpino = atol(numbuff);
512 if (*p == 0)
513 return 1; /* may NOT happen */
514 p++;
515 bzero(numbuff, sizeof(numbuff));
516 pp = numbuff;
517 /* read tapenum */
518 while ((*p != 0) && (*p != '\t'))
519 *pp++ = *p++;
520 if (*p == 0)
521 return 1; /* may NOT happen */
522 tmptnum = atol(numbuff);
523 p++;
524 bzero(numbuff, sizeof(numbuff));
525 pp = numbuff;
526 /* read tapepos */
527 while ((*p != 0) && (*p != '\t'))
528 *pp++ = *p++;
529 tmptpos = atoll(numbuff);
530
531 if (tmpino > ino)
532 return 0;
533 if (tmpino == ino || !exactmatch) {
534 *tnum = tmptnum;
535 *tpos = tmptpos;
536 }
537 if (tmpino == ino)
538 return 0;
539 }
540 return 0;
541 }
542
543 #endif /* USE_QFA */
544
545 #ifdef DUMP_MACOSX
546 int
547 CreateAppleDoubleFileRes(char *oFile, FndrFileInfo *finderinfo, mode_t mode, int flags,
548 struct timeval *timep, u_int32_t uid, u_int32_t gid)
549 {
550 int err = 0;
551 int fdout;
552 char *p;
553 char *pp;
554 ASDHeaderPtr hp;
555 ASDEntryPtr ep;
556 long thesize;
557 long n;
558
559
560 n = 1; /* number of entries in double file ._ only finderinfo */
561 /*
562 no data fork
563 n++;
564 currently no resource fork
565 n++;
566 */
567
568 thesize = sizeof(ASDHeader) + (n * sizeof(ASDEntry)) + INFOLEN;
569 if ((pp = p = (char *)malloc(thesize)) == NULL) {
570 err = errno;
571 return err;
572 }
573
574 hp = (ASDHeaderPtr)p;
575 p += sizeof(ASDHeader);
576 ep = (ASDEntryPtr)p;
577 p += sizeof(ASDEntry) * n;
578
579 hp->magic = ADOUBLE_MAGIC;
580 hp->version = ASD_VERSION2;
581
582 bzero(&hp->filler, sizeof(hp->filler));
583 hp->entries = (short)n;
584
585 ep->entryID = EntryFinderInfo;
586 ep->offset = p - pp - CORRECT;
587 ep->len = INFOLEN; /* sizeof(MacFInfo) + sizeof(FXInfo); */
588 bzero(p, ep->len);
589 bcopy(finderinfo, p, sizeof(FndrFileInfo));
590 p += ep->len;
591 ep++;
592
593 if ((fdout = open(oFile, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
594 err = errno;
595 free(pp);
596 return err;
597 }
598
599 /* write the ASDHeader */
600 if (write(fdout, pp, sizeof(ASDHeader) - CORRECT) == -1) {
601 err = errno;
602 close(fdout);
603 free(pp);
604 unlink(oFile);
605 return err;
606 }
607 /* write the ASDEntries */
608 if (write(fdout, pp + sizeof(ASDHeader), thesize - sizeof(ASDHeader)) == -1) {
609 err = errno;
610 close(fdout);
611 free(pp);
612 unlink(oFile);
613 return err;
614 }
615
616 (void)fchown(fdout, uid, gid);
617 (void)fchmod(fdout, mode);
618 close(fdout);
619 (void)lsetflags(oFile, flags);
620 utimes(oFile, timep);
621 free(pp);
622 return err;
623 }
624 #endif /* DUMP_MACOSX */
625
626 int
627 lgetflags(const char *path, unsigned long *flags, unsigned long inode_flags)
628 {
629 int err;
630 struct stat sb;
631
632 err = lstat(path, &sb);
633 if (err < 0)
634 return err;
635
636 if (S_ISLNK(sb.st_mode) || S_ISFIFO(sb.st_mode)) {
637 // no way to get/set flags on a symlink
638 *flags = inode_flags & EXT4_EXTENTS_FL;
639 return 0;
640 }
641 else
642 return fgetflags(path, flags);
643 }
644
645 int
646 lsetflags(const char *path, unsigned long flags)
647 {
648 int err;
649 struct stat sb;
650
651 err = lstat(path, &sb);
652 if (err < 0)
653 return err;
654
655 if (S_ISLNK(sb.st_mode) || S_ISFIFO(sb.st_mode)) {
656 // no way to get/set flags on a symlink
657 return 0;
658 }
659 else
660 return fsetflags(path, flags);
661 }