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