Issue #5297 has been updated by Luis Lavena.

Status changed from Open to Closed

This has been solved already associated to another bug report.
----------------------------------------
Bug #5297: Either File.expand_path or File.join is corrupting string encoding
http://redmine.ruby-lang.org/issues/5297

Author: Luis Lavena
Status: Closed
Priority: Normal
Assignee: Usaku NAKAMURA
Category: core
Target version: 1.9.x
ruby -v: ruby 1.9.4dev (2011-09-07 trunk 33212) [i386-mingw32]


Hello,

While working on some API improvements for Windows, found the following issue:

https://gist.github.com/1202366

<pre>
V:\f坦単竪>ruby -v
ruby 1.9.4dev (2011-09-07 trunk 33212) [i386-mingw32]

V:\f坦単竪>chcp 1252
Active code page: 1252

V:\f坦単竪>ruby -e "puts Encoding.default_external"
Windows-1252

V:\f坦単竪>irb
irb(main):001:0> a = File.expand_path "."
=> "V:/f坦単竪"
irb(main):002:0> a.encoding
=> #<Encoding:Windows-1252>
irb(main):003:0> b = Dir.glob("../*").first
=> "../f坦単竪"
irb(main):004:0> b.encoding
=> #<Encoding:Windows-1252>
irb(main):005:0> File.expand_path b
=> "V:/f坦単竪"
irb(main):006:0> c = File.expand_path b
=> "V:/f坦単竪"
irb(main):007:0> c.encoding
=> #<Encoding:Windows-1252>
irb(main):008:0> d = File.join(a, "foo")
=> "V:/f\xF3\xF1\xE8/foo"
irb(main):009:0> d.encoding
=> #<Encoding:ASCII-8BIT>                          # <= FUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
irb(main):010:0> e = "#{a}/foo"
=> "V:/f坦単竪/foo"
irb(main):011:0> e.encoding
=> #<Encoding:Windows-1252>
irb(main):012:0> File.open(d, "w+") { |f| f.puts "hi" }
Errno::ENOENT: No such file or directory - V:/f坦単竪/foo       # <= W.T.F.????
        from (irb):12:in `initialize'
        from (irb):12:in `open'
        from (irb):12
        from C:/Users/Luis/Tools/Ruby/ruby-head-i386-mingw32/bin/irb:12:in `<main>'
irb(main):013:0> File.open(e, "w+") { |f| f.puts "hi" }
Errno::ENOENT: No such file or directory - V:/f坦単竪/foo       # <= W.T.F. * 20!
        from (irb):13:in `initialize'
        from (irb):13:in `open'
        from (irb):13
        from C:/Users/Luis/Tools/Ruby/ruby-head-i386-mingw32/bin/irb:12:in `<main>'
irb(main):014:0>
</pre>

It is not clear why while File.expand_path worked, File.join broke but string interpolation didn't.

Even worse is that File.open failed.

I'm working on a replacement function for expand_path that rely on MultiByteToWideChar + GetFullPathNameW + WideCharToMultiByte and then uses rb_filesystem_str_new_cstr to return the string.

The funny fact is that replacement work properly:

<pre>
C:\Users\Luis\Projects\oss\me\fenix>ripl -Ilib
>> require "fenix"
=> true
>> Dir.chdir "V:"
=> 0
>> Dir.pwd
=> "V:/f坦単竪"
>> c = Fenix::File.expand_path "."
=> "V:/f坦単竪"
>> c.encoding
=> #<Encoding:Windows-1252>
>> File.join(c, "foo").encoding
=> #<Encoding:Windows-1252>
>> d = "#{c}/foo"
=> "V:/f坦単竪/foo"
>> d.encoding
=> #<Encoding:Windows-1252>
>> File.open(d, "w") { |f| f.puts "hi" }
=> nil
</pre>



-- 
http://redmine.ruby-lang.org