-
Notifications
You must be signed in to change notification settings - Fork 609
/
io.cpp
355 lines (277 loc) · 8.06 KB
/
io.cpp
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
#include "vm.hpp"
#include "vm/object_utils.hpp"
#include "builtin/array.hpp"
#include "builtin/fixnum.hpp"
#include "builtin/io.hpp"
#include "objectmemory.hpp"
#include "primitives.hpp"
#include "capi/capi.hpp"
#include "capi/18/include/ruby.h"
#include "windows_compat.h"
#include <fcntl.h>
using namespace rubinius;
using namespace rubinius::capi;
namespace rubinius {
namespace capi {
IO* capi_get_io(NativeMethodEnvironment* env, VALUE io_handle) {
Handle* handle = Handle::from(io_handle);
handle->flush(env);
env->check_tracked_handle(handle, false);
return c_as<IO>(handle->object());
}
static const char* flags_modestr(int oflags)
{
#ifdef O_BINARY
# define MODE_BINARY(a,b) ((oflags & O_BINARY) ? (b) : (a))
#else
# define MODE_BINARY(a,b) (a)
#endif
int accmode = oflags & (O_RDONLY|O_WRONLY|O_RDWR);
if (oflags & O_APPEND) {
if (accmode == O_WRONLY) {
return MODE_BINARY("a", "ab");
}
if (accmode == O_RDWR) {
return MODE_BINARY("a+", "ab+");
}
}
switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) {
case O_RDONLY:
return MODE_BINARY("r", "rb");
case O_WRONLY:
return MODE_BINARY("w", "wb");
case O_RDWR:
return MODE_BINARY("r+", "rb+");
}
return NULL;
}
RIO* Handle::as_rio(NativeMethodEnvironment* env) {
IO* io_obj = c_as<IO>(object());
if(type_ != cRIO) {
env->shared().capi_ds_lock().lock();
if(type_ != cRIO) {
int fd = (int)io_obj->descriptor()->to_native();
env->shared().capi_ds_lock().unlock();
if(fd == -1) {
rb_raise(rb_eIOError, "%s (%d)", strerror(errno), errno);
}
FILE* f = fdopen(fd, flags_modestr(io_obj->mode()->to_native()));
if(!f) {
std::cerr << "Error convert fd (" << fd << ") to lowlevel IO: "
<< strerror(errno) << " (" << errno << ")" << std::endl;
env->shared().capi_ds_lock().unlock();
rb_raise(rb_eTypeError,
"unable to convert fd (%d) to lowlevel IO: %s (%d)",
fd, strerror(errno), errno);
}
RIO* rf = new RIO;
rf->handle = as_value();
rf->fd = fd;
rf->f = f;
rf->f2 = NULL;
rf->stdio_file = NULL;
rf->finalize = NULL;
// Disable all buffering so that it doesn't get out of sync with
// the normal IO buffer.
setvbuf(rf->f, 0, _IONBF, 0);
type_ = cRIO;
as_.rio = rf;
}
env->shared().capi_ds_lock().unlock();
}
return as_.rio;
}
bool Handle::rio_close() {
if(type_ != cRIO) return true;
RIO* rio = as_.rio;
if(rio->finalize) rio->finalize(rio, true);
bool ok = (fclose(rio->f) == 0);
rio->f = NULL;
return ok;
}
}
}
extern "C" {
struct RIO* capi_rio_struct(VALUE io_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
return Handle::from(io_handle)->as_rio(env);
}
void rb_eof_error() {
rb_raise(rb_eEOFError, "end of file reached");
}
VALUE rb_io_addstr(VALUE io, VALUE str) {
rb_io_write(io, str);
return io;
}
VALUE rb_io_print(int argc, VALUE *argv, VALUE io) {
return rb_funcall2(io, rb_intern("print"), argc, argv);
}
VALUE rb_io_printf(int argc, VALUE *argv, VALUE io) {
return rb_funcall2(io, rb_intern("printf"), argc, argv);
}
VALUE rb_io_puts(int argc, VALUE *argv, VALUE io) {
return rb_funcall2(io, rb_intern("puts"), argc, argv);
}
VALUE rb_io_write(VALUE io, VALUE str) {
return rb_funcall(io, rb_intern("write"), 1, str);
}
VALUE rb_io_close(VALUE io) {
return rb_funcall(io, rb_intern("close"), 0);
}
int rb_io_fd(VALUE io_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO* io = c_as<IO>(env->get_object(io_handle));
return io->descriptor()->to_native();
}
long rb_io_fread(char* ptr, int len, FILE* f) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
long ret;
LEAVE_CAPI(env->state());
{
GCIndependent guard(env);
ret = fread(ptr, 1, len, f);
}
ENTER_CAPI(env->state());
return ret;
}
int rb_io_wait_readable(int fd) {
bool retry = false;
switch(errno) {
case EINTR:
#ifdef ERESTART
case ERESTART:
#endif
retry = true;
break;
case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
break;
default:
// this is the MRI api. If errno is not on of these, say to the caller
// "um, i guess nothing more to do?"
return Qfalse;
}
fd_set fds;
FD_ZERO(&fds);
FD_SET((int_fd_t)fd, &fds);
int count = 0;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
LEAVE_CAPI(env->state());
{
GCIndependent guard(env);
while(count == 0) {
count = select(fd+1, &fds, 0, 0, 0);
if(!retry) break;
}
}
ENTER_CAPI(env->state());
return Qtrue;
}
int rb_io_wait_writable(int fd) {
bool retry = false;
switch(errno) {
case EINTR:
#ifdef ERESTART
case ERESTART:
#endif
retry = true;
break;
case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
break;
default:
// this is the MRI api. If errno is not on of these, say to the caller
// "um, i guess nothing more to do?"
return Qfalse;
}
fd_set fds;
FD_ZERO(&fds);
FD_SET((int_fd_t)fd, &fds);
int count = 0;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
LEAVE_CAPI(env->state());
{
GCIndependent guard(env);
while(count == 0) {
count = select(fd+1, &fds, 0, 0, 0);
if(!retry) break;
}
}
ENTER_CAPI(env->state());
return Qtrue;
}
/*
* rb_thread_wait_fd actually waits until a read is
* available on the given fd
*/
void rb_thread_wait_fd(int fd) {
fd_set fds;
FD_ZERO(&fds);
FD_SET((int_fd_t)fd, &fds);
int count = 0;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
LEAVE_CAPI(env->state());
{
GCIndependent guard(env);
while(count == 0) {
count = select(fd+1, &fds, 0, 0, 0);
}
}
ENTER_CAPI(env->state());
}
/*
* rb_thread_fd_writable waits until the given fd
* is available for writing
*/
void rb_thread_fd_writable(int fd) {
fd_set fds;
FD_ZERO(&fds);
FD_SET((int_fd_t)fd, &fds);
int ready = 0;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
LEAVE_CAPI(env->state());
{
GCIndependent guard(env);
while(!ready) {
ready = select(fd+1, 0, &fds, 0, 0);
}
}
ENTER_CAPI(env->state());
}
void rb_io_set_nonblock(rb_io_t* iot) {
VALUE io_handle = iot->handle;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO* io = c_as<IO>(env->get_object(io_handle));
io->set_nonblock(env->state());
}
void rb_io_check_closed(rb_io_t* iot) {
VALUE io_handle = iot->handle;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO* io = c_as<IO>(env->get_object(io_handle));
if(io->descriptor()->to_native() == -1) {
rb_raise(rb_eIOError, "closed stream");
}
}
void rb_io_check_readable(rb_io_t* iot) {
VALUE io_handle = iot->handle;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO* io = c_as<IO>(env->get_object(io_handle));
int io_mode = io->mode()->to_native() & O_ACCMODE;
if (!(O_RDONLY == io_mode || O_RDWR == io_mode)) {
rb_raise(rb_eIOError, "not opened for reading");
}
}
void rb_io_check_writable(rb_io_t* iot) {
VALUE io_handle = iot->handle;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO* io = c_as<IO>(env->get_object(io_handle));
int io_mode = io->mode()->to_native() & O_ACCMODE;
if (!(O_WRONLY == io_mode || O_RDWR == io_mode)) {
rb_raise(rb_eIOError, "not opened for writing");
}
}
}