xref: /minix3/external/bsd/nvi/dist/ex/ex_preserve.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_preserve.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: ex_preserve.c,v 10.15 2001/06/25 15:19:18 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:18 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_preserve.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc 
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <errno.h>
2784d9c625SLionel Sambuc #include <limits.h>
2884d9c625SLionel Sambuc #include <stdio.h>
2984d9c625SLionel Sambuc #include <string.h>
3084d9c625SLionel Sambuc 
3184d9c625SLionel Sambuc #include "../common/common.h"
3284d9c625SLionel Sambuc 
3384d9c625SLionel Sambuc /*
3484d9c625SLionel Sambuc  * ex_preserve -- :pre[serve]
3584d9c625SLionel Sambuc  *	Push the file to recovery.
3684d9c625SLionel Sambuc  *
3784d9c625SLionel Sambuc  * PUBLIC: int ex_preserve __P((SCR *, EXCMD *));
3884d9c625SLionel Sambuc  */
3984d9c625SLionel Sambuc int
ex_preserve(SCR * sp,EXCMD * cmdp)4084d9c625SLionel Sambuc ex_preserve(SCR *sp, EXCMD *cmdp)
4184d9c625SLionel Sambuc {
4284d9c625SLionel Sambuc 	db_recno_t lno;
4384d9c625SLionel Sambuc 
4484d9c625SLionel Sambuc 	NEEDFILE(sp, cmdp);
4584d9c625SLionel Sambuc 
4684d9c625SLionel Sambuc 	if (!F_ISSET(sp->ep, F_RCV_ON)) {
4784d9c625SLionel Sambuc 		msgq(sp, M_ERR, "142|Preservation of this file not possible");
4884d9c625SLionel Sambuc 		return (1);
4984d9c625SLionel Sambuc 	}
5084d9c625SLionel Sambuc 
5184d9c625SLionel Sambuc 	/* If recovery not initialized, do so. */
5284d9c625SLionel Sambuc 	if (F_ISSET(sp->ep, F_FIRSTMODIFY) && rcv_init(sp))
5384d9c625SLionel Sambuc 		return (1);
5484d9c625SLionel Sambuc 
5584d9c625SLionel Sambuc 	/* Force the file to be read in, in case it hasn't yet. */
5684d9c625SLionel Sambuc 	if (db_last(sp, &lno))
5784d9c625SLionel Sambuc 		return (1);
5884d9c625SLionel Sambuc 
5984d9c625SLionel Sambuc 	/* Sync to disk. */
6084d9c625SLionel Sambuc 	if (rcv_sync(sp, RCV_SNAPSHOT))
6184d9c625SLionel Sambuc 		return (1);
6284d9c625SLionel Sambuc 
6384d9c625SLionel Sambuc 	msgq(sp, M_INFO, "143|File preserved");
6484d9c625SLionel Sambuc 	return (0);
6584d9c625SLionel Sambuc }
6684d9c625SLionel Sambuc 
6784d9c625SLionel Sambuc /*
6884d9c625SLionel Sambuc  * ex_recover -- :rec[over][!] file
6984d9c625SLionel Sambuc  *	Recover the file.
7084d9c625SLionel Sambuc  *
7184d9c625SLionel Sambuc  * PUBLIC: int ex_recover __P((SCR *, EXCMD *));
7284d9c625SLionel Sambuc  */
7384d9c625SLionel Sambuc int
ex_recover(SCR * sp,EXCMD * cmdp)7484d9c625SLionel Sambuc ex_recover(SCR *sp, EXCMD *cmdp)
7584d9c625SLionel Sambuc {
7684d9c625SLionel Sambuc 	ARGS *ap;
7784d9c625SLionel Sambuc 	FREF *frp;
7884d9c625SLionel Sambuc 	const char *np;
7984d9c625SLionel Sambuc 	size_t nlen;
8084d9c625SLionel Sambuc 
8184d9c625SLionel Sambuc 	ap = cmdp->argv[0];
8284d9c625SLionel Sambuc 
8384d9c625SLionel Sambuc 	/* Set the alternate file name. */
8484d9c625SLionel Sambuc 	INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
8584d9c625SLionel Sambuc 	set_alt_name(sp, np);
8684d9c625SLionel Sambuc 
8784d9c625SLionel Sambuc 	/*
8884d9c625SLionel Sambuc 	 * Check for modifications.  Autowrite did not historically
8984d9c625SLionel Sambuc 	 * affect :recover.
9084d9c625SLionel Sambuc 	 */
9184d9c625SLionel Sambuc 	if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
9284d9c625SLionel Sambuc 		return (1);
9384d9c625SLionel Sambuc 
9484d9c625SLionel Sambuc 	/* Get a file structure for the file. */
9584d9c625SLionel Sambuc 	INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
9684d9c625SLionel Sambuc 	if ((frp = file_add(sp, np)) == NULL)
9784d9c625SLionel Sambuc 		return (1);
9884d9c625SLionel Sambuc 
9984d9c625SLionel Sambuc 	/* Set the recover bit. */
10084d9c625SLionel Sambuc 	F_SET(frp, FR_RECOVER);
10184d9c625SLionel Sambuc 
10284d9c625SLionel Sambuc 	/* Switch files. */
10384d9c625SLionel Sambuc 	if (file_init(sp, frp, NULL, FS_SETALT |
10484d9c625SLionel Sambuc 	    (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
10584d9c625SLionel Sambuc 		return (1);
10684d9c625SLionel Sambuc 
10784d9c625SLionel Sambuc 	F_SET(sp, SC_FSWITCH);
10884d9c625SLionel Sambuc 	return (0);
10984d9c625SLionel Sambuc }
110