79
|
1
|
80
|
2 //This implementation will change quite a bit once the transaction system is working
|
|
3 //and I've finalized how File I/O will fit into that. This is just a very basic
|
|
4 //implementation to allow the compiler to be self-hosting.
|
|
5
|
|
6 Foreign C:libc
|
|
7 {
|
|
8 open[name(Array,Raw Pointer),flags(Int32,Naked):filedes(Int32,Naked)]
|
|
9 close[filedes(Int32,Naked):status(Int32,Naked)]
|
|
10 fsync[filedes(Int32,Naked):status(Int32,Naked)]
|
|
11 fstat[filedes(Int32,Naked),buf(Stat,Raw Pointer):err(Int32,Naked),buf]
|
|
12 }
|
79
|
13
|
|
14 Blueprint Stat
|
|
15 {
|
80
|
16 Device(UInt64,Naked)
|
|
17 Inode(UInt64,Naked)
|
|
18 Link Count(UInt64,Naked)
|
|
19 Mode(UInt32,Naked)
|
|
20 Owner ID(UInt32,Naked)
|
|
21 Group ID(UInt32,Naked)
|
|
22 Pad0(Int32,Naked)
|
|
23 RDevice(UInt64,Naked)
|
|
24 Size(Int64,Naked)
|
|
25 Block Size(Int64,Naked)
|
|
26 Block Count(Int64,Naked)
|
|
27 Access Time(Int64,Naked)
|
|
28 Access Time Nano(UInt64,Naked)
|
|
29 Mod Time(Int64,Naked)
|
|
30 Mod Time Nano(UInt64,Naked)
|
|
31 Status Time(Int64,Naked)
|
|
32 Status Time Nano(UInt64,Naked)
|
|
33 Unused1(Int64,Naked)
|
|
34 Unused2(Int64,Naked)
|
|
35 Unused3(Int64,Naked)
|
|
36 }
|
|
37
|
|
38 Stat[fd:out]
|
|
39 {
|
|
40 err,out <- fstat[fd, Build[Stat()]]
|
79
|
41 }
|
|
42
|
|
43 Blueprint File
|
|
44 {
|
|
45 Name
|
|
46 }
|
|
47
|
|
48 Blueprint File Read
|
|
49 {
|
|
50 Name
|
|
51 Descriptor(Int32,Naked)
|
|
52 Info
|
|
53 }
|
|
54
|
|
55 Blueprint File Write
|
|
56 {
|
|
57 Name
|
|
58 Descriptor(Int32,Naked)
|
|
59 Info
|
|
60 }
|
|
61
|
|
62 Blueprint File ReadWrite
|
|
63 {
|
|
64 Name
|
|
65 Read Offset(Int64,Naked)
|
|
66 Descriptor(Int32,Naked)
|
|
67 Info
|
|
68 }
|
|
69
|
|
70 File[name:out]
|
|
71 {
|
|
72 out <- [Build[File()]]Name <<[name]
|
|
73 }
|
|
74
|
|
75
|
|
76 Open@File[file,mode:out,error]
|
|
77 {
|
|
78 If[[mode]=["r"]]
|
|
79 {
|
80
|
80 imode <- 0i32
|
79
|
81 type <- File Read()
|
|
82 out <- Val[ofile]
|
|
83 }{
|
|
84 If[[mode]=["w"]]
|
|
85 {
|
80
|
86 imode <- 65i32
|
79
|
87 type <- File Write()
|
|
88 out <- Val[ofile]
|
|
89 }{
|
|
90 ,error <- If[[mode]=["rw"]]
|
|
91 {
|
80
|
92 imode <- 66i32
|
79
|
93 type <- File ReadWrite()
|
|
94 out <- [ofile]Read Offset <<[0i64]
|
|
95 }
|
|
96 }
|
|
97 }
|
|
98 fd <- open[[file]Name >>]
|
|
99 error <- If[[fd]=[-1i32]] {}
|
|
100 {
|
80
|
101 ofile <- [[[Build[type]
|
79
|
102 ]Name <<[ [file]Name >> ]
|
|
103 ]Descriptor <<[fd]
|
|
104 ]Info <<[Stat[fd]]
|
|
105 }
|
|
106 }
|
|
107
|
|
108 Read@File[file,bytes:data,outfile,error]
|
|
109 {
|
|
110 ,error <- [file]Open["r"]
|
|
111 {
|
|
112 data,outfile,error <- [~]Read[bytes]
|
|
113 }
|
|
114 }
|
80
|
115 /*
|
79
|
116 Append@File[file,data:out,error]
|
|
117 {
|
|
118 ,error <- [file]Open["w"]
|
|
119 {
|
|
120 out,error <- [~]Append[data]
|
|
121 }
|
80
|
122 }*/
|
79
|
123
|
|
124 String@File[file:out,error]
|
|
125 {
|
|
126 ,error <- [file]Open["w"]
|
|
127 {
|
|
128 out,error <- String[~]
|
|
129 }
|
|
130 }
|
|
131
|
|
132 Read@File Read[file,inbytes(Int32):data,outfile,error]
|
|
133 {
|
80
|
134 readbytes, mdata <- read[[file]Descriptor >>, [Array[]]Set[inbytes, 0u8], Int64[inbytes]]
|
|
135 error <- If[[readbytes]<[0i64]] {}
|
|
136 {
|
|
137 outfile <- file
|
|
138 data <- [mdata]Length <<[ Trunc Int32[readbytes] ]
|
|
139 }
|
|
140 /*
|
|
141 //Skip buffering for now. Buffering approach may need to change later anyway
|
79
|
142 buflen <- [[file]Buffer >>]Length
|
|
143 If[buflen]
|
|
144 {
|
|
145 If[[inbytes]=[buflen]]
|
|
146 {
|
|
147 data <- [file]Buffer >>
|
|
148 outfile <- [file]Buffer <<[Array[]]
|
|
149 }{
|
|
150 If[[inbytes]<[buflen]]
|
|
151 {
|
|
152
|
|
153 }{
|
|
154
|
|
155 }
|
|
156 }
|
|
157 }{
|
|
158 readbytes, mdata <- read[[file]Descriptor >>, [Array[]]Set[inbytes, 0u8], Int64[inbytes]]
|
|
159 error <- If[[readbytes]<[0i64]] {}
|
|
160 {
|
|
161 outfile <- file
|
|
162 data <- [mdata]Length <<[ Trunc Int32[readbytes] ]
|
|
163 }
|
80
|
164 } */
|
|
165 }
|
|
166
|
|
167 Length@File Read[file:out]
|
|
168 {
|
|
169 out <- Trunc Int32[[[file]Info >>]Size >>]
|
79
|
170 }
|
|
171
|
80
|
172 Close@File Read[file:out]
|
|
173 {
|
|
174 close[[file]Descriptor >>]
|
|
175 out <- File[[file]Name >>]
|
|
176 }
|
|
177
|
|
178
|