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(void *jcr, 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");
136 * Now actually open the database. This can generate errors,
137 * which are returned in the errmsg
140 db_open_database(B_DB *mdb)
147 Dmsg1(200, "db_open_database() %s\n", mdb->db_name);
151 if ((errstat=rwl_init(&mdb->lock)) != 0) {
152 Mmsg1(&mdb->errmsg, _("Unable to initialize DB lock. ERR=%s\n"), strerror(errstat));
157 Dmsg0(200, "make_filename\n");
158 dbf = make_filename(mdb, DB_CONTROL_FILENAME);
159 mdb->cfd = open(dbf, O_CREAT|O_RDWR, 0600);
162 Mmsg2(&mdb->errmsg, _("Unable to open Catalog DB control file %s: ERR=%s\n"),
163 dbf, strerror(errno));
167 Dmsg0(200, "DB open\n");
168 /* See if the file was previously written */
169 filend = lseek(mdb->cfd, 0, SEEK_END);
170 if (filend == 0) { /* No, initialize everything */
171 Dmsg0(200, "Init DB files\n");
172 memset(&mdb->control, 0, sizeof(mdb->control));
173 mdb->control.bdb_version = BDB_VERSION;
174 bdb_write_control_file(mdb);
176 /* Create Jobs File */
177 dbf = make_filename(mdb, DB_JOBS_FILENAME);
178 fd = open(dbf, O_CREAT|O_RDWR, 0600);
182 /* Create Pools File */
183 dbf = make_filename(mdb, DB_POOLS_FILENAME);
184 fd = open(dbf, O_CREAT|O_RDWR, 0600);
188 /* Create Media File */
189 dbf = make_filename(mdb, DB_MEDIA_FILENAME);
190 fd = open(dbf, O_CREAT|O_RDWR, 0600);
194 /* Create JobMedia File */
195 dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
196 fd = open(dbf, O_CREAT|O_RDWR, 0600);
200 /* Create Client File */
201 dbf = make_filename(mdb, DB_CLIENT_FILENAME);
202 fd = open(dbf, O_CREAT|O_RDWR, 0600);
206 /* Create FileSet File */
207 dbf = make_filename(mdb, DB_FILESET_FILENAME);
208 fd = open(dbf, O_CREAT|O_RDWR, 0600);
213 Dmsg0(200, "Read control file\n");
215 lseek(mdb->cfd, 0, SEEK_SET); /* seek to begining of control file */
216 if (read(mdb->cfd, &mdb->control, sizeof(mdb->control)) != sizeof(mdb->control)) {
217 Mmsg1(&mdb->errmsg, _("Error reading catalog DB control file. ERR=%s\n"), strerror(errno));
219 } else if (mdb->control.bdb_version != BDB_VERSION) {
220 Mmsg2(&mdb->errmsg, _("Error, catalog DB control file wrong version. \
222 Please reinitialize the working directory.\n"),
223 BDB_VERSION, mdb->control.bdb_version);
234 void db_close_database(B_DB *mdb)
238 if (mdb->ref_count == 0) {
240 /* close file descriptors */
252 fclose(mdb->mediafd);
254 if (mdb->jobmediafd) {
255 fclose(mdb->jobmediafd);
258 fclose(mdb->clientfd);
260 if (mdb->filesetfd) {
261 fclose(mdb->filesetfd);
263 /* pthread_mutex_destroy(&mdb->mutex); */
264 rwl_destroy(&mdb->lock);
265 free_pool_memory(mdb->errmsg);
266 free_pool_memory(mdb->cmd);
267 free_pool_memory(mdb->cached_path);
274 void db_escape_string(char *snew, char *old, int len)
279 char *db_strerror(B_DB *mdb)
284 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
290 * Open the Jobs file for reading/writing
292 int bdb_open_jobs_file(B_DB *mdb)
297 dbf = make_filename(mdb, DB_JOBS_FILENAME);
298 mdb->jobfd = fopen(dbf, "r+");
300 Mmsg2(&mdb->errmsg, "Error opening DB Jobs file %s: ERR=%s\n",
301 dbf, strerror(errno));
302 Emsg0(M_FATAL, 0, mdb->errmsg);
312 * Open the JobMedia file for reading/writing
314 int bdb_open_jobmedia_file(B_DB *mdb)
318 if (!mdb->jobmediafd) {
319 dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
320 mdb->jobmediafd = fopen(dbf, "r+");
321 if (!mdb->jobmediafd) {
322 Mmsg2(&mdb->errmsg, "Error opening DB JobMedia file %s: ERR=%s\n",
323 dbf, strerror(errno));
324 Emsg0(M_FATAL, 0, mdb->errmsg);
335 * Open the Pools file for reading/writing
337 int bdb_open_pools_file(B_DB *mdb)
342 dbf = make_filename(mdb, DB_POOLS_FILENAME);
343 mdb->poolfd = fopen(dbf, "r+");
345 Mmsg2(&mdb->errmsg, "Error opening DB Pools file %s: ERR=%s\n",
346 dbf, strerror(errno));
347 Emsg0(M_FATAL, 0, mdb->errmsg);
351 Dmsg1(200, "Opened pool file %s\n", dbf);
358 * Open the Client file for reading/writing
360 int bdb_open_client_file(B_DB *mdb)
364 if (!mdb->clientfd) {
365 dbf = make_filename(mdb, DB_CLIENT_FILENAME);
366 mdb->clientfd = fopen(dbf, "r+");
367 if (!mdb->clientfd) {
368 Mmsg2(&mdb->errmsg, "Error opening DB Clients file %s: ERR=%s\n",
369 dbf, strerror(errno));
370 Emsg0(M_FATAL, 0, mdb->errmsg);
380 * Open the FileSet file for reading/writing
382 int bdb_open_fileset_file(B_DB *mdb)
386 if (!mdb->filesetfd) {
387 dbf = make_filename(mdb, DB_CLIENT_FILENAME);
388 mdb->filesetfd = fopen(dbf, "r+");
389 if (!mdb->filesetfd) {
390 Mmsg2(&mdb->errmsg, "Error opening DB FileSet file %s: ERR=%s\n",
391 dbf, strerror(errno));
392 Emsg0(M_FATAL, 0, mdb->errmsg);
404 * Open the Media file for reading/writing
406 int bdb_open_media_file(B_DB *mdb)
411 dbf = make_filename(mdb, DB_MEDIA_FILENAME);
412 mdb->mediafd = fopen(dbf, "r+");
414 Mmsg2(&mdb->errmsg, "Error opening DB Media file %s: ERR=%s\n",
415 dbf, strerror(errno));
425 void _db_lock(char *file, int line, B_DB *mdb)
428 if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
429 e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
434 void _db_unlock(char *file, int line, B_DB *mdb)
437 if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
438 e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
444 * Start a transaction. This groups inserts and makes things
445 * much more efficient. Usually started when inserting
448 void db_start_transaction(B_DB *mdb)
452 void db_end_transaction(B_DB *mdb)
457 #endif /* HAVE_BACULA_DB */