Encoding of Dir#path

e$B0J2<$N$h$&$K!"e(BDir#path
e$B$GJV$C$F$/$k$b$N$N%(%s%3!<%G%#%s%0$,!"e(B
Dir.open e$B$KEO$7$?$b$N$H0[$J$k$N$OJQ$G$O$J$$$G$7$g$&$+!#e(B

% ./ruby -Ku -ve ’
dirname = “/tmp/\x91\x92\x93”
Dir.mkdir(dirname)
p [dirname, dirname.encoding]
d = Dir.open(dirname)
dirname2 = d.path
p [dirname2, dirname2.encoding]

ruby 1.9.0 (2008-07-29 revision 18257) [i686-linux]
["/tmp/\x91\x92\x93", #Encoding:UTF-8]
["/tmp/\x91\x92\x93", #Encoding:EUC-JP]

e$B$H$$$&$o$1$G0J2<$N$h$&$K$9$k$N$O$I$&$G$7$g$&!#e(B

Index: dir.c

— dir.c (revision 18254)
+++ dir.c (working copy)
@@ -290,16 +290,21 @@ VALUE rb_cDir;

struct dir_data {
DIR *dir;

  • char *path;
  • VALUE path;
    rb_encoding *extenc;
    };

static void
+mark_dir(struct dir_data *dir)
+{

  • rb_gc_mark(dir->path);
    +}

+static void
free_dir(struct dir_data *dir)
{
if (dir) {
if (dir->dir) closedir(dir->dir);

  • if (dir->path) xfree(dir->path);
    }
    xfree(dir);
    }
    @@ -310,10 +315,10 @@ static VALUE
    dir_s_alloc(VALUE klass)
    {
    struct dir_data *dirp;
  • VALUE obj = Data_Make_Struct(klass, struct dir_data, 0, free_dir,
    dirp);
  • VALUE obj = Data_Make_Struct(klass, struct dir_data, mark_dir,
    free_dir, dirp);

    dirp->dir = NULL;

  • dirp->path = NULL;
  • dirp->path = Qnil;
    dirp->extenc = NULL;

    return obj;
    @@ -358,9 +363,8 @@ dir_initialize(int argc, VALUE *argv, VA

    Data_Get_Struct(dir, struct dir_data, dp);
    if (dp->dir) closedir(dp->dir);

  • if (dp->path) xfree(dp->path);
    dp->dir = NULL;
  • dp->path = NULL;
  • dp->path = Qnil;
    dp->extenc = extencoding;
    dp->dir = opendir(RSTRING_PTR(dirname));
    if (dp->dir == NULL) {
    @@ -372,7 +376,7 @@ dir_initialize(int argc, VALUE *argv, VA
    rb_sys_fail(RSTRING_PTR(dirname));
    }
    }
  • dp->path = strdup(RSTRING_PTR(dirname));
  • dp->path = rb_str_dup_frozen(dirname);

    return dir;
    }
    @@ -392,7 +396,7 @@ static VALUE
    dir_s_open(int argc, VALUE *argv, VALUE klass)
    {
    struct dir_data *dp;

  • VALUE dir = Data_Make_Struct(klass, struct dir_data, 0, free_dir,
    dp);
  • VALUE dir = Data_Make_Struct(klass, struct dir_data, mark_dir,
    free_dir, dp);

    dir_initialize(argc, argv, dir);
    if (rb_block_given_p()) {
    @@ -441,9 +445,9 @@ dir_inspect(VALUE dir)
    struct dir_data *dirp;

    Data_Get_Struct(dir, struct dir_data, dirp);

  • if (dirp->path) {
  • if (!NIL_P(dirp->path)) {
    const char *c = rb_obj_classname(dir);
  • return rb_sprintf("#<%s:%s>", c, dirp->path);
  • return rb_sprintf("#<%s:%s>", c, RSTRING_PTR(dirp->path));
    }
    return rb_funcall(dir, rb_intern(“to_s”), 0, 0);
    }
    @@ -463,8 +467,8 @@ dir_path(VALUE dir)
    struct dir_data *dirp;

    Data_Get_Struct(dir, struct dir_data, dirp);

  • if (!dirp->path) return Qnil;
  • return dir_enc_str(rb_str_new2(dirp->path), dirp);
  • if (NIL_P(dirp->path)) return Qnil;
  • return rb_str_dup(dirp->path);
    }

/*

e$B@.@%$G$9!#e(B

At Tue, 29 Jul 2008 20:43:39 +0900,
Tanaka A. wrote:

p [dirname2, dirname2.encoding]

ruby 1.9.0 (2008-07-29 revision 18257) [i686-linux]
["/tmp/\x91\x92\x93", #Encoding:UTF-8]
["/tmp/\x91\x92\x93", #Encoding:EUC-JP]

e$B$H$$$&$o$1$G0J2<$N$h$&$K$9$k$N$O$I$&$G$7$g$&!#e(B

e$B$$$$$N$G$O$J$$$G$7$g$&$+!"%3%_%C%H$7$F$/$@$5$$!#e(B

e$B$H$3$m$G!"$3$Ne(B dir_data e$B$Ne(B path e$B$Ne(B encoding e$B$G!“e(B
dir_data e$B$Ne(B extenc
e$B$rBeMQ$G$-$=$&$K;W$($k$N$G$9$,!”$I$&$G$7$g$&$+!#e(B

Unix:
path: US-ASCII e$B0J30e(B ->
extenc e$B;XDj;~e(B:
e$BN><T$,0[$J$k>l9g$,LdBj$@$,!“e(Bforce_encoding(extenc)e$B$+$J$!e(B
extenc e$BL$;XDje(B: e$BLdBj$J$7e(B
path: US-ASCII ->
extenc e$B;XDj;~e(B: US-ASCII e$B0J30$N>l9g$Oe(B path e$B$re(B
force_encoding(extenc)
extenc e$BL$;XDje(B: path e$B$re(B
force_encoding(filesystem_encoding)
Windows (ANSI):
path, extenc e$B$H$b$K>o$Ke(B locale e$B8GDje(B
Windows (Unicode):
path e$B$re(B encode(extenc) e$B$K$”$o$;$ke(B

e$B$J$N$G!"$$$`$M$&$^$/$$$-$=$&$K8+$($k$N$G$9$,!#e(B

In article [email protected],
“NARUSE, Yui” [email protected] writes:

e$B0J2<$N$h$&$K!"e(BDir#path e$B$GJV$C$F$/$k$b$N$N%(%s%3!<%G%#%s%0$,!"e(B
Dir.open e$B$KEO$7$?$b$N$H0[$J$k$N$OJQ$G$O$J$$$G$7$g$&$+!#e(B

e$B$H$3$m$G!"$3$Ne(B dir_data e$B$Ne(B path e$B$Ne(B encoding e$B$G!“e(B
dir_data e$B$Ne(B extenc e$B$rBeMQ$G$-$=$&$K;W$($k$N$G$9$,!”$I$&$G$7$g$&$+!#e(B

Dir.open e$B$KEO$7$?$b$N$H$O0[$J$k$b$N$,e(B Dir#path e$B$GJV$C$F$/$ke(B
e$B$3$H$O$"$j$^$;$s$+e(B?

e$B@.@%$G$9!#e(B

At Wed, 30 Jul 2008 11:58:13 +0900,
Tanaka A. wrote:

Dir.open e$B$KEO$7$?$b$N$H$O0[$J$k$b$N$,e(B Dir#path e$B$GJV$C$F$/$ke(B
e$B$3$H$O$"$j$^$;$s$+e(B?

e$B$&!<$s!"3N$+$K$=$&$G$9$M!#e(B
e$BJL$K$7$F$*$$$?J}$,$$$$$+!#e(B