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
18 Copyright (C) 2001, 2002 Kern Sibbald and John Walker
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation; either version 2 of
23 the License, or (at your option) any later version.
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 General Public License for more details.
30 You should have received a copy of the GNU General Public
31 License along with this program; if not, write to the Free
32 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
38 /* The following is necessary so that we do not include
39 * the dummy external definition of DB.
41 #define __SQL_C /* indicate that this is sql.c */
49 /* Forward referenced functions */
50 int db_get_media_record(B_DB *mdb, MEDIA_DBR *mr);
51 int db_get_pool_record(B_DB *mdb, POOL_DBR *pr);
54 /* -----------------------------------------------------------------------
56 * Bacula specific defines and subroutines
58 * -----------------------------------------------------------------------
63 * Get Job record for given JobId
64 * Returns: 0 on failure
68 int db_get_job_record(B_DB *mdb, JOB_DBR *jr)
77 if (jr->JobId == 0 && jr->Name[0] == 0) { /* he wants # of Job records */
78 jr->JobId = mdb->control.JobId;
82 Dmsg0(200, "Open Jobs\n");
83 if (!bdb_open_jobs_file(mdb)) {
87 fseek(mdb->jobfd, 0L, SEEK_SET); /* rewind file */
89 /* Linear search through Job records
92 while (fread(&ojr, len, 1, mdb->jobfd) > 0) {
93 /* If id not zero, search by Id */
95 if (jr->JobId == ojr.JobId) {
99 } else if (strcmp(jr->Job, ojr.Job) == 0) {
101 Dmsg1(200, "Found Job: %s\n", ojr.Job);
104 rec_addr = ftell(mdb->jobfd); /* save start next record */
107 /* Found desired record, now return it */
108 memcpy(jr, &ojr, len);
109 jr->rec_addr = rec_addr;
111 Dmsg2(200, "Found job record: JobId=%d Job=%s",
116 strcpy(mdb->errmsg, "Job record not found.\n");
119 Dmsg1(200, "Return job stat=%d\n", stat);
125 * Get the number of pool records
127 * Returns: -1 on failure
130 int db_get_num_pool_records(B_DB *mdb)
135 stat = mdb->control.PoolId;
141 * This function returns a list of all the Pool record ids.
142 * The caller must free ids if non-NULL.
144 * Returns 0: on failure
147 int db_get_pool_ids(B_DB *mdb, int *num_ids, uint32_t *ids[])
156 if (!bdb_open_pools_file(mdb)) {
160 fseek(mdb->poolfd, 0L, SEEK_SET); /* rewind file */
161 /* Linear search through Pool records
164 *num_ids = mdb->control.PoolId;
165 id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
166 while (fread(&opr, len, 1, mdb->poolfd) > 0) {
167 id[i++] = opr.PoolId;
177 * If the PoolId is non-zero, we get its record,
178 * otherwise, we search on the PoolName
180 * Returns: 0 on failure
183 int db_get_pool_record(B_DB *mdb, POOL_DBR *pr)
192 Dmsg0(200, "Open pools\n");
193 if (!bdb_open_pools_file(mdb)) {
197 fseek(mdb->poolfd, 0L, SEEK_SET); /* rewind file */
199 /* Linear search through Pool records
202 while (fread(&opr, len, 1, mdb->poolfd) > 0) {
203 /* If id not zero, search by Id */
204 if (pr->PoolId != 0) {
205 if (pr->PoolId == opr.PoolId) {
209 } else if (strcmp(pr->Name, opr.Name) == 0) {
211 Dmsg1(200, "Found pool: %s\n", opr.Name);
214 rec_addr = ftell(mdb->poolfd); /* save start next record */
217 /* Found desired record, now return it */
218 memcpy(pr, &opr, len);
219 pr->rec_addr = rec_addr;
221 Dmsg3(200, "Found pool record: PoolId=%d Name=%s PoolType=%s\n",
222 opr.PoolId, opr.Name, opr.PoolType);
226 strcpy(mdb->errmsg, "Pool record not found.\n");
229 Dmsg1(200, "Return pool stat=%d\n", stat);
234 * Get the number of Media records
236 * Returns: -1 on failure
239 int db_get_num_media_records(B_DB *mdb)
244 stat = mdb->control.MediaId;
250 * This function returns a list of all the Media record ids.
251 * The caller must free ids if non-NULL.
253 * Returns 0: on failure
256 int db_get_media_ids(B_DB *mdb, int *num_ids, uint32_t *ids[])
265 if (!bdb_open_media_file(mdb)) {
269 fseek(mdb->mediafd, 0L, SEEK_SET); /* rewind file */
270 /* Linear search through Pool records
273 *num_ids = mdb->control.MediaId;
274 id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
275 while (fread(&omr, len, 1, mdb->mediafd) > 0) {
276 id[i++] = omr.MediaId;
285 * If the MediaId is non-zero, we get its record,
286 * otherwise, we search on the MediaName
288 * Returns: 0 on failure
291 int db_get_media_record(B_DB *mdb, MEDIA_DBR *mr)
300 if (!bdb_open_media_file(mdb)) {
304 fseek(mdb->mediafd, 0L, SEEK_SET); /* rewind file */
306 /* Linear search through Media records
309 while (fread(&omr, len, 1, mdb->mediafd) > 0) {
310 if (omr.MediaId == 0) {
311 continue; /* deleted record */
313 Dmsg1(200, "VolName=%s\n", omr.VolumeName);
314 /* If id not zero, search by Id */
315 if (mr->MediaId != 0) {
316 Dmsg1(200, "MediaId=%d\n", mr->MediaId);
317 if (mr->MediaId == omr.MediaId) {
321 } else if (strcmp(mr->VolumeName, omr.VolumeName) == 0) {
325 rec_addr = ftell(mdb->mediafd); /* save start next record */
328 /* Found desired record, now return it */
329 memcpy(mr, &omr, len);
330 mr->rec_addr = rec_addr;
332 Dmsg3(200, "Found media record: MediaId=%d Name=%s MediaType=%s\n",
333 omr.MediaId, omr.VolumeName, mr->MediaType);
337 strcpy(mdb->errmsg, "Could not find requested Media record.\n");
344 * Find VolumeNames for a give JobId
345 * Returns: 0 on error or no Volumes found
346 * number of volumes on success
347 * Volumes are concatenated in VolumeNames
348 * separated by a vertical bar (|).
350 int db_get_job_volume_names(B_DB *mdb, uint32_t JobId, char *VolumeNames)
358 if (!bdb_open_jobmedia_file(mdb)) {
362 if (!bdb_open_media_file(mdb)) {
369 fseek(mdb->jobmediafd, 0L, SEEK_SET); /* rewind the file */
370 while (fread(&jm, jmlen, 1, mdb->jobmediafd) > 0) {
371 if (jm.JobId == JobId) {
372 /* Now look up VolumeName in Media file given MediaId */
373 fseek(mdb->mediafd, 0L, SEEK_SET);
374 while (fread(&mr, mrlen, 1, mdb->mediafd) > 0) {
375 if (jm.MediaId == mr.MediaId) {
376 if (*VolumeNames != 0) { /* if not first name, */
377 strcat(VolumeNames, "|"); /* add separator */
379 strcat(VolumeNames, mr.VolumeName); /* add Volume Name */
386 strcpy(mdb->errmsg, "No Volumes found.\n");
394 * If the ClientId is non-zero, we get its record,
395 * otherwise, we search on the Name
397 * Returns: 0 on failure
400 int db_get_client_record(B_DB *mdb, CLIENT_DBR *cr)
407 if (!bdb_open_client_file(mdb)) {
411 fseek(mdb->clientfd, 0L, SEEK_SET); /* rewind file */
413 * Linear search through Client records
416 while (fread(&lcr, len, 1, mdb->clientfd)) {
417 /* If id not zero, search by Id */
418 if (cr->ClientId != 0) {
419 if (cr->ClientId != lcr.ClientId) {
423 } else if (strcmp(cr->Name, lcr.Name) != 0) {
424 continue; /* not found */
426 memcpy(cr, &lcr, len);
428 Dmsg2(200, "Found Client record: ClientId=%d Name=%s\n",
429 lcr.ClientId, lcr.Name);
433 strcpy(mdb->errmsg, "Client record not found.\n");
440 * Get FileSet Record (We read the FILESET_DBR structure)
441 * If the FileSetId is non-zero, we get its record,
442 * otherwise, we search on the FileSet (its name).
444 * Returns: 0 on failure
447 int db_get_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
453 if (!bdb_open_fileset_file(mdb)) {
457 fseek(mdb->filesetfd, 0L, SEEK_SET); /* rewind file */
459 * Linear search through FileSet records
461 while (fread(&lfsr, sizeof(lfsr), 1, mdb->filesetfd) > 0) {
462 /* If id not zero, search by Id */
463 if (fsr->FileSetId != 0) {
464 if (fsr->FileSetId != lfsr.FileSetId) {
467 /* Search by Name & MD5 */
468 } else if (strcmp(fsr->FileSet, lfsr.FileSet) != 0 ||
469 strcmp(fsr->MD5, lfsr.MD5) != 0) {
470 continue; /* not found */
472 /* Found desired record, now return it */
473 memcpy(fsr, &lfsr, sizeof(lfsr));
474 stat = fsr->FileSetId;
475 Dmsg2(200, "Found FileSet record: FileSetId=%d FileSet=%s\n",
476 lfsr.FileSetId, lfsr.FileSet);
480 strcpy(mdb->errmsg, "FileSet record not found.\n");
488 int db_get_file_attributes_record(B_DB *mdb, char *fname, FILE_DBR *fdbr)
493 #endif /* HAVE_BACULA_DB */