Project

General

Profile

Feature #12654 » 0001-On-Windows-use-UTF-8-as-filesystem-encoding.patch

davispuh (Dāvis Mosāns), 08/04/2016 02:22 AM

View differences:

include/ruby/win32.h
#define lseek(_f, _o, _w) _lseeki64(_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)
......
#define fstati64(fd,st) rb_w32_fstati64(fd,st)
#undef execv
#define execv(path,argv) rb_w32_aspawn(P_OVERLAY,path,argv)
#define execv(path,argv) rb_w32_uaspawn(P_OVERLAY,path,argv)
#undef isatty
#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 */
#if SIZEOF_OFF_T == 8
......
#if !defined(_MSC_VER) || RUBY_MSVCRT_VERSION < 80
#define stati64 _stati64
#ifndef _stati64
#define _stati64(path, st) rb_w32_stati64(path, st)
#define _stati64(path, st) rb_w32_ustati64(path, st)
#endif
#else
#define stati64 _stat64
#define _stat64(path, st) rb_w32_stati64(path, st)
#define _stat64(path, st) rb_w32_ustati64(path, st)
#endif
#else
#define stat(path,st) rb_w32_stat(path,st)
#define fstat(fd,st) rb_w32_fstat(fd,st)
#define stat(path,st) rb_w32_ustat(path,st)
#define fstat(fd,st) rb_w32_ufstat(fd,st)
extern int rb_w32_stat(const char *, struct stat *);
extern int rb_w32_fstat(int, struct stat *);
#endif
#define lstat(path,st) rb_w32_lstati64(path,st)
#define access(path,mode) rb_w32_access(path,mode)
#define lstat(path,st) rb_w32_ulstati64(path,st)
#define access(path,mode) rb_w32_uaccess(path,mode)
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
......
#define getenv(n) rb_w32_getenv(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)
localeinit.c
int idx;
#if defined NO_LOCALE_CHARMAP
# error NO_LOCALE_CHARMAP defined
#elif defined _WIN32
char cp[SIZEOF_CP_NAME];
CP_FORMAT(cp, AreFileApisANSI() ? GetACP() : GetOEMCP());
idx = rb_enc_find_index(cp);
if (idx < 0) idx = ENCINDEX_ASCII;
#elif defined __CYGWIN__
#elif defined _WIN32 || defined __CYGWIN__
idx = ENCINDEX_UTF_8;
#else
idx = rb_enc_to_index(rb_default_external_encoding());
test/ruby/test_dir_m17n.rb
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
opts = {}
ents = Dir.entries(".", opts)
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
opts = {}
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")
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
......
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
opts = {}
ents = Dir.entries(".", opts)
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
opts = {}
ents = Dir.entries(".", opts)
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)
......
with_tmpdir {|d|
orig = %W"d\u{e9}tente x\u{304c 304e 3050 3052 3054}"
orig.each {|n| open(n, "w") {}}
if /mswin|mingw/ =~ RUBY_PLATFORM
opts = {:encoding => Encoding.default_external}
orig.map! {|o| o.encode("filesystem") rescue o.tr("^a-z", "?")}
else
enc = Encoding.find("filesystem")
enc = Encoding::ASCII_8BIT if enc == Encoding::US_ASCII
orig.each {|o| o.force_encoding(enc) }
end
enc = Encoding.find("filesystem")
enc = Encoding::ASCII_8BIT if enc == Encoding::US_ASCII
orig.each {|o| o.force_encoding(enc) }
opts = {}
ents = Dir.entries(".", opts).reject {|n| /\A\./ =~ n}
ents.sort!
PP.assert_equal(orig, ents, bug7267)
......
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
enc = Encoding.find("filesystem")
enc = Encoding::ASCII_8BIT if enc == Encoding::US_ASCII
n = o.dup.force_encoding(enc)
expected << n
with_tmpdir {
Dir.mkdir(o)
test/ruby/test_process.rb
system(*PWD, :chdir => "テスト", :out => "open_chdir_テスト")
assert_file.exist?("open_chdir_テスト")
assert_file.not_exist?("テスト/open_chdir_テスト")
assert_equal("#{d}/テスト", File.read("open_chdir_テスト").chomp.encode(__ENCODING__))
assert_equal("#{d}/テスト", File.read("open_chdir_テスト", encoding: __ENCODING__).chomp)
}
end if windows? || Encoding.find('locale') == Encoding::UTF_8
win32/dir.h
void rb_w32_rewinddir(DIR *);
void rb_w32_closedir(DIR *);
#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))
win32/file.c
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 */
win32/win32.c
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';
......
translate_char(p, '\\', '/', CP_UTF8);
rp = rb_str_conv_enc_opts(rb_utf8_str_new_cstr(p), NULL, rb_filesystem_encoding(), ECONV_UNDEF_REPLACE, Qnil);
if (!buffer) {
free(p);
p = strdup(RSTRING_PTR(rp));
} else {
if (RSTRING_LEN(rp) >= size) {
errno = ERANGE;
return NULL;
}
strncpy(p, RSTRING_PTR(rp), size - 1);
p[size - 1] = '\0';
}
return p;
}
    (1-1/1)