]> git.wh0rd.org Git - dump.git/blob - dump/main.c
Fixed restore to use the full volume path when doing a compare.
[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 <pop@noos.fr>, 1999-2000
6  *      Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41
42 #ifndef lint
43 static const char rcsid[] =
44         "$Id: main.c,v 1.56 2001/08/13 16:17:52 stelian Exp $";
45 #endif /* not lint */
46
47 #include <config.h>
48 #include <compatlfs.h>
49 #include <ctype.h>
50 #include <compaterr.h>
51 #include <fcntl.h>
52 #include <fstab.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include <sys/param.h>
60 #include <sys/time.h>
61 #include <time.h>
62 #ifdef __linux__
63 #ifdef HAVE_EXT2FS_EXT2_FS_H
64 #include <ext2fs/ext2_fs.h>
65 #else
66 #include <linux/ext2_fs.h>
67 #endif
68 #include <ext2fs/ext2fs.h>
69 #include <sys/stat.h>
70 #include <bsdcompat.h>
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 /*
92  * Dump maps used to describe what is to be dumped.
93  */
94 int     mapsize;        /* size of the state maps */
95 char    *usedinomap;    /* map of allocated inodes */
96 char    *dumpdirmap;    /* map of directories to be dumped */
97 char    *dumpinomap;    /* map of files to be dumped */
98
99 const char *disk;       /* name of the disk file */
100 char    tape[MAXPATHLEN];/* name of the tape file */
101 char    *tapeprefix;    /* prefix of the tape file */
102 char    *dumpdates;     /* name of the file containing dump date information*/
103 char    lastlevel;      /* dump level of previous dump */
104 char    level;          /* dump level of this dump */
105 int     uflag;          /* update flag */
106 int     Mflag;          /* multi-volume flag */
107 char    *eot_script;    /* end of volume script fiag */
108 int     diskfd;         /* disk file descriptor */
109 int     tapefd;         /* tape file descriptor */
110 int     pipeout;        /* true => output to standard output */
111 int     fifoout;        /* true => output to fifo */
112 dump_ino_t curino;      /* current inumber; used globally */
113 int     newtape;        /* new tape flag */
114 int     density;        /* density in 0.1" units */
115 long    tapesize;       /* estimated tape size, blocks */
116 long    tsize;          /* tape size in 0.1" units */
117 long    asize;          /* number of 0.1" units written on current tape */
118 int     etapes;         /* estimated number of tapes */
119 int     nonodump;       /* if set, do not honor UF_NODUMP user flags */
120 int     unlimited;      /* if set, write to end of medium */
121 int     compressed;     /* if set, dump is to be compressed */
122 long long bytes_written;/* total bytes written to tape */
123 long    uncomprblks;    /* uncompressed blocks written to tape */
124 int     notify;         /* notify operator flag */
125 int     blockswritten;  /* number of blocks written on current tape */
126 int     tapeno;         /* current tape number */
127 time_t  tstart_writing; /* when started writing the first tape block */
128 time_t  tend_writing;   /* after writing the last tape block */
129 #ifdef __linux__
130 ext2_filsys fs;
131 #else
132 struct  fs *sblock;     /* the file system super block */
133 char    sblock_buf[MAXBSIZE];
134 #endif
135 long    xferrate;       /* averaged transfer rate of all volumes */
136 long    dev_bsize;      /* block size of underlying disk device */
137 int     dev_bshift;     /* log2(dev_bsize) */
138 int     tp_bshift;      /* log2(TP_BSIZE) */
139
140 #ifdef USE_QFA
141 int     gTapeposfd;
142 char    *gTapeposfile;
143 char    gTps[255];
144 int32_t gThisDumpDate;
145 #endif /* USE_QFA */
146
147 struct  dumptime *dthead;       /* head of the list version */
148 int     nddates;                /* number of records (might be zero) */
149 int     ddates_in;              /* we have read the increment file */
150 struct  dumpdates **ddatev;     /* the arrayfied version */
151
152 int     notify = 0;     /* notify operator flag */
153 int     blockswritten = 0;      /* number of blocks written on current tape */
154 int     tapeno = 0;     /* current tape number */
155 int     density = 0;    /* density in bytes/0.1" " <- this is for hilit19 */
156 int     ntrec = NTREC;  /* # tape blocks in each tape record */
157 int     cartridge = 0;  /* Assume non-cartridge tape */
158 #ifdef USE_QFA
159 int     tapepos = 0;    /* assume no QFA tapeposition needed by user */
160 #endif /* USE_QFA */
161 int     dokerberos = 0; /* Use Kerberos authentication */
162 long    dev_bsize = 1;  /* recalculated below */
163 long    blocksperfile;  /* output blocks per file */
164 char    *host = NULL;   /* remote host (if any) */
165 int     sizest = 0;     /* return size estimate only */
166 int     compressed = 0; /* use zlib to compress the output, compress level 1-9 */
167 long long bytes_written = 0; /* total bytes written */
168 long    uncomprblks = 0;/* uncompressed blocks written */
169
170 #ifdef  __linux__
171 char    *__progname;
172 #endif
173
174 int     maxbsize = 64*1024;     /* XXX MAXBSIZE from sys/param.h */
175 static long numarg __P((const char *, long, long));
176 static void obsolete __P((int *, char **[]));
177 static void usage __P((void));
178 static void do_exclude_from_file __P((char *));
179 static void do_exclude_ino_str __P((char *));
180
181 static dump_ino_t iexclude_list[IEXCLUDE_MAXNUM];/* the inode exclude list */
182 static int iexclude_num = 0;                    /* number of elements in the list */
183
184 int
185 main(int argc, char *argv[])
186 {
187         register dump_ino_t ino;
188         register int dirty;
189         register struct dinode *dp;
190         register struct fstab *dt;
191         register char *map;
192         register int ch;
193         int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
194         dump_ino_t maxino;
195         struct STAT statbuf;
196         dev_t filedev = 0;
197 #ifdef  __linux__
198         errcode_t retval;
199         char directory[MAXPATHLEN];
200         char pathname[MAXPATHLEN];
201 #endif
202         time_t tnow;
203         char *diskparam;
204
205         spcl.c_label[0] = '\0';
206         spcl.c_date = time(NULL);
207
208 #ifdef __linux__
209         __progname = argv[0];
210         directory[0] = 0;
211         initialize_ext2_error_table();
212 #endif
213
214         tsize = 0;      /* Default later, based on 'c' option for cart tapes */
215         unlimited = 1;
216         eot_script = NULL;
217         if ((tapeprefix = getenv("TAPE")) == NULL)
218                 tapeprefix = _PATH_DEFTAPE;
219         dumpdates = _PATH_DUMPDATES;
220         if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
221                 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
222         level = '0';
223
224         if (argc < 2)
225                 usage();
226
227         obsolete(&argc, &argv);
228
229 #ifdef USE_QFA
230         gTapeposfd = -1;
231 #endif /* USE_QFA */
232
233         while ((ch = getopt(argc, argv,
234                             "0123456789aB:b:cd:e:E:f:F:h:L:"
235 #ifdef KERBEROS
236                             "k"
237 #endif
238                             "Mn"
239 #ifdef USE_QFA
240                             "Q:"
241 #endif
242                             "s:ST:uWw"
243 #ifdef HAVE_ZLIB
244                             "z::"
245 #endif
246                             )) != -1)
247                 switch (ch) {
248                 /* dump level */
249                 case '0': case '1': case '2': case '3': case '4':
250                 case '5': case '6': case '7': case '8': case '9':
251                         level = ch;
252                         break;
253
254                 case 'a':               /* `auto-size', Write to EOM. */
255                         unlimited = 1;
256                         break;
257
258                 case 'B':               /* blocks per output file */
259                         unlimited = 0;
260                         blocksperfile = numarg("number of blocks per file",
261                             1L, 0L);
262                         break;
263
264                 case 'b':               /* blocks per tape write */
265                         ntrec = numarg("number of blocks per write",
266                             1L, 1000L);
267                         if (ntrec > maxbsize/1024) {
268                                 msg("Please choose a blocksize <= %dkB\n",
269                                         maxbsize/1024);
270                                 msg("The ENTIRE dump is aborted.\n");
271                                 exit(X_STARTUP);
272                         }
273                         bflag = 1;
274                         break;
275
276                 case 'c':               /* Tape is cart. not 9-track */
277                         unlimited = 0;
278                         cartridge = 1;
279                         break;
280
281                 case 'd':               /* density, in bits per inch */
282                         unlimited = 0;
283                         density = numarg("density", 10L, 327670L) / 10;
284                         if (density >= 625 && !bflag)
285                                 ntrec = HIGHDENSITYTREC;
286                         break;
287                         
288                                         /* 04-Feb-00 ILC */
289                 case 'e':               /* exclude an inode */
290                         {
291                         char *p = optarg, *q;
292                         while ((q = strchr(p, ','))) {
293                                 *q = '\0';
294                                 do_exclude_ino_str(p);
295                                 p = q + 1;
296                         }
297                         do_exclude_ino_str(p);
298                         }
299                         break;
300
301                 case 'E':               /* exclude inodes read from file */
302                         do_exclude_from_file(optarg);
303                         break;
304
305                 case 'f':               /* output file */
306                         tapeprefix = optarg;
307                         break;
308
309                 case 'F':               /* end of tape script */
310                         eot_script = optarg;
311                         break;
312
313                 case 'h':
314                         honorlevel = numarg("honor level", 0L, 10L);
315                         break;
316
317 #ifdef KERBEROS
318                 case 'k':
319                         dokerberos = 1;
320                         break;
321 #endif
322
323                 case 'L':
324                         /*
325                          * Note that although there are LBLSIZE characters,
326                          * the last must be '\0', so the limit on strlen()
327                          * is really LBLSIZE-1.
328                          */
329                         strncpy(spcl.c_label, optarg, LBLSIZE);
330                         spcl.c_label[LBLSIZE-1] = '\0';
331                         if (strlen(optarg) > LBLSIZE-1) {
332                                 msg(
333                 "WARNING Label `%s' is larger than limit of %d characters.\n",
334                                     optarg, LBLSIZE-1);
335                                 msg("WARNING: Using truncated label `%s'.\n",
336                                     spcl.c_label);
337                         }
338                         break;
339
340                 case 'M':               /* multi-volume flag */
341                         Mflag = 1;
342                         break;
343
344                 case 'n':               /* notify operators */
345                         notify = 1;
346                         break;
347
348 #ifdef USE_QFA
349                 case 'Q':               /* create tapeposfile */
350                         gTapeposfile = optarg;
351                         tapepos = 1;
352                         break;
353 #endif /* USE_QFA */
354                         
355                 case 's':               /* tape size, feet */
356                         unlimited = 0;
357                         tsize = numarg("tape size", 1L, 0L) * 12 * 10;
358                         break;
359
360                 case 'S':
361                         sizest = 1;     /* return size estimate only */
362                         break;
363
364                 case 'T':               /* time of last dump */
365                         spcl.c_ddate = unctime(optarg);
366                         if (spcl.c_ddate < 0) {
367                                 msg("bad time \"%s\"\n", optarg);
368                                 msg("The ENTIRE dump is aborted.\n");
369                                 exit(X_STARTUP);
370                         }
371                         Tflag = 1;
372                         lastlevel = '?';
373                         break;
374
375                 case 'u':               /* update dumpdates */
376                         uflag = 1;
377                         break;
378
379                 case 'W':               /* what to do */
380                 case 'w':
381                         lastdump(ch);
382                         exit(X_FINOK);  /* do nothing else */
383 #ifdef HAVE_ZLIB
384                 case 'z':
385                         compressed = 2;
386                         if (optarg)
387                                 compressed = numarg("compress level", 1L, 9L);
388                         break;
389 #endif /* HAVE_ZLIB */
390
391                 default:
392                         usage();
393                 }
394         argc -= optind;
395         argv += optind;
396
397         if (argc < 1) {
398                 msg("Must specify disk or filesystem\n");
399                 msg("The ENTIRE dump is aborted.\n");
400                 exit(X_STARTUP);
401         }
402         diskparam = *argv++;
403         if (strlen(diskparam) >= MAXPATHLEN) {
404                 msg("Disk or filesystem name too long: %s\n", diskparam);
405                 msg("The ENTIRE dump is aborted.\n");
406                 exit(X_STARTUP);
407         }
408         argc--;
409         if (Tflag && uflag) {
410                 msg("You cannot use the T and u flags together.\n");
411                 msg("The ENTIRE dump is aborted.\n");
412                 exit(X_STARTUP);
413         }
414         if (strcmp(tapeprefix, "-") == 0) {
415                 pipeout++;
416                 tapeprefix = "standard output";
417         }
418
419         if (blocksperfile && !compressed)
420                 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
421         else if (!unlimited) {
422                 /*
423                  * Determine how to default tape size and density
424                  *
425                  *              density                         tape size
426                  * 9-track      1600 bpi (160 bytes/.1")        2300 ft.
427                  * 9-track      6250 bpi (625 bytes/.1")        2300 ft.
428                  * cartridge    8000 bpi (100 bytes/.1")        1700 ft.
429                  *                                              (450*4 - slop)
430                  * hilit19 hits again: "
431                  */
432                 if (density == 0)
433                         density = cartridge ? 100 : 160;
434                 if (tsize == 0)
435                         tsize = cartridge ? 1700L*120L : 2300L*120L;
436         }
437
438         if (strchr(tapeprefix, ':')) {
439                 host = tapeprefix;
440                 tapeprefix = strchr(host, ':');
441                 *tapeprefix++ = '\0';
442 #ifdef RDUMP
443                 if (index(tapeprefix, '\n')) {
444                         msg("invalid characters in tape\n");
445                         msg("The ENTIRE dump is aborted.\n");
446                         exit(X_STARTUP);
447                 }
448                 if (rmthost(host) == 0)
449                         exit(X_STARTUP);
450 #else
451                 msg("remote dump not enabled\n");
452                 msg("The ENTIRE dump is aborted.\n");
453                 exit(X_STARTUP);
454 #endif
455         }
456         (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
457
458         if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
459                 signal(SIGHUP, sig);
460         if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
461                 signal(SIGTRAP, sig);
462         if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
463                 signal(SIGFPE, sig);
464         if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
465                 signal(SIGBUS, sig);
466         if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
467                 signal(SIGSEGV, sig);
468         if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
469                 signal(SIGTERM, sig);
470         if (signal(SIGINT, interrupt) == SIG_IGN)
471                 signal(SIGINT, SIG_IGN);
472         set_operators();        /* /etc/group snarfed */
473         getfstab();             /* /etc/fstab snarfed */
474
475         /*
476          *      disk may end in / and this can confuse
477          *      fstabsearch.
478          */
479         i = strlen(diskparam) - 1;
480         if (i > 1 && diskparam[i] == '/')
481                 diskparam[i] = '\0';
482
483         disk = get_device_name(diskparam);
484         if (!disk) {            /* null means the disk is some form
485                                    of LABEL= or UID= but it was not
486                                    found */
487                 msg("Cannot find a disk having %s\n", diskparam);
488                 msg("The ENTIRE dump is aborted.\n");
489                 exit(X_STARTUP);
490         }
491         /*
492          *      disk can be either the full special file name,
493          *      the suffix of the special file name,
494          *      the special name missing the leading '/',
495          *      the file system name with or without the leading '/'.
496          */
497         if ((dt = fstabsearch(disk)) != NULL) {
498                 /* if found then only one parameter (i.e. partition)
499                  * is allowed */
500                 if (argc >= 1) {
501                         (void)fprintf(stderr, "Unknown arguments to dump:");
502                         while (argc--)
503                                 (void)fprintf(stderr, " %s", *argv++);
504                         (void)fprintf(stderr, "\n");
505                         msg("The ENTIRE dump is aborted.\n");
506                         exit(X_STARTUP);
507                 }
508                 disk = rawname(dt->fs_spec);
509                 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
510                 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
511         } else {
512 #ifdef  __linux__
513 #ifdef  HAVE_REALPATH
514                 if (realpath(disk, pathname) == NULL)
515 #endif
516                         strcpy(pathname, disk);
517                 /*
518                  * The argument could be now a mountpoint of
519                  * a filesystem specified in fstab. Search for it.
520                  */
521                 if ((dt = fstabsearch(pathname)) != NULL) {
522                         disk = rawname(dt->fs_spec);
523                         (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
524                         (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
525                 } else {
526                         /*
527                          * The argument was not found in the fstab
528                          * assume that this is a subtree and search for it
529                          */
530                         dt = fstabsearchdir(pathname, directory);
531                         if (dt != NULL) {
532                                 char name[MAXPATHLEN];
533                                 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
534                                 (void)snprintf(name, sizeof(name), "%s (dir %s)",
535                                               dt->fs_file, directory);
536                                 (void)strncpy(spcl.c_filesys, name, NAMELEN);
537                                 disk = rawname(dt->fs_spec);
538                         } else {
539                                 (void)strncpy(spcl.c_dev, disk, NAMELEN);
540                                 (void)strncpy(spcl.c_filesys, "an unlisted file system",
541                                     NAMELEN);
542                         }
543                 }
544 #else
545                 (void)strncpy(spcl.c_dev, disk, NAMELEN);
546                 (void)strncpy(spcl.c_filesys, "an unlisted file system",
547                     NAMELEN);
548 #endif
549         }
550
551         if (directory[0] != 0) {
552                 if (level != '0') {
553                         msg("Only level 0 dumps are allowed on a subdirectory\n");
554                         msg("The ENTIRE dump is aborted.\n");
555                         exit(X_STARTUP);
556                 }
557                 if (uflag) {
558                         msg("You can't update the dumpdates file when dumping a subdirectory\n");
559                         msg("The ENTIRE dump is aborted.\n");
560                         exit(X_STARTUP);
561                 }
562         }
563         spcl.c_dev[NAMELEN-1] = '\0';
564         spcl.c_filesys[NAMELEN-1] = '\0';
565         (void)gethostname(spcl.c_host, NAMELEN);
566         spcl.c_host[NAMELEN-1] = '\0';
567         spcl.c_level = level - '0';
568         spcl.c_type = TS_TAPE;
569         if (!Tflag)
570                 getdumptime(uflag);             /* dumpdates snarfed */
571
572         if (spcl.c_ddate == 0 && spcl.c_level) {
573                 msg("WARNING: There is no inferior level dump on this filesystem\n"); 
574                 msg("WARNING: Assuming a level 0 dump by default\n");
575                 level = '0';
576                 spcl.c_level = 0;
577         }
578
579         if (Mflag)
580                 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno + 1);
581         else
582                 strncpy(tape, tapeprefix, MAXPATHLEN);
583         tape[MAXPATHLEN - 1] = '\0';
584
585         if (!pipeout) {
586                 if (STAT(tape, &statbuf) != -1)
587                         fifoout= statbuf.st_mode & S_IFIFO;
588         }
589
590         if (!sizest) {
591
592                 msg("Date of this level %c dump: %s", level,
593                     ctime4(&spcl.c_date));
594 #ifdef USE_QFA
595                 gThisDumpDate = spcl.c_date;
596 #endif
597                 if (spcl.c_ddate)
598                         msg("Date of last level %c dump: %s", lastlevel,
599                             ctime4(&spcl.c_ddate));
600                 msg("Dumping %s (%s) ", disk, spcl.c_filesys);
601                 if (host)
602                         msgtail("to %s on host %s\n", tape, host);
603                 else
604                         msgtail("to %s\n", tape);
605         } /* end of size estimate */
606
607 #ifdef  __linux__
608         retval = dump_fs_open(disk, &fs);
609         if (retval) {
610                 com_err(disk, retval, "while opening filesystem");
611                 if (retval == EXT2_ET_REV_TOO_HIGH)
612                         msg("Get a newer version of dump!\n");
613                 msg("The ENTIRE dump is aborted.\n");
614                 exit(X_STARTUP);
615         }
616         if (fs->super->s_rev_level > DUMP_CURRENT_REV) {
617                 com_err(disk, retval, "while opening filesystem");
618                 msg("Get a newer version of dump!\n");
619                 msg("The ENTIRE dump is aborted.\n");
620                 exit(X_STARTUP);
621         }
622         if ((diskfd = OPEN(disk, O_RDONLY)) < 0) {
623                 msg("Cannot open %s\n", disk);
624                 msg("The ENTIRE dump is aborted.\n");
625                 exit(X_STARTUP);
626         }
627         /* if no user label specified, use ext2 filesystem label if available */
628         if (spcl.c_label[0] == '\0') {
629                 const char *lbl;
630                 if ( (lbl = get_device_label(disk)) != NULL) {
631                         strncpy(spcl.c_label, lbl, LBLSIZE);
632                         spcl.c_label[LBLSIZE-1] = '\0';
633                 }
634                 else
635                         strcpy(spcl.c_label, "none");   /* safe strcpy. */
636         }
637         sync();
638         dev_bsize = DEV_BSIZE;
639         dev_bshift = ffs(dev_bsize) - 1;
640         if (dev_bsize != (1 << dev_bshift))
641                 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
642         tp_bshift = ffs(TP_BSIZE) - 1;
643         if (TP_BSIZE != (1 << tp_bshift))
644                 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
645         maxino = fs->super->s_inodes_count;
646 #if     0
647         spcl.c_flags |= DR_NEWINODEFMT;
648 #endif
649 #else   /* __linux __*/
650         if ((diskfd = open(disk, O_RDONLY)) < 0) {
651                 msg("Cannot open %s\n", disk);
652                 msg("The ENTIRE dump is aborted.\n");
653                 exit(X_STARTUP);
654         }
655         sync();
656         sblock = (struct fs *)sblock_buf;
657         bread(SBOFF, (char *) sblock, SBSIZE);
658         if (sblock->fs_magic != FS_MAGIC)
659                 quit("bad sblock magic number\n");
660         dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
661         dev_bshift = ffs(dev_bsize) - 1;
662         if (dev_bsize != (1 << dev_bshift))
663                 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
664         tp_bshift = ffs(TP_BSIZE) - 1;
665         if (TP_BSIZE != (1 << tp_bshift))
666                 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
667 #ifdef FS_44INODEFMT
668         if (sblock->fs_inodefmt >= FS_44INODEFMT)
669                 spcl.c_flags |= DR_NEWINODEFMT;
670 #endif
671         maxino = sblock->fs_ipg * sblock->fs_ncg;
672 #endif  /* __linux__ */
673         mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
674         usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
675         dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
676         dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
677         if (usedinomap == NULL || dumpdirmap == NULL || dumpinomap == NULL)
678                 quit("out of memory allocating inode maps\n");
679         tapesize = 2 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
680
681         nonodump = spcl.c_level < honorlevel;
682
683         if (!sizest) {
684                 msg("Label: %s\n", spcl.c_label);
685
686                 if (compressed)
687                         msg("Compressing output at compression level %d\n", 
688                             compressed);
689         }
690
691 #if defined(SIGINFO)
692         (void)signal(SIGINFO, statussig);
693 #endif
694
695         if (!sizest)
696                 msg("mapping (Pass I) [regular files]\n");
697 #ifdef  __linux__
698         if (directory[0] == 0)
699                 anydirskipped = mapfiles(maxino, &tapesize);
700         else {
701                 if (STAT(pathname, &statbuf) == -1) {
702                         msg("File cannot be accessed (%s).\n", pathname);
703                         msg("The ENTIRE dump is aborted.\n");
704                         exit(X_STARTUP);
705                 }
706                 filedev = statbuf.st_dev;
707                 if (!(statbuf.st_mode & S_IFDIR))       /* is a file */
708                         anydirskipped = maponefile(maxino, &tapesize, 
709                                                    directory);
710                 else
711                         anydirskipped = mapfilesfromdir(maxino, &tapesize, 
712                                                         directory);
713         }
714         while (argc--) {
715                 int anydirskipped2;
716                 char *p = *argv;
717                 /* check if file is available */
718                 if (STAT(p, &statbuf) == -1) {
719                         msg("File cannot be accessed (%s).\n", p);
720                         msg("The ENTIRE dump is aborted.\n");
721                         exit(X_STARTUP);
722                 }
723                 /* check if file is on same unix partiton as the first 
724                  * argument */
725                 if (statbuf.st_dev != filedev) {
726                         msg("Files are not on same file system (%s).\n", p);
727                         msg("The ENTIRE dump is aborted.\n");
728                         exit(X_STARTUP);
729                 }
730                 /* check if file is a directory */
731                 if (!(statbuf.st_mode & S_IFDIR))
732                         anydirskipped2 = maponefile(maxino, &tapesize, 
733                                                     p+strlen(dt->fs_file));
734                 else
735                         /* read directory inodes.
736                          * NOTE: nested directories are not recognized 
737                          * so inodes may be umped twice!
738                          */
739                         anydirskipped2 = mapfilesfromdir(maxino, &tapesize, 
740                                                          p+strlen(dt->fs_file));
741                 if (!anydirskipped)
742                         anydirskipped = anydirskipped2;
743                 argv++;
744         }               
745 #else
746         anydirskipped = mapfiles(maxino, &tapesize);
747 #endif
748
749         if (!sizest)
750                 msg("mapping (Pass II) [directories]\n");
751         while (anydirskipped) {
752                 anydirskipped = mapdirs(maxino, &tapesize);
753         }
754
755         if (sizest) {
756                 printf("%.0f\n", ((double)tapesize + 1 + ntrec) * TP_BSIZE);
757                 exit(X_FINOK);
758         } /* stop here for size estimate */
759
760         if (pipeout || unlimited) {
761                 tapesize += 1 + ntrec;  /* 1 map header + trailer blocks */
762                 msg("estimated %ld tape blocks.\n", tapesize);
763         } else {
764                 double fetapes;
765
766                 if (blocksperfile)
767                         fetapes = (double) tapesize / blocksperfile;
768                 else if (cartridge) {
769                         /* Estimate number of tapes, assuming streaming stops at
770                            the end of each block written, and not in mid-block.
771                            Assume no erroneous blocks; this can be compensated
772                            for with an artificially low tape size. */
773                         fetapes =
774                         (         (double) tapesize     /* blocks */
775                                 * TP_BSIZE      /* bytes/block */
776                                 * (1.0/density) /* 0.1" / byte " */
777                           +
778                                   (double) tapesize     /* blocks */
779                                 * (1.0/ntrec)   /* streaming-stops per block */
780                                 * 15.48         /* 0.1" / streaming-stop " */
781                         ) * (1.0 / tsize );     /* tape / 0.1" " */
782                 } else {
783                         /* Estimate number of tapes, for old fashioned 9-track
784                            tape */
785                         int tenthsperirg = (density == 625) ? 3 : 7;
786                         fetapes =
787                         (         (double) tapesize     /* blocks */
788                                 * TP_BSIZE      /* bytes / block */
789                                 * (1.0/density) /* 0.1" / byte " */
790                           +
791                                   (double) tapesize     /* blocks */
792                                 * (1.0/ntrec)   /* IRG's / block */
793                                 * tenthsperirg  /* 0.1" / IRG " */
794                         ) * (1.0 / tsize );     /* tape / 0.1" " */
795                 }
796                 etapes = fetapes;               /* truncating assignment */
797                 etapes++;
798                 /* count the dumped inodes map on each additional tape */
799                 tapesize += (etapes - 1) *
800                         (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
801                 tapesize += etapes + ntrec;     /* headers + trailer blks */
802                 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
803                     tapesize, fetapes);
804         }
805
806 #ifdef USE_QFA
807         if (tapepos) {
808                 msg("writing QFA positions to %s\n", gTapeposfile);
809                 if ((gTapeposfd = open(gTapeposfile, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR)) < 0)
810                         quit("can't open tapeposfile\n");
811                 /* print QFA-file header */
812                 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
813                 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps))
814                         quit("can't write tapeposfile\n");
815                 sprintf(gTps, "ino\ttapeno\ttapepos\n");
816                 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps))
817                         quit("can't write tapeposfile\n");
818         }
819 #endif /* USE_QFA */
820
821         /*
822          * Allocate tape buffer.
823          */
824         if (!alloctape())
825                 quit(
826         "can't allocate tape buffers - try a smaller blocking factor.\n");
827
828         startnewtape(1);
829         tstart_writing = time(NULL);
830         dumpmap(usedinomap, TS_CLRI, maxino - 1);
831
832         msg("dumping (Pass III) [directories]\n");
833         dirty = 0;              /* XXX just to get gcc to shut up */
834         for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
835                 if (((ino - 1) % NBBY) == 0)    /* map is offset by 1 */
836                         dirty = *map++;
837                 else
838                         dirty >>= 1;
839                 if ((dirty & 1) == 0)
840                         continue;
841                 /*
842                  * Skip directory inodes deleted and maybe reallocated
843                  */
844                 dp = getino(ino);
845                 if ((dp->di_mode & IFMT) != IFDIR)
846                         continue;
847 #ifdef  __linux__
848                 /*
849                  * Skip directory inodes deleted and not yes reallocated...
850                  */
851                 if (dp->di_nlink == 0 || dp->di_dtime != 0)
852                         continue;
853                 (void)dumpdirino(dp, ino);
854 #else
855                 (void)dumpino(dp, ino);
856 #endif
857         }
858
859         msg("dumping (Pass IV) [regular files]\n");
860         for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
861                 if (((ino - 1) % NBBY) == 0)    /* map is offset by 1 */
862                         dirty = *map++;
863                 else
864                         dirty >>= 1;
865                 if ((dirty & 1) == 0)
866                         continue;
867                 /*
868                  * Skip inodes deleted and reallocated as directories.
869                  */
870                 dp = getino(ino);
871                 if ((dp->di_mode & IFMT) == IFDIR)
872                         continue;
873 #ifdef __linux__
874                 /*
875                  * No need to check here for deleted and not yet reallocated
876                  * inodes since this is done in dumpino().
877                  */
878 #endif
879                 (void)dumpino(dp, ino);
880         }
881
882         tend_writing = time(NULL);
883         spcl.c_type = TS_END;
884         /*
885          * Finish off the current tape record with trailer blocks, to ensure
886          * at least the data in the last partial record makes it to tape.
887          * Also make sure we write at least 1 trailer block.
888          */
889         for (i = ntrec - (spcl.c_tapea % ntrec); i; --i)
890                 writeheader(maxino - 1);
891
892         tnow = trewind();
893
894         if (pipeout || fifoout)
895                 msg("%ld tape blocks (%.2fMB)\n", spcl.c_tapea,
896                         ((double)spcl.c_tapea * TP_BSIZE / 1048576));
897         else
898                 msg("%ld tape blocks (%.2fMB) on %d volume(s)\n",
899                     spcl.c_tapea, 
900                     ((double)spcl.c_tapea * TP_BSIZE / 1048576),
901                     spcl.c_volume);
902
903         /* report dump performance, avoid division by zero */
904         if (tend_writing - tstart_writing == 0)
905                 msg("finished in less than a second\n");
906         else
907                 msg("finished in %d seconds, throughput %d kBytes/sec\n",
908                     tend_writing - tstart_writing,
909                     spcl.c_tapea / (tend_writing - tstart_writing));
910
911         putdumptime();
912         msg("Date of this level %c dump: %s", level,
913                 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
914         msg("Date this dump completed:  %s", ctime(&tnow));
915
916         msg("Average transfer rate: %ld kB/s\n", xferrate / tapeno);
917         if (compressed) {
918                 long tapekb = bytes_written / 1024;
919                 double rate = .0005 + (double) spcl.c_tapea / tapekb;
920                 msg("Wrote %ldkB uncompressed, %ldkB compressed, %1.3f:1\n",
921                         spcl.c_tapea, tapekb, rate);
922         }
923
924         broadcast("DUMP IS DONE!\7\7\n");
925         msg("DUMP IS DONE\n");
926         Exit(X_FINOK);
927         /* NOTREACHED */
928         return 0;       /* gcc - shut up */
929 }
930
931 static void
932 usage(void)
933 {
934         char white[MAXPATHLEN];
935         const char *ext2ver, *ext2date;
936
937         memset(white, ' ', MAXPATHLEN);
938         white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
939
940 #ifdef __linux__
941         ext2fs_get_library_version(&ext2ver, &ext2date);
942         fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
943                 __progname, _DUMP_VERSION, ext2ver, ext2date);
944 #else
945         fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
946 #endif
947         fprintf(stderr,
948                 "usage:\t%s [-0123456789ac"
949 #ifdef KERBEROS
950                 "k"
951 #endif
952                 "MnSu"
953                 "] [-B records] [-b blocksize] [-d density]\n"
954                 "\t%s [-e inode#,inode#,...] [-E file] [-f file] [-h level] "
955 #ifdef USE_QFA
956                 "[-Q file] "
957 #endif
958                 "\n\t%s [-s feet] [-T date] "
959 #ifdef HAVE_ZLIB
960                 "[-z zlevel] "
961 #endif
962                 "filesystem\n"
963                 "\t%s [-W | -w]\n", 
964                 __progname, white, white, __progname);
965         exit(X_STARTUP);
966 }
967
968 /*
969  * Pick up a numeric argument.  It must be nonnegative and in the given
970  * range (except that a vmax of 0 means unlimited).
971  */
972 static long
973 numarg(const char *meaning, long vmin, long vmax)
974 {
975         char *p;
976         long val;
977
978         val = strtol(optarg, &p, 10);
979         if (*p)
980                 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
981         if (val < vmin || (vmax && val > vmax))
982                 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
983         return (val);
984 }
985
986 void
987 sig(int signo)
988 {
989         switch(signo) {
990         case SIGALRM:
991         case SIGBUS:
992         case SIGFPE:
993         case SIGHUP:
994         case SIGTERM:
995         case SIGTRAP:
996                 if (pipeout || fifoout)
997                         quit("Signal on pipe: cannot recover\n");
998                 msg("Rewriting attempted as response to unknown signal: %d.\n", signo);
999                 (void)fflush(stderr);
1000                 (void)fflush(stdout);
1001                 close_rewind();
1002                 exit(X_REWRITE);
1003                 /* NOTREACHED */
1004         case SIGSEGV:
1005                 msg("SIGSEGV: ABORTING!\n");
1006                 (void)signal(SIGSEGV, SIG_DFL);
1007                 (void)kill(0, SIGSEGV);
1008                 /* NOTREACHED */
1009         }
1010 }
1011
1012 const char *
1013 rawname(const char *cp)
1014 {
1015 #ifdef  __linux__
1016         return cp;
1017 #else   /* __linux__ */
1018         static char rawbuf[MAXPATHLEN];
1019         char *dp = strrchr(cp, '/');
1020
1021         if (dp == NULL)
1022                 return (NULL);
1023         (void)strncpy(rawbuf, cp, min(dp-cp, MAXPATHLEN - 1));
1024         rawbuf[min(dp-cp, MAXPATHLEN-1)] = '\0';
1025         (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
1026         (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
1027         return (rawbuf);
1028 #endif  /* __linux__ */
1029 }
1030
1031 /*
1032  * obsolete --
1033  *      Change set of key letters and ordered arguments into something
1034  *      getopt(3) will like.
1035  */
1036 static void
1037 obsolete(int *argcp, char **argvp[])
1038 {
1039         int argc, flags;
1040         char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL;
1041
1042         /* Setup. */
1043         argv = *argvp;
1044         argc = *argcp;
1045
1046         /* Return if no arguments or first argument has leading dash. */
1047         ap = argv[1];
1048         if (argc == 1 || *ap == '-')
1049                 return;
1050
1051         /* Allocate space for new arguments. */
1052         if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
1053             (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
1054                 err(X_STARTUP, "malloc new args");
1055
1056         *nargv++ = *argv;
1057         argv += 2;
1058
1059         for (flags = 0; *ap; ++ap) {
1060                 switch (*ap) {
1061                 case 'B':
1062                 case 'b':
1063                 case 'd':
1064                 case 'e':
1065                 case 'E':
1066                 case 'f':
1067                 case 'F':
1068                 case 'h':
1069                 case 'L':
1070                 case 'Q':
1071                 case 's':
1072                 case 'T':
1073                         if (*argv == NULL) {
1074                                 warnx("option requires an argument -- %c", *ap);
1075                                 usage();
1076                         }
1077                         if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
1078                                 err(X_STARTUP, "malloc arg");
1079                         nargv[0][0] = '-';
1080                         nargv[0][1] = *ap;
1081                         (void)strcpy(&nargv[0][2], *argv);
1082                         ++argv;
1083                         ++nargv;
1084                         break;
1085                 default:
1086                         if (!flags) {
1087                                 *p++ = '-';
1088                                 flags = 1;
1089                         }
1090                         *p++ = *ap;
1091                         break;
1092                 }
1093         }
1094
1095         /* Terminate flags. */
1096         if (flags) {
1097                 *p = '\0';
1098                 *nargv++ = flagsp;
1099         }
1100
1101         /* Copy remaining arguments. */
1102         while ((*nargv++ = *argv++));
1103
1104         /* Update argument count. */
1105         *argcp = nargv - *argvp - 1;
1106 }
1107
1108 /*
1109  * This tests whether an inode is in the exclude list
1110  */
1111 int
1112 exclude_ino(dump_ino_t ino)
1113 {
1114         /* 04-Feb-00 ILC */
1115         if (iexclude_num) {     /* if there are inodes in the exclude list */
1116                 int idx;        /* then check this inode against it */
1117                 for (idx = 0; idx < iexclude_num; idx++)
1118                         if (ino == iexclude_list[idx])
1119                                 return 1;
1120         }
1121         return 0;
1122 }
1123
1124 /*
1125  * This tests adds an inode to the exclusion list if it isn't already there
1126  */
1127 void
1128 do_exclude_ino(dump_ino_t ino, const char *reason)
1129 {
1130         if (!exclude_ino(ino)) {
1131                 if (iexclude_num == IEXCLUDE_MAXNUM) {
1132                         msg("Too many exclude options\n");
1133                         msg("The ENTIRE dump is aborted.\n");
1134                         exit(X_STARTUP);
1135                 }
1136                 if (reason)
1137                         msg("Added inode %u to exclude list (%s)\n", 
1138                             ino, reason);
1139                 else
1140                         msg("Added inode %u to exclude list\n", ino);
1141                 iexclude_list[iexclude_num++] = ino;
1142         }
1143 }
1144
1145 static void
1146 do_exclude_ino_str(char * ino) {
1147         char *r;
1148         unsigned long inod;
1149
1150         inod = strtoul(ino, &r, 10);
1151         if (*r != '\0' || inod <= ROOTINO) {
1152                 msg("Invalid inode argument %s\n", ino);
1153                 msg("The ENTIRE dump is aborted.\n");
1154                 exit(X_STARTUP);
1155         }
1156         do_exclude_ino(inod, NULL);
1157 }
1158
1159 /*
1160  * This reads a file containing one inode number per line and exclude them all
1161  */
1162 static void 
1163 do_exclude_from_file(char *file) {
1164         FILE *f;
1165         char *p, fname[MAXPATHLEN];
1166         
1167
1168         if (!( f = fopen(file, "r")) ) {
1169                 msg("Cannot open file for reading: %s\n", file);
1170                 msg("The ENTIRE dump is aborted.\n");
1171                 exit(X_STARTUP);
1172         }
1173         while (( p = fgets(fname, MAXPATHLEN, f))) {
1174                 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */
1175                         *(p + strlen(p) - 1) = '\0';
1176                 if ( !*p ) /* skip empty lines */
1177                         continue;
1178                 do_exclude_ino_str(p);
1179         }
1180         fclose(f);
1181 }