1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

win32: Use UTF-8 as filesystem encoding [Feature #12654]

Co-Authored-By: Dāvis Mosāns <davispuh@gmail.com>
This commit is contained in:
Nobuyoshi Nakada 2020-12-20 02:16:31 +09:00
parent feea436feb
commit 5b98b2ce39
Notes: git 2020-12-20 18:34:28 +09:00
6 changed files with 37 additions and 59 deletions

View file

@ -138,18 +138,18 @@ typedef int clockid_t;
#undef stat
#undef fstat
#ifdef RUBY_EXPORT
#define utime(_p, _t) rb_w32_utime(_p, _t)
#define utime(_p, _t) rb_w32_uutime(_p, _t)
#undef HAVE_UTIMES
#define HAVE_UTIMES 1
#define utimes(_p, _t) rb_w32_utimes(_p, _t)
#define utimes(_p, _t) rb_w32_uutimes(_p, _t)
#undef HAVE_UTIMENSAT
#define HAVE_UTIMENSAT 1
#define AT_FDCWD -100
#define utimensat(_d, _p, _t, _f) rb_w32_utimensat(_d, _p, _t, _f)
#define utimensat(_d, _p, _t, _f) rb_w32_uutimensat(_d, _p, _t, _f)
#define lseek(_f, _o, _w) rb_w32_lseek(_f, _o, _w)
#define pipe(p) rb_w32_pipe(p)
#define open rb_w32_open
#define open rb_w32_uopen
#define close(h) rb_w32_close(h)
#define fclose(f) rb_w32_fclose(f)
#define read(f, b, s) rb_w32_read(f, b, s)
@ -165,11 +165,11 @@ typedef int clockid_t;
#define isatty(h) rb_w32_isatty(h)
#undef mkdir
#define mkdir(p, m) rb_w32_mkdir(p, m)
#define mkdir(p, m) rb_w32_umkdir(p, m)
#undef rmdir
#define rmdir(p) rb_w32_rmdir(p)
#define rmdir(p) rb_w32_urmdir(p)
#undef unlink
#define unlink(p) rb_w32_unlink(p)
#define unlink(p) rb_w32_uunlink(p)
#endif /* RUBY_EXPORT */
/* same with stati64 except the size of st_ino and nanosecond timestamps */
@ -200,9 +200,9 @@ struct stati128 {
#define HAVE_STRUCT_STAT_ST_MTIMENSEC
#define HAVE_STRUCT_STAT_ST_CTIMENSEC
#define fstat(fd,st) rb_w32_fstati128(fd,st)
#define stati128(path, st) rb_w32_stati128(path,st)
#define lstat(path,st) rb_w32_lstati128(path,st)
#define access(path,mode) rb_w32_access(path,mode)
#define stati128(path, st) rb_w32_ustati128(path,st)
#define lstat(path,st) rb_w32_ulstati128(path,st)
#define access(path,mode) rb_w32_uaccess(path,mode)
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
@ -287,6 +287,7 @@ extern int socketpair(int, int, int, int *);
extern int getifaddrs(struct ifaddrs **);
extern void freeifaddrs(struct ifaddrs *);
extern char * rb_w32_getcwd(char *, int);
extern char * rb_w32_ugetcwd(char *, int);
extern char * rb_w32_ugetenv(const char *);
extern char * rb_w32_getenv(const char *);
extern int rb_w32_rename(const char *, const char *);
@ -433,11 +434,7 @@ extern int rb_w32_utruncate(const char *path, off_t length);
#undef HAVE_TRUNCATE
#define HAVE_TRUNCATE 1
#if defined HAVE_TRUNCATE64
#define truncate truncate64
#else
#define truncate rb_w32_truncate
#endif
#define truncate rb_w32_utruncate
#if defined(_MSC_VER) && _MSC_VER >= 1400 && _MSC_VER < 1800
#define strtoll _strtoi64
@ -706,13 +703,13 @@ extern char *rb_w32_strerror(int);
#define get_osfhandle(h) rb_w32_get_osfhandle(h)
#undef getcwd
#define getcwd(b, s) rb_w32_getcwd(b, s)
#define getcwd(b, s) rb_w32_ugetcwd(b, s)
#undef getenv
#define getenv(n) rb_w32_ugetenv(n)
#undef rename
#define rename(o, n) rb_w32_rename(o, n)
#define rename(o, n) rb_w32_urename(o, n)
#undef times
#define times(t) rb_w32_times(t)

View file

@ -123,8 +123,9 @@ Init_enc_set_filesystem_encoding(void)
idx = ENCINDEX_US_ASCII;
#elif defined _WIN32
char cp[SIZEOF_CP_NAME];
const UINT codepage = ruby_w32_codepage[1] ? ruby_w32_codepage[1] :
AreFileApisANSI() ? GetACP() : GetOEMCP();
const UINT codepage = ruby_w32_codepage[1];
if (!codepage) return ENCINDEX_UTF_8;
/* for debugging */
CP_FORMAT(cp, codepage);
idx = rb_enc_find_index(cp);
if (idx < 0) idx = ENCINDEX_ASCII;

View file

@ -17,27 +17,19 @@ class TestDir_M17N < Test::Unit::TestCase
assert_separately(["-E#{encoding}"], <<-EOS, :chdir=>dir)
filename = #{code}.chr('UTF-8').force_encoding("#{encoding}")
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
ents = Dir.entries(".", **(opts||{}))
ents = Dir.entries(".")
if /mswin|mingw/ =~ RUBY_PLATFORM
filename = filename.encode("UTF-8")
end
assert_include(ents, filename)
EOS
return if /cygwin/ =~ RUBY_PLATFORM
assert_separately(%w[-EASCII-8BIT], <<-EOS, :chdir=>dir)
filename = #{code}.chr('UTF-8').force_encoding("ASCII-8BIT")
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
ents = Dir.entries(".", **(opts||{}))
expected_filename = #{code}.chr('UTF-8').encode(Encoding.find("filesystem")) rescue expected_filename = "?"
expected_filename = expected_filename.force_encoding("ASCII-8BIT")
ents = Dir.entries(".")
if /mswin|mingw/ =~ RUBY_PLATFORM
case
when ents.include?(filename)
when ents.include?(expected_filename)
filename = expected_filename
else
ents = Dir.entries(".", :encoding => Encoding.find("filesystem"))
filename = expected_filename
end
filename.force_encoding("UTF-8")
end
assert_include(ents, filename)
EOS
@ -199,27 +191,23 @@ class TestDir_M17N < Test::Unit::TestCase
assert_separately(%w[-EEUC-JP], <<-'EOS', :chdir=>d)
filename = "\xA4\xA2".force_encoding("euc-jp")
File.open(filename, "w") {}
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
ents = Dir.entries(".", **(opts||{}))
ents = Dir.entries(".")
if /darwin/ =~ RUBY_PLATFORM
filename = filename.encode("utf-8").force_encoding("euc-jp")
elsif /mswin|mingw/ =~ RUBY_PLATFORM
filename = filename.encode("utf-8")
end
assert_include(ents, filename)
EOS
assert_separately(%w[-EASCII-8BIT], <<-'EOS', :chdir=>d)
filename = "\xA4\xA2".force_encoding('ASCII-8BIT')
win_expected_filename = filename.encode(Encoding.find("filesystem"), "euc-jp") rescue "?"
opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
ents = Dir.entries(".", **(opts||{}))
ents = Dir.entries(".")
unless ents.include?(filename)
case RUBY_PLATFORM
when /darwin/
filename = filename.encode("utf-8", "euc-jp").b
when /mswin|mingw/
if ents.include?(win_expected_filename.b)
ents = Dir.entries(".", :encoding => Encoding.find("filesystem"))
filename = win_expected_filename
end
filename = filename.encode("utf-8", "euc-jp")
end
end
assert_include(ents, filename)
@ -414,13 +402,8 @@ class TestDir_M17N < Test::Unit::TestCase
orig.each {|n| open(n, "w") {}}
enc = Encoding.find("filesystem")
enc = Encoding::ASCII_8BIT if enc == Encoding::US_ASCII
if /mswin|mingw/ =~ RUBY_PLATFORM
opts = {:encoding => enc}
orig.map! {|o| o.encode("filesystem") rescue o.tr("^a-z", "?")}
else
orig.each {|o| o.force_encoding(enc) }
end
ents = Dir.entries(".", **(opts||{})).reject {|n| /\A\./ =~ n}
ents = Dir.entries(".").reject {|n| /\A\./ =~ n}
ents.sort!
PP.assert_equal(orig, ents, bug7267)
}
@ -431,13 +414,9 @@ class TestDir_M17N < Test::Unit::TestCase
expected = []
results = []
orig.each {|o|
if /mswin|mingw/ =~ RUBY_PLATFORM
n = (o.encode("filesystem") rescue next)
else
enc = Encoding.find("filesystem")
enc = Encoding::ASCII_8BIT if enc == Encoding::US_ASCII
n = o.dup.force_encoding(enc)
end
expected << n
with_tmpdir {
Dir.mkdir(o)

View file

@ -38,11 +38,12 @@ void rb_w32_rewinddir(DIR *);
void rb_w32_closedir(DIR *);
char *rb_w32_ugetcwd(char *, int);
#define opendir(s) rb_w32_opendir((s))
#define opendir(s) rb_w32_uopendir((s))
#define readdir(d) rb_w32_readdir((d), 0)
#define telldir(d) rb_w32_telldir((d))
#define seekdir(d, l) rb_w32_seekdir((d), (l))
#define rewinddir(d) rb_w32_rewinddir((d))
#define closedir(d) rb_w32_closedir((d))
#define getcwd(b, s) rb_w32_ugetcwd(b, s)
#endif /* RUBY_WIN32_DIR_H */

View file

@ -347,7 +347,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
/* use filesystem encoding if expanding home dir */
path_encoding = rb_filesystem_encoding();
cp = path_cp = system_code_page();
cp = path_cp = code_page(path_encoding);
}
/* ignores dir since we are expanding home */

View file

@ -2270,7 +2270,7 @@ rb_w32_conv_from_wstr(const WCHAR *wstr, long *lenp, rb_encoding *enc)
long len;
char *ptr;
if (NIL_P(str)) return wstr_to_filecp(wstr, lenp);
if (NIL_P(str)) return wstr_to_utf8(wstr, lenp);
*lenp = len = RSTRING_LEN(str);
memcpy(ptr = malloc(len + 1), RSTRING_PTR(str), len);
ptr[len] = '\0';