]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/label.c
More data spooling updates
[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    DEVRES *device = (DEVRES *)dev->device;
331
332    Dmsg0(90, "Start create_volume_label()\n");
333
334    ASSERT(dev != NULL);
335
336    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
337
338    bstrncpy(dev->VolHdr.Id, BaculaId, sizeof(dev->VolHdr.Id));
339    dev->VolHdr.VerNum = BaculaTapeVersion;
340    dev->VolHdr.LabelType = PRE_LABEL;  /* Mark tape as unused */
341    bstrncpy(dev->VolHdr.VolName, VolName, sizeof(dev->VolHdr.VolName));
342    bstrncpy(dev->VolHdr.PoolName, PoolName, sizeof(dev->VolHdr.PoolName));
343    bstrncpy(dev->VolHdr.MediaType, device->media_type, sizeof(dev->VolHdr.MediaType));
344
345    bstrncpy(dev->VolHdr.PoolType, "Backup", sizeof(dev->VolHdr.PoolType));
346
347    dev->VolHdr.label_btime = get_current_btime();
348    dev->VolHdr.label_date = 0;
349    dev->VolHdr.label_time = 0;
350
351    if (gethostname(dev->VolHdr.HostName, sizeof(dev->VolHdr.HostName)) != 0) {
352       dev->VolHdr.HostName[0] = 0;
353    }
354    bstrncpy(dev->VolHdr.LabelProg, my_name, sizeof(dev->VolHdr.LabelProg));
355    sprintf(dev->VolHdr.ProgVersion, "Ver. %s %s", VERSION, BDATE);
356    sprintf(dev->VolHdr.ProgDate, "Build %s %s", __DATE__, __TIME__);
357    dev->state |= ST_LABEL;            /* set has Bacula label */
358    if (debug_level >= 90) {
359       dump_volume_label(dev);
360    }
361 }
362
363 /*
364  * Write a Volume Label
365  *  !!! Note, this is ONLY used for writing
366  *            a fresh volume label.  Any data
367  *            after the label will be destroyed,
368  *            in fact, we write the label 5 times !!!!
369  * 
370  *  This routine expects that open_device() was previously called.
371  *
372  *  This routine should be used only when labeling a blank tape.
373  */
374 int write_volume_label_to_dev(JCR *jcr, DEVRES *device, char *VolName, char *PoolName)
375 {
376    DEVICE *dev = device->dev;
377    DEV_RECORD rec;   
378    DEV_BLOCK *block;
379    int stat = 1;
380
381
382    Dmsg0(99, "write_volume_label()\n");
383    create_volume_label(dev, VolName, PoolName);
384
385    if (!rewind_dev(dev)) {
386       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
387       Dmsg2(30, "Bad status on %s from rewind. ERR=%s\n", dev_name(dev), strerror_dev(dev));
388       return 0;
389    }
390
391    block = new_block(dev);
392    memset(&rec, 0, sizeof(rec));
393    rec.data = get_memory(SER_LENGTH_Volume_Label);
394    create_volume_label_record(jcr, dev, &rec);
395    rec.Stream = 0;
396
397    if (!write_record_to_block(block, &rec)) {
398       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
399       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
400       free_block(block);
401       free_pool_memory(rec.data);
402       return 0;
403    } else {
404       Dmsg2(30, "Wrote label of %d bytes to %s\n", rec.data_len, dev_name(dev));
405    }
406    free_pool_memory(rec.data);
407       
408    Dmsg0(99, "Call write_block_to_dev()\n");
409    if (!write_block_to_dev(jcr->dcr, block)) {
410       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
411       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
412       stat = 9;
413    }
414    Dmsg0(99, " Wrote block to device\n");
415      
416    flush_dev(dev);
417    weof_dev(dev, 1);
418    dev->state |= ST_LABEL;
419
420    if (debug_level >= 20)  {
421       dump_volume_label(dev);
422    }
423    free_block(block);
424    return stat;
425 }     
426
427
428 /*
429  * Create session label
430  *  The pool memory must be released by the calling program
431  */
432 void create_session_label(JCR *jcr, DEV_RECORD *rec, int label)
433 {
434    DCR *dcr = jcr->dcr;
435    ser_declare;
436
437    rec->VolSessionId   = jcr->VolSessionId;
438    rec->VolSessionTime = jcr->VolSessionTime;
439    rec->Stream         = jcr->JobId;
440
441    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Session_Label);
442    ser_begin(rec->data, SER_LENGTH_Session_Label);
443    ser_string(BaculaId);
444    ser_uint32(BaculaTapeVersion);
445
446    ser_uint32(jcr->JobId);
447
448    /* Changed in VerNum 11 */
449    ser_btime(get_current_btime());
450    ser_float64(0);
451
452    ser_string(jcr->pool_name);
453    ser_string(jcr->pool_type);
454    ser_string(jcr->job_name);         /* base Job name */
455    ser_string(jcr->client_name);
456
457    /* Added in VerNum 10 */
458    ser_string(jcr->Job);              /* Unique name of this Job */
459    ser_string(jcr->fileset_name);
460    ser_uint32(jcr->JobType);
461    ser_uint32(jcr->JobLevel);
462    /* Added in VerNum 11 */
463    ser_string(jcr->fileset_md5);
464
465    if (label == EOS_LABEL) {
466       ser_uint32(jcr->JobFiles);
467       ser_uint64(jcr->JobBytes);
468       ser_uint32(dcr->StartBlock);
469       ser_uint32(dcr->EndBlock);
470       ser_uint32(dcr->StartFile);
471       ser_uint32(dcr->EndFile);
472       ser_uint32(jcr->JobErrors);
473
474       /* Added in VerNum 11 */
475       ser_uint32(jcr->JobStatus);
476    }
477    ser_end(rec->data, SER_LENGTH_Session_Label);
478    rec->data_len = ser_length(rec->data);
479 }
480
481 /* Write session label
482  *  Returns: 0 on failure
483  *           1 on success 
484  */
485 int write_session_label(JCR *jcr, DEV_BLOCK *block, int label)
486 {
487    DEVICE *dev = jcr->device->dev;
488    DCR *dcr = jcr->dcr;
489    DEV_RECORD *rec;
490
491    rec = new_record();
492    Dmsg1(90, "session_label record=%x\n", rec);
493    switch (label) {
494    case SOS_LABEL:
495       if (dev->state & ST_TAPE) {
496          dcr->StartBlock = dev->block_num;
497          dcr->StartFile  = dev->file;
498       } else {
499          dcr->StartBlock = (uint32_t)dev->file_addr;
500          dcr->StartFile = (uint32_t)(dev->file_addr >> 32);
501       }
502       break;
503    case EOS_LABEL:
504       if (dev->state & ST_TAPE) {
505          dcr->EndBlock = dev->EndBlock;
506          dcr->EndFile  = dev->EndFile;
507       } else {
508          dcr->EndBlock = (uint32_t)dev->file_addr;
509          dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
510       }
511       break;
512    default:
513       Jmsg1(jcr, M_ABORT, 0, _("Bad session label = %d\n"), label);
514       break;
515    }
516    create_session_label(jcr, rec, label);
517    rec->FileIndex = label;
518
519    /* 
520     * We guarantee that the session record can totally fit
521     *  into a block. If not, write the block, and put it in
522     *  the next block. Having the sesssion record totally in
523     *  one block makes reading them much easier (no need to
524     *  read the next block).
525     */
526    if (!can_write_record_to_block(block, rec)) {
527       Dmsg0(100, "Cannot write session label to block.\n");
528       if (!write_block_to_device(jcr->dcr, block)) {
529          Dmsg0(90, "Got session label write_block_to_dev error.\n");
530          Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"), 
531                            dev_vol_name(dev), strerror(errno));
532          free_record(rec);
533          return 0;
534       }
535    }
536    if (!write_record_to_block(block, rec)) {
537       Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"), 
538                         dev_vol_name(dev), strerror(errno));
539       free_record(rec);
540       return 0;
541    }
542
543    Dmsg6(20, "Write sesson_label record JobId=%d FI=%s SessId=%d Strm=%s len=%d\n\
544 remainder=%d\n", jcr->JobId,
545       FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
546       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
547       rec->remainder);
548
549    free_record(rec);
550    Dmsg2(20, "Leave write_session_label Block=%d File=%d\n", 
551       dev->block_num, dev->file);
552    return 1;
553 }
554
555 void dump_volume_label(DEVICE *dev)
556 {
557    int dbl = debug_level;
558    uint32_t File;
559    char *LabelType, buf[30];
560    struct tm tm;
561    struct date_time dt;
562
563    debug_level = 1;
564    File = dev->file;
565    switch (dev->VolHdr.LabelType) {
566       case PRE_LABEL:
567          LabelType = "PRE_LABEL";
568          break;
569       case VOL_LABEL:
570          LabelType = "VOL_LABEL";
571          break;
572       case EOM_LABEL:
573          LabelType = "EOM_LABEL";
574          break;
575       case SOS_LABEL:
576          LabelType = "SOS_LABEL";
577          break;
578       case EOS_LABEL:
579          LabelType = "EOS_LABEL";
580          break;
581       case EOT_LABEL:
582          goto bail_out;
583       default:
584          LabelType = buf;
585          sprintf(buf, "Unknown %d", dev->VolHdr.LabelType);
586          break;
587    }
588               
589    
590    Pmsg11(-1, "\nVolume Label:\n\
591 Id                : %s\
592 VerNo             : %d\n\
593 VolName           : %s\n\
594 PrevVolName       : %s\n\
595 VolFile           : %d\n\
596 LabelType         : %s\n\
597 LabelSize         : %d\n\
598 PoolName          : %s\n\
599 MediaType         : %s\n\
600 PoolType          : %s\n\
601 HostName          : %s\n\
602 ",
603              dev->VolHdr.Id, dev->VolHdr.VerNum,
604              dev->VolHdr.VolName, dev->VolHdr.PrevVolName,
605              File, LabelType, dev->VolHdr.LabelSize, 
606              dev->VolHdr.PoolName, dev->VolHdr.MediaType, 
607              dev->VolHdr.PoolType, dev->VolHdr.HostName);
608
609    if (dev->VolHdr.VerNum >= 11) {
610       char dt[50];
611       bstrftime(dt, sizeof(dt), btime_to_unix(dev->VolHdr.label_btime));
612       Pmsg1(-1, "Date label written: %s\n", dt);
613    } else {
614    dt.julian_day_number   = dev->VolHdr.label_date;
615    dt.julian_day_fraction = dev->VolHdr.label_time;
616    tm_decode(&dt, &tm);
617    Pmsg5(-1, "\
618 Date label written: %04d-%02d-%02d at %02d:%02d\n", 
619       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
620    }
621
622 bail_out:
623    debug_level = dbl;
624 }
625
626 int unser_session_label(SESSION_LABEL *label, DEV_RECORD *rec) 
627 {
628    ser_declare;
629
630    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Session_Label);
631    unser_begin(rec->data, SER_LENGTH_Session_Label);
632    unser_string(label->Id);
633    unser_uint32(label->VerNum);
634    unser_uint32(label->JobId);
635    if (label->VerNum >= 11) {
636       unser_btime(label->write_btime);
637    } else {
638       unser_float64(label->write_date);
639    }
640    unser_float64(label->write_time);
641    unser_string(label->PoolName);
642    unser_string(label->PoolType);
643    unser_string(label->JobName);
644    unser_string(label->ClientName);
645    if (label->VerNum >= 10) {
646       unser_string(label->Job);          /* Unique name of this Job */
647       unser_string(label->FileSetName);
648       unser_uint32(label->JobType);
649       unser_uint32(label->JobLevel);
650    }
651    if (label->VerNum >= 11) {
652       unser_string(label->FileSetMD5);
653    } else {
654       label->FileSetMD5[0] = 0;
655    }
656    if (rec->FileIndex == EOS_LABEL) {
657       unser_uint32(label->JobFiles);
658       unser_uint64(label->JobBytes);
659       unser_uint32(label->StartBlock);
660       unser_uint32(label->EndBlock);
661       unser_uint32(label->StartFile);
662       unser_uint32(label->EndFile);
663       unser_uint32(label->JobErrors);
664       if (label->VerNum >= 11) {
665          unser_uint32(label->JobStatus);
666       } else {
667          label->JobStatus = JS_Terminated; /* kludge */
668       }
669    }      
670    return 1;
671 }
672
673
674 static void dump_session_label(DEV_RECORD *rec, char *type)
675 {
676    int dbl;
677    struct date_time dt;
678    struct tm tm;
679    SESSION_LABEL label;
680    char ec1[30], ec2[30], ec3[30], ec4[30], ec5[30], ec6[30], ec7[30];
681
682    unser_session_label(&label, rec);
683    dbl = debug_level;
684    debug_level = 1;
685    Pmsg7(-1, "\n%s Record:\n\
686 JobId             : %d\n\
687 VerNum            : %d\n\
688 PoolName          : %s\n\
689 PoolType          : %s\n\
690 JobName           : %s\n\
691 ClientName        : %s\n\
692 ",    type, label.JobId, label.VerNum,
693       label.PoolName, label.PoolType,
694       label.JobName, label.ClientName);
695
696    if (label.VerNum >= 10) {
697       Pmsg4(-1, "\
698 Job (unique name) : %s\n\
699 FileSet           : %s\n\
700 JobType           : %c\n\
701 JobLevel          : %c\n\
702 ", label.Job, label.FileSetName, label.JobType, label.JobLevel);
703    }
704
705    if (rec->FileIndex == EOS_LABEL) {
706       Pmsg8(-1, "\
707 JobFiles          : %s\n\
708 JobBytes          : %s\n\
709 StartBlock        : %s\n\
710 EndBlock          : %s\n\
711 StartFile         : %s\n\
712 EndFile           : %s\n\
713 JobErrors         : %s\n\
714 JobStatus         : %c\n\
715 ",
716          edit_uint64_with_commas(label.JobFiles, ec1),
717          edit_uint64_with_commas(label.JobBytes, ec2),
718          edit_uint64_with_commas(label.StartBlock, ec3),
719          edit_uint64_with_commas(label.EndBlock, ec4),
720          edit_uint64_with_commas(label.StartFile, ec5),
721          edit_uint64_with_commas(label.EndFile, ec6),
722          edit_uint64_with_commas(label.JobErrors, ec7), 
723          label.JobStatus);
724    }
725    if (label.VerNum >= 11) {
726       char dt[50];
727       bstrftime(dt, sizeof(dt), btime_to_unix(label.write_btime));
728       Pmsg1(-1, _("Date written      : %s\n"), dt);
729    } else {
730       dt.julian_day_number   = label.write_date;
731       dt.julian_day_fraction = label.write_time;
732       tm_decode(&dt, &tm);
733       Pmsg5(-1, _("\
734 Date written      : %04d-%02d-%02d at %02d:%02d\n"),
735       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min);
736    }
737
738    debug_level = dbl;
739 }
740
741 void dump_label_record(DEVICE *dev, DEV_RECORD *rec, int verbose)
742 {
743    char *type;
744    int dbl;
745
746    dbl = debug_level;
747    debug_level = 1;
748    switch (rec->FileIndex) {
749       case PRE_LABEL:
750          type = _("Fresh Volume");   
751          break;
752       case VOL_LABEL:
753          type = _("Volume");
754          break;
755       case SOS_LABEL:
756          type = _("Begin Session");
757          break;
758       case EOS_LABEL:
759          type = _("End Session");
760          break;
761       case EOM_LABEL:
762          type = _("End of Media");
763          break;
764       case EOT_LABEL:
765          type = ("End of Tape");
766          break;
767       default:
768          type = _("Unknown");
769          break;
770    }
771    if (verbose) {
772       switch (rec->FileIndex) {
773          case PRE_LABEL:
774          case VOL_LABEL:
775             unser_volume_label(dev, rec);
776             dump_volume_label(dev);
777             break;
778          case SOS_LABEL:
779             dump_session_label(rec, type);
780             break;
781          case EOS_LABEL:
782             dump_session_label(rec, type);
783             break;
784          case EOM_LABEL:
785             Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
786                type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
787             break;
788          case EOT_LABEL:
789             Pmsg0(-1, _("End of physical tape.\n"));
790             break;
791          default:
792             Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
793                type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
794             break;
795       }
796    } else {
797       switch (rec->FileIndex) {
798          case SOS_LABEL:
799          case EOS_LABEL:
800             SESSION_LABEL label;
801             unser_session_label(&label, rec);
802             Pmsg6(-1, "%s Record: SessId=%d SessTime=%d JobId=%d Level=%c \
803 Type=%c\n",
804                type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, 
805                label.JobLevel, label.JobType);
806             break;
807          case EOM_LABEL:
808          case PRE_LABEL:
809          case VOL_LABEL:
810          default:
811             Pmsg5(-1, "%s Record: SessId=%d SessTime=%d JobId=%d DataLen=%d\n",
812          type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, rec->data_len);
813             break;
814          case EOT_LABEL:
815             break;
816       }
817    }
818    debug_level = dbl;
819 }