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-2003 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 void 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 void write_findex(UAContext *ua, RBSR_FINDEX *fi,
71 int32_t FirstIndex, int32_t LastIndex, FILE *fd)
73 for ( ; fi; fi=fi->next) {
74 int32_t findex, findex2;
75 if ((fi->findex >= FirstIndex && fi->findex <= LastIndex) ||
76 (fi->findex2 >= FirstIndex && fi->findex2 <= LastIndex) ||
77 (fi->findex < FirstIndex && fi->findex2 > LastIndex)) {
78 findex = fi->findex < FirstIndex ? FirstIndex : fi->findex;
79 findex2 = fi->findex2 > LastIndex ? LastIndex : fi->findex2;
80 if (findex == findex2) {
81 fprintf(fd, "FileIndex=%d\n", findex);
83 fprintf(fd, "FileIndex=%d-%d\n", findex, findex2);
90 * Find out if Volume defined with FirstIndex and LastIndex
91 * falls within the range of selected files in the bsr.
93 static bool is_volume_selected(RBSR_FINDEX *fi,
94 int32_t FirstIndex, int32_t LastIndex)
97 if ((fi->findex >= FirstIndex && fi->findex <= LastIndex) ||
98 (fi->findex2 >= FirstIndex && fi->findex2 <= LastIndex) ||
99 (fi->findex < FirstIndex && fi->findex2 > LastIndex)) {
102 return is_volume_selected(fi->next, FirstIndex, LastIndex);
109 static void print_findex(UAContext *ua, RBSR_FINDEX *fi)
111 bsendmsg(ua, "fi=0x%x\n", (unsigned)fi);
112 for ( ; fi; fi=fi->next) {
113 if (fi->findex == fi->findex2) {
114 bsendmsg(ua, "FileIndex=%d\n", fi->findex);
115 // Dmsg1(000, "FileIndex=%d\n", fi->findex);
117 bsendmsg(ua, "FileIndex=%d-%d\n", fi->findex, fi->findex2);
118 // Dmsg2(000, "FileIndex=%d-%d\n", fi->findex, fi->findex2);
123 /* Create a new bootstrap record */
126 RBSR *bsr = (RBSR *)bmalloc(sizeof(RBSR));
127 memset(bsr, 0, sizeof(RBSR));
131 /* Free the entire BSR */
132 void free_bsr(RBSR *bsr)
135 free_findex(bsr->fi);
137 if (bsr->VolParams) {
138 free(bsr->VolParams);
145 * Complete the BSR by filling in the VolumeName and
146 * VolSessionId and VolSessionTime using the JobId
148 int complete_bsr(UAContext *ua, RBSR *bsr)
152 memset(&jr, 0, sizeof(jr));
153 jr.JobId = bsr->JobId;
154 if (!db_get_job_record(ua->jcr, ua->db, &jr)) {
155 bsendmsg(ua, _("Unable to get Job record. ERR=%s\n"), db_strerror(ua->db));
158 bsr->VolSessionId = jr.VolSessionId;
159 bsr->VolSessionTime = jr.VolSessionTime;
160 if ((bsr->VolCount=db_get_job_volume_parameters(ua->jcr, ua->db, bsr->JobId,
161 &(bsr->VolParams))) == 0) {
162 bsendmsg(ua, _("Unable to get Job Volume Parameters. ERR=%s\n"), db_strerror(ua->db));
163 if (bsr->VolParams) {
164 free(bsr->VolParams);
165 bsr->VolParams = NULL;
169 return complete_bsr(ua, bsr->next);
175 * Write the bootstrap record to file
177 int write_bsr_file(UAContext *ua, RBSR *bsr)
180 POOLMEM *fname = get_pool_memory(PM_MESSAGE);
183 Mmsg(&fname, "%s/restore.bsr", working_directory);
184 fd = fopen(fname, "w+");
186 bsendmsg(ua, _("Unable to create bootstrap file %s. ERR=%s\n"),
187 fname, strerror(errno));
188 free_pool_memory(fname);
191 /* Write them to file */
192 write_bsr(ua, bsr, fd);
195 bsendmsg(ua, _("Bootstrap records written to %s\n"), fname);
197 /* Tell the user what he will need to mount */
199 bsendmsg(ua, _("The restore job will require the following Volumes:\n"));
200 /* Create Unique list of Volumes using prompt list */
201 start_prompt(ua, "");
202 for (RBSR *nbsr=bsr; nbsr; nbsr=nbsr->next) {
203 for (int i=0; i < nbsr->VolCount; i++) {
204 if (nbsr->VolParams[i].VolumeName[0]) {
205 add_prompt(ua, nbsr->VolParams[i].VolumeName);
209 for (int i=0; i < ua->num_prompts; i++) {
210 bsendmsg(ua, " %s\n", ua->prompt[i]);
215 free_pool_memory(fname);
219 static void write_bsr(UAContext *ua, RBSR *bsr, FILE *fd)
223 * For a given volume, loop over all the JobMedia records.
224 * VolCount is the number of JobMedia records.
226 for (int i=0; i < bsr->VolCount; i++) {
227 if (!is_volume_selected(bsr->fi, bsr->VolParams[i].FirstIndex,
228 bsr->VolParams[i].LastIndex)) {
229 bsr->VolParams[i].VolumeName[0] = 0; /* zap VolumeName */
232 fprintf(fd, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
233 fprintf(fd, "VolSessionId=%u\n", bsr->VolSessionId);
234 fprintf(fd, "VolSessionTime=%u\n", bsr->VolSessionTime);
235 fprintf(fd, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
236 bsr->VolParams[i].EndFile);
237 fprintf(fd, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
238 bsr->VolParams[i].EndBlock);
240 // Dmsg2(000, "bsr VolParam FI=%u LI=%u\n",
241 // bsr->VolParams[i].FirstIndex, bsr->VolParams[i].LastIndex);
242 write_findex(ua, bsr->fi, bsr->VolParams[i].FirstIndex,
243 bsr->VolParams[i].LastIndex, fd);
245 write_bsr(ua, bsr->next, fd);
249 void print_bsr(UAContext *ua, RBSR *bsr)
252 for (int i=0; i < bsr->VolCount; i++) {
253 bsendmsg(ua, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
254 bsendmsg(ua, "VolSessionId=%u\n", bsr->VolSessionId);
255 bsendmsg(ua, "VolSessionTime=%u\n", bsr->VolSessionTime);
256 bsendmsg(ua, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
257 bsr->VolParams[i].EndFile);
258 bsendmsg(ua, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
259 bsr->VolParams[i].EndBlock);
260 print_findex(ua, bsr->fi);
262 print_bsr(ua, bsr->next);
268 * Add a FileIndex to the list of BootStrap records.
269 * Here we are only dealing with JobId's and the FileIndexes
270 * associated with those JobIds.
272 void add_findex(RBSR *bsr, uint32_t JobId, int32_t findex)
275 RBSR_FINDEX *fi, *lfi;
278 return; /* probably a dummy directory */
281 if (bsr->fi == NULL) { /* if no FI add one */
282 /* This is the first FileIndex item in the chain */
283 bsr->fi = new_findex();
285 bsr->fi->findex = findex;
286 bsr->fi->findex2 = findex;
289 /* Walk down list of bsrs until we find the JobId */
290 if (bsr->JobId != JobId) {
291 for (nbsr=bsr->next; nbsr; nbsr=nbsr->next) {
292 if (nbsr->JobId == JobId) {
298 if (!nbsr) { /* Must add new JobId */
299 /* Add new JobId at end of chain */
300 for (nbsr=bsr; nbsr->next; nbsr=nbsr->next)
302 nbsr->next = new_bsr();
303 nbsr->next->JobId = JobId;
304 nbsr->next->fi = new_findex();
305 nbsr->next->fi->findex = findex;
306 nbsr->next->fi->findex2 = findex;
312 * At this point, bsr points to bsr containing this JobId,
313 * and we are sure that there is at least one fi record.
316 /* Check if this findex is smaller than first item */
317 if (findex < fi->findex) {
318 if ((findex+1) == fi->findex) {
319 fi->findex = findex; /* extend down */
322 fi = new_findex(); /* yes, insert before first item */
324 fi->findex2 = findex;
329 /* Walk down fi chain and find where to insert insert new FileIndex */
330 for ( ; fi; fi=fi->next) {
331 if (findex == (fi->findex2 + 1)) { /* extend up */
333 fi->findex2 = findex;
335 * If the following record contains one higher, merge its
336 * file index by extending it up.
338 if (fi->next && ((findex+1) == fi->next->findex)) {
340 fi->findex2 = nfi->findex2;
341 fi->next = nfi->next;
346 if (findex < fi->findex) { /* add before */
347 if ((findex+1) == fi->findex) {
355 /* Add to last place found */
358 fi->findex2 = findex;
359 fi->next = lfi->next;