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