astdb2bdb.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2011, Digium, Inc.
00005  *
00006  * Mark Spencer <twilson@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief SQLite 3 astdb to Berkeley DB converter
00022  *
00023  * \author Terry Wilson <twilson@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <fcntl.h>
00035 #include <unistd.h>
00036 #include <sqlite3.h>
00037 #include <libgen.h> /* OS X doesn't have the basename function in strings.h */
00038 
00039 #include "db1-ast/include/db.h"
00040 
00041 static sqlite3 *sql3db;
00042 static DB *bdb;
00043 
00044 static int add_row_to_bdb(void *arg, int columns, char **values, char **column_names)
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 }
00066 
00067 static int convert_bdb_to_sqlite3(void)
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 }
00077 
00078 static int db_open_sqlite3(const char *dbname)
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 }
00088 
00089 static int create_bdb_astdb(void)
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 }
00097 
00098 int main(int argc, char *argv[])
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 }

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