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