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