2 * Bacula Catalog Database List records interface routines
4 * Bacula Catalog Database routines written specifically
5 * for Bacula. Note, these routines are VERY dumb and
6 * do not provide all the functionality of an SQL database.
7 * The purpose of these routines is to ensure that Bacula
8 * can limp along if no real database is loaded on the
11 * Kern Sibbald, January MMI
17 Copyright (C) 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
37 /* The following is necessary so that we do not include
38 * the dummy external definition of DB.
40 #define __SQL_C /* indicate that this is sql.c */
48 /* Forward referenced functions */
50 /* -----------------------------------------------------------------------
52 * Bacula specific defines and subroutines
54 * -----------------------------------------------------------------------
58 * Submit general SQL query
60 int db_list_sql_query(B_DB *mdb, char *query, DB_LIST_HANDLER *sendit, void *ctx,
63 sendit(ctx, "SQL Queries not implemented with internal database.\n");
69 * List all the pool records
71 void db_list_pool_records(B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx)
76 Dmsg0(90, "Enter list_pool_records\n");
78 if (!bdb_open_pools_file(mdb)) {
82 sendit(ctx, " PoolId NumVols MaxVols Type PoolName\n");
83 sendit(ctx, "===================================================\n");
84 fseek(mdb->poolfd, 0L, SEEK_SET); /* rewind file */
86 while (fread(&pr, len, 1, mdb->poolfd) > 0) {
87 Mmsg(&mdb->cmd, " %7d %6d %6d %-10s %s\n",
88 pr.PoolId, pr.NumVols, pr.MaxVols, pr.PoolType, pr.Name);
89 sendit(ctx, mdb->cmd);
91 sendit(ctx, "===================================================\n");
93 Dmsg0(90, "Leave list_pool_records\n");
101 void db_list_media_records(B_DB *mdb, MEDIA_DBR *mdbr, DB_LIST_HANDLER *sendit, void *ctx)
108 if (!bdb_open_media_file(mdb)) {
112 sendit(ctx, " Status VolBytes MediaType VolumeName\n");
113 sendit(ctx, "=============================================================\n");
114 fseek(mdb->mediafd, 0L, SEEK_SET); /* rewind file */
116 while (fread(&mr, len, 1, mdb->mediafd) > 0) {
117 Mmsg(&mdb->cmd, " %-10s %17s %-15s %s\n",
118 mr.VolStatus, edit_uint64_with_commas(mr.VolBytes, ewc),
119 mr.MediaType, mr.VolumeName);
120 sendit(ctx, mdb->cmd);
122 sendit(ctx, "====================================================================\n");
127 void db_list_jobmedia_records(B_DB *mdb, uint32_t JobId, DB_LIST_HANDLER *sendit, void *ctx)
134 if (!bdb_open_jobmedia_file(mdb)) {
138 if (!bdb_open_media_file(mdb)) {
142 sendit(ctx, " JobId VolumeName FirstIndex LastIndex\n");
143 sendit(ctx, "============================================\n");
146 fseek(mdb->jobmediafd, 0L, SEEK_SET); /* rewind the file */
147 while (fread(&jm, jmlen, 1, mdb->jobmediafd) > 0) {
150 if (jm.JobId == JobId) {
151 /* Now find VolumeName in corresponding Media record */
152 fseek(mdb->mediafd, 0L, SEEK_SET);
153 while (fread(&mr, mrlen, 1, mdb->mediafd) > 0) {
154 if (mr.MediaId == jm.MediaId) {
155 Mmsg(&mdb->cmd, " %7d %-10s %10d %10d\n",
156 jm.JobId, mr.VolumeName, jm.FirstIndex, jm.LastIndex);
157 sendit(ctx, mdb->cmd);
163 /* List all records */
164 fseek(mdb->mediafd, 0L, SEEK_SET);
165 while (fread(&mr, mrlen, 1, mdb->mediafd) > 0) {
166 if (mr.MediaId == jm.MediaId) {
167 Mmsg(&mdb->cmd, " %7d %-10s %10d %10d\n",
168 jm.JobId, mr.VolumeName, jm.FirstIndex, jm.LastIndex);
169 sendit(ctx, mdb->cmd);
176 sendit(ctx, "============================================\n");
185 void db_list_job_records(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
190 char ewc1[30], ewc2[30];
191 char dt[MAX_TIME_LENGTH];
195 if (!bdb_open_jobs_file(mdb)) {
199 fseek(mdb->jobfd, 0L, SEEK_SET); /* rewind file */
201 * Linear search through Job records
203 sendit(ctx, " JobId StartTime Type Level Bytes Files Stat JobName\n");
204 sendit(ctx, "==========================================================================\n");
206 while (!done && fread(&ojr, jrlen, 1, mdb->jobfd) > 0) {
207 if (jr->JobId != 0) {
208 if (jr->JobId == ojr.JobId) {
214 localtime_r(&ojr.StartTime, &tm);
215 strftime(dt, sizeof(dt), "%m-%d %H:%M", &tm);
216 Mmsg(&mdb->cmd, " %7d %-10s %c %c %14s %10s %c %s\n",
217 ojr.JobId, dt, (char)ojr.Type, (char)ojr.Level,
218 edit_uint64_with_commas(ojr.JobBytes, ewc1),
219 edit_uint64_with_commas(ojr.JobFiles, ewc2),
220 (char)ojr.JobStatus, ojr.Name);
221 sendit(ctx, mdb->cmd);
223 sendit(ctx, "============================================================================\n");
232 void db_list_job_totals(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
234 char ewc1[30], ewc2[30], ewc3[30];
237 uint64_t total_bytes = 0;
238 uint64_t total_files = 0;
239 uint32_t total_jobs = 0;
242 if (!bdb_open_jobs_file(mdb)) {
246 fseek(mdb->jobfd, 0L, SEEK_SET); /* rewind file */
248 * Linear search through JobStart records
250 sendit(ctx, " NumJobs NumFiles NumBytes\n");
251 sendit(ctx, "=======================================\n");
253 while (fread(&ojr, jrlen, 1, mdb->jobfd) > 0) {
254 total_files += ojr.JobFiles;
255 total_bytes += ojr.JobBytes;
258 Mmsg(&mdb->cmd, " %7s %10s %15s\n",
259 edit_uint64_with_commas(total_jobs, ewc1),
260 edit_uint64_with_commas(total_files, ewc2),
261 edit_uint64_with_commas(total_bytes, ewc3));
262 sendit(ctx, mdb->cmd);
263 sendit(ctx, "=======================================\n");
270 void db_list_files_for_job(B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx) {}
272 #endif /* HAVE_BACULA_DB */