2 * Bacula Catalog Database Get record interface routines
3 * Note, these routines generally get a record by id or
4 * by name. If more logic is involved, the routine
7 * Bacula Catalog Database routines written specifically
8 * for Bacula. Note, these routines are VERY dumb and
9 * do not provide all the functionality of an SQL database.
10 * The purpose of these routines is to ensure that Bacula
11 * can limp along if no real database is loaded on the
14 * Kern Sibbald, January MMI
20 Copyright (C) 2001, 2002 Kern Sibbald and John Walker
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2 of
25 the License, or (at your option) any later version.
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to the Free
34 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
40 /* The following is necessary so that we do not include
41 * the dummy external definition of DB.
43 #define __SQL_C /* indicate that this is sql.c */
51 /* Forward referenced functions */
52 int db_get_media_record(B_DB *mdb, MEDIA_DBR *mr);
53 int db_get_pool_record(B_DB *mdb, POOL_DBR *pr);
56 /* -----------------------------------------------------------------------
58 * Bacula specific defines and subroutines
60 * -----------------------------------------------------------------------
65 * Get Job record for given JobId
66 * Returns: 0 on failure
70 int db_get_job_record(B_DB *mdb, JOB_DBR *jr)
79 if (jr->JobId == 0 && jr->Name[0] == 0) { /* he wants # of Job records */
80 jr->JobId = mdb->control.JobId;
84 Dmsg0(200, "Open Jobs\n");
85 if (!bdb_open_jobs_file(mdb)) {
89 fseek(mdb->jobfd, 0L, SEEK_SET); /* rewind file */
91 /* Linear search through Job records
94 while (fread(&ojr, len, 1, mdb->jobfd) > 0) {
95 /* If id not zero, search by Id */
97 if (jr->JobId == ojr.JobId) {
101 } else if (strcmp(jr->Job, ojr.Job) == 0) {
103 Dmsg1(200, "Found Job: %s\n", ojr.Job);
106 rec_addr = ftell(mdb->jobfd); /* save start next record */
109 /* Found desired record, now return it */
110 memcpy(jr, &ojr, len);
111 jr->rec_addr = rec_addr;
113 Dmsg2(200, "Found job record: JobId=%d Job=%s",
118 strcpy(mdb->errmsg, "Job record not found.\n");
121 Dmsg1(200, "Return job stat=%d\n", stat);
127 * Get the number of pool records
129 * Returns: -1 on failure
132 int db_get_num_pool_records(B_DB *mdb)
137 stat = mdb->control.PoolId;
143 * This function returns a list of all the Pool record ids.
144 * The caller must free ids if non-NULL.
146 * Returns 0: on failure
149 int db_get_pool_ids(B_DB *mdb, int *num_ids, uint32_t *ids[])
158 if (!bdb_open_pools_file(mdb)) {
162 fseek(mdb->poolfd, 0L, SEEK_SET); /* rewind file */
163 /* Linear search through Pool records
166 *num_ids = mdb->control.PoolId;
167 id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
168 while (fread(&opr, len, 1, mdb->poolfd) > 0) {
169 id[i++] = opr.PoolId;
179 * If the PoolId is non-zero, we get its record,
180 * otherwise, we search on the PoolName
182 * Returns: 0 on failure
185 int db_get_pool_record(B_DB *mdb, POOL_DBR *pr)
194 Dmsg0(200, "Open pools\n");
195 if (!bdb_open_pools_file(mdb)) {
199 fseek(mdb->poolfd, 0L, SEEK_SET); /* rewind file */
201 /* Linear search through Pool records
204 while (fread(&opr, len, 1, mdb->poolfd) > 0) {
205 /* If id not zero, search by Id */
206 if (pr->PoolId != 0) {
207 if (pr->PoolId == opr.PoolId) {
211 } else if (strcmp(pr->Name, opr.Name) == 0) {
213 Dmsg1(200, "Found pool: %s\n", opr.Name);
216 rec_addr = ftell(mdb->poolfd); /* save start next record */
219 /* Found desired record, now return it */
220 memcpy(pr, &opr, len);
221 pr->rec_addr = rec_addr;
223 Dmsg3(200, "Found pool record: PoolId=%d Name=%s PoolType=%s\n",
224 opr.PoolId, opr.Name, opr.PoolType);
228 strcpy(mdb->errmsg, "Pool record not found.\n");
231 Dmsg1(200, "Return pool stat=%d\n", stat);
236 * Get the number of Media records
238 * Returns: -1 on failure
241 int db_get_num_media_records(B_DB *mdb)
246 stat = mdb->control.MediaId;
252 * This function returns a list of all the Media record ids.
253 * The caller must free ids if non-NULL.
255 * Returns 0: on failure
258 int db_get_media_ids(B_DB *mdb, int *num_ids, uint32_t *ids[])
267 if (!bdb_open_media_file(mdb)) {
271 fseek(mdb->mediafd, 0L, SEEK_SET); /* rewind file */
272 /* Linear search through Pool records
275 if (mdb->control.MediaId == 0) {
279 *num_ids = mdb->control.MediaId;
280 id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
281 while (fread(&omr, len, 1, mdb->mediafd) > 0) {
282 id[i++] = omr.MediaId;
291 * If the MediaId is non-zero, we get its record,
292 * otherwise, we search on the MediaName
294 * Returns: 0 on failure
297 int db_get_media_record(B_DB *mdb, MEDIA_DBR *mr)
306 if (!bdb_open_media_file(mdb)) {
310 fseek(mdb->mediafd, 0L, SEEK_SET); /* rewind file */
312 /* Linear search through Media records
315 while (fread(&omr, len, 1, mdb->mediafd) > 0) {
316 if (omr.MediaId == 0) {
317 continue; /* deleted record */
319 Dmsg1(200, "VolName=%s\n", omr.VolumeName);
320 /* If id not zero, search by Id */
321 if (mr->MediaId != 0) {
322 Dmsg1(200, "MediaId=%d\n", mr->MediaId);
323 if (mr->MediaId == omr.MediaId) {
327 } else if (strcmp(mr->VolumeName, omr.VolumeName) == 0) {
331 rec_addr = ftell(mdb->mediafd); /* save start next record */
334 /* Found desired record, now return it */
335 memcpy(mr, &omr, len);
336 mr->rec_addr = rec_addr;
338 Dmsg3(200, "Found media record: MediaId=%d Name=%s MediaType=%s\n",
339 omr.MediaId, omr.VolumeName, mr->MediaType);
343 strcpy(mdb->errmsg, "Could not find requested Media record.\n");
350 * Find VolumeNames for a give JobId
351 * Returns: 0 on error or no Volumes found
352 * number of volumes on success
353 * Volumes are concatenated in VolumeNames
354 * separated by a vertical bar (|).
356 int db_get_job_volume_names(B_DB *mdb, uint32_t JobId, POOLMEM **VolumeNames)
364 if (!bdb_open_jobmedia_file(mdb)) {
368 if (!bdb_open_media_file(mdb)) {
375 fseek(mdb->jobmediafd, 0L, SEEK_SET); /* rewind the file */
376 while (fread(&jm, jmlen, 1, mdb->jobmediafd) > 0) {
377 if (jm.JobId == JobId) {
378 /* Now look up VolumeName in Media file given MediaId */
379 fseek(mdb->mediafd, 0L, SEEK_SET);
380 while (fread(&mr, mrlen, 1, mdb->mediafd) > 0) {
381 if (jm.MediaId == mr.MediaId) {
382 if (*VolumeNames != 0) { /* if not first name, */
383 pm_strcat(VolumeNames, "|"); /* add separator */
385 pm_strcat(VolumeNames, mr.VolumeName); /* add Volume Name */
392 strcpy(mdb->errmsg, "No Volumes found.\n");
400 * If the ClientId is non-zero, we get its record,
401 * otherwise, we search on the Name
403 * Returns: 0 on failure
406 int db_get_client_record(B_DB *mdb, CLIENT_DBR *cr)
413 if (!bdb_open_client_file(mdb)) {
417 fseek(mdb->clientfd, 0L, SEEK_SET); /* rewind file */
419 * Linear search through Client records
422 while (fread(&lcr, len, 1, mdb->clientfd)) {
423 /* If id not zero, search by Id */
424 if (cr->ClientId != 0) {
425 if (cr->ClientId != lcr.ClientId) {
429 } else if (strcmp(cr->Name, lcr.Name) != 0) {
430 continue; /* not found */
432 memcpy(cr, &lcr, len);
434 Dmsg2(200, "Found Client record: ClientId=%d Name=%s\n",
435 lcr.ClientId, lcr.Name);
439 strcpy(mdb->errmsg, "Client record not found.\n");
446 * Get FileSet Record (We read the FILESET_DBR structure)
447 * If the FileSetId is non-zero, we get its record,
448 * otherwise, we search on the FileSet (its name).
450 * Returns: 0 on failure
453 int db_get_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
459 if (!bdb_open_fileset_file(mdb)) {
463 fseek(mdb->filesetfd, 0L, SEEK_SET); /* rewind file */
465 * Linear search through FileSet records
467 while (fread(&lfsr, sizeof(lfsr), 1, mdb->filesetfd) > 0) {
468 /* If id not zero, search by Id */
469 if (fsr->FileSetId != 0) {
470 if (fsr->FileSetId != lfsr.FileSetId) {
473 /* Search by Name & MD5 */
474 } else if (strcmp(fsr->FileSet, lfsr.FileSet) != 0 ||
475 strcmp(fsr->MD5, lfsr.MD5) != 0) {
476 continue; /* not found */
478 /* Found desired record, now return it */
479 memcpy(fsr, &lfsr, sizeof(lfsr));
480 stat = fsr->FileSetId;
481 Dmsg2(200, "Found FileSet record: FileSetId=%d FileSet=%s\n",
482 lfsr.FileSetId, lfsr.FileSet);
486 strcpy(mdb->errmsg, "FileSet record not found.\n");
494 int db_get_file_attributes_record(B_DB *mdb, char *fname, FILE_DBR *fdbr)
499 #endif /* HAVE_BACULA_DB */