source3/intl/lan_tdb.c: convert to dbwrap.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 15 Jun 2012 04:09:19 +0000 (13:39 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 15 Jun 2012 04:09:19 +0000 (13:39 +0930)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
source3/intl/lang_tdb.c

index 6070e29e5ade69209fd6fdb8ef99ff1d6a6dcbb0..248d9ea8b3bb9a54e1ef7032536a1c37710f0a5f 100644 (file)
 #include "system/filesys.h"
 #include "intl/lang_tdb.h"
 #include "util_tdb.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
 
-static TDB_CONTEXT *tdb;
+static struct db_context *db;
 
 /* the currently selected language */
 static char *current_lang;
@@ -42,13 +44,13 @@ static bool load_msg(const char *msg_file)
                return False;
        }
 
-       if (tdb_lockall(tdb) != 0) {
+       if (dbwrap_transaction_start(db) != 0) {
                TALLOC_FREE(lines);
                return False;
        }
 
        /* wipe the db */
-       tdb_wipe_all(tdb);
+       dbwrap_wipe(db);
 
        msgid = NULL;
        
@@ -66,13 +68,13 @@ static bool load_msg(const char *msg_file)
                        all_string_sub(msgid, "\\n", "\n", 0);
                        all_string_sub(msgstr, "\\n", "\n", 0);
                        data = string_term_tdb_data(msgstr);
-                       tdb_store_bystring(tdb, msgid, data, 0);
+                       dbwrap_store_bystring(db, msgid, data, 0);
                        msgid = NULL;
                }
        }
 
        TALLOC_FREE(lines);
-       tdb_unlockall(tdb);
+       dbwrap_transaction_commit(db);
 
        return True;
 }
@@ -102,7 +104,7 @@ bool lang_tdb_init(const char *lang)
        char *msg_path = NULL;
        struct stat st;
        static int initialised;
-       time_t loadtime;
+       int32_t loadtime;
        bool result = False;
 
        /* we only want to init once per process, unless given
@@ -112,10 +114,7 @@ bool lang_tdb_init(const char *lang)
 
        if (initialised) {
                /* we are re-initialising, free up any old init */
-               if (tdb) {
-                       tdb_close(tdb);
-                       tdb = NULL;
-               }
+               TALLOC_FREE(db);
                SAFE_FREE(current_lang);
        }
 
@@ -149,10 +148,10 @@ bool lang_tdb_init(const char *lang)
 
        DEBUG(10, ("lang_tdb_init: loading %s\n", path));
 
-       tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
-       if (!tdb) {
-               tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
-               if (!tdb) {
+       db = db_s3open(NULL, path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
+       if (!db) {
+               db = db_s3open(NULL, path, 0, TDB_DEFAULT, O_RDONLY, 0);
+               if (!db) {
                        DEBUG(10, ("lang_tdb_init: %s: %s\n", path,
                                   strerror(errno)));
                        goto done;
@@ -162,11 +161,10 @@ bool lang_tdb_init(const char *lang)
                goto done;
        }
 
-       loadtime = tdb_fetch_int32(tdb, "/LOADTIME/");
-
-       if (loadtime == -1 || loadtime < st.st_mtime) {
+       if (!NT_STATUS_IS_OK(dbwrap_fetch_int32(db, "/LOADTIME/", &loadtime))
+           || loadtime < st.st_mtime) {
                load_msg(msg_path);
-               tdb_store_int32(tdb, "/LOADTIME/", (int)time(NULL));
+               dbwrap_store_int32(db, "/LOADTIME/", (int)time(NULL));
        }
 
        current_lang = SMB_STRDUP(lang);
@@ -191,7 +189,7 @@ const char *lang_msg(const char *msgid)
 
        lang_tdb_init(NULL);
 
-       if (!tdb) return msgid;
+       if (!db) return msgid;
 
        /* Due to the way quotes in msgids are escaped in the msg file we
           must replace " with \" before doing a lookup in the tdb. */
@@ -220,15 +218,15 @@ const char *lang_msg(const char *msgid)
 
        *q = 0;
 
-       data = tdb_fetch_bystring(tdb, msgid_quoted);
+       if (!NT_STATUS_IS_OK(dbwrap_fetch_bystring(db, NULL, msgid_quoted,
+                                                  &data))) {
+               free(msgid_quoted);
+               /* if the message isn't found then we still need to
+                  return a pointer that can be freed. Pity. */
+               return talloc_strdup(db, msgid);
+       }
 
        free(msgid_quoted);
-
-       /* if the message isn't found then we still need to return a pointer
-          that can be freed. Pity. */
-       if (!data.dptr)
-               return SMB_STRDUP(msgid);
-
        return (const char *)data.dptr;
 }
 
@@ -236,8 +234,8 @@ const char *lang_msg(const char *msgid)
 /* free up a string from lang_msg() */
 void lang_msg_free(const char *msgstr)
 {
-       if (!tdb) return;
-       free(discard_const_p(void, msgstr));
+       if (!db) return;
+       talloc_free(discard_const_p(void, msgstr));
 }
 
 /*