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, 2001, 2002 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 /* Forward referenced functions */
49 extern char *working_directory;
51 /* List of open databases */
52 static BQUEUE db_list = {&db_list, &db_list};
53 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
55 /* -----------------------------------------------------------------------
57 * Bacula specific defines and subroutines
59 * -----------------------------------------------------------------------
63 #define DB_CONTROL_FILENAME "control.db"
64 #define DB_JOBS_FILENAME "jobs.db"
65 #define DB_POOLS_FILENAME "pools.db"
66 #define DB_MEDIA_FILENAME "media.db"
67 #define DB_JOBMEDIA_FILENAME "jobmedia.db"
68 #define DB_CLIENT_FILENAME "client.db"
69 #define DB_FILESET_FILENAME "fileset.db"
71 static POOLMEM *make_filename(B_DB *mdb, char *name)
76 dbf = get_pool_memory(PM_FNAME);
77 if (working_directory[strlen(working_directory)-1] == '/') {
82 Mmsg(&dbf, "%s%c%s-%s", working_directory, sep, mdb->db_name, name);
86 int bdb_write_control_file(B_DB *mdb)
88 mdb->control.time = time(NULL);
89 lseek(mdb->cfd, 0, SEEK_SET);
90 if (write(mdb->cfd, &mdb->control, sizeof(mdb->control)) != sizeof(mdb->control)) {
91 Mmsg1(&mdb->errmsg, "Error writing control file. ERR=%s\n", strerror(errno));
92 Emsg0(M_FATAL, 0, mdb->errmsg);
99 * Initialize database data structure. In principal this should
100 * never have errors, or it is really fatal.
103 db_init_database(char *db_name, char *db_user, char *db_password)
106 P(mutex); /* lock DB queue */
107 /* Look to see if DB already open */
108 for (mdb=NULL; (mdb=(B_DB *)qnext(&db_list, &mdb->bq)); ) {
109 if (strcmp(mdb->db_name, db_name) == 0) {
110 Dmsg2(200, "DB REopen %d %s\n", mdb->ref_count, db_name);
113 return mdb; /* already open */
116 Dmsg0(200, "db_open first time\n");
117 mdb = (B_DB *) malloc(sizeof(B_DB));
118 memset(mdb, 0, sizeof(B_DB));
119 Dmsg0(200, "DB struct init\n");
120 mdb->db_name = bstrdup(db_name);
121 mdb->errmsg = get_pool_memory(PM_EMSG);
123 mdb->cmd = get_pool_memory(PM_EMSG); /* command buffer */
125 mdb->cached_path = get_pool_memory(PM_FNAME);
126 mdb->cached_path_id = 0;
127 qinsert(&db_list, &mdb->bq); /* put db in list */
128 Dmsg0(200, "Done db_open_database()\n");
135 * Now actually open the database. This can generate errors,
136 * which are returned in the errmsg
139 db_open_database(B_DB *mdb)
146 Dmsg1(200, "db_open_database() %s\n", mdb->db_name);
150 if ((errstat = pthread_mutex_init(&(mdb->mutex), NULL)) != 0) {
151 Mmsg1(&mdb->errmsg, "Unable to initialize DB mutex. ERR=%s\n", strerror(errstat));
155 db_lock(mdb); /* test it once */
159 if (rwl_init(&mdb->lock) != 0) {
160 Mmsg1(&mdb->errmsg, "Unable to initialize DB lock. ERR=%s\n", strerror(errno));
165 Dmsg0(200, "make_filename\n");
166 dbf = make_filename(mdb, DB_CONTROL_FILENAME);
167 mdb->cfd = open(dbf, O_CREAT|O_RDWR, 0600);
170 Mmsg2(&mdb->errmsg, "Unable to open Catalog DB control file %s: ERR=%s\n",
171 dbf, strerror(errno));
175 Dmsg0(200, "DB open\n");
176 /* See if the file was previously written */
177 filend = lseek(mdb->cfd, 0, SEEK_END);
178 if (filend == 0) { /* No, initialize everything */
179 Dmsg0(200, "Init DB files\n");
180 memset(&mdb->control, 0, sizeof(mdb->control));
181 mdb->control.bdb_version = BDB_VERSION;
182 bdb_write_control_file(mdb);
184 /* Create Jobs File */
185 dbf = make_filename(mdb, DB_JOBS_FILENAME);
186 fd = open(dbf, O_CREAT|O_RDWR, 0600);
190 /* Create Pools File */
191 dbf = make_filename(mdb, DB_POOLS_FILENAME);
192 fd = open(dbf, O_CREAT|O_RDWR, 0600);
196 /* Create Media File */
197 dbf = make_filename(mdb, DB_MEDIA_FILENAME);
198 fd = open(dbf, O_CREAT|O_RDWR, 0600);
202 /* Create JobMedia File */
203 dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
204 fd = open(dbf, O_CREAT|O_RDWR, 0600);
208 /* Create Client File */
209 dbf = make_filename(mdb, DB_CLIENT_FILENAME);
210 fd = open(dbf, O_CREAT|O_RDWR, 0600);
214 /* Create FileSet File */
215 dbf = make_filename(mdb, DB_FILESET_FILENAME);
216 fd = open(dbf, O_CREAT|O_RDWR, 0600);
221 Dmsg0(200, "Read control file\n");
223 lseek(mdb->cfd, 0, SEEK_SET); /* seek to begining of control file */
224 if (read(mdb->cfd, &mdb->control, sizeof(mdb->control)) != sizeof(mdb->control)) {
225 Mmsg1(&mdb->errmsg, "Error reading catalog DB control file. ERR=%s\n", strerror(errno));
227 } else if (mdb->control.bdb_version != BDB_VERSION) {
228 Mmsg2(&mdb->errmsg, "Error, catalog DB control file wrong version. \
230 Please reinitialize the working directory.\n",
231 BDB_VERSION, mdb->control.bdb_version);
242 void db_close_database(B_DB *mdb)
246 if (mdb->ref_count == 0) {
248 /* close file descriptors */
260 fclose(mdb->mediafd);
262 if (mdb->jobmediafd) {
263 fclose(mdb->jobmediafd);
266 fclose(mdb->clientfd);
268 if (mdb->filesetfd) {
269 fclose(mdb->filesetfd);
271 /* pthread_mutex_destroy(&mdb->mutex); */
272 rwl_destroy(&mdb->lock);
273 free_pool_memory(mdb->errmsg);
274 free_pool_memory(mdb->cmd);
275 free_pool_memory(mdb->cached_path);
282 void db_escape_string(char *snew, char *old, int len)
287 char *db_strerror(B_DB *mdb)
292 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
298 * Open the Jobs file for reading/writing
300 int bdb_open_jobs_file(B_DB *mdb)
305 dbf = make_filename(mdb, DB_JOBS_FILENAME);
306 mdb->jobfd = fopen(dbf, "r+");
308 Mmsg2(&mdb->errmsg, "Error opening DB Jobs file %s: ERR=%s\n",
309 dbf, strerror(errno));
310 Emsg0(M_FATAL, 0, mdb->errmsg);
320 * Open the JobMedia file for reading/writing
322 int bdb_open_jobmedia_file(B_DB *mdb)
326 if (!mdb->jobmediafd) {
327 dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
328 mdb->jobmediafd = fopen(dbf, "r+");
329 if (!mdb->jobmediafd) {
330 Mmsg2(&mdb->errmsg, "Error opening DB JobMedia file %s: ERR=%s\n",
331 dbf, strerror(errno));
332 Emsg0(M_FATAL, 0, mdb->errmsg);
343 * Open the Pools file for reading/writing
345 int bdb_open_pools_file(B_DB *mdb)
350 dbf = make_filename(mdb, DB_POOLS_FILENAME);
351 mdb->poolfd = fopen(dbf, "r+");
353 Mmsg2(&mdb->errmsg, "Error opening DB Pools file %s: ERR=%s\n",
354 dbf, strerror(errno));
355 Emsg0(M_FATAL, 0, mdb->errmsg);
359 Dmsg1(200, "Opened pool file %s\n", dbf);
366 * Open the Client file for reading/writing
368 int bdb_open_client_file(B_DB *mdb)
372 if (!mdb->clientfd) {
373 dbf = make_filename(mdb, DB_CLIENT_FILENAME);
374 mdb->clientfd = fopen(dbf, "r+");
375 if (!mdb->clientfd) {
376 Mmsg2(&mdb->errmsg, "Error opening DB Clients file %s: ERR=%s\n",
377 dbf, strerror(errno));
378 Emsg0(M_FATAL, 0, mdb->errmsg);
388 * Open the FileSet file for reading/writing
390 int bdb_open_fileset_file(B_DB *mdb)
394 if (!mdb->filesetfd) {
395 dbf = make_filename(mdb, DB_CLIENT_FILENAME);
396 mdb->filesetfd = fopen(dbf, "r+");
397 if (!mdb->filesetfd) {
398 Mmsg2(&mdb->errmsg, "Error opening DB FileSet file %s: ERR=%s\n",
399 dbf, strerror(errno));
400 Emsg0(M_FATAL, 0, mdb->errmsg);
412 * Open the Media file for reading/writing
414 int bdb_open_media_file(B_DB *mdb)
419 dbf = make_filename(mdb, DB_MEDIA_FILENAME);
420 mdb->mediafd = fopen(dbf, "r+");
422 Mmsg2(&mdb->errmsg, "Error opening DB Media file %s: ERR=%s\n",
423 dbf, strerror(errno));
433 void _db_lock(char *file, int line, B_DB *mdb)
436 if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
437 e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
442 void _db_unlock(char *file, int line, B_DB *mdb)
445 if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
446 e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
451 #endif /* HAVE_BACULA_DB */