e0db9afa83b5c68e9ed70d73e81450901f4a2123
[debian/dump] / 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. 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 #ifndef lint
39 static const char rcsid[] =
40         "$Id: utilities.c,v 1.24 2003/11/22 16:52:16 stelian Exp $";
41 #endif /* not lint */
42
43 #include <config.h>
44 #include <compatlfs.h>
45 #include <sys/types.h>
46 #include <errno.h>
47 #include <compaterr.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include <sys/param.h>
53 #include <sys/stat.h>
54
55 #ifdef  __linux__
56 #include <sys/time.h>
57 #include <time.h>
58 #include <fcntl.h>
59 #ifdef HAVE_EXT2FS_EXT2_FS_H
60 #include <ext2fs/ext2_fs.h>
61 #else
62 #include <linux/ext2_fs.h>
63 #endif
64 #include <ext2fs/ext2fs.h>
65 #include <bsdcompat.h>
66 #else   /* __linux__ */
67 #ifdef sunos
68 #include <sys/time.h>
69 #include <sys/fcntl.h>
70 #include <bsdcompat.h>
71 #else
72 #include <ufs/ufs/dinode.h>
73 #include <ufs/ufs/dir.h>
74 #endif
75 #endif  /* __linux__ */
76 #ifdef DUMP_MACOSX
77 #include "darwin.h"
78 #endif
79 #include "restore.h"
80 #include "extern.h"
81
82 /*
83  * Insure that all the components of a pathname exist.
84  */
85 void
86 pathcheck(char *name)
87 {
88         char *cp;
89         struct entry *ep;
90         char *start;
91
92         start = strchr(name, '/');
93         if (start == 0)
94                 return;
95         for (cp = start; *cp != '\0'; cp++) {
96                 if (*cp != '/')
97                         continue;
98                 *cp = '\0';
99                 ep = lookupname(name);
100                 if (ep == NULL) {
101                         /* Safe; we know the pathname exists in the dump. */
102                         ep = addentry(name, pathsearch(name)->d_ino, NODE);
103                         newnode(ep);
104                 }
105                 ep->e_flags |= NEW|KEEP;
106                 *cp = '/';
107         }
108 }
109
110 /*
111  * Change a name to a unique temporary name.
112  */
113 void
114 mktempname(struct entry *ep)
115 {
116         char oldname[MAXPATHLEN];
117
118         if (ep->e_flags & TMPNAME)
119                 badentry(ep, "mktempname: called with TMPNAME");
120         ep->e_flags |= TMPNAME;
121         (void) strcpy(oldname, myname(ep));
122         freename(ep->e_name);
123         ep->e_name = savename(gentempname(ep));
124         ep->e_namlen = strlen(ep->e_name);
125         renameit(oldname, myname(ep));
126 }
127
128 /*
129  * Generate a temporary name for an entry.
130  */
131 char *
132 gentempname(struct entry *ep)
133 {
134         static char name[MAXPATHLEN];
135         struct entry *np;
136         long i = 0;
137
138         for (np = lookupino(ep->e_ino);
139             np != NULL && np != ep; np = np->e_links)
140                 i++;
141         if (np == NULL)
142                 badentry(ep, "not on ino list");
143         (void) snprintf(name, sizeof(name), "%s%ld%lu", TMPHDR, i, (unsigned long)ep->e_ino);
144         return (name);
145 }
146
147 /*
148  * Rename a file or directory.
149  */
150 void
151 renameit(char *from, char *to)
152 {
153         if (!Nflag && rename(from, to) < 0) {
154                 warn("cannot rename %s to %s", from, to);
155                 return;
156         }
157         Vprintf(stdout, "rename %s to %s\n", from, to);
158 }
159
160 /*
161  * Create a new node (directory).
162  */
163 void
164 newnode(struct entry *np)
165 {
166         char *cp;
167         if (np->e_type != NODE)
168                 badentry(np, "newnode: not a node");
169         cp = myname(np);
170         if (command == 'C') return;
171
172         if (!Nflag && mkdir(cp, 0777) < 0 && !uflag) {
173                 np->e_flags |= EXISTED;
174                 warn("%s", cp);
175                 return;
176         }
177         Vprintf(stdout, "Make node %s\n", cp);
178 }
179
180 /*
181  * Remove an old node (directory).
182  */
183 void
184 removenode(struct entry *ep)
185 {
186         char *cp;
187
188         if (ep->e_type != NODE)
189                 badentry(ep, "removenode: not a node");
190         if (ep->e_entries != NULL)
191                 badentry(ep, "removenode: non-empty directory");
192         ep->e_flags |= REMOVED;
193         ep->e_flags &= ~TMPNAME;
194         cp = myname(ep);
195         if (!Nflag && rmdir(cp) < 0) {
196                 warn("%s", cp);
197                 return;
198         }
199         Vprintf(stdout, "Remove node %s\n", cp);
200 }
201
202 /*
203  * Remove a leaf.
204  */
205 void
206 removeleaf(struct entry *ep)
207 {
208         char *cp;
209
210         if (command == 'C') return;
211
212         if (ep->e_type != LEAF)
213                 badentry(ep, "removeleaf: not a leaf");
214         ep->e_flags |= REMOVED;
215         ep->e_flags &= ~TMPNAME;
216         cp = myname(ep);
217         if (!Nflag && unlink(cp) < 0) {
218                 warn("%s", cp);
219                 return;
220         }
221         Vprintf(stdout, "Remove leaf %s\n", cp);
222 }
223
224 /*
225  * Create a link.
226  */
227 int
228 linkit(char *existing, char *new, int type)
229 {
230
231         /* if we want to unlink first, do it now so *link() won't fail */
232         if (uflag && !Nflag)
233                 (void)unlink(new);
234
235         if (type == SYMLINK) {
236                 if (!Nflag && symlink(existing, new) < 0) {
237                         warn("cannot create symbolic link %s->%s",
238                             new, existing);
239                         return (FAIL);
240                 }
241         } else if (type == HARDLINK) {
242                 int ret;
243
244                 if (!Nflag && (ret = link(existing, new)) < 0) {
245
246 #if !defined(__linux__) && !defined(sunos)
247                         struct stat s;
248
249                         /*
250                          * Most likely, the schg flag is set.  Clear the
251                          * flags and try again.
252                          */
253                         if (stat(existing, &s) == 0 && s.st_flags != 0 &&
254                             chflags(existing, 0) == 0) {
255                                 ret = link(existing, new);
256                                 chflags(existing, s.st_flags);
257                         }
258 #else
259                         unsigned long s;
260
261                         /*
262                          * Most likely, the immutable or append-only attribute
263                          * is set. Clear the attributes and try again.
264                          */
265 #ifdef sunos
266 #else
267                         if (fgetflags (existing, &s) != -1 &&
268                             fsetflags (existing, 0) != -1) {
269                                 ret = link(existing, new);
270                                 fsetflags(existing, s);
271                         }
272 #endif
273 #endif
274                         if (ret < 0) {
275                                 warn("warning: cannot create hard link %s->%s",
276                                     new, existing);
277                                 return (FAIL);
278                         }
279                 }
280         } else {
281                 panic("linkit: unknown type %d\n", type);
282                 return (FAIL);
283         }
284         Vprintf(stdout, "Create %s link %s->%s\n",
285                 type == SYMLINK ? "symbolic" : "hard", new, existing);
286         return (GOOD);
287 }
288
289 #if !defined(__linux__) && !defined(sunos)
290 /*
291  * Create a whiteout.
292  */
293 int
294 addwhiteout(char *name)
295 {
296
297         if (!Nflag && mknod(name, S_IFWHT, 0) < 0) {
298                 warn("cannot create whiteout %s", name);
299                 return (FAIL);
300         }
301         Vprintf(stdout, "Create whiteout %s\n", name);
302         return (GOOD);
303 }
304
305 /*
306  * Delete a whiteout.
307  */
308 void
309 delwhiteout(struct entry *ep)
310 {
311         char *name;
312
313         if (ep->e_type != LEAF)
314                 badentry(ep, "delwhiteout: not a leaf");
315         ep->e_flags |= REMOVED;
316         ep->e_flags &= ~TMPNAME;
317         name = myname(ep);
318         if (!Nflag && undelete(name) < 0) {
319                 warn("cannot delete whiteout %s", name);
320                 return;
321         }
322         Vprintf(stdout, "Delete whiteout %s\n", name);
323 }
324 #endif
325
326 /*
327  * find lowest number file (above "start") that needs to be extracted
328  */
329 dump_ino_t
330 lowerbnd(dump_ino_t start)
331 {
332         struct entry *ep;
333
334         for ( ; start < maxino; start++) {
335                 ep = lookupino(start);
336                 if (ep == NULL || ep->e_type == NODE)
337                         continue;
338                 if (ep->e_flags & (NEW|EXTRACT))
339                         return (start);
340         }
341         return (start);
342 }
343
344 /*
345  * find highest number file (below "start") that needs to be extracted
346  */
347 dump_ino_t
348 upperbnd(dump_ino_t start)
349 {
350         struct entry *ep;
351
352         for ( ; start > ROOTINO; start--) {
353                 ep = lookupino(start);
354                 if (ep == NULL || ep->e_type == NODE)
355                         continue;
356                 if (ep->e_flags & (NEW|EXTRACT))
357                         return (start);
358         }
359         return (start);
360 }
361
362 /*
363  * report on a badly formed entry
364  */
365 void
366 badentry(struct entry *ep, const char *msg)
367 {
368
369         fprintf(stderr, "bad entry: %s\n", msg);
370         fprintf(stderr, "name: %s\n", myname(ep));
371         fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
372         if (ep->e_sibling != NULL)
373                 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
374         if (ep->e_entries != NULL)
375                 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
376         if (ep->e_links != NULL)
377                 fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
378         if (ep->e_next != NULL)
379                 fprintf(stderr,
380                     "next hashchain name: %s\n", myname(ep->e_next));
381         fprintf(stderr, "entry type: %s\n",
382                 ep->e_type == NODE ? "NODE" : "LEAF");
383         fprintf(stderr, "inode number: %lu\n", (unsigned long)ep->e_ino);
384         panic("flags: %s\n", flagvalues(ep));
385 }
386
387 /*
388  * Construct a string indicating the active flag bits of an entry.
389  */
390 char *
391 flagvalues(struct entry *ep)
392 {
393         static char flagbuf[BUFSIZ];
394
395         (void) strcpy(flagbuf, "|NIL");
396         flagbuf[0] = '\0';
397         if (ep->e_flags & REMOVED)
398                 (void) strcat(flagbuf, "|REMOVED");
399         if (ep->e_flags & TMPNAME)
400                 (void) strcat(flagbuf, "|TMPNAME");
401         if (ep->e_flags & EXTRACT)
402                 (void) strcat(flagbuf, "|EXTRACT");
403         if (ep->e_flags & NEW)
404                 (void) strcat(flagbuf, "|NEW");
405         if (ep->e_flags & KEEP)
406                 (void) strcat(flagbuf, "|KEEP");
407         if (ep->e_flags & EXISTED)
408                 (void) strcat(flagbuf, "|EXISTED");
409         return (&flagbuf[1]);
410 }
411
412 /*
413  * Check to see if a name is on a dump tape.
414  */
415 dump_ino_t
416 dirlookup(const char *name)
417 {
418         struct direct *dp;
419         dump_ino_t ino;
420
421         ino = ((dp = pathsearch(name)) == NULL) ? 0 : dp->d_ino;
422
423         if (ino == 0 || TSTINO(ino, dumpmap) == 0)
424                 fprintf(stderr, "%s is not on the tape\n", name);
425         return (ino);
426 }
427
428 /*
429  * Elicit a reply.
430  */
431 int
432 reply(const char *question)
433 {
434         char c;
435
436         do      {
437                 fprintf(stderr, "%s? [yn] ", question);
438                 (void) fflush(stderr);
439                 c = getc(terminal);
440                 while (c != '\n' && getc(terminal) != '\n')
441                         if (feof(terminal))
442                                 return (FAIL);
443         } while (c != 'y' && c != 'n');
444         if (c == 'y')
445                 return (GOOD);
446         return (FAIL);
447 }
448
449 /*
450  * handle unexpected inconsistencies
451  */
452 #ifdef __STDC__
453 #include <stdarg.h>
454 #else
455 #include <varargs.h>
456 #endif
457
458 void
459 #ifdef __STDC__
460 panic(const char *fmt, ...)
461 #else
462 panic(fmt, va_alist)
463         char *fmt;
464         va_dcl
465 #endif
466 {
467         va_list ap;
468 #ifdef __STDC__
469         va_start(ap, fmt);
470 #else
471         va_start(ap);
472 #endif
473
474         vfprintf(stderr, fmt, ap);
475         if (yflag)
476                 return;
477         if (reply("abort") == GOOD) {
478                 if (reply("dump core") == GOOD)
479                         abort();
480                 exit(1);
481         }
482 }
483
484 void resizemaps(dump_ino_t oldmax, dump_ino_t newmax)
485 {
486         char *map;
487
488         if (usedinomap) {
489                 map = calloc((unsigned)1, (unsigned)howmany(newmax, NBBY));
490                 if (map == NULL)
491                         errx(1, "no memory for active inode map");
492                 memcpy(map, usedinomap, howmany(oldmax, NBBY));
493                 free(usedinomap);
494                 usedinomap = map;
495         }
496         if (dumpmap) {
497                 map = calloc((unsigned)1, (unsigned)howmany(newmax, NBBY));
498                 if (map == NULL)
499                         errx(1, "no memory for file dump list");
500                 memcpy(map, dumpmap, howmany(oldmax, NBBY));
501                 free(dumpmap);
502                 dumpmap = map;
503         }
504 }
505
506 void
507 GetPathFile(char *source, char *path, char *fname)
508 {
509         char *p, *s;
510
511         *path = 0;
512         *fname = 0;
513         p = s = source;
514         while (*s) {
515                 if (*s == '/') {
516                         p = s + 1;
517                 }
518                 s++;
519         }
520         if (p == source) {
521                 *path = 0;
522         } else {
523                 strncpy(path, source, p - source);
524                 path[p - source] = 0;
525         }
526         strcpy(fname, p);
527 }
528
529 #ifdef USE_QFA
530 /*
531  * search for ino in QFA file
532  *
533  * if exactmatch:
534  * if ino found return tape number and tape position
535  * if ino not found return tnum=0 and tpos=0
536  *
537  * if not exactmatch:
538  * if ino found return tape number and tape position
539  * if ino not found return tape number and tape position of last smaller ino
540  * if no smaller inode found return tnum=0 and tpos=0
541  */
542 int
543 Inode2Tapepos(dump_ino_t ino, long *tnum, long long *tpos, int exactmatch)
544 {
545         char *p, *pp;
546         char numbuff[32];
547         unsigned long tmpino;
548         long tmptnum;
549         long long tmptpos;
550
551         *tpos = 0;
552         *tnum = 0;
553         if (fseek(gTapeposfp, gSeekstart, SEEK_SET) == -1)
554                 return errno;
555         while (1) {
556                 gSeekstart = ftell(gTapeposfp); /* remember for later use */
557                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL) {
558                         return 0;
559                 }
560                 gTps[strlen(gTps) - 1] = 0;     /* delete end of line */
561                 p = gTps;
562                 bzero(numbuff, sizeof(numbuff));
563                 pp = numbuff;
564                 /* read inode */
565                 while ((*p != 0) && (*p != '\t'))
566                         *pp++ = *p++;
567                 tmpino = atol(numbuff);
568                 if (*p == 0)
569                         return 1;       /* may NOT happen */    
570                 p++;
571                 bzero(numbuff, sizeof(numbuff));
572                 pp = numbuff;
573                 /* read tapenum */
574                 while ((*p != 0) && (*p != '\t'))
575                         *pp++ = *p++;
576                 if (*p == 0)
577                         return 1;       /* may NOT happen */    
578                 tmptnum = atol(numbuff);
579                 p++;
580                 bzero(numbuff, sizeof(numbuff));
581                 pp = numbuff;
582                 /* read tapepos */
583                 while ((*p != 0) && (*p != '\t'))
584                         *pp++ = *p++;
585                 tmptpos = atoll(numbuff);
586
587                 if (exactmatch) {
588                         if (tmpino == ino)  {
589                                 *tnum = tmptnum;
590                                 *tpos = tmptpos;
591                                 return 0;
592                         }
593                 } else {
594                         if (tmpino > ino)  {
595                                 return 0;
596                         } else {
597                                 *tnum = tmptnum;
598                                 *tpos = tmptpos;
599                         }
600                 }
601         }
602         return 0;
603 }
604
605 #ifdef sunos
606 int
607 GetSCSIIDFromPath(char *devPath, long *id)
608 {
609         int     len;
610         char    fbuff[2048];
611         char    path[2048];
612         char    fname[2048];
613         char    *fpn = fname;
614         char    idstr[32];
615         char    *ip = idstr;
616
617         bzero(fbuff, sizeof(fbuff));
618         if ((len = readlink(devPath, fbuff, 2048)) == -1) {
619                 return errno;
620         }
621         fbuff[len] = 0;
622         GetPathFile(fbuff, path, fname);
623         (void)memset(idstr, 0, sizeof(idstr));
624         while (*fpn && (*fpn != ',')) {
625                 if (*fpn <= '9' && *fpn >= '0') {
626                         *ip = *fpn;
627                         ip++;
628                 }
629                 fpn++;
630         }
631         if (*idstr) {
632                 *id = atol(idstr);
633         } else {
634                 *id = -1;
635         }
636         return 0;
637 }
638 #endif
639 #endif /* USE_QFA */
640
641 #ifdef DUMP_MACOSX
642 int
643 CreateAppleDoubleFileRes(char *oFile, FndrFileInfo *finderinfo, mode_t mode, int flags,
644                 struct timeval *timep, u_int32_t uid, u_int32_t gid)
645 {
646         int             err = 0;
647         int             fdout;
648         char            *p;
649         char            *pp;
650         ASDHeaderPtr    hp;
651         ASDEntryPtr     ep;
652         long            thesize;
653         long            n;
654
655
656         n = 1;  /* number of entries in double file ._ only finderinfo */
657         /*
658         no data fork
659         n++;
660         currently no resource fork
661         n++;
662         */
663
664         thesize = sizeof(ASDHeader) + (n * sizeof(ASDEntry)) + INFOLEN;
665         if ((pp = p = (char *)malloc(thesize)) == NULL) {
666                 err = errno;
667                 return err;
668         }
669
670         hp = (ASDHeaderPtr)p;
671         p += sizeof(ASDHeader);
672         ep = (ASDEntryPtr)p;
673         p += sizeof(ASDEntry) * n;
674
675         hp->magic = ADOUBLE_MAGIC;
676         hp->version = ASD_VERSION2;
677
678         bzero(&hp->filler, sizeof(hp->filler));
679         hp->entries = (short)n;
680         
681         ep->entryID = EntryFinderInfo;
682         ep->offset = p - pp - CORRECT;
683         ep->len = INFOLEN; /*  sizeof(MacFInfo) + sizeof(FXInfo); */
684         bzero(p, ep->len);
685         bcopy(finderinfo, p, sizeof(FndrFileInfo));
686         p += ep->len;
687         ep++;
688
689         if ((fdout = open(oFile, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
690                 err = errno;
691                 free(pp);
692                 return err;
693         }
694
695         /* write the ASDHeader */
696         if (write(fdout, pp, sizeof(ASDHeader) - CORRECT) == -1) {
697                 err = errno;
698                 close(fdout);
699                 free(pp);
700                 unlink(oFile);
701                 return err;
702         }
703         /* write the ASDEntries */
704         if (write(fdout, pp + sizeof(ASDHeader), thesize - sizeof(ASDHeader)) == -1) {
705                 err = errno;
706                 close(fdout);
707                 free(pp);
708                 unlink(oFile);
709                 return err;
710         }
711
712         (void)fchown(fdout, uid, gid);
713         (void)fchmod(fdout, mode);
714         close(fdout);
715         (void)fsetflags(oFile, flags);
716         utimes(oFile, timep);
717         free(pp);
718         return err;
719 }
720 #endif /* DUMP_MACOSX */