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