]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/dbif.c
big merge for db frontend and unicode.
[netatalk.git] / etc / cnid_dbd / dbif.c
1 /*
2  * $Id: dbif.c,v 1.1.4.1 2003-09-09 16:42:20 didg Exp $
3  *
4  * Copyright (C) Joerg Lenneis 2003
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <errno.h>
14
15 #include <db.h>
16 #include <atalk/logger.h>
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
19 #endif /* HAVE_SYS_TYPES_H */
20
21 #include <string.h>
22 #include "db_param.h"
23 #include "dbif.h"
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27
28 #define DB_ERRLOGFILE "db_errlog"
29
30
31 static DB_ENV *db_env = NULL;
32 static DB_TXN *db_txn = NULL;
33 static FILE   *db_errlog = NULL;
34
35 #ifdef CNID_BACKEND_DBD_TXN
36 #define DBOPTIONS    (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
37 #else
38 #define DBOPTIONS    (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL) 
39 #endif
40
41 static struct db_table {
42      char            *name;
43      DB              *db;
44      u_int32_t       general_flags;
45      DBTYPE          type;
46 } db_table[] =
47 {
48      { "cnid2.db",       NULL,      0, DB_HASH},
49      { "devino.db",      NULL,      0, DB_HASH},
50      { "didname.db",     NULL,      0, DB_HASH},
51 };
52
53 /*
54  *  We assume our current directory is already the BDB homedir. Otherwise
55  *  opening the databases will not work as expected.
56  */
57
58 extern int didname(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey);
59 extern int devino(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey);
60
61 static int env_init(struct db_param *dbp)
62 {
63     int ret;
64
65     if ((ret = db_env_create(&db_env, 0))) {
66         LOG(log_error, logtype_cnid, "error creating DB environment: %s", 
67             db_strerror(ret));
68         return -1;
69     }    
70 #ifdef CNID_BACKEND_DBD_TXN
71     if (db_errlog != NULL)
72         db_env->set_errfile(db_env, db_errlog); 
73     db_env->set_verbose(db_env, DB_VERB_RECOVERY, 1);
74     db_env->set_verbose(db_env, DB_VERB_CHKPOINT, 1);
75     if (ret = db_env->open(db_env, ".", DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | 
76                            DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE | DB_RECOVER, 0)) {
77         LOG(log_error, logtype_cnid, "error opening DB environment: %s", 
78             db_strerror(ret));
79         db_env->close(db_env, 0);
80         return -1;
81     }
82
83     if (db_errlog != NULL)
84         fflush(db_errlog);
85
86     if (ret = db_env->close(db_env, 0)) {
87         LOG(log_error, logtype_cnid, "error closining DB environment after recovery: %s", 
88             db_strerror(ret));
89         return -1;
90     }
91 #endif
92     if ((ret = db_env_create(&db_env, 0))) {
93         LOG(log_error, logtype_cnid, "error creating DB environment after recovery: %s",
94             db_strerror(ret));
95         return -1;
96     }
97     if ((ret = db_env->set_cachesize(db_env, 0, 1024 * dbp->cachesize, 0))) {
98         LOG(log_error, logtype_cnid, "error settining DB environment cachesize to %i: %s",
99             dbp->cachesize, db_strerror(ret));
100         db_env->close(db_env, 0);
101         return -1;
102     }
103     
104     if (db_errlog != NULL)
105         db_env->set_errfile(db_env, db_errlog);
106     if ((ret = db_env->open(db_env, ".", DBOPTIONS , 0))) {
107         LOG(log_error, logtype_cnid, "error opening DB environment after recovery: %s",
108             db_strerror(ret));
109         db_env->close(db_env, 0);
110         return -1;      
111     }
112
113 #ifdef CNID_BACKEND_DBD_TXN
114     if (dbp->nosync && (ret = db_env->set_flags(db_env, DB_TXN_NOSYNC, 1))) {
115         LOG(log_error, logtype_cnid, "error setting TXN_NOSYNC flag: %s",
116             db_strerror(ret));
117         db_env->close(db_env, 0);
118         return -1;      
119     }
120 #endif
121     return 0;
122 }
123
124 /* --------------- */
125 static int  db_compat_associate (DB *p, DB *s,
126                    int (*callback)(DB *, const DBT *,const DBT *, DBT *),
127                    u_int32_t flags)
128 {
129 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
130     return p->associate(p, NULL, s, callback, flags);
131 #else
132     return p->associate(p,       s, callback, flags);
133 #endif
134 }
135
136 /* --------------- */
137 static int db_compat_open(DB *db, char *file, char *name, DBTYPE type, int mode)
138 {
139     int ret;
140
141 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
142 #ifdef CNID_BACKEND_DBD_TXN
143     ret = db->open(db, NULL, file, name, type, DB_CREATE | DB_AUTO_COMMIT, mode); 
144 #else 
145     ret = db->open(db, NULL, file, name, type, DB_CREATE                 , mode); 
146 #endif
147 #else
148     ret = db->open(db,       file, name, type, DB_CREATE                 , mode); 
149 #endif
150
151     if (ret) {
152         LOG(log_error, logtype_cnid, "error opening database %s: %s", name, db_strerror(ret));
153         return -1;
154     } else {
155         return 0;
156     }
157 }
158
159 /* --------------- */
160 int dbif_stamp(void *buffer, int size)
161 {
162     struct stat st;
163     int         rc;
164
165     if (size < 8)
166         return -1;
167
168     if ((rc = stat(db_table[0].name, &st)) < 0) {
169         LOG(log_error, logtype_cnid, "error stating database %s: %s", db_table[0].name, db_strerror(rc));
170         return -1;
171     }
172     memset(buffer, 0, size);
173     memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
174
175     return 0;
176 }
177
178 /* --------------- */
179 int dbif_open(struct db_param *dbp)
180 {
181     int ret;
182     int i;
183
184     if ((db_errlog = fopen(DB_ERRLOGFILE, "a")) == NULL)
185         LOG(log_warning, logtype_cnid, "error creating/opening DB errlogfile: %s", strerror(errno));
186
187     if (env_init(dbp) < 0)
188         return -1;
189
190     /* db_env will point to a valid environment handle from here onwards if
191        transactions are used or to NULL otherwise */
192
193     for (i = 0; i != DBIF_DB_CNT; i++) {
194         if ((ret = db_create(&(db_table[i].db), db_env, 0))) {
195             LOG(log_error, logtype_cnid, "error creating handle for database %s: %s", 
196                 db_table[i].name, db_strerror(ret));
197             return -1;
198         }
199         if (db_table[i].general_flags) { 
200             if ((ret = db_table[i].db->set_flags(db_table[i].db, db_table[i].general_flags))) {
201                 LOG(log_error, logtype_cnid, "error setting flags for database %s: %s", 
202                     db_table[i].name, db_strerror(ret));
203                 return -1;
204             }
205         }
206 #if 0
207 #ifndef CNID_BACKEND_DBD_TXN
208         if ((ret = db_table[i].db->set_cachesize(db_table[i].db, 0, 1024 * dbp->cachesize, 0))) {
209             LOG(log_error, logtype_cnid, "error setting DB cachesize to %i for database %s: %s",
210                 dbp->cachesize, db_table[i].name, db_strerror(ret));
211             return -1;
212         }
213 #endif /* CNID_BACKEND_DBD_TXN */
214 #endif
215         if (db_compat_open(db_table[i].db, db_table[0].name, db_table[i].name, db_table[i].type, 0664) < 0)
216             return -1;
217         if (db_errlog != NULL)
218             db_table[i].db->set_errfile(db_table[i].db, db_errlog);
219     }     
220     
221     /* TODO: Implement CNID DB versioning info on new databases. */
222     /* TODO: Make transaction support a runtime option. */
223     /* Associate the secondary with the primary. */
224     if ((ret = db_compat_associate(db_table[0].db, db_table[DBIF_IDX_DIDNAME].db, didname, 0)) != 0) {
225         LOG(log_error, logtype_cnid, "Failed to associate didname database: %s",db_strerror(ret));
226         return -1;
227     }
228  
229     if ((ret = db_compat_associate(db_table[0].db, db_table[DBIF_IDX_DEVINO].db, devino, 0)) != 0) {
230         LOG(log_error, logtype_cnid, "Failed to associate devino database: %s",db_strerror(ret));
231         return -1;
232     }
233     return 0;
234 }
235
236 /* ------------------------ */
237 int dbif_close()
238 {
239     int i;
240     int ret;
241     int err = 0;
242      
243     for (i = DBIF_DB_CNT -1; i >= 0; i--) {
244         if (db_table[i].db != NULL && (ret = db_table[i].db->close(db_table[i].db, 0))) {
245             LOG(log_error, logtype_cnid, "error closing database %s: %s", db_table[i].name, db_strerror(ret));
246             err++;
247         }
248     }
249     if (db_env != NULL && (ret = db_env->close(db_env, 0))) { 
250         LOG(log_error, logtype_cnid, "error closing DB environment: %s", db_strerror(ret));
251         err++;
252     }
253     if (db_errlog != NULL && fclose(db_errlog) == EOF) {
254         LOG(log_error, logtype_cnid, "error closing DB logfile: %s", strerror(errno));
255         err++;
256     }
257     if (err)
258         return -1;
259     else
260         return 0;
261 }
262
263 /*
264  *  The following three functions are wrappers for DB->get(), DB->put() and
265  *  DB->del(). We define them here because we want access to the db_txn
266  *  transaction handle and the database handles limited to the functions in this
267  *  file. A consequence is that there is always only one transaction in
268  *  progress. For nontransactional access db_txn is NULL. All three return -1 on
269  *  error. dbif_get()/dbif_del return 1 if the key was found and 0
270  *  otherwise. dbif_put() returns 0 if key/val was successfully updated and 1 if
271  *  the DB_NOOVERWRITE flag was specified and the key already exists.
272  *  
273  *  All return codes other than DB_NOTFOUND and DB_KEYEXIST from the DB->()
274  *  functions are not expected and therefore error conditions.
275  */
276
277 int dbif_get(const int dbi, DBT *key, DBT *val, u_int32_t flags)
278 {
279     int ret;
280     DB *db = db_table[dbi].db;
281
282     ret = db->get(db, db_txn, key, val, flags);
283      
284     if (ret == DB_NOTFOUND)
285         return 0;
286     if (ret) {
287         LOG(log_error, logtype_cnid, "error retrieving value from %s: %s", db_table[dbi].name, db_strerror(errno));
288         return -1;
289     } else 
290         return 1;
291 }
292
293 int dbif_put(const int dbi, DBT *key, DBT *val, u_int32_t flags)
294 {
295     int ret;
296     DB *db = db_table[dbi].db;
297
298     ret = db->put(db, db_txn, key, val, flags);
299      
300     if (ret) {
301         if ((flags & DB_NOOVERWRITE) && ret == DB_KEYEXIST) {
302             return 1;
303         } else {
304             LOG(log_error, logtype_cnid, "error setting key/value in %s: %s", db_table[dbi].name, db_strerror(errno));
305             return -1;
306         }
307     } else
308         return 0;
309 }
310
311 int dbif_del(const int dbi, DBT *key, u_int32_t flags)
312 {
313     int ret;
314     DB *db = db_table[dbi].db;
315
316     ret = db->del(db, db_txn, key, flags);
317
318     if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD)
319         return 0;
320     if (ret) {
321         LOG(log_error, logtype_cnid, "error deleting key/value from %s: %s", db_table[dbi].name, db_strerror(errno));
322         return -1;
323     } else
324         return 1;
325 }
326
327 #ifdef CNID_BACKEND_DBD_TXN
328
329 int dbif_txn_begin()
330 {
331     int ret;
332 #if DB_VERSION_MAJOR >= 4
333     ret = db_env->txn_begin(db_env, NULL, &db_txn, 0);
334 #else     
335     ret = txn_begin(db_env, NULL, &db_txn, 0);
336 #endif
337     if (ret) {
338         LOG(log_error, logtype_cnid, "error starting transaction: %s", db_strerror(errno));
339         return -1;
340     } else 
341         return 0;
342 }
343
344 int dbif_txn_commit()
345 {
346     int ret;
347 #if DB_VERSION_MAJOR >= 4
348     ret = db_txn->commit(db_txn, 0);
349 #else
350     ret = txn_commit(db_txn, 0);
351 #endif
352     if (ret) {
353         LOG(log_error, logtype_cnid, "error committing transaction: %s", db_strerror(errno));
354         return -1;
355     } else 
356         return 0;
357 }
358
359 int dbif_txn_abort()
360 {
361     int ret;
362 #if DB_VERSION_MAJOR >= 4
363     ret = db_txn->abort(db_txn);
364 #else
365     ret = txn_abort(db_txn);
366 #endif
367     if (ret) {
368         LOG(log_error, logtype_cnid, "error aborting transaction: %s", db_strerror(errno));
369         return -1;
370     } else
371         return 0;
372 }
373
374 int dbif_txn_checkpoint(u_int32_t kbyte, u_int32_t min, u_int32_t flags)
375 {
376     int ret;
377 #if DB_VERSION_MAJOR >= 4
378     ret = db_env->txn_checkpoint(db_env, kbyte, min, flags);
379 #else 
380     ret = txn_checkpoint(db_env, kbyte, min, flags);
381 #endif
382     if (ret) {
383         LOG(log_error, logtype_cnid, "error checkpointing transaction susystem: %s", db_strerror(errno));
384         return -1;
385     } else 
386         return 0;
387 }
388
389 #else
390
391 int dbif_sync()
392 {
393     int i;
394     int ret;
395     int err = 0;
396      
397     for (i = 0; i != /* DBIF_DB_CNT*/ 1; i++) {
398         if ((ret = db_table[i].db->sync(db_table[i].db, 0))) {
399             LOG(log_error, logtype_cnid, "error syncing database %s: %s", db_table[i].name, db_strerror(ret));
400             err++;
401         }
402     }
403  
404     if (err)
405         return -1;
406     else
407         return 0;
408 }
409
410 #endif /* CNID_BACKEND_DBD_TXN */
411