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
17 Copyright (C) 2001-2003 Kern Sibbald and John Walker
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.
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.
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,
37 /* The following is necessary so that we do not include
38 * the dummy external definition of DB.
40 #define __SQL_C /* indicate that this is sql.c */
48 /* Forward referenced functions */
49 int db_create_pool_record(B_DB *mdb, POOL_DBR *pr);
51 /* -----------------------------------------------------------------------
53 * Bacula specific defines and subroutines
55 * -----------------------------------------------------------------------
58 int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
60 /* *****FIXME***** implement this */
64 int db_create_file_item(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
66 /****FIXME***** not implemented */
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.
76 * Returns: 0 on failure
79 int db_create_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
84 if (!bdb_open_jobs_file(mdb)) {
89 bdb_write_control_file(mdb);
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));
104 /* Create a JobMedia record for Volume used this job
105 * Returns: 0 on failure
106 * record-id on success
108 int db_create_jobmedia_record(JCR *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
113 if (!bdb_open_jobmedia_file(mdb)) {
117 mdb->control.JobMediaId++;
118 jm->JobMediaId = mdb->control.JobMediaId;
119 bdb_write_control_file(mdb);
121 len = sizeof(JOBMEDIA_DBR);
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));
129 fflush(mdb->jobmediafd);
131 return jm->JobMediaId;
136 * Create a unique Pool record
137 * Returns: 0 on failure
140 int db_create_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
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);
153 if (!bdb_open_pools_file(mdb)) {
158 mdb->control.PoolId++;
159 pr->PoolId = mdb->control.PoolId;
160 bdb_write_control_file(mdb);
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));
176 * Create Unique Media record. This record
177 * contains all the data pertaining to a specific
180 * Returns: 0 on failure
183 int db_create_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
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);
198 mdb->control.MediaId++;
199 mr->MediaId = mdb->control.MediaId;
200 bdb_write_control_file(mdb);
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));
209 fflush(mdb->mediafd);
216 * Create a unique Client record or return existing record
217 * Returns: 0 on failure
220 int db_create_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
227 if (db_get_client_record(jcr, mdb, cr)) {
228 Mmsg1(&mdb->errmsg, "Client record %s already exists\n", cr->Name);
233 if (!bdb_open_client_file(mdb)) {
238 mdb->control.ClientId++;
239 cr->ClientId = mdb->control.ClientId;
240 bdb_write_control_file(mdb);
242 fseek(mdb->clientfd, 0L, SEEK_END);
244 if (fwrite(cr, len, 1, mdb->clientfd) != 1) {
245 Mmsg1(&mdb->errmsg, "Error writing DB Client file. ERR=%s\n", strerror(errno));
249 fflush(mdb->clientfd);
255 * Create a unique FileSet record or return existing record
257 * Note, here we write the FILESET_DBR structure
259 * Returns: 0 on failure
262 int db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
269 if (db_get_fileset_record(jcr, mdb, fsr)) {
270 Mmsg1(&mdb->errmsg, "FileSet record %s already exists\n", fsr->FileSet);
275 if (!bdb_open_fileset_file(mdb)) {
280 mdb->control.FileSetId++;
281 fsr->FileSetId = mdb->control.FileSetId;
282 bdb_write_control_file(mdb);
284 fseek(mdb->clientfd, 0L, SEEK_END);
286 if (fwrite(fsr, len, 1, mdb->filesetfd) != 1) {
287 Mmsg1(&mdb->errmsg, "Error writing DB FileSet file. ERR=%s\n", strerror(errno));
291 fflush(mdb->filesetfd);
296 int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
300 #endif /* HAVE_BACULA_DB */