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