summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/back-ldbm/dbhelp.c
blob: 93a0de88249487e9f783853a79e1d691387a66ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif


/* 
 * File for helper functions related to BerkeleyDB.
 * This exists because dblayer.c is 5k+ lines long, 
 * so it seems time to move code to a new file.
 */

#include "back-ldbm.h"
#include "dblayer.h"

static int dblayer_copy_file_keybykey(DB_ENV *env, char *source_file_name, char *destination_file_name, int overwrite, dblayer_private *priv)
{
	int retval = 0;
	int retval_cleanup = 0;
	DB *source_file = NULL;
	DB *destination_file = NULL;
	DBC *source_cursor = NULL;
	DBTYPE dbtype = 0;
	PRUint32 dbflags = 0;
	PRUint32 dbpagesize = 0;
	int cursor_flag = 0;
	int finished = 0;
	int mode = 0;

	LDAPDebug( LDAP_DEBUG_TRACE, "=> dblayer_copy_file_keybykey\n", 0, 0, 0 );

	if (!env) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Out of memory\n", 0, 0, 0);
		goto error;
	}

	if (priv->dblayer_file_mode)
		mode = priv->dblayer_file_mode;
	dblayer_set_env_debugging(env, priv);

	/* Open the source file */
	retval = db_create(&source_file, env, 0);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Create error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	retval = (source_file->open)(source_file, NULL, source_file_name, NULL, DB_UNKNOWN, DB_RDONLY, 0);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Open error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	/* Get the info we need from the source file */
	retval = source_file->get_flags(source_file, &dbflags);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, get_flags error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	retval = source_file->get_type(source_file, &dbtype);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, get_type error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	retval = source_file->get_pagesize(source_file, &dbpagesize);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, get_pagesize error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	/* Open the destination file
	 * and make sure that it has the correct page size, the correct access method, and the correct flags (dup etc)
	 */
	retval = db_create(&destination_file, env, 0);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Create error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	retval = destination_file->set_flags(destination_file,dbflags);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, set_flags error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	retval = destination_file->set_pagesize(destination_file,dbpagesize);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, set_pagesize error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	retval = (destination_file->open)(destination_file, NULL, destination_file_name, NULL, dbtype, DB_CREATE | DB_EXCL, mode);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Open error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	/* Open a cursor on the source file */
	retval = source_file->cursor(source_file,NULL,&source_cursor,0);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Create cursor error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	/* Seek to the first key */
	cursor_flag = DB_FIRST;
	/* Loop seeking to the next key until they're all done */
	while (!finished) {
		DBT key = {0};
		DBT data = {0};
		retval = source_cursor->c_get(source_cursor, &key, &data, cursor_flag);
		if (retval) {
			/* DB_NOTFOUND is expected when we find the end, log a message for any other error.
			 * In either case, set finished=1 so we can hop down and close the cursor. */
			if ( DB_NOTFOUND != retval )
			{
				LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, c_get error %d: %s\n", retval, db_strerror(retval), 0);
				goto error;
			}
			retval = 0; /* DB_NOTFOUND was OK... */
			finished = 1;
		} else {
			/* For each key, insert into the destination file */
			retval = destination_file->put(destination_file, NULL, &key, &data, 0);
			if (retval) {
				LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, put error %d: %s\n", retval, db_strerror(retval), 0);
				goto error;
			}
			cursor_flag = DB_NEXT;
		}
	}

error:
	/* Close the cursor */
	if (source_cursor) {
		retval_cleanup = source_cursor->c_close(source_cursor);
		if (retval_cleanup) {
			LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Close cursor error %d: %s\n", retval_cleanup, db_strerror(retval_cleanup), 0);
			retval += retval_cleanup;
		}
	}
	/* Close the source file */
	if (source_file) {
		retval_cleanup = source_file->close(source_file,0);
		source_file = NULL;
		if (retval_cleanup) {
			LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Close error %d: %s\n", retval_cleanup, db_strerror(retval_cleanup), 0); 
			retval += retval_cleanup;
		}
	}
	/* Close the destination file */
	if (destination_file) {
		retval_cleanup = destination_file->close(destination_file,0);
		destination_file = NULL;
		if (retval_cleanup) {
			LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_keybykey, Close error %d: %s\n", retval_cleanup, db_strerror(retval_cleanup), 0);
			retval += retval_cleanup;
		}
	}

	LDAPDebug( LDAP_DEBUG_TRACE, "<= dblayer_copy_file_keybykey\n", 0, 0, 0 );
	return retval;
}

