Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ansi_label.cc « stored « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0927ab0c8b4c59c93ca2623ccace11c59820471 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2005-2009 Free Software Foundation Europe e.V.
   Copyright (C) 2011-2012 Planets Communications B.V.
   Copyright (C) 2013-2021 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/
// Kern Sibbald, MMV
/**
 * @file
 * ansi_label.c routines to handle ANSI tape labels.
 */

#include "include/bareos.h" /* pull in global headers */
#include "stored/device_control_record.h"
#include "stored/stored.h" /* pull in Storage Daemon headers */
#include "stored/stored_globals.h"
#include "stored/label.h"
#include "stored/ebcdic.h"
#include "include/jcr.h"
#include "lib/berrno.h"

namespace storagedaemon {

/* Forward referenced functions */
static char* ansi_date(time_t td, char* buf);
static bool SameLabelNames(char* bareos_name, char* ansi_name);

/**
 * We read an ANSI label and compare the Volume name. We require
 * a VOL1 record of 80 characters followed by a HDR1 record containing
 * BAREOS.DATA in the filename field. We then read up to 3 more
 * header records (they are not required) and an EOF, at which
 * point, all is good.
 *
 * Returns:
 *    VOL_OK            Volume name OK
 *    VOL_NO_LABEL      No ANSI label on Volume
 *    VOL_IO_ERROR      I/O error on read
 *    VOL_NAME_ERROR    Wrong name in VOL1 record
 *    VOL_LABEL_ERROR   Probably an ANSI label, but something wrong
 */
int ReadAnsiIbmLabel(DeviceControlRecord* dcr)
{
  Device* volatile dev = dcr->dev;
  JobControlRecord* jcr = dcr->jcr;
  char label[80]; /* tape label */
  int status, i;
  char* VolName = dcr->VolumeName;
  bool ok = false;

  /*
   * Read VOL1, HDR1, HDR2 labels, but ignore the data
   * If tape read the following EOF mark, on disk do not read.
   */
  Dmsg0(100, "Read ansi label.\n");
  if (!dev->IsTape()) { return VOL_OK; }

  dev->label_type = B_BAREOS_LABEL; /* assume Bareos label */

  // Read a maximum of 5 records VOL1, HDR1, ... HDR4
  for (i = 0; i < 6; i++) {
    do {
      status = dev->read(label, sizeof(label));
    } while (status == -1 && errno == EINTR);

    if (status < 0) {
      BErrNo be;
      dev->clrerror(-1);
      Dmsg1(100, "Read device got: ERR=%s\n", be.bstrerror());
      Mmsg2(jcr->errmsg, _("Read error on device %s in ANSI label. ERR=%s\n"),
            dev->archive_device_string, be.bstrerror());
      Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
      dev->VolCatInfo.VolCatErrors++;
      return VOL_IO_ERROR;
    }

    if (status == 0) {
      if (dev->AtEof()) {
        dev->SetEot(); /* second eof, set eot bit */
        Dmsg0(100, "EOM on ANSI label\n");
        Mmsg0(jcr->errmsg,
              _("Insane! End of tape while reading ANSI label.\n"));
        return VOL_LABEL_ERROR; /* at EOM this shouldn't happen */
      } else {
        dev->SetAteof(); /* set eof state */
      }
    }

    switch (i) {
      case 0: /* Want VOL1 label */
        if (status == 80) {
          if (bstrncmp("VOL1", label, 4)) {
            ok = true;
            dev->label_type = B_ANSI_LABEL;
            Dmsg0(100, "Got ANSI VOL1 label\n");
          } else {
            // Try EBCDIC
            EbcdicToAscii(label, label, sizeof(label));
            if (bstrncmp("VOL1", label, 4)) {
              ok = true;
              ;
              dev->label_type = B_IBM_LABEL;
              Dmsg0(100, "Found IBM label.\n");
              Dmsg0(100, "Got IBM VOL1 label\n");
            }
          }
        }

        if (!ok) {
          Dmsg0(100, "No VOL1 label\n");
          Mmsg0(jcr->errmsg,
                _("No VOL1 label while reading ANSI/IBM label.\n"));
          return VOL_NO_LABEL; /* No ANSI label */
        }

        // Compare Volume Names allow special wild card
        if (VolName && *VolName && *VolName != '*') {
          if (!SameLabelNames(VolName, &label[4])) {
            char* p = &label[4];
            char* q;

            FreeVolume(dev);

            // Store new Volume name
            q = dev->VolHdr.VolumeName;
            for (int i = 0; *p != ' ' && i < 6; i++) { *q++ = *p++; }
            *q = 0;
            Dmsg0(100, "Call reserve_volume\n");
            //  why is this reserve_volume() needed???? KES
            reserve_volume(dcr, dev->VolHdr.VolumeName);
            dev = dcr->dev; /* may have changed in reserve_volume */
            Dmsg2(100, "Wanted ANSI Vol %s got %6s\n", VolName,
                  dev->VolHdr.VolumeName);
            Mmsg2(jcr->errmsg, _("Wanted ANSI Volume \"%s\" got \"%s\"\n"),
                  VolName, dev->VolHdr.VolumeName);
            return VOL_NAME_ERROR;
          }
        }
        break;
      case 1:
        if (dev->label_type == B_IBM_LABEL) {
          EbcdicToAscii(label, label, sizeof(label));
        }

        if (status != 80 || !bstrncmp("HDR1", label, 4)) {
          Dmsg0(100, "No HDR1 label\n");
          Mmsg0(jcr->errmsg, _("No HDR1 label while reading ANSI label.\n"));
          return VOL_LABEL_ERROR;
        }

        if (me->compatible) {
          if (!bstrncmp("BACULA.DATA", &label[4], 11)
              && !bstrncmp("BAREOS.DATA", &label[4], 11)) {
            Dmsg1(100,
                  "HD1 not Bacula/Bareos label. Wanted BACULA.DATA/BAREOS.DATA "
                  "got %11s\n",
                  &label[4]);
            Mmsg1(jcr->errmsg,
                  _("ANSI/IBM Volume \"%s\" does not belong to Bareos.\n"),
                  dev->VolHdr.VolumeName);
            return VOL_NAME_ERROR; /* Not a Bareos label */
          }
        } else {
          if (!bstrncmp("BAREOS.DATA", &label[4], 11)) {
            Dmsg1(100, "HD1 not Bareos label. Wanted BAREOS.DATA got %11s\n",
                  &label[4]);
            Mmsg1(jcr->errmsg,
                  _("ANSI/IBM Volume \"%s\" does not belong to Bareos.\n"),
                  dev->VolHdr.VolumeName);
            return VOL_NAME_ERROR; /* Not a Bareos label */
          }
        }
        Dmsg0(100, "Got HDR1 label\n");
        break;
      case 2:
        if (dev->label_type == B_IBM_LABEL) {
          EbcdicToAscii(label, label, sizeof(label));
        }

        if (status != 80 || !bstrncmp("HDR2", label, 4)) {
          Dmsg0(100, "No HDR2 label\n");
          Mmsg0(jcr->errmsg,
                _("No HDR2 label while reading ANSI/IBM label.\n"));
          return VOL_LABEL_ERROR;
        }
        Dmsg0(100, "Got ANSI HDR2 label\n");
        break;
      default:
        if (status == 0) {
          Dmsg0(100, "ANSI label OK\n");
          return VOL_OK;
        }

        if (dev->label_type == B_IBM_LABEL) {
          EbcdicToAscii(label, label, sizeof(label));
        }

        if (status != 80 || !bstrncmp("HDR", label, 3)) {
          Dmsg0(100, "Unknown or bad ANSI/IBM label record.\n");
          Mmsg0(jcr->errmsg, _("Unknown or bad ANSI/IBM label record.\n"));
          return VOL_LABEL_ERROR;
        }

        Dmsg0(100, "Got HDR label\n");
        break;
    }
  }
  Dmsg0(100, "Too many records in ANSI/IBM label.\n");
  Mmsg0(jcr->errmsg, _("Too many records in while reading ANSI/IBM label.\n"));
  return VOL_LABEL_ERROR;
}

/**
 * ANSI/IBM VOL1 label
 *  80 characters blank filled
 * Pos   count   Function      What Bareos puts
 * 0-3     4     "VOL1"          VOL1
 * 4-9     6     Volume name     Volume name
 * 10-10   1     Access code
 * 11-36   26    Unused
 *
 * ANSI
 * 37-50   14    Owner
 * 51-78   28    reserved
 * 79       1    ANSI level        3
 *
 * IBM
 * 37-40   4     reserved
 * 41-50   10    Owner
 * 51-79   29    reserved
 *
 * ANSI/IBM HDR1 label
 *  80 characters blank filled
 * Pos   count   Function          What Bareos puts
 * 0-3     4     "HDR1"               HDR1
 * 4-20    17    File name           BAREOS.DATA
 * 21-26   6     Volume name          Volume name
 * 27-30   4     Vol seq num           0001
 * 31-34   4     file num              0001
 * 35-38   4     Generation            0001
 * 39-40   2     Gen version           00
 * 41-46   6     Create date bYYDDD    yesterday
 * 47-52   6     Expire date bYYDDD    today
 * 53-53   1     Access
 * 54-59   6     Block count           000000
 * 60-72   13    Software name         Bareos
 * 73-79   7     Reserved
 *
 * ANSI/IBM HDR2 label
 *  80 characters blank filled
 * Pos   count   Function          What Bareos puts
 * 0-3     4     "HDR2"               HDR2
 * 4-4     1     Record format        D   (V if IBM) => variable
 * 5-9     5     Block length         32000
 * 10-14   5     Rec length           32000
 * 15-15   1     Density
 * 16-16   1     Continued
 * 17-33   17    Job
 * 34-35   2     Recording
 * 36-36   1     cr/lf ctl
 * 37-37   1     reserved
 * 38-38   1     Blocked flag
 * 39-49   11    reserved
 * 50-51   2     offset
 * 52-79   28    reserved
 */
static const char* labels[] = {"HDR", "EOF", "EOV"};

/**
 * Write an ANSI or IBM 80 character tape label
 *
 * Type determines whether we are writing HDR, EOF, or EOV labels
 * Assume we are positioned to write the labels
 * Returns:  true of OK
 *           false if error
 */
bool WriteAnsiIbmLabels(DeviceControlRecord* dcr, int type, const char* VolName)
{
  Device* dev = dcr->dev;
  JobControlRecord* jcr = dcr->jcr;
  char ansi_volname[7]; /* 6 char + \0 */
  char label[80];       /* tape label */
  char date[20];        /* ansi date buffer */
  time_t now;
  int len, status, label_type;

  /*
   * If the Device requires a specific label type use it,
   * otherwise, use the type requested by the Director
   */
  if (dcr->device_resource->label_type != B_BAREOS_LABEL) {
    label_type = dcr->device_resource->label_type; /* force label type */
  } else {
    label_type = dcr->VolCatInfo.LabelType; /* accept Dir type */
  }

  switch (label_type) {
    case B_BAREOS_LABEL:
      return true;
    case B_ANSI_LABEL:
    case B_IBM_LABEL:
      ser_declare;
      Dmsg1(100, "Write ANSI label type=%d\n", label_type);
      len = strlen(VolName);
      if (len > 6) {
        Jmsg1(jcr, M_FATAL, 0,
              _("ANSI Volume label name \"%s\" longer than 6 chars.\n"),
              VolName);
        return false;
      }

      /*
       * ANSI labels have 6 characters, and are padded with spaces 'vol1\0' =>
       * 'vol1   \0'
       */
      strcpy(ansi_volname, VolName);
      for (int i = len; i < 6; i++) { ansi_volname[i] = ' '; }
      ansi_volname[6] = '\0'; /* only for debug */

      if (type == ANSI_VOL_LABEL) {
        SerBegin(label, sizeof(label));
        SerBytes("VOL1", 4);
        SerBytes(ansi_volname, 6);

        // Write VOL1 label
        if (label_type == B_IBM_LABEL) {
          AsciiToEbcdic(label, label, sizeof(label));
        } else {
          label[79] = '3'; /* ANSI label flag */
        }

        status = dev->write(label, sizeof(label));
        if (status != sizeof(label)) {
          BErrNo be;
          Jmsg3(jcr, M_FATAL, 0,
                _("Could not write ANSI VOL1 label. Wanted size=%d got=%d "
                  "ERR=%s\n"),
                sizeof(label), status, be.bstrerror());
          return false;
        }
      }

      // Now construct HDR1 label
      memset(label, ' ', sizeof(label));
      SerBegin(label, sizeof(label));
      SerBytes(labels[type], 3);
      SerBytes("1", 1);
      if (me->compatible) {
        SerBytes("BACULA.DATA", 11); /* Filename field */
      } else {
        SerBytes("BAREOS.DATA", 11); /* Filename field */
      }
      SerBegin(&label[21], sizeof(label) - 21); /* fileset field */
      SerBytes(ansi_volname, 6);                /* write Vol Ser No. */
      SerBegin(&label[27], sizeof(label) - 27);
      SerBytes("00010001000100",
               14); /* File section, File seq no, Generation no */
      now = time(NULL);
      SerBytes(ansi_date(now, date), 6);             /* current date */
      SerBytes(ansi_date(now - 24 * 3600, date), 6); /* created yesterday */
      SerBytes(" 000000Bareos              ", 27);

      // Write HDR1 label
      if (label_type == B_IBM_LABEL) {
        AsciiToEbcdic(label, label, sizeof(label));
      }

      // This could come at the end of a tape, ignore EOT errors.
      status = dev->write(label, sizeof(label));
      if (status != sizeof(label)) {
        BErrNo be;
        if (status == -1) {
          dev->clrerror(-1);
          if (dev->dev_errno == 0) {
            dev->dev_errno = ENOSPC; /* out of space */
          }
          if (dev->dev_errno != ENOSPC) {
            Jmsg1(jcr, M_FATAL, 0,
                  _("Could not write ANSI HDR1 label. ERR=%s\n"),
                  be.bstrerror());
            return false;
          }
        } else {
          Jmsg(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label.\n"));
          return false;
        }
      }

      // Now construct HDR2 label
      memset(label, ' ', sizeof(label));
      SerBegin(label, sizeof(label));
      SerBytes(labels[type], 3);
      SerBytes("2D3200032000", 12);

      // Write HDR2 label
      if (label_type == B_IBM_LABEL) {
        label[4] = 'V';
        AsciiToEbcdic(label, label, sizeof(label));
      }
      status = dev->write(label, sizeof(label));
      if (status != sizeof(label)) {
        BErrNo be;
        if (status == -1) {
          dev->clrerror(-1);
          if (dev->dev_errno == 0) {
            dev->dev_errno = ENOSPC; /* out of space */
          }
          if (dev->dev_errno != ENOSPC) {
            Jmsg1(jcr, M_FATAL, 0,
                  _("Could not write ANSI HDR1 label. ERR=%s\n"),
                  be.bstrerror());
            return false;
          }
          dev->weof(1);
          return true;
        } else {
          Jmsg(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label.\n"));
          return false;
        }
      }
      if (!dev->weof(1)) {
        Jmsg(jcr, M_FATAL, 0, _("Error writing EOF to tape. ERR=%s\n"),
             dev->errmsg);
        return false;
      }
      return true;
    default:
      Jmsg0(jcr, M_ABORT, 0,
            _("write_ansi_ibm_label called for non-ANSI/IBM type\n"));
      return false; /* should not get here */
  }
}

// Check a Bareos Volume name against an ANSI Volume name
static bool SameLabelNames(char* bareos_name, char* ansi_name)
{
  char* a = ansi_name;
  char* b = bareos_name;

  // Six characters max
  for (int i = 0; i < 6; i++) {
    if (*a == *b) {
      a++;
      b++;
      continue;
    }
    // ANSI labels are blank filled, Bareos's are zero terminated
    if (*a == ' ' && *b == 0) { return true; }

    return false;
  }

  // Reached 6 characters
  b++;
  if (*b == 0) { return true; }

  return false;
}

/**
 * ANSI date
 *  ' 'YYDDD
 */
static char* ansi_date(time_t td, char* buf)
{
  struct tm* tm;

  if (td == 0) { td = time(NULL); }
  tm = gmtime(&td);
  Bsnprintf(buf, 10, " %05d ",
            1000 * (tm->tm_year + 1900 - 2000) + tm->tm_yday);

  return buf;
}

} /* namespace storagedaemon */