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)) {
284 if (nocase) c1 = rb_enc_toupper(c1, enc);
286 if (nocase) c2 = rb_enc_toupper(c2, enc);
287 if (c1 < c2)
continue;
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;
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;
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);
473 if (dir->dir) closedir(dir->dir);
476RUBY_REFERENCES(dir_refs) = {
477 RUBY_REF_EDGE(
struct dir_data, path),
484 RUBY_REFS_LIST_PTR(dir_refs),
488 0, NULL, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE
494dir_s_alloc(
VALUE klass)
507nogvl_opendir(
void *ptr)
509 const char *path = ptr;
511 return (
void *)opendir(path);
515opendir_without_gvl(
const char *path)
517 if (vm_initialized) {
518 union {
const void *in;
void *out; } u;
525 return opendir(path);
529dir_initialize(rb_execution_context_t *ec,
VALUE dir,
VALUE dirname,
VALUE enc)
534 rb_encoding *fsenc =
NIL_P(enc) ? rb_filesystem_encoding() : rb_to_encoding(enc);
538 dirname = rb_str_encode_ospath(dirname);
542 if (dp->dir) closedir(dp->dir);
546 path = RSTRING_PTR(dirname);
547 dp->dir = opendir_without_gvl(path);
548 if (dp->dir == NULL) {
550 if (rb_gc_for_fd(e)) {
551 dp->dir = opendir_without_gvl(path);
553#ifdef HAVE_GETATTRLIST
555 u_int32_t attrbuf[1];
556 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0};
557 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW) == 0) {
558 dp->dir = opendir_without_gvl(path);
562 if (dp->dir == NULL) {
564 rb_syserr_fail_path(e, orig);
573dir_s_open(rb_execution_context_t *ec,
VALUE klass,
VALUE dirname,
VALUE enc)
578 dir_initialize(ec, dir, dirname, enc);
584dir_s_close(rb_execution_context_t *ec,
VALUE klass,
VALUE dir)
586 return dir_close(dir);
589# if defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD)
616 if (!(dp->dir = fdopendir(
NUM2INT(fd)))) {
617 rb_sys_fail(
"fdopendir");
625#define dir_s_for_fd rb_f_notimplement
628NORETURN(
static void dir_closed(
void));
646 struct dir_data *dirp = dir_get(dir);
647 if (!dirp->dir) dir_closed();
651#define GetDIR(obj, dirp) ((dirp) = dir_check(obj))
664dir_inspect(
VALUE dir)
669 if (!
NIL_P(dirp->path)) {
677 return rb_funcallv(dir, idTo_s, 0, 0);
683#if defined(__sun) && !defined(HAVE_DIRFD)
684# if defined(HAVE_DIR_D_FD)
685# define dirfd(x) ((x)->d_fd)
687# elif defined(HAVE_DIR_DD_FD)
688# define dirfd(x) ((x)->dd_fd)
715 fd = dirfd(dirp->dir);
717 rb_sys_fail(
"dirfd");
721#define dir_fileno rb_f_notimplement
741 return rb_str_dup(dirp->path);
746fundamental_encoding_p(rb_encoding *enc)
748 switch (rb_enc_to_index(enc)) {
749 case ENCINDEX_ASCII_8BIT:
750 case ENCINDEX_US_ASCII:
757# define READDIR(dir, enc) rb_w32_readdir((dir), (enc))
759# define READDIR(dir, enc) readdir((dir))
764to_be_skipped(
const struct dirent *dp)
766 const char *name = dp->d_name;
767 if (name[0] !=
'.')
return FALSE;
768#ifdef HAVE_DIRENT_NAMLEN
769 switch (NAMLEN(dp)) {
771 if (name[1] !=
'.')
return FALSE;
778 if (!name[1])
return TRUE;
779 if (name[1] !=
'.')
return FALSE;
780 if (!name[2])
return TRUE;
807 if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
848 return dir_each_entry(dir, dir_yield,
Qnil, FALSE);
856 IF_NORMALIZE_UTF8PATH(
int norm_p);
859 rewinddir(dirp->dir);
860 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path)));
861 while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
862 const char *name = dp->d_name;
863 size_t namlen = NAMLEN(dp);
866 if (children_only && name[0] ==
'.') {
867 if (namlen == 1)
continue;
868 if (namlen == 2 && name[1] ==
'.')
continue;
870#if NORMALIZE_UTF8PATH
871 if (norm_p && has_nonascii(name, namlen) &&
872 !
NIL_P(path = rb_str_normalize_ospath(name, namlen))) {
873 path = rb_external_str_with_enc(path, dirp->enc);
904 pos = telldir(dirp->dir);
905 return rb_int2inum(pos);
908#define dir_tell rb_f_notimplement
939 seekdir(dirp->dir, p);
943#define dir_seek rb_f_notimplement
974#define dir_set_pos rb_f_notimplement
998 rewinddir(dirp->dir);
1021 dirp = dir_get(dir);
1022 if (!dirp->dir)
return Qnil;
1023 closedir(dirp->dir);
1030nogvl_chdir(
void *ptr)
1032 const char *path = ptr;
1034 return (
void *)(
VALUE)chdir(path);
1038dir_chdir0(
VALUE path)
1040 if (chdir(RSTRING_PTR(path)) < 0)
1041 rb_sys_fail_path(path);
1044static int chdir_blocking = 0;
1048 VALUE old_path, new_path;
1057 dir_chdir0(args->new_path);
1060 if (
NIL_P(chdir_thread))
1066chdir_restore(
VALUE v)
1071 if (chdir_blocking == 0)
1072 chdir_thread =
Qnil;
1073 dir_chdir0(args->old_path);
1079chdir_path(
VALUE path,
bool yield_path)
1081 if (chdir_blocking > 0) {
1083 rb_raise(
rb_eRuntimeError,
"conflicting chdir during another chdir block");
1085 rb_warn(
"conflicting chdir during another chdir block");
1091 args.old_path = rb_str_encode_ospath(rb_dir_getwd());
1092 args.new_path = path;
1094 args.yield_path = yield_path;
1098 char *p = RSTRING_PTR(path);
1102 rb_sys_fail_path(path);
1177 path = rb_str_encode_ospath(rb_get_path(argv[0]));
1180 const char *dist = getenv(
"HOME");
1182 dist = getenv(
"LOGDIR");
1183 if (!dist) rb_raise(rb_eArgError,
"HOME/LOGDIR not set");
1188 return chdir_path(path,
true);
1191#if defined(HAVE_FCHDIR) && defined(HAVE_DIRFD) && HAVE_FCHDIR && HAVE_DIRFD
1193nogvl_fchdir(
void *ptr)
1195 const int *fd = ptr;
1197 return (
void *)(
VALUE)fchdir(*fd);
1204 rb_sys_fail(
"fchdir");
1214fchdir_yield(
VALUE v)
1216 struct fchdir_data *args = (
void *)v;
1217 dir_fchdir(args->fd);
1220 if (
NIL_P(chdir_thread))
1226fchdir_restore(
VALUE v)
1228 struct fchdir_data *args = (
void *)v;
1231 if (chdir_blocking == 0)
1232 chdir_thread =
Qnil;
1233 dir_fchdir(
RB_NUM2INT(dir_fileno(args->old_dir)));
1235 dir_close(args->old_dir);
1296 if (chdir_blocking > 0) {
1298 rb_raise(
rb_eRuntimeError,
"conflicting chdir during another chdir block");
1300 rb_warn(
"conflicting chdir during another chdir block");
1304 struct fchdir_data args;
1305 args.old_dir = dir_s_alloc(klass);
1306 dir_initialize(NULL, args.old_dir, rb_fstring_cstr(
"."),
Qnil);
1315 rb_sys_fail(
"fchdir");
1321#define dir_s_fchdir rb_f_notimplement
1350#if defined(HAVE_FCHDIR) && defined(HAVE_DIRFD) && HAVE_FCHDIR && HAVE_DIRFD
1351 return dir_s_fchdir(
rb_cDir, dir_fileno(dir));
1353 return chdir_path(dir_get(dir)->path,
false);
1359rb_dir_getwd_ospath(
void)
1365#undef RUBY_UNTYPED_DATA_WARNING
1366#define RUBY_UNTYPED_DATA_WARNING 0
1371 cwd = rb_str_normalize_ospath(path, strlen(path));
1385 rb_encoding *fs = rb_filesystem_encoding();
1386 int fsenc = rb_enc_to_index(fs);
1387 VALUE cwd = rb_dir_getwd_ospath();
1390 case ENCINDEX_US_ASCII:
1391 fsenc = ENCINDEX_ASCII_8BIT;
1392 case ENCINDEX_ASCII_8BIT:
1394#if defined _WIN32 || defined __APPLE__
1399 return rb_enc_associate_index(cwd, fsenc);
1413dir_s_getwd(
VALUE dir)
1415 return rb_dir_getwd();
1419check_dirname(
VALUE dir)
1427 enc = rb_enc_get(d);
1430 pend = rb_enc_path_end(rb_enc_path_skip_prefix(path, pend, enc), pend, enc);
1431 if (pend - path <
len) {
1432 d = rb_str_subseq(d, 0, pend - path);
1435 return rb_str_encode_ospath(d);
1438#if defined(HAVE_CHROOT)
1454 path = check_dirname(path);
1455 if (chroot(RSTRING_PTR(path)) == -1)
1456 rb_sys_fail_path(path);
1461#define dir_s_chroot rb_f_notimplement
1470nogvl_mkdir(
void *ptr)
1474 return (
void *)(
VALUE)mkdir(m->path, m->mode);
1500 if (
rb_scan_args(argc, argv,
"11", &path, &vmode) == 2) {
1507 path = check_dirname(path);
1508 m.path = RSTRING_PTR(path);
1511 rb_sys_fail_path(path);
1517nogvl_rmdir(
void *ptr)
1519 const char *path = ptr;
1521 return (
void *)(
VALUE)rmdir(path);
1540 dir = check_dirname(dir);
1541 p = RSTRING_PTR(dir);
1544 rb_sys_fail_path(dir);
1550#ifdef RUBY_FUNCTION_NAME_STRING
1557#ifndef RUBY_FUNCTION_NAME_STRING
1558#define sys_enc_warning_in(func, mesg, enc) sys_enc_warning(mesg, enc)
1562sys_warning_1(
VALUE mesg)
1565#ifdef RUBY_FUNCTION_NAME_STRING
1566 rb_sys_enc_warning(arg->enc,
"%s: %s", arg->func, arg->mesg);
1568 rb_sys_enc_warning(arg->enc,
"%s", arg->mesg);
1574sys_enc_warning_in(
const char *func,
const char *mesg, rb_encoding *enc)
1577#ifdef RUBY_FUNCTION_NAME_STRING
1582 rb_protect(sys_warning_1, (
VALUE)&arg, 0);
1585#define GLOB_VERBOSE (1U << (sizeof(int) * CHAR_BIT - 1))
1586#define sys_warning(val, enc) \
1587 ((flags & GLOB_VERBOSE) ? sys_enc_warning_in(RUBY_FUNCTION_NAME_STRING, (val), (enc)) :(void)0)
1590glob_alloc_size(
size_t x,
size_t y)
1602glob_alloc_n(
size_t x,
size_t y)
1604 return malloc(glob_alloc_size(x, y));
1608glob_realloc_n(
void *p,
size_t x,
size_t y)
1610 return realloc(p, glob_alloc_size(x, y));
1613#define GLOB_ALLOC(type) ((type *)malloc(sizeof(type)))
1614#define GLOB_ALLOC_N(type, n) ((type *)glob_alloc_n(sizeof(type), n))
1615#define GLOB_REALLOC(ptr, size) realloc((ptr), (size))
1616#define GLOB_REALLOC_N(ptr, n) glob_realloc_n(ptr, sizeof(*(ptr)), n)
1617#define GLOB_FREE(ptr) free(ptr)
1618#define GLOB_JUMP_TAG(status) (((status) == -1) ? rb_memerror() : rb_jump_tag(status))
1624ALWAYS_INLINE(
static int to_be_ignored(
int e));
1628 return e == ENOENT || e == ENOTDIR;
1632#define STAT(p, s) rb_w32_ustati128((p), (s))
1634#define lstat(p, s) rb_w32_ulstati128((p), (s))
1636#define STAT(p, s) stat((p), (s))
1639typedef int ruby_glob_errfunc(
const char*,
VALUE,
const void*,
int);
1642 ruby_glob_errfunc *error;
1646at_subpath(
int fd,
size_t baselen,
const char *path)
1649 if (fd != (
int)AT_FDCWD && baselen > 0) {
1651 if (*path ==
'/') ++path;
1654 return *path ? path :
".";
1659do_stat(
int fd,
size_t baselen,
const char *path,
struct stat *pst,
int flags, rb_encoding *enc)
1662 int ret = fstatat(fd, at_subpath(fd, baselen, path), pst, 0);
1664 int ret = STAT(path, pst);
1666 if (ret < 0 && !to_be_ignored(
errno))
1667 sys_warning(path, enc);
1672#if defined HAVE_LSTAT || defined lstat || USE_OPENDIR_AT
1674do_lstat(
int fd,
size_t baselen,
const char *path,
struct stat *pst,
int flags, rb_encoding *enc)
1677 int ret = fstatat(fd, at_subpath(fd, baselen, path), pst, AT_SYMLINK_NOFOLLOW);
1679 int ret = lstat(path, pst);
1681 if (ret < 0 && !to_be_ignored(
errno))
1682 sys_warning(path, enc);
1687#define do_lstat do_stat
1696with_gvl_gc_for_fd(
void *ptr)
1700 return (
void *)RBOOL(rb_gc_for_fd(*e));
1704gc_for_fd_with_gvl(
int e)
1709 return RBOOL(rb_gc_for_fd(e));
1713nogvl_opendir_at(
void *ptr)
1719 const int opendir_flags = (O_RDONLY|O_CLOEXEC|
1724 int fd = openat(oaa->basefd, oaa->path, opendir_flags);
1726 dirp = fd >= 0 ? fdopendir(fd) : 0;
1730 switch (gc_for_fd_with_gvl(e)) {
1732 if (fd < 0) fd = openat(oaa->basefd, oaa->path, opendir_flags);
1733 if (fd >= 0) dirp = fdopendir(fd);
1734 if (dirp)
return dirp;
1739 if (fd >= 0) close(fd);
1744 dirp = opendir(oaa->path);
1745 if (!dirp && gc_for_fd_with_gvl(
errno))
1746 dirp = opendir(oaa->path);
1753opendir_at(
int basefd,
const char *path)
1757 oaa.basefd = basefd;
1763 return nogvl_opendir_at(&oaa);
1767do_opendir(
const int basefd,
size_t baselen,
const char *path,
int flags, rb_encoding *enc,
1768 ruby_glob_errfunc *errfunc,
VALUE arg,
int *status)
1773 if (!fundamental_encoding_p(enc)) {
1774 tmp = rb_enc_str_new(path, strlen(path), enc);
1775 tmp = rb_str_encode_ospath(tmp);
1776 path = RSTRING_PTR(tmp);
1779 dirp = opendir_at(basefd, at_subpath(basefd, baselen, path));
1784 if (!to_be_ignored(e)) {
1786 *status = (*errfunc)(path, arg, enc, e);
1789 sys_warning(path, enc);
1794 if (tmp) rb_str_resize(tmp, 0);
1801enum glob_pattern_type { PLAIN, ALPHA, BRACE, MAGICAL, RECURSIVE, MATCH_ALL, MATCH_DIR };
1804static enum glob_pattern_type
1805has_magic(
const char *p,
const char *pend,
int flags, rb_encoding *enc)
1807 const int escape = !(flags & FNM_NOESCAPE);
1813 while (p < pend && (c = *p++) != 0) {
1825 if (escape && p++ >= pend)
1844 p = Next(p-1, pend, enc);
1847 return hasmagical ? MAGICAL : hasalpha ? ALPHA : PLAIN;
1852find_dirsep(
const char *p,
const char *pend,
int flags, rb_encoding *enc)
1854 const int escape = !(flags & FNM_NOESCAPE);
1859 while ((c = *p++) != 0) {
1881 if (escape && !(c = *p++))
1886 p = Next(p-1, pend, enc);
1894remove_backslashes(
char *p,
register const char *pend, rb_encoding *enc)
1902 memmove(t, s, p - s);
1913 memmove(t, s, p - s);
1920 enum glob_pattern_type
type;
1924static void glob_free_pattern(
struct glob_pattern *list);
1927glob_make_pattern(
const char *p,
const char *e,
int flags, rb_encoding *enc)
1933 while (p < e && *p) {
1935 if (!tmp)
goto error;
1936 if (p + 2 < e && p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/') {
1938 do { p += 3;
while (*p ==
'/') p++; }
while (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/');
1939 tmp->type = RECURSIVE;
1945 const char *m = find_dirsep(p, e, flags, enc);
1946 const enum glob_pattern_type magic = has_magic(p, m, flags, enc);
1947 const enum glob_pattern_type non_magic = (USE_NAME_ON_FS || FNM_SYSCASE) ? PLAIN : ALPHA;
1950 if (!(FNM_SYSCASE || magic > non_magic) && !recursive && *m) {
1952 while (has_magic(m+1, m2 = find_dirsep(m+1, e, flags, enc), flags, enc) <= non_magic &&
1957 buf = GLOB_ALLOC_N(
char, m-p+1);
1962 memcpy(buf, p, m-p);
1964 tmp->type = magic > MAGICAL ? MAGICAL : magic > non_magic ? magic : PLAIN;
1983 tmp->type = dirsep ? MATCH_DIR : MATCH_ALL;
1992 glob_free_pattern(list);
2003 GLOB_FREE(tmp->str);
2009join_path(
const char *path,
size_t len,
int dirsep,
const char *name,
size_t namlen)
2011 char *buf = GLOB_ALLOC_N(
char,
len+namlen+(dirsep?1:0)+1);
2014 memcpy(buf, path,
len);
2018 memcpy(buf+
len, name, namlen);
2019 buf[
len+namlen] =
'\0';
2023#ifdef HAVE_GETATTRLIST
2024# if defined HAVE_FGETATTRLIST
2025# define is_case_sensitive(dirp, path) is_case_sensitive(dirp)
2027# define is_case_sensitive(dirp, path) is_case_sensitive(path)
2030is_case_sensitive(
DIR *dirp,
const char *path)
2034 vol_capabilities_attr_t cap[1];
2036 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, 0, ATTR_VOL_INFO|ATTR_VOL_CAPABILITIES};
2037 const vol_capabilities_attr_t *
const cap = attrbuf[0].cap;
2038 const int idx = VOL_CAPABILITIES_FORMAT;
2039 const uint32_t mask = VOL_CAP_FMT_CASE_SENSITIVE;
2041# if defined HAVE_FGETATTRLIST
2042 if (fgetattrlist(dirfd(dirp), &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW))
2045 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW))
2048 if (!(cap->valid[idx] & mask))
2050 return (cap->capabilities[idx] & mask) != 0;
2054replace_real_basename(
char *path,
long base, rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
2058 attrreference_t ref[1];
2059 fsobj_type_t objtype;
2060 char path[MAXPATHLEN * 3];
2062 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_NAME|ATTR_CMN_OBJTYPE};
2063 const attrreference_t *
const ar = attrbuf[0].ref;
2067 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
2070 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), FSOPT_NOFOLLOW)) {
2071 if (!to_be_ignored(
errno))
2072 sys_warning(path, enc);
2076 switch (attrbuf[0].objtype) {
2077 case VREG: *
type = path_regular;
break;
2078 case VDIR: *
type = path_directory;
break;
2079 case VLNK: *
type = path_symlink;
break;
2080 default: *
type = path_exist;
break;
2082 name = (
char *)ar + ar->attr_dataoffset;
2083 len = (long)ar->attr_length - 1;
2084 if (name +
len > (
char *)attrbuf +
sizeof(attrbuf))
2087# if NORMALIZE_UTF8PATH
2088 if (norm_p && has_nonascii(name,
len)) {
2089 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name,
len))) {
2095 tmp = GLOB_REALLOC(path, base +
len + 1);
2098 memcpy(path + base, name,
len);
2099 path[base +
len] =
'\0';
2101 IF_NORMALIZE_UTF8PATH(
if (!
NIL_P(utf8str)) rb_str_resize(utf8str, 0));
2105VALUE rb_w32_conv_from_wchar(
const WCHAR *wstr, rb_encoding *enc);
2106int rb_w32_reparse_symlink_p(
const WCHAR *path);
2109replace_real_basename(
char *path,
long base, rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
2111 char *plainname = path;
2112 volatile VALUE tmp = 0;
2113 WIN32_FIND_DATAW fd;
2114 WIN32_FILE_ATTRIBUTE_DATA fa;
2116 HANDLE h = INVALID_HANDLE_VALUE;
2119 if (!fundamental_encoding_p(enc)) {
2121 tmp = rb_str_encode_ospath(tmp);
2122 plainname = RSTRING_PTR(tmp);
2124 wplain = rb_w32_mbstr_to_wstr(CP_UTF8, plainname, -1, &wlen);
2125 if (tmp) rb_str_resize(tmp, 0);
2126 if (!wplain)
return path;
2127 if (GetFileAttributesExW(wplain, GetFileExInfoStandard, &fa)) {
2128 h = FindFirstFileW(wplain, &fd);
2129 e = rb_w32_map_errno(GetLastError());
2131 if (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2132 if (!rb_w32_reparse_symlink_p(wplain))
2133 fa.dwFileAttributes &= ~FILE_ATTRIBUTE_REPARSE_POINT;
2136 if (h == INVALID_HANDLE_VALUE) {
2138 if (e && !to_be_ignored(e)) {
2140 sys_warning(path, enc);
2146 (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ? path_symlink :
2147 (fa.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? path_directory :
2151 tmp = rb_w32_conv_from_wchar(fd.cFileName, enc);
2152 wlen = RSTRING_LEN(tmp);
2153 buf = GLOB_REALLOC(path, base + wlen + 1);
2156 memcpy(path + base, RSTRING_PTR(tmp), wlen);
2157 path[base + wlen] = 0;
2159 rb_str_resize(tmp, 0);
2163 wlen = WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, NULL, 0, NULL, NULL);
2164 utf8filename = GLOB_REALLOC(0, wlen);
2167 WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, utf8filename, wlen, NULL, NULL);
2168 buf = GLOB_REALLOC(path, base + wlen + 1);
2171 memcpy(path + base, utf8filename, wlen);
2172 path[base + wlen] = 0;
2174 GLOB_FREE(utf8filename);
2179#elif USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2180# error not implemented
2184# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
2189# define S_ISLNK(m) (0)
2191# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
2196 void (*func)(
const char *,
VALUE,
void *);
2204#define glob_call_func(func, path, arg, enc) (*(func))((path), (arg), (void *)(enc))
2207glob_func_caller(
VALUE val)
2211 glob_call_func(args->func, args->path, args->value, args->enc);
2222glob_func_warning(
VALUE val)
2225 rb_syserr_enc_warning(arg->error, arg->enc,
"%s", arg->path);
2231rb_glob_warning(
const char *path,
VALUE a,
const void *enc,
int error)
2239 rb_protect(glob_func_warning, (
VALUE)&args, &status);
2244NORETURN(
static VALUE glob_func_error(
VALUE val));
2247glob_func_error(
VALUE val)
2256rb_glob_error(
const char *path,
VALUE a,
const void *enc,
int error)
2267 errfunc = glob_func_warning;
2272 rb_protect(errfunc, (
VALUE)&args, &status);
2280 const char *d_altname;
2286dirent_match(
const char *pat, rb_encoding *enc,
const char *name,
const rb_dirent_t *dp,
int flags)
2288 if (fnmatch(pat, enc, name, flags) == 0)
return 1;
2290 if (dp->d_altname && (flags & FNM_SHORTNAME)) {
2291 if (fnmatch(pat, enc, dp->d_altname, flags) == 0)
return 1;
2303 rb_pathtype_t pathtype;
2311 const rb_dirent_t *dp;
2316dirent_match_brace(
const char *pattern,
VALUE val,
void *enc)
2320 return dirent_match(pattern, enc, arg->name, arg->dp, arg->flags);
2329 size_t path_len = 0;
2331 for (p = *beg; p; p = p->next) {
2346 path_len = strlen(str);
2347 path = GLOB_ALLOC_N(
char, path_len + 1);
2349 memcpy(path, str, path_len);
2350 path[path_len] =
'\0';
2354 size_t len = strlen(str);
2356 tmp = GLOB_REALLOC(path, path_len +
len + 2);
2359 path[path_len++] =
'/';
2360 memcpy(path + path_len, str,
len);
2362 path[path_len] =
'\0';
2369static int push_caller(
const char *path,
VALUE val,
void *enc);
2372 rb_encoding *enc,
VALUE var);
2374static const size_t rb_dirent_name_offset =
2375 offsetof(rb_dirent_t, d_type) +
sizeof(uint8_t);
2378dirent_copy(
const struct dirent *dp, rb_dirent_t *rdp)
2380 if (!dp)
return NULL;
2381 size_t namlen = NAMLEN(dp);
2382 const size_t altlen =
2384 dp->d_altlen ? dp->d_altlen + 1 :
2387 rb_dirent_t *newrdp = rdp;
2388 if (!rdp && !(newrdp = malloc(rb_dirent_name_offset + namlen + 1 + altlen)))
2390 newrdp->d_namlen = namlen;
2392 char *name = (
char *)newrdp + rb_dirent_name_offset;
2393 memcpy(name, dp->d_name, namlen);
2394 name[namlen] =
'\0';
2396 newrdp->d_altname = NULL;
2398 char *
const altname = name + namlen + 1;
2399 memcpy(altname, dp->d_altname, altlen - 1);
2400 altname[altlen - 1] =
'\0';
2401 newrdp->d_altname = altname;
2404 newrdp->d_name = name;
2407 newrdp->d_name = dp->d_name;
2409 newrdp->d_altname = dp->d_altname;
2413 newrdp->d_type = dp->d_type;
2427 rb_dirent_t **entries;
2432glob_sort_cmp(
const void *a,
const void *b,
void *e)
2434 const rb_dirent_t *ent1 = *(
void **)a;
2435 const rb_dirent_t *ent2 = *(
void **)b;
2436 return strcmp(ent1->d_name, ent2->d_name);
2442 if (flags & FNM_GLOB_NOSORT) {
2443 closedir(ent->nosort.dirp);
2444 ent->nosort.dirp = NULL;
2446 else if (ent->sort.entries) {
2447 for (
size_t i = 0, count = ent->sort.count; i < count;) {
2448 GLOB_FREE(ent->sort.entries[i++]);
2450 GLOB_FREE(ent->sort.entries);
2451 ent->sort.entries = NULL;
2452 ent->sort.count = ent->sort.idx = 0;
2460 if (flags & FNM_GLOB_NOSORT) {
2461 ent->nosort.dirp = dirp;
2467 size_t count = 0, capacity = 0;
2468 ent->sort.count = 0;
2470 ent->sort.entries = 0;
2472 if ((capacity = dirp->nfiles) > 0) {
2473 if (!(newp = GLOB_ALLOC_N(rb_dirent_t, capacity))) {
2477 ent->sort.entries = newp;
2480 while ((dp = READDIR(dirp, enc)) != NULL) {
2481 rb_dirent_t *rdp = dirent_copy(dp, NULL);
2485 if (count >= capacity) {
2487 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, capacity)))
2489 ent->sort.entries = newp;
2491 ent->sort.entries[count++] = rdp;
2492 ent->sort.count = count;
2495 if (count < capacity) {
2496 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, count))) {
2497 glob_dir_finish(ent, 0);
2500 ent->sort.entries = newp;
2502 ruby_qsort(ent->sort.entries, ent->sort.count,
sizeof(ent->sort.entries[0]),
2503 glob_sort_cmp, NULL);
2508 glob_dir_finish(ent, 0);
2516 if (flags & FNM_GLOB_NOSORT) {
2517 return dirent_copy(READDIR(ent->nosort.dirp, enc), &ent->nosort.ent);
2519 else if (ent->sort.idx < ent->sort.count) {
2520 return ent->sort.entries[ent->sort.idx++];
2534 rb_pathtype_t pathtype,
2545 int plain = 0, brace = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0;
2546 int escape = !(flags & FNM_NOESCAPE);
2547 size_t pathlen = baselen + namelen;
2549 rb_check_stack_overflow();
2551 for (cur = beg; cur < end; ++cur) {
2553 if (p->type == RECURSIVE) {
2562#if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2569 if (!recursive || strchr(p->str,
'/')) {
2583 rb_bug(
"continuous RECURSIVEs");
2589 char* brace_path = join_path_from_pattern(beg);
2590 if (!brace_path)
return -1;
2593 args.baselen = baselen;
2594 args.namelen = namelen;
2595 args.dirsep = dirsep;
2596 args.pathtype = pathtype;
2600 status = ruby_brace_expand(brace_path, flags, push_caller, (
VALUE)&args, enc,
Qfalse);
2601 GLOB_FREE(brace_path);
2606 if (match_all && pathtype == path_unknown) {
2607 if (do_lstat(fd, baselen, path, &st, flags, enc) == 0) {
2608 pathtype = IFTODT(st.st_mode);
2611 pathtype = path_noent;
2614 if (match_dir && (pathtype == path_unknown || pathtype == path_symlink)) {
2615 if (do_stat(fd, baselen, path, &st, flags, enc) == 0) {
2616 pathtype = IFTODT(st.st_mode);
2619 pathtype = path_noent;
2622 if (match_all && pathtype > path_noent) {
2623 const char *subpath = path + baselen + (baselen && path[baselen] ==
'/');
2624 status = glob_call_func(funcs->match, subpath, arg, enc);
2625 if (status)
return status;
2627 if (match_dir && pathtype == path_directory) {
2628 int seplen = (baselen && path[baselen] ==
'/');
2629 const char *subpath = path + baselen + seplen;
2630 char *tmp = join_path(subpath, namelen - seplen, dirsep,
"", 0);
2631 if (!tmp)
return -1;
2632 status = glob_call_func(funcs->match, tmp, arg, enc);
2634 if (status)
return status;
2638 if (pathtype == path_noent)
return 0;
2640 if (magical || recursive) {
2643# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2644 char *plainname = 0;
2646 IF_NORMALIZE_UTF8PATH(
int norm_p);
2647# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2648 if (cur + 1 == end && (*cur)->type <= ALPHA) {
2649 plainname = join_path(path, pathlen, dirsep, (*cur)->str, strlen((*cur)->str));
2650 if (!plainname)
return -1;
2651 dirp = do_opendir(fd, basename, plainname, flags, enc, funcs->error, arg, &status);
2652 GLOB_FREE(plainname);
2658 dirp = do_opendir(fd, baselen, path, flags, enc, funcs->error, arg, &status);
2660# if FNM_SYSCASE || NORMALIZE_UTF8PATH
2661 if ((magical < 2) && !recursive && (
errno == EACCES)) {
2668 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *path ? path :
"."));
2670# if NORMALIZE_UTF8PATH
2671 if (!(norm_p || magical || recursive)) {
2676# ifdef HAVE_GETATTRLIST
2677 if (is_case_sensitive(dirp, path) == 0)
2678 flags |= FNM_CASEFOLD;
2681 if (!glob_opendir(&globent, dirp, flags, enc)) {
2684 status = (*funcs->error)(path, arg, enc, ENOMEM);
2687 sys_warning(path, enc);
2692 int skipdot = (flags & FNM_GLOB_SKIPDOT);
2693 flags |= FNM_GLOB_SKIPDOT;
2695 while ((dp = glob_getent(&globent, flags, enc)) != NULL) {
2697 rb_pathtype_t new_pathtype = path_unknown;
2701 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
2704 namlen = dp->d_namlen;
2705 if (name[0] ==
'.') {
2709 if (recursive && !(flags & FNM_DOTMATCH))
continue;
2710 if (skipdot)
continue;
2712 new_pathtype = path_directory;
2714 else if (namlen == 2 && name[1] ==
'.') {
2720# if NORMALIZE_UTF8PATH
2721 if (norm_p && has_nonascii(name, namlen)) {
2722 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name, namlen))) {
2727 buf = join_path(path, pathlen, dirsep, name, namlen);
2728 IF_NORMALIZE_UTF8PATH(
if (!
NIL_P(utf8str)) rb_str_resize(utf8str, 0));
2733 name = buf + pathlen + (dirsep != 0);
2735 if (dp->d_type != DT_UNKNOWN) {
2737 new_pathtype = dp->d_type;
2740 if (recursive && dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1) &&
2741 new_pathtype == path_unknown) {
2743 if (do_lstat(fd, baselen, buf, &st, flags, enc) == 0)
2744 new_pathtype = IFTODT(st.st_mode);
2746 new_pathtype = path_noent;
2749 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, (end - beg) * 2);
2756 for (cur = beg; cur < end; ++cur) {
2759 if (p->type == RECURSIVE) {
2760 if (new_pathtype == path_directory ||
2761 new_pathtype == path_exist) {
2762 if (dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1))
2772 if (ruby_brace_expand(p->str, flags, dirent_match_brace,
2774 *new_end++ = p->next;
2777# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2779 *new_end++ = p->next;
2785 if (dirent_match(p->str, enc, name, dp, flags))
2786 *new_end++ = p->next;
2792 status = glob_helper(fd, buf, baselen, name - buf - baselen + namlen, 1,
2793 new_pathtype, new_beg, new_end,
2794 flags, funcs, arg, enc);
2800 glob_dir_finish(&globent, flags);
2805# if FNM_SYSCASE || NORMALIZE_UTF8PATH
2808 copy_beg = copy_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
2809 if (!copy_beg)
return -1;
2810 for (cur = beg; cur < end; ++cur)
2811 *copy_end++ = (*cur)->type <= ALPHA ? *cur : 0;
2813 for (cur = copy_beg; cur < copy_end; ++cur) {
2815 rb_pathtype_t new_pathtype = path_unknown;
2818 size_t len = strlen((*cur)->str) + 1;
2819 name = GLOB_ALLOC_N(
char,
len);
2824 memcpy(name, (*cur)->str,
len);
2826 len = remove_backslashes(name, name+
len-1, enc) - name;
2828 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
2834 *new_end++ = (*cur)->next;
2835 for (cur2 = cur + 1; cur2 < copy_end; ++cur2) {
2836 if (*cur2 && fnmatch((*cur2)->str, enc, name, flags) == 0) {
2837 *new_end++ = (*cur2)->next;
2842 buf = join_path(path, pathlen, dirsep, name,
len);
2849#if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2850 if ((*cur)->type == ALPHA) {
2851 buf = replace_real_basename(buf, pathlen + (dirsep != 0), enc,
2852 IF_NORMALIZE_UTF8PATH(1)+0,
2853 flags, &new_pathtype);
2857 status = glob_helper(fd, buf, baselen,
2858 namelen + strlen(buf + pathlen), 1,
2859 new_pathtype, new_beg, new_end,
2860 flags, funcs, arg, enc);
2867 GLOB_FREE(copy_beg);
2874push_caller(
const char *path,
VALUE val,
void *enc)
2880 list = glob_make_pattern(path, path + strlen(path), arg->flags, enc);
2884 status = glob_helper(arg->fd, arg->path, arg->baselen, arg->namelen, arg->dirsep,
2885 arg->pathtype, &list, &list + 1, arg->flags, arg->funcs,
2887 glob_free_pattern(list);
2891static int ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
2903push_glob0_caller(
const char *path,
VALUE val,
void *enc)
2906 return ruby_glob0(path, arg->fd, arg->base, arg->flags, arg->funcs, arg->arg, enc);
2910ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
2915 const char *root, *start;
2917 size_t n, baselen = 0;
2918 int status, dirsep = FALSE;
2920 start = root = path;
2929 return ruby_brace_expand(path, flags, push_glob0_caller, (
VALUE)&args, enc,
Qfalse);
2932 flags |= FNM_SYSCASE;
2934 root = rb_enc_path_skip_prefix(root, root + strlen(root), enc);
2937 if (*root ==
'/') root++;
2946 buf = GLOB_ALLOC_N(
char, n + 1);
2947 if (!buf)
return -1;
2948 MEMCPY(buf, start,
char, n);
2951 list = glob_make_pattern(root, root + strlen(root), flags, enc);
2956 status = glob_helper(fd, buf, baselen, n-baselen, dirsep,
2957 path_unknown, &list, &list + 1,
2958 flags, funcs, arg, enc);
2959 glob_free_pattern(list);
2971 return ruby_glob0(path, AT_FDCWD, 0, flags & ~GLOB_VERBOSE,
2972 &funcs, arg, rb_ascii8bit_encoding());
2976rb_glob_caller(
const char *path,
VALUE a,
void *enc)
2982 rb_protect(glob_func_caller, a, &status);
2987 rb_glob_caller, rb_glob_error,
2991rb_glob(
const char *path,
void (*func)(
const char *,
VALUE,
void *),
VALUE arg)
2998 args.enc = rb_ascii8bit_encoding();
3000 status = ruby_glob0(path, AT_FDCWD, 0, GLOB_VERBOSE, &rb_glob_funcs,
3001 (
VALUE)&args, args.enc);
3002 if (status) GLOB_JUMP_TAG(status);
3006push_pattern(
const char *path,
VALUE ary,
void *enc)
3008#if defined _WIN32 || defined __APPLE__
3010 rb_encoding *eenc = rb_default_internal_encoding();
3015 rb_ary_push(ary, name);
3020 rb_encoding *enc,
VALUE var)
3022 const int escape = !(flags & FNM_NOESCAPE);
3023 const char *p = str;
3024 const char *pend = p + strlen(p);
3026 const char *lbrace = 0, *rbrace = 0;
3027 int nest = 0, status = 0;
3030 if (*p ==
'{' && nest++ == 0) {
3033 if (*p ==
'}' && lbrace && --nest == 0) {
3037 if (*p ==
'\\' && escape) {
3043 if (lbrace && rbrace) {
3044 size_t len = strlen(s) + 1;
3045 char *buf = GLOB_ALLOC_N(
char,
len);
3048 if (!buf)
return -1;
3049 memcpy(buf, s, lbrace-s);
3052 while (p < rbrace) {
3053 const char *t = ++p;
3055 while (p < rbrace && !(*p ==
',' && nest == 0)) {
3056 if (*p ==
'{') nest++;
3057 if (*p ==
'}') nest--;
3058 if (*p ==
'\\' && escape) {
3059 if (++p == rbrace)
break;
3063 memcpy(buf+shift, t, p-t);
3064 strlcpy(buf+shift+(p-t), rbrace+1,
len-(shift+(p-t)));
3065 status = ruby_brace_expand(buf, flags, func, arg, enc, var);
3070 else if (!lbrace && !rbrace) {
3071 status = glob_call_func(func, s, arg, enc);
3085glob_brace(
const char *path,
VALUE val,
void *enc)
3089 return ruby_glob0(path, AT_FDCWD, 0, arg->flags, &arg->funcs, arg->value, enc);
3093ruby_brace_glob_with_enc(
const char *str,
int flags,
ruby_glob_func *func,
VALUE arg, rb_encoding *enc)
3097 flags &= ~GLOB_VERBOSE;
3098 args.funcs.match = func;
3099 args.funcs.error = 0;
3102 return ruby_brace_expand(str, flags, glob_brace, (
VALUE)&args, enc,
Qfalse);
3108 return ruby_brace_glob_with_enc(str, flags, func, arg, rb_ascii8bit_encoding());
3116 rb_encoding *enc = rb_enc_get(str);
3118#if defined _WIN32 || defined __APPLE__
3119 str = rb_str_encode_ospath(str);
3121 if (rb_enc_to_index(enc) == ENCINDEX_US_ASCII)
3122 enc = rb_filesystem_encoding();
3123 if (rb_enc_to_index(enc) == ENCINDEX_US_ASCII)
3124 enc = rb_ascii8bit_encoding();
3125 flags |= GLOB_VERBOSE;
3126 args.func = push_pattern;
3132 if (!RB_TYPE_P(base,
T_STRING) || !rb_enc_check(str, base)) {
3133 struct dir_data *dirp = RTYPEDDATA_GET_DATA(base);
3134 if (!dirp->dir) dir_closed();
3136 if ((fd = dirfd(dirp->dir)) == -1)
3137 rb_sys_fail_path(dir_inspect(base));
3141 args.base = RSTRING_PTR(base);
3143#if defined _WIN32 || defined __APPLE__
3144 enc = rb_utf8_encoding();
3147 return ruby_glob0(RSTRING_PTR(str), fd, args.base, flags, &rb_glob_funcs,
3152rb_push_glob(
VALUE str,
VALUE base,
int flags)
3161 else if (!rb_str_to_cstr(str)) {
3162 rb_raise(rb_eArgError,
"nul-separated glob pattern is deprecated");
3165 rb_enc_check(str, rb_enc_from_encoding(rb_usascii_encoding()));
3169 status = push_glob(ary, str, base, flags);
3170 if (status) GLOB_JUMP_TAG(status);
3178 VALUE ary = rb_ary_new();
3185 status = push_glob(ary, str, base, flags);
3186 if (status) GLOB_JUMP_TAG(status);
3194dir_glob_option_base(
VALUE base)
3196 if (NIL_OR_UNDEF_P(base)) {
3205 if (!RSTRING_LEN(base))
return Qnil;
3210dir_glob_option_sort(
VALUE sort)
3212 return (rb_bool_expected(sort,
"sort", TRUE) ? 0 : FNM_GLOB_NOSORT);
3218 const int flags = dir_glob_option_sort(sort);
3219 base = dir_glob_option_base(base);
3221 return rb_push_glob(
RARRAY_AREF(args, 0), base, flags);
3223 return dir_globs(args, base, flags);
3229 VALUE ary = rb_check_array_type(str);
3230 const int flags = (
NUM2INT(rflags) | dir_glob_option_sort(sort)) & ~FNM_CASEFOLD;
3231 base = dir_glob_option_base(base);
3233 ary = rb_push_glob(str, base, flags);
3236 ary = dir_globs(ary, base, flags);
3247dir_open_dir(
int argc,
VALUE *argv)
3293 dir = dir_open_dir(argc, argv);
3294 rb_ensure(dir_each, dir, dir_close, dir);
3299dir_collect(
VALUE dir)
3301 VALUE ary = rb_ary_new();
3302 dir_each_entry(dir, rb_ary_push, ary, FALSE);
3328 dir = dir_open_dir(argc, argv);
3329 return rb_ensure(dir_collect, dir, dir_close, dir);
3333dir_each_child(
VALUE dir)
3335 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3347dir_s_each_child(
int argc,
VALUE *argv,
VALUE io)
3352 dir = dir_open_dir(argc, argv);
3353 rb_ensure(dir_each_child, dir, dir_close, dir);
3376dir_each_child_m(
VALUE dir)
3379 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3394dir_collect_children(
VALUE dir)
3396 VALUE ary = rb_ary_new();
3397 dir_each_entry(dir, rb_ary_push, ary, TRUE);
3421dir_s_children(
int argc,
VALUE *argv,
VALUE io)
3425 dir = dir_open_dir(argc, argv);
3426 return rb_ensure(dir_collect_children, dir, dir_close, dir);
3430fnmatch_brace(
const char *pattern,
VALUE val,
void *enc)
3433 VALUE path = arg->value;
3434 rb_encoding *enc_pattern = enc;
3435 rb_encoding *enc_path = rb_enc_get(path);
3437 if (enc_pattern != enc_path) {
3438 if (!rb_enc_asciicompat(enc_pattern))
3440 if (!rb_enc_asciicompat(enc_path))
3444 long len = strlen(pattern);
3446 enc_pattern, &cr) !=
len)
3452 return (fnmatch(pattern, enc, RSTRING_PTR(path), arg->flags) == 0);
3457file_s_fnmatch(
int argc,
VALUE *argv,
VALUE obj)
3459 VALUE pattern, path;
3463 if (
rb_scan_args(argc, argv,
"21", &pattern, &path, &rflags) == 3)
3471 if (flags & FNM_EXTGLOB) {
3476 if (ruby_brace_expand(RSTRING_PTR(pattern), flags, fnmatch_brace,
3477 (
VALUE)&args, rb_enc_get(pattern), pattern) > 0)
3481 rb_encoding *enc = rb_enc_compatible(pattern, path);
3483 if (fnmatch(RSTRING_PTR(pattern), enc, RSTRING_PTR(path), flags) == 0)
3510 user = (argc > 0) ? argv[0] :
Qnil;
3516 return rb_home_dir_of(user,
rb_str_new(0, 0));
3519 return rb_default_home_dir(
rb_str_new(0, 0));
3538rb_file_directory_p(
void)
3544nogvl_dir_empty_p(
void *ptr)
3546 const char *path = ptr;
3547 DIR *dir = opendir(path);
3553 switch (gc_for_fd_with_gvl(e)) {
3555 dir = opendir(path);
3560 if (e == ENOTDIR)
return (
void *)
Qfalse;
3564 while ((dp = READDIR(dir, NULL)) != NULL) {
3565 if (!to_be_skipped(dp)) {
3571 return (
void *)result;
3594 enum {false_on_notdir = 1};
3598 dirname = rb_str_encode_ospath(dirname);
3600 path = RSTRING_PTR(dirname);
3602#if defined HAVE_GETATTRLIST && defined ATTR_DIR_ENTRYCOUNT
3604 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
3605 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
3606 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), 0) != 0)
3607 rb_sys_fail_path(orig);
3608 if (*(
const fsobj_tag_t *)(attrbuf+1) == VT_HFS) {
3610 al.dirattr = ATTR_DIR_ENTRYCOUNT;
3611 if (getattrlist(path, &al, attrbuf,
sizeof(attrbuf), 0) == 0) {
3612 if (attrbuf[0] >= 2 *
sizeof(u_int32_t))
3613 return RBOOL(attrbuf[1] == 0);
3614 if (false_on_notdir)
return Qfalse;
3616 rb_sys_fail_path(orig);
3624 rb_syserr_fail_path((
int)
FIX2LONG(result), orig);
3678 rb_file_const(
"FNM_NOESCAPE",
INT2FIX(FNM_NOESCAPE));
3681 rb_file_const(
"FNM_PATHNAME",
INT2FIX(FNM_PATHNAME));
3684 rb_file_const(
"FNM_DOTMATCH",
INT2FIX(FNM_DOTMATCH));
3687 rb_file_const(
"FNM_CASEFOLD",
INT2FIX(FNM_CASEFOLD));
3690 rb_file_const(
"FNM_EXTGLOB",
INT2FIX(FNM_EXTGLOB));
3693 rb_file_const(
"FNM_SYSCASE",
INT2FIX(FNM_SYSCASE));
3696 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_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.
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 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 FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
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.
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 unless $VERBOSE is nil.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_cFile
File class.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
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.
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_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.
#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.
#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.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
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.
#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.
int len
Length of the buffer.
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 ...
#define RB_NUM2INT
Just another name of rb_num2int_inline.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
static int rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
VALUE type(ANYARGS)
ANYARGS-ed function type.
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.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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 errno
Ractor-aware version of errno.
#define FilePathStringValue(v)
This macro actually does the same thing as FilePathValue now.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
uintptr_t VALUE
Type that represents a Ruby object.