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

ruby-changes:46925

From: nobu <ko1@a...>
Date: Thu, 8 Jun 2017 10:58:48 +0900 (JST)
Subject: [ruby-changes:46925] nobu:r59040 (trunk): file.c: realpath in OS path encoding

nobu	2017-06-08 10:58:44 +0900 (Thu, 08 Jun 2017)

  New Revision: 59040

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59040

  Log:
    file.c: realpath in OS path encoding
    
    * dir.c (rb_dir_getwd_ospath): return cwd path in the OS path
      encoding.
    
    * file.c (rb_realpath_internal): work in the OS path encoding

  Modified files:
    trunk/dir.c
    trunk/file.c
    trunk/internal.h
Index: dir.c
===================================================================
--- dir.c	(revision 59039)
+++ dir.c	(revision 59040)
@@ -1082,37 +1082,52 @@ dir_s_chdir(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/dir.c#L1082
 }
 
 VALUE
-rb_dir_getwd(void)
+rb_dir_getwd_ospath(void)
 {
     char *path;
     VALUE cwd;
-    rb_encoding *fs = rb_filesystem_encoding();
-    int fsenc = rb_enc_to_index(fs);
     VALUE path_guard;
 
 #undef RUBY_UNTYPED_DATA_WARNING
 #define RUBY_UNTYPED_DATA_WARNING 0
-    if (fsenc == ENCINDEX_US_ASCII) fsenc = ENCINDEX_ASCII;
     path_guard = Data_Wrap_Struct((VALUE)0, NULL, RUBY_DEFAULT_FREE, NULL);
     path = my_getcwd();
     DATA_PTR(path_guard) = path;
 #ifdef _WIN32
-    cwd = rb_str_conv_enc(rb_utf8_str_new_cstr(path), NULL, fs);
-#else
-#ifdef __APPLE__
+    cwd = rb_utf8_str_new_cstr(path);
+    OBJ_TAINT(cwd);
+#elif defined __APPLE__
     cwd = rb_str_normalize_ospath(path, strlen(path));
     OBJ_TAINT(cwd);
 #else
     cwd = rb_tainted_str_new2(path);
 #endif
-    rb_enc_associate_index(cwd, fsenc);
-#endif
     DATA_PTR(path_guard) = 0;
 
     xfree(path);
     return cwd;
 }
 
