]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/bdb_list.c
Remove old code from bdb.c
[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, 2002 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(B_DB *mdb, char *query, DB_LIST_HANDLER *sendit, void *ctx)
61 {
62    sendit(ctx, "SQL Queries not implemented with internal database.\n");
63    return 0;
64 }
65
66
67 /*
68  * List all the pool records
69  */
70 void db_list_pool_records(B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx)
71 {
72    int len;
73    POOL_DBR pr;
74
75    Dmsg0(90, "Enter list_pool_records\n");
76    db_lock(mdb);
77    if (!bdb_open_pools_file(mdb)) {
78       db_unlock(mdb);
79       return;
80    }
81    sendit(ctx, "  PoolId NumVols MaxVols  Type       PoolName\n");
82    sendit(ctx, "===================================================\n");
83    fseek(mdb->poolfd, 0L, SEEK_SET);   /* rewind file */
84    len = sizeof(pr);
85    while (fread(&pr, len, 1, mdb->poolfd) > 0) {
86          Mmsg(&mdb->cmd, " %7d  %6d  %6d  %-10s %s\n",
87             pr.PoolId, pr.NumVols, pr.MaxVols, pr.PoolType, pr.Name);
88          sendit(ctx, mdb->cmd);
89    }
90    sendit(ctx, "===================================================\n");
91    db_unlock(mdb);
92    Dmsg0(90, "Leave list_pool_records\n");
93    return;
94 }
95
96
97 /*
98  * List Media records
99  */
100 void db_list_media_records(B_DB *mdb, MEDIA_DBR *mdbr, DB_LIST_HANDLER *sendit, void *ctx)
101 {
102    char ewc[30];
103    int len;
104    MEDIA_DBR mr;
105
106    db_lock(mdb);
107    if (!bdb_open_media_file(mdb)) {
108       db_unlock(mdb);
109       return;
110    }
111    sendit(ctx, "  Status           VolBytes  MediaType        VolumeName\n");
112    sendit(ctx, "=============================================================\n");
113    fseek(mdb->mediafd, 0L, SEEK_SET);   /* rewind file */
114    len = sizeof(mr);
115    while (fread(&mr, len, 1, mdb->mediafd) > 0) {
116          Mmsg(&mdb->cmd, " %-10s %17s %-15s  %s\n",
117             mr.VolStatus, edit_uint64_with_commas(mr.VolBytes, ewc),
118             mr.MediaType, mr.VolumeName);
119          sendit(ctx, mdb->cmd);
120    }
121    sendit(ctx, "====================================================================\n");
122    db_unlock(mdb);
123    return;
124 }
125
126 void db_list_jobmedia_records(B_DB *mdb, uint32_t JobId, DB_LIST_HANDLER *sendit, void *ctx)
127 {
128    JOBMEDIA_DBR jm;
129    MEDIA_DBR mr;
130    int jmlen, mrlen;
131
132    db_lock(mdb);
133    if (!bdb_open_jobmedia_file(mdb)) {
134       db_unlock(mdb);
135       return;
136    }
137    if (!bdb_open_media_file(mdb)) {
138       db_unlock(mdb);
139       return;
140    }
141    sendit(ctx, "    JobId VolumeName    FirstIndex LastIndex\n");
142    sendit(ctx, "============================================\n");
143    jmlen = sizeof(jm);
144    mrlen = sizeof(mr);
145    fseek(mdb->jobmediafd, 0L, SEEK_SET); /* rewind the file */
146    while (fread(&jm, jmlen, 1, mdb->jobmediafd) > 0) {
147       /* List by JobId */
148       if (JobId != 0) {
149          if (jm.JobId == JobId) {
150             /* Now find VolumeName in corresponding Media record */
151             fseek(mdb->mediafd, 0L, SEEK_SET);
152             while (fread(&mr, mrlen, 1, mdb->mediafd) > 0) {
153                if (mr.MediaId == jm.MediaId) {
154                   Mmsg(&mdb->cmd, " %7d  %-10s %10d %10d\n",
155                        jm.JobId, mr.VolumeName, jm.FirstIndex, jm.LastIndex);
156                   sendit(ctx, mdb->cmd);
157                   break;
158                }
159             }
160          }
161       } else {
162          /* List all records */
163          fseek(mdb->mediafd, 0L, SEEK_SET);
164          while (fread(&mr, mrlen, 1, mdb->mediafd) > 0) {
165             if (mr.MediaId == jm.MediaId) {
166                Mmsg(&mdb->cmd, " %7d  %-10s %10d %10d\n",
167                     jm.JobId, mr.VolumeName, jm.FirstIndex, jm.LastIndex);
168                sendit(ctx, mdb->cmd);
169                break;
170             }
171          }
172       }
173    }
174
175    sendit(ctx, "============================================\n");
176    db_unlock(mdb);
177    return;
178 }
179
180
181 /*
182  * List Job records
183  */
184 void db_list_job_records(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
185 {
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.Type, (char)ojr.Level, 
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    return;
225 }
226
227
228 /*
229  * List Job Totals
230  */
231 void db_list_job_totals(B_DB *mdb, JOB_DBR *jr, DB_LIST_HANDLER *sendit, void *ctx)
232 {
233    char ewc1[30], ewc2[30], ewc3[30];
234    int jrlen;
235    JOB_DBR ojr;
236    uint64_t total_bytes = 0;
237    uint64_t total_files = 0;
238    uint32_t total_jobs = 0;
239
240    db_lock(mdb);
241    if (!bdb_open_jobs_file(mdb)) {
242       db_unlock(mdb);
243       return;
244    }
245    fseek(mdb->jobfd, 0L, SEEK_SET);   /* rewind file */
246    /* 
247     * Linear search through JobStart records
248     */
249    sendit(ctx, "   NumJobs   NumFiles          NumBytes\n");
250    sendit(ctx, "=======================================\n");
251    jrlen = sizeof(ojr);
252    while (fread(&ojr, jrlen, 1, mdb->jobfd) > 0) {
253       total_files += ojr.JobFiles;
254       total_bytes += ojr.JobBytes;
255       total_jobs++;
256    }
257    Mmsg(&mdb->cmd, " %7s  %10s   %15s\n", 
258              edit_uint64_with_commas(total_jobs, ewc1),
259              edit_uint64_with_commas(total_files, ewc2), 
260              edit_uint64_with_commas(total_bytes, ewc3));
261    sendit(ctx, mdb->cmd);
262    sendit(ctx, "=======================================\n");
263    db_unlock(mdb);
264    return;
265 }
266
267
268
269 void db_list_files_for_job(B_DB *mdb, uint32_t jobid, DB_LIST_HANDLER *sendit, void *ctx) {}
270
271 #endif /* HAVE_BACULA_DB */