summaryrefslogtreecommitdiff
path: root/sys/src/cmd/tapefs/cpiofs.c
blob: 591c80f1d2344996d67de8843e6e57a6f6fdeb2c (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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "tapefs.h"

/*
 * File system for cpio tapes (read-only)
 */

union hblock {
	char tbuf[Maxbuf];
} dblock;

typedef  void HdrReader(Fileinf *);

Biobuf	*tape;

static void
addrfatal(char *fmt, va_list arg)
{
	char buf[1024];

	vseprint(buf, buf+sizeof(buf), fmt, arg);
	fprint(2, "%s: %#llx: %s\n", argv0, Bseek(tape, 0, 1), buf);
	exits(buf);
}

static int
egetc(void)
{
	int c;

	if((c = Bgetc(tape)) == Beof)
		sysfatal("unexpected eof");
	if(c < 0)
		sysfatal("read error: %r");
	return c;
}

static ushort
rd16le()
{
	ushort x;

	return x = egetc(), x |= egetc()<<8;
}

static ulong
rd3211()
{
	ulong x;

	return x = egetc()<<16, x |= egetc()<<24, x |= egetc(), x |= egetc()<<8;
}

/* sysvr3 and sysvr4 skip records with names longer than 256. pwb 1.0,
32V, sysiii, sysvr1, and sysvr2 overrun their 256 byte buffer */
static void
rdpwb(Fileinf *f, ushort (*rd16)(void), ulong (*rd32)(void))
{
	int namesz, n;
	static char buf[256];

	rd16();	/* dev */
	rd16();	/* ino */
	f->mode = rd16();
	f->uid = rd16();
	f->gid = rd16();
	rd16();	/* nlink */
	rd16();	/* rdev */
	f->mdate = rd32();
	namesz = rd16();
	f->size = rd32();

	/* namesz include the trailing nul */
	if(namesz == 0)
		sysfatal("name too small");
	if(namesz > sizeof(buf))
		sysfatal("name too big");

	if((n = Bread(tape, buf, namesz)) < 0)
		sysfatal("read error: %r");
	if(n < namesz)
		sysfatal("unexpected eof");

	if(buf[n-1] != '\0')
		sysfatal("no nul after file name");
	if((n = strlen(buf)) != namesz-1)
		sysfatal("mismatched name length: saw %d; expected %d", n, namesz-1);
	f->name = buf;

	/* skip padding */
	if(Bseek(tape, 0, 1) & 1)
		egetc();
}

static void
rdpwb11(Fileinf *f)
{
	rdpwb(f, rd16le, rd3211);
}

static vlong
rdasc(int n)
{
	vlong x;
	int y;

	for(x = 0; n > 0; n--) {
		if((y = egetc() - '0') & ~7)
			sysfatal("not octal");
		x = x<<3 | y;
	}
	return x;
}

/* sysvr3 and sysvr4 skip records with names longer than 256. sysiii,
sysvr1, and sysvr2 overrun their 256 byte buffer */
static void
rdsysiii(Fileinf *f)
{
	int namesz, n;
	static char buf[256];

	rdasc(6);	/* dev */
	rdasc(6);	/* ino */
	f->mode = rdasc(6);
	f->uid = rdasc(6);
	f->gid = rdasc(6);
	rdasc(6);	/* nlink */
	rdasc(6);	/* rdev */
	f->mdate = rdasc(11);
	namesz = rdasc(6);
	f->size = rdasc(11);

	/* namesz includes the trailing nul */
	if(namesz == 0)
		sysfatal("name too small");
	if(namesz > sizeof (buf))
		sysfatal("name too big");

	if((n = Bread(tape, buf, namesz)) < 0)
		sysfatal("read error: %r");
	if(n < namesz)
		sysfatal("unexpected eof");

	if(buf[n-1] != '\0')
		sysfatal("no nul after file name");
	if((n = strlen(buf)) != namesz-1)
		sysfatal("mismatched name length: saw %d; expected %d", n, namesz-1);
	f->name = buf;
}

static HdrReader *
rdmagic(void)
{
	uchar buf[6];

	buf[0] = egetc();
	buf[1] = egetc();
	if(buf[0] == 0xc7 && buf[1] == 0x71)
		return rdpwb11;

	buf[2] = egetc();
	buf[3] = egetc();
	buf[4] = egetc();
	buf[5] = egetc();
	if(memcmp(buf, "070707", 6) == 0)
		return rdsysiii;

	sysfatal("Out of phase--get MERT help");
	return nil;
}

void
populate(char *name)
{
	HdrReader *rdhdr, *prevhdr;
	Fileinf f;

	/* the tape buffer may not be the ideal size for scanning the
	record headers */
	if((tape = Bopen(name, OREAD)) == nil)
		sysfatal("Can't open argument file");

	extern void (*_sysfatal)(char *, va_list);
	_sysfatal = addrfatal;

	prevhdr = nil;
	replete = 1;
	for(;;) {
		/* sysiii and sysv implementations don't allow
		multiple header types within a single tape, so we
		won't either */
		rdhdr = rdmagic();
		if(prevhdr != nil && rdhdr != prevhdr)
			sysfatal("mixed headers");
		rdhdr(&f);

		while(f.name[0] == '/')
			f.name++;
		if(f.name[0] == '\0')
			sysfatal("nameless record");
		if(strcmp(f.name, "TRAILER!!!") == 0)
			break;
		switch(f.mode & 0170000) {
		case 0040000:
			f.mode = DMDIR | f.mode&0777;
			break;
		case 0100000:	/* normal file */
		case 0120000:	/* symlink */
			f.mode &= 0777;
			break;
		default:	/* sockets, pipes, devices */
			f.mode = 0;
			break;
		}
		f.addr = Bseek(tape, 0, 1);
		poppath(f, 1);

		Bseek(tape, f.size, 1);

		/* skip padding */
		if(rdhdr == rdpwb11 && (Bseek(tape, 0, 1) & 1))
			egetc();
	}
}

void
dotrunc(Ram *r)
{
	USED(r);
}

void
docreate(Ram *r)
{
	USED(r);
}

char *
doread(Ram *r, vlong off, long cnt)
{
	Bseek(tape, r->addr+off, 0);
	if (cnt>sizeof(dblock.tbuf))
		sysfatal("read too big");
	Bread(tape, dblock.tbuf, cnt);
	return dblock.tbuf;
}

void
popdir(Ram *r)
{
	USED(r);
}

void
dowrite(Ram *r, char *buf, long off, long cnt)
{
	USED(r); USED(buf); USED(off); USED(cnt);
}

int
dopermw(Ram *r)
{
	USED(r);
	return 0;
}