ruby-changes:30131
From: nobu <ko1@a...>
Date: Fri, 26 Jul 2013 13:04:32 +0900 (JST)
Subject: [ruby-changes:30131] nobu:r42183 (trunk): load.c: search in OS path encoding
nobu 2013-07-26 13:04:23 +0900 (Fri, 26 Jul 2013) New Revision: 42183 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42183 Log: load.c: search in OS path encoding * load.c (rb_load_internal): use rb_load_file_str() to keep path encoding. * load.c (rb_require_safe): search in OS path encoding for Windows. * ruby.c (rb_load_file_str): load file with keeping path encoding. * win32/file.c (rb_file_load_ok): use WCHAR type API assuming incoming path is encoded in UTF-8. [ruby-core:56136] [Bug #8676] Modified files: trunk/ChangeLog trunk/include/ruby/intern.h trunk/load.c trunk/ruby.c trunk/test/ruby/test_require.rb trunk/win32/file.c Index: include/ruby/intern.h =================================================================== --- include/ruby/intern.h (revision 42182) +++ include/ruby/intern.h (revision 42183) @@ -644,6 +644,7 @@ int rb_reg_options(VALUE); https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L644 RUBY_EXTERN VALUE rb_argv0; VALUE rb_get_argv(void); void *rb_load_file(const char*); +void *rb_load_file_str(VALUE); /* signal.c */ VALUE rb_f_kill(int, VALUE*); #ifdef POSIX_SIGNAL Index: ChangeLog =================================================================== --- ChangeLog (revision 42182) +++ ChangeLog (revision 42183) @@ -1,4 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Fri Jul 26 13:01:57 2013 Nobuyoshi Nakada <nobu@r...> +Fri Jul 26 13:04:15 2013 Nobuyoshi Nakada <nobu@r...> + + * load.c (rb_load_internal): use rb_load_file_str() to keep path + encoding. + + * load.c (rb_require_safe): search in OS path encoding for Windows. + + * ruby.c (rb_load_file_str): load file with keeping path encoding. + + * win32/file.c (rb_file_load_ok): use WCHAR type API assuming incoming + path is encoded in UTF-8. [ruby-core:56136] [Bug #8676] * file.c (rb_str_encode_ospath): simplify using rb_str_conv_enc(). Index: load.c =================================================================== --- load.c (revision 42182) +++ load.c (revision 42183) @@ -591,7 +591,7 @@ rb_load_internal(VALUE fname, int wrap) https://github.com/ruby/ruby/blob/trunk/load.c#L591 VALUE iseq; th->mild_compile_error++; - node = (NODE *)rb_load_file(RSTRING_PTR(fname)); + node = (NODE *)rb_load_file_str(fname); loaded = TRUE; iseq = rb_iseq_new_top(node, rb_str_new2("<top (required)>"), fname, rb_realpath_internal(Qnil, fname, 1), Qfalse); th->mild_compile_error--; @@ -941,7 +941,8 @@ rb_require_safe(VALUE fname, int safe) https://github.com/ruby/ruby/blob/trunk/load.c#L941 rb_sourceline()); } - found = search_required(fname, &path, safe); + path = rb_str_encode_ospath(fname); + found = search_required(path, &path, safe); if (RUBY_DTRACE_FIND_REQUIRE_RETURN_ENABLED()) { RUBY_DTRACE_FIND_REQUIRE_RETURN(StringValuePtr(fname), Index: win32/file.c =================================================================== --- win32/file.c (revision 42182) +++ win32/file.c (revision 42183) @@ -681,16 +681,22 @@ rb_file_expand_path_internal(VALUE fname https://github.com/ruby/ruby/blob/trunk/win32/file.c#L681 int rb_file_load_ok(const char *path) { + DWORD attr; int ret = 1; - DWORD attr = GetFileAttributes(path); + size_t len; + wchar_t* wpath; + + convert_mb_to_wchar(path, &wpath, &len, CP_UTF8); + + attr = GetFileAttributesW(wpath); if (attr == INVALID_FILE_ATTRIBUTES || - attr & FILE_ATTRIBUTE_DIRECTORY) { + (attr & FILE_ATTRIBUTE_DIRECTORY)) { ret = 0; } else { - HANDLE h = CreateFile(path, GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE h = CreateFileW(wpath, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (h != INVALID_HANDLE_VALUE) { CloseHandle(h); } @@ -698,6 +704,7 @@ rb_file_load_ok(const char *path) https://github.com/ruby/ruby/blob/trunk/win32/file.c#L704 ret = 0; } } + xfree(wpath); return ret; } Index: ruby.c =================================================================== --- ruby.c (revision 42182) +++ ruby.c (revision 42183) @@ -1768,8 +1768,14 @@ load_file(VALUE parser, VALUE fname, int https://github.com/ruby/ruby/blob/trunk/ruby.c#L1768 void * rb_load_file(const char *fname) { - struct cmdline_options opt; VALUE fname_v = rb_str_new_cstr(fname); + return rb_load_file_str(fname_v); +} + +void * +rb_load_file_str(VALUE fname_v) +{ + struct cmdline_options opt; return load_file(rb_parser_new(), fname_v, 0, cmdline_options_init(&opt)); } Index: test/ruby/test_require.rb =================================================================== --- test/ruby/test_require.rb (revision 42182) +++ test/ruby/test_require.rb (revision 42183) @@ -61,10 +61,17 @@ class TestRequire < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L61 def test_require_nonascii_path bug8165 = '[ruby-core:53733] [Bug #8165]' - encoding = /mswin|mingw/ =~ RUBY_PLATFORM ? 'filesystem' : 'UTF-8' + encoding = 'filesystem' assert_require_nonascii_path(encoding, bug8165) end + def test_require_nonascii_path_utf8 + bug8676 = '[ruby-core:56136] [Bug #8676]' + encoding = Encoding::UTF_8 + return if Encoding.find('filesystem') == encoding + assert_require_nonascii_path(encoding, bug8676) + end + def assert_require_nonascii_path(encoding, bug) Dir.mktmpdir {|tmp| dir = "\u3042" * 5 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/