Asterisk - The Open Source Telephony Project  18.5.0
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[email protected][email protected][email protected]igium[email protected].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.

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

Referenced by convert_bdb_to_sqlite3().

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

◆ convert_bdb_to_sqlite3()

static int convert_bdb_to_sqlite3 ( void  )
static

Definition at line 67 of file astdb2bdb.c.

References add_row_to_bdb(), NULL, and sql3db.

Referenced by main().

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

◆ create_bdb_astdb()

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().

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 NULL
Definition: resample.c:96
#define AST_FILE_MODE
Definition: asterisk.h:32
Definition: db.h:103
int errno
static DB * bdb
Definition: astdb2bdb.c:42
DB * dbopen(char *fname, int flags, int mode, DBTYPE type, const void *openinfo) const

◆ db_open_sqlite3()

static int db_open_sqlite3 ( const char *  dbname)
static

Definition at line 78 of file astdb2bdb.c.

References sql3db.

Referenced by main().

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 sqlite3 * sql3db
Definition: astdb2bdb.c:41
static struct ast_str * dbname
Definition: cdr_mysql.c:77

◆ main()

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.

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 
123  if (convert_bdb_to_sqlite3()) {
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 sqlite3 * sql3db
Definition: astdb2bdb.c:41
static int create_bdb_astdb(void)
Definition: astdb2bdb.c:89
int errno
static int convert_bdb_to_sqlite3(void)
Definition: astdb2bdb.c:67

Variable Documentation

◆ bdb

DB* bdb
static

Definition at line 42 of file astdb2bdb.c.

Referenced by convert_bdb_to_sqlite3().

◆ sql3db

sqlite3* sql3db
static

Definition at line 41 of file astdb2bdb.c.

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