astdb2bdb.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011, Digium, Inc.
  5. *
  6. * Mark Spencer <twilson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief SQLite 3 astdb to Berkeley DB converter
  21. *
  22. * \author Terry Wilson <twilson@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <sqlite3.h>
  33. #include <libgen.h> /* OS X doesn't have the basename function in strings.h */
  34. #include "db1-ast/include/db.h"
  35. static sqlite3 *sql3db;
  36. static DB *bdb;
  37. static int add_row_to_bdb(void *arg, int columns, char **values, char **column_names)
  38. {
  39. DBT key = { 0, }, value = { 0, };
  40. if (columns != 2 || strcmp(column_names[0], "key") || strcmp(column_names[1], "value")) {
  41. fprintf(stderr, "Unknown row type\n");
  42. return SQLITE_ABORT;
  43. }
  44. key.data = values[0];
  45. key.size = strlen(values[0]) + 1;
  46. value.data = values[1];
  47. value.size = strlen(values[1]) + 1;
  48. if (bdb->put(bdb, &key, &value, 0)) {
  49. return SQLITE_ABORT;
  50. }
  51. bdb->sync(bdb, 0);
  52. return 0;
  53. }
  54. static int convert_bdb_to_sqlite3(void)
  55. {
  56. char *errmsg = NULL;
  57. if (sqlite3_exec(sql3db, "SELECT key,value FROM astdb", add_row_to_bdb, NULL, &errmsg) != SQLITE_OK) {
  58. fprintf(stderr, "Could not add row to Berkeley DB: %s\n", errmsg);
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static int db_open_sqlite3(const char *dbname)
  64. {
  65. if (sqlite3_open(dbname, &sql3db) != SQLITE_OK) {
  66. fprintf(stderr, "Unable to open Asterisk database '%s': %s\n", dbname, sqlite3_errmsg(sql3db));
  67. sqlite3_close(sql3db);
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. static int create_bdb_astdb(void)
  73. {
  74. if (!bdb && !(bdb = dbopen("astdb", O_CREAT | O_RDWR | O_TRUNC, AST_FILE_MODE, DB_BTREE, NULL))) {
  75. fprintf(stderr, "Unable to create astdb: %s\n", strerror(errno));
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. int main(int argc, char *argv[])
  81. {
  82. struct stat dont_care;
  83. if (argc != 2) {
  84. fprintf(stderr, "%s takes the path of SQLite3 astdb as its only argument\n", basename(argv[0]));
  85. fprintf(stderr, "and will produce a file 'astdb' in the current directory\n"
  86. "Make a backup of any existing Berkeley DB astdb you have and copy\n"
  87. "the new astdb to its location: often /var/lib/asterisk/astdb\n");
  88. exit(-1);
  89. }
  90. if (stat(argv[1], &dont_care)) {
  91. fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
  92. exit(-1);
  93. }
  94. if (db_open_sqlite3(argv[1])) {
  95. exit(-1);
  96. }
  97. if (create_bdb_astdb()) {
  98. exit(-1);
  99. }
  100. if (convert_bdb_to_sqlite3()) {
  101. fprintf(stderr, "Database conversion failed!\n");
  102. exit(-1);
  103. sqlite3_close(sql3db);
  104. }
  105. printf("Created ./astdb. Back up any existing astdb and copy the created\n");
  106. printf("astdb file to the original's location. Often /var/lib/asterisk/astdb.\n");
  107. sqlite3_close(sql3db);
  108. return 0;
  109. }