-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtar
816 lines (773 loc) · 15.4 KB
/
rtar
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
From: Arnold Robbins <linus!gatech!arnold>
Subject: Diffs to tar to use a remote system's tape drive
Newsgroups: mod.sources
Approved: john@genrad.UUCP
Mod.sources: Volume 2, Issue 5
Submitted by: Arnold Robbins <linus!gatech!arnold>
* WARNING: The man page rmt(8) for /etc/rmt documents the remote mag
* tape protocol which rdump and rrestore use. Unfortunately, the man
* page is *WRONG*. The author of the routines I'm including originally
* wrote his code just based on the man page, and it didn't work, so he
* went to the rdump source to figure out why. The only thing he had to
* change was to check for the 'F' return code in addition to the 'E',
* and to separate the various arguments with \n instead of a space. I
* personally don't think that this is much of a problem, but I wanted to
* point it out.
*
* Arnold Robbins
The following context diffs to the 4.2 BSD tar.c and man page allow tar
to read and write tapes on a remote system's tape drive. The routines
at the end that deal with /etc/rmt across a pipe are general purpose and
in the public domain.
It is interesting to note that what rmt(8) says about its protocol, and what
you actually have to do to get it to work are quite different.
The command line syntax has not changed. Instead, the file name for the -f
option looks sorta like what rcp takes, e.g.
$ tar -cvf gatech:/dev/rmt8 /usr/src
In particular, if "/dev/" does not follow the colon, tar decides it is using
a regular, local file.
The diffs are for the tar that comes with BRL Unix. Your line numbers
may vary.
Arnold Robbins (rmt stuff courtesy of Jeff Lee, gatech!jeff)
arnold@gatech.{CSNET, UUCP}
--------------------- cut here ----------------------------
*** /usr/src/bin/tar.c Wed Nov 14 00:09:23 1984
--- tar.c Mon Jul 1 14:25:40 1985
***************
*** 115,120
char *getcwd();
char *getwd();
main(argc, argv)
int argc;
char *argv[];
--- 115,141 -----
char *getcwd();
char *getwd();
+ int open();
+ int read();
+ int write();
+ int close();
+ int ioctl();
+ long lseek();
+
+ int rmtopen();
+ int rmtread();
+ int rmtwrite();
+ int rmtclose();
+ int rmtioctl();
+ long rmtlseek();
+
+ int (*t_open)();
+ int (*t_read)();
+ int (*t_write)();
+ int (*t_close)();
+ int (*t_ioctl)();
+ long (*t_lseek)();
+
main(argc, argv)
int argc;
char *argv[];
***************
*** 264,269
nblock);
done(1);
}
if (rflag) {
if (cflag && tfile != NULL)
usage();
--- 285,293 -----
nblock);
done(1);
}
+
+ fix_remote();
+
if (rflag) {
if (cflag && tfile != NULL)
usage();
***************
*** 285,291
}
mt = dup(1);
nblock = 1;
! } else if ((mt = open(usefile, 2)) < 0) {
if (cflag == 0 || (mt = creat(usefile, 0666)) < 0) {
fprintf(stderr,
"tar: cannot open %s\n", usefile);
--- 309,315 -----
}
mt = dup(1);
nblock = 1;
! } else if ((mt = (*t_open)(usefile, 2)) < 0) {
if (cflag == 0 || (mt = creat(usefile, 0666)) < 0) {
fprintf(stderr,
"tar: cannot open %s\n", usefile);
***************
*** 298,304
if (strcmp(usefile, "-") == 0) {
mt = dup(0);
nblock = 1;
! } else if ((mt = open(usefile, 0)) < 0) {
fprintf(stderr, "tar: cannot open %s\n", usefile);
done(1);
}
--- 322,328 -----
if (strcmp(usefile, "-") == 0) {
mt = dup(0);
nblock = 1;
! } else if ((mt = (*t_open)(usefile, 0)) < 0) {
fprintf(stderr, "tar: cannot open %s\n", usefile);
done(1);
}
***************
*** 306,311
doxtract(argv);
else
dotable();
done(0);
}
--- 330,336 -----
doxtract(argv);
else
dotable();
+ (*t_close) (mt);
done(0);
}
***************
*** 1195,1201
{
first = 1;
if (recno >= nblock) {
! if (write(mt, tbuf, TBLOCK*nblock) < 0) {
fprintf(stderr, "tar: tape write error\n");
done(2);
}
--- 1220,1226 -----
{
first = 1;
if (recno >= nblock) {
! if ((*t_write)(mt, tbuf, TBLOCK*nblock) < 0) {
fprintf(stderr, "tar: tape write error\n");
done(2);
}
***************
*** 1209,1215
* residual to the tape buffer.
*/
while (recno == 0 && n >= nblock) {
! if (write(mt, buffer, TBLOCK*nblock) < 0) {
fprintf(stderr, "tar: tape write error\n");
done(2);
}
--- 1234,1240 -----
* residual to the tape buffer.
*/
while (recno == 0 && n >= nblock) {
! if ((*t_write)(mt, buffer, TBLOCK*nblock) < 0) {
fprintf(stderr, "tar: tape write error\n");
done(2);
}
***************
*** 1221,1227
bcopy(buffer, (char *)&tbuf[recno++], TBLOCK);
buffer += TBLOCK;
if (recno >= nblock) {
! if (write(mt, tbuf, TBLOCK*nblock) < 0) {
fprintf(stderr, "tar: tape write error\n");
done(2);
}
--- 1246,1252 -----
bcopy(buffer, (char *)&tbuf[recno++], TBLOCK);
buffer += TBLOCK;
if (recno >= nblock) {
! if ((*t_write)(mt, tbuf, TBLOCK*nblock) < 0) {
fprintf(stderr, "tar: tape write error\n");
done(2);
}
***************
*** 1240,1246
struct mtget mtget;
if (mtdev == 1)
! mtdev = ioctl(mt, MTIOCGET, &mtget);
if (mtdev == 0) {
if (ioctl(mt, MTIOCTOP, &mtop) < 0) {
fprintf(stderr, "tar: tape backspace error\n");
--- 1265,1271 -----
struct mtget mtget;
if (mtdev == 1)
! mtdev = (*t_ioctl)(mt, MTIOCGET, &mtget);
if (mtdev == 0) {
if ((*t_ioctl)(mt, MTIOCTOP, &mtop) < 0) {
fprintf(stderr, "tar: tape backspace error\n");
***************
*** 1242,1248
if (mtdev == 1)
mtdev = ioctl(mt, MTIOCGET, &mtget);
if (mtdev == 0) {
! if (ioctl(mt, MTIOCTOP, &mtop) < 0) {
fprintf(stderr, "tar: tape backspace error\n");
done(4);
}
--- 1267,1273 -----
if (mtdev == 1)
mtdev = (*t_ioctl)(mt, MTIOCGET, &mtget);
if (mtdev == 0) {
! if ((*t_ioctl)(mt, MTIOCTOP, &mtop) < 0) {
fprintf(stderr, "tar: tape backspace error\n");
done(4);
}
***************
*** 1247,1253
done(4);
}
} else
! lseek(mt, (long) -TBLOCK*nblock, 1);
recno--;
}
--- 1272,1278 -----
done(4);
}
} else
! (*t_lseek)(mt, (long) -TBLOCK*nblock, 1);
recno--;
}
***************
*** 1253,1259
flushtape()
{
! write(mt, tbuf, TBLOCK*nblock);
}
bread(fd, buf, size)
--- 1278,1284 -----
flushtape()
{
! (*t_write)(mt, tbuf, TBLOCK*nblock);
}
bread(fd, buf, size)
***************
*** 1265,1271
static int lastread = 0;
if (!Bflag)
! return (read(fd, buf, size));
for (count = 0; count < size; count += lastread) {
if (lastread < 0) {
if (count > 0)
--- 1290,1296 -----
static int lastread = 0;
if (!Bflag)
! return ((*t_read)(fd, buf, size));
for (count = 0; count < size; count += lastread) {
if (lastread < 0) {
if (count > 0)
***************
*** 1272,1278
return (count);
return (lastread);
}
! lastread = read(fd, buf, size - count);
buf += lastread;
}
return (count);
--- 1297,1303 -----
return (count);
return (lastread);
}
! lastread = (*t_read)(fd, buf, size - count);
buf += lastread;
}
return (count);
***************
*** 1288,1291
exit(1);
}
return (buf);
}
--- 1313,1758 -----
exit(1);
}
return (buf);
+ }
+
+ fix_remote ()
+ {
+ char *cp, *index ();
+
+ if ((cp = index (usefile, ':')) == NULL ||
+ strncmp (cp + 1, "/dev/", 5) != 0)
+ {
+ t_open = open;
+ t_read = read;
+ t_write = write;
+ t_close = close;
+ t_ioctl = ioctl;
+ t_lseek = lseek;
+ }
+ else
+ {
+ t_open = rmtopen;
+ t_read = rmtread;
+ t_write = rmtwrite;
+ t_close = rmtclose;
+ t_ioctl = rmtioctl;
+ t_lseek = rmtlseek;
+ }
+ }
+
+ /*
+ * rmt --- remote tape emulator subroutines
+ *
+ * Originally written by Jeff Lee, modified some by Arnold Robbins
+ */
+
+ /* these are included above, except setjmp.h */
+ /*
+ #include <stdio.h>
+ #include <errno.h>
+ #include <setjmp.h>
+ #include <signal.h>
+
+ #include <sys/types.h>
+ #include <sys/ioctl.h>
+ #include <sys/mtio.h>
+ */
+
+ #include <setjmp.h>
+
+ /*
+ * MAXUNIT --- Maximum number of remote tape file units
+ *
+ * READ --- Return the number of the read side file descriptor
+ * WRITE --- Return the number of the write side file descriptor
+ */
+
+ #define MAXUNIT 4
+
+ #define READ(fd) (Ctp[fd][0])
+ #define WRITE(fd) (Ptc[fd][1])
+
+ static int Ctp[MAXUNIT][2] = { -1, -1, -1, -1, -1, -1, -1, -1 };
+ static int Ptc[MAXUNIT][2] = { -1, -1, -1, -1, -1, -1, -1, -1 };
+
+ jmp_buf Jmpbuf;
+ extern int errno;
+
+
+
+ /*
+ * abort --- close off a remote tape connection
+ */
+
+ static abort(tfd)
+ int tfd;
+ {
+ close(READ(tfd));
+ close(WRITE(tfd));
+ READ(tfd) = -1;
+ WRITE(tfd) = -1;
+ }
+
+
+
+ /*
+ * command --- attempt to perform a remote tape command
+ */
+
+ static command(tfd, buf)
+ char *buf;
+ int tfd;
+ {
+ int blen;
+ int (*pstat)();
+
+ /*
+ * save current pipe status and try to make the request
+ */
+
+ blen = strlen(buf);
+ pstat = signal(SIGPIPE, SIG_IGN);
+ if (write(WRITE(tfd), buf, blen) == blen)
+ {
+ signal(SIGPIPE, pstat);
+ return(0);
+ }
+
+ /*
+ * something went wrong. close down and go home
+ */
+
+ signal(SIGPIPE, pstat);
+ abort(tfd);
+
+ errno = EIO;
+ return(-1);
+ }
+
+
+
+ /*
+ * status --- retrieve the status from the pipe
+ */
+
+ static status(tfd)
+ int tfd;
+ {
+ int i;
+ char c, *cp;
+ char buf[64];
+
+ /*
+ * read the reply command line
+ */
+
+ for (i = 0, cp = buf; i < 64; i++, cp++)
+ {
+ if (read(READ(tfd), cp, 1) != 1)
+ {
+ abort(tfd);
+ errno = EIO;
+ return(-1);
+ }
+ if (*cp == '\n')
+ {
+ *cp = 0;
+ break;
+ }
+ }
+
+ if (i == 64)
+ {
+ abort(tfd);
+ errno = EIO;
+ return(-1);
+ }
+
+ /*
+ * check the return status
+ */
+
+ for (cp = buf; *cp; cp++)
+ if (*cp != ' ')
+ break;
+
+ if (*cp == 'E' || *cp == 'F')
+ {
+ errno = atoi(cp + 1);
+ while (read(READ(tfd), &c, 1) == 1)
+ if (c == '\n')
+ break;
+
+ if (*cp == 'F')
+ abort(tfd);
+
+ return(-1);
+ }
+
+ /*
+ * check for mis-synced pipes
+ */
+
+ if (*cp != 'A')
+ {
+ abort(tfd);
+ errno = EIO;
+ return(-1);
+ }
+
+ return(atoi(cp + 1));
+ }
+
+
+
+ /*
+ * rmtopen --- open a magtape device on system specified
+ */
+
+ rmtopen(dev, mode)
+ char *dev;
+ int mode;
+ {
+ int i, rc;
+ char buf[64];
+ char *sys;
+
+ /*
+ * first, find an open pair of file descriptors
+ */
+
+ for (i = 0; i < MAXUNIT; i++)
+ if (READ(i) == -1 && WRITE(i) == -1)
+ break;
+
+ if (i == MAXUNIT)
+ {
+ errno = EMFILE;
+ return(-1);
+ }
+
+ /*
+ * pull apart system and device
+ */
+ for (sys = dev; *dev != ':'; dev++)
+ ;
+ *dev++ = '\0';
+
+ /*
+ * setup the pipes for the 'rsh' command and fork
+ */
+
+ if (pipe(Ptc[i]) == -1 || pipe(Ctp[i]) == -1)
+ return(-1);
+
+ if ((rc = fork()) == -1)
+ return(-1);
+
+ if (rc == 0)
+ {
+ close(0);
+ dup(Ptc[i][0]);
+ close(Ptc[i][0]); close(Ptc[i][1]);
+ close(1);
+ dup(Ctp[i][1]);
+ close(Ctp[i][0]); close(Ctp[i][1]);
+
+ execl("/usr/ucb/rsh", "rsh", sys, "/etc/rmt", (char *) 0);
+
+ /*
+ * bad problems if we get here
+ */
+
+ perror("exec");
+ exit(1);
+ }
+
+ close(Ptc[i][0]); close(Ctp[i][1]);
+
+ /*
+ * now attempt to open the tape device
+ */
+
+ sprintf(buf, "O%s\n%d\n", dev, mode);
+ if (command(i, buf) == -1 || status(i) == -1)
+ return(-1);
+
+ return(i);
+ }
+
+
+
+ /*
+ * rmtclose --- close a remote magtape unit and shut down
+ */
+
+ rmtclose(tfd)
+ int tfd;
+ {
+ int rc;
+
+ if (command(tfd, "C\n") != -1)
+ {
+ rc = status(tfd);
+
+ abort(tfd);
+ return(rc);
+ }
+
+ return(-1);
+ }
+
+
+
+ /*
+ * rmtread --- read a buffer from a remote tape
+ */
+
+ rmtread(tfd, data, cnt)
+ int tfd, cnt;
+ char *data;
+ {
+ int rc, i;
+ char buf[64];
+
+ sprintf(buf, "R%d\n", cnt);
+ if (command(tfd, buf) == -1 || (rc = status(tfd)) == -1)
+ return(-1);
+
+ for (i = 0; i < rc; i += cnt, data += cnt)
+ {
+ cnt = read(READ(tfd), data, rc);
+ if (cnt <= 0)
+ {
+ abort(tfd);
+ errno = EIO;
+ return(-1);
+ }
+ }
+
+ return(rc);
+ }
+
+
+
+ /*
+ * rmtwrite --- write a buffer to the remote tape
+ */
+
+ rmtwrite(tfd, data, cnt)
+ int tfd, cnt;
+ char *data;
+ {
+ int rc;
+ char buf[64];
+ int (*pstat)();
+
+ sprintf(buf, "W%d\n", cnt);
+ if (command(tfd, buf) == -1)
+ return(-1);
+
+ pstat = signal(SIGPIPE, SIG_IGN);
+ if (write(WRITE(tfd), data, cnt) == cnt)
+ return(status(tfd));
+
+ abort(tfd);
+ errno = EIO;
+ return(-1);
+ }
+
+
+
+ /*
+ * rmtlseek --- perform an imitation lseek operation remotely
+ */
+
+ rmtlseek(tfd, wh, off)
+ int tfd, wh, off;
+ {
+ char buf[64];
+
+ sprintf(buf, "L%d\n%d\n", wh, off);
+ if (command(tfd, buf) == -1)
+ return(-1);
+
+ return(status(tfd));
+ }
+
+
+
+ /*
+ * rmtioctl --- perform raw tape operations remotely
+ */
+
+ rmtioctl(tfd, op, arg)
+ int tfd, op;
+ char *arg;
+ {
+ char c;
+ int rc, cnt;
+ char buf[64];
+
+ /*
+ * MTIOCOP is the easy one. nothing is transfered in binary
+ */
+
+ if (op == MTIOCTOP)
+ {
+ sprintf(buf, "I%d\n%d\n", ((struct mtop *) arg)->mt_op,
+ ((struct mtop *) arg)->mt_count);
+ if (command(tfd, buf) == -1)
+ return(-1);
+ return(status(tfd));
+ }
+
+ /*
+ * we can only handle 2 ops, if not the other one, punt
+ */
+
+ if (op != MTIOCGET)
+ {
+ errno = EINVAL;
+ return(-1);
+ }
+
+ /*
+ * grab the status and read it directly into the structure
+ * this assumes that the status buffer is (hopefully) not
+ * padded and that 2 shorts fit in a long without any word
+ * alignment problems, ie - the whole struct is contiguous
+ * NOTE - this is probably NOT a good assumption.
+ */
+
+ if (command(tfd, "S\n") == -1 || (rc = status(tfd)) == -1)
+ return(-1);
+
+ for (; rc > 0; rc -= cnt, arg += cnt)
+ {
+ cnt = read(READ(tfd), arg, rc);
+ if (cnt <= 0)
+ {
+ abort(tfd);
+ errno = EIO;
+ return(-1);
+ }
+ }
+
+ /*
+ * now we check for byte position. mt_type is a small integer field
+ * (normally) so we will check its magnitude. if it is larger than
+ * 256, we will assume that the bytes are swapped and go through
+ * and reverse all the bytes
+ */
+
+ if (((struct mtget *) arg)->mt_type < 256)
+ return(0);
+
+ for (cnt = 0; cnt < rc; cnt += 2)
+ {
+ c = arg[cnt];
+ arg[cnt] = arg[cnt+1];
+ arg[cnt+1] = c;
+ }
+
+ return(0);
}
*** /usr/man/man1/tar.1 Mon Jun 27 00:35:14 1983
--- tar.1 Mon Jul 1 13:51:09 1985
***************
*** 95,101
.B f
.I Tar
uses the next argument as the name of the archive instead of
! /dev/rmt?. If the name of the file is `\-', tar writes to standard output or
reads from standard input, whichever is appropriate. Thus,
.I tar
can be used as the head or tail of a filter chain.
--- 95,113 -----
.B f
.I Tar
uses the next argument as the name of the archive instead of
! /dev/rmt?.
! .sp
! If the file name has the form
! .IR system :/dev/???,
! .I tar
! will use the tape drive /dev/??? on the remote system
! .IR system ,
! via
! .IR rsh (1),
! and
! .IR rmt (8).
! .sp
! If the name of the file is `\-', tar writes to standard output or
reads from standard input, whichever is appropriate. Thus,
.I tar
can be used as the head or tail of a filter chain.
***************
*** 179,181
The current limit on file name length is 100 characters.
.br
There is no way to selectively follow symbolic links.
--- 191,195 -----
The current limit on file name length is 100 characters.
.br
There is no way to selectively follow symbolic links.
+ .br
+ Using a remote system's tape drive can be slow.