dir.c: revert r60772, r60770, and r60769
Using readdir(3) without any locking causes thread-safety problems if directory streams get shared between threads. On ancient platforms, readdir(3) may have thread-safety problems even on different directory streams. Using readdir_r(3) is not viable, either, as it's deprecated due to name overflow problems. So for now, rely on GVL as in previous Rubies and perhaps consider per-"struct dir_data" mutexes for modern platforms which allow concurrent calls to readdir(3) on different directory streams. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60774 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e5b968f571
commit
0cc57ea018
41
dir.c
41
dir.c
@ -743,23 +743,6 @@ to_be_skipped(const struct dirent *dp)
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *
|
|
||||||
nogvl_readdir(void *ptr)
|
|
||||||
{
|
|
||||||
struct dir_data *dirp = ptr;
|
|
||||||
|
|
||||||
return READDIR(dirp->dir, dirp->enc);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct dirent *
|
|
||||||
readdir_without_gvl(struct dir_data *dirp)
|
|
||||||
{
|
|
||||||
if (rb_cThread) /* VM is running */
|
|
||||||
return rb_thread_call_without_gvl(nogvl_readdir, dirp, RUBY_UBF_IO, 0);
|
|
||||||
else
|
|
||||||
return READDIR(dirp->dir, dirp->enc);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* dir.read -> string or nil
|
* dir.read -> string or nil
|
||||||
@ -780,7 +763,7 @@ dir_read(VALUE dir)
|
|||||||
|
|
||||||
GetDIR(dir, dirp);
|
GetDIR(dir, dirp);
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if ((dp = readdir_without_gvl(dirp))) {
|
if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
|
||||||
return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc);
|
return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -835,7 +818,7 @@ dir_each_entry(VALUE dir, VALUE (*each)(VALUE, VALUE), VALUE arg, int children_o
|
|||||||
GetDIR(dir, dirp);
|
GetDIR(dir, dirp);
|
||||||
rewinddir(dirp->dir);
|
rewinddir(dirp->dir);
|
||||||
IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path)));
|
IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path)));
|
||||||
while ((dp = readdir_without_gvl(dirp)) != NULL) {
|
while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
|
||||||
const char *name = dp->d_name;
|
const char *name = dp->d_name;
|
||||||
size_t namlen = NAMLEN(dp);
|
size_t namlen = NAMLEN(dp);
|
||||||
VALUE path;
|
VALUE path;
|
||||||
@ -2023,27 +2006,25 @@ glob_helper(
|
|||||||
if (pathtype == path_noent) return 0;
|
if (pathtype == path_noent) return 0;
|
||||||
|
|
||||||
if (magical || recursive) {
|
if (magical || recursive) {
|
||||||
struct dir_data dd;
|
|
||||||
struct dirent *dp;
|
struct dirent *dp;
|
||||||
|
DIR *dirp;
|
||||||
# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
|
# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
|
||||||
char *plainname = 0;
|
char *plainname = 0;
|
||||||
# endif
|
# endif
|
||||||
IF_NORMALIZE_UTF8PATH(int norm_p);
|
IF_NORMALIZE_UTF8PATH(int norm_p);
|
||||||
|
|
||||||
dd.enc = enc;
|
|
||||||
# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
|
# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
|
||||||
if (cur + 1 == end && (*cur)->type <= ALPHA) {
|
if (cur + 1 == end && (*cur)->type <= ALPHA) {
|
||||||
plainname = join_path(path, pathlen, dirsep, (*cur)->str, strlen((*cur)->str));
|
plainname = join_path(path, pathlen, dirsep, (*cur)->str, strlen((*cur)->str));
|
||||||
if (!plainname) return -1;
|
if (!plainname) return -1;
|
||||||
dd.dir = do_opendir(fd, plainname, flags, enc, funcs->error, arg, &status);
|
dirp = do_opendir(fd, plainname, flags, enc, funcs->error, arg, &status);
|
||||||
GLOB_FREE(plainname);
|
GLOB_FREE(plainname);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
# else
|
# else
|
||||||
;
|
;
|
||||||
# endif
|
# endif
|
||||||
dd.dir = do_opendir(fd, *base ? base : ".", flags, enc, funcs->error, arg, &status);
|
dirp = do_opendir(fd, *base ? base : ".", flags, enc, funcs->error, arg, &status);
|
||||||
if (dd.dir == NULL) {
|
if (dirp == NULL) {
|
||||||
# if FNM_SYSCASE || NORMALIZE_UTF8PATH
|
# if FNM_SYSCASE || NORMALIZE_UTF8PATH
|
||||||
if ((magical < 2) && !recursive && (errno == EACCES)) {
|
if ((magical < 2) && !recursive && (errno == EACCES)) {
|
||||||
/* no read permission, fallback */
|
/* no read permission, fallback */
|
||||||
@ -2052,19 +2033,19 @@ glob_helper(
|
|||||||
# endif
|
# endif
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dd.dir, *base ? base : "."));
|
IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *base ? base : "."));
|
||||||
|
|
||||||
# if NORMALIZE_UTF8PATH
|
# if NORMALIZE_UTF8PATH
|
||||||
if (!(norm_p || magical || recursive)) {
|
if (!(norm_p || magical || recursive)) {
|
||||||
closedir(dd.dir);
|
closedir(dirp);
|
||||||
goto literally;
|
goto literally;
|
||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
# ifdef HAVE_GETATTRLIST
|
# ifdef HAVE_GETATTRLIST
|
||||||
if (is_case_sensitive(dd.dir, path) == 0)
|
if (is_case_sensitive(dirp, path) == 0)
|
||||||
flags |= FNM_CASEFOLD;
|
flags |= FNM_CASEFOLD;
|
||||||
# endif
|
# endif
|
||||||
while ((dp = readdir_without_gvl(&dd)) != NULL) {
|
while ((dp = READDIR(dirp, enc)) != NULL) {
|
||||||
char *buf;
|
char *buf;
|
||||||
rb_pathtype_t new_pathtype = path_unknown;
|
rb_pathtype_t new_pathtype = path_unknown;
|
||||||
const char *name;
|
const char *name;
|
||||||
@ -2159,7 +2140,7 @@ glob_helper(
|
|||||||
if (status) break;
|
if (status) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
closedir(dd.dir);
|
closedir(dirp);
|
||||||
}
|
}
|
||||||
else if (plain) {
|
else if (plain) {
|
||||||
struct glob_pattern **copy_beg, **copy_end, **cur2;
|
struct glob_pattern **copy_beg, **copy_end, **cur2;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user