]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/bdb_list.c
1.19 24Apr02
[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
14 /*
15    Copyright (C) 2001, 2002 Kern Sibbald and John Walker
16
17    This program is free software; you can redistribute it and/or
18    modify it under the terms of the GNU General Public License as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public
28    License along with this program; if not, write to the Free
29    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30    MA 02111-1307, USA.
31
32  */
33
34
35 /* The following is necessary so that we do not include
36  * the dummy external definition of DB.
37  */
38 #define __SQL_C                       /* indicate that this is sql.c */
39
40 #include "bacula.h"
41 #include "cats.h"
42 #include "bdb.h"
43
44 #ifdef HAVE_BACULA_DB
45
46 /* Forward referenced functions */
47
48 /* -----------------------------------------------------------------------
49  *
50  *   Bacula specific defines and subroutines
51  *
52  * -----------------------------------------------------------------------
53  */
54
55 /* 
56  * Submit general SQL query
57  */
58 int db_list_sql_query(B_DB *mdb, char *query, DB_LIST_HANDLER *sendit, void *ctx)
59 {
60    sendit(ctx, "SQL Queries not implemented with internal database.\n");
61    return 0;
62 }
63
64
65 /*
66  * List all the pool records
67  */
68 void db_list_pool_records(B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx)
69 {
70    int len;
71    POOL_DBR pr;
72
73    Dmsg0(90, "Enter list_pool_records\n");
74    P(mdb->mutex);
75    if (!bdb_open_pools_file(mdb)) {
76       V(mdb->mutex);
77       return;
78    }
79    sendit(ctx, "  PoolId NumVols MaxVols  Type       PoolName\n");
80    sendit(ctx, "===================================================\n");
81    fseek(mdb->poolfd, 0L, SEEK_SET);   /* rewind file */
82    len = sizeof(pr);
83    while (fread(&pr, len, 1, mdb->poolfd) > 0) {
84          Mmsg(&mdb->cmd, " %7d  %6d  %6d  %-10s %s\n",
85             pr.PoolId, pr.NumVols, pr.MaxVols, pr.PoolType, pr.Name);
86          sendit(ctx, mdb->cmd);
87    }
88    sendit(ctx, "===================================================\n");
89    V(mdb->mutex);
90    Dmsg0(90, "Leave list_pool_records\n");
91    return;
92 }
93
94
95 /*
96  * List Media records
97  */
98 void db_list_media_records(B_DB *mdb, MEDIA_DBR *mdbr, DB_LIST_HANDLER *sendit, void *ctx)
99 {
100    char ewc[30];
101    int len;
102    MEDIA_DBR mr;
103
104    P(mdb->mutex);
105    if (!bdb_open_media_file(mdb)) {
106       V(mdb->mutex);
107       return;
108    }
109    sendit(ctx, "  Status           VolBytes  MediaType        VolumeName\n");
110    sendit(ctx, "=============================================================\n");
111    fseek(mdb->mediafd, 0L, SEEK_SET);   /* rewind file */
112    len = sizeof(mr);
113    while (fread(&mr, len, 1, mdb->mediafd) > 0) {
114          Mmsg(&mdb->cmd, " %-10s %17s %-15s  %s\n",
115             mr.VolStatus, edit_uint_with_commas(mr.VolBytes, ewc),
116             mr.MediaType, mr.VolumeName);
117          sendit(ctx, mdb->cmd);
118    }
119    sendit(ctx, "====================================================================\n");
120    V(mdb->mutex);
121    return;
122 }
123
124 void db_list_jobmedia_records(B_DB *mdb, uint32_t JobId, DB_LIST_HANDLER *sendit, void *ctx)
125 {
126    JOBMEDIA_DBR jm;
127    MEDIA_DBR mr;
128    int jmlen, mrlen;
129
130    P(mdb->mutex);
131    if (!bdb_open_jobmedia_file(mdb)) {
132       V(mdb->mutex);
133       return;
134    }
135    if (!bdb_open_media_file(mdb)) {
136       V(mdb->mutex);
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    V(mdb->mutex);
175    return;
176 }
177
178
179 /*
180  * List Job records
181  */
182 void db_list_job_records(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
183 {
184    int jrlen;
185    JOB_DBR ojr;
186    int done = 0;
187    char ewc1[30], ewc2[30];
188    char dt[MAX_TIME_LENGTH];
189    struct tm tm;
190
191    P(mdb->mutex);
192    if (!bdb_open_jobs_file(mdb)) {
193       V(mdb->mutex);
194       return;
195    }
196    fseek(mdb->jobfd, 0L, SEEK_SET);   /* rewind file */
197    /* 
198     * Linear search through Job records
199     */
200    sendit(ctx, "   JobId   StartTime   Type Level         Bytes      Files Stat JobName\n");
201    sendit(ctx, "==========================================================================\n");
202    jrlen = sizeof(ojr);
203    while (!done && fread(&ojr, jrlen, 1, mdb->jobfd) > 0) {
204       if (jr->JobId != 0) {
205          if (jr->JobId == ojr.JobId) {
206             done = 1;
207          } else {
208             continue;
209          }
210       }
211       localtime_r(&ojr.StartTime, &tm);
212       strftime(dt, sizeof(dt), "%m-%d %H:%M", &tm);
213       Mmsg(&mdb->cmd, " %7d  %-10s   %c    %c   %14s %10s  %c  %s\n", 
214                 ojr.JobId, dt, (char)ojr.Type, (char)ojr.Level, 
215                 edit_uint_with_commas(ojr.JobBytes, ewc1), 
216                 edit_uint_with_commas(ojr.JobFiles, ewc2),
217                 (char)ojr.JobStatus, ojr.Name);
218       sendit(ctx, mdb->cmd);
219    }
220    sendit(ctx, "============================================================================\n");
221    V(mdb->mutex);
222    return;
223 }
224
225
226 /*
227  * List Job Totals
228  */
229 void db_list_job_totals(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
230 {
231    char ewc1[30], ewc2[30], ewc3[30];
232    int jrlen;
233    JOB_DBR ojr;
234    uint64_t total_bytes = 0;
235    uint64_t total_files = 0;
236    uint32_t total_jobs = 0;
237
238    P(mdb->mutex);
239    if (!bdb_open_jobs_file(mdb)) {
240       V(mdb->mutex);
241       return;
242    }
243    fseek(mdb->jobfd, 0L, SEEK_SET);   /* rewind file */
244    /* 
245     * Linear search through JobStart records
246     */
247    sendit(ctx, "   NumJobs   NumFiles          NumBytes\n");
248    sendit(ctx, "=======================================\n");
249    jrlen = sizeof(ojr);
250    while (fread(&ojr, jrlen, 1, mdb->jobfd) > 0) {
251       total_files += ojr.JobFiles;
252       total_bytes += ojr.JobBytes;
253       total_jobs++;
254    }
255    Mmsg(&mdb->cmd, " %7s  %10s   %15s\n", 
256              edit_uint_with_commas(total_jobs, ewc1),
257              edit_uint_with_commas(total_files, ewc2), 
258              edit_uint_with_commas(total_bytes, ewc3));
259    sendit(ctx, mdb->cmd);
260    sendit(ctx, "=======================================\n");
261    V(mdb->mutex);
262    return;
263 }
264
265
266
267 void db_list_files_for_job(B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx) {}
268
269 #endif /* HAVE_BACULA_DB */