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