]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/bdb.c
Remove old code from bdb.c
[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    off_t filend;
144
145    Dmsg1(200, "db_open_database() %s\n", mdb->db_name);
146
147    P(mutex);
148
149    if (rwl_init(&mdb->lock) != 0) {
150       Mmsg1(&mdb->errmsg, "Unable to initialize DB lock. ERR=%s\n", strerror(errno));
151       V(mutex);
152       return 0;
153    }
154
155    Dmsg0(200, "make_filename\n");
156    dbf = make_filename(mdb, DB_CONTROL_FILENAME);
157    mdb->cfd = open(dbf, O_CREAT|O_RDWR, 0600); 
158    free_memory(dbf);
159    if (mdb->cfd < 0) {
160       Mmsg2(&mdb->errmsg, "Unable to open Catalog DB control file %s: ERR=%s\n", 
161          dbf, strerror(errno));
162       V(mutex);
163       return 0;
164    }
165    Dmsg0(200, "DB open\n");
166    /* See if the file was previously written */
167    filend = lseek(mdb->cfd, 0, SEEK_END);
168    if (filend == 0) {                 /* No, initialize everything */
169       Dmsg0(200, "Init DB files\n");
170       memset(&mdb->control, 0, sizeof(mdb->control));
171       mdb->control.bdb_version = BDB_VERSION;
172       bdb_write_control_file(mdb);
173
174       /* Create Jobs File */
175       dbf = make_filename(mdb, DB_JOBS_FILENAME);
176       fd = open(dbf, O_CREAT|O_RDWR, 0600);
177       free_memory(dbf);
178       close(fd);
179
180       /* Create Pools File */
181       dbf = make_filename(mdb, DB_POOLS_FILENAME);
182       fd = open(dbf, O_CREAT|O_RDWR, 0600);
183       free_memory(dbf);
184       close(fd);
185
186       /* Create Media File */
187       dbf = make_filename(mdb, DB_MEDIA_FILENAME);
188       fd = open(dbf, O_CREAT|O_RDWR, 0600);
189       free_memory(dbf);
190       close(fd);
191
192       /* Create JobMedia File */
193       dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
194       fd = open(dbf, O_CREAT|O_RDWR, 0600);
195       free_memory(dbf);
196       close(fd);
197
198       /* Create Client File */
199       dbf = make_filename(mdb, DB_CLIENT_FILENAME);
200       fd = open(dbf, O_CREAT|O_RDWR, 0600);
201       free_memory(dbf);
202       close(fd);
203
204       /* Create FileSet File */
205       dbf = make_filename(mdb, DB_FILESET_FILENAME);
206       fd = open(dbf, O_CREAT|O_RDWR, 0600);
207       free_memory(dbf);
208       close(fd);
209    }
210
211    Dmsg0(200, "Read control file\n");
212    badctl = 0;
213    lseek(mdb->cfd, 0, SEEK_SET);      /* seek to begining of control file */
214    if (read(mdb->cfd, &mdb->control, sizeof(mdb->control)) != sizeof(mdb->control)) {
215       Mmsg1(&mdb->errmsg, "Error reading catalog DB control file. ERR=%s\n", strerror(errno));
216       badctl = 1;
217    } else if (mdb->control.bdb_version != BDB_VERSION) {
218       Mmsg2(&mdb->errmsg, "Error, catalog DB control file wrong version. \
219 Wanted %d, got %d\n\
220 Please reinitialize the working directory.\n", 
221          BDB_VERSION, mdb->control.bdb_version);
222       badctl = 1;
223    }
224    if (badctl) {
225       V(mutex);
226       return 0;
227    }
228    V(mutex);
229    return 1;
230 }
231
232 void db_close_database(B_DB *mdb)            
233 {
234    P(mutex);
235    mdb->ref_count--;
236    if (mdb->ref_count == 0) {
237       qdchain(&mdb->bq);
238       /*  close file descriptors */
239       if (mdb->cfd >= 0) {
240          close(mdb->cfd);
241       }
242       free(mdb->db_name);
243       if (mdb->jobfd) {
244          fclose(mdb->jobfd);
245       }
246       if (mdb->poolfd) {
247          fclose(mdb->poolfd);
248       }
249       if (mdb->mediafd) {
250          fclose(mdb->mediafd);
251       }
252       if (mdb->jobmediafd) {
253          fclose(mdb->jobmediafd);
254       }
255       if (mdb->clientfd) {
256          fclose(mdb->clientfd);
257       }
258       if (mdb->filesetfd) {
259          fclose(mdb->filesetfd);
260       }
261 /*    pthread_mutex_destroy(&mdb->mutex); */
262       rwl_destroy(&mdb->lock);       
263       free_pool_memory(mdb->errmsg);
264       free_pool_memory(mdb->cmd);
265       free_pool_memory(mdb->cached_path);
266       free(mdb);
267    }
268    V(mutex);
269 }
270
271
272 void db_escape_string(char *snew, char *old, int len)
273 {
274    strcpy(snew, old);
275 }
276
277 char *db_strerror(B_DB *mdb)
278 {
279    return mdb->errmsg;
280 }
281
282 int db_sql_query(B_DB *mdb, char *query, DB_RESULT_HANDLER *result_handler, void *ctx)
283 {
284    return 1;
285 }
286
287 /*
288  * Open the Jobs file for reading/writing
289  */
290 int bdb_open_jobs_file(B_DB *mdb)
291 {
292    char *dbf;
293
294    if (!mdb->jobfd) {  
295       dbf = make_filename(mdb, DB_JOBS_FILENAME);
296       mdb->jobfd = fopen(dbf, "r+");
297       if (!mdb->jobfd) {
298          Mmsg2(&mdb->errmsg, "Error opening DB Jobs file %s: ERR=%s\n", 
299             dbf, strerror(errno));
300          Emsg0(M_FATAL, 0, mdb->errmsg);
301          free_memory(dbf);
302          return 0;
303       }
304       free_memory(dbf);
305    }
306    return 1;
307 }
308
309 /*
310  * Open the JobMedia file for reading/writing
311  */
312 int bdb_open_jobmedia_file(B_DB *mdb)
313 {
314    char *dbf;
315
316    if (!mdb->jobmediafd) {  
317       dbf = make_filename(mdb, DB_JOBMEDIA_FILENAME);
318       mdb->jobmediafd = fopen(dbf, "r+");
319       if (!mdb->jobmediafd) {
320          Mmsg2(&mdb->errmsg, "Error opening DB JobMedia file %s: ERR=%s\n", 
321             dbf, strerror(errno));
322          Emsg0(M_FATAL, 0, mdb->errmsg);
323          free_memory(dbf);
324          return 0;
325       }
326       free_memory(dbf);
327    }
328    return 1;
329 }
330
331
332 /*
333  * Open the Pools file for reading/writing
334  */
335 int bdb_open_pools_file(B_DB *mdb)
336 {
337    char *dbf;
338
339    if (!mdb->poolfd) {  
340       dbf = make_filename(mdb, DB_POOLS_FILENAME);
341       mdb->poolfd = fopen(dbf, "r+");
342       if (!mdb->poolfd) {
343          Mmsg2(&mdb->errmsg, "Error opening DB Pools file %s: ERR=%s\n", 
344             dbf, strerror(errno));
345          Emsg0(M_FATAL, 0, mdb->errmsg);
346          free_memory(dbf);
347          return 0;
348       }
349       Dmsg1(200, "Opened pool file %s\n", dbf);
350       free_memory(dbf);
351    }
352    return 1;
353 }
354
355 /*
356  * Open the Client file for reading/writing
357  */
358 int bdb_open_client_file(B_DB *mdb)
359 {
360    char *dbf;
361
362    if (!mdb->clientfd) {  
363       dbf = make_filename(mdb, DB_CLIENT_FILENAME);
364       mdb->clientfd = fopen(dbf, "r+");
365       if (!mdb->clientfd) {
366          Mmsg2(&mdb->errmsg, "Error opening DB Clients file %s: ERR=%s\n", 
367             dbf, strerror(errno));
368          Emsg0(M_FATAL, 0, mdb->errmsg);
369          free_memory(dbf);
370          return 0;
371       }
372       free_memory(dbf);
373    }
374    return 1;
375 }
376
377 /*
378  * Open the FileSet file for reading/writing
379  */
380 int bdb_open_fileset_file(B_DB *mdb)
381 {
382    char *dbf;
383
384    if (!mdb->filesetfd) {  
385       dbf = make_filename(mdb, DB_CLIENT_FILENAME);
386       mdb->filesetfd = fopen(dbf, "r+");
387       if (!mdb->filesetfd) {
388          Mmsg2(&mdb->errmsg, "Error opening DB FileSet file %s: ERR=%s\n", 
389             dbf, strerror(errno));
390          Emsg0(M_FATAL, 0, mdb->errmsg);
391          free_memory(dbf);
392          return 0;
393       }
394       free_memory(dbf);
395    }
396    return 1;
397 }
398
399
400
401 /*
402  * Open the Media file for reading/writing
403  */
404 int bdb_open_media_file(B_DB *mdb)
405 {
406    char *dbf;
407
408    if (!mdb->mediafd) {  
409       dbf = make_filename(mdb, DB_MEDIA_FILENAME);
410       mdb->mediafd = fopen(dbf, "r+");
411       if (!mdb->mediafd) {
412          Mmsg2(&mdb->errmsg, "Error opening DB Media file %s: ERR=%s\n", 
413             dbf, strerror(errno));
414          free_memory(dbf);
415          return 0;
416       }
417       free_memory(dbf);
418    }
419    return 1;
420 }
421
422
423 void _db_lock(char *file, int line, B_DB *mdb)
424 {
425    int errstat;
426    if ((errstat=rwl_writelock(&mdb->lock)) != 0) {
427       e_msg(file, line, M_ABORT, 0, "rwl_writelock failure. ERR=%s\n",
428            strerror(errstat));
429    }
430 }    
431
432 void _db_unlock(char *file, int line, B_DB *mdb)
433 {
434    int errstat;
435    if ((errstat=rwl_writeunlock(&mdb->lock)) != 0) {
436       e_msg(file, line, M_ABORT, 0, "rwl_writeunlock failure. ERR=%s\n",
437            strerror(errstat));
438    }
439 }    
440
441 #endif /* HAVE_BACULA_DB */