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