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/vm.h"
127 #define vm_initialized rb_cThread
132 # define chdir(p) rb_w32_uchdir(p)
134 # define mkdir(p, m) rb_w32_umkdir((p), (m))
136 # define rmdir(p) rb_w32_urmdir(p)
138 # define opendir(p) rb_w32_uopendir(p)
139 # define ruby_getcwd() rb_w32_ugetcwd(NULL, 0)
145 #if NORMALIZE_UTF8PATH
146 # if defined HAVE_FGETATTRLIST || !defined HAVE_GETATTRLIST
147 # define need_normalization(dirp, path) need_normalization(dirp)
149 # define need_normalization(dirp, path) need_normalization(path)
152 need_normalization(
DIR *dirp,
const char *path)
154 # if defined HAVE_FGETATTRLIST || defined HAVE_GETATTRLIST
155 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
156 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
157 # if defined HAVE_FGETATTRLIST
158 int ret = fgetattrlist(dirfd(dirp), &al, attrbuf,
sizeof(attrbuf), 0);
160 int ret = getattrlist(path, &al, attrbuf,
sizeof(attrbuf), 0);
163 const fsobj_tag_t *tag = (
void *)(attrbuf+1);
175 has_nonascii(
const char *ptr,
size_t len)
185 # define IF_NORMALIZE_UTF8PATH(something) something
187 # define IF_NORMALIZE_UTF8PATH(something)
191 # define IFTODT(m) (((m) & S_IFMT) / ((~S_IFMT & (S_IFMT-1)) + 1))
196 path_exist = DT_UNKNOWN,
197 path_directory = DT_DIR,
198 path_regular = DT_REG,
199 path_symlink = DT_LNK,
202 path_directory = IFTODT(S_IFDIR),
203 path_regular = IFTODT(S_IFREG),
204 path_symlink = IFTODT(S_IFLNK),
210 #define FNM_NOESCAPE 0x01
211 #define FNM_PATHNAME 0x02
212 #define FNM_DOTMATCH 0x04
213 #define FNM_CASEFOLD 0x08
214 #define FNM_EXTGLOB 0x10
215 #if CASEFOLD_FILESYSTEM
216 #define FNM_SYSCASE FNM_CASEFOLD
218 #define FNM_SYSCASE 0
221 #define FNM_SHORTNAME 0x20
223 #define FNM_SHORTNAME 0
225 #define FNM_GLOB_NOSORT 0x40
226 #define FNM_GLOB_SKIPDOT 0x80
228 #define FNM_NOMATCH 1
231 # define Next(p, e, enc) ((p)+ rb_enc_mbclen((p), (e), (enc)))
232 # define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
243 const int nocase = flags & FNM_CASEFOLD;
244 const int escape = !(flags & FNM_NOESCAPE);
249 if (p >= pend)
return NULL;
250 if (*p ==
'!' || *p ==
'^') {
257 if (escape && *t1 ==
'\\')
262 if (p >= pend)
return NULL;
263 if (p[0] ==
'-' && p[1] !=
']') {
264 const char *t2 = p + 1;
266 if (escape && *t2 ==
'\\')
272 if ((r <= (send-s) && memcmp(t1, s, r) == 0) ||
273 (r2 <= (send-s) && memcmp(t2, s, r2) == 0)) {
281 if (c1 < c2)
continue;
284 if (c1 > c2)
continue;
288 if (r <= (send-s) && memcmp(t1, s, r) == 0) {
292 if (!nocase)
continue;
295 if (c1 != c2)
continue;
300 return ok == not ? NULL : (
char *)p + 1;
308 #define UNESCAPE(p) (escape && *(p) == '\\' ? (p) + 1 : (p))
309 #define ISEND(p) (!*(p) || (pathname && *(p) == '/'))
310 #define RETURN(val) return *pcur = p, *scur = s, (val);
319 const int period = !(flags & FNM_DOTMATCH);
320 const int pathname = flags & FNM_PATHNAME;
321 const int escape = !(flags & FNM_NOESCAPE);
322 const int nocase = flags & FNM_CASEFOLD;
324 const char *ptmp = 0;
325 const char *stmp = 0;
327 const char *p = *pcur;
328 const char *pend = p + strlen(p);
329 const char *s = *scur;
330 const char *send = s + strlen(s);
334 if (period && *s ==
'.' && *UNESCAPE(p) !=
'.')
340 do { p++; }
while (*p ==
'*');
341 if (ISEND(UNESCAPE(p))) {
362 if ((t = bracket(p + 1, pend, s, send, flags, enc)) != 0) {
374 RETURN(ISEND(p) ? 0 : FNM_NOMATCH);
380 if (r <= (send-s) && memcmp(p, s, r) == 0) {
385 if (!nocase)
goto failed;
396 Inc(stmp, send, enc);
411 const char *p = pattern;
412 const char *s = string;
413 const char *send = s + strlen(
string);
414 const int period = !(flags & FNM_DOTMATCH);
415 const int pathname = flags & FNM_PATHNAME;
417 const char *ptmp = 0;
418 const char *stmp = 0;
422 if (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/') {
423 do { p += 3; }
while (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/');
427 if (fnmatch_helper(&p, &s, flags, enc) == 0) {
428 while (*s && *s !=
'/') Inc(s, send, enc);
438 if (ptmp && stmp && !(period && *stmp ==
'.')) {
439 while (*stmp && *stmp !=
'/') Inc(stmp, send, enc);
451 return fnmatch_helper(&p, &s, flags, enc);
474 if (dir->dir) closedir(dir->dir);
479 dir_memsize(
const void *ptr)
486 {dir_mark, dir_free, dir_memsize,},
487 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
493 dir_s_alloc(
VALUE klass)
506 nogvl_opendir(
void *ptr)
508 const char *path = ptr;
510 return (
void *)opendir(path);
514 opendir_without_gvl(
const char *path)
516 if (vm_initialized) {
517 union {
const void *in;
void *out; } u;
524 return opendir(path);
541 if (dp->dir) closedir(dp->dir);
546 dp->dir = opendir_without_gvl(path);
547 if (dp->dir == NULL) {
549 if (rb_gc_for_fd(e)) {
550 dp->dir = opendir_without_gvl(path);
552 #ifdef HAVE_GETATTRLIST
554 u_int32_t attrbuf[1];
555 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0};
556 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW) == 0) {
557 dp->dir = opendir_without_gvl(path);
561 if (dp->dir == NULL) {
563 rb_syserr_fail_path(e, orig);
577 dir_initialize(ec, dir, dirname, enc);
585 return dir_close(dir);
588 NORETURN(
static void dir_closed(
void));
606 struct dir_data *dirp = dir_get(dir);
607 if (!dirp->dir) dir_closed();
611 #define GetDIR(obj, dirp) ((dirp) = dir_check(obj))
621 dir_inspect(
VALUE dir)
626 if (!
NIL_P(dirp->path)) {
640 #if defined(__sun) && !defined(HAVE_DIRFD)
641 # if defined(HAVE_DIR_D_FD)
642 # define dirfd(x) ((x)->d_fd)
643 # define HAVE_DIRFD 1
644 # elif defined(HAVE_DIR_DD_FD)
645 # define dirfd(x) ((x)->dd_fd)
646 # define HAVE_DIRFD 1
666 dir_fileno(
VALUE dir)
672 fd = dirfd(dirp->dir);
678 #define dir_fileno rb_f_notimplement
707 case ENCINDEX_US_ASCII:
714 # define READDIR(dir, enc) rb_w32_readdir((dir), (enc))
716 # define READDIR(dir, enc) readdir((dir))
721 to_be_skipped(
const struct dirent *dp)
723 const char *name = dp->d_name;
724 if (name[0] !=
'.')
return FALSE;
725 #ifdef HAVE_DIRENT_NAMLEN
726 switch (NAMLEN(dp)) {
728 if (name[1] !=
'.')
return FALSE;
735 if (!name[1])
return TRUE;
736 if (name[1] !=
'.')
return FALSE;
737 if (!name[2])
return TRUE;
762 if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
804 return dir_each_entry(dir, dir_yield,
Qnil, FALSE);
812 IF_NORMALIZE_UTF8PATH(
int norm_p);
815 rewinddir(dirp->dir);
816 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir,
RSTRING_PTR(dirp->path)));
817 while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
818 const char *name = dp->d_name;
819 size_t namlen = NAMLEN(dp);
822 if (children_only && name[0] ==
'.') {
823 if (namlen == 1)
continue;
824 if (namlen == 2 && name[1] ==
'.')
continue;
826 #if NORMALIZE_UTF8PATH
827 if (norm_p && has_nonascii(name, namlen) &&
828 !
NIL_P(path = rb_str_normalize_ospath(name, namlen))) {
829 path = rb_external_str_with_enc(path, dirp->enc);
859 pos = telldir(dirp->dir);
863 #define dir_tell rb_f_notimplement
888 seekdir(dirp->dir, p);
892 #define dir_seek rb_f_notimplement
916 #define dir_set_pos rb_f_notimplement
931 dir_rewind(
VALUE dir)
936 rewinddir(dirp->dir);
956 if (!dirp->dir)
return Qnil;
964 nogvl_chdir(
void *ptr)
966 const char *path = ptr;
968 return (
void *)(
VALUE)chdir(path);
972 dir_chdir(
VALUE path)
975 rb_sys_fail_path(path);
978 static int chdir_blocking = 0;
982 VALUE old_path, new_path;
990 dir_chdir(args->new_path);
993 if (
NIL_P(chdir_thread))
999 chdir_restore(
VALUE v)
1004 if (chdir_blocking == 0)
1005 chdir_thread =
Qnil;
1006 dir_chdir(args->old_path);
1052 dir_s_chdir(
int argc,
VALUE *argv,
VALUE obj)
1060 const char *dist = getenv(
"HOME");
1062 dist = getenv(
"LOGDIR");
1068 if (chdir_blocking > 0) {
1072 rb_warn(
"conflicting chdir during another chdir block");
1079 args.new_path = path;
1088 rb_sys_fail_path(path);
1096 rb_dir_getwd_ospath(
void)
1102 #undef RUBY_UNTYPED_DATA_WARNING
1103 #define RUBY_UNTYPED_DATA_WARNING 0
1108 cwd = rb_str_normalize_ospath(path, strlen(path));
1124 VALUE cwd = rb_dir_getwd_ospath();
1127 case ENCINDEX_US_ASCII:
1128 fsenc = ENCINDEX_ASCII;
1129 case ENCINDEX_ASCII:
1131 #if defined _WIN32 || defined __APPLE__
1152 dir_s_getwd(
VALUE dir)
1158 check_dirname(
VALUE dir)
1170 if (pend - path < len) {
1177 #if defined(HAVE_CHROOT)
1190 path = check_dirname(path);
1192 rb_sys_fail_path(path);
1197 #define dir_s_chroot rb_f_notimplement
1206 nogvl_mkdir(
void *ptr)
1210 return (
void *)(
VALUE)mkdir(m->path, m->mode);
1228 dir_s_mkdir(
int argc,
VALUE *argv,
VALUE obj)
1234 if (
rb_scan_args(argc, argv,
"11", &path, &vmode) == 2) {
1241 path = check_dirname(path);
1245 rb_sys_fail_path(path);
1251 nogvl_rmdir(
void *ptr)
1253 const char *path = ptr;
1255 return (
void *)(
VALUE)rmdir(path);
1273 dir = check_dirname(dir);
1277 rb_sys_fail_path(dir);
1283 #ifdef RUBY_FUNCTION_NAME_STRING
1290 #ifndef RUBY_FUNCTION_NAME_STRING
1291 #define sys_enc_warning_in(func, mesg, enc) sys_enc_warning(mesg, enc)
1295 sys_warning_1(
VALUE mesg)
1298 #ifdef RUBY_FUNCTION_NAME_STRING
1299 rb_sys_enc_warning(arg->enc,
"%s: %s", arg->func, arg->mesg);
1301 rb_sys_enc_warning(arg->enc,
"%s", arg->mesg);
1307 sys_enc_warning_in(
const char *func,
const char *mesg,
rb_encoding *enc)
1310 #ifdef RUBY_FUNCTION_NAME_STRING
1318 #define GLOB_VERBOSE (1U << (sizeof(int) * CHAR_BIT - 1))
1319 #define sys_warning(val, enc) \
1320 ((flags & GLOB_VERBOSE) ? sys_enc_warning_in(RUBY_FUNCTION_NAME_STRING, (val), (enc)) :(void)0)
1322 static inline size_t
1323 glob_alloc_size(
size_t x,
size_t y)
1334 static inline void *
1335 glob_alloc_n(
size_t x,
size_t y)
1337 return malloc(glob_alloc_size(x, y));
1340 static inline void *
1341 glob_realloc_n(
void *p,
size_t x,
size_t y)
1343 return realloc(p, glob_alloc_size(x, y));
1346 #define GLOB_ALLOC(type) ((type *)malloc(sizeof(type)))
1347 #define GLOB_ALLOC_N(type, n) ((type *)glob_alloc_n(sizeof(type), n))
1348 #define GLOB_REALLOC(ptr, size) realloc((ptr), (size))
1349 #define GLOB_REALLOC_N(ptr, n) glob_realloc_n(ptr, sizeof(*(ptr)), n)
1350 #define GLOB_FREE(ptr) free(ptr)
1351 #define GLOB_JUMP_TAG(status) (((status) == -1) ? rb_memerror() : rb_jump_tag(status))
1357 ALWAYS_INLINE(
static int to_be_ignored(
int e));
1359 to_be_ignored(
int e)
1361 return e == ENOENT || e == ENOTDIR;
1365 #define STAT(p, s) rb_w32_ustati128((p), (s))
1367 #define lstat(p, s) rb_w32_ulstati128((p), (s))
1369 #define STAT(p, s) stat((p), (s))
1372 typedef int ruby_glob_errfunc(
const char*,
VALUE,
const void*,
int);
1375 ruby_glob_errfunc *error;
1379 at_subpath(
int fd,
size_t baselen,
const char *path)
1382 if (fd != (
int)AT_FDCWD && baselen > 0) {
1384 if (*path ==
'/') ++path;
1387 return *path ? path :
".";
1392 do_stat(
int fd,
size_t baselen,
const char *path,
struct stat *pst,
int flags,
rb_encoding *enc)
1395 int ret = fstatat(fd, at_subpath(fd, baselen, path), pst, 0);
1397 int ret = STAT(path, pst);
1399 if (ret < 0 && !to_be_ignored(errno))
1400 sys_warning(path, enc);
1405 #if defined HAVE_LSTAT || defined lstat || USE_OPENDIR_AT
1407 do_lstat(
int fd,
size_t baselen,
const char *path,
struct stat *pst,
int flags,
rb_encoding *enc)
1410 int ret = fstatat(fd, at_subpath(fd, baselen, path), pst, AT_SYMLINK_NOFOLLOW);
1412 int ret = lstat(path, pst);
1414 if (ret < 0 && !to_be_ignored(errno))
1415 sys_warning(path, enc);
1420 #define do_lstat do_stat
1429 with_gvl_gc_for_fd(
void *ptr)
1433 return (
void *)RBOOL(rb_gc_for_fd(*e));
1437 gc_for_fd_with_gvl(
int e)
1442 return RBOOL(rb_gc_for_fd(e));
1446 nogvl_opendir_at(
void *ptr)
1452 const int opendir_flags = (O_RDONLY|O_CLOEXEC|
1457 int fd = openat(oaa->basefd, oaa->path, opendir_flags);
1459 dirp = fd >= 0 ? fdopendir(fd) : 0;
1463 switch (gc_for_fd_with_gvl(e)) {
1465 if (fd < 0) fd = openat(oaa->basefd, oaa->path, opendir_flags);
1466 if (fd >= 0) dirp = fdopendir(fd);
1467 if (dirp)
return dirp;
1472 if (fd >= 0) close(fd);
1477 dirp = opendir(oaa->path);
1478 if (!dirp && gc_for_fd_with_gvl(errno))
1479 dirp = opendir(oaa->path);
1486 opendir_at(
int basefd,
const char *path)
1490 oaa.basefd = basefd;
1496 return nogvl_opendir_at(&oaa);
1500 do_opendir(
const int basefd,
size_t baselen,
const char *path,
int flags,
rb_encoding *enc,
1501 ruby_glob_errfunc *errfunc,
VALUE arg,
int *status)
1506 if (!fundamental_encoding_p(enc)) {
1512 dirp = opendir_at(basefd, at_subpath(basefd, baselen, path));
1517 if (!to_be_ignored(e)) {
1519 *status = (*errfunc)(path, arg, enc, e);
1522 sys_warning(path, enc);
1534 enum glob_pattern_type { PLAIN, ALPHA, BRACE, MAGICAL, RECURSIVE, MATCH_ALL, MATCH_DIR };
1537 static enum glob_pattern_type
1538 has_magic(
const char *p,
const char *pend,
int flags,
rb_encoding *enc)
1540 const int escape = !(flags & FNM_NOESCAPE);
1546 while (p < pend && (c = *p++) != 0) {
1558 if (escape && p++ >= pend)
1577 p = Next(p-1, pend, enc);
1580 return hasmagical ? MAGICAL : hasalpha ? ALPHA : PLAIN;
1585 find_dirsep(
const char *p,
const char *pend,
int flags,
rb_encoding *enc)
1587 const int escape = !(flags & FNM_NOESCAPE);
1592 while ((c = *p++) != 0) {
1614 if (escape && !(c = *p++))
1619 p = Next(p-1, pend, enc);
1627 remove_backslashes(
char *p,
register const char *pend,
rb_encoding *enc)
1635 memmove(t, s, p - s);
1646 memmove(t, s, p - s);
1653 enum glob_pattern_type type;
1657 static void glob_free_pattern(
struct glob_pattern *list);
1660 glob_make_pattern(
const char *p,
const char *e,
int flags,
rb_encoding *enc)
1666 while (p < e && *p) {
1668 if (!tmp)
goto error;
1669 if (p + 2 < e && p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/') {
1671 do { p += 3;
while (*p ==
'/') p++; }
while (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/');
1672 tmp->type = RECURSIVE;
1678 const char *m = find_dirsep(p, e, flags, enc);
1679 const enum glob_pattern_type magic = has_magic(p, m, flags, enc);
1680 const enum glob_pattern_type non_magic = (USE_NAME_ON_FS || FNM_SYSCASE) ? PLAIN : ALPHA;
1683 if (!(FNM_SYSCASE || magic > non_magic) && !recursive && *m) {
1685 while (has_magic(m+1, m2 = find_dirsep(m+1, e, flags, enc), flags, enc) <= non_magic &&
1690 buf = GLOB_ALLOC_N(
char, m-p+1);
1695 memcpy(buf, p, m-p);
1697 tmp->type = magic > MAGICAL ? MAGICAL : magic > non_magic ? magic : PLAIN;
1716 tmp->type = dirsep ? MATCH_DIR : MATCH_ALL;
1725 glob_free_pattern(list);
1736 GLOB_FREE(tmp->str);
1742 join_path(
const char *path,
size_t len,
int dirsep,
const char *name,
size_t namlen)
1744 char *buf = GLOB_ALLOC_N(
char, len+namlen+(dirsep?1:0)+1);
1747 memcpy(buf, path, len);
1751 memcpy(buf+len, name, namlen);
1752 buf[len+namlen] =
'\0';
1756 #ifdef HAVE_GETATTRLIST
1757 # if defined HAVE_FGETATTRLIST
1758 # define is_case_sensitive(dirp, path) is_case_sensitive(dirp)
1760 # define is_case_sensitive(dirp, path) is_case_sensitive(path)
1763 is_case_sensitive(
DIR *dirp,
const char *path)
1767 vol_capabilities_attr_t cap[1];
1768 } __attribute__((aligned(4), packed)) attrbuf[1];
1769 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, 0, ATTR_VOL_INFO|ATTR_VOL_CAPABILITIES};
1770 const vol_capabilities_attr_t *
const cap = attrbuf[0].cap;
1771 const int idx = VOL_CAPABILITIES_FORMAT;
1772 const uint32_t mask = VOL_CAP_FMT_CASE_SENSITIVE;
1774 # if defined HAVE_FGETATTRLIST
1775 if (fgetattrlist(dirfd(dirp), &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW))
1778 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW))
1781 if (!(cap->valid[idx] & mask))
1783 return (cap->capabilities[idx] & mask) != 0;
1787 replace_real_basename(
char *path,
long base,
rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
1791 attrreference_t ref[1];
1792 fsobj_type_t objtype;
1793 char path[MAXPATHLEN * 3];
1794 } __attribute__((aligned(4), packed)) attrbuf[1];
1795 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_NAME|ATTR_CMN_OBJTYPE};
1796 const attrreference_t *
const ar = attrbuf[0].ref;
1800 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
1803 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW)) {
1804 if (!to_be_ignored(errno))
1805 sys_warning(path, enc);
1809 switch (attrbuf[0].objtype) {
1810 case VREG: *
type = path_regular;
break;
1811 case VDIR: *
type = path_directory;
break;
1812 case VLNK: *
type = path_symlink;
break;
1813 default: *
type = path_exist;
break;
1815 name = (
char *)ar + ar->attr_dataoffset;
1816 len = (
long)ar->attr_length - 1;
1817 if (name + len > (
char *)attrbuf +
sizeof(attrbuf))
1820 # if NORMALIZE_UTF8PATH
1821 if (norm_p && has_nonascii(name, len)) {
1822 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name, len))) {
1828 tmp = GLOB_REALLOC(path, base + len + 1);
1831 memcpy(path + base, name, len);
1832 path[base + len] =
'\0';
1837 #elif defined _WIN32
1839 int rb_w32_reparse_symlink_p(
const WCHAR *path);
1842 replace_real_basename(
char *path,
long base,
rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
1844 char *plainname = path;
1845 volatile VALUE tmp = 0;
1846 WIN32_FIND_DATAW fd;
1847 WIN32_FILE_ATTRIBUTE_DATA fa;
1849 HANDLE h = INVALID_HANDLE_VALUE;
1852 if (!fundamental_encoding_p(enc)) {
1857 wplain = rb_w32_mbstr_to_wstr(CP_UTF8, plainname, -1, &wlen);
1859 if (!wplain)
return path;
1860 if (GetFileAttributesExW(wplain, GetFileExInfoStandard, &fa)) {
1861 h = FindFirstFileW(wplain, &fd);
1862 e = rb_w32_map_errno(GetLastError());
1864 if (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1865 if (!rb_w32_reparse_symlink_p(wplain))
1866 fa.dwFileAttributes &= ~FILE_ATTRIBUTE_REPARSE_POINT;
1869 if (h == INVALID_HANDLE_VALUE) {
1871 if (e && !to_be_ignored(e)) {
1873 sys_warning(path, enc);
1879 (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ? path_symlink :
1880 (fa.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? path_directory :
1884 tmp = rb_w32_conv_from_wchar(fd.cFileName, enc);
1886 buf = GLOB_REALLOC(path, base + wlen + 1);
1890 path[base + wlen] = 0;
1896 wlen = WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, NULL, 0, NULL, NULL);
1897 utf8filename = GLOB_REALLOC(0, wlen);
1900 WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, utf8filename, wlen, NULL, NULL);
1901 buf = GLOB_REALLOC(path, base + wlen + 1);
1904 memcpy(path + base, utf8filename, wlen);
1905 path[base + wlen] = 0;
1907 GLOB_FREE(utf8filename);
1912 #elif USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
1913 # error not implemented
1917 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1922 # define S_ISLNK(m) (0)
1924 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1929 void (*func)(
const char *,
VALUE,
void *);
1937 #define glob_call_func(func, path, arg, enc) (*(func))((path), (arg), (void *)(enc))
1940 glob_func_caller(
VALUE val)
1944 glob_call_func(args->func, args->path, args->value, args->enc);
1955 glob_func_warning(
VALUE val)
1958 rb_syserr_enc_warning(arg->error, arg->enc,
"%s", arg->path);
1964 rb_glob_warning(
const char *path,
VALUE a,
const void *enc,
int error)
1977 NORETURN(
static VALUE glob_func_error(
VALUE val));
1980 glob_func_error(
VALUE val)
1989 rb_glob_error(
const char *path,
VALUE a,
const void *enc,
int error)
1995 if (error == EACCES) {
1996 errfunc = glob_func_warning;
2009 const char *d_altname;
2017 if (fnmatch(pat, enc, name, flags) == 0)
return 1;
2019 if (dp->d_altname && (flags & FNM_SHORTNAME)) {
2020 if (fnmatch(pat, enc, dp->d_altname, flags) == 0)
return 1;
2032 rb_pathtype_t pathtype;
2045 dirent_match_brace(
const char *pattern,
VALUE val,
void *enc)
2049 return dirent_match(pattern, enc, arg->name, arg->dp, arg->flags);
2058 size_t path_len = 0;
2060 for (p = *beg; p; p = p->next) {
2075 path_len = strlen(str);
2076 path = GLOB_ALLOC_N(
char, path_len + 1);
2078 memcpy(path, str, path_len);
2079 path[path_len] =
'\0';
2083 size_t len = strlen(str);
2085 tmp = GLOB_REALLOC(path, path_len + len + 2);
2088 path[path_len++] =
'/';
2089 memcpy(path + path_len, str, len);
2091 path[path_len] =
'\0';
2098 static int push_caller(
const char *path,
VALUE val,
void *enc);
2103 static const size_t rb_dirent_name_offset =
2107 dirent_copy(
const struct dirent *dp,
rb_dirent_t *rdp)
2109 if (!dp)
return NULL;
2110 size_t namlen = NAMLEN(dp);
2111 const size_t altlen =
2113 dp->d_altlen ? dp->d_altlen + 1 :
2117 if (!rdp && !(newrdp = malloc(rb_dirent_name_offset + namlen + 1 + altlen)))
2119 newrdp->d_namlen = namlen;
2121 char *name = (
char *)newrdp + rb_dirent_name_offset;
2122 memcpy(name, dp->d_name, namlen);
2123 name[namlen] =
'\0';
2125 newrdp->d_altname = NULL;
2127 char *
const altname = name + namlen + 1;
2128 memcpy(altname, dp->d_altname, altlen - 1);
2129 altname[altlen - 1] =
'\0';
2130 newrdp->d_altname = altname;
2133 newrdp->d_name = name;
2136 newrdp->d_name = dp->d_name;
2138 newrdp->d_altname = dp->d_altname;
2142 newrdp->d_type = dp->d_type;
2161 glob_sort_cmp(
const void *a,
const void *b,
void *e)
2165 return strcmp(ent1->d_name, ent2->d_name);
2171 if (flags & FNM_GLOB_NOSORT) {
2172 closedir(ent->nosort.dirp);
2173 ent->nosort.dirp = NULL;
2175 else if (ent->sort.entries) {
2176 for (
size_t i = 0, count = ent->sort.count; i < count;) {
2177 GLOB_FREE(ent->sort.entries[i++]);
2179 GLOB_FREE(ent->sort.entries);
2180 ent->sort.entries = NULL;
2181 ent->sort.count = ent->sort.idx = 0;
2189 if (flags & FNM_GLOB_NOSORT) {
2190 ent->nosort.dirp = dirp;
2196 size_t count = 0, capacity = 0;
2197 ent->sort.count = 0;
2199 ent->sort.entries = 0;
2201 if ((capacity = dirp->nfiles) > 0) {
2202 if (!(newp = GLOB_ALLOC_N(
rb_dirent_t, capacity))) {
2206 ent->sort.entries = newp;
2209 while ((dp = READDIR(dirp, enc)) != NULL) {
2214 if (count >= capacity) {
2216 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, capacity)))
2218 ent->sort.entries = newp;
2220 ent->sort.entries[count++] = rdp;
2221 ent->sort.count = count;
2224 if (count < capacity) {
2225 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, count))) {
2226 glob_dir_finish(ent, 0);
2229 ent->sort.entries = newp;
2231 ruby_qsort(ent->sort.entries, ent->sort.count,
sizeof(ent->sort.entries[0]),
2232 glob_sort_cmp, NULL);
2237 glob_dir_finish(ent, 0);
2245 if (flags & FNM_GLOB_NOSORT) {
2246 return dirent_copy(READDIR(ent->nosort.dirp, enc), &ent->nosort.ent);
2248 else if (ent->sort.idx < ent->sort.count) {
2249 return ent->sort.entries[ent->sort.idx++];
2263 rb_pathtype_t pathtype,
2274 int plain = 0, brace = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0;
2275 int escape = !(flags & FNM_NOESCAPE);
2276 size_t pathlen = baselen + namelen;
2278 rb_check_stack_overflow();
2280 for (cur = beg; cur < end; ++cur) {
2282 if (p->type == RECURSIVE) {
2291 #if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2298 if (!recursive || strchr(p->str,
'/')) {
2312 rb_bug(
"continuous RECURSIVEs");
2318 char* brace_path = join_path_from_pattern(beg);
2319 if (!brace_path)
return -1;
2322 args.baselen = baselen;
2323 args.namelen = namelen;
2324 args.dirsep = dirsep;
2325 args.pathtype = pathtype;
2329 status = ruby_brace_expand(brace_path, flags, push_caller, (
VALUE)&args, enc,
Qfalse);
2330 GLOB_FREE(brace_path);
2335 if (match_all && pathtype == path_unknown) {
2336 if (do_lstat(fd, baselen, path, &st, flags, enc) == 0) {
2337 pathtype = IFTODT(st.st_mode);
2340 pathtype = path_noent;
2343 if (match_dir && (pathtype == path_unknown || pathtype == path_symlink)) {
2344 if (do_stat(fd, baselen, path, &st, flags, enc) == 0) {
2345 pathtype = IFTODT(st.st_mode);
2348 pathtype = path_noent;
2351 if (match_all && pathtype > path_noent) {
2352 const char *subpath = path + baselen + (baselen && path[baselen] ==
'/');
2353 status = glob_call_func(funcs->match, subpath, arg, enc);
2354 if (status)
return status;
2356 if (match_dir && pathtype == path_directory) {
2357 int seplen = (baselen && path[baselen] ==
'/');
2358 const char *subpath = path + baselen + seplen;
2359 char *tmp = join_path(subpath, namelen - seplen, dirsep,
"", 0);
2360 if (!tmp)
return -1;
2361 status = glob_call_func(funcs->match, tmp, arg, enc);
2363 if (status)
return status;
2367 if (pathtype == path_noent)
return 0;
2369 if (magical || recursive) {
2372 # if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2373 char *plainname = 0;
2375 IF_NORMALIZE_UTF8PATH(
int norm_p);
2376 # if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2377 if (cur + 1 == end && (*cur)->type <= ALPHA) {
2378 plainname = join_path(path, pathlen, dirsep, (*cur)->str, strlen((*cur)->str));
2379 if (!plainname)
return -1;
2380 dirp = do_opendir(fd, basename, plainname, flags, enc, funcs->error, arg, &status);
2381 GLOB_FREE(plainname);
2387 dirp = do_opendir(fd, baselen, path, flags, enc, funcs->error, arg, &status);
2389 # if FNM_SYSCASE || NORMALIZE_UTF8PATH
2390 if ((magical < 2) && !recursive && (errno == EACCES)) {
2397 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *path ? path :
"."));
2399 # if NORMALIZE_UTF8PATH
2400 if (!(norm_p || magical || recursive)) {
2405 # ifdef HAVE_GETATTRLIST
2406 if (is_case_sensitive(dirp, path) == 0)
2407 flags |= FNM_CASEFOLD;
2410 if (!glob_opendir(&globent, dirp, flags, enc)) {
2413 status = (*funcs->error)(path, arg, enc, ENOMEM);
2416 sys_warning(path, enc);
2421 int skipdot = (flags & FNM_GLOB_SKIPDOT);
2422 flags |= FNM_GLOB_SKIPDOT;
2424 while ((dp = glob_getent(&globent, flags, enc)) != NULL) {
2426 rb_pathtype_t new_pathtype = path_unknown;
2430 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
2433 namlen = dp->d_namlen;
2434 if (name[0] ==
'.') {
2438 if (recursive && !(flags & FNM_DOTMATCH))
continue;
2439 if (skipdot)
continue;
2441 new_pathtype = path_directory;
2443 else if (namlen == 2 && name[1] ==
'.') {
2449 # if NORMALIZE_UTF8PATH
2450 if (norm_p && has_nonascii(name, namlen)) {
2451 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name, namlen))) {
2456 buf = join_path(path, pathlen, dirsep, name, namlen);
2462 name = buf + pathlen + (dirsep != 0);
2464 if (dp->d_type != DT_UNKNOWN) {
2466 new_pathtype = dp->d_type;
2469 if (recursive && dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1) &&
2470 new_pathtype == path_unknown) {
2472 if (do_lstat(fd, baselen, buf, &st, flags, enc) == 0)
2473 new_pathtype = IFTODT(st.st_mode);
2475 new_pathtype = path_noent;
2478 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, (end - beg) * 2);
2485 for (cur = beg; cur < end; ++cur) {
2488 if (p->type == RECURSIVE) {
2489 if (new_pathtype == path_directory ||
2490 new_pathtype == path_exist) {
2491 if (dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1))
2501 if (ruby_brace_expand(p->str, flags, dirent_match_brace,
2503 *new_end++ = p->next;
2506 # if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2508 *new_end++ = p->next;
2514 if (dirent_match(p->str, enc, name, dp, flags))
2515 *new_end++ = p->next;
2521 status = glob_helper(fd, buf, baselen, name - buf - baselen + namlen, 1,
2522 new_pathtype, new_beg, new_end,
2523 flags, funcs, arg, enc);
2529 glob_dir_finish(&globent, flags);
2534 # if FNM_SYSCASE || NORMALIZE_UTF8PATH
2537 copy_beg = copy_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
2538 if (!copy_beg)
return -1;
2539 for (cur = beg; cur < end; ++cur)
2540 *copy_end++ = (*cur)->type <= ALPHA ? *cur : 0;
2542 for (cur = copy_beg; cur < copy_end; ++cur) {
2544 rb_pathtype_t new_pathtype = path_unknown;
2547 size_t len = strlen((*cur)->str) + 1;
2548 name = GLOB_ALLOC_N(
char, len);
2553 memcpy(name, (*cur)->str, len);
2555 len = remove_backslashes(name, name+len-1, enc) - name;
2557 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
2563 *new_end++ = (*cur)->next;
2564 for (cur2 = cur + 1; cur2 < copy_end; ++cur2) {
2565 if (*cur2 && fnmatch((*cur2)->str, enc, name, flags) == 0) {
2566 *new_end++ = (*cur2)->next;
2571 buf = join_path(path, pathlen, dirsep, name, len);
2578 #if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2579 if ((*cur)->type == ALPHA) {
2580 buf = replace_real_basename(buf, pathlen + (dirsep != 0), enc,
2581 IF_NORMALIZE_UTF8PATH(1)+0,
2582 flags, &new_pathtype);
2586 status = glob_helper(fd, buf, baselen,
2587 namelen + strlen(buf + pathlen), 1,
2588 new_pathtype, new_beg, new_end,
2589 flags, funcs, arg, enc);
2596 GLOB_FREE(copy_beg);
2603 push_caller(
const char *path,
VALUE val,
void *enc)
2609 list = glob_make_pattern(path, path + strlen(path), arg->flags, enc);
2613 status = glob_helper(arg->fd, arg->path, arg->baselen, arg->namelen, arg->dirsep,
2614 arg->pathtype, &list, &list + 1, arg->flags, arg->funcs,
2616 glob_free_pattern(list);
2620 static int ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
2632 push_glob0_caller(
const char *path,
VALUE val,
void *enc)
2635 return ruby_glob0(path, arg->fd, arg->base, arg->flags, arg->funcs, arg->arg, enc);
2639 ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
2644 const char *root, *start;
2646 size_t n, baselen = 0;
2647 int status, dirsep = FALSE;
2649 start = root = path;
2658 return ruby_brace_expand(path, flags, push_glob0_caller, (
VALUE)&args, enc,
Qfalse);
2661 flags |= FNM_SYSCASE;
2666 if (*root ==
'/') root++;
2675 buf = GLOB_ALLOC_N(
char, n + 1);
2676 if (!buf)
return -1;
2677 MEMCPY(buf, start,
char, n);
2680 list = glob_make_pattern(root, root + strlen(root), flags, enc);
2685 status = glob_helper(fd, buf, baselen, n-baselen, dirsep,
2686 path_unknown, &list, &list + 1,
2687 flags, funcs, arg, enc);
2688 glob_free_pattern(list);
2700 return ruby_glob0(path, AT_FDCWD, 0, flags & ~GLOB_VERBOSE,
2705 rb_glob_caller(
const char *path,
VALUE a,
void *enc)
2716 rb_glob_caller, rb_glob_error,
2729 status = ruby_glob0(path, AT_FDCWD, 0, GLOB_VERBOSE, &rb_glob_funcs,
2730 (
VALUE)&args, args.enc);
2731 if (status) GLOB_JUMP_TAG(status);
2735 push_pattern(
const char *path,
VALUE ary,
void *enc)
2737 #if defined _WIN32 || defined __APPLE__
2751 const int escape = !(flags & FNM_NOESCAPE);
2752 const char *p = str;
2753 const char *pend = p + strlen(p);
2755 const char *lbrace = 0, *rbrace = 0;
2756 int nest = 0, status = 0;
2759 if (*p ==
'{' && nest++ == 0) {
2762 if (*p ==
'}' && lbrace && --nest == 0) {
2766 if (*p ==
'\\' && escape) {
2772 if (lbrace && rbrace) {
2773 size_t len = strlen(s) + 1;
2774 char *buf = GLOB_ALLOC_N(
char, len);
2777 if (!buf)
return -1;
2778 memcpy(buf, s, lbrace-s);
2781 while (p < rbrace) {
2782 const char *t = ++p;
2784 while (p < rbrace && !(*p ==
',' && nest == 0)) {
2785 if (*p ==
'{') nest++;
2786 if (*p ==
'}') nest--;
2787 if (*p ==
'\\' && escape) {
2788 if (++p == rbrace)
break;
2792 memcpy(buf+shift, t, p-t);
2793 strlcpy(buf+shift+(p-t), rbrace+1, len-(shift+(p-t)));
2794 status = ruby_brace_expand(buf, flags, func, arg, enc, var);
2799 else if (!lbrace && !rbrace) {
2800 status = glob_call_func(func, s, arg, enc);
2814 glob_brace(
const char *path,
VALUE val,
void *enc)
2818 return ruby_glob0(path, AT_FDCWD, 0, arg->flags, &arg->funcs, arg->value, enc);
2826 flags &= ~GLOB_VERBOSE;
2827 args.funcs.match = func;
2828 args.funcs.error = 0;
2831 return ruby_brace_expand(str, flags, glob_brace, (
VALUE)&args, enc,
Qfalse);
2847 #if defined _WIN32 || defined __APPLE__
2854 flags |= GLOB_VERBOSE;
2855 args.func = push_pattern;
2863 if (!dirp->dir) dir_closed();
2865 if ((fd = dirfd(dirp->dir)) == -1)
2866 rb_sys_fail_path(dir_inspect(base));
2872 #if defined _WIN32 || defined __APPLE__
2876 return ruby_glob0(
RSTRING_PTR(str), fd, args.base, flags, &rb_glob_funcs,
2881 rb_push_glob(
VALUE str,
VALUE base,
int flags)
2890 else if (!rb_str_to_cstr(str)) {
2898 status = push_glob(ary, str, base, flags);
2899 if (status) GLOB_JUMP_TAG(status);
2914 status = push_glob(ary, str, base, flags);
2915 if (status) GLOB_JUMP_TAG(status);
2923 dir_glob_option_base(
VALUE base)
2939 dir_glob_option_sort(
VALUE sort)
2941 return (rb_bool_expected(sort,
"sort") ? 0 : FNM_GLOB_NOSORT);
2947 const int flags = dir_glob_option_sort(sort);
2948 base = dir_glob_option_base(base);
2950 return rb_push_glob(
RARRAY_AREF(args, 0), base, flags);
2952 return dir_globs(args, base, flags);
2959 const int flags = (
NUM2INT(rflags) | dir_glob_option_sort(sort)) & ~FNM_CASEFOLD;
2960 base = dir_glob_option_base(base);
2962 ary = rb_push_glob(str, base, flags);
2965 ary = dir_globs(ary, base, flags);
2976 dir_open_dir(
int argc,
VALUE *argv)
3013 dir = dir_open_dir(argc, argv);
3014 rb_ensure(dir_each, dir, dir_close, dir);
3019 dir_collect(
VALUE dir)
3046 dir = dir_open_dir(argc, argv);
3047 return rb_ensure(dir_collect, dir, dir_close, dir);
3051 dir_each_child(
VALUE dir)
3053 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3078 dir_s_each_child(
int argc,
VALUE *argv,
VALUE io)
3083 dir = dir_open_dir(argc, argv);
3084 rb_ensure(dir_each_child, dir, dir_close, dir);
3109 dir_each_child_m(
VALUE dir)
3112 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3127 dir_collect_children(
VALUE dir)
3150 dir_s_children(
int argc,
VALUE *argv,
VALUE io)
3154 dir = dir_open_dir(argc, argv);
3155 return rb_ensure(dir_collect_children, dir, dir_close, dir);
3159 fnmatch_brace(
const char *pattern,
VALUE val,
void *enc)
3162 VALUE path = arg->value;
3166 if (enc_pattern != enc_path) {
3173 long len = strlen(pattern);
3175 enc_pattern, &cr) != len)
3181 return (fnmatch(pattern, enc,
RSTRING_PTR(path), arg->flags) == 0);
3186 file_s_fnmatch(
int argc,
VALUE *argv,
VALUE obj)
3188 VALUE pattern, path;
3192 if (
rb_scan_args(argc, argv,
"21", &pattern, &path, &rflags) == 3)
3200 if (flags & FNM_EXTGLOB) {
3205 if (ruby_brace_expand(
RSTRING_PTR(pattern), flags, fnmatch_brace,
3235 user = (argc > 0) ? argv[0] :
Qnil;
3241 return rb_home_dir_of(user,
rb_str_new(0, 0));
3244 return rb_default_home_dir(
rb_str_new(0, 0));
3267 rb_warn_deprecated(
"Dir.exists?",
"Dir.exist?");
3272 nogvl_dir_empty_p(
void *ptr)
3274 const char *path = ptr;
3275 DIR *dir = opendir(path);
3281 switch (gc_for_fd_with_gvl(e)) {
3283 dir = opendir(path);
3288 if (e == ENOTDIR)
return (
void *)
Qfalse;
3293 while ((dp = READDIR(dir, NULL)) != NULL) {
3294 if (!to_be_skipped(dp)) {
3300 return (
void *)result;
3315 enum {false_on_notdir = 1};
3323 #if defined HAVE_GETATTRLIST && defined ATTR_DIR_ENTRYCOUNT
3325 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
3326 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
3327 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), 0) != 0)
3328 rb_sys_fail_path(orig);
3329 if (*(
const fsobj_tag_t *)(attrbuf+1) == VT_HFS) {
3331 al.dirattr = ATTR_DIR_ENTRYCOUNT;
3332 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), 0) == 0) {
3333 if (attrbuf[0] >= 2 *
sizeof(u_int32_t))
3335 if (false_on_notdir)
return Qfalse;
3337 rb_sys_fail_path(orig);
3345 rb_sys_fail_path(orig);
3399 rb_file_const(
"FNM_NOESCAPE",
INT2FIX(FNM_NOESCAPE));
3406 rb_file_const(
"FNM_PATHNAME",
INT2FIX(FNM_PATHNAME));
3413 rb_file_const(
"FNM_DOTMATCH",
INT2FIX(FNM_DOTMATCH));
3420 rb_file_const(
"FNM_CASEFOLD",
INT2FIX(FNM_CASEFOLD));
3426 rb_file_const(
"FNM_EXTGLOB",
INT2FIX(FNM_EXTGLOB));
3433 rb_file_const(
"FNM_SYSCASE",
INT2FIX(FNM_SYSCASE));
3440 rb_file_const(
"FNM_SHORTNAME",
INT2FIX(FNM_SHORTNAME));
3443 #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 xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#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 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.
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, 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 always regardless of runtime -W flag.
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_enc_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.
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.
#define rb_check_frozen
Just another name of rb_check_frozen.
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...
void rb_gc_mark(VALUE obj)
Marks an object.
void rb_memerror(void)
Triggers out-of-memory error.
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.
#define RUBY_UBF_IO
A special UBF for blocking IO operations.
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.
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Allows the passed function to run in parallel with other Ruby threads.
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.
VALUE rb_int2inum(intptr_t i)
Converts a C's intptr_t into an instance of rb_cInteger.
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 Data_Wrap_Struct(klass, mark, free, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define DATA_PTR(obj)
Convenient getter macro.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
#define SafeStringValue(v)
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.
#define FilePathStringValue(v)
#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.