]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/sql_update.c
Update bdb to use JCR * instead of void *jcr
[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-2003 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
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    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
110    db_unlock(mdb);
111    mdb->changes = 0;
112    return stat;
113 }
114
115
116
117 /*
118  * Update the Job record at end of Job
119  *
120  *  Returns: 0 on failure
121  *           1 on success
122  */
123 int
124 db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
125 {
126    char dt[MAX_TIME_LENGTH];
127    time_t ttime;
128    struct tm tm;
129    int stat;
130    char ed1[30], ed2[30];
131    btime_t JobTDate;
132        
133    ttime = jr->EndTime;
134    localtime_r(&ttime, &tm);
135    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
136    JobTDate = ttime;
137
138    db_lock(mdb);
139    Mmsg(&mdb->cmd,
140       "UPDATE Job SET JobStatus='%c', EndTime='%s', \
141 ClientId=%u, JobBytes=%s, JobFiles=%u, JobErrors=%u, VolSessionId=%u, \
142 VolSessionTime=%u, PoolId=%u, FileSetId=%u, JobTDate=%s WHERE JobId=%u",
143       (char)(jr->JobStatus), dt, jr->ClientId, edit_uint64(jr->JobBytes, ed1), 
144       jr->JobFiles, jr->JobErrors, jr->VolSessionId, jr->VolSessionTime, 
145       jr->PoolId, jr->FileSetId, edit_uint64(JobTDate, ed2), jr->JobId);
146
147    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
148    db_unlock(mdb);
149    return stat;
150 }
151
152
153 int
154 db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
155 {
156    int stat;
157    char ed1[50], ed2[50];
158    CLIENT_DBR tcr;
159
160    db_lock(mdb);
161    memcpy(&tcr, cr, sizeof(tcr));
162    if (!db_create_client_record(jcr, mdb, &tcr)) {
163       db_unlock(mdb);
164       return 0;
165    }
166
167    Mmsg(&mdb->cmd,
168 "UPDATE Client SET AutoPrune=%d,FileRetention=%s,JobRetention=%s," 
169 "Uname='%s' WHERE Name='%s'",
170       cr->AutoPrune,
171       edit_uint64(cr->FileRetention, ed1),
172       edit_uint64(cr->JobRetention, ed2),
173       cr->Uname, cr->Name);
174
175    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
176    db_unlock(mdb);
177    return stat;
178 }
179
180
181 int
182 db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
183 {
184    int stat;
185    char ed1[50], ed2[50], ed3[50];
186
187    db_lock(mdb);
188    Mmsg(&mdb->cmd,
189 "UPDATE Pool SET NumVols=%u,MaxVols=%u,UseOnce=%d,UseCatalog=%d," 
190 "AcceptAnyVolume=%d,VolRetention='%s',VolUseDuration='%s',"
191 "MaxVolJobs=%u,MaxVolFiles=%u,MaxVolBytes=%s,Recycle=%d,"
192 "AutoPrune=%d,LabelFormat='%s' WHERE PoolId=%u",
193       pr->NumVols, pr->MaxVols, pr->UseOnce, pr->UseCatalog,
194       pr->AcceptAnyVolume, edit_uint64(pr->VolRetention, ed1),
195       edit_uint64(pr->VolUseDuration, ed2),
196       pr->MaxVolJobs, pr->MaxVolFiles,
197       edit_uint64(pr->MaxVolBytes, ed3),
198       pr->Recycle, pr->AutoPrune,
199       pr->LabelFormat, pr->PoolId);
200
201    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
202    db_unlock(mdb);
203    return stat;
204 }
205
206 /* 
207  * Update the Media Record at end of Session
208  *
209  * Returns: 0 on failure
210  *          numrows on success
211  */
212 int
213 db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr) 
214 {
215    char dt[MAX_TIME_LENGTH];
216    time_t ttime;
217    struct tm tm;
218    int stat;
219    char ed1[30], ed2[30];
220        
221
222    Dmsg1(100, "update_media: FirstWritten=%d\n", mr->FirstWritten);
223    db_lock(mdb);
224    if (mr->VolJobs == 1) {
225       Dmsg1(400, "Set FirstWritten Vol=%s\n", mr->VolumeName);
226       ttime = mr->FirstWritten;
227       localtime_r(&ttime, &tm);
228       strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
229       Mmsg(&mdb->cmd, "UPDATE Media SET FirstWritten='%s'\
230  WHERE VolumeName='%s'", dt, mr->VolumeName);
231       stat = UPDATE_DB(jcr, mdb, mdb->cmd);
232       Dmsg1(400, "Firstwritten stat=%d\n", stat);
233    }
234
235    ttime = mr->LastWritten;
236    localtime_r(&ttime, &tm);
237    strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
238
239    Mmsg(&mdb->cmd, "UPDATE Media SET VolJobs=%u,\
240  VolFiles=%u,VolBlocks=%u,VolBytes=%s,VolMounts=%u,VolErrors=%u,\
241  VolWrites=%u,MaxVolBytes=%s,LastWritten='%s',VolStatus='%s',\
242  Slot=%d WHERE VolumeName='%s'",
243    mr->VolJobs, mr->VolFiles, mr->VolBlocks, edit_uint64(mr->VolBytes, ed1),
244    mr->VolMounts, mr->VolErrors, mr->VolWrites, 
245    edit_uint64(mr->MaxVolBytes, ed2), dt, 
246    mr->VolStatus, mr->Slot, mr->VolumeName);
247
248    Dmsg1(400, "%s\n", mdb->cmd);
249
250    stat = UPDATE_DB(jcr, mdb, mdb->cmd);
251    db_unlock(mdb);
252    return stat;
253 }
254
255 #endif /* HAVE_MYSQL || HAVE_SQLITE */