]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_update.c
Fix uninitialized stack variable + seg fault in label + PoolId in InChanger clear
[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-2004 Kern Sibbald and John Walker
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_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(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
63    db_lock(mdb);
64    Mmsg(&mdb->cmd, "UPDATE File SET MD5='%s' WHERE FileId=%u", SIG, FileId);
65    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
66    db_unlock(mdb);
67    return stat;
68 }
69
70 /* Mark the file record as being visited during database
71  * verify compare. Stuff JobId into MarkedId field
72  */
73 int db_mark_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, JobId_t JobId) 
74 {
75    int stat;
76
77    db_lock(mdb);
78    Mmsg(&mdb->cmd, "UPDATE File SET MarkId=%u WHERE FileId=%u", JobId, FileId);
79    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
80    db_unlock(mdb);
81    return stat;
82 }
83
84 /*
85  * Update the Job record at end of Job
86  *
87  *  Returns: 0 on failure
88  *           1 on success
89  */
90 int
91 db_update_job_start_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
92 {
93    char dt[MAX_TIME_LENGTH];
94    time_t stime;
95    struct tm tm;
96    btime_t JobTDate;
97    int stat;
98    char ed1[30];
99        
100    stime = jr->StartTime;
101    localtime_r(&stime, &tm);
102    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
103    JobTDate = (btime_t)stime;
104
105    db_lock(mdb);
106    Mmsg(&mdb->cmd, "UPDATE Job SET Level='%c', StartTime='%s',"
107 "ClientId=%u, JobTDate=%s WHERE JobId=%u",
108       (char)(jr->Level), dt, jr->ClientId, edit_uint64(JobTDate, ed1), jr->JobId);
109
110    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
111    mdb->changes = 0;
112    db_unlock(mdb);
113    return stat;
114 }
115
116 /*
117  * Given an incoming integer, set the string buffer to either NULL or the value
118  *
119  *
120  */
121 void edit_num_or_null(char *s, size_t n, uint32_t id) {
122         bsnprintf(s, n, id ? "%u" : "NULL", id);
123 }
124
125
126 /*
127  * Update the Job record at end of Job
128  *
129  *  Returns: 0 on failure
130  *           1 on success
131  */
132 int
133 db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
134 {
135    char dt[MAX_TIME_LENGTH];
136    time_t ttime;
137    struct tm tm;
138    int stat;
139    char ed1[30], ed2[30];
140    btime_t JobTDate;
141    char PoolId    [50];
142    char FileSetId [50];
143    char ClientId  [50];
144
145
146    /* some values are set to zero, which translates to NULL in SQL */
147    edit_num_or_null(PoolId,    sizeof(PoolId),    jr->PoolId);
148    edit_num_or_null(FileSetId, sizeof(FileSetId), jr->FileSetId);
149    edit_num_or_null(ClientId,  sizeof(ClientId),  jr->ClientId);
150        
151    ttime = jr->EndTime;
152    localtime_r(&ttime, &tm);
153    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
154    JobTDate = ttime;
155
156    db_lock(mdb);
157    Mmsg(&mdb->cmd,
158       "UPDATE Job SET JobStatus='%c', EndTime='%s', \
159 ClientId=%s, JobBytes=%s, JobFiles=%u, JobErrors=%u, VolSessionId=%u, \
160 VolSessionTime=%u, PoolId=%s, FileSetId=%s, JobTDate=%s WHERE JobId=%u",
161       (char)(jr->JobStatus), dt, ClientId, edit_uint64(jr->JobBytes, ed1), 
162       jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime, 
163       PoolId, FileSetId, edit_uint64(JobTDate, ed2), jr->JobId);
164
165    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
166    db_unlock(mdb);
167    return stat;
168 }
169
170
171 /*
172  * Update Client record 
173  *   Returns: 0 on failure
174  *            1 on success
175  */
176 int
177 db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
178 {
179    int stat;
180    char ed1[50], ed2[50];
181    CLIENT_DBR tcr;
182
183    db_lock(mdb);
184    memcpy(&tcr, cr, sizeof(tcr));
185    if (!db_create_client_record(jcr, mdb, &tcr)) {
186       db_unlock(mdb);
187       return 0;
188    }
189
190    Mmsg(&mdb->cmd,
191 "UPDATE Client SET AutoPrune=%d,FileRetention=%s,JobRetention=%s," 
192 "Uname='%s' WHERE Name='%s'",
193       cr->AutoPrune,
194       edit_uint64(cr->FileRetention, ed1),
195       edit_uint64(cr->JobRetention, ed2),
196       cr->Uname, cr->Name);
197
198    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
199    db_unlock(mdb);
200    return stat;
201 }
202
203
204 /*
205  * Update Counters record
206  *   Returns: 0 on failure
207  *            1 on success
208  */
209 int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
210 {
211    db_lock(mdb);
212
213    Mmsg(&mdb->cmd,
214 "UPDATE Counters SET MinValue=%d,MaxValue=%d,CurrentValue=%d," 
215 "WrapCounter='%s' WHERE Counter='%s'",
216       cr->MinValue, cr->MaxValue, cr->CurrentValue,
217       cr->WrapCounter, cr->Counter);
218
219    int stat = UPDATE_DB(jcr, mdb, mdb->cmd);
220    db_unlock(mdb);
221    return stat;
222 }
223
224
225 int
226 db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
227 {
228    int stat;
229    char ed1[50], ed2[50], ed3[50];
230
231    db_lock(mdb);
232    Mmsg(&mdb->cmd,
233 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d," 
234 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
235 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
236 "AutoPrune=%d,LabelFormat='%s' WHERE PoolId=%u",
237       pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
238       pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
239       edit_uint64(pr->VolUseDuration, ed2),
240       pr->MaxVolJobs, pr->MaxVolFiles,
241       edit_uint64(pr->MaxVolBytes, ed3),
242       pr->Recycle, pr->AutoPrune,
243       pr->LabelFormat, pr->PoolId);
244
245    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
246    db_unlock(mdb);
247    return stat;
248 }
249
250 /* 
251  * Update the Media Record at end of Session
252  *
253  * Returns: 0 on failure
254  *          numrows on success
255  */
256 int
257 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) 
258 {
259    char dt[MAX_TIME_LENGTH];
260    time_t ttime;
261    struct tm tm;
262    int stat;
263    char ed1[30], ed2[30], ed3[30], ed4[30];
264        
265
266    Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
267    db_lock(mdb);
268    if (mr->VolJobs == 1) {
269       Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
270       ttime = mr->FirstWritten;
271       localtime_r(&ttime, &tm);
272       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
273       Mmsg(&mdb->cmd, "UPDATE Media SET FirstWritten='%s'\
274  WHERE VolumeName='%s'", dt, mr->VolumeName);
275       stat = UPDATE_DB(jcr, mdb, mdb->cmd);
276       Dmsg1(400, "Firstwritten stat=%d\n", stat);
277    }
278
279    /* Label just done? */
280    if (mr->VolBytes == 1) {
281       ttime = mr->LabelDate;
282       if (ttime == 0) {
283          ttime = time(NULL);
284       }
285       localtime_r(&ttime, &tm);
286       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
287       Mmsg(&mdb->cmd, "UPDATE Media SET LabelDate='%s' "
288            "WHERE VolumeName='%s'", dt, mr->VolumeName);
289       stat = UPDATE_DB(jcr, mdb, mdb->cmd);
290    }
291    
292    if (mr->LastWritten != 0) {
293       ttime = mr->LastWritten;
294       localtime_r(&ttime, &tm);
295       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
296
297       Mmsg(&mdb->cmd, "UPDATE Media SET VolJobs=%u,"
298            "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
299            "VolWrites=%u,MaxVolBytes=%s,LastWritten='%s',VolStatus='%s',"
300            "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s "
301            " WHERE VolumeName='%s'",
302            mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
303            mr->VolMounts, mr->VolErrors, mr->VolWrites, 
304            edit_uint64(mr->MaxVolBytes, ed2), dt, 
305            mr->VolStatus, mr->Slot, mr->InChanger, 
306            edit_uint64(mr->VolReadTime, ed3),
307            edit_uint64(mr->VolWriteTime, ed4),
308            mr->VolumeName);
309    } else {
310       Mmsg(&mdb->cmd, "UPDATE Media SET VolJobs=%u,"
311            "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
312            "VolWrites=%u,MaxVolBytes=%s,VolStatus='%s',"
313            "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s "
314            " WHERE VolumeName='%s'",
315            mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
316            mr->VolMounts, mr->VolErrors, mr->VolWrites, 
317            edit_uint64(mr->MaxVolBytes, ed2), 
318            mr->VolStatus, mr->Slot, mr->InChanger, 
319            edit_uint64(mr->VolReadTime, ed3),
320            edit_uint64(mr->VolWriteTime, ed4),
321            mr->VolumeName);
322    }
323
324    Dmsg1(400, "%s\n", mdb->cmd);
325
326    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
327
328    /* Make sure InChanger is 0 for any record having the same Slot */
329    db_make_inchanger_unique(jcr, mdb, mr);
330
331    db_unlock(mdb);
332    return stat;
333 }
334
335 /* 
336  * If we have a non-zero InChanger, ensure that no other Media
337  *  record has InChanger set on the same Slot.
338  *
339  * This routine assumes the database is already locked.
340  */
341 void
342 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) 
343 {
344    if (mr->InChanger != 0 && mr->Slot != 0) {
345       Mmsg(&mdb->cmd, "UPDATE Media SET InChanger=0 WHERE "
346            "Slot=%d AND MediaId!=%u", mr->Slot, mr->MediaId);
347       Dmsg1(400, "%s\n", mdb->cmd);
348       UPDATE_DB(jcr, mdb, mdb->cmd);
349    }
350 }
351
352 #endif /* HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/