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