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