]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/bdb.c
Fix verify
[bacula/bacula] / bacula / src / cats / bdb.c
1 /*
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
7  *  system.
8  *   
9  *    Kern Sibbald, January MMI 
10  *
11  *    Version $Id$
12  *
13  */
14
15
16 /*
17    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
18
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.
23
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.
28
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,
32    MA 02111-1307, USA.
33
34  */
35
36
37 /* The following is necessary so that we do not include
38  * the dummy external definition of DB.
39  */
40 #define __SQL_C                       /* indicate that this is sql.c */
41
42 #include "bacula.h"
43 #include "cats.h"
44
45 #ifdef HAVE_BACULA_DB
46
47 /* Forward referenced functions */
48
49 extern char *working_directory;
50
51 /* List of open databases */
52 static BQUEUE db_list = {&db_list, &db_list};
53 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
54
55 /* -----------------------------------------------------------------------
56  *
57  *   Bacula specific defines and subroutines
58  *
59  * -----------------------------------------------------------------------
60  */
61
62
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"
70
71 static POOLMEM *make_filename(B_DB *mdb, char *name)
72 {
73    char sep;
74    POOLMEM *dbf;
75
76    dbf = get_pool_memory(PM_FNAME);
77    if (working_directory[strlen(working_directory)-1] == '/') {
78       sep = 0;
79    } else {
80       sep = '/'; 
81    }
82    Mmsg(&dbf, "%s%c%s-%s", working_directory, sep, mdb->db_name, name);
83    return dbf;
84 }
85
86 int bdb_write_control_file(B_DB *mdb)
87 {
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);
93       return 0;
94    }
95    return 1;
96 }
97
98 /*
99  * Initialize database data structure. In principal this should
100  * never have errors, or it is really fatal.
101  */
102 B_DB *
103 db_init_database(char *db_name, char *db_user, char *db_password)
104 {
105    B_DB *mdb;
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);
111          mdb->ref_count++;
112          V(mutex);
113          return mdb;                  /* already open */
114       }
115    }
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);
122    *mdb->errmsg = 0;
123    mdb->cmd = get_pool_memory(PM_EMSG);  /* command buffer */
124    mdb->ref_count = 1;
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");
129    mdb->cfd = -1;
130    V(mutex);
131    return mdb;
132 }
133
134 /*
135  * Now actually open the database.  This can generate errors,
136  * which are returned in the errmsg
137  */
138 int
139 db_open_database(B_DB *mdb)
140 {
141    char *dbf;
142    int fd, badctl;
143    int errstat;
144    off_t filend;
145
146    Dmsg1(200, "db_open_database() %s\n", mdb->db_name);
147
148    P(mutex);
149 #ifdef needed
150    if ((errstat = pthread_mutex_init(&(mdb->mutex), NULL)) != 0) {
151       Mmsg1(&mdb->errmsg, "Unable to initialize DB mutex. ERR=%s\n", strerror(errstat));
152       V(mutex);
153       return 0;
154    }
155    db_lock(mdb);                     /* test it once */
156    db_unlock(mdb);
157 #endif
158
159    if (rwl_init(&mdb->lock) != 0) {
160       Mmsg1(&mdb->errmsg, "Unable to initialize DB lock. ERR=%s\n", strerror(errno));
161       V(mutex);
162       return 0;
163    }
164
165    Dmsg0(200, "make_filename\n");
166    dbf = make_filename(mdb, DB_CONTROL_FILENAME);
167    mdb->cfd = open(dbf, O_CREAT|O_RDWR, 0600); 
168    free_memory(dbf);
169    if (mdb->cfd < 0) {
170       Mmsg2(&mdb->errmsg, "Unable to open Catalog DB control file %s: ERR=%s\n", 
171          dbf, strerror(errno));
172       V(mutex);
173       return 0;
174    }
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);
183
184       /* Create Jobs File */
185       dbf = make_filename(mdb, DB_JOBS_FILENAME);
186       fd = open(dbf, O_CREAT|O_RDWR, 0600);
187       free_memory(dbf);
188       close(fd);
189
190       /* Create Pools File */
191       dbf = make_filename(mdb, DB_POOLS_FILENAME);
192       fd = open(dbf, O_CREAT|O_RDWR, 0600);
193       free_memory(dbf);
194       close(fd);
195
196       /* Create Media File */
197       dbf = make_filename(mdb, DB_MEDIA_FILENAME);
198       fd = open(dbf, O_CREAT|O_RDWR, 0600);
199       free_memory(dbf);
200       close(fd);
201
202       /* Create JobMedia File */
203       dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
204       fd = open(dbf, O_CREAT|O_RDWR, 0600);
205       free_memory(dbf);
206       close(fd);
207
208       /* Create Client File */
209       dbf = make_filename(mdb, DB_CLIENT_FILENAME);
210       fd = open(dbf, O_CREAT|O_RDWR, 0600);
211       free_memory(dbf);
212       close(fd);
213
214       /* Create FileSet File */
215       dbf = make_filename(mdb, DB_FILESET_FILENAME);
216       fd = open(dbf, O_CREAT|O_RDWR, 0600);
217       free_memory(dbf);
218       close(fd);
219    }
220
221    Dmsg0(200, "Read control file\n");
222    badctl = 0;
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));
226       badctl = 1;
227    } else if (mdb->control.bdb_version != BDB_VERSION) {
228       Mmsg2(&mdb->errmsg, "Error, catalog DB control file wrong version. \
229 Wanted %d, got %d\n\
230 Please reinitialize the working directory.\n", 
231          BDB_VERSION, mdb->control.bdb_version);
232       badctl = 1;
233    }
234    if (badctl) {
235       V(mutex);
236       return 0;
237    }
238    V(mutex);
239    return 1;
240 }
241
242 void db_close_database(B_DB *mdb)            
243 {
244    P(mutex);
245    mdb->ref_count--;
246    if (mdb->ref_count == 0) {
247       qdchain(&mdb->bq);
248       /*  close file descriptors */
249       if (mdb->cfd >= 0) {
250          close(mdb->cfd);
251       }
252       free(mdb->db_name);
253       if (mdb->jobfd) {
254          fclose(mdb->jobfd);
255       }
256       if (mdb->poolfd) {
257          fclose(mdb->poolfd);
258       }
259       if (mdb->mediafd) {
260          fclose(mdb->mediafd);
261       }
262       if (mdb->jobmediafd) {
263          fclose(mdb->jobmediafd);
264       }
265       if (mdb->clientfd) {
266          fclose(mdb->clientfd);
267       }
268       if (mdb->filesetfd) {
269          fclose(mdb->filesetfd);
270       }
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);
276       free(mdb);
277    }
278    V(mutex);
279 }
280
281
282 void db_escape_string(char *snew, char *old, int len)
283 {
284    strcpy(snew, old);
285 }
286
287 char *db_strerror(B_DB *mdb)
288 {
289    return mdb->errmsg;
290 }
291
292 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
293 {
294    return 1;
295 }
296
297 /*
298  * Open the Jobs file for reading/writing
299  */
300 int bdb_open_jobs_file(B_DB *mdb)
301 {
302    char *dbf;
303
304    if (!mdb->jobfd) {  
305       dbf = make_filename(mdb, DB_JOBS_FILENAME);
306       mdb->jobfd = fopen(dbf, "r+");
307       if (!mdb->jobfd) {
308          Mmsg2(&mdb->errmsg, "Error opening DB Jobs file %s: ERR=%s\n", 
309             dbf, strerror(errno));
310          Emsg0(M_FATAL, 0, mdb->errmsg);
311          free_memory(dbf);
312          return 0;
313       }
314       free_memory(dbf);
315    }
316    return 1;
317 }
318
319 /*
320  * Open the JobMedia file for reading/writing
321  */
322 int bdb_open_jobmedia_file(B_DB *mdb)
323 {
324    char *dbf;
325
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);
333          free_memory(dbf);
334          return 0;
335       }
336       free_memory(dbf);
337    }
338    return 1;
339 }
340
341
342 /*
343  * Open the Pools file for reading/writing
344  */
345 int bdb_open_pools_file(B_DB *mdb)
346 {
347    char *dbf;
348
349    if (!mdb->poolfd) {  
350       dbf = make_filename(mdb, DB_POOLS_FILENAME);
351       mdb->poolfd = fopen(dbf, "r+");
352       if (!mdb->poolfd) {
353          Mmsg2(&mdb->errmsg, "Error opening DB Pools file %s: ERR=%s\n", 
354             dbf, strerror(errno));
355          Emsg0(M_FATAL, 0, mdb->errmsg);
356          free_memory(dbf);
357          return 0;
358       }
359       Dmsg1(200, "Opened pool file %s\n", dbf);
360       free_memory(dbf);
361    }
362    return 1;
363 }
364
365 /*
366  * Open the Client file for reading/writing
367  */
368 int bdb_open_client_file(B_DB *mdb)
369 {
370    char *dbf;
371
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);
379          free_memory(dbf);
380          return 0;
381       }
382       free_memory(dbf);
383    }
384    return 1;
385 }
386
387 /*
388  * Open the FileSet file for reading/writing
389  */
390 int bdb_open_fileset_file(B_DB *mdb)
391 {
392    char *dbf;
393
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);
401          free_memory(dbf);
402          return 0;
403       }
404       free_memory(dbf);
405    }
406    return 1;
407 }
408
409
410
411 /*
412  * Open the Media file for reading/writing
413  */
414 int bdb_open_media_file(B_DB *mdb)
415 {
416    char *dbf;
417
418    if (!mdb->mediafd) {  
419       dbf = make_filename(mdb, DB_MEDIA_FILENAME);
420       mdb->mediafd = fopen(dbf, "r+");
421       if (!mdb->mediafd) {
422          Mmsg2(&mdb->errmsg, "Error opening DB Media file %s: ERR=%s\n", 
423             dbf, strerror(errno));
424          free_memory(dbf);
425          return 0;
426       }
427       free_memory(dbf);
428    }
429    return 1;
430 }
431
432
433 void _db_lock(char *file, int line, B_DB *mdb)
434 {
435    int errstat;
436    if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
437       e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
438            strerror(errstat));
439    }
440 }    
441
442 void _db_unlock(char *file, int line, B_DB *mdb)
443 {
444    int errstat;
445    if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
446       e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
447            strerror(errstat));
448    }
449 }    
450
451 #endif /* HAVE_BACULA_DB */