make it optional where the hlr database is stored
Add --database to define where the database is stored. The default was changed to not store the file in /tmp anymore.
This commit is contained in:
parent
aa0fb362c0
commit
bde361064a
|
@ -24,7 +24,7 @@
|
|||
|
||||
#include <openbsc/gsm_subscriber.h>
|
||||
|
||||
int db_init();
|
||||
int db_init(const char *name);
|
||||
int db_prepare();
|
||||
int db_fini();
|
||||
|
||||
|
|
|
@ -46,6 +46,7 @@ static struct gsm_network *gsmnet;
|
|||
/* MCC and MNC for the Location Area Identifier */
|
||||
static int MCC = 1;
|
||||
static int MNC = 1;
|
||||
static const char *database_name = "hlr.sqlite3";
|
||||
|
||||
|
||||
/* The following definitions are for OM and NM packets that we cannot yet
|
||||
|
@ -653,6 +654,7 @@ static void print_help()
|
|||
printf(" -s --disable-color\n");
|
||||
printf(" -n --network-code number(MNC) \n");
|
||||
printf(" -c --country-code number (MCC) \n");
|
||||
printf(" -l --database db-name The database to use\n");
|
||||
printf(" -h --help this text\n");
|
||||
}
|
||||
|
||||
|
@ -666,6 +668,7 @@ static void handle_options(int argc, char** argv)
|
|||
{"disable-color", 0, 0, 's'},
|
||||
{"network-code", 1, 0, 'n'},
|
||||
{"country-code", 1, 0, 'c'},
|
||||
{"database", 1, 0, 'l'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
|
@ -691,6 +694,9 @@ static void handle_options(int argc, char** argv)
|
|||
case 'c':
|
||||
MCC = atoi(optarg);
|
||||
break;
|
||||
case 'l':
|
||||
database_name = strdup(optarg);
|
||||
break;
|
||||
default:
|
||||
/* ignore */
|
||||
break;
|
||||
|
@ -771,7 +777,7 @@ int main(int argc, char **argv)
|
|||
/* parse options */
|
||||
handle_options(argc, argv);
|
||||
|
||||
if (db_init()) {
|
||||
if (db_init(database_name)) {
|
||||
printf("DB: Failed to init database. Please check the option settings.\n");
|
||||
return 1;
|
||||
}
|
||||
|
|
19
src/db.c
19
src/db.c
|
@ -19,11 +19,14 @@
|
|||
|
||||
#include <openbsc/db.h>
|
||||
|
||||
#include <libgen.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dbi/dbi.h>
|
||||
|
||||
static char *db_basename = NULL;
|
||||
static char *db_dirname = NULL;
|
||||
dbi_conn conn;
|
||||
|
||||
void db__error_func(dbi_conn conn, void* data) {
|
||||
|
@ -32,7 +35,7 @@ void db__error_func(dbi_conn conn, void* data) {
|
|||
printf("DBI: %s\n", msg);
|
||||
}
|
||||
|
||||
int db_init() {
|
||||
int db_init(const char *name) {
|
||||
dbi_initialize(NULL);
|
||||
conn = dbi_conn_new("sqlite3");
|
||||
if (conn==NULL) {
|
||||
|
@ -51,10 +54,15 @@ int db_init() {
|
|||
*/
|
||||
|
||||
/* SqLite 3 */
|
||||
dbi_conn_set_option(conn, "sqlite3_dbdir", "/tmp");
|
||||
dbi_conn_set_option(conn, "dbname", "hlr.sqlite3");
|
||||
char *db_basename = strdup(name);
|
||||
char *db_dirname = strdup(name);
|
||||
dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
|
||||
dbi_conn_set_option(conn, "dbname", basename(db_basename));
|
||||
|
||||
if (dbi_conn_connect(conn) < 0) {
|
||||
free(db_dirname);
|
||||
free(db_basename);
|
||||
db_dirname = db_basename = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -138,6 +146,11 @@ int db_prepare() {
|
|||
int db_fini() {
|
||||
dbi_conn_close(conn);
|
||||
dbi_shutdown();
|
||||
|
||||
if (db_dirname)
|
||||
free(db_dirname);
|
||||
if (db_basename)
|
||||
free(db_basename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
|
||||
int main() {
|
||||
|
||||
if (db_init()) {
|
||||
if (db_init("hlr.sqlite3")) {
|
||||
printf("DB: Failed to init database. Please check the option settings.\n");
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue