14#include "ruby/internal/config.h"
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)
51# define NAMLEN(dirent) (dirent)->d_namlen
52# define HAVE_DIRENT_NAMLEN 1
53# ifdef HAVE_SYS_NDIR_H
63# include "win32/dir.h"
72char *strchr(
char*,
char);
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
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)
152need_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);
175has_nonascii(
const char *ptr,
size_t len)
185# define IF_NORMALIZE_UTF8PATH(something) something
187# define IF_NORMALIZE_UTF8PATH(something)
190#if defined(IFTODT) && defined(DT_UNKNOWN)
191# define EMULATE_IFTODT 0
193# define EMULATE_IFTODT 1
197# define IFTODT(m) (((m) & S_IFMT) / ((~S_IFMT & (S_IFMT-1)) + 1))
202 path_exist = DT_UNKNOWN,
203 path_directory = DT_DIR,
204 path_regular = DT_REG,
205 path_symlink = DT_LNK,
208 path_directory = IFTODT(S_IFDIR),
209 path_regular = IFTODT(S_IFREG),
210 path_symlink = IFTODT(S_IFLNK),
216#define FNM_NOESCAPE 0x01
217#define FNM_PATHNAME 0x02
218#define FNM_DOTMATCH 0x04
219#define FNM_CASEFOLD 0x08
220#define FNM_EXTGLOB 0x10
221#if CASEFOLD_FILESYSTEM
222#define FNM_SYSCASE FNM_CASEFOLD
227#define FNM_SHORTNAME 0x20
229#define FNM_SHORTNAME 0
231#define FNM_GLOB_NOSORT 0x40
232#define FNM_GLOB_SKIPDOT 0x80
237# define Next(p, e, enc) ((p)+ rb_enc_mbclen((p), (e), (enc)))
238# define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
249 const int nocase = flags & FNM_CASEFOLD;
250 const int escape = !(flags & FNM_NOESCAPE);
255 if (p >= pend)
return NULL;
256 if (*p ==
'!' || *p ==
'^') {
263 if (escape && *t1 ==
'\\')
267 p = t1 + (r = rb_enc_mbclen(t1, pend, enc));
268 if (p >= pend)
return NULL;
269 if (p[0] ==
'-' && p[1] !=
']') {
270 const char *t2 = p + 1;
272 if (escape && *t2 ==
'\\')
276 p = t2 + (r2 = rb_enc_mbclen(t2, pend, enc));
278 if ((r <= (send-s) && memcmp(t1, s, r) == 0) ||
279 (r2 <= (send-s) && memcmp(t2, s, r2) == 0)) {
283 c1 = rb_enc_codepoint(s, send, enc);
284 if (nocase) c1 = rb_enc_toupper(c1, enc);
285 c2 = rb_enc_codepoint(t1, pend, enc);
286 if (nocase) c2 = rb_enc_toupper(c2, enc);
287 if (c1 < c2)
continue;
288 c2 = rb_enc_codepoint(t2, pend, enc);
289 if (nocase) c2 = rb_enc_toupper(c2, enc);
290 if (c1 > c2)
continue;
294 if (r <= (send-s) && memcmp(t1, s, r) == 0) {
298 if (!nocase)
continue;
299 c1 = rb_enc_toupper(rb_enc_codepoint(s, send, enc), enc);
300 c2 = rb_enc_toupper(rb_enc_codepoint(p, pend, enc), enc);
301 if (c1 != c2)
continue;
306 return ok == not ? NULL : (
char *)p + 1;
314#define UNESCAPE(p) (escape && *(p) == '\\' ? (p) + 1 : (p))
315#define ISEND(p) (!*(p) || (pathname && *(p) == '/'))
316#define RETURN(val) return *pcur = p, *scur = s, (val);
325 const int period = !(flags & FNM_DOTMATCH);
326 const int pathname = flags & FNM_PATHNAME;
327 const int escape = !(flags & FNM_NOESCAPE);
328 const int nocase = flags & FNM_CASEFOLD;
330 const char *ptmp = 0;
331 const char *stmp = 0;
333 const char *p = *pcur;
334 const char *pend = p + strlen(p);
335 const char *s = *scur;
336 const char *send = s + strlen(s);
340 if (period && *s ==
'.' && *UNESCAPE(p) !=
'.')
346 do { p++; }
while (*p ==
'*');
347 if (ISEND(UNESCAPE(p))) {
368 if ((t = bracket(p + 1, pend, s, send, flags, enc)) != 0) {
380 RETURN(ISEND(p) ? 0 : FNM_NOMATCH);
383 r = rb_enc_precise_mbclen(p, pend, enc);
386 if (r <= (send-s) && memcmp(p, s, r) == 0) {
391 if (!nocase)
goto failed;
392 if (rb_enc_toupper(rb_enc_codepoint(p, pend, enc), enc) !=
393 rb_enc_toupper(rb_enc_codepoint(s, send, enc), enc))
402 Inc(stmp, send, enc);
417 const char *p = pattern;
418 const char *s = string;
419 const char *send = s + strlen(
string);
420 const int period = !(flags & FNM_DOTMATCH);
421 const int pathname = flags & FNM_PATHNAME;
423 const char *ptmp = 0;
424 const char *stmp = 0;
428 if (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/') {
429 do { p += 3; }
while (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/');
433 if (fnmatch_helper(&p, &s, flags, enc) == 0) {
434 while (*s && *s !=
'/') Inc(s, send, enc);
444 if (ptmp && stmp && !(period && *stmp ==
'.')) {
445 while (*stmp && *stmp !=
'/') Inc(stmp, send, enc);
457 return fnmatch_helper(&p, &s, flags, enc);
472 rb_gc_mark(dir->path);
480 if (dir->dir) closedir(dir->dir);
485dir_memsize(
const void *ptr)
492 {dir_mark, dir_free, dir_memsize,},
493 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY
499dir_s_alloc(
VALUE klass)
512nogvl_opendir(
void *ptr)
514 const char *path = ptr;
516 return (
void *)opendir(path);
520opendir_without_gvl(
const char *path)
522 if (vm_initialized) {
523 union {
const void *in;
void *out; } u;
530 return opendir(path);
539 rb_encoding *fsenc =
NIL_P(enc) ? rb_filesystem_encoding() : rb_to_encoding(enc);
543 dirname = rb_str_encode_ospath(dirname);
547 if (dp->dir) closedir(dp->dir);
551 path = RSTRING_PTR(dirname);
552 dp->dir = opendir_without_gvl(path);
553 if (dp->dir == NULL) {
555 if (rb_gc_for_fd(e)) {
556 dp->dir = opendir_without_gvl(path);
558#ifdef HAVE_GETATTRLIST
560 u_int32_t attrbuf[1];
561 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0};
562 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW) == 0) {
563 dp->dir = opendir_without_gvl(path);
567 if (dp->dir == NULL) {
569 rb_syserr_fail_path(e, orig);
583 dir_initialize(ec, dir, dirname, enc);
591 return dir_close(dir);
594NORETURN(
static void dir_closed(
void));
612 struct dir_data *dirp = dir_get(dir);
613 if (!dirp->dir) dir_closed();
617#define GetDIR(obj, dirp) ((dirp) = dir_check(obj))
627dir_inspect(
VALUE dir)
632 if (!
NIL_P(dirp->path)) {
640 return rb_funcallv(dir, idTo_s, 0, 0);
646#if defined(__sun) && !defined(HAVE_DIRFD)
647# if defined(HAVE_DIR_D_FD)
648# define dirfd(x) ((x)->d_fd)
650# elif defined(HAVE_DIR_DD_FD)
651# define dirfd(x) ((x)->dd_fd)
678 fd = dirfd(dirp->dir);
684#define dir_fileno rb_f_notimplement
711 switch (rb_enc_to_index(enc)) {
712 case ENCINDEX_ASCII_8BIT:
713 case ENCINDEX_US_ASCII:
720# define READDIR(dir, enc) rb_w32_readdir((dir), (enc))
722# define READDIR(dir, enc) readdir((dir))
727to_be_skipped(
const struct dirent *dp)
729 const char *name = dp->d_name;
730 if (name[0] !=
'.')
return FALSE;
731#ifdef HAVE_DIRENT_NAMLEN
732 switch (NAMLEN(dp)) {
734 if (name[1] !=
'.')
return FALSE;
741 if (!name[1])
return TRUE;
742 if (name[1] !=
'.')
return FALSE;
743 if (!name[2])
return TRUE;
768 if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
810 return dir_each_entry(dir, dir_yield,
Qnil, FALSE);
818 IF_NORMALIZE_UTF8PATH(
int norm_p);
821 rewinddir(dirp->dir);
822 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path)));
823 while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
824 const char *name = dp->d_name;
825 size_t namlen = NAMLEN(dp);
828 if (children_only && name[0] ==
'.') {
829 if (namlen == 1)
continue;
830 if (namlen == 2 && name[1] ==
'.')
continue;
832#if NORMALIZE_UTF8PATH
833 if (norm_p && has_nonascii(name, namlen) &&
834 !
NIL_P(path = rb_str_normalize_ospath(name, namlen))) {
835 path = rb_external_str_with_enc(path, dirp->enc);
865 pos = telldir(dirp->dir);
866 return rb_int2inum(pos);
869#define dir_tell rb_f_notimplement
894 seekdir(dirp->dir, p);
898#define dir_seek rb_f_notimplement
922#define dir_set_pos rb_f_notimplement
942 rewinddir(dirp->dir);
962 if (!dirp->dir)
return Qnil;
970nogvl_chdir(
void *ptr)
972 const char *path = ptr;
974 return (
void *)(
VALUE)chdir(path);
980 if (chdir(RSTRING_PTR(path)) < 0)
981 rb_sys_fail_path(path);
984static int chdir_blocking = 0;
988 VALUE old_path, new_path;
996 dir_chdir(args->new_path);
999 if (
NIL_P(chdir_thread))
1005chdir_restore(
VALUE v)
1010 if (chdir_blocking == 0)
1011 chdir_thread =
Qnil;
1012 dir_chdir(args->old_path);
1062 if (rb_check_arity(argc, 0, 1) == 1) {
1063 path = rb_str_encode_ospath(rb_get_path(argv[0]));
1066 const char *dist = getenv(
"HOME");
1068 dist = getenv(
"LOGDIR");
1074 if (chdir_blocking > 0) {
1078 rb_warn(
"conflicting chdir during another chdir block");
1084 args.old_path = rb_str_encode_ospath(rb_dir_getwd());
1085 args.new_path = path;
1090 char *p = RSTRING_PTR(path);
1094 rb_sys_fail_path(path);
1102rb_dir_getwd_ospath(
void)
1108#undef RUBY_UNTYPED_DATA_WARNING
1109#define RUBY_UNTYPED_DATA_WARNING 0
1114 cwd = rb_str_normalize_ospath(path, strlen(path));
1129 int fsenc = rb_enc_to_index(fs);
1130 VALUE cwd = rb_dir_getwd_ospath();
1133 case ENCINDEX_US_ASCII:
1134 fsenc = ENCINDEX_ASCII_8BIT;
1135 case ENCINDEX_ASCII_8BIT:
1137#if defined _WIN32 || defined __APPLE__
1142 return rb_enc_associate_index(cwd, fsenc);
1158dir_s_getwd(
VALUE dir)
1160 return rb_dir_getwd();
1164check_dirname(
VALUE dir)
1172 enc = rb_enc_get(d);
1175 pend = rb_enc_path_end(rb_enc_path_skip_prefix(path, pend, enc), pend, enc);
1176 if (pend - path < len) {
1180 return rb_str_encode_ospath(d);
1183#if defined(HAVE_CHROOT)
1196 path = check_dirname(path);
1197 if (chroot(RSTRING_PTR(path)) == -1)
1198 rb_sys_fail_path(path);
1203#define dir_s_chroot rb_f_notimplement
1212nogvl_mkdir(
void *ptr)
1216 return (
void *)(
VALUE)mkdir(m->path, m->mode);
1240 if (rb_scan_args(argc, argv,
"11", &path, &vmode) == 2) {
1247 path = check_dirname(path);
1248 m.path = RSTRING_PTR(path);
1251 rb_sys_fail_path(path);
1257nogvl_rmdir(
void *ptr)
1259 const char *path = ptr;
1261 return (
void *)(
VALUE)rmdir(path);
1279 dir = check_dirname(dir);
1280 p = RSTRING_PTR(dir);
1283 rb_sys_fail_path(dir);
1289#ifdef RUBY_FUNCTION_NAME_STRING
1296#ifndef RUBY_FUNCTION_NAME_STRING
1297#define sys_enc_warning_in(func, mesg, enc) sys_enc_warning(mesg, enc)
1301sys_warning_1(
VALUE mesg)
1304#ifdef RUBY_FUNCTION_NAME_STRING
1305 rb_sys_enc_warning(arg->enc,
"%s: %s", arg->func, arg->mesg);
1307 rb_sys_enc_warning(arg->enc,
"%s", arg->mesg);
1313sys_enc_warning_in(
const char *func,
const char *mesg,
rb_encoding *enc)
1316#ifdef RUBY_FUNCTION_NAME_STRING
1321 rb_protect(sys_warning_1, (
VALUE)&arg, 0);
1324#define GLOB_VERBOSE (1U << (sizeof(int) * CHAR_BIT - 1))
1325#define sys_warning(val, enc) \
1326 ((flags & GLOB_VERBOSE) ? sys_enc_warning_in(RUBY_FUNCTION_NAME_STRING, (val), (enc)) :(void)0)
1329glob_alloc_size(
size_t x,
size_t y)
1341glob_alloc_n(
size_t x,
size_t y)
1343 return malloc(glob_alloc_size(x, y));
1347glob_realloc_n(
void *p,
size_t x,
size_t y)
1349 return realloc(p, glob_alloc_size(x, y));
1352#define GLOB_ALLOC(type) ((type *)malloc(sizeof(type)))
1353#define GLOB_ALLOC_N(type, n) ((type *)glob_alloc_n(sizeof(type), n))
1354#define GLOB_REALLOC(ptr, size) realloc((ptr), (size))
1355#define GLOB_REALLOC_N(ptr, n) glob_realloc_n(ptr, sizeof(*(ptr)), n)
1356#define GLOB_FREE(ptr) free(ptr)
1357#define GLOB_JUMP_TAG(status) (((status) == -1) ? rb_memerror() : rb_jump_tag(status))
1363ALWAYS_INLINE(
static int to_be_ignored(
int e));
1367 return e == ENOENT || e == ENOTDIR;
1371#define STAT(p, s) rb_w32_ustati128((p), (s))
1373#define lstat(p, s) rb_w32_ulstati128((p), (s))
1375#define STAT(p, s) stat((p), (s))
1378typedef int ruby_glob_errfunc(
const char*,
VALUE,
const void*,
int);
1381 ruby_glob_errfunc *error;
1385at_subpath(
int fd,
size_t baselen,
const char *path)
1388 if (fd != (
int)AT_FDCWD && baselen > 0) {
1390 if (*path ==
'/') ++path;
1393 return *path ? path :
".";
1398do_stat(
int fd,
size_t baselen,
const char *path,
struct stat *pst,
int flags,
rb_encoding *enc)
1401 int ret = fstatat(fd, at_subpath(fd, baselen, path), pst, 0);
1403 int ret = STAT(path, pst);
1405 if (ret < 0 && !to_be_ignored(errno))
1406 sys_warning(path, enc);
1411#if defined HAVE_LSTAT || defined lstat || USE_OPENDIR_AT
1413do_lstat(
int fd,
size_t baselen,
const char *path,
struct stat *pst,
int flags,
rb_encoding *enc)
1416 int ret = fstatat(fd, at_subpath(fd, baselen, path), pst, AT_SYMLINK_NOFOLLOW);
1418 int ret = lstat(path, pst);
1420 if (ret < 0 && !to_be_ignored(errno))
1421 sys_warning(path, enc);
1426#define do_lstat do_stat
1435with_gvl_gc_for_fd(
void *ptr)
1439 return (
void *)RBOOL(rb_gc_for_fd(*e));
1443gc_for_fd_with_gvl(
int e)
1448 return RBOOL(rb_gc_for_fd(e));
1452nogvl_opendir_at(
void *ptr)
1458 const int opendir_flags = (O_RDONLY|O_CLOEXEC|
1463 int fd = openat(oaa->basefd, oaa->path, opendir_flags);
1465 dirp = fd >= 0 ? fdopendir(fd) : 0;
1469 switch (gc_for_fd_with_gvl(e)) {
1471 if (fd < 0) fd = openat(oaa->basefd, oaa->path, opendir_flags);
1472 if (fd >= 0) dirp = fdopendir(fd);
1473 if (dirp)
return dirp;
1478 if (fd >= 0) close(fd);
1483 dirp = opendir(oaa->path);
1484 if (!dirp && gc_for_fd_with_gvl(errno))
1485 dirp = opendir(oaa->path);
1492opendir_at(
int basefd,
const char *path)
1496 oaa.basefd = basefd;
1502 return nogvl_opendir_at(&oaa);
1506do_opendir(
const int basefd,
size_t baselen,
const char *path,
int flags,
rb_encoding *enc,
1507 ruby_glob_errfunc *errfunc,
VALUE arg,
int *status)
1512 if (!fundamental_encoding_p(enc)) {
1513 tmp = rb_enc_str_new(path, strlen(path), enc);
1514 tmp = rb_str_encode_ospath(tmp);
1515 path = RSTRING_PTR(tmp);
1518 dirp = opendir_at(basefd, at_subpath(basefd, baselen, path));
1523 if (!to_be_ignored(e)) {
1525 *status = (*errfunc)(path, arg, enc, e);
1528 sys_warning(path, enc);
1540enum glob_pattern_type { PLAIN, ALPHA, BRACE, MAGICAL, RECURSIVE, MATCH_ALL, MATCH_DIR };
1543static enum glob_pattern_type
1544has_magic(
const char *p,
const char *pend,
int flags,
rb_encoding *enc)
1546 const int escape = !(flags & FNM_NOESCAPE);
1552 while (p < pend && (c = *p++) != 0) {
1564 if (escape && p++ >= pend)
1583 p = Next(p-1, pend, enc);
1586 return hasmagical ? MAGICAL : hasalpha ? ALPHA : PLAIN;
1591find_dirsep(
const char *p,
const char *pend,
int flags,
rb_encoding *enc)
1593 const int escape = !(flags & FNM_NOESCAPE);
1598 while ((c = *p++) != 0) {
1620 if (escape && !(c = *p++))
1625 p = Next(p-1, pend, enc);
1633remove_backslashes(
char *p,
register const char *pend,
rb_encoding *enc)
1641 memmove(t, s, p - s);
1652 memmove(t, s, p - s);
1659 enum glob_pattern_type type;
1663static void glob_free_pattern(
struct glob_pattern *list);
1666glob_make_pattern(
const char *p,
const char *e,
int flags,
rb_encoding *enc)
1672 while (p < e && *p) {
1674 if (!tmp)
goto error;
1675 if (p + 2 < e && p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/') {
1677 do { p += 3;
while (*p ==
'/') p++; }
while (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/');
1678 tmp->type = RECURSIVE;
1684 const char *m = find_dirsep(p, e, flags, enc);
1685 const enum glob_pattern_type magic = has_magic(p, m, flags, enc);
1686 const enum glob_pattern_type non_magic = (USE_NAME_ON_FS || FNM_SYSCASE) ? PLAIN : ALPHA;
1689 if (!(FNM_SYSCASE || magic > non_magic) && !recursive && *m) {
1691 while (has_magic(m+1, m2 = find_dirsep(m+1, e, flags, enc), flags, enc) <= non_magic &&
1696 buf = GLOB_ALLOC_N(
char, m-p+1);
1701 memcpy(buf, p, m-p);
1703 tmp->type = magic > MAGICAL ? MAGICAL : magic > non_magic ? magic : PLAIN;
1722 tmp->type = dirsep ? MATCH_DIR : MATCH_ALL;
1731 glob_free_pattern(list);
1742 GLOB_FREE(tmp->str);
1748join_path(
const char *path,
size_t len,
int dirsep,
const char *name,
size_t namlen)
1750 char *buf = GLOB_ALLOC_N(
char, len+namlen+(dirsep?1:0)+1);
1753 memcpy(buf, path, len);
1757 memcpy(buf+len, name, namlen);
1758 buf[len+namlen] =
'\0';
1762#ifdef HAVE_GETATTRLIST
1763# if defined HAVE_FGETATTRLIST
1764# define is_case_sensitive(dirp, path) is_case_sensitive(dirp)
1766# define is_case_sensitive(dirp, path) is_case_sensitive(path)
1769is_case_sensitive(
DIR *dirp,
const char *path)
1773 vol_capabilities_attr_t cap[1];
1775 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, 0, ATTR_VOL_INFO|ATTR_VOL_CAPABILITIES};
1776 const vol_capabilities_attr_t *
const cap = attrbuf[0].cap;
1777 const int idx = VOL_CAPABILITIES_FORMAT;
1778 const uint32_t mask = VOL_CAP_FMT_CASE_SENSITIVE;
1780# if defined HAVE_FGETATTRLIST
1781 if (fgetattrlist(dirfd(dirp), &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW))
1784 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW))
1787 if (!(cap->valid[idx] & mask))
1789 return (cap->capabilities[idx] & mask) != 0;
1793replace_real_basename(
char *path,
long base,
rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
1797 attrreference_t ref[1];
1798 fsobj_type_t objtype;
1799 char path[MAXPATHLEN * 3];
1801 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_NAME|ATTR_CMN_OBJTYPE};
1802 const attrreference_t *
const ar = attrbuf[0].ref;
1806 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
1809 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW)) {
1810 if (!to_be_ignored(errno))
1811 sys_warning(path, enc);
1815 switch (attrbuf[0].objtype) {
1816 case VREG: *
type = path_regular;
break;
1817 case VDIR: *
type = path_directory;
break;
1818 case VLNK: *
type = path_symlink;
break;
1819 default: *
type = path_exist;
break;
1821 name = (
char *)ar + ar->attr_dataoffset;
1822 len = (long)ar->attr_length - 1;
1823 if (name + len > (
char *)attrbuf +
sizeof(attrbuf))
1826# if NORMALIZE_UTF8PATH
1827 if (norm_p && has_nonascii(name, len)) {
1828 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name, len))) {
1834 tmp = GLOB_REALLOC(path, base + len + 1);
1837 memcpy(path + base, name, len);
1838 path[base + len] =
'\0';
1845int rb_w32_reparse_symlink_p(
const WCHAR *path);
1848replace_real_basename(
char *path,
long base,
rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
1850 char *plainname = path;
1851 volatile VALUE tmp = 0;
1852 WIN32_FIND_DATAW fd;
1853 WIN32_FILE_ATTRIBUTE_DATA fa;
1855 HANDLE h = INVALID_HANDLE_VALUE;
1858 if (!fundamental_encoding_p(enc)) {
1859 tmp = rb_enc_str_new_cstr(plainname, enc);
1860 tmp = rb_str_encode_ospath(tmp);
1861 plainname = RSTRING_PTR(tmp);
1863 wplain = rb_w32_mbstr_to_wstr(CP_UTF8, plainname, -1, &wlen);
1865 if (!wplain)
return path;
1866 if (GetFileAttributesExW(wplain, GetFileExInfoStandard, &fa)) {
1867 h = FindFirstFileW(wplain, &fd);
1868 e = rb_w32_map_errno(GetLastError());
1870 if (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1871 if (!rb_w32_reparse_symlink_p(wplain))
1872 fa.dwFileAttributes &= ~FILE_ATTRIBUTE_REPARSE_POINT;
1875 if (h == INVALID_HANDLE_VALUE) {
1877 if (e && !to_be_ignored(e)) {
1879 sys_warning(path, enc);
1885 (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ? path_symlink :
1886 (fa.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? path_directory :
1890 tmp = rb_w32_conv_from_wchar(fd.cFileName, enc);
1891 wlen = RSTRING_LEN(tmp);
1892 buf = GLOB_REALLOC(path, base + wlen + 1);
1895 memcpy(path + base, RSTRING_PTR(tmp), wlen);
1896 path[base + wlen] = 0;
1902 wlen = WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, NULL, 0, NULL, NULL);
1903 utf8filename = GLOB_REALLOC(0, wlen);
1906 WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, utf8filename, wlen, NULL, NULL);
1907 buf = GLOB_REALLOC(path, base + wlen + 1);
1910 memcpy(path + base, utf8filename, wlen);
1911 path[base + wlen] = 0;
1913 GLOB_FREE(utf8filename);
1918#elif USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
1919# error not implemented
1923# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1928# define S_ISLNK(m) (0)
1930# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1935 void (*func)(
const char *,
VALUE,
void *);
1943#define glob_call_func(func, path, arg, enc) (*(func))((path), (arg), (void *)(enc))
1946glob_func_caller(
VALUE val)
1950 glob_call_func(args->func, args->path, args->value, args->enc);
1961glob_func_warning(
VALUE val)
1964 rb_syserr_enc_warning(arg->error, arg->enc,
"%s", arg->path);
1970rb_glob_warning(
const char *path,
VALUE a,
const void *enc,
int error)
1978 rb_protect(glob_func_warning, (
VALUE)&args, &status);
1983NORETURN(
static VALUE glob_func_error(
VALUE val));
1986glob_func_error(
VALUE val)
1989 VALUE path = rb_enc_str_new_cstr(arg->path, arg->enc);
1995rb_glob_error(
const char *path,
VALUE a,
const void *enc,
int error)
2006 errfunc = glob_func_warning;
2011 rb_protect(errfunc, (
VALUE)&args, &status);
2019 const char *d_altname;
2027 if (fnmatch(pat, enc, name, flags) == 0)
return 1;
2029 if (dp->d_altname && (flags & FNM_SHORTNAME)) {
2030 if (fnmatch(pat, enc, dp->d_altname, flags) == 0)
return 1;
2042 rb_pathtype_t pathtype;
2055dirent_match_brace(
const char *pattern,
VALUE val,
void *enc)
2059 return dirent_match(pattern, enc, arg->name, arg->dp, arg->flags);
2068 size_t path_len = 0;
2070 for (p = *beg; p; p = p->next) {
2085 path_len = strlen(str);
2086 path = GLOB_ALLOC_N(
char, path_len + 1);
2088 memcpy(path, str, path_len);
2089 path[path_len] =
'\0';
2093 size_t len = strlen(str);
2095 tmp = GLOB_REALLOC(path, path_len + len + 2);
2098 path[path_len++] =
'/';
2099 memcpy(path + path_len, str, len);
2101 path[path_len] =
'\0';
2108static int push_caller(
const char *path,
VALUE val,
void *enc);
2113static const size_t rb_dirent_name_offset =
2117dirent_copy(
const struct dirent *dp,
rb_dirent_t *rdp)
2119 if (!dp)
return NULL;
2120 size_t namlen = NAMLEN(dp);
2121 const size_t altlen =
2123 dp->d_altlen ? dp->d_altlen + 1 :
2127 if (!rdp && !(newrdp = malloc(rb_dirent_name_offset + namlen + 1 + altlen)))
2129 newrdp->d_namlen = namlen;
2131 char *name = (
char *)newrdp + rb_dirent_name_offset;
2132 memcpy(name, dp->d_name, namlen);
2133 name[namlen] =
'\0';
2135 newrdp->d_altname = NULL;
2137 char *
const altname = name + namlen + 1;
2138 memcpy(altname, dp->d_altname, altlen - 1);
2139 altname[altlen - 1] =
'\0';
2140 newrdp->d_altname = altname;
2143 newrdp->d_name = name;
2146 newrdp->d_name = dp->d_name;
2148 newrdp->d_altname = dp->d_altname;
2152 newrdp->d_type = dp->d_type;
2171glob_sort_cmp(
const void *a,
const void *b,
void *e)
2175 return strcmp(ent1->d_name, ent2->d_name);
2181 if (flags & FNM_GLOB_NOSORT) {
2182 closedir(ent->nosort.dirp);
2183 ent->nosort.dirp = NULL;
2185 else if (ent->sort.entries) {
2186 for (
size_t i = 0, count = ent->sort.count; i < count;) {
2187 GLOB_FREE(ent->sort.entries[i++]);
2189 GLOB_FREE(ent->sort.entries);
2190 ent->sort.entries = NULL;
2191 ent->sort.count = ent->sort.idx = 0;
2199 if (flags & FNM_GLOB_NOSORT) {
2200 ent->nosort.dirp = dirp;
2206 size_t count = 0, capacity = 0;
2207 ent->sort.count = 0;
2209 ent->sort.entries = 0;
2211 if ((capacity = dirp->nfiles) > 0) {
2212 if (!(newp = GLOB_ALLOC_N(
rb_dirent_t, capacity))) {
2216 ent->sort.entries = newp;
2219 while ((dp = READDIR(dirp, enc)) != NULL) {
2224 if (count >= capacity) {
2226 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, capacity)))
2228 ent->sort.entries = newp;
2230 ent->sort.entries[count++] = rdp;
2231 ent->sort.count = count;
2234 if (count < capacity) {
2235 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, count))) {
2236 glob_dir_finish(ent, 0);
2239 ent->sort.entries = newp;
2241 ruby_qsort(ent->sort.entries, ent->sort.count,
sizeof(ent->sort.entries[0]),
2242 glob_sort_cmp, NULL);
2247 glob_dir_finish(ent, 0);
2255 if (flags & FNM_GLOB_NOSORT) {
2256 return dirent_copy(READDIR(ent->nosort.dirp, enc), &ent->nosort.ent);
2258 else if (ent->sort.idx < ent->sort.count) {
2259 return ent->sort.entries[ent->sort.idx++];
2273 rb_pathtype_t pathtype,
2284 int plain = 0, brace = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0;
2285 int escape = !(flags & FNM_NOESCAPE);
2286 size_t pathlen = baselen + namelen;
2288 rb_check_stack_overflow();
2290 for (cur = beg; cur < end; ++cur) {
2292 if (p->type == RECURSIVE) {
2301#if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2308 if (!recursive || strchr(p->str,
'/')) {
2322 rb_bug(
"continuous RECURSIVEs");
2328 char* brace_path = join_path_from_pattern(beg);
2329 if (!brace_path)
return -1;
2332 args.baselen = baselen;
2333 args.namelen = namelen;
2334 args.dirsep = dirsep;
2335 args.pathtype = pathtype;
2339 status = ruby_brace_expand(brace_path, flags, push_caller, (
VALUE)&args, enc,
Qfalse);
2340 GLOB_FREE(brace_path);
2345 if (match_all && pathtype == path_unknown) {
2346 if (do_lstat(fd, baselen, path, &st, flags, enc) == 0) {
2347 pathtype = IFTODT(st.st_mode);
2350 pathtype = path_noent;
2353 if (match_dir && (pathtype == path_unknown || pathtype == path_symlink)) {
2354 if (do_stat(fd, baselen, path, &st, flags, enc) == 0) {
2355 pathtype = IFTODT(st.st_mode);
2358 pathtype = path_noent;
2361 if (match_all && pathtype > path_noent) {
2362 const char *subpath = path + baselen + (baselen && path[baselen] ==
'/');
2363 status = glob_call_func(funcs->match, subpath, arg, enc);
2364 if (status)
return status;
2366 if (match_dir && pathtype == path_directory) {
2367 int seplen = (baselen && path[baselen] ==
'/');
2368 const char *subpath = path + baselen + seplen;
2369 char *tmp = join_path(subpath, namelen - seplen, dirsep,
"", 0);
2370 if (!tmp)
return -1;
2371 status = glob_call_func(funcs->match, tmp, arg, enc);
2373 if (status)
return status;
2377 if (pathtype == path_noent)
return 0;
2379 if (magical || recursive) {
2382# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2383 char *plainname = 0;
2385 IF_NORMALIZE_UTF8PATH(
int norm_p);
2386# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2387 if (cur + 1 == end && (*cur)->type <= ALPHA) {
2388 plainname = join_path(path, pathlen, dirsep, (*cur)->str, strlen((*cur)->str));
2389 if (!plainname)
return -1;
2390 dirp = do_opendir(fd, basename, plainname, flags, enc, funcs->error, arg, &status);
2391 GLOB_FREE(plainname);
2397 dirp = do_opendir(fd, baselen, path, flags, enc, funcs->error, arg, &status);
2399# if FNM_SYSCASE || NORMALIZE_UTF8PATH
2400 if ((magical < 2) && !recursive && (errno == EACCES)) {
2407 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *path ? path :
"."));
2409# if NORMALIZE_UTF8PATH
2410 if (!(norm_p || magical || recursive)) {
2415# ifdef HAVE_GETATTRLIST
2416 if (is_case_sensitive(dirp, path) == 0)
2417 flags |= FNM_CASEFOLD;
2420 if (!glob_opendir(&globent, dirp, flags, enc)) {
2423 status = (*funcs->error)(path, arg, enc, ENOMEM);
2426 sys_warning(path, enc);
2431 int skipdot = (flags & FNM_GLOB_SKIPDOT);
2432 flags |= FNM_GLOB_SKIPDOT;
2434 while ((dp = glob_getent(&globent, flags, enc)) != NULL) {
2436 rb_pathtype_t new_pathtype = path_unknown;
2440 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
2443 namlen = dp->d_namlen;
2444 if (name[0] ==
'.') {
2448 if (recursive && !(flags & FNM_DOTMATCH))
continue;
2449 if (skipdot)
continue;
2451 new_pathtype = path_directory;
2453 else if (namlen == 2 && name[1] ==
'.') {
2459# if NORMALIZE_UTF8PATH
2460 if (norm_p && has_nonascii(name, namlen)) {
2461 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name, namlen))) {
2466 buf = join_path(path, pathlen, dirsep, name, namlen);
2472 name = buf + pathlen + (dirsep != 0);
2474 if (dp->d_type != DT_UNKNOWN) {
2476 new_pathtype = dp->d_type;
2479 if (recursive && dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1) &&
2480 new_pathtype == path_unknown) {
2482 if (do_lstat(fd, baselen, buf, &st, flags, enc) == 0)
2483 new_pathtype = IFTODT(st.st_mode);
2485 new_pathtype = path_noent;
2488 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, (end - beg) * 2);
2495 for (cur = beg; cur < end; ++cur) {
2498 if (p->type == RECURSIVE) {
2499 if (new_pathtype == path_directory ||
2500 new_pathtype == path_exist) {
2501 if (dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1))
2511 if (ruby_brace_expand(p->str, flags, dirent_match_brace,
2513 *new_end++ = p->next;
2516# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2518 *new_end++ = p->next;
2524 if (dirent_match(p->str, enc, name, dp, flags))
2525 *new_end++ = p->next;
2531 status = glob_helper(fd, buf, baselen, name - buf - baselen + namlen, 1,
2532 new_pathtype, new_beg, new_end,
2533 flags, funcs, arg, enc);
2539 glob_dir_finish(&globent, flags);
2544# if FNM_SYSCASE || NORMALIZE_UTF8PATH
2547 copy_beg = copy_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
2548 if (!copy_beg)
return -1;
2549 for (cur = beg; cur < end; ++cur)
2550 *copy_end++ = (*cur)->type <= ALPHA ? *cur : 0;
2552 for (cur = copy_beg; cur < copy_end; ++cur) {
2554 rb_pathtype_t new_pathtype = path_unknown;
2557 size_t len = strlen((*cur)->str) + 1;
2558 name = GLOB_ALLOC_N(
char, len);
2563 memcpy(name, (*cur)->str, len);
2565 len = remove_backslashes(name, name+len-1, enc) - name;
2567 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
2573 *new_end++ = (*cur)->next;
2574 for (cur2 = cur + 1; cur2 < copy_end; ++cur2) {
2575 if (*cur2 && fnmatch((*cur2)->str, enc, name, flags) == 0) {
2576 *new_end++ = (*cur2)->next;
2581 buf = join_path(path, pathlen, dirsep, name, len);
2588#if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2589 if ((*cur)->type == ALPHA) {
2590 buf = replace_real_basename(buf, pathlen + (dirsep != 0), enc,
2591 IF_NORMALIZE_UTF8PATH(1)+0,
2592 flags, &new_pathtype);
2596 status = glob_helper(fd, buf, baselen,
2597 namelen + strlen(buf + pathlen), 1,
2598 new_pathtype, new_beg, new_end,
2599 flags, funcs, arg, enc);
2606 GLOB_FREE(copy_beg);
2613push_caller(
const char *path,
VALUE val,
void *enc)
2619 list = glob_make_pattern(path, path + strlen(path), arg->flags, enc);
2623 status = glob_helper(arg->fd, arg->path, arg->baselen, arg->namelen, arg->dirsep,
2624 arg->pathtype, &list, &list + 1, arg->flags, arg->funcs,
2626 glob_free_pattern(list);
2630static int ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
2642push_glob0_caller(
const char *path,
VALUE val,
void *enc)
2645 return ruby_glob0(path, arg->fd, arg->base, arg->flags, arg->funcs, arg->arg, enc);
2649ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
2654 const char *root, *start;
2656 size_t n, baselen = 0;
2657 int status, dirsep = FALSE;
2659 start = root = path;
2668 return ruby_brace_expand(path, flags, push_glob0_caller, (
VALUE)&args, enc,
Qfalse);
2671 flags |= FNM_SYSCASE;
2673 root = rb_enc_path_skip_prefix(root, root + strlen(root), enc);
2676 if (*root ==
'/') root++;
2685 buf = GLOB_ALLOC_N(
char, n + 1);
2686 if (!buf)
return -1;
2687 MEMCPY(buf, start,
char, n);
2690 list = glob_make_pattern(root, root + strlen(root), flags, enc);
2695 status = glob_helper(fd, buf, baselen, n-baselen, dirsep,
2696 path_unknown, &list, &list + 1,
2697 flags, funcs, arg, enc);
2698 glob_free_pattern(list);
2710 return ruby_glob0(path, AT_FDCWD, 0, flags & ~GLOB_VERBOSE,
2711 &funcs, arg, rb_ascii8bit_encoding());
2715rb_glob_caller(
const char *path,
VALUE a,
void *enc)
2721 rb_protect(glob_func_caller, a, &status);
2726 rb_glob_caller, rb_glob_error,
2730rb_glob(
const char *path,
void (*func)(
const char *,
VALUE,
void *),
VALUE arg)
2737 args.enc = rb_ascii8bit_encoding();
2739 status = ruby_glob0(path, AT_FDCWD, 0, GLOB_VERBOSE, &rb_glob_funcs,
2740 (
VALUE)&args, args.enc);
2741 if (status) GLOB_JUMP_TAG(status);
2745push_pattern(
const char *path,
VALUE ary,
void *enc)
2747#if defined _WIN32 || defined __APPLE__
2749 rb_encoding *eenc = rb_default_internal_encoding();
2754 rb_ary_push(ary, name);
2761 const int escape = !(flags & FNM_NOESCAPE);
2762 const char *p = str;
2763 const char *pend = p + strlen(p);
2765 const char *lbrace = 0, *rbrace = 0;
2766 int nest = 0, status = 0;
2769 if (*p ==
'{' && nest++ == 0) {
2772 if (*p ==
'}' && lbrace && --nest == 0) {
2776 if (*p ==
'\\' && escape) {
2782 if (lbrace && rbrace) {
2783 size_t len = strlen(s) + 1;
2784 char *buf = GLOB_ALLOC_N(
char, len);
2787 if (!buf)
return -1;
2788 memcpy(buf, s, lbrace-s);
2791 while (p < rbrace) {
2792 const char *t = ++p;
2794 while (p < rbrace && !(*p ==
',' && nest == 0)) {
2795 if (*p ==
'{') nest++;
2796 if (*p ==
'}') nest--;
2797 if (*p ==
'\\' && escape) {
2798 if (++p == rbrace)
break;
2802 memcpy(buf+shift, t, p-t);
2803 strlcpy(buf+shift+(p-t), rbrace+1, len-(shift+(p-t)));
2804 status = ruby_brace_expand(buf, flags, func, arg, enc, var);
2809 else if (!lbrace && !rbrace) {
2810 status = glob_call_func(func, s, arg, enc);
2824glob_brace(
const char *path,
VALUE val,
void *enc)
2828 return ruby_glob0(path, AT_FDCWD, 0, arg->flags, &arg->funcs, arg->value, enc);
2836 flags &= ~GLOB_VERBOSE;
2837 args.funcs.match = func;
2838 args.funcs.error = 0;
2841 return ruby_brace_expand(str, flags, glob_brace, (
VALUE)&args, enc,
Qfalse);
2847 return ruby_brace_glob_with_enc(str, flags, func, arg, rb_ascii8bit_encoding());
2857#if defined _WIN32 || defined __APPLE__
2858 str = rb_str_encode_ospath(str);
2860 if (rb_enc_to_index(enc) == ENCINDEX_US_ASCII)
2861 enc = rb_filesystem_encoding();
2862 if (rb_enc_to_index(enc) == ENCINDEX_US_ASCII)
2863 enc = rb_ascii8bit_encoding();
2864 flags |= GLOB_VERBOSE;
2865 args.func = push_pattern;
2871 if (!RB_TYPE_P(base,
T_STRING) || !rb_enc_check(str, base)) {
2873 if (!dirp->dir) dir_closed();
2875 if ((fd = dirfd(dirp->dir)) == -1)
2876 rb_sys_fail_path(dir_inspect(base));
2880 args.base = RSTRING_PTR(base);
2882#if defined _WIN32 || defined __APPLE__
2883 enc = rb_utf8_encoding();
2886 return ruby_glob0(RSTRING_PTR(str), fd, args.base, flags, &rb_glob_funcs,
2891rb_push_glob(
VALUE str,
VALUE base,
int flags)
2900 else if (!rb_str_to_cstr(str)) {
2904 rb_enc_check(str, rb_enc_from_encoding(rb_usascii_encoding()));
2908 status = push_glob(ary, str, base, flags);
2909 if (status) GLOB_JUMP_TAG(status);
2917 VALUE ary = rb_ary_new();
2924 status = push_glob(ary, str, base, flags);
2925 if (status) GLOB_JUMP_TAG(status);
2933dir_glob_option_base(
VALUE base)
2935 if (NIL_OR_UNDEF_P(base)) {
2944 if (!RSTRING_LEN(base))
return Qnil;
2949dir_glob_option_sort(
VALUE sort)
2951 return (rb_bool_expected(sort,
"sort", TRUE) ? 0 : FNM_GLOB_NOSORT);
2957 const int flags = dir_glob_option_sort(sort);
2958 base = dir_glob_option_base(base);
2960 return rb_push_glob(
RARRAY_AREF(args, 0), base, flags);
2962 return dir_globs(args, base, flags);
2968 VALUE ary = rb_check_array_type(str);
2969 const int flags = (
NUM2INT(rflags) | dir_glob_option_sort(sort)) & ~FNM_CASEFOLD;
2970 base = dir_glob_option_base(base);
2972 ary = rb_push_glob(str, base, flags);
2975 ary = dir_globs(ary, base, flags);
2986dir_open_dir(
int argc,
VALUE *argv)
3023 dir = dir_open_dir(argc, argv);
3024 rb_ensure(dir_each, dir, dir_close, dir);
3029dir_collect(
VALUE dir)
3031 VALUE ary = rb_ary_new();
3032 dir_each_entry(dir, rb_ary_push, ary, FALSE);
3056 dir = dir_open_dir(argc, argv);
3057 return rb_ensure(dir_collect, dir, dir_close, dir);
3061dir_each_child(
VALUE dir)
3063 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3088dir_s_each_child(
int argc,
VALUE *argv,
VALUE io)
3093 dir = dir_open_dir(argc, argv);
3094 rb_ensure(dir_each_child, dir, dir_close, dir);
3119dir_each_child_m(
VALUE dir)
3122 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3137dir_collect_children(
VALUE dir)
3139 VALUE ary = rb_ary_new();
3140 dir_each_entry(dir, rb_ary_push, ary, TRUE);
3160dir_s_children(
int argc,
VALUE *argv,
VALUE io)
3164 dir = dir_open_dir(argc, argv);
3165 return rb_ensure(dir_collect_children, dir, dir_close, dir);
3169fnmatch_brace(
const char *pattern,
VALUE val,
void *enc)
3172 VALUE path = arg->value;
3176 if (enc_pattern != enc_path) {
3177 if (!rb_enc_asciicompat(enc_pattern))
3179 if (!rb_enc_asciicompat(enc_path))
3183 long len = strlen(pattern);
3185 enc_pattern, &cr) != len)
3191 return (fnmatch(pattern, enc, RSTRING_PTR(path), arg->flags) == 0);
3196file_s_fnmatch(
int argc,
VALUE *argv,
VALUE obj)
3198 VALUE pattern, path;
3202 if (rb_scan_args(argc, argv,
"21", &pattern, &path, &rflags) == 3)
3210 if (flags & FNM_EXTGLOB) {
3215 if (ruby_brace_expand(RSTRING_PTR(pattern), flags, fnmatch_brace,
3216 (
VALUE)&args, rb_enc_get(pattern), pattern) > 0)
3220 rb_encoding *enc = rb_enc_compatible(pattern, path);
3222 if (fnmatch(RSTRING_PTR(pattern), enc, RSTRING_PTR(path), flags) == 0)
3244 rb_check_arity(argc, 0, 1);
3245 user = (argc > 0) ? argv[0] :
Qnil;
3251 return rb_home_dir_of(user,
rb_str_new(0, 0));
3254 return rb_default_home_dir(
rb_str_new(0, 0));
3268rb_file_directory_p(
void)
3274nogvl_dir_empty_p(
void *ptr)
3276 const char *path = ptr;
3277 DIR *dir = opendir(path);
3283 switch (gc_for_fd_with_gvl(e)) {
3285 dir = opendir(path);
3290 if (e == ENOTDIR)
return (
void *)
Qfalse;
3295 while ((dp = READDIR(dir, NULL)) != NULL) {
3296 if (!to_be_skipped(dp)) {
3302 return (
void *)result;
3317 enum {false_on_notdir = 1};
3321 dirname = rb_str_encode_ospath(dirname);
3323 path = RSTRING_PTR(dirname);
3325#if defined HAVE_GETATTRLIST && defined ATTR_DIR_ENTRYCOUNT
3327 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
3328 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
3329 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), 0) != 0)
3330 rb_sys_fail_path(orig);
3331 if (*(
const fsobj_tag_t *)(attrbuf+1) == VT_HFS) {
3333 al.dirattr = ATTR_DIR_ENTRYCOUNT;
3334 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), 0) == 0) {
3335 if (attrbuf[0] >= 2 *
sizeof(u_int32_t))
3336 return RBOOL(attrbuf[1] == 0);
3337 if (false_on_notdir)
return Qfalse;
3339 rb_sys_fail_path(orig);
3346 if (UNDEF_P(result)) {
3347 rb_sys_fail_path(orig);
3400 rb_file_const(
"FNM_NOESCAPE",
INT2FIX(FNM_NOESCAPE));
3407 rb_file_const(
"FNM_PATHNAME",
INT2FIX(FNM_PATHNAME));
3414 rb_file_const(
"FNM_DOTMATCH",
INT2FIX(FNM_DOTMATCH));
3421 rb_file_const(
"FNM_CASEFOLD",
INT2FIX(FNM_CASEFOLD));
3427 rb_file_const(
"FNM_EXTGLOB",
INT2FIX(FNM_EXTGLOB));
3434 rb_file_const(
"FNM_SYSCASE",
INT2FIX(FNM_SYSCASE));
3441 rb_file_const(
"FNM_SHORTNAME",
INT2FIX(FNM_SHORTNAME));
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
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_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 rb_str_cat2
Old name of rb_str_cat_cstr.
#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_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_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_warn(const char *fmt,...)
Identical to rb_warning(), except it reports always regardless of runtime -W flag.
VALUE rb_eArgError
ArgumentError exception.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_cFile
File class.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
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.
Defines RBIMPL_HAS_BUILTIN.
#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.
#define rb_utf8_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "UTF-8" encoding.
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_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
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.
#define rb_str_dup_frozen
Just another name of rb_str_new_frozen.
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C 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.
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.
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
char * ruby_getcwd(void)
This is our own version of getcwd(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
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.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#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)
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
#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...
#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.
uintptr_t VALUE
Type that represents a Ruby object.