-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtapeio.c
676 lines (588 loc) · 16.7 KB
/
tapeio.c
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
Routines to do magtape I/O to local tapes, remote tapes, and tape image
files.
Copyright 1998, 1999 John Wilson and Eric Smith
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as published
by the Free Software Foundation. Note that permission is not granted
to redistribute this program under the terms of any other version of the
General Public 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
08/10/93 JMBW IBM mainframe TCP socket stuff (was using many files).
07/08/94 JMBW Local magtape code.
03/13/95 JMBW Converted to separate routines.
07/19/98 JMBW Added support for "rmt" remote tape protocol.
11/16/98 ELS Provide struct for per-instance variables.
02/06/98 ELS Reorganization, and added skiprec and skipfile.
*/
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h> /* for lseek() SEEK_SET, SEEK_END under Linux */
#include <errno.h>
#ifdef _AIX /* maybe this will be enough to make it compile on AIX */
#include <sys/tape.h>
#define MTWEOF STWEOF
#define MTREW STREW
#define MTFSR STFSR
#define MTFSF STFSF
#define MTBSR STRSR
#define MTIOCTOP STIOCTOP
/* not sure about these two (SCSI only): */
#define MTSETBLK STSETBLK
#define MTSETDENSITY STSETDENSITY
#define mtop stop
#elif __APPLE__
#define MTWEOF 0
#define MTREW 0
#define MTFSR 0
#define MTFSF 0
#define MTBSR 0
#define MTIOCTOP 0
#define MTSETBLK 0
#define MTSETDENSITY 0
struct mtop { int mt_op; int mt_count; };
#else
#include <sys/mtio.h>
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#define MTSETBLK MTSETBSIZ
#define MTSETDENSITY MTSETDNSTY
#endif
#endif
#include "tapeio.h"
#define TT_UNK 0
#define TT_TAPE 1 /* honest to god tape drive */
#define TT_IMAGE 2 /* file containing image of a tape */
#define TT_RMT 3 /* rmt tape server */
struct mtape_t
{
int tape_type;
int tapefd; /* tape drive, file, or socket file descriptor */
int seek_ok;
int flags;
unsigned long bpi; /* tape density (for tape length msg) */
int waccess; /* NZ => tape opened for write access access */
unsigned long count; /* count of frames written to tape */
char netbuf[80]; /* buffer for net commands and responses */
};
#ifndef O_BINARY
#define O_BINARY 0
#endif
/* default tape drive device name */
#define TAPE "/dev/nst0"
/* default tape density */
#define BPI 1600
/* magtape commands */
static struct mtop mt_weof={ MTWEOF, 1 }; /* operation, count */
static struct mtop mt_rew={ MTREW, 1 };
static struct mtop mt_fsr={ MTFSR, 1 };
static struct mtop mt_fsf={ MTFSF, 1 };
static struct mtop mt_bsr={ MTBSR, 1 };
/* SCSI only: */
static struct mtop mt_setblk={ MTSETBLK, 0 }; /* blockize = 0 (variable) */
static struct mtop mt_setden={ MTSETDENSITY, 0x02 }; /* density = 1600 */
#define FAIL(msg) do { fprintf (stderr, msg); goto fail; } while (0)
/* do a write and check the return status, punt on error */
static void dowrite (int handle, void *buf, int len)
{
if (write (handle, buf, len) != len)
{
perror ("?Error on write");
exit (1);
}
}
/* do a read and keep trying until we get all bytes */
static void doread (int handle, void *buf, int len)
{
int n;
while(len)
{
if ((n = read (handle, buf, len)) < 0)
{
perror("?Error on read");
exit (1);
}
if (n == 0)
{
fprintf (stderr, "?Unexpected end of file\n");
exit (1);
}
buf += n;
len -= n;
}
}
/* get response from "rmt" server */
static int response (tape_handle_t mtape)
{
char c, rc;
int n;
doread (mtape->tapefd, &rc, 1); /* get success/error code */
if (rc != 'A' && rc != 'E')
{ /* must be Acknowledge or Error */
fprintf (stderr, "?Invalid rmt response code: %c\n",rc);
exit (1);
}
/* get numeric value (returned by both A and E responses) */
for (n=0;;)
{
doread (mtape->tapefd, &c, 1); /* get next digit */
if (c < '0' || c > '9')
break; /* not a digit */
n = n * 10 + (c - '0'); /* add new digit in */
/* ideally would check for overflow */
}
if (c != '\n')
{ /* first non-digit char must be <LF> */
fprintf (stderr, "?Invalid rmt response terminator: %3.3o\n",
((int) c) & 0377);
exit (1);
}
if (rc == 'A')
return (n); /* success, return value >=0 */
/* (unless overflowed) */
do
doread (mtape->tapefd, &c, 1);
while (c != '\n'); /* ignore until next LF */
errno = n; /* set error number */
return (-1);
}
/* send ioctl() command to local or remote tape drive */
static int doioctl (tape_handle_t mtape, struct mtop *op)
{
int len;
if (mtape->tape_type == TT_TAPE)
return (ioctl (mtape->tapefd, MTIOCTOP, op));
else
{ /* "rmt" tape server */
/* form cmd (better hope remote MT_OP values are the same) */
len = sprintf (mtape->netbuf, "I%d\n%d\n", op->mt_op, op->mt_count);
dowrite (mtape->tapefd, mtape->netbuf, len);
return (response (mtape));
}
}
/* open the tape drive (or whatever) */
/* "create" =1 to create if file, "writable" =1 to open with write access */
tape_handle_t opentape (char *name, int create, int writable)
{
char *p, *user, *port;
int len;
char *host = NULL;
tape_handle_t mtape = NULL;
mtape = (tape_handle_t) calloc (1, sizeof (struct mtape_t));
if (! mtape)
FAIL ("?can't allocate mtape struct\n");
mtape->bpi = BPI;
mtape->waccess = writable; /* remember if we're writing */
mtape->count = 0; /* nothing transferred yet */
/* get tape filename */
if (name == NULL)
name = getenv("TAPE"); /* get from environment */
if (name == NULL)
name = TAPE; /* or use our default */
/* just a file if no colon in filename */
if ((p = index (name, ':')) == NULL)
{
/* there's probably a better way to handle this, in case a file is really
a link to a tape drive -- handler index or something? */
if (strncmp (name, "/dev/", 5) == 0)
{
/* assume tape if starts with /dev/ */
mtape->tape_type = TT_TAPE;
mtape->tapefd = open (name, (writable ? O_RDWR : O_RDONLY), 0);
}
else
{ /* otherwise file */
mtape->tape_type = TT_IMAGE;
if (strcmp (name, "-") ==0 )
{ /* stdin/stdout */
if (writable)
mtape->tapefd=1;
else
mtape->tapefd=0;
}
else
{
if (create)
mtape->tapefd = open (name, O_CREAT | O_TRUNC |
O_WRONLY | O_BINARY, 0644);
else
{
mtape->tapefd = open (name, (writable ? O_RDWR : O_RDONLY) |
O_BINARY, 0);
mtape->seek_ok = 1;
}
}
}
if (mtape->tapefd < 0)
FAIL ("?can't open device or file\n");
}
else
{ /* "rmt" tape server on remote host */
mtape->tape_type = TT_RMT;
/* split filename around ':' */
len = p-name;
port = p+1;
/* can't necessarily modify tape[] so copy it first */
if ((host = malloc (len + 1)) == NULL)
FAIL ("?can't allocate string for hostname\n");
strncpy (host, name, len); /* copy hostname */
host [len] = 0; /* tack on null */
/* connect to "rexec" server */
if ((p = index (host, '@')) == NULL)
{
p = host; /* no @, point at hostname */
user = NULL;
}
else
{
*p++ = '\0'; /* shoot out @, point at host name */
user = (*p != '\0') ? host : NULL; /* keep non-null user */
}
#if !defined(__APPLE__) && !defined(__OpenBSD__)
if ((mtape->tapefd = rexec (&p, htons (512), user, NULL, "/etc/rmt",
(int *) NULL)) < 0)
FAIL ("?Connection failed\n");
#endif
/* build rmt "open device" command */
if ((1 + strlen (port) + 1 + 1 + 1 + 1) > sizeof (mtape->netbuf))
FAIL ("?Device name too long\n");
len = sprintf (mtape->netbuf, "O%s\n%d\n", port, writable ? O_RDWR : O_RDONLY);
dowrite (mtape->tapefd, mtape->netbuf, len);
if (response (mtape) < 0)
FAIL ("?Error opening tape drive");
}
/* SCSI setup for local/remote tape drive */
if ((mtape->tape_type == TT_TAPE) ||
(mtape->tape_type == TT_RMT))
{
/* (ignore errors in case not SCSI) */
/* set variable record length mode */
doioctl (mtape, & mt_setblk);
/* set density to 1600 */
doioctl (mtape, & mt_setden);
}
if (host)
free (host);
return (mtape);
fail:
if (mtape)
{
if (host)
free (host);
free (mtape);
}
return (NULL);
}
/* close the tape drive */
void closetape (tape_handle_t mtape)
{
if (mtape->waccess)
{ /* opened for create/append */
tapemark (mtape); /* add one more tape mark */
/* (should have one already) */
}
if (mtape->tape_type == TT_RMT)
{
dowrite (mtape->tapefd, "C\n", 2);
if (response (mtape) < 0)
{
perror("?Error closing remote tape");
exit(1);
}
}
if (close (mtape->tapefd) < 0)
{
perror("?Error closing tape");
exit(1);
}
free (mtape);
}
/* rewind tape */
void posnbot (tape_handle_t mtape)
{
if (mtape->tape_type == TT_IMAGE)
{ /* image file */
if (lseek (mtape->tapefd, 0L, SEEK_SET) < 0)
{
perror("?Seek failed");
exit(1);
}
}
else
{ /* local/remote tape drive */
if (doioctl (mtape, & mt_rew) < 0)
{
perror("?Rewind failed");
exit(1);
}
}
}
/* position tape at EOT (between the two tape marks) */
void posneot (tape_handle_t mtape)
{
if (mtape->tape_type == TT_IMAGE)
{ /* image file */
if (lseek (mtape->tapefd, -4L, SEEK_END) < 0)
{
perror("?Seek failed");
exit(1);
}
}
else
{ /* local/remote tape drive */
doioctl (mtape, & mt_bsr); /* in case already at LEOT */
while (1)
{
/* space forward a file */
if (doioctl (mtape, & mt_fsf) < 0)
{
perror("?Error spacing to EOT");
exit(1);
}
/* space one record more to see if double EOF */
if (doioctl (mtape, & mt_fsr) < 0)
break;
/* might want to check errno to make sure it's the right error */
}
#if 1
/* "man mtio" doesn't say whether MTFSR actually moves past */
/* the tape mark, let's assume it does */
if (doioctl (mtape, & mt_bsr) < 0)
{ /* get between them */
perror("?Error backspacing at EOT");
exit(1);
}
#endif
}
}
/* read a tape record, return actual length (0=tape mark) */
int getrec (tape_handle_t mtape, void *buf, int len)
{
unsigned char byte [4]; /* 32 bits for length field(s) */
unsigned long l; /* at least 32 bits */
int i;
if (mtape->tape_type == TT_IMAGE)
{ /* image file */
doread (mtape->tapefd, byte, 4); /* get record length */
l=((unsigned long)byte[3]<<24L)|((unsigned long)byte[2]<<16L)|
((unsigned long)byte[1]<<8L)|(unsigned long)byte[0];
/* compose into longword */
if (l > len)
goto toolong; /* don't read if too long for buf */
if (l != 0)
{ /* get data unless tape mark */
char x;
doread (mtape->tapefd, buf, l); /* read data */
if ((l & 1) != 0 && (mtape->flags & TF_SIMH) != 0)
doread (mtape->tapefd, &x, 1);
doread (mtape->tapefd, byte, 4); /* get trailing record length */
if((((unsigned long)byte[3]<<24L)|
((unsigned long)byte[2]<<16L)|
((unsigned long)byte[1]<<8)|
(unsigned long)byte[0])!=l)
{ /* should match */
fprintf (stderr,"?Corrupt tape image\n");
exit(1);
}
}
}
else if (mtape->tape_type == TT_RMT)
{ /* rmt tape server */
len = sprintf (mtape->netbuf, "R%d\n", len);
dowrite (mtape->tapefd, mtape->netbuf, len);
if ((i = response (mtape)) < 0)
{
perror("?Error reading tape");
exit(1);
}
l = i;
if (l)
doread (mtape->tapefd, buf, l);
}
else
{ /* local tape drive */
if ((i = read (mtape->tapefd, buf, len)) < 0)
{
perror("?Error reading tape");
exit(1);
}
l = i;
}
return(l);
toolong:
fprintf(stderr,"?%ld byte tape record too long for %d byte buffer\n",
l,len);
exit(1);
}
/* write a tape record */
void putrec (tape_handle_t mtape, void *buf, int len)
{
unsigned char l [4];
if (mtape->tape_type == TT_IMAGE)
{ /* image file */
l [0] = len & 0377; /* PDP-11 byte order */
l [1] = (len >> 8) &0377;
l [2] = 0; /* our recs are always < 64 KB */
l [3] = 0;
dowrite (mtape->tapefd, l, 4); /* write longword length */
dowrite (mtape->tapefd, buf, len); /* write data */
dowrite (mtape->tapefd, l, 4); /* write length again */
}
else if (mtape->tape_type == TT_RMT)
{ /* rmt tape */
int n;
n = sprintf (mtape->netbuf, "W%d\n", len);
dowrite (mtape->tapefd, mtape->netbuf, n);
dowrite (mtape->tapefd, buf, len);
}
else
dowrite (mtape->tapefd, buf, len); /* just write the data if tape */
mtape->count += len + (mtape->bpi * 3 /5); /* add to byte count
(+0.6" tape gap) */
}
/* write a tape mark */
void tapemark (tape_handle_t mtape)
{
static char zero [4] = { 0, 0, 0, 0 };
if (mtape->tape_type == TT_IMAGE)
{ /* image file */
dowrite (mtape->tapefd, zero, 4); /* write longword length */
}
else
{ /* local/remote tape drive */
if (doioctl (mtape, & mt_weof) < 0)
{
perror ("?Failed writing tape mark");
exit (1);
}
}
mtape->count += 3 * mtape->bpi; /* 3" of tape */
}
/* skip records (negative for reverse) */
void skiprec (tape_handle_t mtape, int count)
{
unsigned char byte [4]; /* 32 bits for length field(s) */
unsigned long l; /* at least 32 bits */
if (mtape->tape_type != TT_IMAGE)
{
fprintf (stderr, "?Record skip only implemented for image files");
exit (1);
}
if (count < 0)
{
fprintf (stderr, "?Record skip reverse not yet implemented");
exit (1);
}
while (count--)
{
doread (mtape->tapefd, byte, 4); /* get record length */
/* compose into longword */
l=((unsigned long)byte[3]<<24L)|((unsigned long)byte[2]<<16L)|
((unsigned long)byte[1]<<8L)|(unsigned long)byte[0];
if (l == 0) /* hit tape mark? */
return; /* note that we've effectively skipped over the tape mark */
/* skip record */
if (lseek (mtape->tapefd, l, SEEK_CUR) < 0)
{
perror ("?Seek failed");
exit (1);
}
doread (mtape->tapefd, byte, 4); /* get trailing record length */
if((((unsigned long)byte[3]<<24L)|
((unsigned long)byte[2]<<16L)|
((unsigned long)byte[1]<<8)|
(unsigned long)byte[0])!=l)
{ /* should match */
fprintf (stderr,"?Corrupt tape image\n");
exit(1);
}
}
}
/* skip forward to the next file mark, and leave the tape positioned
after the mark */
static void skip_to_mark (tape_handle_t mtape)
{
unsigned char byte [4]; /* 32 bits for length field(s) */
unsigned long l; /* at least 32 bits */
static char scratch_buf [4096];
if (mtape->tape_type != TT_IMAGE)
{
fprintf (stderr, "?Record skip only implemented for image files");
exit (1);
}
for (;;)
{
doread (mtape->tapefd, byte, 4); /* get record length */
/* compose into longword */
l=((unsigned long)byte[3]<<24L)|((unsigned long)byte[2]<<16L)|
((unsigned long)byte[1]<<8L)|(unsigned long)byte[0];
if (l == 0) /* hit tape mark? */
return; /* note that we've effectively skipped over the tape mark */
/* skip record */
if (mtape->seek_ok)
{
if (lseek (mtape->tapefd, l, SEEK_CUR) < 0)
{
perror ("?Seek failed");
exit (1);
}
}
else
{
int len, len2;
len = l;
while (len > 0)
{
len2 = len;
if (len2 > sizeof (scratch_buf))
len2 = sizeof (scratch_buf);
doread (mtape->tapefd, scratch_buf, len2); /* read data */
len -= len2;
}
}
doread (mtape->tapefd, byte, 4); /* get trailing record length */
if((((unsigned long)byte[3]<<24L)|
((unsigned long)byte[2]<<16L)|
((unsigned long)byte[1]<<8)|
(unsigned long)byte[0])!=l)
{ /* should match */
fprintf (stderr,"?Corrupt tape image\n");
exit(1);
}
}
}
/* skip files (negative for reverse) */
void skipfile (tape_handle_t mtape, int count)
{
if (mtape->tape_type != TT_IMAGE)
{
fprintf (stderr, "?File skip only implemented for image files");
exit (1);
}
if (count < 0)
{
fprintf (stderr, "?File skip reverse not yet implemented");
exit (1);
}
while (count--)
{
skip_to_mark (mtape);
}
}
/* set tape flags */
void tapeflags (tape_handle_t mtape, int flags)
{
mtape->flags = flags;
}