Skip to content

Instantly share code, notes, and snippets.

@seanbright
Created March 28, 2023 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanbright/f8331df57aacdd06b6d391dfec2f59c6 to your computer and use it in GitHub Desktop.
Save seanbright/f8331df57aacdd06b6d391dfec2f59c6 to your computer and use it in GitHub Desktop.
0001-cdr_sqlite3_custom-Make-database-file-path-configura.patch
From ec76a68ff3eb8acf308608a907b23e6f5b593fb8 Mon Sep 17 00:00:00 2001
From: Sean Bright <sean@seanbright.com>
Date: Tue, 28 Mar 2023 08:58:51 -0400
Subject: [PATCH] cdr_sqlite3_custom: Make database file path configurable.
Change-Id: I85263ad6b2bd96cd0ee55b1940fe976e05d81b84
---
cdr/cdr_sqlite3_custom.c | 18 +++++++++++++++---
configs/samples/cdr_sqlite3_custom.conf.sample | 3 ++-
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/cdr/cdr_sqlite3_custom.c b/cdr/cdr_sqlite3_custom.c
index 9f71c8481c..9e72bec7b0 100644
--- a/cdr/cdr_sqlite3_custom.c
+++ b/cdr/cdr_sqlite3_custom.c
@@ -59,6 +59,7 @@ static const char desc[] = "Customizable SQLite3 CDR Backend";
static const char name[] = "cdr_sqlite3_custom";
static sqlite3 *db = NULL;
+static char filename[1024];
static char table[80];
static char *columns;
static int busy_timeout;
@@ -174,6 +175,18 @@ static int load_config(int reload)
return -1;
}
+ tmp = ast_variable_retrieve(cfg, "master", "filename");
+ if (ast_strlen_zero(tmp)) {
+ tmp = "master.db";
+ }
+
+ /* Allow absolute paths as well as log-dir relative ones */
+ if (*tmp == '/') {
+ ast_copy_string(filename, tmp, sizeof(filename));
+ } else {
+ snprintf(filename, sizeof(filename), "%s/%s", ast_config_AST_LOG_DIR, tmp);
+ }
+
/* Mapping must have a table name */
if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "master", "table"))) {
ast_copy_string(table, tmp, sizeof(table));
@@ -299,7 +312,6 @@ static int unload_module(void)
static int load_module(void)
{
char *error;
- char filename[PATH_MAX];
int res;
char *sql;
@@ -308,10 +320,10 @@ static int load_module(void)
}
/* is the database there? */
- snprintf(filename, sizeof(filename), "%s/master.db", ast_config_AST_LOG_DIR);
res = sqlite3_open(filename, &db);
if (res != SQLITE_OK) {
- ast_log(LOG_ERROR, "Could not open database %s.\n", filename);
+ ast_log(LOG_ERROR, "Could not open database %s: %s.\n",
+ filename, sqlite3_errstr(res));
free_config(0);
return AST_MODULE_LOAD_DECLINE;
}
diff --git a/configs/samples/cdr_sqlite3_custom.conf.sample b/configs/samples/cdr_sqlite3_custom.conf.sample
index 4b88d58d48..72bfb5cf2f 100644
--- a/configs/samples/cdr_sqlite3_custom.conf.sample
+++ b/configs/samples/cdr_sqlite3_custom.conf.sample
@@ -1,7 +1,8 @@
;
; Mappings for custom config file
;
-[master] ; currently, only file "master.db" is supported, with only one table at a time.
+[master] ; currently, only one database file is supported, with only one table at a time.
+;filename = master.db
;table => cdr
;columns => calldate, clid, dcontext, channel, dstchannel, lastapp, lastdata, duration, billsec, disposition, amaflags, accountcode, uniqueid, userfield, test
;values => '${CDR(start)}','${CDR(clid)}','${CDR(dcontext)}','${CDR(channel)}','${CDR(dstchannel)}','${CDR(lastapp)}','${CDR(lastdata)}','${CDR(duration)}','${CDR(billsec)}','${CDR(disposition)}','${CDR(amaflags)}','${CDR(accountcode)}','${CDR(uniqueid)}','${CDR(userfield)}','${CDR(test)}'
--
2.34.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment