1/*	$NetBSD: ex_preserve.c,v 1.1.1.2 2008/05/18 14:31:09 aymeric 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#ifndef lint
15static const char sccsid[] = "Id: ex_preserve.c,v 10.15 2001/06/25 15:19:18 skimo Exp (Berkeley) Date: 2001/06/25 15:19:18";
16#endif /* not lint */
17
18#include <sys/types.h>
19#include <sys/queue.h>
20
21#include <bitstring.h>
22#include <errno.h>
23#include <limits.h>
24#include <stdio.h>
25#include <string.h>
26
27#include "../common/common.h"
28
29/*
30 * ex_preserve -- :pre[serve]
31 *	Push the file to recovery.
32 *
33 * PUBLIC: int ex_preserve __P((SCR *, EXCMD *));
34 */
35int
36ex_preserve(SCR *sp, EXCMD *cmdp)
37{
38	db_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 */
69int
70ex_recover(SCR *sp, EXCMD *cmdp)
71{
72	ARGS *ap;
73	FREF *frp;
74	const char *np;
75	size_t nlen;
76
77	ap = cmdp->argv[0];
78
79	/* Set the alternate file name. */
80	INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
81	set_alt_name(sp, np);
82
83	/*
84	 * Check for modifications.  Autowrite did not historically
85	 * affect :recover.
86	 */
87	if (file_m2(sp, FL_ISSET(cmdp->iflags, E_C_FORCE)))
88		return (1);
89
90	/* Get a file structure for the file. */
91	INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
92	if ((frp = file_add(sp, np)) == NULL)
93		return (1);
94
95	/* Set the recover bit. */
96	F_SET(frp, FR_RECOVER);
97
98	/* Switch files. */
99	if (file_init(sp, frp, NULL, FS_SETALT |
100	    (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
101		return (1);
102
103	F_SET(sp, SC_FSWITCH);
104	return (0);
105}
106