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