doc: add rdoc for Kgio::File.tryopen
[kgio.git] / ext / kgio / tryopen.c
blob82488de78df67d6540fe52239bc37aec9ef55c03
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"
19 static ID id_for_fd, id_to_path, id_path;
20 static st_table *errno2sym;
22 struct open_args {
23 const char *pathname;
24 int flags;
25 mode_t mode;
28 static VALUE nogvl_open(void *ptr)
30 struct open_args *o = ptr;
32 return (VALUE)open(o->pathname, o->flags, o->mode);
35 #ifndef HAVE_RB_THREAD_BLOCKING_REGION
36 # define RUBY_UBF_IO ((void *)(-1))
37 # include "rubysig.h"
38 typedef void rb_unblock_function_t(void *);
39 typedef VALUE rb_blocking_function_t(void *);
40 static VALUE rb_thread_blocking_region(
41 rb_blocking_function_t *fn, void *data1,
42 rb_unblock_function_t *ubf, void *data2)
44 VALUE rv;
46 TRAP_BEG; /* for FIFO */
47 rv = fn(data1);
48 TRAP_END;
50 return rv;
52 #endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
55 * call-seq:
57 * Kgio::File.tryopen(filename, [, mode [, perm]]) -> Kgio::File or Symbol
59 * Returns a Kgio::File object on a successful open. +filename+ is a
60 * path to any file on the filesystem. If specified, +mode+ is a bitmask
61 * of flags (see IO.sysopen) and +perm+ should be an octal number.
63 * This does not raise errors for most failures, but installs returns a
64 * Ruby symbol for the constant in the Errno::* namespace.
66 * Common error symbols are:
68 * - :ENOENT
69 * - :EACCES
71 * See your open(2) manpage for more information on open(2) errors.
73 static VALUE s_tryopen(int argc, VALUE *argv, VALUE klass)
75 int fd;
76 VALUE pathname, flags, mode;
77 struct open_args o;
78 int retried = 0;
79 VALUE rv;
81 rb_scan_args(argc, argv, "12", &pathname, &flags, &mode);
82 if (rb_respond_to(pathname, id_to_path))
83 pathname = rb_funcall(pathname, id_to_path, 0);
84 o.pathname = StringValueCStr(pathname);
86 switch (TYPE(flags)) {
87 case T_NIL: o.flags = O_RDONLY; break;
88 case T_FIXNUM: o.flags = FIX2INT(flags); break;
89 case T_BIGNUM: o.flags = NUM2INT(flags); break;
90 default: rb_raise(rb_eArgError, "flags must be an Integer");
92 switch (TYPE(mode)) {
93 case T_NIL: o.mode = 0666; break;
94 case T_FIXNUM: o.mode = FIX2INT(mode); break;
95 case T_BIGNUM: o.mode = NUM2INT(mode); break;
96 default: rb_raise(rb_eArgError, "mode must be an Integer");
99 retry:
100 fd = (int)rb_thread_blocking_region(nogvl_open, &o, RUBY_UBF_IO, 0);
101 if (fd == -1) {
102 if (errno == EMFILE || errno == ENFILE) {
103 rb_gc();
104 if (retried)
105 rb_sys_fail(o.pathname);
106 retried = 1;
107 goto retry;
109 if (fd == -1) {
110 int saved_errno = errno;
112 if (!st_lookup(errno2sym, (st_data_t)errno, &rv)) {
113 errno = saved_errno;
114 rb_sys_fail(o.pathname);
116 return rv;
119 rv = rb_funcall(klass, id_for_fd, 1, INT2FIX(fd));
120 set_file_path(rv, pathname);
121 return rv;
124 void init_kgio_tryopen(void)
126 VALUE mKgio = rb_define_module("Kgio");
127 VALUE cFile;
128 VALUE tmp;
129 VALUE *ptr;
130 long len;
132 id_path = rb_intern("path");
133 id_for_fd = rb_intern("for_fd");
134 id_to_path = rb_intern("to_path");
137 * Document-class: Kgio::File
139 * This subclass of the core File class adds the "tryopen" singleton
140 * method for opening files. A single "tryopen" and check for the
141 * return value may be used to avoid unnecessary stat(2) syscalls
142 * or File.open exceptions when checking for the existence of a file
143 * and opening it.
145 cFile = rb_define_class_under(mKgio, "File", rb_cFile);
146 rb_define_singleton_method(cFile, "tryopen", s_tryopen, -1);
148 if (!rb_funcall(cFile, rb_intern("method_defined?"), 1,
149 ID2SYM(id_to_path)))
150 rb_define_alias(cFile, "to_path", "path");
152 errno2sym = st_init_numtable();
153 tmp = rb_funcall(rb_mErrno, rb_intern("constants"), 0);
154 ptr = RARRAY_PTR(tmp);
155 len = RARRAY_LEN(tmp);
156 for (; --len >= 0; ptr++) {
157 VALUE error;
158 ID const_id;
160 switch (TYPE(*ptr)) {
161 case T_SYMBOL: const_id = SYM2ID(*ptr); break;
162 case T_STRING: const_id = rb_intern(RSTRING_PTR(*ptr)); break;
163 default: rb_bug("constant not a symbol or string");
166 error = rb_const_get(rb_mErrno, const_id);
167 if ((TYPE(error) != T_CLASS) ||
168 !rb_const_defined(error, rb_intern("Errno")))
169 continue;
171 error = rb_const_get(error, rb_intern("Errno"));
172 switch (TYPE(error)) {
173 case T_FIXNUM:
174 case T_BIGNUM:
175 st_insert(errno2sym, (st_data_t)NUM2INT(error),
176 ID2SYM(const_id));