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 * Kern Sibbald, March 2000
13 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* *****FIXME**** fix fixed length of select_cmd[] and insert_cmd[] */
34 /* The following is necessary so that we do not include
35 * the dummy external definition of DB.
37 #define __SQL_C /* indicate that this is sql.c */
42 #if HAVE_MYSQL || HAVE_SQLITE
44 /* -----------------------------------------------------------------------
46 * Generic Routines (or almost generic)
48 * -----------------------------------------------------------------------
51 /* Forward referenced functions */
52 static int db_get_file_record(B_DB *mdb, FILE_DBR *fdbr);
53 static int db_get_filename_record(B_DB *mdb, char *fname);
54 static int db_get_path_record(B_DB *mdb, char *path);
57 /* Imported subroutines */
58 extern void print_result(B_DB *mdb);
59 extern int QueryDB(char *file, int line, B_DB *db, char *select_cmd);
63 * Given a full filename (with path), look up the File record
64 * (with attributes) in the database.
66 * Returns: 0 on failure
67 * 1 on success with the File record in FILE_DBR
69 int db_get_file_attributes_record(B_DB *mdb, char *fname, FILE_DBR *fdbr)
75 char spath[MAXSTRING];
77 Dmsg1(20, "Enter get_file_from_catalog fname=%s \n", fname);
79 /* Find path without the filename.
80 * I.e. everything after the last / is a "filename".
81 * OK, maybe it is a directory name, but we treat it like
82 * a filename. If we don't find a / then the whole name
83 * must be a path name (e.g. c:).
85 for (p=l=fname; *p; p++) {
90 if (*l == '/') { /* did we find a slash? */
91 l++; /* yes, point to filename */
92 } else { /* no, whole thing must be path name */
96 /* If filename doesn't exist (i.e. root directory), we
97 * simply create a blank name consisting of a single
98 * space. This makes handling zero length filenames
103 Jmsg1(mdb->jcr, M_WARNING, 0, _("Filename truncated to 255 chars: %s\n"), l);
107 strncpy(file, l, fnl); /* copy filename */
110 file[0] = ' '; /* blank filename */
117 Jmsg1(mdb->jcr, M_WARNING, 0, _("Path name truncated to 255 chars: %s\n"), fname);
120 strncpy(spath, fname, pnl);
124 Mmsg1(&mdb->errmsg, _("Path length is zero. File=%s\n"), fname);
125 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
131 Dmsg1(100, "spath=%s\n", spath);
132 Dmsg1(100, "file=%s\n", file);
134 db_escape_string(buf, file, fnl);
135 fdbr->FilenameId = db_get_filename_record(mdb, buf);
136 Dmsg2(100, "db_get_filename_record FilenameId=%u file=%s\n", fdbr->FilenameId, buf);
138 db_escape_string(buf, spath, pnl);
139 fdbr->PathId = db_get_path_record(mdb, buf);
140 Dmsg2(100, "db_get_path_record PathId=%u path=%s\n", fdbr->PathId, buf);
142 stat = db_get_file_record(mdb, fdbr);
150 * Returns: 0 on failure
153 * DO NOT use Jmsg in this routine.
155 * Note in this routine, we do not use Jmsg because it may be
156 * called to get attributes of a non-existent file, which is
157 * "normal" if a new file is found during Verify.
160 int db_get_file_record(B_DB *mdb, FILE_DBR *fdbr)
167 "SELECT FileId, LStat, MD5 from File where File.JobId=%u and File.PathId=%u and \
168 File.FilenameId=%u", fdbr->JobId, fdbr->PathId, fdbr->FilenameId);
170 Dmsg3(050, "Get_file_record JobId=%u FilenameId=%u PathId=%u\n",
171 fdbr->JobId, fdbr->FilenameId, fdbr->PathId);
173 Dmsg1(100, "Query=%s\n", mdb->cmd);
175 if (QUERY_DB(mdb, mdb->cmd)) {
177 mdb->num_rows = sql_num_rows(mdb);
178 Dmsg1(050, "get_file_record num_rows=%d\n", (int)mdb->num_rows);
180 if (mdb->num_rows > 1) {
181 Mmsg1(&mdb->errmsg, _("get_file_record want 1 got rows=%d\n"),
184 if (mdb->num_rows >= 1) {
185 if ((row = sql_fetch_row(mdb)) == NULL) {
186 Mmsg1(&mdb->errmsg, _("Error fetching row: %s\n"), sql_strerror(mdb));
188 fdbr->FileId = (FileId_t)strtod(row[0], NULL);
189 strncpy(fdbr->LStat, row[1], sizeof(fdbr->LStat));
190 fdbr->LStat[sizeof(fdbr->LStat)] = 0;
191 strncpy(fdbr->MD5, row[2], sizeof(fdbr->MD5));
192 fdbr->MD5[sizeof(fdbr->MD5)] = 0;
196 sql_free_result(mdb);
203 /* Get Filename record
204 * Returns: 0 on failure
205 * FilenameId on success
207 * DO NOT use Jmsg in this routine (see notes for get_file_record)
209 static int db_get_filename_record(B_DB *mdb, char *fname)
215 Mmsg0(&mdb->errmsg, _("Null name given to db_get_filename_record\n"));
216 Emsg0(M_ABORT, 0, mdb->errmsg);
220 Mmsg(&mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", fname);
221 if (QUERY_DB(mdb, mdb->cmd)) {
223 mdb->num_rows = sql_num_rows(mdb);
225 if (mdb->num_rows > 1) {
226 Mmsg1(&mdb->errmsg, _("More than one Filename!: %d\n"), (int)(mdb->num_rows));
228 if (mdb->num_rows >= 1) {
229 if ((row = sql_fetch_row(mdb)) == NULL) {
230 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
232 FilenameId = atoi(row[0]);
233 if (FilenameId <= 0) {
234 Mmsg2(&mdb->errmsg, _("Get DB Filename record %s found bad record: %d\n"),
235 mdb->cmd, FilenameId);
240 sql_free_result(mdb);
247 * Returns: 0 on failure
250 * DO NOT use Jmsg in this routine (see notes for get_file_record)
252 static int db_get_path_record(B_DB *mdb, char *path)
258 Emsg0(M_ABORT, 0, _("Null path given to db_get_path_record\n"));
263 if (mdb->cached_path_id != 0 && strcmp(mdb->cached_path, path) == 0) {
265 return mdb->cached_path_id;
268 Mmsg(&mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", path);
270 if (QUERY_DB(mdb, mdb->cmd)) {
272 mdb->num_rows = sql_num_rows(mdb);
274 if (mdb->num_rows > 1) {
275 Mmsg1(&mdb->errmsg, _("More than one Path!: %s\n"),
276 edit_uint64(mdb->num_rows, ed1));
277 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
278 } else if (mdb->num_rows == 1) {
279 if ((row = sql_fetch_row(mdb)) == NULL) {
280 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
282 PathId = atoi(row[0]);
284 Mmsg2(&mdb->errmsg, _("Get DB path record %s found bad record: %u\n"),
289 if (PathId != mdb->cached_path_id) {
290 mdb->cached_path_id = PathId;
291 mdb->cached_path = check_pool_memory_size(mdb->cached_path,
293 strcpy(mdb->cached_path, path);
298 sql_free_result(mdb);
306 * Get Job record for given JobId or Job name
307 * Returns: 0 on failure
310 int db_get_job_record(B_DB *mdb, JOB_DBR *jr)
315 if (jr->JobId == 0) {
316 Mmsg(&mdb->cmd, "SELECT VolSessionId,VolSessionTime,\
317 PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,\
319 FROM Job WHERE Job='%s'", jr->Job);
321 Mmsg(&mdb->cmd, "SELECT VolSessionId,VolSessionTime,\
322 PoolId,StartTime,EndTime,JobFiles,JobBytes,JobTDate,Job,JobStatus,\
324 FROM Job WHERE JobId=%u", jr->JobId);
327 if (!QUERY_DB(mdb, mdb->cmd)) {
329 return 0; /* failed */
331 if ((row = sql_fetch_row(mdb)) == NULL) {
332 Mmsg1(&mdb->errmsg, _("No Job found for JobId %u\n"), jr->JobId);
333 sql_free_result(mdb);
335 return 0; /* failed */
338 jr->VolSessionId = atol(row[0]);
339 jr->VolSessionTime = atol(row[1]);
340 jr->PoolId = atoi(row[2]);
341 strcpy(jr->cStartTime, row[3]);
342 strcpy(jr->cEndTime, row[4]);
343 jr->JobFiles = atol(row[5]);
344 jr->JobBytes = (uint64_t)strtod(row[6], NULL);
345 jr->JobTDate = (btime_t)strtod(row[7], NULL);
346 strcpy(jr->Job, row[8]);
347 jr->JobStatus = (int)*row[9];
348 jr->Type = (int)*row[10];
349 jr->Level = (int)*row[11];
350 sql_free_result(mdb);
357 * Find VolumeNames for a give JobId
358 * Returns: 0 on error or no Volumes found
359 * number of volumes on success
360 * Volumes are concatenated in VolumeNames
361 * separated by a vertical bar (|).
363 * Returns: number of volumes on success
365 int db_get_job_volume_names(B_DB *mdb, uint32_t JobId, POOLMEM **VolumeNames)
373 "SELECT VolumeName FROM JobMedia,Media WHERE JobMedia.JobId=%u \
374 AND JobMedia.MediaId=Media.MediaId", JobId);
376 Dmsg1(130, "VolNam=%s\n", mdb->cmd);
378 if (QUERY_DB(mdb, mdb->cmd)) {
379 mdb->num_rows = sql_num_rows(mdb);
380 Dmsg1(130, "Num rows=%d\n", mdb->num_rows);
381 if (mdb->num_rows <= 0) {
382 Mmsg1(&mdb->errmsg, _("No volumes found for JobId=%d\n"), JobId);
385 stat = mdb->num_rows;
386 for (i=0; i < stat; i++) {
387 if ((row = sql_fetch_row(mdb)) == NULL) {
388 Mmsg2(&mdb->errmsg, _("Error fetching row %d: ERR=%s\n"), i, sql_strerror(mdb));
389 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
393 if (*VolumeNames[0] != 0) {
394 pm_strcat(VolumeNames, "|");
396 pm_strcat(VolumeNames, row[0]);
400 sql_free_result(mdb);
408 * Get the number of pool records
410 * Returns: -1 on failure
413 int db_get_num_pool_records(B_DB *mdb)
418 Mmsg(&mdb->cmd, "SELECT count(*) from Pool");
419 stat = get_sql_record_max(mdb);
425 * This function returns a list of all the Pool record ids.
426 * The caller must free ids if non-NULL.
428 * Returns 0: on failure
431 int db_get_pool_ids(B_DB *mdb, int *num_ids, uint32_t *ids[])
440 Mmsg(&mdb->cmd, "SELECT PoolId FROM Pool");
441 if (QUERY_DB(mdb, mdb->cmd)) {
442 *num_ids = sql_num_rows(mdb);
444 id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
445 while ((row = sql_fetch_row(mdb)) != NULL) {
446 id[i++] = (uint32_t)atoi(row[0]);
450 sql_free_result(mdb);
453 Mmsg(&mdb->errmsg, _("Pool id select failed: ERR=%s\n"), sql_strerror(mdb));
454 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
463 * If the PoolId is non-zero, we get its record,
464 * otherwise, we search on the PoolName
466 * Returns: 0 on failure
469 int db_get_pool_record(B_DB *mdb, POOL_DBR *pdbr)
475 if (pdbr->PoolId != 0) { /* find by id */
477 "SELECT PoolId, Name, NumVols, MaxVols, UseOnce, UseCatalog, AcceptAnyVolume, \
478 AutoPrune, Recycle, VolRetention, \
479 PoolType, LabelFormat FROM Pool WHERE Pool.PoolId=%d", pdbr->PoolId);
480 } else { /* find by name */
482 "SELECT PoolId, Name, NumVols, MaxVols, UseOnce, UseCatalog, AcceptAnyVolume, \
483 AutoPrune, Recycle, VolRetention, \
484 PoolType, LabelFormat FROM Pool WHERE Pool.Name='%s'", pdbr->Name);
487 if (QUERY_DB(mdb, mdb->cmd)) {
488 mdb->num_rows = sql_num_rows(mdb);
489 if (mdb->num_rows > 1) {
491 Mmsg1(&mdb->errmsg, _("More than one Pool!: %s\n"),
492 edit_uint64(mdb->num_rows, ed1));
493 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
494 } else if (mdb->num_rows == 1) {
495 if ((row = sql_fetch_row(mdb)) == NULL) {
496 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
497 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
499 pdbr->PoolId = atoi(row[0]);
500 strcpy(pdbr->Name, row[1]);
501 pdbr->NumVols = atoi(row[2]);
502 pdbr->MaxVols = atoi(row[3]);
503 pdbr->UseOnce = atoi(row[4]);
504 pdbr->UseCatalog = atoi(row[5]);
505 pdbr->AcceptAnyVolume = atoi(row[6]);
506 pdbr->AutoPrune = atoi(row[7]);
507 pdbr->Recycle = atoi(row[8]);
508 pdbr->VolRetention = (btime_t)strtod(row[9], NULL);
509 strcpy(pdbr->PoolType, row[10]);
511 strcpy(pdbr->LabelFormat, row[11]);
513 pdbr->LabelFormat[0] = 0;
518 sql_free_result(mdb);
524 /* Get FileSet Record
525 * If the FileSetId is non-zero, we get its record,
526 * otherwise, we search on the name
528 * Returns: 0 on failure
531 int db_get_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
537 if (fsr->FileSetId != 0) { /* find by id */
539 "SELECT FileSetId, FileSet, MD5 FROM FileSet "
540 "WHERE FileSetId=%u", fsr->FileSetId);
541 } else { /* find by name */
543 "SELECT FileSetId, FileSet, MD5 FROM FileSet "
544 "WHERE FileSet='%s'", fsr->FileSet);
547 if (QUERY_DB(mdb, mdb->cmd)) {
548 mdb->num_rows = sql_num_rows(mdb);
549 if (mdb->num_rows > 1) {
551 Mmsg1(&mdb->errmsg, _("Error got %s FileSets but expected only one!\n"),
552 edit_uint64(mdb->num_rows, ed1));
553 sql_data_seek(mdb, mdb->num_rows-1);
555 if ((row = sql_fetch_row(mdb)) == NULL) {
556 Mmsg1(&mdb->errmsg, _("Error: FileSet record \"%s\" not found\n"), fsr->FileSet);
558 fsr->FileSetId = atoi(row[0]);
559 strcpy(fsr->FileSet, row[1]);
560 strcpy(fsr->MD5, row[2]);
561 stat = fsr->FileSetId;
563 sql_free_result(mdb);
571 * Get the number of Media records
573 * Returns: -1 on failure
576 int db_get_num_media_records(B_DB *mdb)
581 Mmsg(&mdb->cmd, "SELECT count(*) from Media");
582 stat = get_sql_record_max(mdb);
589 * This function returns a list of all the Media record ids.
590 * The caller must free ids if non-NULL.
592 * Returns 0: on failure
595 int db_get_media_ids(B_DB *mdb, int *num_ids, uint32_t *ids[])
604 Mmsg(&mdb->cmd, "SELECT MediaId FROM Media");
605 if (QUERY_DB(mdb, mdb->cmd)) {
606 *num_ids = sql_num_rows(mdb);
608 id = (uint32_t *)malloc(*num_ids * sizeof(uint32_t));
609 while ((row = sql_fetch_row(mdb)) != NULL) {
610 id[i++] = (uint32_t)atoi(row[0]);
614 sql_free_result(mdb);
617 Mmsg(&mdb->errmsg, _("Media id select failed: ERR=%s\n"), sql_strerror(mdb));
618 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
628 * Returns: 0 on failure
631 int db_get_media_record(B_DB *mdb, MEDIA_DBR *mr)
637 if (mr->MediaId == 0 && mr->VolumeName[0] == 0) {
638 Mmsg(&mdb->cmd, "SELECT count(*) from Media");
639 mr->MediaId = get_sql_record_max(mdb);
643 if (mr->MediaId != 0) { /* find by id */
644 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
645 VolBytes,VolMounts,VolErrors,VolWrites,VolMaxBytes,VolCapacityBytes,\
646 MediaType,VolStatus,PoolId,VolRetention,Recycle,Slot \
647 FROM Media WHERE MediaId=%d", mr->MediaId);
648 } else { /* find by name */
649 Mmsg(&mdb->cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks,\
650 VolBytes,VolMounts,VolErrors,VolWrites,VolMaxBytes,VolCapacityBytes,\
651 MediaType,VolStatus,PoolId,VolRetention,Recycle,Slot \
652 FROM Media WHERE VolumeName='%s'", mr->VolumeName);
655 if (QUERY_DB(mdb, mdb->cmd)) {
656 mdb->num_rows = sql_num_rows(mdb);
657 if (mdb->num_rows > 1) {
659 Mmsg1(&mdb->errmsg, _("More than one Volume!: %s\n"),
660 edit_uint64(mdb->num_rows, ed1));
661 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
662 } else if (mdb->num_rows == 1) {
663 if ((row = sql_fetch_row(mdb)) == NULL) {
664 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
665 Jmsg(mdb->jcr, M_ERROR, 0, "%s", mdb->errmsg);
668 mr->MediaId = atoi(row[0]);
669 strcpy(mr->VolumeName, row[1]);
670 mr->VolJobs = atoi(row[2]);
671 mr->VolFiles = atoi(row[3]);
672 mr->VolBlocks = atoi(row[4]);
673 mr->VolBytes = (uint64_t)strtod(row[5], NULL);
674 mr->VolMounts = atoi(row[6]);
675 mr->VolErrors = atoi(row[7]);
676 mr->VolWrites = atoi(row[8]);
677 mr->VolMaxBytes = (uint64_t)strtod(row[9], NULL);
678 mr->VolCapacityBytes = (uint64_t)strtod(row[10], NULL);
679 strcpy(mr->MediaType, row[11]);
680 strcpy(mr->VolStatus, row[12]);
681 mr->PoolId = atoi(row[13]);
682 mr->VolRetention = (btime_t)strtod(row[14], NULL);
683 mr->Recycle = atoi(row[15]);
684 mr->Slot = atoi(row[16]);
688 Mmsg0(&mdb->errmsg, _("Media record not found.\n"));
690 sql_free_result(mdb);
697 #endif /* HAVE_MYSQL || HAVE_SQLITE */