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
142 142
#define lseek(_f, _o, _w)	_lseeki64(_f, _o, _w)
143 143
#define pipe(p)			rb_w32_pipe(p)
144
#define open			rb_w32_open
144
#define open			rb_w32_uopen
145 145
#define close(h)		rb_w32_close(h)
146 146
#define fclose(f)		rb_w32_fclose(f)
147 147
#define read(f, b, s)		rb_w32_read(f, b, s)
......
154 154
#define fstati64(fd,st) 	rb_w32_fstati64(fd,st)
155 155
#undef execv
156
#define execv(path,argv)	rb_w32_aspawn(P_OVERLAY,path,argv)
156
#define execv(path,argv)	rb_w32_uaspawn(P_OVERLAY,path,argv)
157 157
#undef isatty
158 158
#define isatty(h)		rb_w32_isatty(h)
159 159
#undef mkdir
160
#define mkdir(p, m)		rb_w32_mkdir(p, m)
160
#define mkdir(p, m)		rb_w32_umkdir(p, m)
161 161
#undef rmdir
162
#define rmdir(p)		rb_w32_rmdir(p)
162
#define rmdir(p)		rb_w32_urmdir(p)
163 163
#undef unlink
164
#define unlink(p)		rb_w32_unlink(p)
164
#define unlink(p)		rb_w32_uunlink(p)
165 165
#endif /* RUBY_EXPORT */
166 166
#if SIZEOF_OFF_T == 8
......
173 173
#if !defined(_MSC_VER) || RUBY_MSVCRT_VERSION < 80
174 174
#define stati64 _stati64
175 175
#ifndef _stati64
176
#define _stati64(path, st) rb_w32_stati64(path, st)
176
#define _stati64(path, st) rb_w32_ustati64(path, st)
177 177
#endif
178 178
#else
179 179
#define stati64 _stat64
180
#define _stat64(path, st) rb_w32_stati64(path, st)
180
#define _stat64(path, st) rb_w32_ustati64(path, st)
181 181
#endif
182 182
#else
183
#define stat(path,st)		rb_w32_stat(path,st)
184
#define fstat(fd,st)		rb_w32_fstat(fd,st)
183
#define stat(path,st)		rb_w32_ustat(path,st)
184
#define fstat(fd,st)		rb_w32_ufstat(fd,st)
185 185
extern int rb_w32_stat(const char *, struct stat *);
186 186
extern int rb_w32_fstat(int, struct stat *);
187 187
#endif
188
#define lstat(path,st)		rb_w32_lstati64(path,st)
189
#define access(path,mode)	rb_w32_access(path,mode)
188
#define lstat(path,st)		rb_w32_ulstati64(path,st)
189
#define access(path,mode)	rb_w32_uaccess(path,mode)
190 190
#define strcasecmp		_stricmp
191 191
#define strncasecmp		_strnicmp
......
698 698
#define getenv(n)		rb_w32_getenv(n)
699 699
#undef rename
700
#define rename(o, n)		rb_w32_rename(o, n)
700
#define rename(o, n)		rb_w32_urename(o, n)
701 701
#undef times
702 702
#define times(t)		rb_w32_times(t)
localeinit.c
100 100
    int idx;
101 101
#if defined NO_LOCALE_CHARMAP
102 102
# error NO_LOCALE_CHARMAP defined
103
#elif defined _WIN32
104
    char cp[SIZEOF_CP_NAME];
105
    CP_FORMAT(cp, AreFileApisANSI() ? GetACP() : GetOEMCP());
106
    idx = rb_enc_find_index(cp);
107
    if (idx < 0) idx = ENCINDEX_ASCII;
108
#elif defined __CYGWIN__
103
#elif defined _WIN32 || defined __CYGWIN__
109 104
    idx = ENCINDEX_UTF_8;
110 105
#else
111 106
    idx = rb_enc_to_index(rb_default_external_encoding());
