-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
4643 lines (4141 loc) · 119 KB
/
io.c
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MacRuby implementation of Ruby 1.9's io.c.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2007-2009, Apple Inc. All rights reserved.
* Copyright (C) 1993-2007 Yukihiro Matsumoto
* Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
* Copyright (C) 2000 Information-technology Promotion Agency, Japan
*/
#include "ruby/ruby.h"
#include "ruby/io.h"
#include "ruby/util.h"
#include "ruby/node.h"
#include "vm.h"
#include "objc.h"
#include "id.h"
#include <errno.h>
#include <paths.h>
#include <fcntl.h>
#include <unistd.h>
#include <copyfile.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/syscall.h>
#include <spawn.h>
#include <crt_externs.h>
extern void Init_File(void);
VALUE rb_cIO;
VALUE rb_eEOFError;
VALUE rb_eIOError;
VALUE rb_stdin = 0, rb_stdout = 0, rb_stderr = 0;
VALUE rb_deferr; /* rescue VIM plugin */
static VALUE orig_stdout, orig_stderr;
// TODO: After Object#untrusted? and Object#trusted? get implemented,
// place the appropriate checks on #inspect, #reopen, #close, #close_read,
// and #close_write.
VALUE rb_output_fs;
VALUE rb_rs;
VALUE rb_output_rs;
VALUE rb_default_rs;
static VALUE argf;
static ID id_write, id_read, id_getc, id_flush, id_encode, id_readpartial;
struct argf {
VALUE filename, current_file;
int gets_lineno;
int init_p, next_p;
VALUE lineno;
VALUE argv;
char *inplace;
int binmode;
rb_encoding *enc, *enc2;
};
#define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
#define ARGF argf_of(argf)
static VALUE
pop_last_hash(int *argc_p, VALUE *argv)
{
VALUE last, tmp;
if (*argc_p == 0) {
return Qnil;
}
last = argv[*argc_p-1];
tmp = rb_check_convert_type(last, T_HASH, "Hash", "to_hash");
if (NIL_P(tmp)) {
return Qnil;
}
(*argc_p)--;
return tmp;
}
static inline VALUE
rb_io_get_io(VALUE io)
{
return rb_convert_type(io, T_FILE, "IO", "to_io");
}
static inline VALUE
rb_io_check_io(VALUE io)
{
return rb_check_convert_type(io, T_FILE, "IO", "to_io");
}
static int
convert_mode_string_to_fmode(VALUE rstr)
{
int fmode = 0;
const char *m = RSTRING_PTR(rstr);
switch (*m++) {
case 'r':
fmode |= FMODE_READABLE;
break;
case 'w':
fmode |= FMODE_WRITABLE | FMODE_TRUNC | FMODE_CREATE;
break;
case 'a':
fmode |= FMODE_WRITABLE | FMODE_APPEND | FMODE_CREATE;
break;
default:
error:
rb_raise(rb_eArgError, "invalid access mode %s", m);
}
while (*m) {
switch (*m++) {
case 'b':
fmode |= FMODE_BINMODE;
break;
case 't':
fmode |= FMODE_TEXTMODE;
break;
case '+':
fmode |= FMODE_READWRITE;
break;
case ':':
goto finished;
default:
rb_raise(rb_eArgError, "invalid access mode %s", m);
}
}
finished:
if ((fmode & FMODE_BINMODE) && (fmode & FMODE_TEXTMODE))
goto error;
return fmode;
}
static int
convert_fmode_to_oflags(int fmode)
{
int oflags = 0;
switch (fmode & FMODE_READWRITE) {
case FMODE_READABLE:
oflags |= O_RDONLY;
break;
case FMODE_WRITABLE:
oflags |= O_WRONLY;
break;
case FMODE_READWRITE:
oflags |= O_RDWR;
break;
}
if (fmode & FMODE_APPEND) {
oflags |= O_APPEND;
}
if (fmode & FMODE_TRUNC) {
oflags |= O_TRUNC;
}
if (fmode & FMODE_CREATE) {
oflags |= O_CREAT;
}
return oflags;
}
static int
convert_mode_string_to_oflags(VALUE s)
{
if (TYPE(s) == T_FIXNUM) {
return NUM2INT(s);
}
StringValue(s);
return convert_fmode_to_oflags(convert_mode_string_to_fmode(s));
}
static inline int
convert_oflags_to_fmode(int mode)
{
int flags = 0;
switch (mode & (O_RDONLY|O_WRONLY|O_RDWR)) {
case O_RDONLY:
flags = FMODE_READABLE;
break;
case O_WRONLY:
flags = FMODE_WRITABLE;
break;
case O_RDWR:
flags = FMODE_READWRITE;
break;
}
if (mode & O_APPEND) {
flags |= FMODE_APPEND;
}
if (mode & O_CREAT) {
flags |= FMODE_CREATE;
}
return flags;
}
void
rb_eof_error(void)
{
rb_raise(rb_eEOFError, "end of file reached");
}
VALUE
rb_io_taint_check(VALUE io)
{
if (!OBJ_TAINTED(io) && rb_safe_level() >= 4) {
rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
}
rb_check_frozen(io);
return io;
}
static inline bool
rb_io_is_open(rb_io_t *io_struct)
{
return io_struct->fd != -1;
}
static inline void
rb_io_assert_open(rb_io_t *io_struct)
{
if (!rb_io_is_open(io_struct)) {
rb_raise(rb_eIOError,
"cannot perform that operation on a closed stream");
}
}
static inline bool
rb_io_is_closed_for_reading(rb_io_t *io_struct)
{
return io_struct->read_fd == -1;
}
static inline bool
rb_io_is_closed_for_writing(rb_io_t *io_struct)
{
return io_struct->write_fd == -1;
}
static inline bool
rb_io_is_readable(rb_io_t *io_struct)
{
return !rb_io_is_closed_for_reading(io_struct);
}
static inline bool
rb_io_is_writable(rb_io_t *io_struct)
{
return !rb_io_is_closed_for_writing(io_struct);
}
static int
rb_io_calculate_mode_flags(rb_io_t *io_struct)
{
int flags = 0;
if (rb_io_is_readable(io_struct)) {
flags |= FMODE_READABLE;
}
if (rb_io_is_writable(io_struct)) {
flags |= FMODE_WRITABLE;
}
return flags;
}
static VALUE
io_alloc(VALUE klass, SEL sel)
{
NEWOBJ(io, struct RFile);
OBJSETUP(io, klass, T_FILE);
GC_WB(&io->fptr, ALLOC(rb_io_t));
io->fptr->fd = -1;
io->fptr->read_fd = -1;
io->fptr->write_fd = -1;
io->fptr->pid = -1;
return (VALUE)io;
}
static inline void
prepare_io_from_fd(rb_io_t *io_struct, int fd, int mode)
{
// While getting rid of the FMODE_* constants and replacing them with the
// POSIX constants would be very nice, it would be a mistake on Darwin,
// as O_RDONLY|O_WRONLY != O_RDWR, whereas FMODE_READABLE|FMODE_WRITABLE =
// FMODE_READWRITE. As such, we have to redefine a whole lot of system-wide
// constants, which sucks. But we don't have any other option.
bool read = false, write = false;
switch (mode & FMODE_READWRITE) {
case FMODE_READABLE:
read = true;
break;
case FMODE_WRITABLE:
write = true;
break;
case FMODE_READWRITE:
read = write = true;
break;
}
assert(read || write);
if (read) {
io_struct->read_fd = fd;
}
if (write) {
io_struct->write_fd = fd;
}
io_struct->fd = fd;
io_struct->pid = -1;
io_struct->mode = mode;
}
static void
io_close(rb_io_t *io_struct, bool close_read, bool close_write)
{
// TODO we must check the return value of close(2) and appropriately call
// rb_sys_fail().
if (close_read) {
if (io_struct->read_fd != io_struct->write_fd) {
close(io_struct->read_fd);
}
else {
io_struct->fd = -1;
}
io_struct->read_fd = -1;
}
if (close_write) {
if (io_struct->write_fd != io_struct->read_fd) {
close(io_struct->write_fd);
}
else {
io_struct->fd = -1;
}
io_struct->write_fd = -1;
}
if (io_struct->pid != -1) {
// Don't commit seppuku!
rb_last_status_set(0, io_struct->pid);
if (io_struct->pid != 0 && io_struct->pid != getpid()) {
kill(io_struct->pid, SIGTERM);
}
io_struct->pid = -1;
}
if (io_struct->fd != -1 && io_struct->read_fd == -1
&& io_struct->write_fd == -1) {
close(io_struct->fd);
io_struct->fd = -1;
}
}
static VALUE
prep_io(int fd, int mode, VALUE klass)
{
VALUE io = io_alloc(klass, 0);
rb_io_t *io_struct = ExtractIOStruct(io);
prepare_io_from_fd(io_struct, fd, mode);
return io;
}
/*
* call-seq:
* ios.syswrite(string) => integer
*
* Writes the given string to <em>ios</em> using a low-level write.
* Returns the number of bytes written. Do not mix with other methods
* that write to <em>ios</em> or you may get unpredictable results.
* Raises <code>SystemCallError</code> on error.
*
* f = File.new("out", "w")
* f.syswrite("ABCDEF") #=> 6
*/
/*
* call-seq:
* ios.write(string) => integer
*
* Writes the given string to <em>ios</em>. The stream must be opened
* for writing. If the argument is not a string, it will be converted
* to a string using <code>to_s</code>. Returns the number of bytes
* written.
*
* count = $stdout.write( "This is a test\n" )
* puts "That was #{count} bytes of data"
*
* <em>produces:</em>
*
* This is a test
* That was 15 bytes of data
*/
static VALUE
io_write(VALUE io, SEL sel, VALUE to_write)
{
rb_secure(4);
VALUE tmp = rb_io_check_io(io);
if (NIL_P(tmp)) {
// receiver is not IO, dispatch the write method on it
return rb_vm_call(io, selWrite, 1, &to_write, false);
}
io = tmp;
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_writable(io_struct);
to_write = rb_obj_as_string(to_write);
UInt8 *buffer;
size_t length;
if (CLASS_OF(to_write) == rb_cByteString) {
CFMutableDataRef data = rb_bytestring_wrapped_data(to_write);
buffer = CFDataGetMutableBytePtr(data);
length = CFDataGetLength(data);
}
else {
buffer = (UInt8 *)RSTRING_PTR(to_write);
if (buffer == NULL) {
rb_raise(rb_eRuntimeError,
"could not extract a string from the read data.");
}
length = strlen((char *)buffer);
}
if (length == 0) {
return INT2FIX(0);
}
ssize_t code = write(io_struct->write_fd, buffer, length);
if (code == -1) {
rb_sys_fail("write() failed");
}
if (io_struct->buf != NULL) {
if (length > CFDataGetLength(io_struct->buf) - io_struct->buf_offset) {
CFDataIncreaseLength(io_struct->buf, length);
}
UInt8 *data = CFDataGetMutableBytePtr(io_struct->buf);
memcpy(data + io_struct->buf_offset, buffer, length);
io_struct->buf_offset += length;
}
return LONG2FIX(code);
}
/*
* call-seq:
* ios << obj => ios
*
* String Output---Writes <i>obj</i> to <em>ios</em>.
* <i>obj</i> will be converted to a string using
* <code>to_s</code>.
*
* $stdout << "Hello " << "world!\n"
*
* <em>produces:</em>
*
* Hello world!
*/
VALUE
rb_io_addstr(VALUE io, SEL sel, VALUE str)
{
io_write(io, 0, str);
return io;
}
/*
* call-seq:
* ios.flush => ios
*
* Flushes any buffered data within <em>ios</em> to the underlying
* operating system (note that this is Ruby internal buffering only;
* the OS may buffer the data as well).
*
* $stdout.print "no newline"
* $stdout.flush
*
* <em>produces:</em>
*
* no newline
*/
VALUE
rb_io_flush(VALUE io, SEL sel)
{
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_open(io_struct);
// IO#flush on MacRuby is a no-op, as MacRuby does not buffer its IO
// streams internally
return io;
}
/*
* call-seq:
* ios.pos => integer
* ios.tell => integer
*
* Returns the current offset (in bytes) of <em>ios</em>.
*
* f = File.new("testfile")
* f.pos #=> 0
* f.gets #=> "This is line one\n"
* f.pos #=> 17
*/
static inline off_t
rb_io_read_stream_get_offset(CFReadStreamRef stream)
{
off_t result;
CFNumberRef pos = CFReadStreamCopyProperty(stream,
kCFStreamPropertyFileCurrentOffset);
if (pos == NULL) {
return -1;
}
CFNumberGetValue(pos, kCFNumberLongLongType, (void*)&result);
CFRelease(pos);
return result;
}
static inline void
rb_io_read_stream_set_offset(CFReadStreamRef stream, off_t offset)
{
CFNumberRef pos = CFNumberCreate(NULL, kCFNumberSInt64Type,
(const void*)&offset);
CFReadStreamSetProperty(stream, kCFStreamPropertyFileCurrentOffset, pos);
CFRelease(pos);
}
static off_t
ltell(int fd)
{
off_t code = lseek(fd, 0, SEEK_CUR);
if (code == -1) {
rb_sys_fail("lseek() failed");
}
return code;
}
static VALUE
rb_io_tell(VALUE io, SEL sel)
{
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_readable(io_struct);
return OFFT2NUM(ltell(io_struct->read_fd));
}
static VALUE
rb_io_seek(VALUE io, VALUE offset, int whence)
{
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_readable(io_struct);
off_t off = NUM2OFFT(offset);
// if (whence == SEEK_CUR) {
// off += ltell(io_struct->read_fd);
// }
const off_t code = lseek(io_struct->read_fd, off, whence);
if (code == -1) {
rb_sys_fail("lseek() failed");
}
if (io_struct->buf != NULL) {
io_struct->buf_offset = code;
}
return INT2FIX(0);
}
/*
* call-seq:
* ios.sysseek(offset, whence=SEEK_SET) => integer
*
* Seeks to a given <i>offset</i> in the stream according to the value
* of <i>whence</i> (see <code>IO#seek</code> for values of
* <i>whence</i>). Returns the new offset into the file.
*
* f = File.new("testfile")
* f.sysseek(-13, IO::SEEK_END) #=> 53
* f.sysread(10) #=> "And so on."
*/
/*
* call-seq:
* ios.seek(amount, whence=SEEK_SET) -> 0
*
* Seeks to a given offset <i>anInteger</i> in the stream according to
* the value of <i>whence</i>:
*
* IO::SEEK_CUR | Seeks to _amount_ plus current position
* --------------+----------------------------------------------------
* IO::SEEK_END | Seeks to _amount_ plus end of stream (you probably
* | want a negative value for _amount_)
* --------------+----------------------------------------------------
* IO::SEEK_SET | Seeks to the absolute location given by _amount_
*
* Example:
*
* f = File.new("testfile")
* f.seek(-13, IO::SEEK_END) #=> 0
* f.readline #=> "And so on...\n"
*/
static VALUE
rb_io_seek_m(VALUE io, SEL sel, int argc, VALUE *argv)
{
VALUE offset, ptrname;
int whence = SEEK_SET;
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
whence = NUM2INT(ptrname);
}
return rb_io_seek(io, offset, whence);
}
/*
* call-seq:
* ios.pos = integer => integer
*
* Seeks to the given position (in bytes) in <em>ios</em>.
*
* f = File.new("testfile")
* f.pos = 17
* f.gets #=> "This is line two\n"
*/
static VALUE
rb_io_set_pos(VALUE io, SEL sel, VALUE offset)
{
return rb_io_seek(io, offset, SEEK_SET);
}
/*
* call-seq:
* ios.rewind => 0
*
* Positions <em>ios</em> to the beginning of input, resetting
* <code>lineno</code> to zero.
*
* f = File.new("testfile")
* f.readline #=> "This is line one\n"
* f.rewind #=> 0
* f.lineno #=> 0
* f.readline #=> "This is line one\n"
*/
static VALUE
rb_io_rewind(VALUE io, SEL sel)
{
ExtractIOStruct(io)->lineno = 0;
return rb_io_seek(io, INT2FIX(0), SEEK_SET);
}
/*
* call-seq:
* ios.eof => true or false
* ios.eof? => true or false
*
* Returns true if <em>ios</em> is at end of file that means
* there are no more data to read.
* The stream must be opened for reading or an <code>IOError</code> will be
* raised.
*
* f = File.new("testfile")
* dummy = f.readlines
* f.eof #=> true
*
* If <em>ios</em> is a stream such as pipe or socket, <code>IO#eof?</code>
* blocks until the other end sends some data or closes it.
*
* r, w = IO.pipe
* Thread.new { sleep 1; w.close }
* r.eof? #=> true after 1 second blocking
*
* r, w = IO.pipe
* Thread.new { sleep 1; w.puts "a" }
* r.eof? #=> false after 1 second blocking
*
* r, w = IO.pipe
* r.eof? # blocks forever
*
* Note that <code>IO#eof?</code> reads data to a input buffer.
* So <code>IO#sysread</code> doesn't work with <code>IO#eof?</code>.
*/
static inline long rb_io_read_internal(rb_io_t *io_struct, UInt8 *buffer,
long len);
VALUE
rb_io_eof(VALUE io, SEL sel)
{
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_readable(io_struct);
if (rb_io_read_pending(io_struct)) {
return Qfalse;
}
UInt8 c;
if (rb_io_read_internal(io_struct, &c, 1) != 1) {
return Qtrue;
}
if (io_struct->buf != NULL) {
io_struct->buf_offset--;
}
return Qfalse;
}
/*
* call-seq:
* ios.sync => true or false
*
* Returns the current ``sync mode'' of <em>ios</em>. When sync mode is
* true, all output is immediately flushed to the underlying operating
* system and is not buffered by Ruby internally. See also
* <code>IO#fsync</code>.
*
* f = File.new("testfile")
* f.sync #=> false
*/
static VALUE
rb_io_sync(VALUE io, SEL sel)
{
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_open(io_struct);
return (io_struct->mode & FMODE_SYNC) ? Qtrue : Qfalse;
}
/*
* call-seq:
* ios.sync = boolean => boolean
*
* Sets the ``sync mode'' to <code>true</code> or <code>false</code>.
* When sync mode is true, all output is immediately flushed to the
* underlying operating system and is not buffered internally. Returns
* the new state. See also <code>IO#fsync</code>.
*
* f = File.new("testfile")
* f.sync = true
*
* <em>(produces no output)</em>
*/
static VALUE
rb_io_set_sync(VALUE io, SEL sel, VALUE mode)
{
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_open(io_struct);
if (RTEST(mode)) {
io_struct->mode |= FMODE_SYNC;
}
else {
io_struct->mode &= ~FMODE_SYNC;
}
return mode;
}
/*
* call-seq:
* ios.fsync => 0 or nil
*
* Immediately writes all buffered data in <em>ios</em> to disk.
* Returns <code>nil</code> if the underlying operating system does not
* support <em>fsync(2)</em>. Note that <code>fsync</code> differs from
* using <code>IO#sync=</code>. The latter ensures that data is flushed
* from Ruby's buffers, but doesn't not guarantee that the underlying
* operating system actually writes it to disk.
*/
static VALUE
rb_io_fsync(VALUE io, SEL sel)
{
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_writable(io_struct);
if (fsync(io_struct->fd) < 0) {
rb_sys_fail("fsync() failed.");
}
return INT2FIX(0);
}
/*
* call-seq:
* ios.fileno => fixnum
* ios.to_i => fixnum
*
* Returns an integer representing the numeric file descriptor for
* <em>ios</em>.
*
* $stdin.fileno #=> 0
* $stdout.fileno #=> 1
*/
static VALUE
rb_io_fileno(VALUE io, SEL sel)
{
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_open(io_struct);
return INT2FIX(io_struct->fd);
}
/*
* call-seq:
* ios.pid => fixnum
*
* Returns the process ID of a child process associated with
* <em>ios</em>. This will be set by <code>IO::popen</code>.
*
* pipe = IO.popen("-")
* if pipe
* $stderr.puts "In parent, child pid is #{pipe.pid}"
* else
* $stderr.puts "In child, pid is #{$$}"
* end
*
* <em>produces:</em>
*
* In child, pid is 26209
* In parent, child pid is 26209
*/
static VALUE
rb_io_pid(VALUE io, SEL sel)
{
rb_io_t *io_struct = ExtractIOStruct(io);
rb_io_assert_open(io_struct);
return io_struct->pid == -1 ? Qnil : INT2FIX(io_struct->pid);
}
/*
* call-seq:
* ios.inspect => string
*
* Return a string describing this IO object.
*/
static VALUE
rb_io_inspect(VALUE io, SEL sel)
{
rb_io_t *io_struct = ExtractIOStruct(io);
if ((io_struct == NULL) || (io_struct->path == NULL)) {
return rb_any_to_s(io);
}
const char *status = (rb_io_is_open(io_struct) ? "" : " (closed)");
CFStringRef s = CFStringCreateWithFormat(NULL, NULL, CFSTR("#<%s:%@%s>"),
rb_obj_classname(io), io_struct->path, status);
CFMakeCollectable(s);
return (VALUE)s;
}
/*
* call-seq:
* ios.to_io -> ios
*
* Returns <em>ios</em>.
*/
static VALUE
rb_io_to_io(VALUE io, SEL sel)
{
return io;
}
static inline bool
__rb_io_wait_readable(int fd)
{
if (errno == EINTR) {
fd_set readset;
FD_ZERO(&readset);
FD_SET(fd, &readset);
return select(fd + 1, &readset, NULL, NULL, NULL) >= 0;
}
return false;
}
bool
rb_io_wait_readable(int fd)
{
return __rb_io_wait_readable(fd);
}
bool
rb_io_wait_writable(int fd)
{
// TODO
return false;
}
static inline long
read_internal(int fd, UInt8 *buffer, long len)
{
long code;
retry:
code = read(fd, buffer, len);
if (code == -1) {
if (__rb_io_wait_readable(fd)) {
goto retry;
}
rb_sys_fail("read() failed");
}
return code;
}
static inline void
rb_io_create_buf(rb_io_t *io_struct)
{
if (io_struct->buf == NULL) {
CFMutableDataRef data = CFDataCreateMutable(NULL, 0);
GC_WB(&io_struct->buf, data);
CFMakeCollectable(data);
}
}
static inline long
rb_io_read_internal(rb_io_t *io_struct, UInt8 *buffer, long len)
{
assert(io_struct->read_fd != -1);
if (io_struct->buf == NULL || CFDataGetLength(io_struct->buf) == 0) {
struct stat buf;
if (fstat(io_struct->read_fd, &buf) == -1 || buf.st_size == 0
|| lseek(io_struct->read_fd, 0, SEEK_CUR) > 0) {
// Either a pipe, stdio, or a regular file that was seeked.
return read_internal(io_struct->read_fd, buffer, len);
}
// TODO don't pre-read more than a certain threshold...
const long size = buf.st_size;
rb_io_create_buf(io_struct);
CFDataSetLength(io_struct->buf, size);
const long s = read_internal(io_struct->read_fd,
CFDataGetMutableBytePtr(io_struct->buf), size);
CFDataSetLength(io_struct->buf, s);
}
const long s = CFDataGetLength(io_struct->buf);
if (s == 0 || len == 0) {
return 0;
}
const long n = len > s - io_struct->buf_offset
? s - io_struct->buf_offset : len;
memcpy(buffer, CFDataGetBytePtr(io_struct->buf) + io_struct->buf_offset, n);
io_struct->buf_offset += n;
lseek(io_struct->read_fd, io_struct->buf_offset, SEEK_SET);
if (io_struct->buf_offset == s) {
CFDataSetLength(io_struct->buf, 0);
io_struct->buf_offset = 0;
}
return n;
}
static VALUE
rb_io_read_all(rb_io_t *io_struct, VALUE bytestring_buffer)
{
const long BUFSIZE = 512;
CFMutableDataRef data = rb_bytestring_wrapped_data(bytestring_buffer);
long bytes_read = 0;
const long original_position = (long)CFDataGetLength(data);
for (;;) {
CFDataIncreaseLength(data, BUFSIZE);
UInt8 *b = CFDataGetMutableBytePtr(data) + original_position
+ bytes_read;
const long last_read = rb_io_read_internal(io_struct, b, BUFSIZE);
bytes_read += last_read;
if (last_read == 0) {
break;
}
}
CFDataSetLength(data, original_position + bytes_read);
return bytestring_buffer;
}