Skip to content

Commit

Permalink
file.c: normalize cwd
Browse files Browse the repository at this point in the history
* file.c (append_fspath): normalize directory name to be appended
  on OS X.  [ruby-core:75957] [Ruby trunk Bug#12483]
  rails/rails#25303 (comment)

git-svn-id: svn+ssh://svn.ruby-lang.org/ruby/trunk@55385 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Jun 12, 2016
1 parent 9c038ee commit 6f1ce73
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
@@ -1,3 +1,9 @@
Sun Jun 12 09:24:34 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>

* file.c (append_fspath): normalize directory name to be appended
on OS X. [ruby-core:75957] [Ruby trunk Bug#12483]
https://github.com/rails/rails/issues/25303#issuecomment-224834804

Sat Jun 11 23:07:32 2016 NAKAMURA Usaku <usa@ruby-lang.org>

* lib/forwardable.rb (_delegator_method): get rid of a warning which
Expand Down
22 changes: 20 additions & 2 deletions file.c
Expand Up @@ -235,6 +235,7 @@ rb_str_encode_ospath(VALUE path)
}

#ifdef __APPLE__
# define NORMALIZE_UTF8PATH 1
static VALUE
rb_str_append_normalized_ospath(VALUE str, const char *ptr, long len)
{
Expand Down Expand Up @@ -334,6 +335,8 @@ ignored_char_p(const char *p, const char *e, rb_encoding *enc)
}
return 0;
}
#else
# define NORMALIZE_UTF8PATH 0
#endif

#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
Expand Down Expand Up @@ -3211,19 +3214,34 @@ rb_default_home_dir(VALUE result)
}

#ifndef _WIN32
static VALUE
ospath_new(const char *ptr, long len, rb_encoding *fsenc)
{
#if NORMALIZE_UTF8PATH
VALUE path = rb_str_normalize_ospath(ptr, len);
rb_enc_associate(path, fsenc);
return path;
#else
return rb_enc_str_new(ptr, len, fsenc);
#endif
}

static char *
append_fspath(VALUE result, VALUE fname, char *dir, rb_encoding **enc, rb_encoding *fsenc)
{
char *buf, *cwdp = dir;
VALUE dirname = Qnil;
size_t dirlen = strlen(dir), buflen = rb_str_capacity(result);

if (*enc != fsenc) {
rb_encoding *direnc = rb_enc_check(fname, dirname = rb_enc_str_new(dir, dirlen, fsenc));
if (NORMALIZE_UTF8PATH || *enc != fsenc) {
rb_encoding *direnc = rb_enc_check(fname, dirname = ospath_new(dir, dirlen, fsenc));
if (direnc != fsenc) {
dirname = rb_str_conv_enc(dirname, fsenc, direnc);
RSTRING_GETMEM(dirname, cwdp, dirlen);
}
else if (NORMALIZE_UTF8PATH) {
RSTRING_GETMEM(dirname, cwdp, dirlen);
}
*enc = direnc;
}
do {buflen *= 2;} while (dirlen > buflen);
Expand Down
18 changes: 18 additions & 0 deletions test/ruby/test_file_exhaustive.rb
Expand Up @@ -1094,6 +1094,24 @@ def test_expand_path_with_drive_letter
assert_equal('z:/bar/foo', File.expand_path('z:foo', '/bar'), bug10858)
end if DRIVE

def test_expand_path_compose
pp = Object.new.extend(Test::Unit::Assertions)
def pp.mu_pp(str) #:nodoc:
str.dump
end

Dir.mktmpdir do |dir|
Dir.chdir(dir) do
orig = %W"d\u{e9}tente x\u{304c 304e 3050 3052 3054}"
orig.each do |o|
Dir.mkdir(o)
n = Dir.chdir(o) {File.expand_path(".")}
pp.assert_equal(o, File.basename(n))
end
end
end
end

def test_basename
assert_equal(File.basename(regular_file).sub(/\.test$/, ""), File.basename(regular_file, ".test"))
assert_equal(File.basename(utf8_file).sub(/\.test$/, ""), File.basename(utf8_file, ".test"))
Expand Down

0 comments on commit 6f1ce73

Please sign in to comment.