14 #include "ruby/internal/config.h"
18 #include <sys/types.h>
29 #ifndef USE_OPENDIR_AT
30 # if defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD) && \
31 defined(HAVE_OPENAT) && defined(HAVE_FSTATAT)
32 # define USE_OPENDIR_AT 1
34 # define USE_OPENDIR_AT 0
42 #undef HAVE_DIRENT_NAMLEN
43 #if defined HAVE_DIRENT_H && !defined _WIN32
45 # define NAMLEN(dirent) strlen((dirent)->d_name)
46 #elif defined HAVE_DIRECT_H && !defined _WIN32
48 # define NAMLEN(dirent) strlen((dirent)->d_name)
50 # define dirent direct
51 # define NAMLEN(dirent) (dirent)->d_namlen
52 # define HAVE_DIRENT_NAMLEN 1
53 # ifdef HAVE_SYS_NDIR_H
54 # include <sys/ndir.h>
56 # ifdef HAVE_SYS_DIR_H
63 # include "win32/dir.h"
72 char *strchr(
char*,
char);
75 #ifdef HAVE_SYS_ATTR_H
79 #define USE_NAME_ON_FS_REAL_BASENAME 1
81 #define USE_NAME_ON_FS_BY_FNMATCH 2
84 #ifdef HAVE_GETATTRLIST
85 # define USE_NAME_ON_FS USE_NAME_ON_FS_REAL_BASENAME
86 # define RUP32(size) ((size)+3/4)
87 # define SIZEUP32(type) RUP32(sizeof(type))
89 # define USE_NAME_ON_FS USE_NAME_ON_FS_REAL_BASENAME
91 # define USE_NAME_ON_FS USE_NAME_ON_FS_BY_FNMATCH
93 # define USE_NAME_ON_FS 0
97 # define NORMALIZE_UTF8PATH 1
98 # include <sys/param.h>
99 # include <sys/mount.h>
100 # include <sys/vnode.h>
102 # define NORMALIZE_UTF8PATH 0
105 #include "encindex.h"
107 #include "internal.h"
108 #include "internal/array.h"
109 #include "internal/dir.h"
110 #include "internal/encoding.h"
111 #include "internal/error.h"
112 #include "internal/file.h"
113 #include "internal/gc.h"
114 #include "internal/io.h"
115 #include "internal/object.h"
116 #include "internal/imemo.h"
117 #include "internal/vm.h"
128 #define vm_initialized rb_cThread
133 # define chdir(p) rb_w32_uchdir(p)
135 # define mkdir(p, m) rb_w32_umkdir((p), (m))
137 # define rmdir(p) rb_w32_urmdir(p)
139 # define opendir(p) rb_w32_uopendir(p)
140 # define ruby_getcwd() rb_w32_ugetcwd(NULL, 0)
146 #ifdef HAVE_GETATTRLIST
147 struct getattrlist_args {
150 struct attrlist *list;
153 unsigned int options;
156 # define GETATTRLIST_ARGS(list_, buf_, options_) (struct getattrlist_args) \
157 {.list = list_, .buf = buf_, .size = sizeof(buf_), .options = options_}
160 nogvl_getattrlist(
void *args)
162 struct getattrlist_args *arg = args;
163 return (
void *)(
VALUE)getattrlist(arg->path, arg->list, arg->buf, arg->size, arg->options);
167 gvl_getattrlist(
struct getattrlist_args *args,
const char *path)
170 return IO_WITHOUT_GVL_INT(nogvl_getattrlist, args);
173 # ifdef HAVE_FGETATTRLIST
175 nogvl_fgetattrlist(
void *args)
177 struct getattrlist_args *arg = args;
178 return (
void *)(
VALUE)fgetattrlist(arg->fd, arg->list, arg->buf, arg->size, arg->options);
182 gvl_fgetattrlist(
struct getattrlist_args *args,
int fd)
185 return IO_WITHOUT_GVL_INT(nogvl_fgetattrlist, args);
190 #if NORMALIZE_UTF8PATH
191 # if defined HAVE_FGETATTRLIST || !defined HAVE_GETATTRLIST
192 # define need_normalization(dirp, path) need_normalization(dirp)
194 # define need_normalization(dirp, path) need_normalization(path)
197 need_normalization(
DIR *dirp,
const char *path)
199 # if defined HAVE_FGETATTRLIST || defined HAVE_GETATTRLIST
200 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
201 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
202 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, 0);
203 # if defined HAVE_FGETATTRLIST
204 int ret = gvl_fgetattrlist(&args, dirfd(dirp));
206 int ret = gvl_getattrlist(&args, path);
209 const fsobj_tag_t *tag = (
void *)(attrbuf+1);
221 has_nonascii(
const char *
ptr,
size_t len)
231 # define IF_NORMALIZE_UTF8PATH(something) something
233 # define IF_NORMALIZE_UTF8PATH(something)
236 #if defined(IFTODT) && defined(DT_UNKNOWN)
237 # define EMULATE_IFTODT 0
239 # define EMULATE_IFTODT 1
243 # define IFTODT(m) (((m) & S_IFMT) / ((~S_IFMT & (S_IFMT-1)) + 1))
248 path_exist = DT_UNKNOWN,
249 path_directory = DT_DIR,
250 path_regular = DT_REG,
251 path_symlink = DT_LNK,
254 path_directory = IFTODT(S_IFDIR),
255 path_regular = IFTODT(S_IFREG),
256 path_symlink = IFTODT(S_IFLNK),
262 #define FNM_NOESCAPE 0x01
263 #define FNM_PATHNAME 0x02
264 #define FNM_DOTMATCH 0x04
265 #define FNM_CASEFOLD 0x08
266 #define FNM_EXTGLOB 0x10
267 #if CASEFOLD_FILESYSTEM
268 #define FNM_SYSCASE FNM_CASEFOLD
270 #define FNM_SYSCASE 0
273 #define FNM_SHORTNAME 0x20
275 #define FNM_SHORTNAME 0
277 #define FNM_GLOB_NOSORT 0x40
278 #define FNM_GLOB_SKIPDOT 0x80
280 #define FNM_NOMATCH 1
283 # define Next(p, e, enc) ((p)+ rb_enc_mbclen((p), (e), (enc)))
284 # define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
295 const int nocase = flags & FNM_CASEFOLD;
296 const int escape = !(flags & FNM_NOESCAPE);
301 if (p >= pend)
return NULL;
302 if (*p ==
'!' || *p ==
'^') {
309 if (escape && *t1 ==
'\\')
314 if (p >= pend)
return NULL;
315 if (p[0] ==
'-' && p[1] !=
']') {
316 const char *t2 = p + 1;
318 if (escape && *t2 ==
'\\')
324 if ((r <= (send-s) && memcmp(t1, s, r) == 0) ||
325 (r2 <= (send-s) && memcmp(t2, s, r2) == 0)) {
333 if (c1 < c2)
continue;
336 if (c1 > c2)
continue;
340 if (r <= (send-s) && memcmp(t1, s, r) == 0) {
344 if (!nocase)
continue;
347 if (c1 != c2)
continue;
352 return ok == not ? NULL : (
char *)p + 1;
360 #define UNESCAPE(p) (escape && *(p) == '\\' ? (p) + 1 : (p))
361 #define ISEND(p) (!*(p) || (pathname && *(p) == '/'))
362 #define RETURN(val) return *pcur = p, *scur = s, (val);
371 const int period = !(flags & FNM_DOTMATCH);
372 const int pathname = flags & FNM_PATHNAME;
373 const int escape = !(flags & FNM_NOESCAPE);
374 const int nocase = flags & FNM_CASEFOLD;
376 const char *ptmp = 0;
377 const char *stmp = 0;
379 const char *p = *pcur;
380 const char *pend = p + strlen(p);
381 const char *s = *scur;
382 const char *send = s + strlen(s);
386 if (period && *s ==
'.' && *UNESCAPE(p) !=
'.')
392 do { p++; }
while (*p ==
'*');
393 if (ISEND(UNESCAPE(p))) {
414 if ((t = bracket(p + 1, pend, s, send, flags, enc)) != 0) {
426 RETURN(ISEND(p) ? 0 : FNM_NOMATCH);
432 if (r <= (send-s) && memcmp(p, s, r) == 0) {
437 if (!nocase)
goto failed;
448 Inc(stmp, send, enc);
463 const char *p = pattern;
464 const char *s = string;
465 const char *send = s + strlen(
string);
466 const int period = !(flags & FNM_DOTMATCH);
467 const int pathname = flags & FNM_PATHNAME;
469 const char *ptmp = 0;
470 const char *stmp = 0;
474 if (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/') {
475 do { p += 3; }
while (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/');
479 if (fnmatch_helper(&p, &s, flags, enc) == 0) {
480 while (*s && *s !=
'/') Inc(s, send, enc);
490 if (ptmp && stmp && !(period && *stmp ==
'.')) {
491 while (*stmp && *stmp !=
'/') Inc(stmp, send, enc);
503 return fnmatch_helper(&p, &s, flags, enc);
519 if (dir->dir) closedir(dir->dir);
522 RUBY_REFERENCES(dir_refs) = {
523 RUBY_REF_EDGE(
struct dir_data, path),
530 RUBY_REFS_LIST_PTR(dir_refs),
534 0, NULL, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE
540 dir_s_alloc(
VALUE klass)
553 nogvl_opendir(
void *
ptr)
555 const char *path =
ptr;
557 return opendir(path);
561 opendir_without_gvl(
const char *path)
563 if (vm_initialized) {
564 union {
const void *in;
void *out; } u;
568 return IO_WITHOUT_GVL(nogvl_opendir, u.out);
571 return opendir(path);
578 if (closedir(dp->dir) < 0) {
587 check_closedir(
DIR *dirp)
589 if (closedir(dirp) < 0)
611 dp->dir = opendir_without_gvl(path);
612 if (dp->dir == NULL) {
614 if (rb_gc_for_fd(e)) {
615 dp->dir = opendir_without_gvl(path);
617 #ifdef HAVE_GETATTRLIST
619 u_int32_t attrbuf[1];
620 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0};
621 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, FSOPT_NOFOLLOW);
622 if (gvl_getattrlist(&args, path) == 0) {
623 dp->dir = opendir_without_gvl(path);
627 if (dp->dir == NULL) {
629 rb_syserr_fail_path(e, orig);
643 dir_initialize(ec, dir, dirname, enc);
651 return dir_close(dir);
654 # if defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD)
656 nogvl_fdopendir(
void *fd)
658 return fdopendir((
int)(
VALUE)fd);
687 if (!(dp->dir = IO_WITHOUT_GVL(nogvl_fdopendir, (
void *)(
VALUE)
NUM2INT(fd)))) {
696 #define dir_s_for_fd rb_f_notimplement
699 NORETURN(
static void dir_closed(
void));
710 rb_check_frozen(dir);
717 struct dir_data *dirp = dir_get(dir);
718 if (!dirp->dir) dir_closed();
722 #define GetDIR(obj, dirp) ((dirp) = dir_check(obj))
735 dir_inspect(
VALUE dir)
740 if (!
NIL_P(dirp->path)) {
754 #if defined(__sun) && !defined(HAVE_DIRFD)
755 # if defined(HAVE_DIR_D_FD)
756 # define dirfd(x) ((x)->d_fd)
757 # define HAVE_DIRFD 1
758 # elif defined(HAVE_DIR_DD_FD)
759 # define dirfd(x) ((x)->dd_fd)
760 # define HAVE_DIRFD 1
780 dir_fileno(
VALUE dir)
786 fd = dirfd(dirp->dir);
792 #define dir_fileno rb_f_notimplement
820 case ENCINDEX_ASCII_8BIT:
821 case ENCINDEX_US_ASCII:
828 # define READDIR(dir, enc) rb_w32_readdir((dir), (enc))
829 # define READDIR_NOGVL READDIR
831 NORETURN(
static void *sys_failure(
void *
function));
833 sys_failure(
void *
function)
839 nogvl_readdir(
void *dir)
842 if ((dir = readdir(dir)) == NULL) {
849 # define READDIR(dir, enc) IO_WITHOUT_GVL(nogvl_readdir, (void *)(dir))
850 # define READDIR_NOGVL(dir, enc) nogvl_readdir((dir))
855 to_be_skipped(
const struct dirent *dp)
857 const char *name = dp->d_name;
858 if (name[0] !=
'.')
return FALSE;
859 #ifdef HAVE_DIRENT_NAMLEN
860 switch (NAMLEN(dp)) {
862 if (name[1] !=
'.')
return FALSE;
869 if (!name[1])
return TRUE;
870 if (name[1] !=
'.')
return FALSE;
871 if (!name[2])
return TRUE;
898 if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
939 return dir_each_entry(dir, dir_yield,
Qnil, FALSE);
947 IF_NORMALIZE_UTF8PATH(
int norm_p);
950 rewinddir(dirp->dir);
951 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir,
RSTRING_PTR(dirp->path)));
952 while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
953 const char *name = dp->d_name;
954 size_t namlen = NAMLEN(dp);
957 if (children_only && name[0] ==
'.') {
958 if (namlen == 1)
continue;
959 if (namlen == 2 && name[1] ==
'.')
continue;
961 #if NORMALIZE_UTF8PATH
962 if (norm_p && has_nonascii(name, namlen) &&
963 !
NIL_P(path = rb_str_normalize_ospath(name, namlen))) {
964 path = rb_external_str_with_enc(path, dirp->enc);
995 if((pos = telldir(dirp->dir)) < 0)
1000 #define dir_tell rb_f_notimplement
1031 seekdir(dirp->dir, p);
1035 #define dir_seek rb_f_notimplement
1066 #define dir_set_pos rb_f_notimplement
1085 dir_rewind(
VALUE dir)
1090 rewinddir(dirp->dir);
1109 dir_close(
VALUE dir)
1113 dirp = dir_get(dir);
1114 if (!dirp->dir)
return Qnil;
1115 close_dir_data(dirp);
1121 nogvl_chdir(
void *
ptr)
1123 const char *path =
ptr;
1125 return (
void *)(
VALUE)chdir(path);
1129 dir_chdir0(
VALUE path)
1131 if (IO_WITHOUT_GVL_INT(nogvl_chdir, (
void*)
RSTRING_PTR(path)) < 0)
1132 rb_sys_fail_path(path);
1141 .blocking = 0, .thread =
Qnil,
1142 .path =
Qnil, .line = 0,
1148 if (chdir_lock.blocking == 0) {
1149 chdir_lock.path = rb_source_location(&chdir_lock.line);
1151 chdir_lock.blocking++;
1152 if (
NIL_P(chdir_lock.thread)) {
1160 chdir_lock.blocking--;
1161 if (chdir_lock.blocking == 0) {
1162 chdir_lock.thread =
Qnil;
1163 chdir_lock.path =
Qnil;
1164 chdir_lock.line = 0;
1169 chdir_alone_block_p(
void)
1172 if (chdir_lock.blocking > 0) {
1176 if (!
NIL_P(chdir_lock.path)) {
1177 rb_warn(
"conflicting chdir during another chdir block\n"
1178 "%" PRIsVALUE
":%d: note: previous chdir was here",
1179 chdir_lock.path, chdir_lock.line);
1182 rb_warn(
"conflicting chdir during another chdir block");
1190 VALUE old_path, new_path;
1196 chdir_yield(
VALUE v)
1199 dir_chdir0(args->new_path);
1206 chdir_restore(
VALUE v)
1211 dir_chdir0(args->old_path);
1217 chdir_path(
VALUE path,
bool yield_path)
1219 if (chdir_alone_block_p()) {
1223 args.new_path = path;
1225 args.yield_path = yield_path;
1230 int r = IO_WITHOUT_GVL_INT(nogvl_chdir, p);
1232 rb_sys_fail_path(path);
1302 dir_s_chdir(
int argc,
VALUE *argv,
VALUE obj)
1310 const char *dist = getenv(
"HOME");
1312 dist = getenv(
"LOGDIR");
1318 return chdir_path(path,
true);
1321 #if defined(HAVE_FCHDIR) && defined(HAVE_DIRFD) && HAVE_FCHDIR && HAVE_DIRFD
1323 nogvl_fchdir(
void *
ptr)
1325 const int *fd =
ptr;
1327 return (
void *)(
VALUE)fchdir(*fd);
1333 if (IO_WITHOUT_GVL_INT(nogvl_fchdir, (
void *)&fd) < 0)
1337 struct fchdir_data {
1344 fchdir_yield(
VALUE v)
1346 struct fchdir_data *args = (
void *)v;
1347 dir_fchdir(args->fd);
1354 fchdir_restore(
VALUE v)
1356 struct fchdir_data *args = (
void *)v;
1359 dir_fchdir(
RB_NUM2INT(dir_fileno(args->old_dir)));
1361 dir_close(args->old_dir);
1422 if (chdir_alone_block_p()) {
1423 struct fchdir_data args;
1424 args.old_dir = dir_s_alloc(klass);
1425 dir_initialize(NULL, args.old_dir, rb_fstring_cstr(
"."),
Qnil);
1431 int r = IO_WITHOUT_GVL_INT(nogvl_fchdir, &fd);
1439 #define dir_s_fchdir rb_f_notimplement
1466 dir_chdir(
VALUE dir)
1468 #if defined(HAVE_FCHDIR) && defined(HAVE_DIRFD) && HAVE_FCHDIR && HAVE_DIRFD
1469 return dir_s_fchdir(
rb_cDir, dir_fileno(dir));
1471 return chdir_path(dir_get(dir)->path,
false);
1477 rb_dir_getwd_ospath(
void)
1483 path_guard = rb_imemo_tmpbuf_auto_free_pointer();
1485 rb_imemo_tmpbuf_set_ptr(path_guard, path);
1487 cwd = rb_str_normalize_ospath(path, strlen(path));
1491 rb_free_tmp_buffer(&path_guard);
1501 VALUE cwd = rb_dir_getwd_ospath();
1504 case ENCINDEX_US_ASCII:
1505 fsenc = ENCINDEX_ASCII_8BIT;
1506 case ENCINDEX_ASCII_8BIT:
1508 #if defined _WIN32 || defined __APPLE__
1527 dir_s_getwd(
VALUE dir)
1533 check_dirname(
VALUE dir)
1545 if (pend - path <
len) {
1552 #if defined(HAVE_CHROOT)
1554 nogvl_chroot(
void *dirname)
1556 return (
void *)(
VALUE)chroot((
const char *)dirname);
1574 path = check_dirname(path);
1575 if (IO_WITHOUT_GVL_INT(nogvl_chroot, (
void *)
RSTRING_PTR(path)) == -1)
1576 rb_sys_fail_path(path);
1581 #define dir_s_chroot rb_f_notimplement
1590 nogvl_mkdir(
void *
ptr)
1594 return (
void *)(
VALUE)mkdir(m->path, m->mode);
1614 dir_s_mkdir(
int argc,
VALUE *argv,
VALUE obj)
1620 if (
rb_scan_args(argc, argv,
"11", &path, &vmode) == 2) {
1627 path = check_dirname(path);
1629 r = IO_WITHOUT_GVL_INT(nogvl_mkdir, &m);
1631 rb_sys_fail_path(path);
1637 nogvl_rmdir(
void *
ptr)
1639 const char *path =
ptr;
1641 return (
void *)(
VALUE)rmdir(path);
1660 dir = check_dirname(dir);
1662 r = IO_WITHOUT_GVL_INT(nogvl_rmdir, (
void *)p);
1664 rb_sys_fail_path(dir);
1670 #ifdef RUBY_FUNCTION_NAME_STRING
1677 #ifndef RUBY_FUNCTION_NAME_STRING
1678 #define sys_enc_warning_in(func, mesg, enc) sys_enc_warning(mesg, enc)
1682 sys_warning_1(
VALUE mesg)
1685 #ifdef RUBY_FUNCTION_NAME_STRING
1686 rb_sys_enc_warning(arg->enc,
"%s: %s", arg->func, arg->mesg);
1688 rb_sys_enc_warning(arg->enc,
"%s", arg->mesg);
1694 sys_enc_warning_in(
const char *func,
const char *mesg,
rb_encoding *enc)
1697 #ifdef RUBY_FUNCTION_NAME_STRING
1705 #define GLOB_VERBOSE (1U << (sizeof(int) * CHAR_BIT - 1))
1706 #define sys_warning(val, enc) \
1707 ((flags & GLOB_VERBOSE) ? sys_enc_warning_in(RUBY_FUNCTION_NAME_STRING, (val), (enc)) :(void)0)
1709 static inline size_t
1710 glob_alloc_size(
size_t x,
size_t y)
1721 static inline void *
1722 glob_alloc_n(
size_t x,
size_t y)
1724 return malloc(glob_alloc_size(x, y));
1727 static inline void *
1728 glob_realloc_n(
void *p,
size_t x,
size_t y)
1730 return realloc(p, glob_alloc_size(x, y));
1733 #define GLOB_ALLOC(type) ((type *)malloc(sizeof(type)))
1734 #define GLOB_ALLOC_N(type, n) ((type *)glob_alloc_n(sizeof(type), n))
1735 #define GLOB_REALLOC(ptr, size) realloc((ptr), (size))
1736 #define GLOB_REALLOC_N(ptr, n) glob_realloc_n(ptr, sizeof(*(ptr)), n)
1737 #define GLOB_FREE(ptr) free(ptr)
1738 #define GLOB_JUMP_TAG(status) (((status) == -1) ? rb_memerror() : rb_jump_tag(status))
1744 ALWAYS_INLINE(
static int to_be_ignored(
int e));
1746 to_be_ignored(
int e)
1748 return e == ENOENT || e == ENOTDIR;
1752 #define STAT(args) (int)(VALUE)nogvl_stat(&(args))
1753 #define LSTAT(args) (int)(VALUE)nogvl_lstat(&(args))
1755 #define STAT(args) IO_WITHOUT_GVL_INT(nogvl_stat, (void *)&(args))
1756 #define LSTAT(args) IO_WITHOUT_GVL_INT(nogvl_lstat, (void *)&(args))
1759 typedef int ruby_glob_errfunc(
const char*,
VALUE,
const void*,
int);
1762 ruby_glob_errfunc *error;
1766 at_subpath(
int fd,
size_t baselen,
const char *path)
1769 if (fd != (
int)AT_FDCWD && baselen > 0) {
1771 if (*path ==
'/') ++path;
1774 return *path ? path :
".";
1778 struct fstatat_args {
1786 nogvl_fstatat(
void *args)
1788 struct fstatat_args *arg = (
struct fstatat_args *)args;
1789 return (
void *)(
VALUE)fstatat(arg->fd, arg->path, arg->pst, arg->flag);
1798 nogvl_stat(
void *args)
1801 return (
void *)(
VALUE)stat(arg->path, arg->pst);
1807 do_stat(
int fd,
size_t baselen,
const char *path,
struct stat *pst,
int flags,
rb_encoding *enc)
1810 struct fstatat_args args;
1815 int ret = IO_WITHOUT_GVL_INT(nogvl_fstatat, (
void *)&args);
1820 int ret = STAT(args);
1822 if (ret < 0 && !to_be_ignored(
errno))
1823 sys_warning(path, enc);
1828 #if defined HAVE_LSTAT || defined lstat || USE_OPENDIR_AT
1831 nogvl_lstat(
void *args)
1834 return (
void *)(
VALUE)lstat(arg->path, arg->pst);
1839 do_lstat(
int fd,
size_t baselen,
const char *path,
struct stat *pst,
int flags,
rb_encoding *enc)
1842 struct fstatat_args args;
1846 args.flag = AT_SYMLINK_NOFOLLOW;
1847 int ret = IO_WITHOUT_GVL_INT(nogvl_fstatat, (
void *)&args);
1852 int ret = LSTAT(args);
1854 if (ret < 0 && !to_be_ignored(
errno))
1855 sys_warning(path, enc);
1860 #define do_lstat do_stat
1869 with_gvl_gc_for_fd(
void *
ptr)
1873 return (
void *)RBOOL(rb_gc_for_fd(*e));
1877 gc_for_fd_with_gvl(
int e)
1882 return RBOOL(rb_gc_for_fd(e));
1886 nogvl_opendir_at(
void *
ptr)
1892 const int opendir_flags = (O_RDONLY|O_CLOEXEC|
1897 int fd = openat(oaa->basefd, oaa->path, opendir_flags);
1899 dirp = fd >= 0 ? fdopendir(fd) : 0;
1903 switch (gc_for_fd_with_gvl(e)) {
1905 if (fd < 0) fd = openat(oaa->basefd, oaa->path, opendir_flags);
1906 if (fd >= 0) dirp = fdopendir(fd);
1907 if (dirp)
return dirp;
1912 if (fd >= 0) close(fd);
1917 dirp = opendir(oaa->path);
1918 if (!dirp && gc_for_fd_with_gvl(
errno))
1919 dirp = opendir(oaa->path);
1926 opendir_at(
int basefd,
const char *path)
1930 oaa.basefd = basefd;
1934 return IO_WITHOUT_GVL(nogvl_opendir_at, &oaa);
1936 return nogvl_opendir_at(&oaa);
1940 do_opendir(
const int basefd,
size_t baselen,
const char *path,
int flags,
rb_encoding *enc,
1941 ruby_glob_errfunc *errfunc,
VALUE arg,
int *status)
1946 if (!fundamental_encoding_p(enc)) {
1952 dirp = opendir_at(basefd, at_subpath(basefd, baselen, path));
1957 if (!to_be_ignored(e)) {
1959 *status = (*errfunc)(path, arg, enc, e);
1962 sys_warning(path, enc);
1974 enum glob_pattern_type { PLAIN, ALPHA, BRACE, MAGICAL, RECURSIVE, MATCH_ALL, MATCH_DIR };
1977 static enum glob_pattern_type
1978 has_magic(
const char *p,
const char *pend,
int flags,
rb_encoding *enc)
1980 const int escape = !(flags & FNM_NOESCAPE);
1986 while (p < pend && (c = *p++) != 0) {
1998 if (escape && p++ >= pend)
2017 p = Next(p-1, pend, enc);
2020 return hasmagical ? MAGICAL : hasalpha ? ALPHA : PLAIN;
2025 find_dirsep(
const char *p,
const char *pend,
int flags,
rb_encoding *enc)
2027 const int escape = !(flags & FNM_NOESCAPE);
2032 while ((c = *p++) != 0) {
2054 if (escape && !(c = *p++))
2059 p = Next(p-1, pend, enc);
2067 remove_backslashes(
char *p,
register const char *pend,
rb_encoding *enc)
2075 memmove(t, s, p - s);
2086 memmove(t, s, p - s);
2093 enum glob_pattern_type type;
2097 static void glob_free_pattern(
struct glob_pattern *list);
2100 glob_make_pattern(
const char *p,
const char *e,
int flags,
rb_encoding *enc)
2106 while (p < e && *p) {
2108 if (!tmp)
goto error;
2109 if (p + 2 < e && p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/') {
2111 do { p += 3;
while (*p ==
'/') p++; }
while (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/');
2112 tmp->type = RECURSIVE;
2118 const char *m = find_dirsep(p, e, flags, enc);
2119 const enum glob_pattern_type magic = has_magic(p, m, flags, enc);
2120 const enum glob_pattern_type non_magic = (USE_NAME_ON_FS || FNM_SYSCASE) ? PLAIN : ALPHA;
2123 if (!(FNM_SYSCASE || magic > non_magic) && !recursive && *m) {
2125 while (has_magic(m+1, m2 = find_dirsep(m+1, e, flags, enc), flags, enc) <= non_magic &&
2130 buf = GLOB_ALLOC_N(
char, m-p+1);
2135 memcpy(buf, p, m-p);
2137 tmp->type = magic > MAGICAL ? MAGICAL : magic > non_magic ? magic : PLAIN;
2156 tmp->type = dirsep ? MATCH_DIR : MATCH_ALL;
2165 glob_free_pattern(list);
2176 GLOB_FREE(tmp->str);
2182 join_path(
const char *path,
size_t len,
int dirsep,
const char *name,
size_t namlen)
2184 char *buf = GLOB_ALLOC_N(
char,
len+namlen+(dirsep?1:0)+1);
2187 memcpy(buf, path,
len);
2191 memcpy(buf+
len, name, namlen);
2192 buf[
len+namlen] =
'\0';
2196 #ifdef HAVE_GETATTRLIST
2197 # if defined HAVE_FGETATTRLIST
2198 # define is_case_sensitive(dirp, path) is_case_sensitive(dirp)
2200 # define is_case_sensitive(dirp, path) is_case_sensitive(path)
2203 is_case_sensitive(
DIR *dirp,
const char *path)
2207 vol_capabilities_attr_t cap[1];
2209 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, 0, ATTR_VOL_INFO|ATTR_VOL_CAPABILITIES};
2210 const vol_capabilities_attr_t *
const cap = attrbuf[0].cap;
2211 const int idx = VOL_CAPABILITIES_FORMAT;
2212 const uint32_t mask = VOL_CAP_FMT_CASE_SENSITIVE;
2213 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, FSOPT_NOFOLLOW);
2214 # if defined HAVE_FGETATTRLIST
2215 int ret = gvl_fgetattrlist(&args, dirfd(dirp));
2217 int ret = gvl_getattrlist(&args, path);
2222 if (!(cap->valid[idx] & mask))
2224 return (cap->capabilities[idx] & mask) != 0;
2228 replace_real_basename(
char *path,
long base,
rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
2232 attrreference_t ref[1];
2233 fsobj_type_t objtype;
2234 char path[MAXPATHLEN * 3];
2236 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_NAME|ATTR_CMN_OBJTYPE};
2237 const attrreference_t *
const ar = attrbuf[0].ref;
2241 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
2244 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, FSOPT_NOFOLLOW);
2245 if (gvl_getattrlist(&args, path)) {
2246 if (!to_be_ignored(
errno))
2247 sys_warning(path, enc);
2251 switch (attrbuf[0].objtype) {
2252 case VREG: *
type = path_regular;
break;
2253 case VDIR: *
type = path_directory;
break;
2254 case VLNK: *
type = path_symlink;
break;
2255 default: *
type = path_exist;
break;
2257 name = (
char *)ar + ar->attr_dataoffset;
2258 len = (
long)ar->attr_length - 1;
2259 if (name +
len > (
char *)attrbuf +
sizeof(attrbuf))
2262 # if NORMALIZE_UTF8PATH
2263 if (norm_p && has_nonascii(name,
len)) {
2264 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name,
len))) {
2270 tmp = GLOB_REALLOC(path, base +
len + 1);
2273 memcpy(path + base, name,
len);
2274 path[base +
len] =
'\0';
2279 #elif defined _WIN32
2281 int rb_w32_reparse_symlink_p(
const WCHAR *path);
2284 replace_real_basename(
char *path,
long base,
rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
2286 char *plainname = path;
2287 volatile VALUE tmp = 0;
2288 WIN32_FIND_DATAW fd;
2289 WIN32_FILE_ATTRIBUTE_DATA fa;
2291 HANDLE h = INVALID_HANDLE_VALUE;
2294 if (!fundamental_encoding_p(enc)) {
2299 wplain = rb_w32_mbstr_to_wstr(CP_UTF8, plainname, -1, &wlen);
2301 if (!wplain)
return path;
2302 if (GetFileAttributesExW(wplain, GetFileExInfoStandard, &fa)) {
2303 h = FindFirstFileW(wplain, &fd);
2304 e = rb_w32_map_errno(GetLastError());
2306 if (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2307 if (!rb_w32_reparse_symlink_p(wplain))
2308 fa.dwFileAttributes &= ~FILE_ATTRIBUTE_REPARSE_POINT;
2311 if (h == INVALID_HANDLE_VALUE) {
2313 if (e && !to_be_ignored(e)) {
2315 sys_warning(path, enc);
2321 (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ? path_symlink :
2322 (fa.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? path_directory :
2326 tmp = rb_w32_conv_from_wchar(fd.cFileName, enc);
2328 buf = GLOB_REALLOC(path, base + wlen + 1);
2332 path[base + wlen] = 0;
2338 wlen = WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, NULL, 0, NULL, NULL);
2339 utf8filename = GLOB_REALLOC(0, wlen);
2342 WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, utf8filename, wlen, NULL, NULL);
2343 buf = GLOB_REALLOC(path, base + wlen + 1);
2346 memcpy(path + base, utf8filename, wlen);
2347 path[base + wlen] = 0;
2349 GLOB_FREE(utf8filename);
2354 #elif USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2355 # error not implemented
2359 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
2364 # define S_ISLNK(m) (0)
2366 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
2371 void (*func)(
const char *,
VALUE,
void *);
2379 #define glob_call_func(func, path, arg, enc) (*(func))((path), (arg), (void *)(enc))
2382 glob_func_caller(
VALUE val)
2386 glob_call_func(args->func, args->path, args->value, args->enc);
2397 glob_func_warning(
VALUE val)
2400 rb_syserr_enc_warning(arg->error, arg->enc,
"%s", arg->path);
2406 rb_glob_warning(
const char *path,
VALUE a,
const void *enc,
int error)
2419 NORETURN(
static VALUE glob_func_error(
VALUE val));
2422 glob_func_error(
VALUE val)
2431 rb_glob_error(
const char *path,
VALUE a,
const void *enc,
int error)
2442 errfunc = glob_func_warning;
2455 const char *d_altname;
2463 if (fnmatch(pat, enc, name, flags) == 0)
return 1;
2465 if (dp->d_altname && (flags & FNM_SHORTNAME)) {
2466 if (fnmatch(pat, enc, dp->d_altname, flags) == 0)
return 1;
2478 rb_pathtype_t pathtype;
2491 dirent_match_brace(
const char *pattern,
VALUE val,
void *enc)
2495 return dirent_match(pattern, enc, arg->name, arg->dp, arg->flags);
2504 size_t path_len = 0;
2506 for (p = *beg; p; p = p->next) {
2521 path_len = strlen(str);
2522 path = GLOB_ALLOC_N(
char, path_len + 1);
2524 memcpy(path, str, path_len);
2525 path[path_len] =
'\0';
2529 size_t len = strlen(str);
2531 tmp = GLOB_REALLOC(path, path_len +
len + 2);
2534 path[path_len++] =
'/';
2535 memcpy(path + path_len, str,
len);
2537 path[path_len] =
'\0';
2544 static int push_caller(
const char *path,
VALUE val,
void *enc);
2549 static const size_t rb_dirent_name_offset =
2553 dirent_copy(
const struct dirent *dp,
rb_dirent_t *rdp)
2555 if (!dp)
return NULL;
2556 size_t namlen = NAMLEN(dp);
2557 const size_t altlen =
2559 dp->d_altlen ? dp->d_altlen + 1 :
2563 if (!rdp && !(newrdp = malloc(rb_dirent_name_offset + namlen + 1 + altlen)))
2565 newrdp->d_namlen = namlen;
2567 char *name = (
char *)newrdp + rb_dirent_name_offset;
2568 memcpy(name, dp->d_name, namlen);
2569 name[namlen] =
'\0';
2571 newrdp->d_altname = NULL;
2573 char *
const altname = name + namlen + 1;
2574 memcpy(altname, dp->d_altname, altlen - 1);
2575 altname[altlen - 1] =
'\0';
2576 newrdp->d_altname = altname;
2579 newrdp->d_name = name;
2582 newrdp->d_name = dp->d_name;
2584 newrdp->d_altname = dp->d_altname;
2588 newrdp->d_type = dp->d_type;
2607 glob_sort_cmp(
const void *a,
const void *b,
void *e)
2611 return strcmp(ent1->d_name, ent2->d_name);
2617 if (flags & FNM_GLOB_NOSORT) {
2618 check_closedir(ent->nosort.dirp);
2619 ent->nosort.dirp = NULL;
2621 else if (ent->sort.entries) {
2622 for (
size_t i = 0, count = ent->sort.count; i < count;) {
2623 GLOB_FREE(ent->sort.entries[i++]);
2625 GLOB_FREE(ent->sort.entries);
2626 ent->sort.entries = NULL;
2627 ent->sort.count = ent->sort.idx = 0;
2635 if (flags & FNM_GLOB_NOSORT) {
2636 ent->nosort.dirp = dirp;
2642 size_t count = 0, capacity = 0;
2643 ent->sort.count = 0;
2645 ent->sort.entries = 0;
2647 if ((capacity = dirp->nfiles) > 0) {
2648 if (!(newp = GLOB_ALLOC_N(
rb_dirent_t, capacity))) {
2649 check_closedir(dirp);
2652 ent->sort.entries = newp;
2655 while ((dp = READDIR(dirp, enc)) != NULL) {
2660 if (count >= capacity) {
2662 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, capacity)))
2664 ent->sort.entries = newp;
2666 ent->sort.entries[count++] = rdp;
2667 ent->sort.count = count;
2669 check_closedir(dirp);
2670 if (count < capacity) {
2671 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, count))) {
2672 glob_dir_finish(ent, 0);
2675 ent->sort.entries = newp;
2677 ruby_qsort(ent->sort.entries, ent->sort.count,
sizeof(ent->sort.entries[0]),
2678 glob_sort_cmp, NULL);
2683 glob_dir_finish(ent, 0);
2684 check_closedir(dirp);
2691 if (flags & FNM_GLOB_NOSORT) {
2692 return dirent_copy(READDIR(ent->nosort.dirp, enc), &ent->nosort.ent);
2694 else if (ent->sort.idx < ent->sort.count) {
2695 return ent->sort.entries[ent->sort.idx++];
2709 rb_pathtype_t pathtype,
2720 int plain = 0, brace = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0;
2721 int escape = !(flags & FNM_NOESCAPE);
2722 size_t pathlen = baselen + namelen;
2724 rb_check_stack_overflow();
2726 for (cur = beg; cur < end; ++cur) {
2728 if (p->type == RECURSIVE) {
2737 #if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2744 if (!recursive || strchr(p->str,
'/')) {
2758 rb_bug(
"continuous RECURSIVEs");
2764 char* brace_path = join_path_from_pattern(beg);
2765 if (!brace_path)
return -1;
2768 args.baselen = baselen;
2769 args.namelen = namelen;
2770 args.dirsep = dirsep;
2771 args.pathtype = pathtype;
2775 status = ruby_brace_expand(brace_path, flags, push_caller, (
VALUE)&args, enc,
Qfalse);
2776 GLOB_FREE(brace_path);
2781 if (match_all && pathtype == path_unknown) {
2782 if (do_lstat(fd, baselen, path, &st, flags, enc) == 0) {
2783 pathtype = IFTODT(st.st_mode);
2786 pathtype = path_noent;
2789 if (match_dir && (pathtype == path_unknown || pathtype == path_symlink)) {
2790 if (do_stat(fd, baselen, path, &st, flags, enc) == 0) {
2791 pathtype = IFTODT(st.st_mode);
2794 pathtype = path_noent;
2797 if (match_all && pathtype > path_noent) {
2798 const char *subpath = path + baselen + (baselen && path[baselen] ==
'/');
2799 status = glob_call_func(funcs->match, subpath, arg, enc);
2800 if (status)
return status;
2802 if (match_dir && pathtype == path_directory) {
2803 int seplen = (baselen && path[baselen] ==
'/');
2804 const char *subpath = path + baselen + seplen;
2805 char *tmp = join_path(subpath, namelen - seplen, dirsep,
"", 0);
2806 if (!tmp)
return -1;
2807 status = glob_call_func(funcs->match, tmp, arg, enc);
2809 if (status)
return status;
2813 if (pathtype == path_noent)
return 0;
2815 if (magical || recursive) {
2818 # if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2819 char *plainname = 0;
2821 IF_NORMALIZE_UTF8PATH(
int norm_p);
2822 # if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2823 if (cur + 1 == end && (*cur)->type <= ALPHA) {
2824 plainname = join_path(path, pathlen, dirsep, (*cur)->str, strlen((*cur)->str));
2825 if (!plainname)
return -1;
2826 dirp = do_opendir(fd, basename, plainname, flags, enc, funcs->error, arg, &status);
2827 GLOB_FREE(plainname);
2833 dirp = do_opendir(fd, baselen, path, flags, enc, funcs->error, arg, &status);
2835 # if FNM_SYSCASE || NORMALIZE_UTF8PATH
2836 if ((magical < 2) && !recursive && (
errno == EACCES)) {
2843 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *path ? path :
"."));
2845 # if NORMALIZE_UTF8PATH
2846 if (!(norm_p || magical || recursive)) {
2847 check_closedir(dirp);
2851 # ifdef HAVE_GETATTRLIST
2852 if (is_case_sensitive(dirp, path) == 0)
2853 flags |= FNM_CASEFOLD;
2856 if (!glob_opendir(&globent, dirp, flags, enc)) {
2859 status = (*funcs->error)(path, arg, enc, ENOMEM);
2862 sys_warning(path, enc);
2867 int skipdot = (flags & FNM_GLOB_SKIPDOT);
2868 flags |= FNM_GLOB_SKIPDOT;
2870 while ((dp = glob_getent(&globent, flags, enc)) != NULL) {
2872 rb_pathtype_t new_pathtype = path_unknown;
2876 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
2879 namlen = dp->d_namlen;
2880 if (name[0] ==
'.') {
2884 if (recursive && !(flags & FNM_DOTMATCH))
continue;
2885 if (skipdot)
continue;
2887 new_pathtype = path_directory;
2889 else if (namlen == 2 && name[1] ==
'.') {
2895 # if NORMALIZE_UTF8PATH
2896 if (norm_p && has_nonascii(name, namlen)) {
2897 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name, namlen))) {
2902 buf = join_path(path, pathlen, dirsep, name, namlen);
2908 name = buf + pathlen + (dirsep != 0);
2910 if (dp->d_type != DT_UNKNOWN) {
2912 new_pathtype = dp->d_type;
2915 if (recursive && dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1) &&
2916 new_pathtype == path_unknown) {
2918 if (do_lstat(fd, baselen, buf, &st, flags, enc) == 0)
2919 new_pathtype = IFTODT(st.st_mode);
2921 new_pathtype = path_noent;
2924 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, (end - beg) * 2);
2931 for (cur = beg; cur < end; ++cur) {
2934 if (p->type == RECURSIVE) {
2935 if (new_pathtype == path_directory ||
2936 new_pathtype == path_exist) {
2937 if (dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1))
2947 if (ruby_brace_expand(p->str, flags, dirent_match_brace,
2949 *new_end++ = p->next;
2952 # if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2954 *new_end++ = p->next;
2960 if (dirent_match(p->str, enc, name, dp, flags))
2961 *new_end++ = p->next;
2967 status = glob_helper(fd, buf, baselen, name - buf - baselen + namlen, 1,
2968 new_pathtype, new_beg, new_end,
2969 flags, funcs, arg, enc);
2975 glob_dir_finish(&globent, flags);
2980 # if FNM_SYSCASE || NORMALIZE_UTF8PATH
2983 copy_beg = copy_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
2984 if (!copy_beg)
return -1;
2985 for (cur = beg; cur < end; ++cur)
2986 *copy_end++ = (*cur)->type <= ALPHA ? *cur : 0;
2988 for (cur = copy_beg; cur < copy_end; ++cur) {
2990 rb_pathtype_t new_pathtype = path_unknown;
2993 size_t len = strlen((*cur)->str) + 1;
2994 name = GLOB_ALLOC_N(
char,
len);
2999 memcpy(name, (*cur)->str,
len);
3001 len = remove_backslashes(name, name+
len-1, enc) - name;
3003 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
3009 *new_end++ = (*cur)->next;
3010 for (cur2 = cur + 1; cur2 < copy_end; ++cur2) {
3011 if (*cur2 && fnmatch((*cur2)->str, enc, name, flags) == 0) {
3012 *new_end++ = (*cur2)->next;
3017 buf = join_path(path, pathlen, dirsep, name,
len);
3024 #if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
3025 if ((*cur)->type == ALPHA) {
3026 buf = replace_real_basename(buf, pathlen + (dirsep != 0), enc,
3027 IF_NORMALIZE_UTF8PATH(1)+0,
3028 flags, &new_pathtype);
3032 status = glob_helper(fd, buf, baselen,
3033 namelen + strlen(buf + pathlen), 1,
3034 new_pathtype, new_beg, new_end,
3035 flags, funcs, arg, enc);
3042 GLOB_FREE(copy_beg);
3049 push_caller(
const char *path,
VALUE val,
void *enc)
3055 list = glob_make_pattern(path, path + strlen(path), arg->flags, enc);
3059 status = glob_helper(arg->fd, arg->path, arg->baselen, arg->namelen, arg->dirsep,
3060 arg->pathtype, &list, &list + 1, arg->flags, arg->funcs,
3062 glob_free_pattern(list);
3066 static int ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
3078 push_glob0_caller(
const char *path,
VALUE val,
void *enc)
3081 return ruby_glob0(path, arg->fd, arg->base, arg->flags, arg->funcs, arg->arg, enc);
3085 ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
3090 const char *root, *start;
3092 size_t n, baselen = 0;
3093 int status, dirsep = FALSE;
3095 start = root = path;
3104 return ruby_brace_expand(path, flags, push_glob0_caller, (
VALUE)&args, enc,
Qfalse);
3107 flags |= FNM_SYSCASE;
3112 if (*root ==
'/') root++;
3121 buf = GLOB_ALLOC_N(
char, n + 1);
3122 if (!buf)
return -1;
3123 MEMCPY(buf, start,
char, n);
3126 list = glob_make_pattern(root, root + strlen(root), flags, enc);
3131 status = glob_helper(fd, buf, baselen, n-baselen, dirsep,
3132 path_unknown, &list, &list + 1,
3133 flags, funcs, arg, enc);
3134 glob_free_pattern(list);
3146 return ruby_glob0(path, AT_FDCWD, 0, flags & ~GLOB_VERBOSE,
3151 rb_glob_caller(
const char *path,
VALUE a,
void *enc)
3162 rb_glob_caller, rb_glob_error,
3175 status = ruby_glob0(path, AT_FDCWD, 0, GLOB_VERBOSE, &rb_glob_funcs,
3176 (
VALUE)&args, args.enc);
3177 if (status) GLOB_JUMP_TAG(status);
3181 push_pattern(
const char *path,
VALUE ary,
void *enc)
3183 #if defined _WIN32 || defined __APPLE__
3197 const int escape = !(flags & FNM_NOESCAPE);
3198 const char *p = str;
3199 const char *pend = p + strlen(p);
3201 const char *lbrace = 0, *rbrace = 0;
3202 int nest = 0, status = 0;
3205 if (*p ==
'{' && nest++ == 0) {
3208 if (*p ==
'}' && lbrace && --nest == 0) {
3212 if (*p ==
'\\' && escape) {
3218 if (lbrace && rbrace) {
3219 size_t len = strlen(s) + 1;
3220 char *buf = GLOB_ALLOC_N(
char,
len);
3223 if (!buf)
return -1;
3224 memcpy(buf, s, lbrace-s);
3227 while (p < rbrace) {
3228 const char *t = ++p;
3230 while (p < rbrace && !(*p ==
',' && nest == 0)) {
3231 if (*p ==
'{') nest++;
3232 if (*p ==
'}') nest--;
3233 if (*p ==
'\\' && escape) {
3234 if (++p == rbrace)
break;
3238 memcpy(buf+shift, t, p-t);
3239 strlcpy(buf+shift+(p-t), rbrace+1,
len-(shift+(p-t)));
3240 status = ruby_brace_expand(buf, flags, func, arg, enc, var);
3245 else if (!lbrace && !rbrace) {
3246 status = glob_call_func(func, s, arg, enc);
3260 glob_brace(
const char *path,
VALUE val,
void *enc)
3264 return ruby_glob0(path, AT_FDCWD, 0, arg->flags, &arg->funcs, arg->value, enc);
3272 flags &= ~GLOB_VERBOSE;
3273 args.funcs.match = func;
3274 args.funcs.error = 0;
3277 return ruby_brace_expand(str, flags, glob_brace, (
VALUE)&args, enc,
Qfalse);
3293 #if defined _WIN32 || defined __APPLE__
3300 flags |= GLOB_VERBOSE;
3301 args.func = push_pattern;
3308 struct dir_data *dirp = RTYPEDDATA_GET_DATA(base);
3309 if (!dirp->dir) dir_closed();
3311 if ((fd = dirfd(dirp->dir)) == -1)
3312 rb_sys_fail_path(dir_inspect(base));
3318 #if defined _WIN32 || defined __APPLE__
3322 return ruby_glob0(
RSTRING_PTR(str), fd, args.base, flags, &rb_glob_funcs,
3327 rb_push_glob(
VALUE str,
VALUE base,
int flags)
3336 else if (!rb_str_to_cstr(str)) {
3344 status = push_glob(ary, str, base, flags);
3345 if (status) GLOB_JUMP_TAG(status);
3360 status = push_glob(ary, str, base, flags);
3361 if (status) GLOB_JUMP_TAG(status);
3369 dir_glob_option_base(
VALUE base)
3371 if (NIL_OR_UNDEF_P(base)) {
3385 dir_glob_option_sort(
VALUE sort)
3387 return (rb_bool_expected(sort,
"sort", TRUE) ? 0 : FNM_GLOB_NOSORT);
3393 const int flags = dir_glob_option_sort(sort);
3394 base = dir_glob_option_base(base);
3396 return rb_push_glob(
RARRAY_AREF(args, 0), base, flags);
3398 return dir_globs(args, base, flags);
3405 const int flags = (
NUM2INT(rflags) | dir_glob_option_sort(sort)) & ~FNM_CASEFOLD;
3406 base = dir_glob_option_base(base);
3408 ary = rb_push_glob(str, base, flags);
3411 ary = dir_globs(ary, base, flags);
3422 dir_open_dir(
int argc,
VALUE *argv)
3468 dir = dir_open_dir(argc, argv);
3469 rb_ensure(dir_each, dir, dir_close, dir);
3474 dir_collect(
VALUE dir)
3503 dir = dir_open_dir(argc, argv);
3504 return rb_ensure(dir_collect, dir, dir_close, dir);
3508 dir_each_child(
VALUE dir)
3510 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3522 dir_s_each_child(
int argc,
VALUE *argv,
VALUE io)
3527 dir = dir_open_dir(argc, argv);
3528 rb_ensure(dir_each_child, dir, dir_close, dir);
3551 dir_each_child_m(
VALUE dir)
3554 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3569 dir_collect_children(
VALUE dir)
3596 dir_s_children(
int argc,
VALUE *argv,
VALUE io)
3600 dir = dir_open_dir(argc, argv);
3601 return rb_ensure(dir_collect_children, dir, dir_close, dir);
3605 fnmatch_brace(
const char *pattern,
VALUE val,
void *enc)
3608 VALUE path = arg->value;
3612 if (enc_pattern != enc_path) {
3619 long len = strlen(pattern);
3621 enc_pattern, &cr) !=
len)
3627 return (fnmatch(pattern, enc,
RSTRING_PTR(path), arg->flags) == 0);
3632 file_s_fnmatch(
int argc,
VALUE *argv,
VALUE obj)
3634 VALUE pattern, path;
3638 if (
rb_scan_args(argc, argv,
"21", &pattern, &path, &rflags) == 3)
3646 if (flags & FNM_EXTGLOB) {
3651 if (ruby_brace_expand(
RSTRING_PTR(pattern), flags, fnmatch_brace,
3685 user = (argc > 0) ? argv[0] :
Qnil;
3691 return rb_home_dir_of(user,
rb_str_new(0, 0));
3694 return rb_default_home_dir(
rb_str_new(0, 0));
3719 nogvl_dir_empty_p(
void *
ptr)
3721 const char *path =
ptr;
3722 DIR *dir = opendir(path);
3728 switch (gc_for_fd_with_gvl(e)) {
3730 dir = opendir(path);
3735 if (e == ENOTDIR)
return (
void *)
Qfalse;
3739 while ((dp = READDIR_NOGVL(dir, NULL)) != NULL) {
3740 if (!to_be_skipped(dp)) {
3745 check_closedir(dir);
3746 return (
void *)result;
3769 enum {false_on_notdir = 1};
3777 #if defined HAVE_GETATTRLIST && defined ATTR_DIR_ENTRYCOUNT
3779 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
3780 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
3781 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, 0);
3782 if (gvl_getattrlist(&args, path) != 0)
3783 rb_sys_fail_path(orig);
3784 if (*(
const fsobj_tag_t *)(attrbuf+1) == VT_HFS) {
3786 al.dirattr = ATTR_DIR_ENTRYCOUNT;
3787 if (gvl_getattrlist(&args, path) == 0) {
3788 if (attrbuf[0] >= 2 *
sizeof(u_int32_t))
3789 return RBOOL(attrbuf[1] == 0);
3790 if (false_on_notdir)
return Qfalse;
3792 rb_sys_fail_path(orig);
3797 result = (
VALUE)IO_WITHOUT_GVL(nogvl_dir_empty_p, (
void *)path);
3799 rb_syserr_fail_path((
int)
FIX2LONG(result), orig);
3856 rb_file_const(
"FNM_NOESCAPE",
INT2FIX(FNM_NOESCAPE));
3859 rb_file_const(
"FNM_PATHNAME",
INT2FIX(FNM_PATHNAME));
3862 rb_file_const(
"FNM_DOTMATCH",
INT2FIX(FNM_DOTMATCH));
3865 rb_file_const(
"FNM_CASEFOLD",
INT2FIX(FNM_CASEFOLD));
3868 rb_file_const(
"FNM_EXTGLOB",
INT2FIX(FNM_EXTGLOB));
3871 rb_file_const(
"FNM_SYSCASE",
INT2FIX(FNM_SYSCASE));
3874 rb_file_const(
"FNM_SHORTNAME",
INT2FIX(FNM_SHORTNAME));
3877 #include "dir.rbinc"
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
int rb_enc_toupper(int c, rb_encoding *enc)
Identical to rb_toupper(), except it additionally takes an encoding.
int ruby_glob(const char *pattern, int flags, ruby_glob_func *func, VALUE arg)
Identical to rb_glob(), except it returns opaque exception states instead of raising exceptions.
void rb_glob(const char *pattern, void(*func)(const char *path, VALUE arg, void *enc), VALUE arg)
The "glob" operator.
int ruby_brace_glob(const char *pattern, int flags, ruby_glob_func *func, VALUE arg)
Identical to ruby_glob(), @shyouhei currently suspects.
int ruby_glob_func(const char *path, VALUE arg, void *enc)
Type of a glob callback function.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
#define T_STRING
Old name of RUBY_T_STRING.
#define INT2FIX
Old name of RB_INT2FIX.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define ISALPHA
Old name of rb_isalpha.
#define ISASCII
Old name of rb_isascii.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
void rb_raise(VALUE exc_class, const char *fmt,...)
Exception entry point.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
void rb_sys_fail(const char *mesg)
Converts a C errno into a Ruby exception, then raises it.
VALUE rb_eIOError
IOError exception.
void rb_syserr_fail_str(int e, VALUE mesg)
Identical to rb_syserr_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_cFile
File class.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
Queries the number of bytes of the character at the passed pointer.
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
rb_encoding * rb_enc_compatible(VALUE str1, VALUE str2)
Look for the "common" encoding between the two.
rb_encoding * rb_default_internal_encoding(void)
Queries the "default internal" encoding.
int rb_enc_to_index(rb_encoding *enc)
Queries the index of the encoding.
rb_encoding * rb_to_encoding(VALUE obj)
Identical to rb_find_encoding(), except it raises an exception instead of returning NULL.
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
Identical to rb_enc_compatible(), except it raises an exception instead of returning NULL.
VALUE rb_enc_from_encoding(rb_encoding *enc)
Queries the Ruby-level counterpart instance of rb_cEncoding that corresponds to the passed encoding.
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
static unsigned int rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
Queries the code point of character pointed by the passed pointer.
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
Queries the number of bytes of the character at the passed pointer.
VALUE rb_enc_associate_index(VALUE obj, int encindex)
Identical to rb_enc_set_index(), except it additionally does contents fix-up depending on the passed ...
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
rb_encoding * rb_filesystem_encoding(void)
Queries the "filesystem" encoding.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
VALUE rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_str_new(), except it additionally takes an encoding.
VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *enc)
Identical to rb_external_str_new(), except it additionally takes an encoding.
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
long rb_str_coderange_scan_restartable(const char *str, const char *end, rb_encoding *enc, int *cr)
Scans the passed string until it finds something odd.
VALUE rb_funcallv_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_funcallv(), except you can specify how to handle the last element of the given array.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
void rb_memerror(void)
Triggers out-of-memory error.
void rb_gc_register_address(VALUE *valptr)
Inform the garbage collector that the global or static variable pointed by valptr stores a live Ruby ...
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_each(VALUE ary)
Iteratively yields each element of the passed array to the implicitly passed block if any.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_dir_getwd(void)
Queries the path of the current working directory of the current process.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_str_encode_ospath(VALUE path)
Converts a string into an "OS Path" encoding, if any.
VALUE rb_file_directory_p(VALUE _, VALUE path)
Queries if the given path is either a directory, or a symlink that (potentially recursively) points t...
VALUE rb_protect(VALUE(*func)(VALUE args), VALUE args, int *state)
Protects a function call from potential global escapes from the function.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_utf8_str_new_cstr(const char *ptr)
Identical to rb_str_new_cstr(), except it generates a string of "UTF-8" encoding.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
VALUE rb_str_dup_frozen(VALUE)
Just another name of rb_str_new_frozen.
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_thread_current(void)
Obtains the "current" thread.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
int len
Length of the buffer.
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
char * ruby_getcwd(void)
This is our own version of getcwd(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
#define RB_NUM2INT
Just another name of rb_num2int_inline.
VALUE rb_int2inum(intptr_t i)
Converts a C's intptr_t into an instance of rb_cInteger.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
static int rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
VALUE type(ANYARGS)
ANYARGS-ed function type.
char * rb_enc_path_end(const char *path, const char *end, rb_encoding *enc)
This just returns the passed end basically.
char * rb_enc_path_skip_prefix(const char *path, const char *end, rb_encoding *enc)
Seeks for non-prefix part of a pathname.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
VALUE rb_get_path(VALUE obj)
Converts an object to a path.
#define FilePathValue(v)
Ensures that the parameter object is a path.
int rb_errno(void)
Identical to system errno.
void rb_errno_set(int err)
Set the errno.
#define errno
Ractor-aware version of errno.
#define FilePathStringValue(v)
This macro actually does the same thing as FilePathValue now.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
This is the struct that holds necessary info for a struct.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.