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