ruby-changes:51853
From: nobu <ko1@a...>
Date: Thu, 26 Jul 2018 21:48:10 +0900 (JST)
Subject: [ruby-changes:51853] nobu:r64067 (trunk): dir.c: fix glob with base when no DT_UNKNOWN
nobu 2018-07-26 21:48:02 +0900 (Thu, 26 Jul 2018) New Revision: 64067 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64067 Log: dir.c: fix glob with base when no DT_UNKNOWN * dir.c (do_stat, do_lstat, do_opendir): need the length of the base path for fstatat() when fd is valid. * dir.c (glob_helper): fix for platforms where DT_UNKNOWN is not available, e.g. Solaris. Modified files: trunk/dir.c Index: dir.c =================================================================== --- dir.c (revision 64066) +++ dir.c (revision 64067) @@ -1378,12 +1378,24 @@ typedef struct { https://github.com/ruby/ruby/blob/trunk/dir.c#L1378 ruby_glob_errfunc *error; } ruby_glob_funcs_t; +static const char * +at_subpath(int fd, size_t baselen, const char *path) +{ +#if USE_OPENDIR_AT + if (fd != (int)AT_FDCWD && baselen > 0) { + path += baselen; + if (*path == '/') ++path; + } +#endif + return *path ? path : "."; +} + /* System call with warning */ static int -do_stat(int fd, const char *path, struct stat *pst, int flags, rb_encoding *enc) +do_stat(int fd, size_t baselen, const char *path, struct stat *pst, int flags, rb_encoding *enc) { #if USE_OPENDIR_AT - int ret = fstatat(fd, path, pst, 0); + int ret = fstatat(fd, at_subpath(fd, baselen, path), pst, 0); #else int ret = STAT(path, pst); #endif @@ -1395,10 +1407,10 @@ do_stat(int fd, const char *path, struct https://github.com/ruby/ruby/blob/trunk/dir.c#L1407 #if defined HAVE_LSTAT || defined lstat || USE_OPENDIR_AT static int -do_lstat(int fd, const char *path, struct stat *pst, int flags, rb_encoding *enc) +do_lstat(int fd, size_t baselen, const char *path, struct stat *pst, int flags, rb_encoding *enc) { #if USE_OPENDIR_AT - int ret = fstatat(fd, path, pst, AT_SYMLINK_NOFOLLOW); + int ret = fstatat(fd, at_subpath(fd, baselen, path), pst, AT_SYMLINK_NOFOLLOW); #else int ret = lstat(path, pst); #endif @@ -1488,7 +1500,7 @@ opendir_at(int basefd, const char *path) https://github.com/ruby/ruby/blob/trunk/dir.c#L1500 } static DIR * -do_opendir(const int basefd, const char *path, int flags, rb_encoding *enc, +do_opendir(const int basefd, size_t baselen, const char *path, int flags, rb_encoding *enc, ruby_glob_errfunc *errfunc, VALUE arg, int *status) { DIR *dirp; @@ -1500,7 +1512,7 @@ do_opendir(const int basefd, const char https://github.com/ruby/ruby/blob/trunk/dir.c#L1512 path = RSTRING_PTR(tmp); } #endif - dirp = opendir_at(basefd, path); + dirp = opendir_at(basefd, at_subpath(basefd, baselen, path)); if (!dirp) { int e = errno; @@ -2010,9 +2022,6 @@ glob_helper( https://github.com/ruby/ruby/blob/trunk/dir.c#L2022 int plain = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0; int escape = !(flags & FNM_NOESCAPE); size_t pathlen = baselen + namelen; - const char *base = path; - - if (fd != AT_FDCWD && *(base += baselen) == '/') base++; for (cur = beg; cur < end; ++cur) { struct glob_pattern *p = *cur; @@ -2045,9 +2054,9 @@ glob_helper( https://github.com/ruby/ruby/blob/trunk/dir.c#L2054 } } - if (*base) { + if (*path) { if (match_all && pathtype == path_unknown) { - if (do_lstat(fd, base, &st, flags, enc) == 0) { + if (do_lstat(fd, baselen, path, &st, flags, enc) == 0) { pathtype = IFTODT(st.st_mode); } else { @@ -2055,7 +2064,7 @@ glob_helper( https://github.com/ruby/ruby/blob/trunk/dir.c#L2064 } } if (match_dir && (pathtype == path_unknown || pathtype == path_symlink)) { - if (do_stat(fd, base, &st, flags, enc) == 0) { + if (do_stat(fd, baselen, path, &st, flags, enc) == 0) { pathtype = IFTODT(st.st_mode); } else { @@ -2091,14 +2100,14 @@ glob_helper( https://github.com/ruby/ruby/blob/trunk/dir.c#L2100 if (cur + 1 == end && (*cur)->type <= ALPHA) { plainname = join_path(path, pathlen, dirsep, (*cur)->str, strlen((*cur)->str)); if (!plainname) return -1; - dirp = do_opendir(fd, plainname, flags, enc, funcs->error, arg, &status); + dirp = do_opendir(fd, basename, plainname, flags, enc, funcs->error, arg, &status); GLOB_FREE(plainname); } else # else ; # endif - dirp = do_opendir(fd, *base ? base : ".", flags, enc, funcs->error, arg, &status); + dirp = do_opendir(fd, baselen, path, flags, enc, funcs->error, arg, &status); if (dirp == NULL) { # if FNM_SYSCASE || NORMALIZE_UTF8PATH if ((magical < 2) && !recursive && (errno == EACCES)) { @@ -2108,7 +2117,7 @@ glob_helper( https://github.com/ruby/ruby/blob/trunk/dir.c#L2117 # endif return status; } - IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *base ? base : ".")); + IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *path ? path : ".")); # if NORMALIZE_UTF8PATH if (!(norm_p || magical || recursive)) { @@ -2167,7 +2176,7 @@ glob_helper( https://github.com/ruby/ruby/blob/trunk/dir.c#L2176 if (recursive && dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1) && new_pathtype == path_unknown) { /* RECURSIVE never match dot files unless FNM_DOTMATCH is set */ - if (do_lstat(fd, buf, &st, flags, enc) == 0) + if (do_lstat(fd, baselen, buf, &st, flags, enc) == 0) new_pathtype = IFTODT(st.st_mode); else new_pathtype = path_noent; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/