Skip to content

Commit ef02e6c

Browse files
ManzurManzur
Manzur
authored and
Manzur
committed
- Added non-complete fileserver implementation(myfs.b);
- Made modules appropriate to be used from fileserver; - Made some stylistic changes;
1 parent 3dda4c4 commit ef02e6c

31 files changed

+697
-306
lines changed

cat-file.b

+16-31
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ implement Catfile;
33
include "sys.m";
44
sys: Sys;
55

6-
include "draw.m";
7-
86
include "tables.m";
97
tables: Tables;
108
Strhash: import tables;
@@ -16,49 +14,36 @@ Iobuf: import bufio;
1614
include "utils.m";
1715
utils: Utils;
1816

19-
include "arg.m";
20-
arg: Arg;
21-
22-
Catfile: module
23-
{
24-
init: fn(nil: ref Draw->Context, args: list of string);
25-
};
17+
include "cat-file.m";
2618

19+
msgchan: chan of array of byte;
2720
printtypeonly := 0;
21+
REPOPATH: string;
2822

29-
init(nil: ref Draw->Context, args: list of string)
23+
init(repopath: string, typeonly: int, path: string, ch: chan of array of byte)
3024
{
3125
sys = load Sys Sys->PATH;
32-
if(len args < 2)
33-
usage();
3426
utils = load Utils Utils->PATH;
35-
arg = load Arg Arg->PATH;
36-
utils->init();
37-
arg->init(args);
38-
while((c := arg->opt()) != 0)
39-
{
40-
case c
41-
{
42-
't' => printtypeonly = 1;
43-
* => usage();return;
44-
}
45-
}
46-
catfile(hd arg->argv());
47-
}
4827

28+
REPOPATH = repopath;
29+
utils->init(repopath);
30+
31+
printtypeonly = typeonly;
32+
msgchan = ch;
33+
catfile(path);
34+
}
4935

