aboutsummaryrefslogtreecommitdiffstats
path: root/file.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-28 05:17:01 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-28 05:17:01 +0000
commitd705b2194a8e2528a0ad65991bef31aff81bc971 (patch)
treebc8d70ec24849e66b921063bd5d380c21e4a25a4 /file.c
parentc44ff4e4267090965f8b682ea15c3360358ce7af (diff)
downloadruby-d705b2194a8e2528a0ad65991bef31aff81bc971.tar.gz
file.c: get rid of useless conversion
* file.c (rb_file_s_stat): File.stat does not accept an IO object as trying conversion to path name string first. skip conversion to IO and try stat(2) only. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62606 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'file.c')
-rw-r--r--file.c47
1 files changed, 35 insertions, 12 deletions
diff --git a/file.c b/file.c
index 9c777ca2b7..a1ee7f4897 100644
--- a/file.c
+++ b/file.c
@@ -1082,6 +1082,17 @@ no_gvl_fstat(void *data)
return (VALUE)fstat(arg->file.fd, arg->st);
}
+static int
+fstat_without_gvl(int fd, struct stat *st)
+{
+ no_gvl_stat_data data;
+
+ data.file.fd = fd;
+ data.st = st;
+
+ return (int)(VALUE)rb_thread_io_blocking_region(no_gvl_fstat, &data, fd);
+}
+
static void *
no_gvl_stat(void * data)
{
@@ -1090,27 +1101,38 @@ no_gvl_stat(void * data)
}
static int
-rb_stat(VALUE file, struct stat *st)
+stat_without_gvl(const char *path, struct stat *st)
{
- VALUE tmp;
- VALUE result;
no_gvl_stat_data data;
+ data.file.path = path;
data.st = st;
+
+ return (int)(VALUE)rb_thread_call_without_gvl(no_gvl_stat, &data,
+ RUBY_UBF_IO, NULL);
+}
+
+static int
+rb_stat(VALUE file, struct stat *st)
+{
+ VALUE tmp;
+ int result;
+
tmp = rb_check_convert_type_with_id(file, T_FILE, "IO", idTo_io);
if (!NIL_P(tmp)) {
rb_io_t *fptr;
GetOpenFile(tmp, fptr);
- data.file.fd = fptr->fd;
- result = rb_thread_io_blocking_region(no_gvl_fstat, &data, fptr->fd);
- return (int)result;
+ result = fstat_without_gvl(fptr->fd, st);
+ file = tmp;
+ }
+ else {
+ FilePathValue(file);
+ file = rb_str_encode_ospath(file);
+ result = stat_without_gvl(RSTRING_PTR(file), st);
}
- FilePathValue(file);
- file = rb_str_encode_ospath(file);
- data.file.path = StringValueCStr(file);
- result = (VALUE)rb_thread_call_without_gvl(no_gvl_stat, &data, RUBY_UBF_IO, NULL);
- return (int)result;
+ RB_GC_GUARD(file);
+ return result;
}
/*
@@ -1130,7 +1152,8 @@ rb_file_s_stat(VALUE klass, VALUE fname)
struct stat st;
FilePathValue(fname);
- if (rb_stat(fname, &st) < 0) {
+ fname = rb_str_encode_ospath(fname);
+ if (stat_without_gvl(RSTRING_PTR(fname), &st) < 0) {
rb_sys_fail_path(fname);
}
return rb_stat_new(&st);