3 * Bacula Director -- Bootstrap Record routines.
5 * BSR (bootstrap record) handling routines split from
6 * ua_restore.c July MMIII
8 * Kern Sibbald, July MMII
14 Copyright (C) 2002-2004 Kern Sibbald and John Walker
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation; either version 2 of
19 the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with this program; if not, write to the Free
28 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
36 /* Forward referenced functions */
37 static int write_bsr(UAContext *ua, RBSR *bsr, FILE *fd);
38 void print_bsr(UAContext *ua, RBSR *bsr);
42 * Create new FileIndex entry for BSR
44 RBSR_FINDEX *new_findex()
46 RBSR_FINDEX *fi = (RBSR_FINDEX *)bmalloc(sizeof(RBSR_FINDEX));
47 memset(fi, 0, sizeof(RBSR_FINDEX));
51 /* Free all BSR FileIndex entries */
52 static void free_findex(RBSR_FINDEX *fi)
55 free_findex(fi->next);
61 * Our data structures were not designed completely
62 * correctly, so the file indexes cover the full
63 * range regardless of volume. The FirstIndex and LastIndex
64 * passed in here are for the current volume, so when
65 * writing out the fi, constrain them to those values.
67 * We are called here once for each JobMedia record
70 static uint32_t write_findex(UAContext *ua, RBSR_FINDEX *fi,
71 int32_t FirstIndex, int32_t LastIndex, FILE *fd)
74 for ( ; fi; fi=fi->next) {
75 int32_t findex, findex2;
76 if ((fi->findex >= FirstIndex && fi->findex <= LastIndex) ||
77 (fi->findex2 >= FirstIndex && fi->findex2 <= LastIndex) ||
78 (fi->findex < FirstIndex && fi->findex2 > LastIndex)) {
79 findex = fi->findex < FirstIndex ? FirstIndex : fi->findex;
80 findex2 = fi->findex2 > LastIndex ? LastIndex : fi->findex2;
81 if (findex == findex2) {
82 fprintf(fd, "FileIndex=%d\n", findex);
85 fprintf(fd, "FileIndex=%d-%d\n", findex, findex2);
86 count += findex2 - findex + 1;
94 * Find out if Volume defined with FirstIndex and LastIndex
95 * falls within the range of selected files in the bsr.
97 static bool is_volume_selected(RBSR_FINDEX *fi,
98 int32_t FirstIndex, int32_t LastIndex)
101 if ((fi->findex >= FirstIndex && fi->findex <= LastIndex) ||
102 (fi->findex2 >= FirstIndex && fi->findex2 <= LastIndex) ||
103 (fi->findex < FirstIndex && fi->findex2 > LastIndex)) {
106 return is_volume_selected(fi->next, FirstIndex, LastIndex);
113 static void print_findex(UAContext *ua, RBSR_FINDEX *fi)
115 bsendmsg(ua, "fi=0x%lx\n", fi);
116 for ( ; fi; fi=fi->next) {
117 if (fi->findex == fi->findex2) {
118 bsendmsg(ua, "FileIndex=%d\n", fi->findex);
119 // Dmsg1(100, "FileIndex=%d\n", fi->findex);
121 bsendmsg(ua, "FileIndex=%d-%d\n", fi->findex, fi->findex2);
122 // Dmsg2(100, "FileIndex=%d-%d\n", fi->findex, fi->findex2);
127 /* Create a new bootstrap record */
130 RBSR *bsr = (RBSR *)bmalloc(sizeof(RBSR));
131 memset(bsr, 0, sizeof(RBSR));
135 /* Free the entire BSR */
136 void free_bsr(RBSR *bsr)
139 free_findex(bsr->fi);
140 if (bsr->VolParams) {
141 free(bsr->VolParams);
149 * Complete the BSR by filling in the VolumeName and
150 * VolSessionId and VolSessionTime using the JobId
152 int complete_bsr(UAContext *ua, RBSR *bsr)
156 memset(&jr, 0, sizeof(jr));
157 jr.JobId = bsr->JobId;
158 if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
159 bsendmsg(ua, _("Unable to get Job record. ERR=%s\n"), db_strerror(ua->db));
162 bsr->VolSessionId = jr.VolSessionId;
163 bsr->VolSessionTime = jr.VolSessionTime;
164 if ((bsr->VolCount=db_get_job_volume_parameters(ua->jcr, ua->db, bsr->JobId,
165 &(bsr->VolParams))) == 0) {
166 bsendmsg(ua, _("Unable to get Job Volume Parameters. ERR=%s\n"), db_strerror(ua->db));
167 if (bsr->VolParams) {
168 free(bsr->VolParams);
169 bsr->VolParams = NULL;
173 return complete_bsr(ua, bsr->next);
179 * Write the bootstrap records to file
181 int write_bsr_file(UAContext *ua, RBSR *bsr)
184 POOLMEM *fname = get_pool_memory(PM_MESSAGE);
188 Mmsg(fname, "%s/restore.bsr", working_directory);
189 fd = fopen(fname, "w+");
192 bsendmsg(ua, _("Unable to create bootstrap file %s. ERR=%s\n"),
193 fname, be.strerror());
196 /* Write them to file */
197 count = write_bsr(ua, bsr, fd);
201 bsendmsg(ua, _("Error writing bsr file.\n"));
207 bsendmsg(ua, _("Bootstrap records written to %s\n"), fname);
209 /* Tell the user what he will need to mount */
211 bsendmsg(ua, _("The job will require the following Volumes:\n"));
212 /* Create Unique list of Volumes using prompt list */
213 start_prompt(ua, "");
214 for (RBSR *nbsr=bsr; nbsr; nbsr=nbsr->next) {
215 for (int i=0; i < nbsr->VolCount; i++) {
216 if (nbsr->VolParams[i].VolumeName[0]) {
217 add_prompt(ua, nbsr->VolParams[i].VolumeName);
221 for (int i=0; i < ua->num_prompts; i++) {
222 bsendmsg(ua, " %s\n", ua->prompt[i]);
225 if (ua->num_prompts == 0) {
226 bsendmsg(ua, _("No Volumes found to restore.\n"));
233 free_pool_memory(fname);
237 static int write_bsr(UAContext *ua, RBSR *bsr, FILE *fd)
242 * For a given volume, loop over all the JobMedia records.
243 * VolCount is the number of JobMedia records.
245 for (int i=0; i < bsr->VolCount; i++) {
246 if (!is_volume_selected(bsr->fi, bsr->VolParams[i].FirstIndex,
247 bsr->VolParams[i].LastIndex)) {
248 bsr->VolParams[i].VolumeName[0] = 0; /* zap VolumeName */
251 fprintf(fd, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
252 fprintf(fd, "VolSessionId=%u\n", bsr->VolSessionId);
253 fprintf(fd, "VolSessionTime=%u\n", bsr->VolSessionTime);
254 if (bsr->VolParams[i].StartFile == bsr->VolParams[i].EndFile) {
255 fprintf(fd, "VolFile=%u\n", bsr->VolParams[i].StartFile);
257 fprintf(fd, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
258 bsr->VolParams[i].EndFile);
260 if (bsr->VolParams[i].StartBlock == bsr->VolParams[i].EndBlock) {
261 fprintf(fd, "VolFile=%u\n", bsr->VolParams[i].StartBlock);
263 fprintf(fd, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
264 bsr->VolParams[i].EndBlock);
266 // Dmsg2(100, "bsr VolParam FI=%u LI=%u\n",
267 // bsr->VolParams[i].FirstIndex, bsr->VolParams[i].LastIndex);
269 count = write_findex(ua, bsr->fi, bsr->VolParams[i].FirstIndex,
270 bsr->VolParams[i].LastIndex, fd);
272 fprintf(fd, "Count=%u\n", count);
275 write_bsr(ua, bsr->next, fd);
280 void print_bsr(UAContext *ua, RBSR *bsr)
283 for (int i=0; i < bsr->VolCount; i++) {
284 bsendmsg(ua, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
285 bsendmsg(ua, "VolSessionId=%u\n", bsr->VolSessionId);
286 bsendmsg(ua, "VolSessionTime=%u\n", bsr->VolSessionTime);
287 bsendmsg(ua, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
288 bsr->VolParams[i].EndFile);
289 bsendmsg(ua, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
290 bsr->VolParams[i].EndBlock);
291 print_findex(ua, bsr->fi);
293 print_bsr(ua, bsr->next);
299 * Add a FileIndex to the list of BootStrap records.
300 * Here we are only dealing with JobId's and the FileIndexes
301 * associated with those JobIds.
303 void add_findex(RBSR *bsr, uint32_t JobId, int32_t findex)
306 RBSR_FINDEX *fi, *lfi;
309 return; /* probably a dummy directory */
312 if (bsr->fi == NULL) { /* if no FI add one */
313 /* This is the first FileIndex item in the chain */
314 bsr->fi = new_findex();
316 bsr->fi->findex = findex;
317 bsr->fi->findex2 = findex;
320 /* Walk down list of bsrs until we find the JobId */
321 if (bsr->JobId != JobId) {
322 for (nbsr=bsr->next; nbsr; nbsr=nbsr->next) {
323 if (nbsr->JobId == JobId) {
329 if (!nbsr) { /* Must add new JobId */
330 /* Add new JobId at end of chain */
331 for (nbsr=bsr; nbsr->next; nbsr=nbsr->next)
333 nbsr->next = new_bsr();
334 nbsr->next->JobId = JobId;
335 nbsr->next->fi = new_findex();
336 nbsr->next->fi->findex = findex;
337 nbsr->next->fi->findex2 = findex;
343 * At this point, bsr points to bsr containing this JobId,
344 * and we are sure that there is at least one fi record.
347 /* Check if this findex is smaller than first item */
348 if (findex < fi->findex) {
349 if ((findex+1) == fi->findex) {
350 fi->findex = findex; /* extend down */
353 fi = new_findex(); /* yes, insert before first item */
355 fi->findex2 = findex;
360 /* Walk down fi chain and find where to insert insert new FileIndex */
361 for ( ; fi; fi=fi->next) {
362 if (findex == (fi->findex2 + 1)) { /* extend up */
364 fi->findex2 = findex;
366 * If the following record contains one higher, merge its
367 * file index by extending it up.
369 if (fi->next && ((findex+1) == fi->next->findex)) {
371 fi->findex2 = nfi->findex2;
372 fi->next = nfi->next;
377 if (findex < fi->findex) { /* add before */
378 if ((findex+1) == fi->findex) {
386 /* Add to last place found */
389 fi->findex2 = findex;
390 fi->next = lfi->next;