xref: /csrg-svn/sbin/dump/main.c (revision 5342)
1*5342Smckusic static	char *sccsid = "@(#)main.c	1.6 (Berkeley) 01/06/82";
21423Sroot #include "dump.h"
31423Sroot 
41423Sroot int	notify = 0;	/* notify operator flag */
51423Sroot int	blockswritten = 0;	/* number of blocks written on current tape */
61423Sroot int	tapeno = 0;	/* current tape number */
71423Sroot int	density = 160;	/* density in 0.1" units */
81423Sroot 
91423Sroot main(argc, argv)
101423Sroot 	int	argc;
111423Sroot 	char	*argv[];
121423Sroot {
131423Sroot 	char		*arg;
145328Smckusic 	int		i;
151423Sroot 	float		fetapes;
161423Sroot 	register	struct	fstab	*dt;
171423Sroot 
181423Sroot 	time(&(spcl.c_date));
191423Sroot 
201423Sroot 	tsize = 2300L*12L*10L;
211423Sroot 	tape = TAPE;
221423Sroot 	disk = DISK;
231423Sroot 	increm = NINCREM;
245328Smckusic 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) {
255328Smckusic 		msg("TP_BSIZE must be a multiple of DEV_BSIZE\n");
265328Smckusic 		dumpabort();
275328Smckusic 	}
281423Sroot 	incno = '9';
291423Sroot 	uflag = 0;
301423Sroot 	arg = "u";
311423Sroot 	if(argc > 1) {
321423Sroot 		argv++;
331423Sroot 		argc--;
341423Sroot 		arg = *argv;
351423Sroot 		if (*arg == '-')
361423Sroot 			argc++;
371423Sroot 	}
381423Sroot 	while(*arg)
391423Sroot 	switch (*arg++) {
401463Sroot 	case 'w':
411463Sroot 		lastdump('w');		/* tell us only what has to be done */
421463Sroot 		exit(0);
431463Sroot 		break;
441423Sroot 	case 'W':			/* what to do */
451463Sroot 		lastdump('W');		/* tell us the current state of what has been done */
461423Sroot 		exit(0);		/* do nothing else */
471423Sroot 		break;
481423Sroot 
491423Sroot 	case 'J':			/* update old to new */
501423Sroot 		o_nconvert();
511423Sroot 		exit(0);		/* do nothing else */
521423Sroot 		break;
531423Sroot 
541423Sroot 	case 'f':			/* output file */
551423Sroot 		if(argc > 1) {
561423Sroot 			argv++;
571423Sroot 			argc--;
581423Sroot 			tape = *argv;
591423Sroot 		}
601423Sroot 		break;
611423Sroot 
621423Sroot 	case 'd':			/* density, in bits per inch */
631423Sroot 		if (argc > 1) {
641423Sroot 			argv++;
651423Sroot 			argc--;
661423Sroot 			density = atoi(*argv) / 10;
671423Sroot 		}
681423Sroot 		break;
691423Sroot 
701423Sroot 	case 's':			/* tape size, feet */
711423Sroot 		if(argc > 1) {
721423Sroot 			argv++;
731423Sroot 			argc--;
741423Sroot 			tsize = atol(*argv);
751423Sroot 			tsize *= 12L*10L;
761423Sroot 		}
771423Sroot 		break;
781423Sroot 
791423Sroot 	case '0':			/* dump level */
801423Sroot 	case '1':
811423Sroot 	case '2':
821423Sroot 	case '3':
831423Sroot 	case '4':
841423Sroot 	case '5':
851423Sroot 	case '6':
861423Sroot 	case '7':
871423Sroot 	case '8':
881423Sroot 	case '9':
891423Sroot 		incno = arg[-1];
901423Sroot 		break;
911423Sroot 
921423Sroot 	case 'u':			/* update /etc/dumpdates */
931423Sroot 		uflag++;
941423Sroot 		break;
951423Sroot 
961423Sroot 	case 'n':			/* notify operators */
971423Sroot 		notify++;
981423Sroot 		break;
991423Sroot 
1001423Sroot 	default:
1011423Sroot 		printf("bad key '%c%'\n", arg[-1]);
1021423Sroot 		Exit(X_ABORT);
1031423Sroot 	}
1041423Sroot 	if(argc > 1) {
1051423Sroot 		argv++;
1061423Sroot 		argc--;
1071423Sroot 		disk = *argv;
1081423Sroot 	}
1091423Sroot 
1101423Sroot 	if (signal(SIGHUP, sighup) == SIG_IGN)
1111423Sroot 		signal(SIGHUP, SIG_IGN);
1121423Sroot 	if (signal(SIGTRAP, sigtrap) == SIG_IGN)
1131423Sroot 		signal(SIGTRAP, SIG_IGN);
1141423Sroot 	if (signal(SIGFPE, sigfpe) == SIG_IGN)
1151423Sroot 		signal(SIGFPE, SIG_IGN);
1161423Sroot 	if (signal(SIGBUS, sigbus) == SIG_IGN)
1171423Sroot 		signal(SIGBUS, SIG_IGN);
1181423Sroot 	if (signal(SIGSEGV, sigsegv) == SIG_IGN)
1191423Sroot 		signal(SIGSEGV, SIG_IGN);
1201423Sroot 	if (signal(SIGTERM, sigterm) == SIG_IGN)
1211423Sroot 		signal(SIGTERM, SIG_IGN);
1221423Sroot 
1231423Sroot 
1241423Sroot 	if (signal(SIGINT, interrupt) == SIG_IGN)
1251423Sroot 		signal(SIGINT, SIG_IGN);
1261423Sroot 
1271423Sroot 	set_operators();	/* /etc/group snarfed */
1281423Sroot 	getfstab();		/* /etc/fstab snarfed */
1291423Sroot 	/*
1301423Sroot 	 *	disk can be either the full special file name,
1311423Sroot 	 *	the suffix of the special file name,
1321423Sroot 	 *	the special name missing the leading '/',
1331423Sroot 	 *	the file system name with or without the leading '/'.
1341423Sroot 	 */
1351423Sroot 	dt = fstabsearch(disk);
1361423Sroot 	if (dt != 0)
1371423Sroot 		disk = rawname(dt->fs_spec);
1381423Sroot 	getitime();		/* /etc/dumpdates snarfed */
1391423Sroot 
1401423Sroot 	msg("Date of this level %c dump: %s\n", incno, prdate(spcl.c_date));
1411423Sroot 	msg("Date of last level %c dump: %s\n", incno, prdate(spcl.c_ddate));
1421423Sroot 	msg("Dumping %s ", disk);
1431423Sroot 	if (dt != 0)
1441423Sroot 		msgtail("(%s) ", dt->fs_file);
1451423Sroot 	msgtail("to %s\n", tape);
1461423Sroot 
1471423Sroot 	fi = open(disk, 0);
1481423Sroot 	if (fi < 0) {
1491423Sroot 		msg("Cannot open %s\n", disk);
1501423Sroot 		Exit(X_ABORT);
1511423Sroot 	}
1521423Sroot 	esize = 0;
1535328Smckusic 	sblock = (struct fs *)buf;
1545328Smckusic 	sync();
155*5342Smckusic 	bread(SBLOCK, sblock, SBSIZE);
1565328Smckusic 	if (sblock->fs_magic != FS_MAGIC) {
1575328Smckusic 		msg("bad sblock magic number\n");
1585328Smckusic 		dumpabort();
1595328Smckusic 	}
1605328Smckusic 	msiz = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
1615328Smckusic 		TP_BSIZE);
1625328Smckusic 	clrmap = (char *)calloc(msiz, sizeof(char));
1635328Smckusic 	dirmap = (char *)calloc(msiz, sizeof(char));
1645328Smckusic 	nodmap = (char *)calloc(msiz, sizeof(char));
1651423Sroot 
1661423Sroot 	msg("mapping (Pass I) [regular files]\n");
1675328Smckusic 	pass(mark, (char *)NULL);		/* mark updates esize */
1681423Sroot 
1691423Sroot 	do {
1701423Sroot 		msg("mapping (Pass II) [directories]\n");
1711423Sroot 		nadded = 0;
1721423Sroot 		pass(add, dirmap);
1731423Sroot 	} while(nadded);
1741423Sroot 
1751423Sroot 	bmapest(clrmap);
1761423Sroot 	bmapest(nodmap);
1771423Sroot 
1781423Sroot 	fetapes =
1795328Smckusic 		(	  esize		/* blocks */
1805328Smckusic 			* TP_BSIZE	/* bytes / block */
1815328Smckusic 			* (1.0/density)	/* 0.1" / byte */
1821423Sroot 		  +
1835328Smckusic 			  esize		/* blocks */
1845328Smckusic 			* (1.0/NTREC)	/* IRG's / block */
1855328Smckusic 			* 7		/* 0.1" / IRG */
1861423Sroot 		) * (1.0 / tsize )	/* tape / 0.1" */
1871423Sroot 	;
1881423Sroot 	etapes = fetapes;		/* truncating assignment */
1891423Sroot 	etapes++;
1905328Smckusic 	/* count the nodemap on each additional tape */
1915328Smckusic 	for (i = 1; i < etapes; i++)
1925328Smckusic 		bmapest(nodmap);
1935328Smckusic 	esize += i + 10;	/* headers + 10 trailer blocks */
1941423Sroot 	msg("estimated %ld tape blocks on %3.2f tape(s).\n", esize, fetapes);
1951423Sroot 
1961423Sroot 	otape();			/* bitmap is the first to tape write */
1971423Sroot 	time(&(tstart_writing));
1981423Sroot 	bitmap(clrmap, TS_CLRI);
1991423Sroot 
2001423Sroot 	msg("dumping (Pass III) [directories]\n");
2011423Sroot 	pass(dump, dirmap);
2021423Sroot 
2031423Sroot 	msg("dumping (Pass IV) [regular files]\n");
2041423Sroot 	pass(dump, nodmap);
2051423Sroot 
2061423Sroot 	spcl.c_type = TS_END;
2075328Smckusic 	for(i = 0; i < NTREC; i++)
2081423Sroot 		spclrec();
2091423Sroot 	msg("DUMP: %ld tape blocks on %d tape(s)\n",spcl.c_tapea,spcl.c_volume);
2101423Sroot 	msg("DUMP IS DONE\n");
2111423Sroot 
2121423Sroot 	putitime();
2131423Sroot 	close(to);
2141423Sroot 	rewind();
2151423Sroot 	broadcast("DUMP IS DONE!\7\7\n");
2161423Sroot 	Exit(X_FINOK);
2171423Sroot }
2181423Sroot 
2191423Sroot int	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
2201423Sroot int	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
2211423Sroot int	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
2221423Sroot int	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
2231423Sroot int	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
2241423Sroot int	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
2251423Sroot int	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
2261423Sroot 
2271423Sroot sigAbort()
2281423Sroot {
2291423Sroot 	msg("Rewriting attempted as response to unknown signal.\n");
2301423Sroot 	fflush(stderr);
2311423Sroot 	fflush(stdout);
2321423Sroot 	close_rewind();
2331423Sroot 	exit(X_REWRITE);
2341423Sroot }
2351423Sroot 
2361423Sroot char *rawname(cp)
2371423Sroot 	char *cp;
2381423Sroot {
2391423Sroot 	static char rawbuf[32];
2404608Smckusic 	char *rindex();
2411423Sroot 	char *dp = rindex(cp, '/');
2421423Sroot 
2431423Sroot 	if (dp == 0)
2441423Sroot 		return (0);
2451423Sroot 	*dp = 0;
2461423Sroot 	strcpy(rawbuf, cp);
2471423Sroot 	*dp = '/';
2481423Sroot 	strcat(rawbuf, "/r");
2491423Sroot 	strcat(rawbuf, dp+1);
2501423Sroot 	return (rawbuf);
2511423Sroot }
252