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, 2002 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(B_DB *mdb, ATTR_DBR *ar)
60 /* *****FIXME***** implement this */
64 int db_create_file_item(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(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(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(B_DB *mdb, POOL_DBR *pr)
145 memset(&mpr, 0, sizeof(mpr));
146 strcpy(mpr.Name, pr->Name);
147 if (db_get_pool_record(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(B_DB *mdb, MEDIA_DBR *mr)
188 memset(&mmr, 0, sizeof(mmr));
189 strcpy(mmr.VolumeName, mr->VolumeName);
190 if (db_get_media_record(mdb, &mmr)) {
191 Mmsg1(&mdb->errmsg, "Media record %s already exists\n", mmr.VolumeName);
197 mdb->control.MediaId++;
198 mr->MediaId = mdb->control.MediaId;
199 bdb_write_control_file(mdb);
202 fseek(mdb->mediafd, 0L, SEEK_END);
203 if (fwrite(mr, len, 1, mdb->mediafd) != 1) {
204 Mmsg1(&mdb->errmsg, "Error writing DB Media file. ERR=%s\n", strerror(errno));
208 fflush(mdb->mediafd);
215 * Create a unique Client record or return existing record
216 * Returns: 0 on failure
219 int db_create_client_record(B_DB *mdb, CLIENT_DBR *cr)
225 if (db_get_client_record(mdb, cr)) {
226 Mmsg1(&mdb->errmsg, "Client record %s already exists\n", cr->Name);
231 if (!bdb_open_client_file(mdb)) {
236 mdb->control.ClientId++;
237 cr->ClientId = mdb->control.ClientId;
238 bdb_write_control_file(mdb);
240 fseek(mdb->clientfd, 0L, SEEK_END);
242 if (fwrite(cr, len, 1, mdb->clientfd) != 1) {
243 Mmsg1(&mdb->errmsg, "Error writing DB Client file. ERR=%s\n", strerror(errno));
247 fflush(mdb->clientfd);
253 * Create a unique FileSet record or return existing record
255 * Note, here we write the FILESET_DBR structure
257 * Returns: 0 on failure
260 int db_create_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
266 if (db_get_fileset_record(mdb, fsr)) {
267 Mmsg1(&mdb->errmsg, "FileSet record %s already exists\n", fsr->FileSet);
272 if (!bdb_open_fileset_file(mdb)) {
277 mdb->control.FileSetId++;
278 fsr->FileSetId = mdb->control.FileSetId;
279 bdb_write_control_file(mdb);
281 fseek(mdb->clientfd, 0L, SEEK_END);
283 if (fwrite(fsr, len, 1, mdb->filesetfd) != 1) {
284 Mmsg1(&mdb->errmsg, "Error writing DB FileSet file. ERR=%s\n", strerror(errno));
288 fflush(mdb->filesetfd);
293 #endif /* HAVE_BACULA_DB */