int dblayer_copy_file_resetlsns(char *home_dir ,char *source_file_name, char *destination_file_name, int overwrite, dblayer_private *priv)
{
	int retval = 0;
	DB_ENV *env = NULL;

	LDAPDebug( LDAP_DEBUG_TRACE, "=> dblayer_copy_file_resetlsns\n", 0, 0, 0 );
	/* Make the environment */

	retval = dblayer_make_private_simple_env(home_dir,&env);
	if (retval || !env) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_resetlsns: Call to dblayer_make_private_simple_env failed!\n" 
			"Unable to open an environment.", 0, 0, 0);
		goto out;
	}
	/* Do the copy */
	retval = dblayer_copy_file_keybykey(env, source_file_name, destination_file_name, overwrite, priv);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_resetlsns: Copy not completed successfully.", 0, 0, 0);
	}
out:
	/* Close the environment */
	if (env) {
		int retval2 = 0;
		retval2 = env->close(env,0);
		if (retval2) {
			if (0 == retval) {
				retval = retval2;
				LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_resetlsns, error %d: %s\n", retval, db_strerror(retval), 0);
			}
		}
	}

	LDAPDebug( LDAP_DEBUG_TRACE, "<= dblayer_copy_file_resetlsns\n", 0, 0, 0 );
	return retval;
}

void dblayer_set_env_debugging(DB_ENV *pEnv, dblayer_private *priv)
{
	pEnv->set_errpfx(pEnv, "ns-slapd");
    if (priv->dblayer_verbose) {
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4300
        /* DB_VERB_CHKPOINT removed in 43 */
#else
        pEnv->set_verbose(pEnv, DB_VERB_CHKPOINT, 1);    /* 1 means on */
#endif
        pEnv->set_verbose(pEnv, DB_VERB_DEADLOCK, 1);    /* 1 means on */
        pEnv->set_verbose(pEnv, DB_VERB_RECOVERY, 1);    /* 1 means on */
        pEnv->set_verbose(pEnv, DB_VERB_WAITSFOR, 1);    /* 1 means on */
    }
    if (priv->dblayer_debug) {
        pEnv->set_errcall(pEnv, dblayer_log_print);
    }

}

/* Make an environment to be used for isolated recovery (e.g. during a partial restore operation) */
int dblayer_make_private_recovery_env(char *db_home_dir, dblayer_private *priv, DB_ENV **env)
{
	int retval = 0;
	DB_ENV *ret_env = NULL;

	LDAPDebug( LDAP_DEBUG_TRACE, "=> dblayer_make_private_recovery_env\n", 0, 0, 0 );
	if (NULL == env) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_recovery_env: Null environment.  Cannot continue.", 0, 0, 0);
		return -1;
	}
	*env = NULL;

	retval = db_env_create(&ret_env,0);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_recovery_env, Create error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}
	dblayer_set_env_debugging(ret_env, priv);

	retval = (ret_env->open)(ret_env,db_home_dir, DB_INIT_TXN | DB_RECOVER_FATAL | DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE,0);
	if (0 == retval) {
		*env = ret_env;
	} else {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_recovery_env, Open error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}

error:
	LDAPDebug( LDAP_DEBUG_TRACE, "<= dblayer_make_private_recovery_env\n", 0, 0, 0 );
	return retval;
}

/* Make an environment to be used for simple non-transacted database operations, e.g. fixup during upgrade */
int dblayer_make_private_simple_env(char *db_home_dir, DB_ENV **env)
{
	int retval = 0;
	DB_ENV *ret_env = NULL;

	LDAPDebug( LDAP_DEBUG_TRACE, "=> dblayer_make_private_simple_env\n", 0, 0, 0 );
	if (NULL == env) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_simple_env: Null environment.  Cannot continue.", 0, 0, 0);
		return -1;
	}
	*env = NULL;

	retval = db_env_create(&ret_env,0);
	if (retval) {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_simple_env, error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}

	retval = (ret_env->open)(ret_env,db_home_dir,DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE,0);
	if (0 == retval) {
		*env = ret_env;
	} else {
		LDAPDebug(LDAP_DEBUG_ANY, "dblayer_make_private_simple_env, error %d: %s\n", retval, db_strerror(retval), 0);
		goto error;
	}

error:
	LDAPDebug( LDAP_DEBUG_TRACE, "<= dblayer_make_private_simple_env\n", 0, 0, 0 );
	return retval;
}

char* last_four_chars(const char* s)
{
    size_t l = strlen(s);
    return ((char*)s + (l - 4));
}