-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.b
372 lines (304 loc) · 6.51 KB
/
utils.b
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
implement Utils;
include "utils.m";
include "sys.m";
sys: Sys;
include "draw.m";
include "keyring.m";
keyring: Keyring;
include "filter.m";
inflate: Filter;
deflate: Filter;
include "bufio.m";
bufio: Bufio;
Iobuf: import bufio;
include "string.m";
strmodule: String;
include "tables.m";
tables: Tables;
Strhash: import tables;
REPOPATH: string;
hex := array[16] of {"0","1","2", "3", "4", "5", "6", "7", "8", "9",
"a","b","c", "d", "e", "f"};
stderr: ref Sys->FD;
init(repopath: string)
{
sys = load Sys Sys->PATH;
keyring = load Keyring Keyring->PATH;
inflate = load Filter Filter->INFLATEPATH;
deflate = load Filter Filter->DEFLATEPATH;
bufio = load Bufio Bufio->PATH;
strmodule = load String String->PATH;
tables = load Tables Tables->PATH;
inflate->init();
deflate->init();
stderr = sys->fildes(2);
REPOPATH = repopath;
}
writesha1file(ch: chan of (int, array of byte))
{
rqchan := deflate->start("z9");
old := array[0] of byte;
buf: array of byte;
sz := 0;
mainloop:
while(1)
{
pick rq := <-rqchan
{
Finished => if(len rq.buf > 0)
sys->print("Data remained\n");
break mainloop;
Result =>
new := array[len old + len rq.buf] of byte;
new[:] = old;
new[len old:] = rq.buf;
old = new;
rq.reply <-= 0;
Fill =>
(sz, buf) = <-ch;
rq.buf[:] = buf[:];
rq.reply <-= sz;
Error =>
sys->print("error ocurred: %s\n", rq.e);
return;
}
}
sha := bufsha1(old);
save2file(string2path(sha2string(sha)), old);
sys->print("file was written to: %s\n", sha2string(sha));
ch <-= (SHALEN, sha);
}
readsha1file(shafilename: string): (string, int, array of byte)
{
sys->print("in readshafile\n");
fd := sys->open(string2path(shafilename), Sys->OREAD);
if(fd == nil){
sys->print("file not found\n");
return ("", 0, nil);
}
sys->print("after if\n");
rqchan := inflate->start("z");
old := array[0] of byte;
mainloop:
while(1){
pick rq := <-rqchan
{
Finished => if(len rq.buf > 0)
sys->print("Data remained\n");
break mainloop;
Result =>
new := array[len old + len rq.buf] of byte;
new[:] = old;
new[len old:] = rq.buf;
old = new;
rq.reply <-= 0;
Fill =>
buf := array[len rq.buf] of byte;
cnt := sys->read(fd, buf, len buf);
rq.buf[:] = buf[:];
rq.reply <-= cnt;
Error =>
sys->print("error ocurred: %s\n", rq.e);
return ("", 0, nil);
}
}
for(i := 0; i < len old; i++){
if(old[i] == byte 0)
break;
}
if(i < 0 || i >= len old){
sys->print("wrong file format\n");
return ("",0,nil);
}
pos := strchr(string old[:i], ' ');
sys->print("finishing readhs1\n");
return (string old[:pos], bytes2int(old, pos + 1), old[i+1:]);
}
strchr(s: string, ch: int): int
{
for(i := 0; i < len s; i++){
if(s[i] == ch)
return i;
}
return -1;
}
string2path(filename: string): string
{
return REPOPATH + "objects/" + filename[:2] + "/" + filename[2:];
}
sha2string(sha: array of byte): string
{
ret : string = "";
for(i := 0; i < SHALEN; i++){
i1 : int = int sha[i] & 16rf;
i2 : int = (int sha[i] & 16rf0) >> 4;
ret += hex[i2] + hex[i1];
}
return ret;
}
bufsha1(buf: array of byte): array of byte
{
sha := array[SHALEN] of byte;
keyring->sha1(buf, len buf, sha, nil);
return sha;
}
exists(shaname: string): int
{
return sys->open(string2path(shaname), Sys->OREAD) != nil;
}
filesha1(filename: string): array of byte
{
fd := sys->open(filename, Sys->OREAD);
if(fd == nil){
return nil;
}
buf := array[Sys->ATOMICIO] of byte;
cnt : int;
state : ref Keyring->DigestState = nil;
while((cnt = sys->read(fd, buf, Sys->ATOMICIO)) > 0){
state = keyring->sha1(buf,cnt, nil, state);
}
sha := array[SHALEN] of byte;
keyring->sha1(buf,0, sha, state);
return sha;
}
save2file(path: string, buf: array of byte): int
{
fd := sys->create(path, Sys->OWRITE, 8r644);
if(fd == nil || sys->write(fd, buf, len buf) != len buf)
return 1;
return 0;
}
int2string(num: int): string
{
sys = load Sys Sys->PATH;
ret := "";
do{
ret = hex[num % 10] + ret;
num /= 10;
}while(num > 0);
return ret;
}
bytes2int(buf: array of byte, offset: int): int
{
ret := 0;
for(i := 0; i < INTSZ; i++)
ret |= int(buf[offset + i]) << (i * 8);
return ret;
}
bytes2big(buf: array of byte, offset: int): big
{
return (big bytes2int(buf,offset + INTSZ) << INTSZ * 8)|
(big bytes2int(buf, offset));
}
big2bytes(n: big): array of byte
{
ret := array[BIGSZ] of byte;
part1 := int (n >> INTSZ * 8);
part2 := int n;
copyarray(ret, 0, int2bytes(part2), 0, INTSZ);
copyarray(ret, INTSZ, int2bytes(part1), 0, INTSZ);
return ret;
}
int2bytes(number: int): array of byte
{
ret := array[INTSZ] of byte;
ret[0] = byte (number & 16rff);
number >>= 8;
ret[1] = byte (number & 16rff);
number >>= 8;
ret[2] = byte (number & 16rff);
number >>= 8;
ret[3] = byte (number & 16rff);
return ret;
}
allocnr(num: int): int
{
return (num + 16) * 3 / 2;
}
copyarray(dst : array of byte, doffset : int, src : array of byte, soffset, count : int)
{
for(i := 0; i < count; i++)
dst[i + doffset] = src[i + soffset];
}
equalqids(q1, q2: Sys->Qid): int
{
return q1.path == q2.path &&
q1.vers == q2.vers &&
q1.qtype == q2.qtype;
}
extractfile(shafilename: string): string
{
tempfile := "/tmp/gitfile";
buf := (readsha1file(shafilename)).t2;
fd := sys->create(tempfile, Sys->OWRITE, 8r644);
offset := 0;
while(offset < len buf)
offset += sys->write(fd, buf[offset:len buf], len buf - offset);
return tempfile;
}
getuserinfo(): ref Strhash[ref Config]
{
iobuf := bufio->open(REPOPATH + "config", Bufio->OREAD);
config := Strhash[ref Config].new(10, nil);
while((s := iobuf.gets('\n')) != "")
{
if(iswhitespace(s))
return nil;
s = chomp(s);
(s1, s2) := strmodule->splitl(s, "=");
s2 = s2[1:];
config.add(s1, ref Config(s1, s2));
}
return config;
}
iswhitespace(s: string): int
{
return s == "\n" || s == "\t" || s == " ";
}
chomp(s: string): string
{
return s[:len s - 1];
}
readline(ibuf: ref Iobuf): string
{
ret := "";
i := 0;
while(1)
{
c := ibuf.getc();
if(c == '\n' || c < 0)
break;
ret[i++] = c;
}
return ret;
}
equalshas(sha1, sha2: array of byte): int
{
for(i := 0; i < SHALEN; i++){
if(sha1[i] != sha2[i])
return 0;
}
return 1;
}
fail(s: string)
{
warn(s);
exit;
}
warn(s: string)
{
sys->fprint(sys->fildes(2), "%s\n", s);
}
isdir(mode: int): int
{
return mode & 16384;
}
bytepos(a: array of byte, offset: int, delim: byte): int
{
for(i := offset; i < len a; i++){
if(a[i] == delim)
return i;
}
return -1;
}