]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_update.c
Vacation work -- see tech log
[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
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,VolParts=%d,"
302            "LabelType=%d"
303            " WHERE VolumeName='%s'",
304            mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
305            mr->VolMounts, mr->VolErrors, mr->VolWrites,
306            edit_uint64(mr->MaxVolBytes, ed2), dt,
307            mr->VolStatus, mr->Slot, mr->InChanger,
308            edit_uint64(mr->VolReadTime, ed3),
309            edit_uint64(mr->VolWriteTime, ed4),
310            mr->VolParts,
311            mr->LabelType,
312            mr->VolumeName);
313    } else {
314       Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
315            "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
316            "VolWrites=%u,MaxVolBytes=%s,VolStatus='%s',"
317            "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s,VolParts=%d,"
318            "LabelType=%d"
319            " WHERE VolumeName='%s'",
320            mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
321            mr->VolMounts, mr->VolErrors, mr->VolWrites,
322            edit_uint64(mr->MaxVolBytes, ed2),
323            mr->VolStatus, mr->Slot, mr->InChanger,
324            edit_uint64(mr->VolReadTime, ed3),
325            edit_uint64(mr->VolWriteTime, ed4),
326            mr->VolParts,
327            mr->LabelType,
328            mr->VolumeName);
329    }
330
331    Dmsg1(400, "%s\n", mdb->cmd);
332
333    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
334
335    /* Make sure InChanger is 0 for any record having the same Slot */
336    db_make_inchanger_unique(jcr, mdb, mr);
337
338    db_unlock(mdb);
339    return stat;
340 }
341
342 /*
343  * Update the Media Record Default values from Pool
344  *
345  * Returns: 0 on failure
346  *          numrows on success
347  */
348 int
349 db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
350 {
351    int stat;
352    char ed1[30], ed2[30], ed3[30];
353
354
355    db_lock(mdb);
356    if (mr->VolumeName[0]) {
357       Mmsg(mdb->cmd, "UPDATE Media SET "
358            "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
359            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
360            " WHERE VolumeName='%s'",
361            mr->Recycle,edit_uint64(mr->VolRetention, ed1),
362            edit_uint64(mr->VolUseDuration, ed2),
363            mr->MaxVolJobs, mr->MaxVolFiles,
364            edit_uint64(mr->VolBytes, ed3),
365            mr->VolumeName);
366    } else {
367       Mmsg(mdb->cmd, "UPDATE Media SET "
368            "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
369            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
370            " WHERE PoolId=%u",
371            mr->Recycle,edit_uint64(mr->VolRetention, ed1),
372            edit_uint64(mr->VolUseDuration, ed2),
373            mr->MaxVolJobs, mr->MaxVolFiles,
374            edit_uint64(mr->VolBytes, ed3),
375            mr->PoolId);
376    }
377
378    Dmsg1(400, "%s\n", mdb->cmd);
379
380    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
381
382    db_unlock(mdb);
383    return stat;
384 }
385
386
387 /*
388  * If we have a non-zero InChanger, ensure that no other Media
389  *  record has InChanger set on the same Slot.
390  *
391  * This routine assumes the database is already locked.
392  */
393 void
394 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
395 {
396    if (mr->InChanger != 0 && mr->Slot != 0) {
397       Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0 WHERE "
398            "Slot=%d AND PoolId=%u AND MediaId!=%u",
399             mr->Slot, mr->PoolId, mr->MediaId);
400       Dmsg1(400, "%s\n", mdb->cmd);
401       UPDATE_DB(jcr, mdb, mdb->cmd);
402    }
403 }
404
405 #else
406
407 void
408 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
409 {
410   /* DUMMY func for Bacula_DB */
411   return;
412 }
413
414 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/