14#include "ruby/internal/config.h"
18# include "missing/file.h"
30# if !(defined(__has_feature) && defined(__has_attribute))
35# define API_AVAILABLE(...)
36# define API_DEPRECATED(...)
38# include <CoreFoundation/CFString.h>
55#ifdef HAVE_SYS_PARAM_H
56# include <sys/param.h>
59# define MAXPATHLEN 1024
64#elif defined HAVE_SYS_UTIME_H
65# include <sys/utime.h>
72#ifdef HAVE_SYS_SYSMACROS_H
73# include <sys/sysmacros.h>
79#ifdef HAVE_SYS_MKDEV_H
80# include <sys/mkdev.h>
83#if defined(HAVE_FCNTL_H)
87#if defined(HAVE_SYS_TIME_H)
91#if !defined HAVE_LSTAT && !defined lstat
97# include "win32/file.h"
99# define chmod(p, m) rb_w32_uchmod((p), (m))
101# define chown(p, o, g) rb_w32_uchown((p), (o), (g))
103# define lchown(p, o, g) rb_w32_ulchown((p), (o), (g))
105# define link(f, t) rb_w32_ulink((f), (t))
107# define readlink(f, t, l) rb_w32_ureadlink((f), (t), (l))
109# define symlink(s, l) rb_w32_usymlink((s), (l))
118#ifdef HAVE_STRUCT_STATX_STX_BTIME
119# define ST_(name) stx_ ## name
122# define ST_(name) st_ ## name
126#if defined _WIN32 || defined __APPLE__
128# define TO_OSPATH(str) rb_str_encode_ospath(str)
131# define TO_OSPATH(str) (str)
135#if defined DOSISH || defined __CYGWIN__
140#if defined HAVE_REALPATH && defined __sun && defined __SVR4
153#include "internal/compilers.h"
154#include "internal/dir.h"
155#include "internal/encoding.h"
156#include "internal/error.h"
157#include "internal/file.h"
158#include "internal/io.h"
159#include "internal/load.h"
160#include "internal/object.h"
161#include "internal/process.h"
162#include "internal/thread.h"
163#include "internal/vm.h"
168#define UIANY2NUM(x) \
169 ((sizeof(x) <= sizeof(unsigned int)) ? \
170 UINT2NUM((unsigned)(x)) : \
171 (sizeof(x) <= sizeof(unsigned long)) ? \
172 ULONG2NUM((unsigned long)(x)) : \
173 ULL2NUM((unsigned LONG_LONG)(x)))
180file_path_convert(
VALUE name)
185 if (ENCINDEX_US_ASCII != fname_encidx &&
186 ENCINDEX_ASCII_8BIT != fname_encidx &&
187 (fs_encidx = rb_filesystem_encindex()) != fname_encidx &&
188 rb_default_internal_encoding() &&
192 rb_encoding *fname_encoding = rb_enc_from_index(fname_encidx);
193 rb_encoding *fs_encoding = rb_enc_from_index(fs_encidx);
201check_path_encoding(
VALUE str)
203 if (RB_UNLIKELY(!rb_str_enc_fastpath(str))) {
205 if (!rb_enc_asciicompat(enc)) {
213rb_get_path_check_to_string(
VALUE obj)
222 tmp = rb_check_funcall_default(obj, to_path, 0, 0, obj);
228rb_get_path_check_convert(
VALUE obj)
230 obj = file_path_convert(obj);
231 rb_get_path_check_no_convert(obj);
237rb_get_path_check_no_convert(
VALUE obj)
239 check_path_encoding(obj);
240 if (!rb_str_to_cstr(obj)) {
241 rb_raise(rb_eArgError,
"path name contains null byte");
250 return rb_get_path(obj);
256 return rb_get_path_check_convert(rb_get_path_check_to_string(obj));
260check_path(
VALUE obj,
const char **cstr)
262 VALUE str = rb_get_path_check_convert(rb_get_path_check_to_string(obj));
266 *cstr = RSTRING_PTR(str);
270#define CheckPath(str, cstr) RB_GC_GUARD(str) = check_path(str, &cstr);
277 if (encidx != ENCINDEX_ASCII_8BIT && encidx != ENCINDEX_UTF_8) {
287# define NORMALIZE_UTF8PATH 1
289# ifdef HAVE_WORKING_FORK
290static CFMutableStringRef
291mutable_CFString_new(CFStringRef *s,
const char *ptr,
rb_len_t len)
293 const CFAllocatorRef alloc = kCFAllocatorDefault;
294 *s = CFStringCreateWithBytesNoCopy(alloc, (
const UInt8 *)ptr,
len,
295 kCFStringEncodingUTF8, FALSE,
297 return CFStringCreateMutableCopy(alloc,
len, *s);
300# define mutable_CFString_release(m, s) (CFRelease(m), CFRelease(s))
303rb_CFString_class_initialize_before_fork(
void)
326 const char small_str[] =
"/";
335 for (
int i = 0; i < 2; i++) {
336 CFMutableStringRef m = mutable_CFString_new(&s, small_str,
len);
337 mutable_CFString_release(m, s);
348 CFMutableStringRef m = mutable_CFString_new(&s, ptr,
len);
351 CFStringNormalize(m, kCFStringNormalizationFormC);
352 all = CFRangeMake(0, CFStringGetLength(m));
353 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE, NULL, 0, &buflen);
355 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE,
356 (UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
358 mutable_CFString_release(m, s);
363rb_str_normalize_ospath(
const char *ptr,
rb_len_t len)
366 const char *e = ptr +
len;
375 rb_enc_associate(str, enc);
380 int r = rb_enc_precise_mbclen(p, e, enc);
384 rb_str_append_normalized_ospath(str, p1, p-p1);
391 c = rb_enc_mbc_to_codepoint(p, e, enc);
392 if ((0x2000 <= c && c <= 0x2FFF) || (0xF900 <= c && c <= 0xFAFF) ||
393 (0x2F800 <= c && c <= 0x2FAFF)) {
395 rb_str_append_normalized_ospath(str, p1, p-p1);
406 rb_str_append_normalized_ospath(str, p1, p-p1);
413ignored_char_p(
const char *p,
const char *e,
rb_encoding *enc)
416 if (p+3 > e)
return 0;
417 switch ((
unsigned char)*p) {
419 switch ((
unsigned char)p[1]) {
421 c = (
unsigned char)p[2];
423 if (c >= 0x8c && c <= 0x8f)
return 3;
425 if (c >= 0xaa && c <= 0xae)
return 3;
428 c = (
unsigned char)p[2];
430 if (c >= 0xaa && c <= 0xaf)
return 3;
436 if ((
unsigned char)p[1] == 0xbb &&
437 (
unsigned char)p[2] == 0xbf)
444# define NORMALIZE_UTF8PATH 0
447#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
458 int (*func)(
const char *,
void *);
464no_gvl_apply2files(
void *ptr)
468 for (aa->i = 0; aa->i < aa->argc; aa->i++) {
469 if (aa->func(aa->fn[aa->i].ptr, aa->arg) < 0) {
478NORETURN(
static void utime_failed(
struct apply_arg *));
479static int utime_internal(
const char *,
void *);
483apply2files(
int (*func)(
const char *,
void *),
int argc,
VALUE *argv,
void *arg)
495 for (aa->i = 0; aa->i < argc; aa->i++) {
496 VALUE path = rb_get_path(argv[aa->i]);
498 path = rb_str_encode_ospath(path);
499 aa->fn[aa->i].ptr = RSTRING_PTR(path);
500 aa->fn[aa->i].path = path;
503 IO_WITHOUT_GVL(no_gvl_apply2files, aa);
506 if (func == utime_internal) {
510 rb_syserr_fail_path(aa->errnum, aa->fn[aa->i].path);
529 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
533 rb_io_stat_data stat;
546rb_stat_new(
const struct stat *st)
552# define CP(m) .stx_ ## m = st->st_ ## m
553# define CP_32(m) .stx_ ## m = (uint32_t)st->st_ ## m
554# define CP_TS(m) .stx_ ## m = stat_ ## m ## spec(st)
555 rb_st->stat = (
struct statx){
556 .stx_mask = STATX_BASIC_STATS,
573 rb_st->initialized =
true;
581rb_statx_new(
const rb_io_stat_data *st)
587 rb_st->initialized =
true;
593static rb_io_stat_data*
598 if (!rb_st->initialized) rb_raise(
rb_eTypeError,
"uninitialized File::Stat");
604statx_mtimespec(
const rb_io_stat_data *st)
606 return st->stx_mtime;
609# define statx_mtimespec stat_mtimespec
652 if (ts1.tv_sec == ts2.tv_sec) {
653 if (ts1.tv_nsec == ts2.tv_nsec)
return INT2FIX(0);
654 if (ts1.tv_nsec < ts2.tv_nsec)
return INT2FIX(-1);
657 if (ts1.tv_sec < ts2.tv_sec)
return INT2FIX(-1);
663#define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
666# define NUM2DEVT(v) NUM2UINT(v)
669# define DEVT2NUM(v) UINT2NUM(v)
671#ifndef PRI_DEVT_PREFIX
672# define PRI_DEVT_PREFIX ""
691rb_stat_dev(
VALUE self)
694 unsigned int m = get_stat(self)->stx_dev_major;
695 unsigned int n = get_stat(self)->stx_dev_minor;
697#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
698 return DEVT2NUM(get_stat(self)->st_dev);
699#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
700 return ULONG2NUM(get_stat(self)->st_dev);
702 return ULL2NUM(get_stat(self)->st_dev);
725rb_stat_dev_major(
VALUE self)
728 return UINT2NUM(get_stat(self)->stx_dev_major);
730 return UINT2NUM(major(get_stat(self)->st_dev));
755rb_stat_dev_minor(
VALUE self)
758 return UINT2NUM(get_stat(self)->stx_dev_minor);
760 return UINT2NUM(minor(get_stat(self)->st_dev));
783rb_stat_ino(
VALUE self)
785 rb_io_stat_data *ptr = get_stat(self);
786#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
788 return rb_integer_unpack(&ptr->st_ino, 2,
789 SIZEOF_STRUCT_STAT_ST_INO, 0,
793 return UIANY2NUM(ptr->ST_(ino));
817rb_stat_mode(
VALUE self)
819 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
835rb_stat_nlink(
VALUE self)
838 const rb_io_stat_data *ptr = get_stat(self);
840 return UIANY2NUM(ptr->ST_(nlink));
854rb_stat_uid(
VALUE self)
856 return UIDT2NUM(get_stat(self)->ST_(uid));
881rb_stat_gid(
VALUE self)
883 return GIDT2NUM(get_stat(self)->ST_(gid));
899rb_stat_rdev(
VALUE self)
902 unsigned int m = get_stat(self)->stx_rdev_major;
903 unsigned int n = get_stat(self)->stx_rdev_minor;
905#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
907#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
908 return DEVT2NUM(get_stat(self)->ST_(rdev));
909#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
910 return ULONG2NUM(get_stat(self)->ST_(rdev));
912 return ULL2NUM(get_stat(self)->ST_(rdev));
927rb_stat_rdev_major(
VALUE self)
930 return UINT2NUM(get_stat(self)->stx_rdev_major);
931#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
932 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
949rb_stat_rdev_minor(
VALUE self)
952 return UINT2NUM(get_stat(self)->stx_rdev_minor);
953#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
954 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
990rb_stat_size(
VALUE self)
992 return OFFT2NUM(get_stat(self)->ST_(size));
1014rb_stat_blksize(
VALUE self)
1016#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1017 return ULONG2NUM(get_stat(self)->ST_(blksize));
1041rb_stat_blocks(
VALUE self)
1043#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1044# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
1045 return ULL2NUM(get_stat(self)->ST_(blocks));
1047 return ULONG2NUM(get_stat(self)->ST_(blocks));
1055stat_atimespec(
const struct stat *st)
1058 ts.tv_sec = st->st_atime;
1059#if defined(HAVE_STRUCT_STAT_ST_ATIM)
1060 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
1061#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1062 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
1063#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
1064 ts.tv_nsec = (uint32_t)st->st_atimensec;
1073statx_atimespec(
const rb_io_stat_data *st)
1075 return st->stx_atime;
1078# define statx_atimespec stat_atimespec
1088stat_atime(
const struct stat *st)
1090 return stat_time(stat_atimespec(st));
1094stat_mtimespec(
const struct stat *st)
1097 ts.tv_sec = st->st_mtime;
1098#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1099 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1100#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1101 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1102#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1103 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1111stat_mtime(
const struct stat *st)
1113 return stat_time(stat_mtimespec(st));
1117stat_ctimespec(
const struct stat *st)
1120 ts.tv_sec = st->st_ctime;
1121#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1122 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1123#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1124 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1125#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1126 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1135statx_ctimespec(
const rb_io_stat_data *st)
1137 return st->stx_ctime;
1140# define statx_ctimespec stat_ctimespec
1144stat_ctime(
const struct stat *st)
1146 return stat_time(stat_ctimespec(st));
1149#define HAVE_STAT_BIRTHTIME
1150#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1152statx_birthtime(
const rb_io_stat_data *st)
1157#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1158static VALUE statx_birthtime(
const rb_io_stat_data *st);
1159#elif defined(_WIN32)
1160# define statx_birthtime stat_ctime
1162# undef HAVE_STAT_BIRTHTIME
1211rb_stat_atime(
VALUE self)
1213 return stat_time(statx_atimespec(get_stat(self)));
1244rb_stat_mtime(
VALUE self)
1246 return stat_time(statx_mtimespec(get_stat(self)));
1283rb_stat_ctime(
VALUE self)
1285 return stat_time(statx_ctimespec(get_stat(self)));
1288#if defined(HAVE_STAT_BIRTHTIME)
1311rb_stat_birthtime(
VALUE self)
1313 return statx_birthtime(get_stat(self));
1316# define rb_stat_birthtime rb_f_notimplement
1376rb_stat_inspect(
VALUE self)
1380 static const struct {
1384 {
"dev", rb_stat_dev},
1385 {
"ino", rb_stat_ino},
1386 {
"mode", rb_stat_mode},
1387 {
"nlink", rb_stat_nlink},
1388 {
"uid", rb_stat_uid},
1389 {
"gid", rb_stat_gid},
1390 {
"rdev", rb_stat_rdev},
1391 {
"size", rb_stat_size},
1392 {
"blksize", rb_stat_blksize},
1393 {
"blocks", rb_stat_blocks},
1394 {
"atime", rb_stat_atime},
1395 {
"mtime", rb_stat_mtime},
1396 {
"ctime", rb_stat_ctime},
1397#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1398 {
"birthtime", rb_stat_birthtime},
1404 if (!rb_st->initialized) {
1412 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1420 v = (*member[i].func)(self);
1422 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1424 else if (i == 0 || i == 6) {
1425 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1445no_gvl_fstat(
void *data)
1448 return (
VALUE)fstat(arg->file.fd, arg->st);
1452fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1456 data.file.fd = fptr->
fd;
1459 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1463no_gvl_stat(
void * data)
1466 return (
void *)(
VALUE)stat(arg->file.path, arg->st);
1470stat_without_gvl(
const char *path,
struct stat *st)
1474 data.file.path = path;
1477 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1480#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1481 defined(HAVE_STRUCT_STATX_STX_BTIME)
1483# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1486# ifdef HAVE_SYSCALL_H
1487# include <syscall.h>
1488# elif defined HAVE_SYS_SYSCALL_H
1489# include <sys/syscall.h>
1491# if defined __linux__
1492# include <linux/stat.h>
1494statx(
int dirfd,
const char *pathname,
int flags,
1495 unsigned int mask,
struct statx *statxbuf)
1497 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1502typedef struct no_gvl_rb_io_stat_data {
1508} no_gvl_rb_io_stat_data;
1511io_blocking_statx(
void *data)
1513 no_gvl_rb_io_stat_data *arg = data;
1514 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1518no_gvl_statx(
void *data)
1520 return (
void *)io_blocking_statx(data);
1524statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1526 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1529 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1533lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1535 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1538 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1542fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1544 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1547 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1550#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1553rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1558 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1563 result = fstatx_without_gvl(fptr, stx, mask);
1568 file = rb_str_encode_ospath(file);
1569 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1575# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1577NORETURN(
static void statx_notimplement(
const char *field_name));
1582statx_notimplement(
const char *field_name)
1585 "%s is unimplemented on this filesystem",
1590statx_birthtime(
const rb_io_stat_data *stx)
1592 if (!statx_has_birthtime(stx)) {
1594 statx_notimplement(
"birthtime");
1596 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1601# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1602# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1603# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1604# define rb_statx(file, stx, mask) rb_stat(file, stx)
1605# define STATX(path, st, mask) stat(path, st)
1607#if defined(HAVE_STAT_BIRTHTIME)
1608# define statx_has_birthtime(st) 1
1610# define statx_has_birthtime(st) 0
1617# define FSTAT(fd, st) fstat(fd, st)
1626 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1631 result = fstat_without_gvl(fptr, st);
1636 file = rb_str_encode_ospath(file);
1637 result = stat_without_gvl(RSTRING_PTR(file), st);
1675 fname = rb_str_encode_ospath(fname);
1676 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1677 rb_sys_fail_path(fname);
1679 return rb_statx_new(&st);
1711rb_io_stat(
VALUE obj)
1717 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1718 rb_sys_fail_path(fptr->
pathv);
1720 return rb_statx_new(&st);
1725no_gvl_lstat(
void *ptr)
1728 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1732lstat_without_gvl(
const char *path,
struct stat *st)
1736 data.file.path = path;
1739 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1776 fname = rb_str_encode_ospath(fname);
1777 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1778 rb_sys_fail_path(fname);
1780 return rb_statx_new(&st);
1782 return rb_file_s_stat(klass, fname);
1815rb_file_lstat(
VALUE obj)
1824 path = rb_str_encode_ospath(fptr->
pathv);
1825 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1826 rb_sys_fail_path(fptr->
pathv);
1828 return rb_statx_new(&st);
1830 return rb_io_stat(obj);
1835rb_group_member(GETGROUPS_T gid)
1837#if !defined(HAVE_GETGROUPS)
1846 if (getgid() == gid || getegid() == gid)
1849 groups = getgroups(0, NULL);
1850 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1851 anum = getgroups(groups, gary);
1852 while (--anum >= 0) {
1853 if (gary[anum] == gid) {
1866# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1869#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1870#define USE_GETEUID 1
1875eaccess(
const char *path,
int mode)
1884 if (getuid() == euid && getgid() == getegid())
1885 return access(path, mode);
1887 if (stat(path, &st) < 0)
1897 if (st.st_mode & S_IXUGO)
1903 if (st.st_uid == euid)
1905 else if (rb_group_member(st.st_gid))
1908 if ((
int)(st.st_mode & mode) == mode)
return 0;
1912 return access(path, mode);
1923nogvl_eaccess(
void *ptr)
1927 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1931rb_eaccess(
VALUE fname,
int mode)
1936 fname = rb_str_encode_ospath(fname);
1940 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1944nogvl_access(
void *ptr)
1948 return (
void *)(
VALUE)access(aa->path, aa->mode);
1952rb_access(
VALUE fname,
int mode)
1957 fname = rb_str_encode_ospath(fname);
1961 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1996# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
2002 if (S_ISDIR(st.st_mode))
return Qtrue;
2031# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
2037 if (S_ISFIFO(st.st_mode))
return Qtrue;
2069# define S_ISLNK(m) _S_ISLNK(m)
2072# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
2075# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
2085 fname = rb_str_encode_ospath(fname);
2087 if (S_ISLNK(st.st_mode))
return Qtrue;
2122# define S_ISSOCK(m) _S_ISSOCK(m)
2125# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
2128# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
2138 if (S_ISSOCK(st.st_mode))
return Qtrue;
2170# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
2172# define S_ISBLK(m) (0)
2180 if (S_ISBLK(st.st_mode))
return Qtrue;
2207# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2213 if (S_ISCHR(st.st_mode))
return Qtrue;
2283 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2297rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2299 return RBOOL(rb_access(fname, R_OK) >= 0);
2303# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2307# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2336rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2342 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2343 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2377 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2394rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2396 return RBOOL(rb_access(fname, W_OK) >= 0);
2425rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2431 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2432 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2462 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2476rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2478 return RBOOL(rb_access(fname, X_OK) >= 0);
2482# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2510 return RBOOL(S_ISREG(st.st_mode));
2570 return RBOOL(st.st_size == 0);
2608 if (st.st_size == 0)
return Qnil;
2642 return RBOOL(st.st_uid == geteuid());
2651 return RBOOL(st.st_uid == getuid());
2687 if (rb_group_member(st.st_gid))
return Qtrue;
2692#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2694check3rdbyte(
VALUE fname,
int mode)
2699 return RBOOL(st.st_mode & mode);
2732 return check3rdbyte(fname, S_ISUID);
2767 return check3rdbyte(fname, S_ISGID);
2803 return check3rdbyte(fname, S_ISVTX);
2840 struct stat st1, st2;
2844 if (st1.st_dev != st2.st_dev)
return Qfalse;
2845 if (st1.st_ino != st2.st_ino)
return Qfalse;
2849 return rb_w32_file_identical_p(fname1, fname2);
2875 if (
rb_stat(fname, &st) < 0) {
2878 rb_syserr_fail_path(e, fname);
2884rb_file_ftype(mode_t mode)
2888 if (S_ISREG(mode)) {
2891 else if (S_ISDIR(mode)) {
2894 else if (S_ISCHR(mode)) {
2895 t =
"characterSpecial";
2898 else if (S_ISBLK(mode)) {
2903 else if (S_ISFIFO(mode)) {
2908 else if (S_ISLNK(mode)) {
2913 else if (S_ISSOCK(mode)) {
2921 return rb_fstring_cstr(t);
2970 fname = rb_str_encode_ospath(fname);
2972 rb_sys_fail_path(fname);
2975 return rb_file_ftype(st.st_mode);
3031 if (
rb_stat(fname, &st) < 0) {
3034 rb_syserr_fail_path(e, fname);
3036 return stat_time(stat_atimespec(&st));
3068rb_file_atime(
VALUE obj)
3074 if (fstat(fptr->
fd, &st) == -1) {
3075 rb_sys_fail_path(fptr->
pathv);
3077 return stat_time(stat_atimespec(&st));
3117 if (
rb_stat(fname, &st) < 0) {
3120 rb_syserr_fail_path(e, fname);
3122 return stat_time(stat_mtimespec(&st));
3147rb_file_mtime(
VALUE obj)
3153 if (fstat(fptr->
fd, &st) == -1) {
3154 rb_sys_fail_path(fptr->
pathv);
3156 return stat_time(stat_mtimespec(&st));
3200 if (
rb_stat(fname, &st) < 0) {
3203 rb_syserr_fail_path(e, fname);
3205 return stat_time(stat_ctimespec(&st));
3242rb_file_ctime(
VALUE obj)
3248 if (fstat(fptr->
fd, &st) == -1) {
3249 rb_sys_fail_path(fptr->
pathv);
3251 return stat_time(stat_ctimespec(&st));
3254#if defined(HAVE_STAT_BIRTHTIME)
3280 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3283 rb_syserr_fail_path(e, fname);
3285 return statx_birthtime(&st);
3288# define rb_file_s_birthtime rb_f_notimplement
3291#if defined(HAVE_STAT_BIRTHTIME)
3312rb_file_birthtime(
VALUE obj)
3318 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3319 rb_sys_fail_path(fptr->
pathv);
3321 return statx_birthtime(&st);
3324# define rb_file_birthtime rb_f_notimplement
3336 rb_io_flush_raw(file, 0);
3339 if (fstat(fptr->
fd, &st) == -1) {
3340 rb_sys_fail_path(fptr->
pathv);
3366file_size(
VALUE self)
3368 return OFFT2NUM(rb_file_size(self));
3377nogvl_chmod(
void *ptr)
3380 int ret = chmod(data->path, data->mode);
3381 return (
void *)(
VALUE)ret;
3385rb_chmod(
const char *path, mode_t mode)
3391 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3395chmod_internal(
const char *path,
void *mode)
3397 return chmod(path, *(mode_t *)mode);
3430 return apply2files(chmod_internal, argc, argv, &mode);
3434struct nogvl_fchmod_data {
3440io_blocking_fchmod(
void *ptr)
3442 struct nogvl_fchmod_data *data = ptr;
3443 int ret = fchmod(data->fd, data->mode);
3448rb_fchmod(
struct rb_io* io, mode_t mode)
3451 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3452 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3481#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3489 if (rb_fchmod(fptr, mode) == -1) {
3490 if (HAVE_FCHMOD ||
errno != ENOSYS)
3491 rb_sys_fail_path(fptr->
pathv);
3494 if (!HAVE_FCHMOD)
return INT2FIX(0);
3497#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3499 path = rb_str_encode_ospath(fptr->
pathv);
3500 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3501 rb_sys_fail_path(fptr->
pathv);
3507#if defined(HAVE_LCHMOD)
3509lchmod_internal(
const char *path,
void *mode)
3511 return lchmod(path, *(mode_t *)mode);
3548 return apply2files(lchmod_internal, argc, argv, &mode);
3551#define rb_file_s_lchmod rb_f_notimplement
3554static inline rb_uid_t
3558 return (rb_uid_t)-1;
3563static inline rb_gid_t
3567 return (rb_gid_t)-1;
3578chown_internal(
const char *path,
void *arg)
3581 return chown(path, args->owner, args->group);
3635 arg.owner = to_uid(*argv++);
3636 arg.group = to_gid(*argv++);
3638 return apply2files(chown_internal, argc, argv, &arg);
3650nogvl_chown(
void *ptr)
3653 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3657rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3660 .as = {.path = path},
3661 .new = {.owner = owner, .group = group},
3663 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3668nogvl_fchown(
void *ptr)
3671 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3675rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3680 .new = {.owner = owner, .group = group},
3682 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3716 path = rb_str_encode_ospath(fptr->
pathv);
3717 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3718 rb_sys_fail_path(fptr->
pathv);
3720 if (rb_fchown(fptr->
fd, o, g) == -1)
3721 rb_sys_fail_path(fptr->
pathv);
3727#if defined(HAVE_LCHOWN)
3729lchown_internal(
const char *path,
void *arg)
3732 return lchown(path, args->owner, args->group);
3789 arg.owner = to_uid(*argv++);
3790 arg.group = to_gid(*argv++);
3792 return apply2files(lchown_internal, argc, argv, &arg);
3795#define rb_file_s_lchown rb_f_notimplement
3805NORETURN(
static void utime_failed(
struct apply_arg *));
3811 VALUE path = aa->fn[aa->i].path;
3814 if (ua->tsp && e == EINVAL) {
3817 VALUE atime = ua->atime;
3818 VALUE mtime = ua->mtime;
3820 if (!
NIL_P(atime)) {
3823 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3826 if (
NIL_P(a)) e[0] = m;
3842 rb_syserr_fail_path(e, path);
3846#if defined(HAVE_UTIMES)
3848# if !defined(HAVE_UTIMENSAT)
3850# elif defined(__APPLE__) && \
3851 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3853# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3854typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3858static inline utimensat_func *
3865# define utimensat rb_utimensat()
3872utime_internal(
const char *path,
void *arg)
3875 const struct timespec *tsp = v->tsp;
3876 struct timeval tvbuf[2], *tvp = NULL;
3878#if defined(HAVE_UTIMENSAT)
3879# if defined(__APPLE__)
3880 const int try_utimensat = utimensat != NULL;
3881 const int try_utimensat_follow = utimensat != NULL;
3883# define TRY_UTIMENSAT 1
3884 static int try_utimensat = 1;
3885# ifdef AT_SYMLINK_NOFOLLOW
3886 static int try_utimensat_follow = 1;
3888 const int try_utimensat_follow = 0;
3893 if (v->follow ? try_utimensat_follow : try_utimensat) {
3894# ifdef AT_SYMLINK_NOFOLLOW
3896 flags = AT_SYMLINK_NOFOLLOW;
3900 int result = utimensat(AT_FDCWD, path, tsp, flags);
3901# ifdef TRY_UTIMENSAT
3902 if (result < 0 &&
errno == ENOSYS) {
3903# ifdef AT_SYMLINK_NOFOLLOW
3904 try_utimensat_follow = 0;
3916 tvbuf[0].tv_sec = tsp[0].tv_sec;
3917 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3918 tvbuf[1].tv_sec = tsp[1].tv_sec;
3919 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3923 if (v->follow)
return lutimes(path, tvp);
3925 return utimes(path, tvp);
3930#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3938utime_internal(
const char *path,
void *arg)
3942 struct utimbuf utbuf, *utp = NULL;
3944 utbuf.actime = tsp[0].tv_sec;
3945 utbuf.modtime = tsp[1].tv_sec;
3948 return utime(path, utp);
3953utime_internal_i(
int argc,
VALUE *argv,
int follow)
3956 struct timespec tss[2], *tsp = NULL;
3959 args.atime = *argv++;
3960 args.mtime = *argv++;
3962 args.follow = follow;
3964 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3967 if (args.atime == args.mtime)
3974 return apply2files(utime_internal, argc, argv, &args);
4020 return utime_internal_i(argc, argv, FALSE);
4023#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
4081 return utime_internal_i(argc, argv, TRUE);
4084#define rb_file_s_lutime rb_f_notimplement
4087#ifdef RUBY_FUNCTION_NAME_STRING
4088# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
4090# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
4092#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
4093NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
4095syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
4099 const int max_pathlen = MAX_PATH;
4101 const int max_pathlen = MAXPATHLEN;
4112#ifdef RUBY_FUNCTION_NAME_STRING
4113 rb_syserr_fail_path_in(func, e, str);
4115 rb_syserr_fail_path(e, str);
4150 from = rb_str_encode_ospath(from);
4151 to = rb_str_encode_ospath(to);
4154 sys_fail2(from, to);
4159#define rb_file_s_link rb_f_notimplement
4200 from = rb_str_encode_ospath(from);
4201 to = rb_str_encode_ospath(to);
4204 sys_fail2(from, to);
4209#define rb_file_s_symlink rb_f_notimplement
4237 return rb_readlink(path, rb_filesystem_encoding());
4240struct readlink_arg {
4247nogvl_readlink(
void *ptr)
4249 struct readlink_arg *ra = ptr;
4251 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4255readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4257 struct readlink_arg ra;
4259 ra.path = RSTRING_PTR(path);
4260 ra.buf = RSTRING_PTR(buf);
4263 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4274 path = rb_str_encode_ospath(path);
4275 v = rb_enc_str_new(0, size, enc);
4276 while ((rv = readlink_without_gvl(path, v, size)) == size
4278 || (rv < 0 &&
errno == ERANGE)
4287 rb_str_resize(v, 0);
4288 rb_syserr_fail_path(e, path);
4290 rb_str_resize(v, rv);
4295#define rb_file_s_readlink rb_f_notimplement
4299unlink_internal(
const char *path,
void *arg)
4301 return unlink(path);
4344rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4346 return apply2files(unlink_internal, argc, argv, 0);
4355no_gvl_rename(
void *ptr)
4359 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4437 f = rb_str_encode_ospath(from);
4438 t = rb_str_encode_ospath(to);
4441#if defined __CYGWIN__
4444 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4449 if (chmod(ra.dst, 0666) == 0 &&
4450 unlink(ra.dst) == 0 &&
4451 rename(ra.src, ra.dst) == 0)
4455 syserr_fail2(e, from, to);
4489 rb_error_arity(argc, 0, 1);
4497#if defined __CYGWIN__ || defined DOSISH
4499#define DOSISH_DRIVE_LETTER
4500#define FILE_ALT_SEPARATOR '\\'
4502#ifdef FILE_ALT_SEPARATOR
4503#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4505static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4508#define isdirsep(x) ((x) == '/')
4520#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4521#define isADS(x) ((x) == ':')
4523#define istrailinggarbage(x) 0
4526#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4528#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4529#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4531#if defined(DOSISH_UNC)
4532#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4534#define has_unc(buf) 0
4537#ifdef DOSISH_DRIVE_LETTER
4539has_drive_letter(
const char *buf)
4541 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4566 if (chdir(drive) == 0) {
4567 drvcwd = rb_dir_getwd_ospath();
4579not_same_drive(
VALUE path,
int drive)
4581 const char *p = RSTRING_PTR(path);
4582 if (RSTRING_LEN(path) < 2)
return 0;
4583 if (has_drive_letter(p)) {
4594skiproot(
const char *path,
const char *end)
4596#ifdef DOSISH_DRIVE_LETTER
4597 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4599 while (path < end && isdirsep(*path)) path++;
4600 return (
char *)path;
4604enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4606 while (s < e && !isdirsep(*s)) {
4607 Inc(s, e, mb_enc, enc);
4612#define nextdirsep rb_enc_path_next
4616 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4619#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4620#define skipprefix enc_path_skip_prefix
4622#define skipprefix(path, end, mb_enc, enc) (path)
4625enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4627#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4629 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4631 while (path < end && isdirsep(*path)) path++;
4632 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4633 path + 2 <= end && !isdirsep(path[1])) {
4634 path = enc_path_next(path + 1, end, mb_enc, enc);
4636 return (
char *)path;
4639#ifdef DOSISH_DRIVE_LETTER
4640 if (path + 2 <= end && has_drive_letter(path))
4641 return (
char *)(path + 2);
4644 return (
char *)path;
4648rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4650 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4654skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4656#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4657 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4658 while (p < end && isdirsep(*p)) p++;
4661 return skiproot(path, end);
4666rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4668 return skipprefixroot(path, end, enc);
4672enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4675 while (path < end) {
4676 if (isdirsep(*path)) {
4677 const char *tmp = path++;
4678 while (path < end && isdirsep(*path)) path++;
4679 if (path >= end)
break;
4683 Inc(path, end, mb_enc, enc);
4689rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4691 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4695strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4697 if (RB_UNLIKELY(mb_enc)) {
4698 return enc_path_last_separator(path, end, mb_enc, enc);
4701 const char *cursor = end - 1;
4703 while (cursor >= path && isdirsep(cursor[0])) {
4707 while (cursor >= path) {
4708 if (isdirsep(cursor[0])) {
4709 while (cursor > path && isdirsep(cursor[-1])) {
4712 return (
char *)cursor;
4720chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4722 while (path < end) {
4723 if (isdirsep(*path)) {
4724 const char *last = path++;
4725 while (path < end && isdirsep(*path)) path++;
4726 if (path >= end)
return (
char *)last;
4729 Inc(path, end, mb_enc, enc);
4732 return (
char *)path;
4738 if (path < end && isdirsep(*path)) path++;
4739 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4745 rb_encoding *enc = rb_enc_check_str(path1, path2);
4746 int encidx = rb_enc_to_index(enc);
4747 if (encidx == ENCINDEX_US_ASCII) {
4748 encidx = rb_enc_get_index(path1);
4749 if (encidx == ENCINDEX_US_ASCII)
4750 encidx = rb_enc_get_index(path2);
4751 enc = rb_enc_from_index(encidx);
4758ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4760 while (path < end && *path ==
'.') path++;
4761 while (path < end && !isADS(*path)) {
4762 if (istrailinggarbage(*path)) {
4763 const char *last = path++;
4764 while (path < end && istrailinggarbage(*path)) path++;
4765 if (path >= end || isADS(*path))
return (
char *)last;
4767 else if (isdirsep(*path)) {
4768 const char *last = path++;
4769 while (path < end && isdirsep(*path)) path++;
4770 if (path >= end)
return (
char *)last;
4771 if (isADS(*path)) path++;
4774 Inc(path, end, mb_enc, enc);
4777 return (
char *)path;
4781#define BUFCHECK(cond) do {\
4784 do {buflen *= 2;} while (cond);\
4785 rb_str_resize(result, buflen);\
4786 buf = RSTRING_PTR(result);\
4788 pend = buf + buflen;\
4792#define BUFINIT(result, buf, p, pend) do {\
4793 if (!result) { result = rb_usascii_str_new(0, 1); } \
4794 p = buf = RSTRING_PTR(result); \
4795 buflen = RSTRING_LEN(result); \
4796 pend = p + buflen; \
4800# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4802# define SKIPPATHSEP(p) 1
4805#define BUFCOPY(srcptr, srclen) do { \
4806 const int skip = SKIPPATHSEP(p); \
4807 rb_str_set_len(result, p-buf+skip); \
4808 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4810 memcpy(p, (srcptr), (srclen)); \
4814#define WITH_ROOTDIFF(stmt) do { \
4815 rb_len_t rootdiff = root - buf; \
4817 root = buf + rootdiff; \
4821copy_home_path(
VALUE result,
const char *dir)
4827 dirlen = strlen(dir);
4828 rb_str_resize(result, dirlen);
4829 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4830 encidx = rb_filesystem_encindex();
4831 rb_enc_associate_index(result, encidx);
4832#if defined FILE_ALT_SEPARATOR
4834 bool mb_enc = enc_mbclen_needed(enc);
4835 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4836 if (*p == FILE_ALT_SEPARATOR) {
4848 VALUE dirname = rb_getpwdirnam_for_login(user);
4849 if (dirname ==
Qnil) {
4850 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4852 const char *dir = RSTRING_PTR(dirname);
4854 extern char *getlogin(
void);
4855 const char *pwPtr = 0;
4857 # define endpwent() ((void)0)
4858 const char *dir, *username = RSTRING_PTR(user);
4869 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4870 dir = pwPtr = getenv(
"HOME");
4872 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4875 copy_home_path(result, dir);
4881rb_default_home_dir(
VALUE result)
4883 const char *dir = getenv(
"HOME");
4885#if defined HAVE_PWD_H
4899 VALUE login_name = rb_getlogin();
4901# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4906 if (
NIL_P(login_name)) {
4907 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4911 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4912 if (
NIL_P(pw_dir)) {
4913 pw_dir = rb_getpwdiruid();
4914 if (
NIL_P(pw_dir)) {
4915 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4920 copy_home_path(result, RSTRING_PTR(pw_dir));
4921 rb_str_resize(pw_dir, 0);
4926 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4928 return copy_home_path(result, dir);
4934#if NORMALIZE_UTF8PATH
4935 VALUE path = rb_str_normalize_ospath(ptr,
len);
4936 rb_enc_associate(path, fsenc);
4939 return rb_enc_str_new(ptr,
len, fsenc);
4946 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4948 rb_enc_associate(dirname, fsenc);
4952 size_t dirlen = RSTRING_LEN(dirname);
4955 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4956 if (!rb_enc_compatible(fname, dirname)) {
4959 rb_enc_check(fname, dirname);
4960 rb_bug(
"unreachable");
4962 rb_encoding *direnc = fs_enc_check(fname, dirname);
4963 if (direnc != fsenc) {
4970 do {buflen *= 2;}
while (dirlen > buflen);
4971 rb_str_resize(result, buflen);
4972 buf = RSTRING_PTR(result);
4973 memcpy(buf, cwdp, dirlen);
4974 rb_enc_associate(result, *enc);
4975 return buf + dirlen;
4979rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4981 const char *s, *b, *fend;
4982 char *buf, *p, *pend, *root;
4983 size_t buflen, bdiff;
4984 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4987 fend = s + RSTRING_LEN(fname);
4988 enc = rb_str_enc_get(fname);
4989 bool mb_enc = enc_mbclen_needed(enc);
4990 if (!mb_enc &&
RTEST(dname)) {
4991 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4994 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4995 BUFINIT(result, buf, p, pend);
4998 if (s + 1 == fend || isdirsep(s[1])) {
5002 if (++s < fend) ++s;
5003 rb_default_home_dir(result);
5006 s = nextdirsep(b = s, fend, enc);
5009 BUFCHECK(bdiff + userlen >= buflen);
5010 memcpy(p, b, userlen);
5013 rb_enc_associate(result, enc);
5014 rb_home_dir_of(result, result);
5018 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
5020 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
5021 (
int)userlen, b, fname);
5024 rb_raise(rb_eArgError,
"non-absolute home");
5027 BUFINIT(result, buf, p, pend);
5030#ifdef DOSISH_DRIVE_LETTER
5032 else if (s + 1 < fend && has_drive_letter(s)) {
5033 BUFINIT(result, buf, p, pend);
5035 if (s + 2 < fend && isdirsep(s[2])) {
5038 BUFCHECK(bdiff + 2 >= buflen);
5042 rb_enc_copy(result, fname);
5047 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
5048 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
5049 BUFINIT(result, buf, p, pend);
5056 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
5057 BUFINIT(result, buf, p, pend);
5061 rb_enc_associate(result, enc = fs_enc_check(result, fname));
5064 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
5069 else if (s == fend || !rb_is_absolute_path(s)) {
5071 if (!
NIL_P(dname)) {
5073 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
5077 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
5079 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
5084 result = resized_result;
5088 rb_enc_associate(result, fs_enc_check(result, fname));
5089 BUFINIT(result, buf, p, pend);
5093 VALUE cwd = rb_dir_getwd_ospath();
5097 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
5098 BUFINIT(result, buf, p, pend);
5101#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5102 if (s < fend && isdirsep(*s)) {
5105 p = skipprefix(buf, p, mb_enc, enc);
5109 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
5112 BUFINIT(result, buf, p, pend);
5116 do s++;
while (s < fend && isdirsep(*s));
5119 BUFCHECK(bdiff >= buflen);
5120 memset(buf,
'/',
len);
5122 rb_enc_associate(result, fs_enc_check(result, fname));
5124 if (p > buf && p[-1] ==
'/')
5128 BUFCHECK(bdiff + 1 >= buflen);
5133 BUFCHECK(bdiff + 1 >= buflen);
5135 root = skipprefix(buf, p+1, mb_enc, enc);
5148 if (s+1 == fend || isdirsep(*(s+1))) {
5152 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
5162#if defined FILE_ALT_SEPARATOR
5163 case FILE_ALT_SEPARATOR:
5174#if defined FILE_ALT_SEPARATOR
5175 case FILE_ALT_SEPARATOR:
5178 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5186 int n = ignored_char_p(s, fend, enc);
5189 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5197 Inc(s, fend, mb_enc, enc);
5206 if (p == skiproot(buf, p + !!*p) - 1) p++;
5209 rb_enc_check(fname, result);
5216str_shrink(
VALUE str)
5218 rb_str_resize(str, RSTRING_LEN(str));
5222#define expand_path(fname, dname, abs_mode, long_name, result) \
5223 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5225#define check_expand_path_args(fname, dname) \
5226 (((fname) = rb_get_path(fname)), \
5227 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5233 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5239 check_expand_path_args(fname, dname);
5240 return expand_path(fname, dname, 0, 1,
Qfalse);
5244rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5246 return expand_path(fname, dname, 0, 0,
Qfalse);
5250rb_file_s_expand_path(
int argc,
const VALUE *argv)
5253 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5299 return rb_file_s_expand_path(c, v);
5305 check_expand_path_args(fname, dname);
5306 return expand_path(fname, dname, 1, 1,
Qfalse);
5310rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5313 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5356 return rb_file_s_absolute_path(c, v);
5384 VALUE path = rb_get_path(fname);
5386 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5390enum rb_realpath_mode {
5394 RB_REALPATH_MODE_MAX
5398realpath_rec(
rb_len_t *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5399 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5401 const char *pend = unresolved + strlen(unresolved);
5405 while (unresolved < pend) {
5406 const char *testname = unresolved;
5407 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5408 rb_len_t testnamelen = unresolved_firstsep - unresolved;
5409 const char *unresolved_nextname = unresolved_firstsep;
5410 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5411 unresolved_nextname++;
5412 unresolved = unresolved_nextname;
5413 if (testnamelen == 1 && testname[0] ==
'.') {
5415 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5416 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5417 bool mb_enc = enc_mbclen_needed(enc);
5418 const char *resolved_str = RSTRING_PTR(*resolvedp);
5419 const char *resolved_names = resolved_str + *prefixlenp;
5420 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5421 rb_len_t len = lastsep ? lastsep - resolved_names : 0;
5422 rb_str_resize(*resolvedp, *prefixlenp +
len);
5428 if (*prefixlenp < RSTRING_LEN(testpath))
5430#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5431 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5432 const char *prefix = RSTRING_PTR(testpath);
5433 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5438 checkval = rb_hash_aref(loopcheck, testpath);
5439 if (!
NIL_P(checkval)) {
5440 if (checkval ==
ID2SYM(resolving)) {
5441 if (mode == RB_REALPATH_CHECK) {
5445 rb_syserr_fail_path(ELOOP, testpath);
5454 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5457 if (e == ENOENT && !
NIL_P(fallback)) {
5458 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5463 if (mode == RB_REALPATH_CHECK)
return -1;
5465 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5466 rb_syserr_fail_path(e, testpath);
5467 *resolvedp = testpath;
5471 rb_syserr_fail_path(e, testpath);
5475 if (S_ISLNK(sbuf.st_mode)) {
5478 const char *link_prefix, *link_names;
5480 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5481 link = rb_readlink(testpath, enc);
5482 link_prefix = RSTRING_PTR(link);
5483 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5484 link_prefixlen = link_names - link_prefix;
5485 if (link_prefixlen > 0) {
5489 tmpenc = fs_enc_check(*resolvedp, link);
5492 *prefixlenp = link_prefixlen;
5494 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5495 loopcheck, mode, !*unresolved_firstsep))
5504 rb_hash_aset(loopcheck, s, s);
5505 *resolvedp = testpath;
5513#ifdef DOSISH_DRIVE_LETTER
5517ospath_for_expand(
VALUE path)
5520 case ENCINDEX_ASCII_8BIT:
5521 case ENCINDEX_US_ASCII:
5522 return rb_enc_associate_index(
rb_str_dup(path), rb_filesystem_encindex());
5524 return TO_OSPATH(path);
5529rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5533 VALUE unresolved_path;
5538 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5539 char *ptr, *prefixptr = NULL, *pend;
5542#ifdef DOSISH_DRIVE_LETTER
5543 VALUE rootdir = path;
5545 if (!
NIL_P(basedir) && skipprefixroot(ptr, ptr +
len, rb_enc_get(path)) == ptr) {
5550 if (
len >= 2 && has_drive_letter(ptr) && (
len == 2 || !isdirsep(ptr[2]))) {
5553 if (!
NIL_P(basedir)) basedir = ospath_for_expand(rb_get_path(basedir));
5554 path = rb_file_absolute_path(ospath_for_expand(path), basedir);
5561 if (!
NIL_P(basedir)) {
5566 enc = rb_enc_get(unresolved_path);
5567 unresolved_path = TO_OSPATH(unresolved_path);
5569 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5570 if (ptr != path_names) {
5571 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5575 if (!
NIL_P(basedir)) {
5577 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5578 if (ptr != basedir_names) {
5584 curdir = rb_dir_getwd_ospath();
5586 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5591 pend = prefixptr + prefixlen;
5592 bool mb_enc = enc_mbclen_needed(enc);
5593 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5595 prefixlen = ++ptr - prefixptr;
5598#ifdef FILE_ALT_SEPARATOR
5599 while (prefixptr < ptr) {
5600 if (*prefixptr == FILE_ALT_SEPARATOR) {
5603 Inc(prefixptr, pend, mb_enc, enc);
5607 switch (rb_enc_to_index(enc)) {
5608 case ENCINDEX_ASCII_8BIT:
5609 case ENCINDEX_US_ASCII:
5610 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5613 loopcheck = rb_hash_new();
5615 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5618 if (basedir_names) {
5619 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5622 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5625 if (origenc && origenc != rb_enc_get(resolved)) {
5627 rb_enc_associate(resolved, origenc);
5641#ifndef HAVE_REALPATH
5643rb_check_realpath_emulate_try(
VALUE arg)
5646 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5650rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5657rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5660 VALUE unresolved_path;
5661 char *resolved_ptr = NULL;
5664 if (mode == RB_REALPATH_DIR) {
5665 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5669 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5670 VALUE paths[2] = {basedir, unresolved_path};
5671 unresolved_path = rb_file_join(2, paths);
5673 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5675 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), NULL)) == NULL) {
5684 if (
errno == ENOTSUP ||
5686 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5687 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5690 if (mode == RB_REALPATH_CHECK) {
5693 rb_sys_fail_path(unresolved_path);
5695 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5698# if !defined(__linux__) && !defined(__APPLE__)
5702 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5703 if (mode == RB_REALPATH_CHECK) {
5706 rb_sys_fail_path(unresolved_path);
5710 if (origenc && origenc != rb_enc_get(resolved)) {
5714 rb_enc_associate(resolved, origenc);
5717 if (is_broken_string(resolved)) {
5718 rb_enc_associate(resolved, rb_filesystem_encoding());
5719 if (is_broken_string(resolved)) {
5720 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5727 if (mode == RB_REALPATH_CHECK) {
5731 arg[2] = (
VALUE)origenc;
5734 rb_check_realpath_emulate_rescue,
Qnil);
5737 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5743rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5745 const enum rb_realpath_mode mode =
5746 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5747 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5753 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5770rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5773 VALUE path = argv[0];
5775 return rb_realpath_internal(basedir, path, 1);
5791rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5794 VALUE path = argv[0];
5796 return rb_realpath_internal(basedir, path, 0);
5804 const char *s, *last;
5806 if (!e || !l2)
return 0;
5808 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5809 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5810 if (c ==
'.')
return l0;
5815 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5820 if (l1 < l2)
return l1;
5823 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5824#if CASEFOLD_FILESYSTEM
5825#define fncomp strncasecmp
5827#define fncomp strncmp
5829 if (fncomp(s, e, l2) == 0) {
5835static inline const char *
5838 const char *p, *q, *e, *end;
5841 rb_len_t len = (alllen ? (size_t)*alllen : strlen(name));
5848 name = skipprefix(name, end, mb_enc, enc);
5849#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5850 const char *root = name;
5853 while (name < end && isdirsep(*name)) {
5860#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5864#ifdef DOSISH_DRIVE_LETTER
5865 else if (*p ==
':') {
5878 p = strrdirsep(name, end, mb_enc, enc);
5883 while (isdirsep(*p)) {
5888 n = ntfs_tail(p, end, mb_enc, enc) - p;
5890 n = chompdirsep(p, end, mb_enc, enc) - p;
5892 for (q = p; q - p < n && *q ==
'.'; q++);
5893 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5894 if (*q ==
'.') e = q;
5916 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5960 const char *name, *p, *fp = 0;
5966 CheckPath(fname, name);
5970 check_path_encoding(fext);
5972 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5973 enc = rb_str_enc_get(fname);
5976 n = RSTRING_LEN(fname);
5977 if (n <= 0 || !*name) {
5978 return rb_enc_str_new(0, 0, enc);
5981 bool mb_enc = enc_mbclen_needed(enc);
5982 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5988 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5993 if (f == RSTRING_LEN(fname)) {
5998 return rb_enc_str_new(p, f, enc);
6001static VALUE rb_file_dirname_n(
VALUE fname,
int n);
6024rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
6030 return rb_file_dirname_n(argv[0], n);
6036 return rb_file_dirname_n(fname, 1);
6040rb_file_dirname_n(
VALUE fname,
int n)
6042 const char *name, *root, *p, *end;
6045 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
6046 CheckPath(fname, name);
6047 end = name + RSTRING_LEN(fname);
6049 bool mb_enc = !rb_str_enc_fastpath(fname);
6052 root = skiproot(name, end);
6054 if (root > name + 1 && isdirsep(*name))
6055 root = skipprefix(name = root - 2, end, mb_enc, enc);
6057 if (root > name + 1)
6060 if (n > (end - root + 1) / 2) {
6066 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
6075 return rb_enc_str_new(
".", 1, enc);
6077#ifdef DOSISH_DRIVE_LETTER
6078 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
6079 const char *top = skiproot(name + 2, end);
6080 dirname = rb_enc_str_new(name, 3, enc);
6085 dirname = rb_enc_str_new(name, p - name, enc);
6086#ifdef DOSISH_DRIVE_LETTER
6087 if (root == name + 2 && p == root && name[1] ==
':')
6093static inline const char *
6096 const char *p, *e, *end = name + (
len ? *
len : (
rb_len_t)strlen(name));
6098 p = strrdirsep(name, end, mb_enc, enc);
6102 do name = ++p;
while (isdirsep(*p));
6105 while (*p && *p ==
'.') p++;
6107 if (*p ==
'.' || istrailinggarbage(*p)) {
6109 const char *last = p++, *dot = last;
6110 while (istrailinggarbage(*p)) {
6111 if (*p ==
'.') dot = p;
6114 if (!*p || isADS(*p)) {
6118 if (*last ==
'.' || dot > last) e = dot;
6125 else if (isADS(*p)) {
6129 else if (isdirsep(*p))
6131 Inc(p, end, mb_enc, enc);
6136 if (!e || e == name)
6161 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
6208 CheckPath(fname, name);
6212 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
6215 bool mb_enc = !rb_str_enc_fastpath(fname);
6218 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
6219 return rb_enc_str_new(ext,
len, enc);
6243 return rb_get_path(fname);
6275file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6277 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6278 return rb_file_join_ary(arg);
6282rb_file_join_ary(
VALUE ary)
6286 const char *name, *tail;
6296 check_path_encoding(tmp);
6297 len += RSTRING_LEN(tmp);
6305 RBASIC_CLEAR_CLASS(result);
6308 switch (OBJ_BUILTIN_TYPE(tmp)) {
6310 if (!checked) check_path_encoding(tmp);
6315 rb_raise(rb_eArgError,
"recursive array");
6327 rb_enc_copy(result, tmp);
6330 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6331 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6334 else if (tail == name +
len) {
6338 enc = fs_enc_check(result, tmp);
6340 rb_enc_associate(result, enc);
6353 for (i = 0; i < argc; i++) {
6354 VALUE tmp = args[i];
6356 size += RSTRING_LEN(tmp);
6369 const char *name = RSTRING_PTR(result);
6370 for (i = 1; i < argc; i++) {
6371 VALUE tmp = args[i];
6378 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6381 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6386 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6393 rb_enc_associate(result, new_enc);
6394 encidx = rb_enc_to_index(new_enc);
6400 rb_str_null_check(result);
6407 if (RB_UNLIKELY(argc == 0)) {
6411 VALUE result = rb_file_join_fastpath(argc, args);
6412 if (RB_LIKELY(result)) {
6432rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6434 return rb_file_join(argc, argv);
6437#if defined(HAVE_TRUNCATE)
6438struct truncate_arg {
6444nogvl_truncate(
void *ptr)
6446 struct truncate_arg *ta = ptr;
6447 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6479 struct truncate_arg ta;
6484 path = rb_str_encode_ospath(path);
6487 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6489 rb_sys_fail_path(path);
6493#define rb_file_s_truncate rb_f_notimplement
6496#if defined(HAVE_FTRUNCATE)
6497struct ftruncate_arg {
6503nogvl_ftruncate(
void *ptr)
6505 struct ftruncate_arg *fa = ptr;
6507 return (
VALUE)ftruncate(fa->fd, fa->pos);
6548 struct ftruncate_arg fa;
6555 rb_io_flush_raw(obj, 0);
6557 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6558 rb_sys_fail_path(fptr->
pathv);
6563#define rb_file_truncate rb_f_notimplement
6580#include <winerror.h>
6584rb_thread_flock(
void *data)
6587 int old_errno =
errno;
6589 int *op = data, ret = flock(op[0], op[1]);
6592 if (GetLastError() == ERROR_NOT_LOCKED) {
6650 op[1] = op1 =
NUM2INT(operation);
6655 rb_io_flush_raw(obj, 0);
6657 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6662#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6665 if (op1 & LOCK_NB)
return Qfalse;
6668 time.tv_usec = 100 * 1000;
6674#if defined(ERESTART)
6680 rb_syserr_fail_path(e, fptr->
pathv);
6687test_check(
int n,
int argc,
VALUE *argv)
6693 for (i=1; i<n; i++) {
6700#define CHECK(n) test_check((n), argc, argv)
6792 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6796 return rb_file_blockdev_p(0, argv[1]);
6799 return rb_file_chardev_p(0, argv[1]);
6802 return rb_file_directory_p(0, argv[1]);
6805 return rb_file_exist_p(0, argv[1]);
6808 return rb_file_file_p(0, argv[1]);
6811 return rb_file_sgid_p(0, argv[1]);
6814 return rb_file_grpowned_p(0, argv[1]);
6817 return rb_file_sticky_p(0, argv[1]);
6820 return rb_file_symlink_p(0, argv[1]);
6823 return rb_file_owned_p(0, argv[1]);
6826 return rb_file_rowned_p(0, argv[1]);
6829 return rb_file_pipe_p(0, argv[1]);
6832 return rb_file_readable_p(0, argv[1]);
6835 return rb_file_readable_real_p(0, argv[1]);
6838 return rb_file_size_p(0, argv[1]);
6841 return rb_file_socket_p(0, argv[1]);
6844 return rb_file_suid_p(0, argv[1]);
6847 return rb_file_writable_p(0, argv[1]);
6850 return rb_file_writable_real_p(0, argv[1]);
6853 return rb_file_executable_p(0, argv[1]);
6856 return rb_file_executable_real_p(0, argv[1]);
6859 return rb_file_zero_p(0, argv[1]);
6863 if (strchr(
"MAC", cmd)) {
6865 VALUE fname = argv[1];
6868 if (
rb_stat(fname, &st) == -1) {
6871 rb_syserr_fail_path(e, fname);
6876 return stat_atime(&st);
6878 return stat_mtime(&st);
6880 return stat_ctime(&st);
6886 return rb_file_identical_p(0, argv[1], argv[2]);
6889 if (strchr(
"=<>", cmd)) {
6890 struct stat st1, st2;
6897 t1 = stat_mtimespec(&st1);
6898 t2 = stat_mtimespec(&st2);
6902 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6906 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6907 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6911 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6912 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6919 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6922 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6971rb_stat_s_alloc(
VALUE klass)
6974 stat_alloc(klass, &obj);
7000 fname = rb_str_encode_ospath(fname);
7002 rb_sys_fail_path(fname);
7009 rb_st->initialized =
true;
7026 *copy_rb_st = *orig_rb_st;
7072rb_stat_ftype(
VALUE obj)
7074 return rb_file_ftype(get_stat(obj)->ST_(mode));
7096 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
7123 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
7160 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
7192 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
7222 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
7250 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7285rb_stat_owned(
VALUE obj)
7287 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7292rb_stat_rowned(
VALUE obj)
7294 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7321rb_stat_grpowned(
VALUE obj)
7324 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7353 rb_io_stat_data *st = get_stat(obj);
7356 if (geteuid() == 0)
return Qtrue;
7359 if (rb_stat_owned(obj))
7360 return RBOOL(st->ST_(mode) & S_IRUSR);
7363 if (rb_stat_grpowned(obj))
7364 return RBOOL(st->ST_(mode) & S_IRGRP);
7367 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7385 rb_io_stat_data *st = get_stat(obj);
7388 if (getuid() == 0)
return Qtrue;
7391 if (rb_stat_rowned(obj))
7392 return RBOOL(st->ST_(mode) & S_IRUSR);
7395 if (rb_group_member(get_stat(obj)->ST_(gid)))
7396 return RBOOL(st->ST_(mode) & S_IRGRP);
7399 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7427rb_stat_wr(
VALUE obj)
7430 rb_io_stat_data *st = get_stat(obj);
7431 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7432 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7464 rb_io_stat_data *st = get_stat(obj);
7467 if (geteuid() == 0)
return Qtrue;
7470 if (rb_stat_owned(obj))
7471 return RBOOL(st->ST_(mode) & S_IWUSR);
7474 if (rb_stat_grpowned(obj))
7475 return RBOOL(st->ST_(mode) & S_IWGRP);
7478 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7499 rb_io_stat_data *st = get_stat(obj);
7502 if (getuid() == 0)
return Qtrue;
7505 if (rb_stat_rowned(obj))
7506 return RBOOL(st->ST_(mode) & S_IWUSR);
7509 if (rb_group_member(get_stat(obj)->ST_(gid)))
7510 return RBOOL(st->ST_(mode) & S_IWGRP);
7513 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7542rb_stat_ww(
VALUE obj)
7545 rb_io_stat_data *st = get_stat(obj);
7546 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7547 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7578 rb_io_stat_data *st = get_stat(obj);
7581 if (geteuid() == 0) {
7582 return RBOOL(st->ST_(mode) & S_IXUGO);
7586 if (rb_stat_owned(obj))
7587 return RBOOL(st->ST_(mode) & S_IXUSR);
7590 if (rb_stat_grpowned(obj))
7591 return RBOOL(st->ST_(mode) & S_IXGRP);
7594 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7611 rb_io_stat_data *st = get_stat(obj);
7614 if (getuid() == 0) {
7615 return RBOOL(st->ST_(mode) & S_IXUGO);
7619 if (rb_stat_rowned(obj))
7620 return RBOOL(st->ST_(mode) & S_IXUSR);
7623 if (rb_group_member(get_stat(obj)->ST_(gid)))
7624 return RBOOL(st->ST_(mode) & S_IXGRP);
7627 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7651 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7696 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7728 rb_off_t size = get_stat(obj)->ST_(size);
7730 if (size == 0)
return Qnil;
7763rb_stat_suid(
VALUE obj)
7766 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7800rb_stat_sgid(
VALUE obj)
7803 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7837rb_stat_sticky(
VALUE obj)
7840 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7845#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7846#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7857nogvl_mkfifo(
void *ptr)
7859 struct mkfifo_arg *ma = ptr;
7861 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7889 struct mkfifo_arg ma;
7898 path = rb_str_encode_ospath(path);
7899 ma.path = RSTRING_PTR(path);
7900 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7901 rb_sys_fail_path(path);
7906#define rb_file_s_mkfifo rb_f_notimplement
7909static VALUE rb_mFConst;
7912rb_file_const(
const char *name,
VALUE value)
7914 rb_define_const(rb_mFConst, name, value);
7918rb_is_absolute_path(
const char *path)
7920#ifdef DOSISH_DRIVE_LETTER
7921 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7924 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7927 if (path[0] ==
'/')
return 1;
7933ruby_is_fd_loadable(
int fd)
7940 if (fstat(fd, &st) < 0)
7943 if (S_ISREG(st.st_mode))
7946 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7949 if (S_ISDIR(st.st_mode))
7960rb_file_load_ok(
const char *path)
7967 int mode = (O_RDONLY |
7968#if defined O_NONBLOCK
7970#elif defined O_NDELAY
7976 if (!rb_gc_for_fd(
errno))
return 0;
7978 if (fd < 0)
return 0;
7981 ret = ruby_is_fd_loadable(fd);
7988is_explicit_relative(
const char *path)
7990 if (*path++ !=
'.')
return 0;
7991 if (*path ==
'.') path++;
7992 return isdirsep(*path);
7998 int encidx = rb_enc_get_index(orig);
7999 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
8000 encidx = rb_filesystem_encindex();
8001 rb_enc_associate_index(path, encidx);
8009nav_component_p(
const char *s,
const char *send)
8011 if ((send - s) >= 2 && s[0] ==
'.') {
8012 return s[1] ==
'.' || isdirsep(s[1]);
8018fname_need_expansion_p(
VALUE fname)
8020 const char *s = RSTRING_PTR(fname);
8022 const char *send = s +
len;
8024 if (nav_component_p(s, send)) {
8029 bool mbenc = enc_mbclen_needed(enc);
8031 s = enc_path_next(s, send, mbenc, enc);
8033 if (nav_component_p(s, send)) {
8037 s = enc_path_next(s, send, mbenc, enc);
8043expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
8045 rb_len_t dname_len = RSTRING_LEN(dname);
8046 const char *dname_ptr = RSTRING_PTR(dname);
8050 if (need_expansion || dname_ptr[0] ==
'~') {
8051 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
8056 if (!isdirsep(dname_ptr[dname_len - 1])) {
8065rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
8068 VALUE fname = *filep;
8072 if (!ext[0])
return 0;
8075 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
8076 f = RSTRING_PTR(fname);
8081 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
8082 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
8083 fnlen = RSTRING_LEN(fname);
8084 for (i=0; ext[i]; i++) {
8086 if (rb_file_load_ok(RSTRING_PTR(fname))) {
8087 *filep = copy_path_class(fname, *filep);
8095 rb_len_t expanded_load_path_maxlen;
8096 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8097 if (!load_path)
return 0;
8100 RBASIC_CLEAR_CLASS(fname);
8101 fnlen = RSTRING_LEN(fname);
8102 bool need_expansion = fname_need_expansion_p(fname);
8105 rb_enc_associate_index(tmp, rb_usascii_encindex());
8107 for (j=0; ext[j]; j++) {
8109 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
8111 if (!RSTRING_LEN(dname))
continue;
8112 expand_feature(fname, dname, tmp, need_expansion);
8114 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8115 *filep = copy_path_class(tmp, *filep);
8121 rb_str_resize(tmp, 0);
8134 path = copy_path_class(file_expand_path_1(path, 0), path);
8135 f = RSTRING_PTR(path);
8139 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
8140 if (!rb_file_load_ok(f))
return 0;
8142 path = copy_path_class(file_expand_path_1(path, 0), path);
8146 rb_len_t expanded_load_path_maxlen;
8147 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8150 bool need_expansion = fname_need_expansion_p(path);
8152 rb_enc_associate_index(tmp, rb_usascii_encindex());
8155 if (!RSTRING_LEN(dname))
continue;
8156 expand_feature(path, dname, tmp, need_expansion);
8158 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8159 return copy_path_class(tmp, path);
8162 rb_str_resize(tmp, 0);
8170#define define_filetest_function(name, func, argc) do { \
8171 rb_define_module_function(rb_mFileTest, name, func, argc); \
8172 rb_define_singleton_method(rb_cFile, name, func, argc); \
8175const char ruby_null_device[] =
8978#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8979 rb_CFString_class_initialize_before_fork();
8987 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8988 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8989 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8990 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8991 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8992 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8993 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8994 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8995 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8996 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8997 define_filetest_function(
"file?", rb_file_file_p, 1);
8998 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8999 define_filetest_function(
"empty?", rb_file_zero_p, 1);
9000 define_filetest_function(
"size?", rb_file_size_p, 1);
9001 define_filetest_function(
"size", rb_file_s_size, 1);
9002 define_filetest_function(
"owned?", rb_file_owned_p, 1);
9003 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
9005 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
9006 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
9007 define_filetest_function(
"socket?", rb_file_socket_p, 1);
9009 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
9010 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
9012 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
9013 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
9014 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
9016 define_filetest_function(
"identical?", rb_file_identical_p, 2);
9054 separator = rb_fstring_lit(
"/");
9056 rb_define_const(
rb_cFile,
"Separator", separator);
9058 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9407 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9410 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9412 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9414 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9416 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9418 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9420 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9421#if defined(O_NDELAY) || defined(O_NONBLOCK)
9423# define O_NONBLOCK O_NDELAY
9426 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9429 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9432 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9438 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9439#ifndef O_SHARE_DELETE
9440# define O_SHARE_DELETE 0
9443 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9446 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9450 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9454 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9458 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9462 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9466 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9470 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9474 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9476 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9478 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9480 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9483 rb_define_const(rb_mFConst,
"NULL", rb_fstring_cstr(ruby_null_device));
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define PATH_SEP
The delimiter of PATH environment variable.
#define GIDT2NUM
Converts a C's gid_t into an instance of rb_cInteger.
#define NUM2GIDT
Converts an instance of rb_cNumeric into C's gid_t.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
#define ENCODING_SET_INLINED(obj, i)
Old name of RB_ENCODING_SET_INLINED.
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
#define T_FILE
Old name of RUBY_T_FILE.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define NUM2ULONG
Old name of RB_NUM2ULONG.
#define ALLOCV
Old name of RB_ALLOCV.
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ID2SYM
Old name of RB_ID2SYM.
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
#define LONG2FIX
Old name of RB_INT2FIX.
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define ISALPHA
Old name of rb_isalpha.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define TOLOWER
Old name of rb_tolower.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
#define ISPRINT
Old name of rb_isprint.
#define NUM2CHR
Old name of RB_NUM2CHR.
#define ENCODING_GET_INLINED(obj)
Old name of RB_ENCODING_GET_INLINED.
#define ENC_CODERANGE_CLEAR(obj)
Old name of RB_ENC_CODERANGE_CLEAR.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eIOError
IOError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Identical to rb_raise(), except it additionally takes an encoding.
VALUE rb_eSystemCallError
SystemCallError exception.
VALUE rb_cObject
Object class.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_cStat
File::Stat class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_mFileTest
FileTest module.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
VALUE rb_mComparable
Comparable module.
VALUE rb_cFile
File class.
VALUE rb_cString
String class.
static char * rb_enc_left_char_head(const char *s, const char *p, const char *e, rb_encoding *enc)
Queries the left boundary of a character.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
VALUE rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new(), except it assumes the passed pointer is a pointer to a C string.
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
VALUE rb_ary_new_from_values(rb_len_t n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define INTEGER_PACK_2COMP
Uses 2's complement representation.
#define INTEGER_PACK_LSWORD_FIRST
Stores/interprets the least significant word as the first word.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_update_max_fd(int fd)
Informs the interpreter that the passed fd can be the max.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
void rb_str_modify_expand(VALUE str, rb_len_t capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_subseq(VALUE str, rb_len_t beg, rb_len_t len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_new_shared(VALUE str)
Identical to rb_str_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_str_plus(VALUE lhs, VALUE rhs)
Generates a new string, concatenating the former to the latter.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(rb_len_t len)
Allocates a "temporary" string.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_str_buf_cat
Just another name of rb_str_cat.
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_ellipsize(VALUE str, rb_len_t len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
VALUE rb_str_cat(VALUE dst, const char *src, rb_len_t srclen)
Destructively appends the passed contents to the string.
VALUE rb_str_replace(VALUE dst, VALUE src)
Replaces the contents of the former object with the stringised contents of the latter.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
VALUE rb_str_buf_new(rb_len_t capa)
Allocates a "string buffer".
#define rb_str_dup_frozen
Just another name of rb_str_new_frozen.
#define rb_utf8_str_new(str, len)
Identical to rb_str_new, except it generates a string of "UTF-8" encoding.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
VALUE rb_time_nano_new(time_t sec, long nsec)
Identical to rb_time_new(), except it accepts the time in nanoseconds resolution.
struct timespec rb_time_timespec(VALUE time)
Identical to rb_time_timeval(), except for return type.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
#define GetOpenFile
This is an old name of RB_IO_POINTER.
#define FMODE_WRITABLE
The IO is opened for writing.
#define RB_IO_POINTER(obj, fp)
Queries the underlying IO pointer.
void rb_io_check_closed(rb_io_t *fptr)
This badly named function asserts that the passed IO is open.
int len
Length of the buffer.
char * ruby_getcwd(void)
This is our own version of getcwd(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
#define MODET2NUM
Converts an instance of rb_cNumeric into C's mode_t.
VALUE rb_rescue(type *q, VALUE w, type *e, VALUE r)
An equivalent of rescue clause.
Defines RBIMPL_ATTR_NONSTRING.
#define RBIMPL_ATTR_NONSTRING()
Wraps (or simulates) __attribute__((nonstring))
#define OFFT2NUM
Converts a C's off_t into an instance of rb_cInteger.
#define NUM2OFFT
Converts an instance of rb_cNumeric into C's off_t.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define StringValue(v)
Ensures that the parameter object is a String.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define FilePathValue(v)
Ensures that the parameter object is a path.
#define errno
Ractor-aware version of errno.
#define FilePathStringValue(v)
This macro actually does the same thing as FilePathValue now.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
Ruby's IO, metadata and buffers.
enum rb_io_mode mode
mode flags: FMODE_XXXs
VALUE pathv
pathname for file
#define UIDT2NUM
Converts a C's uid_t into an instance of rb_cInteger.
#define NUM2UIDT
Converts an instance of rb_cNumeric into C's uid_t.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
#define RBIMPL_WARNING_IGNORED(flag)
Suppresses a warning.
#define RBIMPL_WARNING_PUSH()
Pushes compiler warning state.
#define RBIMPL_WARNING_POP()
Pops compiler warning state.