astdb2bdb.c File Reference

SQLite 3 astdb to Berkeley DB converter. More...

#include "asterisk.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sqlite3.h>
#include <libgen.h>
#include "db1-ast/include/db.h"

Include dependency graph for astdb2bdb.c:

Go to the source code of this file.

Functions

static int add_row_to_bdb (void *arg, int columns, char **values, char **column_names)
static int convert_bdb_to_sqlite3 (void)
static int create_bdb_astdb (void)
static int db_open_sqlite3 (const char *dbname)
int main (int argc, char *argv[])

Variables

static DBbdb
static sqlite3 * sql3db


Detailed Description

SQLite 3 astdb to Berkeley DB converter.

Author:
Terry Wilson <twilson@digium.com>

Definition in file astdb2bdb.c.


Function Documentation

static int add_row_to_bdb ( void *  arg,
int  columns,
char **  values,
char **  column_names 
) [static]

Definition at line 44 of file astdb2bdb.c.

References DBT::data, ast_db_entry::key, DBT::size, and value.

Referenced by convert_bdb_to_sqlite3().

00045 {
00046    DBT key = { 0, }, value = { 0, };
00047 
00048    if (columns != 2 || strcmp(column_names[0], "key") || strcmp(column_names[1], "value")) {
00049       fprintf(stderr, "Unknown row type\n");
00050       return SQLITE_ABORT;
00051    }
00052 
00053    key.data = values[0];
00054    key.size = strlen(values[0]) + 1;
00055    value.data = values[1];
00056    value.size = strlen(values[1]) + 1;
00057 
00058    if (bdb->put(bdb, &key, &value, 0)) {
00059       return SQLITE_ABORT;
00060    }
00061 
00062    bdb->sync(bdb, 0);
00063 
00064    return 0;
00065 }

static int convert_bdb_to_sqlite3 ( void   )  [static]

Definition at line 67 of file astdb2bdb.c.

References add_row_to_bdb(), NULL, and sql3db.

00068 {
00069    char *errmsg = NULL;
00070    if (sqlite3_exec(sql3db, "SELECT key,value FROM astdb", add_row_to_bdb, NULL, &errmsg) != SQLITE_OK) {
00071       fprintf(stderr, "Could not add row to Berkeley DB: %s\n", errmsg);
00072       return -1;
00073    }
00074 
00075    return 0;
00076 }

static int create_bdb_astdb ( void   )  [static]

Definition at line 89 of file astdb2bdb.c.

References AST_FILE_MODE, DB_BTREE, dbopen(), errno, and NULL.

Referenced by main().

00090 {
00091    if (!bdb && !(bdb = dbopen("astdb", O_CREAT | O_RDWR | O_TRUNC, AST_FILE_MODE, DB_BTREE, NULL))) {
00092       fprintf(stderr, "Unable to create astdb: %s\n", strerror(errno));
00093       return -1;
00094    }
00095    return 0;
00096 }

static int db_open_sqlite3 ( const char *  dbname  )  [static]

Definition at line 78 of file astdb2bdb.c.

References sql3db.

Referenced by main().

00079 {
00080    if (sqlite3_open(dbname, &sql3db) != SQLITE_OK) {
00081       fprintf(stderr, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(sql3db));
00082       sqlite3_close(sql3db);
00083       return -1;
00084    }
00085 
00086    return 0;
00087 }

int main ( int  argc,
char *  argv[] 
)

Definition at line 98 of file astdb2bdb.c.

References convert_bdb_to_sqlite3(), create_bdb_astdb(), db_open_sqlite3(), errno, and sql3db.

00099 {
00100    struct stat dont_care;
00101 
00102    if (argc != 2) {
00103       fprintf(stderr, "%s takes the path of SQLite3 astdb as its only argument\n", basename(argv[0]));
00104       fprintf(stderr, "and will produce a file 'astdb' in the current directory\n"
00105             "Make a backup of any existing Berkeley DB astdb you have and copy\n"
00106             "the new astdb to its location: often /var/lib/asterisk/astdb\n");
00107       exit(-1);
00108    }
00109 
00110    if (stat(argv[1], &dont_care)) {
00111       fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
00112       exit(-1);
00113    }
00114 
00115    if (db_open_sqlite3(argv[1])) {
00116       exit(-1);
00117    }
00118 
00119    if (create_bdb_astdb()) {
00120       exit(-1);
00121    }
00122 
00123    if (convert_bdb_to_sqlite3()) {
00124       fprintf(stderr, "Database conversion failed!\n");
00125       exit(-1);
00126       sqlite3_close(sql3db);
00127    }
00128 
00129    printf("Created ./astdb. Back up any existing astdb and copy the created\n");
00130    printf("astdb file to the original's location. Often /var/lib/asterisk/astdb.\n");
00131 
00132    sqlite3_close(sql3db);
00133    return 0;
00134 }


Variable Documentation

DB* bdb [static]

Definition at line 42 of file astdb2bdb.c.

Referenced by convert_bdb_to_sqlite3().

sqlite3* sql3db [static]

Definition at line 41 of file astdb2bdb.c.

Referenced by convert_bdb_to_sqlite3(), db_open_sqlite3(), and main().


Generated on Thu Apr 16 06:28:56 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6