io-extra 1.4.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/{CHANGES → CHANGES.md} +26 -17
- data/Gemfile +2 -0
- data/LICENSE +177 -0
- data/{MANIFEST → MANIFEST.md} +4 -4
- data/README.md +127 -0
- data/Rakefile +107 -0
- data/certs/djberg96_pub.pem +26 -0
- data/doc/io_extra.txt +85 -0
- data/examples/example_io_extra.rb +53 -0
- data/examples/example_pread.rb +24 -0
- data/examples/example_writev.rb +171 -0
- data/examples/writev_benchmark.rb +19 -0
- data/ext/extconf.rb +2 -0
- data/ext/extra.bundle.dSYM/Contents/Info.plist +20 -0
- data/ext/extra.bundle.dSYM/Contents/Resources/Relocations/aarch64/extra.bundle.yml +5 -0
- data/ext/io/extra.c +292 -81
- data/io-extra.gemspec +51 -0
- data/lib/io-extra.rb +3 -1
- data/spec/io_extra_spec.rb +235 -0
- data.tar.gz.sig +0 -0
- metadata +76 -16
- metadata.gz.sig +0 -0
- data/README +0 -97
- data/test/test_io_extra.rb +0 -148
data/ext/io/extra.c
CHANGED
|
@@ -87,9 +87,18 @@ static int open_max(void){
|
|
|
87
87
|
*/
|
|
88
88
|
static VALUE io_closefrom(VALUE klass, VALUE v_low_fd){
|
|
89
89
|
int i, lowfd;
|
|
90
|
-
int maxfd
|
|
90
|
+
int maxfd;
|
|
91
|
+
|
|
91
92
|
lowfd = NUM2INT(v_low_fd);
|
|
92
93
|
|
|
94
|
+
if(lowfd < 0)
|
|
95
|
+
rb_raise(rb_eArgError, "lowfd must be non-negative");
|
|
96
|
+
|
|
97
|
+
maxfd = open_max();
|
|
98
|
+
|
|
99
|
+
if(maxfd < 0)
|
|
100
|
+
rb_raise(rb_eRuntimeError, "failed to determine maximum file descriptor");
|
|
101
|
+
|
|
93
102
|
for(i = lowfd; i < maxfd; i++) {
|
|
94
103
|
if(!RB_RESERVED_FD_P(i))
|
|
95
104
|
close(i);
|
|
@@ -99,54 +108,7 @@ static VALUE io_closefrom(VALUE klass, VALUE v_low_fd){
|
|
|
99
108
|
}
|
|
100
109
|
|
|
101
110
|
#ifndef HAVE_FDWALK
|
|
102
|
-
|
|
103
|
-
int rv = 0;
|
|
104
|
-
int fd;
|
|
105
|
-
|
|
106
|
-
#ifdef PROC_SELF_FD_DIR
|
|
107
|
-
DIR *dir = opendir(PROC_SELF_FD_DIR);
|
|
108
|
-
|
|
109
|
-
if(dir){ /* procfs may not be mounted... */
|
|
110
|
-
struct dirent *ent;
|
|
111
|
-
int saved_errno;
|
|
112
|
-
int dir_fd = dirfd(dir);
|
|
113
|
-
|
|
114
|
-
while((ent = readdir(dir))){
|
|
115
|
-
char *err = NULL;
|
|
116
|
-
|
|
117
|
-
if(ent->d_name[0] == '.')
|
|
118
|
-
continue;
|
|
119
|
-
|
|
120
|
-
errno = 0;
|
|
121
|
-
fd = (int)strtol(ent->d_name, &err, 10);
|
|
122
|
-
|
|
123
|
-
if (errno || ! err || *err || fd == dir_fd)
|
|
124
|
-
continue;
|
|
125
|
-
|
|
126
|
-
if ((rv = func(data, fd)))
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
saved_errno = errno;
|
|
130
|
-
closedir(dir);
|
|
131
|
-
errno = saved_errno;
|
|
132
|
-
} else
|
|
133
|
-
#endif /* PROC_SELF_FD_DIR */
|
|
134
|
-
{
|
|
135
|
-
int maxfd = open_max();
|
|
136
|
-
|
|
137
|
-
for(fd = 0; fd < maxfd; fd++){
|
|
138
|
-
/* use fcntl to detect whether fd is a valid file descriptor */
|
|
139
|
-
errno = 0;
|
|
140
|
-
if(fcntl(fd, F_GETFD) < 0)
|
|
141
|
-
continue;
|
|
142
|
-
|
|
143
|
-
errno = 0;
|
|
144
|
-
if ((rv = func(data, fd)))
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return rv;
|
|
149
|
-
}
|
|
111
|
+
/* Define HAVE_FDWALK so the io_fdwalk implementation below is compiled */
|
|
150
112
|
#define HAVE_FDWALK
|
|
151
113
|
#endif
|
|
152
114
|
|
|
@@ -169,6 +131,74 @@ static int close_func(void* lowfd, int fd){
|
|
|
169
131
|
return 0;
|
|
170
132
|
}
|
|
171
133
|
|
|
134
|
+
/* Structure to pass data to fdwalk_body and fdwalk_ensure */
|
|
135
|
+
struct fdwalk_data {
|
|
136
|
+
int lowfd;
|
|
137
|
+
#ifdef PROC_SELF_FD_DIR
|
|
138
|
+
DIR *dir;
|
|
139
|
+
#endif
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/* Cleanup function for rb_ensure */
|
|
143
|
+
static VALUE fdwalk_ensure(VALUE arg){
|
|
144
|
+
struct fdwalk_data *data = (struct fdwalk_data *)arg;
|
|
145
|
+
#ifdef PROC_SELF_FD_DIR
|
|
146
|
+
if(data->dir){
|
|
147
|
+
closedir(data->dir);
|
|
148
|
+
data->dir = NULL;
|
|
149
|
+
}
|
|
150
|
+
#endif
|
|
151
|
+
(void)data; /* Suppress unused warning when PROC_SELF_FD_DIR not defined */
|
|
152
|
+
return Qnil;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/* Main fdwalk iteration body */
|
|
156
|
+
static VALUE fdwalk_body(VALUE arg){
|
|
157
|
+
struct fdwalk_data *data = (struct fdwalk_data *)arg;
|
|
158
|
+
int fd;
|
|
159
|
+
|
|
160
|
+
#ifdef PROC_SELF_FD_DIR
|
|
161
|
+
if(data->dir){
|
|
162
|
+
struct dirent *ent;
|
|
163
|
+
int dir_fd = dirfd(data->dir);
|
|
164
|
+
|
|
165
|
+
while((ent = readdir(data->dir))){
|
|
166
|
+
char *err = NULL;
|
|
167
|
+
|
|
168
|
+
if(ent->d_name[0] == '.')
|
|
169
|
+
continue;
|
|
170
|
+
|
|
171
|
+
errno = 0;
|
|
172
|
+
fd = (int)strtol(ent->d_name, &err, 10);
|
|
173
|
+
|
|
174
|
+
if (errno || !err || *err || fd == dir_fd)
|
|
175
|
+
continue;
|
|
176
|
+
|
|
177
|
+
/* Validate fd is still open before calling callback (reduces race window) */
|
|
178
|
+
if(fcntl(fd, F_GETFD) < 0)
|
|
179
|
+
continue;
|
|
180
|
+
|
|
181
|
+
close_func(&data->lowfd, fd);
|
|
182
|
+
}
|
|
183
|
+
} else
|
|
184
|
+
#endif /* PROC_SELF_FD_DIR */
|
|
185
|
+
{
|
|
186
|
+
int maxfd = open_max();
|
|
187
|
+
|
|
188
|
+
for(fd = 0; fd < maxfd; fd++){
|
|
189
|
+
/* use fcntl to detect whether fd is a valid file descriptor */
|
|
190
|
+
errno = 0;
|
|
191
|
+
if(fcntl(fd, F_GETFD) < 0)
|
|
192
|
+
continue;
|
|
193
|
+
|
|
194
|
+
errno = 0;
|
|
195
|
+
close_func(&data->lowfd, fd);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return Qnil;
|
|
200
|
+
}
|
|
201
|
+
|
|
172
202
|
/*
|
|
173
203
|
* call-seq:
|
|
174
204
|
* IO.fdwalk(lowfd){ |fh| ... }
|
|
@@ -179,12 +209,19 @@ static int close_func(void* lowfd, int fd){
|
|
|
179
209
|
*/
|
|
180
210
|
static VALUE io_fdwalk(int argc, VALUE* argv, VALUE klass){
|
|
181
211
|
VALUE v_low_fd, v_block;
|
|
182
|
-
|
|
212
|
+
struct fdwalk_data data;
|
|
183
213
|
|
|
184
214
|
rb_scan_args(argc, argv, "1&", &v_low_fd, &v_block);
|
|
185
|
-
lowfd = NUM2INT(v_low_fd);
|
|
215
|
+
data.lowfd = NUM2INT(v_low_fd);
|
|
216
|
+
|
|
217
|
+
if(data.lowfd < 0)
|
|
218
|
+
rb_raise(rb_eArgError, "lowfd must be non-negative");
|
|
186
219
|
|
|
187
|
-
|
|
220
|
+
#ifdef PROC_SELF_FD_DIR
|
|
221
|
+
data.dir = opendir(PROC_SELF_FD_DIR);
|
|
222
|
+
#endif
|
|
223
|
+
|
|
224
|
+
rb_ensure(fdwalk_body, (VALUE)&data, fdwalk_ensure, (VALUE)&data);
|
|
188
225
|
|
|
189
226
|
return klass;
|
|
190
227
|
}
|
|
@@ -284,29 +321,51 @@ static VALUE io_set_directio(VALUE self, VALUE v_advice){
|
|
|
284
321
|
}
|
|
285
322
|
#endif
|
|
286
323
|
|
|
287
|
-
/*
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
324
|
+
/* Structure to track iovec allocation */
|
|
325
|
+
struct iovec_buffer {
|
|
326
|
+
struct iovec *iov;
|
|
327
|
+
int iovcnt;
|
|
328
|
+
ssize_t expected;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
/* Convert Ruby array to iovec using heap allocation */
|
|
332
|
+
static void ary2iovec(struct iovec_buffer *buf, VALUE ary) {
|
|
333
|
+
VALUE *cur;
|
|
334
|
+
struct iovec *tmp;
|
|
335
|
+
long n;
|
|
336
|
+
|
|
337
|
+
if (TYPE(ary) != T_ARRAY)
|
|
338
|
+
rb_raise(rb_eArgError, "must be an array of strings");
|
|
339
|
+
|
|
340
|
+
cur = RARRAY_PTR(ary);
|
|
341
|
+
n = RARRAY_LEN(ary);
|
|
342
|
+
|
|
343
|
+
if (n > IOV_MAX)
|
|
344
|
+
rb_raise(rb_eArgError, "array is larger than IOV_MAX");
|
|
345
|
+
|
|
346
|
+
buf->iov = tmp = ALLOC_N(struct iovec, n);
|
|
347
|
+
buf->expected = 0;
|
|
348
|
+
buf->iovcnt = (int)n;
|
|
349
|
+
|
|
350
|
+
for (; --n >= 0; tmp++, cur++) {
|
|
351
|
+
if (TYPE(*cur) != T_STRING) {
|
|
352
|
+
xfree(buf->iov);
|
|
353
|
+
buf->iov = NULL;
|
|
354
|
+
rb_raise(rb_eArgError, "must be an array of strings");
|
|
355
|
+
}
|
|
356
|
+
tmp->iov_base = RSTRING_PTR(*cur);
|
|
357
|
+
tmp->iov_len = RSTRING_LEN(*cur);
|
|
358
|
+
buf->expected += tmp->iov_len;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/* Free iovec buffer */
|
|
363
|
+
static void free_iovec_buffer(struct iovec_buffer *buf) {
|
|
364
|
+
if (buf->iov) {
|
|
365
|
+
xfree(buf->iov);
|
|
366
|
+
buf->iov = NULL;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
310
369
|
|
|
311
370
|
#if defined(HAVE_WRITEV)
|
|
312
371
|
struct writev_args {
|
|
@@ -336,13 +395,17 @@ static VALUE s_io_writev(VALUE klass, VALUE fd, VALUE ary) {
|
|
|
336
395
|
ssize_t result = 0;
|
|
337
396
|
ssize_t left;
|
|
338
397
|
struct writev_args args;
|
|
398
|
+
struct iovec_buffer iov_buf;
|
|
339
399
|
|
|
340
400
|
// Allow a fileno or filehandle
|
|
341
401
|
if(rb_respond_to(fd, rb_intern("fileno")))
|
|
342
402
|
fd = rb_funcall(fd, rb_intern("fileno"), 0, 0);
|
|
343
403
|
|
|
344
404
|
args.fd = NUM2INT(fd);
|
|
345
|
-
|
|
405
|
+
ary2iovec(&iov_buf, ary);
|
|
406
|
+
args.iov = iov_buf.iov;
|
|
407
|
+
args.iovcnt = iov_buf.iovcnt;
|
|
408
|
+
left = iov_buf.expected;
|
|
346
409
|
|
|
347
410
|
for(;;) {
|
|
348
411
|
ssize_t w = (ssize_t)rb_thread_call_without_gvl(
|
|
@@ -359,6 +422,7 @@ static VALUE s_io_writev(VALUE klass, VALUE fd, VALUE ary) {
|
|
|
359
422
|
* we'll let the next write (or close) fail instead */
|
|
360
423
|
break;
|
|
361
424
|
}
|
|
425
|
+
free_iovec_buffer(&iov_buf);
|
|
362
426
|
rb_sys_fail("writev");
|
|
363
427
|
}
|
|
364
428
|
}
|
|
@@ -376,13 +440,25 @@ static VALUE s_io_writev(VALUE klass, VALUE fd, VALUE ary) {
|
|
|
376
440
|
left -= w;
|
|
377
441
|
|
|
378
442
|
// Skip over iovecs we've already written completely
|
|
379
|
-
for(i = 0; i < args.iovcnt; i
|
|
443
|
+
for(i = 0; i < args.iovcnt; i++){
|
|
380
444
|
if (w == 0)
|
|
381
445
|
break;
|
|
382
446
|
|
|
447
|
+
// Bounds check before pointer arithmetic
|
|
448
|
+
if(new_iov == NULL){
|
|
449
|
+
free_iovec_buffer(&iov_buf);
|
|
450
|
+
rb_raise(rb_eRuntimeError, "writev: iovec bounds check failed");
|
|
451
|
+
}
|
|
452
|
+
|
|
383
453
|
// Partially written iov, modify and retry with current iovec in front
|
|
384
454
|
if(new_iov->iov_len > (size_t)w){
|
|
385
|
-
|
|
455
|
+
char* base = (char*)new_iov->iov_base;
|
|
456
|
+
|
|
457
|
+
// Validate base pointer before arithmetic
|
|
458
|
+
if(base == NULL){
|
|
459
|
+
free_iovec_buffer(&iov_buf);
|
|
460
|
+
rb_raise(rb_eRuntimeError, "writev: null iov_base");
|
|
461
|
+
}
|
|
386
462
|
|
|
387
463
|
new_iov->iov_len -= w;
|
|
388
464
|
new_iov->iov_base = (void *)(base + w);
|
|
@@ -390,6 +466,13 @@ static VALUE s_io_writev(VALUE klass, VALUE fd, VALUE ary) {
|
|
|
390
466
|
}
|
|
391
467
|
|
|
392
468
|
w -= new_iov->iov_len;
|
|
469
|
+
new_iov++;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// Validate we haven't exceeded bounds before modifying args
|
|
473
|
+
if(i > args.iovcnt){
|
|
474
|
+
free_iovec_buffer(&iov_buf);
|
|
475
|
+
rb_raise(rb_eRuntimeError, "writev: exceeded iovec array bounds");
|
|
393
476
|
}
|
|
394
477
|
|
|
395
478
|
// Retry without the already-written iovecs
|
|
@@ -398,10 +481,127 @@ static VALUE s_io_writev(VALUE klass, VALUE fd, VALUE ary) {
|
|
|
398
481
|
}
|
|
399
482
|
}
|
|
400
483
|
|
|
484
|
+
free_iovec_buffer(&iov_buf);
|
|
401
485
|
return LONG2NUM(result);
|
|
402
486
|
}
|
|
403
487
|
#endif
|
|
404
488
|
|
|
489
|
+
#ifdef HAVE_READV
|
|
490
|
+
struct readv_args {
|
|
491
|
+
int fd, count, positional;
|
|
492
|
+
off_t offset;
|
|
493
|
+
struct iovec *iov;
|
|
494
|
+
VALUE lengths;
|
|
495
|
+
ssize_t result;
|
|
496
|
+
int error;
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
static void *nogvl_readv(void *ptr) {
|
|
500
|
+
struct readv_args *args = ptr;
|
|
501
|
+
#ifdef HAVE_PREADV
|
|
502
|
+
if (args->positional)
|
|
503
|
+
args->result = preadv(args->fd, args->iov, args->count, args->offset);
|
|
504
|
+
else
|
|
505
|
+
#endif
|
|
506
|
+
args->result = readv(args->fd, args->iov, args->count);
|
|
507
|
+
args->error = errno;
|
|
508
|
+
return NULL;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
static VALUE readv_cleanup(VALUE ptr) {
|
|
512
|
+
struct readv_args *args = (struct readv_args *)ptr;
|
|
513
|
+
int i;
|
|
514
|
+
if (args->iov) {
|
|
515
|
+
for (i = 0; i < args->count; i++) xfree(args->iov[i].iov_base);
|
|
516
|
+
xfree(args->iov);
|
|
517
|
+
}
|
|
518
|
+
return Qnil;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
static VALUE readv_body(VALUE ptr) {
|
|
522
|
+
struct readv_args *args = (struct readv_args *)ptr;
|
|
523
|
+
VALUE result;
|
|
524
|
+
size_t total = 0;
|
|
525
|
+
ssize_t remaining;
|
|
526
|
+
int i;
|
|
527
|
+
|
|
528
|
+
args->iov = ALLOC_N(struct iovec, args->count);
|
|
529
|
+
memset(args->iov, 0, sizeof(struct iovec) * args->count);
|
|
530
|
+
for (i = 0; i < args->count; i++) {
|
|
531
|
+
VALUE length = rb_ary_entry(args->lengths, i);
|
|
532
|
+
long n = NUM2LONG(length);
|
|
533
|
+
if (n < 0 || (size_t)n > (size_t)SSIZE_MAX - total)
|
|
534
|
+
rb_raise(rb_eArgError, "invalid buffer length or total exceeds SSIZE_MAX");
|
|
535
|
+
total += (size_t)n;
|
|
536
|
+
args->iov[i].iov_len = (size_t)n;
|
|
537
|
+
args->iov[i].iov_base = n ? xmalloc((size_t)n) : NULL;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
do {
|
|
541
|
+
rb_thread_call_without_gvl(nogvl_readv, args, RUBY_UBF_IO, NULL);
|
|
542
|
+
if (args->result >= 0) break;
|
|
543
|
+
errno = args->error;
|
|
544
|
+
if (errno == EINTR) {
|
|
545
|
+
rb_thread_check_ints();
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
if ((errno == EAGAIN || errno == EWOULDBLOCK) && rb_io_wait_readable(args->fd))
|
|
549
|
+
continue;
|
|
550
|
+
errno = args->error;
|
|
551
|
+
rb_sys_fail(args->positional ? "preadv" : "readv");
|
|
552
|
+
} while (1);
|
|
553
|
+
|
|
554
|
+
remaining = args->result;
|
|
555
|
+
result = rb_ary_new_capa(args->count);
|
|
556
|
+
for (i = 0; i < args->count; i++) {
|
|
557
|
+
size_t n = args->iov[i].iov_len;
|
|
558
|
+
if (n > (size_t)remaining) n = (size_t)remaining;
|
|
559
|
+
rb_ary_push(result, rb_str_new(args->iov[i].iov_base, (long)n));
|
|
560
|
+
remaining -= (ssize_t)n;
|
|
561
|
+
}
|
|
562
|
+
return result;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/*
|
|
566
|
+
* IO.readv(io, lengths, offset = nil) -> array of binary strings
|
|
567
|
+
*
|
|
568
|
+
* Reads up to each length in order using readv. With an offset, uses preadv
|
|
569
|
+
* and leaves the file position unchanged. Short reads produce shorter strings
|
|
570
|
+
* and empty strings for any remaining entries; EOF yields all empty strings.
|
|
571
|
+
* This operates on the underlying descriptor, bypassing Ruby IO buffering.
|
|
572
|
+
*
|
|
573
|
+
* File.binwrite('example.txt', 'Hello world')
|
|
574
|
+
* File.open('example.txt', 'rb') do |file|
|
|
575
|
+
* IO.readv(file, [5, 1, 5]) # => ["Hello", " ", "world"]
|
|
576
|
+
* end
|
|
577
|
+
*/
|
|
578
|
+
static VALUE s_io_readv(int argc, VALUE *argv, VALUE klass) {
|
|
579
|
+
VALUE fd, lengths, offset;
|
|
580
|
+
struct readv_args args = {0};
|
|
581
|
+
long count;
|
|
582
|
+
rb_scan_args(argc, argv, "21", &fd, &lengths, &offset);
|
|
583
|
+
Check_Type(lengths, T_ARRAY);
|
|
584
|
+
count = RARRAY_LEN(lengths);
|
|
585
|
+
if (count > IOV_MAX || count > INT_MAX)
|
|
586
|
+
rb_raise(rb_eArgError, "array is larger than IOV_MAX");
|
|
587
|
+
if (rb_respond_to(fd, rb_intern("fileno")))
|
|
588
|
+
fd = rb_funcall(fd, rb_intern("fileno"), 0);
|
|
589
|
+
args.fd = NUM2INT(fd);
|
|
590
|
+
args.count = (int)count;
|
|
591
|
+
args.lengths = lengths;
|
|
592
|
+
if (!NIL_P(offset)) {
|
|
593
|
+
#ifdef HAVE_PREADV
|
|
594
|
+
args.offset = NUM2OFFT(offset);
|
|
595
|
+
if (args.offset < 0) rb_raise(rb_eArgError, "offset must be non-negative");
|
|
596
|
+
args.positional = 1;
|
|
597
|
+
#else
|
|
598
|
+
rb_raise(rb_eNotImpError, "preadv is not available on this platform");
|
|
599
|
+
#endif
|
|
600
|
+
}
|
|
601
|
+
return rb_ensure(readv_body, (VALUE)&args, readv_cleanup, (VALUE)&args);
|
|
602
|
+
}
|
|
603
|
+
#endif
|
|
604
|
+
|
|
405
605
|
#ifdef HAVE_TTYNAME
|
|
406
606
|
/*
|
|
407
607
|
* io.ttyname
|
|
@@ -418,8 +618,15 @@ static VALUE io_get_ttyname(VALUE self){
|
|
|
418
618
|
|
|
419
619
|
int fd = NUM2INT(rb_funcall(self, rb_intern("fileno"), 0, 0));
|
|
420
620
|
|
|
421
|
-
if(
|
|
422
|
-
|
|
621
|
+
if(fd < 0)
|
|
622
|
+
rb_raise(rb_eArgError, "invalid file descriptor");
|
|
623
|
+
|
|
624
|
+
errno = 0;
|
|
625
|
+
if(isatty(fd)){
|
|
626
|
+
char *name = ttyname(fd);
|
|
627
|
+
if(name != NULL)
|
|
628
|
+
v_return = rb_str_new2(name);
|
|
629
|
+
}
|
|
423
630
|
|
|
424
631
|
return v_return;
|
|
425
632
|
}
|
|
@@ -428,7 +635,7 @@ static VALUE io_get_ttyname(VALUE self){
|
|
|
428
635
|
/* Adds the IO.closefrom, IO.fdwalk class methods, as well as the IO#directio
|
|
429
636
|
* and IO#directio? instance methods (if supported on your platform).
|
|
430
637
|
*/
|
|
431
|
-
void Init_extra(){
|
|
638
|
+
void Init_extra(void){
|
|
432
639
|
rb_define_singleton_method(rb_cIO, "closefrom", io_closefrom, 1);
|
|
433
640
|
|
|
434
641
|
#ifdef HAVE_FDWALK
|
|
@@ -452,6 +659,10 @@ void Init_extra(){
|
|
|
452
659
|
rb_define_singleton_method(rb_cIO, "writev", s_io_writev, 2);
|
|
453
660
|
#endif
|
|
454
661
|
|
|
662
|
+
#ifdef HAVE_READV
|
|
663
|
+
rb_define_singleton_method(rb_cIO, "readv", s_io_readv, -1);
|
|
664
|
+
#endif
|
|
665
|
+
|
|
455
666
|
#ifdef HAVE_TTYNAME
|
|
456
667
|
rb_define_method(rb_cIO, "ttyname", io_get_ttyname, 0);
|
|
457
668
|
#endif
|
data/io-extra.gemspec
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rbconfig'
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |spec|
|
|
5
|
+
if File::ALT_SEPARATOR
|
|
6
|
+
STDERR.puts 'Not supported on this platform. Exiting.'
|
|
7
|
+
exit(-1)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
spec.name = 'io-extra'
|
|
11
|
+
spec.version = '1.6.0'
|
|
12
|
+
spec.author = 'Daniel J. Berger'
|
|
13
|
+
spec.license = 'Apache-2.0'
|
|
14
|
+
spec.email = 'djberg96@gmail.com'
|
|
15
|
+
spec.homepage = 'https://github.com/djberg96/io-extra'
|
|
16
|
+
spec.summary = 'Adds extra methods to the IO class'
|
|
17
|
+
spec.test_files = Dir['spec/*_spec.rb']
|
|
18
|
+
spec.extensions = ['ext/extconf.rb']
|
|
19
|
+
spec.cert_chain = ['certs/djberg96_pub.pem']
|
|
20
|
+
spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
|
|
21
|
+
|
|
22
|
+
spec.extra_rdoc_files = [
|
|
23
|
+
'CHANGES.md',
|
|
24
|
+
'README.md',
|
|
25
|
+
'MANIFEST.md',
|
|
26
|
+
'ext/io/extra.c'
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
spec.add_development_dependency('rake')
|
|
30
|
+
spec.add_development_dependency('rspec', '~> 3.9')
|
|
31
|
+
spec.add_development_dependency('rubocop')
|
|
32
|
+
spec.add_development_dependency('rubocop-rspec')
|
|
33
|
+
|
|
34
|
+
spec.metadata = {
|
|
35
|
+
'homepage_uri' => 'https://github.com/djberg96/io-extra',
|
|
36
|
+
'bug_tracker_uri' => 'https://github.com/djberg96/io-extra/issues',
|
|
37
|
+
'changelog_uri' => 'https://github.com/djberg96/io-extra/blob/main/CHANGES.md',
|
|
38
|
+
'documentation_uri' => 'https://github.com/djberg96/io-extra/wiki',
|
|
39
|
+
'source_code_uri' => 'https://github.com/djberg96/io-extra',
|
|
40
|
+
'wiki_uri' => 'https://github.com/djberg96/io-extra/wiki',
|
|
41
|
+
'rubygems_mfa_required' => 'true',
|
|
42
|
+
'github_repo' => 'https://github.com/djberg96/io-extra',
|
|
43
|
+
'funding_uri' => 'https://github.com/sponsors/djberg96',
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
spec.description = <<-EOF
|
|
47
|
+
Adds the IO.closefrom, IO.fdwalk, IO.pread, IO.pwrite, IO.readv, and IO.writev
|
|
48
|
+
singleton methods as well as the IO#directio, IO#directio? and IO#ttyname
|
|
49
|
+
instance methods (for those platforms that support them).
|
|
50
|
+
EOF
|
|
51
|
+
end
|
data/lib/io-extra.rb
CHANGED