From 9233c9a14ce2772a46ff72e3e348d535bf96e61c Mon Sep 17 00:00:00 2001 From: akr Date: Sat, 27 Nov 2004 13:05:46 +0000 Subject: * io.c: avoid avoid data loss with nonblocking fd and stdio buffering in sync mode. [ruby-dev:24966] based on matz's patch [ruby-dev:24967] (io_fwrite): new primitive writing function which writes directly if sync mode. (rb_io_fwrite): wrapper for io_fwrite now. (io_write): call io_fwrite instead of rb_io_fwrite. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@7392 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- io.c | 53 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 13 deletions(-) (limited to 'io.c') diff --git a/io.c b/io.c index 8e1dbbacd7..fd52a8100f 100644 --- a/io.c +++ b/io.c @@ -388,14 +388,34 @@ rb_io_wait_writable(f) /* writing functions */ long -rb_io_fwrite(ptr, len, f) +io_fwrite(ptr, len, fptr) const char *ptr; long len; - FILE *f; + OpenFile *fptr; { long n, r; + FILE *f = GetWriteFile(fptr); if ((n = len) <= 0) return n; + if (fptr->mode & FMODE_SYNC) { + io_fflush(f, fptr); + if (!rb_thread_fd_writable(fileno(f))) { + rb_io_check_closed(fptr); + } + retry: + r = write(fileno(f), ptr, n); + if (r == n) return len; + if (0 <= r) { + ptr += r; + n -= r; + errno = EAGAIN; + } + if (rb_io_wait_writable(fileno(f))) { + rb_io_check_closed(fptr); + goto retry; + } + return -1L; + } #if defined(__human68k__) || defined(__vms) do { if (fputc(*ptr++, f) == EOF) { @@ -414,6 +434,7 @@ rb_io_fwrite(ptr, len, f) if (!errno) errno = EAGAIN; #endif if (rb_io_wait_writable(fileno(f))) { + rb_io_check_closed(fptr); clearerr(f); continue; } @@ -424,6 +445,21 @@ rb_io_fwrite(ptr, len, f) return len - n; } +long +rb_io_fwrite(ptr, len, f) + const char *ptr; + long len; + FILE *f; +{ + OpenFile of; + + of.f = f; + of.f2 = NULL; + of.mode = FMODE_WRITABLE; + of.path = "(rb_io_fwrite)"; + return io_fwrite(ptr, len, &of); +} + /* * call-seq: * ios.write(string) => integer @@ -447,7 +483,6 @@ io_write(io, str) VALUE io, str; { OpenFile *fptr; - FILE *f; long n; rb_secure(4); @@ -462,16 +497,12 @@ io_write(io, str) GetOpenFile(io, fptr); rb_io_check_writable(fptr); - f = GetWriteFile(fptr); rb_str_locktmp(str); - n = rb_io_fwrite(RSTRING(str)->ptr, RSTRING(str)->len, f); + n = io_fwrite(RSTRING(str)->ptr, RSTRING(str)->len, fptr); rb_str_unlocktmp(str); if (n == -1L) rb_sys_fail(fptr->path); - if (fptr->mode & FMODE_SYNC) { - io_fflush(f, fptr); - } - else { + if (!(fptr->mode & FMODE_SYNC)) { fptr->mode |= FMODE_WBUF; } @@ -1390,7 +1421,6 @@ rb_io_gets_m(argc, argv, io) VALUE io; { VALUE rs, str; - OpenFile *fptr; if (argc == 0) { rs = rb_rs; @@ -1534,7 +1564,6 @@ rb_io_readlines(argc, argv, io) { VALUE line, ary; VALUE rs; - OpenFile *fptr; if (argc == 0) { rs = rb_rs; @@ -1577,7 +1606,6 @@ rb_io_each_line(argc, argv, io) VALUE io; { VALUE str; - OpenFile *fptr; VALUE rs; if (argc == 0) { @@ -4804,7 +4832,6 @@ rb_io_s_foreach(argc, argv) VALUE *argv; { VALUE fname; - OpenFile *fptr; struct foreach_arg arg; rb_scan_args(argc, argv, "11", &fname, &arg.sep); -- cgit v1.2.3