]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_update.c
- Reset NumVols in Pool record from database on every update
[bacula/bacula] / bacula / src / cats / sql_update.c
1 /*
2  * Bacula Catalog Database Update record interface routines
3  *
4  *    Kern Sibbald, March 2000
5  *
6  *    Version $Id$
7  */
8
9 /*
10    Copyright (C) 2000-2005 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 /* The following is necessary so that we do not include
30  * the dummy external definition of DB.
31  */
32 #define __SQL_C                       /* indicate that this is sql.c */
33
34 #include "bacula.h"
35 #include "cats.h"
36
37 #if    HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL
38
39 /* -----------------------------------------------------------------------
40  *
41  *   Generic Routines (or almost generic)
42  *
43  * -----------------------------------------------------------------------
44  */
45
46 /* Imported subroutines */
47 extern void print_result(B_DB *mdb);
48 extern int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
49
50 /* -----------------------------------------------------------------------
51  *
52  *   Generic Routines (or almost generic)
53  *
54  * -----------------------------------------------------------------------
55  */
56 /* Update the attributes record by adding the MD5 signature */
57 int
58 db_add_SIG_to_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, char *SIG,
59                           int type)
60 {
61    int stat;
62    char ed1[50];
63
64    db_lock(mdb);
65    Mmsg(mdb->cmd, "UPDATE File SET MD5='%s' WHERE FileId=%s", SIG, 
66       edit_int64(FileId, ed1));
67    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
68    db_unlock(mdb);
69    return stat;
70 }
71
72 /* Mark the file record as being visited during database
73  * verify compare. Stuff JobId into MarkedId field
74  */
75 int db_mark_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, JobId_t JobId)
76 {
77    int stat;
78    char ed1[50], ed2[50];
79
80    db_lock(mdb);
81    Mmsg(mdb->cmd, "UPDATE File SET MarkId=%s WHERE FileId=%s", 
82       edit_int64(JobId, ed1), edit_int64(FileId, ed2));
83    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
84    db_unlock(mdb);
85    return stat;
86 }
87
88 /*
89  * Update the Job record at start of Job
90  *
91  *  Returns: false on failure
92  *           true  on success
93  */
94 bool
95 db_update_job_start_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
96 {
97    char dt[MAX_TIME_LENGTH];
98    time_t stime;
99    struct tm tm;
100    btime_t JobTDate;
101    int stat;
102    char ed1[50], ed2[50], ed3[50];
103
104    stime = jr->StartTime;
105    localtime_r(&stime, &tm);
106    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
107    JobTDate = (btime_t)stime;
108
109    db_lock(mdb);
110    Mmsg(mdb->cmd, "UPDATE Job SET JobStatus='%c',Level='%c',StartTime='%s',"
111 "ClientId=%s,JobTDate=%s WHERE JobId=%s",
112       (char)(jcr->JobStatus),
113       (char)(jr->JobLevel), dt, 
114       edit_int64(jr->ClientId, ed1),
115       edit_uint64(JobTDate, ed2), 
116       edit_int64(jr->JobId, ed3));
117
118    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
119    mdb->changes = 0;
120    db_unlock(mdb);
121    return stat;
122 }
123
124 /*
125  * Given an incoming integer, set the string buffer to either NULL or the value
126  *
127  */
128 static void edit_num_or_null(char *s, size_t n, uint64_t id) {
129    char ed1[50];
130    bsnprintf(s, n, id ? "%s" : "NULL", edit_int64(id, ed1));
131 }
132
133
134 /*
135  * Update the Job record at end of Job
136  *
137  *  Returns: 0 on failure
138  *           1 on success
139  */
140 int
141 db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
142 {
143    char dt[MAX_TIME_LENGTH];
144    time_t ttime;
145    struct tm tm;
146    int stat;
147    char ed1[30], ed2[30], ed3[50];
148    btime_t JobTDate;
149    char PoolId    [50];
150    char FileSetId [50];
151    char ClientId  [50];
152
153
154    /* some values are set to zero, which translates to NULL in SQL */
155    edit_num_or_null(PoolId,    sizeof(PoolId),    jr->PoolId);
156    edit_num_or_null(FileSetId, sizeof(FileSetId), jr->FileSetId);
157    edit_num_or_null(ClientId,  sizeof(ClientId),  jr->ClientId);
158
159    ttime = jr->EndTime;
160    localtime_r(&ttime, &tm);
161    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
162    JobTDate = ttime;
163
164    db_lock(mdb);
165    Mmsg(mdb->cmd,
166       "UPDATE Job SET JobStatus='%c', EndTime='%s', "
167 "ClientId=%s, JobBytes=%s, JobFiles=%u, JobErrors=%u, VolSessionId=%u, "
168 "VolSessionTime=%u, PoolId=%s, FileSetId=%s, JobTDate=%s WHERE JobId=%s",
169       (char)(jr->JobStatus), dt, ClientId, edit_uint64(jr->JobBytes, ed1),
170       jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
171       PoolId, FileSetId, edit_uint64(JobTDate, ed2), 
172       edit_int64(jr->JobId, ed3));
173
174    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
175    db_unlock(mdb);
176    return stat;
177 }
178
179
180 /*
181  * Update Client record
182  *   Returns: 0 on failure
183  *            1 on success
184  */
185 int
186 db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
187 {
188    int stat;
189    char ed1[50], ed2[50];
190    CLIENT_DBR tcr;
191
192    db_lock(mdb);
193    memcpy(&tcr, cr, sizeof(tcr));
194    if (!db_create_client_record(jcr, mdb, &tcr)) {
195       db_unlock(mdb);
196       return 0;
197    }
198
199    Mmsg(mdb->cmd,
200 "UPDATE Client SET AutoPrune=%d,FileRetention=%s,JobRetention=%s,"
201 "Uname='%s' WHERE Name='%s'",
202       cr->AutoPrune,
203       edit_uint64(cr->FileRetention, ed1),
204       edit_uint64(cr->JobRetention, ed2),
205       cr->Uname, cr->Name);
206
207    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
208    db_unlock(mdb);
209    return stat;
210 }
211
212
213 /*
214  * Update Counters record
215  *   Returns: 0 on failure
216  *            1 on success
217  */
218 int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
219 {
220    db_lock(mdb);
221
222    Mmsg(mdb->cmd,
223 "UPDATE Counters SET MinValue=%d,MaxValue=%d,CurrentValue=%d,"
224 "WrapCounter='%s' WHERE Counter='%s'",
225       cr->MinValue, cr->MaxValue, cr->CurrentValue,
226       cr->WrapCounter, cr->Counter);
227
228    int stat = UPDATE_DB(jcr, mdb, mdb->cmd);
229    db_unlock(mdb);
230    return stat;
231 }
232
233
234 int
235 db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
236 {
237    int stat;
238    char ed1[50], ed2[50], ed3[50], ed4[50];
239
240    db_lock(mdb);
241    Mmsg(mdb->cmd, "SELECT count(*) from Pool");
242    pr->NumVols = get_sql_record_max(jcr, mdb);
243
244    Mmsg(mdb->cmd,
245 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d,"
246 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
247 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
248 "AutoPrune=%d,LabelType=%d,LabelFormat='%s' WHERE PoolId=%s",
249       pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
250       pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
251       edit_uint64(pr->VolUseDuration, ed2),
252       pr->MaxVolJobs, pr->MaxVolFiles,
253       edit_uint64(pr->MaxVolBytes, ed3),
254       pr->Recycle, pr->AutoPrune, pr->LabelType,
255       pr->LabelFormat, 
256       edit_int64(pr->PoolId, ed4));
257
258    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
259    db_unlock(mdb);
260    return stat;
261 }
262
263 bool
264 db_update_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
265 {
266    int stat;
267    char ed1[50];
268
269    db_lock(mdb);
270    Mmsg(mdb->cmd, "UPDATE Storage SET AutoChanger=%d WHERE StorageId=%s", 
271       sr->AutoChanger, edit_int64(sr->StorageId, ed1));
272
273    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
274    db_unlock(mdb);
275    return stat;
276 }
277
278
279 /*
280  * Update the Media Record at end of Session
281  *
282  * Returns: 0 on failure
283  *          numrows on success
284  */
285 int
286 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
287 {
288    char dt[MAX_TIME_LENGTH];
289    time_t ttime;
290    struct tm tm;
291    int stat;
292    char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50];
293
294
295    Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
296    db_lock(mdb);
297    if (mr->set_first_written) {
298       Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
299       ttime = mr->FirstWritten;
300       localtime_r(&ttime, &tm);
301       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
302       Mmsg(mdb->cmd, "UPDATE Media SET FirstWritten='%s'"
303            " WHERE VolumeName='%s'", dt, mr->VolumeName);
304       stat = UPDATE_DB(jcr, mdb, mdb->cmd);
305       Dmsg1(400, "Firstwritten=%d\n", mr->FirstWritten);
306    }
307
308    /* Label just done? */
309    if (mr->set_label_date) {
310       ttime = mr->LabelDate;
311       if (ttime == 0) {
312          ttime = time(NULL);
313       }
314       localtime_r(&ttime, &tm);
315       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
316       Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
317            "WHERE VolumeName='%s'", dt, mr->VolumeName);
318       UPDATE_DB(jcr, mdb, mdb->cmd);
319    }
320
321    if (mr->LastWritten != 0) {
322       ttime = mr->LastWritten;
323       localtime_r(&ttime, &tm);
324       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
325       Mmsg(mdb->cmd, "UPDATE Media Set LastWritten='%s' "
326            "WHERE VolumeName='%s'", dt, mr->VolumeName);
327       UPDATE_DB(jcr, mdb, mdb->cmd);
328    }
329
330    Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
331         "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
332         "VolWrites=%u,MaxVolBytes=%s,VolStatus='%s',"
333         "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s,VolParts=%d,"
334         "LabelType=%d,StorageId=%s"
335         " WHERE VolumeName='%s'",
336         mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
337         mr->VolMounts, mr->VolErrors, mr->VolWrites,
338         edit_uint64(mr->MaxVolBytes, ed2),
339         mr->VolStatus, mr->Slot, mr->InChanger,
340         edit_uint64(mr->VolReadTime, ed3),
341         edit_uint64(mr->VolWriteTime, ed4),
342         mr->VolParts,
343         mr->LabelType,
344         edit_int64(mr->StorageId, ed5),
345         mr->VolumeName);
346
347    Dmsg1(400, "%s\n", mdb->cmd);
348
349    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
350
351    /* Make sure InChanger is 0 for any record having the same Slot */
352    db_make_inchanger_unique(jcr, mdb, mr);
353
354    db_unlock(mdb);
355    return stat;
356 }
357
358 /*
359  * Update the Media Record Default values from Pool
360  *
361  * Returns: 0 on failure
362  *          numrows on success
363  */
364 int
365 db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
366 {
367    int stat;
368    char ed1[50], ed2[50], ed3[50], ed4[50];
369
370
371    db_lock(mdb);
372    if (mr->VolumeName[0]) {
373       Mmsg(mdb->cmd, "UPDATE Media SET "
374            "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
375            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
376            " WHERE VolumeName='%s'",
377            mr->Recycle,edit_uint64(mr->VolRetention, ed1),
378            edit_uint64(mr->VolUseDuration, ed2),
379            mr->MaxVolJobs, mr->MaxVolFiles,
380            edit_uint64(mr->VolBytes, ed3),
381            mr->VolumeName);
382    } else {
383       Mmsg(mdb->cmd, "UPDATE Media SET "
384            "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
385            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
386            " WHERE PoolId=%s",
387            mr->Recycle,edit_uint64(mr->VolRetention, ed1),
388            edit_uint64(mr->VolUseDuration, ed2),
389            mr->MaxVolJobs, mr->MaxVolFiles,
390            edit_uint64(mr->VolBytes, ed3),
391            edit_int64(mr->PoolId, ed4));
392    }
393
394    Dmsg1(400, "%s\n", mdb->cmd);
395
396    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
397
398    db_unlock(mdb);
399    return stat;
400 }
401
402
403 /*
404  * If we have a non-zero InChanger, ensure that no other Media
405  *  record has InChanger set on the same Slot.
406  *
407  * This routine assumes the database is already locked.
408  */
409 void
410 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
411 {
412    char ed1[50], ed2[50];
413    if (mr->InChanger != 0 && mr->Slot != 0) {
414       Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0 WHERE "
415            "Slot=%d AND StorageId=%s AND MediaId!=%s",
416             mr->Slot, 
417             edit_int64(mr->StorageId, ed1), edit_int64(mr->MediaId, ed2));
418       Dmsg1(400, "%s\n", mdb->cmd);
419       UPDATE_DB(jcr, mdb, mdb->cmd);
420    }
421 }
422
423 #else
424
425 void
426 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
427 {
428   /* DUMMY func for Bacula_DB */
429   return;
430 }
431
432 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/