]> git.wh0rd.org - dump.git/blob - dump/main.c
Multifile option to dump.
[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.43 2001/04/10 13:42:22 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 /* USA_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;
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 /* USA_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 /* USA_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 msg("Label: %s\n", spcl.c_label);
608
609 if (compressed)
610 msg("Compressing output at compression level %d\n", compressed);
611
612 #if defined(SIGINFO)
613 (void)signal(SIGINFO, statussig);
614 #endif
615
616 if (!sizest)
617 msg("mapping (Pass I) [regular files]\n");
618 #ifdef __linux__
619 if (directory[0] == 0)
620 anydirskipped = mapfiles(maxino, &tapesize);
621 else {
622 if (stat(pathname, &statbuf) == -1) {
623 msg("File cannot be accessed (%s).\n", pathname);
624 msg("The ENTIRE dump is aborted.\n");
625 exit(X_STARTUP);
626 }
627 filedev = statbuf.st_dev;
628 if (!(statbuf.st_mode & S_IFDIR)) /* is a file */
629 anydirskipped = maponefile(maxino, &tapesize,
630 directory);
631 else
632 anydirskipped = mapfilesfromdir(maxino, &tapesize,
633 directory);
634 }
635 while (argc--) {
636 int anydirskipped2;
637 char *p = *argv;
638 /* check if file is available */
639 if (stat(p, &statbuf) == -1) {
640 msg("File cannot be accessed (%s).\n", p);
641 msg("The ENTIRE dump is aborted.\n");
642 exit(X_STARTUP);
643 }
644 /* check if file is on same unix partiton as the first
645 * argument */
646 if (statbuf.st_dev != filedev) {
647 msg("Files are not on same file system (%s).\n", p);
648 msg("The ENTIRE dump is aborted.\n");
649 exit(X_STARTUP);
650 }
651 /* check if file is a directory */
652 if (!(statbuf.st_mode & S_IFDIR))
653 anydirskipped2 = maponefile(maxino, &tapesize,
654 p+strlen(dt->fs_file));
655 else
656 /* read directory inodes.
657 * NOTE: nested directories are not recognized
658 * so inodes may be umped twice!
659 */
660 anydirskipped2 = mapfilesfromdir(maxino, &tapesize,
661 p+strlen(dt->fs_file));
662 if (!anydirskipped)
663 anydirskipped = anydirskipped2;
664 argv++;
665 }
666 #else
667 anydirskipped = mapfiles(maxino, &tapesize);
668 #endif
669
670 if (!sizest)
671 msg("mapping (Pass II) [directories]\n");
672 while (anydirskipped) {
673 anydirskipped = mapdirs(maxino, &tapesize);
674 }
675
676 if (sizest) {
677 printf("%.0f\n", ((double)tapesize + 1 + ntrec) * TP_BSIZE);
678 exit(X_FINOK);
679 } /* stop here for size estimate */
680
681 if (pipeout || unlimited) {
682 tapesize += 1 + ntrec; /* 1 map header + trailer blocks */
683 msg("estimated %ld tape blocks.\n", tapesize);
684 } else {
685 double fetapes;
686
687 if (blocksperfile)
688 fetapes = (double) tapesize / blocksperfile;
689 else if (cartridge) {
690 /* Estimate number of tapes, assuming streaming stops at
691 the end of each block written, and not in mid-block.
692 Assume no erroneous blocks; this can be compensated
693 for with an artificially low tape size. */
694 fetapes =
695 ( (double) tapesize /* blocks */
696 * TP_BSIZE /* bytes/block */
697 * (1.0/density) /* 0.1" / byte " */
698 +
699 (double) tapesize /* blocks */
700 * (1.0/ntrec) /* streaming-stops per block */
701 * 15.48 /* 0.1" / streaming-stop " */
702 ) * (1.0 / tsize ); /* tape / 0.1" " */
703 } else {
704 /* Estimate number of tapes, for old fashioned 9-track
705 tape */
706 int tenthsperirg = (density == 625) ? 3 : 7;
707 fetapes =
708 ( (double) tapesize /* blocks */
709 * TP_BSIZE /* bytes / block */
710 * (1.0/density) /* 0.1" / byte " */
711 +
712 (double) tapesize /* blocks */
713 * (1.0/ntrec) /* IRG's / block */
714 * tenthsperirg /* 0.1" / IRG " */
715 ) * (1.0 / tsize ); /* tape / 0.1" " */
716 }
717 etapes = fetapes; /* truncating assignment */
718 etapes++;
719 /* count the dumped inodes map on each additional tape */
720 tapesize += (etapes - 1) *
721 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
722 tapesize += etapes + ntrec; /* headers + trailer blks */
723 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
724 tapesize, fetapes);
725 }
726
727 #ifdef USE_QFA
728 if (tapepos) {
729 msg("writing QFA positions to %s\n", gTapeposfile);
730 if ((gTapeposfd = open(gTapeposfile, O_RDWR|O_CREAT)) < 0)
731 quit("can't open tapeposfile\n");
732 /* print QFA-file header */
733 sprintf(gTps, "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
734 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps))
735 quit("can't write tapeposfile\n");
736 sprintf(gTps, "ino\ttapeno\ttapepos\n");
737 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps))
738 quit("can't write tapeposfile\n");
739 }
740 #endif /* USE_QFA */
741
742 /*
743 * Allocate tape buffer.
744 */
745 if (!alloctape())
746 quit(
747 "can't allocate tape buffers - try a smaller blocking factor.\n");
748
749 startnewtape(1);
750 tstart_writing = time(NULL);
751 dumpmap(usedinomap, TS_CLRI, maxino - 1);
752
753 msg("dumping (Pass III) [directories]\n");
754 dirty = 0; /* XXX just to get gcc to shut up */
755 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
756 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
757 dirty = *map++;
758 else
759 dirty >>= 1;
760 if ((dirty & 1) == 0)
761 continue;
762 /*
763 * Skip directory inodes deleted and maybe reallocated
764 */
765 dp = getino(ino);
766 if ((dp->di_mode & IFMT) != IFDIR)
767 continue;
768 #ifdef __linux__
769 /*
770 * Skip directory inodes deleted and not yes reallocated...
771 */
772 if (dp->di_nlink == 0 || dp->di_dtime != 0)
773 continue;
774 (void)dumpdirino(dp, ino);
775 #else
776 (void)dumpino(dp, ino);
777 #endif
778 }
779
780 msg("dumping (Pass IV) [regular files]\n");
781 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
782 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
783 dirty = *map++;
784 else
785 dirty >>= 1;
786 if ((dirty & 1) == 0)
787 continue;
788 /*
789 * Skip inodes deleted and reallocated as directories.
790 */
791 dp = getino(ino);
792 if ((dp->di_mode & IFMT) == IFDIR)
793 continue;
794 #ifdef __linux__
795 /*
796 * No need to check here for deleted and not yet reallocated
797 * inodes since this is done in dumpino().
798 */
799 #endif
800 (void)dumpino(dp, ino);
801 }
802
803 tend_writing = time(NULL);
804 spcl.c_type = TS_END;
805 /*
806 * Finish off the current tape record with trailer blocks, to ensure
807 * at least the data in the last partial record makes it to tape.
808 * Also make sure we write at least 1 trailer block.
809 */
810 for (i = ntrec - (spcl.c_tapea % ntrec); i; --i)
811 writeheader(maxino - 1);
812
813 tnow = trewind();
814
815 if (pipeout)
816 msg("%ld tape blocks (%.2fMB)\n", spcl.c_tapea,
817 ((double)spcl.c_tapea * TP_BSIZE / 1048576));
818 else
819 msg("%ld tape blocks (%.2fMB) on %d volume(s)\n",
820 spcl.c_tapea,
821 ((double)spcl.c_tapea * TP_BSIZE / 1048576),
822 spcl.c_volume);
823
824 /* report dump performance, avoid division by zero */
825 if (tend_writing - tstart_writing == 0)
826 msg("finished in less than a second\n");
827 else
828 msg("finished in %d seconds, throughput %d kBytes/sec\n",
829 tend_writing - tstart_writing,
830 spcl.c_tapea / (tend_writing - tstart_writing));
831
832 putdumptime();
833 msg("Date of this level %c dump: %s", level,
834 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
835 msg("Date this dump completed: %s", ctime(&tnow));
836
837 msg("Average transfer rate: %ld kB/s\n", xferrate / tapeno);
838 if (compressed) {
839 long tapekb = bytes_written / 1024;
840 double rate = .0005 + (double) spcl.c_tapea / tapekb;
841 msg("Wrote %ldkB uncompressed, %ldkB compressed, %1.3f:1\n",
842 spcl.c_tapea, tapekb, rate);
843 }
844
845 broadcast("DUMP IS DONE!\7\7\n");
846 msg("DUMP IS DONE\n");
847 Exit(X_FINOK);
848 /* NOTREACHED */
849 return 0; /* gcc - shut up */
850 }
851
852 static void
853 usage(void)
854 {
855 char white[MAXPATHLEN];
856 const char *ext2ver, *ext2date;
857
858 memset(white, ' ', MAXPATHLEN);
859 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
860
861 #ifdef __linux__
862 ext2fs_get_library_version(&ext2ver, &ext2date);
863 fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
864 __progname, _DUMP_VERSION, ext2ver, ext2date);
865 #else
866 fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
867 #endif
868 fprintf(stderr,
869 "usage:\t%s [-0123456789ac"
870 #ifdef KERBEROS
871 "k"
872 #endif
873 "MnSu"
874 "] [-B records] [-b blocksize] [-d density]\n"
875 "\t%s [-e inode#] [-f file] [-h level] "
876 #ifdef USE_QFA
877 "[-Q file] "
878 #endif
879 "[-s feet]\n"
880 "\t%s [-T date] "
881 #ifdef HAVE_ZLIB
882 "[-z zlevel] "
883 #endif
884 "filesystem\n"
885 "\t%s [-W | -w]\n",
886 __progname, white, white, __progname);
887 exit(X_STARTUP);
888 }
889
890 /*
891 * Pick up a numeric argument. It must be nonnegative and in the given
892 * range (except that a vmax of 0 means unlimited).
893 */
894 static long
895 numarg(const char *meaning, long vmin, long vmax)
896 {
897 char *p;
898 long val;
899
900 val = strtol(optarg, &p, 10);
901 if (*p)
902 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
903 if (val < vmin || (vmax && val > vmax))
904 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
905 return (val);
906 }
907
908 void
909 sig(int signo)
910 {
911 switch(signo) {
912 case SIGALRM:
913 case SIGBUS:
914 case SIGFPE:
915 case SIGHUP:
916 case SIGTERM:
917 case SIGTRAP:
918 if (pipeout)
919 quit("Signal on pipe: cannot recover\n");
920 msg("Rewriting attempted as response to unknown signal.\n");
921 (void)fflush(stderr);
922 (void)fflush(stdout);
923 close_rewind();
924 exit(X_REWRITE);
925 /* NOTREACHED */
926 case SIGSEGV:
927 msg("SIGSEGV: ABORTING!\n");
928 (void)signal(SIGSEGV, SIG_DFL);
929 (void)kill(0, SIGSEGV);
930 /* NOTREACHED */
931 }
932 }
933
934 const char *
935 rawname(const char *cp)
936 {
937 #ifdef __linux__
938 return cp;
939 #else /* __linux__ */
940 static char rawbuf[MAXPATHLEN];
941 char *dp = strrchr(cp, '/');
942
943 if (dp == NULL)
944 return (NULL);
945 (void)strncpy(rawbuf, cp, min(dp-cp, MAXPATHLEN - 1));
946 rawbuf[min(dp-cp, MAXPATHLEN-1)] = '\0';
947 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
948 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
949 return (rawbuf);
950 #endif /* __linux__ */
951 }
952
953 /*
954 * obsolete --
955 * Change set of key letters and ordered arguments into something
956 * getopt(3) will like.
957 */
958 static void
959 obsolete(int *argcp, char **argvp[])
960 {
961 int argc, flags;
962 char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL;
963
964 /* Setup. */
965 argv = *argvp;
966 argc = *argcp;
967
968 /* Return if no arguments or first argument has leading dash. */
969 ap = argv[1];
970 if (argc == 1 || *ap == '-')
971 return;
972
973 /* Allocate space for new arguments. */
974 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
975 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
976 err(X_STARTUP, "malloc new args");
977
978 *nargv++ = *argv;
979 argv += 2;
980
981 for (flags = 0; *ap; ++ap) {
982 switch (*ap) {
983 case 'B':
984 case 'b':
985 case 'd':
986 case 'e':
987 case 'f':
988 case 'F':
989 case 'h':
990 case 'L':
991 case 'Q':
992 case 's':
993 case 'T':
994 if (*argv == NULL) {
995 warnx("option requires an argument -- %c", *ap);
996 usage();
997 }
998 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
999 err(X_STARTUP, "malloc arg");
1000 nargv[0][0] = '-';
1001 nargv[0][1] = *ap;
1002 (void)strcpy(&nargv[0][2], *argv);
1003 ++argv;
1004 ++nargv;
1005 break;
1006 default:
1007 if (!flags) {
1008 *p++ = '-';
1009 flags = 1;
1010 }
1011 *p++ = *ap;
1012 break;
1013 }
1014 }
1015
1016 /* Terminate flags. */
1017 if (flags) {
1018 *p = '\0';
1019 *nargv++ = flagsp;
1020 }
1021
1022 /* Copy remaining arguments. */
1023 while ((*nargv++ = *argv++));
1024
1025 /* Update argument count. */
1026 *argcp = nargv - *argvp - 1;
1027 }