14 #include <sys/types.h>
17 #include "set_file_path.h"
19 static ID id_for_fd
, id_to_path
, id_path
;
20 static st_table
*errno2sym
;
28 #ifndef HAVE_RB_CLOEXEC_OPEN
29 # define rb_cloexec_open(p,f,m) open((p),(f),(m))
32 static VALUE
nogvl_open(void *ptr
)
34 struct open_args
*o
= ptr
;
36 return (VALUE
)rb_cloexec_open(o
->pathname
, o
->flags
, o
->mode
);
39 #ifndef HAVE_RB_THREAD_BLOCKING_REGION
40 # define RUBY_UBF_IO ((void *)(-1))
42 typedef void rb_unblock_function_t(void *);
43 typedef VALUE
rb_blocking_function_t(void *);
44 static VALUE
rb_thread_blocking_region(
45 rb_blocking_function_t
*fn
, void *data1
,
46 rb_unblock_function_t
*ubf
, void *data2
)
50 TRAP_BEG
; /* for FIFO */
56 #endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
61 * Kgio::File.tryopen(filename, [, mode [, perm]]) -> Kgio::File or Symbol
63 * Returns a Kgio::File object on a successful open. +filename+ is a
64 * path to any file on the filesystem. If specified, +mode+ is a bitmask
65 * of flags (see IO.sysopen) and +perm+ should be an octal number.
67 * This does not raise errors for most failures, but installs returns a
68 * Ruby symbol for the constant in the Errno::* namespace.
70 * Common error symbols are:
75 * See your open(2) manpage for more information on open(2) errors.
77 static VALUE
s_tryopen(int argc
, VALUE
*argv
, VALUE klass
)
80 VALUE pathname
, flags
, mode
;
85 rb_scan_args(argc
, argv
, "12", &pathname
, &flags
, &mode
);
86 if (rb_respond_to(pathname
, id_to_path
))
87 pathname
= rb_funcall(pathname
, id_to_path
, 0);
88 o
.pathname
= StringValueCStr(pathname
);
90 switch (TYPE(flags
)) {
91 case T_NIL
: o
.flags
= O_RDONLY
; break;
92 case T_FIXNUM
: o
.flags
= FIX2INT(flags
); break;
93 case T_BIGNUM
: o
.flags
= NUM2INT(flags
); break;
94 default: rb_raise(rb_eArgError
, "flags must be an Integer");
97 case T_NIL
: o
.mode
= 0666; break;
98 case T_FIXNUM
: o
.mode
= FIX2INT(mode
); break;
99 case T_BIGNUM
: o
.mode
= NUM2INT(mode
); break;
100 default: rb_raise(rb_eArgError
, "mode must be an Integer");
104 fd
= (int)rb_thread_blocking_region(nogvl_open
, &o
, RUBY_UBF_IO
, 0);
106 if (errno
== EMFILE
|| errno
== ENFILE
|| errno
== ENOMEM
) {
109 rb_sys_fail(o
.pathname
);
114 int saved_errno
= errno
;
116 if (!st_lookup(errno2sym
, (st_data_t
)errno
, &rv
)) {
118 rb_sys_fail(o
.pathname
);
123 rv
= rb_funcall(klass
, id_for_fd
, 1, INT2FIX(fd
));
124 set_file_path(rv
, pathname
);
128 void init_kgio_tryopen(void)
130 VALUE mKgio
= rb_define_module("Kgio");
131 VALUE mPipeMethods
= rb_const_get(mKgio
, rb_intern("PipeMethods"));
137 id_path
= rb_intern("path");
138 id_for_fd
= rb_intern("for_fd");
139 id_to_path
= rb_intern("to_path");
142 * Document-class: Kgio::File
144 * This subclass of the core File class adds the "tryopen" singleton
145 * method for opening files. A single "tryopen" and check for the
146 * return value may be used to avoid unnecessary stat(2) syscalls
147 * or File.open exceptions when checking for the existence of a file
150 cFile
= rb_define_class_under(mKgio
, "File", rb_cFile
);
151 rb_define_singleton_method(cFile
, "tryopen", s_tryopen
, -1);
152 rb_include_module(cFile
, mPipeMethods
);
154 if (!rb_funcall(cFile
, rb_intern("method_defined?"), 1,
156 rb_define_alias(cFile
, "to_path", "path");
158 errno2sym
= st_init_numtable();
159 tmp
= rb_funcall(rb_mErrno
, rb_intern("constants"), 0);
160 ptr
= RARRAY_PTR(tmp
);
161 len
= RARRAY_LEN(tmp
);
162 for (; --len
>= 0; ptr
++) {
166 switch (TYPE(*ptr
)) {
167 case T_SYMBOL
: const_id
= SYM2ID(*ptr
); break;
168 case T_STRING
: const_id
= rb_intern(RSTRING_PTR(*ptr
)); break;
169 default: rb_bug("constant not a symbol or string");
172 error
= rb_const_get(rb_mErrno
, const_id
);
173 if ((TYPE(error
) != T_CLASS
) ||
174 !rb_const_defined(error
, rb_intern("Errno")))
177 error
= rb_const_get(error
, rb_intern("Errno"));
178 switch (TYPE(error
)) {
181 st_insert(errno2sym
, (st_data_t
)NUM2INT(error
),