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