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