]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/label.c
c4a431b0c8066d71f33cf290b42309f88c3c6f59
[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-2004 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(DCR *dcr, 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(DCR *dcr, DEV_BLOCK *block)
60 {
61    JCR *jcr = dcr->jcr;
62    DEVICE *dev = dcr->dev;
63    char *VolName = jcr->VolumeName;
64    DEV_RECORD *record;
65    bool ok = false;
66
67    Dmsg3(100, "Enter read_volume_label device=%s vol=%s dev_Vol=%s\n", 
68       dev_name(dev), VolName, dev->VolHdr.VolName);
69
70    if (dev_state(dev, ST_LABEL)) {       /* did we already read label? */
71       /* Compare Volume Names allow special wild card */
72       if (VolName && *VolName && *VolName != '*' && strcmp(dev->VolHdr.VolName, VolName) != 0) {
73          Mmsg(jcr->errmsg, _("Wrong Volume mounted on device %s: Wanted %s have %s\n"),
74             dev_name(dev), VolName, dev->VolHdr.VolName);
75          /*
76           * Cancel Job if too many label errors
77           *  => we are in a loop
78           */
79          if (!dev->poll && jcr->label_errors++ > 100) {
80             Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
81          }
82          return jcr->label_status = VOL_NAME_ERROR;
83       }
84       Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
85       return jcr->label_status = VOL_OK;       /* label already read */
86    }
87
88    dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ);  /* set no label, no append */
89
90    if (!rewind_dev(dev)) {
91       Mmsg(jcr->errmsg, _("Couldn't rewind device %s ERR=%s\n"), dev_name(dev),
92          strerror_dev(dev));
93       return jcr->label_status = VOL_NO_MEDIA;
94    }
95    bstrncpy(dev->VolHdr.Id, "**error**", sizeof(dev->VolHdr.Id));
96
97    /* Read the Volume label block */
98    record = new_record();
99    Dmsg0(90, "Big if statement in read_volume_label\n");
100    if (!read_block_from_dev(dcr, block, NO_BLOCK_NUMBER_CHECK)) { 
101       Mmsg(jcr->errmsg, _("Requested Volume \"%s\" on %s is not a Bacula "
102            "labeled Volume, because: ERR=%s"), NPRT(VolName), dev_name(dev), 
103            strerror_dev(dev));
104    } else if (!read_record_from_block(block, record)) {
105       Mmsg(jcr->errmsg, _("Could not read Volume label from block.\n"));
106    } else if (!unser_volume_label(dev, record)) {
107       Mmsg(jcr->errmsg, _("Could not unserialize Volume label: ERR=%s\n"),
108          strerror_dev(dev));
109    } else if (strcmp(dev->VolHdr.Id, BaculaId) != 0 && 
110               strcmp(dev->VolHdr.Id, OldBaculaId) != 0) {
111       Mmsg(jcr->errmsg, _("Volume Header Id bad: %s\n"), dev->VolHdr.Id);
112    } else {
113       ok = true;
114    }
115    if (!ok) {
116       free_record(record);
117       if (forge_on || jcr->ignore_label_errors) {
118          dev->state |= ST_LABEL;      /* set has Bacula label */
119          Jmsg(jcr, M_ERROR, 0, "%s", jcr->errmsg);
120          return jcr->label_status = VOL_OK;
121       }
122       empty_block(block);
123       rewind_dev(dev);
124       return jcr->label_status = VOL_NO_LABEL;
125    }
126
127    free_record(record);
128    /* If we are a streaming device, we only get one chance to read */
129    if (!dev_cap(dev, CAP_STREAM)) {
130       empty_block(block);
131       rewind_dev(dev);
132    }
133
134    if (dev->VolHdr.VerNum != BaculaTapeVersion && 
135        dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion1 &&  
136        dev->VolHdr.VerNum != OldCompatibleBaculaTapeVersion2) {
137       Mmsg(jcr->errmsg, _("Volume on %s has wrong Bacula version. Wanted %d got %d\n"),
138          dev_name(dev), BaculaTapeVersion, dev->VolHdr.VerNum);
139       return jcr->label_status = VOL_VERSION_ERROR;
140    }
141
142    /* We are looking for either an unused Bacula tape (PRE_LABEL) or
143     * a Bacula volume label (VOL_LABEL)
144     */
145    if (dev->VolHdr.LabelType != PRE_LABEL && dev->VolHdr.LabelType != VOL_LABEL) {
146       Mmsg(jcr->errmsg, _("Volume on %s has bad Bacula label type: %x\n"), 
147           dev_name(dev), dev->VolHdr.LabelType);
148       return jcr->label_status = VOL_LABEL_ERROR;
149    }
150
151    dev->state |= ST_LABEL;            /* set has Bacula label */
152
153    /* Compare Volume Names */
154    Dmsg2(30, "Compare Vol names: VolName=%s hdr=%s\n", VolName?VolName:"*", dev->VolHdr.VolName);
155    if (VolName && *VolName && *VolName != '*' && strcmp(dev->VolHdr.VolName, VolName) != 0) {
156       Mmsg(jcr->errmsg, _("Wrong Volume mounted on device %s: Wanted %s have %s\n"),
157            dev_name(dev), VolName, dev->VolHdr.VolName);
158       /*
159        * Cancel Job if too many label errors
160        *  => we are in a loop
161        */
162       if (!dev->poll && jcr->label_errors++ > 100) {
163          Jmsg(jcr, M_FATAL, 0, "Too many tries: %s", jcr->errmsg);
164       }
165       return jcr->label_status = VOL_NAME_ERROR;
166    }
167    Dmsg1(30, "Copy vol_name=%s\n", dev->VolHdr.VolName);
168
169    if (debug_level >= 10) {
170       dump_volume_label(dev);
171    }
172    Dmsg0(30, "Leave read_volume_label() VOL_OK\n");
173    return jcr->label_status = VOL_OK;
174 }
175
176 /*  unser_volume_label 
177  *  
178  * Unserialize the Volume label into the device Volume_Label
179  * structure.
180  *
181  * Assumes that the record is already read.
182  *
183  * Returns: false on error
184  *          true  on success
185 */
186
187 bool unser_volume_label(DEVICE *dev, DEV_RECORD *rec)
188 {
189    ser_declare;
190
191    if (rec->FileIndex != VOL_LABEL && rec->FileIndex != PRE_LABEL) {
192       Mmsg3(dev->errmsg, _("Expecting Volume Label, got FI=%s Stream=%s len=%d\n"), 
193               FI_to_ascii(rec->FileIndex), 
194               stream_to_ascii(rec->Stream, rec->FileIndex),
195               rec->data_len);
196       if (!forge_on) {
197          return false;
198       }
199    }
200
201    dev->VolHdr.LabelType = rec->FileIndex;
202    dev->VolHdr.LabelSize = rec->data_len;
203
204
205    /* Unserialize the record into the Volume Header */
206    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Volume_Label);
207    ser_begin(rec->data, SER_LENGTH_Volume_Label);
208    unser_string(dev->VolHdr.Id);
209    unser_uint32(dev->VolHdr.VerNum);
210
211    if (dev->VolHdr.VerNum >= 11) {
212       unser_btime(dev->VolHdr.label_btime);
213       unser_btime(dev->VolHdr.write_btime);
214    } else { /* old way */ 
215       unser_float64(dev->VolHdr.label_date);
216       unser_float64(dev->VolHdr.label_time);
217    }
218    unser_float64(dev->VolHdr.write_date);    /* Unused with VerNum >= 11 */
219    unser_float64(dev->VolHdr.write_time);    /* Unused with VerNum >= 11 */
220
221    unser_string(dev->VolHdr.VolName);
222    unser_string(dev->VolHdr.PrevVolName);
223    unser_string(dev->VolHdr.PoolName);
224    unser_string(dev->VolHdr.PoolType);
225    unser_string(dev->VolHdr.MediaType);
226
227    unser_string(dev->VolHdr.HostName);
228    unser_string(dev->VolHdr.LabelProg);
229    unser_string(dev->VolHdr.ProgVersion);
230    unser_string(dev->VolHdr.ProgDate);
231
232    ser_end(rec->data, SER_LENGTH_Volume_Label);
233    Dmsg0(90, "ser_read_vol\n");
234    if (debug_level >= 90) {
235       dump_volume_label(dev);      
236    }
237    return true;
238 }
239
240 /*
241  * Put a volume label into the block
242  *
243  *  Returns: false on failure
244  *           true  on success
245  */
246 bool write_volume_label_to_block(DCR *dcr, DEV_BLOCK *block)
247 {
248    DEV_RECORD rec;
249    DEVICE *dev = dcr->dev;
250    JCR *jcr = dcr->jcr;
251
252    Dmsg0(20, "write Label in write_volume_label_to_block()\n");
253    memset(&rec, 0, sizeof(rec));
254    rec.data = get_memory(SER_LENGTH_Volume_Label);
255
256    create_volume_label_record(dcr, &rec);
257
258    empty_block(block);                /* Volume label always at beginning */
259    block->BlockNumber = 0;
260    if (!write_record_to_block(block, &rec)) {
261       free_pool_memory(rec.data);
262       Jmsg1(jcr, M_FATAL, 0, _("Cannot write Volume label to block for device %s\n"),
263          dev_name(dev));
264       return false;
265    } else {
266       Dmsg1(90, "Wrote label of %d bytes to block\n", rec.data_len);
267    }
268    free_pool_memory(rec.data);
269    return true;
270 }
271
272 /* 
273  *  create_volume_label_record
274  *   Serialize label (from dev->VolHdr structure) into device record.
275  *   Assumes that the dev->VolHdr structure is properly 
276  *   initialized.
277 */
278 static void create_volume_label_record(DCR *dcr, DEV_RECORD *rec)
279 {
280    ser_declare;
281    struct date_time dt;
282    DEVICE *dev = dcr->dev;
283    JCR *jcr = dcr->jcr;
284
285    /* Serialize the label into the device record. */
286
287    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Volume_Label);
288    ser_begin(rec->data, SER_LENGTH_Volume_Label);
289    ser_string(dev->VolHdr.Id);
290
291    ser_uint32(dev->VolHdr.VerNum);
292
293    if (dev->VolHdr.VerNum >= 11) {
294       ser_btime(dev->VolHdr.label_btime);
295       dev->VolHdr.write_btime = get_current_btime();
296       ser_btime(dev->VolHdr.write_btime);
297       dev->VolHdr.write_date = 0;
298       dev->VolHdr.write_time = 0;
299    } else {
300       /* OLD WAY DEPRECATED */
301       ser_float64(dev->VolHdr.label_date);
302       ser_float64(dev->VolHdr.label_time);
303       get_current_time(&dt);
304       dev->VolHdr.write_date = dt.julian_day_number;
305       dev->VolHdr.write_time = dt.julian_day_fraction;
306    }
307    ser_float64(dev->VolHdr.write_date);   /* 0 if VerNum >= 11 */
308    ser_float64(dev->VolHdr.write_time);   /* 0  if VerNum >= 11 */
309
310    ser_string(dev->VolHdr.VolName);
311    ser_string(dev->VolHdr.PrevVolName);
312    ser_string(dev->VolHdr.PoolName);
313    ser_string(dev->VolHdr.PoolType);
314    ser_string(dev->VolHdr.MediaType);
315
316    ser_string(dev->VolHdr.HostName);
317    ser_string(dev->VolHdr.LabelProg);
318    ser_string(dev->VolHdr.ProgVersion);
319    ser_string(dev->VolHdr.ProgDate);
320
321    ser_end(rec->data, SER_LENGTH_Volume_Label);
322    rec->data_len = ser_length(rec->data);
323    rec->FileIndex = dev->VolHdr.LabelType;
324    rec->VolSessionId = jcr->VolSessionId;
325    rec->VolSessionTime = jcr->VolSessionTime;
326    rec->Stream = jcr->NumVolumes;
327    Dmsg2(100, "Created Vol label rec: FI=%s len=%d\n", FI_to_ascii(rec->FileIndex),
328       rec->data_len);
329 }     
330
331
332 /*
333  * Create a volume label in memory
334  */
335 void create_volume_label(DEVICE *dev, const char *VolName, const char *PoolName)
336 {
337    DEVRES *device = (DEVRES *)dev->device;
338
339    Dmsg0(90, "Start create_volume_label()\n");
340
341    ASSERT(dev != NULL);
342
343    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
344
345    bstrncpy(dev->VolHdr.Id, BaculaId, sizeof(dev->VolHdr.Id));
346    dev->VolHdr.VerNum = BaculaTapeVersion;
347    dev->VolHdr.LabelType = PRE_LABEL;  /* Mark tape as unused */
348    bstrncpy(dev->VolHdr.VolName, VolName, sizeof(dev->VolHdr.VolName));
349    bstrncpy(dev->VolHdr.PoolName, PoolName, sizeof(dev->VolHdr.PoolName));
350    bstrncpy(dev->VolHdr.MediaType, device->media_type, sizeof(dev->VolHdr.MediaType));
351
352    bstrncpy(dev->VolHdr.PoolType, "Backup", sizeof(dev->VolHdr.PoolType));
353
354    dev->VolHdr.label_btime = get_current_btime();
355    dev->VolHdr.label_date = 0;
356    dev->VolHdr.label_time = 0;
357
358    if (gethostname(dev->VolHdr.HostName, sizeof(dev->VolHdr.HostName)) != 0) {
359       dev->VolHdr.HostName[0] = 0;
360    }
361    bstrncpy(dev->VolHdr.LabelProg, my_name, sizeof(dev->VolHdr.LabelProg));
362    sprintf(dev->VolHdr.ProgVersion, "Ver. %s %s", VERSION, BDATE);
363    sprintf(dev->VolHdr.ProgDate, "Build %s %s", __DATE__, __TIME__);
364    dev->state |= ST_LABEL;            /* set has Bacula label */
365    if (debug_level >= 90) {
366       dump_volume_label(dev);
367    }
368 }
369
370 /*
371  * Write a Volume Label
372  *  !!! Note, this is ONLY used for writing
373  *            a fresh volume label.  Any data
374  *            after the label will be destroyed,
375  *            in fact, we write the label 5 times !!!!
376  * 
377  *  This routine expects that open_device() was previously called.
378  *
379  *  This routine should be used only when labeling a blank tape.
380  */
381 bool write_new_volume_label_to_dev(DCR *dcr, const char *VolName, const char *PoolName)
382 {
383    DEV_RECORD rec;   
384    DEV_BLOCK *block;
385    bool ok = false;
386    DEVICE *dev = dcr->dev;
387
388
389    Dmsg0(99, "write_volume_label()\n");
390    create_volume_label(dev, VolName, PoolName);
391
392    if (!rewind_dev(dev)) {
393       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
394       Dmsg2(30, "Bad status on %s from rewind. ERR=%s\n", dev_name(dev), strerror_dev(dev));
395       if (!forge_on) {
396          return false;
397       }
398    }
399
400    block = new_block(dev);
401    memset(&rec, 0, sizeof(rec));
402    rec.data = get_memory(SER_LENGTH_Volume_Label);
403    create_volume_label_record(dcr, &rec);
404    rec.Stream = 0;
405
406    if (!write_record_to_block(block, &rec)) {
407       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
408       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
409       goto bail_out;
410    } else {
411       Dmsg2(30, "Wrote label of %d bytes to %s\n", rec.data_len, dev_name(dev));
412    }
413       
414    Dmsg0(99, "Call write_block_to_dev()\n");
415    if (!write_block_to_dev(dcr, block)) {
416       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
417       Dmsg2(30, "Bad Label write on %s. ERR=%s\n", dev_name(dev), strerror_dev(dev));
418       goto bail_out;
419    }
420    Dmsg0(99, " Wrote block to device\n");
421      
422    weof_dev(dev, 1);
423    dev->state |= ST_LABEL;
424    ok = true;
425
426    if (debug_level >= 20)  {
427       dump_volume_label(dev);
428    }
429
430 bail_out:
431    free_block(block);
432    free_pool_memory(rec.data);
433    return ok;
434 }     
435
436
437 /*
438  * Create session label
439  *  The pool memory must be released by the calling program
440  */
441 void create_session_label(DCR *dcr, DEV_RECORD *rec, int label)
442 {
443    JCR *jcr = dcr->jcr;
444    ser_declare;
445
446    rec->VolSessionId   = jcr->VolSessionId;
447    rec->VolSessionTime = jcr->VolSessionTime;
448    rec->Stream         = jcr->JobId;
449
450    rec->data = check_pool_memory_size(rec->data, SER_LENGTH_Session_Label);
451    ser_begin(rec->data, SER_LENGTH_Session_Label);
452    ser_string(BaculaId);
453    ser_uint32(BaculaTapeVersion);
454
455    ser_uint32(jcr->JobId);
456
457    /* Changed in VerNum 11 */
458    ser_btime(get_current_btime());
459    ser_float64(0);
460
461    ser_string(jcr->pool_name);
462    ser_string(jcr->pool_type);
463    ser_string(jcr->job_name);         /* base Job name */
464    ser_string(jcr->client_name);
465
466    /* Added in VerNum 10 */
467    ser_string(jcr->Job);              /* Unique name of this Job */
468    ser_string(jcr->fileset_name);
469    ser_uint32(jcr->JobType);
470    ser_uint32(jcr->JobLevel);
471    /* Added in VerNum 11 */
472    ser_string(jcr->fileset_md5);
473
474    if (label == EOS_LABEL) {
475       ser_uint32(jcr->JobFiles);
476       ser_uint64(jcr->JobBytes);
477       ser_uint32(dcr->StartBlock);
478       ser_uint32(dcr->EndBlock);
479       ser_uint32(dcr->StartFile);
480       ser_uint32(dcr->EndFile);
481       ser_uint32(jcr->JobErrors);
482
483       /* Added in VerNum 11 */
484       ser_uint32(jcr->JobStatus);
485    }
486    ser_end(rec->data, SER_LENGTH_Session_Label);
487    rec->data_len = ser_length(rec->data);
488 }
489
490 /* Write session label
491  *  Returns: false on failure
492  *           true  on success 
493  */
494 bool write_session_label(DCR *dcr, DEV_BLOCK *block, int label)
495 {
496    JCR *jcr = dcr->jcr;
497    DEVICE *dev = dcr->dev;
498    DEV_RECORD *rec;
499
500    rec = new_record();
501    Dmsg1(90, "session_label record=%x\n", rec);
502    switch (label) {
503    case SOS_LABEL:
504       if (dev->state & ST_TAPE) {
505          dcr->StartBlock = dev->block_num;
506          dcr->StartFile  = dev->file;
507       } else {
508          dcr->StartBlock = (uint32_t)dev->file_addr;
509          dcr->StartFile = (uint32_t)(dev->file_addr >> 32);
510       }
511       break;
512    case EOS_LABEL:
513       if (dev->state & ST_TAPE) {
514          dcr->EndBlock = dev->EndBlock;
515          dcr->EndFile  = dev->EndFile;
516       } else {
517          dcr->EndBlock = (uint32_t)dev->file_addr;
518          dcr->EndFile = (uint32_t)(dev->file_addr >> 32);
519       }
520       break;
521    default:
522       Jmsg1(jcr, M_ABORT, 0, _("Bad session label = %d\n"), label);
523       break;
524    }
525    create_session_label(dcr, rec, label);
526    rec->FileIndex = label;
527
528    /* 
529     * We guarantee that the session record can totally fit
530     *  into a block. If not, write the block, and put it in
531     *  the next block. Having the sesssion record totally in
532     *  one block makes reading them much easier (no need to
533     *  read the next block).
534     */
535    if (!can_write_record_to_block(block, rec)) {
536       Dmsg0(100, "Cannot write session label to block.\n");
537       if (!write_block_to_device(jcr->dcr, block)) {
538          Dmsg0(90, "Got session label write_block_to_dev error.\n");
539          Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"), 
540                            dev_vol_name(dev), strerror(errno));
541          free_record(rec);
542          return false;
543       }
544    }
545    if (!write_record_to_block(block, rec)) {
546       Jmsg(jcr, M_FATAL, 0, _("Error writing Session label to %s: %s\n"), 
547                         dev_vol_name(dev), strerror(errno));
548       free_record(rec);
549       return false;
550    }
551
552    Dmsg6(20, "Write sesson_label record JobId=%d FI=%s SessId=%d Strm=%s len=%d\n\
553 remainder=%d\n", jcr->JobId,
554       FI_to_ascii(rec->FileIndex), rec->VolSessionId, 
555       stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len,
556       rec->remainder);
557
558    free_record(rec);
559    Dmsg2(20, "Leave write_session_label Block=%d File=%d\n", 
560       dev->block_num, dev->file);
561    return true;
562 }
563
564 void dump_volume_label(DEVICE *dev)
565 {
566    int dbl = debug_level;
567    uint32_t File;
568    const char *LabelType;
569    char 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 bool 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 true;
681 }
682
683
684 static void dump_session_label(DEV_RECORD *rec, const 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    const 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 Job Session");
767       break;
768    case EOS_LABEL:
769       type = _("End Job 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       SESSION_LABEL label;
808       switch (rec->FileIndex) {
809       case SOS_LABEL:
810          unser_session_label(&label, rec);
811          Pmsg6(-1, "%s Record: SessId=%d SessTime=%d JobId=%d Level=%c Type=%c\n",
812             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, 
813             label.JobLevel, label.JobType);
814          break;
815       case EOS_LABEL:
816          char ed1[30], ed2[30];
817          unser_session_label(&label, rec);
818          Pmsg6(-1, "%s Record: SessId=%d SessTime=%d JobId=%d Level=%c Type=%c\n",
819             type, rec->VolSessionId, rec->VolSessionTime, rec->Stream, 
820             label.JobLevel, label.JobType);
821          Pmsg4(-1, "   Files=%s Bytes=%s Errors=%d Status=%c\n",
822             edit_uint64_with_commas(label.JobFiles, ed1),
823             edit_uint64_with_commas(label.JobBytes, ed2),
824             label.JobErrors, (char)label.JobStatus);
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 }