test/ruby/test_dir_m17n.rb
16 16
      assert_separately(["-E#{encoding}"], <<-EOS, :chdir=>dir)
17 17
        filename = #{code}.chr('UTF-8').force_encoding("#{encoding}")
18 18
        File.open(filename, "w") {}
19
        opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
19
        opts = {}
20 20
        ents = Dir.entries(".", opts)
21
        if /mswin|mingw/ =~ RUBY_PLATFORM
22
          filename = filename.encode("UTF-8")
23
        end
21 24
        assert_include(ents, filename)
22 25
      EOS
23 26
      return if /cygwin/ =~ RUBY_PLATFORM
24 27
      assert_separately(%w[-EASCII-8BIT], <<-EOS, :chdir=>dir)
25 28
        filename = #{code}.chr('UTF-8').force_encoding("ASCII-8BIT")
26
        opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
29
        opts = {}
27 30
        ents = Dir.entries(".", opts)
28
        expected_filename = #{code}.chr('UTF-8').encode(Encoding.find("filesystem")) rescue expected_filename = "?"
29
        expected_filename = expected_filename.force_encoding("ASCII-8BIT")
30 31
        if /mswin|mingw/ =~ RUBY_PLATFORM
31
          case
32
          when ents.include?(filename)
33
          when ents.include?(expected_filename)
34
            filename = expected_filename
35
          else
36
            ents = Dir.entries(".", {:encoding => Encoding.find("filesystem")})
37
            filename = expected_filename
38
          end
32
          filename.force_encoding("UTF-8")
39 33
        end
40 34
        assert_include(ents, filename)
41 35
      EOS
......
196 190
      assert_separately(%w[-EEUC-JP], <<-'EOS', :chdir=>d)
197 191
        filename = "\xA4\xA2".force_encoding("euc-jp")
198 192
        File.open(filename, "w") {}
199
        opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
193
        opts = {}
200 194
        ents = Dir.entries(".", opts)
201 195
        if /darwin/ =~ RUBY_PLATFORM
202 196
          filename = filename.encode("utf-8").force_encoding("euc-jp")
197
        elsif /mswin|mingw/ =~ RUBY_PLATFORM
198
          filename = filename.encode("utf-8")
203 199
        end
204 200
        assert_include(ents, filename)
205 201
      EOS
206 202
      assert_separately(%w[-EASCII-8BIT], <<-'EOS', :chdir=>d)
207 203
        filename = "\xA4\xA2".force_encoding('ASCII-8BIT')
208
        win_expected_filename = filename.encode(Encoding.find("filesystem"), "euc-jp") rescue "?"
209
        opts = {:encoding => Encoding.default_external} if /mswin|mingw/ =~ RUBY_PLATFORM
204
        opts = {}
210 205
        ents = Dir.entries(".", opts)
211 206
        unless ents.include?(filename)
212 207
          case RUBY_PLATFORM
213 208
          when /darwin/
214 209
            filename = filename.encode("utf-8", "euc-jp").b
215 210
          when /mswin|mingw/
216
            if ents.include?(win_expected_filename.b)
217
              ents = Dir.entries(".", {:encoding => Encoding.find("filesystem")})
218
              filename = win_expected_filename
219
            end
211
            filename = filename.encode("utf-8", "euc-jp")
220 212
          end
221 213
        end
222 214
        assert_include(ents, filename)
