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
|
implement Mkext;
include "sys.m";
sys: Sys;
Dir, sprint, fprint: import sys;
include "draw.m";
include "bufio.m";
bufio: Bufio;
Iobuf: import bufio;
include "string.m";
str: String;
include "arg.m";
arg: Arg;
Mkext: module
{
init: fn(nil: ref Draw->Context, nil: list of string);
};
LEN: con Sys->ATOMICIO;
NFLDS: con 6; # filename, modes, uid, gid, mtime, bytes
bin: ref Iobuf;
uflag := 0;
tflag := 0;
hflag := 0;
vflag := 0;
fflag := 0;
stderr: ref Sys->FD;
bout: ref Iobuf;
argv0 := "mkext";
init(nil: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
stderr = sys->fildes(2);
bufio = load Bufio Bufio->PATH;
if(bufio == nil)
error(sys->sprint("cannot load %s: %r\n", Bufio->PATH));
str = load String String->PATH;
if(str == nil)
error(sys->sprint("cannot load %s: %r\n", String->PATH));
arg = load Arg Arg->PATH;
if(arg == nil)
error(sys->sprint("cannot load %s: %r\n", Arg->PATH));
destdir := "";
arg->init(args);
arg->setusage("mkext [-h] [-d destdir] [-T] [-u] [-v] [file ...]");
while((c := arg->opt()) != 0)
case c {
'd' =>
destdir = arg->earg();
'f' =>
fflag = 1;
'h' =>
hflag = 1;
bout = bufio->fopen(sys->fildes(1), Sys->OWRITE);
if(bout == nil)
error(sys->sprint("can't access standard output: %r"));
'u' =>
uflag = 1;
tflag = 1;
't' or 'T' =>
tflag = 1;
'v' =>
vflag = 1;
* =>
arg->usage();
}
args = arg->argv();
bin = bufio->fopen(sys->fildes(0), Sys->OREAD);
if(bin == nil)
error(sys->sprint("can't access standard input: %r"));
while((p := bin.gets('\n')) != nil){
if(p == "end of archive\n"){
fprint(stderr, "done\n");
quit(nil);
}
fields := str->unquoted(p);
if(len fields != NFLDS){
warn("too few fields in file header");
continue;
}
name := hd fields;
fields = tl fields;
(mode, nil) := str->toint(hd fields, 8);
fields = tl fields;
uid := hd fields;
fields = tl fields;
gid := hd fields;
fields = tl fields;
(mtime, nil) := str->toint(hd fields, 10);
fields = tl fields;
(bytes, nil) := str->tobig(hd fields, 10);
if(args != nil){
if(!selected(name, args)){
if(bytes != big 0)
seekpast(bytes);
continue;
}
mkdirs(destdir, name);
}
name = destdir+name;
if(hflag){
bout.puts(sys->sprint("%q %s %s %s %ud %bd\n",
name, octal(mode), uid, gid, mtime, bytes));
if(bytes != big 0)
seekpast(bytes);
continue;
}
if(mode & Sys->DMDIR)
mkdir(name, mode, mtime, uid, gid);
else
extract(name, mode, mtime, uid, gid, bytes);
}
fprint(stderr, "premature end of archive\n");
quit("eof");
}
quit(s: string)
{
if(bout != nil)
bout.flush();
if(s != nil)
raise "fail: "+s;
exit;
}
fileprefix(prefix, s: string): int
{
n := len prefix;
m := len s;
if(n > m || !str->prefix(prefix, s))
return 0;
if(m > n && s[n] != '/')
return 0;
return 1;
}
selected(s: string, args: list of string): int
{
for(; args != nil; args = tl args)
if(fileprefix(hd args, s))
return 1;
return 0;
}
mkdirs(basedir, name: string)
{
(nil, names) := sys->tokenize(name, "/");
while(names != nil) {
#sys->print("mkdir %s\n", basedir);
create(basedir, Sys->OREAD, 8r775|Sys->DMDIR);
if(tl names == nil)
break;
basedir = basedir + "/" + hd names;
names = tl names;
}
}
mkdir(name: string, mode: int, mtime: int, uid: string, gid: string)
{
d: Dir;
i: int;
fd := create(name, Sys->OREAD, mode);
if(fd == nil){
(i, d) = sys->stat(name);
if(i < 0 || !(d.mode & Sys->DMDIR)){
warn(sys->sprint("can't make directory %s: %r", name));
return;
}
}else{
(i, d) = sys->fstat(fd);
if(i < 0)
warn(sys->sprint("can't stat %s: %r", name));
fd = nil;
}
d = sys->nulldir;
(nil, p) := str->splitr(name, "/");
if(p == nil)
p = name;
d.name = p;
if(tflag)
d.mtime = mtime;
if(uflag){
d.uid = uid;
d.gid = gid;
}
d.mode = mode;
if(sys->wstat(name, d) < 0)
warn(sys->sprint("can't set modes for %s: %r", name));
if(uflag){
(i, d) = sys->stat(name);
if(i < 0)
warn(sys->sprint("can't reread modes for %s: %r", name));
if(d.mtime != mtime)
warn(sys->sprint("%s: time mismatch %ud %ud\n", name, mtime, d.mtime));
if(uid != d.uid)
warn(sys->sprint("%s: uid mismatch %s %s", name, uid, d.uid));
if(gid != d.gid)
warn(sys->sprint("%s: gid mismatch %s %s", name, gid, d.gid));
}
}
extract(name: string, mode: int, mtime: int, uid: string, gid: string, bytes: big)
{
n: int;
if(vflag)
sys->print("x %s %bd bytes\n", name, bytes);
sfd := create(name, Sys->OWRITE, mode);
if(sfd == nil) {
if(!fflag || sys->remove(name) == -1 ||
(sfd = create(name, Sys->OWRITE, mode)) == nil) {
warn(sys->sprint("can't make file %s: %r", name));
seekpast(bytes);
return;
}
}
b := bufio->fopen(sfd, Bufio->OWRITE);
if (b == nil) {
warn(sys->sprint("can't open file %s for bufio : %r", name));
seekpast(bytes);
return;
}
buf := array [LEN] of byte;
for(tot := big 0; tot < bytes; tot += big n){
n = len buf;
if(tot + big n > bytes)
n = int(bytes - tot);
n = bin.read(buf, n);
if(n <= 0)
error(sys->sprint("premature eof reading %s", name));
if(b.write(buf, n) != n)
warn(sys->sprint("error writing %s: %r", name));
}
(i, nil) := sys->fstat(b.fd);
if(i < 0)
warn(sys->sprint("can't stat %s: %r", name));
d := sys->nulldir;
(nil, p) := str->splitr(name, "/");
if(p == nil)
p = name;
d.name = p;
if(tflag)
d.mtime = mtime;
if(uflag){
d.uid = uid;
d.gid = gid;
}
d.mode = mode;
if(b.flush() == Bufio->ERROR)
warn(sys->sprint("error writing %s: %r", name));
if(sys->fwstat(b.fd, d) < 0)
warn(sys->sprint("can't set modes for %s: %r", name));
if(uflag){
(i, d) = sys->fstat(b.fd);
if(i < 0)
warn(sys->sprint("can't reread modes for %s: %r", name));
if(d.mtime != mtime)
warn(sys->sprint("%s: time mismatch %ud %ud\n", name, mtime, d.mtime));
if(d.uid != uid)
warn(sys->sprint("%s: uid mismatch %s %s", name, uid, d.uid));
if(d.gid != gid)
warn(sys->sprint("%s: gid mismatch %s %s", name, gid, d.gid));
}
b.close();
}
seekpast(bytes: big)
{
n: int;
buf := array [LEN] of byte;
for(tot := big 0; tot < bytes; tot += big n){
n = len buf;
if(tot + big n > bytes)
n = int(bytes - tot);
n = bin.read(buf, n);
if(n <= 0)
error("premature eof");
}
}
error(s: string)
{
fprint(stderr, "%s: %s\n", argv0, s);
quit("error");
}
warn(s: string)
{
fprint(stderr, "%s: %s\n", argv0, s);
}
octal(i: int): string
{
s := "";
do {
t: string;
t[0] = '0' + (i&7);
s = t+s;
} while((i = (i>>3)&~(7<<29)) != 0);
return s;
}
parent(name : string) : string
{
slash := -1;
for (i := 0; i < len name; i++)
if (name[i] == '/')
slash = i;
if (slash > 0)
return name[0:slash];
return "/";
}
create(name : string, rw : int, mode : int) : ref Sys->FD
{
fd := sys->create(name, rw, mode);
if (fd == nil) {
p := parent(name);
(ok, d) := sys->stat(p);
if (ok < 0)
return nil;
omode := d.mode;
d = sys->nulldir;
d.mode = omode | 8r222; # ensure parent is writable
if(sys->wstat(p, d) < 0) {
warn(sys->sprint("can't set modes for %s: %r", p));
return nil;
}
fd = sys->create(name, rw, mode);
d.mode = omode;
sys->wstat(p, d);
}
return fd;
}
|