]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_update.c
- Get next volume from Scratch pool before creating a volume.
[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 file digest */
57 int
58 db_add_digest_to_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, char *digest,
59                           int type)
60 {
61    int stat;
62    char ed1[CRYPTO_DIGEST_MAX_SIZE];
63
64    db_lock(mdb);
65    Mmsg(mdb->cmd, "UPDATE File SET MD5='%s' WHERE FileId=%s", digest, 
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 Media WHERE PoolId=%s",
242       edit_int64(pr->PoolId, ed4));
243    pr->NumVols = get_sql_record_max(jcr, mdb);
244    Dmsg1(400, "NumVols=%d\n", pr->NumVols);
245
246    Mmsg(mdb->cmd,
247 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d,"
248 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
249 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
250 "AutoPrune=%d,LabelType=%d,LabelFormat='%s' WHERE PoolId=%s",
251       pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
252       pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
253       edit_uint64(pr->VolUseDuration, ed2),
254       pr->MaxVolJobs, pr->MaxVolFiles,
255       edit_uint64(pr->MaxVolBytes, ed3),
256       pr->Recycle, pr->AutoPrune, pr->LabelType,
257       pr->LabelFormat, 
258       ed4);
259
260    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
261    db_unlock(mdb);
262    return stat;
263 }
264
265 bool
266 db_update_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
267 {
268    int stat;
269    char ed1[50];
270
271    db_lock(mdb);
272    Mmsg(mdb->cmd, "UPDATE Storage SET AutoChanger=%d WHERE StorageId=%s", 
273       sr->AutoChanger, edit_int64(sr->StorageId, ed1));
274
275    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
276    db_unlock(mdb);
277    return stat;
278 }
279
280
281 /*
282  * Update the Media Record at end of Session
283  *
284  * Returns: 0 on failure
285  *          numrows on success
286  */
287 int
288 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
289 {
290    char dt[MAX_TIME_LENGTH];
291    time_t ttime;
292    struct tm tm;
293    int stat;
294    char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50], ed7[50];
295    char ed8[50], ed9[50];
296
297
298    Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
299    db_lock(mdb);
300    if (mr->set_first_written) {
301       Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
302       ttime = mr->FirstWritten;
303       localtime_r(&ttime, &tm);
304       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
305       Mmsg(mdb->cmd, "UPDATE Media SET FirstWritten='%s'"
306            " WHERE VolumeName='%s'", dt, mr->VolumeName);
307       stat = UPDATE_DB(jcr, mdb, mdb->cmd);
308       Dmsg1(400, "Firstwritten=%d\n", mr->FirstWritten);
309    }
310
311    /* Label just done? */
312    if (mr->set_label_date) {
313       ttime = mr->LabelDate;
314       if (ttime == 0) {
315          ttime = time(NULL);
316       }
317       localtime_r(&ttime, &tm);
318       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
319       Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
320            "WHERE VolumeName='%s'", dt, mr->VolumeName);
321       UPDATE_DB(jcr, mdb, mdb->cmd);
322    }
323
324    if (mr->LastWritten != 0) {
325       ttime = mr->LastWritten;
326       localtime_r(&ttime, &tm);
327       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
328       Mmsg(mdb->cmd, "UPDATE Media Set LastWritten='%s' "
329            "WHERE VolumeName='%s'", dt, mr->VolumeName);
330       UPDATE_DB(jcr, mdb, mdb->cmd);
331    }
332
333    Mmsg(mdb->cmd, "UPDATE Media SET VolJobs=%u,"
334         "VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,"
335         "VolWrites=%u,MaxVolBytes=%s,VolStatus='%s',"
336         "Slot=%d,InChanger=%d,VolReadTime=%s,VolWriteTime=%s,VolParts=%d,"
337         "LabelType=%d,StorageId=%s,PoolId=%s,VolRetention=%s,VolUseDuration=%s,"
338         "MaxVolJobs=%d,MaxVolFiles=%d,MaxVolBytes=%s"
339         " WHERE VolumeName='%s'",
340         mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
341         mr->VolMounts, mr->VolErrors, mr->VolWrites,
342         edit_uint64(mr->MaxVolBytes, ed2),
343         mr->VolStatus, mr->Slot, mr->InChanger,
344         edit_uint64(mr->VolReadTime, ed3),
345         edit_uint64(mr->VolWriteTime, ed4),
346         mr->VolParts,
347         mr->LabelType,
348         edit_int64(mr->StorageId, ed5),
349         edit_int64(mr->PoolId, ed6),
350         edit_uint64(mr->VolRetention, ed7),
351         edit_uint64(mr->VolUseDuration, ed8),
352         mr->MaxVolJobs, mr->MaxVolFiles,
353         edit_uint64(mr->MaxVolBytes, ed9),
354         mr->VolumeName);
355
356    Dmsg1(400, "%s\n", mdb->cmd);
357
358    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
359
360    /* Make sure InChanger is 0 for any record having the same Slot */
361    db_make_inchanger_unique(jcr, mdb, mr);
362
363    db_unlock(mdb);
364    return stat;
365 }
366
367 /*
368  * Update the Media Record Default values from Pool
369  *
370  * Returns: 0 on failure
371  *          numrows on success
372  */
373 int
374 db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
375 {
376    int stat;
377    char ed1[50], ed2[50], ed3[50], ed4[50];
378
379
380    db_lock(mdb);
381    if (mr->VolumeName[0]) {
382       Mmsg(mdb->cmd, "UPDATE Media SET "
383            "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
384            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
385            " WHERE VolumeName='%s'",
386            mr->Recycle,edit_uint64(mr->VolRetention, ed1),
387            edit_uint64(mr->VolUseDuration, ed2),
388            mr->MaxVolJobs, mr->MaxVolFiles,
389            edit_uint64(mr->MaxVolBytes, ed3),
390            mr->VolumeName);
391    } else {
392       Mmsg(mdb->cmd, "UPDATE Media SET "
393            "Recycle=%d,VolRetention=%s,VolUseDuration=%s,"
394            "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s"
395            " WHERE PoolId=%s",
396            mr->Recycle,edit_uint64(mr->VolRetention, ed1),
397            edit_uint64(mr->VolUseDuration, ed2),
398            mr->MaxVolJobs, mr->MaxVolFiles,
399            edit_uint64(mr->MaxVolBytes, ed3),
400            edit_int64(mr->PoolId, ed4));
401    }
402
403    Dmsg1(400, "%s\n", mdb->cmd);
404
405    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
406
407    db_unlock(mdb);
408    return stat;
409 }
410
411
412 /*
413  * If we have a non-zero InChanger, ensure that no other Media
414  *  record has InChanger set on the same Slot.
415  *
416  * This routine assumes the database is already locked.
417  */
418 void
419 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
420 {
421    char ed1[50], ed2[50];
422    if (mr->InChanger != 0 && mr->Slot != 0) {
423       Mmsg(mdb->cmd, "UPDATE Media SET InChanger=0 WHERE "
424            "Slot=%d AND StorageId=%s AND MediaId!=%s",
425             mr->Slot, 
426             edit_int64(mr->StorageId, ed1), edit_int64(mr->MediaId, ed2));
427       Dmsg1(400, "%s\n", mdb->cmd);
428       UPDATE_DB(jcr, mdb, mdb->cmd);
429    }
430 }
431
432 #else
433
434 void
435 db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
436 {
437   /* DUMMY func for Bacula_DB */
438   return;
439 }
440
441 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL*/