5036
catfile(path: string)
5137
{
5238
(filetype, filesize, buf) := utils->readsha1file(path);
5339
offset := 0;
54-
sys->print("filetype: %s\n", filetype);
55-
if(printtypeonly)
40+
41+
msgchan <-= sys->aprint("filetype: %s\n", filetype);
42+
if(printtypeonly){
43+
msgchan <-= nil;
5644
return;
57-
while(offset < len buf)
58-
{
59-
cnt := sys->write(sys->fildes(1), buf[offset:], len buf - offset);
60-
offset += cnt;
6145
}
46+
msgchan <-= buf[offset:];
6247
}
6348

6449
usage()

cat-file.m

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Catfile: module
2+
{
3+
PATH: con "/dis/git/cat-file.dis";
4+
init: fn(repopath: string, printtypeonly: int, path: string, ch: chan of array of byte);
5+
};

checkout-index.b

+21-36
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
implement Checkoutindex;
22

3+
include "checkout-index.m";
4+
35
include "sys.m";
46
sys: Sys;
57

6-
include "draw.m";
7-
88
include "tables.m";
99
tables: Tables;
1010
Strhash: import tables;
@@ -24,28 +24,26 @@ INDEXPATH, sha2string,string2path, readsha1file: import utils;
2424
include "arg.m";
2525
arg: Arg;
2626

27-
Checkoutindex: module
28-
{
29-
init: fn(nil: ref Draw->Context, args: list of string);
30-
};
31-
32-
3327
index: ref Index;
3428
stderr: ref Sys->FD;
3529
all: int = 0;
3630
force: int = 0;
31+
REPOPATH: string;
3732

38-
init(nil: ref Draw->Context, args: list of string)
33+
init(args: list of string)
3934
{
4035
sys = load Sys Sys->PATH;
4136
arg = load Arg Arg->PATH;
4237
gitindex = load Gitindex Gitindex->PATH;
4338
tables = load Tables Tables->PATH;
4439
utils = load Utils Utils->PATH;
45-
utils->init();
46-
index = Index.new();
40+
41+
REPOPATH = hd args;
42+
args = tl args;
43+
utils->init(REPOPATH);
44+
index = Index.new(REPOPATH);
4745
stderr = sys->fildes(2);
48-
if(!index.readindex(INDEXPATH))
46+
if(!index.readindex(REPOPATH + INDEXPATH))
4947
{
5048
sys->fprint(stderr, "index read error\n");
5149
exit;
@@ -77,29 +75,25 @@ init(nil: ref Draw->Context, args: list of string)
7775

7876
checkoutfile(path: string)
7977
{
78+
fullpath := REPOPATH + path;
8079
entry := index.entries.find(path);
81-
if(entry == nil)
82-
{
80+
if(entry == nil){
8381
sys->print("No such file: %s\n", path);
8482
return;
8583
}
8684
#if file exists and force flag is off
87-
if(!force && sys->open(path, Sys->OREAD) != nil)
88-
{
85+
if(!force && sys->open(fullpath, Sys->OREAD) != nil){
8986
sys->fprint(stderr, "file(%s) exists\n", path);
9087
return;
9188
}
92-
fd := sys->create(path, Sys->OWRITE, 8r644);
93-
if(fd == nil)
94-
{
95-
makedirs(dirname(path));
96-
fd = sys->create(path,Sys->OWRITE, 8r644);
97-
if(fd == nil)
98-
{
89+
fd := sys->create(fullpath, Sys->OWRITE, 8r644);
90+
if(fd == nil){
91+
makedirs(dirname(fullpath));
92+
fd = sys->create(fullpath, Sys->OWRITE, 8r644);
93+
if(fd == nil){
9994
sys->fprint(stderr, "file(%s) couldn't be created\n", path);
10095
return;
10196
}
102-
10397
}
10498
checkoutentry(fd, entry);
10599
}
@@ -108,15 +102,13 @@ checkoutentry(fd: ref Sys->FD, entry: ref Entry)
108102
{
109103
(filetype, filesize, buf) := readsha1file(sha2string(entry.sha1));
110104
sys->print("filetype: %s; filesize: %d\n", filetype, filesize);
111-
if(sys->write(fd, buf, len buf) != len buf)
112-
{
105+
if(sys->write(fd, buf, len buf) != len buf){
113106
sys->fprint(stderr, "error occured while writing to file: %r\n");
114107
return;
115108
}
116109
dirstat := sys->nulldir;
117110
dirstat.mode = entry.mode & 1023;
118-
if(sys->fwstat(fd, dirstat))
119-
{
111+
if(sys->fwstat(fd, dirstat)){
120112
sys->fprint(stderr, "stat can't be changed: %r\n");
121113
return;
122114
}
@@ -126,29 +118,22 @@ checkoutentry(fd: ref Sys->FD, entry: ref Entry)
126118
checkoutall()
127119
{
128120
for(l := index.entries.all(); l != nil; l = tl l)
129-
{
130121
checkoutfile((hd l).name);
131-
}
132122
}
133123

134124
dirname(path: string): string
135125
{
136-
for(i := len path - 1; i >= 0; i--)
137-
{
126+
for(i := len path - 1; i >= 0; i--){
138127
if(path[i] == '/')
139-
{
140128
return path[0:i];
141-
}
142129
}
143130
return ".";
144131
}
145132

146133
makedirs(path: string)
147134
{
148135
if(!exists(path))
149-
{
150136
makedirs(dirname(path));
151-
}
152137
sys->create(path, Sys->OREAD, Sys->DMDIR|8r755);
153138
}
154139

checkout-index.m

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
Checkoutindex: module
3+
{
4+
init: fn(args: list of string);
5+
};
6+
7+

checkrepo.b

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
implement Checkrepo;
22

3+
include "checkrepo.m";
4+
35
include "sys.m";
46
sys: Sys;
57

6-
include "draw.m";
7-
88
include "tables.m";
99
Strhash: import Tables;
1010

@@ -16,18 +16,12 @@ include "utils.m";
1616
utils: Utils;
1717
SHALEN, readsha1file, isdir, sha2string, OBJECTSTOREPATH: import utils;
1818

19-
2019
include "readdir.m";
2120
readdir: Readdir;
2221

2322
include "string.m";
2423
stringmodule: String;
2524

26-
Checkrepo: module
27-
{
28-
init: fn(nil: ref Draw->Context, args: list of string);
29-
};
30-
3125
Object: adt
3226
{
3327
name: string;
@@ -44,15 +38,18 @@ objects := array[HASHSZ] of list of ref Object;
4438

4539
checkvalidness: int;
4640
stderr: ref Sys->FD;
41+
REPOPATH: string;
4742

48-
init(nil: ref Draw->Context, args: list of string)
43+
init(args: list of string)
4944
{
5045
sys = load Sys Sys->PATH;
5146
utils = load Utils Utils->PATH;
5247
bufio = load Bufio Bufio->PATH;
5348
readdir = load Readdir Readdir->PATH;
5449
stringmodule = load String String->PATH;
55-
utils->init();
50+
51+
REPOPATH = hd args;
52+
utils->init(REPOPATH);
5653
stderr = sys->fildes(2);
5754
check();
5855
}
@@ -62,7 +59,6 @@ check()
6259
readrepo(OBJECTSTOREPATH);
6360

6461
cnt := 0;
65-
6662
for(i := 0; i < HASHSZ; i++){
6763
cnt += len objects[i];
6864
for(l := objects[i]; l != nil; l = tl l){

checkrepo.m

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Checkrepo: module
2+
{
3+
init: fn(args: list of string);
4+
};
5+

commit-tree.b

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
implement Committree;
22

3+
include "commit-tree.m";
4+
35
include "sys.m";
46
sys: Sys;
57

6-
include "draw.m";
7-
88
include "arg.m";
99
arg: Arg;
1010

@@ -33,15 +33,10 @@ include "env.m";
3333
env: Env;
3434

3535
stderr: ref Sys->FD;
36-
37-
Committree: module
38-
{
39-
init: fn(nil: ref Draw->Context, args: list of string);
40-
};
41-
36+
REPOPATH: string;
4237

4338

44-
init(nil: ref Draw->Context, args: list of string)
39+
init(args: list of string)
4540
{
4641
sys = load Sys Sys->PATH;
4742
arg = load Arg Arg->PATH;
@@ -57,11 +52,14 @@ init(nil: ref Draw->Context, args: list of string)
5752
if(len args < 2)
5853
{
5954
usage();
60-
return;
55+
exit;
6156
}
57+
58+
REPOPATH = hd args;
59+
args = tl args;
6260

6361
arg->init(args);
64-
utils->init();
62+
utils->init(REPOPATH);
6563
deflate->init();
6664

6765
parents: list of string = nil;
@@ -82,24 +80,21 @@ init(nil: ref Draw->Context, args: list of string)
8280

8381
commit(treesha: string, parents: list of string): string
8482
{
85-
if(!utils->exists(treesha))
86-
{
83+
if(!utils->exists(treesha)){
8784
sys->fprint(stderr, "no such tree file\n");
8885
return "";
8986
}
9087

91-
for(l := parents; l != nil; l = tl l)
92-
{
88+
for(l := parents; l != nil; l = tl l){
9389
if(!utils->exists(hd l)){
9490
sys->fprint(stderr, "no such sha file: %s\n", hd l);
9591
return "";
9692
}
97-
9893
}
94+
9995
commitmsg := "";
10096
commitmsg += "tree " + treesha + "\n";
101-
while(parents != nil)
102-
{
97+
while(parents != nil){
10398
commitmsg += "parent " + (hd parents) + "\n";
10499
parents = tl parents;
105100
}
@@ -128,7 +123,7 @@ commit(treesha: string, parents: list of string): string
128123
#6 - "commit", 1 - " ", 1 - '\0'
129124
buf := array[6 + 1 + len commitlen + 1 + len commitmsg] of byte;
130125

131-
buf[:] = array of byte ("commit " + int2string(len commitmsg));
126+
buf[:] = sys->aprint("commit %d", len commitmsg);
132127
buf[7 + len commitlen] = byte 0;
133128
buf[7 + len commitlen + 1:] = array of byte commitmsg;
134129

@@ -142,8 +137,9 @@ commit(treesha: string, parents: list of string): string
142137
ch <-= (0, buf);
143138
(sz, sha) = <-ch;
144139

140+
fd := sys->open(REPOPATH + "head", Sys->OWRITE);
141+
sys->fprint(fd, "%s", treesha);
145142
return sha2string(sha);
146-
147143
}
148144

149145

commit-tree.m

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
Committree: module
3+
{
4+
init: fn(args: list of string);
5+
};

0 commit comments

Comments
 (0)