xref: /original-bsd/sbin/dump/main.c (revision fbed46ce)
1 static	char *sccsid = "@(#)main.c	1.6 (Berkeley) 01/06/82";
2 #include "dump.h"
3 
4 int	notify = 0;	/* notify operator flag */
5 int	blockswritten = 0;	/* number of blocks written on current tape */
6 int	tapeno = 0;	/* current tape number */
7 int	density = 160;	/* density in 0.1" units */
8 
9 main(argc, argv)
10 	int	argc;
11 	char	*argv[];
12 {
13 	char		*arg;
14 	int		i;
15 	float		fetapes;
16 	register	struct	fstab	*dt;
17 
18 	time(&(spcl.c_date));
19 
20 	tsize = 2300L*12L*10L;
21 	tape = TAPE;
22 	disk = DISK;
23 	increm = NINCREM;
24 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) {
25 		msg("TP_BSIZE must be a multiple of DEV_BSIZE\n");
26 		dumpabort();
27 	}
28 	incno = '9';
29 	uflag = 0;
30 	arg = "u";
31 	if(argc > 1) {
32 		argv++;
33 		argc--;
34 		arg = *argv;
35 		if (*arg == '-')
36 			argc++;
37 	}
38 	while(*arg)
39 	switch (*arg++) {
40 	case 'w':
41 		lastdump('w');		/* tell us only what has to be done */
42 		exit(0);
43 		break;
44 	case 'W':			/* what to do */
45 		lastdump('W');		/* tell us the current state of what has been done */
46 		exit(0);		/* do nothing else */
47 		break;
48 
49 	case 'J':			/* update old to new */
50 		o_nconvert();
51 		exit(0);		/* do nothing else */
52 		break;
53 
54 	case 'f':			/* output file */
55 		if(argc > 1) {
56 			argv++;
57 			argc--;
58 			tape = *argv;
59 		}
60 		break;
61 
62 	case 'd':			/* density, in bits per inch */
63 		if (argc > 1) {
64 			argv++;
65 			argc--;
66 			density = atoi(*argv) / 10;
67 		}
68 		break;
69 
70 	case 's':			/* tape size, feet */
71 		if(argc > 1) {
72 			argv++;
73 			argc--;
74 			tsize = atol(*argv);
75 			tsize *= 12L*10L;
76 		}
77 		break;
78 
79 	case '0':			/* dump level */
80 	case '1':
81 	case '2':
82 	case '3':
83 	case '4':
84 	case '5':
85 	case '6':
86 	case '7':
87 	case '8':
88 	case '9':
89 		incno = arg[-1];
90 		break;
91 
92 	case 'u':			/* update /etc/dumpdates */
93 		uflag++;
94 		break;
95 
96 	case 'n':			/* notify operators */
97 		notify++;
98 		break;
99 
100 	default:
101 		printf("bad key '%c%'\n", arg[-1]);
102 		Exit(X_ABORT);
103 	}
104 	if(argc > 1) {
105 		argv++;
106 		argc--;
107 		disk = *argv;
108 	}
109 
110 	if (signal(SIGHUP, sighup) == SIG_IGN)
111 		signal(SIGHUP, SIG_IGN);
112 	if (signal(SIGTRAP, sigtrap) == SIG_IGN)
113 		signal(SIGTRAP, SIG_IGN);
114 	if (signal(SIGFPE, sigfpe) == SIG_IGN)
115 		signal(SIGFPE, SIG_IGN);
116 	if (signal(SIGBUS, sigbus) == SIG_IGN)
117 		signal(SIGBUS, SIG_IGN);
118 	if (signal(SIGSEGV, sigsegv) == SIG_IGN)
119 		signal(SIGSEGV, SIG_IGN);
120 	if (signal(SIGTERM, sigterm) == SIG_IGN)
121 		signal(SIGTERM, SIG_IGN);
122 
123 
124 	if (signal(SIGINT, interrupt) == SIG_IGN)
125 		signal(SIGINT, SIG_IGN);
126 
127 	set_operators();	/* /etc/group snarfed */
128 	getfstab();		/* /etc/fstab snarfed */
129 	/*
130 	 *	disk can be either the full special file name,
131 	 *	the suffix of the special file name,
132 	 *	the special name missing the leading '/',
133 	 *	the file system name with or without the leading '/'.
134 	 */
135 	dt = fstabsearch(disk);
136 	if (dt != 0)
137 		disk = rawname(dt->fs_spec);
138 	getitime();		/* /etc/dumpdates snarfed */
139 
140 	msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
141 	msg("Date of last level %c dump: %s\n", incno, prdate(spcl.c_ddate));
142 	msg("Dumping %s ", disk);
143 	if (dt != 0)
144 		msgtail("(%s) ", dt->fs_file);
145 	msgtail("to %s\n", tape);
146 
147 	fi = open(disk, 0);
148 	if (fi < 0) {
149 		msg("Cannot open %s\n", disk);
150 		Exit(X_ABORT);
151 	}
152 	esize = 0;
153 	sblock = (struct fs *)buf;
154 	sync();
155 	bread(SBLOCK, sblock, SBSIZE);
156 	if (sblock->fs_magic != FS_MAGIC) {
157 		msg("bad sblock magic number\n");
158 		dumpabort();
159 	}
160 	msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
161 		TP_BSIZE);
162 	clrmap = (char *)calloc(msiz, sizeof(char));
163 	dirmap = (char *)calloc(msiz, sizeof(char));
164 	nodmap = (char *)calloc(msiz, sizeof(char));
165 
166 	msg("mapping (Pass I) [regular files]\n");
167 	pass(mark, (char *)NULL);		/* mark updates esize */
168 
169 	do {
170 		msg("mapping (Pass II) [directories]\n");
171 		nadded = 0;
172 		pass(add, dirmap);
173 	} while(nadded);
174 
175 	bmapest(clrmap);
176 	bmapest(nodmap);
177 
178 	fetapes =
179 		(	  esize		/* blocks */
180 			* TP_BSIZE	/* bytes / block */
181 			* (1.0/density)	/* 0.1" / byte */
182 		  +
183 			  esize		/* blocks */
184 			* (1.0/NTREC)	/* IRG's / block */
185 			* 7		/* 0.1" / IRG */
186 		) * (1.0 / tsize )	/* tape / 0.1" */
187 	;
188 	etapes = fetapes;		/* truncating assignment */
189 	etapes++;
190 	/* count the nodemap on each additional tape */
191 	for (i = 1; i < etapes; i++)
192 		bmapest(nodmap);
193 	esize += i + 10;	/* headers + 10 trailer blocks */
194 	msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
195 
196 	otape();			/* bitmap is the first to tape write */
197 	time(&(tstart_writing));
198 	bitmap(clrmap, TS_CLRI);
199 
200 	msg("dumping (Pass III) [directories]\n");
201 	pass(dump, dirmap);
202 
203 	msg("dumping (Pass IV) [regular files]\n");
204 	pass(dump, nodmap);
205 
206 	spcl.c_type = TS_END;
207 	for(i = 0; i < NTREC; i++)
208 		spclrec();
209 	msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
210 	msg("DUMP IS DONE\n");
211 
212 	putitime();
213 	close(to);
214 	rewind();
215 	broadcast("DUMP IS DONE!\7\7\n");
216 	Exit(X_FINOK);
217 }
218 
219 int	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
220 int	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
221 int	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
222 int	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
223 int	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
224 int	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
225 int	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
226 
227 sigAbort()
228 {
229 	msg("Rewriting attempted as response to unknown signal.\n");
230 	fflush(stderr);
231 	fflush(stdout);
232 	close_rewind();
233 	exit(X_REWRITE);
234 }
235 
236 char *rawname(cp)
237 	char *cp;
238 {
239 	static char rawbuf[32];
240 	char *rindex();
241 	char *dp = rindex(cp, '/');
242 
243 	if (dp == 0)
244 		return (0);
245 	*dp = 0;
246 	strcpy(rawbuf, cp);
247 	*dp = '/';
248 	strcat(rawbuf, "/r");
249 	strcat(rawbuf, dp+1);
250 	return (rawbuf);
251 }
252