]> git.wh0rd.org Git - dump.git/blob - dump/main.c
Small prototype fix.
[dump.git] / dump / 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) 1980, 1991, 1993, 1994
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.91 2004/01/27 10:46:55 stelian Exp $";
41 #endif /* not lint */
42
43 #include <config.h>
44 #include <compatlfs.h>
45 #include <ctype.h>
46 #include <compaterr.h>
47 #include <fcntl.h>
48 #include <fstab.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <errno.h>
55 #include <mntent.h>
56
57 #include <sys/param.h>
58 #include <sys/time.h>
59 #include <time.h>
60 #ifdef __linux__
61 #ifdef HAVE_EXT2FS_EXT2_FS_H
62 #include <ext2fs/ext2_fs.h>
63 #else
64 #include <linux/ext2_fs.h>
65 #endif
66 #include <ext2fs/ext2fs.h>
67 #include <sys/stat.h>
68 #include <bsdcompat.h>
69 #include <linux/fs.h>   /* for definition of BLKFLSBUF */
70 #elif defined sunos
71 #include <sys/vnode.h>
72
73 #include <ufs/inode.h>
74 #include <ufs/fs.h>
75 #else
76 #include <ufs/ufs/dinode.h>
77 #include <ufs/ffs/fs.h>
78 #endif
79
80 #include <protocols/dumprestore.h>
81
82 #include "dump.h"
83 #include "pathnames.h"
84 #include "bylabel.h"
85
86 #ifndef SBOFF
87 #define SBOFF (SBLOCK * DEV_BSIZE)
88 #endif
89
90 int abortifconnerr = 1;         /* set to 1 if lib dumprmt.o should exit on connection errors
91                                 otherwise just print a message using msg */
92 /*
93  * Dump maps used to describe what is to be dumped.
94  */
95 int     mapsize;        /* size of the state maps */
96 char    *usedinomap;    /* map of allocated inodes */
97 char    *dumpdirmap;    /* map of directories to be dumped */
98 char    *dumpinomap;    /* map of files to be dumped */
99 char    *metainomap;    /* which of the inodes in dumpinomap will get
100                            only their metadata dumped */
101
102 const char *disk;       /* name of the disk file */
103 char    tape[MAXPATHLEN];/* name of the tape file */
104 char    *tapeprefix;    /* prefix of the tape file */
105 char    *dumpdates;     /* name of the file containing dump date information*/
106 char    lastlevel;      /* dump level of previous dump */
107 char    level;          /* dump level of this dump */
108 int     zipflag;        /* which compression method */
109 int     Afile = -1;     /* archive file descriptor */
110 int     AfileActive = 1;/* Afile flag */
111 int     uflag;          /* update flag */
112 int     mflag;          /* dump metadata only if possible */
113 int     Mflag;          /* multi-volume flag */
114 int     qflag;          /* quit on errors flag */
115 int     vflag;          /* verbose flag */
116 int     breademax = 32; /* maximum number of bread errors before we quit */
117 char    *eot_script;    /* end of volume script fiag */
118 int     diskfd;         /* disk file descriptor */
119 int     tapefd;         /* tape file descriptor */
120 int     pipeout;        /* true => output to standard output */
121 int     fifoout;        /* true => output to fifo */
122 dump_ino_t curino;      /* current inumber; used globally */
123 int     newtape;        /* new tape flag */
124 int     density;        /* density in 0.1" units */
125 long    tapesize;       /* estimated tape size, blocks */
126 long    tsize;          /* tape size in 0.1" units */
127 long    asize;          /* number of 0.1" units written on current tape */
128 int     etapes;         /* estimated number of tapes */
129 int     nonodump;       /* if set, do not honor UF_NODUMP user flags */
130 int     unlimited;      /* if set, write to end of medium */
131 int     compressed;     /* if set, dump is to be compressed */
132 long long bytes_written;/* total bytes written to tape */
133 long    uncomprblks;    /* uncompressed blocks written to tape */
134 int     notify;         /* notify operator flag */
135 int     blockswritten;  /* number of blocks written on current tape */
136 int     tapeno;         /* current tape number */
137 time_t  tstart_writing; /* when started writing the first tape block */
138 time_t  tend_writing;   /* after writing the last tape block */
139 #ifdef __linux__
140 ext2_filsys fs;
141 #else
142 struct  fs *sblock;     /* the file system super block */
143 char    sblock_buf[MAXBSIZE];
144 #endif
145 long    xferrate;       /* averaged transfer rate of all volumes */
146 long    dev_bsize;      /* block size of underlying disk device */
147 int     dev_bshift;     /* log2(dev_bsize) */
148 int     tp_bshift;      /* log2(TP_BSIZE) */
149 dump_ino_t volinfo[TP_NINOS];/* which inode on which volume archive info */
150
151 #ifdef USE_QFA
152 int     gTapeposfd;
153 char    *gTapeposfile;
154 char    gTps[255];
155 int32_t gThisDumpDate;
156 #endif /* USE_QFA */
157
158 struct  dumptime *dthead;       /* head of the list version */
159 int     nddates;                /* number of records (might be zero) */
160 int     ddates_in;              /* we have read the increment file */
161 struct  dumpdates **ddatev;     /* the arrayfied version */
162
163 int     notify = 0;     /* notify operator flag */
164 int     blockswritten = 0;      /* number of blocks written on current tape */
165 int     tapeno = 0;     /* current tape number */
166 int     density = 0;    /* density in bytes/0.1" " <- this is for hilit19 */
167 int     ntrec = NTREC;  /* # blocks in each tape record */
168 int     cartridge = 0;  /* Assume non-cartridge tape */
169 #ifdef USE_QFA
170 int     tapepos = 0;    /* assume no QFA tapeposition needed by user */
171 #endif /* USE_QFA */
172 int     dokerberos = 0; /* Use Kerberos authentication */
173 long    dev_bsize = 1;  /* recalculated below */
174 long    *blocksperfiles = NULL; /* output blocks per file(s) */
175 char    *host = NULL;   /* remote host (if any) */
176 int     sizest = 0;     /* return size estimate only */
177 int     compressed = 0; /* use zlib to compress the output, compress level 1-9 */
178 long long bytes_written = 0; /* total bytes written */
179 long    uncomprblks = 0;/* uncompressed blocks written */
180
181 long smtc_errno;
182
183 #ifdef  __linux__
184 char    *__progname;
185 #endif
186
187 int     maxbsize = 1024*1024;     /* XXX MAXBSIZE from sys/param.h */
188 static long numarg __P((const char *, long, long));
189 static long *numlistarg __P((const char *, long, long));
190 static void obsolete __P((int *, char **[]));
191 static void usage __P((void));
192 static void do_exclude_from_file __P((char *));
193 static void do_exclude_ino_str __P((char *));
194 static void incompat_flags __P((int, char, char));
195
196 static char* iexclude_bitmap = NULL;            /* the inode exclude bitmap */
197 static unsigned int iexclude_bitmap_bytes = 0;  /* size of bitmap in bytes */
198
199 int
200 main(int argc, char *argv[])
201 {
202         dump_ino_t ino;
203         int dirty;
204         struct dinode *dp;
205         struct mntent *dt;
206         char *map;
207         int ch;
208         int i, anydirskipped;
209         int aflag = 0, bflag = 0, Tflag = 0, honorlevel = 1;
210         dump_ino_t maxino;
211         struct STAT statbuf;
212         dev_t filedev = 0;
213 #ifdef  __linux__
214         errcode_t retval;
215         char directory[MAXPATHLEN];
216         char pathname[MAXPATHLEN];
217 #endif
218         time_t tnow;
219         char *diskparam;
220         char *Apath = NULL;
221
222         spcl.c_label[0] = '\0';
223         spcl.c_date = time(NULL);
224
225 #ifdef __linux__
226         __progname = argv[0];
227         directory[0] = 0;
228         initialize_ext2_error_table();
229 #endif
230
231         tsize = 0;      /* Default later, based on 'c' option for cart tapes */
232         unlimited = 1;
233         eot_script = NULL;
234         if ((tapeprefix = getenv("TAPE")) == NULL)
235                 tapeprefix = _PATH_DEFTAPE;
236         dumpdates = _PATH_DUMPDATES;
237         if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
238                 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
239         level = '0';
240
241         if (argc < 2)
242                 usage();
243
244         obsolete(&argc, &argv);
245
246 #ifdef USE_QFA
247         gTapeposfd = -1;
248 #endif /* USE_QFA */
249
250         while ((ch = getopt(argc, argv,
251                             "0123456789A:aB:b:cd:D:e:E:f:F:h:I:"
252 #ifdef HAVE_BZLIB
253                             "j::"
254 #endif
255                             "L:"
256 #ifdef KERBEROS
257                             "k"
258 #endif
259                             "mMnq"
260 #ifdef USE_QFA
261                             "Q:"
262 #endif
263                             "s:ST:uvWw"
264 #ifdef HAVE_LZO
265                             "y"
266 #endif
267 #ifdef HAVE_ZLIB
268                             "z::"
269 #endif
270                             )) != -1)
271                 switch (ch) {
272                 /* dump level */
273                 case '0': case '1': case '2': case '3': case '4':
274                 case '5': case '6': case '7': case '8': case '9':
275                         level = ch;
276                         break;
277
278                 case 'A':               /* archive file */
279                         Apath = optarg;
280                         break;
281
282                 case 'a':               /* `auto-size', Write to EOM. */
283                         unlimited = 1;
284                         aflag = 1;
285                         break;
286
287                 case 'B':               /* blocks per output file */
288                         unlimited = 0;
289                         blocksperfiles = numlistarg("number of blocks per file",
290                             1L, 0L);
291                         break;
292
293                 case 'b':               /* blocks per tape write */
294                         ntrec = numarg("number of blocks per write",
295                             1L, 1048576L);
296                         if (ntrec > maxbsize/1024) {
297                                 msg("Please choose a blocksize <= %dkB\n",
298                                         maxbsize/1024);
299                                 msg("The ENTIRE dump is aborted.\n");
300                                 exit(X_STARTUP);
301                         }
302                         bflag = 1;
303                         break;
304
305                 case 'c':               /* Tape is cart. not 9-track */
306                         unlimited = 0;
307                         cartridge = 1;
308                         break;
309
310                 case 'd':               /* density, in bits per inch */
311                         unlimited = 0;
312                         density = numarg("density", 10L, 327670L) / 10;
313                         if (density >= 625 && !bflag)
314                                 ntrec = HIGHDENSITYTREC;
315                         break;
316
317                 case 'D':               /* path of dumpdates file */
318                         dumpdates = optarg;
319                         break;
320                         
321                                         /* 04-Feb-00 ILC */
322                 case 'e':               /* exclude an inode */
323                         {
324                         char *p = optarg, *q;
325                         while ((q = strchr(p, ','))) {
326                                 *q = '\0';
327                                 do_exclude_ino_str(p);
328                                 p = q + 1;
329                         }
330                         do_exclude_ino_str(p);
331                         }
332                         break;
333
334                 case 'E':               /* exclude inodes read from file */
335                         do_exclude_from_file(optarg);
336                         break;
337
338                 case 'f':               /* output file */
339                         tapeprefix = optarg;
340                         break;
341
342                 case 'F':               /* end of tape script */
343                         eot_script = optarg;
344                         break;
345
346                 case 'h':
347                         honorlevel = numarg("honor level", 0L, 10L);
348                         break;
349
350 #ifdef HAVE_BZLIB
351                 case 'j':
352                         compressed = 2;
353                         zipflag = COMPRESS_BZLIB;
354                         if (optarg)
355                                 compressed = numarg("compress level", 1L, 9L);
356                         break;
357 #endif /* HAVE_BZLIB */
358
359                 case 'I':
360                         breademax =
361                           numarg ("number of errors to ignore", 0L, 0L);
362                         break;
363
364 #ifdef KERBEROS
365                 case 'k':
366                         dokerberos = 1;
367                         break;
368 #endif
369
370                 case 'L':
371                         /*
372                          * Note that although there are LBLSIZE characters,
373                          * the last must be '\0', so the limit on strlen()
374                          * is really LBLSIZE-1.
375                          */
376                         strncpy(spcl.c_label, optarg, LBLSIZE);
377                         spcl.c_label[LBLSIZE-1] = '\0';
378                         if (strlen(optarg) > LBLSIZE-1) {
379                                 msg(
380                 "WARNING Label `%s' is larger than limit of %d characters.\n",
381                                     optarg, LBLSIZE-1);
382                                 msg("WARNING: Using truncated label `%s'.\n",
383                                     spcl.c_label);
384                         }
385                         break;
386
387                 case 'm':               /* metadata only flag */
388                         mflag = 1;
389                         break;
390
391                 case 'M':               /* multi-volume flag */
392                         Mflag = 1;
393                         break;
394
395                 case 'n':               /* notify operators */
396                         notify = 1;
397                         break;
398
399                 case 'q':
400                         qflag = 1;
401                         break;
402
403 #ifdef USE_QFA
404                 case 'Q':               /* create tapeposfile */
405                         gTapeposfile = optarg;
406                         tapepos = 1;
407                         break;
408 #endif /* USE_QFA */
409                         
410                 case 's':               /* tape size, feet */
411                         unlimited = 0;
412                         tsize = numarg("tape size", 1L, 0L) * 12 * 10;
413                         break;
414
415                 case 'S':
416                         sizest = 1;     /* return size estimate only */
417                         break;
418
419                 case 'T':               /* time of last dump */
420                         spcl.c_ddate = unctime(optarg);
421                         if (spcl.c_ddate < 0) {
422                                 msg("bad time \"%s\"\n", optarg);
423                                 msg("The ENTIRE dump is aborted.\n");
424                                 exit(X_STARTUP);
425                         }
426                         Tflag = 1;
427                         lastlevel = '?';
428                         break;
429
430                 case 'u':               /* update dumpdates */
431                         uflag = 1;
432                         break;
433
434                 case 'v':               /* verbose */
435                         vflag = 1;
436                         break;
437
438                 case 'W':               /* what to do */
439                 case 'w':
440                         lastdump(ch);
441                         exit(X_FINOK);  /* do nothing else */
442 #ifdef HAVE_LZO
443                 case 'y':
444                         compressed = 2;
445                         zipflag = COMPRESS_LZO;
446                         break;
447 #endif /* HAVE_LZO */
448
449 #ifdef HAVE_ZLIB
450                 case 'z':
451                         compressed = 2;
452                         zipflag = COMPRESS_ZLIB;
453                         if (optarg)
454                                 compressed = numarg("compress level", 1L, 9L);
455                         break;
456 #endif /* HAVE_ZLIB */
457
458                 default:
459                         usage();
460                 }
461         argc -= optind;
462         argv += optind;
463
464         if (argc < 1) {
465                 msg("Must specify disk or filesystem\n");
466                 msg("The ENTIRE dump is aborted.\n");
467                 exit(X_STARTUP);
468         }
469         diskparam = *argv++;
470         if (strlen(diskparam) >= MAXPATHLEN) {
471                 msg("Disk or filesystem name too long: %s\n", diskparam);
472                 msg("The ENTIRE dump is aborted.\n");
473                 exit(X_STARTUP);
474         }
475         argc--;
476         incompat_flags(Tflag && uflag, 'T', 'u');
477         incompat_flags(aflag && blocksperfiles, 'a', 'B');
478         incompat_flags(aflag && cartridge, 'a', 'c');
479         incompat_flags(aflag && density, 'a', 'd');
480         incompat_flags(aflag && tsize, 'a', 's');
481
482         if (strcmp(tapeprefix, "-") == 0) {
483                 pipeout++;
484                 tapeprefix = "standard output";
485         }
486
487         if (blocksperfiles && !compressed)
488                 for (i = 1; i <= *blocksperfiles; i++)
489                         blocksperfiles[i] = blocksperfiles[i] / ntrec * ntrec; /* round down */
490         else if (!unlimited) {
491                 /*
492                  * Determine how to default tape size and density
493                  *
494                  *              density                         tape size
495                  * 9-track      1600 bpi (160 bytes/.1")        2300 ft.
496                  * 9-track      6250 bpi (625 bytes/.1")        2300 ft.
497                  * cartridge    8000 bpi (100 bytes/.1")        1700 ft.
498                  *                                              (450*4 - slop)
499                  * hilit19 hits again: "
500                  */
501                 if (density == 0)
502                         density = cartridge ? 100 : 160;
503                 if (tsize == 0)
504                         tsize = cartridge ? 1700L*120L : 2300L*120L;
505         }
506
507         {
508                 int i;
509                 char *n;
510
511                 if ((n = strchr(tapeprefix, ':'))) {
512                         for (i = 0; i < (n - tapeprefix); i++) {
513                                 if (tapeprefix[i] == '/')
514                                         break;
515                         }
516                         if (tapeprefix[i] != '/') {
517                                 host = tapeprefix;
518                                 tapeprefix = strchr(host, ':');
519                                 *tapeprefix++ = '\0';
520 #ifdef RDUMP
521                                 if (index(tapeprefix, '\n')) {
522                                         msg("invalid characters in tape\n");
523                                         msg("The ENTIRE dump is aborted.\n");
524                                         exit(X_STARTUP);
525                                 }
526                                 if (rmthost(host) == 0)
527                                         exit(X_STARTUP);
528 #else
529                                 msg("remote dump not enabled\n");
530                                 msg("The ENTIRE dump is aborted.\n");
531                                 exit(X_STARTUP);
532 #endif
533                         }
534                 }
535         }
536
537         (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
538         if (Apath && (Afile = open(Apath, O_WRONLY|O_CREAT|O_TRUNC,
539                                    S_IRUSR | S_IWUSR | S_IRGRP |
540                                    S_IWGRP | S_IROTH | S_IWOTH)) < 0) {
541                 msg("Cannot open %s for writing: %s\n",
542                     optarg, strerror(errno));
543                 msg("The ENTIRE dump is aborted.\n");
544                 exit(X_STARTUP);
545         }
546
547         if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
548                 signal(SIGHUP, sig);
549         if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
550                 signal(SIGTRAP, sig);
551         if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
552                 signal(SIGFPE, sig);
553         if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
554                 signal(SIGBUS, sig);
555         if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
556                 signal(SIGSEGV, sig);
557         if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
558                 signal(SIGTERM, sig);
559         if (signal(SIGINT, interrupt) == SIG_IGN)
560                 signal(SIGINT, SIG_IGN);
561 #ifdef SIGXCPU
562         signal(SIGXCPU, SIG_IGN);
563 #endif /* SIGXCPU */
564 #ifdef SIGXFSZ
565         signal(SIGXFSZ, SIG_IGN);
566 #endif /* SIGXFSZ */
567
568         set_operators();        /* /etc/group snarfed */
569         getfstab();             /* /etc/fstab snarfed */
570
571         /*
572          *      disk may end in / and this can confuse
573          *      fstabsearch.
574          */
575         i = strlen(diskparam) - 1;
576         if (i > 1 && diskparam[i] == '/')
577                 if (!(i == 6 && !strcmp(diskparam, "LABEL=/")))
578                         diskparam[i] = '\0';
579
580         disk = get_device_name(diskparam);
581         if (!disk) {            /* null means the disk is some form
582                                    of LABEL= or UID= but it was not
583                                    found */
584                 msg("Cannot find a disk having %s\n", diskparam);
585                 msg("The ENTIRE dump is aborted.\n");
586                 exit(X_STARTUP);
587         }
588         /*
589          *      disk can be either the full special file name,
590          *      the suffix of the special file name,
591          *      the special name missing the leading '/',
592          *      the file system name with or without the leading '/'.
593          */
594         if ((dt = fstabsearch(disk)) != NULL) {
595                 /* if found then only one parameter (i.e. partition)
596                  * is allowed */
597                 if (argc >= 1) {
598                         (void)fprintf(stderr, "Unknown arguments to dump:");
599                         while (argc--)
600                                 (void)fprintf(stderr, " %s", *argv++);
601                         (void)fprintf(stderr, "\n");
602                         msg("The ENTIRE dump is aborted.\n");
603                         exit(X_STARTUP);
604                 }
605                 disk = rawname(dt->mnt_fsname);
606                 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
607                 (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
608         } else {
609 #ifdef  __linux__
610 #ifdef  HAVE_REALPATH
611                 if (realpath(disk, pathname) == NULL)
612 #endif
613                         strcpy(pathname, disk);
614                 /*
615                  * The argument could be now a mountpoint of
616                  * a filesystem specified in fstab. Search for it.
617                  */
618                 if ((dt = fstabsearch(pathname)) != NULL) {
619                         disk = rawname(dt->mnt_fsname);
620                         (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
621                         (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
622                 } else {
623                         /*
624                          * The argument was not found in the fstab
625                          * assume that this is a subtree and search for it
626                          */
627                         dt = fstabsearchdir(pathname, directory);
628                         if (dt != NULL) {
629                                 char name[MAXPATHLEN];
630                                 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
631                                 (void)snprintf(name, sizeof(name), "%s (dir %s)",
632                                               dt->mnt_dir, directory);
633                                 (void)strncpy(spcl.c_filesys, name, NAMELEN);
634                                 disk = rawname(dt->mnt_fsname);
635                         } else {
636                                 (void)strncpy(spcl.c_dev, disk, NAMELEN);
637                                 (void)strncpy(spcl.c_filesys, "an unlisted file system",
638                                     NAMELEN);
639                         }
640                 }
641 #else
642                 (void)strncpy(spcl.c_dev, disk, NAMELEN);
643                 (void)strncpy(spcl.c_filesys, "an unlisted file system",
644                     NAMELEN);
645 #endif
646         }
647
648         if (directory[0] != 0) {
649                 if (level != '0') {
650                         msg("Only level 0 dumps are allowed on a subdirectory\n");
651                         msg("The ENTIRE dump is aborted.\n");
652                         exit(X_STARTUP);
653                 }
654                 if (uflag) {
655                         msg("You can't update the dumpdates file when dumping a subdirectory\n");
656                         msg("The ENTIRE dump is aborted.\n");
657                         exit(X_STARTUP);
658                 }
659         }
660         spcl.c_dev[NAMELEN-1] = '\0';
661         spcl.c_filesys[NAMELEN-1] = '\0';
662         (void)gethostname(spcl.c_host, NAMELEN);
663         spcl.c_host[NAMELEN-1] = '\0';
664         spcl.c_level = level - '0';
665         spcl.c_type = TS_TAPE;
666         if (!Tflag)
667                 getdumptime(uflag);             /* dumpdates snarfed */
668
669         if (spcl.c_ddate == 0 && spcl.c_level) {
670                 msg("WARNING: There is no inferior level dump on this filesystem\n"); 
671                 msg("WARNING: Assuming a level 0 dump by default\n");
672                 level = '0';
673                 spcl.c_level = 0;
674         }
675
676         if (Mflag)
677                 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno + 1);
678         else
679                 strncpy(tape, tapeprefix, MAXPATHLEN);
680         tape[MAXPATHLEN - 1] = '\0';
681
682         if (!pipeout) {
683                 if (STAT(tape, &statbuf) != -1)
684                         fifoout= statbuf.st_mode & S_IFIFO;
685         }
686
687         if (!sizest) {
688
689                 msg("Date of this level %c dump: %s", level,
690                     ctime4(&spcl.c_date));
691 #ifdef USE_QFA
692                 gThisDumpDate = spcl.c_date;
693 #endif
694                 if (spcl.c_ddate)
695                         msg("Date of last level %c dump: %s", lastlevel,
696                             ctime4(&spcl.c_ddate));
697                 msg("Dumping %s (%s) ", disk, spcl.c_filesys);
698                 if (host)
699                         msgtail("to %s on host %s\n", tape, host);
700                 else
701                         msgtail("to %s\n", tape);
702         } /* end of size estimate */
703
704 #ifdef  __linux__
705         if ((diskfd = OPEN(disk, O_RDONLY)) < 0) {
706                 msg("Cannot open %s\n", disk);
707                 msg("The ENTIRE dump is aborted.\n");
708                 exit(X_STARTUP);
709         }
710 #ifdef BLKFLSBUF
711         (void)ioctl(diskfd, BLKFLSBUF, 0);
712 #endif
713         retval = dump_fs_open(disk, &fs);
714         if (retval) {
715                 com_err(disk, retval, "while opening filesystem");
716                 if (retval == EXT2_ET_REV_TOO_HIGH)
717                         msg("Get a newer version of dump!\n");
718                 msg("The ENTIRE dump is aborted.\n");
719                 exit(X_STARTUP);
720         }
721         if (fs->super->s_rev_level > DUMP_CURRENT_REV) {
722                 com_err(disk, retval, "while opening filesystem");
723                 msg("Get a newer version of dump!\n");
724                 msg("The ENTIRE dump is aborted.\n");
725                 exit(X_STARTUP);
726         }
727         /* if no user label specified, use ext2 filesystem label if available */
728         if (spcl.c_label[0] == '\0') {
729                 const char *lbl;
730                 if ( (lbl = get_device_label(disk)) != NULL) {
731                         strncpy(spcl.c_label, lbl, LBLSIZE);
732                         spcl.c_label[LBLSIZE-1] = '\0';
733                 }
734                 else
735                         strcpy(spcl.c_label, "none");   /* safe strcpy. */
736         }
737         sync();
738         dev_bsize = DEV_BSIZE;
739         dev_bshift = ffs(dev_bsize) - 1;
740         if (dev_bsize != (1 << dev_bshift))
741                 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
742         tp_bshift = ffs(TP_BSIZE) - 1;
743         if (TP_BSIZE != (1 << tp_bshift))
744                 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
745         maxino = fs->super->s_inodes_count + 1;
746         spcl.c_flags |= DR_NEWINODEFMT;
747 #else   /* __linux __*/
748         if ((diskfd = open(disk, O_RDONLY)) < 0) {
749                 msg("Cannot open %s\n", disk);
750                 msg("The ENTIRE dump is aborted.\n");
751                 exit(X_STARTUP);
752         }
753         sync();
754         sblock = (struct fs *)sblock_buf;
755         bread(SBOFF, (char *) sblock, SBSIZE);
756         if (sblock->fs_magic != FS_MAGIC)
757                 quit("bad sblock magic number\n");
758         dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
759         dev_bshift = ffs(dev_bsize) - 1;
760         if (dev_bsize != (1 << dev_bshift))
761                 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
762         tp_bshift = ffs(TP_BSIZE) - 1;
763         if (TP_BSIZE != (1 << tp_bshift))
764                 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
765 #ifdef FS_44INODEFMT
766         if (sblock->fs_inodefmt >= FS_44INODEFMT)
767                 spcl.c_flags |= DR_NEWINODEFMT;
768 #endif
769         maxino = sblock->fs_ipg * sblock->fs_ncg;
770 #endif  /* __linux__ */
771         mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
772         usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
773         dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
774         dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
775         metainomap = (char *)calloc((unsigned) mapsize, sizeof(char));
776         if (usedinomap == NULL || dumpdirmap == NULL || 
777             dumpinomap == NULL || metainomap == NULL)
778                 quit("out of memory allocating inode maps\n");
779         tapesize = 2 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
780
781         nonodump = spcl.c_level < honorlevel;
782
783         if (!sizest) {
784                 msg("Label: %s\n", spcl.c_label);
785                 
786                 msg("Writing %d Kilobyte records\n", ntrec);
787
788                 if (compressed) {
789                         if (zipflag == COMPRESS_LZO) 
790                                 msg("Compressing output (lzo)\n");
791                         else
792                                 msg("Compressing output at compression level %d (%s)\n", 
793                                         compressed, zipflag == COMPRESS_ZLIB ? "zlib" : "bzlib");
794                 }
795         }
796
797 #if defined(SIGINFO)
798         (void)signal(SIGINFO, statussig);
799 #endif
800
801         if (!sizest)
802                 msg("mapping (Pass I) [regular files]\n");
803 #ifdef  __linux__
804         if (directory[0] == 0)
805                 anydirskipped = mapfiles(maxino, &tapesize);
806         else {
807                 if (LSTAT(pathname, &statbuf) == -1) {
808                         msg("File cannot be accessed (%s).\n", pathname);
809                         msg("The ENTIRE dump is aborted.\n");
810                         exit(X_STARTUP);
811                 }
812                 filedev = statbuf.st_dev;
813                 if (!(statbuf.st_mode & S_IFDIR))       /* is a file */
814                         anydirskipped = maponefile(maxino, &tapesize, 
815                                                    directory);
816                 else
817                         anydirskipped = mapfilesfromdir(maxino, &tapesize, 
818                                                         directory);
819         }
820         while (argc--) {
821                 int anydirskipped2;
822                 char *p = *argv;
823                 /* check if file is available */
824                 if (LSTAT(p, &statbuf) == -1) {
825                         msg("File cannot be accessed (%s).\n", p);
826                         msg("The ENTIRE dump is aborted.\n");
827                         exit(X_STARTUP);
828                 }
829                 /* check if file is on same unix partiton as the first 
830                  * argument */
831                 if (statbuf.st_dev != filedev) {
832                         msg("Files are not on same file system (%s).\n", p);
833                         msg("The ENTIRE dump is aborted.\n");
834                         exit(X_STARTUP);
835                 }
836                 /* check if file is a directory */
837                 if (!(statbuf.st_mode & S_IFDIR))
838                         anydirskipped2 = maponefile(maxino, &tapesize, 
839                                                     p+strlen(dt->mnt_dir));
840                 else
841                         /* read directory inodes.
842                          * NOTE: nested directories are not recognized 
843                          * so inodes may be umped twice!
844                          */
845                         anydirskipped2 = mapfilesfromdir(maxino, &tapesize, 
846                                                          p+strlen(dt->mnt_dir));
847                 if (!anydirskipped)
848                         anydirskipped = anydirskipped2;
849                 argv++;
850         }               
851 #else
852         anydirskipped = mapfiles(maxino, &tapesize);
853 #endif
854
855         if (!sizest)
856                 msg("mapping (Pass II) [directories]\n");
857         while (anydirskipped) {
858                 anydirskipped = mapdirs(maxino, &tapesize);
859         }
860
861         if (sizest) {
862                 printf("%.0f\n", ((double)tapesize + 1 + ntrec) * TP_BSIZE);
863                 exit(X_FINOK);
864         } /* stop here for size estimate */
865
866         if (pipeout || unlimited) {
867                 tapesize += 1 + ntrec;  /* 1 map header + trailer blocks */
868                 msg("estimated %ld blocks.\n", tapesize);
869         } else {
870                 double fetapes;
871
872                 if (blocksperfiles) {
873                         long tapesize_left;
874
875                         tapesize_left = tapesize;
876                         fetapes = 0;
877                         for (i = 1; i < *blocksperfiles && tapesize_left; i++) {
878                                 fetapes++;
879                                 if (tapesize_left > blocksperfiles[i])
880                                         tapesize_left -= blocksperfiles[i];
881                                 else
882                                         tapesize_left = 0;
883                         }
884                         if (tapesize_left)
885                                 fetapes += (double)tapesize_left / blocksperfiles[*blocksperfiles];
886                 } else if (cartridge) {
887                         /* Estimate number of tapes, assuming streaming stops at
888                            the end of each block written, and not in mid-block.
889                            Assume no erroneous blocks; this can be compensated
890                            for with an artificially low tape size. */
891                         fetapes =
892                         (         (double) tapesize     /* blocks */
893                                 * TP_BSIZE      /* bytes/block */
894                                 * (1.0/density) /* 0.1" / byte " */
895                           +
896                                   (double) tapesize     /* blocks */
897                                 * (1.0/ntrec)   /* streaming-stops per block */
898                                 * 15.48         /* 0.1" / streaming-stop " */
899                         ) * (1.0 / tsize );     /* tape / 0.1" " */
900                 } else {
901                         /* Estimate number of tapes, for old fashioned 9-track
902                            tape */
903                         int tenthsperirg = (density == 625) ? 3 : 7;
904                         fetapes =
905                         (         (double) tapesize     /* blocks */
906                                 * TP_BSIZE      /* bytes / block */
907                                 * (1.0/density) /* 0.1" / byte " */
908                           +
909                                   (double) tapesize     /* blocks */
910                                 * (1.0/ntrec)   /* IRG's / block */
911                                 * tenthsperirg  /* 0.1" / IRG " */
912                         ) * (1.0 / tsize );     /* tape / 0.1" " */
913                 }
914                 etapes = fetapes;               /* truncating assignment */
915                 etapes++;
916                 /* count the dumped inodes map on each additional tape */
917                 tapesize += (etapes - 1) *
918                         (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
919                 tapesize += etapes + ntrec;     /* headers + trailer blks */
920                 msg("estimated %ld blocks on %3.2f tape(s).\n",
921                     tapesize, fetapes);
922         }
923
924 #ifdef USE_QFA
925         if (tapepos) {
926                 msg("writing QFA positions to %s\n", gTapeposfile);
927                 if ((gTapeposfd = open(gTapeposfile,
928                                        O_WRONLY|O_CREAT|O_TRUNC,
929                                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
930                                        | S_IROTH | S_IWOTH)) < 0)
931                         quit("can't open tapeposfile\n");
932                 /* print QFA-file header */
933                 snprintf(gTps, sizeof(gTps), "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
934                 gTps[sizeof(gTps) - 1] = '\0';
935                 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
936                         quit("can't write tapeposfile\n");
937                 sprintf(gTps, "ino\ttapeno\ttapepos\n");
938                 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
939                         quit("can't write tapeposfile\n");
940         }
941 #endif /* USE_QFA */
942
943         /*
944          * Allocate tape buffer.
945          */
946         if (!alloctape())
947                 quit(
948         "can't allocate tape buffers - try a smaller blocking factor.\n");
949
950         startnewtape(1);
951         tstart_writing = time(NULL);
952         dumpmap(usedinomap, TS_CLRI, maxino - 1);
953
954         msg("dumping (Pass III) [directories]\n");
955         dirty = 0;              /* XXX just to get gcc to shut up */
956         for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
957                 if (((ino - 1) % NBBY) == 0)    /* map is offset by 1 */
958                         dirty = *map++;
959                 else
960                         dirty >>= 1;
961                 if ((dirty & 1) == 0)
962                         continue;
963                 /*
964                  * Skip directory inodes deleted and maybe reallocated
965                  */
966                 dp = getino(ino);
967                 if ((dp->di_mode & IFMT) != IFDIR)
968                         continue;
969 #ifdef  __linux__
970                 /*
971                  * Skip directory inodes deleted and not yes reallocated...
972                  */
973                 if (dp->di_nlink == 0 || dp->di_dtime != 0)
974                         continue;
975                 if (vflag)
976                         msg("dumping directory inode %lu\n", ino);
977                 (void)dumpdirino(dp, ino);
978 #else
979                 (void)dumpino(dp, ino);
980 #endif
981         }
982
983         msg("dumping (Pass IV) [regular files]\n");
984         for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
985                 if (((ino - 1) % NBBY) == 0)    /* map is offset by 1 */
986                         dirty = *map++;
987                 else
988                         dirty >>= 1;
989                 if ((dirty & 1) == 0)
990                         continue;
991                 /*
992                  * Skip inodes deleted and reallocated as directories.
993                  */
994                 dp = getino(ino);
995                 if ((dp->di_mode & IFMT) == IFDIR)
996                         continue;
997 #ifdef __linux__
998                 /*
999                  * No need to check here for deleted and not yet reallocated
1000                  * inodes since this is done in dumpino().
1001                  */
1002 #endif
1003                 if (vflag) {
1004                         if (mflag && TSTINO(ino, metainomap))
1005                                 msg("dumping regular inode %lu (meta only)\n", ino);
1006                         else
1007                                 msg("dumping regular inode %lu\n", ino);
1008                 }
1009                 (void)dumpino(dp, ino, mflag && TSTINO(ino, metainomap));
1010         }
1011
1012         tend_writing = time(NULL);
1013         spcl.c_type = TS_END;
1014
1015         if (Afile >= 0) {
1016                 volinfo[1] = ROOTINO;
1017                 memcpy(spcl.c_inos, volinfo, TP_NINOS * sizeof(dump_ino_t));
1018                 spcl.c_flags |= DR_INODEINFO;
1019         }
1020
1021         /*
1022          * Finish off the current tape record with trailer blocks, to ensure
1023          * at least the data in the last partial record makes it to tape.
1024          * Also make sure we write at least 1 trailer block.
1025          */
1026         for (i = ntrec - (spcl.c_tapea % ntrec); i; --i)
1027                 writeheader(maxino - 1);
1028
1029         tnow = trewind();
1030
1031         if (pipeout || fifoout)
1032                 msg("%ld blocks (%.2fMB)\n", spcl.c_tapea,
1033                         ((double)spcl.c_tapea * TP_BSIZE / 1048576));
1034         else
1035                 msg("%ld blocks (%.2fMB) on %d volume(s)\n",
1036                     spcl.c_tapea, 
1037                     ((double)spcl.c_tapea * TP_BSIZE / 1048576),
1038                     spcl.c_volume);
1039
1040         /* report dump performance, avoid division by zero */
1041         if (tend_writing - tstart_writing == 0)
1042                 msg("finished in less than a second\n");
1043         else
1044                 msg("finished in %d seconds, throughput %d kBytes/sec\n",
1045                     tend_writing - tstart_writing,
1046                     spcl.c_tapea / (tend_writing - tstart_writing));
1047
1048         putdumptime();
1049         msg("Date of this level %c dump: %s", level,
1050                 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
1051         msg("Date this dump completed:  %s", ctime(&tnow));
1052
1053         msg("Average transfer rate: %ld kB/s\n", xferrate / tapeno);
1054         if (compressed) {
1055                 long tapekb = bytes_written / 1024;
1056                 double rate = .0005 + (double) spcl.c_tapea / tapekb;
1057                 msg("Wrote %ldkB uncompressed, %ldkB compressed, %1.3f:1\n",
1058                         spcl.c_tapea, tapekb, rate);
1059         }
1060
1061         if (Afile >= 0)
1062                 msg("Archiving dump to %s\n", Apath);
1063
1064         broadcast("DUMP IS DONE!\7\7\n");
1065         msg("DUMP IS DONE\n");
1066         Exit(X_FINOK);
1067         /* NOTREACHED */
1068         return 0;       /* gcc - shut up */
1069 }
1070
1071 static void
1072 usage(void)
1073 {
1074         char white[MAXPATHLEN];
1075         const char *ext2ver, *ext2date;
1076
1077         memset(white, ' ', MAXPATHLEN);
1078         white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
1079
1080 #ifdef __linux__
1081         ext2fs_get_library_version(&ext2ver, &ext2date);
1082         fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
1083                 __progname, _DUMP_VERSION, ext2ver, ext2date);
1084 #else
1085         fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
1086 #endif
1087         fprintf(stderr,
1088                 "usage:\t%s [-0123456789ac"
1089 #ifdef KERBEROS
1090                 "k"
1091 #endif
1092                 "mMnqSuv"
1093                 "] [-A file] [-B records] [-b blocksize]\n"
1094                 "\t%s [-d density] [-D file] [-e inode#,inode#,...] [-E file]\n"
1095                 "\t%s [-f file] [-h level] [-I nr errors] "
1096 #ifdef HAVE_BZLIB
1097                 "[-j zlevel] "
1098 #endif
1099 #ifdef USE_QFA
1100                 "[-Q file]\n"
1101 #endif
1102                 "\t%s [-s feet] "
1103                 "[-T date] "
1104 #ifdef HAVE_LZO
1105                 "[-y] "
1106 #endif
1107 #ifdef HAVE_ZLIB
1108                 "[-z zlevel] "
1109 #endif
1110                 "filesystem\n"
1111                 "\t%s [-W | -w]\n", 
1112                 __progname, white, white, white, __progname);
1113         exit(X_STARTUP);
1114 }
1115
1116 /*
1117  * Pick up a numeric argument.  It must be nonnegative and in the given
1118  * range (except that a vmax of 0 means unlimited).
1119  */
1120 static long
1121 numarg(const char *meaning, long vmin, long vmax)
1122 {
1123         char *p;
1124         long val;
1125
1126         val = strtol(optarg, &p, 10);
1127         if (*p)
1128                 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
1129         if (val < vmin || (vmax && val > vmax))
1130                 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
1131         return (val);
1132 }
1133
1134 /*
1135  * The same as numarg, just that it expects a comma separated list of numbers
1136  * and returns an array of longs with the first element containing the number
1137  * values in that array.
1138  */
1139 static long *
1140 numlistarg(const char *meaning, long vmin, long vmax)
1141 {
1142         char *p;
1143         long *vals,*curval;
1144         long valnum;
1145
1146         p = optarg;
1147         vals = NULL;
1148         valnum = 0;
1149         do {
1150                 valnum++;
1151                 if ( !(vals = realloc(vals, (valnum + 1) * sizeof(*vals))) )
1152                         errx(X_STARTUP, "allocating memory failed");
1153                 curval = vals + valnum;
1154                 *curval = strtol(p, &p, 10);
1155                 if (*p && *p!=',')
1156                         errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
1157                 if (*curval < vmin || (vmax && *curval > vmax))
1158                         errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
1159                 *vals = valnum;
1160                 if (*p) p++;
1161         } while(*p);
1162         return (vals);
1163 }
1164
1165 void
1166 sig(int signo)
1167 {
1168         switch(signo) {
1169         case SIGALRM:
1170         case SIGBUS:
1171         case SIGFPE:
1172         case SIGHUP:
1173         case SIGTERM:
1174         case SIGTRAP:
1175                 if (pipeout || fifoout)
1176                         quit("Signal on pipe: cannot recover\n");
1177                 msg("Rewriting attempted as response to unknown signal: %d.\n", signo);
1178                 (void)fflush(stderr);
1179                 (void)fflush(stdout);
1180                 close_rewind();
1181                 exit(X_REWRITE);
1182                 /* NOTREACHED */
1183         case SIGSEGV:
1184                 msg("SIGSEGV: ABORTING!\n");
1185                 (void)signal(SIGSEGV, SIG_DFL);
1186                 (void)kill(0, SIGSEGV);
1187                 /* NOTREACHED */
1188         }
1189 }
1190
1191 const char *
1192 rawname(const char *cp)
1193 {
1194 #ifdef  __linux__
1195         return cp;
1196 #else   /* __linux__ */
1197         static char rawbuf[MAXPATHLEN];
1198         char *dp = strrchr(cp, '/');
1199
1200         if (dp == NULL)
1201                 return (NULL);
1202         (void)strncpy(rawbuf, cp, min(dp-cp, MAXPATHLEN - 1));
1203         rawbuf[min(dp-cp, MAXPATHLEN-1)] = '\0';
1204         (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
1205         (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
1206         return (rawbuf);
1207 #endif  /* __linux__ */
1208 }
1209
1210 /*
1211  * obsolete --
1212  *      Change set of key letters and ordered arguments into something
1213  *      getopt(3) will like.
1214  */
1215 static void
1216 obsolete(int *argcp, char **argvp[])
1217 {
1218         int argc, flags;
1219         char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL;
1220
1221         /* Setup. */
1222         argv = *argvp;
1223         argc = *argcp;
1224
1225         /* Return if no arguments or first argument has leading dash. */
1226         ap = argv[1];
1227         if (argc == 1 || *ap == '-')
1228                 return;
1229
1230         /* Allocate space for new arguments. */
1231         if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
1232             (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
1233                 err(X_STARTUP, "malloc new args");
1234
1235         *nargv++ = *argv;
1236         argv += 2;
1237
1238         for (flags = 0; *ap; ++ap) {
1239                 switch (*ap) {
1240                 case 'A':
1241                 case 'B':
1242                 case 'b':
1243                 case 'd':
1244                 case 'D':
1245                 case 'e':
1246                 case 'E':
1247                 case 'f':
1248                 case 'F':
1249                 case 'h':
1250                 case 'L':
1251                 case 'Q':
1252                 case 's':
1253                 case 'T':
1254                         if (*argv == NULL) {
1255                                 warnx("option requires an argument -- %c", *ap);
1256                                 usage();
1257                         }
1258                         if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
1259                                 err(X_STARTUP, "malloc arg");
1260                         nargv[0][0] = '-';
1261                         nargv[0][1] = *ap;
1262                         (void)strcpy(&nargv[0][2], *argv);
1263                         ++argv;
1264                         ++nargv;
1265                         break;
1266                 default:
1267                         if (!flags) {
1268                                 *p++ = '-';
1269                                 flags = 1;
1270                         }
1271                         *p++ = *ap;
1272                         break;
1273                 }
1274         }
1275
1276         /* Terminate flags. */
1277         if (flags) {
1278                 *p = '\0';
1279                 *nargv++ = flagsp;
1280         }
1281
1282         /* Copy remaining arguments. */
1283         while ((*nargv++ = *argv++));
1284
1285         /* Update argument count. */
1286         *argcp = nargv - *argvp - 1;
1287 }
1288
1289 /*
1290  * This tests whether an inode is in the exclude bitmap
1291  */
1292 int
1293 exclude_ino(dump_ino_t ino)
1294 {
1295         /* if the inode exclude bitmap exists and covers given inode */
1296         if (iexclude_bitmap && iexclude_bitmap_bytes > ino / 8) {
1297                 /* then check this inode against it */
1298                 int idx = iexclude_bitmap[ino / 8];
1299                 if (idx & (1 << (ino % 8)))
1300                         return 1;
1301         }
1302         return 0;
1303 }
1304
1305 /*
1306  * This adds an inode to the exclusion bitmap if it isn't already there
1307  */
1308 void
1309 do_exclude_ino(dump_ino_t ino, const char *reason)
1310 {
1311         if (exclude_ino(ino))
1312                 return;
1313
1314         if (vflag) {
1315                 if (reason)
1316                         msg("Excluding inode %u (%s) from dump\n", ino, reason);
1317                 else
1318                         msg("Excluding inode %u from dump\n", ino);
1319         }
1320
1321         /* check for enough mem; initialize */
1322         if ((ino/8 + 1) > iexclude_bitmap_bytes) {
1323                 if (iexclude_bitmap_bytes == 0) {
1324                         unsigned int j;
1325                         iexclude_bitmap_bytes = 2 * (ino/8 + 1);
1326                         iexclude_bitmap = (char*) malloc(iexclude_bitmap_bytes);
1327                         if (iexclude_bitmap == NULL) {
1328                                 msg("allocating memory failed\n");
1329                                 exit(X_STARTUP);
1330                         }
1331                         for (j = 0; j < iexclude_bitmap_bytes; j++)
1332                                 iexclude_bitmap[j] = 0;
1333                 }
1334                 else {
1335                         unsigned int oldsize = iexclude_bitmap_bytes;
1336                         iexclude_bitmap_bytes *= 
1337                                 (ino / 8 + 1) / iexclude_bitmap_bytes + 1;
1338                         iexclude_bitmap = (char*) realloc(iexclude_bitmap, 
1339                                 iexclude_bitmap_bytes);
1340                         if (iexclude_bitmap == NULL) {
1341                                 msg("allocating memory failed\n");
1342                                 exit(X_STARTUP);
1343                         }
1344                         for( ; oldsize < iexclude_bitmap_bytes; oldsize++)
1345                                 iexclude_bitmap[oldsize] = 0;
1346                 }
1347         }
1348                 
1349         iexclude_bitmap[ino / 8] |= 1 << (ino % 8);
1350 }
1351
1352 static void
1353 do_exclude_ino_str(char * ino) {
1354         char *r;
1355         unsigned long inod;
1356
1357         inod = strtoul(ino, &r, 10);
1358         if (( *r != '\0' && !isspace(*r) ) || inod <= ROOTINO) {
1359                 msg("Invalid inode argument %s\n", ino);
1360                 msg("The ENTIRE dump is aborted.\n");
1361                 exit(X_STARTUP);
1362         }
1363         do_exclude_ino(inod, NULL);
1364 }
1365
1366 /*
1367  * This reads a file containing one inode number per line and exclude them all
1368  */
1369 static void 
1370 do_exclude_from_file(char *file) {
1371         FILE *f;
1372         char *p, fname[MAXPATHLEN];
1373         
1374
1375         if (!( f = fopen(file, "r")) ) {
1376                 msg("Cannot open file for reading: %s\n", file);
1377                 msg("The ENTIRE dump is aborted.\n");
1378                 exit(X_STARTUP);
1379         }
1380         while (( p = fgets(fname, MAXPATHLEN, f))) {
1381                 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */
1382                         *(p + strlen(p) - 1) = '\0';
1383                 if ( !*p ) /* skip empty lines */
1384                         continue;
1385                 do_exclude_ino_str(p);
1386         }
1387         fclose(f);
1388 }
1389
1390 static void incompat_flags(int cond, char flag1, char flag2) {
1391         if (cond) {
1392                 msg("You cannot use the %c and %c flags together.\n", 
1393                     flag1, flag2);
1394                 msg("The ENTIRE dump is aborted.\n");
1395                 exit(X_STARTUP);
1396         }
1397 }