+VALUE
+rb_dir_getwd(void)
+{
+    rb_encoding *fs = rb_filesystem_encoding();
+    int fsenc = rb_enc_to_index(fs);
+    VALUE cwd = rb_dir_getwd_ospath();
+
+    switch (fsenc) {
+      case ENCINDEX_US_ASCII:
+	fsenc = ENCINDEX_ASCII;
+      case ENCINDEX_ASCII:
+	break;
+#if defined _WIN32 || defined __APPLE__
+      default:
+	return rb_str_conv_enc(cwd, NULL, fs);
+#endif
+    }
+    return rb_enc_associate_index(cwd, fsenc);
+}
+
 /*
  *  call-seq:
  *     Dir.getwd -> string
Index: file.c
===================================================================
--- file.c	(revision 59039)
+++ file.c	(revision 59040)
@@ -127,6 +127,14 @@ int flock(int, int); https://github.com/ruby/ruby/blob/trunk/file.c#L127
 #define STAT(p, s)	stat((p), (s))
 #endif
 
+#if defined _WIN32 || defined __APPLE__
+# define USE_OSPATH 1
+# define TO_OSPATH(str) rb_str_encode_ospath(str)
+#else
+# define USE_OSPATH 0
+# define TO_OSPATH(str) (str)
+#endif
+
 VALUE rb_cFile;
 VALUE rb_mFileTest;
 VALUE rb_cStat;
@@ -222,7 +230,7 @@ rb_get_path(VALUE obj) https://github.com/ruby/ruby/blob/trunk/file.c#L230
 VALUE
 rb_str_encode_ospath(VALUE path)
 {
-#if defined _WIN32 || defined __APPLE__
+#if USE_OSPATH
     int encidx = ENCODING_GET(path);
 #ifdef _WIN32
     if (encidx == ENCINDEX_ASCII) {
@@ -3811,11 +3819,10 @@ realpath_rec(long *prefixlenp, VALUE *re https://github.com/ruby/ruby/blob/trunk/file.c#L3819
             else {
                 struct stat sbuf;
                 int ret;
-                VALUE testpath2 = rb_str_encode_ospath(testpath);
 #ifdef __native_client__
-                ret = stat(RSTRING_PTR(testpath2), &sbuf);
+                ret = stat(RSTRING_PTR(testpath), &sbuf);
 #else
-                ret = lstat(RSTRING_PTR(testpath2), &sbuf);
+                ret = lstat(RSTRING_PTR(testpath), &sbuf);
 #endif
                 if (ret == -1) {
 		    int e = errno;
@@ -3890,9 +3897,12 @@ rb_realpath_internal(VALUE basedir, VALU https://github.com/ruby/ruby/blob/trunk/file.c#L3897
 
     if (!NIL_P(basedir)) {
         FilePathValue(basedir);
-        basedir = rb_str_dup_frozen(basedir);
+        basedir = TO_OSPATH(rb_str_dup_frozen(basedir));
     }
 
+    enc = rb_enc_get(unresolved_path);
+    origenc = enc;
+    unresolved_path = TO_OSPATH(unresolved_path);
     RSTRING_GETMEM(unresolved_path, ptr, len);
     path_names = skipprefixroot(ptr, ptr + len, rb_enc_get(unresolved_path));
     if (ptr != path_names) {
@@ -3909,7 +3919,7 @@ rb_realpath_internal(VALUE basedir, VALU https://github.com/ruby/ruby/blob/trunk/file.c#L3919
         }
     }
 
-    curdir = rb_dir_getwd();
+    curdir = rb_dir_getwd_ospath();
     RSTRING_GETMEM(curdir, ptr, len);
     curdir_names = skipprefixroot(ptr, ptr + len, rb_enc_get(curdir));
     resolved = rb_str_subseq(curdir, 0, curdir_names - ptr);
@@ -3917,7 +3927,6 @@ rb_realpath_internal(VALUE basedir, VALU https://github.com/ruby/ruby/blob/trunk/file.c#L3927
   root_found:
     RSTRING_GETMEM(resolved, prefixptr, prefixlen);
     pend = prefixptr + prefixlen;
-    enc = rb_enc_get(resolved);
     ptr = chompdirsep(prefixptr, pend, enc);
     if (ptr < pend) {
         prefixlen = ++ptr - prefixptr;
@@ -3932,7 +3941,6 @@ rb_realpath_internal(VALUE basedir, VALU https://github.com/ruby/ruby/blob/trunk/file.c#L3941
     }
 #endif
 
-    origenc = enc;
     switch (rb_enc_to_index(enc)) {
       case ENCINDEX_ASCII:
       case ENCINDEX_US_ASCII:
@@ -3946,8 +3954,14 @@ rb_realpath_internal(VALUE basedir, VALU https://github.com/ruby/ruby/blob/trunk/file.c#L3954
         realpath_rec(&prefixlen, &resolved, basedir_names, loopcheck, 1, 0);
     realpath_rec(&prefixlen, &resolved, path_names, loopcheck, strict, 1);
 
-    if (origenc != enc && rb_enc_str_asciionly_p(resolved))
-	rb_enc_associate(resolved, origenc);
+    if (origenc != rb_enc_get(resolved)) {
+	if (rb_enc_str_asciionly_p(resolved)) {
+	    rb_enc_associate(resolved, origenc);
+	}
+	else {
+	    resolved = rb_str_conv_enc(resolved, NULL, origenc);
+	}
+    }
 
     OBJ_TAINT(resolved);
     return resolved;
Index: internal.h
===================================================================
--- internal.h	(revision 59039)
+++ internal.h	(revision 59040)
@@ -1088,6 +1088,9 @@ void ruby_register_rollback_func_for_ens https://github.com/ruby/ruby/blob/trunk/internal.h#L1088
 /* debug.c */
 PRINTF_ARGS(void ruby_debug_printf(const char*, ...), 1, 2);
 
+/* dir.c */
+VALUE rb_dir_getwd_ospath(void);
+
 /* dmyext.c */
 void Init_enc(void);
 void Init_ext(void);

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

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