]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.c
ccdedd48bcb9a63302010f62ef648da47f16fe5a
[bacula/bacula] / bacula / src / cats / cats.c
1 /* 
2    Bacula(R) - The Network Backup Solution
3  
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
6  
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9  
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14  
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */ 
20 /* 
21  * Generic catalog class methods. 
22  * 
23  * Note: at one point, this file was assembled from parts of other files 
24  *  by a programmer, and other than "wrapping" in a class, which is a trivial  
25  *  change for a C++ programmer, nothing substantial was done, yet all the  
26  *  code was recommitted under this programmer's name.  Consequently, we  
27  *  undo those changes here.  
28  */ 
29  
30 #include "bacula.h" 
31  
32 #if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL 
33  
34 #include "cats.h" 
35  
36 bool BDB::bdb_match_database(const char *db_driver, const char *db_name, 
37                              const char *db_address, int db_port) 
38
39    BDB *mdb = this; 
40    bool match; 
41  
42    if (db_driver) { 
43       match = strcasecmp(mdb->m_db_driver, db_driver) == 0 && 
44               bstrcmp(mdb->m_db_name, db_name) && 
45               bstrcmp(mdb->m_db_address, db_address) && 
46               mdb->m_db_port == db_port && 
47               mdb->m_dedicated == false; 
48    } else { 
49       match = bstrcmp(mdb->m_db_name, db_name) && 
50               bstrcmp(mdb->m_db_address, db_address) && 
51               mdb->m_db_port == db_port && 
52               mdb->m_dedicated == false; 
53    } 
54    return match; 
55
56  
57 BDB *BDB::bdb_clone_database_connection(JCR *jcr, bool mult_db_connections) 
58
59    BDB *mdb = this; 
60    /* 
61     * See if its a simple clone e.g. with mult_db_connections set to false 
62     * then we just return the calling class pointer. 
63     */ 
64    if (!mult_db_connections) { 
65       mdb->m_ref_count++; 
66       return mdb; 
67    } 
68  
69    /* 
70     * A bit more to do here just open a new session to the database. 
71     */ 
72    return db_init_database(jcr, mdb->m_db_driver, mdb->m_db_name, 
73              mdb->m_db_user, mdb->m_db_password, mdb->m_db_address, 
74              mdb->m_db_port, mdb->m_db_socket,
75              mdb->m_db_ssl_key, mdb->m_db_ssl_cert,
76              mdb->m_db_ssl_ca, mdb->m_db_ssl_capath,
77              mdb->m_db_ssl_cipher, true,
78              mdb->m_disabled_batch_insert); 
79
80  
81 const char *BDB::bdb_get_engine_name(void) 
82
83    BDB *mdb = this; 
84    switch (mdb->m_db_driver_type) { 
85    case SQL_DRIVER_TYPE_MYSQL: 
86       return "MySQL"; 
87    case SQL_DRIVER_TYPE_POSTGRESQL: 
88       return "PostgreSQL"; 
89    case SQL_DRIVER_TYPE_SQLITE3: 
90       return "SQLite3"; 
91    default: 
92       return "Unknown"; 
93    } 
94
95  
96 /* 
97  * Lock database, this can be called multiple times by the same 
98  * thread without blocking, but must be unlocked the number of 
99  * times it was locked using db_unlock(). 
100  */ 
101 void BDB::bdb_lock(const char *file, int line) 
102
103    int errstat; 
104    BDB *mdb = this; 
105  
106    if ((errstat = rwl_writelock_p(&mdb->m_lock, file, line)) != 0) { 
107       berrno be; 
108       e_msg(file, line, M_FATAL, 0, "rwl_writelock failure. stat=%d: ERR=%s\n", 
109             errstat, be.bstrerror(errstat)); 
110    } 
111
112  
113 /* 
114  * Unlock the database. This can be called multiple times by the 
115  * same thread up to the number of times that thread called 
116  * db_lock()/ 
117  */ 
118 void BDB::bdb_unlock(const char *file, int line) 
119
120    int errstat; 
121    BDB *mdb = this; 
122  
123    if ((errstat = rwl_writeunlock(&mdb->m_lock)) != 0) { 
124       berrno be; 
125       e_msg(file, line, M_FATAL, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n", 
126             errstat, be.bstrerror(errstat)); 
127    } 
128
129  
130 bool BDB::bdb_sql_query(const char *query, int flags) 
131
132    bool retval; 
133    BDB *mdb = this; 
134  
135    bdb_lock(); 
136    retval = sql_query(query, flags); 
137    if (!retval) { 
138       Mmsg(mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror()); 
139    } 
140    bdb_unlock(); 
141    return retval; 
142
143  
144 void BDB::print_lock_info(FILE *fp) 
145
146    BDB *mdb = this; 
147    if (mdb->m_lock.valid == RWLOCK_VALID) { 
148       fprintf(fp, "\tRWLOCK=%p w_active=%i w_wait=%i\n",  
149          &mdb->m_lock, mdb->m_lock.w_active, mdb->m_lock.w_wait); 
150    } 
151
152  
153 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL */