2 * Bacula Catalog Database Create record routines
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
11 * Kern Sibbald, January MMI
16 Bacula® - The Network Backup Solution
18 Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
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
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.
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
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.
44 /* The following is necessary so that we do not include
45 * the dummy external definition of DB.
47 #define __SQL_C /* indicate that this is sql.c */
55 /* Forward referenced functions */
56 bool db_create_pool_record(B_DB *mdb, POOL_DBR *pr);
58 /* -----------------------------------------------------------------------
60 * Bacula specific defines and subroutines
62 * -----------------------------------------------------------------------
65 bool db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
67 /* *****FIXME***** implement this */
71 int db_create_file_item(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
73 /****FIXME***** not implemented */
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.
83 * Returns: 0 on failure
86 bool db_create_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
91 if (!bdb_open_jobs_file(mdb)) {
96 bdb_write_control_file(mdb);
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));
111 /* Create a JobMedia record for Volume used this job
112 * Returns: 0 on failure
113 * record-id on success
115 bool db_create_jobmedia_record(JCR *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
120 if (!bdb_open_jobmedia_file(mdb)) {
124 mdb->control.JobMediaId++;
125 jm->JobMediaId = mdb->control.JobMediaId;
126 bdb_write_control_file(mdb);
128 len = sizeof(JOBMEDIA_DBR);
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));
136 fflush(mdb->jobmediafd);
138 return jm->JobMediaId;
143 * Create a unique Pool record
144 * Returns: 0 on failure
147 bool db_create_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
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);
160 if (!bdb_open_pools_file(mdb)) {
165 mdb->control.PoolId++;
166 pr->PoolId = mdb->control.PoolId;
167 bdb_write_control_file(mdb);
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));
181 bool db_create_device_record(JCR *jcr, B_DB *mdb, DEVICE_DBR *dr)
184 bool db_create_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *dr)
187 bool db_create_mediatype_record(JCR *jcr, B_DB *mdb, MEDIATYPE_DBR *dr)
192 * Create Unique Media record. This record
193 * contains all the data pertaining to a specific
196 * Returns: 0 on failure
199 int db_create_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
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);
214 mdb->control.MediaId++;
215 mr->MediaId = mdb->control.MediaId;
216 bdb_write_control_file(mdb);
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));
225 fflush(mdb->mediafd);
232 * Create a unique Client record or return existing record
233 * Returns: 0 on failure
236 int db_create_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
243 if (db_get_client_record(jcr, mdb, cr)) {
244 Mmsg1(&mdb->errmsg, "Client record %s already exists\n", cr->Name);
249 if (!bdb_open_client_file(mdb)) {
254 mdb->control.ClientId++;
255 cr->ClientId = mdb->control.ClientId;
256 bdb_write_control_file(mdb);
258 fseek(mdb->clientfd, 0L, SEEK_END);
260 if (fwrite(cr, len, 1, mdb->clientfd) != 1) {
261 Mmsg1(&mdb->errmsg, "Error writing DB Client file. ERR=%s\n", strerror(errno));
265 fflush(mdb->clientfd);
271 * Create a unique FileSet record or return existing record
273 * Note, here we write the FILESET_DBR structure
275 * Returns: 0 on failure
278 bool db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
285 if (db_get_fileset_record(jcr, mdb, fsr)) {
286 Mmsg1(&mdb->errmsg, "FileSet record %s already exists\n", fsr->FileSet);
291 if (!bdb_open_fileset_file(mdb)) {
296 mdb->control.FileSetId++;
297 fsr->FileSetId = mdb->control.FileSetId;
298 bdb_write_control_file(mdb);
300 fseek(mdb->clientfd, 0L, SEEK_END);
302 if (fwrite(fsr, len, 1, mdb->filesetfd) != 1) {
303 Mmsg1(&mdb->errmsg, "Error writing DB FileSet file. ERR=%s\n", strerror(errno));
307 fflush(mdb->filesetfd);
312 int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
315 bool db_write_batch_file_records(JCR *jcr) { return false; }
316 bool my_batch_start(JCR *jcr, B_DB *mdb) { return false; }
317 bool my_batch_end(JCR *jcr, B_DB *mdb, const char *error) { return false; }
318 bool my_batch_insert(JCR *jcr, B_DB *mdb, ATTR_DBR *ar) { return false; }
321 #endif /* HAVE_BACULA_DB */