summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2023-02-09 13:34:37 -0800
committerJeremy Evans <code@jeremyevans.net>2023-03-24 11:18:57 -0700
commit836e9a192ba4fdc56a2d3d94f5840869f77fd3bf (patch)
treefe69ae558f9f382ca7f8826f8349405eed9055d1 /dir.c
parent3be65f63c79492908e898d8d7281035445a2b9a1 (diff)
downloadruby-836e9a192ba4fdc56a2d3d94f5840869f77fd3bf.tar.gz
Add Dir.for_fd
This returns a Dir instance for the given directory file descriptor. If fdopendir is not supported, this raises NotImplementedError. Implements [Feature #19347]
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/dir.c b/dir.c
index 93f2657eb0..93f4889958 100644
--- a/dir.c
+++ b/dir.c
@@ -588,6 +588,44 @@ dir_s_close(rb_execution_context_t *ec, VALUE klass, VALUE dir)
return dir_close(dir);
}
+# if defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD)
+/*
+ * call-seq:
+ * Dir.fdopendir(integer) -> aDir
+ *
+ * Returns a Dir representing the directory specified by the given
+ * directory file descriptor. Note that the returned Dir will not
+ * have an associated path.
+ *
+ * d1 = Dir.new('..')
+ * d2 = Dir.for_fd(d1.fileno)
+ * d1.path # => '..'
+ * d2.path # => nil
+ * d1.chdir{Dir.pwd} == d2.chdir{Dir.pwd} # => true
+ *
+ * This method uses fdopendir() function defined by POSIX 2008.
+ * NotImplementedError is raised on other platforms, such as Windows,
+ * which doesn't provide the function.
+ *
+ */
+static VALUE
+dir_s_for_fd(VALUE klass, VALUE fd)
+{
+ struct dir_data *dp;
+ VALUE dir = TypedData_Make_Struct(klass, struct dir_data, &dir_data_type, dp);
+
+ if (!(dp->dir = fdopendir(NUM2INT(fd)))) {
+ rb_sys_fail("fdopendir");
+ UNREACHABLE_RETURN(Qnil);
+ }
+
+ RB_OBJ_WRITE(dir, &dp->path, Qnil);
+ return dir;
+}
+#else
+#define dir_s_for_fd rb_f_notimplement
+#endif
+
NORETURN(static void dir_closed(void));
static void
@@ -3507,6 +3545,7 @@ Init_Dir(void)
rb_include_module(rb_cDir, rb_mEnumerable);
rb_define_alloc_func(rb_cDir, dir_s_alloc);
+ rb_define_singleton_method(rb_cDir,"for_fd", dir_s_for_fd, 1);
rb_define_singleton_method(rb_cDir, "foreach", dir_foreach, -1);
rb_define_singleton_method(rb_cDir, "entries", dir_entries, -1);
rb_define_singleton_method(rb_cDir, "each_child", dir_s_each_child, -1);