summaryrefslogtreecommitdiff
path: root/file.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-03-02 08:25:51 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-03-02 08:25:51 +0000
commit579ffd94da9fa533c8e4fb63a388d8b2957f7b85 (patch)
tree6efbab518ce34230d52ab4d934cf96124c9b3fa2 /file.c
parent607927bf36a17a479b8e740d396b20c135d77ff3 (diff)
merge revision(s) 62606,62607: [Backport #14557]
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. file.c: realpath on special symlink * file.c (realpath_rec): fallback to symlink path when it is accessible but the link target is not actual entry on file systems. [ruby-dev:50487] [Bug #14557] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_5@62632 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'file.c')
-rw-r--r--file.c63
1 files changed, 46 insertions, 17 deletions
diff --git a/file.c b/file.c
index 54d8fb1812..b9e7a8307b 100644
--- a/file.c
+++ b/file.c
@@ -1081,6 +1081,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)
{
@@ -1089,27 +1100,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;
}
- 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;
+ else {
+ FilePathValue(file);
+ file = rb_str_encode_ospath(file);
+ result = stat_without_gvl(RSTRING_PTR(file), st);
+ }
+ RB_GC_GUARD(file);
+ return result;
}
/*
@@ -1129,7 +1151,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);
@@ -3919,7 +3942,7 @@ enum rb_realpath_mode {
};
static int
-realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved,
+realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved, VALUE fallback,
VALUE loopcheck, enum rb_realpath_mode mode, int last)
{
const char *pend = unresolved + strlen(unresolved);
@@ -3977,6 +4000,12 @@ realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved,
ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
if (ret == -1) {
int e = errno;
+ if (e == ENOENT && !NIL_P(fallback)) {
+ if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
+ rb_str_replace(*resolvedp, fallback);
+ return 0;
+ }
+ }
if (mode == RB_REALPATH_CHECK) return -1;
if (e == ENOENT) {
if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
@@ -4008,7 +4037,7 @@ realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved,
*resolvedp = link;
*prefixlenp = link_prefixlen;
}
- if (realpath_rec(prefixlenp, resolvedp, link_names,
+ if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
loopcheck, mode, !*unresolved_firstsep))
return -1;
RB_GC_GUARD(link_orig);
@@ -4097,14 +4126,14 @@ rb_check_realpath_internal(VALUE basedir, VALUE path, enum rb_realpath_mode mode
loopcheck = rb_hash_new();
if (curdir_names) {
- if (realpath_rec(&prefixlen, &resolved, curdir_names, loopcheck, mode, 0))
+ if (realpath_rec(&prefixlen, &resolved, curdir_names, Qnil, loopcheck, mode, 0))
return Qnil;
}
if (basedir_names) {
- if (realpath_rec(&prefixlen, &resolved, basedir_names, loopcheck, mode, 0))
+ if (realpath_rec(&prefixlen, &resolved, basedir_names, Qnil, loopcheck, mode, 0))
return Qnil;
}
- if (realpath_rec(&prefixlen, &resolved, path_names, loopcheck, mode, 1))
+ if (realpath_rec(&prefixlen, &resolved, path_names, Qnil, loopcheck, mode, 1))
return Qnil;
if (origenc != rb_enc_get(resolved)) {