]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/bsr.c
dd6f4b0a0e78aabb91297bfbef27dcea9a8fffb3
[bacula/bacula] / bacula / src / dird / bsr.c
1 /*
2  *
3  *   Bacula Director -- Bootstrap Record routines.
4  *
5  *      BSR (bootstrap record) handling routines split from
6  *        ua_restore.c July MMIII
7  *
8  *     Kern Sibbald, July MMII
9  *
10  *   Version $Id$
11  */
12
13 /*
14    Copyright (C) 2002-2005 Kern Sibbald
15
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.
20
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.
25
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,
29    MA 02111-1307, USA.
30
31  */
32
33 #include "bacula.h"
34 #include "dird.h"
35
36 /* Forward referenced functions */
37 static uint32_t write_bsr(UAContext *ua, RBSR *bsr, FILE *fd);
38 void print_bsr(UAContext *ua, RBSR *bsr);
39
40
41 /*
42  * Create new FileIndex entry for BSR
43  */
44 RBSR_FINDEX *new_findex()
45 {
46    RBSR_FINDEX *fi = (RBSR_FINDEX *)bmalloc(sizeof(RBSR_FINDEX));
47    memset(fi, 0, sizeof(RBSR_FINDEX));
48    return fi;
49 }
50
51 /* Free all BSR FileIndex entries */
52 static void free_findex(RBSR_FINDEX *fi)
53 {
54    if (fi) {
55       free_findex(fi->next);
56       free(fi);
57    }
58 }
59
60 /*
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.
66  *
67  * We are called here once for each JobMedia record
68  *  for each Volume.
69  */
70 static uint32_t write_findex(UAContext *ua, RBSR_FINDEX *fi,
71               int32_t FirstIndex, int32_t LastIndex, FILE *fd)
72 {
73    uint32_t count = 0;
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);
83             count++;
84          } else {
85             fprintf(fd, "FileIndex=%d-%d\n", findex, findex2);
86             count += findex2 - findex + 1;
87          }
88       }
89    }
90    return count;
91 }
92
93 /*
94  * Find out if Volume defined with FirstIndex and LastIndex
95  *   falls within the range of selected files in the bsr.
96  */
97 static bool is_volume_selected(RBSR_FINDEX *fi,
98               int32_t FirstIndex, int32_t LastIndex)
99 {
100    if (fi) {
101       if ((fi->findex >= FirstIndex && fi->findex <= LastIndex) ||
102           (fi->findex2 >= FirstIndex && fi->findex2 <= LastIndex) ||
103           (fi->findex < FirstIndex && fi->findex2 > LastIndex)) {
104          return true;
105       }
106       return is_volume_selected(fi->next, FirstIndex, LastIndex);
107    }
108    return false;
109 }
110
111
112
113 static void print_findex(UAContext *ua, RBSR_FINDEX *fi)
114 {
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);
120       } else {
121          bsendmsg(ua, "FileIndex=%d-%d\n", fi->findex, fi->findex2);
122 //       Dmsg2(100, "FileIndex=%d-%d\n", fi->findex, fi->findex2);
123       }
124    }
125 }
126
127 /* Create a new bootstrap record */
128 RBSR *new_bsr()
129 {
130    RBSR *bsr = (RBSR *)bmalloc(sizeof(RBSR));
131    memset(bsr, 0, sizeof(RBSR));
132    return bsr;
133 }
134
135 /* Free the entire BSR */
136 void free_bsr(RBSR *bsr)
137 {
138    if (bsr) {
139       free_findex(bsr->fi);
140       if (bsr->VolParams) {
141          free(bsr->VolParams);
142       }
143       free_bsr(bsr->next);
144       free(bsr);
145    }
146 }
147
148 /*
149  * Complete the BSR by filling in the VolumeName and
150  *  VolSessionId and VolSessionTime using the JobId
151  */
152 int complete_bsr(UAContext *ua, RBSR *bsr)
153 {
154    if (bsr) {
155       JOB_DBR jr;
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));
160          return 0;
161       }
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;
170          }
171          return 0;
172       }
173       return complete_bsr(ua, bsr->next);
174    }
175    return 1;
176 }
177
178 /*
179  * Write the bootstrap records to file
180  */
181 uint32_t write_bsr_file(UAContext *ua, RBSR *bsr)
182 {
183    FILE *fd;
184    POOLMEM *fname = get_pool_memory(PM_MESSAGE);
185    uint32_t count = 0;;
186    bool err;
187
188    Mmsg(fname, "%s/restore.bsr", working_directory);
189    fd = fopen(fname, "w+");
190    if (!fd) {
191       berrno be;
192       bsendmsg(ua, _("Unable to create bootstrap file %s. ERR=%s\n"),
193          fname, be.strerror());
194       goto bail_out;
195    }
196    /* Write them to file */
197    count = write_bsr(ua, bsr, fd);
198    err = ferror(fd);
199    fclose(fd);
200    if (err) {
201       bsendmsg(ua, _("Error writing bsr file.\n"));
202       count = 0;
203       goto bail_out;
204    }
205
206
207    bsendmsg(ua, _("Bootstrap records written to %s\n"), fname);
208
209    /* Tell the user what he will need to mount */
210    bsendmsg(ua, "\n");
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);
218          }
219       }
220    }
221    for (int i=0; i < ua->num_prompts; i++) {
222       bsendmsg(ua, "   %s\n", ua->prompt[i]);
223       free(ua->prompt[i]);
224    }
225    if (ua->num_prompts == 0) {
226       bsendmsg(ua, _("No Volumes found to restore.\n"));
227       count = 0;
228    }
229    ua->num_prompts = 0;
230    bsendmsg(ua, "\n");
231
232 bail_out:
233    free_pool_memory(fname);
234    return count;
235 }
236
237 static uint32_t write_bsr(UAContext *ua, RBSR *bsr, FILE *fd)
238 {
239    uint32_t count = 0;
240    uint32_t total_count = 0;
241    uint32_t LastIndex = 0;
242    bool first = true;
243    if (bsr) {
244       /*
245        * For a given volume, loop over all the JobMedia records.
246        *   VolCount is the number of JobMedia records.
247        */
248       for (int i=0; i < bsr->VolCount; i++) {
249          if (!is_volume_selected(bsr->fi, bsr->VolParams[i].FirstIndex,
250               bsr->VolParams[i].LastIndex)) {
251             bsr->VolParams[i].VolumeName[0] = 0;  /* zap VolumeName */
252             continue;
253          }
254          fprintf(fd, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
255          fprintf(fd, "MediaType=\"%s\"\n", bsr->VolParams[i].MediaType);
256          fprintf(fd, "VolSessionId=%u\n", bsr->VolSessionId);
257          fprintf(fd, "VolSessionTime=%u\n", bsr->VolSessionTime);
258          if (bsr->VolParams[i].StartFile == bsr->VolParams[i].EndFile) {
259             fprintf(fd, "VolFile=%u\n", bsr->VolParams[i].StartFile);
260          } else {
261             fprintf(fd, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
262                     bsr->VolParams[i].EndFile);
263          }
264          if (bsr->VolParams[i].StartBlock == bsr->VolParams[i].EndBlock) {
265             fprintf(fd, "VolFile=%u\n", bsr->VolParams[i].StartBlock);
266          } else {
267             fprintf(fd, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
268                     bsr->VolParams[i].EndBlock);
269          }
270 //       Dmsg2(100, "bsr VolParam FI=%u LI=%u\n",
271 //          bsr->VolParams[i].FirstIndex, bsr->VolParams[i].LastIndex);
272
273          count = write_findex(ua, bsr->fi, bsr->VolParams[i].FirstIndex,
274                               bsr->VolParams[i].LastIndex, fd);
275          if (count) {
276             fprintf(fd, "Count=%u\n", count);
277          }
278          total_count += count;
279          /* If the same file is present on two tapes or in two files
280           *   on a tape, it is a continuation, and should not be treated
281           *   twice in the totals.
282           */
283          if (!first && LastIndex == bsr->VolParams[i].FirstIndex) {
284             total_count--;
285          }
286          first = false;
287          LastIndex = bsr->VolParams[i].LastIndex;
288       }
289       write_bsr(ua, bsr->next, fd);
290    }
291    return total_count;
292 }
293
294 void print_bsr(UAContext *ua, RBSR *bsr)
295 {
296    if (bsr) {
297       for (int i=0; i < bsr->VolCount; i++) {
298          bsendmsg(ua, "Volume=\"%s\"\n", bsr->VolParams[i].VolumeName);
299          bsendmsg(ua, "MediaType\"%s\"\n", bsr->VolParams[i].MediaType);
300          bsendmsg(ua, "VolSessionId=%u\n", bsr->VolSessionId);
301          bsendmsg(ua, "VolSessionTime=%u\n", bsr->VolSessionTime);
302          bsendmsg(ua, "VolFile=%u-%u\n", bsr->VolParams[i].StartFile,
303                   bsr->VolParams[i].EndFile);
304          bsendmsg(ua, "VolBlock=%u-%u\n", bsr->VolParams[i].StartBlock,
305                   bsr->VolParams[i].EndBlock);
306          print_findex(ua, bsr->fi);
307       }
308       print_bsr(ua, bsr->next);
309    }
310 }
311
312
313
314
315 /*
316  * Add a FileIndex to the list of BootStrap records.
317  *  Here we are only dealing with JobId's and the FileIndexes
318  *  associated with those JobIds.
319  */
320 void add_findex(RBSR *bsr, uint32_t JobId, int32_t findex)
321 {
322    RBSR *nbsr;
323    RBSR_FINDEX *fi, *lfi;
324
325    if (findex == 0) {
326       return;                         /* probably a dummy directory */
327    }
328
329    if (bsr->fi == NULL) {             /* if no FI add one */
330       /* This is the first FileIndex item in the chain */
331       bsr->fi = new_findex();
332       bsr->JobId = JobId;
333       bsr->fi->findex = findex;
334       bsr->fi->findex2 = findex;
335       return;
336    }
337    /* Walk down list of bsrs until we find the JobId */
338    if (bsr->JobId != JobId) {
339       for (nbsr=bsr->next; nbsr; nbsr=nbsr->next) {
340          if (nbsr->JobId == JobId) {
341             bsr = nbsr;
342             break;
343          }
344       }
345
346       if (!nbsr) {                    /* Must add new JobId */
347          /* Add new JobId at end of chain */
348          for (nbsr=bsr; nbsr->next; nbsr=nbsr->next)
349             {  }
350          nbsr->next = new_bsr();
351          nbsr->next->JobId = JobId;
352          nbsr->next->fi = new_findex();
353          nbsr->next->fi->findex = findex;
354          nbsr->next->fi->findex2 = findex;
355          return;
356       }
357    }
358
359    /*
360     * At this point, bsr points to bsr containing this JobId,
361     *  and we are sure that there is at least one fi record.
362     */
363    lfi = fi = bsr->fi;
364    /* Check if this findex is smaller than first item */
365    if (findex < fi->findex) {
366       if ((findex+1) == fi->findex) {
367          fi->findex = findex;         /* extend down */
368          return;
369       }
370       fi = new_findex();              /* yes, insert before first item */
371       fi->findex = findex;
372       fi->findex2 = findex;
373       fi->next = lfi;
374       bsr->fi = fi;
375       return;
376    }
377    /* Walk down fi chain and find where to insert insert new FileIndex */
378    for ( ; fi; fi=fi->next) {
379       if (findex == (fi->findex2 + 1)) {  /* extend up */
380          RBSR_FINDEX *nfi;
381          fi->findex2 = findex;
382          /*
383           * If the following record contains one higher, merge its
384           *   file index by extending it up.
385           */
386          if (fi->next && ((findex+1) == fi->next->findex)) {
387             nfi = fi->next;
388             fi->findex2 = nfi->findex2;
389             fi->next = nfi->next;
390             free(nfi);
391          }
392          return;
393       }
394       if (findex < fi->findex) {      /* add before */
395          if ((findex+1) == fi->findex) {
396             fi->findex = findex;
397             return;
398          }
399          break;
400       }
401       lfi = fi;
402    }
403    /* Add to last place found */
404    fi = new_findex();
405    fi->findex = findex;
406    fi->findex2 = findex;
407    fi->next = lfi->next;
408    lfi->next = fi;
409    return;
410 }
411
412 /*
413  * Add all possible  FileIndexes to the list of BootStrap records.
414  *  Here we are only dealing with JobId's and the FileIndexes
415  *  associated with those JobIds.
416  */
417 void add_findex_all(RBSR *bsr, uint32_t JobId)
418 {
419    RBSR *nbsr;
420    RBSR_FINDEX *fi;
421
422    if (bsr->fi == NULL) {             /* if no FI add one */
423       /* This is the first FileIndex item in the chain */
424       bsr->fi = new_findex();
425       bsr->JobId = JobId;
426       bsr->fi->findex = 1;
427       bsr->fi->findex2 = INT32_MAX;
428       return;
429    }
430    /* Walk down list of bsrs until we find the JobId */
431    if (bsr->JobId != JobId) {
432       for (nbsr=bsr->next; nbsr; nbsr=nbsr->next) {
433          if (nbsr->JobId == JobId) {
434             bsr = nbsr;
435             break;
436          }
437       }
438
439       if (!nbsr) {                    /* Must add new JobId */
440          /* Add new JobId at end of chain */
441          for (nbsr=bsr; nbsr->next; nbsr=nbsr->next)
442             {  }
443          nbsr->next = new_bsr();
444          nbsr->next->JobId = JobId;
445          nbsr->next->fi = new_findex();
446          nbsr->next->fi->findex = 1;
447          nbsr->next->fi->findex2 = INT32_MAX;
448          return;
449       }
450    }
451
452    /*
453     * At this point, bsr points to bsr containing this JobId,
454     *  and we are sure that there is at least one fi record.
455     */
456    fi = bsr->fi;
457    fi->findex = 1;
458    fi->findex2 = INT32_MAX;
459    return;
460 }
461