tryopen: remember to include ancient_ruby.h for 1.8.5
[kgio.git] / ext / kgio / tryopen.c
blob6b636730bcfdc986aa10a5a53172409e52d27ca3
1 #include <ruby.h>
2 #ifdef HAVE_RUBY_IO_H
3 # include <ruby/io.h>
4 #else
5 # include <rubyio.h>
6 #endif
8 #ifdef HAVE_RUBY_ST_H
9 # include <ruby/st.h>
10 #else
11 # include <st.h>
12 #endif
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include "set_file_path.h"
18 #include "ancient_ruby.h"
20 static ID id_for_fd, id_to_path, id_path;
21 static st_table *errno2sym;
23 struct open_args {
24 const char *pathname;
25 int flags;
26 mode_t mode;
29 #ifndef HAVE_RB_CLOEXEC_OPEN
30 # define rb_cloexec_open(p,f,m) open((p),(f),(m))
31 #endif
33 static VALUE nogvl_open(void *ptr)
35 struct open_args *o = ptr;
37 return (VALUE)rb_cloexec_open(o->pathname, o->flags, o->mode);
40 #ifndef HAVE_RB_THREAD_BLOCKING_REGION
41 # define RUBY_UBF_IO ((void *)(-1))
42 # include "rubysig.h"
43 typedef void rb_unblock_function_t(void *);
44 typedef VALUE rb_blocking_function_t(void *);
45 static VALUE rb_thread_blocking_region(
46 rb_blocking_function_t *fn, void *data1,
47 rb_unblock_function_t *ubf, void *data2)
49 VALUE rv;
51 TRAP_BEG; /* for FIFO */
52 rv = fn(data1);
53 TRAP_END;
55 return rv;
57 #endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
60 * call-seq:
62 * Kgio::File.tryopen(filename, [, mode [, perm]]) -> Kgio::File or Symbol
64 * Returns a Kgio::File object on a successful open. +filename+ is a
65 * path to any file on the filesystem. If specified, +mode+ is a bitmask
66 * of flags (see IO.sysopen) and +perm+ should be an octal number.
68 * This does not raise errors for most failures, but installs returns a
69 * Ruby symbol for the constant in the Errno::* namespace.
71 * Common error symbols are:
73 * - :ENOENT
74 * - :EACCES
76 * See your open(2) manpage for more information on open(2) errors.
78 static VALUE s_tryopen(int argc, VALUE *argv, VALUE klass)
80 int fd;
81 VALUE pathname, flags, mode;
82 struct open_args o;
83 int retried = 0;
84 VALUE rv;
86 rb_scan_args(argc, argv, "12", &pathname, &flags, &mode);
87 if (rb_respond_to(pathname, id_to_path))
88 pathname = rb_funcall(pathname, id_to_path, 0);
89 o.pathname = StringValueCStr(pathname);
91 switch (TYPE(flags)) {
92 case T_NIL: o.flags = O_RDONLY; break;
93 case T_FIXNUM: o.flags = FIX2INT(flags); break;
94 case T_BIGNUM: o.flags = NUM2INT(flags); break;
95 default: rb_raise(rb_eArgError, "flags must be an Integer");
97 switch (TYPE(mode)) {
98 case T_NIL: o.mode = 0666; break;
99 case T_FIXNUM: o.mode = FIX2INT(mode); break;
100 case T_BIGNUM: o.mode = NUM2INT(mode); break;
101 default: rb_raise(rb_eArgError, "mode must be an Integer");
104 retry:
105 fd = (int)rb_thread_blocking_region(nogvl_open, &o, RUBY_UBF_IO, 0);
106 if (fd == -1) {
107 if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
108 rb_gc();
109 if (retried)
110 rb_sys_fail(o.pathname);
111 retried = 1;
112 goto retry;
114 if (fd == -1) {
115 int saved_errno = errno;
117 if (!st_lookup(errno2sym, (st_data_t)errno, &rv)) {
118 errno = saved_errno;
119 rb_sys_fail(o.pathname);
121 return rv;
124 rv = rb_funcall(klass, id_for_fd, 1, INT2FIX(fd));
125 set_file_path(rv, pathname);
126 return rv;
129 void init_kgio_tryopen(void)
131 VALUE mKgio = rb_define_module("Kgio");
132 VALUE mPipeMethods = rb_const_get(mKgio, rb_intern("PipeMethods"));
133 VALUE cFile;
134 VALUE tmp;
135 VALUE *ptr;
136 long len;
138 id_path = rb_intern("path");
139 id_for_fd = rb_intern("for_fd");
140 id_to_path = rb_intern("to_path");
143 * Document-class: Kgio::File
145 * This subclass of the core File class adds the "tryopen" singleton
146 * method for opening files. A single "tryopen" and check for the
147 * return value may be used to avoid unnecessary stat(2) syscalls
148 * or File.open exceptions when checking for the existence of a file
149 * and opening it.
151 cFile = rb_define_class_under(mKgio, "File", rb_cFile);
152 rb_define_singleton_method(cFile, "tryopen", s_tryopen, -1);
153 rb_include_module(cFile, mPipeMethods);
155 if (!rb_funcall(cFile, rb_intern("method_defined?"), 1,
156 ID2SYM(id_to_path)))
157 rb_define_alias(cFile, "to_path", "path");
159 errno2sym = st_init_numtable();
160 tmp = rb_funcall(rb_mErrno, rb_intern("constants"), 0);
161 ptr = RARRAY_PTR(tmp);
162 len = RARRAY_LEN(tmp);
163 for (; --len >= 0; ptr++) {
164 VALUE error;
165 ID const_id;
167 switch (TYPE(*ptr)) {
168 case T_SYMBOL: const_id = SYM2ID(*ptr); break;
169 case T_STRING: const_id = rb_intern(RSTRING_PTR(*ptr)); break;
170 default: rb_bug("constant not a symbol or string");
173 error = rb_const_get(rb_mErrno, const_id);
174 if ((TYPE(error) != T_CLASS) ||
175 !rb_const_defined(error, rb_intern("Errno")))
176 continue;
178 error = rb_const_get(error, rb_intern("Errno"));
179 switch (TYPE(error)) {
180 case T_FIXNUM:
181 case T_BIGNUM:
182 st_insert(errno2sym, (st_data_t)NUM2INT(error),
183 ID2SYM(const_id));