]> git.wh0rd.org Git - dump.git/blob - dump/main.c
Regenerate configure.
[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.99 2011/06/10 13:41:41 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 #include <linux/types.h>
62 #ifdef HAVE_EXT2FS_EXT2_FS_H
63 #include <ext2fs/ext2_fs.h>
64 #else
65 #include <linux/ext2_fs.h>
66 #endif
67 #include <ext2fs/ext2fs.h>
68 #include <sys/stat.h>
69 #include <bsdcompat.h>
70 #include <linux/fs.h>   /* for definition of BLKFLSBUF */
71 #elif defined sunos
72 #include <sys/vnode.h>
73
74 #include <ufs/inode.h>
75 #include <ufs/fs.h>
76 #else
77 #include <ufs/ufs/dinode.h>
78 #include <ufs/ffs/fs.h>
79 #endif
80
81 #include <protocols/dumprestore.h>
82
83 #include "dump.h"
84 #include "pathnames.h"
85 #include "bylabel.h"
86
87 #include "transformation.h"
88 #include "indexer.h"
89
90 #ifndef SBOFF
91 #define SBOFF (SBLOCK * DEV_BSIZE)
92 #endif
93
94 int abortifconnerr = 1;         /* set to 1 if lib dumprmt.o should exit on connection errors
95                                 otherwise just print a message using msg */
96 /*
97  * Dump maps used to describe what is to be dumped.
98  */
99 int     mapsize;        /* size of the state maps */
100 char    *usedinomap;    /* map of allocated inodes */
101 char    *dumpdirmap;    /* map of directories to be dumped */
102 char    *dumpinomap;    /* map of files to be dumped */
103 char    *metainomap;    /* which of the inodes in dumpinomap will get
104                            only their metadata dumped */
105
106 const char *disk;       /* name of the disk file */
107 char    tape[MAXPATHLEN];/* name of the tape file */
108 char    *tapeprefix;    /* prefix of the tape file */
109 char    *dumpdates;     /* name of the file containing dump date information*/
110 char    lastlevel[NUM_STR_SIZE];/* dump level of previous dump */
111 char    level[NUM_STR_SIZE];/* dump level of this dump */
112 int     zipflag;        /* which compression method */
113 int     Afile = -1;     /* archive file descriptor */
114 int     AfileActive = 1;/* Afile flag */
115 int     uflag;          /* update flag */
116 int     mflag;          /* dump metadata only if possible */
117 int     Mflag;          /* multi-volume flag */
118 int     qflag;          /* quit on errors flag */
119 int     vflag;          /* verbose flag */
120 int     breademax = 32; /* maximum number of bread errors before we quit */
121 char    *eot_script;    /* end of volume script fiag */
122 int     diskfd;         /* disk file descriptor */
123 int     tapefd;         /* tape file descriptor */
124 int     pipeout;        /* true => output to standard output */
125 int     fifoout;        /* true => output to fifo */
126 dump_ino_t curino;      /* current inumber; used globally */
127 int     newtape;        /* new tape flag */
128 int     density;        /* density in 0.1" units */
129 long long tapesize;     /* estimated tape size, blocks */
130 long    tsize;          /* tape size in 0.1" units */
131 long    asize;          /* number of 0.1" units written on current tape */
132 int     etapes;         /* estimated number of tapes */
133 int     nonodump;       /* if set, do not honor UF_NODUMP user flags */
134 int     unlimited;      /* if set, write to end of medium */
135 int     compressed;     /* if set, dump is to be compressed */
136 long long bytes_written;/* total bytes written to tape */
137 long    uncomprblks;    /* uncompressed blocks written to tape */
138 int     notify;         /* notify operator flag */
139 int     blockswritten;  /* number of blocks written on current tape */
140 int     tapeno;         /* current tape number */
141 time_t  tstart_writing; /* when started writing the first tape block */
142 time_t  tend_writing;   /* after writing the last tape block */
143 #ifdef __linux__
144 ext2_filsys fs;
145 #else
146 struct  fs *sblock;     /* the file system super block */
147 char    sblock_buf[MAXBSIZE];
148 #endif
149 long    xferrate;       /* averaged transfer rate of all volumes */
150 long    dev_bsize;      /* block size of underlying disk device */
151 int     dev_bshift;     /* log2(dev_bsize) */
152 int     tp_bshift;      /* log2(TP_BSIZE) */
153 dump_ino_t volinfo[TP_NINOS];/* which inode on which volume archive info */
154
155 struct  dumptime *dthead;       /* head of the list version */
156 int     nddates;                /* number of records (might be zero) */
157 int     ddates_in;              /* we have read the increment file */
158 struct  dumpdates **ddatev;     /* the arrayfied version */
159
160 int     notify = 0;     /* notify operator flag */
161 int     blockswritten = 0;      /* number of blocks written on current tape */
162 int     tapeno = 0;     /* current tape number */
163 int     density = 0;    /* density in bytes/0.1" " <- this is for hilit19 */
164 int     ntrec = NTREC;  /* # blocks in each tape record */
165 int     cartridge = 0;  /* Assume non-cartridge tape */
166 #ifdef USE_QFA
167 int     tapepos = 0;    /* assume no QFA tapeposition needed by user */
168 #endif /* USE_QFA */
169 int     dokerberos = 0; /* Use Kerberos authentication */
170 long    dev_bsize = 1;  /* recalculated below */
171 long    *blocksperfiles = NULL; /* output blocks per file(s) */
172 char    *host = NULL;   /* remote host (if any) */
173 int     sizest = 0;     /* return size estimate only */
174 int     compressed = 0; /* use zlib to compress the output, compress level 1-9 */
175 long long bytes_written = 0; /* total bytes written */
176 long    uncomprblks = 0;/* uncompressed blocks written */
177 Transformation *transformation = &transformation_null;
178
179 long smtc_errno;
180
181 #ifdef  __linux__
182 char    *__progname;
183 #endif
184
185 int     maxbsize = 1024*1024;     /* XXX MAXBSIZE from sys/param.h */
186 static long numarg __P((const char *, long, long));
187 static long *numlistarg __P((const char *, long, long));
188 static void obsolete __P((int *, char **[]));
189 static void usage __P((void));
190 static void do_exclude_from_file __P((char *));
191 static void do_exclude_ino_str __P((char *));
192 static void incompat_flags __P((int, char, char));
193
194 static char* iexclude_bitmap = NULL;            /* the inode exclude bitmap */
195 static unsigned int iexclude_bitmap_bytes = 0;  /* size of bitmap in bytes */
196
197 Indexer *indexer = &indexer_legacy;
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, pch = 0;
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         memset(&lastlevel, 0, NUM_STR_SIZE);
240         memset(&level, 0, NUM_STR_SIZE);
241         transformation = &transformation_null;
242         /* Default dump level is zero. */
243         level[0] = '0';
244
245         if (argc < 2)
246                 usage();
247
248         obsolete(&argc, &argv);
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                         if ((pch >= '0') && (pch <= '9') && (strlen(level) < NUM_STR_SIZE))
276                                 level[strlen(level)] = ch;
277                         else 
278                                 level[0] = ch;
279                         pch = ch;
280                         break;
281
282                 case 'A':               /* archive file */
283                         Apath = optarg;
284                         break;
285
286                 case 'a':               /* `auto-size', Write to EOM. */
287                         unlimited = 1;
288                         aflag = 1;
289                         break;
290
291                 case 'B':               /* blocks per output file */
292                         unlimited = 0;
293                         blocksperfiles = numlistarg("number of blocks per file",
294                             1L, 0L);
295                         break;
296
297                 case 'b':               /* blocks per tape write */
298                         ntrec = numarg("number of blocks per write",
299                             1L, 1048576L);
300                         if (ntrec > maxbsize/1024) {
301                                 msg("Please choose a blocksize <= %dkB\n",
302                                         maxbsize/1024);
303                                 msg("The ENTIRE dump is aborted.\n");
304                                 exit(X_STARTUP);
305                         }
306                         bflag = 1;
307                         break;
308
309                 case 'c':               /* Tape is cart. not 9-track */
310                         unlimited = 0;
311                         cartridge = 1;
312                         break;
313
314                 case 'd':               /* density, in bits per inch */
315                         unlimited = 0;
316                         density = numarg("density", 10L, 327670L) / 10;
317                         if (density >= 625 && !bflag)
318                                 ntrec = HIGHDENSITYTREC;
319                         break;
320
321                 case 'D':               /* path of dumpdates file */
322                         dumpdates = optarg;
323                         break;
324                         
325                                         /* 04-Feb-00 ILC */
326                 case 'e':               /* exclude an inode */
327                         {
328                         char *p = optarg, *q;
329                         while ((q = strchr(p, ','))) {
330                                 *q = '\0';
331                                 do_exclude_ino_str(p);
332                                 p = q + 1;
333                         }
334                         do_exclude_ino_str(p);
335                         }
336                         break;
337
338                 case 'E':               /* exclude inodes read from file */
339                         do_exclude_from_file(optarg);
340                         break;
341
342                 case 'f':               /* output file */
343                         tapeprefix = optarg;
344                         break;
345
346                 case 'F':               /* end of tape script */
347                         eot_script = optarg;
348                         break;
349
350                 case 'h':
351                         honorlevel = numarg("honor level", 0L, 10L);
352                         break;
353
354 #ifdef HAVE_BZLIB
355                 case 'j':
356                         compressed = 2;
357                         zipflag = COMPRESS_BZLIB;
358                         transformation = transformation_bzlib_factory(1, 2);
359                         if (optarg)
360                                 compressed = numarg("compress level", 1L, 9L);
361                         break;
362 #endif /* HAVE_BZLIB */
363
364                 case 'I':
365                         breademax =
366                           numarg ("number of errors to ignore", 0L, 0L);
367                         break;
368
369 #ifdef KERBEROS
370                 case 'k':
371                         dokerberos = 1;
372                         break;
373 #endif
374
375                 case 'L':
376                         /*
377                          * Note that although there are LBLSIZE characters,
378                          * the last must be '\0', so the limit on strlen()
379                          * is really LBLSIZE-1.
380                          */
381                         strncpy(spcl.c_label, optarg, LBLSIZE);
382                         spcl.c_label[LBLSIZE-1] = '\0';
383                         if (strlen(optarg) > LBLSIZE-1) {
384                                 msg(
385                 "WARNING Label `%s' is larger than limit of %d characters.\n",
386                                     optarg, LBLSIZE-1);
387                                 msg("WARNING: Using truncated label `%s'.\n",
388                                     spcl.c_label);
389                         }
390                         break;
391
392                 case 'm':               /* metadata only flag */
393                         mflag = 1;
394                         break;
395
396                 case 'M':               /* multi-volume flag */
397                         Mflag = 1;
398                         break;
399
400                 case 'n':               /* notify operators */
401                         notify = 1;
402                         break;
403
404                 case 'q':
405                         qflag = 1;
406                         break;
407
408 #ifdef USE_QFA
409                 case 'Q':               /* create tapeposfile */
410                         //gTapeposfile = optarg;  // FIXME - communicate filename to indexer.
411                         tapepos = 1;
412                         break;
413 #endif /* USE_QFA */
414                         
415                 case 's':               /* tape size, feet */
416                         unlimited = 0;
417                         tsize = numarg("tape size", 1L, 0L) * 12 * 10;
418                         break;
419
420                 case 'S':
421                         sizest = 1;     /* return size estimate only */
422                         break;
423
424                 case 'T':               /* time of last dump */
425                         spcl.c_ddate = unctime(optarg);
426                         if (spcl.c_ddate < 0) {
427                                 msg("bad time \"%s\"\n", optarg);
428                                 msg("The ENTIRE dump is aborted.\n");
429                                 exit(X_STARTUP);
430                         }
431                         Tflag = 1;
432                         lastlevel[0] = '?'; lastlevel[1] = '\0'; 
433                         break;
434
435                 case 'u':               /* update dumpdates */
436                         uflag = 1;
437                         break;
438
439                 case 'v':               /* verbose */
440                         vflag = 1;
441                         break;
442
443                 case 'W':               /* what to do */
444                 case 'w':
445                         lastdump(ch);
446                         exit(X_FINOK);  /* do nothing else */
447 #ifdef HAVE_LZO
448                 case 'y':
449                         compressed = 2;
450                                 transformation = transformation_lzo_factory(1);
451                         zipflag = COMPRESS_LZO;
452                         break;
453 #endif /* HAVE_LZO */
454
455 #ifdef HAVE_ZLIB
456                 case 'z':
457                         compressed = 2;
458                         transformation = transformation_zlib_factory(1, 2);
459                         zipflag = COMPRESS_ZLIB;
460                         if (optarg)
461                                 compressed = numarg("compress level", 1L, 9L);
462                         break;
463 #endif /* HAVE_ZLIB */
464
465                 default:
466                         usage();
467                 }
468         argc -= optind;
469         argv += optind;
470
471         if (argc < 1) {
472                 msg("Must specify disk or filesystem\n");
473                 msg("The ENTIRE dump is aborted.\n");
474                 exit(X_STARTUP);
475         }
476         diskparam = *argv++;
477         if (strlen(diskparam) >= MAXPATHLEN) {
478                 msg("Disk or filesystem name too long: %s\n", diskparam);
479                 msg("The ENTIRE dump is aborted.\n");
480                 exit(X_STARTUP);
481         }
482         argc--;
483         incompat_flags(Tflag && uflag, 'T', 'u');
484         incompat_flags(aflag && blocksperfiles, 'a', 'B');
485         incompat_flags(aflag && cartridge, 'a', 'c');
486         incompat_flags(aflag && density, 'a', 'd');
487         incompat_flags(aflag && tsize, 'a', 's');
488
489         if (strcmp(tapeprefix, "-") == 0) {
490                 pipeout++;
491                 tapeprefix = "standard output";
492         }
493
494         if (blocksperfiles && !compressed)
495                 for (i = 1; i <= *blocksperfiles; i++)
496                         blocksperfiles[i] = blocksperfiles[i] / ntrec * ntrec; /* round down */
497         else if (!unlimited) {
498                 /*
499                  * Determine how to default tape size and density
500                  *
501                  *              density                         tape size
502                  * 9-track      1600 bpi (160 bytes/.1")        2300 ft.
503                  * 9-track      6250 bpi (625 bytes/.1")        2300 ft.
504                  * cartridge    8000 bpi (100 bytes/.1")        1700 ft.
505                  *                                              (450*4 - slop)
506                  * hilit19 hits again: "
507                  */
508                 if (density == 0)
509                         density = cartridge ? 100 : 160;
510                 if (tsize == 0)
511                         tsize = cartridge ? 1700L*120L : 2300L*120L;
512         }
513
514         {
515                 int i;
516                 char *n;
517
518                 if ((n = strchr(tapeprefix, ':'))) {
519                         for (i = 0; i < (n - tapeprefix); i++) {
520                                 if (tapeprefix[i] == '/')
521                                         break;
522                         }
523                         if (tapeprefix[i] != '/') {
524                                 host = tapeprefix;
525                                 tapeprefix = strchr(host, ':');
526                                 *tapeprefix++ = '\0';
527 #ifdef RDUMP
528                                 if (index(tapeprefix, '\n')) {
529                                         msg("invalid characters in tape\n");
530                                         msg("The ENTIRE dump is aborted.\n");
531                                         exit(X_STARTUP);
532                                 }
533                                 if (rmthost(host) == 0)
534                                         exit(X_STARTUP);
535 #else
536                                 msg("remote dump not enabled\n");
537                                 msg("The ENTIRE dump is aborted.\n");
538                                 exit(X_STARTUP);
539 #endif
540                         }
541                 }
542         }
543
544         (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
545         if (Apath && (Afile = OPEN(Apath, O_WRONLY|O_CREAT|O_TRUNC,
546                                    S_IRUSR | S_IWUSR | S_IRGRP |
547                                    S_IWGRP | S_IROTH | S_IWOTH)) < 0) {
548                 msg("Cannot open %s for writing: %s\n",
549                     optarg, strerror(errno));
550                 msg("The ENTIRE dump is aborted.\n");
551                 exit(X_STARTUP);
552         }
553
554         if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
555                 signal(SIGHUP, sig);
556         if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
557                 signal(SIGTRAP, sig);
558         if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
559                 signal(SIGFPE, sig);
560         if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
561                 signal(SIGBUS, sig);
562         if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
563                 signal(SIGSEGV, sig);
564         if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
565                 signal(SIGTERM, sig);
566         if (signal(SIGINT, interrupt) == SIG_IGN)
567                 signal(SIGINT, SIG_IGN);
568 #ifdef SIGXCPU
569         signal(SIGXCPU, SIG_IGN);
570 #endif /* SIGXCPU */
571 #ifdef SIGXFSZ
572         signal(SIGXFSZ, SIG_IGN);
573 #endif /* SIGXFSZ */
574
575         set_operators();        /* /etc/group snarfed */
576         getfstab();             /* /etc/fstab snarfed */
577
578         /*
579          *      disk may end in / and this can confuse
580          *      fstabsearch.
581          */
582         i = strlen(diskparam) - 1;
583         if (i > 1 && diskparam[i] == '/')
584                 if (!(i == 6 && !strcmp(diskparam, "LABEL=/")))
585                         diskparam[i] = '\0';
586
587         disk = get_device_name(diskparam);
588         if (!disk)
589                 disk = strdup(diskparam);
590
591         /*
592          *      disk can be either the full special file name,
593          *      the suffix of the special file name,
594          *      the special name missing the leading '/',
595          *      the file system name with or without the leading '/'.
596          */
597         if ((dt = fstabsearch(disk)) != NULL) {
598                 /* if found then only one parameter (i.e. partition)
599                  * is allowed */
600                 if (argc >= 1) {
601                         (void)fprintf(stderr, "Unknown arguments to dump:");
602                         while (argc--)
603                                 (void)fprintf(stderr, " %s", *argv++);
604                         (void)fprintf(stderr, "\n");
605                         msg("The ENTIRE dump is aborted.\n");
606                         exit(X_STARTUP);
607                 }
608                 disk = rawname(dt->mnt_fsname);
609                 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
610                 (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
611         } else {
612 #ifdef  __linux__
613 #ifdef  HAVE_REALPATH
614                 if (realpath(disk, pathname) == NULL)
615 #endif
616                         strcpy(pathname, disk);
617                 /*
618                  * The argument could be now a mountpoint of
619                  * a filesystem specified in fstab. Search for it.
620                  */
621                 if ((dt = fstabsearch(pathname)) != NULL) {
622                         disk = rawname(dt->mnt_fsname);
623                         (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
624                         (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
625                 } else {
626                         /*
627                          * The argument was not found in the fstab
628                          * assume that this is a subtree and search for it
629                          */
630                         dt = fstabsearchdir(pathname, directory);
631                         if (dt != NULL) {
632                                 char name[MAXPATHLEN];
633                                 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
634                                 (void)snprintf(name, sizeof(name), "%s (dir %s)",
635                                               dt->mnt_dir, directory);
636                                 (void)strncpy(spcl.c_filesys, name, NAMELEN);
637                                 disk = rawname(dt->mnt_fsname);
638                         } else {
639                                 (void)strncpy(spcl.c_dev, disk, NAMELEN);
640                                 (void)strncpy(spcl.c_filesys, "an unlisted file system",
641                                     NAMELEN);
642                         }
643                 }
644 #else
645                 (void)strncpy(spcl.c_dev, disk, NAMELEN);
646                 (void)strncpy(spcl.c_filesys, "an unlisted file system",
647                     NAMELEN);
648 #endif
649         }
650
651         if (directory[0] != 0) {
652                 if (atoi(level) != 0) {
653                         msg("Only level 0 dumps are allowed on a subdirectory\n");
654                         msg("The ENTIRE dump is aborted.\n");
655                         exit(X_STARTUP);
656                 }
657                 if (uflag) {
658                         msg("You can't update the dumpdates file when dumping a subdirectory\n");
659                         msg("The ENTIRE dump is aborted.\n");
660                         exit(X_STARTUP);
661                 }
662         }
663         spcl.c_dev[NAMELEN-1] = '\0';
664         spcl.c_filesys[NAMELEN-1] = '\0';
665         (void)gethostname(spcl.c_host, NAMELEN);
666         spcl.c_host[NAMELEN-1] = '\0';
667         spcl.c_level = atoi(level);
668         spcl.c_type = TS_TAPE;
669         if (!Tflag)
670                 getdumptime(uflag);             /* dumpdates snarfed */
671
672         if (spcl.c_ddate == 0 && spcl.c_level) {
673                 msg("WARNING: There is no inferior level dump on this filesystem\n"); 
674                 msg("WARNING: Assuming a level 0 dump by default\n");
675                 level[0] = '0'; level[1] = '\0';
676                 spcl.c_level = 0;
677         }
678
679         if (Mflag)
680                 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno + 1);
681         else
682                 strncpy(tape, tapeprefix, MAXPATHLEN);
683         tape[MAXPATHLEN - 1] = '\0';
684
685         if (!pipeout) {
686                 if (STAT(tape, &statbuf) != -1)
687                         fifoout= statbuf.st_mode & S_IFIFO;
688         }
689
690         if (!sizest) {
691
692                 msg("Date of this level %s dump: %s", level,
693                     ctime4(&spcl.c_date));
694                 if (spcl.c_ddate)
695                         msg("Date of last level %s 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 transformation 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 %lld blocks.\n", tapesize);
869         } else {
870                 double fetapes;
871
872                 if (blocksperfiles) {
873                         long 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 %lld blocks on %3.2f tape(s).\n",
921                     tapesize, fetapes);
922         }
923
924 #ifdef USE_QFA
925         indexer->openQfa();
926 #endif /* USE_QFA */
927
928         /*
929          * Allocate tape buffer.
930          */
931         if (!alloctape())
932                 quit(
933         "can't allocate tape buffers - try a smaller blocking factor.\n");
934
935         startnewtape(1);
936         tstart_writing = time(NULL);
937         dumpmap(usedinomap, TS_CLRI, maxino - 1);
938
939         msg("dumping (Pass III) [directories]\n");
940         dirty = 0;              /* XXX just to get gcc to shut up */
941         for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
942                 if (((ino - 1) % NBBY) == 0)    /* map is offset by 1 */
943                         dirty = *map++;
944                 else
945                         dirty >>= 1;
946                 if ((dirty & 1) == 0)
947                         continue;
948                 /*
949                  * Skip directory inodes deleted and maybe reallocated
950                  */
951                 dp = getino(ino);
952                 if ((dp->di_mode & IFMT) != IFDIR)
953                         continue;
954 #ifdef  __linux__
955                 /*
956                  * Skip directory inodes deleted and not yes reallocated...
957                  */
958                 if (dp->di_nlink == 0 || dp->di_dtime != 0)
959                         continue;
960                 if (vflag)
961                         msg("dumping directory inode %lu\n", ino);
962                 (void)dumpdirino(dp, ino);
963 #else
964                 (void)dumpino(dp, ino);
965 #endif
966         }
967
968         msg("dumping (Pass IV) [regular files]\n");
969         for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
970                 if (((ino - 1) % NBBY) == 0)    /* map is offset by 1 */
971                         dirty = *map++;
972                 else
973                         dirty >>= 1;
974                 if ((dirty & 1) == 0)
975                         continue;
976                 /*
977                  * Skip inodes deleted and reallocated as directories.
978                  */
979                 dp = getino(ino);
980                 if ((dp->di_mode & IFMT) == IFDIR)
981                         continue;
982 #ifdef __linux__
983                 /*
984                  * No need to check here for deleted and not yet reallocated
985                  * inodes since this is done in dumpino().
986                  */
987 #endif
988                 if (vflag) {
989                         if (mflag && TSTINO(ino, metainomap))
990                                 msg("dumping regular inode %lu (meta only)\n", ino);
991                         else
992                                 msg("dumping regular inode %lu\n", ino);
993                 }
994                 (void)dumpino(dp, ino, mflag && TSTINO(ino, metainomap));
995         }
996
997         tend_writing = time(NULL);
998         spcl.c_type = TS_END;
999
1000         indexer->foo();
1001
1002         if (Afile >= 0) {
1003                 volinfo[1] = ROOTINO;
1004                 memcpy(spcl.c_inos, volinfo, TP_NINOS * sizeof(dump_ino_t));
1005                 spcl.c_flags |= DR_INODEINFO;
1006         }
1007
1008         /*
1009          * Finish off the current tape record with trailer blocks, to ensure
1010          * at least the data in the last partial record makes it to tape.
1011          * Also make sure we write at least 1 trailer block.
1012          */
1013         for (i = ntrec - (spcl.c_tapea % ntrec); i; --i)
1014                 writeheader(maxino - 1);
1015
1016         tnow = trewind();
1017
1018         if (pipeout || fifoout)
1019                 msg("%ld blocks (%.2fMB)\n", spcl.c_tapea,
1020                         ((double)spcl.c_tapea * TP_BSIZE / 1048576));
1021         else
1022                 msg("%ld blocks (%.2fMB) on %d volume(s)\n",
1023                     spcl.c_tapea, 
1024                     ((double)spcl.c_tapea * TP_BSIZE / 1048576),
1025                     spcl.c_volume);
1026
1027         /* report dump performance, avoid division by zero */
1028         if (tend_writing - tstart_writing == 0)
1029                 msg("finished in less than a second\n");
1030         else
1031                 msg("finished in %d seconds, throughput %d kBytes/sec\n",
1032                     tend_writing - tstart_writing,
1033                     spcl.c_tapea / (tend_writing - tstart_writing));
1034
1035         putdumptime();
1036         msg("Date of this level %s dump: %s", level,
1037                 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
1038         msg("Date this dump completed:  %s", ctime(&tnow));
1039
1040         msg("Average transfer rate: %ld kB/s\n", xferrate / tapeno);
1041         if (compressed) {
1042                 long tapekb = bytes_written / 1024;
1043                 double rate = .0005 + (double) spcl.c_tapea / tapekb;
1044                 msg("Wrote %ldkB uncompressed, %ldkB compressed, %1.3f:1\n",
1045                         spcl.c_tapea, tapekb, rate);
1046         }
1047
1048         indexer->close();
1049         indexer->closeQfa();
1050
1051         broadcast("DUMP IS DONE!\7\7\n");
1052         msg("DUMP IS DONE\n");
1053         Exit(X_FINOK);
1054         /* NOTREACHED */
1055         return 0;       /* gcc - shut up */
1056 }
1057
1058 static void
1059 usage(void)
1060 {
1061         char white[MAXPATHLEN];
1062         const char *ext2ver, *ext2date;
1063
1064         memset(white, ' ', MAXPATHLEN);
1065         white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
1066
1067 #ifdef __linux__
1068         ext2fs_get_library_version(&ext2ver, &ext2date);
1069         fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
1070                 __progname, _DUMP_VERSION, ext2ver, ext2date);
1071 #else
1072         fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
1073 #endif
1074         fprintf(stderr,
1075                 "usage:\t%s [-level#] [-ac"
1076 #ifdef KERBEROS
1077                 "k"
1078 #endif
1079                 "mMnqSuv"
1080                 "] [-A file] [-B records] [-b blocksize]\n"
1081                 "\t%s [-d density] [-D file] [-e inode#,inode#,...] [-E file]\n"
1082                 "\t%s [-f file] [-h level] [-I nr errors] "
1083 #ifdef HAVE_BZLIB
1084                 "[-j zlevel] "
1085 #endif
1086 #ifdef USE_QFA
1087                 "[-Q file]\n"
1088 #endif
1089                 "\t%s [-s feet] "
1090                 "[-T date] "
1091 #ifdef HAVE_LZO
1092                 "[-y] "
1093 #endif
1094 #ifdef HAVE_ZLIB
1095                 "[-z zlevel] "
1096 #endif
1097                 "filesystem\n"
1098                 "\t%s [-W | -w]\n", 
1099                 __progname, white, white, white, __progname);
1100         exit(X_STARTUP);
1101 }
1102
1103 /*
1104  * Pick up a numeric argument.  It must be nonnegative and in the given
1105  * range (except that a vmax of 0 means unlimited).
1106  */
1107 static long
1108 numarg(const char *meaning, long vmin, long vmax)
1109 {
1110         char *p;
1111         long val;
1112
1113         val = strtol(optarg, &p, 10);
1114         if (*p)
1115                 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
1116         if (val < vmin || (vmax && val > vmax))
1117                 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
1118         return (val);
1119 }
1120
1121 /*
1122  * The same as numarg, just that it expects a comma separated list of numbers
1123  * and returns an array of longs with the first element containing the number
1124  * values in that array.
1125  */
1126 static long *
1127 numlistarg(const char *meaning, long vmin, long vmax)
1128 {
1129         char *p;
1130         long *vals,*curval;
1131         long valnum;
1132
1133         p = optarg;
1134         vals = NULL;
1135         valnum = 0;
1136         do {
1137                 valnum++;
1138                 if ( !(vals = realloc(vals, (valnum + 1) * sizeof(*vals))) )
1139                         errx(X_STARTUP, "allocating memory failed");
1140                 curval = vals + valnum;
1141                 *curval = strtol(p, &p, 10);
1142                 if (*p && *p!=',')
1143                         errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
1144                 if (*curval < vmin || (vmax && *curval > vmax))
1145                         errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
1146                 *vals = valnum;
1147                 if (*p) p++;
1148         } while(*p);
1149         return (vals);
1150 }
1151
1152 void
1153 sig(int signo)
1154 {
1155         switch(signo) {
1156         case SIGALRM:
1157         case SIGBUS:
1158         case SIGFPE:
1159         case SIGHUP:
1160         case SIGTERM:
1161         case SIGTRAP:
1162                 if (pipeout || fifoout)
1163                         quit("Signal on pipe: cannot recover\n");
1164                 msg("Rewriting attempted as response to unknown signal: %d.\n", signo);
1165                 (void)fflush(stderr);
1166                 (void)fflush(stdout);
1167                 close_rewind();
1168                 exit(X_REWRITE);
1169                 /* NOTREACHED */
1170         case SIGSEGV:
1171                 msg("SIGSEGV: ABORTING!\n");
1172                 (void)signal(SIGSEGV, SIG_DFL);
1173                 (void)kill(0, SIGSEGV);
1174                 /* NOTREACHED */
1175         }
1176 }
1177
1178 const char *
1179 rawname(const char *cp)
1180 {
1181 #ifdef  __linux__
1182         return cp;
1183 #else   /* __linux__ */
1184         static char rawbuf[MAXPATHLEN];
1185         char *dp = strrchr(cp, '/');
1186
1187         if (dp == NULL)
1188                 return (NULL);
1189         (void)strncpy(rawbuf, cp, min(dp-cp, MAXPATHLEN - 1));
1190         rawbuf[min(dp-cp, MAXPATHLEN-1)] = '\0';
1191         (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
1192         (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
1193         return (rawbuf);
1194 #endif  /* __linux__ */
1195 }
1196
1197 /*
1198  * obsolete --
1199  *      Change set of key letters and ordered arguments into something
1200  *      getopt(3) will like.
1201  */
1202 static void
1203 obsolete(int *argcp, char **argvp[])
1204 {
1205         int argc, flags;
1206         char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL;
1207
1208         /* Setup. */
1209         argv = *argvp;
1210         argc = *argcp;
1211
1212         /* Return if no arguments or first argument has leading dash. */
1213         ap = argv[1];
1214         if (argc == 1 || *ap == '-')
1215                 return;
1216
1217         /* Allocate space for new arguments. */
1218         if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
1219             (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
1220                 err(X_STARTUP, "malloc new args");
1221
1222         *nargv++ = *argv;
1223         argv += 2;
1224
1225         for (flags = 0; *ap; ++ap) {
1226                 switch (*ap) {
1227                 case 'A':
1228                 case 'B':
1229                 case 'b':
1230                 case 'd':
1231                 case 'D':
1232                 case 'e':
1233                 case 'E':
1234                 case 'f':
1235                 case 'F':
1236                 case 'h':
1237                 case 'L':
1238                 case 'Q':
1239                 case 's':
1240                 case 'T':
1241                         if (*argv == NULL) {
1242                                 warnx("option requires an argument -- %c", *ap);
1243                                 usage();
1244                         }
1245                         if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
1246                                 err(X_STARTUP, "malloc arg");
1247                         nargv[0][0] = '-';
1248                         nargv[0][1] = *ap;
1249                         (void)strcpy(&nargv[0][2], *argv);
1250                         ++argv;
1251                         ++nargv;
1252                         break;
1253                 default:
1254                         if (!flags) {
1255                                 *p++ = '-';
1256                                 flags = 1;
1257                         }
1258                         *p++ = *ap;
1259                         break;
1260                 }
1261         }
1262
1263         /* Terminate flags. */
1264         if (flags) {
1265                 *p = '\0';
1266                 *nargv++ = flagsp;
1267         }
1268
1269         /* Copy remaining arguments. */
1270         while ((*nargv++ = *argv++));
1271
1272         /* Update argument count. */
1273         *argcp = nargv - *argvp - 1;
1274 }
1275
1276 /*
1277  * This tests whether an inode is in the exclude bitmap
1278  */
1279 int
1280 exclude_ino(dump_ino_t ino)
1281 {
1282         /* if the inode exclude bitmap exists and covers given inode */
1283         if (iexclude_bitmap && iexclude_bitmap_bytes > ino / 8) {
1284                 /* then check this inode against it */
1285                 int idx = iexclude_bitmap[ino / 8];
1286                 if (idx & (1 << (ino % 8)))
1287                         return 1;
1288         }
1289         return 0;
1290 }
1291
1292 /*
1293  * This adds an inode to the exclusion bitmap if it isn't already there
1294  */
1295 void
1296 do_exclude_ino(dump_ino_t ino, const char *reason)
1297 {
1298         if (exclude_ino(ino))
1299                 return;
1300
1301         if (vflag) {
1302                 if (reason)
1303                         msg("Excluding inode %u (%s) from dump\n", ino, reason);
1304                 else
1305                         msg("Excluding inode %u from dump\n", ino);
1306         }
1307
1308         /* check for enough mem; initialize */
1309         if ((ino/8 + 1) > iexclude_bitmap_bytes) {
1310                 if (iexclude_bitmap_bytes == 0) {
1311                         unsigned int j;
1312                         iexclude_bitmap_bytes = 2 * (ino/8 + 1);
1313                         iexclude_bitmap = (char*) malloc(iexclude_bitmap_bytes);
1314                         if (iexclude_bitmap == NULL) {
1315                                 msg("allocating memory failed\n");
1316                                 exit(X_STARTUP);
1317                         }
1318                         for (j = 0; j < iexclude_bitmap_bytes; j++)
1319                                 iexclude_bitmap[j] = 0;
1320                 }
1321                 else {
1322                         unsigned int oldsize = iexclude_bitmap_bytes;
1323                         iexclude_bitmap_bytes *= 
1324                                 (ino / 8 + 1) / iexclude_bitmap_bytes + 1;
1325                         iexclude_bitmap = (char*) realloc(iexclude_bitmap, 
1326                                 iexclude_bitmap_bytes);
1327                         if (iexclude_bitmap == NULL) {
1328                                 msg("allocating memory failed\n");
1329                                 exit(X_STARTUP);
1330                         }
1331                         for( ; oldsize < iexclude_bitmap_bytes; oldsize++)
1332                                 iexclude_bitmap[oldsize] = 0;
1333                 }
1334         }
1335                 
1336         iexclude_bitmap[ino / 8] |= 1 << (ino % 8);
1337 }
1338
1339 static void
1340 do_exclude_ino_str(char * ino) {
1341         char *r;
1342         unsigned long inod;
1343
1344         inod = strtoul(ino, &r, 10);
1345         if (( *r != '\0' && !isspace(*r) ) || inod <= ROOTINO) {
1346                 msg("Invalid inode argument %s\n", ino);
1347                 msg("The ENTIRE dump is aborted.\n");
1348                 exit(X_STARTUP);
1349         }
1350         do_exclude_ino(inod, NULL);
1351 }
1352
1353 /*
1354  * This reads a file containing one inode number per line and exclude them all
1355  */
1356 static void 
1357 do_exclude_from_file(char *file) {
1358         FILE *f;
1359         char *p, fname[MAXPATHLEN];
1360         
1361
1362         if (!( f = fopen(file, "r")) ) {
1363                 msg("Cannot open file for reading: %s\n", file);
1364                 msg("The ENTIRE dump is aborted.\n");
1365                 exit(X_STARTUP);
1366         }
1367         while (( p = fgets(fname, MAXPATHLEN, f))) {
1368                 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */
1369                         *(p + strlen(p) - 1) = '\0';
1370                 if ( !*p ) /* skip empty lines */
1371                         continue;
1372                 do_exclude_ino_str(p);
1373         }
1374         fclose(f);
1375 }
1376
1377 static void incompat_flags(int cond, char flag1, char flag2) {
1378         if (cond) {
1379                 msg("You cannot use the %c and %c flags together.\n", 
1380                     flag1, flag2);
1381                 msg("The ENTIRE dump is aborted.\n");
1382                 exit(X_STARTUP);
1383         }
1384 }