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