[前][次][番号順一覧][スレッド一覧]

ruby-changes:27645

From: luislavena <ko1@a...>
Date: Sun, 10 Mar 2013 23:39:19 +0900 (JST)
Subject: [ruby-changes:27645] luislavena:r39697 (trunk): Expand home directory when used in dir_string

luislavena	2013-03-10 23:39:09 +0900 (Sun, 10 Mar 2013)

  New Revision: 39697

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39697

  Log:
    Expand home directory when used in dir_string
    
    * win32/file.c (rb_file_expand_path_internal):  Expand home directory when
      used as second parameter (dir_string).  [ruby-core:53168] [Bug #8034]
    * test/ruby/test_file_exhaustive.rb: add test to verify.

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_file_exhaustive.rb
    trunk/win32/file.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39696)
+++ ChangeLog	(revision 39697)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Mar 10 23:38:15 2013  Luis Lavena  <luislavena@g...>
+
+	* win32/file.c (rb_file_expand_path_internal):  Expand home directory when
+	  used as second parameter (dir_string).  [ruby-core:53168] [Bug #8034]
+	* test/ruby/test_file_exhaustive.rb: add test to verify.
+
 Sun Mar 10 23:27:54 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/rubygems/ext/ext_conf_builder.rb (Gem::Ext::ExtConfBuilder.build):
Index: win32/file.c
===================================================================
--- win32/file.c	(revision 39696)
+++ win32/file.c	(revision 39697)
@@ -323,7 +323,8 @@ rb_file_expand_path_internal(VALUE fname https://github.com/ruby/ruby/blob/trunk/win32/file.c#L323
     size_t size = 0, wpath_len = 0, wdir_len = 0, whome_len = 0;
     size_t buffer_len = 0;
     char *fullpath = NULL;
-    wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL, *wdir = NULL;
+    wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
+    wchar_t *wdir = NULL, *wdir_pos = NULL;
     wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
     UINT path_cp, cp;
     VALUE path = fname, dir = dname;
@@ -442,9 +443,38 @@ rb_file_expand_path_internal(VALUE fname https://github.com/ruby/ruby/blob/trunk/win32/file.c#L443
 	}
 
 	/* convert char * to wchar_t */
-	convert_mb_to_wchar(dir, &wdir, NULL, &wdir_len, cp);
+	convert_mb_to_wchar(dir, &wdir, &wdir_pos, &wdir_len, cp);
 
-	if (wdir_len >= 2 && wdir[1] == L':') {
+	if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L'~' &&
+	    (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
+	    /* tainted if expanding '~' */
+	    tainted = 1;
+
+	    whome = home_dir();
+	    if (whome == NULL) {
+		xfree(wpath);
+		xfree(wdir);
+		rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
+	    }
+	    whome_len = wcslen(whome);
+
+	    if (PathIsRelativeW(whome) && !(whome_len >= 2 && IS_DIR_UNC_P(whome))) {
+		xfree(wpath);
+		xfree(wdir);
+		rb_raise(rb_eArgError, "non-absolute home");
+	    }
+
+	    /* exclude ~ from the result */
+	    wdir_pos++;
+	    wdir_len--;
+
+	    /* exclude separator if present */
+	    if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
+		wdir_pos++;
+		wdir_len--;
+	    }
+	}
+	else if (wdir_len >= 2 && wdir[1] == L':') {
 	    dir_drive = wdir[0];
 	    if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
 		wdir_len = 2;
@@ -515,7 +545,7 @@ rb_file_expand_path_internal(VALUE fname https://github.com/ruby/ruby/blob/trunk/win32/file.c#L545
 	if (!tainted && OBJ_TAINTED(dir))
 	    tainted = 1;
 
-	wcsncpy(buffer_pos, wdir, wdir_len);
+	wcsncpy(buffer_pos, wdir_pos, wdir_len);
 	buffer_pos += wdir_len;
     }
 
Index: test/ruby/test_file_exhaustive.rb
===================================================================
--- test/ruby/test_file_exhaustive.rb	(revision 39696)
+++ test/ruby/test_file_exhaustive.rb	(revision 39697)
@@ -502,6 +502,18 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L502
     end
   end
 
+  def test_expand_path_home_dir_string
+    home = ENV["HOME"]
+    new_home = "#{DRIVE}/UserHome"
+    ENV["HOME"] = new_home
+    bug8034 = "[ruby-core:53168]"
+
+    assert_equal File.join(new_home, "foo"), File.expand_path("foo", "~")
+    assert_equal File.join(new_home, "bar", "foo"), File.expand_path("foo", "~/bar")
+  ensure
+    ENV["HOME"] = home
+  end if DRIVE
+
   def test_expand_path_remove_trailing_alternative_data
     assert_equal File.join(@rootdir, "aaa"), File.expand_path("#{@rootdir}/aaa::$DATA")
     assert_equal File.join(@rootdir, "aa:a"), File.expand_path("#{@rootdir}/aa:a:$DATA")

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]