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