......
402 394
    with_tmpdir {|d|
403 395
      orig = %W"d\u{e9}tente x\u{304c 304e 3050 3052 3054}"
404 396
      orig.each {|n| open(n, "w") {}}
405
      if /mswin|mingw/ =~ RUBY_PLATFORM
406
        opts = {:encoding => Encoding.default_external}
407
        orig.map! {|o| o.encode("filesystem") rescue o.tr("^a-z", "?")}
408
      else
409
        enc = Encoding.find("filesystem")
410
        enc = Encoding::ASCII_8BIT if enc == Encoding::US_ASCII
411
        orig.each {|o| o.force_encoding(enc) }
412
      end
397
      enc = Encoding.find("filesystem")
398
      enc = Encoding::ASCII_8BIT if enc == Encoding::US_ASCII
399
      orig.each {|o| o.force_encoding(enc) }
400
      opts = {}
413 401
      ents = Dir.entries(".", opts).reject {|n| /\A\./ =~ n}
414 402
      ents.sort!
415 403
      PP.assert_equal(orig, ents, bug7267)
......
421 409
    expected = []
422 410
    results = []
423 411
    orig.each {|o|
424
      if /mswin|mingw/ =~ RUBY_PLATFORM
425
        n = (o.encode("filesystem") rescue next)
426
      else
427
        enc = Encoding.find("filesystem")
428
        enc = Encoding::ASCII_8BIT if enc == Encoding::US_ASCII
429
        n = o.dup.force_encoding(enc)
430
      end
412
      enc = Encoding.find("filesystem")
413
      enc = Encoding::ASCII_8BIT if enc == Encoding::US_ASCII
414
      n = o.dup.force_encoding(enc)
431 415
      expected << n
432 416
      with_tmpdir {
433 417
        Dir.mkdir(o)
test/ruby/test_process.rb
432 432
      system(*PWD, :chdir => "テスト", :out => "open_chdir_テスト")
433 433
      assert_file.exist?("open_chdir_テスト")
434 434
      assert_file.not_exist?("テスト/open_chdir_テスト")
435
      assert_equal("#{d}/テスト", File.read("open_chdir_テスト").chomp.encode(__ENCODING__))
435
      assert_equal("#{d}/テスト", File.read("open_chdir_テスト", encoding: __ENCODING__).chomp)
436 436
    }
437 437
  end if windows? || Encoding.find('locale') == Encoding::UTF_8
win32/dir.h
34 34
void           rb_w32_rewinddir(DIR *);
35 35
void           rb_w32_closedir(DIR *);
36
#define opendir(s)   rb_w32_opendir((s))
36
#define opendir(s)   rb_w32_uopendir((s))
37 37
#define readdir(d)   rb_w32_readdir((d), 0)
38 38
#define telldir(d)   rb_w32_telldir((d))
39 39
#define seekdir(d, l)   rb_w32_seekdir((d), (l))
win32/file.c
385 385
	if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
386 386
	    /* use filesystem encoding if expanding home dir */
387 387
	    path_encoding = rb_filesystem_encoding();
388
	    cp = path_cp = system_code_page();
388
	    cp = path_cp = code_page(path_encoding);
389 389
	}
390 390
	/* ignores dir since we are expanding home */
win32/win32.c
2139 2139
    long len;
2140 2140
    char *ptr;
2141
    if (NIL_P(str)) return wstr_to_filecp(wstr, lenp);
2141
    if (NIL_P(str)) return wstr_to_utf8(wstr, lenp);
2142 2142
    *lenp = len = RSTRING_LEN(str);
2143 2143
    memcpy(ptr = malloc(len + 1), RSTRING_PTR(str), len);
2144 2144
    ptr[len] = '\0';
......
4645 4645
    translate_char(p, '\\', '/', CP_UTF8);
4646
    rp = rb_str_conv_enc_opts(rb_utf8_str_new_cstr(p), NULL, rb_filesystem_encoding(), ECONV_UNDEF_REPLACE, Qnil);
4647
    if (!buffer) {
4648
      free(p);
4649
      p = strdup(RSTRING_PTR(rp));
4650
    } else {
4651
      if (RSTRING_LEN(rp) >= size) {
4652
        errno = ERANGE;
4653
        return NULL;
4654
      }
4655
      strncpy(p, RSTRING_PTR(rp), size - 1);
4656
      p[size - 1] = '\0';
4657
    }
4658

  
4659 4646
    return p;
4660 4647
}
4661
-