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
|
implement Bundle, Fsmodule;
include "sys.m";
sys: Sys;
include "bufio.m";
bufio: Bufio;
Iobuf: import bufio;
include "readdir.m";
readdir: Readdir;
include "draw.m";
include "sh.m";
include "alphabet/reports.m";
reports: Reports;
Report, quit, report: import reports;
include "alphabet/fs.m";
fs: Fs;
Value: import fs;
Fschan, Fsdata, Entrychan, Entry,
Gatechan, Gatequery, Nilentry, Option,
Next, Down, Skip, Quit: import Fs;
Bundle: module {};
# XXX if we can't open a directory, is it ever worth passing its metadata
# through anyway?
EOF: con "end of archive\n";
types(): string
{
return "fx";
}
badmod(p: string)
{
sys->fprint(sys->fildes(2), "fs: bundle: cannot load %s: %r\n", p);
raise "fail:bad module";
}
init()
{
sys = load Sys Sys->PATH;
readdir = load Readdir Readdir->PATH;
if(readdir == nil)
badmod(Readdir->PATH);
bufio = load Bufio Bufio->PATH;
if(bufio == nil)
badmod(Readdir->PATH);
bufio->fopen(nil, Sys->OREAD); # XXX no bufio->init!
fs = load Fs Fs->PATH;
if(fs == nil)
badmod(Readdir->PATH);
fs->init();
reports = load Reports Reports->PATH;
if(reports == nil)
badmod(Reports->PATH);
}
run(nil: ref Draw->Context, r: ref Report,
nil: list of Option, args: list of ref Value): ref Value
{
f := chan of ref Sys->FD;
spawn bundleproc((hd args).x().i, f, r.start("bundle"));
return ref Value.Vf(f);
}
#bundle(r: ref Report, iob: ref Iobuf, c: Fschan): chan of string
bundle(nil: ref Report, nil: ref Iobuf, nil: Fschan): chan of string
{
return nil;
# sync := chan[1] of string;
# spawn bundleproc(c, sync, iob, r.start("bundle"));
# return sync;
}
bundleproc(c: Fschan, f: chan of ref Sys->FD, errorc: chan of string)
{
f <-= nil;
if((fd := <-f) == nil){
(<-c).t1 <-= Quit;
quit(errorc);
}
iob := bufio->fopen(fd, Sys->OWRITE);
fd = nil;
(d, reply) := <-c;
if(d.dir == nil){
report(errorc, "no root directory");
endarchive(iob, errorc);
}
if(puts(iob, dir2header(d.dir), errorc) == -1){
reply <-= Quit;
quit(errorc);
}
reply <-= Down;
bundledir(d.dir.name, d, c, iob, errorc);
endarchive(iob, errorc);
}
endarchive(iob: ref Iobuf, errorc: chan of string)
{
{
if(puts(iob, EOF, errorc) != -1)
iob.flush();
sys->fprint(iob.fd, "");
} exception {
"write on closed pipe" =>
;
}
quit(errorc);
}
bundledir(path: string, d: Fsdata,
c: Fschan,
iob: ref Iobuf, errorc: chan of string)
{
if(d.dir.mode & Sys->DMDIR){
path[len path] = '/';
for(;;){
(ent, reply) := <-c;
if(ent.dir == nil){
reply <-= Skip;
break;
}
if(puts(iob, dir2header(ent.dir), errorc) == -1){
reply <-= Quit;
quit(errorc);
}
reply <-= Down;
bundledir(path + ent.dir.name, ent, c, iob, errorc);
}
iob.putc('\n');
}else{
buf: array of byte;
reply: chan of int;
length := big d.dir.length;
n := big 0;
for(;;){
((nil, buf), reply) = <-c;
if(buf == nil){
reply <-= Skip;
break;
}
if(write(iob, buf, len buf, errorc) != len buf){
reply <-= Quit;
quit(errorc);
}
n += big len buf;
if(n > length){ # should never happen
report(errorc, sys->sprint("%q is longer than expected (fatal)", path));
reply <-= Quit;
quit(errorc);
}
if(n == length){
reply <-= Skip;
break;
}
reply <-= Next;
}
if(n < length){
report(errorc, sys->sprint("%q is shorter than expected (%bd/%bd); adding null bytes", path, n, length));
buf = array[Sys->ATOMICIO] of {* => byte 0};
while(n < length){
nb := len buf;
if(length - n < big len buf)
nb = int (length - n);
if(write(iob, buf, nb, errorc) != nb){
(<-c).t1 <-= Quit;
quit(errorc);
}
report(errorc, sys->sprint("added %d null bytes", nb));
n += big nb;
}
}
}
}
dir2header(d: ref Sys->Dir): string
{
return sys->sprint("%q %uo %q %q %ud %bd\n", d.name, d.mode, d.uid, d.gid, d.mtime, d.length);
}
puts(iob: ref Iobuf, s: string, errorc: chan of string): int
{
{
if(iob.puts(s) == -1)
report(errorc, sys->sprint("write error: %r"));
return 0;
} exception {
"write on closed pipe" =>
report(errorc, sys->sprint("write on closed pipe"));
return -1;
}
}
write(iob: ref Iobuf, buf: array of byte, n: int, errorc: chan of string): int
{
{
nw := iob.write(buf, n);
if(nw < n){
if(nw >= 0)
report(errorc, "short write");
else{
report(errorc, sys->sprint("write error: %r"));
}
}
return nw;
} exception {
"write on closed pipe" =>
report(errorc, "write on closed pipe");
return -1;
}
}
|