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