Ruby 4.1.0dev (2026-06-14 revision 5e700b36c229e26ec28626bdaa0eec4390df3cc3)
dir.c (5e700b36c229e26ec28626bdaa0eec4390df3cc3)
1/**********************************************************************
2
3 dir.c -
4
5 $Author$
6 created at: Wed Jan 5 09:51:01 JST 1994
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#include "ruby/internal/config.h"
15
16#include <ctype.h>
17#include <errno.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20
21#ifdef HAVE_UNISTD_H
22#include <unistd.h>
23#endif
24
25#ifndef USE_OPENDIR_AT
26# if defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD) && \
27 defined(HAVE_OPENAT) && defined(HAVE_FSTATAT)
28# define USE_OPENDIR_AT 1
29# else
30# define USE_OPENDIR_AT 0
31# endif
32#endif
33
34#ifdef HAVE_FCNTL_H
35# include <fcntl.h>
36#endif
37
38#ifndef O_CLOEXEC
39# define O_CLOEXEC 0
40#endif
41
42#undef HAVE_DIRENT_NAMLEN
43#if defined HAVE_DIRENT_H && !defined _WIN32
44# include <dirent.h>
45# define NAMLEN(dirent) strlen((dirent)->d_name)
46#elif defined HAVE_DIRECT_H && !defined _WIN32
47# include <direct.h>
48# define NAMLEN(dirent) strlen((dirent)->d_name)
49#else
50# define dirent direct
51# define NAMLEN(dirent) (dirent)->d_namlen
52# define HAVE_DIRENT_NAMLEN 1
53# ifdef HAVE_SYS_NDIR_H
54# include <sys/ndir.h>
55# endif
56# ifdef HAVE_SYS_DIR_H
57# include <sys/dir.h>
58# endif
59# ifdef HAVE_NDIR_H
60# include <ndir.h>
61# endif
62# ifdef _WIN32
63# include "win32/dir.h"
64# endif
65#endif
66
67#ifndef HAVE_STDLIB_H
68char *getenv();
69#endif
70
71#ifndef HAVE_STRING_H
72char *strchr(char*,char);
73#endif
74
75#ifdef HAVE_SYS_ATTR_H
76#include <sys/attr.h>
77#endif
78
79#define USE_NAME_ON_FS_REAL_BASENAME 1 /* platform dependent APIs to
80 * get real basenames */
81#define USE_NAME_ON_FS_BY_FNMATCH 2 /* select the matching
82 * basename by fnmatch */
83
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))
88#elif defined _WIN32
89# define USE_NAME_ON_FS USE_NAME_ON_FS_REAL_BASENAME
90#elif defined DOSISH
91# define USE_NAME_ON_FS USE_NAME_ON_FS_BY_FNMATCH
92#else
93# define USE_NAME_ON_FS 0
94#endif
95
96#ifdef __APPLE__
97# define NORMALIZE_UTF8PATH 1
98# include <sys/param.h>
99# include <sys/mount.h>
100# include <sys/vnode.h>
101#else
102# define NORMALIZE_UTF8PATH 0
103#endif
104
105#include "encindex.h"
106#include "id.h"
107#include "internal.h"
108#include "internal/array.h"
109#include "internal/dir.h"
110#include "internal/encoding.h"
111#include "internal/error.h"
112#include "internal/file.h"
113#include "internal/gc.h"
114#include "internal/io.h"
115#include "internal/object.h"
116#include "internal/imemo.h"
117#include "internal/vm.h"
118#include "ruby/encoding.h"
119#include "ruby/ruby.h"
120#include "ruby/thread.h"
121#include "ruby/util.h"
122#include "builtin.h"
123
124#ifndef AT_FDCWD
125# define AT_FDCWD -1
126#endif
127
128#define vm_initialized rb_cThread
129
130/* define system APIs */
131#ifdef _WIN32
132# undef chdir
133# define chdir(p) rb_w32_uchdir(p)
134# undef mkdir
135# define mkdir(p, m) rb_w32_umkdir((p), (m))
136# undef rmdir
137# define rmdir(p) rb_w32_urmdir(p)
138# undef opendir
139# define opendir(p) rb_w32_uopendir(p)
140# define ruby_getcwd() rb_w32_ugetcwd(NULL, 0)
141# define IS_WIN32 1
142#else
143# define IS_WIN32 0
144#endif
145
146#ifdef HAVE_GETATTRLIST
147struct getattrlist_args {
148 const char *path;
149 int fd;
150 struct attrlist *list;
151 void *buf;
152 size_t size;
153 unsigned int options;
154};
155
156# define GETATTRLIST_ARGS(list_, buf_, options_) (struct getattrlist_args) \
157 {.list = list_, .buf = buf_, .size = sizeof(buf_), .options = options_}
158
159static void *
160nogvl_getattrlist(void *args)
161{
162 struct getattrlist_args *arg = args;
163 return (void *)(VALUE)getattrlist(arg->path, arg->list, arg->buf, arg->size, arg->options);
164}
165
166static int
167gvl_getattrlist(struct getattrlist_args *args, const char *path)
168{
169 args->path = path;
170 return IO_WITHOUT_GVL_INT(nogvl_getattrlist, args);
171}
172
173# ifdef HAVE_FGETATTRLIST
174static void *
175nogvl_fgetattrlist(void *args)
176{
177 struct getattrlist_args *arg = args;
178 return (void *)(VALUE)fgetattrlist(arg->fd, arg->list, arg->buf, arg->size, arg->options);
179}
180
181static int
182gvl_fgetattrlist(struct getattrlist_args *args, int fd)
183{
184 args->fd = fd;
185 return IO_WITHOUT_GVL_INT(nogvl_fgetattrlist, args);
186}
187# endif
188#endif
189
190#if NORMALIZE_UTF8PATH
191# if defined HAVE_FGETATTRLIST || !defined HAVE_GETATTRLIST
192# define need_normalization(dirp, path) need_normalization(dirp)
193# else
194# define need_normalization(dirp, path) need_normalization(path)
195# endif
196static inline int
197need_normalization(DIR *dirp, const char *path)
198{
199# if defined HAVE_FGETATTRLIST || defined HAVE_GETATTRLIST
200 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
201 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
202 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, 0);
203# if defined HAVE_FGETATTRLIST
204 int ret = gvl_fgetattrlist(&args, dirfd(dirp));
205# else
206 int ret = gvl_getattrlist(&args, path);
207# endif
208 if (!ret) {
209 const fsobj_tag_t *tag = (void *)(attrbuf+1);
210 switch (*tag) {
211 case VT_HFS:
212 case VT_CIFS:
213 return TRUE;
214 }
215 }
216# endif
217 return FALSE;
218}
219
220static inline int
221has_nonascii(const char *ptr, size_t len)
222{
223 while (len > 0) {
224 if (!ISASCII(*ptr)) return 1;
225 ptr++;
226 --len;
227 }
228 return 0;
229}
230
231# define IF_NORMALIZE_UTF8PATH(something) something
232#else
233# define IF_NORMALIZE_UTF8PATH(something) /* nothing */
234#endif
235
236#if defined(IFTODT) && defined(DT_UNKNOWN)
237# define EMULATE_IFTODT 0
238#else
239# define EMULATE_IFTODT 1
240#endif
241
242#if EMULATE_IFTODT
243# define IFTODT(m) (((m) & S_IFMT) / ((~S_IFMT & (S_IFMT-1)) + 1))
244#endif
245
246typedef enum {
247#if !EMULATE_IFTODT
248 path_exist = DT_UNKNOWN,
249 path_directory = DT_DIR,
250 path_regular = DT_REG,
251 path_symlink = DT_LNK,
252#else
253 path_exist,
254 path_directory = IFTODT(S_IFDIR),
255 path_regular = IFTODT(S_IFREG),
256 path_symlink = IFTODT(S_IFLNK),
257#endif
258 path_noent = -1,
259 path_unknown = -2
260} rb_pathtype_t;
261
262#define FNM_NOESCAPE 0x01
263#define FNM_PATHNAME 0x02
264#define FNM_DOTMATCH 0x04
265#define FNM_CASEFOLD 0x08
266#define FNM_EXTGLOB 0x10
267#if CASEFOLD_FILESYSTEM
268#define FNM_SYSCASE FNM_CASEFOLD
269#else
270#define FNM_SYSCASE 0
271#endif
272#ifdef _WIN32
273#define FNM_SHORTNAME 0x20
274#else
275#define FNM_SHORTNAME 0
276#endif
277#define FNM_GLOB_NOSORT 0x40
278#define FNM_GLOB_SKIPDOT 0x80
279
280#define FNM_NOMATCH 1
281#define FNM_ERROR 2
282
283# define Next(p, e, enc) ((p)+ rb_enc_mbclen((p), (e), (enc)))
284# define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
285
286static char *
287bracket(
288 const char *p, /* pattern (next to '[') */
289 const char *pend,
290 const char *s, /* string */
291 const char *send,
292 int flags,
293 rb_encoding *enc)
294{
295 const int nocase = flags & FNM_CASEFOLD;
296 const int escape = !(flags & FNM_NOESCAPE);
297 unsigned int c1, c2;
298 int r;
299 int ok = 0, not = 0;
300
301 if (p >= pend) return NULL;
302 if (*p == '!' || *p == '^') {
303 not = 1;
304 p++;
305 }
306
307 while (*p != ']') {
308 const char *t1 = p;
309 if (escape && *t1 == '\\')
310 t1++;
311 if (!*t1)
312 return NULL;
313 p = t1 + (r = rb_enc_mbclen(t1, pend, enc));
314 if (p >= pend) return NULL;
315 if (p[0] == '-' && p[1] != ']') {
316 const char *t2 = p + 1;
317 int r2;
318 if (escape && *t2 == '\\')
319 t2++;
320 if (!*t2)
321 return NULL;
322 p = t2 + (r2 = rb_enc_mbclen(t2, pend, enc));
323 if (ok) continue;
324 if ((r <= (send-s) && memcmp(t1, s, r) == 0) ||
325 (r2 <= (send-s) && memcmp(t2, s, r2) == 0)) {
326 ok = 1;
327 continue;
328 }
329 c1 = rb_enc_codepoint(s, send, enc);
330 if (nocase) c1 = rb_enc_toupper(c1, enc);
331 c2 = rb_enc_codepoint(t1, pend, enc);
332 if (nocase) c2 = rb_enc_toupper(c2, enc);
333 if (c1 < c2) continue;
334 c2 = rb_enc_codepoint(t2, pend, enc);
335 if (nocase) c2 = rb_enc_toupper(c2, enc);
336 if (c1 > c2) continue;
337 }
338 else {
339 if (ok) continue;
340 if (r <= (send-s) && memcmp(t1, s, r) == 0) {
341 ok = 1;
342 continue;
343 }
344 if (!nocase) continue;
345 c1 = rb_enc_toupper(rb_enc_codepoint(s, send, enc), enc);
346 c2 = rb_enc_toupper(rb_enc_codepoint(p, pend, enc), enc);
347 if (c1 != c2) continue;
348 }
349 ok = 1;
350 }
351
352 return ok == not ? NULL : (char *)p + 1;
353}
354
355/* If FNM_PATHNAME is set, only path element will be matched. (up to '/' or '\0')
356 Otherwise, entire string will be matched.
357 End marker itself won't be compared.
358 And if function succeeds, *pcur reaches end marker.
359*/
360#define UNESCAPE(p) (escape && *(p) == '\\' ? (p) + 1 : (p))
361#define ISEND(p) (!*(p) || (pathname && *(p) == '/'))
362#define RETURN(val) return *pcur = p, *scur = s, (val);
363
364static int
365fnmatch_helper(
366 const char **pcur, /* pattern */
367 const char **scur, /* string */
368 int flags,
369 rb_encoding *enc)
370{
371 const int period = !(flags & FNM_DOTMATCH);
372 const int pathname = flags & FNM_PATHNAME;
373 const int escape = !(flags & FNM_NOESCAPE);
374 const int nocase = flags & FNM_CASEFOLD;
375
376 const char *ptmp = 0;
377 const char *stmp = 0;
378
379 const char *p = *pcur;
380 const char *pend = p + strlen(p);
381 const char *s = *scur;
382 const char *send = s + strlen(s);
383
384 int r;
385
386 if (period && *s == '.' && *UNESCAPE(p) != '.') /* leading period */
387 RETURN(FNM_NOMATCH);
388
389 while (1) {
390 switch (*p) {
391 case '*':
392 do { p++; } while (*p == '*');
393 if (ISEND(UNESCAPE(p))) {
394 p = UNESCAPE(p);
395 RETURN(0);
396 }
397 if (ISEND(s))
398 RETURN(FNM_NOMATCH);
399 ptmp = p;
400 stmp = s;
401 continue;
402
403 case '?':
404 if (ISEND(s))
405 RETURN(FNM_NOMATCH);
406 p++;
407 Inc(s, send, enc);
408 continue;
409
410 case '[': {
411 const char *t;
412 if (ISEND(s))
413 RETURN(FNM_NOMATCH);
414 if ((t = bracket(p + 1, pend, s, send, flags, enc)) != 0) {
415 p = t;
416 Inc(s, send, enc);
417 continue;
418 }
419 goto failed;
420 }
421 }
422
423 /* ordinary */
424 p = UNESCAPE(p);
425 if (ISEND(s))
426 RETURN(ISEND(p) ? 0 : FNM_NOMATCH);
427 if (ISEND(p))
428 goto failed;
429 r = rb_enc_precise_mbclen(p, pend, enc);
430 if (!MBCLEN_CHARFOUND_P(r))
431 goto failed;
432 if (r <= (send-s) && memcmp(p, s, r) == 0) {
433 p += r;
434 s += r;
435 continue;
436 }
437 if (!nocase) goto failed;
438 if (rb_enc_toupper(rb_enc_codepoint(p, pend, enc), enc) !=
439 rb_enc_toupper(rb_enc_codepoint(s, send, enc), enc))
440 goto failed;
441 p += r;
442 Inc(s, send, enc);
443 continue;
444
445 failed: /* try next '*' position */
446 if (ptmp && stmp) {
447 p = ptmp;
448 Inc(stmp, send, enc); /* !ISEND(*stmp) */
449 s = stmp;
450 continue;
451 }
452 RETURN(FNM_NOMATCH);
453 }
454}
455
456static int
457fnmatch(
458 const char *pattern,
459 rb_encoding *enc,
460 const char *string,
461 int flags)
462{
463 const char *p = pattern;
464 const char *s = string;
465 const char *send = s + strlen(string);
466 const int period = !(flags & FNM_DOTMATCH);
467 const int pathname = flags & FNM_PATHNAME;
468
469 const char *ptmp = 0;
470 const char *stmp = 0;
471
472 if (pathname) {
473 while (1) {
474 if (p[0] == '*' && p[1] == '*' && p[2] == '/') {
475 do { p += 3; } while (p[0] == '*' && p[1] == '*' && p[2] == '/');
476 ptmp = p;
477 stmp = s;
478 }
479 if (fnmatch_helper(&p, &s, flags, enc) == 0) {
480 while (*s && *s != '/') Inc(s, send, enc);
481 if (*p && *s) {
482 p++;
483 s++;
484 continue;
485 }
486 if (!*p && !*s)
487 return 0;
488 }
489 /* failed : try next recursion */
490 if (ptmp && stmp && !(period && *stmp == '.')) {
491 while (*stmp && *stmp != '/') Inc(stmp, send, enc);
492 if (*stmp) {
493 p = ptmp;
494 stmp++;
495 s = stmp;
496 continue;
497 }
498 }
499 return FNM_NOMATCH;
500 }
501 }
502 else
503 return fnmatch_helper(&p, &s, flags, enc);
505
507static VALUE sym_directory, sym_link, sym_file, sym_unknown;
508
509#if defined(DT_BLK) || defined(S_IFBLK)
510static VALUE sym_block_device;
511#endif
512#if defined(DT_CHR) || defined(S_IFCHR)
513static VALUE sym_character_device;
514#endif
515#if defined(DT_FIFO) || defined(S_IFIFO)
516static VALUE sym_fifo;
517#endif
518#if defined(DT_SOCK) || defined(S_IFSOCK)
519static VALUE sym_socket;
520#endif
521
522struct dir_data {
523 DIR *dir;
524 const VALUE path;
525 rb_encoding *enc;
526};
527
528static void
529dir_free(void *ptr)
530{
531 struct dir_data *dir = ptr;
532
533 if (dir->dir) closedir(dir->dir);
534}
535
536RUBY_REFERENCES(dir_refs) = {
537 RUBY_REF_EDGE(struct dir_data, path),
538 RUBY_REF_END
539};
540
541static const rb_data_type_t dir_data_type = {
542 "dir",
543 {
544 RUBY_REFS_LIST_PTR(dir_refs),
545 dir_free,
546 NULL, // Nothing allocated externally, so don't need a memsize function
547 },
548 0, NULL, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE
549};
550
551static VALUE dir_close(VALUE);
552
553static VALUE
554dir_s_alloc(VALUE klass)
555{
556 struct dir_data *dirp;
557 VALUE obj = TypedData_Make_Struct(klass, struct dir_data, &dir_data_type, dirp);
558
559 dirp->dir = NULL;
560 RB_OBJ_WRITE(obj, &dirp->path, Qnil);
561 dirp->enc = NULL;
562
563 return obj;
564}
565
566static void *
567nogvl_opendir(void *ptr)
568{
569 const char *path = ptr;
570
571 return opendir(path);
572}
573
574static DIR *
575opendir_without_gvl(const char *path)
576{
577 if (vm_initialized) {
578 union { const void *in; void *out; } u;
579
580 u.in = path;
581
582 return IO_WITHOUT_GVL(nogvl_opendir, u.out);
583 }
584 else
585 return opendir(path);
586}
587
588static void
589close_dir_data(struct dir_data *dp)
590{
591 if (dp->dir) {
592 if (closedir(dp->dir) < 0) {
593 dp->dir = NULL;
594 rb_sys_fail("closedir");
595 }
596 dp->dir = NULL;
597 }
598}
599
600static void
601check_closedir(DIR *dirp)
602{
603 if (closedir(dirp) < 0)
604 rb_sys_fail("closedir");
605}
606
607static VALUE
608dir_initialize(rb_execution_context_t *ec, VALUE dir, VALUE dirname, VALUE enc)
609{
610 struct dir_data *dp;
611 VALUE orig;
612 const char *path;
613 rb_encoding *fsenc = NIL_P(enc) ? rb_filesystem_encoding() : rb_to_encoding(enc);
614
615 FilePathValue(dirname);
616 orig = rb_str_dup_frozen(dirname);
617 dirname = rb_str_encode_ospath(dirname);
618 dirname = rb_str_dup_frozen(dirname);
619
620 TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dp);
621 close_dir_data(dp);
622 RB_OBJ_WRITE(dir, &dp->path, Qnil);
623 dp->enc = fsenc;
624 path = RSTRING_PTR(dirname);
625 dp->dir = opendir_without_gvl(path);
626 if (dp->dir == NULL) {
627 int e = errno;
628 if (rb_gc_for_fd(e)) {
629 dp->dir = opendir_without_gvl(path);
630 }
631#ifdef HAVE_GETATTRLIST
632 else if (e == EIO) {
633 u_int32_t attrbuf[1];
634 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0};
635 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, FSOPT_NOFOLLOW);
636 if (gvl_getattrlist(&args, path) == 0) {
637 dp->dir = opendir_without_gvl(path);
638 }
639 }
640#endif
641 if (dp->dir == NULL) {
642 RB_GC_GUARD(dirname);
643 rb_syserr_fail_path(e, orig);
644 }
645 }
646 RB_OBJ_WRITE(dir, &dp->path, orig);
647
648 return dir;
649}
650
651static VALUE
652dir_s_open(rb_execution_context_t *ec, VALUE klass, VALUE dirname, VALUE enc)
653{
654 struct dir_data *dp;
655 VALUE dir = TypedData_Make_Struct(klass, struct dir_data, &dir_data_type, dp);
656
657 dir_initialize(ec, dir, dirname, enc);
658
659 return dir;
660}
661
662static VALUE
663dir_s_close(rb_execution_context_t *ec, VALUE klass, VALUE dir)
664{
665 return dir_close(dir);
666}
667
668# if defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD)
669static void *
670nogvl_fdopendir(void *fd)
671{
672 return fdopendir((int)(VALUE)fd);
673}
674
675/*
676 * call-seq:
677 * Dir.for_fd(fd) -> dir
678 *
679 * Returns a new \Dir object representing the directory specified by the given
680 * integer directory file descriptor +fd+:
681 *
682 * d0 = Dir.new('..')
683 * d1 = Dir.for_fd(d0.fileno)
684 *
685 * Note that the returned +d1+ does not have an associated path:
686 *
687 * d0.path # => '..'
688 * d1.path # => nil
689 *
690 * This method uses the
691 * {fdopendir()}[https://www.man7.org/linux/man-pages/man3/fdopendir.3p.html]
692 * function defined by POSIX 2008;
693 * the method is not implemented on non-POSIX platforms (raises NotImplementedError).
694 */
695static VALUE
696dir_s_for_fd(VALUE klass, VALUE fd)
697{
698 struct dir_data *dp;
699 VALUE dir = TypedData_Make_Struct(klass, struct dir_data, &dir_data_type, dp);
700
701 if (!(dp->dir = IO_WITHOUT_GVL(nogvl_fdopendir, (void *)(VALUE)NUM2INT(fd)))) {
702 rb_sys_fail("fdopendir");
704 }
705
706 RB_OBJ_WRITE(dir, &dp->path, Qnil);
707 return dir;
708}
709#else
710#define dir_s_for_fd rb_f_notimplement
711#endif
712
713NORETURN(static void dir_closed(void));
714
715static void
716dir_closed(void)
717{
718 rb_raise(rb_eIOError, "closed directory");
719}
720
721static struct dir_data *
722dir_get(VALUE dir)
723{
724 rb_check_frozen(dir);
725 return rb_check_typeddata(dir, &dir_data_type);
726}
727
728static struct dir_data *
729dir_check(VALUE dir)
730{
731 struct dir_data *dirp = dir_get(dir);
732 if (!dirp->dir) dir_closed();
733 return dirp;
734}
735
736#define GetDIR(obj, dirp) ((dirp) = dir_check(obj))
737
738
739/*
740 * call-seq:
741 * inspect -> string
742 *
743 * Returns a string description of +self+:
744 *
745 * Dir.new('example').inspect # => "#<Dir:example>"
746 *
747 */
748static VALUE
749dir_inspect(VALUE dir)
750{
751 struct dir_data *dirp;
752
753 TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
754 if (!NIL_P(dirp->path)) {
755 VALUE str = rb_str_new_cstr("#<");
757 rb_str_cat2(str, ":");
758 rb_str_append(str, dirp->path);
759 rb_str_cat2(str, ">");
760 return str;
761 }
762 return rb_funcallv(dir, idTo_s, 0, 0);
763}
764
765/* Workaround for Solaris 10 that does not have dirfd.
766 Note: Solaris 11 (POSIX.1-2008 compliant) has dirfd(3C).
767 */
768#if defined(__sun) && !defined(HAVE_DIRFD)
769# if defined(HAVE_DIR_D_FD)
770# define dirfd(x) ((x)->d_fd)
771# define HAVE_DIRFD 1
772# elif defined(HAVE_DIR_DD_FD)
773# define dirfd(x) ((x)->dd_fd)
774# define HAVE_DIRFD 1
775# endif
776#endif
777
778#ifdef HAVE_DIRFD
779/*
780 * call-seq:
781 * fileno -> integer
782 *
783 * Returns the file descriptor used in <em>dir</em>.
784 *
785 * d = Dir.new('..')
786 * d.fileno # => 8
787 *
788 * This method uses the
789 * {dirfd()}[https://www.man7.org/linux/man-pages/man3/dirfd.3.html]
790 * function defined by POSIX 2008;
791 * the method is not implemented on non-POSIX platforms (raises NotImplementedError).
792 */
793static VALUE
794dir_fileno(VALUE dir)
795{
796 struct dir_data *dirp;
797 int fd;
798
799 GetDIR(dir, dirp);
800 fd = dirfd(dirp->dir);
801 if (fd == -1)
802 rb_sys_fail("dirfd");
803 return INT2NUM(fd);
804}
805#else
806#define dir_fileno rb_f_notimplement
807#endif
808
809/*
810 * call-seq:
811 * path -> string or nil
812 *
813 * Returns the +dirpath+ string that was used to create +self+
814 * (or +nil+ if created by method Dir.for_fd):
815 *
816 * Dir.new('example').path # => "example"
817 *
818 */
819static VALUE
820dir_path(VALUE dir)
821{
822 struct dir_data *dirp;
823
824 TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp);
825 if (NIL_P(dirp->path)) return Qnil;
826 return rb_str_dup(dirp->path);
827}
828
829#if defined _WIN32
830static int
831fundamental_encoding_p(rb_encoding *enc)
832{
833 switch (rb_enc_to_index(enc)) {
834 case ENCINDEX_ASCII_8BIT:
835 case ENCINDEX_US_ASCII:
836 case ENCINDEX_UTF_8:
837 return TRUE;
838 default:
839 return FALSE;
840 }
841}
842# define READDIR(dir, enc) rb_w32_readdir((dir), (enc))
843# define READDIR_NOGVL READDIR
844#else
845NORETURN(static void *sys_failure(void *function));
846static void *
847sys_failure(void *function)
848{
849 rb_sys_fail(function);
850}
851
852static void *
853nogvl_readdir(void *dir)
854{
855 rb_errno_set(0);
856 if ((dir = readdir(dir)) == NULL) {
857 if (rb_errno())
858 rb_thread_call_with_gvl(sys_failure, (void *)"readdir");
859 }
860 return dir;
861}
862
863# define READDIR(dir, enc) IO_WITHOUT_GVL(nogvl_readdir, (void *)(dir))
864# define READDIR_NOGVL(dir, enc) nogvl_readdir((dir))
865#endif
866
867/* safe to use without GVL */
868static int
869to_be_skipped(const struct dirent *dp)
870{
871 const char *name = dp->d_name;
872 if (name[0] != '.') return FALSE;
873#ifdef HAVE_DIRENT_NAMLEN
874 switch (NAMLEN(dp)) {
875 case 2:
876 if (name[1] != '.') return FALSE;
877 case 1:
878 return TRUE;
879 default:
880 break;
881 }
882#else
883 if (!name[1]) return TRUE;
884 if (name[1] != '.') return FALSE;
885 if (!name[2]) return TRUE;
886#endif
887 return FALSE;
888}
889
890/*
891 * call-seq:
892 * read -> string or nil
893 *
894 * Reads and returns the next entry name from +self+;
895 * returns +nil+ if at end-of-stream;
896 * see {Dir As Stream-Like}[rdoc-ref:Dir@Dir+As+Stream-Like]:
897 *
898 * dir = Dir.new('example')
899 * dir.read # => "."
900 * dir.read # => ".."
901 * dir.read # => "config.h"
902 *
903 */
904static VALUE
905dir_read(VALUE dir)
906{
907 struct dir_data *dirp;
908 struct dirent *dp;
909
910 GetDIR(dir, dirp);
911 rb_errno_set(0);
912 if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
913 return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc);
914 }
915 else {
916 int e = errno;
917 if (e != 0) rb_syserr_fail(e, 0);
918 return Qnil; /* end of stream */
919 }
921
922struct dir_entry_args {
923 struct dir_data *dirp;
924 struct dirent *dp;
925};
926
927static VALUE dir_each_entry(VALUE, VALUE (*)(VALUE, VALUE, struct dir_entry_args *), VALUE, int);
928
929static VALUE
930dir_yield(VALUE arg, VALUE path, struct dir_entry_args *_unused)
931{
932 return rb_yield(path);
933}
934
935static int do_lstat(int fd, const char *path, struct stat *pst, int flags, rb_encoding *enc);
936
937static VALUE
938dir_yield_with_type(VALUE arg, VALUE path, struct dir_entry_args *dir_entry)
939{
940 VALUE type;
941 switch (dir_entry->dp->d_type) {
942#ifdef DT_BLK
943 case DT_BLK:
944 type = sym_block_device;
945 break;
946#endif
947#ifdef DT_CHR
948 case DT_CHR:
949 type = sym_character_device;
950 break;
951#endif
952 case DT_DIR:
953 type = sym_directory;
954 break;
955#ifdef DT_FIFO
956 case DT_FIFO:
957 type = sym_fifo;
958 break;
959#endif
960 case DT_LNK:
961 type = sym_link;
962 break;
963 case DT_REG:
964 type = sym_file;
965 break;
966#ifdef DT_SOCK
967 case DT_SOCK:
968 type = sym_socket;
969 break;
970#endif
971 default:
972 type = sym_unknown;
973 break;
974 }
975
976#ifdef HAVE_DIRFD
977 if (RUBY_DEBUG || RB_UNLIKELY(type == sym_unknown)) {
978 struct stat st;
979 if (do_lstat(dirfd(dir_entry->dirp->dir), dir_entry->dp->d_name, &st, 0, rb_filesystem_encoding()) == 0) {
980 switch (st.st_mode & S_IFMT) {
981 case S_IFDIR:
982 type = sym_directory;
983 break;
984 case S_IFLNK:
985 type = sym_link;
986 break;
987 case S_IFREG:
988 type = sym_file;
989 break;
990#ifdef S_IFSOCK
991 case S_IFSOCK:
992 type = sym_socket;
993 break;
994#endif
995#ifdef S_IFIFO
996 case S_IFIFO:
997 type = sym_fifo;
998 break;
999#endif
1000#ifdef S_IFBLK
1001 case S_IFBLK:
1002 type = sym_block_device;
1003 break;
1004#endif
1005#ifdef S_IFCHR
1006 case S_IFCHR:
1007 type = sym_character_device;
1008 break;
1009#endif
1010 default:
1011 break;
1012 }
1013 }
1014 }
1015#endif // HAVE_DIRFD
1016
1017 if (NIL_P(arg)) {
1018 return rb_yield_values(2, path, type);
1019 }
1020 else {
1021 return rb_ary_push(arg, rb_assoc_new(path, type));
1022 }
1023}
1024
1025/*
1026 * call-seq:
1027 * each {|entry_name| ... } -> self
1028 *
1029 * Calls the block with each entry name in +self+:
1030 *
1031 * Dir.new('example').each {|entry_name| p entry_name }
1032 *
1033 * Output:
1034
1035 * "."
1036 * ".."
1037 * "config.h"
1038 * "lib"
1039 * "main.rb"
1040 *
1041 * With no block given, returns an Enumerator.
1042 *
1043 */
1044static VALUE
1045dir_each(VALUE dir)
1046{
1047 RETURN_ENUMERATOR(dir, 0, 0);
1048 return dir_each_entry(dir, dir_yield, Qnil, FALSE);
1049}
1050
1051static VALUE
1052dir_each_entry(VALUE dir, VALUE (*each)(VALUE, VALUE, struct dir_entry_args *), VALUE arg, int children_only)
1053{
1054 struct dir_data *dirp;
1055 struct dirent *dp;
1056 IF_NORMALIZE_UTF8PATH(int norm_p);
1057
1058 GetDIR(dir, dirp);
1059 rewinddir(dirp->dir);
1060 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path)));
1061 while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
1062 const char *name = dp->d_name;
1063 size_t namlen = NAMLEN(dp);
1064 VALUE path;
1065
1066 if (children_only && name[0] == '.') {
1067 if (namlen == 1) continue; /* current directory */
1068 if (namlen == 2 && name[1] == '.') continue; /* parent directory */
1069 }
1070#if NORMALIZE_UTF8PATH
1071 if (norm_p && has_nonascii(name, namlen) &&
1072 !NIL_P(path = rb_str_normalize_ospath(name, namlen))) {
1073 path = rb_external_str_with_enc(path, dirp->enc);
1074 }
1075 else
1076#endif
1077 path = rb_external_str_new_with_enc(name, namlen, dirp->enc);
1078 struct dir_entry_args each_args = {
1079 .dirp = dirp,
1080 .dp = dp,
1081 };
1082 (*each)(arg, path, &each_args);
1083 }
1084 return dir;
1085}
1086
1087#ifdef HAVE_TELLDIR
1088/*
1089 * call-seq:
1090 * tell -> integer
1091 *
1092 * Returns the current position of +self+;
1093 * see {Dir As Stream-Like}[rdoc-ref:Dir@Dir+As+Stream-Like]:
1094 *
1095 * dir = Dir.new('example')
1096 * dir.tell # => 0
1097 * dir.read # => "."
1098 * dir.tell # => 1
1099 *
1100 */
1101static VALUE
1102dir_tell(VALUE dir)
1103{
1104 struct dir_data *dirp;
1105 long pos;
1106
1107 GetDIR(dir, dirp);
1108 if((pos = telldir(dirp->dir)) < 0)
1109 rb_sys_fail("telldir");
1110 return rb_int2inum(pos);
1111}
1112#else
1113#define dir_tell rb_f_notimplement
1114#endif
1115
1116#ifdef HAVE_SEEKDIR
1117/*
1118 * call-seq:
1119 * seek(position) -> self
1120 *
1121 * Sets the position in +self+ and returns +self+.
1122 * The value of +position+ should have been returned from an earlier call to #tell;
1123 * if not, the return values from subsequent calls to #read are unspecified.
1124 *
1125 * See {Dir As Stream-Like}[rdoc-ref:Dir@Dir+As+Stream-Like].
1126 *
1127 * Examples:
1128 *
1129 * dir = Dir.new('example')
1130 * dir.pos # => 0
1131 * dir.seek(3) # => #<Dir:example>
1132 * dir.pos # => 3
1133 * dir.seek(30) # => #<Dir:example>
1134 * dir.pos # => 5
1135 *
1136 */
1137static VALUE
1138dir_seek(VALUE dir, VALUE pos)
1139{
1140 struct dir_data *dirp;
1141 long p = NUM2LONG(pos);
1142
1143 GetDIR(dir, dirp);
1144 seekdir(dirp->dir, p);
1145 return dir;
1146}
1147#else
1148#define dir_seek rb_f_notimplement
1149#endif
1150
1151#ifdef HAVE_SEEKDIR
1152/*
1153 * call-seq:
1154 * pos = position -> integer
1155 *
1156 * Sets the position in +self+ and returns +position+.
1157 * The value of +position+ should have been returned from an earlier call to #tell;
1158 * if not, the return values from subsequent calls to #read are unspecified.
1159 *
1160 * See {Dir As Stream-Like}[rdoc-ref:Dir@Dir+As+Stream-Like].
1161 *
1162 * Examples:
1163 *
1164 * dir = Dir.new('example')
1165 * dir.pos # => 0
1166 * dir.pos = 3 # => 3
1167 * dir.pos # => 3
1168 * dir.pos = 30 # => 30
1169 * dir.pos # => 5
1170 *
1171 */
1172static VALUE
1173dir_set_pos(VALUE dir, VALUE pos)
1174{
1175 dir_seek(dir, pos);
1176 return pos;
1177}
1178#else
1179#define dir_set_pos rb_f_notimplement
1180#endif
1181
1182/*
1183 * call-seq:
1184 * rewind -> self
1185 *
1186 * Sets the position in +self+ to zero;
1187 * see {Dir As Stream-Like}[rdoc-ref:Dir@Dir+As+Stream-Like]:
1188 *
1189 * dir = Dir.new('example')
1190 * dir.read # => "."
1191 * dir.read # => ".."
1192 * dir.pos # => 2
1193 * dir.rewind # => #<Dir:example>
1194 * dir.pos # => 0
1195 *
1196 */
1197static VALUE
1198dir_rewind(VALUE dir)
1199{
1200 struct dir_data *dirp;
1201
1202 GetDIR(dir, dirp);
1203 rewinddir(dirp->dir);
1204 return dir;
1205}
1206
1207/*
1208 * call-seq:
1209 * close -> nil
1210 *
1211 * Closes the stream in +self+, if it is open, and returns +nil+;
1212 * ignored if +self+ is already closed:
1213 *
1214 * dir = Dir.new('example')
1215 * dir.read # => "."
1216 * dir.close # => nil
1217 * dir.close # => nil
1218 * dir.read # Raises IOError.
1219 *
1220 */
1221static VALUE
1222dir_close(VALUE dir)
1223{
1224 struct dir_data *dirp;
1225
1226 dirp = dir_get(dir);
1227 if (!dirp->dir) return Qnil;
1228 close_dir_data(dirp);
1229
1230 return Qnil;
1231}
1232
1233static void *
1234nogvl_chdir(void *ptr)
1235{
1236 const char *path = ptr;
1237
1238 return (void *)(VALUE)chdir(path);
1239}
1240
1241static void
1242dir_chdir0(VALUE path)
1243{
1244 if (IO_WITHOUT_GVL_INT(nogvl_chdir, (void*)RSTRING_PTR(path)) < 0)
1245 rb_sys_fail_path(path);
1246}
1247
1248static struct {
1249 VALUE thread;
1250 VALUE path;
1251 int line;
1252 int blocking;
1253} chdir_lock = {
1254 .blocking = 0, .thread = Qnil,
1255 .path = Qnil, .line = 0,
1256};
1257
1258static void
1259chdir_enter(void)
1260{
1261 if (chdir_lock.blocking == 0) {
1262 chdir_lock.path = rb_source_location(&chdir_lock.line);
1263 }
1264 chdir_lock.blocking++;
1265 if (NIL_P(chdir_lock.thread)) {
1266 chdir_lock.thread = rb_thread_current();
1267 }
1268}
1269
1270static void
1271chdir_leave(void)
1272{
1273 chdir_lock.blocking--;
1274 if (chdir_lock.blocking == 0) {
1275 chdir_lock.thread = Qnil;
1276 chdir_lock.path = Qnil;
1277 chdir_lock.line = 0;
1278 }
1279}
1280
1281static int
1282chdir_alone_block_p(void)
1283{
1284 int block_given = rb_block_given_p();
1285 if (chdir_lock.blocking > 0) {
1286 if (rb_thread_current() != chdir_lock.thread)
1287 rb_raise(rb_eRuntimeError, "conflicting chdir during another chdir block");
1288 if (!block_given) {
1289 if (!NIL_P(chdir_lock.path)) {
1290 rb_warn("conflicting chdir during another chdir block\n"
1291 "%" PRIsVALUE ":%d: note: previous chdir was here",
1292 chdir_lock.path, chdir_lock.line);
1293 }
1294 else {
1295 rb_warn("conflicting chdir during another chdir block");
1296 }
1297 }
1298 }
1299 return block_given;
1301
1302struct chdir_data {
1303 VALUE old_path, new_path;
1304 int done;
1305 bool yield_path;
1306};
1307
1308static VALUE
1309chdir_yield(VALUE v)
1310{
1311 struct chdir_data *args = (void *)v;
1312 dir_chdir0(args->new_path);
1313 args->done = TRUE;
1314 chdir_enter();
1315 return args->yield_path ? rb_yield(args->new_path) : rb_yield_values2(0, NULL);
1316}
1317
1318static VALUE
1319chdir_restore(VALUE v)
1320{
1321 struct chdir_data *args = (void *)v;
1322 if (args->done) {
1323 chdir_leave();
1324 dir_chdir0(args->old_path);
1325 }
1326 return Qnil;
1327}
1328
1329static VALUE
1330chdir_path(VALUE path, bool yield_path)
1331{
1332 if (chdir_alone_block_p()) {
1333 struct chdir_data args;
1334
1335 args.old_path = rb_str_encode_ospath(rb_dir_getwd());
1336 args.new_path = path;
1337 args.done = FALSE;
1338 args.yield_path = yield_path;
1339 return rb_ensure(chdir_yield, (VALUE)&args, chdir_restore, (VALUE)&args);
1340 }
1341 else {
1342 char *p = RSTRING_PTR(path);
1343 int r = IO_WITHOUT_GVL_INT(nogvl_chdir, p);
1344 if (r < 0)
1345 rb_sys_fail_path(path);
1346 }
1347
1348 return INT2FIX(0);
1349}
1350
1351/*
1352 * call-seq:
1353 * Dir.chdir(new_dirpath) -> 0
1354 * Dir.chdir -> 0
1355 * Dir.chdir(new_dirpath) {|new_dirpath| ... } -> object
1356 * Dir.chdir {|cur_dirpath| ... } -> object
1357 *
1358 * Changes the current working directory.
1359 *
1360 * With argument +new_dirpath+ and no block,
1361 * changes to the given +dirpath+:
1362 *
1363 * Dir.pwd # => "/example"
1364 * Dir.chdir('..') # => 0
1365 * Dir.pwd # => "/"
1366 *
1367 * With no argument and no block:
1368 *
1369 * - Changes to the value of environment variable +HOME+ if defined.
1370 * - Otherwise changes to the value of environment variable +LOGDIR+ if defined.
1371 * - Otherwise makes no change.
1372 *
1373 * With argument +new_dirpath+ and a block, temporarily changes the working directory:
1374 *
1375 * - Calls the block with the argument.
1376 * - Changes to the given directory.
1377 * - Executes the block (yielding the new path).
1378 * - Restores the previous working directory.
1379 * - Returns the block's return value.
1380 *
1381 * Example:
1382 *
1383 * Dir.chdir('/var/spool/mail')
1384 * Dir.pwd # => "/var/spool/mail"
1385 * Dir.chdir('/tmp') do
1386 * Dir.pwd # => "/tmp"
1387 * end
1388 * Dir.pwd # => "/var/spool/mail"
1389 *
1390 * With no argument and a block,
1391 * calls the block with the current working directory (string)
1392 * and returns the block's return value.
1393 *
1394 * Calls to \Dir.chdir with blocks may be nested:
1395 *
1396 * Dir.chdir('/var/spool/mail')
1397 * Dir.pwd # => "/var/spool/mail"
1398 * Dir.chdir('/tmp') do
1399 * Dir.pwd # => "/tmp"
1400 * Dir.chdir('/usr') do
1401 * Dir.pwd # => "/usr"
1402 * end
1403 * Dir.pwd # => "/tmp"
1404 * end
1405 * Dir.pwd # => "/var/spool/mail"
1406 *
1407 * In a multi-threaded program an error is raised if a thread attempts
1408 * to open a +chdir+ block while another thread has one open,
1409 * or a call to +chdir+ without a block occurs inside
1410 * a block passed to +chdir+ (even in the same thread).
1411 *
1412 * Raises an exception if the target directory does not exist.
1413 */
1414static VALUE
1415dir_s_chdir(int argc, VALUE *argv, VALUE obj)
1416{
1417 VALUE path = Qnil;
1418
1419 if (rb_check_arity(argc, 0, 1) == 1) {
1420 path = rb_str_encode_ospath(rb_get_path(argv[0]));
1421 }
1422 else {
1423 const char *dist = getenv("HOME");
1424 if (!dist) {
1425 dist = getenv("LOGDIR");
1426 if (!dist) rb_raise(rb_eArgError, "HOME/LOGDIR not set");
1427 }
1428 path = rb_str_new2(dist);
1429 }
1430
1431 return chdir_path(path, true);
1432}
1433
1434#if defined(HAVE_FCHDIR) && defined(HAVE_DIRFD) && HAVE_FCHDIR && HAVE_DIRFD
1435static void *
1436nogvl_fchdir(void *ptr)
1437{
1438 const int *fd = ptr;
1439
1440 return (void *)(VALUE)fchdir(*fd);
1441}
1442
1443static void
1444dir_fchdir(int fd)
1445{
1446 if (IO_WITHOUT_GVL_INT(nogvl_fchdir, (void *)&fd) < 0)
1447 rb_sys_fail("fchdir");
1448}
1449
1450struct fchdir_data {
1451 VALUE old_dir;
1452 int fd;
1453 int done;
1454};
1455
1456static VALUE
1457fchdir_yield(VALUE v)
1458{
1459 struct fchdir_data *args = (void *)v;
1460 dir_fchdir(args->fd);
1461 args->done = TRUE;
1462 chdir_enter();
1463 return rb_yield_values(0);
1464}
1465
1466static VALUE
1467fchdir_restore(VALUE v)
1468{
1469 struct fchdir_data *args = (void *)v;
1470 if (args->done) {
1471 chdir_leave();
1472 dir_fchdir(RB_NUM2INT(dir_fileno(args->old_dir)));
1473 }
1474 dir_close(args->old_dir);
1475 return Qnil;
1476}
1477
1478/*
1479 * call-seq:
1480 * Dir.fchdir(fd) -> 0
1481 * Dir.fchdir(fd) { ... } -> object
1482 *
1483 * Changes the current working directory to the directory
1484 * specified by the integer file descriptor +fd+.
1485 *
1486 * When passing a file descriptor over a UNIX socket or to a child process,
1487 * using +fchdir+ instead of +chdir+ avoids the
1488 * {time-of-check to time-of-use vulnerability}[https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use]
1489 *
1490 * With no block, changes to the directory given by +fd+:
1491 *
1492 * Dir.chdir('/var/spool/mail')
1493 * Dir.pwd # => "/var/spool/mail"
1494 * dir = Dir.new('/usr')
1495 * fd = dir.fileno
1496 * Dir.fchdir(fd)
1497 * Dir.pwd # => "/usr"
1498 *
1499 * With a block, temporarily changes the working directory:
1500 *
1501 * - Calls the block with the argument.
1502 * - Changes to the given directory.
1503 * - Executes the block (yields no args).
1504 * - Restores the previous working directory.
1505 * - Returns the block's return value.
1506 *
1507 * Example:
1508 *
1509 * Dir.chdir('/var/spool/mail')
1510 * Dir.pwd # => "/var/spool/mail"
1511 * dir = Dir.new('/tmp')
1512 * fd = dir.fileno
1513 * Dir.fchdir(fd) do
1514 * Dir.pwd # => "/tmp"
1515 * end
1516 * Dir.pwd # => "/var/spool/mail"
1517 *
1518 * This method uses the
1519 * {fchdir()}[https://www.man7.org/linux/man-pages/man3/fchdir.3p.html]
1520 * function defined by POSIX 2008;
1521 * the method is not implemented on non-POSIX platforms (raises NotImplementedError).
1522 *
1523 * Raises an exception if the file descriptor is not valid.
1524 *
1525 * In a multi-threaded program an error is raised if a thread attempts
1526 * to open a +chdir+ block while another thread has one open,
1527 * or a call to +chdir+ without a block occurs inside
1528 * a block passed to +chdir+ (even in the same thread).
1529 */
1530static VALUE
1531dir_s_fchdir(VALUE klass, VALUE fd_value)
1532{
1533 int fd = RB_NUM2INT(fd_value);
1534
1535 if (chdir_alone_block_p()) {
1536 struct fchdir_data args;
1537 args.old_dir = dir_s_alloc(klass);
1538 dir_initialize(NULL, args.old_dir, rb_fstring_cstr("."), Qnil);
1539 args.fd = fd;
1540 args.done = FALSE;
1541 return rb_ensure(fchdir_yield, (VALUE)&args, fchdir_restore, (VALUE)&args);
1542 }
1543 else {
1544 int r = IO_WITHOUT_GVL_INT(nogvl_fchdir, &fd);
1545 if (r < 0)
1546 rb_sys_fail("fchdir");
1547 }
1548
1549 return INT2FIX(0);
1550}
1551#else
1552#define dir_s_fchdir rb_f_notimplement
1553#endif
1554
1555/*
1556 * call-seq:
1557 * chdir -> 0
1558 * chdir { ... } -> object
1559 *
1560 * Changes the current working directory to +self+:
1561 *
1562 * Dir.pwd # => "/"
1563 * dir = Dir.new('example')
1564 * dir.chdir
1565 * Dir.pwd # => "/example"
1566 *
1567 * With a block, temporarily changes the working directory:
1568 *
1569 * - Calls the block.
1570 * - Changes to the given directory.
1571 * - Executes the block (yields no args).
1572 * - Restores the previous working directory.
1573 * - Returns the block's return value.
1574 *
1575 * Uses Dir.fchdir if available, and Dir.chdir if not, see those
1576 * methods for caveats.
1577 */
1578static VALUE
1579dir_chdir(VALUE dir)
1580{
1581#if defined(HAVE_FCHDIR) && defined(HAVE_DIRFD) && HAVE_FCHDIR && HAVE_DIRFD
1582 return dir_s_fchdir(rb_cDir, dir_fileno(dir));
1583#else
1584 return chdir_path(dir_get(dir)->path, false);
1585#endif
1586}
1587
1588static VALUE last_cwd;
1589
1590#ifndef _WIN32
1591static VALUE
1592getcwd_to_str(VALUE arg)
1593{
1594 const char *path = (const char *)arg;
1595#ifdef __APPLE__
1596 return rb_str_normalize_ospath(path, strlen(path));
1597#else
1598 return rb_str_new2(path);
1599#endif
1600}
1601
1602static VALUE
1603getcwd_xfree(VALUE arg)
1604{
1605 xfree((void *)arg);
1606 return Qnil;
1607}
1608
1609static VALUE
1610rb_dir_getwd_ospath_slowpath(void)
1611{
1612 char *path = ruby_getcwd();
1613 return rb_ensure(getcwd_to_str, (VALUE)path, getcwd_xfree, (VALUE)path);
1614}
1615
1616VALUE
1617rb_dir_getwd_ospath(void)
1618{
1619 char buf[PATH_MAX];
1620 char *path = getcwd(buf, PATH_MAX);
1621 if (!path) {
1622 return rb_dir_getwd_ospath_slowpath();
1623 }
1624
1625 VALUE cached_cwd = RUBY_ATOMIC_VALUE_LOAD(last_cwd);
1626
1627 if (!cached_cwd || strcmp(RSTRING_PTR(cached_cwd), path) != 0) {
1628#ifdef __APPLE__
1629 cached_cwd = rb_str_normalize_ospath(path, strlen(path));
1630#else
1631 cached_cwd = rb_str_new2(path);
1632#endif
1633 rb_str_freeze(cached_cwd);
1634 RUBY_ATOMIC_VALUE_SET(last_cwd, cached_cwd);
1635 }
1636 return cached_cwd;
1637}
1638#endif
1640VALUE
1641rb_dir_getwd(void)
1642{
1643 rb_encoding *fs = rb_filesystem_encoding();
1644 int fsenc = rb_enc_to_index(fs);
1645 VALUE cwd = rb_str_new_shared(rb_dir_getwd_ospath());
1646
1647 switch (fsenc) {
1648 case ENCINDEX_US_ASCII:
1649 fsenc = ENCINDEX_ASCII_8BIT;
1650 case ENCINDEX_ASCII_8BIT:
1651 break;
1652#if defined _WIN32 || defined __APPLE__
1653 default:
1654 return rb_str_conv_enc(cwd, NULL, fs);
1655#endif
1656 }
1657 return rb_enc_associate_index(cwd, fsenc);
1658}
1659
1660/*
1661 * call-seq:
1662 * Dir.pwd -> string
1663 *
1664 * Returns the path to the current working directory:
1665 *
1666 * Dir.chdir("/tmp") # => 0
1667 * Dir.pwd # => "/tmp"
1668 *
1669 */
1670static VALUE
1671dir_s_getwd(VALUE dir)
1672{
1673 return rb_dir_getwd();
1674}
1675
1676static VALUE
1677check_dirname(VALUE dir)
1678{
1679 VALUE d = dir;
1680 char *path, *pend;
1681 long len;
1682 rb_encoding *enc;
1683
1684 FilePathValue(d);
1685 enc = rb_enc_get(d);
1686 RSTRING_GETMEM(d, path, len);
1687 pend = path + len;
1688 pend = rb_enc_path_end(rb_enc_path_skip_prefix(path, pend, enc), pend, enc);
1689 if (pend - path < len) {
1690 d = rb_str_subseq(d, 0, pend - path);
1691 StringValueCStr(d);
1692 }
1693 return rb_str_encode_ospath(d);
1694}
1695
1696#if defined(HAVE_CHROOT)
1697static void *
1698nogvl_chroot(void *dirname)
1699{
1700 return (void *)(VALUE)chroot((const char *)dirname);
1701}
1702
1703/*
1704 * call-seq:
1705 * Dir.chroot(dirpath) -> 0
1706 *
1707 * Changes the root directory of the calling process to that specified in +dirpath+.
1708 * The new root directory is used for pathnames beginning with <tt>'/'</tt>.
1709 * The root directory is inherited by all children of the calling process.
1710 *
1711 * Only a privileged process may call +chroot+.
1712 *
1713 * See {Linux chroot}[https://man7.org/linux/man-pages/man2/chroot.2.html].
1714 */
1715static VALUE
1716dir_s_chroot(VALUE dir, VALUE path)
1717{
1718 path = check_dirname(path);
1719 if (IO_WITHOUT_GVL_INT(nogvl_chroot, (void *)RSTRING_PTR(path)) == -1)
1720 rb_sys_fail_path(path);
1721
1722 return INT2FIX(0);
1723}
1724#else
1725#define dir_s_chroot rb_f_notimplement
1726#endif
1727
1728struct mkdir_arg {
1729 const char *path;
1730 mode_t mode;
1731};
1732
1733static void *
1734nogvl_mkdir(void *ptr)
1735{
1736 struct mkdir_arg *m = ptr;
1737
1738 return (void *)(VALUE)mkdir(m->path, m->mode);
1739}
1740
1741/*
1742 * call-seq:
1743 * Dir.mkdir(dirpath, permissions = 0775) -> 0
1744 *
1745 * Creates a directory in the underlying file system
1746 * at +dirpath+ with the given +permissions+;
1747 * see {File Permissions}[rdoc-ref:File@File+Permissions]:
1748 *
1749 * Dir.mkdir('foo')
1750 * File.stat(Dir.new('foo')).mode.to_s(8) # => "40775"
1751 * Dir.mkdir('bar', 0644)
1752 * File.stat(Dir.new('bar')).mode.to_s(8) # => "40644"
1753 * Dir.rmdir('foo')
1754 * Dir.rmdir('bar')
1755 *
1756 * Argument +permissions+ is ignored on Windows.
1757 */
1758static VALUE
1759dir_s_mkdir(int argc, VALUE *argv, VALUE obj)
1760{
1761 struct mkdir_arg m;
1762 VALUE path, vmode;
1763 int r;
1764
1765 if (rb_scan_args(argc, argv, "11", &path, &vmode) == 2) {
1766 m.mode = NUM2MODET(vmode);
1767 }
1768 else {
1769 m.mode = 0777;
1770 }
1771
1772 path = check_dirname(path);
1773 m.path = RSTRING_PTR(path);
1774 r = IO_WITHOUT_GVL_INT(nogvl_mkdir, &m);
1775 if (r < 0)
1776 rb_sys_fail_path(path);
1777
1778 return INT2FIX(0);
1779}
1780
1781static void *
1782nogvl_rmdir(void *ptr)
1783{
1784 const char *path = ptr;
1785
1786 return (void *)(VALUE)rmdir(path);
1787}
1788
1789/*
1790 * call-seq:
1791 * Dir.rmdir(dirpath) -> 0
1792 *
1793 * Removes the directory at +dirpath+ from the underlying file system:
1794 *
1795 * Dir.rmdir('foo') # => 0
1796 *
1797 * Raises an exception if the directory is not empty.
1798 */
1799static VALUE
1800dir_s_rmdir(VALUE obj, VALUE dir)
1801{
1802 const char *p;
1803 int r;
1804
1805 dir = check_dirname(dir);
1806 p = RSTRING_PTR(dir);
1807 r = IO_WITHOUT_GVL_INT(nogvl_rmdir, (void *)p);
1808 if (r < 0)
1809 rb_sys_fail_path(dir);
1810
1811 return INT2FIX(0);
1813
1814struct warning_args {
1815#ifdef RUBY_FUNCTION_NAME_STRING
1816 const char *func;
1817#endif
1818 const char *mesg;
1819 rb_encoding *enc;
1820};
1821
1822#ifndef RUBY_FUNCTION_NAME_STRING
1823#define sys_enc_warning_in(func, mesg, enc) sys_enc_warning(mesg, enc)
1824#endif
1825
1826static VALUE
1827sys_warning_1(VALUE mesg)
1828{
1829 const struct warning_args *arg = (struct warning_args *)mesg;
1830#ifdef RUBY_FUNCTION_NAME_STRING
1831 rb_sys_enc_warning(arg->enc, "%s: %s", arg->func, arg->mesg);
1832#else
1833 rb_sys_enc_warning(arg->enc, "%s", arg->mesg);
1834#endif
1835 return Qnil;
1836}
1837
1838static void
1839sys_enc_warning_in(const char *func, const char *mesg, rb_encoding *enc)
1840{
1841 struct warning_args arg;
1842#ifdef RUBY_FUNCTION_NAME_STRING
1843 arg.func = func;
1844#endif
1845 arg.mesg = mesg;
1846 arg.enc = enc;
1847 rb_protect(sys_warning_1, (VALUE)&arg, 0);
1848}
1849
1850#define GLOB_VERBOSE (1U << (sizeof(int) * CHAR_BIT - 1))
1851#define sys_warning(val, enc) \
1852 ((flags & GLOB_VERBOSE) ? sys_enc_warning_in(RUBY_FUNCTION_NAME_STRING, (val), (enc)) :(void)0)
1853
1854static inline size_t
1855glob_alloc_size(size_t x, size_t y)
1856{
1857 size_t z;
1858 if (rb_mul_size_overflow(x, y, SSIZE_MAX, &z)) {
1859 rb_memerror(); /* or...? */
1860 }
1861 else {
1862 return z;
1863 }
1864}
1865
1866static inline void *
1867glob_alloc_n(size_t x, size_t y)
1868{
1869 return malloc(glob_alloc_size(x, y));
1870}
1871
1872static inline void *
1873glob_realloc_n(void *p, size_t x, size_t y)
1874{
1875 return realloc(p, glob_alloc_size(x, y));
1876}
1877
1878#define GLOB_ALLOC(type) ((type *)malloc(sizeof(type)))
1879#define GLOB_ALLOC_N(type, n) ((type *)glob_alloc_n(sizeof(type), n))
1880#define GLOB_REALLOC(ptr, size) realloc((ptr), (size))
1881#define GLOB_REALLOC_N(ptr, n) glob_realloc_n(ptr, sizeof(*(ptr)), n)
1882#define GLOB_FREE(ptr) free(ptr)
1883#define GLOB_JUMP_TAG(status) (((status) == -1) ? rb_memerror() : rb_jump_tag(status))
1884
1885/*
1886 * ENOTDIR can be returned by stat(2) if a non-leaf element of the path
1887 * is not a directory.
1888 */
1889ALWAYS_INLINE(static int to_be_ignored(int e));
1890static inline int
1891to_be_ignored(int e)
1892{
1893 return e == ENOENT || e == ENOTDIR;
1894}
1895
1896#ifdef _WIN32
1897#define STAT(args) (int)(VALUE)nogvl_stat(&(args))
1898#define LSTAT(args) (int)(VALUE)nogvl_lstat(&(args))
1899#else
1900#define STAT(args) IO_WITHOUT_GVL_INT(nogvl_stat, (void *)&(args))
1901#define LSTAT(args) IO_WITHOUT_GVL_INT(nogvl_lstat, (void *)&(args))
1902#endif
1904typedef int ruby_glob_errfunc(const char*, VALUE, const void*, int);
1905typedef struct {
1906 ruby_glob_func *match;
1907 ruby_glob_errfunc *error;
1909
1910static const char *
1911at_subpath(int fd, size_t baselen, const char *path)
1912{
1913#if USE_OPENDIR_AT
1914 if (fd != (int)AT_FDCWD && baselen > 0) {
1915 path += baselen;
1916 if (*path == '/') ++path;
1917 }
1918#endif
1919 return *path ? path : ".";
1920}
1921
1922#if USE_OPENDIR_AT
1923struct fstatat_args {
1924 int fd;
1925 int flag;
1926 const char *path;
1927 struct stat *pst;
1928};
1929
1930static void *
1931nogvl_fstatat(void *args)
1932{
1933 struct fstatat_args *arg = (struct fstatat_args *)args;
1934 return (void *)(VALUE)fstatat(arg->fd, arg->path, arg->pst, arg->flag);
1936#else
1937struct stat_args {
1938 const char *path;
1939 struct stat *pst;
1940};
1941
1942static void *
1943nogvl_stat(void *args)
1944{
1945 struct stat_args *arg = (struct stat_args *)args;
1946 return (void *)(VALUE)stat(arg->path, arg->pst);
1947}
1948#endif
1949
1950/* System call with warning */
1951static int
1952do_stat(int fd, const char *path, struct stat *pst, int flags, rb_encoding *enc)
1953{
1954#if USE_OPENDIR_AT
1955 struct fstatat_args args;
1956 args.fd = fd;
1957 args.path = path;
1958 args.pst = pst;
1959 args.flag = 0;
1960 int ret = IO_WITHOUT_GVL_INT(nogvl_fstatat, (void *)&args);
1961#else
1962 struct stat_args args;
1963 args.path = path;
1964 args.pst = pst;
1965 int ret = STAT(args);
1966#endif
1967 if (ret < 0 && !to_be_ignored(errno))
1968 sys_warning(path, enc);
1969
1970 return ret;
1971}
1972
1973#if defined HAVE_LSTAT || defined lstat || USE_OPENDIR_AT
1974#if !USE_OPENDIR_AT
1975static void *
1976nogvl_lstat(void *args)
1977{
1978 struct stat_args *arg = (struct stat_args *)args;
1979 return (void *)(VALUE)lstat(arg->path, arg->pst);
1980}
1981#endif
1982
1983static int
1984do_lstat(int fd, const char *path, struct stat *pst, int flags, rb_encoding *enc)
1985{
1986#if USE_OPENDIR_AT
1987 struct fstatat_args args;
1988 args.fd = fd;
1989 args.path = path;
1990 args.pst = pst;
1991 args.flag = AT_SYMLINK_NOFOLLOW;
1992 int ret = IO_WITHOUT_GVL_INT(nogvl_fstatat, (void *)&args);
1993#else
1994 struct stat_args args;
1995 args.path = path;
1996 args.pst = pst;
1997 int ret = LSTAT(args);
1998#endif
1999 if (ret < 0 && !to_be_ignored(errno))
2000 sys_warning(path, enc);
2001
2002 return ret;
2003}
2004#else
2005#define do_lstat do_stat
2006#endif
2007
2008struct opendir_at_arg {
2009 int basefd;
2010 const char *path;
2011};
2012
2013static void *
2014with_gvl_gc_for_fd(void *ptr)
2015{
2016 int *e = ptr;
2017
2018 return (void *)RBOOL(rb_gc_for_fd(*e));
2019}
2020
2021static int
2022gc_for_fd_with_gvl(int e)
2023{
2024 if (vm_initialized)
2025 return (int)(VALUE)rb_thread_call_with_gvl(with_gvl_gc_for_fd, &e);
2026 else
2027 return RBOOL(rb_gc_for_fd(e));
2028}
2029
2030static void *
2031nogvl_opendir_at(void *ptr)
2032{
2033 const struct opendir_at_arg *oaa = ptr;
2034 DIR *dirp;
2035
2036#if USE_OPENDIR_AT
2037 const int opendir_flags = (O_RDONLY|O_CLOEXEC|
2038# ifdef O_DIRECTORY
2039 O_DIRECTORY|
2040# endif /* O_DIRECTORY */
2041 0);
2042 int fd = openat(oaa->basefd, oaa->path, opendir_flags);
2043
2044 dirp = fd >= 0 ? fdopendir(fd) : 0;
2045 if (!dirp) {
2046 int e = errno;
2047
2048 switch (gc_for_fd_with_gvl(e)) {
2049 default:
2050 if (fd < 0) fd = openat(oaa->basefd, oaa->path, opendir_flags);
2051 if (fd >= 0) dirp = fdopendir(fd);
2052 if (dirp) return dirp;
2053
2054 e = errno;
2055 /* fallthrough*/
2056 case 0:
2057 if (fd >= 0) close(fd);
2058 rb_errno_set(e);
2059 }
2060 }
2061#else /* !USE_OPENDIR_AT */
2062 dirp = opendir(oaa->path);
2063 if (!dirp && gc_for_fd_with_gvl(errno))
2064 dirp = opendir(oaa->path);
2065#endif /* !USE_OPENDIR_AT */
2066
2067 return dirp;
2068}
2069
2070static DIR *
2071opendir_at(int basefd, const char *path)
2072{
2073 struct opendir_at_arg oaa;
2074
2075 oaa.basefd = basefd;
2076 oaa.path = path;
2077
2078 if (vm_initialized)
2079 return IO_WITHOUT_GVL(nogvl_opendir_at, &oaa);
2080 else
2081 return nogvl_opendir_at(&oaa);
2082}
2083
2084static DIR *
2085do_opendir(const int basefd, size_t baselen, const char *path, int flags, rb_encoding *enc,
2086 ruby_glob_errfunc *errfunc, VALUE arg, int *status)
2087{
2088 DIR *dirp;
2089#ifdef _WIN32
2090 VALUE tmp = 0;
2091 if (!fundamental_encoding_p(enc)) {
2092 tmp = rb_enc_str_new(path, strlen(path), enc);
2093 tmp = rb_str_encode_ospath(tmp);
2094 path = RSTRING_PTR(tmp);
2095 }
2096#endif
2097 dirp = opendir_at(basefd, at_subpath(basefd, baselen, path));
2098 if (!dirp) {
2099 int e = errno;
2100
2101 *status = 0;
2102 if (!to_be_ignored(e)) {
2103 if (errfunc) {
2104 *status = (*errfunc)(path, arg, enc, e);
2105 }
2106 else {
2107 sys_warning(path, enc);
2108 }
2109 }
2110 }
2111#ifdef _WIN32
2112 if (tmp) rb_str_resize(tmp, 0); /* GC guard */
2113#endif
2114
2115 return dirp;
2116}
2117
2118/* Globing pattern */
2119enum glob_pattern_type { PLAIN, ALPHA, BRACE, MAGICAL, RECURSIVE, MATCH_ALL, MATCH_DIR };
2120
2121/* Return nonzero if S has any special globbing chars in it. */
2122static enum glob_pattern_type
2123has_magic(const char *p, const char *pend, int flags, rb_encoding *enc)
2124{
2125 const int escape = !(flags & FNM_NOESCAPE);
2126 int hasalpha = 0;
2127 int hasmagical = 0;
2128
2129 register char c;
2130
2131 while (p < pend && (c = *p++) != 0) {
2132 switch (c) {
2133 case '{':
2134 return BRACE;
2135
2136 case '*':
2137 case '?':
2138 case '[':
2139 hasmagical = 1;
2140 break;
2141
2142 case '\\':
2143 if (escape && p++ >= pend)
2144 continue;
2145 break;
2146
2147#ifdef _WIN32
2148 case '.':
2149 break;
2150
2151 case '~':
2152 hasalpha = 1;
2153 break;
2154#endif
2155 default:
2156 if (IS_WIN32 || ISALPHA(c)) {
2157 hasalpha = 1;
2158 }
2159 break;
2160 }
2161
2162 p = Next(p-1, pend, enc);
2163 }
2164
2165 return hasmagical ? MAGICAL : hasalpha ? ALPHA : PLAIN;
2166}
2167
2168/* Find separator in globbing pattern. */
2169static char *
2170find_dirsep(const char *p, const char *pend, int flags, rb_encoding *enc)
2171{
2172 const int escape = !(flags & FNM_NOESCAPE);
2173
2174 register char c;
2175 int open = 0;
2176
2177 while ((c = *p++) != 0) {
2178 switch (c) {
2179 case '[':
2180 open = 1;
2181 continue;
2182 case ']':
2183 open = 0;
2184 continue;
2185
2186 case '{':
2187 open = 1;
2188 continue;
2189 case '}':
2190 open = 0;
2191 continue;
2192
2193 case '/':
2194 if (!open)
2195 return (char *)p-1;
2196 continue;
2197
2198 case '\\':
2199 if (escape && !(c = *p++))
2200 return (char *)p-1;
2201 continue;
2202 }
2203
2204 p = Next(p-1, pend, enc);
2205 }
2206
2207 return (char *)p-1;
2208}
2209
2210/* Remove escaping backslashes */
2211static char *
2212remove_backslashes(char *p, register const char *pend, rb_encoding *enc)
2213{
2214 char *t = p;
2215 char *s = p;
2216
2217 while (*p) {
2218 if (*p == '\\') {
2219 if (t != s)
2220 memmove(t, s, p - s);
2221 t += p - s;
2222 s = ++p;
2223 if (!*p) break;
2224 }
2225 Inc(p, pend, enc);
2226 }
2227
2228 while (*p++);
2229
2230 if (t != s)
2231 memmove(t, s, p - s); /* move '\0' too */
2232
2233 return p;
2235
2236struct glob_pattern {
2237 char *str;
2238 enum glob_pattern_type type;
2239 struct glob_pattern *next;
2240};
2241
2242static void glob_free_pattern(struct glob_pattern *list);
2243
2244static struct glob_pattern *
2245glob_make_pattern(const char *p, const char *e, int flags, rb_encoding *enc)
2246{
2247 struct glob_pattern *list, *tmp, **tail = &list;
2248 int dirsep = 0; /* pattern is terminated with '/' */
2249 int recursive = 0;
2250
2251 while (p < e && *p) {
2252 tmp = GLOB_ALLOC(struct glob_pattern);
2253 if (!tmp) goto error;
2254 if (p + 2 < e && p[0] == '*' && p[1] == '*' && p[2] == '/') {
2255 /* fold continuous RECURSIVEs (needed in glob_helper) */
2256 do { p += 3; while (*p == '/') p++; } while (p[0] == '*' && p[1] == '*' && p[2] == '/');
2257 tmp->type = RECURSIVE;
2258 tmp->str = 0;
2259 dirsep = 1;
2260 recursive = 1;
2261 }
2262 else {
2263 const char *m = find_dirsep(p, e, flags, enc);
2264 const enum glob_pattern_type magic = has_magic(p, m, flags, enc);
2265 const enum glob_pattern_type non_magic = (USE_NAME_ON_FS || FNM_SYSCASE) ? PLAIN : ALPHA;
2266 char *buf;
2267
2268 if (!(FNM_SYSCASE || magic > non_magic) && !recursive && *m) {
2269 const char *m2;
2270 while (has_magic(m+1, m2 = find_dirsep(m+1, e, flags, enc), flags, enc) <= non_magic &&
2271 *m2) {
2272 m = m2;
2273 }
2274 }
2275 buf = GLOB_ALLOC_N(char, m-p+1);
2276 if (!buf) {
2277 GLOB_FREE(tmp);
2278 goto error;
2279 }
2280 memcpy(buf, p, m-p);
2281 buf[m-p] = '\0';
2282 tmp->type = magic > MAGICAL ? MAGICAL : magic > non_magic ? magic : PLAIN;
2283 tmp->str = buf;
2284 if (*m) {
2285 dirsep = 1;
2286 p = m + 1;
2287 }
2288 else {
2289 dirsep = 0;
2290 p = m;
2291 }
2292 }
2293 *tail = tmp;
2294 tail = &tmp->next;
2295 }
2296
2297 tmp = GLOB_ALLOC(struct glob_pattern);
2298 if (!tmp) {
2299 goto error;
2300 }
2301 tmp->type = dirsep ? MATCH_DIR : MATCH_ALL;
2302 tmp->str = 0;
2303 *tail = tmp;
2304 tmp->next = 0;
2305
2306 return list;
2307
2308 error:
2309 *tail = 0;
2310 glob_free_pattern(list);
2311 return 0;
2312}
2313
2314static void
2315glob_free_pattern(struct glob_pattern *list)
2316{
2317 while (list) {
2318 struct glob_pattern *tmp = list;
2319 list = list->next;
2320 if (tmp->str)
2321 GLOB_FREE(tmp->str);
2322 GLOB_FREE(tmp);
2323 }
2324}
2325
2326static char *
2327join_path(const char *path, size_t len, int dirsep, const char *name, size_t namlen)
2328{
2329 char *buf = GLOB_ALLOC_N(char, len+namlen+(dirsep?1:0)+1);
2330
2331 if (!buf) return 0;
2332 memcpy(buf, path, len);
2333 if (dirsep) {
2334 buf[len++] = '/';
2335 }
2336 memcpy(buf+len, name, namlen);
2337 buf[len+namlen] = '\0';
2338 return buf;
2339}
2340
2341#ifdef HAVE_GETATTRLIST
2342# if defined HAVE_FGETATTRLIST
2343# define is_case_sensitive(dirp, path) is_case_sensitive(dirp)
2344# else
2345# define is_case_sensitive(dirp, path) is_case_sensitive(path)
2346# endif
2347static int
2348is_case_sensitive(DIR *dirp, const char *path)
2349{
2350 struct {
2351 u_int32_t length;
2352 vol_capabilities_attr_t cap[1];
2353 } __attribute__((aligned(4), packed)) attrbuf[1];
2354 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, 0, ATTR_VOL_INFO|ATTR_VOL_CAPABILITIES};
2355 const vol_capabilities_attr_t *const cap = attrbuf[0].cap;
2356 const int idx = VOL_CAPABILITIES_FORMAT;
2357 const uint32_t mask = VOL_CAP_FMT_CASE_SENSITIVE;
2358 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, FSOPT_NOFOLLOW);
2359# if defined HAVE_FGETATTRLIST
2360 int ret = gvl_fgetattrlist(&args, dirfd(dirp));
2361# else
2362 int ret = gvl_getattrlist(&args, path);
2363# endif
2364 if (ret)
2365 return -1;
2366
2367 if (!(cap->valid[idx] & mask))
2368 return -1;
2369 return (cap->capabilities[idx] & mask) != 0;
2370}
2371
2372static char *
2373replace_real_basename(char *path, long base, rb_encoding *enc, int norm_p, int flags, rb_pathtype_t *type)
2374{
2375 struct {
2376 u_int32_t length;
2377 attrreference_t ref[1];
2378 fsobj_type_t objtype;
2379 char path[MAXPATHLEN * 3];
2380 } __attribute__((aligned(4), packed)) attrbuf[1];
2381 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_NAME|ATTR_CMN_OBJTYPE};
2382 const attrreference_t *const ar = attrbuf[0].ref;
2383 const char *name;
2384 long len;
2385 char *tmp;
2386 IF_NORMALIZE_UTF8PATH(VALUE utf8str = Qnil);
2387
2388 *type = path_noent;
2389 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, FSOPT_NOFOLLOW);
2390 if (gvl_getattrlist(&args, path)) {
2391 if (!to_be_ignored(errno))
2392 sys_warning(path, enc);
2393 return path;
2394 }
2395
2396 switch (attrbuf[0].objtype) {
2397 case VREG: *type = path_regular; break;
2398 case VDIR: *type = path_directory; break;
2399 case VLNK: *type = path_symlink; break;
2400 default: *type = path_exist; break;
2401 }
2402 name = (char *)ar + ar->attr_dataoffset;
2403 len = (long)ar->attr_length - 1;
2404 if (name + len > (char *)attrbuf + sizeof(attrbuf))
2405 return path;
2406
2407# if NORMALIZE_UTF8PATH
2408 if (norm_p && has_nonascii(name, len)) {
2409 if (!NIL_P(utf8str = rb_str_normalize_ospath(name, len))) {
2410 RSTRING_GETMEM(utf8str, name, len);
2411 }
2412 }
2413# endif
2414
2415 tmp = GLOB_REALLOC(path, base + len + 1);
2416 if (tmp) {
2417 path = tmp;
2418 memcpy(path + base, name, len);
2419 path[base + len] = '\0';
2420 }
2421 IF_NORMALIZE_UTF8PATH(if (!NIL_P(utf8str)) rb_str_resize(utf8str, 0));
2422 return path;
2423}
2424#elif defined _WIN32
2425VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc);
2426int rb_w32_reparse_symlink_p(const WCHAR *path);
2427
2428static char *
2429replace_real_basename(char *path, long base, rb_encoding *enc, int norm_p, int flags, rb_pathtype_t *type)
2430{
2431 char *plainname = path;
2432 volatile VALUE tmp = 0;
2433 WIN32_FIND_DATAW fd;
2434 WIN32_FILE_ATTRIBUTE_DATA fa;
2435 WCHAR *wplain;
2436 HANDLE h = INVALID_HANDLE_VALUE;
2437 long wlen;
2438 int e = 0;
2439 if (!fundamental_encoding_p(enc)) {
2440 tmp = rb_enc_str_new_cstr(plainname, enc);
2441 tmp = rb_str_encode_ospath(tmp);
2442 plainname = RSTRING_PTR(tmp);
2443 }
2444 wplain = rb_w32_mbstr_to_wstr(CP_UTF8, plainname, -1, &wlen);
2445 if (tmp) rb_str_resize(tmp, 0);
2446 if (!wplain) return path;
2447 if (GetFileAttributesExW(wplain, GetFileExInfoStandard, &fa)) {
2448 h = FindFirstFileW(wplain, &fd);
2449 e = rb_w32_map_errno(GetLastError());
2450 }
2451 if (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2452 if (!rb_w32_reparse_symlink_p(wplain))
2453 fa.dwFileAttributes &= ~FILE_ATTRIBUTE_REPARSE_POINT;
2454 }
2455 free(wplain);
2456 if (h == INVALID_HANDLE_VALUE) {
2457 *type = path_noent;
2458 if (e && !to_be_ignored(e)) {
2459 errno = e;
2460 sys_warning(path, enc);
2461 }
2462 return path;
2463 }
2464 FindClose(h);
2465 *type =
2466 (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ? path_symlink :
2467 (fa.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? path_directory :
2468 path_regular;
2469 if (tmp) {
2470 char *buf;
2471 tmp = rb_w32_conv_from_wchar(fd.cFileName, enc);
2472 wlen = RSTRING_LEN(tmp);
2473 buf = GLOB_REALLOC(path, base + wlen + 1);
2474 if (buf) {
2475 path = buf;
2476 memcpy(path + base, RSTRING_PTR(tmp), wlen);
2477 path[base + wlen] = 0;
2478 }
2479 rb_str_resize(tmp, 0);
2480 }
2481 else {
2482 char *utf8filename;
2483 wlen = WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, NULL, 0, NULL, NULL);
2484 utf8filename = GLOB_REALLOC(0, wlen);
2485 if (utf8filename) {
2486 char *buf;
2487 WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, utf8filename, wlen, NULL, NULL);
2488 buf = GLOB_REALLOC(path, base + wlen + 1);
2489 if (buf) {
2490 path = buf;
2491 memcpy(path + base, utf8filename, wlen);
2492 path[base + wlen] = 0;
2493 }
2494 GLOB_FREE(utf8filename);
2495 }
2496 }
2497 return path;
2498}
2499#elif USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2500# error not implemented
2501#endif
2502
2503#ifndef S_ISDIR
2504# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
2505#endif
2506
2507#ifndef S_ISLNK
2508# ifndef S_IFLNK
2509# define S_ISLNK(m) (0)
2510# else
2511# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
2512# endif
2513#endif
2514
2515struct glob_args {
2516 void (*func)(const char *, VALUE, void *);
2517 const char *path;
2518 const char *base;
2519 size_t baselen;
2520 VALUE value;
2521 rb_encoding *enc;
2522};
2523
2524#define glob_call_func(func, path, arg, enc) (*(func))((path), (arg), (void *)(enc))
2525
2526static VALUE
2527glob_func_caller(VALUE val)
2528{
2529 struct glob_args *args = (struct glob_args *)val;
2530
2531 glob_call_func(args->func, args->path, args->value, args->enc);
2532 return Qnil;
2534
2535struct glob_error_args {
2536 const char *path;
2537 rb_encoding *enc;
2538 int error;
2539};
2540
2541static VALUE
2542glob_func_warning(VALUE val)
2543{
2544 struct glob_error_args *arg = (struct glob_error_args *)val;
2545 rb_syserr_enc_warning(arg->error, arg->enc, "%s", arg->path);
2546 return Qnil;
2547}
2548
2549#if 0
2550static int
2551rb_glob_warning(const char *path, VALUE a, const void *enc, int error)
2552{
2553 int status;
2554 struct glob_error_args args;
2555
2556 args.path = path;
2557 args.enc = enc;
2558 args.error = error;
2559 rb_protect(glob_func_warning, (VALUE)&args, &status);
2560 return status;
2561}
2562#endif
2563
2564NORETURN(static VALUE glob_func_error(VALUE val));
2565
2566static VALUE
2567glob_func_error(VALUE val)
2568{
2569 struct glob_error_args *arg = (struct glob_error_args *)val;
2570 VALUE path = rb_enc_str_new_cstr(arg->path, arg->enc);
2571 rb_syserr_fail_str(arg->error, path);
2573}
2574
2575static int
2576rb_glob_error(const char *path, VALUE a, const void *enc, int error)
2577{
2578 int status;
2579 struct glob_error_args args;
2580 VALUE (*errfunc)(VALUE) = glob_func_error;
2581
2582 switch (error) {
2583 case EACCES:
2584#ifdef ENOTCAPABLE
2585 case ENOTCAPABLE:
2586#endif
2587 errfunc = glob_func_warning;
2588 }
2589 args.path = path;
2590 args.enc = enc;
2591 args.error = error;
2592 rb_protect(errfunc, (VALUE)&args, &status);
2593 return status;
2595
2596typedef struct rb_dirent {
2597 long d_namlen;
2598 const char *d_name;
2599#ifdef _WIN32
2600 const char *d_altname;
2601#endif
2602 uint8_t d_type;
2603} rb_dirent_t;
2604
2605static inline int
2606dirent_match(const char *pat, rb_encoding *enc, const char *name, const rb_dirent_t *dp, int flags)
2607{
2608 if (fnmatch(pat, enc, name, flags) == 0) return 1;
2609#ifdef _WIN32
2610 if (dp->d_altname && (flags & FNM_SHORTNAME)) {
2611 if (fnmatch(pat, enc, dp->d_altname, flags) == 0) return 1;
2612 }
2613#endif
2614 return 0;
2616
2617struct push_glob_args {
2618 int fd;
2619 const char *path;
2620 size_t baselen;
2621 size_t namelen;
2622 int dirsep; /* '/' should be placed before appending child entry's name to 'path'. */
2623 rb_pathtype_t pathtype; /* type of 'path' */
2624 int flags;
2625 const ruby_glob_funcs_t *funcs;
2626 VALUE arg;
2628
2629struct dirent_brace_args {
2630 const char *name;
2631 const rb_dirent_t *dp;
2632 int flags;
2633};
2634
2635static int
2636dirent_match_brace(const char *pattern, VALUE val, void *enc)
2637{
2638 struct dirent_brace_args *arg = (struct dirent_brace_args *)val;
2639
2640 return dirent_match(pattern, enc, arg->name, arg->dp, arg->flags);
2641}
2642
2643/* join paths from pattern list of glob_make_pattern() */
2644static char*
2645join_path_from_pattern(struct glob_pattern **beg)
2646{
2647 struct glob_pattern *p;
2648 char *path = NULL;
2649 size_t path_len = 0;
2650
2651 for (p = *beg; p; p = p->next) {
2652 const char *str;
2653 switch (p->type) {
2654 case RECURSIVE:
2655 str = "**";
2656 break;
2657 case MATCH_DIR:
2658 /* append last slash */
2659 str = "";
2660 break;
2661 default:
2662 str = p->str;
2663 if (!str) continue;
2664 }
2665 if (!path) {
2666 path_len = strlen(str);
2667 path = GLOB_ALLOC_N(char, path_len + 1);
2668 if (path) {
2669 memcpy(path, str, path_len);
2670 path[path_len] = '\0';
2671 }
2672 }
2673 else {
2674 size_t len = strlen(str);
2675 char *tmp;
2676 tmp = GLOB_REALLOC(path, path_len + len + 2);
2677 if (tmp) {
2678 path = tmp;
2679 path[path_len++] = '/';
2680 memcpy(path + path_len, str, len);
2681 path_len += len;
2682 path[path_len] = '\0';
2683 }
2684 }
2685 }
2686 return path;
2687}
2688
2689static int push_caller(const char *path, VALUE val, void *enc);
2690
2691static int ruby_brace_expand(const char *str, int flags, ruby_glob_func *func, VALUE arg,
2692 rb_encoding *enc, VALUE var);
2693
2694static const size_t rb_dirent_name_offset =
2695 offsetof(rb_dirent_t, d_type) + sizeof(uint8_t);
2696
2697static rb_dirent_t *
2698dirent_copy(const struct dirent *dp, rb_dirent_t *rdp)
2699{
2700 if (!dp) return NULL;
2701 size_t namlen = NAMLEN(dp);
2702 const size_t altlen =
2703#ifdef _WIN32
2704 dp->d_altlen ? dp->d_altlen + 1 :
2705#endif
2706 0;
2707 rb_dirent_t *newrdp = rdp;
2708 if (!rdp && !(newrdp = malloc(rb_dirent_name_offset + namlen + 1 + altlen)))
2709 return NULL;
2710 newrdp->d_namlen = namlen;
2711 if (!rdp) {
2712 char *name = (char *)newrdp + rb_dirent_name_offset;
2713 memcpy(name, dp->d_name, namlen);
2714 name[namlen] = '\0';
2715#ifdef _WIN32
2716 newrdp->d_altname = NULL;
2717 if (altlen) {
2718 char *const altname = name + namlen + 1;
2719 memcpy(altname, dp->d_altname, altlen - 1);
2720 altname[altlen - 1] = '\0';
2721 newrdp->d_altname = altname;
2722 }
2723#endif
2724 newrdp->d_name = name;
2725 }
2726 else {
2727 newrdp->d_name = dp->d_name;
2728#ifdef _WIN32
2729 newrdp->d_altname = dp->d_altname;
2730#endif
2731 }
2732#if !EMULATE_IFTODT
2733 newrdp->d_type = dp->d_type;
2734#else
2735 newrdp->d_type = 0;
2736#endif
2737 return newrdp;
2739
2740typedef union {
2741 struct {
2742 DIR *dirp;
2743 rb_dirent_t ent;
2744 } nosort;
2745 struct {
2746 size_t count, idx;
2747 rb_dirent_t **entries;
2748 } sort;
2750
2751static int
2752glob_sort_cmp(const void *a, const void *b, void *e)
2753{
2754 const rb_dirent_t *ent1 = *(void **)a;
2755 const rb_dirent_t *ent2 = *(void **)b;
2756 return strcmp(ent1->d_name, ent2->d_name);
2757}
2758
2759static void
2760glob_dir_finish(ruby_glob_entries_t *ent, int flags)
2761{
2762 if (flags & FNM_GLOB_NOSORT) {
2763 check_closedir(ent->nosort.dirp);
2764 ent->nosort.dirp = NULL;
2765 }
2766 else if (ent->sort.entries) {
2767 for (size_t i = 0, count = ent->sort.count; i < count;) {
2768 GLOB_FREE(ent->sort.entries[i++]);
2769 }
2770 GLOB_FREE(ent->sort.entries);
2771 ent->sort.entries = NULL;
2772 ent->sort.count = ent->sort.idx = 0;
2773 }
2774}
2775
2776static ruby_glob_entries_t *
2777glob_opendir(ruby_glob_entries_t *ent, DIR *dirp, int flags, rb_encoding *enc)
2778{
2780 if (flags & FNM_GLOB_NOSORT) {
2781 ent->nosort.dirp = dirp;
2782 return ent;
2783 }
2784 else {
2785 void *newp;
2786 struct dirent *dp;
2787 size_t count = 0, capacity = 0;
2788 ent->sort.count = 0;
2789 ent->sort.idx = 0;
2790 ent->sort.entries = 0;
2791#ifdef _WIN32
2792 if ((capacity = dirp->nfiles) > 0) {
2793 if (!(newp = GLOB_ALLOC_N(rb_dirent_t, capacity))) {
2794 check_closedir(dirp);
2795 return NULL;
2796 }
2797 ent->sort.entries = newp;
2798 }
2799#endif
2800 while ((dp = READDIR(dirp, enc)) != NULL) {
2801 rb_dirent_t *rdp = dirent_copy(dp, NULL);
2802 if (!rdp) {
2803 goto nomem;
2804 }
2805 if (count >= capacity) {
2806 capacity += 256;
2807 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, capacity))) {
2808 GLOB_FREE(rdp);
2809 goto nomem;
2810 }
2811 ent->sort.entries = newp;
2812 }
2813 ent->sort.entries[count++] = rdp;
2814 ent->sort.count = count;
2815 }
2816 check_closedir(dirp);
2817 if (count < capacity) {
2818 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, count))) {
2819 glob_dir_finish(ent, 0);
2820 return NULL;
2821 }
2822 ent->sort.entries = newp;
2823 }
2824 ruby_qsort(ent->sort.entries, ent->sort.count, sizeof(ent->sort.entries[0]),
2825 glob_sort_cmp, NULL);
2826 return ent;
2827 }
2828
2829 nomem:
2830 glob_dir_finish(ent, 0);
2831 check_closedir(dirp);
2832 return NULL;
2833}
2834
2835static rb_dirent_t *
2836glob_getent(ruby_glob_entries_t *ent, int flags, rb_encoding *enc)
2837{
2838 if (flags & FNM_GLOB_NOSORT) {
2839 return dirent_copy(READDIR(ent->nosort.dirp, enc), &ent->nosort.ent);
2840 }
2841 else if (ent->sort.idx < ent->sort.count) {
2842 return ent->sort.entries[ent->sort.idx++];
2843 }
2844 else {
2845 return NULL;
2846 }
2847}
2848
2849static int
2850glob_helper(
2851 int fd,
2852 const char *path,
2853 size_t baselen,
2854 size_t namelen,
2855 int dirsep, /* '/' should be placed before appending child entry's name to 'path'. */
2856 rb_pathtype_t pathtype, /* type of 'path' */
2857 struct glob_pattern **beg,
2858 struct glob_pattern **end,
2859 int flags,
2860 const ruby_glob_funcs_t *funcs,
2861 VALUE arg,
2862 rb_encoding *enc)
2863{
2864 struct stat st;
2865 int status = 0;
2866 struct glob_pattern **cur, **new_beg, **new_end;
2867 int plain = 0, brace = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0;
2868 int escape = !(flags & FNM_NOESCAPE);
2869 size_t pathlen = baselen + namelen;
2870
2871 rb_check_stack_overflow();
2872
2873 for (cur = beg; cur < end; ++cur) {
2874 struct glob_pattern *p = *cur;
2875 if (p->type == RECURSIVE) {
2876 recursive = 1;
2877 p = p->next;
2878 }
2879 switch (p->type) {
2880 case PLAIN:
2881 plain = 1;
2882 break;
2883 case ALPHA:
2884#if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2885 plain = 1;
2886#else
2887 magical = 1;
2888#endif
2889 break;
2890 case BRACE:
2891 if (!recursive || strchr(p->str, '/')) {
2892 brace = 1;
2893 }
2894 break;
2895 case MAGICAL:
2896 magical = 2;
2897 break;
2898 case MATCH_ALL:
2899 match_all = 1;
2900 break;
2901 case MATCH_DIR:
2902 match_dir = 1;
2903 break;
2904 case RECURSIVE:
2905 rb_bug("continuous RECURSIVEs");
2906 }
2907 }
2908
2909 if (brace) {
2910 struct push_glob_args args;
2911 char* brace_path = join_path_from_pattern(beg);
2912 if (!brace_path) return -1;
2913 args.fd = fd;
2914 args.path = path;
2915 args.baselen = baselen;
2916 args.namelen = namelen;
2917 args.dirsep = dirsep;
2918 args.pathtype = pathtype;
2919 args.flags = flags;
2920 args.funcs = funcs;
2921 args.arg = arg;
2922 status = ruby_brace_expand(brace_path, flags, push_caller, (VALUE)&args, enc, Qfalse);
2923 GLOB_FREE(brace_path);
2924 return status;
2925 }
2926
2927 if (*path) {
2928 if (match_all && pathtype == path_unknown) {
2929 if (do_lstat(fd, path, &st, flags, enc) == 0) {
2930 pathtype = IFTODT(st.st_mode);
2931 }
2932 else {
2933 pathtype = path_noent;
2934 }
2935 }
2936 if (match_dir && (pathtype == path_unknown || pathtype == path_symlink)) {
2937 if (do_stat(fd, path, &st, flags, enc) == 0) {
2938 pathtype = IFTODT(st.st_mode);
2939 }
2940 else {
2941 pathtype = path_noent;
2942 }
2943 }
2944 if (match_all && pathtype > path_noent) {
2945 const char *subpath = path + baselen + (baselen && path[baselen] == '/');
2946 status = glob_call_func(funcs->match, subpath, arg, enc);
2947 if (status) return status;
2948 }
2949 if (match_dir && pathtype == path_directory) {
2950 int seplen = (baselen && path[baselen] == '/');
2951 const char *subpath = path + baselen + seplen;
2952 char *tmp = join_path(subpath, namelen - seplen, dirsep, "", 0);
2953 if (!tmp) return -1;
2954 status = glob_call_func(funcs->match, tmp, arg, enc);
2955 GLOB_FREE(tmp);
2956 if (status) return status;
2957 }
2958 }
2959
2960 if (pathtype == path_noent) return 0;
2961
2962 if (magical || recursive) {
2963 rb_dirent_t *dp;
2964 DIR *dirp;
2965# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2966 char *plainname = 0;
2967# endif
2968 IF_NORMALIZE_UTF8PATH(int norm_p);
2969# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
2970 if (cur + 1 == end && (*cur)->type <= ALPHA) {
2971 plainname = join_path(path, pathlen, dirsep, (*cur)->str, strlen((*cur)->str));
2972 if (!plainname) return -1;
2973 dirp = do_opendir(fd, basename, plainname, flags, enc, funcs->error, arg, &status);
2974 GLOB_FREE(plainname);
2975 }
2976 else
2977# else
2978 ;
2979# endif
2980 dirp = do_opendir(fd, baselen, path, flags, enc, funcs->error, arg, &status);
2981 if (dirp == NULL) {
2982# if FNM_SYSCASE || NORMALIZE_UTF8PATH
2983 if ((magical < 2) && !recursive && (errno == EACCES)) {
2984 /* no read permission, fallback */
2985 goto literally;
2986 }
2987# endif
2988 return status;
2989 }
2990 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *path ? path : "."));
2991
2992# if NORMALIZE_UTF8PATH
2993 if (!(norm_p || magical || recursive)) {
2994 check_closedir(dirp);
2995 goto literally;
2996 }
2997# endif
2998# ifdef HAVE_GETATTRLIST
2999 if (is_case_sensitive(dirp, path) == 0)
3000 flags |= FNM_CASEFOLD;
3001# endif
3002 ruby_glob_entries_t globent;
3003 if (!glob_opendir(&globent, dirp, flags, enc)) {
3004 status = 0;
3005 if (funcs->error) {
3006 status = (*funcs->error)(path, arg, enc, ENOMEM);
3007 }
3008 else {
3009 sys_warning(path, enc);
3010 }
3011 return status;
3012 }
3013
3014 int skipdot = (flags & FNM_GLOB_SKIPDOT);
3015 flags |= FNM_GLOB_SKIPDOT;
3016
3017 while ((dp = glob_getent(&globent, flags, enc)) != NULL) {
3018 char *buf;
3019 rb_pathtype_t new_pathtype = path_unknown;
3020 const char *name;
3021 size_t namlen;
3022 int dotfile = 0;
3023 IF_NORMALIZE_UTF8PATH(VALUE utf8str = Qnil);
3024
3025 name = dp->d_name;
3026 namlen = dp->d_namlen;
3027 if (name[0] == '.') {
3028 ++dotfile;
3029 if (namlen == 1) {
3030 /* unless DOTMATCH, skip current directories not to recurse infinitely */
3031 if (recursive && !(flags & FNM_DOTMATCH)) continue;
3032 if (skipdot) continue;
3033 ++dotfile;
3034 new_pathtype = path_directory; /* force to skip stat/lstat */
3035 }
3036 else if (namlen == 2 && name[1] == '.') {
3037 /* always skip parent directories not to recurse infinitely */
3038 continue;
3039 }
3040 }
3041
3042# if NORMALIZE_UTF8PATH
3043 if (norm_p && has_nonascii(name, namlen)) {
3044 if (!NIL_P(utf8str = rb_str_normalize_ospath(name, namlen))) {
3045 RSTRING_GETMEM(utf8str, name, namlen);
3046 }
3047 }
3048# endif
3049 buf = join_path(path, pathlen, dirsep, name, namlen);
3050 IF_NORMALIZE_UTF8PATH(if (!NIL_P(utf8str)) rb_str_resize(utf8str, 0));
3051 if (!buf) {
3052 status = -1;
3053 break;
3054 }
3055 name = buf + pathlen + (dirsep != 0);
3056#if !EMULATE_IFTODT
3057 if (dp->d_type != DT_UNKNOWN) {
3058 /* Got it. We need no more lstat. */
3059 new_pathtype = dp->d_type;
3060 }
3061#endif
3062 if (recursive && dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1) &&
3063 new_pathtype == path_unknown) {
3064 /* RECURSIVE never match dot files unless FNM_DOTMATCH is set */
3065 if (do_lstat(fd, buf, &st, flags, enc) == 0)
3066 new_pathtype = IFTODT(st.st_mode);
3067 else
3068 new_pathtype = path_noent;
3069 }
3070
3071 new_beg = new_end = GLOB_ALLOC_N(struct glob_pattern *, (end - beg) * 2);
3072 if (!new_beg) {
3073 GLOB_FREE(buf);
3074 status = -1;
3075 break;
3076 }
3077
3078 for (cur = beg; cur < end; ++cur) {
3079 struct glob_pattern *p = *cur;
3080 struct dirent_brace_args args;
3081 if (p->type == RECURSIVE) {
3082 if (new_pathtype == path_directory || /* not symlink but real directory */
3083 new_pathtype == path_exist) {
3084 if (dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1))
3085 *new_end++ = p; /* append recursive pattern */
3086 }
3087 p = p->next; /* 0 times recursion */
3088 }
3089 switch (p->type) {
3090 case BRACE:
3091 args.name = name;
3092 args.dp = dp;
3093 args.flags = flags;
3094 if (ruby_brace_expand(p->str, flags, dirent_match_brace,
3095 (VALUE)&args, enc, Qfalse) > 0)
3096 *new_end++ = p->next;
3097 break;
3098 case ALPHA:
3099# if USE_NAME_ON_FS == USE_NAME_ON_FS_BY_FNMATCH
3100 if (plainname) {
3101 *new_end++ = p->next;
3102 break;
3103 }
3104# endif
3105 case PLAIN:
3106 case MAGICAL:
3107 if (dirent_match(p->str, enc, name, dp, flags))
3108 *new_end++ = p->next;
3109 default:
3110 break;
3111 }
3112 }
3113
3114 status = glob_helper(fd, buf, baselen, name - buf - baselen + namlen, 1,
3115 new_pathtype, new_beg, new_end,
3116 flags, funcs, arg, enc);
3117 GLOB_FREE(buf);
3118 GLOB_FREE(new_beg);
3119 if (status) break;
3120 }
3121
3122 glob_dir_finish(&globent, flags);
3123 }
3124 else if (plain) {
3125 struct glob_pattern **copy_beg, **copy_end, **cur2;
3126
3127# if FNM_SYSCASE || NORMALIZE_UTF8PATH
3128 literally:
3129# endif
3130 copy_beg = copy_end = GLOB_ALLOC_N(struct glob_pattern *, end - beg);
3131 if (!copy_beg) return -1;
3132 for (cur = beg; cur < end; ++cur)
3133 *copy_end++ = (*cur)->type <= ALPHA ? *cur : 0;
3134
3135 for (cur = copy_beg; cur < copy_end; ++cur) {
3136 if (*cur) {
3137 rb_pathtype_t new_pathtype = path_unknown;
3138 char *buf;
3139 char *name;
3140 size_t len = strlen((*cur)->str) + 1;
3141 name = GLOB_ALLOC_N(char, len);
3142 if (!name) {
3143 status = -1;
3144 break;
3145 }
3146 memcpy(name, (*cur)->str, len);
3147 if (escape)
3148 len = remove_backslashes(name, name+len-1, enc) - name;
3149
3150 new_beg = new_end = GLOB_ALLOC_N(struct glob_pattern *, end - beg);
3151 if (!new_beg) {
3152 GLOB_FREE(name);
3153 status = -1;
3154 break;
3155 }
3156 *new_end++ = (*cur)->next;
3157 for (cur2 = cur + 1; cur2 < copy_end; ++cur2) {
3158 if (*cur2 && fnmatch((*cur2)->str, enc, name, flags) == 0) {
3159 *new_end++ = (*cur2)->next;
3160 *cur2 = 0;
3161 }
3162 }
3163
3164 buf = join_path(path, pathlen, dirsep, name, len);
3165 GLOB_FREE(name);
3166 if (!buf) {
3167 GLOB_FREE(new_beg);
3168 status = -1;
3169 break;
3170 }
3171#if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
3172 if ((*cur)->type == ALPHA) {
3173 buf = replace_real_basename(buf, pathlen + (dirsep != 0), enc,
3174 IF_NORMALIZE_UTF8PATH(1)+0,
3175 flags, &new_pathtype);
3176 if (!buf) break;
3177 }
3178#endif
3179 status = glob_helper(fd, buf, baselen,
3180 namelen + strlen(buf + pathlen), 1,
3181 new_pathtype, new_beg, new_end,
3182 flags, funcs, arg, enc);
3183 GLOB_FREE(buf);
3184 GLOB_FREE(new_beg);
3185 if (status) break;
3186 }
3187 }
3188
3189 GLOB_FREE(copy_beg);
3190 }
3191
3192 return status;
3193}
3194
3195static int
3196push_caller(const char *path, VALUE val, void *enc)
3197{
3198 struct push_glob_args *arg = (struct push_glob_args *)val;
3199 struct glob_pattern *list;
3200 int status;
3201
3202 list = glob_make_pattern(path, path + strlen(path), arg->flags, enc);
3203 if (!list) {
3204 return -1;
3205 }
3206 status = glob_helper(arg->fd, arg->path, arg->baselen, arg->namelen, arg->dirsep,
3207 arg->pathtype, &list, &list + 1, arg->flags, arg->funcs,
3208 arg->arg, enc);
3209 glob_free_pattern(list);
3210 return status;
3211}
3212
3213static int ruby_glob0(const char *path, int fd, const char *base, int flags,
3214 const ruby_glob_funcs_t *funcs, VALUE arg, rb_encoding *enc);
3215
3216struct push_glob0_args {
3217 int fd;
3218 const char *base;
3219 int flags;
3220 const ruby_glob_funcs_t *funcs;
3221 VALUE arg;
3222};
3223
3224static int
3225push_glob0_caller(const char *path, VALUE val, void *enc)
3226{
3227 struct push_glob0_args *arg = (struct push_glob0_args *)val;
3228 return ruby_glob0(path, arg->fd, arg->base, arg->flags, arg->funcs, arg->arg, enc);
3229}
3230
3231static int
3232ruby_glob0(const char *path, int fd, const char *base, int flags,
3233 const ruby_glob_funcs_t *funcs, VALUE arg,
3234 rb_encoding *enc)
3235{
3236 struct glob_pattern *list;
3237 const char *root, *start;
3238 char *buf;
3239 size_t n, baselen = 0;
3240 int status, dirsep = FALSE;
3241
3242 start = root = path;
3243
3244 if (*root == '{') {
3245 struct push_glob0_args args;
3246 args.fd = fd;
3247 args.base = base;
3248 args.flags = flags;
3249 args.funcs = funcs;
3250 args.arg = arg;
3251 return ruby_brace_expand(path, flags, push_glob0_caller, (VALUE)&args, enc, Qfalse);
3252 }
3253
3254 flags |= FNM_SYSCASE;
3255#if defined DOSISH
3256 root = rb_enc_path_skip_prefix(root, root + strlen(root), enc);
3257#endif
3258
3259 if (*root == '/') root++;
3260
3261 n = root - start;
3262 if (!n && base) {
3263 n = strlen(base);
3264 baselen = n;
3265 start = base;
3266 dirsep = TRUE;
3267 }
3268 buf = GLOB_ALLOC_N(char, n + 1);
3269 if (!buf) return -1;
3270 MEMCPY(buf, start, char, n);
3271 buf[n] = '\0';
3272
3273 list = glob_make_pattern(root, root + strlen(root), flags, enc);
3274 if (!list) {
3275 GLOB_FREE(buf);
3276 return -1;
3277 }
3278 status = glob_helper(fd, buf, baselen, n-baselen, dirsep,
3279 path_unknown, &list, &list + 1,
3280 flags, funcs, arg, enc);
3281 glob_free_pattern(list);
3282 GLOB_FREE(buf);
3283
3284 return status;
3285}
3287int
3288ruby_glob(const char *path, int flags, ruby_glob_func *func, VALUE arg)
3289{
3290 ruby_glob_funcs_t funcs;
3291 funcs.match = func;
3292 funcs.error = 0;
3293 return ruby_glob0(path, AT_FDCWD, 0, flags & ~GLOB_VERBOSE,
3294 &funcs, arg, rb_ascii8bit_encoding());
3295}
3296
3297static int
3298rb_glob_caller(const char *path, VALUE a, void *enc)
3299{
3300 int status;
3301 struct glob_args *args = (struct glob_args *)a;
3302
3303 args->path = path;
3304 rb_protect(glob_func_caller, a, &status);
3305 return status;
3306}
3307
3308static const ruby_glob_funcs_t rb_glob_funcs = {
3309 rb_glob_caller, rb_glob_error,
3310};
3311
3312void
3313rb_glob(const char *path, void (*func)(const char *, VALUE, void *), VALUE arg)
3314{
3315 struct glob_args args;
3316 int status;
3317
3318 args.func = func;
3319 args.value = arg;
3320 args.enc = rb_ascii8bit_encoding();
3321
3322 status = ruby_glob0(path, AT_FDCWD, 0, GLOB_VERBOSE, &rb_glob_funcs,
3323 (VALUE)&args, args.enc);
3324 if (status) GLOB_JUMP_TAG(status);
3325}
3326
3327static void
3328push_pattern(const char *path, VALUE ary, void *enc)
3329{
3330#if defined _WIN32 || defined __APPLE__
3331 VALUE name = rb_utf8_str_new_cstr(path);
3332 rb_encoding *eenc = rb_default_internal_encoding();
3333 name = rb_str_conv_enc(name, NULL, eenc ? eenc : enc);
3334#else
3335 VALUE name = rb_external_str_new_with_enc(path, strlen(path), enc);
3336#endif
3337 rb_ary_push(ary, name);
3338}
3339
3340static int
3341ruby_brace_expand(const char *str, int flags, ruby_glob_func *func, VALUE arg,
3342 rb_encoding *enc, VALUE var)
3343{
3344 const int escape = !(flags & FNM_NOESCAPE);
3345 const char *p = str;
3346 const char *pend = p + strlen(p);
3347 const char *s = p;
3348 const char *lbrace = 0, *rbrace = 0;
3349 int nest = 0, status = 0;
3350
3351 while (*p) {
3352 if (*p == '{' && nest++ == 0) {
3353 lbrace = p;
3354 }
3355 if (*p == '}' && lbrace && --nest == 0) {
3356 rbrace = p;
3357 break;
3358 }
3359 if (*p == '\\' && escape) {
3360 if (!*++p) break;
3361 }
3362 Inc(p, pend, enc);
3363 }
3364
3365 if (lbrace && rbrace) {
3366 size_t len = strlen(s) + 1;
3367 char *buf = GLOB_ALLOC_N(char, len);
3368 long shift;
3369
3370 if (!buf) return -1;
3371 memcpy(buf, s, lbrace-s);
3372 shift = (lbrace-s);
3373 p = lbrace;
3374 while (p < rbrace) {
3375 const char *t = ++p;
3376 nest = 0;
3377 while (p < rbrace && !(*p == ',' && nest == 0)) {
3378 if (*p == '{') nest++;
3379 if (*p == '}') nest--;
3380 if (*p == '\\' && escape) {
3381 if (++p == rbrace) break;
3382 }
3383 Inc(p, pend, enc);
3384 }
3385 memcpy(buf+shift, t, p-t);
3386 strlcpy(buf+shift+(p-t), rbrace+1, len-(shift+(p-t)));
3387 status = ruby_brace_expand(buf, flags, func, arg, enc, var);
3388 if (status) break;
3389 }
3390 GLOB_FREE(buf);
3391 }
3392 else if (!lbrace && !rbrace) {
3393 status = glob_call_func(func, s, arg, enc);
3394 }
3395
3396 RB_GC_GUARD(var);
3397 return status;
3399
3400struct brace_args {
3401 ruby_glob_funcs_t funcs;
3402 VALUE value;
3403 int flags;
3404};
3405
3406static int
3407glob_brace(const char *path, VALUE val, void *enc)
3408{
3409 struct brace_args *arg = (struct brace_args *)val;
3410
3411 return ruby_glob0(path, AT_FDCWD, 0, arg->flags, &arg->funcs, arg->value, enc);
3412}
3413
3414int
3415ruby_brace_glob_with_enc(const char *str, int flags, ruby_glob_func *func, VALUE arg, rb_encoding *enc)
3416{
3417 struct brace_args args;
3418
3419 flags &= ~GLOB_VERBOSE;
3420 args.funcs.match = func;
3421 args.funcs.error = 0;
3422 args.value = arg;
3423 args.flags = flags;
3424 return ruby_brace_expand(str, flags, glob_brace, (VALUE)&args, enc, Qfalse);
3425}
3427int
3428ruby_brace_glob(const char *str, int flags, ruby_glob_func *func, VALUE arg)
3429{
3430 return ruby_brace_glob_with_enc(str, flags, func, arg, rb_ascii8bit_encoding());
3431}
3432
3433static int
3434push_glob(VALUE ary, VALUE str, VALUE base, int flags)
3435{
3436 struct glob_args args;
3437 int fd;
3438 rb_encoding *enc = rb_enc_get(str);
3439
3440#if defined _WIN32 || defined __APPLE__
3441 str = rb_str_encode_ospath(str);
3442#endif
3443 if (rb_enc_to_index(enc) == ENCINDEX_US_ASCII)
3444 enc = rb_filesystem_encoding();
3445 if (rb_enc_to_index(enc) == ENCINDEX_US_ASCII)
3446 enc = rb_ascii8bit_encoding();
3447 flags |= GLOB_VERBOSE;
3448 args.func = push_pattern;
3449 args.value = ary;
3450 args.enc = enc;
3451 args.base = 0;
3452 fd = AT_FDCWD;
3453 if (!NIL_P(base)) {
3454 if (!RB_TYPE_P(base, T_STRING) || !rb_enc_check(str, base)) {
3455 struct dir_data *dirp = RTYPEDDATA_GET_DATA(base);
3456 if (!dirp->dir) dir_closed();
3457#ifdef HAVE_DIRFD
3458 if ((fd = dirfd(dirp->dir)) == -1)
3459 rb_sys_fail_path(dir_inspect(base));
3460#endif
3461 base = dirp->path;
3462 }
3463 args.base = RSTRING_PTR(base);
3464 }
3465#if defined _WIN32 || defined __APPLE__
3466 enc = rb_utf8_encoding();
3467#endif
3468
3469 return ruby_glob0(RSTRING_PTR(str), fd, args.base, flags, &rb_glob_funcs,
3470 (VALUE)&args, enc);
3471}
3472
3473static VALUE
3474rb_push_glob(VALUE str, VALUE base, int flags) /* '\0' is delimiter */
3475{
3476 VALUE ary;
3477 int status;
3478
3479 /* can contain null bytes as separators */
3480 if (!RB_TYPE_P(str, T_STRING)) {
3481 FilePathValue(str);
3482 }
3483 else if (!rb_str_to_cstr(str)) {
3484 rb_raise(rb_eArgError, "nul-separated glob pattern is deprecated");
3485 }
3486 else {
3487 rb_enc_check(str, rb_enc_from_encoding(rb_usascii_encoding()));
3488 }
3489 ary = rb_ary_new();
3490
3491 status = push_glob(ary, str, base, flags);
3492 if (status) GLOB_JUMP_TAG(status);
3493
3494 return ary;
3495}
3496
3497static VALUE
3498dir_globs(VALUE args, VALUE base, int flags)
3499{
3500 VALUE ary = rb_ary_new();
3501 long i;
3502
3503 for (i = 0; i < RARRAY_LEN(args); ++i) {
3504 int status;
3505 VALUE str = RARRAY_AREF(args, i);
3506 FilePathValue(str);
3507 status = push_glob(ary, str, base, flags);
3508 if (status) GLOB_JUMP_TAG(status);
3509 }
3510 RB_GC_GUARD(args);
3511
3512 return ary;
3513}
3514
3515static VALUE
3516dir_glob_option_base(VALUE base)
3517{
3518 if (NIL_OR_UNDEF_P(base)) {
3519 return Qnil;
3520 }
3521#if USE_OPENDIR_AT
3522 if (rb_typeddata_is_kind_of(base, &dir_data_type)) {
3523 return base;
3524 }
3525#endif
3526 FilePathValue(base);
3527 if (!RSTRING_LEN(base)) return Qnil;
3528 return base;
3529}
3530
3531static int
3532dir_glob_option_sort(VALUE sort)
3533{
3534 return (rb_bool_expected(sort, "sort", TRUE) ? 0 : FNM_GLOB_NOSORT);
3535}
3536
3537static VALUE
3538dir_s_aref(rb_execution_context_t *ec, VALUE obj, VALUE args, VALUE base, VALUE sort)
3539{
3540 const int flags = dir_glob_option_sort(sort);
3541 base = dir_glob_option_base(base);
3542 if (RARRAY_LEN(args) == 1) {
3543 return rb_push_glob(RARRAY_AREF(args, 0), base, flags);
3544 }
3545 return dir_globs(args, base, flags);
3546}
3547
3548static VALUE
3549dir_s_glob(rb_execution_context_t *ec, VALUE obj, VALUE str, VALUE rflags, VALUE base, VALUE sort)
3550{
3551 VALUE ary = rb_check_array_type(str);
3552 const int flags = (NUM2INT(rflags) | dir_glob_option_sort(sort)) & ~FNM_CASEFOLD;
3553 base = dir_glob_option_base(base);
3554 if (NIL_P(ary)) {
3555 ary = rb_push_glob(str, base, flags);
3556 }
3557 else {
3558 ary = dir_globs(ary, base, flags);
3559 }
3560
3561 if (rb_block_given_p()) {
3562 rb_ary_each(ary);
3563 return Qnil;
3564 }
3565 return ary;
3566}
3567
3568static VALUE
3569dir_open_dir(int argc, VALUE *argv)
3570{
3571 VALUE dir = rb_funcallv_kw(rb_cDir, rb_intern("open"), argc, argv, RB_PASS_CALLED_KEYWORDS);
3572
3573 rb_check_typeddata(dir, &dir_data_type);
3574 return dir;
3575}
3576
3577
3578/*
3579 * call-seq:
3580 * Dir.foreach(dirpath, encoding: 'UTF-8') {|entry_name| ... } -> nil
3581 *
3582 * Calls the block with each entry name in the directory at +dirpath+;
3583 * sets the given encoding onto each passed +entry_name+:
3584 *
3585 * Dir.foreach('/example') {|entry_name| p entry_name }
3586 *
3587 * Output:
3588 *
3589 * "config.h"
3590 * "lib"
3591 * "main.rb"
3592 * ".."
3593 * "."
3594 *
3595 * Encoding:
3596 *
3597 * Dir.foreach('/example') {|entry_name| p entry_name.encoding; break }
3598 * Dir.foreach('/example', encoding: 'US-ASCII') {|entry_name| p entry_name.encoding; break }
3599 *
3600 * Output:
3601 *
3602 * #<Encoding:UTF-8>
3603 * #<Encoding:US-ASCII>
3604 *
3605 * See {String Encoding}[rdoc-ref:encodings.rdoc@String+Encoding].
3606 *
3607 * Returns an enumerator if no block is given.
3608 */
3609static VALUE
3610dir_foreach(int argc, VALUE *argv, VALUE io)
3611{
3612 VALUE dir;
3613
3614 RETURN_ENUMERATOR(io, argc, argv);
3615 dir = dir_open_dir(argc, argv);
3616 rb_ensure(dir_each, dir, dir_close, dir);
3617 return Qnil;
3618}
3619
3620static VALUE
3621dir_entry_ary_push(VALUE ary, VALUE entry, struct dir_entry_args *_unused)
3622{
3623 return rb_ary_push(ary, entry);
3624}
3625
3626static VALUE
3627dir_collect(VALUE dir)
3628{
3629 VALUE ary = rb_ary_new();
3630 dir_each_entry(dir, dir_entry_ary_push, ary, FALSE);
3631 return ary;
3632}
3633
3634/*
3635 * call-seq:
3636 * Dir.entries(dirname, encoding: 'UTF-8') -> array
3637 *
3638 * Returns an array of the entry names in the directory at +dirpath+;
3639 * sets the given encoding onto each returned entry name:
3640 *
3641 * Dir.entries('/example') # => ["config.h", "lib", "main.rb", "..", "."]
3642 * Dir.entries('/example').first.encoding
3643 * # => #<Encoding:UTF-8>
3644 * Dir.entries('/example', encoding: 'US-ASCII').first.encoding
3645 * # => #<Encoding:US-ASCII>
3646 *
3647 * See {String Encoding}[rdoc-ref:encodings.rdoc@String+Encoding].
3648 *
3649 * Raises an exception if the directory does not exist.
3650 */
3651static VALUE
3652dir_entries(int argc, VALUE *argv, VALUE io)
3653{
3654 VALUE dir;
3655
3656 dir = dir_open_dir(argc, argv);
3657 return rb_ensure(dir_collect, dir, dir_close, dir);
3658}
3659
3660static VALUE
3661dir_each_child(VALUE dir)
3662{
3663 return dir_each_entry(dir, dir_yield, Qnil, TRUE);
3664}
3665
3666/*
3667 * call-seq:
3668 * Dir.each_child(dirpath) {|entry_name| ... } -> nil
3669 * Dir.each_child(dirpath, encoding: 'UTF-8') {|entry_name| ... } -> nil
3670 *
3671 * Like Dir.foreach, except that entries <tt>'.'</tt> and <tt>'..'</tt>
3672 * are not included.
3673 */
3674static VALUE
3675dir_s_each_child(int argc, VALUE *argv, VALUE io)
3676{
3677 VALUE dir;
3678
3679 RETURN_ENUMERATOR(io, argc, argv);
3680 dir = dir_open_dir(argc, argv);
3681 rb_ensure(dir_each_child, dir, dir_close, dir);
3682 return Qnil;
3683}
3684
3685/*
3686 * call-seq:
3687 * each_child {|entry_name| ... } -> self
3688 *
3689 * Calls the block with each entry name in +self+
3690 * except <tt>'.'</tt> and <tt>'..'</tt>:
3691 *
3692 * dir = Dir.new('/example')
3693 * dir.each_child {|entry_name| p entry_name }
3694 *
3695 * Output:
3696 *
3697 * "config.h"
3698 * "lib"
3699 * "main.rb"
3700 *
3701 * If no block is given, returns an enumerator.
3702 */
3703static VALUE
3704dir_each_child_m(VALUE dir)
3705{
3706 RETURN_ENUMERATOR(dir, 0, 0);
3707 return dir_each_entry(dir, dir_yield, Qnil, TRUE);
3708}
3709
3710/*
3711 * call-seq:
3712 * children -> array
3713 *
3714 * Returns an array of the entry names in +self+
3715 * except for <tt>'.'</tt> and <tt>'..'</tt>:
3716 *
3717 * dir = Dir.new('/example')
3718 * dir.children # => ["config.h", "lib", "main.rb"]
3719 *
3720 */
3721static VALUE
3722dir_collect_children(VALUE dir)
3723{
3724 VALUE ary = rb_ary_new();
3725 dir_each_entry(dir, dir_entry_ary_push, ary, TRUE);
3726 return ary;
3727}
3728
3729/*
3730 * call-seq:
3731 * children -> array
3732 *
3733 * Returns an array of the entry names in +self+ along with their type
3734 * except for <tt>'.'</tt> and <tt>'..'</tt>:
3735 *
3736 * dir = Dir.new('/example')
3737 * dir.scan # => [["config.h", :file], ["lib", :directory], ["main.rb", :file]]
3738 *
3739 */
3740static VALUE
3741dir_scan_children(VALUE dir)
3742{
3743 if (rb_block_given_p()) {
3744 dir_each_entry(dir, dir_yield_with_type, Qnil, TRUE);
3745 return Qnil;
3746 }
3747 else {
3748 VALUE ary = rb_ary_new();
3749 dir_each_entry(dir, dir_yield_with_type, ary, TRUE);
3750 return ary;
3751 }
3752}
3753
3754/*
3755 * call-seq:
3756 * Dir.children(dirpath) -> array
3757 * Dir.children(dirpath, encoding: 'UTF-8') -> array
3758 *
3759 * Returns an array of the entry names in the directory at +dirpath+
3760 * except for <tt>'.'</tt> and <tt>'..'</tt>;
3761 * sets the given encoding onto each returned entry name:
3762 *
3763 * Dir.children('/example') # => ["config.h", "lib", "main.rb"]
3764 * Dir.children('/example').first.encoding
3765 * # => #<Encoding:UTF-8>
3766 * Dir.children('/example', encoding: 'US-ASCII').first.encoding
3767 * # => #<Encoding:US-ASCII>
3768 *
3769 * See {String Encoding}[rdoc-ref:encodings.rdoc@String+Encoding].
3770 *
3771 * Raises an exception if the directory does not exist.
3772 */
3773static VALUE
3774dir_s_children(int argc, VALUE *argv, VALUE io)
3775{
3776 VALUE dir;
3777
3778 dir = dir_open_dir(argc, argv);
3779 return rb_ensure(dir_collect_children, dir, dir_close, dir);
3780}
3781
3782/*
3783 * call-seq:
3784 * Dir.scan(dirpath) {|entry_name, entry_type| ... } -> nil
3785 * Dir.scan(dirpath, encoding: 'UTF-8') {|entry_name, entry_type| ... } -> nil
3786 * Dir.scan(dirpath) -> [[entry_name, entry_type], ...]
3787 * Dir.scan(dirpath, encoding: 'UTF-8') -> [[entry_name, entry_type], ...]
3788 *
3789 * Yields or returns an array of the entry names in the directory at +dirpath+
3790 * associated with their type, except for <tt>'.'</tt> and <tt>'..'</tt>;
3791 * sets the given encoding onto each returned entry name.
3792 *
3793 * The type symbol is one of:
3794 * ``<code>:file</code>'', ``<code>:directory</code>'',
3795 * ``<code>:characterSpecial</code>'', ``<code>:blockSpecial</code>'',
3796 * ``<code>:fifo</code>'', ``<code>:link</code>'',
3797 * or ``<code>:socket</code>'':
3798 *
3799 * Dir.children('/example') # => [["config.h", :file], ["lib", :directory], ["main.rb", :file]]
3800 * Dir.children('/example').first.first.encoding
3801 * # => #<Encoding:UTF-8>
3802 * Dir.children('/example', encoding: 'US-ASCII').first.encoding
3803 * # => #<Encoding:US-ASCII>
3804 *
3805 * See {String Encoding}[rdoc-ref:encodings.rdoc@String+Encoding].
3806 *
3807 * Raises an exception if the directory does not exist.
3808 */
3809static VALUE
3810dir_s_scan(int argc, VALUE *argv, VALUE klass)
3811{
3812 VALUE dir = dir_open_dir(argc, argv);
3813 return rb_ensure(dir_scan_children, dir, dir_close, dir);
3814}
3815
3816static int
3817fnmatch_brace(const char *pattern, VALUE val, void *enc)
3818{
3819 struct brace_args *arg = (struct brace_args *)val;
3820 VALUE path = arg->value;
3821 rb_encoding *enc_pattern = enc;
3822 rb_encoding *enc_path = rb_enc_get(path);
3823
3824 if (enc_pattern != enc_path) {
3825 if (!rb_enc_asciicompat(enc_pattern))
3826 return FNM_NOMATCH;
3827 if (!rb_enc_asciicompat(enc_path))
3828 return FNM_NOMATCH;
3829 if (!rb_enc_str_asciionly_p(path)) {
3830 int cr = ENC_CODERANGE_7BIT;
3831 long len = strlen(pattern);
3832 if (rb_str_coderange_scan_restartable(pattern, pattern + len,
3833 enc_pattern, &cr) != len)
3834 return FNM_NOMATCH;
3835 if (cr != ENC_CODERANGE_7BIT)
3836 return FNM_NOMATCH;
3837 }
3838 }
3839 return (fnmatch(pattern, enc, RSTRING_PTR(path), arg->flags) == 0);
3840}
3841
3842/* :nodoc: */
3843static VALUE
3844file_s_fnmatch(int argc, VALUE *argv, VALUE obj)
3845{
3846 VALUE pattern, path;
3847 VALUE rflags;
3848 int flags;
3849
3850 if (rb_scan_args(argc, argv, "21", &pattern, &path, &rflags) == 3)
3851 flags = NUM2INT(rflags);
3852 else
3853 flags = 0;
3854
3855 StringValueCStr(pattern);
3856 FilePathStringValue(path);
3857
3858 if (flags & FNM_EXTGLOB) {
3859 struct brace_args args;
3860
3861 args.value = path;
3862 args.flags = flags;
3863 if (ruby_brace_expand(RSTRING_PTR(pattern), flags, fnmatch_brace,
3864 (VALUE)&args, rb_enc_get(pattern), pattern) > 0)
3865 return Qtrue;
3866 }
3867 else {
3868 rb_encoding *enc = rb_enc_compatible(pattern, path);
3869 if (!enc) return Qfalse;
3870 if (fnmatch(RSTRING_PTR(pattern), enc, RSTRING_PTR(path), flags) == 0)
3871 return Qtrue;
3872 }
3873 RB_GC_GUARD(pattern);
3874
3875 return Qfalse;
3876}
3877
3878/*
3879 * call-seq:
3880 * Dir.home(user_name = nil) -> dirpath
3881 *
3882 * Returns the home directory path of the user specified with +user_name+
3883 * if it is not +nil+, or the current login user:
3884 *
3885 * Dir.home # => "/home/me"
3886 * Dir.home('root') # => "/root"
3887 *
3888 * Raises ArgumentError if +user_name+ is not a user name.
3889 */
3890static VALUE
3891dir_s_home(int argc, VALUE *argv, VALUE obj)
3892{
3893 VALUE user;
3894 const char *u = 0;
3895
3896 rb_check_arity(argc, 0, 1);
3897 user = (argc > 0) ? argv[0] : Qnil;
3898 if (!NIL_P(user)) {
3899 StringValue(user);
3900 rb_must_asciicompat(user);
3901 u = StringValueCStr(user);
3902 if (*u) {
3903 return rb_home_dir_of(user, rb_str_new(0, 0));
3904 }
3905 }
3906 return rb_default_home_dir(rb_str_new(0, 0));
3907
3908}
3909
3910#if 0
3911/*
3912 * call-seq:
3913 * Dir.exist?(dirpath) -> true or false
3914 *
3915 * Returns whether +dirpath+ is a directory in the underlying file system:
3916 *
3917 * Dir.exist?('/example') # => true
3918 * Dir.exist?('/nosuch') # => false
3919 * Dir.exist?('/example/main.rb') # => false
3920 *
3921 * Same as File.directory?.
3922 *
3923 */
3924VALUE
3925rb_file_directory_p(void)
3926{
3927}
3928#endif
3929
3930static void *
3931nogvl_dir_empty_p(void *ptr)
3932{
3933 const char *path = ptr;
3934 DIR *dir = opendir(path);
3935 struct dirent *dp;
3936 VALUE result = Qtrue;
3937
3938 if (!dir) {
3939 int e = errno;
3940 switch (gc_for_fd_with_gvl(e)) {
3941 default:
3942 dir = opendir(path);
3943 if (dir) break;
3944 e = errno;
3945 /* fall through */
3946 case 0:
3947 if (e == ENOTDIR) return (void *)Qfalse;
3948 return (void *)INT2FIX(e);
3949 }
3950 }
3951 while ((dp = READDIR_NOGVL(dir, NULL)) != NULL) {
3952 if (!to_be_skipped(dp)) {
3953 result = Qfalse;
3954 break;
3955 }
3956 }
3957 check_closedir(dir);
3958 return (void *)result;
3959}
3960
3961/*
3962 * call-seq:
3963 * Dir.empty?(dirpath) -> true or false
3964 *
3965 * Returns whether +dirpath+ specifies an empty directory:
3966 *
3967 * dirpath = '/tmp/foo'
3968 * Dir.mkdir(dirpath)
3969 * Dir.empty?(dirpath) # => true
3970 * Dir.empty?('/example') # => false
3971 * Dir.empty?('/example/main.rb') # => false
3972 *
3973 * Raises an exception if +dirpath+ does not specify a directory or file
3974 * in the underlying file system.
3975 */
3976static VALUE
3977rb_dir_s_empty_p(VALUE obj, VALUE dirname)
3978{
3979 VALUE result, orig;
3980 const char *path;
3981 enum {false_on_notdir = 1};
3982
3983 FilePathValue(dirname);
3984 orig = rb_str_dup_frozen(dirname);
3985 dirname = rb_str_encode_ospath(dirname);
3986 dirname = rb_str_dup_frozen(dirname);
3987 path = RSTRING_PTR(dirname);
3988
3989#if defined HAVE_GETATTRLIST && defined ATTR_DIR_ENTRYCOUNT
3990 {
3991 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
3992 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
3993 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, 0);
3994 if (gvl_getattrlist(&args, path) != 0)
3995 rb_sys_fail_path(orig);
3996 if (*(const fsobj_tag_t *)(attrbuf+1) == VT_HFS) {
3997 al.commonattr = 0;
3998 al.dirattr = ATTR_DIR_ENTRYCOUNT;
3999 if (gvl_getattrlist(&args, path) == 0) {
4000 if (attrbuf[0] >= 2 * sizeof(u_int32_t))
4001 return RBOOL(attrbuf[1] == 0);
4002 if (false_on_notdir) return Qfalse;
4003 }
4004 rb_sys_fail_path(orig);
4005 }
4006 }
4007#endif
4008
4009 result = (VALUE)IO_WITHOUT_GVL(nogvl_dir_empty_p, (void *)path);
4010 if (FIXNUM_P(result)) {
4011 rb_syserr_fail_path((int)FIX2LONG(result), orig);
4012 }
4013 return result;
4014}
4015
4016void
4017Init_Dir(void)
4018{
4019 sym_directory = ID2SYM(rb_intern("directory"));
4020 sym_link = ID2SYM(rb_intern("link"));
4021 sym_file = ID2SYM(rb_intern("file"));
4022 sym_unknown = ID2SYM(rb_intern("unknown"));
4023
4024#if defined(DT_BLK) || defined(S_IFBLK)
4025 sym_block_device = ID2SYM(rb_intern("blockSpecial"));
4026#endif
4027#if defined(DT_CHR) || defined(S_IFCHR)
4028 sym_character_device = ID2SYM(rb_intern("characterSpecial"));
4029#endif
4030#if defined(DT_FIFO) || defined(S_IFIFO)
4031 sym_fifo = ID2SYM(rb_intern("fifo"));
4032#endif
4033#if defined(DT_SOCK) || defined(S_IFSOCK)
4034 sym_socket = ID2SYM(rb_intern("socket"));
4035#endif
4036
4037 rb_gc_register_address(&chdir_lock.path);
4038 rb_gc_register_address(&chdir_lock.thread);
4039 rb_gc_register_address(&last_cwd);
4040
4042
4044
4045 rb_define_alloc_func(rb_cDir, dir_s_alloc);
4046 rb_define_singleton_method(rb_cDir,"for_fd", dir_s_for_fd, 1);
4047 rb_define_singleton_method(rb_cDir, "foreach", dir_foreach, -1);
4048 rb_define_singleton_method(rb_cDir, "entries", dir_entries, -1);
4049 rb_define_singleton_method(rb_cDir, "each_child", dir_s_each_child, -1);
4050 rb_define_singleton_method(rb_cDir, "children", dir_s_children, -1);
4051 rb_define_singleton_method(rb_cDir, "scan", dir_s_scan, -1);
4052
4053 rb_define_method(rb_cDir,"fileno", dir_fileno, 0);
4054 rb_define_method(rb_cDir,"path", dir_path, 0);
4055 rb_define_method(rb_cDir,"to_path", dir_path, 0);
4056 rb_define_method(rb_cDir,"inspect", dir_inspect, 0);
4057 rb_define_method(rb_cDir,"read", dir_read, 0);
4058 rb_define_method(rb_cDir,"each", dir_each, 0);
4059 rb_define_method(rb_cDir,"each_child", dir_each_child_m, 0);
4060 rb_define_method(rb_cDir,"children", dir_collect_children, 0);
4061 rb_define_method(rb_cDir,"scan", dir_scan_children, 0);
4062 rb_define_method(rb_cDir,"rewind", dir_rewind, 0);
4063 rb_define_method(rb_cDir,"tell", dir_tell, 0);
4064 rb_define_method(rb_cDir,"seek", dir_seek, 1);
4065 rb_define_method(rb_cDir,"pos", dir_tell, 0);
4066 rb_define_method(rb_cDir,"pos=", dir_set_pos, 1);
4067 rb_define_method(rb_cDir,"close", dir_close, 0);
4068 rb_define_method(rb_cDir,"chdir", dir_chdir, 0);
4069
4070 rb_define_singleton_method(rb_cDir,"fchdir", dir_s_fchdir, 1);
4071 rb_define_singleton_method(rb_cDir,"chdir", dir_s_chdir, -1);
4072 rb_define_singleton_method(rb_cDir,"getwd", dir_s_getwd, 0);
4073 rb_define_singleton_method(rb_cDir,"pwd", dir_s_getwd, 0);
4074 rb_define_singleton_method(rb_cDir,"chroot", dir_s_chroot, 1);
4075 rb_define_singleton_method(rb_cDir,"mkdir", dir_s_mkdir, -1);
4076 rb_define_singleton_method(rb_cDir,"rmdir", dir_s_rmdir, 1);
4077 rb_define_singleton_method(rb_cDir,"delete", dir_s_rmdir, 1);
4078 rb_define_singleton_method(rb_cDir,"unlink", dir_s_rmdir, 1);
4079 rb_define_singleton_method(rb_cDir,"home", dir_s_home, -1);
4080
4081 rb_define_singleton_method(rb_cDir,"exist?", rb_file_directory_p, 1);
4082 rb_define_singleton_method(rb_cDir,"empty?", rb_dir_s_empty_p, 1);
4083
4084 rb_define_singleton_method(rb_cFile,"fnmatch", file_s_fnmatch, -1);
4085 rb_define_singleton_method(rb_cFile,"fnmatch?", file_s_fnmatch, -1);
4086
4087 /* {File::FNM_NOESCAPE}[rdoc-ref:File::Constants@File-3A-3AFNM_NOESCAPE] */
4088 rb_file_const("FNM_NOESCAPE", INT2FIX(FNM_NOESCAPE));
4089 /* {File::FNM_PATHNAME}[rdoc-ref:File::Constants@File-3A-3AFNM_PATHNAME] */
4090 rb_file_const("FNM_PATHNAME", INT2FIX(FNM_PATHNAME));
4091 /* {File::FNM_DOTMATCH}[rdoc-ref:File::Constants@File-3A-3AFNM_DOTMATCH] */
4092 rb_file_const("FNM_DOTMATCH", INT2FIX(FNM_DOTMATCH));
4093 /* {File::FNM_CASEFOLD}[rdoc-ref:File::Constants@File-3A-3AFNM_CASEFOLD] */
4094 rb_file_const("FNM_CASEFOLD", INT2FIX(FNM_CASEFOLD));
4095 /* {File::FNM_EXTGLOB}[rdoc-ref:File::Constants@File-3A-3AFNM_EXTGLOB] */
4096 rb_file_const("FNM_EXTGLOB", INT2FIX(FNM_EXTGLOB));
4097 /* {File::FNM_SYSCASE}[rdoc-ref:File::Constants@File-3A-3AFNM_SYSCASE] */
4098 rb_file_const("FNM_SYSCASE", INT2FIX(FNM_SYSCASE));
4099 /* {File::FNM_SHORTNAME}[rdoc-ref:File::Constants@File-3A-3AFNM_SHORTNAME] */
4100 rb_file_const("FNM_SHORTNAME", INT2FIX(FNM_SHORTNAME));
4101}
4102
4103#include "dir.rbinc"
#define RUBY_DEBUG
Define this macro when you want assertions.
Definition assert.h:88
#define RUBY_ATOMIC_VALUE_SET(var, val)
Identical to RUBY_ATOMIC_SET, except it expects its arguments are VALUE.
Definition atomic.h:378
#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.
Definition glob.h:49
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1611
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1404
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.
Definition class.c:3069
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1018
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1676
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1684
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define ISALPHA
Old name of rb_isalpha.
Definition ctype.h:92
#define ISASCII
Old name of rb_isascii.
Definition ctype.h:85
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define NIL_P
Old name of RB_NIL_P.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition encoding.h:516
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define FIXNUM_P
Old name of RB_FIXNUM_P.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
Definition error.c:3970
VALUE rb_eIOError
IOError exception.
Definition io.c:189
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.
Definition error.c:3976
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1428
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.
Definition error.c:1416
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:467
VALUE rb_cDir
Dir class.
Definition dir.c:504
VALUE rb_cObject
Object class.
Definition object.c:61
VALUE rb_mEnumerable
Enumerable module.
Definition enum.c:27
VALUE rb_cFile
File class.
Definition file.c:192
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:456
Encoding relates APIs.
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.
Definition encoding.h:571
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
Definition string.c:1348
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.
Definition string.c:1161
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.
Definition string.c:1354
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
Definition string.c:974
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.
Definition string.c:830
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.
Definition vm_eval.c:1088
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_each(VALUE ary)
Iteratively yields each element of the passed array to the implicitly passed block if any.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
Definition enumerator.h:242
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_str_new_shared(VALUE str)
Identical to rb_str_new_cstr(), except it takes a Ruby's string instead of C's.
Definition string.c:1518
#define rb_utf8_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "UTF-8" encoding.
Definition string.h:1584
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3834
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...
Definition string.c:3187
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2000
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2791
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
Definition string.c:3312
#define rb_str_dup_frozen
Just another name of rb_str_new_frozen.
Definition string.h:632
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1515
VALUE rb_thread_current(void)
Obtains the "current" thread.
Definition thread.c:3194
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition variable.c:500
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.
Definition io.h:8
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
Definition thread.c:2063
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 ...
Definition util.c:579
#define RB_NUM2INT
Just another name of rb_num2int_inline.
Definition int.h:38
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
Definition vm_eval.c:1399
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...
Definition vm_eval.c:1421
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1376
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:360
static int rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
Definition memory.h:544
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
Definition mode_t.h:28
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.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:450
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
Definition rtypeddata.h:122
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:759
#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...
Definition rtypeddata.h:590
#define FilePathValue(v)
Ensures that the parameter object is a path.
Definition ruby.h:90
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
#define FilePathStringValue(v)
This macro actually does the same thing as FilePathValue now.
Definition ruby.h:105
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
Definition scan_args.h:78
Definition dir.h:21
Definition dir.c:920
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:224
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376