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
|
implement Fsmodule;
include "sys.m";
sys: Sys;
include "draw.m";
include "sh.m";
include "fslib.m";
fslib: Fslib;
Report, Value, type2s, report, quit: import fslib;
Fschan, Fsdata, Entrychan, Entry,
Gatechan, Gatequery, Nilentry, Option,
Next, Down, Skip, Quit: import Fslib;
this is a bad idea, i think
i think walk + filter + setroot is good enough.
types(): string
{
# usage: readfile [-f file] name
return "xs-fs";
}
badmod(p: string)
{
sys->fprint(sys->fildes(2), "fs: readfile: cannot load %s: %r\n", p);
raise "fail:bad module";
}
init()
{
sys = load Sys Sys->PATH;
fslib = load Fslib Fslib->PATH;
if(fslib == nil)
badmod(Fslib->PATH);
}
run(nil: ref Draw->Context, report: ref Report,
opts: list of Option, args: list of ref Value): ref Value
{
path: string;
f := (hd args).s().i;
fd: ref Sys->FD;
seekable: int;
if(f == "-"){
if(opts == nil){
sys->fprint(sys->fildes(2), "fs: readfile: must specify a path when reading stdin\n");
return nil;
}
fd = sys->fildes(0);
seekable = 0;
}else{
fd = sys->open(f, Sys->OREAD);
seekable = isseekable(fd);
}
if(fd == nil){
sys->fprint(sys->fildes(2), "fs: readfile: cannot open %s: %r\n", f);
return nil;
}
if(opts != nil)
path = (hd (hd opts).args).s().i;
else
path = f;
(root, file) := pathsplit(path);
if(file == nil || file == "." || file == ".."){
sys->fprint(sys->fildes(2), "fs: readfile: invalid filename %q\n", fname);
return nil;
}
d.name = file;
v := ref Value.X(chan of (Fsdata, chan of int));
spawn readproc(v.i, fd, root, ref d, seekable, report.start("read"));
return v;
}
readproc(c: Fschan, fd: ref Sys->FD, root: string, d: ref Sys->Dir, seekable: int, errorc: chan of string)
{
reply := chan of int;
rd := ref Sys->nulldir;
rd.name = root;
c <-= ((rd, nil), reply);
if(<-reply != Down)
quit(errorc);
c <-= ((d, nil), reply);
case <-reply {
Down =>
sendfile(c, fd, errorc);
Skip or
Quit =>
quit(errorc);
}
c <-= ((nil, nil), reply);
<-reply;
quit(errorc);
}
sendfile(c: Fschan, data: list of array of byte, length: big, errorc: chan of string)
{
reply := chan of int;
for(;;){
buf: array of byte;
if(fd != nil){
buf := array[Sys->ATOMICIO] of byte;
if((n := sys->read(fd, buf, len buf)) <= 0){
if(n < 0)
report(errorc, sys->sprint("read error: %r"));
c <-= ((nil, nil), reply);
if(<-reply == Quit)
quit(errorc);
return;
}
c <-= ((nil, buf), reply);
case <-reply {
Quit =>
quit(errorc);
Skip =>
return;
}
}
}
pathsplit(p: string): (string, string)
{
for (i := len p - 1; i >= 0; i--)
if (p[i] != '/')
break;
if (i < 0)
return (p, nil);
p = p[0:i+1];
for (i = len p - 1; i >=0; i--)
if (p[i] == '/')
break;
if (i < 0)
return (".", p);
return (p[0:i+1], p[i+1:]);
}
# dodgy heuristic... avoid, or using the stat-length of pipes and net connections
isseekable(fd: ref Sys->FD): int
{
(ok, stat) := sys->stat(iob.fd);
if(ok != -1 && stat.dtype == '|' || stat.dtype == 'I')
return 0;
return 1;
}
|