]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/cats.c
Backport from Bacula Enterprise
[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, true, 
75              mdb->m_disabled_batch_insert); 
76
77  
78 const char *BDB::bdb_get_engine_name(void) 
79
80    BDB *mdb = this; 
81    switch (mdb->m_db_driver_type) { 
82    case SQL_DRIVER_TYPE_MYSQL: 
83       return "MySQL"; 
84    case SQL_DRIVER_TYPE_POSTGRESQL: 
85       return "PostgreSQL"; 
86    case SQL_DRIVER_TYPE_SQLITE3: 
87       return "SQLite3"; 
88    default: 
89       return "Unknown"; 
90    } 
91
92  
93 /* 
94  * Lock database, this can be called multiple times by the same 
95  * thread without blocking, but must be unlocked the number of 
96  * times it was locked using db_unlock(). 
97  */ 
98 void BDB::bdb_lock(const char *file, int line) 
99
100    int errstat; 
101    BDB *mdb = this; 
102  
103    if ((errstat = rwl_writelock_p(&mdb->m_lock, file, line)) != 0) { 
104       berrno be; 
105       e_msg(file, line, M_FATAL, 0, "rwl_writelock failure. stat=%d: ERR=%s\n", 
106             errstat, be.bstrerror(errstat)); 
107    } 
108
109  
110 /* 
111  * Unlock the database. This can be called multiple times by the 
112  * same thread up to the number of times that thread called 
113  * db_lock()/ 
114  */ 
115 void BDB::bdb_unlock(const char *file, int line) 
116
117    int errstat; 
118    BDB *mdb = this; 
119  
120    if ((errstat = rwl_writeunlock(&mdb->m_lock)) != 0) { 
121       berrno be; 
122       e_msg(file, line, M_FATAL, 0, "rwl_writeunlock failure. stat=%d: ERR=%s\n", 
123             errstat, be.bstrerror(errstat)); 
124    } 
125
126  
127 bool BDB::bdb_sql_query(const char *query, int flags) 
128
129    bool retval; 
130    BDB *mdb = this; 
131  
132    bdb_lock(); 
133    retval = sql_query(query, flags); 
134    if (!retval) { 
135       Mmsg(mdb->errmsg, _("Query failed: %s: ERR=%s\n"), query, sql_strerror()); 
136    } 
137    bdb_unlock(); 
138    return retval; 
139
140  
141 void BDB::print_lock_info(FILE *fp) 
142
143    BDB *mdb = this; 
144    if (mdb->m_lock.valid == RWLOCK_VALID) { 
145       fprintf(fp, "\tRWLOCK=%p w_active=%i w_wait=%i\n",  
146          &mdb->m_lock, mdb->m_lock.w_active, mdb->m_lock.w_wait); 
147    } 
148
149  
150 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL */