main.c revision 1.32 1 1.32 lukem /* $NetBSD: main.c,v 1.32 2001/05/27 15:07:34 lukem Exp $ */
2 1.6 cgd
3 1.1 cgd /*-
4 1.3 mycroft * Copyright (c) 1980, 1991, 1993, 1994
5 1.3 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.15 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.15 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
39 1.15 lukem The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.6 cgd #if 0
44 1.16 lukem static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
45 1.6 cgd #else
46 1.32 lukem __RCSID("$NetBSD: main.c,v 1.32 2001/05/27 15:07:34 lukem Exp $");
47 1.6 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.3 mycroft #include <sys/param.h>
51 1.3 mycroft #include <sys/time.h>
52 1.14 lukem #include <sys/stat.h>
53 1.14 lukem #include <sys/mount.h>
54 1.14 lukem
55 1.1 cgd #ifdef sunos
56 1.3 mycroft #include <sys/vnode.h>
57 1.3 mycroft
58 1.3 mycroft #include <ufs/inode.h>
59 1.1 cgd #include <ufs/fs.h>
60 1.1 cgd #else
61 1.16 lukem #include <ufs/ufs/dinode.h>
62 1.3 mycroft #include <ufs/ffs/fs.h>
63 1.18 bouyer #include <ufs/ffs/ffs_extern.h>
64 1.1 cgd #endif
65 1.3 mycroft
66 1.1 cgd #include <protocols/dumprestore.h>
67 1.3 mycroft
68 1.3 mycroft #include <ctype.h>
69 1.5 mycroft #include <err.h>
70 1.3 mycroft #include <errno.h>
71 1.1 cgd #include <fcntl.h>
72 1.1 cgd #include <fstab.h>
73 1.3 mycroft #include <signal.h>
74 1.1 cgd #include <stdio.h>
75 1.1 cgd #include <stdlib.h>
76 1.1 cgd #include <string.h>
77 1.10 lukem #include <time.h>
78 1.3 mycroft #include <unistd.h>
79 1.3 mycroft
80 1.1 cgd #include "dump.h"
81 1.1 cgd #include "pathnames.h"
82 1.1 cgd
83 1.27 scw int notify; /* notify operator flag */
84 1.27 scw int blockswritten; /* number of blocks written on current tape */
85 1.27 scw int tapeno; /* current tape number */
86 1.27 scw int density; /* density in bytes/0.1" */
87 1.17 lukem int ntrec = NTREC; /* # tape blocks in each tape record */
88 1.27 scw int cartridge; /* Assume non-cartridge tape */
89 1.17 lukem long dev_bsize = 1; /* recalculated below */
90 1.27 scw long blocksperfile; /* output blocks per file */
91 1.27 scw char *host; /* remote host (if any) */
92 1.22 bouyer int readcache = -1; /* read cache size (in readblksize blks) */
93 1.22 bouyer int readblksize = 32 * 1024; /* read block size */
94 1.1 cgd
95 1.31 lukem int main(int, char *[]);
96 1.31 lukem static long numarg(char *, long, long);
97 1.31 lukem static void obsolete(int *, char **[]);
98 1.31 lukem static void usage(void);
99 1.3 mycroft
100 1.3 mycroft int
101 1.31 lukem main(int argc, char *argv[])
102 1.1 cgd {
103 1.11 lukem ino_t ino;
104 1.11 lukem int dirty;
105 1.11 lukem struct dinode *dp;
106 1.11 lukem struct fstab *dt;
107 1.11 lukem char *map;
108 1.11 lukem int ch;
109 1.3 mycroft int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
110 1.1 cgd ino_t maxino;
111 1.18 bouyer time_t tnow, date;
112 1.14 lukem int dirlist;
113 1.14 lukem char *toplevel;
114 1.17 lukem int just_estimate = 0;
115 1.21 lukem char labelstr[LBLSIZE];
116 1.1 cgd
117 1.1 cgd spcl.c_date = 0;
118 1.3 mycroft (void)time((time_t *)&spcl.c_date);
119 1.1 cgd
120 1.1 cgd tsize = 0; /* Default later, based on 'c' option for cart tapes */
121 1.7 mrg if ((tape = getenv("TAPE")) == NULL)
122 1.7 mrg tape = _PATH_DEFTAPE;
123 1.1 cgd dumpdates = _PATH_DUMPDATES;
124 1.1 cgd temp = _PATH_DTMP;
125 1.21 lukem strcpy(labelstr, "none"); /* XXX safe strcpy. */
126 1.1 cgd if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
127 1.1 cgd quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
128 1.1 cgd level = '0';
129 1.5 mycroft
130 1.5 mycroft if (argc < 2)
131 1.5 mycroft usage();
132 1.5 mycroft
133 1.5 mycroft obsolete(&argc, &argv);
134 1.17 lukem while ((ch = getopt(argc, argv,
135 1.28 tron "0123456789B:b:cd:ef:h:k:L:nr:s:ST:uWw")) != -1)
136 1.5 mycroft switch (ch) {
137 1.5 mycroft /* dump level */
138 1.5 mycroft case '0': case '1': case '2': case '3': case '4':
139 1.5 mycroft case '5': case '6': case '7': case '8': case '9':
140 1.5 mycroft level = ch;
141 1.3 mycroft break;
142 1.1 cgd
143 1.5 mycroft case 'B': /* blocks per output file */
144 1.5 mycroft blocksperfile = numarg("blocks per file", 1L, 0L);
145 1.5 mycroft break;
146 1.1 cgd
147 1.5 mycroft case 'b': /* blocks per tape write */
148 1.5 mycroft ntrec = numarg("blocks per write", 1L, 1000L);
149 1.9 mikel bflag = 1;
150 1.5 mycroft break;
151 1.1 cgd
152 1.5 mycroft case 'c': /* Tape is cart. not 9-track */
153 1.5 mycroft cartridge = 1;
154 1.3 mycroft break;
155 1.1 cgd
156 1.1 cgd case 'd': /* density, in bits per inch */
157 1.5 mycroft density = numarg("density", 10L, 327670L) / 10;
158 1.1 cgd if (density >= 625 && !bflag)
159 1.1 cgd ntrec = HIGHDENSITYTREC;
160 1.28 tron break;
161 1.28 tron
162 1.28 tron case 'e': /* eject full tapes */
163 1.28 tron eflag = 1;
164 1.3 mycroft break;
165 1.1 cgd
166 1.5 mycroft case 'f': /* output file */
167 1.5 mycroft tape = optarg;
168 1.5 mycroft break;
169 1.5 mycroft
170 1.5 mycroft case 'h':
171 1.5 mycroft honorlevel = numarg("honor level", 0L, 10L);
172 1.5 mycroft break;
173 1.5 mycroft
174 1.22 bouyer case 'k':
175 1.22 bouyer readblksize = numarg("read block size", 0, 64) * 1024;
176 1.22 bouyer break;
177 1.22 bouyer
178 1.21 lukem case 'L':
179 1.21 lukem /*
180 1.21 lukem * Note that although there are LBLSIZE characters,
181 1.21 lukem * the last must be '\0', so the limit on strlen()
182 1.21 lukem * is really LBLSIZE-1.
183 1.21 lukem */
184 1.21 lukem strncpy(labelstr, optarg, LBLSIZE);
185 1.21 lukem labelstr[LBLSIZE-1] = '\0';
186 1.21 lukem if (strlen(optarg) > LBLSIZE-1) {
187 1.21 lukem msg(
188 1.21 lukem "WARNING Label `%s' is larger than limit of %d characters.\n",
189 1.21 lukem optarg, LBLSIZE-1);
190 1.21 lukem msg("WARNING: Using truncated label `%s'.\n",
191 1.21 lukem labelstr);
192 1.21 lukem }
193 1.21 lukem break;
194 1.5 mycroft case 'n': /* notify operators */
195 1.5 mycroft notify = 1;
196 1.5 mycroft break;
197 1.5 mycroft
198 1.22 bouyer case 'r': /* read cache size */
199 1.22 bouyer readcache = numarg("read cache size", 0, 512);
200 1.22 bouyer break;
201 1.22 bouyer
202 1.1 cgd case 's': /* tape size, feet */
203 1.5 mycroft tsize = numarg("tape size", 1L, 0L) * 12 * 10;
204 1.3 mycroft break;
205 1.1 cgd
206 1.17 lukem case 'S': /* exit after estimating # of tapes */
207 1.17 lukem just_estimate = 1;
208 1.17 lukem break;
209 1.17 lukem
210 1.1 cgd case 'T': /* time of last dump */
211 1.5 mycroft spcl.c_ddate = unctime(optarg);
212 1.1 cgd if (spcl.c_ddate < 0) {
213 1.3 mycroft (void)fprintf(stderr, "bad time \"%s\"\n",
214 1.5 mycroft optarg);
215 1.3 mycroft exit(X_ABORT);
216 1.1 cgd }
217 1.3 mycroft Tflag = 1;
218 1.1 cgd lastlevel = '?';
219 1.3 mycroft break;
220 1.1 cgd
221 1.1 cgd case 'u': /* update /etc/dumpdates */
222 1.3 mycroft uflag = 1;
223 1.3 mycroft break;
224 1.1 cgd
225 1.5 mycroft case 'W': /* what to do */
226 1.5 mycroft case 'w':
227 1.5 mycroft lastdump(ch);
228 1.5 mycroft exit(0); /* do nothing else */
229 1.1 cgd
230 1.1 cgd default:
231 1.5 mycroft usage();
232 1.1 cgd }
233 1.5 mycroft argc -= optind;
234 1.5 mycroft argv += optind;
235 1.5 mycroft
236 1.1 cgd if (argc < 1) {
237 1.3 mycroft (void)fprintf(stderr, "Must specify disk or filesystem\n");
238 1.3 mycroft exit(X_ABORT);
239 1.1 cgd }
240 1.14 lukem
241 1.14 lukem /*
242 1.14 lukem * determine if disk is a subdirectory, and setup appropriately
243 1.14 lukem */
244 1.14 lukem dirlist = 0;
245 1.14 lukem toplevel = NULL;
246 1.14 lukem for (i = 0; i < argc; i++) {
247 1.14 lukem struct stat sb;
248 1.14 lukem struct statfs fsbuf;
249 1.14 lukem
250 1.14 lukem if (lstat(argv[i], &sb) == -1) {
251 1.14 lukem msg("Cannot lstat %s: %s\n", argv[i], strerror(errno));
252 1.14 lukem exit(X_ABORT);
253 1.14 lukem }
254 1.14 lukem if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
255 1.14 lukem break;
256 1.14 lukem if (statfs(argv[i], &fsbuf) == -1) {
257 1.14 lukem msg("Cannot statfs %s: %s\n", argv[i], strerror(errno));
258 1.14 lukem exit(X_ABORT);
259 1.14 lukem }
260 1.14 lukem if (strcmp(argv[i], fsbuf.f_mntonname) == 0) {
261 1.14 lukem if (dirlist != 0) {
262 1.14 lukem msg("Can't dump a mountpoint and a filelist\n");
263 1.14 lukem exit(X_ABORT);
264 1.14 lukem }
265 1.14 lukem break; /* exit if sole mountpoint */
266 1.14 lukem }
267 1.14 lukem if (!disk) {
268 1.14 lukem if ((toplevel = strdup(fsbuf.f_mntonname)) == NULL) {
269 1.14 lukem msg("Cannot malloc diskname\n");
270 1.14 lukem exit(X_ABORT);
271 1.14 lukem }
272 1.14 lukem disk = toplevel;
273 1.14 lukem if (uflag) {
274 1.14 lukem msg("Ignoring u flag for subdir dump\n");
275 1.14 lukem uflag = 0;
276 1.14 lukem }
277 1.14 lukem if (level > '0') {
278 1.14 lukem msg("Subdir dump is done at level 0\n");
279 1.14 lukem level = '0';
280 1.14 lukem }
281 1.14 lukem msg("Dumping sub files/directories from %s\n", disk);
282 1.14 lukem } else {
283 1.14 lukem if (strcmp(disk, fsbuf.f_mntonname) != 0) {
284 1.14 lukem msg("%s is not on %s\n", argv[i], disk);
285 1.14 lukem exit(X_ABORT);
286 1.14 lukem }
287 1.14 lukem }
288 1.14 lukem msg("Dumping file/directory %s\n", argv[i]);
289 1.14 lukem dirlist++;
290 1.14 lukem }
291 1.14 lukem if (dirlist == 0) {
292 1.14 lukem disk = *argv++;
293 1.14 lukem if (argc != 1) {
294 1.14 lukem (void)fprintf(stderr, "Excess arguments to dump:");
295 1.14 lukem while (--argc)
296 1.14 lukem (void)fprintf(stderr, " %s", *argv++);
297 1.14 lukem (void)fprintf(stderr, "\n");
298 1.14 lukem exit(X_ABORT);
299 1.14 lukem }
300 1.1 cgd }
301 1.1 cgd if (Tflag && uflag) {
302 1.3 mycroft (void)fprintf(stderr,
303 1.1 cgd "You cannot use the T and u flags together.\n");
304 1.3 mycroft exit(X_ABORT);
305 1.1 cgd }
306 1.1 cgd if (strcmp(tape, "-") == 0) {
307 1.1 cgd pipeout++;
308 1.1 cgd tape = "standard output";
309 1.1 cgd }
310 1.1 cgd
311 1.1 cgd if (blocksperfile)
312 1.1 cgd blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
313 1.1 cgd else {
314 1.1 cgd /*
315 1.1 cgd * Determine how to default tape size and density
316 1.1 cgd *
317 1.1 cgd * density tape size
318 1.1 cgd * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
319 1.1 cgd * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
320 1.1 cgd * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
321 1.1 cgd * (450*4 - slop)
322 1.1 cgd */
323 1.1 cgd if (density == 0)
324 1.1 cgd density = cartridge ? 100 : 160;
325 1.1 cgd if (tsize == 0)
326 1.1 cgd tsize = cartridge ? 1700L*120L : 2300L*120L;
327 1.1 cgd }
328 1.1 cgd
329 1.4 mycroft if (strchr(tape, ':')) {
330 1.1 cgd host = tape;
331 1.4 mycroft tape = strchr(host, ':');
332 1.3 mycroft *tape++ = '\0';
333 1.1 cgd #ifdef RDUMP
334 1.1 cgd if (rmthost(host) == 0)
335 1.3 mycroft exit(X_ABORT);
336 1.1 cgd #else
337 1.3 mycroft (void)fprintf(stderr, "remote dump not enabled\n");
338 1.3 mycroft exit(X_ABORT);
339 1.1 cgd #endif
340 1.1 cgd }
341 1.1 cgd
342 1.1 cgd if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
343 1.1 cgd signal(SIGHUP, sig);
344 1.1 cgd if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
345 1.1 cgd signal(SIGTRAP, sig);
346 1.1 cgd if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
347 1.1 cgd signal(SIGFPE, sig);
348 1.1 cgd if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
349 1.1 cgd signal(SIGBUS, sig);
350 1.1 cgd if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
351 1.1 cgd signal(SIGSEGV, sig);
352 1.1 cgd if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
353 1.1 cgd signal(SIGTERM, sig);
354 1.1 cgd if (signal(SIGINT, interrupt) == SIG_IGN)
355 1.1 cgd signal(SIGINT, SIG_IGN);
356 1.1 cgd
357 1.1 cgd set_operators(); /* /etc/group snarfed */
358 1.1 cgd getfstab(); /* /etc/fstab snarfed */
359 1.14 lukem
360 1.1 cgd /*
361 1.1 cgd * disk can be either the full special file name,
362 1.1 cgd * the suffix of the special file name,
363 1.1 cgd * the special name missing the leading '/',
364 1.1 cgd * the file system name with or without the leading '/'.
365 1.1 cgd */
366 1.1 cgd dt = fstabsearch(disk);
367 1.3 mycroft if (dt != NULL) {
368 1.1 cgd disk = rawname(dt->fs_spec);
369 1.3 mycroft (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
370 1.14 lukem if (dirlist != 0)
371 1.14 lukem (void)snprintf(spcl.c_filesys, NAMELEN,
372 1.14 lukem "a subset of %s", dt->fs_file);
373 1.14 lukem else
374 1.14 lukem (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
375 1.1 cgd } else {
376 1.3 mycroft (void)strncpy(spcl.c_dev, disk, NAMELEN);
377 1.3 mycroft (void)strncpy(spcl.c_filesys, "an unlisted file system",
378 1.1 cgd NAMELEN);
379 1.1 cgd }
380 1.21 lukem (void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1);
381 1.3 mycroft (void)gethostname(spcl.c_host, NAMELEN);
382 1.19 mrg spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
383 1.18 bouyer
384 1.18 bouyer if ((diskfd = open(disk, O_RDONLY)) < 0) {
385 1.18 bouyer msg("Cannot open %s\n", disk);
386 1.18 bouyer exit(X_ABORT);
387 1.18 bouyer }
388 1.18 bouyer sync();
389 1.25 perseant
390 1.25 perseant needswap = fs_read_sblock(sblock_buf);
391 1.18 bouyer
392 1.18 bouyer spcl.c_level = iswap32(level - '0');
393 1.18 bouyer spcl.c_type = iswap32(TS_TAPE);
394 1.18 bouyer spcl.c_date = iswap32(spcl.c_date);
395 1.18 bouyer spcl.c_ddate = iswap32(spcl.c_ddate);
396 1.1 cgd if (!Tflag)
397 1.1 cgd getdumptime(); /* /etc/dumpdates snarfed */
398 1.1 cgd
399 1.18 bouyer date = iswap32(spcl.c_date);
400 1.1 cgd msg("Date of this level %c dump: %s", level,
401 1.18 bouyer spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
402 1.18 bouyer date = iswap32(spcl.c_ddate);
403 1.1 cgd msg("Date of last level %c dump: %s", lastlevel,
404 1.18 bouyer spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
405 1.17 lukem msg("Dumping ");
406 1.17 lukem if (dt != NULL && dirlist != 0)
407 1.17 lukem msgtail("a subset of ");
408 1.17 lukem msgtail("%s (%s) ", disk, spcl.c_filesys);
409 1.1 cgd if (host)
410 1.1 cgd msgtail("to %s on host %s\n", tape, host);
411 1.1 cgd else
412 1.1 cgd msgtail("to %s\n", tape);
413 1.21 lukem msg("Label: %s\n", labelstr);
414 1.1 cgd
415 1.25 perseant ufsib = fs_parametrize();
416 1.25 perseant
417 1.1 cgd dev_bshift = ffs(dev_bsize) - 1;
418 1.1 cgd if (dev_bsize != (1 << dev_bshift))
419 1.26 briggs quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
420 1.1 cgd tp_bshift = ffs(TP_BSIZE) - 1;
421 1.1 cgd if (TP_BSIZE != (1 << tp_bshift))
422 1.1 cgd quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
423 1.25 perseant maxino = fs_maxino();
424 1.3 mycroft mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
425 1.1 cgd usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
426 1.1 cgd dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
427 1.1 cgd dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
428 1.1 cgd tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
429 1.1 cgd
430 1.18 bouyer nonodump = iswap32(spcl.c_level) < honorlevel;
431 1.3 mycroft
432 1.22 bouyer initcache(readcache, readblksize);
433 1.22 bouyer
434 1.14 lukem (void)signal(SIGINFO, statussig);
435 1.14 lukem
436 1.1 cgd msg("mapping (Pass I) [regular files]\n");
437 1.14 lukem anydirskipped = mapfiles(maxino, &tapesize, toplevel,
438 1.14 lukem (dirlist ? argv : NULL));
439 1.1 cgd
440 1.1 cgd msg("mapping (Pass II) [directories]\n");
441 1.1 cgd while (anydirskipped) {
442 1.1 cgd anydirskipped = mapdirs(maxino, &tapesize);
443 1.1 cgd }
444 1.1 cgd
445 1.3 mycroft if (pipeout) {
446 1.1 cgd tapesize += 10; /* 10 trailer blocks */
447 1.3 mycroft msg("estimated %ld tape blocks.\n", tapesize);
448 1.3 mycroft } else {
449 1.3 mycroft double fetapes;
450 1.3 mycroft
451 1.1 cgd if (blocksperfile)
452 1.3 mycroft fetapes = (double) tapesize / blocksperfile;
453 1.1 cgd else if (cartridge) {
454 1.1 cgd /* Estimate number of tapes, assuming streaming stops at
455 1.1 cgd the end of each block written, and not in mid-block.
456 1.1 cgd Assume no erroneous blocks; this can be compensated
457 1.1 cgd for with an artificially low tape size. */
458 1.1 cgd fetapes =
459 1.1 cgd ( tapesize /* blocks */
460 1.1 cgd * TP_BSIZE /* bytes/block */
461 1.1 cgd * (1.0/density) /* 0.1" / byte */
462 1.1 cgd +
463 1.1 cgd tapesize /* blocks */
464 1.1 cgd * (1.0/ntrec) /* streaming-stops per block */
465 1.1 cgd * 15.48 /* 0.1" / streaming-stop */
466 1.1 cgd ) * (1.0 / tsize ); /* tape / 0.1" */
467 1.1 cgd } else {
468 1.1 cgd /* Estimate number of tapes, for old fashioned 9-track
469 1.1 cgd tape */
470 1.1 cgd int tenthsperirg = (density == 625) ? 3 : 7;
471 1.1 cgd fetapes =
472 1.1 cgd ( tapesize /* blocks */
473 1.1 cgd * TP_BSIZE /* bytes / block */
474 1.1 cgd * (1.0/density) /* 0.1" / byte */
475 1.1 cgd +
476 1.1 cgd tapesize /* blocks */
477 1.1 cgd * (1.0/ntrec) /* IRG's / block */
478 1.1 cgd * tenthsperirg /* 0.1" / IRG */
479 1.1 cgd ) * (1.0 / tsize ); /* tape / 0.1" */
480 1.1 cgd }
481 1.1 cgd etapes = fetapes; /* truncating assignment */
482 1.1 cgd etapes++;
483 1.1 cgd /* count the dumped inodes map on each additional tape */
484 1.1 cgd tapesize += (etapes - 1) *
485 1.1 cgd (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
486 1.1 cgd tapesize += etapes + 10; /* headers + 10 trailer blks */
487 1.1 cgd msg("estimated %ld tape blocks on %3.2f tape(s).\n",
488 1.1 cgd tapesize, fetapes);
489 1.3 mycroft }
490 1.17 lukem /*
491 1.17 lukem * If the user only wants an estimate of the number of
492 1.17 lukem * tapes, exit now.
493 1.17 lukem */
494 1.17 lukem if (just_estimate)
495 1.17 lukem exit(0);
496 1.1 cgd
497 1.1 cgd /*
498 1.3 mycroft * Allocate tape buffer.
499 1.1 cgd */
500 1.1 cgd if (!alloctape())
501 1.1 cgd quit("can't allocate tape buffers - try a smaller blocking factor.\n");
502 1.1 cgd
503 1.1 cgd startnewtape(1);
504 1.3 mycroft (void)time((time_t *)&(tstart_writing));
505 1.10 lukem xferrate = 0;
506 1.3 mycroft dumpmap(usedinomap, TS_CLRI, maxino - 1);
507 1.1 cgd
508 1.1 cgd msg("dumping (Pass III) [directories]\n");
509 1.3 mycroft dirty = 0; /* XXX just to get gcc to shut up */
510 1.3 mycroft for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
511 1.3 mycroft if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
512 1.1 cgd dirty = *map++;
513 1.1 cgd else
514 1.1 cgd dirty >>= 1;
515 1.1 cgd if ((dirty & 1) == 0)
516 1.1 cgd continue;
517 1.1 cgd /*
518 1.1 cgd * Skip directory inodes deleted and maybe reallocated
519 1.1 cgd */
520 1.1 cgd dp = getino(ino);
521 1.1 cgd if ((dp->di_mode & IFMT) != IFDIR)
522 1.1 cgd continue;
523 1.3 mycroft (void)dumpino(dp, ino);
524 1.1 cgd }
525 1.1 cgd
526 1.1 cgd msg("dumping (Pass IV) [regular files]\n");
527 1.3 mycroft for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
528 1.3 mycroft int mode;
529 1.3 mycroft
530 1.3 mycroft if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
531 1.1 cgd dirty = *map++;
532 1.1 cgd else
533 1.1 cgd dirty >>= 1;
534 1.1 cgd if ((dirty & 1) == 0)
535 1.1 cgd continue;
536 1.1 cgd /*
537 1.1 cgd * Skip inodes deleted and reallocated as directories.
538 1.1 cgd */
539 1.1 cgd dp = getino(ino);
540 1.3 mycroft mode = dp->di_mode & IFMT;
541 1.3 mycroft if (mode == IFDIR)
542 1.1 cgd continue;
543 1.3 mycroft (void)dumpino(dp, ino);
544 1.1 cgd }
545 1.1 cgd
546 1.18 bouyer spcl.c_type = iswap32(TS_END);
547 1.1 cgd for (i = 0; i < ntrec; i++)
548 1.3 mycroft writeheader(maxino - 1);
549 1.1 cgd if (pipeout)
550 1.26 briggs msg("%d tape blocks\n",iswap32(spcl.c_tapea));
551 1.1 cgd else
552 1.26 briggs msg("%d tape blocks on %d volume%s\n",
553 1.18 bouyer iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
554 1.18 bouyer (iswap32(spcl.c_volume) == 1) ? "" : "s");
555 1.10 lukem tnow = do_stats();
556 1.18 bouyer date = iswap32(spcl.c_date);
557 1.10 lukem msg("Date of this level %c dump: %s", level,
558 1.18 bouyer spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
559 1.10 lukem msg("Date this dump completed: %s", ctime(&tnow));
560 1.26 briggs msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
561 1.1 cgd putdumptime();
562 1.29 tron trewind(0);
563 1.1 cgd broadcast("DUMP IS DONE!\7\7\n");
564 1.1 cgd msg("DUMP IS DONE\n");
565 1.1 cgd Exit(X_FINOK);
566 1.1 cgd /* NOTREACHED */
567 1.15 lukem exit(X_FINOK); /* XXX: to satisfy gcc */
568 1.1 cgd }
569 1.1 cgd
570 1.5 mycroft static void
571 1.31 lukem usage(void)
572 1.5 mycroft {
573 1.5 mycroft
574 1.22 bouyer (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
575 1.30 tron "usage: dump [-0123456789cenu] [-B records] [-b blocksize] [-d density]",
576 1.22 bouyer " [-f file] [-h level] [-k read block size] [-L label]",
577 1.22 bouyer " [-r read cache size] [-s feet] [-T date] filesystem",
578 1.21 lukem " dump [-W | -w]");
579 1.5 mycroft exit(1);
580 1.5 mycroft }
581 1.5 mycroft
582 1.3 mycroft /*
583 1.3 mycroft * Pick up a numeric argument. It must be nonnegative and in the given
584 1.3 mycroft * range (except that a vmax of 0 means unlimited).
585 1.3 mycroft */
586 1.3 mycroft static long
587 1.32 lukem numarg(char *meaning, long vmin, long vmax)
588 1.3 mycroft {
589 1.5 mycroft char *p;
590 1.3 mycroft long val;
591 1.3 mycroft
592 1.5 mycroft val = strtol(optarg, &p, 10);
593 1.5 mycroft if (*p)
594 1.5 mycroft errx(1, "illegal %s -- %s", meaning, optarg);
595 1.3 mycroft if (val < vmin || (vmax && val > vmax))
596 1.5 mycroft errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
597 1.3 mycroft return (val);
598 1.3 mycroft }
599 1.3 mycroft
600 1.1 cgd void
601 1.32 lukem sig(int signo)
602 1.1 cgd {
603 1.32 lukem
604 1.1 cgd switch(signo) {
605 1.1 cgd case SIGALRM:
606 1.1 cgd case SIGBUS:
607 1.1 cgd case SIGFPE:
608 1.1 cgd case SIGHUP:
609 1.1 cgd case SIGTERM:
610 1.1 cgd case SIGTRAP:
611 1.1 cgd if (pipeout)
612 1.1 cgd quit("Signal on pipe: cannot recover\n");
613 1.1 cgd msg("Rewriting attempted as response to unknown signal.\n");
614 1.3 mycroft (void)fflush(stderr);
615 1.3 mycroft (void)fflush(stdout);
616 1.1 cgd close_rewind();
617 1.3 mycroft exit(X_REWRITE);
618 1.1 cgd /* NOTREACHED */
619 1.1 cgd case SIGSEGV:
620 1.1 cgd msg("SIGSEGV: ABORTING!\n");
621 1.3 mycroft (void)signal(SIGSEGV, SIG_DFL);
622 1.3 mycroft (void)kill(0, SIGSEGV);
623 1.1 cgd /* NOTREACHED */
624 1.1 cgd }
625 1.1 cgd }
626 1.1 cgd
627 1.1 cgd char *
628 1.32 lukem rawname(char *cp)
629 1.1 cgd {
630 1.1 cgd static char rawbuf[MAXPATHLEN];
631 1.4 mycroft char *dp = strrchr(cp, '/');
632 1.1 cgd
633 1.3 mycroft if (dp == NULL)
634 1.3 mycroft return (NULL);
635 1.3 mycroft *dp = '\0';
636 1.12 mrg (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
637 1.1 cgd *dp = '/';
638 1.1 cgd return (rawbuf);
639 1.1 cgd }
640 1.1 cgd
641 1.5 mycroft /*
642 1.5 mycroft * obsolete --
643 1.5 mycroft * Change set of key letters and ordered arguments into something
644 1.5 mycroft * getopt(3) will like.
645 1.5 mycroft */
646 1.5 mycroft static void
647 1.32 lukem obsolete(int *argcp, char **argvp[])
648 1.1 cgd {
649 1.5 mycroft int argc, flags;
650 1.5 mycroft char *ap, **argv, *flagsp, **nargv, *p;
651 1.5 mycroft
652 1.5 mycroft /* Setup. */
653 1.5 mycroft argv = *argvp;
654 1.5 mycroft argc = *argcp;
655 1.5 mycroft
656 1.5 mycroft /* Return if no arguments or first argument has leading dash. */
657 1.5 mycroft ap = argv[1];
658 1.5 mycroft if (argc == 1 || *ap == '-')
659 1.5 mycroft return;
660 1.5 mycroft
661 1.5 mycroft /* Allocate space for new arguments. */
662 1.5 mycroft if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
663 1.5 mycroft (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
664 1.14 lukem err(1, "malloc");
665 1.5 mycroft
666 1.5 mycroft *nargv++ = *argv;
667 1.5 mycroft argv += 2;
668 1.5 mycroft
669 1.5 mycroft for (flags = 0; *ap; ++ap) {
670 1.5 mycroft switch (*ap) {
671 1.5 mycroft case 'B':
672 1.5 mycroft case 'b':
673 1.5 mycroft case 'd':
674 1.5 mycroft case 'f':
675 1.5 mycroft case 'h':
676 1.5 mycroft case 's':
677 1.5 mycroft case 'T':
678 1.5 mycroft if (*argv == NULL) {
679 1.5 mycroft warnx("option requires an argument -- %c", *ap);
680 1.5 mycroft usage();
681 1.5 mycroft }
682 1.5 mycroft if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
683 1.14 lukem err(1, "malloc");
684 1.5 mycroft nargv[0][0] = '-';
685 1.5 mycroft nargv[0][1] = *ap;
686 1.17 lukem (void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
687 1.5 mycroft ++argv;
688 1.5 mycroft ++nargv;
689 1.5 mycroft break;
690 1.5 mycroft default:
691 1.5 mycroft if (!flags) {
692 1.5 mycroft *p++ = '-';
693 1.5 mycroft flags = 1;
694 1.5 mycroft }
695 1.5 mycroft *p++ = *ap;
696 1.5 mycroft break;
697 1.5 mycroft }
698 1.5 mycroft }
699 1.1 cgd
700 1.5 mycroft /* Terminate flags. */
701 1.5 mycroft if (flags) {
702 1.5 mycroft *p = '\0';
703 1.5 mycroft *nargv++ = flagsp;
704 1.5 mycroft }
705 1.5 mycroft
706 1.5 mycroft /* Copy remaining arguments. */
707 1.14 lukem while ((*nargv++ = *argv++) != NULL)
708 1.14 lukem ;
709 1.5 mycroft
710 1.5 mycroft /* Update argument count. */
711 1.5 mycroft *argcp = nargv - *argvp - 1;
712 1.1 cgd }
713