]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/bdb_create.c
kes Add some additional locking in the cats directory in subroutines
[bacula/bacula] / bacula / src / cats / bdb_create.c
1 /*
2  * Bacula Catalog Database Create record 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  *    Version $Id$
14  */
15 /*
16    Bacula® - The Network Backup Solution
17
18    Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
19
20    The main author of Bacula is Kern Sibbald, with contributions from
21    many others, a complete list can be found in the file AUTHORS.
22    This program is Free Software; you can redistribute it and/or
23    modify it under the terms of version two of the GNU General Public
24    License as published by the Free Software Foundation and included
25    in the file LICENSE.
26
27    This program is distributed in the hope that it will be useful, but
28    WITHOUT ANY WARRANTY; without even the implied warranty of
29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30    General Public License for more details.
31
32    You should have received a copy of the GNU General Public License
33    along with this program; if not, write to the Free Software
34    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35    02110-1301, USA.
36
37    Bacula® is a registered trademark of John Walker.
38    The licensor of Bacula is the Free Software Foundation Europe
39    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
40    Switzerland, email:ftf@fsfeurope.org.
41 */
42
43
44 /* The following is necessary so that we do not include
45  * the dummy external definition of DB.
46  */
47 #define __SQL_C                       /* indicate that this is sql.c */
48
49 #include "bacula.h"
50 #include "cats.h"
51 #include "bdb.h"
52
53 #ifdef HAVE_BACULA_DB
54
55 /* Forward referenced functions */
56 bool db_create_pool_record(B_DB *mdb, POOL_DBR *pr);
57
58 /* -----------------------------------------------------------------------
59  *
60  *   Bacula specific defines and subroutines
61  *
62  * -----------------------------------------------------------------------
63  */
64
65 int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
66 {
67    /* *****FIXME***** implement this */
68    return 1;
69 }
70
71 int db_create_file_item(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
72 {
73    /****FIXME***** not implemented */
74    return 1;
75 }
76
77
78 /*
79  * Create a new record for the Job
80  *   This record is created at the start of the Job,
81  *   it is updated in bdb_update.c when the Job terminates.
82  *
83  * Returns: 0 on failure
84  *          1 on success
85  */
86 bool db_create_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
87 {
88    int len;
89
90    db_lock(mdb);
91    if (!bdb_open_jobs_file(mdb)) {
92       db_unlock(mdb);
93       return 0;
94    }
95    mdb->control.JobId++;
96    bdb_write_control_file(mdb);
97
98    len = sizeof(JOB_DBR);
99    jr->JobId = mdb->control.JobId;
100    fseek(mdb->jobfd, 0L, SEEK_END);
101    if (fwrite(jr, len, 1, mdb->jobfd) != 1) {
102       Mmsg1(&mdb->errmsg, "Error writing DB Jobs file. ERR=%s\n", strerror(errno));
103       db_unlock(mdb);
104       return 0;
105    }
106    fflush(mdb->jobfd);
107    db_unlock(mdb);
108    return 1;
109 }
110
111 /* Create a JobMedia record for Volume used this job
112  * Returns: 0 on failure
113  *          record-id on success
114  */
115 bool db_create_jobmedia_record(JCR *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
116 {
117    int len;
118
119    db_lock(mdb);
120    if (!bdb_open_jobmedia_file(mdb)) {
121       db_unlock(mdb);
122       return 0;
123    }
124    mdb->control.JobMediaId++;
125    jm->JobMediaId = mdb->control.JobMediaId;
126    bdb_write_control_file(mdb);
127
128    len = sizeof(JOBMEDIA_DBR);
129
130    fseek(mdb->jobmediafd, 0L, SEEK_END);
131    if (fwrite(jm, len, 1, mdb->jobmediafd) != 1) {
132       Mmsg1(&mdb->errmsg, "Error writing DB JobMedia file. ERR=%s\n", strerror(errno));
133       db_unlock(mdb);
134       return 0;
135    }
136    fflush(mdb->jobmediafd);
137    db_unlock(mdb);
138    return jm->JobMediaId;
139 }
140
141
142 /*
143  *  Create a unique Pool record
144  * Returns: 0 on failure
145  *          1 on success
146  */
147 bool db_create_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
148 {
149    int len;
150    POOL_DBR mpr;
151
152    memset(&mpr, 0, sizeof(mpr));
153    strcpy(mpr.Name, pr->Name);
154    if (db_get_pool_record(jcr, mdb, &mpr)) {
155       Mmsg1(&mdb->errmsg, "Pool record %s already exists\n", mpr.Name);
156       return 0;
157    }
158
159    db_lock(mdb);
160    if (!bdb_open_pools_file(mdb)) {
161       db_unlock(mdb);
162       return 0;
163    }
164
165    mdb->control.PoolId++;
166    pr->PoolId = mdb->control.PoolId;
167    bdb_write_control_file(mdb);
168
169    len = sizeof(mpr);
170    fseek(mdb->poolfd, 0L, SEEK_END);
171    if (fwrite(pr, len, 1, mdb->poolfd) != 1) {
172       Mmsg1(&mdb->errmsg, "Error writing DB Pools file. ERR=%s\n", strerror(errno));
173       db_unlock(mdb);
174       return 0;
175    }
176    fflush(mdb->poolfd);
177    db_unlock(mdb);
178    return 1;
179 }
180
181 bool db_create_device_record(JCR *jcr, B_DB *mdb, DEVICE_DBR *dr)
182 { return false; }
183
184 bool db_create_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *dr)
185 { return false; }
186
187 bool db_create_mediatype_record(JCR *jcr, B_DB *mdb, MEDIATYPE_DBR *dr)
188 { return false; }
189
190
191 /*
192  * Create Unique Media record.  This record
193  *   contains all the data pertaining to a specific
194  *   Volume.
195  *
196  * Returns: 0 on failure
197  *          1 on success
198  */
199 int db_create_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
200 {
201    int len;
202    MEDIA_DBR mmr;
203
204    db_lock(mdb);
205    memset(&mmr, 0, sizeof(mmr));
206    strcpy(mmr.VolumeName, mr->VolumeName);
207    if (db_get_media_record(jcr, mdb, &mmr)) {
208       Mmsg1(&mdb->errmsg, "Media record %s already exists\n", mmr.VolumeName);
209       db_unlock(mdb);
210       return 0;
211    }
212
213
214    mdb->control.MediaId++;
215    mr->MediaId = mdb->control.MediaId;
216    bdb_write_control_file(mdb);
217
218    len = sizeof(mmr);
219    fseek(mdb->mediafd, 0L, SEEK_END);
220    if (fwrite(mr, len, 1, mdb->mediafd) != 1) {
221       Mmsg1(&mdb->errmsg, "Error writing DB Media file. ERR=%s\n", strerror(errno));
222       db_unlock(mdb);
223       return 0;
224    }
225    fflush(mdb->mediafd);
226    db_unlock(mdb);
227    return 1;
228 }
229
230
231 /*
232  *  Create a unique Client record or return existing record
233  * Returns: 0 on failure
234  *          1 on success
235  */
236 int db_create_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
237 {
238    int len;
239    CLIENT_DBR lcr;
240
241    db_lock(mdb);
242    cr->ClientId = 0;
243    if (db_get_client_record(jcr, mdb, cr)) {
244       Mmsg1(&mdb->errmsg, "Client record %s already exists\n", cr->Name);
245       db_unlock(mdb);
246       return 1;
247    }
248
249    if (!bdb_open_client_file(mdb)) {
250       db_unlock(mdb);
251       return 0;
252    }
253
254    mdb->control.ClientId++;
255    cr->ClientId = mdb->control.ClientId;
256    bdb_write_control_file(mdb);
257
258    fseek(mdb->clientfd, 0L, SEEK_END);
259    len = sizeof(lcr);
260    if (fwrite(cr, len, 1, mdb->clientfd) != 1) {
261       Mmsg1(&mdb->errmsg, "Error writing DB Client file. ERR=%s\n", strerror(errno));
262       db_unlock(mdb);
263       return 0;
264    }
265    fflush(mdb->clientfd);
266    db_unlock(mdb);
267    return 1;
268 }
269
270 /*
271  *  Create a unique FileSet record or return existing record
272  *
273  *   Note, here we write the FILESET_DBR structure
274  *
275  * Returns: 0 on failure
276  *          1 on success
277  */
278 bool db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
279 {
280    int len;
281    FILESET_DBR lfsr;
282
283    db_lock(mdb);
284    fsr->FileSetId = 0;
285    if (db_get_fileset_record(jcr, mdb, fsr)) {
286       Mmsg1(&mdb->errmsg, "FileSet record %s already exists\n", fsr->FileSet);
287       db_unlock(mdb);
288       return 1;
289    }
290
291    if (!bdb_open_fileset_file(mdb)) {
292       db_unlock(mdb);
293       return 0;
294    }
295
296    mdb->control.FileSetId++;
297    fsr->FileSetId = mdb->control.FileSetId;
298    bdb_write_control_file(mdb);
299
300    fseek(mdb->clientfd, 0L, SEEK_END);
301    len = sizeof(lfsr);
302    if (fwrite(fsr,  len, 1, mdb->filesetfd) != 1) {
303       Mmsg1(&mdb->errmsg, "Error writing DB FileSet file. ERR=%s\n", strerror(errno));
304       db_unlock(mdb);
305       return 0;
306    }
307    fflush(mdb->filesetfd);
308    db_unlock(mdb);
309    return 1;
310 }
311
312 int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
313 { return 0; }
314
315
316 #endif /* HAVE_BACULA_DB */