Asterisk - The Open Source Telephony Project GIT-master-7e7a603
Functions | Variables
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 twils.nosp@m.on@d.nosp@m.igium.nosp@m..com

Definition in file astdb2bdb.c.

Function Documentation

◆ add_row_to_bdb()

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

Definition at line 44 of file astdb2bdb.c.

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}
static DB * bdb
Definition: astdb2bdb.c:42
Definition: db.h:85
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
int value
Definition: syslog.c:37

References bdb, DBT::data, DBT::size, and value.

Referenced by convert_bdb_to_sqlite3().

◆ convert_bdb_to_sqlite3()

static int convert_bdb_to_sqlite3 ( void  )
static

Definition at line 67 of file astdb2bdb.c.

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}
static int add_row_to_bdb(void *arg, int columns, char **values, char **column_names)
Definition: astdb2bdb.c:44
static sqlite3 * sql3db
Definition: astdb2bdb.c:41
#define NULL
Definition: resample.c:96

References add_row_to_bdb(), NULL, and sql3db.

Referenced by main().

◆ create_bdb_astdb()

static int create_bdb_astdb ( void  )
static

Definition at line 89 of file astdb2bdb.c.

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}
#define AST_FILE_MODE
Definition: asterisk.h:32
@ DB_BTREE
Definition: db.h:103
int errno
DB * dbopen(char *fname, int flags, int mode, DBTYPE type, const void *openinfo) const

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

Referenced by main().

◆ db_open_sqlite3()

static int db_open_sqlite3 ( const char *  dbname)
static

Definition at line 78 of file astdb2bdb.c.

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}
static char dbname[MAX_DB_OPTION_SIZE]

References dbname, and sql3db.

Referenced by main().

◆ main()

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

Definition at line 98 of file astdb2bdb.c.

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
static int convert_bdb_to_sqlite3(void)
Definition: astdb2bdb.c:67
static int create_bdb_astdb(void)
Definition: astdb2bdb.c:89

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

Variable Documentation

◆ bdb

DB* bdb
static

Definition at line 42 of file astdb2bdb.c.

Referenced by add_row_to_bdb(), convert_bdb_to_sqlite3(), and create_bdb_astdb().

◆ sql3db

sqlite3* sql3db
static

Definition at line 41 of file astdb2bdb.c.

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