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