/
cats.cc
244 lines (218 loc) · 6.75 KB
/
cats.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2011-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2019 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero 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
Affero General Public License for more details.
You should have received a copy of the GNU Affero 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.
*/
/*
* Written by Marco van Wieringen, January 2011
*/
/**
* @file
* Generic catalog class methods.
*/
#include "include/bareos.h"
#if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || HAVE_DBI
#include "cats.h"
#include "sql_pooling.h"
#include "bdb_query_names.inc"
#include "lib/berrno.h"
bool BareosDb::MatchDatabase(const char* db_driver,
const char* db_name,
const char* db_address,
int db_port)
{
bool match;
if (db_driver) {
match = Bstrcasecmp(db_driver_, db_driver) && bstrcmp(db_name_, db_name) &&
bstrcmp(db_address_, db_address) && db_port_ == db_port;
} else {
match = bstrcmp(db_name_, db_name) && bstrcmp(db_address_, db_address) &&
db_port_ == db_port;
}
return match;
}
/**
* Clone a BareosDb class by either increasing the reference count
* (when mult_db_connection == false) or by getting a new
* connection otherwise. We use a method so we can reference
* the protected members of the class.
*/
BareosDb* BareosDb::CloneDatabaseConnection(JobControlRecord* jcr,
bool mult_db_connections,
bool get_pooled_connection,
bool need_private)
{
/*
* See if its a simple clone e.g. with mult_db_connections set to false
* then we just return the calling class pointer.
*/
if (!mult_db_connections && !need_private) {
ref_count_++;
return this;
}
/*
* A bit more to do here just open a new session to the database.
* See if we need to get a pooled or non pooled connection.
*/
if (get_pooled_connection) {
return DbSqlGetPooledConnection(
jcr, db_driver_, db_name_, db_user_, db_password_, db_address_,
db_port_, db_socket_, mult_db_connections, disabled_batch_insert_,
try_reconnect_, exit_on_fatal_, need_private);
} else {
return DbSqlGetNonPooledConnection(
jcr, db_driver_, db_name_, db_user_, db_password_, db_address_,
db_port_, db_socket_, mult_db_connections, disabled_batch_insert_,
try_reconnect_, exit_on_fatal_, need_private);
}
}
const char* BareosDb::GetType(void)
{
switch (db_interface_type_) {
case SQL_INTERFACE_TYPE_MYSQL:
return "MySQL";
case SQL_INTERFACE_TYPE_POSTGRESQL:
return "PostgreSQL";
case SQL_INTERFACE_TYPE_SQLITE3:
return "SQLite3";
case SQL_INTERFACE_TYPE_INGRES:
return "Ingres";
case SQL_INTERFACE_TYPE_DBI:
switch (db_type_) {
case SQL_TYPE_MYSQL:
return "DBI:MySQL";
case SQL_TYPE_POSTGRESQL:
return "DBI:PostgreSQL";
case SQL_TYPE_SQLITE3:
return "DBI:SQLite3";
case SQL_TYPE_INGRES:
return "DBI:Ingres";
default:
return "DBI:Unknown";
}
default:
return "Unknown";
}
}
/**
* Lock database, this can be called multiple times by the same
* thread without blocking, but must be unlocked the number of
* times it was locked using DbUnlock().
*/
void BareosDb::LockDb(const char* file, int line)
{
int errstat;
if ((errstat = RwlWritelock_p(&lock_, file, line)) != 0) {
BErrNo be;
e_msg(file, line, M_FATAL, 0, "RwlWritelock failure. stat=%d: ERR=%s\n",
errstat, be.bstrerror(errstat));
}
}
/**
* Unlock the database. This can be called multiple times by the
* same thread up to the number of times that thread called
* DbLock()/
*/
void BareosDb::UnlockDb(const char* file, int line)
{
int errstat;
if ((errstat = RwlWriteunlock(&lock_)) != 0) {
BErrNo be;
e_msg(file, line, M_FATAL, 0, "RwlWriteunlock failure. stat=%d: ERR=%s\n",
errstat, be.bstrerror(errstat));
}
}
void BareosDb::PrintLockInfo(FILE* fp)
{
if (lock_.valid == RWLOCK_VALID) {
fprintf(fp, "\tRWLOCK=%p w_active=%i w_wait=%i\n", &lock_, lock_.w_active,
lock_.w_wait);
}
}
/**
* Escape strings so that database engine is happy.
*
* NOTE! len is the length of the old string. Your new
* string must be long enough (max 2*old+1) to hold
* the escaped output.
*/
void BareosDb::EscapeString(JobControlRecord* jcr,
char* snew,
const char* old,
int len)
{
char* n;
const char* o;
n = snew;
o = old;
while (len--) {
switch (*o) {
case '\'':
*n++ = '\'';
*n++ = '\'';
o++;
break;
case 0:
*n++ = '\\';
*n++ = 0;
o++;
break;
default:
*n++ = *o++;
break;
}
}
*n = 0;
}
/**
* Escape binary object.
* We base64 encode the data so its normal ASCII
* Memory is stored in BareosDb struct, no need to free it.
*/
char* BareosDb::EscapeObject(JobControlRecord* jcr, char* old, int len)
{
const int MaxLength = Base64LengthUnpadded(len) + 1;
esc_obj = CheckPoolMemorySize(esc_obj, MaxLength + 1);
const int length = BinToBase64(esc_obj, MaxLength, old, len, true);
esc_obj[length] = '\0';
return esc_obj;
}
/**
* Unescape binary object
* We base64 encode the data so its normal ASCII
*/
void BareosDb::UnescapeObject(JobControlRecord* jcr,
char* from,
int32_t expected_len,
POOLMEM*& dest,
int32_t* dest_len)
{
if (!from) {
dest[0] = '\0';
*dest_len = 0;
return;
}
dest = CheckPoolMemorySize(dest, expected_len + 1);
/*
* Note: Base64ToBin() does not check the expected length correctly,
* so we must add 2 to make sure it works.
*/
Base64ToBin(dest, expected_len + 2, from, strlen(from));
*dest_len = expected_len;
dest[expected_len] = '\0';
}
#endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_POSTGRESQL || HAVE_INGRES || \
HAVE_DBI */