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

ruby-changes:34159

From: akr <ko1@a...>
Date: Fri, 30 May 2014 07:28:56 +0900 (JST)
Subject: [ruby-changes:34159] akr:r46240 (trunk): * ext/-test-/dir: Dir#fileno implemented.

akr	2014-05-30 07:28:47 +0900 (Fri, 30 May 2014)

  New Revision: 46240

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

  Log:
    * ext/-test-/dir: Dir#fileno implemented.
    
    * test/lib/minitest/unit.rb (find_fds): Don't return the fd used to
      scan /proc/$$/fd.

  Added directories:
    trunk/ext/-test-/dir/
  Added files:
    trunk/ext/-test-/dir/extconf.rb
    trunk/ext/-test-/dir/fileno.c
    trunk/ext/-test-/dir/init.c
  Modified files:
    trunk/ChangeLog
    trunk/test/lib/minitest/unit.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46239)
+++ ChangeLog	(revision 46240)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri May 30 07:25:46 2014  Tanaka Akira  <akr@f...>
+
+	* ext/-test-/dir: Dir#fileno implemented.
+
+	* test/lib/minitest/unit.rb (find_fds): Don't return the fd used to
+	  scan /proc/$$/fd.
+
 Fri May 30 04:48:00 2014  Eric Wong  <e@8...>
 
 	* parse.y (rb_gc_mark_parser): remove, empty since r8758
Index: ext/-test-/dir/init.c
===================================================================
--- ext/-test-/dir/init.c	(revision 0)
+++ ext/-test-/dir/init.c	(revision 46240)
@@ -0,0 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/dir/init.c#L1
+#include "ruby.h"
+
+#define init(n) {void Init_##n(VALUE klass); Init_##n(klass);}
+
+void
+Init_dir(void)
+{
+    VALUE mBug = rb_define_module("Bug");
+    VALUE klass = rb_define_class_under(mBug, "Dir", rb_cObject);
+    TEST_INIT_FUNCS(init);
+}

Property changes on: ext/-test-/dir/init.c
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: ext/-test-/dir/extconf.rb
===================================================================
--- ext/-test-/dir/extconf.rb	(revision 0)
+++ ext/-test-/dir/extconf.rb	(revision 46240)
@@ -0,0 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/dir/extconf.rb#L1
+have_func("dirfd")
+
+$INCFLAGS << " -I$(topdir) -I$(top_srcdir)"
+$srcs = Dir[File.join($srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
+inits = $srcs.map {|s| File.basename(s, ".*")}
+inits.delete("init")
+inits.map! {|s|"X(#{s})"}
+$defs << "-DTEST_INIT_FUNCS(X)=\"#{inits.join(' ')}\""
+create_makefile("-test-/dir")

Property changes on: ext/-test-/dir/extconf.rb
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: ext/-test-/dir/fileno.c
===================================================================
--- ext/-test-/dir/fileno.c	(revision 0)
+++ ext/-test-/dir/fileno.c	(revision 46240)
@@ -0,0 +1,107 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/dir/fileno.c#L1
+#include "ruby.h"
+#include "ruby/encoding.h"
+#include "internal.h"
+
+#if defined HAVE_DIRENT_H && !defined _WIN32
+# include <dirent.h>
+#elif defined HAVE_DIRECT_H && !defined _WIN32
+# include <direct.h>
+#else
+# define dirent direct
+# if HAVE_SYS_NDIR_H
+#  include <sys/ndir.h>
+# endif
+# if HAVE_SYS_DIR_H
+#  include <sys/dir.h>
+# endif
+# if HAVE_NDIR_H
+#  include <ndir.h>
+# endif
+# ifdef _WIN32
+#  include "win32/dir.h"
+# endif
+#endif
+#if defined(__native_client__) && defined(NACL_NEWLIB)
+# include "nacl/dirent.h"
+# include "nacl/stat.h"
+#endif
+
+struct dir_data {
+    DIR *dir;
+    VALUE path;
+    rb_encoding *enc;
+};
+
+static void *
+rb_check_typeddata0(VALUE obj /*, const rb_data_type_t *data_type */)
+{
+    const char *etype;
+    /* static const char mesg[] = "wrong argument type %s (expected %s)"; */
+
+    if (!RB_TYPE_P(obj, T_DATA)) {
+        etype = rb_builtin_class_name(obj);
+        /* rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); */
+        rb_raise(rb_eTypeError, "wrong argument type %s", etype);
+    }
+/*
+    if (!RTYPEDDATA_P(obj)) {
+        etype = rb_obj_classname(obj);
+        rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name);
+    }
+    else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
+        etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
+        rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name);
+    }
+*/
+    return DATA_PTR(obj);
+}
+
+static void
+dir_closed(void)
+{
+    rb_raise(rb_eIOError, "closed directory");
+}
+
+static struct dir_data *
+dir_check(VALUE dir)
+{
+    struct dir_data *dirp;
+    rb_check_frozen(dir);
+    dirp = rb_check_typeddata0(dir /*, &dir_data_type*/);
+    if (!dirp->dir) dir_closed();
+    return dirp;
+}
+
+#define GetDIR(obj, dirp) ((dirp) = dir_check(obj))
+
+#ifdef HAVE_DIRFD
+/*
+ *  call-seq:
+ *     dir.fileno -> integer
+ *
+ *  Returns the file descriptor used in <em>dir</em>.
+ *
+ *     d = Dir.new("..")
+ *     d.fileno   #=> 8
+ */
+static VALUE
+dir_fileno(VALUE dir)
+{
+    struct dir_data *dirp;
+    int fd;
+
+    GetDIR(dir, dirp);
+    fd = dirfd(dirp->dir);
+    if (fd == -1)
+       rb_sys_fail("dirfd");
+    return INT2NUM(fd);
+}
+#else
+#define dir_fileno rb_f_notimplement
+#endif
+
+void
+Init_fileno(VALUE klass)
+{
+    rb_define_method(rb_cDir,"fileno", dir_fileno, 0);
+}

Property changes on: ext/-test-/dir/fileno.c
___________________________________________________________________
Added: svn:eol-style
   + LF

Index: test/lib/minitest/unit.rb
===================================================================
--- test/lib/minitest/unit.rb	(revision 46239)
+++ test/lib/minitest/unit.rb	(revision 46240)
@@ -2,6 +2,7 @@ https://github.com/ruby/ruby/blob/trunk/test/lib/minitest/unit.rb#L2
 
 require "optparse"
 require "rbconfig"
+require "-test-/dir"
 
 ##
 # Minimal (mostly drop-in) replacement for test-unit.
@@ -992,7 +993,17 @@ module MiniTest https://github.com/ruby/ruby/blob/trunk/test/lib/minitest/unit.rb#L993
     def find_fds
       fd_dir = "/proc/#{$$}/fd"
       if File.directory?(fd_dir)
-        Dir.entries(fd_dir).grep(/\A\d+\z/).map(&:to_i).sort
+        fds = Dir.open(fd_dir) {|d|
+          a = []
+          while fn = d.read
+            a << fn
+          end
+          if d.respond_to? :fileno
+            a -= [d.fileno.to_s]
+          end
+          a
+        }
+        fds.grep(/\A\d+\z/).map(&:to_i).sort
       else
         []
       end

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

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