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