]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/bdb_list.c
Replace explicit checks for "/" with calls to IsPathSeparator, strchr with first_path...
[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 /*
17    Copyright (C) 2001-2006 Kern Sibbald
18
19    This program is free software; you can redistribute it and/or
20    modify it under the terms of the GNU General Public License
21    version 2 as amended with additional clauses defined in the
22    file LICENSE in the main source directory.
23
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 
27    the file LICENSE for additional details.
28
29  */
30
31
32 /* The following is necessary so that we do not include
33  * the dummy external definition of DB.
34  */
35 #define __SQL_C                       /* indicate that this is sql.c */
36
37 #include "bacula.h"
38 #include "cats.h"
39 #include "bdb.h"
40
41 #ifdef HAVE_BACULA_DB
42
43 /* Forward referenced functions */
44
45 /* -----------------------------------------------------------------------
46  *
47  *   Bacula specific defines and subroutines
48  *
49  * -----------------------------------------------------------------------
50  */
51
52 /*
53  * Submit general SQL query
54  */
55 int db_list_sql_query(JCR *jcr, B_DB *mdb, const char *query, DB_LIST_HANDLER *sendit,
56                       void *ctx, int verbose)
57 {
58    sendit(ctx, "SQL Queries not implemented with internal database.\n");
59    return 0;
60 }
61
62
63 /*
64  * List all the pool records
65  */
66 void db_list_pool_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx)
67 {
68    int len;
69    POOL_DBR pr;
70
71    Dmsg0(90, "Enter list_pool_records\n");
72    db_lock(mdb);
73    if (!bdb_open_pools_file(mdb)) {
74       db_unlock(mdb);
75       return;
76    }
77    sendit(ctx, "  PoolId NumVols MaxVols  Type       PoolName\n");
78    sendit(ctx, "===================================================\n");
79    fseek(mdb->poolfd, 0L, SEEK_SET);   /* rewind file */
80    len = sizeof(pr);
81    while (fread(&pr, len, 1, mdb->poolfd) > 0) {
82          Mmsg(mdb->cmd, " %7d  %6d  %6d  %-10s %s\n",
83             pr.PoolId, pr.NumVols, pr.MaxVols, pr.PoolType, pr.Name);
84          sendit(ctx, mdb->cmd);
85    }
86    sendit(ctx, "===================================================\n");
87    db_unlock(mdb);
88    Dmsg0(90, "Leave list_pool_records\n");
89    return;
90 }
91
92
93 /*
94  * List Media records
95  */
96 void db_list_media_records(JCR *jcr, B_DB *mdb, MEDIA_DBR *mdbr,
97                            DB_LIST_HANDLER *sendit, void *ctx)
98 {
99    char ewc[30];
100    int len;
101    MEDIA_DBR mr;
102
103    db_lock(mdb);
104    if (!bdb_open_media_file(mdb)) {
105       db_unlock(mdb);
106       return;
107    }
108    sendit(ctx, "  Status           VolBytes  MediaType        VolumeName\n");
109    sendit(ctx, "=============================================================\n");
110    fseek(mdb->mediafd, 0L, SEEK_SET);   /* rewind file */
111    len = sizeof(mr);
112    while (fread(&mr, len, 1, mdb->mediafd) > 0) {
113          Mmsg(mdb->cmd, " %-10s %17s %-15s  %s\n",
114             mr.VolStatus, edit_uint64_with_commas(mr.VolBytes, ewc),
115             mr.MediaType, mr.VolumeName);
116          sendit(ctx, mdb->cmd);
117    }
118    sendit(ctx, "====================================================================\n");
119    db_unlock(mdb);
120    return;
121 }
122
123 void db_list_jobmedia_records(JCR *jcr, B_DB *mdb, uint32_t JobId,
124                               DB_LIST_HANDLER *sendit, void *ctx)
125 {
126    JOBMEDIA_DBR jm;
127    MEDIA_DBR mr;
128    int jmlen, mrlen;
129
130    db_lock(mdb);
131    if (!bdb_open_jobmedia_file(mdb)) {
132       db_unlock(mdb);
133       return;
134    }
135    if (!bdb_open_media_file(mdb)) {
136       db_unlock(mdb);
137       return;
138    }
139    sendit(ctx, "    JobId VolumeName    FirstIndex LastIndex\n");
140    sendit(ctx, "============================================\n");
141    jmlen = sizeof(jm);
142    mrlen = sizeof(mr);
143    fseek(mdb->jobmediafd, 0L, SEEK_SET); /* rewind the file */
144    while (fread(&jm, jmlen, 1, mdb->jobmediafd) > 0) {
145       /* List by JobId */
146       if (JobId != 0) {
147          if (jm.JobId == JobId) {
148             /* Now find VolumeName in corresponding Media record */
149             fseek(mdb->mediafd, 0L, SEEK_SET);
150             while (fread(&mr, mrlen, 1, mdb->mediafd) > 0) {
151                if (mr.MediaId == jm.MediaId) {
152                   Mmsg(mdb->cmd, " %7d  %-10s %10d %10d\n",
153                        jm.JobId, mr.VolumeName, jm.FirstIndex, jm.LastIndex);
154                   sendit(ctx, mdb->cmd);
155                   break;
156                }
157             }
158          }
159       } else {
160          /* List all records */
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    }
172
173    sendit(ctx, "============================================\n");
174    db_unlock(mdb);
175    return;
176 }
177
178
179 /*
180  * List Job records
181  */
182 void db_list_job_records(JCR *jcr, B_DB *mdb, JOB_DBR *jr,
183                          DB_LIST_HANDLER *sendit, void *ctx)
184 {
185 #ifdef xxx
186    int jrlen;
187    JOB_DBR ojr;
188    int done = 0;
189    char ewc1[30], ewc2[30];
190    char dt[MAX_TIME_LENGTH];
191    struct tm tm;
192
193    db_lock(mdb);
194    if (!bdb_open_jobs_file(mdb)) {
195       db_unlock(mdb);
196       return;
197    }
198    fseek(mdb->jobfd, 0L, SEEK_SET);   /* rewind file */
199    /*
200     * Linear search through Job records
201     */
202    sendit(ctx, "   JobId   StartTime   Type Level         Bytes      Files Stat JobName\n");
203    sendit(ctx, "==========================================================================\n");
204    jrlen = sizeof(ojr);
205    while (!done && fread(&ojr, jrlen, 1, mdb->jobfd) > 0) {
206       if (jr->JobId != 0) {
207          if (jr->JobId == ojr.JobId) {
208             done = 1;
209          } else {
210             continue;
211          }
212       }
213       localtime_r(&ojr.StartTime, &tm);
214       strftime(dt, sizeof(dt), "%m-%d %H:%M", &tm);
215       Mmsg(mdb->cmd, " %7d  %-10s   %c    %c   %14s %10s  %c  %s\n",
216                 ojr.JobId, dt, (char)ojr.JobType, (char)ojr.JobLevel,
217                 edit_uint64_with_commas(ojr.JobBytes, ewc1),
218                 edit_uint64_with_commas(ojr.JobFiles, ewc2),
219                 (char)ojr.JobStatus, ojr.Name);
220       sendit(ctx, mdb->cmd);
221    }
222    sendit(ctx, "============================================================================\n");
223    db_unlock(mdb);
224 #endif
225    return;
226 }
227
228
229 /*
230  * List Job Totals
231  */
232 void db_list_job_totals(JCR *jcr, B_DB *mdb, JOB_DBR *jr,
233                         DB_LIST_HANDLER *sendit, void *ctx)
234 {
235    char ewc1[30], ewc2[30], ewc3[30];
236    int jrlen;
237    JOB_DBR ojr;
238    uint64_t total_bytes = 0;
239    uint64_t total_files = 0;
240    uint32_t total_jobs = 0;
241
242    db_lock(mdb);
243    if (!bdb_open_jobs_file(mdb)) {
244       db_unlock(mdb);
245       return;
246    }
247    fseek(mdb->jobfd, 0L, SEEK_SET);   /* rewind file */
248    /*
249     * Linear search through JobStart records
250     */
251    sendit(ctx, "   NumJobs   NumFiles          NumBytes\n");
252    sendit(ctx, "=======================================\n");
253    jrlen = sizeof(ojr);
254    while (fread(&ojr, jrlen, 1, mdb->jobfd) > 0) {
255       total_files += ojr.JobFiles;
256       total_bytes += ojr.JobBytes;
257       total_jobs++;
258    }
259    Mmsg(mdb->cmd, " %7s  %10s   %15s\n",
260              edit_uint64_with_commas(total_jobs, ewc1),
261              edit_uint64_with_commas(total_files, ewc2),
262              edit_uint64_with_commas(total_bytes, ewc3));
263    sendit(ctx, mdb->cmd);
264    sendit(ctx, "=======================================\n");
265    db_unlock(mdb);
266    return;
267 }
268
269
270
271 void db_list_files_for_job(JCR *jcr, B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx)
272 { }
273
274 void db_list_client_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx)
275 { }
276
277 int db_list_sql_query(JCR *jcr, B_DB *mdb, const char *query, DB_LIST_HANDLER *sendit,
278                       void *ctx, int verbose, e_list_type type)
279 {
280    return 0;
281 }
282
283 void
284 db_list_pool_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
285 { }
286
287 void
288 db_list_media_records(JCR *jcr, B_DB *mdb, MEDIA_DBR *mdbr,
289                       DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
290 { }
291
292 void db_list_jobmedia_records(JCR *jcr, B_DB *mdb, uint32_t JobId,
293                               DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
294 { }
295
296 void
297 db_list_job_records(JCR *jcr, B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit,
298                     void *ctx, e_list_type type)
299 { }
300
301 void
302 db_list_client_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
303 { }
304
305
306
307
308 #endif /* HAVE_BACULA_DB */