| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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> |
| 38 |
|
| 39 |
#include "db1-ast/include/db.h" |
| 40 |
|
| 41 |
#define MAX_DB_FIELD 256 |
| 42 |
#define MIN(a,b) \ |
| 43 |
({ typeof (a) _a = (a); \ |
| 44 |
typeof (b) _b = (b); \ |
| 45 |
a < _b ? _a : _b; }) |
| 46 |
|
| 47 |
static sqlite3 *astdb; |
| 48 |
|
| 49 |
#define DEFINE_SQL_STATEMENT(stmt,sql) static sqlite3_stmt *stmt; \ |
| 50 |
const char stmt##_sql[] = sql; |
| 51 |
|
| 52 |
DEFINE_SQL_STATEMENT(put_stmt, "INSERT OR REPLACE INTO astdb (key, value) VALUES (?, ?)") |
| 53 |
DEFINE_SQL_STATEMENT(create_astdb_stmt, "CREATE TABLE IF NOT EXISTS astdb(key VARCHAR(256), value VARCHAR(256), PRIMARY KEY(key))") |
| 54 |
|
| 55 |
static int db_execute_transaction_sql(const char *sql) |
| 56 |
{ |
| 57 |
char *errmsg = NULL; |
| 58 |
int res =0; |
| 59 |
|
| 60 |
sqlite3_exec(astdb, sql, NULL, NULL, &errmsg); |
| 61 |
if (errmsg) { |
| 62 |
fprintf(stderr, "Error executing SQL: %s\n", errmsg); |
| 63 |
sqlite3_free(errmsg); |
| 64 |
res = -1; |
| 65 |
} |
| 66 |
|
| 67 |
return res; |
| 68 |
} |
| 69 |
|
| 70 |
static int ast_db_begin_transaction(void) |
| 71 |
{ |
| 72 |
return db_execute_transaction_sql("BEGIN TRANSACTION"); |
| 73 |
} |
| 74 |
|
| 75 |
static int ast_db_commit_transaction(void) |
| 76 |
{ |
| 77 |
return db_execute_transaction_sql("COMMIT"); |
| 78 |
} |
| 79 |
|
| 80 |
static int ast_db_rollback_transaction(void) |
| 81 |
{ |
| 82 |
return db_execute_transaction_sql("ROLLBACK"); |
| 83 |
} |
| 84 |
|
| 85 |
static int db_put_raw(const char *key, size_t keylen, const char *value, size_t valuelen) |
| 86 |
{ |
| 87 |
int res = 0; |
| 88 |
|
| 89 |
if (sqlite3_bind_text(put_stmt, 1, key, keylen, SQLITE_STATIC) != SQLITE_OK) { |
| 90 |
fprintf(stderr, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb)); |
| 91 |
res = -1; |
| 92 |
} else if (sqlite3_bind_text(put_stmt, 2, value, valuelen, SQLITE_STATIC) != SQLITE_OK) { |
| 93 |
fprintf(stderr, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb)); |
| 94 |
res = -1; |
| 95 |
} else if (sqlite3_step(put_stmt) != SQLITE_DONE) { |
| 96 |
fprintf(stderr, "Couldn't execute statement: %s\n", sqlite3_errmsg(astdb)); |
| 97 |
res = -1; |
| 98 |
} |
| 99 |
sqlite3_reset(put_stmt); |
| 100 |
|
| 101 |
return res; |
| 102 |
} |
| 103 |
|
| 104 |
static int convert_bdb_to_sqlite3(const char *bdb_dbname) |
| 105 |
{ |
| 106 |
DB *bdb; |
| 107 |
DBT key = { 0, }, value = { 0, }, last_key = { 0, }; |
| 108 |
int res, last = 0; |
| 109 |
char last_key_s[MAX_DB_FIELD]; |
| 110 |
|
| 111 |
if (!(bdb = dbopen(bdb_dbname, O_RDONLY, AST_FILE_MODE, DB_BTREE, NULL))) { |
| 112 |
fprintf(stderr, "Unable to open Asterisk database '%s'\n", bdb_dbname); |
| 113 |
return -1; |
| 114 |
} |
| 115 |
|
| 116 |
if (bdb->seq(bdb, &last_key, &value, R_LAST)) { |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
memcpy(last_key_s, last_key.data, MIN(last_key.size - 1, sizeof(last_key_s))); |
| 122 |
last_key_s[last_key.size - 1] = '\0'; |
| 123 |
for (res = bdb->seq(bdb, &key, &value, R_FIRST); |
| 124 |
!res; res = bdb->seq(bdb, &key, &value, R_NEXT)) { |
| 125 |
last = !strcmp(key.data, last_key_s); |
| 126 |
db_put_raw((const char *) key.data, key.size - 1, (const char *) value.data, value.size - 1); |
| 127 |
if (last) { |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
bdb->close(bdb); |
| 133 |
|
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static int init_stmt(sqlite3_stmt **stmt, const char *sql, size_t len) |
| 138 |
{ |
| 139 |
if (sqlite3_prepare(astdb, sql, len, stmt, NULL) != SQLITE_OK) { |
| 140 |
fprintf(stderr, "Couldn't prepare statement '%s': %s\n", sql, sqlite3_errmsg(astdb)); |
| 141 |
return -1; |
| 142 |
} |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
static int db_create_astdb(void) |
| 148 |
{ |
| 149 |
if (init_stmt(&create_astdb_stmt, create_astdb_stmt_sql, sizeof(create_astdb_stmt_sql))) { |
| 150 |
return -1; |
| 151 |
} |
| 152 |
|
| 153 |
ast_db_begin_transaction(); |
| 154 |
if (sqlite3_step(create_astdb_stmt) != SQLITE_DONE) { |
| 155 |
fprintf(stderr, "Couldn't create astdb table: %s\n", sqlite3_errmsg(astdb)); |
| 156 |
ast_db_rollback_transaction(); |
| 157 |
sqlite3_reset(create_astdb_stmt); |
| 158 |
return -1; |
| 159 |
} |
| 160 |
|
| 161 |
ast_db_commit_transaction(); |
| 162 |
sqlite3_reset(create_astdb_stmt); |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
static int init_statements(void) |
| 168 |
{ |
| 169 |
|
| 170 |
|
| 171 |
return init_stmt(&put_stmt, put_stmt_sql, sizeof(put_stmt_sql)); |
| 172 |
} |
| 173 |
|
| 174 |
static int db_open(const char *dbname) |
| 175 |
{ |
| 176 |
if (sqlite3_open(dbname, &astdb) != SQLITE_OK) { |
| 177 |
fprintf(stderr, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(astdb)); |
| 178 |
sqlite3_close(astdb); |
| 179 |
return -1; |
| 180 |
} |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
static int sql_db_init(const char *dbname) |
| 186 |
{ |
| 187 |
if (db_open(dbname) || db_create_astdb() || init_statements()) { |
| 188 |
return -1; |
| 189 |
} |
| 190 |
|
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
int main(int argc, char *argv[]) |
| 195 |
{ |
| 196 |
char *dbname; |
| 197 |
struct stat dont_care; |
| 198 |
|
| 199 |
if (argc != 2) { |
| 200 |
fprintf(stderr, "%s takes the path of astdb as its only argument\n", basename(argv[0])); |
| 201 |
exit(-1); |
| 202 |
} |
| 203 |
|
| 204 |
if (stat(argv[1], &dont_care)) { |
| 205 |
fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno)); |
| 206 |
exit(-1); |
| 207 |
} |
| 208 |
|
| 209 |
if (!(dbname = alloca(strlen(argv[1]) + sizeof(".sqlite3")))) { |
| 210 |
exit(-1); |
| 211 |
} |
| 212 |
|
| 213 |
strcpy(dbname, argv[1]); |
| 214 |
strcat(dbname, ".sqlite3"); |
| 215 |
|
| 216 |
if (!stat(dbname, &dont_care)) { |
| 217 |
fprintf(stderr, "%s already exists!\n", dbname); |
| 218 |
exit(-1); |
| 219 |
} |
| 220 |
|
| 221 |
if (sql_db_init(dbname)) { |
| 222 |
exit(-1); |
| 223 |
} |
| 224 |
|
| 225 |
if (convert_bdb_to_sqlite3(argv[1])) { |
| 226 |
fprintf(stderr, "Database conversion failed!\n"); |
| 227 |
exit(-1); |
| 228 |
sqlite3_close(astdb); |
| 229 |
} |
| 230 |
|
| 231 |
sqlite3_close(astdb); |
| 232 |
return 0; |
| 233 |
} |