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