Skip to content

Commit

Permalink
Add Dir.for_fd
Browse files Browse the repository at this point in the history
This returns a Dir instance for the given directory file descriptor.
If fdopendir is not supported, this raises NotImplementedError.

Implements [Feature #19347]
  • Loading branch information
jeremyevans committed Mar 24, 2023
1 parent 3be65f6 commit 836e9a1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -17,6 +17,8 @@ Note: We're only listing outstanding class updates.

* Dir

* `Dir.for_fd` added for returning a Dir object for the directory specified
by the provided directory file descriptor. [[Feature #19347]]
* `Dir.fchdir` added for changing the directory to the directory specified
by the provided directory file descriptor. [[Feature #19347]]
* `Dir#chdir` added for changing the directory to the directory specified
Expand Down
39 changes: 39 additions & 0 deletions dir.c
Expand Up @@ -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

This comment has been minimized.

Copy link
@ioquatix

ioquatix Mar 24, 2023

Member

@jeremyevans it seems like the call sequence method name is wrong?

This comment has been minimized.

Copy link
@jeremyevans

jeremyevans Mar 24, 2023

Author Contributor

Good catch. I'll fix that.

*
* 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
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions test/ruby/test_dir.rb
Expand Up @@ -639,6 +639,21 @@ def test_fileno
}
end

def test_for_fd
if Dir.respond_to? :for_fd
begin
new_dir = Dir.new('..')
for_fd_dir = Dir.for_fd(new_dir.fileno)
assert_equal(new_dir.chdir{Dir.pwd}, for_fd_dir.chdir{Dir.pwd})
ensure
new_dir&.close
for_fd_dir&.close
end
else
assert_raise(NotImplementedError) { Dir.for_fd(0) }
end
end

def test_empty?
assert_not_send([Dir, :empty?, @root])
a = File.join(@root, "a")
Expand Down

0 comments on commit 836e9a1

Please sign in to comment.