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

file.c: use filesystem encoding

* file.c (rb_realpath_internal): use filesystem encoding if the
  argument is in ASCII encodings.
* win32/file.c (rb_readlink): needs the result encoding.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51738 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-09-02 07:58:24 +00:00
parent 6bbd9104cb
commit f9ac2106bf
4 changed files with 45 additions and 8 deletions

View file

@ -1,3 +1,10 @@
Wed Sep 2 16:58:21 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* file.c (rb_realpath_internal): use filesystem encoding if the
argument is in ASCII encodings.
* win32/file.c (rb_readlink): needs the result encoding.
Tue Sep 1 18:37:15 2015 Koichi Sasada <ko1@atdot.net> Tue Sep 1 18:37:15 2015 Koichi Sasada <ko1@atdot.net>
* test/thread/test_queue.rb: catch up last commit. * test/thread/test_queue.rb: catch up last commit.

22
file.c
View file

@ -2786,7 +2786,7 @@ rb_file_s_symlink(VALUE klass, VALUE from, VALUE to)
#endif #endif
#ifdef HAVE_READLINK #ifdef HAVE_READLINK
VALUE rb_readlink(VALUE path); VALUE rb_readlink(VALUE path, rb_encoding *enc);
/* /*
* call-seq: * call-seq:
@ -2802,12 +2802,12 @@ VALUE rb_readlink(VALUE path);
static VALUE static VALUE
rb_file_s_readlink(VALUE klass, VALUE path) rb_file_s_readlink(VALUE klass, VALUE path)
{ {
return rb_readlink(path); return rb_readlink(path, rb_filesystem_encoding());
} }
#ifndef _WIN32 #ifndef _WIN32
VALUE VALUE
rb_readlink(VALUE path) rb_readlink(VALUE path, rb_encoding *enc)
{ {
int size = 100; int size = 100;
ssize_t rv; ssize_t rv;
@ -2815,7 +2815,7 @@ rb_readlink(VALUE path)
FilePathValue(path); FilePathValue(path);
path = rb_str_encode_ospath(path); path = rb_str_encode_ospath(path);
v = rb_enc_str_new(0, size, rb_filesystem_encoding()); v = rb_enc_str_new(0, size, enc);
while ((rv = readlink(RSTRING_PTR(path), RSTRING_PTR(v), size)) == size while ((rv = readlink(RSTRING_PTR(path), RSTRING_PTR(v), size)) == size
#ifdef _AIX #ifdef _AIX
|| (rv < 0 && errno == ERANGE) /* quirky behavior of GPFS */ || (rv < 0 && errno == ERANGE) /* quirky behavior of GPFS */
@ -3810,7 +3810,7 @@ realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved, VALUE l
const char *link_prefix, *link_names; const char *link_prefix, *link_names;
long link_prefixlen; long link_prefixlen;
rb_hash_aset(loopcheck, testpath, ID2SYM(resolving)); rb_hash_aset(loopcheck, testpath, ID2SYM(resolving));
link = rb_readlink(testpath); link = rb_readlink(testpath, enc);
link_prefix = RSTRING_PTR(link); link_prefix = RSTRING_PTR(link);
link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link)); link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
link_prefixlen = link_names - link_prefix; link_prefixlen = link_names - link_prefix;
@ -3855,7 +3855,7 @@ rb_realpath_internal(VALUE basedir, VALUE path, int strict)
VALUE loopcheck; VALUE loopcheck;
volatile VALUE curdir = Qnil; volatile VALUE curdir = Qnil;
rb_encoding *enc; rb_encoding *enc, *origenc;
char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL; char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
char *ptr, *prefixptr = NULL, *pend; char *ptr, *prefixptr = NULL, *pend;
long len; long len;
@ -3907,6 +3907,13 @@ rb_realpath_internal(VALUE basedir, VALUE path, int strict)
} }
#endif #endif
origenc = enc;
switch (rb_enc_to_index(enc)) {
case ENCINDEX_ASCII:
case ENCINDEX_US_ASCII:
rb_enc_associate(resolved, rb_filesystem_encoding());
}
loopcheck = rb_hash_new(); loopcheck = rb_hash_new();
if (curdir_names) if (curdir_names)
realpath_rec(&prefixlen, &resolved, curdir_names, loopcheck, 1, 0); realpath_rec(&prefixlen, &resolved, curdir_names, loopcheck, 1, 0);
@ -3914,6 +3921,9 @@ rb_realpath_internal(VALUE basedir, VALUE path, int strict)
realpath_rec(&prefixlen, &resolved, basedir_names, loopcheck, 1, 0); realpath_rec(&prefixlen, &resolved, basedir_names, loopcheck, 1, 0);
realpath_rec(&prefixlen, &resolved, path_names, loopcheck, strict, 1); realpath_rec(&prefixlen, &resolved, path_names, loopcheck, strict, 1);
if (origenc != enc && rb_enc_str_asciionly_p(resolved))
rb_enc_associate(resolved, origenc);
OBJ_TAINT(resolved); OBJ_TAINT(resolved);
return resolved; return resolved;
} }

View file

@ -258,6 +258,26 @@ class TestFile < Test::Unit::TestCase
} }
end end
def test_realpath_encoding
fsenc = Encoding.find("filesystem")
nonascii = "\u{0391 0410 0531 10A0 05d0 2C00 3042}"
tst = "A"
nonascii.each_char {|c| tst << c.encode(fsenc) rescue nil}
Dir.mktmpdir('rubytest-realpath') {|tmpdir|
realdir = File.realpath(tmpdir)
open(File.join(tmpdir, tst), "w") {}
a = File.join(tmpdir, "a")
File.symlink(tst, a)
assert_equal(File.join(realdir, tst), File.realpath(a))
File.unlink(a)
tst = "A" + nonascii
open(File.join(tmpdir, tst), "w") {}
File.symlink(tst, a)
assert_equal(File.join(realdir, tst), File.realpath(a.encode("UTF-8")))
}
end
def test_realdirpath def test_realdirpath
Dir.mktmpdir('rubytest-realdirpath') {|tmpdir| Dir.mktmpdir('rubytest-realdirpath') {|tmpdir|
realdir = File.realpath(tmpdir) realdir = File.realpath(tmpdir)

View file

@ -660,7 +660,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
} }
VALUE VALUE
rb_readlink(VALUE path) rb_readlink(VALUE path, rb_encoding *resultenc)
{ {
DWORD len; DWORD len;
VALUE wtmp = 0, wpathbuf, str; VALUE wtmp = 0, wpathbuf, str;
@ -692,7 +692,7 @@ rb_readlink(VALUE path)
ALLOCV_END(wtmp); ALLOCV_END(wtmp);
rb_syserr_fail_path(rb_w32_map_errno(e), path); rb_syserr_fail_path(rb_w32_map_errno(e), path);
} }
enc = rb_filesystem_encoding(); enc = resultenc;
cp = path_cp = code_page(enc); cp = path_cp = code_page(enc);
if (cp == INVALID_CODE_PAGE) cp = CP_UTF8; if (cp == INVALID_CODE_PAGE) cp = CP_UTF8;
str = append_wstr(rb_enc_str_new(0, 0, enc), wbuf, len, cp, path_cp, enc); str = append_wstr(rb_enc_str_new(0, 0, enc), wbuf, len, cp, path_cp, enc);