]> git.wh0rd.org Git - dump.git/blob - restore/main.c
Fix SELinux xattr handling.
[dump.git] / restore / main.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: main.c,v 1.51 2007/02/22 20:12:50 stelian Exp $";
41 #endif /* not lint */
42
43 #include <config.h>
44 #include <compatlfs.h>
45 #include <sys/types.h>
46 #include <fcntl.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #include <errno.h>
50
51 #ifdef  __linux__
52 #include <sys/time.h>
53 #include <time.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 <bsdcompat.h>
60 #include <signal.h>
61 #include <string.h>
62 #else   /* __linux__ */
63 #ifdef sunos
64 #include <signal.h>
65 #include <string.h>
66 #include <sys/fcntl.h>
67 #include <bsdcompat.h>
68 #include <sys/mtio.h>
69 #else
70 #include <ufs/ufs/dinode.h>
71 #endif
72 #endif  /* __linux__ */
73 #include <protocols/dumprestore.h>
74
75 #include <compaterr.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <unistd.h>
79
80 #ifdef  __linux__
81 #include <ext2fs/ext2fs.h>
82 #include <getopt.h>
83 #endif
84
85 #include "pathnames.h"
86 #include "restore.h"
87 #include "extern.h"
88
89 int abortifconnerr = 1;         /* set to 1 if lib dumprmt.o should exit on connection errors
90                                 otherwise just print a message using msg */
91
92 int     aflag = 0, bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0;
93 int     hflag = 1, mflag = 1, Mflag = 0, Nflag = 0, Vflag = 0, zflag = 0;
94 int     uflag = 0, lflag = 0, Lflag = 0, oflag = 0;
95 int     ufs2flag = 0;
96 char    *Afile = NULL;
97 int     dokerberos = 0;
98 char    command = '\0';
99 long    dumpnum = 1;
100 long    volno = 0;
101 long    ntrec;
102 char    *dumpmap = NULL;
103 char    *usedinomap = NULL;
104 dump_ino_t maxino;
105 time_t  dumptime;
106 time_t  dumpdate;
107 FILE    *terminal;
108 char    *tmpdir;
109 int     compare_ignore_not_found;
110 int     compare_errors;
111 char    filesys[NAMELEN];
112 static const char *stdin_opt = NULL;
113 char    *bot_script = NULL;
114 dump_ino_t volinfo[TP_NINOS];
115 int     wdfd;
116 int     dirhash_size = 1;
117
118 #ifdef USE_QFA
119 FILE    *gTapeposfp;
120 char    *gTapeposfile;
121 char    gTps[255];
122 long    gSeekstart;
123 int     tapeposflag;
124 int     gTapeposfd;
125 int     createtapeposflag;
126 unsigned long qfadumpdate;
127 long long curtapepos;
128 #endif /* USE_QFA */
129
130 #ifdef TRANSSELINUX                     /*GAN6May06 SELinux MLS */
131 int     transselinuxflag = 0;
132 char    *transselinuxarg = NULL;
133 #endif
134
135 long smtc_errno;
136
137 #if defined(__linux__) || defined(sunos)
138 char    *__progname;
139 #endif
140
141 static void obsolete __P((int *, char **[]));
142 static void usage __P((void));
143 static void use_stdin __P((const char *));
144
145 #define FORCED_UMASK (077)
146
147 int
148 main(int argc, char *argv[])
149 {
150         int ch;
151         dump_ino_t ino;
152         char *inputdev = _PATH_DEFTAPE;
153         char *symtbl = "./restoresymtable";
154         char *p, name[MAXPATHLEN];
155         FILE *filelist = NULL;
156         char fname[MAXPATHLEN];
157         mode_t orig_umask;
158 #ifdef DEBUG_QFA
159         time_t tistart, tiend, titaken;
160 #endif
161 #ifdef USE_QFA
162         tapeposflag = 0;
163         createtapeposflag = 0;
164 #endif /* USE_QFA */
165 #ifdef TRANSSELINUX                     /*GAN6May06 SELinux MLS */
166         char transselinuxopt;
167 #endif
168
169         /* Temp files should *not* be readable.  We set permissions later. */
170         orig_umask = umask(FORCED_UMASK);
171         filesys[0] = '\0';
172 #if defined(__linux__) || defined(sunos)
173         __progname = argv[0];
174 #endif
175
176         if (argc < 2)
177                 usage();
178
179         if ((inputdev = getenv("TAPE")) == NULL)
180                 inputdev = _PATH_DEFTAPE;
181         if ((tmpdir = getenv("TMPDIR")) == NULL)
182                 tmpdir = _PATH_TMP;
183         if ((tmpdir = strdup(tmpdir)) == NULL)
184                 err(1, "malloc tmpdir");
185         for (p = tmpdir + strlen(tmpdir) - 1; p >= tmpdir && *p == '/'; p--)
186                 ;                                                               
187         obsolete(&argc, &argv);
188         while ((ch = getopt(argc, argv, 
189                 "aA:b:CcdD:"
190 #ifdef TRANSSELINUX                     /*GAN6May06 SELinux MLS */
191                 "eE:"
192 #endif
193                 "f:F:hH:i"
194 #ifdef KERBEROS
195                 "k"
196 #endif
197                 "lL:mMNo"
198 #ifdef USE_QFA
199                 "P:Q:"
200 #endif
201                 "Rrs:tT:uvVxX:y")) != -1)
202                 switch(ch) {
203                 case 'a':
204                         aflag = 1;
205                         break;
206                 case 'A':
207                         Afile = optarg;
208                         aflag = 1;
209                         break;
210                 case 'b':
211                         /* Change default tape blocksize. */
212                         bflag = 1;
213                         ntrec = strtol(optarg, &p, 10);
214                         if (*p)
215                                 errx(1, "illegal blocksize -- %s", optarg);
216                         if (ntrec <= 0)
217                                 errx(1, "block size must be greater than 0");
218                         break;
219                 case 'c':
220                         cvtflag = 1;
221                         break;
222                 case 'D':
223                         strncpy(filesys, optarg, NAMELEN);
224                         filesys[NAMELEN - 1] = '\0';
225                         break;
226 #ifdef TRANSSELINUX                     /*GAN6May06 SELinux MLS */
227                 case 'e':
228                         transselinuxflag = 1;
229                         transselinuxopt = ch;
230                         break;
231                 case 'E':
232                         transselinuxflag = 1;
233                         transselinuxarg = optarg;
234                         transselinuxopt = ch;
235                         break;
236 #endif
237                 case 'T':
238                         tmpdir = optarg;
239                         break;
240                 case 'd':
241                         dflag = 1;
242                         break;
243                 case 'f':
244                         if( !strcmp(optarg,"-") )
245                                 use_stdin("-f");
246                         inputdev = optarg;
247                         break;
248                 case 'F':
249                         bot_script = optarg;
250                         break;
251                 case 'h':
252                         hflag = 0;
253                         break;
254                 case 'H':
255                         dirhash_size = strtol(optarg, &p, 10);
256                         if (*p)
257                                 errx(1, "illegal hash size -- %s", optarg);
258                         if (dirhash_size < 1)
259                                 errx(1, "hash size must be greater than 0");
260                         break;
261 #ifdef KERBEROS
262                 case 'k':
263                         dokerberos = 1;
264                         break;
265 #endif
266                 case 'C':
267                 case 'i':
268 #ifdef USE_QFA
269                 case 'P':
270 #endif
271                 case 'R':
272                 case 'r':
273                 case 't':
274                 case 'x':
275                         if (command != '\0')
276                                 errx(1,
277                                     "%c and %c options are mutually exclusive",
278                                     ch, command);
279                         command = ch;
280 #ifdef USE_QFA
281                         if (ch == 'P') {
282                                 gTapeposfile = optarg;
283                                 createtapeposflag = 1;
284                         }
285 #endif
286
287                         break;
288                 case 'l':
289                         lflag = 1;
290                         break;
291                 case 'L':
292                         Lflag = strtol(optarg, &p, 10);
293                         if (*p)
294                                 errx(1, "illegal limit -- %s", optarg);
295                         if (Lflag < 0)
296                                 errx(1, "limit must be greater than 0");
297                         break;
298                 case 'm':
299                         mflag = 0;
300                         break;
301                 case 'M':
302                         Mflag = 1;
303                         break;
304                 case 'N':
305                         Nflag = 1;
306                         break;
307                 case 'o':
308                         oflag = 1;
309                         break;
310 #ifdef USE_QFA
311                 case 'Q':
312                         gTapeposfile = optarg;
313                         tapeposflag = 1;
314                         aflag = 1;
315                         break;
316 #endif
317                 case 's':
318                         /* Dumpnum (skip to) for multifile dump tapes. */
319                         dumpnum = strtol(optarg, &p, 10);
320                         if (*p)
321                                 errx(1, "illegal dump number -- %s", optarg);
322                         if (dumpnum <= 0)
323                                 errx(1, "dump number must be greater than 0");
324                         break;
325                 case 'u':
326                         uflag = 1;
327                         break;
328                 case 'v':
329                         vflag = 1;
330                         break;
331                 case 'V':
332                         Vflag = 1;
333                         break;
334                 case 'X':
335                         if( !strcmp(optarg,"-") ) {
336                                 use_stdin("-X");
337                                 filelist = stdin;
338                         }
339                         else
340                                 if ( !(filelist = fopen(optarg,"r")) )
341                                         errx(1, "can't open file for reading -- %s", optarg);
342                         break;
343                 case 'y':
344                         yflag = 1;
345                         break;
346                 default:
347                         usage();
348                 }
349         argc -= optind;
350         argv += optind;
351
352         if (command == '\0')
353                 errx(1, "none of C, i, R, r, t or x options specified");
354
355 #ifdef USE_QFA
356         if (!mflag && tapeposflag)
357                 errx(1, "m and Q options are mutually exclusive");
358
359         if (tapeposflag && command != 'i' && command != 'x' && command != 't')
360                 errx(1, "Q option is not valid for %c command", command);
361 #endif
362         
363         if (Afile && command != 'i' && command != 'x' && command != 't')
364                 errx(1, "A option is not valid for %c command", command);
365
366 #ifdef TRANSSELINUX                     /*GAN6May06 SELinux MLS */
367         if (transselinuxflag && !strchr("CirRx", command))
368                 errx(1, "%c option is not valid for %c command", transselinuxopt, command);
369 #endif
370
371         if (signal(SIGINT, onintr) == SIG_IGN)
372                 (void) signal(SIGINT, SIG_IGN);
373         if (signal(SIGTERM, onintr) == SIG_IGN)
374                 (void) signal(SIGTERM, SIG_IGN);
375         setlinebuf(stderr);
376
377         atexit(cleanup);
378
379         if (command == 'C' && inputdev[0] != '/' && strcmp(inputdev, "-")
380 #ifdef RRESTORE
381             && !strchr(inputdev, ':')
382 #endif
383           ) {
384                 /* since we chdir into the directory we are comparing
385                  * to, we must retain the full tape path */
386                 char wd[MAXPATHLEN], fullpathinput[MAXPATHLEN];
387                 if (!getcwd(wd, MAXPATHLEN))
388                         err(1, "can't get current directory");
389                 snprintf(fullpathinput, MAXPATHLEN, "%s/%s", wd, inputdev);
390                 fullpathinput[MAXPATHLEN - 1] = '\0';
391                 setinput(fullpathinput);
392         }
393         else
394                 setinput(inputdev);
395
396         wdfd = open(".", O_RDONLY);
397         if (wdfd < 0)
398                 err(1, "can't get current directory");
399
400         if (argc == 0 && !filelist) {
401                 argc = 1;
402                 *--argv = ".";
403         }
404
405 #ifdef USE_QFA
406         if (tapeposflag) {
407                 msg("reading QFA positions from %s\n", gTapeposfile);
408                 if ((gTapeposfp = fopen(gTapeposfile, "r")) == NULL)
409                         errx(1, "can't open file for reading -- %s",
410                                 gTapeposfile);
411                 /* start reading header info */
412                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
413                         errx(1, "not requested format of -- %s", gTapeposfile);
414                 gTps[strlen(gTps) - 1] = 0;     /* delete end of line */
415                 if (strcmp(gTps, QFA_MAGIC) != 0)
416                         errx(1, "not requested format of -- %s", gTapeposfile);
417                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
418                         errx(1, "not requested format of -- %s", gTapeposfile);
419                 gTps[strlen(gTps) - 1] = 0;
420                 if (strcmp(gTps, QFA_VERSION) != 0)
421                         errx(1, "not requested format of -- %s", gTapeposfile);
422                 /* read dumpdate */
423                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
424                         errx(1, "not requested format of -- %s", gTapeposfile);
425                 gTps[strlen(gTps) - 1] = 0;
426                 qfadumpdate = atol(gTps);
427                 /* read empty line */
428                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
429                         errx(1, "not requested format of -- %s", gTapeposfile);
430                 gTps[strlen(gTps) - 1] = 0;
431                 /* read table header line */
432                 if (fgets(gTps, sizeof(gTps), gTapeposfp) == NULL)
433                         errx(1, "not requested format of -- %s", gTapeposfile);
434                 gTps[strlen(gTps) - 1] = 0;
435                 /* end reading header info */
436                 /* tape position table starts here */
437                 gSeekstart = ftell(gTapeposfp); /* remember for later use */
438 #ifdef sunos
439                 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
440                         errx(1, "can't get SCSI-ID for %s\n", inputdev);
441                 }
442                 if (scsiid < 0) {
443                         errx(1, "can't get SCSI-ID for %s\n", inputdev);
444                 }
445                 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
446                 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
447                         errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
448                 }
449 #endif
450         }
451 #endif /* USE_QFA */
452
453         switch (command) {
454         /*
455          * Compare contents of tape.
456          */
457         case 'C': {
458                 struct STAT stbuf;
459
460                 Vprintf(stdout, "Begin compare restore\n");
461                 compare_ignore_not_found = 0;
462                 compare_errors = 0;
463                 Nflag = 1;
464                 setup();
465                 printf("filesys = %s\n", filesys);
466                 if (STAT(filesys, &stbuf) < 0)
467                         err(1, "cannot stat directory %s", filesys);
468                 if (chdir(filesys) < 0)
469                         err(1, "cannot cd to %s", filesys);
470                 compare_ignore_not_found = dumptime > 0;
471                 initsymtable((char *)0);
472                 extractdirs(1);
473                 treescan(".", ROOTINO, nodeupdates);
474                 compareleaves();
475                 comparedirmodes();
476                 checkrestore();
477                 if (compare_errors) {
478                         printf("Some files were modified!  %d compare errors\n", compare_errors);
479                         exit(2);
480                 }
481                 break;
482         }
483
484         /*
485          * Interactive mode.
486          */
487         case 'i':
488                 setup();
489                 extractdirs(1);
490                 initsymtable(NULL);
491                 runcmdshell();
492                 break;
493         /*
494          * Incremental restoration of a file system.
495          */
496         case 'r':
497                 aflag = 1;      /* in -r or -R mode, -a is default */
498                 setup();
499                 if (dumptime > 0) {
500                         /*
501                          * This is an incremental dump tape.
502                          */
503                         Vprintf(stdout, "Begin incremental restore\n");
504                         initsymtable(symtbl);
505                         extractdirs(1);
506                         removeoldleaves();
507                         Vprintf(stdout, "Calculate node updates.\n");
508                         treescan(".", ROOTINO, nodeupdates);
509                         findunreflinks();
510                         removeoldnodes();
511                 } else {
512                         /*
513                          * This is a level zero dump tape.
514                          */
515                         Vprintf(stdout, "Begin level 0 restore\n");
516                         initsymtable((char *)0);
517                         extractdirs(1);
518                         Vprintf(stdout, "Calculate extraction list.\n");
519                         treescan(".", ROOTINO, nodeupdates);
520                 }
521                 createleaves(symtbl);
522                 createlinks();
523                 setdirmodes(FORCE);
524                 checkrestore();
525                 if (dflag) {
526                         Vprintf(stdout, "Verify the directory structure\n");
527                         treescan(".", ROOTINO, verifyfile);
528                 }
529                 dumpsymtable(symtbl, (long)1);
530                 break;
531         /*
532          * Resume an incremental file system restoration.
533          */
534         case 'R':
535                 aflag = 1;      /* in -r or -R mode, -a is default */
536                 initsymtable(symtbl);
537                 skipmaps();
538                 skipdirs();
539                 createleaves(symtbl);
540                 createlinks();
541                 setdirmodes(FORCE);
542                 checkrestore();
543                 dumpsymtable(symtbl, (long)1);
544                 break;
545         
546 /* handle file names from either text file (-X) or the command line */
547 #define NEXTFILE(p) \
548         p = NULL; \
549         if (argc) { \
550                 --argc; \
551                 p = *argv++; \
552         } \
553         else if (filelist) { \
554                 if ((p = fgets(fname, MAXPATHLEN, filelist))) { \
555                         if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */ \
556                                 *(p + strlen(p) - 1) = '\0'; \
557                         if ( !*p ) /* skip empty lines */ \
558                                 continue; \
559                         } \
560         }
561         
562         /*
563          * List contents of tape.
564          */
565         case 't':
566                 setup();
567                 extractdirs(0);
568                 initsymtable((char *)0);
569                 printvolinfo();
570                 for (;;) {
571                         NEXTFILE(p);
572                         if (!p)
573                                 break;
574                         canon(p, name, sizeof(name));
575                         ino = dirlookup(name);
576                         if (ino == 0)
577                                 continue;
578                         treescan(name, ino, listfile);
579                 }
580                 break;
581         /*
582          * Batch extraction of tape contents.
583          */
584         case 'x':
585 #ifdef DEBUG_QFA
586                 tistart = time(NULL);
587 #endif
588                 setup();
589                 extractdirs(1);
590                 initsymtable((char *)0);
591                 for (;;) {
592                         NEXTFILE(p);
593                         if (!p)
594                                 break;
595                         canon(p, name, sizeof(name));
596                         ino = dirlookup(name);
597                         if (ino == 0)
598                                 continue;
599                         if (mflag)
600                                 pathcheck(name);
601                         treescan(name, ino, addfile);
602                 }
603                 createfiles();
604                 createlinks();
605                 setdirmodes(oflag ? FORCE : 0);
606                 if (dflag)
607                         checkrestore();
608 #ifdef sunos
609                 if (fdsmtc != -1) {
610                         close(fdsmtc);
611                 }
612 #endif /* sunos */
613 #ifdef DEBUG_QFA
614                 tiend = time(NULL);
615                 titaken = tiend - tistart;
616                 msg("restore took %d:%02d:%02d\n", titaken / 3600, 
617                         (titaken % 3600) / 60, titaken % 60);
618 #endif /* DEBUG_QFA */
619                 break;
620 #ifdef USE_QFA
621         case 'P':
622 #ifdef DEBUG_QFA
623                 tistart = time(NULL);
624 #endif
625 #ifdef sunos
626                 if (GetSCSIIDFromPath(inputdev, &scsiid)) {
627                         errx(1, "can't get SCSI-ID for %s\n", inputdev);
628                 }
629                 if (scsiid < 0) {
630                         errx(1, "can't get SCSI-ID for %s\n", inputdev);
631                 }
632                 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
633                 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
634                         errx(1, "can't open smtc device: %s, %d\n", smtcpath, errno);
635                 }
636 #endif /* sunos */
637                 setup();
638                 msg("writing QFA positions to %s\n", gTapeposfile);
639                 (void) umask(orig_umask);
640                 if ((gTapeposfd = open(gTapeposfile, O_WRONLY|O_CREAT|O_TRUNC,
641                                        S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
642                                        |S_IROTH|S_IWOTH)) < 0)
643                         errx(1, "can't create tapeposfile\n");
644                 (void) umask(FORCED_UMASK);
645                 /* print QFA-file header */
646                 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
647                 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
648                         errx(1, "can't write tapeposfile\n");
649                 sprintf(gTps, "ino\ttapeno\ttapepos\n");
650                 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
651                         errx(1, "can't write tapeposfile\n");
652
653                 extractdirs(1);
654                 initsymtable((char *)0);
655                 for (;;) {
656                         NEXTFILE(p);
657                         if (!p)
658                                 break;
659                         canon(p, name, sizeof(name));
660                         ino = dirlookup(name);
661                         if (ino == 0)
662                                 continue;
663                         if (mflag)
664                                 pathcheck(name);
665                         treescan(name, ino, addfile);
666                 }
667                 createfiles();
668 #ifdef sunos
669                 if (fdsmtc != -1) {
670                         close(fdsmtc);
671                 }
672 #endif /* sunos */
673 #ifdef DEBUG_QFA
674                 tiend = time(NULL);
675                 titaken = tiend - tistart;
676                 msg("writing QFA positions took %d:%02d:%02d\n", titaken / 3600, 
677                         (titaken % 3600) / 60, titaken % 60);
678 #endif /* DEBUG_QFA */
679                 break;
680 #endif  /* USE_QFA */
681         }
682         exit(0);
683         /* NOTREACHED */
684         return 0;       /* gcc shut up */
685 }
686
687 static void
688 usage(void)
689 {
690         char white[MAXPATHLEN];
691         const char *ext2ver, *ext2date;
692
693         memset(white, ' ', MAXPATHLEN);
694         white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
695
696 #ifdef __linux__
697         ext2fs_get_library_version(&ext2ver, &ext2date);
698         (void)fprintf(stderr, "%s %s (using libext2fs %s of %s)\n", 
699                       __progname, _DUMP_VERSION, ext2ver, ext2date);
700 #else
701         (void)fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
702 #endif
703
704 #ifdef KERBEROS
705 #define kerbflag "k"
706 #else
707 #define kerbflag
708 #endif
709
710 #ifdef USE_QFA
711 #define qfaflag "[-Q file] "
712 #else
713 #define qfaflag
714 #endif
715
716 #ifdef TRANSSELINUX                     /*GAN6May06 SELinux MLS */
717 # define tseflag "e"
718 # define tsEflag "[-E mls] "
719 #else
720 # define tseflag
721 # define tsEflag
722 #endif
723         fprintf(stderr,
724                 "usage:"
725                 "\t%s -C [-cd" tseflag "H" kerbflag "lMvVy] [-b blocksize] [-D filesystem] " tsEflag"\n"
726                 "\t%s    [-f file] [-F script] [-L limit] [-s fileno]\n"
727                 "\t%s -i [-acd" tseflag "hH" kerbflag "lmMouvVy] [-A file] [-b blocksize] " tsEflag"\n"
728                 "\t%s    [-f file] [-F script] " qfaflag "[-s fileno]\n"
729 #ifdef USE_QFA
730                 "\t%s -P file [-acdhH" kerbflag "lmMuvVy] [-A file] [-b blocksize]\n"
731                 "\t%s    [-f file] [-F script] [-s fileno] [-X filelist] [file ...]\n"
732 #endif
733                 "\t%s -r [-cd" tseflag "H" kerbflag "lMuvVy] [-b blocksize] " tsEflag"\n"
734                 "\t%s    [-f file] [-F script] [-s fileno] [-T directory]\n"
735                 "\t%s -R [-cd" tseflag "H" kerbflag "lMuvVy] [-b blocksize] " tsEflag"\n"
736                 "\t%s    [-f file] [-F script] [-s fileno] [-T directory]\n"
737                 "\t%s -t [-cdhH" kerbflag "lMuvVy] [-A file] [-b blocksize]\n"
738                 "\t%s    [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n"
739                 "\t%s -x [-acd" tseflag "hH" kerbflag "lmMouvVy] [-A file] [-b blocksize] " tsEflag"\n"
740                 "\t%s    [-f file] [-F script] " qfaflag "[-s fileno] [-X filelist] [file ...]\n",
741                 __progname, white, 
742                 __progname, white, 
743 #ifdef USE_QFA
744                 __progname, white, 
745 #endif
746                 __progname, white,
747                 __progname, white, 
748                 __progname, white, 
749                 __progname, white);
750         exit(1);
751 }
752
753 /*
754  * obsolete --
755  *      Change set of key letters and ordered arguments into something
756  *      getopt(3) will like.
757  */
758 static void
759 obsolete(int *argcp, char **argvp[])
760 {
761         int argc, flags;
762         char *ap, **argv, *flagsp = NULL, **nargv, *p = NULL;
763
764         /* Setup. */
765         argv = *argvp;
766         argc = *argcp;
767
768         /* Return if no arguments or first argument has leading dash. */
769         ap = argv[1];
770         if (argc == 1 || *ap == '-')
771                 return;
772
773         /* Allocate space for new arguments. */
774         if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
775             (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
776                 err(1, "malloc args");
777
778         *nargv++ = *argv;
779         argv += 2, argc -= 2;
780
781         for (flags = 0; *ap; ++ap) {
782                 switch (*ap) {
783                 case 'A':
784                 case 'b':
785                 case 'D':
786                 case 'f':
787                 case 'F':
788                 case 'H':
789                 case 'L':
790                 case 'Q':
791                 case 's':
792                 case 'T':
793                 case 'X':
794                         if (*argv == NULL) {
795                                 warnx("option requires an argument -- %c", *ap);
796                                 usage();
797                         }
798                         if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
799                                 err(1, "malloc arg");
800                         nargv[0][0] = '-';
801                         nargv[0][1] = *ap;
802                         (void)strcpy(&nargv[0][2], *argv);
803                         ++argv;
804                         ++nargv;
805                         break;
806                 default:
807                         if (!flags) {
808                                 *p++ = '-';
809                                 flags = 1;
810                         }
811                         *p++ = *ap;
812                         break;
813                 }
814         }
815
816         /* Terminate flags. */
817         if (flags) {
818                 *p = '\0';
819                 *nargv++ = flagsp;
820         }
821
822         /* Copy remaining arguments. */
823         while ((*nargv++ = *argv++));
824
825         /* Update argument count. */
826         *argcp = nargv - *argvp - 1;
827 }
828
829 /*
830  * use_stdin --
831  *      reserve stdin for opt (avoid conflicts)
832  */
833 void
834 use_stdin(const char *opt)
835 {
836         if (stdin_opt)
837                 errx(1, "can't handle standard input for both %s and %s",
838                         stdin_opt, opt);
839         stdin_opt = opt;
840 }