[前][次][番号順一覧][スレッド一覧]

ruby-changes:38988

From: nobu <ko1@a...>
Date: Tue, 30 Jun 2015 17:28:52 +0900 (JST)
Subject: [ruby-changes:38988] nobu:r51069 (trunk): io.c: reopen OS encoding path

nobu	2015-06-30 17:28:28 +0900 (Tue, 30 Jun 2015)

  New Revision: 51069

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51069

  Log:
    io.c: reopen OS encoding path
    
    * io.c (rb_io_reopen): freopen(3) with OS encoding path.
      [ruby-core:69780] [Bug #11320]
    * win32/file.c (rb_freopen): wrapper of wchar version freopen(3).
      use _wfreopen_s() if available.

  Modified files:
    trunk/ChangeLog
    trunk/configure.in
    trunk/io.c
    trunk/test/ruby/test_io.rb
    trunk/win32/file.c
Index: configure.in
===================================================================
--- configure.in	(revision 51068)
+++ configure.in	(revision 51069)
@@ -1130,6 +1130,7 @@ main() https://github.com/ruby/ruby/blob/trunk/configure.in#L1130
 		    AC_DEFINE(HAVE_TYPE_NET_LUID, 1)
 		fi
 		AC_CHECK_FUNCS(_gmtime64_s)
+		AC_CHECK_FUNCS(_wfreopen_s)
 		AC_LIBOBJ([langinfo])
 		],
 [os2-emx*], [	LIBS="-lm $LIBS"
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51068)
+++ ChangeLog	(revision 51069)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jun 30 17:28:25 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* io.c (rb_io_reopen): freopen(3) with OS encoding path.
+	  [ruby-core:69780] [Bug #11320]
+
+	* win32/file.c (rb_freopen): wrapper of wchar version freopen(3).
+	  use _wfreopen_s() if available.
+
 Tue Jun 30 08:24:08 2015  Eric Wong  <e@8...>
 
 	* io.c (rb_io_oflags_modestr): handle O_TRUNC correctly
Index: io.c
===================================================================
--- io.c	(revision 51068)
+++ io.c	(revision 51069)
@@ -6706,6 +6706,20 @@ io_reopen(VALUE io, VALUE nfile) https://github.com/ruby/ruby/blob/trunk/io.c#L6706
     return io;
 }
 
+#ifdef _WIN32
+int rb_freopen(VALUE fname, const char *mode, FILE *fp);
+#else
+static int
+rb_freopen(VALUE fname, const char *mode, FILE *fp)
+{
+    if (!freopen(RSTRING_PTR(fname), mode, fp)) {
+	RB_GC_GUARD(fname);
+	return errno;
+    }
+    return 0;
+}
+#endif
+
 /*
  *  call-seq:
  *     ios.reopen(other_IO)         -> ios
@@ -6777,9 +6791,10 @@ rb_io_reopen(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/io.c#L6791
     fptr->rbuf.off = fptr->rbuf.len = 0;
 
     if (fptr->stdio_file) {
-        if (freopen(RSTRING_PTR(fptr->pathv), rb_io_oflags_modestr(oflags), fptr->stdio_file) == 0) {
-            rb_sys_fail_path(fptr->pathv);
-        }
+	int e = rb_freopen(rb_str_encode_ospath(fptr->pathv),
+			   rb_io_oflags_modestr(oflags),
+			   fptr->stdio_file);
+	if (e) rb_syserr_fail_path(e, fptr->pathv);
         fptr->fd = fileno(fptr->stdio_file);
         rb_fd_fix_cloexec(fptr->fd);
 #ifdef USE_SETVBUF
Index: win32/file.c
===================================================================
--- win32/file.c	(revision 51068)
+++ win32/file.c	(revision 51069)
@@ -721,6 +721,28 @@ rb_file_load_ok(const char *path) https://github.com/ruby/ruby/blob/trunk/win32/file.c#L721
     return ret;
 }
 
+int
+rb_freopen(VALUE fname, const char *mode, FILE *file)
+{
+    WCHAR *wname, wmode[4];
+    int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
+    if (n > numberof(wmode)) return EINVAL;
+    MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
+    wname = rb_w32_mbstr_to_wstr(CP_UTF8, RSTRING_PTR(fname),
+				 rb_long2int(RSTRING_LEN(fname)), NULL);
+    RB_GC_GUARD(fname);
+#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
+    e = _wfreopen(wname, wmode, file) ? 0 : errno;
+#else
+    {
+	FILE *newfp = 0;
+	e = _wfreopen_s(&newfp, wname, wmode, file);
+    }
+#endif
+    xfree(wname);
+    return e;
+}
+
 void
 Init_w32_codepage(void)
 {
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 51068)
+++ test/ruby/test_io.rb	(revision 51069)
@@ -2164,6 +2164,26 @@ End https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L2164
     }
   end
 
+  bug11320 = '[ruby-core:69780] [Bug #11320]'
+  ["UTF-8", "EUC-JP", "Shift_JIS"].each do |enc|
+    define_method("test_reopen_nonascii(#{enc})") do
+      mkcdtmpdir do
+        fname = "\u{30eb 30d3 30fc}".encode(enc)
+        File.write(fname, '')
+        assert_file.exist?(fname)
+        stdin = $stdin.dup
+        begin
+          assert_nothing_raised(Errno::ENOENT, enc) {
+            $stdin.reopen(fname, 'r')
+          }
+        ensure
+          $stdin.reopen(stdin)
+          stdin.close
+        end
+      end
+    end
+  end
+
   def test_foreach
     a = []
     IO.foreach("|" + EnvUtil.rubybin + " -e 'puts :foo; puts :bar; puts :baz'") {|x| a << x }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]