Asterisk - The Open Source Telephony Project GIT-master-f36a736
astdb2bdb.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2011, Digium, Inc.
5 *
6 * Mark Spencer <twilson@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief SQLite 3 astdb to Berkeley DB converter
22 *
23 * \author Terry Wilson <twilson@digium.com>
24 */
25
26/*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30#include "asterisk.h"
31
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <sqlite3.h>
37#include <libgen.h> /* OS X doesn't have the basename function in strings.h */
38
39#include "db1-ast/include/db.h"
40
41static sqlite3 *sql3db;
42static DB *bdb;
43
44static int add_row_to_bdb(void *arg, int columns, char **values, char **column_names)
45{
46 DBT key = { 0, }, value = { 0, };
47
48 if (columns != 2 || strcmp(column_names[0], "key") || strcmp(column_names[1], "value")) {
49 fprintf(stderr, "Unknown row type\n");
50 return SQLITE_ABORT;
51 }
52
53 key.data = values[0];
54 key.size = strlen(values[0]) + 1;
55 value.data = values[1];
56 value.size = strlen(values[1]) + 1;
57
58 if (bdb->put(bdb, &key, &value, 0)) {
59 return SQLITE_ABORT;
60 }
61
62 bdb->sync(bdb, 0);
63
64 return 0;
65}
66
67static int convert_bdb_to_sqlite3(void)
68{
69 char *errmsg = NULL;
70 if (sqlite3_exec(sql3db, "SELECT key,value FROM astdb", add_row_to_bdb, NULL, &errmsg) != SQLITE_OK) {
71 fprintf(stderr, "Could not add row to Berkeley DB: %s\n", errmsg);
72 return -1;
73 }
74
75 return 0;
76}
77
78static int db_open_sqlite3(const char *dbname)
79{
80 if (sqlite3_open(dbname, &sql3db) != SQLITE_OK) {
81 fprintf(stderr, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(sql3db));
82 sqlite3_close(sql3db);
83 return -1;
84 }
85
86 return 0;
87}
88
89static int create_bdb_astdb(void)
90{
91 if (!bdb && !(bdb = dbopen("astdb", O_CREAT | O_RDWR | O_TRUNC, AST_FILE_MODE, DB_BTREE, NULL))) {
92 fprintf(stderr, "Unable to create astdb: %s\n", strerror(errno));
93 return -1;
94 }
95 return 0;
96}
97
98int main(int argc, char *argv[])
99{
100 struct stat dont_care;
101
102 if (argc != 2) {
103 fprintf(stderr, "%s takes the path of SQLite3 astdb as its only argument\n", basename(argv[0]));
104 fprintf(stderr, "and will produce a file 'astdb' in the current directory\n"
105 "Make a backup of any existing Berkeley DB astdb you have and copy\n"
106 "the new astdb to its location: often /var/lib/asterisk/astdb\n");
107 exit(-1);
108 }
109
110 if (stat(argv[1], &dont_care)) {
111 fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
112 exit(-1);
113 }
114
115 if (db_open_sqlite3(argv[1])) {
116 exit(-1);
117 }
118
119 if (create_bdb_astdb()) {
120 exit(-1);
121 }
122
124 fprintf(stderr, "Database conversion failed!\n");
125 exit(-1);
126 sqlite3_close(sql3db);
127 }
128
129 printf("Created ./astdb. Back up any existing astdb and copy the created\n");
130 printf("astdb file to the original's location. Often /var/lib/asterisk/astdb.\n");
131
132 sqlite3_close(sql3db);
133 return 0;
134}
static int db_open_sqlite3(const char *dbname)
Definition: astdb2bdb.c:78
int main(int argc, char *argv[])
Definition: astdb2bdb.c:98
static int add_row_to_bdb(void *arg, int columns, char **values, char **column_names)
Definition: astdb2bdb.c:44
static int convert_bdb_to_sqlite3(void)
Definition: astdb2bdb.c:67
static DB * bdb
Definition: astdb2bdb.c:42
static int create_bdb_astdb(void)
Definition: astdb2bdb.c:89
static sqlite3 * sql3db
Definition: astdb2bdb.c:41
Asterisk main include file. File version handling, generic pbx functions.
#define AST_FILE_MODE
Definition: asterisk.h:32
@ DB_BTREE
Definition: db.h:103
int errno
static char dbname[MAX_DB_OPTION_SIZE]
#define NULL
Definition: resample.c:96
Definition: db.h:85
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
Definition: db.h:129
int value
Definition: syslog.c:37
DB * dbopen(char *fname, int flags, int mode, DBTYPE type, const void *openinfo) const