main.c revision 1.51 1 1.51 itojun /* $NetBSD: main.c,v 1.51 2002/11/16 14:15:35 itojun 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.51 itojun __RCSID("$NetBSD: main.c,v 1.51 2002/11/16 14:15:35 itojun 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.16 lukem #include <ufs/ufs/dinode.h>
56 1.3 mycroft #include <ufs/ffs/fs.h>
57 1.18 bouyer #include <ufs/ffs/ffs_extern.h>
58 1.3 mycroft
59 1.1 cgd #include <protocols/dumprestore.h>
60 1.3 mycroft
61 1.3 mycroft #include <ctype.h>
62 1.5 mycroft #include <err.h>
63 1.3 mycroft #include <errno.h>
64 1.1 cgd #include <fcntl.h>
65 1.1 cgd #include <fstab.h>
66 1.3 mycroft #include <signal.h>
67 1.1 cgd #include <stdio.h>
68 1.1 cgd #include <stdlib.h>
69 1.1 cgd #include <string.h>
70 1.10 lukem #include <time.h>
71 1.3 mycroft #include <unistd.h>
72 1.3 mycroft
73 1.1 cgd #include "dump.h"
74 1.1 cgd #include "pathnames.h"
75 1.1 cgd
76 1.37 david gid_t egid; /* Retain tty privs for notification */
77 1.44 lukem int timestamp; /* print message timestamps */
78 1.27 scw int notify; /* notify operator flag */
79 1.27 scw int blockswritten; /* number of blocks written on current tape */
80 1.27 scw int tapeno; /* current tape number */
81 1.27 scw int density; /* density in bytes/0.1" */
82 1.17 lukem int ntrec = NTREC; /* # tape blocks in each tape record */
83 1.27 scw int cartridge; /* Assume non-cartridge tape */
84 1.17 lukem long dev_bsize = 1; /* recalculated below */
85 1.27 scw long blocksperfile; /* output blocks per file */
86 1.27 scw char *host; /* remote host (if any) */
87 1.22 bouyer int readcache = -1; /* read cache size (in readblksize blks) */
88 1.22 bouyer int readblksize = 32 * 1024; /* read block size */
89 1.39 blymn char default_time_string[] = "%T %Z"; /* default timestamp string */
90 1.39 blymn char *time_string = default_time_string; /* timestamp string */
91 1.1 cgd
92 1.31 lukem int main(int, char *[]);
93 1.31 lukem static long numarg(char *, long, long);
94 1.31 lukem static void obsolete(int *, char **[]);
95 1.31 lukem static void usage(void);
96 1.3 mycroft
97 1.3 mycroft int
98 1.31 lukem main(int argc, char *argv[])
99 1.1 cgd {
100 1.11 lukem ino_t ino;
101 1.45 lukem int dirty;
102 1.11 lukem struct dinode *dp;
103 1.33 lukem struct fstab *dt;
104 1.33 lukem struct statfs *mntinfo, fsbuf;
105 1.11 lukem char *map;
106 1.11 lukem int ch;
107 1.38 lukem int i, anydirskipped, bflag = 0, Tflag = 0, Fflag = 0, honorlevel = 1;
108 1.1 cgd ino_t maxino;
109 1.18 bouyer time_t tnow, date;
110 1.33 lukem int dirc;
111 1.33 lukem char *mountpoint;
112 1.17 lukem int just_estimate = 0;
113 1.21 lukem char labelstr[LBLSIZE];
114 1.39 blymn char *new_time_format;
115 1.45 lukem
116 1.1 cgd spcl.c_date = 0;
117 1.47 lukem (void)time(&tnow);
118 1.47 lukem spcl.c_date = tnow;
119 1.39 blymn tzset(); /* set up timezone for strftime */
120 1.39 blymn if ((new_time_format = getenv("TIMEFORMAT")) != NULL)
121 1.39 blymn time_string = new_time_format;
122 1.45 lukem
123 1.37 david /* Save setgid bit for use later */
124 1.37 david egid = getegid();
125 1.37 david setegid(getgid());
126 1.37 david
127 1.1 cgd tsize = 0; /* Default later, based on 'c' option for cart tapes */
128 1.7 mrg if ((tape = getenv("TAPE")) == NULL)
129 1.7 mrg tape = _PATH_DEFTAPE;
130 1.1 cgd dumpdates = _PATH_DUMPDATES;
131 1.1 cgd temp = _PATH_DTMP;
132 1.21 lukem strcpy(labelstr, "none"); /* XXX safe strcpy. */
133 1.1 cgd if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
134 1.1 cgd quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
135 1.1 cgd level = '0';
136 1.39 blymn timestamp = 0;
137 1.45 lukem
138 1.5 mycroft if (argc < 2)
139 1.5 mycroft usage();
140 1.5 mycroft
141 1.5 mycroft obsolete(&argc, &argv);
142 1.17 lukem while ((ch = getopt(argc, argv,
143 1.50 bouyer "0123456789aB:b:cd:eFf:h:k:l:L:nr:s:StT:uWw")) != -1)
144 1.5 mycroft switch (ch) {
145 1.5 mycroft /* dump level */
146 1.5 mycroft case '0': case '1': case '2': case '3': case '4':
147 1.5 mycroft case '5': case '6': case '7': case '8': case '9':
148 1.5 mycroft level = ch;
149 1.3 mycroft break;
150 1.1 cgd
151 1.49 lukem case 'a': /* `auto-size', Write to EOM. */
152 1.49 lukem unlimited = 1;
153 1.49 lukem break;
154 1.49 lukem
155 1.5 mycroft case 'B': /* blocks per output file */
156 1.5 mycroft blocksperfile = numarg("blocks per file", 1L, 0L);
157 1.5 mycroft break;
158 1.1 cgd
159 1.5 mycroft case 'b': /* blocks per tape write */
160 1.5 mycroft ntrec = numarg("blocks per write", 1L, 1000L);
161 1.9 mikel bflag = 1;
162 1.5 mycroft break;
163 1.1 cgd
164 1.5 mycroft case 'c': /* Tape is cart. not 9-track */
165 1.5 mycroft cartridge = 1;
166 1.3 mycroft break;
167 1.1 cgd
168 1.1 cgd case 'd': /* density, in bits per inch */
169 1.5 mycroft density = numarg("density", 10L, 327670L) / 10;
170 1.1 cgd if (density >= 625 && !bflag)
171 1.1 cgd ntrec = HIGHDENSITYTREC;
172 1.28 tron break;
173 1.28 tron
174 1.38 lukem case 'e': /* eject full tapes */
175 1.28 tron eflag = 1;
176 1.3 mycroft break;
177 1.1 cgd
178 1.38 lukem case 'F': /* files-to-dump is an fs image */
179 1.38 lukem Fflag = 1;
180 1.38 lukem break;
181 1.38 lukem
182 1.5 mycroft case 'f': /* output file */
183 1.5 mycroft tape = optarg;
184 1.5 mycroft break;
185 1.5 mycroft
186 1.5 mycroft case 'h':
187 1.5 mycroft honorlevel = numarg("honor level", 0L, 10L);
188 1.5 mycroft break;
189 1.5 mycroft
190 1.22 bouyer case 'k':
191 1.22 bouyer readblksize = numarg("read block size", 0, 64) * 1024;
192 1.22 bouyer break;
193 1.22 bouyer
194 1.41 bouyer case 'l': /* autoload after eject full tapes */
195 1.41 bouyer eflag = 1;
196 1.50 bouyer lflag = numarg("timeout (in seconds)", 1, 0);
197 1.41 bouyer break;
198 1.41 bouyer
199 1.21 lukem case 'L':
200 1.21 lukem /*
201 1.21 lukem * Note that although there are LBLSIZE characters,
202 1.21 lukem * the last must be '\0', so the limit on strlen()
203 1.21 lukem * is really LBLSIZE-1.
204 1.21 lukem */
205 1.51 itojun if (strlcpy(labelstr, optarg, sizeof(labelstr))
206 1.51 itojun >= sizeof(labelstr)) {
207 1.21 lukem msg(
208 1.21 lukem "WARNING Label `%s' is larger than limit of %d characters.\n",
209 1.51 itojun optarg, sizeof(labelstr) - 1);
210 1.21 lukem msg("WARNING: Using truncated label `%s'.\n",
211 1.21 lukem labelstr);
212 1.21 lukem }
213 1.21 lukem break;
214 1.5 mycroft case 'n': /* notify operators */
215 1.5 mycroft notify = 1;
216 1.5 mycroft break;
217 1.5 mycroft
218 1.22 bouyer case 'r': /* read cache size */
219 1.22 bouyer readcache = numarg("read cache size", 0, 512);
220 1.22 bouyer break;
221 1.45 lukem
222 1.1 cgd case 's': /* tape size, feet */
223 1.5 mycroft tsize = numarg("tape size", 1L, 0L) * 12 * 10;
224 1.3 mycroft break;
225 1.1 cgd
226 1.17 lukem case 'S': /* exit after estimating # of tapes */
227 1.17 lukem just_estimate = 1;
228 1.17 lukem break;
229 1.17 lukem
230 1.39 blymn case 't':
231 1.39 blymn timestamp = 1;
232 1.39 blymn break;
233 1.39 blymn
234 1.1 cgd case 'T': /* time of last dump */
235 1.5 mycroft spcl.c_ddate = unctime(optarg);
236 1.1 cgd if (spcl.c_ddate < 0) {
237 1.3 mycroft (void)fprintf(stderr, "bad time \"%s\"\n",
238 1.5 mycroft optarg);
239 1.48 lukem exit(X_STARTUP);
240 1.1 cgd }
241 1.3 mycroft Tflag = 1;
242 1.1 cgd lastlevel = '?';
243 1.3 mycroft break;
244 1.1 cgd
245 1.1 cgd case 'u': /* update /etc/dumpdates */
246 1.3 mycroft uflag = 1;
247 1.3 mycroft break;
248 1.1 cgd
249 1.5 mycroft case 'W': /* what to do */
250 1.5 mycroft case 'w':
251 1.5 mycroft lastdump(ch);
252 1.48 lukem exit(X_FINOK); /* do nothing else */
253 1.1 cgd
254 1.1 cgd default:
255 1.5 mycroft usage();
256 1.1 cgd }
257 1.5 mycroft argc -= optind;
258 1.5 mycroft argv += optind;
259 1.5 mycroft
260 1.1 cgd if (argc < 1) {
261 1.38 lukem (void)fprintf(stderr,
262 1.38 lukem "Must specify disk or image, or file list\n");
263 1.48 lukem exit(X_STARTUP);
264 1.1 cgd }
265 1.14 lukem
266 1.33 lukem
267 1.14 lukem /*
268 1.14 lukem * determine if disk is a subdirectory, and setup appropriately
269 1.14 lukem */
270 1.33 lukem getfstab(); /* /etc/fstab snarfed */
271 1.33 lukem disk = NULL;
272 1.33 lukem mountpoint = NULL;
273 1.33 lukem dirc = 0;
274 1.14 lukem for (i = 0; i < argc; i++) {
275 1.14 lukem struct stat sb;
276 1.14 lukem
277 1.33 lukem if (lstat(argv[i], &sb) == -1)
278 1.33 lukem quit("Cannot stat %s: %s\n", argv[i], strerror(errno));
279 1.38 lukem if (Fflag || S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
280 1.33 lukem disk = argv[i];
281 1.33 lukem multicheck:
282 1.33 lukem if (dirc != 0)
283 1.33 lukem quit(
284 1.38 lukem "Can't dump a disk or image at the same time as a file list\n");
285 1.14 lukem break;
286 1.14 lukem }
287 1.33 lukem if ((dt = fstabsearch(argv[i])) != NULL) {
288 1.33 lukem disk = dt->fs_spec;
289 1.34 hannken mountpoint = xstrdup(dt->fs_file);
290 1.33 lukem goto multicheck;
291 1.14 lukem }
292 1.33 lukem if (statfs(argv[i], &fsbuf) == -1)
293 1.33 lukem quit("Cannot statfs %s: %s\n", argv[i],
294 1.33 lukem strerror(errno));
295 1.33 lukem disk = fsbuf.f_mntfromname;
296 1.33 lukem if (strcmp(argv[i], fsbuf.f_mntonname) == 0)
297 1.33 lukem goto multicheck;
298 1.33 lukem if (mountpoint == NULL) {
299 1.33 lukem mountpoint = xstrdup(fsbuf.f_mntonname);
300 1.14 lukem if (uflag) {
301 1.14 lukem msg("Ignoring u flag for subdir dump\n");
302 1.14 lukem uflag = 0;
303 1.14 lukem }
304 1.14 lukem if (level > '0') {
305 1.14 lukem msg("Subdir dump is done at level 0\n");
306 1.14 lukem level = '0';
307 1.14 lukem }
308 1.33 lukem msg("Dumping sub files/directories from %s\n",
309 1.33 lukem mountpoint);
310 1.14 lukem } else {
311 1.33 lukem if (strcmp(mountpoint, fsbuf.f_mntonname) != 0)
312 1.33 lukem quit("%s is not on %s\n", argv[i], mountpoint);
313 1.14 lukem }
314 1.14 lukem msg("Dumping file/directory %s\n", argv[i]);
315 1.33 lukem dirc++;
316 1.14 lukem }
317 1.33 lukem if (mountpoint)
318 1.33 lukem free(mountpoint);
319 1.33 lukem
320 1.33 lukem if (dirc == 0) {
321 1.33 lukem argv++;
322 1.14 lukem if (argc != 1) {
323 1.14 lukem (void)fprintf(stderr, "Excess arguments to dump:");
324 1.14 lukem while (--argc)
325 1.14 lukem (void)fprintf(stderr, " %s", *argv++);
326 1.14 lukem (void)fprintf(stderr, "\n");
327 1.48 lukem exit(X_STARTUP);
328 1.14 lukem }
329 1.1 cgd }
330 1.1 cgd if (Tflag && uflag) {
331 1.44 lukem (void)fprintf(stderr,
332 1.1 cgd "You cannot use the T and u flags together.\n");
333 1.48 lukem exit(X_STARTUP);
334 1.1 cgd }
335 1.1 cgd if (strcmp(tape, "-") == 0) {
336 1.1 cgd pipeout++;
337 1.1 cgd tape = "standard output";
338 1.1 cgd }
339 1.1 cgd
340 1.1 cgd if (blocksperfile)
341 1.1 cgd blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
342 1.49 lukem else if (!unlimited) {
343 1.1 cgd /*
344 1.1 cgd * Determine how to default tape size and density
345 1.1 cgd *
346 1.44 lukem * density tape size
347 1.1 cgd * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
348 1.1 cgd * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
349 1.1 cgd * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
350 1.1 cgd * (450*4 - slop)
351 1.1 cgd */
352 1.1 cgd if (density == 0)
353 1.1 cgd density = cartridge ? 100 : 160;
354 1.1 cgd if (tsize == 0)
355 1.1 cgd tsize = cartridge ? 1700L*120L : 2300L*120L;
356 1.1 cgd }
357 1.1 cgd
358 1.4 mycroft if (strchr(tape, ':')) {
359 1.1 cgd host = tape;
360 1.4 mycroft tape = strchr(host, ':');
361 1.3 mycroft *tape++ = '\0';
362 1.1 cgd #ifdef RDUMP
363 1.1 cgd if (rmthost(host) == 0)
364 1.48 lukem exit(X_STARTUP);
365 1.1 cgd #else
366 1.3 mycroft (void)fprintf(stderr, "remote dump not enabled\n");
367 1.48 lukem exit(X_STARTUP);
368 1.1 cgd #endif
369 1.1 cgd }
370 1.1 cgd
371 1.1 cgd if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
372 1.1 cgd signal(SIGHUP, sig);
373 1.1 cgd if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
374 1.1 cgd signal(SIGTRAP, sig);
375 1.1 cgd if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
376 1.1 cgd signal(SIGFPE, sig);
377 1.1 cgd if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
378 1.1 cgd signal(SIGBUS, sig);
379 1.1 cgd if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
380 1.1 cgd signal(SIGSEGV, sig);
381 1.1 cgd if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
382 1.1 cgd signal(SIGTERM, sig);
383 1.1 cgd if (signal(SIGINT, interrupt) == SIG_IGN)
384 1.1 cgd signal(SIGINT, SIG_IGN);
385 1.1 cgd
386 1.1 cgd set_operators(); /* /etc/group snarfed */
387 1.14 lukem
388 1.1 cgd /*
389 1.33 lukem * disk can be either the full special file name, or
390 1.33 lukem * the file system name.
391 1.1 cgd */
392 1.33 lukem mountpoint = NULL;
393 1.33 lukem if ((dt = fstabsearch(disk)) != NULL) {
394 1.1 cgd disk = rawname(dt->fs_spec);
395 1.33 lukem mountpoint = dt->fs_file;
396 1.33 lukem msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB);
397 1.33 lukem } else if ((mntinfo = mntinfosearch(disk)) != NULL) {
398 1.33 lukem disk = rawname(mntinfo->f_mntfromname);
399 1.33 lukem mountpoint = mntinfo->f_mntonname;
400 1.33 lukem msg("Found %s on %s in mount table\n", disk, mountpoint);
401 1.33 lukem }
402 1.33 lukem if (mountpoint != NULL) {
403 1.33 lukem if (dirc != 0)
404 1.51 itojun (void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
405 1.33 lukem "a subset of %s", mountpoint);
406 1.14 lukem else
407 1.51 itojun (void)strlcpy(spcl.c_filesys, mountpoint,
408 1.51 itojun sizeof(spcl.c_filesys));
409 1.38 lukem } else if (Fflag) {
410 1.51 itojun (void)strlcpy(spcl.c_filesys, "a file system image",
411 1.51 itojun sizeof(spcl.c_filesys));
412 1.1 cgd } else {
413 1.51 itojun (void)strlcpy(spcl.c_filesys, "an unlisted file system",
414 1.51 itojun sizeof(spcl.c_filesys));
415 1.1 cgd }
416 1.51 itojun (void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev));
417 1.51 itojun (void)strlcpy(spcl.c_label, labelstr, sizeof(spcl.c_label));
418 1.51 itojun (void)gethostname(spcl.c_host, sizeof(spcl.c_host));
419 1.19 mrg spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
420 1.18 bouyer
421 1.18 bouyer if ((diskfd = open(disk, O_RDONLY)) < 0) {
422 1.18 bouyer msg("Cannot open %s\n", disk);
423 1.48 lukem exit(X_STARTUP);
424 1.18 bouyer }
425 1.18 bouyer sync();
426 1.25 perseant
427 1.25 perseant needswap = fs_read_sblock(sblock_buf);
428 1.18 bouyer
429 1.18 bouyer spcl.c_level = iswap32(level - '0');
430 1.18 bouyer spcl.c_type = iswap32(TS_TAPE);
431 1.18 bouyer spcl.c_date = iswap32(spcl.c_date);
432 1.18 bouyer spcl.c_ddate = iswap32(spcl.c_ddate);
433 1.1 cgd if (!Tflag)
434 1.44 lukem getdumptime(); /* /etc/dumpdates snarfed */
435 1.1 cgd
436 1.18 bouyer date = iswap32(spcl.c_date);
437 1.1 cgd msg("Date of this level %c dump: %s", level,
438 1.18 bouyer spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
439 1.18 bouyer date = iswap32(spcl.c_ddate);
440 1.1 cgd msg("Date of last level %c dump: %s", lastlevel,
441 1.18 bouyer spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
442 1.17 lukem msg("Dumping ");
443 1.33 lukem if (dirc != 0)
444 1.17 lukem msgtail("a subset of ");
445 1.17 lukem msgtail("%s (%s) ", disk, spcl.c_filesys);
446 1.1 cgd if (host)
447 1.1 cgd msgtail("to %s on host %s\n", tape, host);
448 1.1 cgd else
449 1.1 cgd msgtail("to %s\n", tape);
450 1.21 lukem msg("Label: %s\n", labelstr);
451 1.1 cgd
452 1.25 perseant ufsib = fs_parametrize();
453 1.25 perseant
454 1.1 cgd dev_bshift = ffs(dev_bsize) - 1;
455 1.1 cgd if (dev_bsize != (1 << dev_bshift))
456 1.26 briggs quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
457 1.1 cgd tp_bshift = ffs(TP_BSIZE) - 1;
458 1.1 cgd if (TP_BSIZE != (1 << tp_bshift))
459 1.1 cgd quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
460 1.25 perseant maxino = fs_maxino();
461 1.3 mycroft mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
462 1.33 lukem usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
463 1.33 lukem dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
464 1.33 lukem dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
465 1.1 cgd tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
466 1.1 cgd
467 1.18 bouyer nonodump = iswap32(spcl.c_level) < honorlevel;
468 1.3 mycroft
469 1.22 bouyer initcache(readcache, readblksize);
470 1.45 lukem
471 1.14 lukem (void)signal(SIGINFO, statussig);
472 1.14 lukem
473 1.1 cgd msg("mapping (Pass I) [regular files]\n");
474 1.33 lukem anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
475 1.33 lukem (dirc ? argv : NULL));
476 1.1 cgd
477 1.1 cgd msg("mapping (Pass II) [directories]\n");
478 1.1 cgd while (anydirskipped) {
479 1.1 cgd anydirskipped = mapdirs(maxino, &tapesize);
480 1.1 cgd }
481 1.1 cgd
482 1.49 lukem if (pipeout || unlimited) {
483 1.1 cgd tapesize += 10; /* 10 trailer blocks */
484 1.3 mycroft msg("estimated %ld tape blocks.\n", tapesize);
485 1.3 mycroft } else {
486 1.3 mycroft double fetapes;
487 1.3 mycroft
488 1.1 cgd if (blocksperfile)
489 1.3 mycroft fetapes = (double) tapesize / blocksperfile;
490 1.1 cgd else if (cartridge) {
491 1.1 cgd /* Estimate number of tapes, assuming streaming stops at
492 1.1 cgd the end of each block written, and not in mid-block.
493 1.1 cgd Assume no erroneous blocks; this can be compensated
494 1.1 cgd for with an artificially low tape size. */
495 1.45 lukem fetapes =
496 1.48 lukem ( (double) tapesize /* blocks */
497 1.1 cgd * TP_BSIZE /* bytes/block */
498 1.1 cgd * (1.0/density) /* 0.1" / byte */
499 1.1 cgd +
500 1.48 lukem (double) tapesize /* blocks */
501 1.1 cgd * (1.0/ntrec) /* streaming-stops per block */
502 1.1 cgd * 15.48 /* 0.1" / streaming-stop */
503 1.1 cgd ) * (1.0 / tsize ); /* tape / 0.1" */
504 1.1 cgd } else {
505 1.1 cgd /* Estimate number of tapes, for old fashioned 9-track
506 1.1 cgd tape */
507 1.1 cgd int tenthsperirg = (density == 625) ? 3 : 7;
508 1.1 cgd fetapes =
509 1.1 cgd ( tapesize /* blocks */
510 1.1 cgd * TP_BSIZE /* bytes / block */
511 1.1 cgd * (1.0/density) /* 0.1" / byte */
512 1.1 cgd +
513 1.1 cgd tapesize /* blocks */
514 1.1 cgd * (1.0/ntrec) /* IRG's / block */
515 1.1 cgd * tenthsperirg /* 0.1" / IRG */
516 1.1 cgd ) * (1.0 / tsize ); /* tape / 0.1" */
517 1.1 cgd }
518 1.1 cgd etapes = fetapes; /* truncating assignment */
519 1.1 cgd etapes++;
520 1.1 cgd /* count the dumped inodes map on each additional tape */
521 1.1 cgd tapesize += (etapes - 1) *
522 1.1 cgd (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
523 1.1 cgd tapesize += etapes + 10; /* headers + 10 trailer blks */
524 1.1 cgd msg("estimated %ld tape blocks on %3.2f tape(s).\n",
525 1.1 cgd tapesize, fetapes);
526 1.3 mycroft }
527 1.17 lukem /*
528 1.17 lukem * If the user only wants an estimate of the number of
529 1.17 lukem * tapes, exit now.
530 1.17 lukem */
531 1.17 lukem if (just_estimate)
532 1.48 lukem exit(X_FINOK);
533 1.1 cgd
534 1.1 cgd /*
535 1.3 mycroft * Allocate tape buffer.
536 1.1 cgd */
537 1.1 cgd if (!alloctape())
538 1.1 cgd quit("can't allocate tape buffers - try a smaller blocking factor.\n");
539 1.1 cgd
540 1.1 cgd startnewtape(1);
541 1.3 mycroft (void)time((time_t *)&(tstart_writing));
542 1.10 lukem xferrate = 0;
543 1.3 mycroft dumpmap(usedinomap, TS_CLRI, maxino - 1);
544 1.1 cgd
545 1.1 cgd msg("dumping (Pass III) [directories]\n");
546 1.3 mycroft dirty = 0; /* XXX just to get gcc to shut up */
547 1.3 mycroft for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
548 1.3 mycroft if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
549 1.1 cgd dirty = *map++;
550 1.1 cgd else
551 1.1 cgd dirty >>= 1;
552 1.1 cgd if ((dirty & 1) == 0)
553 1.1 cgd continue;
554 1.1 cgd /*
555 1.1 cgd * Skip directory inodes deleted and maybe reallocated
556 1.1 cgd */
557 1.1 cgd dp = getino(ino);
558 1.1 cgd if ((dp->di_mode & IFMT) != IFDIR)
559 1.1 cgd continue;
560 1.3 mycroft (void)dumpino(dp, ino);
561 1.1 cgd }
562 1.1 cgd
563 1.1 cgd msg("dumping (Pass IV) [regular files]\n");
564 1.3 mycroft for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
565 1.3 mycroft int mode;
566 1.3 mycroft
567 1.3 mycroft if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
568 1.1 cgd dirty = *map++;
569 1.1 cgd else
570 1.1 cgd dirty >>= 1;
571 1.1 cgd if ((dirty & 1) == 0)
572 1.1 cgd continue;
573 1.1 cgd /*
574 1.1 cgd * Skip inodes deleted and reallocated as directories.
575 1.1 cgd */
576 1.1 cgd dp = getino(ino);
577 1.3 mycroft mode = dp->di_mode & IFMT;
578 1.3 mycroft if (mode == IFDIR)
579 1.1 cgd continue;
580 1.3 mycroft (void)dumpino(dp, ino);
581 1.1 cgd }
582 1.1 cgd
583 1.18 bouyer spcl.c_type = iswap32(TS_END);
584 1.1 cgd for (i = 0; i < ntrec; i++)
585 1.3 mycroft writeheader(maxino - 1);
586 1.1 cgd if (pipeout)
587 1.26 briggs msg("%d tape blocks\n",iswap32(spcl.c_tapea));
588 1.1 cgd else
589 1.26 briggs msg("%d tape blocks on %d volume%s\n",
590 1.18 bouyer iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
591 1.18 bouyer (iswap32(spcl.c_volume) == 1) ? "" : "s");
592 1.10 lukem tnow = do_stats();
593 1.18 bouyer date = iswap32(spcl.c_date);
594 1.10 lukem msg("Date of this level %c dump: %s", level,
595 1.18 bouyer spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
596 1.10 lukem msg("Date this dump completed: %s", ctime(&tnow));
597 1.26 briggs msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
598 1.1 cgd putdumptime();
599 1.29 tron trewind(0);
600 1.48 lukem broadcast("DUMP IS DONE!\a\a\n");
601 1.1 cgd msg("DUMP IS DONE\n");
602 1.1 cgd Exit(X_FINOK);
603 1.1 cgd /* NOTREACHED */
604 1.15 lukem exit(X_FINOK); /* XXX: to satisfy gcc */
605 1.1 cgd }
606 1.1 cgd
607 1.5 mycroft static void
608 1.31 lukem usage(void)
609 1.5 mycroft {
610 1.49 lukem const char *prog = getprogname();
611 1.5 mycroft
612 1.49 lukem (void)fprintf(stderr,
613 1.50 bouyer "usage: %s [-0123456789aceFnStu] [-B records] [-b blocksize]\n"
614 1.49 lukem " [-d density] [-f file] [-h level] [-k read-blocksize]\n"
615 1.50 bouyer " [-L label] [-l timeout] [-r read-cache] [-s feet] [-T date] files-to-dump\n"
616 1.49 lukem " %s [-W | -w]\n", prog, prog);
617 1.48 lukem exit(X_STARTUP);
618 1.5 mycroft }
619 1.5 mycroft
620 1.3 mycroft /*
621 1.3 mycroft * Pick up a numeric argument. It must be nonnegative and in the given
622 1.3 mycroft * range (except that a vmax of 0 means unlimited).
623 1.3 mycroft */
624 1.3 mycroft static long
625 1.32 lukem numarg(char *meaning, long vmin, long vmax)
626 1.3 mycroft {
627 1.5 mycroft char *p;
628 1.3 mycroft long val;
629 1.3 mycroft
630 1.5 mycroft val = strtol(optarg, &p, 10);
631 1.5 mycroft if (*p)
632 1.48 lukem errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
633 1.3 mycroft if (val < vmin || (vmax && val > vmax))
634 1.48 lukem errx(X_STARTUP, "%s must be between %ld and %ld",
635 1.48 lukem meaning, vmin, vmax);
636 1.3 mycroft return (val);
637 1.3 mycroft }
638 1.3 mycroft
639 1.1 cgd void
640 1.32 lukem sig(int signo)
641 1.1 cgd {
642 1.32 lukem
643 1.1 cgd switch(signo) {
644 1.1 cgd case SIGALRM:
645 1.1 cgd case SIGBUS:
646 1.1 cgd case SIGFPE:
647 1.1 cgd case SIGHUP:
648 1.1 cgd case SIGTERM:
649 1.1 cgd case SIGTRAP:
650 1.1 cgd if (pipeout)
651 1.1 cgd quit("Signal on pipe: cannot recover\n");
652 1.35 mrg msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]);
653 1.3 mycroft (void)fflush(stderr);
654 1.3 mycroft (void)fflush(stdout);
655 1.1 cgd close_rewind();
656 1.3 mycroft exit(X_REWRITE);
657 1.1 cgd /* NOTREACHED */
658 1.1 cgd case SIGSEGV:
659 1.1 cgd msg("SIGSEGV: ABORTING!\n");
660 1.3 mycroft (void)signal(SIGSEGV, SIG_DFL);
661 1.3 mycroft (void)kill(0, SIGSEGV);
662 1.1 cgd /* NOTREACHED */
663 1.1 cgd }
664 1.1 cgd }
665 1.1 cgd
666 1.1 cgd char *
667 1.32 lukem rawname(char *cp)
668 1.1 cgd {
669 1.1 cgd static char rawbuf[MAXPATHLEN];
670 1.4 mycroft char *dp = strrchr(cp, '/');
671 1.1 cgd
672 1.3 mycroft if (dp == NULL)
673 1.3 mycroft return (NULL);
674 1.3 mycroft *dp = '\0';
675 1.12 mrg (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
676 1.1 cgd *dp = '/';
677 1.1 cgd return (rawbuf);
678 1.1 cgd }
679 1.1 cgd
680 1.5 mycroft /*
681 1.5 mycroft * obsolete --
682 1.5 mycroft * Change set of key letters and ordered arguments into something
683 1.5 mycroft * getopt(3) will like.
684 1.5 mycroft */
685 1.5 mycroft static void
686 1.32 lukem obsolete(int *argcp, char **argvp[])
687 1.1 cgd {
688 1.5 mycroft int argc, flags;
689 1.5 mycroft char *ap, **argv, *flagsp, **nargv, *p;
690 1.5 mycroft
691 1.5 mycroft /* Setup. */
692 1.5 mycroft argv = *argvp;
693 1.5 mycroft argc = *argcp;
694 1.5 mycroft
695 1.5 mycroft /* Return if no arguments or first argument has leading dash. */
696 1.5 mycroft ap = argv[1];
697 1.5 mycroft if (argc == 1 || *ap == '-')
698 1.5 mycroft return;
699 1.5 mycroft
700 1.5 mycroft /* Allocate space for new arguments. */
701 1.33 lukem *argvp = nargv = xmalloc((argc + 1) * sizeof(char *));
702 1.33 lukem p = flagsp = xmalloc(strlen(ap) + 2);
703 1.5 mycroft
704 1.5 mycroft *nargv++ = *argv;
705 1.5 mycroft argv += 2;
706 1.5 mycroft
707 1.5 mycroft for (flags = 0; *ap; ++ap) {
708 1.5 mycroft switch (*ap) {
709 1.5 mycroft case 'B':
710 1.5 mycroft case 'b':
711 1.5 mycroft case 'd':
712 1.5 mycroft case 'f':
713 1.5 mycroft case 'h':
714 1.5 mycroft case 's':
715 1.5 mycroft case 'T':
716 1.5 mycroft if (*argv == NULL) {
717 1.5 mycroft warnx("option requires an argument -- %c", *ap);
718 1.5 mycroft usage();
719 1.5 mycroft }
720 1.33 lukem nargv[0] = xmalloc(strlen(*argv) + 2 + 1);
721 1.5 mycroft nargv[0][0] = '-';
722 1.5 mycroft nargv[0][1] = *ap;
723 1.17 lukem (void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
724 1.5 mycroft ++argv;
725 1.5 mycroft ++nargv;
726 1.5 mycroft break;
727 1.5 mycroft default:
728 1.5 mycroft if (!flags) {
729 1.5 mycroft *p++ = '-';
730 1.5 mycroft flags = 1;
731 1.5 mycroft }
732 1.5 mycroft *p++ = *ap;
733 1.5 mycroft break;
734 1.5 mycroft }
735 1.5 mycroft }
736 1.1 cgd
737 1.5 mycroft /* Terminate flags. */
738 1.5 mycroft if (flags) {
739 1.5 mycroft *p = '\0';
740 1.5 mycroft *nargv++ = flagsp;
741 1.5 mycroft }
742 1.5 mycroft
743 1.5 mycroft /* Copy remaining arguments. */
744 1.14 lukem while ((*nargv++ = *argv++) != NULL)
745 1.14 lukem ;
746 1.5 mycroft
747 1.5 mycroft /* Update argument count. */
748 1.5 mycroft *argcp = nargv - *argvp - 1;
749 1.33 lukem }
750 1.33 lukem
751 1.33 lukem
752 1.33 lukem void *
753 1.33 lukem xcalloc(size_t number, size_t size)
754 1.33 lukem {
755 1.33 lukem void *p;
756 1.33 lukem
757 1.33 lukem p = calloc(number, size);
758 1.33 lukem if (p == NULL)
759 1.33 lukem quit("%s\n", strerror(errno));
760 1.33 lukem return (p);
761 1.33 lukem }
762 1.33 lukem
763 1.33 lukem void *
764 1.33 lukem xmalloc(size_t size)
765 1.33 lukem {
766 1.33 lukem void *p;
767 1.33 lukem
768 1.33 lukem p = malloc(size);
769 1.33 lukem if (p == NULL)
770 1.33 lukem quit("%s\n", strerror(errno));
771 1.33 lukem return (p);
772 1.33 lukem }
773 1.33 lukem
774 1.33 lukem char *
775 1.33 lukem xstrdup(const char *str)
776 1.33 lukem {
777 1.33 lukem char *p;
778 1.33 lukem
779 1.33 lukem p = strdup(str);
780 1.33 lukem if (p == NULL)
781 1.33 lukem quit("%s\n", strerror(errno));
782 1.33 lukem return (p);
783 1.1 cgd }
784