2 * Bacula Catalog Database routines written specifically
3 * for Bacula. Note, these routines are VERY dumb and
4 * do not provide all the functionality of an SQL database.
5 * The purpose of these routines is to ensure that Bacula
6 * can limp along if no real database is loaded on the
9 * Kern Sibbald, January MMI
17 Copyright (C) 2000-2003 Kern Sibbald and John Walker
19 This program is free software; you can redistribute it and/or
20 modify it under the terms of the GNU General Public License as
21 published by the Free Software Foundation; either version 2 of
22 the License, or (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received a copy of the GNU General Public
30 License along with this program; if not, write to the Free
31 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 /* The following is necessary so that we do not include
38 * the dummy external definition of DB.
40 #define __SQL_C /* indicate that this is sql.c */
47 uint32_t bacula_db_version = 0;
49 /* Forward referenced functions */
51 extern char *working_directory;
53 /* List of open databases */
54 static BQUEUE db_list = {&db_list, &db_list};
55 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
57 /* -----------------------------------------------------------------------
59 * Bacula specific defines and subroutines
61 * -----------------------------------------------------------------------
65 #define DB_CONTROL_FILENAME "control.db"
66 #define DB_JOBS_FILENAME "jobs.db"
67 #define DB_POOLS_FILENAME "pools.db"
68 #define DB_MEDIA_FILENAME "media.db"
69 #define DB_JOBMEDIA_FILENAME "jobmedia.db"
70 #define DB_CLIENT_FILENAME "client.db"
71 #define DB_FILESET_FILENAME "fileset.db"
73 static POOLMEM *make_filename(B_DB *mdb, char *name)
78 dbf = get_pool_memory(PM_FNAME);
79 if (working_directory[strlen(working_directory)-1] == '/') {
84 Mmsg(&dbf, "%s%c%s-%s", working_directory, sep, mdb->db_name, name);
88 int bdb_write_control_file(B_DB *mdb)
90 mdb->control.time = time(NULL);
91 lseek(mdb->cfd, 0, SEEK_SET);
92 if (write(mdb->cfd, &mdb->control, sizeof(mdb->control)) != sizeof(mdb->control)) {
93 Mmsg1(&mdb->errmsg, "Error writing control file. ERR=%s\n", strerror(errno));
94 Emsg0(M_FATAL, 0, mdb->errmsg);
101 * Initialize database data structure. In principal this should
102 * never have errors, or it is really fatal.
105 db_init_database(JCR *jcr, char *db_name, char *db_user, char *db_password,
106 char *db_address, int db_port, char *db_socket)
109 P(mutex); /* lock DB queue */
110 /* Look to see if DB already open */
111 for (mdb=NULL; (mdb=(B_DB *)qnext(&db_list, &mdb->bq)); ) {
112 if (strcmp(mdb->db_name, db_name) == 0) {
113 Dmsg2(200, "DB REopen %d %s\n", mdb->ref_count, db_name);
116 return mdb; /* already open */
119 Dmsg0(200, "db_open first time\n");
120 mdb = (B_DB *)malloc(sizeof(B_DB));
121 memset(mdb, 0, sizeof(B_DB));
122 Dmsg0(200, "DB struct init\n");
123 mdb->db_name = bstrdup(db_name);
124 mdb->errmsg = get_pool_memory(PM_EMSG);
126 mdb->cmd = get_pool_memory(PM_EMSG); /* command buffer */
128 mdb->cached_path = get_pool_memory(PM_FNAME);
129 mdb->cached_path_id = 0;
130 qinsert(&db_list, &mdb->bq); /* put db in list */
131 Dmsg0(200, "Done db_open_database()\n");
134 Jmsg(jcr, M_WARNING, 0, _("WARNING!!!! The Internal Database is for TESTING ONLY!\n"));
135 Jmsg(jcr, M_WARNING, 0, _("You should use either SQLite or MySQL\n"));
141 * Now actually open the database. This can generate errors,
142 * which are returned in the errmsg
145 db_open_database(JCR *jcr, B_DB *mdb)
152 Dmsg1(200, "db_open_database() %s\n", mdb->db_name);
156 if ((errstat=rwl_init(&mdb->lock)) != 0) {
157 Mmsg1(&mdb->errmsg, _("Unable to initialize DB lock. ERR=%s\n"), strerror(errstat));
162 Dmsg0(200, "make_filename\n");
163 dbf = make_filename(mdb, DB_CONTROL_FILENAME);
164 mdb->cfd = open(dbf, O_CREAT|O_RDWR, 0600);
167 Mmsg2(&mdb->errmsg, _("Unable to open Catalog DB control file %s: ERR=%s\n"),
168 dbf, strerror(errno));
172 Dmsg0(200, "DB open\n");
173 /* See if the file was previously written */
174 filend = lseek(mdb->cfd, 0, SEEK_END);
175 if (filend == 0) { /* No, initialize everything */
176 Dmsg0(200, "Init DB files\n");
177 memset(&mdb->control, 0, sizeof(mdb->control));
178 mdb->control.bdb_version = BDB_VERSION;
179 bdb_write_control_file(mdb);
181 /* Create Jobs File */
182 dbf = make_filename(mdb, DB_JOBS_FILENAME);
183 fd = open(dbf, O_CREAT|O_RDWR, 0600);
187 /* Create Pools File */
188 dbf = make_filename(mdb, DB_POOLS_FILENAME);
189 fd = open(dbf, O_CREAT|O_RDWR, 0600);
193 /* Create Media File */
194 dbf = make_filename(mdb, DB_MEDIA_FILENAME);
195 fd = open(dbf, O_CREAT|O_RDWR, 0600);
199 /* Create JobMedia File */
200 dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
201 fd = open(dbf, O_CREAT|O_RDWR, 0600);
205 /* Create Client File */
206 dbf = make_filename(mdb, DB_CLIENT_FILENAME);
207 fd = open(dbf, O_CREAT|O_RDWR, 0600);
211 /* Create FileSet File */
212 dbf = make_filename(mdb, DB_FILESET_FILENAME);
213 fd = open(dbf, O_CREAT|O_RDWR, 0600);
218 Dmsg0(200, "Read control file\n");
220 lseek(mdb->cfd, 0, SEEK_SET); /* seek to begining of control file */
221 if (read(mdb->cfd, &mdb->control, sizeof(mdb->control)) != sizeof(mdb->control)) {
222 Mmsg1(&mdb->errmsg, _("Error reading catalog DB control file. ERR=%s\n"), strerror(errno));
224 } else if (mdb->control.bdb_version != BDB_VERSION) {
225 Mmsg2(&mdb->errmsg, _("Error, catalog DB control file wrong version. \
227 Please reinitialize the working directory.\n"),
228 BDB_VERSION, mdb->control.bdb_version);
231 bacula_db_version = mdb->control.bdb_version;
240 void db_close_database(JCR *jcr, B_DB *mdb)
244 if (mdb->ref_count == 0) {
246 /* close file descriptors */
258 fclose(mdb->mediafd);
260 if (mdb->jobmediafd) {
261 fclose(mdb->jobmediafd);
264 fclose(mdb->clientfd);
266 if (mdb->filesetfd) {
267 fclose(mdb->filesetfd);
269 rwl_destroy(&mdb->lock);
270 free_pool_memory(mdb->errmsg);
271 free_pool_memory(mdb->cmd);
272 free_pool_memory(mdb->cached_path);
279 void db_escape_string(char *snew, char *old, int len)
281 memset(snew, 0, len);
282 bstrncpy(snew, old, len);
285 char *db_strerror(B_DB *mdb)
290 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
296 * Open the Jobs file for reading/writing
298 int bdb_open_jobs_file(B_DB *mdb)
303 dbf = make_filename(mdb, DB_JOBS_FILENAME);
304 mdb->jobfd = fopen(dbf, "r+");
306 Mmsg2(&mdb->errmsg, "Error opening DB Jobs file %s: ERR=%s\n",
307 dbf, strerror(errno));
308 Emsg0(M_FATAL, 0, mdb->errmsg);
318 * Open the JobMedia file for reading/writing
320 int bdb_open_jobmedia_file(B_DB *mdb)
324 if (!mdb->jobmediafd) {
325 dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
326 mdb->jobmediafd = fopen(dbf, "r+");
327 if (!mdb->jobmediafd) {
328 Mmsg2(&mdb->errmsg, "Error opening DB JobMedia file %s: ERR=%s\n",
329 dbf, strerror(errno));
330 Emsg0(M_FATAL, 0, mdb->errmsg);
341 * Open the Pools file for reading/writing
343 int bdb_open_pools_file(B_DB *mdb)
348 dbf = make_filename(mdb, DB_POOLS_FILENAME);
349 mdb->poolfd = fopen(dbf, "r+");
351 Mmsg2(&mdb->errmsg, "Error opening DB Pools file %s: ERR=%s\n",
352 dbf, strerror(errno));
353 Emsg0(M_FATAL, 0, mdb->errmsg);
357 Dmsg1(200, "Opened pool file %s\n", dbf);
364 * Open the Client file for reading/writing
366 int bdb_open_client_file(B_DB *mdb)
370 if (!mdb->clientfd) {
371 dbf = make_filename(mdb, DB_CLIENT_FILENAME);
372 mdb->clientfd = fopen(dbf, "r+");
373 if (!mdb->clientfd) {
374 Mmsg2(&mdb->errmsg, "Error opening DB Clients file %s: ERR=%s\n",
375 dbf, strerror(errno));
376 Emsg0(M_FATAL, 0, mdb->errmsg);
386 * Open the FileSet file for reading/writing
388 int bdb_open_fileset_file(B_DB *mdb)
392 if (!mdb->filesetfd) {
393 dbf = make_filename(mdb, DB_CLIENT_FILENAME);
394 mdb->filesetfd = fopen(dbf, "r+");
395 if (!mdb->filesetfd) {
396 Mmsg2(&mdb->errmsg, "Error opening DB FileSet file %s: ERR=%s\n",
397 dbf, strerror(errno));
398 Emsg0(M_FATAL, 0, mdb->errmsg);
410 * Open the Media file for reading/writing
412 int bdb_open_media_file(B_DB *mdb)
417 dbf = make_filename(mdb, DB_MEDIA_FILENAME);
418 mdb->mediafd = fopen(dbf, "r+");
420 Mmsg2(&mdb->errmsg, "Error opening DB Media file %s: ERR=%s\n",
421 dbf, strerror(errno));
431 void _db_lock(char *file, int line, B_DB *mdb)
434 if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
435 e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
440 void _db_unlock(char *file, int line, B_DB *mdb)
443 if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
444 e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
450 * Start a transaction. This groups inserts and makes things
451 * much more efficient. Usually started when inserting
454 void db_start_transaction(JCR *jcr, B_DB *mdb)
458 void db_end_transaction(JCR *jcr, B_DB *mdb)
463 #endif /* HAVE_BACULA_DB */