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)));
851rb_stat_nlink(
VALUE self)
854 const rb_io_stat_data *ptr = get_stat(self);
856 return UIANY2NUM(ptr->ST_(nlink));
870rb_stat_uid(
VALUE self)
872 return UIDT2NUM(get_stat(self)->ST_(uid));
897rb_stat_gid(
VALUE self)
899 return GIDT2NUM(get_stat(self)->ST_(gid));
915rb_stat_rdev(
VALUE self)
918 unsigned int m = get_stat(self)->stx_rdev_major;
919 unsigned int n = get_stat(self)->stx_rdev_minor;
921#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
923#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
924 return DEVT2NUM(get_stat(self)->ST_(rdev));
925#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
926 return ULONG2NUM(get_stat(self)->ST_(rdev));
928 return ULL2NUM(get_stat(self)->ST_(rdev));
943rb_stat_rdev_major(
VALUE self)
946 return UINT2NUM(get_stat(self)->stx_rdev_major);
947#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
948 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
965rb_stat_rdev_minor(
VALUE self)
968 return UINT2NUM(get_stat(self)->stx_rdev_minor);
969#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
970 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
1006rb_stat_size(
VALUE self)
1008 return OFFT2NUM(get_stat(self)->ST_(size));
1030rb_stat_blksize(
VALUE self)
1032#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1033 return ULONG2NUM(get_stat(self)->ST_(blksize));
1057rb_stat_blocks(
VALUE self)
1059#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1060# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
1061 return ULL2NUM(get_stat(self)->ST_(blocks));
1063 return ULONG2NUM(get_stat(self)->ST_(blocks));
1071stat_atimespec(
const struct stat *st)
1074 ts.tv_sec = st->st_atime;
1075#if defined(HAVE_STRUCT_STAT_ST_ATIM)
1076 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
1077#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1078 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
1079#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
1080 ts.tv_nsec = (uint32_t)st->st_atimensec;
1089statx_atimespec(
const rb_io_stat_data *st)
1091 return st->stx_atime;
1094# define statx_atimespec stat_atimespec
1104stat_atime(
const struct stat *st)
1106 return stat_time(stat_atimespec(st));
1110stat_mtimespec(
const struct stat *st)
1113 ts.tv_sec = st->st_mtime;
1114#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1115 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1116#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1117 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1118#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1119 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1127stat_mtime(
const struct stat *st)
1129 return stat_time(stat_mtimespec(st));
1133stat_ctimespec(
const struct stat *st)
1136 ts.tv_sec = st->st_ctime;
1137#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1138 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1139#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1140 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1141#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1142 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1151statx_ctimespec(
const rb_io_stat_data *st)
1153 return st->stx_ctime;
1156# define statx_ctimespec stat_ctimespec
1160stat_ctime(
const struct stat *st)
1162 return stat_time(stat_ctimespec(st));
1165#define HAVE_STAT_BIRTHTIME
1166#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1168statx_birthtime(
const rb_io_stat_data *st)
1173#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1174static VALUE statx_birthtime(
const rb_io_stat_data *st);
1175#elif defined(_WIN32)
1176# define statx_birthtime stat_ctime
1178# undef HAVE_STAT_BIRTHTIME
1227rb_stat_atime(
VALUE self)
1229 return stat_time(statx_atimespec(get_stat(self)));
1259rb_stat_mtime(
VALUE self)
1261 return stat_time(statx_mtimespec(get_stat(self)));
1298rb_stat_ctime(
VALUE self)
1300 return stat_time(statx_ctimespec(get_stat(self)));
1303#if defined(HAVE_STAT_BIRTHTIME)
1326rb_stat_birthtime(
VALUE self)
1328 return statx_birthtime(get_stat(self));
1331# define rb_stat_birthtime rb_f_notimplement
1391rb_stat_inspect(
VALUE self)
1395 static const struct {
1399 {
"dev", rb_stat_dev},
1400 {
"ino", rb_stat_ino},
1401 {
"mode", rb_stat_mode},
1402 {
"nlink", rb_stat_nlink},
1403 {
"uid", rb_stat_uid},
1404 {
"gid", rb_stat_gid},
1405 {
"rdev", rb_stat_rdev},
1406 {
"size", rb_stat_size},
1407 {
"blksize", rb_stat_blksize},
1408 {
"blocks", rb_stat_blocks},
1409 {
"atime", rb_stat_atime},
1410 {
"mtime", rb_stat_mtime},
1411 {
"ctime", rb_stat_ctime},
1412#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1413 {
"birthtime", rb_stat_birthtime},
1419 if (!rb_st->initialized) {
1427 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1435 v = (*member[i].func)(self);
1437 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1439 else if (i == 0 || i == 6) {
1440 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1460no_gvl_fstat(
void *data)
1463 return (
VALUE)fstat(arg->file.fd, arg->st);
1467fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1471 data.file.fd = fptr->
fd;
1474 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1478no_gvl_stat(
void * data)
1481 return (
void *)(
VALUE)stat(arg->file.path, arg->st);
1485stat_without_gvl(
const char *path,
struct stat *st)
1489 data.file.path = path;
1492 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1495#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1496 defined(HAVE_STRUCT_STATX_STX_BTIME)
1498# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1501# ifdef HAVE_SYSCALL_H
1502# include <syscall.h>
1503# elif defined HAVE_SYS_SYSCALL_H
1504# include <sys/syscall.h>
1506# if defined __linux__
1507# include <linux/stat.h>
1509statx(
int dirfd,
const char *pathname,
int flags,
1510 unsigned int mask,
struct statx *statxbuf)
1512 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1517typedef struct no_gvl_rb_io_stat_data {
1523} no_gvl_rb_io_stat_data;
1526io_blocking_statx(
void *data)
1528 no_gvl_rb_io_stat_data *arg = data;
1529 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1533no_gvl_statx(
void *data)
1535 return (
void *)io_blocking_statx(data);
1539statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1541 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1544 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1548lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1550 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1553 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1557fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1559 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1562 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1565#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1568rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1573 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1578 result = fstatx_without_gvl(fptr, stx, mask);
1583 file = rb_str_encode_ospath(file);
1584 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1590# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1592NORETURN(
static void statx_notimplement(
const char *field_name));
1597statx_notimplement(
const char *field_name)
1600 "%s is unimplemented on this filesystem",
1605statx_birthtime(
const rb_io_stat_data *stx)
1607 if (!statx_has_birthtime(stx)) {
1609 statx_notimplement(
"birthtime");
1611 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1616# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1617# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1618# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1619# define rb_statx(file, stx, mask) rb_stat(file, stx)
1620# define STATX(path, st, mask) stat(path, st)
1622#if defined(HAVE_STAT_BIRTHTIME)
1623# define statx_has_birthtime(st) 1
1625# define statx_has_birthtime(st) 0
1632# define FSTAT(fd, st) fstat(fd, st)
1641 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1646 result = fstat_without_gvl(fptr, st);
1651 file = rb_str_encode_ospath(file);
1652 result = stat_without_gvl(RSTRING_PTR(file), st);
1690 fname = rb_str_encode_ospath(fname);
1691 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1692 rb_sys_fail_path(fname);
1694 return rb_statx_new(&st);
1726rb_io_stat(
VALUE obj)
1732 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1733 rb_sys_fail_path(fptr->
pathv);
1735 return rb_statx_new(&st);
1740no_gvl_lstat(
void *ptr)
1743 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1747lstat_without_gvl(
const char *path,
struct stat *st)
1751 data.file.path = path;
1754 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1791 fname = rb_str_encode_ospath(fname);
1792 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1793 rb_sys_fail_path(fname);
1795 return rb_statx_new(&st);
1797 return rb_file_s_stat(klass, fname);
1830rb_file_lstat(
VALUE obj)
1839 path = rb_str_encode_ospath(fptr->
pathv);
1840 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1841 rb_sys_fail_path(fptr->
pathv);
1843 return rb_statx_new(&st);
1845 return rb_io_stat(obj);
1850rb_group_member(GETGROUPS_T gid)
1852#if !defined(HAVE_GETGROUPS)
1861 if (getgid() == gid || getegid() == gid)
1864 groups = getgroups(0, NULL);
1865 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1866 anum = getgroups(groups, gary);
1867 while (--anum >= 0) {
1868 if (gary[anum] == gid) {
1881# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1884#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1885#define USE_GETEUID 1
1890eaccess(
const char *path,
int mode)
1899 if (getuid() == euid && getgid() == getegid())
1900 return access(path, mode);
1902 if (stat(path, &st) < 0)
1912 if (st.st_mode & S_IXUGO)
1918 if (st.st_uid == euid)
1920 else if (rb_group_member(st.st_gid))
1923 if ((
int)(st.st_mode & mode) == mode)
return 0;
1927 return access(path, mode);
1938nogvl_eaccess(
void *ptr)
1942 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1946rb_eaccess(
VALUE fname,
int mode)
1951 fname = rb_str_encode_ospath(fname);
1955 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1959nogvl_access(
void *ptr)
1963 return (
void *)(
VALUE)access(aa->path, aa->mode);
1967rb_access(
VALUE fname,
int mode)
1972 fname = rb_str_encode_ospath(fname);
1976 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
2011# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
2017 if (S_ISDIR(st.st_mode))
return Qtrue;
2046# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
2052 if (S_ISFIFO(st.st_mode))
return Qtrue;
2084# define S_ISLNK(m) _S_ISLNK(m)
2087# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
2090# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
2100 fname = rb_str_encode_ospath(fname);
2102 if (S_ISLNK(st.st_mode))
return Qtrue;
2137# define S_ISSOCK(m) _S_ISSOCK(m)
2140# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
2143# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
2153 if (S_ISSOCK(st.st_mode))
return Qtrue;
2185# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
2187# define S_ISBLK(m) (0)
2195 if (S_ISBLK(st.st_mode))
return Qtrue;
2222# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2228 if (S_ISCHR(st.st_mode))
return Qtrue;
2298 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2312rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2314 return RBOOL(rb_access(fname, R_OK) >= 0);
2318# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2322# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2351rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2357 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2358 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2392 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2409rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2411 return RBOOL(rb_access(fname, W_OK) >= 0);
2440rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2446 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2447 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2477 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2491rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2493 return RBOOL(rb_access(fname, X_OK) >= 0);
2497# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2525 return RBOOL(S_ISREG(st.st_mode));
2585 return RBOOL(st.st_size == 0);
2623 if (st.st_size == 0)
return Qnil;
2658 return RBOOL(st.st_uid == geteuid());
2667 return RBOOL(st.st_uid == getuid());
2703 if (rb_group_member(st.st_gid))
return Qtrue;
2708#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2710check3rdbyte(
VALUE fname,
int mode)
2715 return RBOOL(st.st_mode & mode);
2748 return check3rdbyte(fname, S_ISUID);
2783 return check3rdbyte(fname, S_ISGID);
2819 return check3rdbyte(fname, S_ISVTX);
2856 struct stat st1, st2;
2860 if (st1.st_dev != st2.st_dev)
return Qfalse;
2861 if (st1.st_ino != st2.st_ino)
return Qfalse;
2865 return rb_w32_file_identical_p(fname1, fname2);
2891 if (
rb_stat(fname, &st) < 0) {
2894 rb_syserr_fail_path(e, fname);
2900rb_file_ftype(mode_t mode)
2904 if (S_ISREG(mode)) {
2907 else if (S_ISDIR(mode)) {
2910 else if (S_ISCHR(mode)) {
2911 t =
"characterSpecial";
2914 else if (S_ISBLK(mode)) {
2919 else if (S_ISFIFO(mode)) {
2924 else if (S_ISLNK(mode)) {
2929 else if (S_ISSOCK(mode)) {
2937 return rb_fstring_cstr(t);
2986 fname = rb_str_encode_ospath(fname);
2988 rb_sys_fail_path(fname);
2991 return rb_file_ftype(st.st_mode);
3047 if (
rb_stat(fname, &st) < 0) {
3050 rb_syserr_fail_path(e, fname);
3052 return stat_time(stat_atimespec(&st));
3084rb_file_atime(
VALUE obj)
3090 if (fstat(fptr->
fd, &st) == -1) {
3091 rb_sys_fail_path(fptr->
pathv);
3093 return stat_time(stat_atimespec(&st));
3133 if (
rb_stat(fname, &st) < 0) {
3136 rb_syserr_fail_path(e, fname);
3138 return stat_time(stat_mtimespec(&st));
3166rb_file_mtime(
VALUE obj)
3172 if (fstat(fptr->
fd, &st) == -1) {
3173 rb_sys_fail_path(fptr->
pathv);
3175 return stat_time(stat_mtimespec(&st));
3219 if (
rb_stat(fname, &st) < 0) {
3222 rb_syserr_fail_path(e, fname);
3224 return stat_time(stat_ctimespec(&st));
3261rb_file_ctime(
VALUE obj)
3267 if (fstat(fptr->
fd, &st) == -1) {
3268 rb_sys_fail_path(fptr->
pathv);
3270 return stat_time(stat_ctimespec(&st));
3273#if defined(HAVE_STAT_BIRTHTIME)
3299 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3302 rb_syserr_fail_path(e, fname);
3304 return statx_birthtime(&st);
3307# define rb_file_s_birthtime rb_f_notimplement
3310#if defined(HAVE_STAT_BIRTHTIME)
3331rb_file_birthtime(
VALUE obj)
3337 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3338 rb_sys_fail_path(fptr->
pathv);
3340 return statx_birthtime(&st);
3343# define rb_file_birthtime rb_f_notimplement
3355 rb_io_flush_raw(file, 0);
3358 if (fstat(fptr->
fd, &st) == -1) {
3359 rb_sys_fail_path(fptr->
pathv);
3385file_size(
VALUE self)
3387 return OFFT2NUM(rb_file_size(self));
3396nogvl_chmod(
void *ptr)
3399 int ret = chmod(data->path, data->mode);
3400 return (
void *)(
VALUE)ret;
3404rb_chmod(
const char *path, mode_t mode)
3410 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3414chmod_internal(
const char *path,
void *mode)
3416 return chmod(path, *(mode_t *)mode);
3449 return apply2files(chmod_internal, argc, argv, &mode);
3453struct nogvl_fchmod_data {
3459io_blocking_fchmod(
void *ptr)
3461 struct nogvl_fchmod_data *data = ptr;
3462 int ret = fchmod(data->fd, data->mode);
3467rb_fchmod(
struct rb_io* io, mode_t mode)
3470 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3471 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3500#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3508 if (rb_fchmod(fptr, mode) == -1) {
3509 if (HAVE_FCHMOD ||
errno != ENOSYS)
3510 rb_sys_fail_path(fptr->
pathv);
3513 if (!HAVE_FCHMOD)
return INT2FIX(0);
3516#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3518 path = rb_str_encode_ospath(fptr->
pathv);
3519 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3520 rb_sys_fail_path(fptr->
pathv);
3526#if defined(HAVE_LCHMOD)
3528lchmod_internal(
const char *path,
void *mode)
3530 return lchmod(path, *(mode_t *)mode);
3567 return apply2files(lchmod_internal, argc, argv, &mode);
3570#define rb_file_s_lchmod rb_f_notimplement
3573static inline rb_uid_t
3577 return (rb_uid_t)-1;
3582static inline rb_gid_t
3586 return (rb_gid_t)-1;
3597chown_internal(
const char *path,
void *arg)
3600 return chown(path, args->owner, args->group);
3654 arg.owner = to_uid(*argv++);
3655 arg.group = to_gid(*argv++);
3657 return apply2files(chown_internal, argc, argv, &arg);
3669nogvl_chown(
void *ptr)
3672 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3676rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3679 .as = {.path = path},
3680 .new = {.owner = owner, .group = group},
3682 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3687nogvl_fchown(
void *ptr)
3690 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3694rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3699 .new = {.owner = owner, .group = group},
3701 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3735 path = rb_str_encode_ospath(fptr->
pathv);
3736 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3737 rb_sys_fail_path(fptr->
pathv);
3739 if (rb_fchown(fptr->
fd, o, g) == -1)
3740 rb_sys_fail_path(fptr->
pathv);
3746#if defined(HAVE_LCHOWN)
3748lchown_internal(
const char *path,
void *arg)
3751 return lchown(path, args->owner, args->group);
3808 arg.owner = to_uid(*argv++);
3809 arg.group = to_gid(*argv++);
3811 return apply2files(lchown_internal, argc, argv, &arg);
3814#define rb_file_s_lchown rb_f_notimplement
3824NORETURN(
static void utime_failed(
struct apply_arg *));
3830 VALUE path = aa->fn[aa->i].path;
3833 if (ua->tsp && e == EINVAL) {
3836 VALUE atime = ua->atime;
3837 VALUE mtime = ua->mtime;
3839 if (!
NIL_P(atime)) {
3842 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3845 if (
NIL_P(a)) e[0] = m;
3861 rb_syserr_fail_path(e, path);
3865#if defined(HAVE_UTIMES)
3867# if !defined(HAVE_UTIMENSAT)
3869# elif defined(__APPLE__) && \
3870 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3872# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3873typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3877static inline utimensat_func *
3884# define utimensat rb_utimensat()
3891utime_internal(
const char *path,
void *arg)
3894 const struct timespec *tsp = v->tsp;
3895 struct timeval tvbuf[2], *tvp = NULL;
3897#if defined(HAVE_UTIMENSAT)
3898# if defined(__APPLE__)
3899 const int try_utimensat = utimensat != NULL;
3900 const int try_utimensat_follow = utimensat != NULL;
3902# define TRY_UTIMENSAT 1
3903 static int try_utimensat = 1;
3904# ifdef AT_SYMLINK_NOFOLLOW
3905 static int try_utimensat_follow = 1;
3907 const int try_utimensat_follow = 0;
3912 if (v->follow ? try_utimensat_follow : try_utimensat) {
3913# ifdef AT_SYMLINK_NOFOLLOW
3915 flags = AT_SYMLINK_NOFOLLOW;
3919 int result = utimensat(AT_FDCWD, path, tsp, flags);
3920# ifdef TRY_UTIMENSAT
3921 if (result < 0 &&
errno == ENOSYS) {
3922# ifdef AT_SYMLINK_NOFOLLOW
3923 try_utimensat_follow = 0;
3935 tvbuf[0].tv_sec = tsp[0].tv_sec;
3936 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3937 tvbuf[1].tv_sec = tsp[1].tv_sec;
3938 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3942 if (v->follow)
return lutimes(path, tvp);
3944 return utimes(path, tvp);
3949#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3957utime_internal(
const char *path,
void *arg)
3961 struct utimbuf utbuf, *utp = NULL;
3963 utbuf.actime = tsp[0].tv_sec;
3964 utbuf.modtime = tsp[1].tv_sec;
3967 return utime(path, utp);
3972utime_internal_i(
int argc,
VALUE *argv,
int follow)
3975 struct timespec tss[2], *tsp = NULL;
3978 args.atime = *argv++;
3979 args.mtime = *argv++;
3981 args.follow = follow;
3983 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3986 if (args.atime == args.mtime)
3993 return apply2files(utime_internal, argc, argv, &args);
4039 return utime_internal_i(argc, argv, FALSE);
4042#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
4100 return utime_internal_i(argc, argv, TRUE);
4103#define rb_file_s_lutime rb_f_notimplement
4106#ifdef RUBY_FUNCTION_NAME_STRING
4107# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
4109# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
4111#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
4112NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
4114syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
4118 const int max_pathlen = MAX_PATH;
4120 const int max_pathlen = MAXPATHLEN;
4131#ifdef RUBY_FUNCTION_NAME_STRING
4132 rb_syserr_fail_path_in(func, e, str);
4134 rb_syserr_fail_path(e, str);
4169 from = rb_str_encode_ospath(from);
4170 to = rb_str_encode_ospath(to);
4173 sys_fail2(from, to);
4178#define rb_file_s_link rb_f_notimplement
4219 from = rb_str_encode_ospath(from);
4220 to = rb_str_encode_ospath(to);
4223 sys_fail2(from, to);
4228#define rb_file_s_symlink rb_f_notimplement
4256 return rb_readlink(path, rb_filesystem_encoding());
4259struct readlink_arg {
4266nogvl_readlink(
void *ptr)
4268 struct readlink_arg *ra = ptr;
4270 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4274readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4276 struct readlink_arg ra;
4278 ra.path = RSTRING_PTR(path);
4279 ra.buf = RSTRING_PTR(buf);
4282 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4293 path = rb_str_encode_ospath(path);
4294 v = rb_enc_str_new(0, size, enc);
4295 while ((rv = readlink_without_gvl(path, v, size)) == size
4297 || (rv < 0 &&
errno == ERANGE)
4306 rb_str_resize(v, 0);
4307 rb_syserr_fail_path(e, path);
4309 rb_str_resize(v, rv);
4314#define rb_file_s_readlink rb_f_notimplement
4318unlink_internal(
const char *path,
void *arg)
4320 return unlink(path);
4363rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4365 return apply2files(unlink_internal, argc, argv, 0);
4374no_gvl_rename(
void *ptr)
4378 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4456 f = rb_str_encode_ospath(from);
4457 t = rb_str_encode_ospath(to);
4460#if defined __CYGWIN__
4463 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4468 if (chmod(ra.dst, 0666) == 0 &&
4469 unlink(ra.dst) == 0 &&
4470 rename(ra.src, ra.dst) == 0)
4474 syserr_fail2(e, from, to);
4508 rb_error_arity(argc, 0, 1);
4516#if defined __CYGWIN__ || defined DOSISH
4518#define DOSISH_DRIVE_LETTER
4519#define FILE_ALT_SEPARATOR '\\'
4521#ifdef FILE_ALT_SEPARATOR
4522#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4524static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4527#define isdirsep(x) ((x) == '/')
4539#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4540#define isADS(x) ((x) == ':')
4542#define istrailinggarbage(x) 0
4545#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4547#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4548#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4550#if defined(DOSISH_UNC)
4551#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4553#define has_unc(buf) 0
4556#ifdef DOSISH_DRIVE_LETTER
4558has_drive_letter(
const char *buf)
4560 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4585 if (chdir(drive) == 0) {
4586 drvcwd = rb_dir_getwd_ospath();
4598not_same_drive(
VALUE path,
int drive)
4600 const char *p = RSTRING_PTR(path);
4601 if (RSTRING_LEN(path) < 2)
return 0;
4602 if (has_drive_letter(p)) {
4613skiproot(
const char *path,
const char *end)
4615#ifdef DOSISH_DRIVE_LETTER
4616 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4618 while (path < end && isdirsep(*path)) path++;
4619 return (
char *)path;
4623enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4625 while (s < e && !isdirsep(*s)) {
4626 Inc(s, e, mb_enc, enc);
4631#define nextdirsep rb_enc_path_next
4635 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4638#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4639#define skipprefix enc_path_skip_prefix
4641#define skipprefix(path, end, mb_enc, enc) (path)
4644enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4646#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4648 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4650 while (path < end && isdirsep(*path)) path++;
4651 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4652 path + 2 <= end && !isdirsep(path[1])) {
4653 path = enc_path_next(path + 1, end, mb_enc, enc);
4655 return (
char *)path;
4658#ifdef DOSISH_DRIVE_LETTER
4659 if (path + 2 <= end && has_drive_letter(path))
4660 return (
char *)(path + 2);
4663 return (
char *)path;
4667rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4669 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4673skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4675#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4676 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4677 while (p < end && isdirsep(*p)) p++;
4680 return skiproot(path, end);
4685rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4687 return skipprefixroot(path, end, enc);
4691enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4694 while (path < end) {
4695 if (isdirsep(*path)) {
4696 const char *tmp = path++;
4697 while (path < end && isdirsep(*path)) path++;
4698 if (path >= end)
break;
4702 Inc(path, end, mb_enc, enc);
4708rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4710 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4714strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4716 if (RB_UNLIKELY(mb_enc)) {
4717 return enc_path_last_separator(path, end, mb_enc, enc);
4720 const char *cursor = end - 1;
4722 while (cursor >= path && isdirsep(cursor[0])) {
4726 while (cursor >= path) {
4727 if (isdirsep(cursor[0])) {
4728 while (cursor > path && isdirsep(cursor[-1])) {
4731 return (
char *)cursor;
4739chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4741 while (path < end) {
4742 if (isdirsep(*path)) {
4743 const char *last = path++;
4744 while (path < end && isdirsep(*path)) path++;
4745 if (path >= end)
return (
char *)last;
4748 Inc(path, end, mb_enc, enc);
4751 return (
char *)path;
4757 if (path < end && isdirsep(*path)) path++;
4758 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4764 rb_encoding *enc = rb_enc_check_str(path1, path2);
4765 int encidx = rb_enc_to_index(enc);
4766 if (encidx == ENCINDEX_US_ASCII) {
4767 encidx = rb_enc_get_index(path1);
4768 if (encidx == ENCINDEX_US_ASCII)
4769 encidx = rb_enc_get_index(path2);
4770 enc = rb_enc_from_index(encidx);
4777ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4779 while (path < end && *path ==
'.') path++;
4780 while (path < end && !isADS(*path)) {
4781 if (istrailinggarbage(*path)) {
4782 const char *last = path++;
4783 while (path < end && istrailinggarbage(*path)) path++;
4784 if (path >= end || isADS(*path))
return (
char *)last;
4786 else if (isdirsep(*path)) {
4787 const char *last = path++;
4788 while (path < end && isdirsep(*path)) path++;
4789 if (path >= end)
return (
char *)last;
4790 if (isADS(*path)) path++;
4793 Inc(path, end, mb_enc, enc);
4796 return (
char *)path;
4800#define BUFCHECK(cond) do {\
4803 do {buflen *= 2;} while (cond);\
4804 rb_str_resize(result, buflen);\
4805 buf = RSTRING_PTR(result);\
4807 pend = buf + buflen;\
4811#define BUFINIT(result, buf, p, pend) do {\
4812 if (!result) { result = rb_usascii_str_new(0, 1); } \
4813 p = buf = RSTRING_PTR(result); \
4814 buflen = RSTRING_LEN(result); \
4815 pend = p + buflen; \
4819# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4821# define SKIPPATHSEP(p) 1
4824#define BUFCOPY(srcptr, srclen) do { \
4825 const int skip = SKIPPATHSEP(p); \
4826 rb_str_set_len(result, p-buf+skip); \
4827 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4829 memcpy(p, (srcptr), (srclen)); \
4833#define WITH_ROOTDIFF(stmt) do { \
4834 rb_len_t rootdiff = root - buf; \
4836 root = buf + rootdiff; \
4840copy_home_path(
VALUE result,
const char *dir)
4846 dirlen = strlen(dir);
4847 rb_str_resize(result, dirlen);
4848 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4849 encidx = rb_filesystem_encindex();
4850 rb_enc_associate_index(result, encidx);
4851#if defined FILE_ALT_SEPARATOR
4853 bool mb_enc = enc_mbclen_needed(enc);
4854 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4855 if (*p == FILE_ALT_SEPARATOR) {
4867 VALUE dirname = rb_getpwdirnam_for_login(user);
4868 if (dirname ==
Qnil) {
4869 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4871 const char *dir = RSTRING_PTR(dirname);
4873 extern char *getlogin(
void);
4874 const char *pwPtr = 0;
4876 # define endpwent() ((void)0)
4877 const char *dir, *username = RSTRING_PTR(user);
4888 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4889 dir = pwPtr = getenv(
"HOME");
4891 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4894 copy_home_path(result, dir);
4900rb_default_home_dir(
VALUE result)
4902 const char *dir = getenv(
"HOME");
4904#if defined HAVE_PWD_H
4918 VALUE login_name = rb_getlogin();
4920# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4925 if (
NIL_P(login_name)) {
4926 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4930 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4931 if (
NIL_P(pw_dir)) {
4932 pw_dir = rb_getpwdiruid();
4933 if (
NIL_P(pw_dir)) {
4934 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4939 copy_home_path(result, RSTRING_PTR(pw_dir));
4940 rb_str_resize(pw_dir, 0);
4945 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4947 return copy_home_path(result, dir);
4953#if NORMALIZE_UTF8PATH
4954 VALUE path = rb_str_normalize_ospath(ptr,
len);
4955 rb_enc_associate(path, fsenc);
4958 return rb_enc_str_new(ptr,
len, fsenc);
4965 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4967 rb_enc_associate(dirname, fsenc);
4971 size_t dirlen = RSTRING_LEN(dirname);
4974 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4975 if (!rb_enc_compatible(fname, dirname)) {
4978 rb_enc_check(fname, dirname);
4979 rb_bug(
"unreachable");
4981 rb_encoding *direnc = fs_enc_check(fname, dirname);
4982 if (direnc != fsenc) {
4989 do {buflen *= 2;}
while (dirlen > buflen);
4990 rb_str_resize(result, buflen);
4991 buf = RSTRING_PTR(result);
4992 memcpy(buf, cwdp, dirlen);
4993 rb_enc_associate(result, *enc);
4994 return buf + dirlen;
4998rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
5000 const char *s, *b, *fend;
5001 char *buf, *p, *pend, *root;
5002 size_t buflen, bdiff;
5003 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
5006 fend = s + RSTRING_LEN(fname);
5007 enc = rb_str_enc_get(fname);
5008 bool mb_enc = enc_mbclen_needed(enc);
5009 if (!mb_enc &&
RTEST(dname)) {
5010 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
5013 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
5014 BUFINIT(result, buf, p, pend);
5017 if (s + 1 == fend || isdirsep(s[1])) {
5021 if (++s < fend) ++s;
5022 rb_default_home_dir(result);
5025 s = nextdirsep(b = s, fend, enc);
5028 BUFCHECK(bdiff + userlen >= buflen);
5029 memcpy(p, b, userlen);
5032 rb_enc_associate(result, enc);
5033 rb_home_dir_of(result, result);
5037 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
5039 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
5040 (
int)userlen, b, fname);
5043 rb_raise(rb_eArgError,
"non-absolute home");
5046 BUFINIT(result, buf, p, pend);
5049#ifdef DOSISH_DRIVE_LETTER
5051 else if (s + 1 < fend && has_drive_letter(s)) {
5052 BUFINIT(result, buf, p, pend);
5054 if (s + 2 < fend && isdirsep(s[2])) {
5057 BUFCHECK(bdiff + 2 >= buflen);
5061 rb_enc_copy(result, fname);
5066 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
5067 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
5068 BUFINIT(result, buf, p, pend);
5075 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
5076 BUFINIT(result, buf, p, pend);
5080 rb_enc_associate(result, enc = fs_enc_check(result, fname));
5083 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
5088 else if (s == fend || !rb_is_absolute_path(s)) {
5090 if (!
NIL_P(dname)) {
5092 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
5096 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
5098 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
5103 result = resized_result;
5107 rb_enc_associate(result, fs_enc_check(result, fname));
5108 BUFINIT(result, buf, p, pend);
5112 VALUE cwd = rb_dir_getwd_ospath();
5116 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
5117 BUFINIT(result, buf, p, pend);
5120#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5121 if (s < fend && isdirsep(*s)) {
5124 p = skipprefix(buf, p, mb_enc, enc);
5128 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
5131 BUFINIT(result, buf, p, pend);
5135 do s++;
while (s < fend && isdirsep(*s));
5138 BUFCHECK(bdiff >= buflen);
5139 memset(buf,
'/',
len);
5141 rb_enc_associate(result, fs_enc_check(result, fname));
5143 if (p > buf && p[-1] ==
'/')
5147 BUFCHECK(bdiff + 1 >= buflen);
5152 BUFCHECK(bdiff + 1 >= buflen);
5154 root = skipprefix(buf, p+1, mb_enc, enc);
5167 if (s+1 == fend || isdirsep(*(s+1))) {
5171 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
5181#if defined FILE_ALT_SEPARATOR
5182 case FILE_ALT_SEPARATOR:
5193#if defined FILE_ALT_SEPARATOR
5194 case FILE_ALT_SEPARATOR:
5197 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5205 int n = ignored_char_p(s, fend, enc);
5208 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5216 Inc(s, fend, mb_enc, enc);
5225 if (p == skiproot(buf, p + !!*p) - 1) p++;
5228 rb_enc_check(fname, result);
5235str_shrink(
VALUE str)
5237 rb_str_resize(str, RSTRING_LEN(str));
5241#define expand_path(fname, dname, abs_mode, long_name, result) \
5242 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5244#define check_expand_path_args(fname, dname) \
5245 (((fname) = rb_get_path(fname)), \
5246 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5252 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5258 check_expand_path_args(fname, dname);
5259 return expand_path(fname, dname, 0, 1,
Qfalse);
5263rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5265 return expand_path(fname, dname, 0, 0,
Qfalse);
5269rb_file_s_expand_path(
int argc,
const VALUE *argv)
5272 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5318 return rb_file_s_expand_path(c, v);
5324 check_expand_path_args(fname, dname);
5325 return expand_path(fname, dname, 1, 1,
Qfalse);
5329rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5332 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5375 return rb_file_s_absolute_path(c, v);
5403 VALUE path = rb_get_path(fname);
5405 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5409enum rb_realpath_mode {
5413 RB_REALPATH_MODE_MAX
5417realpath_rec(
rb_len_t *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5418 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5420 const char *pend = unresolved + strlen(unresolved);
5424 while (unresolved < pend) {
5425 const char *testname = unresolved;
5426 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5427 rb_len_t testnamelen = unresolved_firstsep - unresolved;
5428 const char *unresolved_nextname = unresolved_firstsep;
5429 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5430 unresolved_nextname++;
5431 unresolved = unresolved_nextname;
5432 if (testnamelen == 1 && testname[0] ==
'.') {
5434 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5435 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5436 bool mb_enc = enc_mbclen_needed(enc);
5437 const char *resolved_str = RSTRING_PTR(*resolvedp);
5438 const char *resolved_names = resolved_str + *prefixlenp;
5439 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5440 rb_len_t len = lastsep ? lastsep - resolved_names : 0;
5441 rb_str_resize(*resolvedp, *prefixlenp +
len);
5447 if (*prefixlenp < RSTRING_LEN(testpath))
5449#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5450 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5451 const char *prefix = RSTRING_PTR(testpath);
5452 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5457 checkval = rb_hash_aref(loopcheck, testpath);
5458 if (!
NIL_P(checkval)) {
5459 if (checkval ==
ID2SYM(resolving)) {
5460 if (mode == RB_REALPATH_CHECK) {
5464 rb_syserr_fail_path(ELOOP, testpath);
5473 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5476 if (e == ENOENT && !
NIL_P(fallback)) {
5477 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5482 if (mode == RB_REALPATH_CHECK)
return -1;
5484 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5485 rb_syserr_fail_path(e, testpath);
5486 *resolvedp = testpath;
5490 rb_syserr_fail_path(e, testpath);
5494 if (S_ISLNK(sbuf.st_mode)) {
5497 const char *link_prefix, *link_names;
5499 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5500 link = rb_readlink(testpath, enc);
5501 link_prefix = RSTRING_PTR(link);
5502 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5503 link_prefixlen = link_names - link_prefix;
5504 if (link_prefixlen > 0) {
5508 tmpenc = fs_enc_check(*resolvedp, link);
5511 *prefixlenp = link_prefixlen;
5513 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5514 loopcheck, mode, !*unresolved_firstsep))
5523 rb_hash_aset(loopcheck, s, s);
5524 *resolvedp = testpath;
5532#ifdef DOSISH_DRIVE_LETTER
5536ospath_for_expand(
VALUE path)
5539 case ENCINDEX_ASCII_8BIT:
5540 case ENCINDEX_US_ASCII:
5541 return rb_enc_associate_index(
rb_str_dup(path), rb_filesystem_encindex());
5543 return TO_OSPATH(path);
5548rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5552 VALUE unresolved_path;
5557 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5558 char *ptr, *prefixptr = NULL, *pend;
5561#ifdef DOSISH_DRIVE_LETTER
5562 VALUE rootdir = path;
5564 if (!
NIL_P(basedir) && skipprefixroot(ptr, ptr +
len, rb_enc_get(path)) == ptr) {
5569 if (
len >= 2 && has_drive_letter(ptr) && (
len == 2 || !isdirsep(ptr[2]))) {
5572 if (!
NIL_P(basedir)) basedir = ospath_for_expand(rb_get_path(basedir));
5573 path = rb_file_absolute_path(ospath_for_expand(path), basedir);
5580 if (!
NIL_P(basedir)) {
5585 enc = rb_enc_get(unresolved_path);
5586 unresolved_path = TO_OSPATH(unresolved_path);
5588 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5589 if (ptr != path_names) {
5590 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5594 if (!
NIL_P(basedir)) {
5596 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5597 if (ptr != basedir_names) {
5603 curdir = rb_dir_getwd_ospath();
5605 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5610 pend = prefixptr + prefixlen;
5611 bool mb_enc = enc_mbclen_needed(enc);
5612 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5614 prefixlen = ++ptr - prefixptr;
5617#ifdef FILE_ALT_SEPARATOR
5618 while (prefixptr < ptr) {
5619 if (*prefixptr == FILE_ALT_SEPARATOR) {
5622 Inc(prefixptr, pend, mb_enc, enc);
5626 switch (rb_enc_to_index(enc)) {
5627 case ENCINDEX_ASCII_8BIT:
5628 case ENCINDEX_US_ASCII:
5629 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5632 loopcheck = rb_hash_new();
5634 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5637 if (basedir_names) {
5638 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5641 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5644 if (origenc && origenc != rb_enc_get(resolved)) {
5646 rb_enc_associate(resolved, origenc);
5660#ifndef HAVE_REALPATH
5662rb_check_realpath_emulate_try(
VALUE arg)
5665 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5669rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5676rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5679 VALUE unresolved_path;
5680 char *resolved_ptr = NULL;
5683 if (mode == RB_REALPATH_DIR) {
5684 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5688 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5689 VALUE paths[2] = {basedir, unresolved_path};
5690 unresolved_path = rb_file_join(2, paths);
5692 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5694 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), NULL)) == NULL) {
5703 if (
errno == ENOTSUP ||
5705 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5706 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5709 if (mode == RB_REALPATH_CHECK) {
5712 rb_sys_fail_path(unresolved_path);
5714 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5717# if !defined(__linux__) && !defined(__APPLE__)
5721 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5722 if (mode == RB_REALPATH_CHECK) {
5725 rb_sys_fail_path(unresolved_path);
5729 if (origenc && origenc != rb_enc_get(resolved)) {
5733 rb_enc_associate(resolved, origenc);
5736 if (is_broken_string(resolved)) {
5737 rb_enc_associate(resolved, rb_filesystem_encoding());
5738 if (is_broken_string(resolved)) {
5739 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5746 if (mode == RB_REALPATH_CHECK) {
5750 arg[2] = (
VALUE)origenc;
5753 rb_check_realpath_emulate_rescue,
Qnil);
5756 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5762rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5764 const enum rb_realpath_mode mode =
5765 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5766 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5772 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5789rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5792 VALUE path = argv[0];
5794 return rb_realpath_internal(basedir, path, 1);
5810rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5813 VALUE path = argv[0];
5815 return rb_realpath_internal(basedir, path, 0);
5823 const char *s, *last;
5825 if (!e || !l2)
return 0;
5827 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5828 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5829 if (c ==
'.')
return l0;
5834 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5839 if (l1 < l2)
return l1;
5842 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5843#if CASEFOLD_FILESYSTEM
5844#define fncomp strncasecmp
5846#define fncomp strncmp
5848 if (fncomp(s, e, l2) == 0) {
5854static inline const char *
5857 const char *p, *q, *e, *end;
5860 rb_len_t len = (alllen ? (size_t)*alllen : strlen(name));
5867 name = skipprefix(name, end, mb_enc, enc);
5868#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5869 const char *root = name;
5872 while (name < end && isdirsep(*name)) {
5879#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5883#ifdef DOSISH_DRIVE_LETTER
5884 else if (*p ==
':') {
5897 p = strrdirsep(name, end, mb_enc, enc);
5902 while (isdirsep(*p)) {
5907 n = ntfs_tail(p, end, mb_enc, enc) - p;
5909 n = chompdirsep(p, end, mb_enc, enc) - p;
5911 for (q = p; q - p < n && *q ==
'.'; q++);
5912 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5913 if (*q ==
'.') e = q;
5935 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5979 const char *name, *p, *fp = 0;
5985 CheckPath(fname, name);
5989 check_path_encoding(fext);
5991 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5992 enc = rb_str_enc_get(fname);
5995 n = RSTRING_LEN(fname);
5996 if (n <= 0 || !*name) {
5997 return rb_enc_str_new(0, 0, enc);
6000 bool mb_enc = enc_mbclen_needed(enc);
6001 p = enc_find_basename(name, &f, &n, mb_enc, enc);
6007 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
6012 if (f == RSTRING_LEN(fname)) {
6017 return rb_enc_str_new(p, f, enc);
6020static VALUE rb_file_dirname_n(
VALUE fname,
int n);
6043rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
6049 return rb_file_dirname_n(argv[0], n);
6055 return rb_file_dirname_n(fname, 1);
6059rb_file_dirname_n(
VALUE fname,
int n)
6061 const char *name, *root, *p, *end;
6064 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
6065 CheckPath(fname, name);
6066 end = name + RSTRING_LEN(fname);
6068 bool mb_enc = !rb_str_enc_fastpath(fname);
6071 root = skiproot(name, end);
6073 if (root > name + 1 && isdirsep(*name))
6074 root = skipprefix(name = root - 2, end, mb_enc, enc);
6076 if (root > name + 1)
6079 if (n > (end - root + 1) / 2) {
6085 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
6094 return rb_enc_str_new(
".", 1, enc);
6096#ifdef DOSISH_DRIVE_LETTER
6097 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
6098 const char *top = skiproot(name + 2, end);
6099 dirname = rb_enc_str_new(name, 3, enc);
6104 dirname = rb_enc_str_new(name, p - name, enc);
6105#ifdef DOSISH_DRIVE_LETTER
6106 if (root == name + 2 && p == root && name[1] ==
':')
6112static inline const char *
6115 const char *p, *e, *end = name + (
len ? *
len : (
rb_len_t)strlen(name));
6117 p = strrdirsep(name, end, mb_enc, enc);
6121 do name = ++p;
while (isdirsep(*p));
6124 while (*p && *p ==
'.') p++;
6126 if (*p ==
'.' || istrailinggarbage(*p)) {
6128 const char *last = p++, *dot = last;
6129 while (istrailinggarbage(*p)) {
6130 if (*p ==
'.') dot = p;
6133 if (!*p || isADS(*p)) {
6137 if (*last ==
'.' || dot > last) e = dot;
6144 else if (isADS(*p)) {
6148 else if (isdirsep(*p))
6150 Inc(p, end, mb_enc, enc);
6155 if (!e || e == name)
6180 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
6227 CheckPath(fname, name);
6231 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
6234 bool mb_enc = !rb_str_enc_fastpath(fname);
6237 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
6238 return rb_enc_str_new(ext,
len, enc);
6262 return rb_get_path(fname);
6294file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6296 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6297 return rb_file_join_ary(arg);
6301rb_file_join_ary(
VALUE ary)
6305 const char *name, *tail;
6315 check_path_encoding(tmp);
6316 len += RSTRING_LEN(tmp);
6324 RBASIC_CLEAR_CLASS(result);
6327 switch (OBJ_BUILTIN_TYPE(tmp)) {
6329 if (!checked) check_path_encoding(tmp);
6334 rb_raise(rb_eArgError,
"recursive array");
6346 rb_enc_copy(result, tmp);
6349 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6350 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6353 else if (tail == name +
len) {
6357 enc = fs_enc_check(result, tmp);
6359 rb_enc_associate(result, enc);
6372 for (i = 0; i < argc; i++) {
6373 VALUE tmp = args[i];
6375 size += RSTRING_LEN(tmp);
6388 const char *name = RSTRING_PTR(result);
6389 for (i = 1; i < argc; i++) {
6390 VALUE tmp = args[i];
6397 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6400 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6405 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6412 rb_enc_associate(result, new_enc);
6413 encidx = rb_enc_to_index(new_enc);
6419 rb_str_null_check(result);
6426 if (RB_UNLIKELY(argc == 0)) {
6430 VALUE result = rb_file_join_fastpath(argc, args);
6431 if (RB_LIKELY(result)) {
6451rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6453 return rb_file_join(argc, argv);
6456#if defined(HAVE_TRUNCATE)
6457struct truncate_arg {
6463nogvl_truncate(
void *ptr)
6465 struct truncate_arg *ta = ptr;
6466 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6498 struct truncate_arg ta;
6503 path = rb_str_encode_ospath(path);
6506 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6508 rb_sys_fail_path(path);
6512#define rb_file_s_truncate rb_f_notimplement
6515#if defined(HAVE_FTRUNCATE)
6516struct ftruncate_arg {
6522nogvl_ftruncate(
void *ptr)
6524 struct ftruncate_arg *fa = ptr;
6526 return (
VALUE)ftruncate(fa->fd, fa->pos);
6567 struct ftruncate_arg fa;
6574 rb_io_flush_raw(obj, 0);
6576 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6577 rb_sys_fail_path(fptr->
pathv);
6582#define rb_file_truncate rb_f_notimplement
6599#include <winerror.h>
6603rb_thread_flock(
void *data)
6606 int old_errno =
errno;
6608 int *op = data, ret = flock(op[0], op[1]);
6611 if (GetLastError() == ERROR_NOT_LOCKED) {
6669 op[1] = op1 =
NUM2INT(operation);
6674 rb_io_flush_raw(obj, 0);
6676 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6681#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6684 if (op1 & LOCK_NB)
return Qfalse;
6687 time.tv_usec = 100 * 1000;
6693#if defined(ERESTART)
6699 rb_syserr_fail_path(e, fptr->
pathv);
6706test_check(
int n,
int argc,
VALUE *argv)
6712 for (i=1; i<n; i++) {
6719#define CHECK(n) test_check((n), argc, argv)
6811 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6815 return rb_file_blockdev_p(0, argv[1]);
6818 return rb_file_chardev_p(0, argv[1]);
6821 return rb_file_directory_p(0, argv[1]);
6824 return rb_file_exist_p(0, argv[1]);
6827 return rb_file_file_p(0, argv[1]);
6830 return rb_file_sgid_p(0, argv[1]);
6833 return rb_file_grpowned_p(0, argv[1]);
6836 return rb_file_sticky_p(0, argv[1]);
6839 return rb_file_symlink_p(0, argv[1]);
6842 return rb_file_owned_p(0, argv[1]);
6845 return rb_file_rowned_p(0, argv[1]);
6848 return rb_file_pipe_p(0, argv[1]);
6851 return rb_file_readable_p(0, argv[1]);
6854 return rb_file_readable_real_p(0, argv[1]);
6857 return rb_file_size_p(0, argv[1]);
6860 return rb_file_socket_p(0, argv[1]);
6863 return rb_file_suid_p(0, argv[1]);
6866 return rb_file_writable_p(0, argv[1]);
6869 return rb_file_writable_real_p(0, argv[1]);
6872 return rb_file_executable_p(0, argv[1]);
6875 return rb_file_executable_real_p(0, argv[1]);
6878 return rb_file_zero_p(0, argv[1]);
6882 if (strchr(
"MAC", cmd)) {
6884 VALUE fname = argv[1];
6887 if (
rb_stat(fname, &st) == -1) {
6890 rb_syserr_fail_path(e, fname);
6895 return stat_atime(&st);
6897 return stat_mtime(&st);
6899 return stat_ctime(&st);
6905 return rb_file_identical_p(0, argv[1], argv[2]);
6908 if (strchr(
"=<>", cmd)) {
6909 struct stat st1, st2;
6916 t1 = stat_mtimespec(&st1);
6917 t2 = stat_mtimespec(&st2);
6921 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6925 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6926 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6930 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6931 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6938 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6941 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6990rb_stat_s_alloc(
VALUE klass)
6993 stat_alloc(klass, &obj);
7019 fname = rb_str_encode_ospath(fname);
7021 rb_sys_fail_path(fname);
7028 rb_st->initialized =
true;
7045 *copy_rb_st = *orig_rb_st;
7091rb_stat_ftype(
VALUE obj)
7093 return rb_file_ftype(get_stat(obj)->ST_(mode));
7115 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
7144 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
7181 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
7213 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
7243 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
7271 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7302rb_stat_owned(
VALUE obj)
7304 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7309rb_stat_rowned(
VALUE obj)
7311 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7338rb_stat_grpowned(
VALUE obj)
7341 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7370 rb_io_stat_data *st = get_stat(obj);
7373 if (geteuid() == 0)
return Qtrue;
7376 if (rb_stat_owned(obj))
7377 return RBOOL(st->ST_(mode) & S_IRUSR);
7380 if (rb_stat_grpowned(obj))
7381 return RBOOL(st->ST_(mode) & S_IRGRP);
7384 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7402 rb_io_stat_data *st = get_stat(obj);
7405 if (getuid() == 0)
return Qtrue;
7408 if (rb_stat_rowned(obj))
7409 return RBOOL(st->ST_(mode) & S_IRUSR);
7412 if (rb_group_member(get_stat(obj)->ST_(gid)))
7413 return RBOOL(st->ST_(mode) & S_IRGRP);
7416 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7444rb_stat_wr(
VALUE obj)
7447 rb_io_stat_data *st = get_stat(obj);
7448 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7449 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7481 rb_io_stat_data *st = get_stat(obj);
7484 if (geteuid() == 0)
return Qtrue;
7487 if (rb_stat_owned(obj))
7488 return RBOOL(st->ST_(mode) & S_IWUSR);
7491 if (rb_stat_grpowned(obj))
7492 return RBOOL(st->ST_(mode) & S_IWGRP);
7495 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7516 rb_io_stat_data *st = get_stat(obj);
7519 if (getuid() == 0)
return Qtrue;
7522 if (rb_stat_rowned(obj))
7523 return RBOOL(st->ST_(mode) & S_IWUSR);
7526 if (rb_group_member(get_stat(obj)->ST_(gid)))
7527 return RBOOL(st->ST_(mode) & S_IWGRP);
7530 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7559rb_stat_ww(
VALUE obj)
7562 rb_io_stat_data *st = get_stat(obj);
7563 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7564 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7595 rb_io_stat_data *st = get_stat(obj);
7598 if (geteuid() == 0) {
7599 return RBOOL(st->ST_(mode) & S_IXUGO);
7603 if (rb_stat_owned(obj))
7604 return RBOOL(st->ST_(mode) & S_IXUSR);
7607 if (rb_stat_grpowned(obj))
7608 return RBOOL(st->ST_(mode) & S_IXGRP);
7611 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7628 rb_io_stat_data *st = get_stat(obj);
7631 if (getuid() == 0) {
7632 return RBOOL(st->ST_(mode) & S_IXUGO);
7636 if (rb_stat_rowned(obj))
7637 return RBOOL(st->ST_(mode) & S_IXUSR);
7640 if (rb_group_member(get_stat(obj)->ST_(gid)))
7641 return RBOOL(st->ST_(mode) & S_IXGRP);
7644 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7668 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7713 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7745 rb_off_t size = get_stat(obj)->ST_(size);
7747 if (size == 0)
return Qnil;
7780rb_stat_suid(
VALUE obj)
7783 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7817rb_stat_sgid(
VALUE obj)
7820 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7854rb_stat_sticky(
VALUE obj)
7857 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7862#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7863#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7874nogvl_mkfifo(
void *ptr)
7876 struct mkfifo_arg *ma = ptr;
7878 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7906 struct mkfifo_arg ma;
7915 path = rb_str_encode_ospath(path);
7916 ma.path = RSTRING_PTR(path);
7917 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7918 rb_sys_fail_path(path);
7923#define rb_file_s_mkfifo rb_f_notimplement
7926static VALUE rb_mFConst;
7929rb_file_const(
const char *name,
VALUE value)
7931 rb_define_const(rb_mFConst, name, value);
7935rb_is_absolute_path(
const char *path)
7937#ifdef DOSISH_DRIVE_LETTER
7938 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7941 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7944 if (path[0] ==
'/')
return 1;
7950ruby_is_fd_loadable(
int fd)
7957 if (fstat(fd, &st) < 0)
7960 if (S_ISREG(st.st_mode))
7963 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7966 if (S_ISDIR(st.st_mode))
7977rb_file_load_ok(
const char *path)
7984 int mode = (O_RDONLY |
7985#if defined O_NONBLOCK
7987#elif defined O_NDELAY
7993 if (!rb_gc_for_fd(
errno))
return 0;
7995 if (fd < 0)
return 0;
7998 ret = ruby_is_fd_loadable(fd);
8005is_explicit_relative(
const char *path)
8007 if (*path++ !=
'.')
return 0;
8008 if (*path ==
'.') path++;
8009 return isdirsep(*path);
8015 int encidx = rb_enc_get_index(orig);
8016 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
8017 encidx = rb_filesystem_encindex();
8018 rb_enc_associate_index(path, encidx);
8026nav_component_p(
const char *s,
const char *send)
8028 if ((send - s) >= 2 && s[0] ==
'.') {
8029 return s[1] ==
'.' || isdirsep(s[1]);
8035fname_need_expansion_p(
VALUE fname)
8037 const char *s = RSTRING_PTR(fname);
8039 const char *send = s +
len;
8041 if (nav_component_p(s, send)) {
8046 bool mbenc = enc_mbclen_needed(enc);
8048 s = enc_path_next(s, send, mbenc, enc);
8050 if (nav_component_p(s, send)) {
8054 s = enc_path_next(s, send, mbenc, enc);
8060expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
8062 rb_len_t dname_len = RSTRING_LEN(dname);
8063 const char *dname_ptr = RSTRING_PTR(dname);
8067 if (need_expansion || dname_ptr[0] ==
'~') {
8068 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
8073 if (!isdirsep(dname_ptr[dname_len - 1])) {
8082rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
8085 VALUE fname = *filep;
8089 if (!ext[0])
return 0;
8092 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
8093 f = RSTRING_PTR(fname);
8098 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
8099 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
8100 fnlen = RSTRING_LEN(fname);
8101 for (i=0; ext[i]; i++) {
8103 if (rb_file_load_ok(RSTRING_PTR(fname))) {
8104 *filep = copy_path_class(fname, *filep);
8112 rb_len_t expanded_load_path_maxlen;
8113 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8114 if (!load_path)
return 0;
8117 RBASIC_CLEAR_CLASS(fname);
8118 fnlen = RSTRING_LEN(fname);
8119 bool need_expansion = fname_need_expansion_p(fname);
8122 rb_enc_associate_index(tmp, rb_usascii_encindex());
8124 for (j=0; ext[j]; j++) {
8126 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
8128 if (!RSTRING_LEN(dname))
continue;
8129 expand_feature(fname, dname, tmp, need_expansion);
8131 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8132 *filep = copy_path_class(tmp, *filep);
8138 rb_str_resize(tmp, 0);
8151 path = copy_path_class(file_expand_path_1(path, 0), path);
8152 f = RSTRING_PTR(path);
8156 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
8157 if (!rb_file_load_ok(f))
return 0;
8159 path = copy_path_class(file_expand_path_1(path, 0), path);
8163 rb_len_t expanded_load_path_maxlen;
8164 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8167 bool need_expansion = fname_need_expansion_p(path);
8169 rb_enc_associate_index(tmp, rb_usascii_encindex());
8172 if (!RSTRING_LEN(dname))
continue;
8173 expand_feature(path, dname, tmp, need_expansion);
8175 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8176 return copy_path_class(tmp, path);
8179 rb_str_resize(tmp, 0);
8187#define define_filetest_function(name, func, argc) do { \
8188 rb_define_module_function(rb_mFileTest, name, func, argc); \
8189 rb_define_singleton_method(rb_cFile, name, func, argc); \
8192const char ruby_null_device[] =
8995#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8996 rb_CFString_class_initialize_before_fork();
9004 define_filetest_function(
"directory?", rb_file_directory_p, 1);
9005 define_filetest_function(
"exist?", rb_file_exist_p, 1);
9006 define_filetest_function(
"readable?", rb_file_readable_p, 1);
9007 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
9008 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
9009 define_filetest_function(
"writable?", rb_file_writable_p, 1);
9010 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
9011 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
9012 define_filetest_function(
"executable?", rb_file_executable_p, 1);
9013 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
9014 define_filetest_function(
"file?", rb_file_file_p, 1);
9015 define_filetest_function(
"zero?", rb_file_zero_p, 1);
9016 define_filetest_function(
"empty?", rb_file_zero_p, 1);
9017 define_filetest_function(
"size?", rb_file_size_p, 1);
9018 define_filetest_function(
"size", rb_file_s_size, 1);
9019 define_filetest_function(
"owned?", rb_file_owned_p, 1);
9020 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
9022 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
9023 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
9024 define_filetest_function(
"socket?", rb_file_socket_p, 1);
9026 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
9027 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
9029 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
9030 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
9031 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
9033 define_filetest_function(
"identical?", rb_file_identical_p, 2);
9071 separator = rb_fstring_lit(
"/");
9073 rb_define_const(
rb_cFile,
"Separator", separator);
9075 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9424 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9427 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9429 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9431 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9433 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9435 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9437 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9438#if defined(O_NDELAY) || defined(O_NONBLOCK)
9440# define O_NONBLOCK O_NDELAY
9443 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9446 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9449 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9455 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9456#ifndef O_SHARE_DELETE
9457# define O_SHARE_DELETE 0
9460 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9463 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9467 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9471 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9475 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9479 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9483 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9487 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9491 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9493 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9495 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9497 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9500 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.