ruby-changes:12879
From: usa <ko1@a...>
Date: Fri, 21 Aug 2009 17:17:33 +0900 (JST)
Subject: [ruby-changes:12879] Ruby:r24614 (win32-unicode-test): * file.c, include/ruby/intern.h (rb_str_conv_for_path): new function
usa 2009-08-21 17:16:26 +0900 (Fri, 21 Aug 2009) New Revision: 24614 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24614 Log: * file.c, include/ruby/intern.h (rb_str_conv_for_path): new function to convert encoding for pathname. * io.c (rb_sysopen): use above function. * dir.c (dir_chdir, dir_s_chroot, dir_s_mkdir, dir_s_rmdir): ditto. * win32/win32.c, include/ruby/win32.h (rb_w32_wmkdir): new function to make directory with WCHAR pathname. Modified files: branches/win32-unicode-test/ChangeLog branches/win32-unicode-test/dir.c branches/win32-unicode-test/file.c branches/win32-unicode-test/include/ruby/intern.h branches/win32-unicode-test/include/ruby/win32.h branches/win32-unicode-test/io.c branches/win32-unicode-test/win32/win32.c Index: win32-unicode-test/include/ruby/intern.h =================================================================== --- win32-unicode-test/include/ruby/intern.h (revision 24613) +++ win32-unicode-test/include/ruby/intern.h (revision 24614) @@ -358,6 +358,7 @@ char *rb_path_last_separator(const char *); char *rb_path_end(const char *); VALUE rb_file_directory_p(VALUE,VALUE); +VALUE rb_str_conv_for_path(VALUE); /* gc.c */ void ruby_set_stack_size(size_t); NORETURN(void rb_memerror(void)); Index: win32-unicode-test/include/ruby/win32.h =================================================================== --- win32-unicode-test/include/ruby/win32.h (revision 24613) +++ win32-unicode-test/include/ruby/win32.h (revision 24614) @@ -274,6 +274,7 @@ #if !defined(__BORLANDC__) extern int rb_w32_isatty(int); #endif +extern int rb_w32_wmkdir(const WCHAR *, int); extern int rb_w32_mkdir(const char *, int); extern int rb_w32_rmdir(const char *); extern int rb_w32_unlink(const char *); Index: win32-unicode-test/ChangeLog =================================================================== --- win32-unicode-test/ChangeLog (revision 24613) +++ win32-unicode-test/ChangeLog (revision 24614) @@ -1,9 +1,15 @@ -Thu Sep 25 15:16:48 2008 NAKAMURA Usaku <usa@r...> +Fri Aug 21 17:12:39 2009 NAKAMURA Usaku <usa@r...> - * transcode.c (rb_transcode_convertible): follow changes. + * file.c, include/ruby/intern.h (rb_str_conv_for_path): new function + to convert encoding for pathname. - * win32/win32.c (rb_w32_parse_cmdline, rb_w32_write_console): ditto. + * io.c (rb_sysopen): use above function. + * dir.c (dir_chdir, dir_s_chroot, dir_s_mkdir, dir_s_rmdir): ditto. + + * win32/win32.c, include/ruby/win32.h (rb_w32_wmkdir): new function + to make directory with WCHAR pathname. + Fri Aug 21 15:13:08 2009 NARUSE, Yui <naruse@r...> * include/ruby/io.h, io.c (FMODE_SETENC_BY_BOM): Index: win32-unicode-test/io.c =================================================================== --- win32-unicode-test/io.c (revision 24613) +++ win32-unicode-test/io.c (revision 24614) @@ -4407,50 +4407,18 @@ VALUE fname; int oflags; mode_t perm; -#ifdef _WIN32 - int wchar; -#endif }; -#ifdef _WIN32 -static rb_encoding * -w32_utf16(void) -{ - static rb_encoding *utf16 = (rb_encoding *)-1; - if (utf16 == (rb_encoding *)-1) { - utf16 = rb_enc_find("UTF-16LE"); - if (utf16 == rb_ascii8bit_encoding()) - utf16 = NULL; - } - return utf16; -} - -static int -w32_conv_to_utf16(volatile VALUE *strp) -{ - rb_encoding *utf16 = w32_utf16(); - if (utf16) { - VALUE wstr = rb_str_encode(*strp, rb_enc_from_encoding(utf16), 0, Qnil); - rb_enc_str_buf_cat(wstr, "", 1, utf16); /* workaround */ - *strp = wstr; - return 1; - } - else { - return 0; - } -} -#endif - static VALUE sysopen_func(void *ptr) { const struct sysopen_struct *data = ptr; const char *fname = RSTRING_PTR(data->fname); #ifdef _WIN32 - if (data->wchar) - return (VALUE)rb_w32_wopen((WCHAR *)fname, data->oflags, data->perm); -#endif + return (VALUE)rb_w32_wopen((WCHAR *)fname, data->oflags, data->perm); +#else return (VALUE)open(fname, data->oflags, data->perm); +#endif } static inline int @@ -4468,14 +4436,9 @@ #ifdef O_BINARY oflags |= O_BINARY; #endif - data.fname = fname; + data.fname = rb_str_conv_for_path(fname); data.oflags = oflags; data.perm = perm; -#ifdef _WIN32 - if ((data.wchar = w32_conv_to_utf16(&data.fname)) != 0) { - OBJ_FREEZE(data.fname); - } -#endif fd = rb_sysopen_internal(&data); if (fd < 0) { Index: win32-unicode-test/dir.c =================================================================== --- win32-unicode-test/dir.c (revision 24613) +++ win32-unicode-test/dir.c (revision 24614) @@ -718,7 +718,12 @@ static void dir_chdir(VALUE path) { + path = rb_str_conv_for_path(path); +#ifdef _WIN32 + if (_wchdir((WCHAR *)RSTRING_PTR(path)) < 0) +#else if (chdir(RSTRING_PTR(path)) < 0) +#endif rb_sys_fail(RSTRING_PTR(path)); } @@ -883,6 +888,7 @@ { check_dirname(&path); + path = rb_str_conv_for_path(path); if (chroot(RSTRING_PTR(path)) == -1) rb_sys_fail(RSTRING_PTR(path)); @@ -919,7 +925,12 @@ } check_dirname(&path); + path = rb_str_conv_for_path(path); +#ifdef _WIN32 + if (rb_w32_wmkdir((WCHAR *)RSTRING_PTR(path), mode) == -1) +#else if (mkdir(RSTRING_PTR(path), mode) == -1) +#endif rb_sys_fail(RSTRING_PTR(path)); return INT2FIX(0); @@ -938,7 +949,12 @@ dir_s_rmdir(VALUE obj, VALUE dir) { check_dirname(&dir); + dir = rb_str_conv_for_path(dir); +#ifdef _WIN32 + if (_wrmdir((WCHAR *)RSTRING_PTR(dir)) < 0) +#else if (rmdir(RSTRING_PTR(dir)) < 0) +#endif rb_sys_fail(RSTRING_PTR(dir)); return INT2FIX(0); Index: win32-unicode-test/win32/win32.c =================================================================== --- win32-unicode-test/win32/win32.c (revision 24613) +++ win32-unicode-test/win32/win32.c (revision 24614) @@ -5163,6 +5163,24 @@ } int +rb_w32_wmkdir(const WCHAR *path, int mode) +{ + int ret = -1; + RUBY_CRITICAL(do { + if (CreateDirectoryW(path, NULL) == FALSE) { + errno = map_errno(GetLastError()); + break; + } + if (_wchmod(path, mode) == -1) { + RemoveDirectoryW(path); + break; + } + ret = 0; + } while (0)); + return ret; +} + +int rb_w32_mkdir(const char *path, int mode) { int ret = -1; Index: win32-unicode-test/file.c =================================================================== --- win32-unicode-test/file.c (revision 24613) +++ win32-unicode-test/file.c (revision 24614) @@ -142,6 +142,46 @@ return rb_get_path_check(obj, rb_safe_level()); } +VALUE +rb_str_conv_for_path(VALUE path) +{ +#ifdef _WIN32 + static rb_encoding *utf16 = (rb_encoding *)-1; + VALUE wpath; + if (utf16 == (rb_encoding *)-1) { + utf16 = rb_enc_find("UTF-16LE"); + if (utf16 == rb_ascii8bit_encoding()) + utf16 = NULL; + } + if (utf16 && rb_enc_get(path) != rb_ascii8bit_encoding()) { + wpath = rb_str_encode(path, rb_enc_from_encoding(utf16), 0, Qnil); + rb_enc_str_buf_cat(wpath, "", 1, utf16); /* workaround */ + return wpath; + } + else if (RSTRING_LEN(path) > 0) { + UINT cp = AreFileApisANSI() ? CP_ACP : CP_OEMCP; + int len = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), -1, NULL, 0); + WCHAR *tmp; + if (len <= 0) + rb_raise(rb_eArgError, "broken pathname"); + tmp = ALLOCA_N(WCHAR, len); + len = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), -1, tmp, len); + if (len <= 0) + rb_raise(rb_eArgError, "broken pathname"); + wpath = rb_str_new((char *)tmp, len * sizeof(WCHAR)); + OBJ_INFECT(wpath, path); + return wpath; + } + else + return path; +#elif defined __APPLE__ + return rb_str_encode(path, rb_enc_from_encoding(rb_filesystem_encoding()), + 0, Qnil); +#else + return path; +#endif +} + static long apply2files(void (*func)(const char *, void *), VALUE vargs, void *arg) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/