]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_update.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[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(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
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 start of Job
86  *
87  *  Returns: false on failure
88  *           true  on success
89  */
90 bool
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 JobStatus='%c',Level='%c',StartTime='%s',"
107 "ClientId=%u,JobTDate=%s WHERE JobId=%u",
108       (char)(jcr->JobStatus),
109       (char)(jr->JobLevel), dt, jr->ClientId, edit_uint64(JobTDate, ed1), jr->JobId);
110
111    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
112    mdb->changes = 0;
113    db_unlock(mdb);
114    return stat;
115 }
116
117 /*
118  * Given an incoming integer, set the string buffer to either NULL or the value
119  *
120  *
121  */
122 void edit_num_or_null(char *s, size_t n, uint32_t id) {
123         bsnprintf(s, n, id ? "%u" : "NULL", id);
124 }
125
126
127 /*
128  * Update the Job record at end of Job
129  *
130  *  Returns: 0 on failure
131  *           1 on success
132  */
133 int
134 db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
135 {
136    char dt[MAX_TIME_LENGTH];
137    time_t ttime;
138    struct tm tm;
139    int stat;
140    char ed1[30], ed2[30];
141    btime_t JobTDate;
142    char PoolId    [50];
143    char FileSetId [50];
144    char ClientId  [50];
145
146
147    /* some values are set to zero, which translates to NULL in SQL */
148    edit_num_or_null(PoolId,    sizeof(PoolId),    jr->PoolId);
149    edit_num_or_null(FileSetId, sizeof(FileSetId), jr->FileSetId);
150    edit_num_or_null(ClientId,  sizeof(ClientId),  jr->ClientId);
151
152    ttime = jr->EndTime;
153    localtime_r(&ttime, &tm);
154    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
155    JobTDate = ttime;
156
157    db_lock(mdb);
158    Mmsg(mdb->cmd,
159       "UPDATE Job SET JobStatus='%c', EndTime='%s', "
160 "ClientId=%s, JobBytes=%s, JobFiles=%u, JobErrors=%u, VolSessionId=%u, "
161 "VolSessionTime=%u, PoolId=%s, FileSetId=%s, JobTDate=%s WHERE JobId=%u",
162       (char)(jr->JobStatus), dt, ClientId, edit_uint64(jr->JobBytes, ed1),
163       jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime,
164       PoolId, FileSetId, edit_uint64(JobTDate, ed2), jr->JobId);
165
166    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
167    db_unlock(mdb);
168    return stat;
169 }
170
171
172 /*
173  * Update Client record
174  *   Returns: 0 on failure
175  *            1 on success
176  */
177 int
178 db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
179 {
180    int stat;
181    char ed1[50], ed2[50];
182    CLIENT_DBR tcr;
183
184    db_lock(mdb);
185    memcpy(&tcr, cr, sizeof(tcr));
186    if (!db_create_client_record(jcr, mdb, &tcr)) {
187       db_unlock(mdb);
188       return 0;
189    }
190
191    Mmsg(mdb->cmd,
192 "UPDATE Client SET AutoPrune=%d,FileRetention=%s,JobRetention=%s,"
193 "Uname='%s' WHERE Name='%s'",
194       cr->AutoPrune,
195       edit_uint64(cr->FileRetention, ed1),
196       edit_uint64(cr->JobRetention, ed2),
197       cr->Uname, cr->Name);
198
199    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
200    db_unlock(mdb);
201    return stat;
202 }
203
204
205 /*
206  * Update Counters record
207  *   Returns: 0 on failure
208  *            1 on success
209  */
210 int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
211 {
212    db_lock(mdb);
213
214    Mmsg(mdb->cmd,
215 "UPDATE Counters SET MinValue=%d,MaxValue=%d,CurrentValue=%d,"
216 "WrapCounter='%s' WHERE Counter='%s'",
217       cr->MinValue, cr->MaxValue, cr->CurrentValue,
218       cr->WrapCounter, cr->Counter);
219
220    int stat = UPDATE_DB(jcr, mdb, mdb->cmd);
221    db_unlock(mdb);
222    return stat;
223 }
224
225
226 int
227 db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
228 {
229    int stat;
230    char ed1[50], ed2[50], ed3[50];
231
232    db_lock(mdb);
233    Mmsg(mdb->cmd,
234 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d,"
235 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
236 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
237 "AutoPrune=%d,LabelFormat='%s' WHERE PoolId=%u",
238       pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
239       pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
240       edit_uint64(pr->VolUseDuration, ed2),
241       pr->MaxVolJobs, pr->MaxVolFiles,
242       edit_uint64(pr->MaxVolBytes, ed3),
243       pr->Recycle, pr->AutoPrune,
244       pr->LabelFormat, pr->PoolId);
245
246    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
247    db_unlock(mdb);
248    return stat;
249 }
250
251 /*
252  * Update the Media Record at end of Session
253  *
254  * Returns: 0 on failure
255  *          numrows on success
256  */
257 int
258 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
259 {
260    char dt[MAX_TIME_LENGTH];
261    time_t ttime;
262    struct tm tm;
263    int stat;
264    char ed1[30], ed2[30], ed3[30], ed4[30];
265
266
267    Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
268    db_lock(mdb);
269    if (mr->VolJobs == 1) {
270       Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
271       ttime = mr->FirstWritten;
272       localtime_r(&ttime, &tm);
273       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
274       Mmsg(mdb->cmd, "UPDATE Media SET FirstWritten='%s'"
275 " WHERE VolumeName='%s'", dt, mr->VolumeName);
276       stat = UPDATE_DB(jcr, mdb, mdb->cmd);
277       Dmsg1(400, "Firstwritten stat=%d\n", stat);
278    }
279
280    /* Label just done? */
281    if (mr->VolBytes == 1) {
282       ttime = mr->LabelDate;
283       if (ttime == 0) {
284          ttime = time(NULL);
285       }
286       localtime_r(&ttime, &tm);
287       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
288       Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
289            "WHERE VolumeName='%s'", dt, mr->VolumeName);
290       stat = UPDATE_DB(jcr, mdb, mdb->cmd);
291    }
292
293    if (mr->LastWritten != 0) {
294       ttime = mr->LastWritten;
295       localtime_r(&ttime, &tm);
296       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
297
298       Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
299            "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
300            "VolWrites=%u,MaxVolBytes=%s,LastWritten='%s',VolStatus='%s',"
301            "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s "
302            " WHERE VolumeName='%s'",
303            mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
304            mr->VolMounts, mr->VolErrors, mr->VolWrites,
305            edit_uint64(mr->MaxVolBytes, ed2), dt,
306            mr->VolStatus, mr->Slot, mr->InChanger,
307            edit_uint64(mr->VolReadTime, ed3),
308            edit_uint64(mr->VolWriteTime, ed4),
309            mr->VolumeName);
310    } else {
311       Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
312            "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
313            "VolWrites=%u,MaxVolBytes=%s,VolStatus='%s',"
314            "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s "
315            " WHERE VolumeName='%s'",
316            mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
317            mr->VolMounts, mr->VolErrors, mr->VolWrites,
318            edit_uint64(mr->MaxVolBytes, ed2),
319            mr->VolStatus, mr->Slot, mr->InChanger,
320            edit_uint64(mr->VolReadTime, ed3),
321            edit_uint64(mr->VolWriteTime, ed4),
322            mr->VolumeName);
323    }
324
325    Dmsg1(400, "%s\n", mdb->cmd);
326
327    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
328
329    /* Make sure InChanger is 0 for any record having the same Slot */
330    db_make_inchanger_unique(jcr, mdb, mr);
331
332    db_unlock(mdb);
333    return stat;
334 }
335
336 /*
337  * Update the Media Record Default values from Pool
338  *
339  * Returns: 0 on failure
340  *          numrows on success
341  */
342 int
343 db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
344 {
345    int stat;
346    char ed1[30], ed2[30], ed3[30];
347
348
349    db_lock(mdb);
350    if (mr->VolumeName[0]) {
351       Mmsg(mdb->cmd, "UPDATE Media SET "
352            "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
353            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
354            " WHERE VolumeName='%s'",
355            mr->Recycle,edit_uint64(mr->VolRetention, ed1),
356            edit_uint64(mr->VolUseDuration, ed2),
357            mr->MaxVolJobs, mr->MaxVolFiles,
358            edit_uint64(mr->VolBytes, ed3),
359            mr->VolumeName);
360    } else {
361       Mmsg(mdb->cmd, "UPDATE Media SET "
362            "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
363            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
364            " WHERE PoolId=%u",
365            mr->Recycle,edit_uint64(mr->VolRetention, ed1),
366            edit_uint64(mr->VolUseDuration, ed2),
367            mr->MaxVolJobs, mr->MaxVolFiles,
368            edit_uint64(mr->VolBytes, ed3),
369            mr->PoolId);
370    }
371
372    Dmsg1(400, "%s\n", mdb->cmd);
373
374    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
375
376    db_unlock(mdb);
377    return stat;
378 }
379
380
381 /*
382  * If we have a non-zero InChanger, ensure that no other Media
383  *  record has InChanger set on the same Slot.
384  *
385  * This routine assumes the database is already locked.
386  */
387 void
388 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
389 {
390    if (mr->InChanger != 0 && mr->Slot != 0) {
391       Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0 WHERE "
392            "Slot=%d AND PoolId=%u AND MediaId!=%u",
393             mr->Slot, mr->PoolId, mr->MediaId);
394       Dmsg1(400, "%s\n", mdb->cmd);
395       UPDATE_DB(jcr, mdb, mdb->cmd);
396    }
397 }
398
399 #else
400
401 void
402 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
403 {
404   /* DUMMY func for Bacula_DB */
405   return;
406 }
407
408 #endif /* HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/