]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/bdb_update.c
Add retry for opening batch db if it fails.
[bacula/bacula] / bacula / src / cats / bdb_update.c
1 /*
2  * Bacula Catalog Database Update record interface routines
3  *
4  * Bacula Catalog Database routines written specifically
5  *  for Bacula.  Note, these routines are VERY dumb and
6  *  do not provide all the functionality of an SQL database.
7  *  The purpose of these routines is to ensure that Bacula
8  *  can limp along if no real database is loaded on the
9  *  system.
10  *
11  *    Kern Sibbald, January MMI
12  *
13  *
14  *    Version $Id$
15  */
16 /*
17    Bacula® - The Network Backup Solution
18
19    Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
20
21    The main author of Bacula is Kern Sibbald, with contributions from
22    many others, a complete list can be found in the file AUTHORS.
23    This program is Free Software; you can redistribute it and/or
24    modify it under the terms of version two of the GNU General Public
25    License as published by the Free Software Foundation and included
26    in the file LICENSE.
27
28    This program is distributed in the hope that it will be useful, but
29    WITHOUT ANY WARRANTY; without even the implied warranty of
30    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31    General Public License for more details.
32
33    You should have received a copy of the GNU General Public License
34    along with this program; if not, write to the Free Software
35    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
36    02110-1301, USA.
37
38    Bacula® is a registered trademark of John Walker.
39    The licensor of Bacula is the Free Software Foundation Europe
40    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
41    Switzerland, email:ftf@fsfeurope.org.
42 */
43
44
45 /* The following is necessary so that we do not include
46  * the dummy external definition of DB.
47  */
48 #define __SQL_C                       /* indicate that this is sql.c */
49
50 #include "bacula.h"
51 #include "cats.h"
52 #include "bdb.h"
53
54 #ifdef HAVE_BACULA_DB
55
56 /* -----------------------------------------------------------------------
57  *
58  *   Bacula specific defines and subroutines
59  *
60  * -----------------------------------------------------------------------
61  */
62
63
64 /*
65  * This is called at Job start time to add the
66  * most current start fields to the job record.
67  * It is assumed that you did a db_create_job_record() already.
68  */
69 bool db_update_job_start_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
70 {
71    int len, stat = 1;
72    JOB_DBR ojr;
73
74    db_lock(mdb);
75
76    Dmsg0(200, "In db_update_job_start_record\n");
77    len = sizeof(ojr);
78    memcpy(&ojr, jr, len);
79
80    if (!db_get_job_record(jcr, mdb, &ojr)) {
81       db_unlock(mdb);
82       return 0;
83    }
84
85
86    fseek(mdb->jobfd, ojr.rec_addr, SEEK_SET);
87    if (fwrite(jr, len, 1, mdb->jobfd) != 1) {
88       Mmsg1(mdb->errmsg, _("Error updating DB Job file. ERR=%s\n"), strerror(errno));
89       stat = 0;
90    }
91    fflush(mdb->jobfd);
92
93    db_unlock(mdb);
94    return stat;
95 }
96
97 /*
98  * This is called at Job termination time to add all the
99  * other fields to the job record.
100  */
101 int db_update_job_end_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
102 {
103    int len, stat = 1;
104    JOB_DBR ojr;
105
106    db_lock(mdb);
107
108    Dmsg0(200, "In db_update_job_start_record\n");
109    len = sizeof(ojr);
110    memcpy(&ojr, jr, len);
111
112    if (!db_get_job_record(jcr, mdb, &ojr)) {
113       db_unlock(mdb);
114       return 0;
115    }
116
117    fseek(mdb->jobfd, ojr.rec_addr, SEEK_SET);
118    if (fwrite(jr, len, 1, mdb->jobfd) != 1) {
119       Mmsg1(&mdb->errmsg, _("Error updating DB Job file. ERR=%s\n"), strerror(errno));
120       stat = 0;
121    }
122    fflush(mdb->jobfd);
123
124    db_unlock(mdb);
125    return stat;
126 }
127
128
129 int db_update_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
130 {
131    int stat = 1;
132    MEDIA_DBR omr;
133    int len;
134
135    db_lock(mdb);
136    Dmsg0(200, "In db_update_media_record\n");
137    mr->MediaId = 0;
138    len = sizeof(omr);
139    memcpy(&omr, mr, len);
140
141    if (!db_get_media_record(jcr, mdb, &omr)) {
142       db_unlock(mdb);
143       return 0;
144    }
145
146
147    /* Don't allow some fields to change by copying from master record */
148    strcpy(mr->VolumeName, omr.VolumeName);
149    strcpy(mr->MediaType, omr.MediaType);
150    mr->MediaId = omr.MediaId;
151    mr->PoolId = omr.PoolId;
152    mr->MaxVolBytes = omr.MaxVolBytes;
153    mr->VolCapacityBytes = omr.VolCapacityBytes;
154    mr->Recycle = omr.Recycle;
155
156    fseek(mdb->mediafd, omr.rec_addr, SEEK_SET);
157    if (fwrite(mr, len, 1, mdb->mediafd) != 1) {
158       Mmsg1(mdb->errmsg, _("Error updating DB Media file. ERR=%s\n"), strerror(errno));
159       stat = 0;
160    }
161    fflush(mdb->mediafd);
162
163    db_unlock(mdb);
164    return stat;
165 }
166
167 int db_update_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
168 {
169    int stat = 1;
170    POOL_DBR opr;
171    int len;
172
173    db_lock(mdb);
174    Dmsg0(200, "In db_update_pool_record\n");
175    len = sizeof(opr);
176    memcpy(&opr, pr, len);
177
178    if (!db_get_pool_record(jcr, mdb, &opr)) {
179       db_unlock(mdb);
180       return 0;
181    }
182
183
184    /* Update specific fields */
185    opr.NumVols = pr->NumVols;
186    opr.MaxVols = pr->MaxVols;
187    opr.UseOnce = pr->UseOnce;
188    opr.UseCatalog = pr->UseCatalog;
189    opr.AcceptAnyVolume = pr->AcceptAnyVolume;
190    strcpy(opr.LabelFormat, pr->LabelFormat);
191
192    fseek(mdb->poolfd, opr.rec_addr, SEEK_SET);
193    if (fwrite(&opr, len, 1, mdb->poolfd) != 1) {
194       Mmsg1(mdb->errmsg, _("Error updating DB Media file. ERR=%s\n"), strerror(errno));
195       stat = 0;
196    } else {
197       memcpy(pr, &opr, len);          /* return record written */
198    }
199    fflush(mdb->poolfd);
200
201    db_unlock(mdb);
202    return stat;
203 }
204
205 int db_add_digest_to_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, char *digest, int type)
206 {
207    return 1;
208 }
209
210 int db_mark_file_record(JCR *jcr, B_DB *mdb, FileId_t FileId, JobId_t JobId)
211 {
212    return 1;
213 }
214
215 int db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
216 {
217    return 1;
218 }
219
220 int db_update_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
221 {
222    return 0;
223 }
224
225 int db_update_media_defaults(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
226 {
227    return 1;
228 }
229
230 void db_make_inchanger_unique(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
231 {
232   return;
233 }
234
235 #endif /* HAVE_BACULA_DB */