xref: /openbsd-src/usr.bin/vi/ex/ex_preserve.c (revision a0b15055027e40896769bb61ce100804e66327ea)
1 /*	$OpenBSD: ex_preserve.c,v 1.7 2016/01/06 22:28:52 millert 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
ex_preserve(SCR * sp,EXCMD * cmdp)32 ex_preserve(SCR *sp, EXCMD *cmdp)
33 {
34 	recno_t lno;
35 
36 	NEEDFILE(sp, cmdp);
37 
38 	if (!F_ISSET(sp->ep, F_RCV_ON)) {
39 		msgq(sp, M_ERR, "Preservation of this file not possible");
40 		return (1);
41 	}
42 
43 	/* If recovery not initialized, do so. */
44 	if (F_ISSET(sp->ep, F_FIRSTMODIFY) && rcv_init(sp))
45 		return (1);
46 
47 	/* Force the file to be read in, in case it hasn't yet. */
48 	if (db_last(sp, &lno))
49 		return (1);
50 
51 	/* Sync to disk. */
52 	if (rcv_sync(sp, RCV_SNAPSHOT))
53 		return (1);
54 
55 	msgq(sp, M_INFO, "File preserved");
56 	return (0);
57 }
58 
59 /*
60  * ex_recover -- :rec[over][!] file
61  *	Recover the file.
62  *
63  * PUBLIC: int ex_recover(SCR *, EXCMD *);
64  */
65 int
ex_recover(SCR * sp,EXCMD * cmdp)66 ex_recover(SCR *sp, EXCMD *cmdp)
67 {
68 	ARGS *ap;
69 	FREF *frp;
70 
71 	ap = cmdp->argv[0];
72 
73 	/* Set the alternate file name. */
74 	set_alt_name(sp, ap->bp);
75 
76 	/*
77 	 * Check for modifications.  Autowrite did not historically
78 	 * affect :recover.
79 	 */
80 	if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
81 		return (1);
82 
83 	/* Get a file structure for the file. */
84 	if ((frp = file_add(sp, ap->bp)) == NULL)
85 		return (1);
86 
87 	/* Set the recover bit. */
88 	F_SET(frp, FR_RECOVER);
89 
90 	/* Switch files. */
91 	if (file_init(sp, frp, NULL, FS_SETALT |
92 	    (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
93 		return (1);
94 
95 	F_SET(sp, SC_FSWITCH);
96 	return (0);
97 }
98