]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.c
Ensure that bvfs SQL link is not shared
[bacula/bacula] / bacula / src / cats / cats.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2011-2011 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24
25    The licensor of Bacula is the Free Software Foundation Europe
26    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
27    Switzerland, email:ftf@fsfeurope.org.
28 */
29 /*
30  * Generic catalog class methods.
31  *
32  * Written by Marco van Wieringen, January 2011
33  */
34
35 #include "bacula.h"
36
37 #if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
38
39 #include "cats.h"
40 #include "bdb_priv.h"
41 #include "sql_glue.h"
42
43 bool B_DB::db_match_database(const char *db_driver, const char *db_name,
44                              const char *db_address, int db_port)
45 {
46    bool match;
47
48    if (db_driver) {
49       match = strcasecmp(m_db_driver, db_driver) == 0 &&
50               bstrcmp(m_db_name, db_name) &&
51               bstrcmp(m_db_address, db_address) &&
52               m_db_port == db_port &&
53               m_dedicated == false;
54    } else {
55       match = bstrcmp(m_db_name, db_name) &&
56               bstrcmp(m_db_address, db_address) &&
57               m_db_port == db_port &&
58               m_dedicated == false;
59    }
60    return match;
61 }
62
63 B_DB *B_DB::db_clone_database_connection(JCR *jcr, bool mult_db_connections)
64 {
65    /*
66     * See if its a simple clone e.g. with mult_db_connections set to false
67     * then we just return the calling class pointer.
68     */
69    if (!mult_db_connections) {
70       m_ref_count++;
71       return this;
72    }
73
74    /*
75     * A bit more to do here just open a new session to the database.
76     */
77    return db_init_database(jcr, m_db_driver, m_db_name, m_db_user, m_db_password,
78                            m_db_address, m_db_port, m_db_socket, true, m_disabled_batch_insert);
79 }
80
81 const char *B_DB::db_get_type(void)
82 {
83    switch (m_db_interface_type) {
84    case SQL_INTERFACE_TYPE_MYSQL:
85       return "MySQL";
86    case SQL_INTERFACE_TYPE_POSTGRESQL:
87       return "PostgreSQL";
88    case SQL_INTERFACE_TYPE_SQLITE3:
89       return "SQLite3";
90    case SQL_INTERFACE_TYPE_INGRES:
91       return "Ingres";
92    case SQL_INTERFACE_TYPE_DBI:
93       switch (m_db_type) {
94       case SQL_TYPE_MYSQL:
95          return "DBI:MySQL";
96       case SQL_TYPE_POSTGRESQL:
97          return "DBI:PostgreSQL";
98       case SQL_TYPE_SQLITE3:
99          return "DBI:SQLite3";
100       case SQL_TYPE_INGRES:
101          return "DBI:Ingres";
102       default:
103          return "DBI:Unknown";
104       }
105    default:
106       return "Unknown";
107    }
108 }
109
110 /*
111  * Lock database, this can be called multiple times by the same
112  * thread without blocking, but must be unlocked the number of
113  * times it was locked using db_unlock().
114  */
115 void B_DB::_db_lock(const char *file, int line)
116 {
117    int errstat;
118
119    if ((errstat = rwl_writelock_p(&m_lock, file, line)) != 0) {
120       berrno be;
121       e_msg(file, line, M_FATAL, 0, "rwl_writelock failure. stat=%d: ERR=%s\n",
122             errstat, be.bstrerror(errstat));
123    }
124 }
125
126 /*
127  * Unlock the database. This can be called multiple times by the
128  * same thread up to the number of times that thread called
129  * db_lock()/
130  */
131 void B_DB::_db_unlock(const char *file, int line)
132 {
133    int errstat;
134
135    if ((errstat = rwl_writeunlock(&m_lock)) != 0) {
136       berrno be;
137       e_msg(file, line, M_FATAL, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n",
138             errstat, be.bstrerror(errstat));
139    }
140 }
141
142 bool B_DB::db_sql_query(const char *query, int flags)
143 {
144    bool retval;
145
146    db_lock(this);
147    retval = ((B_DB_PRIV *)this)->sql_query(query, flags);
148    if (!retval) {
149       Mmsg(errmsg, _("Query failed: %s: ERR=%s\n"), query, ((B_DB_PRIV *)this)->sql_strerror());
150    }
151    db_unlock(this);
152    return retval;
153 }
154
155 void B_DB::print_lock_info(FILE *fp)
156 {
157    if (m_lock.valid == RWLOCK_VALID) {
158       fprintf(fp, "\tRWLOCK=%p w_active=%i w_wait=%i\n", &m_lock, m_lock.w_active, m_lock.w_wait);
159    }
160 }
161
162 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI */