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
15 Copyright (C) 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
35 /* The following is necessary so that we do not include
36 * the dummy external definition of DB.
38 #define __SQL_C /* indicate that this is sql.c */
46 /* Forward referenced functions */
47 int db_create_pool_record(B_DB *mdb, POOL_DBR *pr);
49 /* -----------------------------------------------------------------------
51 * Bacula specific defines and subroutines
53 * -----------------------------------------------------------------------
56 int db_create_file_attributes_record(B_DB *mdb, ATTR_DBR *ar)
58 /* *****FIXME***** implement this */
62 int db_create_file_item(B_DB *mdb, ATTR_DBR *ar)
64 /****FIXME***** not implemented */
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.
74 * Returns: 0 on failure
77 int db_create_job_record(B_DB *mdb, JOB_DBR *jr)
82 if (!bdb_open_jobs_file(mdb)) {
87 bdb_write_control_file(mdb);
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));
102 /* Create a JobMedia record for Volume used this job
103 * Returns: 0 on failure
104 * record-id on success
106 int db_create_jobmedia_record(B_DB *mdb, JOBMEDIA_DBR *jm)
111 if (!bdb_open_jobmedia_file(mdb)) {
115 mdb->control.JobMediaId++;
116 jm->JobMediaId = mdb->control.JobMediaId;
117 bdb_write_control_file(mdb);
119 len = sizeof(JOBMEDIA_DBR);
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));
127 fflush(mdb->jobmediafd);
129 return jm->JobMediaId;
134 * Create a unique Pool record
135 * Returns: 0 on failure
138 int db_create_pool_record(B_DB *mdb, POOL_DBR *pr)
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);
151 if (!bdb_open_pools_file(mdb)) {
156 mdb->control.PoolId++;
157 pr->PoolId = mdb->control.PoolId;
158 bdb_write_control_file(mdb);
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));
174 * Create Unique Media record. This record
175 * contains all the data pertaining to a specific
178 * Returns: 0 on failure
181 int db_create_media_record(B_DB *mdb, MEDIA_DBR *mr)
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);
195 mdb->control.MediaId++;
196 mr->MediaId = mdb->control.MediaId;
197 bdb_write_control_file(mdb);
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));
206 fflush(mdb->mediafd);
213 * Create a unique Client record or return existing record
214 * Returns: 0 on failure
217 int db_create_client_record(B_DB *mdb, CLIENT_DBR *cr)
223 if (db_get_client_record(mdb, cr)) {
224 Mmsg1(&mdb->errmsg, "Client record %s already exists\n", cr->Name);
229 if (!bdb_open_client_file(mdb)) {
234 mdb->control.ClientId++;
235 cr->ClientId = mdb->control.ClientId;
236 bdb_write_control_file(mdb);
238 fseek(mdb->clientfd, 0L, SEEK_END);
240 if (fwrite(cr, len, 1, mdb->clientfd) != 1) {
241 Mmsg1(&mdb->errmsg, "Error writing DB Client file. ERR=%s\n", strerror(errno));
245 fflush(mdb->clientfd);
251 * Create a unique FileSet record or return existing record
253 * Note, here we write the FILESET_DBR structure
255 * Returns: 0 on failure
258 int db_create_fileset_record(B_DB *mdb, FILESET_DBR *fsr)
264 if (db_get_fileset_record(mdb, fsr)) {
265 Mmsg1(&mdb->errmsg, "FileSet record %s already exists\n", fsr->FileSet);
270 if (!bdb_open_fileset_file(mdb)) {
275 mdb->control.FileSetId++;
276 fsr->FileSetId = mdb->control.FileSetId;
277 bdb_write_control_file(mdb);
279 fseek(mdb->clientfd, 0L, SEEK_END);
281 if (fwrite(fsr, len, 1, mdb->filesetfd) != 1) {
282 Mmsg1(&mdb->errmsg, "Error writing DB FileSet file. ERR=%s\n", strerror(errno));
286 fflush(mdb->filesetfd);
291 #endif /* HAVE_BACULA_DB */