]> git.wh0rd.org Git - dump.git/blob - dump/main.c
Made dump understand the LABEL= and UUID= notations.
[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@cybercable.fr>, 1999-2000
6  */
7
8 /*-
9  * Copyright (c) 1980, 1991, 1993, 1994
10  *      The Regents of the University of California.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the University of
23  *      California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40
41 #ifndef lint
42 static const char rcsid[] =
43         "$Id: main.c,v 1.24 2000/08/20 19:41:50 stelian Exp $";
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #ifdef __linux__
49 #include <linux/ext2_fs.h>
50 #include <sys/stat.h>
51 #include <bsdcompat.h>
52 #else
53 #ifdef sunos
54 #include <sys/vnode.h>
55
56 #include <ufs/inode.h>
57 #include <ufs/fs.h>
58 #else
59 #include <ufs/ufs/dinode.h>
60 #include <ufs/ffs/fs.h>
61 #endif
62 #endif
63
64 #include <protocols/dumprestore.h>
65
66 #include <ctype.h>
67 #include <compaterr.h>
68 #include <fcntl.h>
69 #include <fstab.h>
70 #include <signal.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75
76 #ifdef __linux__
77 #include <ext2fs/ext2fs.h>
78 #endif
79
80 #include "dump.h"
81 #include "pathnames.h"
82 #include "bylabel.h"
83
84 #ifndef SBOFF
85 #define SBOFF (SBLOCK * DEV_BSIZE)
86 #endif
87
88 int     notify = 0;     /* notify operator flag */
89 int     blockswritten = 0;      /* number of blocks written on current tape */
90 int     tapeno = 0;     /* current tape number */
91 int     density = 0;    /* density in bytes/0.1" " <- this is for hilit19 */
92 int     ntrec = NTREC;  /* # tape blocks in each tape record */
93 int     cartridge = 0;  /* Assume non-cartridge tape */
94 int     dokerberos = 0; /* Use Kerberos authentication */
95 long    dev_bsize = 1;  /* recalculated below */
96 long    blocksperfile;  /* output blocks per file */
97 char    *host = NULL;   /* remote host (if any) */
98 int     sizest = 0;     /* return size estimate only */
99
100 #ifdef  __linux__
101 char    *__progname;
102 #endif
103
104 int     maxbsize = 64*1024;     /* XXX MAXBSIZE from sys/param.h */
105 static long numarg __P((const char *, long, long));
106 static void obsolete __P((int *, char **[]));
107 static void usage __P((void));
108
109 ino_t iexclude_list[IEXCLUDE_MAXNUM];   /* the inode exclude list */
110 int iexclude_num = 0;                   /* number of elements in the list */
111
112 int
113 main(int argc, char *argv[])
114 {
115         register ino_t ino;
116         register int dirty;
117         register struct dinode *dp;
118         register struct fstab *dt;
119         register char *map;
120         register int ch;
121         int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
122         ino_t maxino;
123 #ifdef  __linux__
124         errcode_t retval;
125         char directory[MAXPATHLEN];
126         char pathname[MAXPATHLEN];
127 #endif
128         time_t tnow;
129         char labelstr[LBLSIZE];
130         char *diskparam;
131
132         spcl.c_date = 0;
133 #ifdef  __linux__
134         (void)time4(&spcl.c_date);
135 #else
136         (void)time((time_t *)&spcl.c_date);
137 #endif
138
139 #ifdef __linux__
140         __progname = argv[0];
141         directory[0] = 0;
142         initialize_ext2_error_table();
143 #endif
144
145         tsize = 0;      /* Default later, based on 'c' option for cart tapes */
146         eot_script = NULL;
147         if ((tapeprefix = getenv("TAPE")) == NULL)
148                 tapeprefix = _PATH_DEFTAPE;
149         dumpdates = _PATH_DUMPDATES;
150         strcpy(labelstr, "none");       /* XXX safe strcpy. */
151         if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
152                 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
153         level = '0';
154
155         if (argc < 2)
156                 usage();
157
158         obsolete(&argc, &argv);
159 #ifdef KERBEROS
160 #define optstring "0123456789aB:b:cd:e:f:F:h:kL:Mns:ST:uWw"
161 #else
162 #define optstring "0123456789aB:b:cd:e:f:F:h:L:Mns:ST:uWw"
163 #endif
164         while ((ch = getopt(argc, argv, optstring)) != -1)
165 #undef optstring
166                 switch (ch) {
167                 /* dump level */
168                 case '0': case '1': case '2': case '3': case '4':
169                 case '5': case '6': case '7': case '8': case '9':
170                         level = ch;
171                         break;
172
173                 case 'a':               /* `auto-size', Write to EOM. */
174                         unlimited = 1;
175                         break;
176
177                 case 'B':               /* blocks per output file */
178                         blocksperfile = numarg("number of blocks per file",
179                             1L, 0L);
180                         break;
181
182                 case 'b':               /* blocks per tape write */
183                         ntrec = numarg("number of blocks per write",
184                             1L, 1000L);
185                         if (ntrec > maxbsize/1024) {
186                                 msg("Please choose a blocksize <= %dKB\n",
187                                         maxbsize/1024);
188                                 exit(X_STARTUP);
189                         }
190                         bflag = 1;
191                         break;
192
193                 case 'c':               /* Tape is cart. not 9-track */
194                         cartridge = 1;
195                         break;
196
197                 case 'd':               /* density, in bits per inch */
198                         density = numarg("density", 10L, 327670L) / 10;
199                         if (density >= 625 && !bflag)
200                                 ntrec = HIGHDENSITYTREC;
201                         break;
202                         
203                                         /* 04-Feb-00 ILC */
204                 case 'e':               /* exclude an inode */
205                         iexclude_list[iexclude_num++] = 
206                           numarg("inode to exclude",0L,0L);
207                         msg("Added %d to exclude list\n",
208                             iexclude_list[iexclude_num-1]);
209                         break;
210
211                 case 'f':               /* output file */
212                         tapeprefix = optarg;
213                         break;
214
215                 case 'F':               /* end of tape script */
216                         eot_script = optarg;
217                         break;
218
219                 case 'h':
220                         honorlevel = numarg("honor level", 0L, 10L);
221                         break;
222
223 #ifdef KERBEROS
224                 case 'k':
225                         dokerberos = 1;
226                         break;
227 #endif
228
229                 case 'L':
230                         /*
231                          * Note that although there are LBLSIZE characters,
232                          * the last must be '\0', so the limit on strlen()
233                          * is really LBLSIZE-1.
234                          */
235                         strncpy(labelstr, optarg, LBLSIZE);
236                         labelstr[LBLSIZE-1] = '\0';
237                         if (strlen(optarg) > LBLSIZE-1) {
238                                 msg(
239                 "WARNING Label `%s' is larger than limit of %d characters.\n",
240                                     optarg, LBLSIZE-1);
241                                 msg("WARNING: Using truncated label `%s'.\n",
242                                     labelstr);
243                         }
244                         break;
245
246                 case 'M':               /* multi-volume flag */
247                         Mflag = 1;
248                         break;
249
250                 case 'n':               /* notify operators */
251                         notify = 1;
252                         break;
253
254                 case 's':               /* tape size, feet */
255                         tsize = numarg("tape size", 1L, 0L) * 12 * 10;
256                         break;
257
258                 case 'S':
259                         sizest = 1;     /* return size estimate only */
260                         break;
261
262                 case 'T':               /* time of last dump */
263                         spcl.c_ddate = unctime(optarg);
264                         if (spcl.c_ddate < 0) {
265                                 (void)fprintf(stderr, "bad time \"%s\"\n",
266                                     optarg);
267                                 exit(X_STARTUP);
268                         }
269                         Tflag = 1;
270                         lastlevel = '?';
271                         break;
272
273                 case 'u':               /* update dumpdates */
274                         uflag = 1;
275                         break;
276
277                 case 'W':               /* what to do */
278                 case 'w':
279                         lastdump(ch);
280                         exit(X_FINOK);  /* do nothing else */
281
282                 default:
283                         usage();
284                 }
285         argc -= optind;
286         argv += optind;
287
288         if (argc < 1) {
289                 (void)fprintf(stderr, "Must specify disk or filesystem\n");
290                 exit(X_STARTUP);
291         }
292         diskparam = *argv++;
293         if (strlen(diskparam) >= MAXPATHLEN) {
294                 (void)fprintf(stderr, "Disk or filesystem name too long: %s\n", diskparam);
295                 exit(X_STARTUP);
296         }
297         argc--;
298         if (argc >= 1) {
299                 (void)fprintf(stderr, "Unknown arguments to dump:");
300                 while (argc--)
301                         (void)fprintf(stderr, " %s", *argv++);
302                 (void)fprintf(stderr, "\n");
303                 exit(X_STARTUP);
304         }
305         if (Tflag && uflag) {
306                 (void)fprintf(stderr,
307                     "You cannot use the T and u flags together.\n");
308                 exit(X_STARTUP);
309         }
310         if (strcmp(tapeprefix, "-") == 0) {
311                 pipeout++;
312                 tapeprefix = "standard output";
313         }
314
315         if (blocksperfile)
316                 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
317         else if (!unlimited) {
318                 /*
319                  * Determine how to default tape size and density
320                  *
321                  *              density                         tape size
322                  * 9-track      1600 bpi (160 bytes/.1")        2300 ft.
323                  * 9-track      6250 bpi (625 bytes/.1")        2300 ft.
324                  * cartridge    8000 bpi (100 bytes/.1")        1700 ft.
325                  *                                              (450*4 - slop)
326                  * hilit19 hits again: "
327                  */
328                 if (density == 0)
329                         density = cartridge ? 100 : 160;
330                 if (tsize == 0)
331                         tsize = cartridge ? 1700L*120L : 2300L*120L;
332         }
333
334         if (strchr(tapeprefix, ':')) {
335                 host = tapeprefix;
336                 tapeprefix = strchr(host, ':');
337                 *tapeprefix++ = '\0';
338 #ifdef RDUMP
339                 if (index(tapeprefix, '\n')) {
340                     (void)fprintf(stderr, "invalid characters in tape\n");
341                     exit(X_STARTUP);
342                 }
343                 if (rmthost(host) == 0)
344                         exit(X_STARTUP);
345 #else
346                 (void)fprintf(stderr, "remote dump not enabled\n");
347                 exit(X_STARTUP);
348 #endif
349         }
350         (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
351
352         if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
353                 signal(SIGHUP, sig);
354         if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
355                 signal(SIGTRAP, sig);
356         if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
357                 signal(SIGFPE, sig);
358         if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
359                 signal(SIGBUS, sig);
360         if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
361                 signal(SIGSEGV, sig);
362         if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
363                 signal(SIGTERM, sig);
364         if (signal(SIGINT, interrupt) == SIG_IGN)
365                 signal(SIGINT, SIG_IGN);
366         set_operators();        /* /etc/group snarfed */
367         getfstab();             /* /etc/fstab snarfed */
368
369         disk = get_device_name(diskparam);
370         if (!disk) {            /* null means the disk is some form
371                                    of LABEL= or UID= but it was not
372                                    found */
373                 msg("Cannot find a disk having %s\n", diskparam);
374                 exit(X_STARTUP);
375         }
376         /*
377          *      disk may end in / and this can confuse
378          *      fstabsearch.
379          */
380         if (strlen(disk) > 1 && disk[strlen(disk) - 1] == '/')
381           disk[strlen(disk) - 1] = '\0';
382         /*
383          *      disk can be either the full special file name,
384          *      the suffix of the special file name,
385          *      the special name missing the leading '/',
386          *      the file system name with or without the leading '/'.
387          */
388         if ((dt = fstabsearch(disk)) != NULL) {
389                 disk = rawname(dt->fs_spec);
390                 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
391                 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
392 #ifdef  __linux__
393         } else {
394                 /*
395                  * The argument was not found in the fstab
396                  * assume that this is a subtree and search for it
397                  */
398 #ifdef  HAVE_REALPATH
399                 if (realpath(disk, pathname) == NULL)
400 #endif
401                         strcpy(pathname, disk);
402                 dt = fstabsearchdir(pathname, directory);
403                 if (dt != NULL) {
404                         char name[MAXPATHLEN];
405                         (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
406                         (void)snprintf(name, sizeof(name), "%s (dir %s)",
407                                       dt->fs_file, directory);
408                         (void)strncpy(spcl.c_filesys, name, NAMELEN);
409                         disk = rawname(dt->fs_spec);
410                 } else {
411                         (void)strncpy(spcl.c_dev, disk, NAMELEN);
412                         (void)strncpy(spcl.c_filesys, "an unlisted file system",
413                             NAMELEN);
414                 }
415         }
416 #else
417         } else {
418                 (void)strncpy(spcl.c_dev, disk, NAMELEN);
419                 (void)strncpy(spcl.c_filesys, "an unlisted file system",
420                     NAMELEN);
421         }
422 #endif
423
424         if (directory[0] != 0) {
425                 if (level != '0') {
426                         (void)fprintf(stderr, "Only level 0 dumps are allowed on a subdirectory\n");
427                         exit(X_STARTUP);
428                 }
429                 if (uflag) {
430                         (void)fprintf(stderr, "You can't update the dumpdates file when dumping a subdirectory\n");
431                         exit(X_STARTUP);
432                 }
433         }
434         spcl.c_dev[NAMELEN-1]='\0';
435         spcl.c_filesys[NAMELEN-1]='\0';
436         (void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1);
437         (void)gethostname(spcl.c_host, NAMELEN);
438         spcl.c_level = level - '0';
439         spcl.c_type = TS_TAPE;
440         if (!Tflag)
441                 getdumptime(uflag);             /* dumpdates snarfed */
442
443         if (spcl.c_ddate == 0 && spcl.c_level) {
444                 msg("WARNING: There is no inferior level dump on this filesystem\n"); 
445                 msg("WARNING: Assuming a level 0 dump by default\n");
446                 level = '0';
447                 spcl.c_level = 0;
448         }
449
450         if (Mflag)
451                 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno + 1);
452         else
453                 strncpy(tape, tapeprefix, MAXPATHLEN);
454         tape[MAXPATHLEN - 1] = '\0';
455
456         if (!sizest) {
457
458                 msg("Date of this level %c dump: %s", level,
459 #ifdef  __linux__
460                         spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
461 #else
462                         spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
463 #endif
464                 msg("Date of last level %c dump: %s", lastlevel,
465 #ifdef  __linux__
466                         spcl.c_ddate == 0 ? "the epoch\n" : ctime4(&spcl.c_ddate));
467 #else
468                         spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
469 #endif
470                 msg("Dumping %s (%s) ", disk, spcl.c_filesys);
471                 if (host)
472                         msgtail("to %s on host %s\n", tape, host);
473                 else
474                         msgtail("to %s\n", tape);
475                 msg("Label: %s\n", labelstr);
476
477         } /* end of size estimate */
478
479 #ifdef  __linux__
480         retval = dump_fs_open(disk, &fs);
481         if (retval) {
482                 com_err(disk, retval, "while opening filesystem");
483                 if (retval == EXT2_ET_REV_TOO_HIGH)
484                         printf ("Get a newer version of dump!\n");
485                 exit(X_STARTUP);
486         }
487         if (fs->super->s_rev_level > DUMP_CURRENT_REV) {
488                 com_err(disk, retval, "while opening filesystem");
489                 printf ("Get a newer version of dump!\n");
490                 exit(X_STARTUP);
491         }
492         if ((diskfd = open(disk, O_RDONLY)) < 0) {
493                 msg("Cannot open %s\n", disk);
494                 exit(X_STARTUP);
495         }
496         sync();
497         dev_bsize = DEV_BSIZE;
498         dev_bshift = ffs(dev_bsize) - 1;
499         if (dev_bsize != (1 << dev_bshift))
500                 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
501         tp_bshift = ffs(TP_BSIZE) - 1;
502         if (TP_BSIZE != (1 << tp_bshift))
503                 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
504         maxino = fs->super->s_inodes_count;
505 #if     0
506         spcl.c_flags |= DR_NEWINODEFMT;
507 #endif
508 #else   /* __linux __*/
509         if ((diskfd = open(disk, O_RDONLY)) < 0) {
510                 msg("Cannot open %s\n", disk);
511                 exit(X_STARTUP);
512         }
513         sync();
514         sblock = (struct fs *)sblock_buf;
515         bread(SBOFF, (char *) sblock, SBSIZE);
516         if (sblock->fs_magic != FS_MAGIC)
517                 quit("bad sblock magic number\n");
518         dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
519         dev_bshift = ffs(dev_bsize) - 1;
520         if (dev_bsize != (1 << dev_bshift))
521                 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
522         tp_bshift = ffs(TP_BSIZE) - 1;
523         if (TP_BSIZE != (1 << tp_bshift))
524                 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
525 #ifdef FS_44INODEFMT
526         if (sblock->fs_inodefmt >= FS_44INODEFMT)
527                 spcl.c_flags |= DR_NEWINODEFMT;
528 #endif
529         maxino = sblock->fs_ipg * sblock->fs_ncg;
530 #endif  /* __linux__ */
531         mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
532         usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
533         dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
534         dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
535         tapesize = 2 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
536
537         nonodump = spcl.c_level < honorlevel;
538
539 #if defined(SIGINFO)
540         (void)signal(SIGINFO, statussig);
541 #endif
542
543         if (!sizest)
544                 msg("mapping (Pass I) [regular files]\n");
545 #ifdef  __linux__
546         if (directory[0] == 0)
547                 anydirskipped = mapfiles(maxino, &tapesize);
548         else
549                 anydirskipped = mapfilesfromdir(maxino, &tapesize, directory);
550 #else
551         anydirskipped = mapfiles(maxino, &tapesize);
552 #endif
553
554         if (!sizest)
555                 msg("mapping (Pass II) [directories]\n");
556         while (anydirskipped) {
557                 anydirskipped = mapdirs(maxino, &tapesize);
558         }
559
560         if (sizest) {
561                 printf("%.0f\n", ((double)tapesize + 11) * TP_BSIZE);
562                 exit(X_FINOK);
563         } /* stop here for size estimate */
564
565         if (pipeout || unlimited) {
566                 tapesize += 11; /* 10 trailer blocks + 1 map header */
567                 msg("estimated %ld tape blocks.\n", tapesize);
568         } else {
569                 double fetapes;
570
571                 if (blocksperfile)
572                         fetapes = (double) tapesize / blocksperfile;
573                 else if (cartridge) {
574                         /* Estimate number of tapes, assuming streaming stops at
575                            the end of each block written, and not in mid-block.
576                            Assume no erroneous blocks; this can be compensated
577                            for with an artificially low tape size. */
578                         fetapes =
579                         (         (double) tapesize     /* blocks */
580                                 * TP_BSIZE      /* bytes/block */
581                                 * (1.0/density) /* 0.1" / byte " */
582                           +
583                                   (double) tapesize     /* blocks */
584                                 * (1.0/ntrec)   /* streaming-stops per block */
585                                 * 15.48         /* 0.1" / streaming-stop " */
586                         ) * (1.0 / tsize );     /* tape / 0.1" " */
587                 } else {
588                         /* Estimate number of tapes, for old fashioned 9-track
589                            tape */
590                         int tenthsperirg = (density == 625) ? 3 : 7;
591                         fetapes =
592                         (         (double) tapesize     /* blocks */
593                                 * TP_BSIZE      /* bytes / block */
594                                 * (1.0/density) /* 0.1" / byte " */
595                           +
596                                   (double) tapesize     /* blocks */
597                                 * (1.0/ntrec)   /* IRG's / block */
598                                 * tenthsperirg  /* 0.1" / IRG " */
599                         ) * (1.0 / tsize );     /* tape / 0.1" " */
600                 }
601                 etapes = fetapes;               /* truncating assignment */
602                 etapes++;
603                 /* count the dumped inodes map on each additional tape */
604                 tapesize += (etapes - 1) *
605                         (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
606                 tapesize += etapes + 10;        /* headers + 10 trailer blks */
607                 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
608                     tapesize, fetapes);
609         }
610
611         /*
612          * Allocate tape buffer.
613          */
614         if (!alloctape())
615                 quit(
616         "can't allocate tape buffers - try a smaller blocking factor.\n");
617
618         startnewtape(1);
619 #ifdef __linux__
620         (void)time4(&(tstart_writing));
621 #else
622         (void)time((time_t *)&(tstart_writing));
623 #endif
624         dumpmap(usedinomap, TS_CLRI, maxino - 1);
625
626         msg("dumping (Pass III) [directories]\n");
627         dirty = 0;              /* XXX just to get gcc to shut up */
628         for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
629                 if (((ino - 1) % NBBY) == 0)    /* map is offset by 1 */
630                         dirty = *map++;
631                 else
632                         dirty >>= 1;
633                 if ((dirty & 1) == 0)
634                         continue;
635                 /*
636                  * Skip directory inodes deleted and maybe reallocated
637                  */
638                 dp = getino(ino);
639                 if ((dp->di_mode & IFMT) != IFDIR)
640                         continue;
641 #ifdef  __linux__
642                 /*
643                  * Skip directory inodes deleted and not yes reallocated...
644                  */
645                 if (dp->di_nlink == 0 || dp->di_dtime != 0)
646                         continue;
647                 (void)dumpdirino(dp, ino);
648 #else
649                 (void)dumpino(dp, ino);
650 #endif
651         }
652
653         msg("dumping (Pass IV) [regular files]\n");
654         for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
655                 if (((ino - 1) % NBBY) == 0)    /* map is offset by 1 */
656                         dirty = *map++;
657                 else
658                         dirty >>= 1;
659                 if ((dirty & 1) == 0)
660                         continue;
661                 /*
662                  * Skip inodes deleted and reallocated as directories.
663                  */
664                 dp = getino(ino);
665                 if ((dp->di_mode & IFMT) == IFDIR)
666                         continue;
667 #ifdef __linux__
668                 /*
669                  * No need to check here for deleted and not yes reallocated inodes
670                  * since this is done in dumpino().
671                  */
672 #endif
673                 (void)dumpino(dp, ino);
674         }
675
676 #ifdef __linux__
677         (void)time4(&(tend_writing));
678 #else
679         (void)time((time_t *)&(tend_writing));
680 #endif
681         spcl.c_type = TS_END;
682         for (i = 0; i < ntrec; i++)
683                 writeheader(maxino - 1);
684
685         tnow = trewind();
686
687         if (pipeout)
688                 msg("%ld tape blocks (%.2fMB)\n", spcl.c_tapea,
689                         ((double)spcl.c_tapea * TP_BSIZE / 1048576));
690         else
691                 msg("%ld tape blocks (%.2fMB) on %d volume(s)\n",
692                     spcl.c_tapea, 
693                     ((double)spcl.c_tapea * TP_BSIZE / 1048576),
694                     spcl.c_volume);
695
696         /* report dump performance, avoid division through zero */
697         if (tend_writing - tstart_writing == 0)
698                 msg("finished in less than a second\n");
699         else
700                 msg("finished in %d seconds, throughput %d KBytes/sec\n",
701                     tend_writing - tstart_writing,
702                     spcl.c_tapea / (tend_writing - tstart_writing));
703
704         putdumptime();
705 #ifdef __linux__
706         msg("Date of this level %c dump: %s", level,
707                 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
708 #else
709         msg("Date of this level %c dump: %s", level,
710                 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
711 #endif
712         msg("Date this dump completed:  %s", ctime(&tnow));
713
714         msg("Average transfer rate: %ld KB/s\n", xferrate / tapeno);
715
716         broadcast("DUMP IS DONE!\7\7\n");
717         msg("DUMP IS DONE\n");
718         Exit(X_FINOK);
719         /* NOTREACHED */
720         return 0;       /* gcc - shut up */
721 }
722
723 static void
724 usage(void)
725 {
726         char white[MAXPATHLEN];
727         int i;
728         
729         strncpy(white, __progname, MAXPATHLEN-1);
730         white[MAXPATHLEN-1] = '\0';
731         for (i=0; i<MAXPATHLEN; ++i)
732                 if (white[i] != '\0') white[i] = ' ';
733
734         fprintf(stderr,
735                 "%s %s\n", __progname, _DUMP_VERSION);
736         fprintf(stderr,
737                 "usage:\t%s [-0123456789ac"
738 #ifdef KERBEROS
739                 "k"
740 #endif
741                 "MnSu] [-B records] [-b blocksize] [-d density]\n"
742                 "\t%s [-e inode#] [-f file] [-h level] [-s feet] [-T date] filesystem\n"
743                 "\t%s [-W | -w]\n", __progname, white, __progname);
744         exit(X_STARTUP);
745 }
746
747 /*
748  * Pick up a numeric argument.  It must be nonnegative and in the given
749  * range (except that a vmax of 0 means unlimited).
750  */
751 static long
752 numarg(const char *meaning, long vmin, long vmax)
753 {
754         char *p;
755         long val;
756
757         val = strtol(optarg, &p, 10);
758         if (*p)
759                 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
760         if (val < vmin || (vmax && val > vmax))
761                 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
762         return (val);
763 }
764
765 void
766 sig(int signo)
767 {
768         switch(signo) {
769         case SIGALRM:
770         case SIGBUS:
771         case SIGFPE:
772         case SIGHUP:
773         case SIGTERM:
774         case SIGTRAP:
775                 if (pipeout)
776                         quit("Signal on pipe: cannot recover\n");
777                 msg("Rewriting attempted as response to unknown signal.\n");
778                 (void)fflush(stderr);
779                 (void)fflush(stdout);
780                 close_rewind();
781                 exit(X_REWRITE);
782                 /* NOTREACHED */
783         case SIGSEGV:
784                 msg("SIGSEGV: ABORTING!\n");
785                 (void)signal(SIGSEGV, SIG_DFL);
786                 (void)kill(0, SIGSEGV);
787                 /* NOTREACHED */
788         }
789 }
790
791 char *
792 rawname(char *cp)
793 {
794 #ifdef  __linux__
795         return cp;
796 #else   /* __linux__ */
797         static char rawbuf[MAXPATHLEN];
798         char *dp = strrchr(cp, '/');
799
800         if (dp == NULL)
801                 return (NULL);
802         *dp = '\0';
803         (void)strncpy(rawbuf, cp, MAXPATHLEN - 1);
804         rawbuf[MAXPATHLEN-1] = '\0';
805         *dp = '/';
806         (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
807         (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
808         return (rawbuf);
809 #endif  /* __linux__ */
810 }
811
812 /*
813  * obsolete --
814  *      Change set of key letters and ordered arguments into something
815  *      getopt(3) will like.
816  */
817 static void
818 obsolete(int *argcp, char **argvp[])
819 {
820         int argc, flags;
821         char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL;
822
823         /* Setup. */
824         argv = *argvp;
825         argc = *argcp;
826
827         /* Return if no arguments or first argument has leading dash. */
828         ap = argv[1];
829         if (argc == 1 || *ap == '-')
830                 return;
831
832         /* Allocate space for new arguments. */
833         if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
834             (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
835                 err(X_STARTUP, "malloc new args");
836
837         *nargv++ = *argv;
838         argv += 2;
839
840         for (flags = 0; *ap; ++ap) {
841                 switch (*ap) {
842                 case 'B':
843                 case 'b':
844                 case 'd':
845                 case 'e':
846                 case 'f':
847                 case 'F':
848                 case 'h':
849                 case 'L':
850                 case 's':
851                 case 'T':
852                         if (*argv == NULL) {
853                                 warnx("option requires an argument -- %c", *ap);
854                                 usage();
855                         }
856                         if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
857                                 err(X_STARTUP, "malloc arg");
858                         nargv[0][0] = '-';
859                         nargv[0][1] = *ap;
860                         (void)strcpy(&nargv[0][2], *argv);
861                         ++argv;
862                         ++nargv;
863                         break;
864                 default:
865                         if (!flags) {
866                                 *p++ = '-';
867                                 flags = 1;
868                         }
869                         *p++ = *ap;
870                         break;
871                 }
872         }
873
874         /* Terminate flags. */
875         if (flags) {
876                 *p = '\0';
877                 *nargv++ = flagsp;
878         }
879
880         /* Copy remaining arguments. */
881         while ((*nargv++ = *argv++));
882
883         /* Update argument count. */
884         *argcp = nargv - *argvp - 1;
885 }