]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/bdb_list.c
ebl fix escape bug
[bacula/bacula] / bacula / src / cats / bdb_list.c
1 /*
2  * Bacula Catalog Database List records interface routines
3  *
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
9  *  system.
10  *
11  *    Kern Sibbald, January MMI
12  *
13  *    Version $Id$
14  */
15 /*
16    Bacula® - The Network Backup Solution
17
18    Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
19
20    The main author of Bacula is Kern Sibbald, with contributions from
21    many others, a complete list can be found in the file AUTHORS.
22    This program is Free Software; you can redistribute it and/or
23    modify it under the terms of version two of the GNU General Public
24    License as published by the Free Software Foundation plus additions
25    that are listed in the file LICENSE.
26
27    This program is distributed in the hope that it will be useful, but
28    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.
31
32    You should have received a copy of the GNU General Public License
33    along with this program; if not, write to the Free Software
34    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35    02110-1301, USA.
36
37    Bacula® is a registered trademark of John Walker.
38    The licensor of Bacula is the Free Software Foundation Europe
39    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
40    Switzerland, email:ftf@fsfeurope.org.
41 */
42
43
44 /* The following is necessary so that we do not include
45  * the dummy external definition of DB.
46  */
47 #define __SQL_C                       /* indicate that this is sql.c */
48
49 #include "bacula.h"
50 #include "cats.h"
51 #include "bdb.h"
52
53 #ifdef HAVE_BACULA_DB
54
55 /* Forward referenced functions */
56
57 /* -----------------------------------------------------------------------
58  *
59  *   Bacula specific defines and subroutines
60  *
61  * -----------------------------------------------------------------------
62  */
63
64 /*
65  * Submit general SQL query
66  */
67 int db_list_sql_query(JCR *jcr, B_DB *mdb, const char *query, DB_LIST_HANDLER *sendit,
68                       void *ctx, int verbose)
69 {
70    sendit(ctx, "SQL Queries not implemented with internal database.\n");
71    return 0;
72 }
73
74
75 /*
76  * List all the pool records
77  */
78 void db_list_pool_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx)
79 {
80    int len;
81    POOL_DBR pr;
82
83    Dmsg0(90, "Enter list_pool_records\n");
84    db_lock(mdb);
85    if (!bdb_open_pools_file(mdb)) {
86       db_unlock(mdb);
87       return;
88    }
89    sendit(ctx, "  PoolId NumVols MaxVols  Type       PoolName\n");
90    sendit(ctx, "===================================================\n");
91    fseek(mdb->poolfd, 0L, SEEK_SET);   /* rewind file */
92    len = sizeof(pr);
93    while (fread(&pr, len, 1, mdb->poolfd) > 0) {
94          Mmsg(mdb->cmd, " %7d  %6d  %6d  %-10s %s\n",
95             pr.PoolId, pr.NumVols, pr.MaxVols, pr.PoolType, pr.Name);
96          sendit(ctx, mdb->cmd);
97    }
98    sendit(ctx, "===================================================\n");
99    db_unlock(mdb);
100    Dmsg0(90, "Leave list_pool_records\n");
101    return;
102 }
103
104
105 /*
106  * List Media records
107  */
108 void db_list_media_records(JCR *jcr, B_DB *mdb, MEDIA_DBR *mdbr,
109                            DB_LIST_HANDLER *sendit, void *ctx)
110 {
111    char ewc[30];
112    int len;
113    MEDIA_DBR mr;
114
115    db_lock(mdb);
116    if (!bdb_open_media_file(mdb)) {
117       db_unlock(mdb);
118       return;
119    }
120    sendit(ctx, "  Status           VolBytes  MediaType        VolumeName\n");
121    sendit(ctx, "=============================================================\n");
122    fseek(mdb->mediafd, 0L, SEEK_SET);   /* rewind file */
123    len = sizeof(mr);
124    while (fread(&mr, len, 1, mdb->mediafd) > 0) {
125          Mmsg(mdb->cmd, " %-10s %17s %-15s  %s\n",
126             mr.VolStatus, edit_uint64_with_commas(mr.VolBytes, ewc),
127             mr.MediaType, mr.VolumeName);
128          sendit(ctx, mdb->cmd);
129    }
130    sendit(ctx, "====================================================================\n");
131    db_unlock(mdb);
132    return;
133 }
134
135 void db_list_jobmedia_records(JCR *jcr, B_DB *mdb, uint32_t JobId,
136                               DB_LIST_HANDLER *sendit, void *ctx)
137 {
138    JOBMEDIA_DBR jm;
139    MEDIA_DBR mr;
140    int jmlen, mrlen;
141
142    db_lock(mdb);
143    if (!bdb_open_jobmedia_file(mdb)) {
144       db_unlock(mdb);
145       return;
146    }
147    if (!bdb_open_media_file(mdb)) {
148       db_unlock(mdb);
149       return;
150    }
151    sendit(ctx, "    JobId VolumeName    FirstIndex LastIndex\n");
152    sendit(ctx, "============================================\n");
153    jmlen = sizeof(jm);
154    mrlen = sizeof(mr);
155    fseek(mdb->jobmediafd, 0L, SEEK_SET); /* rewind the file */
156    while (fread(&jm, jmlen, 1, mdb->jobmediafd) > 0) {
157       /* List by JobId */
158       if (JobId != 0) {
159          if (jm.JobId == JobId) {
160             /* Now find VolumeName in corresponding Media record */
161             fseek(mdb->mediafd, 0L, SEEK_SET);
162             while (fread(&mr, mrlen, 1, mdb->mediafd) > 0) {
163                if (mr.MediaId == jm.MediaId) {
164                   Mmsg(mdb->cmd, " %7d  %-10s %10d %10d\n",
165                        jm.JobId, mr.VolumeName, jm.FirstIndex, jm.LastIndex);
166                   sendit(ctx, mdb->cmd);
167                   break;
168                }
169             }
170          }
171       } else {
172          /* List all records */
173          fseek(mdb->mediafd, 0L, SEEK_SET);
174          while (fread(&mr, mrlen, 1, mdb->mediafd) > 0) {
175             if (mr.MediaId == jm.MediaId) {
176                Mmsg(mdb->cmd, " %7d  %-10s %10d %10d\n",
177                     jm.JobId, mr.VolumeName, jm.FirstIndex, jm.LastIndex);
178                sendit(ctx, mdb->cmd);
179                break;
180             }
181          }
182       }
183    }
184
185    sendit(ctx, "============================================\n");
186    db_unlock(mdb);
187    return;
188 }
189
190
191 /*
192  * List Job records
193  */
194 void db_list_job_records(JCR *jcr, B_DB *mdb, JOB_DBR *jr,
195                          DB_LIST_HANDLER *sendit, void *ctx)
196 {
197 #ifdef xxx
198    int jrlen;
199    JOB_DBR ojr;
200    int done = 0;
201    char ewc1[30], ewc2[30];
202    char dt[MAX_TIME_LENGTH];
203    struct tm tm;
204
205    db_lock(mdb);
206    if (!bdb_open_jobs_file(mdb)) {
207       db_unlock(mdb);
208       return;
209    }
210    fseek(mdb->jobfd, 0L, SEEK_SET);   /* rewind file */
211    /*
212     * Linear search through Job records
213     */
214    sendit(ctx, "   JobId   StartTime   Type Level         Bytes      Files Stat JobName\n");
215    sendit(ctx, "==========================================================================\n");
216    jrlen = sizeof(ojr);
217    while (!done && fread(&ojr, jrlen, 1, mdb->jobfd) > 0) {
218       if (jr->JobId != 0) {
219          if (jr->JobId == ojr.JobId) {
220             done = 1;
221          } else {
222             continue;
223          }
224       }
225       localtime_r(&ojr.StartTime, &tm);
226       strftime(dt, sizeof(dt), "%m-%d %H:%M", &tm);
227       Mmsg(mdb->cmd, " %7d  %-10s   %c    %c   %14s %10s  %c  %s\n",
228                 ojr.JobId, dt, (char)ojr.JobType, (char)ojr.JobLevel,
229                 edit_uint64_with_commas(ojr.JobBytes, ewc1),
230                 edit_uint64_with_commas(ojr.JobFiles, ewc2),
231                 (char)ojr.JobStatus, ojr.Name);
232       sendit(ctx, mdb->cmd);
233    }
234    sendit(ctx, "============================================================================\n");
235    db_unlock(mdb);
236 #endif
237    return;
238 }
239
240
241 /*
242  * List Job Totals
243  */
244 void db_list_job_totals(JCR *jcr, B_DB *mdb, JOB_DBR *jr,
245                         DB_LIST_HANDLER *sendit, void *ctx)
246 {
247    char ewc1[30], ewc2[30], ewc3[30];
248    int jrlen;
249    JOB_DBR ojr;
250    uint64_t total_bytes = 0;
251    uint64_t total_files = 0;
252    uint32_t total_jobs = 0;
253
254    db_lock(mdb);
255    if (!bdb_open_jobs_file(mdb)) {
256       db_unlock(mdb);
257       return;
258    }
259    fseek(mdb->jobfd, 0L, SEEK_SET);   /* rewind file */
260    /*
261     * Linear search through JobStart records
262     */
263    sendit(ctx, "   NumJobs   NumFiles          NumBytes\n");
264    sendit(ctx, "=======================================\n");
265    jrlen = sizeof(ojr);
266    while (fread(&ojr, jrlen, 1, mdb->jobfd) > 0) {
267       total_files += ojr.JobFiles;
268       total_bytes += ojr.JobBytes;
269       total_jobs++;
270    }
271    Mmsg(mdb->cmd, " %7s  %10s   %15s\n",
272              edit_uint64_with_commas(total_jobs, ewc1),
273              edit_uint64_with_commas(total_files, ewc2),
274              edit_uint64_with_commas(total_bytes, ewc3));
275    sendit(ctx, mdb->cmd);
276    sendit(ctx, "=======================================\n");
277    db_unlock(mdb);
278    return;
279 }
280
281
282
283 void db_list_files_for_job(JCR *jcr, B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx)
284 { }
285
286 void db_list_client_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx)
287 { }
288
289 int db_list_sql_query(JCR *jcr, B_DB *mdb, const char *query, DB_LIST_HANDLER *sendit,
290                       void *ctx, int verbose, e_list_type type)
291 {
292    return 0;
293 }
294
295 void
296 db_list_pool_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
297 { }
298
299 void
300 db_list_media_records(JCR *jcr, B_DB *mdb, MEDIA_DBR *mdbr,
301                       DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
302 { }
303
304 void db_list_jobmedia_records(JCR *jcr, B_DB *mdb, uint32_t JobId,
305                               DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
306 { }
307
308 void
309 db_list_job_records(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit,
310                     void *ctx, e_list_type type)
311 { }
312
313 void
314 db_list_client_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
315 { }
316
317
318
319
320 #endif /* HAVE_BACULA_DB */