From 6708ee76a0a0c42530fa89a528e0576c391f4390 Mon Sep 17 00:00:00 2001
From: Terry Wilson <twilson@digium.com>
Date: Mon, 10 Oct 2011 22:54:03 +0000
Subject: [PATCH] Merged revisions 340219-340220 via svnmerge from
 https://origsvn.digium.com/svn/asterisk/branches/10

........
  r340219 | twilson | 2011-10-10 15:38:06 -0700 (Mon, 10 Oct 2011) | 8 lines

  Add astdb conversion utility for Berkeley to SQLite 3

  If someone wants to backtrack from Asterisk 1.8 to 10 they can use the
  astdb2bdb utility to convert the database back to the Berkeley format
  that Asterisk 1.8 uses.

  Review: https://reviewboard.asterisk.org/r/1502/
........
  r340220 | twilson | 2011-10-10 15:39:41 -0700 (Mon, 10 Oct 2011) | 2 lines

  Add a missing file for the astdb2bdb conversion utility
........


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@340221 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 UPGRADE.txt       |   4 +-
 utils/Makefile    |   6 ++-
 utils/astdb2bdb.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++
 utils/utils.xml   |   3 ++
 4 files changed, 141 insertions(+), 2 deletions(-)
 create mode 100644 utils/astdb2bdb.c

diff --git a/UPGRADE.txt b/UPGRADE.txt
index c0ee9ca91f..05df0a1c40 100644
--- a/UPGRADE.txt
+++ b/UPGRADE.txt
@@ -87,7 +87,9 @@ Asterisk Database:
    SQLite 3. An existing Berkeley astdb file can be converted with the astdb2sqlite3
    utility in the UTILS section of menuselect. If an existing astdb is found and no
    astdb.sqlite3 exists, astdb2sqlite3 will be compiled automatically. Asterisk will
-   convert an existing astdb to the SQLite3 version automatically at runtime.
+   convert an existing astdb to the SQLite3 version automatically at runtime. If
+   moving back from Asterisk 10 to Asterisk 1.8, the astdb2bdb utility can be used
+   to create a Berkeley DB copy of the SQLite3 astdb that Asterisk 10 uses.
 
 Manager:
  - The AMI protocol version was incremented to 1.2 as a result of changing two
diff --git a/utils/Makefile b/utils/Makefile
index 1abf3603a9..718b9ba0c0 100644
--- a/utils/Makefile
+++ b/utils/Makefile
@@ -214,7 +214,11 @@ db1-ast/libdb1.a: CHECK_SUBDIR
 
 astdb2sqlite3: LIBS+=$(SQLITE3_LIB)
 astdb2sqlite3: _ASTCFLAGS+=$(SQLITE3_INCLUDE)
-astdb2sqlite3: db1-ast/libdb1.a 
+astdb2sqlite3: db1-ast/libdb1.a
+
+astdb2bdb: LIBS+=$(SQLITE3_LIB)
+astdb2bdb: _ASTCFLAGS+=$(SQLITE3_INCLUDE)
+astdb2bdb: db1-ast/libdb1.a
 
 ifneq ($(wildcard .*.d),)
    include .*.d
diff --git a/utils/astdb2bdb.c b/utils/astdb2bdb.c
new file mode 100644
index 0000000000..51f96ed299
--- /dev/null
+++ b/utils/astdb2bdb.c
@@ -0,0 +1,130 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2011, Digium, Inc.
+ *
+ * Mark Spencer <twilson@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief SQLite 3 astdb to Berkely DB converter
+ *
+ * \author Terry Wilson <twilson@digium.com>
+ */
+
+#include "asterisk.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sqlite3.h>
+#include <libgen.h> /* OS X doesn't have the basename function in strings.h */
+
+#include "db1-ast/include/db.h"
+
+static sqlite3 *sql3db;
+static DB *bdb;
+
+static int add_row_to_bdb(void *arg, int columns, char **values, char **column_names)
+{
+	DBT key = { 0, }, value = { 0, };
+
+	if (columns != 2 || strcmp(column_names[0], "key") || strcmp(column_names[1], "value")) {
+		fprintf(stderr, "Unknown row type\n");
+		return SQLITE_ABORT;
+	}
+
+	key.data = values[0];
+	key.size = strlen(values[0]) + 1;
+	value.data = values[1];
+	value.size = strlen(values[1]) + 1;
+
+	if (bdb->put(bdb, &key, &value, 0)) {
+		return SQLITE_ABORT;
+	}
+
+	bdb->sync(bdb, 0);
+
+	return 0;
+}
+
+static int convert_bdb_to_sqlite3(void)
+{
+	char *errmsg = NULL;
+	if (sqlite3_exec(sql3db, "SELECT key,value FROM astdb", add_row_to_bdb, NULL, &errmsg) != SQLITE_OK) {
+		fprintf(stderr, "Could not add row to Berkeley DB: %s\n", errmsg);
+		return -1;
+	}
+
+	return 0;
+}
+
+static int db_open_sqlite3(const char *dbname)
+{
+	if (sqlite3_open(dbname, &sql3db) != SQLITE_OK) {
+		fprintf(stderr, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(sql3db));
+		sqlite3_close(sql3db);
+		return -1;
+	}
+
+	return 0;
+}
+
+static int create_bdb_astdb(void)
+{
+	if (!bdb && !(bdb = dbopen("astdb", O_CREAT | O_RDWR | O_TRUNC, AST_FILE_MODE, DB_BTREE, NULL))) {
+		fprintf(stderr, "Unable to create astdb: %s\n", strerror(errno));
+		return -1;
+	}
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	struct stat dont_care;
+
+	if (argc != 2) {
+		fprintf(stderr, "%s takes the path of SQLite3 astdb as its only argument\n", basename(argv[0]));
+		fprintf(stderr, "and will produce a file 'astdb' in the current directory\n"
+				"Make a backup of any existing Berkeley DB astdb you have and copy\n"
+				"the new astdb to its location: often /var/lib/asterisk/astdb\n");
+		exit(-1);
+	}
+
+	if (stat(argv[1], &dont_care)) {
+		fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
+		exit(-1);
+	}
+
+	if (db_open_sqlite3(argv[1])) {
+		exit(-1);
+	}
+
+	if (create_bdb_astdb()) {
+		exit(-1);
+	}
+
+	if (convert_bdb_to_sqlite3()) {
+		fprintf(stderr, "Database conversion failed!\n");
+		exit(-1);
+		sqlite3_close(sql3db);
+	}
+
+	printf("Created ./astdb. Back up any existing astdb and copy the created\n");
+	printf("astdb file to the original's location. Often /var/lib/asterisk/astdb.\n");
+
+	sqlite3_close(sql3db);
+	return 0;
+}
diff --git a/utils/utils.xml b/utils/utils.xml
index 207495f52e..733bd535e0 100644
--- a/utils/utils.xml
+++ b/utils/utils.xml
@@ -9,6 +9,9 @@
   <member name="astdb2sqlite3">
 	<defaultenabled>yes</defaultenabled>
   </member>
+  <member name="astdb2bdb">
+	<defaultenabled>yes</defaultenabled>
+  </member>
   <member name="astman">
 	<defaultenabled>no</defaultenabled>
 	<depend>newt</depend>
-- 
GitLab