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