1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * db_log.cc
24*7c478bd9Sstevel@tonic-gate *
25*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
26*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
27*7c478bd9Sstevel@tonic-gate */
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <errno.h>
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include <malloc.h>
33*7c478bd9Sstevel@tonic-gate #include <string.h>
34*7c478bd9Sstevel@tonic-gate #ifdef TDRPC
35*7c478bd9Sstevel@tonic-gate #include <sysent.h>
36*7c478bd9Sstevel@tonic-gate #endif
37*7c478bd9Sstevel@tonic-gate #include <unistd.h>
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate #include "db_headers.h"
40*7c478bd9Sstevel@tonic-gate #include "db_log.h"
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate static void
delete_log_entry(db_log_entry * lentry)45*7c478bd9Sstevel@tonic-gate delete_log_entry(db_log_entry *lentry)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate db_query *q;
48*7c478bd9Sstevel@tonic-gate entry_object *obj;
49*7c478bd9Sstevel@tonic-gate if (lentry) {
50*7c478bd9Sstevel@tonic-gate if ((q = lentry->get_query())) {
51*7c478bd9Sstevel@tonic-gate delete q;
52*7c478bd9Sstevel@tonic-gate }
53*7c478bd9Sstevel@tonic-gate if ((obj = lentry->get_object())) {
54*7c478bd9Sstevel@tonic-gate free_entry(obj);
55*7c478bd9Sstevel@tonic-gate }
56*7c478bd9Sstevel@tonic-gate delete lentry;
57*7c478bd9Sstevel@tonic-gate }
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate /*
61*7c478bd9Sstevel@tonic-gate * Execute given function 'func' on log.
62*7c478bd9Sstevel@tonic-gate * function takes as arguments: pointer to log entry, character pointer to
63*7c478bd9Sstevel@tonic-gate * another argument, and pointer to an integer, which is used as a counter.
64*7c478bd9Sstevel@tonic-gate * 'func' should increment this value for each successful application.
65*7c478bd9Sstevel@tonic-gate * The log is traversed until either 'func' returns FALSE, or when the log
66*7c478bd9Sstevel@tonic-gate * is exhausted. The second argument to 'execute_on_log' is passed as the
67*7c478bd9Sstevel@tonic-gate * second argument to 'func'. The third argument, 'clean' determines whether
68*7c478bd9Sstevel@tonic-gate * the log entry is deleted after the function has been applied.
69*7c478bd9Sstevel@tonic-gate * Returns the number of times that 'func' incremented its third argument.
70*7c478bd9Sstevel@tonic-gate */
71*7c478bd9Sstevel@tonic-gate int
execute_on_log(bool_t (* func)(db_log_entry *,char *,int *),char * arg,bool_t clean)72*7c478bd9Sstevel@tonic-gate db_log::execute_on_log(bool_t (*func) (db_log_entry *, char *, int *),
73*7c478bd9Sstevel@tonic-gate char* arg, bool_t clean)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate db_log_entry *j;
76*7c478bd9Sstevel@tonic-gate int count = 0;
77*7c478bd9Sstevel@tonic-gate bool_t done = FALSE;
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate WRITELOCK(this, 0, "w db_log::execute_on_log");
80*7c478bd9Sstevel@tonic-gate if (open() == TRUE) { // open log
81*7c478bd9Sstevel@tonic-gate while (!done) {
82*7c478bd9Sstevel@tonic-gate j = get();
83*7c478bd9Sstevel@tonic-gate if (j == NULL)
84*7c478bd9Sstevel@tonic-gate break;
85*7c478bd9Sstevel@tonic-gate if ((*func)(j, arg, &count) == FALSE) done = TRUE;
86*7c478bd9Sstevel@tonic-gate if (clean) delete_log_entry(j);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate
89*7c478bd9Sstevel@tonic-gate sync_log();
90*7c478bd9Sstevel@tonic-gate close();
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, count, "wu db_log::execute_on_log");
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate return (count);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate static bool_t
print_log_entry(db_log_entry * j,char *,int * count)98*7c478bd9Sstevel@tonic-gate print_log_entry(db_log_entry *j, char * /* dummy */, int *count)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate j->print();
101*7c478bd9Sstevel@tonic-gate ++ *count;
102*7c478bd9Sstevel@tonic-gate return (TRUE);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate /* Print contents of log file to stdout */
106*7c478bd9Sstevel@tonic-gate int
print()107*7c478bd9Sstevel@tonic-gate db_log::print()
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate return (execute_on_log(&(print_log_entry), NULL));
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate /* Make copy of current log to log pointed to by 'f'. */
113*7c478bd9Sstevel@tonic-gate int
copy(db_log * f)114*7c478bd9Sstevel@tonic-gate db_log::copy(db_log *f)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate db_log_entry *j;
117*7c478bd9Sstevel@tonic-gate int l, ret = 0;
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate WRITELOCK(f, -1, "w f db_log::copy");
120*7c478bd9Sstevel@tonic-gate if ((l = acqnonexcl()) != 0) {
121*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(f, l, "wu f db_log::copy");
122*7c478bd9Sstevel@tonic-gate return (l);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate for (;;) {
125*7c478bd9Sstevel@tonic-gate j = get();
126*7c478bd9Sstevel@tonic-gate if (j == NULL)
127*7c478bd9Sstevel@tonic-gate break;
128*7c478bd9Sstevel@tonic-gate if (f->append(j) < 0) {
129*7c478bd9Sstevel@tonic-gate WARNING_M(
130*7c478bd9Sstevel@tonic-gate "db_log::copy: could not append to log file: ");
131*7c478bd9Sstevel@tonic-gate ret = -1;
132*7c478bd9Sstevel@tonic-gate break;
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate delete_log_entry(j);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate if ((l = relnonexcl()) != 0) {
137*7c478bd9Sstevel@tonic-gate ret = l;
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(f, ret, "wu f db_log::copy");
140*7c478bd9Sstevel@tonic-gate return (ret);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate
143*7c478bd9Sstevel@tonic-gate /* Rewinds current log */
144*7c478bd9Sstevel@tonic-gate int
rewind()145*7c478bd9Sstevel@tonic-gate db_log::rewind()
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate return (fseek(file, 0L, 0));
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate /*
151*7c478bd9Sstevel@tonic-gate * Return the next element in current log; return NULL if end of log or error.
152*7c478bd9Sstevel@tonic-gate * Log must have been opened for READ.
153*7c478bd9Sstevel@tonic-gate */
154*7c478bd9Sstevel@tonic-gate db_log_entry
get()155*7c478bd9Sstevel@tonic-gate *db_log::get()
156*7c478bd9Sstevel@tonic-gate {
157*7c478bd9Sstevel@tonic-gate db_log_entry *j;
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate READLOCK(this, NULL, "r db_log::get");
160*7c478bd9Sstevel@tonic-gate if (mode != PICKLE_READ) {
161*7c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_log::get");
162*7c478bd9Sstevel@tonic-gate return (NULL);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate j = new db_log_entry;
166*7c478bd9Sstevel@tonic-gate
167*7c478bd9Sstevel@tonic-gate if (j == NULL) {
168*7c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_log::get");
169*7c478bd9Sstevel@tonic-gate return (NULL);
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate if (xdr_db_log_entry(&(xdr), j) == FALSE) {
172*7c478bd9Sstevel@tonic-gate delete_log_entry (j);
173*7c478bd9Sstevel@tonic-gate /* WARNING("Could not sucessfully finish reading log"); */
174*7c478bd9Sstevel@tonic-gate READUNLOCK(this, NULL, "ru db_log::get");
175*7c478bd9Sstevel@tonic-gate return (NULL);
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate if (! j->sane()) {
178*7c478bd9Sstevel@tonic-gate WARNING("truncated log entry found");
179*7c478bd9Sstevel@tonic-gate delete_log_entry(j);
180*7c478bd9Sstevel@tonic-gate j = NULL;
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate READUNLOCK(this, j, "ru db_log::get");
183*7c478bd9Sstevel@tonic-gate return (j);
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate
186*7c478bd9Sstevel@tonic-gate /* Append given log entry to log. */
187*7c478bd9Sstevel@tonic-gate int
append(db_log_entry * j)188*7c478bd9Sstevel@tonic-gate db_log::append(db_log_entry *j)
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate int status;
191*7c478bd9Sstevel@tonic-gate
192*7c478bd9Sstevel@tonic-gate WRITELOCK(this, -1, "w db_log::append");
193*7c478bd9Sstevel@tonic-gate if (mode != PICKLE_APPEND) {
194*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, -1, "wu db_log::append");
195*7c478bd9Sstevel@tonic-gate return (-1);
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate
198*7c478bd9Sstevel@tonic-gate /* xdr returns TRUE if successful, FALSE otherwise */
199*7c478bd9Sstevel@tonic-gate status = ((xdr_db_log_entry(&(xdr), j)) ? 0 : -1);
200*7c478bd9Sstevel@tonic-gate if (status < 0) {
201*7c478bd9Sstevel@tonic-gate WARNING("db_log: could not write log entry");
202*7c478bd9Sstevel@tonic-gate } else {
203*7c478bd9Sstevel@tonic-gate syncstate++;
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, status, "wu db_log::append");
206*7c478bd9Sstevel@tonic-gate return (status);
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate
209*7c478bd9Sstevel@tonic-gate int
copy_log_file(char * oldname,char * newname)210*7c478bd9Sstevel@tonic-gate copy_log_file(char *oldname, char *newname) {
211*7c478bd9Sstevel@tonic-gate
212*7c478bd9Sstevel@tonic-gate int from, to, ret = 0;
213*7c478bd9Sstevel@tonic-gate ssize_t size, w, b;
214*7c478bd9Sstevel@tonic-gate char buf[8192];
215*7c478bd9Sstevel@tonic-gate
216*7c478bd9Sstevel@tonic-gate if ((from = open(oldname, O_RDONLY, 0666)) < 0) {
217*7c478bd9Sstevel@tonic-gate if (errno == ENOENT) {
218*7c478bd9Sstevel@tonic-gate return (0);
219*7c478bd9Sstevel@tonic-gate } else {
220*7c478bd9Sstevel@tonic-gate return (errno);
221*7c478bd9Sstevel@tonic-gate }
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate if ((to = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0) {
224*7c478bd9Sstevel@tonic-gate ret = errno;
225*7c478bd9Sstevel@tonic-gate (void) close(from);
226*7c478bd9Sstevel@tonic-gate return (ret);
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate
229*7c478bd9Sstevel@tonic-gate while ((size = read(from, buf, sizeof (buf))) > 0) {
230*7c478bd9Sstevel@tonic-gate b = 0;
231*7c478bd9Sstevel@tonic-gate while (size > 0) {
232*7c478bd9Sstevel@tonic-gate w = write(to, &buf[b], size);
233*7c478bd9Sstevel@tonic-gate if (w < 0) {
234*7c478bd9Sstevel@tonic-gate size == -1;
235*7c478bd9Sstevel@tonic-gate break;
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate size -= w;
238*7c478bd9Sstevel@tonic-gate b += w;
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate if (size != 0) {
241*7c478bd9Sstevel@tonic-gate ret = errno;
242*7c478bd9Sstevel@tonic-gate break;
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate }
245*7c478bd9Sstevel@tonic-gate
246*7c478bd9Sstevel@tonic-gate (void) close(from);
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate if (ret != 0) {
249*7c478bd9Sstevel@tonic-gate errno = ret;
250*7c478bd9Sstevel@tonic-gate WARNING_M("db_log: error copying log file")
251*7c478bd9Sstevel@tonic-gate (void) close(to);
252*7c478bd9Sstevel@tonic-gate return (ret);
253*7c478bd9Sstevel@tonic-gate }
254*7c478bd9Sstevel@tonic-gate
255*7c478bd9Sstevel@tonic-gate if (fsync(to) != 0) {
256*7c478bd9Sstevel@tonic-gate ret = errno;
257*7c478bd9Sstevel@tonic-gate WARNING_M("db_log: error syncing log file");
258*7c478bd9Sstevel@tonic-gate }
259*7c478bd9Sstevel@tonic-gate
260*7c478bd9Sstevel@tonic-gate (void) close(to);
261*7c478bd9Sstevel@tonic-gate
262*7c478bd9Sstevel@tonic-gate return (ret);
263*7c478bd9Sstevel@tonic-gate
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate
266*7c478bd9Sstevel@tonic-gate /*
267*7c478bd9Sstevel@tonic-gate * Return value is expected to be the usual C convention of non-zero
268*7c478bd9Sstevel@tonic-gate * for success, 0 for failure.
269*7c478bd9Sstevel@tonic-gate */
270*7c478bd9Sstevel@tonic-gate int
sync_log()271*7c478bd9Sstevel@tonic-gate db_log::sync_log()
272*7c478bd9Sstevel@tonic-gate {
273*7c478bd9Sstevel@tonic-gate int status, err;
274*7c478bd9Sstevel@tonic-gate
275*7c478bd9Sstevel@tonic-gate WRITELOCK(this, -1, "w db_log::sync_log");
276*7c478bd9Sstevel@tonic-gate status = fflush(file);
277*7c478bd9Sstevel@tonic-gate if (status < 0) {
278*7c478bd9Sstevel@tonic-gate WARNING("db_log: could not flush log entry to disk");
279*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, status, "wu db_log::sync_log");
280*7c478bd9Sstevel@tonic-gate return (status);
281*7c478bd9Sstevel@tonic-gate }
282*7c478bd9Sstevel@tonic-gate
283*7c478bd9Sstevel@tonic-gate status = fsync(fileno(file));
284*7c478bd9Sstevel@tonic-gate if (status < 0) {
285*7c478bd9Sstevel@tonic-gate WARNING("db_log: could not sync log entry to disk");
286*7c478bd9Sstevel@tonic-gate } else if (tmplog != 0) {
287*7c478bd9Sstevel@tonic-gate if (syncstate == 0) {
288*7c478bd9Sstevel@tonic-gate /* Log already stable; nothing to do */
289*7c478bd9Sstevel@tonic-gate err = 0;
290*7c478bd9Sstevel@tonic-gate } else if ((err = copy_log_file(tmplog, stablelog)) == 0) {
291*7c478bd9Sstevel@tonic-gate if (rename(stablelog, oldlog) != 0) {
292*7c478bd9Sstevel@tonic-gate WARNING_M("db_log: could not mv stable log");
293*7c478bd9Sstevel@tonic-gate } else {
294*7c478bd9Sstevel@tonic-gate syncstate = 0;
295*7c478bd9Sstevel@tonic-gate }
296*7c478bd9Sstevel@tonic-gate } else {
297*7c478bd9Sstevel@tonic-gate errno = err;
298*7c478bd9Sstevel@tonic-gate WARNING_M("db_log: could not stabilize log");
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate status = (err == 0);
301*7c478bd9Sstevel@tonic-gate } else {
302*7c478bd9Sstevel@tonic-gate /*
303*7c478bd9Sstevel@tonic-gate * Successful sync of file, but no tmplog to sync
304*7c478bd9Sstevel@tonic-gate * so we make sure we return 'success'.
305*7c478bd9Sstevel@tonic-gate */
306*7c478bd9Sstevel@tonic-gate status = 1;
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, status, "wu db_log::sync_log");
309*7c478bd9Sstevel@tonic-gate return (status);
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate
312*7c478bd9Sstevel@tonic-gate int
close()313*7c478bd9Sstevel@tonic-gate db_log::close() {
314*7c478bd9Sstevel@tonic-gate
315*7c478bd9Sstevel@tonic-gate int ret;
316*7c478bd9Sstevel@tonic-gate
317*7c478bd9Sstevel@tonic-gate WRITELOCK(this, -1, "w db_log::close");
318*7c478bd9Sstevel@tonic-gate if (mode != PICKLE_READ && oldlog != 0) {
319*7c478bd9Sstevel@tonic-gate if (syncstate != 0) {
320*7c478bd9Sstevel@tonic-gate WARNING("db_log: closing unstable tmp log");
321*7c478bd9Sstevel@tonic-gate }
322*7c478bd9Sstevel@tonic-gate filename = oldlog;
323*7c478bd9Sstevel@tonic-gate oldlog = 0;
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate
326*7c478bd9Sstevel@tonic-gate ret = pickle_file::close();
327*7c478bd9Sstevel@tonic-gate if (tmplog != 0) {
328*7c478bd9Sstevel@tonic-gate (void) unlink(tmplog);
329*7c478bd9Sstevel@tonic-gate delete tmplog;
330*7c478bd9Sstevel@tonic-gate tmplog = 0;
331*7c478bd9Sstevel@tonic-gate }
332*7c478bd9Sstevel@tonic-gate if (stablelog != 0) {
333*7c478bd9Sstevel@tonic-gate delete stablelog;
334*7c478bd9Sstevel@tonic-gate stablelog = 0;
335*7c478bd9Sstevel@tonic-gate }
336*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::close");
337*7c478bd9Sstevel@tonic-gate return (ret);
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate
340*7c478bd9Sstevel@tonic-gate bool_t
open(void)341*7c478bd9Sstevel@tonic-gate db_log::open(void) {
342*7c478bd9Sstevel@tonic-gate
343*7c478bd9Sstevel@tonic-gate int len, cpstat;
344*7c478bd9Sstevel@tonic-gate bool_t ret;
345*7c478bd9Sstevel@tonic-gate
346*7c478bd9Sstevel@tonic-gate WRITELOCK(this, FALSE, "w db_log::open");
347*7c478bd9Sstevel@tonic-gate if (mode == PICKLE_READ || (!copylog)) {
348*7c478bd9Sstevel@tonic-gate ret = pickle_file::open();
349*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::open");
350*7c478bd9Sstevel@tonic-gate return (ret);
351*7c478bd9Sstevel@tonic-gate }
352*7c478bd9Sstevel@tonic-gate
353*7c478bd9Sstevel@tonic-gate len = strlen(filename);
354*7c478bd9Sstevel@tonic-gate tmplog = new char[len + sizeof (".tmp")];
355*7c478bd9Sstevel@tonic-gate if (tmplog == 0) {
356*7c478bd9Sstevel@tonic-gate WARNING("db_log: could not allocate tmp log name");
357*7c478bd9Sstevel@tonic-gate ret = pickle_file::open();
358*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::open");
359*7c478bd9Sstevel@tonic-gate return (ret);
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate stablelog = new char[len + sizeof (".stable")];
362*7c478bd9Sstevel@tonic-gate if (stablelog == 0) {
363*7c478bd9Sstevel@tonic-gate WARNING("db_log: could not allocate stable log name");
364*7c478bd9Sstevel@tonic-gate delete tmplog;
365*7c478bd9Sstevel@tonic-gate tmplog = 0;
366*7c478bd9Sstevel@tonic-gate ret = pickle_file::open();
367*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::open");
368*7c478bd9Sstevel@tonic-gate return (ret);
369*7c478bd9Sstevel@tonic-gate }
370*7c478bd9Sstevel@tonic-gate sprintf(tmplog, "%s.tmp", filename);
371*7c478bd9Sstevel@tonic-gate sprintf(stablelog, "%s.stable", filename);
372*7c478bd9Sstevel@tonic-gate
373*7c478bd9Sstevel@tonic-gate if ((cpstat = copy_log_file(filename, tmplog)) == 0) {
374*7c478bd9Sstevel@tonic-gate oldlog = filename;
375*7c478bd9Sstevel@tonic-gate filename = tmplog;
376*7c478bd9Sstevel@tonic-gate } else {
377*7c478bd9Sstevel@tonic-gate syslog(LOG_WARNING,
378*7c478bd9Sstevel@tonic-gate "db_log: Error copying \"%s\" to \"%s\": %s",
379*7c478bd9Sstevel@tonic-gate filename, tmplog, strerror(cpstat));
380*7c478bd9Sstevel@tonic-gate delete tmplog;
381*7c478bd9Sstevel@tonic-gate tmplog = 0;
382*7c478bd9Sstevel@tonic-gate delete stablelog;
383*7c478bd9Sstevel@tonic-gate stablelog = 0;
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate
386*7c478bd9Sstevel@tonic-gate ret = pickle_file::open();
387*7c478bd9Sstevel@tonic-gate WRITEUNLOCK(this, ret, "wu db_log::open");
388*7c478bd9Sstevel@tonic-gate return (ret);
389*7c478bd9Sstevel@tonic-gate }
390