]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/label.c
Doc + ignore label errors option in bls
[bacula/bacula] / bacula / src / stored / label.c
1 /*
2  *
3  *  label.c  Bacula routines to handle labels 
4  *
5  *   Kern Sibbald, MM
6  *                            
7  *
8  *   Version $Id$
9  */
10 /*
11    Copyright (C) 2000-2003 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"                   /* pull in global headers */
31 #include "stored.h"                   /* pull in Storage Deamon headers */
32
33 /* Forward referenced functions */
34 static void create_volume_label_record(JCR *jcr, DEVICE *dev, DEV_RECORD *rec);
35
36 extern char my_name[];
37 extern int debug_level;
38
39 /*
40  * Read the volume label
41  *
42  *  If jcr->VolumeName == NULL, we accept any Bacula Volume
43  *  If jcr->VolumeName[0] == 0, we accept any Bacula Volume
44  *  otherwise jcr->VolumeName must match the Volume.
45  *
46  *  If VolName given, ensure that it matches   
47  *
48  *  Returns VOL_  code as defined in record.h
49  *    VOL_NOT_READ
50  *    VOL_OK
51  *    VOL_NO_LABEL
52  *    VOL_IO_ERROR
53  *    VOL_NAME_ERROR
54  *    VOL_CREATE_ERROR
55  *    VOL_VERSION_ERROR
56  *    VOL_LABEL_ERROR
57  *    VOL_NO_MEDIA
58  */  
59 int read_dev_volume_label(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
60 {
61    char *VolName = jcr->VolumeName;
62    DEV_RECORD *record;
63    int ok = 0;
64
65    Dmsg3(100, "Enter read_volume_label device=%s vol=%s dev_Vol=%s\n", 
66       dev_name(dev), VolName, dev->VolHdr.VolName);
67
68    if (dev_state(dev, ST_LABEL)) {       /* did we already read label? */
69       /* Compare Volume Names allow special wild card */
70       if (VolName && *VolName && *VolName != '*' && strcmp(dev->VolHdr.VolName, VolName) != 0) {
71          Mmsg(&jcr->errmsg, _("Wrong Volume mounted on device %s: Wanted %s have %s\n"),
72             dev_name(dev), VolName, dev->VolHdr.VolName);
73          /*
74           * Cancel Job if too many label errors
75           *  => we are in a loop
76           */
77          if (jcr->label_errors++ > 100) {
78             Jmsg(jcr, M_FATAL, 0, "%s", jcr->errmsg);
79          }
80          return jcr->label_status = VOL_NAME_ERROR;
81       }
82       Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
83       return jcr->label_status = VOL_OK;       /* label already read */
84    }
85
86    dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ);  /* set no label, no append */
87
88    if (!rewind_dev(dev)) {
89       Mmsg(&jcr->errmsg, _("Couldn't rewind device %s ERR=%s\n"), dev_name(dev),
90          strerror_dev(dev));
91       return jcr->label_status = VOL_NO_MEDIA;
92    }
93    bstrncpy(dev->VolHdr.Id, "**error**", sizeof(dev->VolHdr.Id));
94
95    /* Read the Volume label block */
96    record = new_record();
97    Dmsg0(90, "Big if statement in read_volume_label\n");
98    if (!read_block_from_dev(jcr, dev, block, NO_BLOCK_NUMBER_CHECK)) { 
99       Mmsg(&jcr->errmsg, _("Volume on %s is not a Bacula labeled Volume, \
100 because:\n   %s"), dev_name(dev), strerror_dev(dev));
101    } else if (!read_record_from_block(block, record)) {
102       Mmsg(&jcr->errmsg, _("Could not read Volume label from block.\n"));
103    } else if (!unser_volume_label(dev, record)) {
104       Mmsg(&jcr->errmsg, _("Could not unserialize Volume label: %s\n"),
105          strerror_dev(dev));
106    } else if (strcmp(dev->VolHdr.Id, BaculaId) != 0 && 
107               strcmp(dev->VolHdr.Id, OldBaculaId) != 0) {
108       Mmsg(&jcr->errmsg, _("Volume Header Id bad: %s\n"), dev->VolHdr.Id);
109    } else {
110       ok = 1;
111    }
112    if (!ok) {
113       free_record(record);
114       if (jcr->ignore_label_errors) {
115          dev->state |= ST_LABEL;      /* set has Bacula label */
116          Jmsg(jcr, M_ERROR, 0, "%s", jcr->errmsg);
117          return jcr->label_status = VOL_OK;
118       }
119       empty_block(block);
120       rewind_dev(dev);
121       return jcr->label_status = VOL_NO_LABEL;
122    }
123
124    free_record(record);
125    /* If we are a streaming device, we only get one chance to read */
126    if (!dev_cap(dev, CAP_STREAM)) {
127       empty_block(block);
128       rewind_dev(dev);
129    }
130
131    if (dev->VolHdr.VerNum != BaculaTapeVersion && 
132        dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion1 &&  
133        dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion2) {
134       Mmsg(&jcr->errmsg, _("Volume on %s has wrong Bacula version. Wanted %d got %d\n"),
135          dev_name(dev), BaculaTapeVersion, dev->VolHdr.VerNum);
136       return jcr->label_status = VOL_VERSION_ERROR;
137    }
138
139    /* We are looking for either an unused Bacula tape (PRE_LABEL) or
140     * a Bacula volume label (VOL_LABEL)
141     */
142    if (dev->VolHdr.LabelType != PRE_LABEL && dev->VolHdr.LabelType != VOL_LABEL) {
143       Mmsg(&jcr->errmsg, _("Volume on %s has bad Bacula label type: %x\n"), 
144           dev_name(dev), dev->VolHdr.LabelType);
145       return jcr->label_status = VOL_LABEL_ERROR;
146    }
147
148    dev->state |= ST_LABEL;            /* set has Bacula label */
149
150    /* Compare Volume Names */
151    Dmsg2(30, "Compare Vol names: VolName=%s hdr=%s\n", VolName?VolName:"*", dev->VolHdr.VolName);
152    if (VolName && *VolName && *VolName != '*' && strcmp(dev->VolHdr.VolName, VolName) != 0) {
153       Mmsg(&jcr->errmsg, _("Wrong Volume mounted on device %s: Wanted %s have %s\n"),
154            dev_name(dev), VolName, dev->VolHdr.VolName);
155       /*
156        * Cancel Job if too many label errors
157        *  => we are in a loop
158        */
159       if (jcr->label_errors++ > 100) {
160          Jmsg(jcr, M_FATAL, 0, "%s", jcr->errmsg);
161       }
162       return jcr->label_status = VOL_NAME_ERROR;
163    }
164    Dmsg1(30, "Copy vol_name=%s\n", dev->VolHdr.VolName);
165
166    if (debug_level >= 10) {
167       dump_volume_label(dev);
168    }
169    Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
170    return jcr->label_status = VOL_OK;
171 }
172
173 /*  unser_volume_label 
174  *  
175  * Unserialize the Volume label into the device Volume_Label
176  * structure.
177  *
178  * Assumes that the record is already read.
179  *
180  * Returns: 0 on error
181  *          1 on success
182 */
183
184 int unser_volume_label(DEVICE *dev, DEV_RECORD *rec)
185 {
186    ser_declare;
187
188    if (rec->FileIndex != VOL_LABEL && rec->FileIndex != PRE_LABEL) {
189       Mmsg3(&dev->errmsg, _("Expecting Volume Label, got FI=%s Stream=%s len=%d\n"), 
190               FI_to_ascii(rec->FileIndex), 
191               stream_to_ascii(rec->Stream, rec->FileIndex),
192               rec->data_len);
193       return 0;
194    }
195
196    dev->VolHdr.LabelType = rec->FileIndex;
197    dev->VolHdr.LabelSize = rec->data_len;
198
199
200    /* Unserialize the record into the Volume Header */
201    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Volume_Label);
202    ser_begin(rec->data, SER_LENGTH_Volume_Label);
203    unser_string(dev->VolHdr.Id);
204    unser_uint32(dev->VolHdr.VerNum);
205
206    if (dev->VolHdr.VerNum >= 11) {
207       unser_btime(dev->VolHdr.label_btime);
208       unser_btime(dev->VolHdr.write_btime);
209    } else { /* old way */ 
210       unser_float64(dev->VolHdr.label_date);
211       unser_float64(dev->VolHdr.label_time);
212    }
213    unser_float64(dev->VolHdr.write_date);    /* Unused with VerNum >= 11 */
214    unser_float64(dev->VolHdr.write_time);    /* Unused with VerNum >= 11 */
215
216    unser_string(dev->VolHdr.VolName);
217    unser_string(dev->VolHdr.PrevVolName);
218    unser_string(dev->VolHdr.PoolName);
219    unser_string(dev->VolHdr.PoolType);
220    unser_string(dev->VolHdr.MediaType);
221
222    unser_string(dev->VolHdr.HostName);
223    unser_string(dev->VolHdr.LabelProg);
224    unser_string(dev->VolHdr.ProgVersion);
225    unser_string(dev->VolHdr.ProgDate);
226
227    ser_end(rec->data, SER_LENGTH_Volume_Label);
228    Dmsg0(90, "ser_read_vol\n");
229    if (debug_level >= 90) {
230       dump_volume_label(dev);      
231    }
232    return 1;
233 }
234
235 /*
236  * Put a volume label into the block
237  *
238  *  Returns: 0 on failure
239  *           1 on success
240  */
241 int write_volume_label_to_block(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
242 {
243    DEV_RECORD rec;
244
245    Dmsg0(20, "write Label in write_volume_label_to_block()\n");
246    memset(&rec, 0, sizeof(rec));
247    rec.data = get_memory(SER_LENGTH_Volume_Label);
248
249    create_volume_label_record(jcr, dev, &rec);
250
251    empty_block(block);                /* Volume label always at beginning */
252    block->BlockNumber = 0;
253    if (!write_record_to_block(block, &rec)) {
254       free_pool_memory(rec.data);
255       Jmsg1(jcr, M_FATAL, 0, _("Cannot write Volume label to block for device %s\n"),
256          dev_name(dev));
257       return 0;
258    } else {
259       Dmsg1(90, "Wrote label of %d bytes to block\n", rec.data_len);
260    }
261    free_pool_memory(rec.data);
262    return 1;
263 }
264
265 /* 
266  *  create_volume_label_record
267  *   Serialize label (from dev->VolHdr structure) into device record.
268  *   Assumes that the dev->VolHdr structure is properly 
269  *   initialized.
270 */
271 static void create_volume_label_record(JCR *jcr, DEVICE *dev, DEV_RECORD *rec)
272 {
273    ser_declare;
274    struct date_time dt;
275
276    /* Serialize the label into the device record. */
277
278    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Volume_Label);
279    ser_begin(rec->data, SER_LENGTH_Volume_Label);
280    ser_string(dev->VolHdr.Id);
281
282    ser_uint32(dev->VolHdr.VerNum);
283
284    if (dev->VolHdr.VerNum >= 11) {
285       ser_btime(dev->VolHdr.label_btime);
286       dev->VolHdr.write_btime = get_current_btime();
287       ser_btime(dev->VolHdr.write_btime);
288       dev->VolHdr.write_date = 0;
289       dev->VolHdr.write_time = 0;
290    } else {
291       /* OLD WAY DEPRECATED */
292       ser_float64(dev->VolHdr.label_date);
293       ser_float64(dev->VolHdr.label_time);
294       get_current_time(&dt);
295       dev->VolHdr.write_date = dt.julian_day_number;
296       dev->VolHdr.write_time = dt.julian_day_fraction;
297    }
298    ser_float64(dev->VolHdr.write_date);   /* 0 if VerNum >= 11 */
299    ser_float64(dev->VolHdr.write_time);   /* 0  if VerNum >= 11 */
300
301    ser_string(dev->VolHdr.VolName);
302    ser_string(dev->VolHdr.PrevVolName);
303    ser_string(dev->VolHdr.PoolName);
304    ser_string(dev->VolHdr.PoolType);
305    ser_string(dev->VolHdr.MediaType);
306
307    ser_string(dev->VolHdr.HostName);
308    ser_string(dev->VolHdr.LabelProg);
309    ser_string(dev->VolHdr.ProgVersion);
310    ser_string(dev->VolHdr.ProgDate);
311
312    ser_end(rec->data, SER_LENGTH_Volume_Label);
313    rec->data_len = ser_length(rec->data);
314    rec->FileIndex = dev->VolHdr.LabelType;
315    rec->VolSessionId = jcr->VolSessionId;
316    rec->VolSessionTime = jcr->VolSessionTime;
317    rec->Stream = jcr->NumVolumes;
318    Dmsg2(100, "Created Vol label rec: FI=%s len=%d\n", FI_to_ascii(rec->FileIndex),
319       rec->data_len);
320 }     
321
322
323 /*
324  * Create a volume label in memory
325  *  Returns: 0 on error
326  *           1 on success
327  */
328 void create_volume_label(DEVICE *dev, char *VolName, char *PoolName)
329 {
330    struct date_time dt;
331    DEVRES *device = (DEVRES *)dev->device;
332
333    Dmsg0(90, "Start create_volume_label()\n");
334
335    ASSERT(dev != NULL);
336
337    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
338
339    /* ***FIXME*** we really need to get the volume name,    
340     * pool name, and pool type from the database.
341     */
342    bstrncpy(dev->VolHdr.Id, BaculaId, sizeof(dev->VolHdr.Id));
343    dev->VolHdr.VerNum = BaculaTapeVersion;
344    dev->VolHdr.LabelType = PRE_LABEL;  /* Mark tape as unused */
345    bstrncpy(dev->VolHdr.VolName, VolName, sizeof(dev->VolHdr.VolName));
346    bstrncpy(dev->VolHdr.PoolName, PoolName, sizeof(dev->VolHdr.PoolName));
347    bstrncpy(dev->VolHdr.MediaType, device->media_type, sizeof(dev->VolHdr.MediaType));
348
349    bstrncpy(dev->VolHdr.PoolType, "Backup", sizeof(dev->VolHdr.PoolType));
350
351    /* Put label time/date in header */
352    if (BaculaTapeVersion >= 11) {
353       dev->VolHdr.label_btime = get_current_btime();
354       dev->VolHdr.label_date = 0;
355       dev->VolHdr.label_time = 0;
356    } else {
357       /* OLD WAY DEPRECATED */
358       get_current_time(&dt);
359       dev->VolHdr.label_date = dt.julian_day_number;
360       dev->VolHdr.label_time = dt.julian_day_fraction;
361    }
362
363    if (gethostname(dev->VolHdr.HostName, sizeof(dev->VolHdr.HostName)) != 0) {
364       dev->VolHdr.HostName[0] = 0;
365    }
366    bstrncpy(dev->VolHdr.LabelProg, my_name, sizeof(dev->VolHdr.LabelProg));
367    sprintf(dev->VolHdr.ProgVersion, "Ver. %s %s", VERSION, BDATE);
368    sprintf(dev->VolHdr.ProgDate, "Build %s %s", __DATE__, __TIME__);
369    dev->state |= ST_LABEL;            /* set has Bacula label */
370    if (debug_level >= 90) {
371       dump_volume_label(dev);
372    }
373 }
374
375 /*
376  * Write a Volume Label
377  *  !!! Note, this is ONLY used for writing
378  *            a fresh volume label.  Any data
379  *            after the label will be destroyed,
380  *            in fact, we write the label 5 times !!!!
381  * 
382  *  This routine expects that open_device() was previously called.
383  *
384  *  This routine should be used only when labeling a blank tape.
385  */
386 int write_volume_label_to_dev(JCR *jcr, DEVRES *device, char *VolName, char *PoolName)
387 {
388    DEVICE *dev = device->dev;
389    DEV_RECORD rec;   
390    DEV_BLOCK *block;
391    int stat = 1;
392
393
394    Dmsg0(99, "write_volume_label()\n");
395    create_volume_label(dev, VolName, PoolName);
396
397    if (!rewind_dev(dev)) {
398       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
399       Dmsg2(30, "Bad status on %s from rewind. ERR=%s\n", dev_name(dev), strerror_dev(dev));
400       return 0;
401    }
402
403    block = new_block(dev);
404    memset(&rec, 0, sizeof(rec));
405    rec.data = get_memory(SER_LENGTH_Volume_Label);
406    create_volume_label_record(jcr, dev, &rec);
407    rec.Stream = 0;
408
409    if (!write_record_to_block(block, &rec)) {
410       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
411       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
412       free_block(block);
413       free_pool_memory(rec.data);
414       return 0;
415    } else {
416       Dmsg2(30, "Wrote label of %d bytes to %s\n", rec.data_len, dev_name(dev));
417    }
418    free_pool_memory(rec.data);
419       
420    Dmsg0(99, "Call write_block_to_device()\n");
421    if (!write_block_to_dev(jcr, dev, block)) {
422       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
423       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
424       stat = 9;
425    }
426    Dmsg0(99, " Wrote block to device\n");
427      
428    flush_dev(dev);
429    weof_dev(dev, 1);
430    dev->state |= ST_LABEL;
431
432    if (debug_level >= 20)  {
433       dump_volume_label(dev);
434    }
435    free_block(block);
436    return stat;
437 }     
438
439
440 /*
441  * Create session label
442  *  The pool memory must be released by the calling program
443  */
444 void create_session_label(JCR *jcr, DEV_RECORD *rec, int label)
445 {
446    ser_declare;
447
448    rec->VolSessionId   = jcr->VolSessionId;
449    rec->VolSessionTime = jcr->VolSessionTime;
450    rec->Stream         = jcr->JobId;
451
452    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Session_Label);
453    ser_begin(rec->data, SER_LENGTH_Session_Label);
454    ser_string(BaculaId);
455    ser_uint32(BaculaTapeVersion);
456
457    ser_uint32(jcr->JobId);
458
459    /* Changed in VerNum 11 */
460    ser_btime(get_current_btime());
461    ser_float64(0);
462
463    ser_string(jcr->pool_name);
464    ser_string(jcr->pool_type);
465    ser_string(jcr->job_name);         /* base Job name */
466    ser_string(jcr->client_name);
467
468    /* Added in VerNum 10 */
469    ser_string(jcr->Job);              /* Unique name of this Job */
470    ser_string(jcr->fileset_name);
471    ser_uint32(jcr->JobType);
472    ser_uint32(jcr->JobLevel);
473    /* Added in VerNum 11 */
474    ser_string(jcr->fileset_md5);
475
476    if (label == EOS_LABEL) {
477       ser_uint32(jcr->JobFiles);
478       ser_uint64(jcr->JobBytes);
479       ser_uint32(jcr->StartBlock);
480       ser_uint32(jcr->EndBlock);
481       ser_uint32(jcr->StartFile);
482       ser_uint32(jcr->EndFile);
483       ser_uint32(jcr->JobErrors);
484
485       /* Added in VerNum 11 */
486       ser_uint32(jcr->JobStatus);
487    }
488    ser_end(rec->data, SER_LENGTH_Session_Label);
489    rec->data_len = ser_length(rec->data);
490 }
491
492 /* Write session label
493  *  Returns: 0 on failure
494  *           1 on success 
495  */
496 int write_session_label(JCR *jcr, DEV_BLOCK *block, int label)
497 {
498    DEVICE *dev = jcr->device->dev;
499    DEV_RECORD *rec;
500
501    rec = new_record();
502    Dmsg1(90, "session_label record=%x\n", rec);
503    switch (label) {
504    case SOS_LABEL:
505       if (dev->state & ST_TAPE) {
506          jcr->StartBlock = dev->block_num;
507          jcr->StartFile  = dev->file;
508       } else {
509          jcr->StartBlock = (uint32_t)dev->file_addr;
510          jcr->StartFile = (uint32_t)(dev->file_addr >> 32);
511       }
512       break;
513    case EOS_LABEL:
514       if (dev->state & ST_TAPE) {
515          jcr->EndBlock = dev->EndBlock;
516          jcr->EndFile  = dev->EndFile;
517       } else {
518          jcr->EndBlock = (uint32_t)dev->file_addr;
519          jcr->EndFile = (uint32_t)(dev->file_addr >> 32);
520       }
521       break;
522    default:
523       Jmsg1(jcr, M_ABORT, 0, _("Bad session label = %d\n"), label);
524       break;
525    }
526    create_session_label(jcr, rec, label);
527    rec->FileIndex = label;
528
529    /* 
530     * We guarantee that the session record can totally fit
531     *  into a block. If not, write the block, and put it in
532     *  the next block. Having the sesssion record totally in
533     *  one block makes reading them much easier (no need to
534     *  read the next block).
535     */
536    if (!can_write_record_to_block(block, rec)) {
537       Dmsg0(100, "Cannot write session label to block.\n");
538       if (!write_block_to_device(jcr, dev, block)) {
539          Dmsg0(90, "Got session label write_block_to_dev error.\n");
540          Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"), 
541                            dev_vol_name(dev), strerror(errno));
542          free_record(rec);
543          return 0;
544       }
545    }
546    if (!write_record_to_block(block, rec)) {
547       Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"), 
548                         dev_vol_name(dev), strerror(errno));
549       free_record(rec);
550       return 0;
551    }
552
553    Dmsg6(20, "Write sesson_label record JobId=%d FI=%s SessId=%d Strm=%s len=%d\n\
554 remainder=%d\n", jcr->JobId,
555       FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
556       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
557       rec->remainder);
558
559    free_record(rec);
560    Dmsg2(20, "Leave write_session_label Block=%d File=%d\n", 
561       dev->block_num, dev->file);
562    return 1;
563 }
564
565 void dump_volume_label(DEVICE *dev)
566 {
567    int dbl = debug_level;
568    uint32_t File;
569    char *LabelType, buf[30];
570    struct tm tm;
571    struct date_time dt;
572
573    debug_level = 1;
574    File = dev->file;
575    switch (dev->VolHdr.LabelType) {
576       case PRE_LABEL:
577          LabelType = "PRE_LABEL";
578          break;
579       case VOL_LABEL:
580          LabelType = "VOL_LABEL";
581          break;
582       case EOM_LABEL:
583          LabelType = "EOM_LABEL";
584          break;
585       case SOS_LABEL:
586          LabelType = "SOS_LABEL";
587          break;
588       case EOS_LABEL:
589          LabelType = "EOS_LABEL";
590          break;
591       case EOT_LABEL:
592          goto bail_out;
593       default:
594          LabelType = buf;
595          sprintf(buf, "Unknown %d", dev->VolHdr.LabelType);
596          break;
597    }
598               
599    
600    Pmsg11(-1, "\nVolume Label:\n\
601 Id                : %s\
602 VerNo             : %d\n\
603 VolName           : %s\n\
604 PrevVolName       : %s\n\
605 VolFile           : %d\n\
606 LabelType         : %s\n\
607 LabelSize         : %d\n\
608 PoolName          : %s\n\
609 MediaType         : %s\n\
610 PoolType          : %s\n\
611 HostName          : %s\n\
612 ",
613              dev->VolHdr.Id, dev->VolHdr.VerNum,
614              dev->VolHdr.VolName, dev->VolHdr.PrevVolName,
615              File, LabelType, dev->VolHdr.LabelSize, 
616              dev->VolHdr.PoolName, dev->VolHdr.MediaType, 
617              dev->VolHdr.PoolType, dev->VolHdr.HostName);
618
619    if (dev->VolHdr.VerNum >= 11) {
620       char dt[50];
621       bstrftime(dt, sizeof(dt), btime_to_unix(dev->VolHdr.label_btime));
622       Pmsg1(-1, "Date label written: %s\n", dt);
623    } else {
624    dt.julian_day_number   = dev->VolHdr.label_date;
625    dt.julian_day_fraction = dev->VolHdr.label_time;
626    tm_decode(&dt, &tm);
627    Pmsg5(-1, "\
628 Date label written: %04d-%02d-%02d at %02d:%02d\n", 
629       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
630    }
631
632 bail_out:
633    debug_level = dbl;
634 }
635
636 int unser_session_label(SESSION_LABEL *label, DEV_RECORD *rec) 
637 {
638    ser_declare;
639
640    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Session_Label);
641    unser_begin(rec->data, SER_LENGTH_Session_Label);
642    unser_string(label->Id);
643    unser_uint32(label->VerNum);
644    unser_uint32(label->JobId);
645    if (label->VerNum >= 11) {
646       unser_btime(label->write_btime);
647    } else {
648       unser_float64(label->write_date);
649    }
650    unser_float64(label->write_time);
651    unser_string(label->PoolName);
652    unser_string(label->PoolType);
653    unser_string(label->JobName);
654    unser_string(label->ClientName);
655    if (label->VerNum >= 10) {
656       unser_string(label->Job);          /* Unique name of this Job */
657       unser_string(label->FileSetName);
658       unser_uint32(label->JobType);
659       unser_uint32(label->JobLevel);
660    }
661    if (label->VerNum >= 11) {
662       unser_string(label->FileSetMD5);
663    } else {
664       label->FileSetMD5[0] = 0;
665    }
666    if (rec->FileIndex == EOS_LABEL) {
667       unser_uint32(label->JobFiles);
668       unser_uint64(label->JobBytes);
669       unser_uint32(label->StartBlock);
670       unser_uint32(label->EndBlock);
671       unser_uint32(label->StartFile);
672       unser_uint32(label->EndFile);
673       unser_uint32(label->JobErrors);
674       if (label->VerNum >= 11) {
675          unser_uint32(label->JobStatus);
676       } else {
677          label->JobStatus = JS_Terminated; /* kludge */
678       }
679    }      
680    return 1;
681 }
682
683
684 static void dump_session_label(DEV_RECORD *rec, char *type)
685 {
686    int dbl;
687    struct date_time dt;
688    struct tm tm;
689    SESSION_LABEL label;
690    char ec1[30], ec2[30], ec3[30], ec4[30], ec5[30], ec6[30], ec7[30];
691
692    unser_session_label(&label, rec);
693    dbl = debug_level;
694    debug_level = 1;
695    Pmsg7(-1, "\n%s Record:\n\
696 JobId             : %d\n\
697 VerNum            : %d\n\
698 PoolName          : %s\n\
699 PoolType          : %s\n\
700 JobName           : %s\n\
701 ClientName        : %s\n\
702 ",    type, label.JobId, label.VerNum,
703       label.PoolName, label.PoolType,
704       label.JobName, label.ClientName);
705
706    if (label.VerNum >= 10) {
707       Pmsg4(-1, "\
708 Job (unique name) : %s\n\
709 FileSet           : %s\n\
710 JobType           : %c\n\
711 JobLevel          : %c\n\
712 ", label.Job, label.FileSetName, label.JobType, label.JobLevel);
713    }
714
715    if (rec->FileIndex == EOS_LABEL) {
716       Pmsg8(-1, "\
717 JobFiles          : %s\n\
718 JobBytes          : %s\n\
719 StartBlock        : %s\n\
720 EndBlock          : %s\n\
721 StartFile         : %s\n\
722 EndFile           : %s\n\
723 JobErrors         : %s\n\
724 JobStatus         : %c\n\
725 ",
726          edit_uint64_with_commas(label.JobFiles, ec1),
727          edit_uint64_with_commas(label.JobBytes, ec2),
728          edit_uint64_with_commas(label.StartBlock, ec3),
729          edit_uint64_with_commas(label.EndBlock, ec4),
730          edit_uint64_with_commas(label.StartFile, ec5),
731          edit_uint64_with_commas(label.EndFile, ec6),
732          edit_uint64_with_commas(label.JobErrors, ec7), 
733          label.JobStatus);
734    }
735    if (label.VerNum >= 11) {
736       char dt[50];
737       bstrftime(dt, sizeof(dt), btime_to_unix(label.write_btime));
738       Pmsg1(-1, _("Date written      : %s\n"), dt);
739    } else {
740       dt.julian_day_number   = label.write_date;
741       dt.julian_day_fraction = label.write_time;
742       tm_decode(&dt, &tm);
743       Pmsg5(-1, _("\
744 Date written      : %04d-%02d-%02d at %02d:%02d\n"),
745       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
746    }
747
748    debug_level = dbl;
749 }
750
751 void dump_label_record(DEVICE *dev, DEV_RECORD *rec, int verbose)
752 {
753    char *type;
754    int dbl;
755
756    dbl = debug_level;
757    debug_level = 1;
758    switch (rec->FileIndex) {
759       case PRE_LABEL:
760          type = _("Fresh Volume");   
761          break;
762       case VOL_LABEL:
763          type = _("Volume");
764          break;
765       case SOS_LABEL:
766          type = _("Begin Session");
767          break;
768       case EOS_LABEL:
769          type = _("End Session");
770          break;
771       case EOM_LABEL:
772          type = _("End of Media");
773          break;
774       case EOT_LABEL:
775          type = ("End of Tape");
776          break;
777       default:
778          type = _("Unknown");
779          break;
780    }
781    if (verbose) {
782       switch (rec->FileIndex) {
783          case PRE_LABEL:
784          case VOL_LABEL:
785             unser_volume_label(dev, rec);
786             dump_volume_label(dev);
787             break;
788          case SOS_LABEL:
789             dump_session_label(rec, type);
790             break;
791          case EOS_LABEL:
792             dump_session_label(rec, type);
793             break;
794          case EOM_LABEL:
795             Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
796                type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
797             break;
798          case EOT_LABEL:
799             Pmsg0(-1, _("End of physical tape.\n"));
800             break;
801          default:
802             Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
803                type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
804             break;
805       }
806    } else {
807       switch (rec->FileIndex) {
808          case SOS_LABEL:
809          case EOS_LABEL:
810             SESSION_LABEL label;
811             unser_session_label(&label, rec);
812             Pmsg6(-1, "%s Record: SessId=%d SessTime=%d JobId=%d Level=%c \
813 Type=%c\n",
814                type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, 
815                label.JobLevel, label.JobType);
816             break;
817          case EOM_LABEL:
818          case PRE_LABEL:
819          case VOL_LABEL:
820          default:
821             Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
822          type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
823             break;
824          case EOT_LABEL:
825             break;
826       }
827    }
828    debug_level = dbl;
829 }