X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;ds=sidebyside;f=bacula%2Fsrc%2Fcats%2Fsql.c;h=56b9ccaf0421b7395bf77d3ef953f4d22e671403;hb=85f8d20ff16a2bce53a64ccabaf23ecde3caf507;hp=933c96663a7219c4a9b0b162e2494a22f0bec0ed;hpb=e18da82dec310aa4144c6fa2bed2449590d7f38a;p=bacula%2Fbacula diff --git a/bacula/src/cats/sql.c b/bacula/src/cats/sql.c index 933c96663a..56b9ccaf04 100644 --- a/bacula/src/cats/sql.c +++ b/bacula/src/cats/sql.c @@ -1,34 +1,43 @@ +/* + Bacula® - The Network Backup Solution + + Copyright (C) 2000-2008 Free Software Foundation Europe e.V. + + The main author of Bacula is Kern Sibbald, with contributions from + many others, a complete list can be found in the file AUTHORS. + This program is Free Software; you can redistribute it and/or + modify it under the terms of version two of the GNU General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + + Bacula® is a registered trademark of John Walker. + The licensor of Bacula is the Free Software Foundation Europe + (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, + Switzerland, email:ftf@fsfeurope.org. +*/ /* * Bacula Catalog Database interface routines * * Almost generic set of SQL database interface routines * (with a little more work) + * SQL engine specific routines are in mysql.c, postgresql.c, + * sqlite.c, ... * * Kern Sibbald, March 2000 * * Version $Id$ */ -/* - Copyright (C) 2000-2005 Kern Sibbald - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of - the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public - License along with this program; if not, write to the Free - Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, - MA 02111-1307, USA. - - */ - /* The following is necessary so that we do not include * the dummy external definition of B_DB. */ @@ -37,14 +46,67 @@ #include "bacula.h" #include "cats.h" -#if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL +#if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL || HAVE_DBI uint32_t bacula_db_version = 0; +int db_type = -1; + /* Forward referenced subroutines */ void print_dashes(B_DB *mdb); void print_result(B_DB *mdb); +B_DB *db_init(JCR *jcr, const char *db_driver, const char *db_name, const char *db_user, + const char *db_password, const char *db_address, int db_port, + const char *db_socket, int mult_db_connections) +{ +#ifdef HAVE_DBI + char *p; + if (!db_driver) { + Jmsg0(jcr, M_ABORT, 0, _("Driver type not specified in Catalog resource.\n")); + } + if (strlen(db_driver) < 5 || db_driver[3] != ':' || strncasecmp(db_driver, "dbi", 3) != 0) { + Jmsg0(jcr, M_ABORT, 0, _("Invalid driver type, must be \"dbi:\"\n")); + } + p = (char *)(db_driver + 4); + if (strcasecmp(p, "mysql") == 0) { + db_type = SQL_TYPE_MYSQL; + } else if (strcasecmp(p, "postgresql") == 0) { + db_type = SQL_TYPE_POSTGRESQL; + } else if (strcasecmp(p, "sqlite") == 0) { + db_type = SQL_TYPE_SQLITE; + } else { + Jmsg1(jcr, M_ABORT, 0, _("Unknown database type: %s\n"), p); + } +#elif HAVE_MYSQL + db_type = SQL_TYPE_MYSQL; +#elif HAVE_POSTGRESQL + db_type = SQL_TYPE_POSTGRESQL; +#elif HAVE_SQLITE + db_type = SQL_TYPE_SQLITE; +#elif HAVE_SQLITE3 + db_type = SQL_TYPE_SQLITE; +#endif + + return db_init_database(jcr, db_name, db_user, db_password, db_address, + db_port, db_socket, mult_db_connections); +} + +dbid_list::dbid_list() +{ + memset(this, 0, sizeof(dbid_list)); + max_ids = 1000; + DBId = (DBId_t *)malloc(max_ids * sizeof(DBId_t)); + num_ids = num_seen = tot_ids = 0; + PurgedFiles = NULL; +} + +dbid_list::~dbid_list() +{ + free(DBId); +} + + /* * Called here to retrieve an integer from the database */ @@ -65,6 +127,21 @@ static int int_handler(void *ctx, int num_fields, char **row) return 0; } +/* + * Called here to retrieve a 32/64 bit integer from the database. + * The returned integer will be extended to 64 bit. + */ +int db_int64_handler(void *ctx, int num_fields, char **row) +{ + db_int64_ctx *lctx = (db_int64_ctx *)ctx; + + if (row[0]) { + lctx->value = str_to_int64(row[0]); + lctx->count++; + } + return 0; +} + /* NOTE!!! The following routines expect that the @@ -72,22 +149,22 @@ static int int_handler(void *ctx, int num_fields, char **row) */ /* Check that the tables correspond to the version we want */ -int check_tables_version(JCR *jcr, B_DB *mdb) +bool check_tables_version(JCR *jcr, B_DB *mdb) { const char *query = "SELECT VersionId FROM Version"; bacula_db_version = 0; if (!db_sql_query(mdb, query, int_handler, (void *)&bacula_db_version)) { - Mmsg(mdb->errmsg, "Database not created or server not running.\n"); Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg); + return false; } if (bacula_db_version != BDB_VERSION) { Mmsg(mdb->errmsg, "Version error for database \"%s\". Wanted %d, got %d\n", mdb->db_name, BDB_VERSION, bacula_db_version); Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg); - return 0; + return false; } - return 1; + return true; } /* Utility routine for queries. The database MUST be locked before calling here. */ @@ -95,6 +172,8 @@ int QueryDB(const char *file, int line, JCR *jcr, B_DB *mdb, char *cmd) { int status; + + sql_free_result(mdb); if ((status=sql_query(mdb, cmd)) != 0) { m_msg(file, line, &mdb->errmsg, _("query %s failed:\n%s\n"), cmd, sql_strerror(mdb)); j_msg(file, line, jcr, M_FATAL, 0, "%s", mdb->errmsg); @@ -162,8 +241,8 @@ UpdateDB(const char *file, int line, JCR *jcr, B_DB *mdb, char *cmd) mdb->num_rows = sql_affected_rows(mdb); if (mdb->num_rows < 1) { char ed1[30]; - m_msg(file, line, &mdb->errmsg, _("Update problem: affected_rows=%s\n"), - edit_uint64(mdb->num_rows, ed1)); + m_msg(file, line, &mdb->errmsg, _("Update failed: affected_rows=%s for %s\n"), + edit_uint64(mdb->num_rows, ed1), cmd); if (verbose) { // j_msg(file, line, jcr, M_INFO, 0, "%s\n", cmd); } @@ -241,7 +320,7 @@ void _db_lock(const char *file, int line, B_DB *mdb) if ((errstat=rwl_writelock(&mdb->lock)) != 0) { berrno be; e_msg(file, line, M_FATAL, 0, "rwl_writelock failure. stat=%d: ERR=%s\n", - errstat, be.strerror(errstat)); + errstat, be.bstrerror(errstat)); } } @@ -256,7 +335,7 @@ void _db_unlock(const char *file, int line, B_DB *mdb) if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) { berrno be; e_msg(file, line, M_FATAL, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n", - errstat, be.strerror(errstat)); + errstat, be.bstrerror(errstat)); } } @@ -376,11 +455,11 @@ void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname) * must be a path name (e.g. c:). */ for (p=f=fname; *p; p++) { - if (*p == '/') { + if (IsPathSeparator(*p)) { f = p; /* set pos of last slash */ } } - if (*f == '/') { /* did we find a slash? */ + if (IsPathSeparator(*f)) { /* did we find a slash? */ f++; /* yes, point to filename */ } else { /* no, whole thing must be path name */ f = p;