xref: /netbsd-src/external/bsd/nvi/dist/ex/ex_preserve.c (revision 2f698edb5c1cb2dcd9e762b0abb50c41dde8b6b7)
1 /*	$NetBSD: ex_preserve.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
2 /*-
3  * Copyright (c) 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 1993, 1994, 1995, 1996
6  *	Keith Bostic.  All rights reserved.
7  *
8  * See the LICENSE file for redistribution information.
9  */
10 
11 #include "config.h"
12 
13 #include <sys/cdefs.h>
14 #if 0
15 #ifndef lint
16 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 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: ex_preserve.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20 #endif
21 
22 #include <sys/types.h>
23 #include <sys/queue.h>
24 
25 #include <bitstring.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include "../common/common.h"
32 
33 /*
34  * ex_preserve -- :pre[serve]
35  *	Push the file to recovery.
36  *
37  * PUBLIC: int ex_preserve __P((SCR *, EXCMD *));
38  */
39 int
ex_preserve(SCR * sp,EXCMD * cmdp)40 ex_preserve(SCR *sp, EXCMD *cmdp)
41 {
42 	db_recno_t lno;
43 
44 	NEEDFILE(sp, cmdp);
45 
46 	if (!F_ISSET(sp->ep, F_RCV_ON)) {
47 		msgq(sp, M_ERR, "142|Preservation of this file not possible");
48 		return (1);
49 	}
50 
51 	/* If recovery not initialized, do so. */
52 	if (F_ISSET(sp->ep, F_FIRSTMODIFY) && rcv_init(sp))
53 		return (1);
54 
55 	/* Force the file to be read in, in case it hasn't yet. */
56 	if (db_last(sp, &lno))
57 		return (1);
58 
59 	/* Sync to disk. */
60 	if (rcv_sync(sp, RCV_SNAPSHOT))
61 		return (1);
62 
63 	msgq(sp, M_INFO, "143|File preserved");
64 	return (0);
65 }
66 
67 /*
68  * ex_recover -- :rec[over][!] file
69  *	Recover the file.
70  *
71  * PUBLIC: int ex_recover __P((SCR *, EXCMD *));
72  */
73 int
ex_recover(SCR * sp,EXCMD * cmdp)74 ex_recover(SCR *sp, EXCMD *cmdp)
75 {
76 	ARGS *ap;
77 	FREF *frp;
78 	const char *np;
79 	size_t nlen;
80 
81 	ap = cmdp->argv[0];
82 
83 	/* Set the alternate file name. */
84 	INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
85 	set_alt_name(sp, np);
86 
87 	/*
88 	 * Check for modifications.  Autowrite did not historically
89 	 * affect :recover.
90 	 */
91 	if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
92 		return (1);
93 
94 	/* Get a file structure for the file. */
95 	INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
96 	if ((frp = file_add(sp, np)) == NULL)
97 		return (1);
98 
99 	/* Set the recover bit. */
100 	F_SET(frp, FR_RECOVER);
101 
102 	/* Switch files. */
103 	if (file_init(sp, frp, NULL, FS_SETALT |
104 	    (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
105 		return (1);
106 
107 	F_SET(sp, SC_FSWITCH);
108 	return (0);
109 }
110