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