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