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,
long 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[] =
"/";
327 long len =
sizeof(small_str) - 1;
335 for (
int i = 0; i < 2; i++) {
336 CFMutableStringRef m = mutable_CFString_new(&s, small_str,
len);
337 mutable_CFString_release(m, s);
343rb_str_append_normalized_ospath(
VALUE str,
const char *ptr,
long len)
348 CFMutableStringRef m = mutable_CFString_new(&s, ptr,
len);
349 long oldlen = RSTRING_LEN(str);
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,
long 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)
487 const long len = (long)(offsetof(
struct apply_arg, fn) + (size * argc));
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
1335rb_stat_inspect(
VALUE self)
1339 static const struct {
1343 {
"dev", rb_stat_dev},
1344 {
"ino", rb_stat_ino},
1345 {
"mode", rb_stat_mode},
1346 {
"nlink", rb_stat_nlink},
1347 {
"uid", rb_stat_uid},
1348 {
"gid", rb_stat_gid},
1349 {
"rdev", rb_stat_rdev},
1350 {
"size", rb_stat_size},
1351 {
"blksize", rb_stat_blksize},
1352 {
"blocks", rb_stat_blocks},
1353 {
"atime", rb_stat_atime},
1354 {
"mtime", rb_stat_mtime},
1355 {
"ctime", rb_stat_ctime},
1356#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1357 {
"birthtime", rb_stat_birthtime},
1363 if (!rb_st->initialized) {
1371 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1379 v = (*member[i].func)(self);
1381 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1383 else if (i == 0 || i == 6) {
1384 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1404no_gvl_fstat(
void *data)
1407 return (
VALUE)fstat(arg->file.fd, arg->st);
1411fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1415 data.file.fd = fptr->
fd;
1418 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1422no_gvl_stat(
void * data)
1425 return (
void *)(
VALUE)stat(arg->file.path, arg->st);
1429stat_without_gvl(
const char *path,
struct stat *st)
1433 data.file.path = path;
1436 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1439#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1440 defined(HAVE_STRUCT_STATX_STX_BTIME)
1442# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1445# ifdef HAVE_SYSCALL_H
1446# include <syscall.h>
1447# elif defined HAVE_SYS_SYSCALL_H
1448# include <sys/syscall.h>
1450# if defined __linux__
1451# include <linux/stat.h>
1453statx(
int dirfd,
const char *pathname,
int flags,
1454 unsigned int mask,
struct statx *statxbuf)
1456 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1461typedef struct no_gvl_rb_io_stat_data {
1467} no_gvl_rb_io_stat_data;
1470io_blocking_statx(
void *data)
1472 no_gvl_rb_io_stat_data *arg = data;
1473 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1477no_gvl_statx(
void *data)
1479 return (
void *)io_blocking_statx(data);
1483statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1485 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1488 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1492lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1494 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1497 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1501fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1503 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1506 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1509#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1512rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1517 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1522 result = fstatx_without_gvl(fptr, stx, mask);
1527 file = rb_str_encode_ospath(file);
1528 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1534# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1536NORETURN(
static void statx_notimplement(
const char *field_name));
1541statx_notimplement(
const char *field_name)
1544 "%s is unimplemented on this filesystem",
1549statx_birthtime(
const rb_io_stat_data *stx)
1551 if (!statx_has_birthtime(stx)) {
1553 statx_notimplement(
"birthtime");
1555 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1560# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1561# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1562# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1563# define rb_statx(file, stx, mask) rb_stat(file, stx)
1564# define STATX(path, st, mask) stat(path, st)
1566#if defined(HAVE_STAT_BIRTHTIME)
1567# define statx_has_birthtime(st) 1
1569# define statx_has_birthtime(st) 0
1576# define FSTAT(fd, st) fstat(fd, st)
1585 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1590 result = fstat_without_gvl(fptr, st);
1595 file = rb_str_encode_ospath(file);
1596 result = stat_without_gvl(RSTRING_PTR(file), st);
1634 fname = rb_str_encode_ospath(fname);
1635 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1636 rb_sys_fail_path(fname);
1638 return rb_statx_new(&st);
1670rb_io_stat(
VALUE obj)
1676 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1677 rb_sys_fail_path(fptr->
pathv);
1679 return rb_statx_new(&st);
1684no_gvl_lstat(
void *ptr)
1687 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1691lstat_without_gvl(
const char *path,
struct stat *st)
1695 data.file.path = path;
1698 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1735 fname = rb_str_encode_ospath(fname);
1736 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1737 rb_sys_fail_path(fname);
1739 return rb_statx_new(&st);
1741 return rb_file_s_stat(klass, fname);
1774rb_file_lstat(
VALUE obj)
1783 path = rb_str_encode_ospath(fptr->
pathv);
1784 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1785 rb_sys_fail_path(fptr->
pathv);
1787 return rb_statx_new(&st);
1789 return rb_io_stat(obj);
1794rb_group_member(GETGROUPS_T gid)
1796#if !defined(HAVE_GETGROUPS)
1805 if (getgid() == gid || getegid() == gid)
1808 groups = getgroups(0, NULL);
1809 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1810 anum = getgroups(groups, gary);
1811 while (--anum >= 0) {
1812 if (gary[anum] == gid) {
1825# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1828#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1829#define USE_GETEUID 1
1834eaccess(
const char *path,
int mode)
1843 if (getuid() == euid && getgid() == getegid())
1844 return access(path, mode);
1846 if (stat(path, &st) < 0)
1856 if (st.st_mode & S_IXUGO)
1862 if (st.st_uid == euid)
1864 else if (rb_group_member(st.st_gid))
1867 if ((
int)(st.st_mode & mode) == mode)
return 0;
1871 return access(path, mode);
1882nogvl_eaccess(
void *ptr)
1886 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1890rb_eaccess(
VALUE fname,
int mode)
1895 fname = rb_str_encode_ospath(fname);
1899 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1903nogvl_access(
void *ptr)
1907 return (
void *)(
VALUE)access(aa->path, aa->mode);
1911rb_access(
VALUE fname,
int mode)
1916 fname = rb_str_encode_ospath(fname);
1920 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1955# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1961 if (S_ISDIR(st.st_mode))
return Qtrue;
1990# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1996 if (S_ISFIFO(st.st_mode))
return Qtrue;
2028# define S_ISLNK(m) _S_ISLNK(m)
2031# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
2034# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
2044 fname = rb_str_encode_ospath(fname);
2046 if (S_ISLNK(st.st_mode))
return Qtrue;
2081# define S_ISSOCK(m) _S_ISSOCK(m)
2084# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
2087# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
2097 if (S_ISSOCK(st.st_mode))
return Qtrue;
2129# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
2131# define S_ISBLK(m) (0)
2139 if (S_ISBLK(st.st_mode))
return Qtrue;
2166# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2172 if (S_ISCHR(st.st_mode))
return Qtrue;
2242 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2256rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2258 return RBOOL(rb_access(fname, R_OK) >= 0);
2262# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2266# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2295rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2301 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2302 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2336 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2353rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2355 return RBOOL(rb_access(fname, W_OK) >= 0);
2384rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2390 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2391 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2421 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2435rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2437 return RBOOL(rb_access(fname, X_OK) >= 0);
2441# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2469 return RBOOL(S_ISREG(st.st_mode));
2529 return RBOOL(st.st_size == 0);
2567 if (st.st_size == 0)
return Qnil;
2601 return RBOOL(st.st_uid == geteuid());
2610 return RBOOL(st.st_uid == getuid());
2646 if (rb_group_member(st.st_gid))
return Qtrue;
2651#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2653check3rdbyte(
VALUE fname,
int mode)
2658 return RBOOL(st.st_mode & mode);
2691 return check3rdbyte(fname, S_ISUID);
2726 return check3rdbyte(fname, S_ISGID);
2762 return check3rdbyte(fname, S_ISVTX);
2799 struct stat st1, st2;
2803 if (st1.st_dev != st2.st_dev)
return Qfalse;
2804 if (st1.st_ino != st2.st_ino)
return Qfalse;
2808 return rb_w32_file_identical_p(fname1, fname2);
2834 if (
rb_stat(fname, &st) < 0) {
2837 rb_syserr_fail_path(e, fname);
2843rb_file_ftype(mode_t mode)
2847 if (S_ISREG(mode)) {
2850 else if (S_ISDIR(mode)) {
2853 else if (S_ISCHR(mode)) {
2854 t =
"characterSpecial";
2857 else if (S_ISBLK(mode)) {
2862 else if (S_ISFIFO(mode)) {
2867 else if (S_ISLNK(mode)) {
2872 else if (S_ISSOCK(mode)) {
2880 return rb_fstring_cstr(t);
2929 fname = rb_str_encode_ospath(fname);
2931 rb_sys_fail_path(fname);
2934 return rb_file_ftype(st.st_mode);
2990 if (
rb_stat(fname, &st) < 0) {
2993 rb_syserr_fail_path(e, fname);
2995 return stat_time(stat_atimespec(&st));
3027rb_file_atime(
VALUE obj)
3033 if (fstat(fptr->
fd, &st) == -1) {
3034 rb_sys_fail_path(fptr->
pathv);
3036 return stat_time(stat_atimespec(&st));
3076 if (
rb_stat(fname, &st) < 0) {
3079 rb_syserr_fail_path(e, fname);
3081 return stat_time(stat_mtimespec(&st));
3106rb_file_mtime(
VALUE obj)
3112 if (fstat(fptr->
fd, &st) == -1) {
3113 rb_sys_fail_path(fptr->
pathv);
3115 return stat_time(stat_mtimespec(&st));
3159 if (
rb_stat(fname, &st) < 0) {
3162 rb_syserr_fail_path(e, fname);
3164 return stat_time(stat_ctimespec(&st));
3201rb_file_ctime(
VALUE obj)
3207 if (fstat(fptr->
fd, &st) == -1) {
3208 rb_sys_fail_path(fptr->
pathv);
3210 return stat_time(stat_ctimespec(&st));
3213#if defined(HAVE_STAT_BIRTHTIME)
3239 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3242 rb_syserr_fail_path(e, fname);
3244 return statx_birthtime(&st);
3247# define rb_file_s_birthtime rb_f_notimplement
3250#if defined(HAVE_STAT_BIRTHTIME)
3271rb_file_birthtime(
VALUE obj)
3277 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3278 rb_sys_fail_path(fptr->
pathv);
3280 return statx_birthtime(&st);
3283# define rb_file_birthtime rb_f_notimplement
3295 rb_io_flush_raw(file, 0);
3298 if (fstat(fptr->
fd, &st) == -1) {
3299 rb_sys_fail_path(fptr->
pathv);
3325file_size(
VALUE self)
3327 return OFFT2NUM(rb_file_size(self));
3336nogvl_chmod(
void *ptr)
3339 int ret = chmod(data->path, data->mode);
3340 return (
void *)(
VALUE)ret;
3344rb_chmod(
const char *path, mode_t mode)
3350 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3354chmod_internal(
const char *path,
void *mode)
3356 return chmod(path, *(mode_t *)mode);
3389 return apply2files(chmod_internal, argc, argv, &mode);
3393struct nogvl_fchmod_data {
3399io_blocking_fchmod(
void *ptr)
3401 struct nogvl_fchmod_data *data = ptr;
3402 int ret = fchmod(data->fd, data->mode);
3407rb_fchmod(
struct rb_io* io, mode_t mode)
3410 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3411 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3440#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3448 if (rb_fchmod(fptr, mode) == -1) {
3449 if (HAVE_FCHMOD ||
errno != ENOSYS)
3450 rb_sys_fail_path(fptr->
pathv);
3453 if (!HAVE_FCHMOD)
return INT2FIX(0);
3456#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3458 path = rb_str_encode_ospath(fptr->
pathv);
3459 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3460 rb_sys_fail_path(fptr->
pathv);
3466#if defined(HAVE_LCHMOD)
3468lchmod_internal(
const char *path,
void *mode)
3470 return lchmod(path, *(mode_t *)mode);
3507 return apply2files(lchmod_internal, argc, argv, &mode);
3510#define rb_file_s_lchmod rb_f_notimplement
3513static inline rb_uid_t
3517 return (rb_uid_t)-1;
3522static inline rb_gid_t
3526 return (rb_gid_t)-1;
3537chown_internal(
const char *path,
void *arg)
3540 return chown(path, args->owner, args->group);
3594 arg.owner = to_uid(*argv++);
3595 arg.group = to_gid(*argv++);
3597 return apply2files(chown_internal, argc, argv, &arg);
3609nogvl_chown(
void *ptr)
3612 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3616rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3619 .as = {.path = path},
3620 .new = {.owner = owner, .group = group},
3622 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3627nogvl_fchown(
void *ptr)
3630 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3634rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3639 .new = {.owner = owner, .group = group},
3641 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3675 path = rb_str_encode_ospath(fptr->
pathv);
3676 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3677 rb_sys_fail_path(fptr->
pathv);
3679 if (rb_fchown(fptr->
fd, o, g) == -1)
3680 rb_sys_fail_path(fptr->
pathv);
3686#if defined(HAVE_LCHOWN)
3688lchown_internal(
const char *path,
void *arg)
3691 return lchown(path, args->owner, args->group);
3748 arg.owner = to_uid(*argv++);
3749 arg.group = to_gid(*argv++);
3751 return apply2files(lchown_internal, argc, argv, &arg);
3754#define rb_file_s_lchown rb_f_notimplement
3764NORETURN(
static void utime_failed(
struct apply_arg *));
3770 VALUE path = aa->fn[aa->i].path;
3773 if (ua->tsp && e == EINVAL) {
3776 VALUE atime = ua->atime;
3777 VALUE mtime = ua->mtime;
3779 if (!
NIL_P(atime)) {
3782 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3785 if (
NIL_P(a)) e[0] = m;
3801 rb_syserr_fail_path(e, path);
3805#if defined(HAVE_UTIMES)
3807# if !defined(HAVE_UTIMENSAT)
3809# elif defined(__APPLE__) && \
3810 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3812# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3813typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3817static inline utimensat_func *
3824# define utimensat rb_utimensat()
3831utime_internal(
const char *path,
void *arg)
3834 const struct timespec *tsp = v->tsp;
3835 struct timeval tvbuf[2], *tvp = NULL;
3837#if defined(HAVE_UTIMENSAT)
3838# if defined(__APPLE__)
3839 const int try_utimensat = utimensat != NULL;
3840 const int try_utimensat_follow = utimensat != NULL;
3842# define TRY_UTIMENSAT 1
3843 static int try_utimensat = 1;
3844# ifdef AT_SYMLINK_NOFOLLOW
3845 static int try_utimensat_follow = 1;
3847 const int try_utimensat_follow = 0;
3852 if (v->follow ? try_utimensat_follow : try_utimensat) {
3853# ifdef AT_SYMLINK_NOFOLLOW
3855 flags = AT_SYMLINK_NOFOLLOW;
3859 int result = utimensat(AT_FDCWD, path, tsp, flags);
3860# ifdef TRY_UTIMENSAT
3861 if (result < 0 &&
errno == ENOSYS) {
3862# ifdef AT_SYMLINK_NOFOLLOW
3863 try_utimensat_follow = 0;
3875 tvbuf[0].tv_sec = tsp[0].tv_sec;
3876 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3877 tvbuf[1].tv_sec = tsp[1].tv_sec;
3878 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3882 if (v->follow)
return lutimes(path, tvp);
3884 return utimes(path, tvp);
3889#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3897utime_internal(
const char *path,
void *arg)
3901 struct utimbuf utbuf, *utp = NULL;
3903 utbuf.actime = tsp[0].tv_sec;
3904 utbuf.modtime = tsp[1].tv_sec;
3907 return utime(path, utp);
3912utime_internal_i(
int argc,
VALUE *argv,
int follow)
3915 struct timespec tss[2], *tsp = NULL;
3918 args.atime = *argv++;
3919 args.mtime = *argv++;
3921 args.follow = follow;
3923 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3926 if (args.atime == args.mtime)
3933 return apply2files(utime_internal, argc, argv, &args);
3979 return utime_internal_i(argc, argv, FALSE);
3982#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
4040 return utime_internal_i(argc, argv, TRUE);
4043#define rb_file_s_lutime rb_f_notimplement
4046#ifdef RUBY_FUNCTION_NAME_STRING
4047# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
4049# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
4051#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
4052NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
4054syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
4058 const int max_pathlen = MAX_PATH;
4060 const int max_pathlen = MAXPATHLEN;
4071#ifdef RUBY_FUNCTION_NAME_STRING
4072 rb_syserr_fail_path_in(func, e, str);
4074 rb_syserr_fail_path(e, str);
4109 from = rb_str_encode_ospath(from);
4110 to = rb_str_encode_ospath(to);
4113 sys_fail2(from, to);
4118#define rb_file_s_link rb_f_notimplement
4159 from = rb_str_encode_ospath(from);
4160 to = rb_str_encode_ospath(to);
4163 sys_fail2(from, to);
4168#define rb_file_s_symlink rb_f_notimplement
4196 return rb_readlink(path, rb_filesystem_encoding());
4199struct readlink_arg {
4206nogvl_readlink(
void *ptr)
4208 struct readlink_arg *ra = ptr;
4210 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4214readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4216 struct readlink_arg ra;
4218 ra.path = RSTRING_PTR(path);
4219 ra.buf = RSTRING_PTR(buf);
4222 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4233 path = rb_str_encode_ospath(path);
4234 v = rb_enc_str_new(0, size, enc);
4235 while ((rv = readlink_without_gvl(path, v, size)) == size
4237 || (rv < 0 &&
errno == ERANGE)
4246 rb_str_resize(v, 0);
4247 rb_syserr_fail_path(e, path);
4249 rb_str_resize(v, rv);
4254#define rb_file_s_readlink rb_f_notimplement
4258unlink_internal(
const char *path,
void *arg)
4260 return unlink(path);
4303rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4305 return apply2files(unlink_internal, argc, argv, 0);
4314no_gvl_rename(
void *ptr)
4318 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4396 f = rb_str_encode_ospath(from);
4397 t = rb_str_encode_ospath(to);
4400#if defined __CYGWIN__
4403 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4408 if (chmod(ra.dst, 0666) == 0 &&
4409 unlink(ra.dst) == 0 &&
4410 rename(ra.src, ra.dst) == 0)
4414 syserr_fail2(e, from, to);
4448 rb_error_arity(argc, 0, 1);
4456#if defined __CYGWIN__ || defined DOSISH
4458#define DOSISH_DRIVE_LETTER
4459#define FILE_ALT_SEPARATOR '\\'
4461#ifdef FILE_ALT_SEPARATOR
4462#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4464static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4467#define isdirsep(x) ((x) == '/')
4479#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4480#define isADS(x) ((x) == ':')
4482#define istrailinggarbage(x) 0
4485#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4487#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4488#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4490#if defined(DOSISH_UNC)
4491#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4493#define has_unc(buf) 0
4496#ifdef DOSISH_DRIVE_LETTER
4498has_drive_letter(
const char *buf)
4500 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4525 if (chdir(drive) == 0) {
4526 drvcwd = rb_dir_getwd_ospath();
4538not_same_drive(
VALUE path,
int drive)
4540 const char *p = RSTRING_PTR(path);
4541 if (RSTRING_LEN(path) < 2)
return 0;
4542 if (has_drive_letter(p)) {
4553skiproot(
const char *path,
const char *end)
4555#ifdef DOSISH_DRIVE_LETTER
4556 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4558 while (path < end && isdirsep(*path)) path++;
4559 return (
char *)path;
4563enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4565 while (s < e && !isdirsep(*s)) {
4566 Inc(s, e, mb_enc, enc);
4571#define nextdirsep rb_enc_path_next
4575 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4578#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4579#define skipprefix enc_path_skip_prefix
4581#define skipprefix(path, end, mb_enc, enc) (path)
4584enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4586#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4588 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4590 while (path < end && isdirsep(*path)) path++;
4591 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4592 path + 2 <= end && !isdirsep(path[1])) {
4593 path = enc_path_next(path + 1, end, mb_enc, enc);
4595 return (
char *)path;
4598#ifdef DOSISH_DRIVE_LETTER
4599 if (path + 2 <= end && has_drive_letter(path))
4600 return (
char *)(path + 2);
4603 return (
char *)path;
4607rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4609 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4613skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4615#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4616 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4617 while (p < end && isdirsep(*p)) p++;
4620 return skiproot(path, end);
4625rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4627 return skipprefixroot(path, end, enc);
4631enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4634 while (path < end) {
4635 if (isdirsep(*path)) {
4636 const char *tmp = path++;
4637 while (path < end && isdirsep(*path)) path++;
4638 if (path >= end)
break;
4642 Inc(path, end, mb_enc, enc);
4648rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4650 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4654strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4656 if (RB_UNLIKELY(mb_enc)) {
4657 return enc_path_last_separator(path, end, mb_enc, enc);
4660 const char *cursor = end - 1;
4662 while (cursor >= path && isdirsep(cursor[0])) {
4666 while (cursor >= path) {
4667 if (isdirsep(cursor[0])) {
4668 while (cursor > path && isdirsep(cursor[-1])) {
4671 return (
char *)cursor;
4679chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4681 while (path < end) {
4682 if (isdirsep(*path)) {
4683 const char *last = path++;
4684 while (path < end && isdirsep(*path)) path++;
4685 if (path >= end)
return (
char *)last;
4688 Inc(path, end, mb_enc, enc);
4691 return (
char *)path;
4697 if (path < end && isdirsep(*path)) path++;
4698 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4704 rb_encoding *enc = rb_enc_check_str(path1, path2);
4705 int encidx = rb_enc_to_index(enc);
4706 if (encidx == ENCINDEX_US_ASCII) {
4707 encidx = rb_enc_get_index(path1);
4708 if (encidx == ENCINDEX_US_ASCII)
4709 encidx = rb_enc_get_index(path2);
4710 enc = rb_enc_from_index(encidx);
4717ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4719 while (path < end && *path ==
'.') path++;
4720 while (path < end && !isADS(*path)) {
4721 if (istrailinggarbage(*path)) {
4722 const char *last = path++;
4723 while (path < end && istrailinggarbage(*path)) path++;
4724 if (path >= end || isADS(*path))
return (
char *)last;
4726 else if (isdirsep(*path)) {
4727 const char *last = path++;
4728 while (path < end && isdirsep(*path)) path++;
4729 if (path >= end)
return (
char *)last;
4730 if (isADS(*path)) path++;
4733 Inc(path, end, mb_enc, enc);
4736 return (
char *)path;
4740#define BUFCHECK(cond) do {\
4743 do {buflen *= 2;} while (cond);\
4744 rb_str_resize(result, buflen);\
4745 buf = RSTRING_PTR(result);\
4747 pend = buf + buflen;\
4751#define BUFINIT(result, buf, p, pend) do {\
4752 if (!result) { result = rb_usascii_str_new(0, 1); } \
4753 p = buf = RSTRING_PTR(result); \
4754 buflen = RSTRING_LEN(result); \
4755 pend = p + buflen; \
4759# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4761# define SKIPPATHSEP(p) 1
4764#define BUFCOPY(srcptr, srclen) do { \
4765 const int skip = SKIPPATHSEP(p); \
4766 rb_str_set_len(result, p-buf+skip); \
4767 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4769 memcpy(p, (srcptr), (srclen)); \
4773#define WITH_ROOTDIFF(stmt) do { \
4774 long rootdiff = root - buf; \
4776 root = buf + rootdiff; \
4780copy_home_path(
VALUE result,
const char *dir)
4786 dirlen = strlen(dir);
4787 rb_str_resize(result, dirlen);
4788 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4789 encidx = rb_filesystem_encindex();
4790 rb_enc_associate_index(result, encidx);
4791#if defined FILE_ALT_SEPARATOR
4793 bool mb_enc = enc_mbclen_needed(enc);
4794 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4795 if (*p == FILE_ALT_SEPARATOR) {
4807 VALUE dirname = rb_getpwdirnam_for_login(user);
4808 if (dirname ==
Qnil) {
4809 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4811 const char *dir = RSTRING_PTR(dirname);
4813 extern char *getlogin(
void);
4814 const char *pwPtr = 0;
4816 # define endpwent() ((void)0)
4817 const char *dir, *username = RSTRING_PTR(user);
4828 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4829 dir = pwPtr = getenv(
"HOME");
4831 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4834 copy_home_path(result, dir);
4840rb_default_home_dir(
VALUE result)
4842 const char *dir = getenv(
"HOME");
4844#if defined HAVE_PWD_H
4858 VALUE login_name = rb_getlogin();
4860# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4865 if (
NIL_P(login_name)) {
4866 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4870 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4871 if (
NIL_P(pw_dir)) {
4872 pw_dir = rb_getpwdiruid();
4873 if (
NIL_P(pw_dir)) {
4874 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4879 copy_home_path(result, RSTRING_PTR(pw_dir));
4880 rb_str_resize(pw_dir, 0);
4885 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4887 return copy_home_path(result, dir);
4893#if NORMALIZE_UTF8PATH
4894 VALUE path = rb_str_normalize_ospath(ptr,
len);
4895 rb_enc_associate(path, fsenc);
4898 return rb_enc_str_new(ptr,
len, fsenc);
4905 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4907 rb_enc_associate(dirname, fsenc);
4911 size_t dirlen = RSTRING_LEN(dirname);
4914 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4915 if (!rb_enc_compatible(fname, dirname)) {
4918 rb_enc_check(fname, dirname);
4919 rb_bug(
"unreachable");
4921 rb_encoding *direnc = fs_enc_check(fname, dirname);
4922 if (direnc != fsenc) {
4929 do {buflen *= 2;}
while (dirlen > buflen);
4930 rb_str_resize(result, buflen);
4931 buf = RSTRING_PTR(result);
4932 memcpy(buf, cwdp, dirlen);
4933 rb_enc_associate(result, *enc);
4934 return buf + dirlen;
4938rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4940 const char *s, *b, *fend;
4941 char *buf, *p, *pend, *root;
4942 size_t buflen, bdiff;
4943 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4946 fend = s + RSTRING_LEN(fname);
4947 enc = rb_str_enc_get(fname);
4948 bool mb_enc = enc_mbclen_needed(enc);
4949 if (!mb_enc &&
RTEST(dname)) {
4950 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4953 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4954 BUFINIT(result, buf, p, pend);
4957 if (s + 1 == fend || isdirsep(s[1])) {
4961 if (++s < fend) ++s;
4962 rb_default_home_dir(result);
4965 s = nextdirsep(b = s, fend, enc);
4968 BUFCHECK(bdiff + userlen >= buflen);
4969 memcpy(p, b, userlen);
4972 rb_enc_associate(result, enc);
4973 rb_home_dir_of(result, result);
4977 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4979 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4980 (
int)userlen, b, fname);
4983 rb_raise(rb_eArgError,
"non-absolute home");
4986 BUFINIT(result, buf, p, pend);
4989#ifdef DOSISH_DRIVE_LETTER
4991 else if (s + 1 < fend && has_drive_letter(s)) {
4992 BUFINIT(result, buf, p, pend);
4994 if (s + 2 < fend && isdirsep(s[2])) {
4997 BUFCHECK(bdiff + 2 >= buflen);
5001 rb_enc_copy(result, fname);
5006 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
5007 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
5008 BUFINIT(result, buf, p, pend);
5015 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
5016 BUFINIT(result, buf, p, pend);
5020 rb_enc_associate(result, enc = fs_enc_check(result, fname));
5023 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
5028 else if (s == fend || !rb_is_absolute_path(s)) {
5030 if (!
NIL_P(dname)) {
5032 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
5036 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
5038 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
5043 result = resized_result;
5047 rb_enc_associate(result, fs_enc_check(result, fname));
5048 BUFINIT(result, buf, p, pend);
5052 VALUE cwd = rb_dir_getwd_ospath();
5056 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
5057 BUFINIT(result, buf, p, pend);
5060#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5061 if (s < fend && isdirsep(*s)) {
5064 p = skipprefix(buf, p, mb_enc, enc);
5068 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
5071 BUFINIT(result, buf, p, pend);
5075 do s++;
while (s < fend && isdirsep(*s));
5078 BUFCHECK(bdiff >= buflen);
5079 memset(buf,
'/',
len);
5081 rb_enc_associate(result, fs_enc_check(result, fname));
5083 if (p > buf && p[-1] ==
'/')
5087 BUFCHECK(bdiff + 1 >= buflen);
5092 BUFCHECK(bdiff + 1 >= buflen);
5094 root = skipprefix(buf, p+1, mb_enc, enc);
5107 if (s+1 == fend || isdirsep(*(s+1))) {
5111 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
5121#if defined FILE_ALT_SEPARATOR
5122 case FILE_ALT_SEPARATOR:
5133#if defined FILE_ALT_SEPARATOR
5134 case FILE_ALT_SEPARATOR:
5137 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5145 int n = ignored_char_p(s, fend, enc);
5148 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5156 Inc(s, fend, mb_enc, enc);
5165 if (p == skiproot(buf, p + !!*p) - 1) p++;
5168 rb_enc_check(fname, result);
5175str_shrink(
VALUE str)
5177 rb_str_resize(str, RSTRING_LEN(str));
5181#define expand_path(fname, dname, abs_mode, long_name, result) \
5182 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5184#define check_expand_path_args(fname, dname) \
5185 (((fname) = rb_get_path(fname)), \
5186 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5189file_expand_path_1(
VALUE fname,
long extra_capa)
5192 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5198 check_expand_path_args(fname, dname);
5199 return expand_path(fname, dname, 0, 1,
Qfalse);
5203rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5205 return expand_path(fname, dname, 0, 0,
Qfalse);
5209rb_file_s_expand_path(
int argc,
const VALUE *argv)
5212 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5258 return rb_file_s_expand_path(c, v);
5264 check_expand_path_args(fname, dname);
5265 return expand_path(fname, dname, 1, 1,
Qfalse);
5269rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5272 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5315 return rb_file_s_absolute_path(c, v);
5343 VALUE path = rb_get_path(fname);
5345 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5349enum rb_realpath_mode {
5353 RB_REALPATH_MODE_MAX
5357realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5358 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5360 const char *pend = unresolved + strlen(unresolved);
5364 while (unresolved < pend) {
5365 const char *testname = unresolved;
5366 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5367 long testnamelen = unresolved_firstsep - unresolved;
5368 const char *unresolved_nextname = unresolved_firstsep;
5369 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5370 unresolved_nextname++;
5371 unresolved = unresolved_nextname;
5372 if (testnamelen == 1 && testname[0] ==
'.') {
5374 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5375 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5376 bool mb_enc = enc_mbclen_needed(enc);
5377 const char *resolved_str = RSTRING_PTR(*resolvedp);
5378 const char *resolved_names = resolved_str + *prefixlenp;
5379 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5380 long len = lastsep ? lastsep - resolved_names : 0;
5381 rb_str_resize(*resolvedp, *prefixlenp +
len);
5387 if (*prefixlenp < RSTRING_LEN(testpath))
5389#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5390 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5391 const char *prefix = RSTRING_PTR(testpath);
5392 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5397 checkval = rb_hash_aref(loopcheck, testpath);
5398 if (!
NIL_P(checkval)) {
5399 if (checkval ==
ID2SYM(resolving)) {
5400 if (mode == RB_REALPATH_CHECK) {
5404 rb_syserr_fail_path(ELOOP, testpath);
5413 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5416 if (e == ENOENT && !
NIL_P(fallback)) {
5417 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5422 if (mode == RB_REALPATH_CHECK)
return -1;
5424 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5425 rb_syserr_fail_path(e, testpath);
5426 *resolvedp = testpath;
5430 rb_syserr_fail_path(e, testpath);
5434 if (S_ISLNK(sbuf.st_mode)) {
5437 const char *link_prefix, *link_names;
5438 long link_prefixlen;
5439 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5440 link = rb_readlink(testpath, enc);
5441 link_prefix = RSTRING_PTR(link);
5442 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5443 link_prefixlen = link_names - link_prefix;
5444 if (link_prefixlen > 0) {
5448 tmpenc = fs_enc_check(*resolvedp, link);
5451 *prefixlenp = link_prefixlen;
5453 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5454 loopcheck, mode, !*unresolved_firstsep))
5463 rb_hash_aset(loopcheck, s, s);
5464 *resolvedp = testpath;
5472#ifdef DOSISH_DRIVE_LETTER
5476ospath_for_expand(
VALUE path)
5479 case ENCINDEX_ASCII_8BIT:
5480 case ENCINDEX_US_ASCII:
5481 return rb_enc_associate_index(
rb_str_dup(path), rb_filesystem_encindex());
5483 return TO_OSPATH(path);
5488rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5492 VALUE unresolved_path;
5497 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5498 char *ptr, *prefixptr = NULL, *pend;
5501#ifdef DOSISH_DRIVE_LETTER
5502 VALUE rootdir = path;
5504 if (!
NIL_P(basedir) && skipprefixroot(ptr, ptr +
len, rb_enc_get(path)) == ptr) {
5509 if (
len >= 2 && has_drive_letter(ptr) && (
len == 2 || !isdirsep(ptr[2]))) {
5512 if (!
NIL_P(basedir)) basedir = ospath_for_expand(rb_get_path(basedir));
5513 path = rb_file_absolute_path(ospath_for_expand(path), basedir);
5520 if (!
NIL_P(basedir)) {
5525 enc = rb_enc_get(unresolved_path);
5526 unresolved_path = TO_OSPATH(unresolved_path);
5528 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5529 if (ptr != path_names) {
5530 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5534 if (!
NIL_P(basedir)) {
5536 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5537 if (ptr != basedir_names) {
5543 curdir = rb_dir_getwd_ospath();
5545 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5550 pend = prefixptr + prefixlen;
5551 bool mb_enc = enc_mbclen_needed(enc);
5552 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5554 prefixlen = ++ptr - prefixptr;
5557#ifdef FILE_ALT_SEPARATOR
5558 while (prefixptr < ptr) {
5559 if (*prefixptr == FILE_ALT_SEPARATOR) {
5562 Inc(prefixptr, pend, mb_enc, enc);
5566 switch (rb_enc_to_index(enc)) {
5567 case ENCINDEX_ASCII_8BIT:
5568 case ENCINDEX_US_ASCII:
5569 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5572 loopcheck = rb_hash_new();
5574 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5577 if (basedir_names) {
5578 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5581 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5584 if (origenc && origenc != rb_enc_get(resolved)) {
5586 rb_enc_associate(resolved, origenc);
5598static VALUE rb_file_join(
long argc,
VALUE *args);
5600#ifndef HAVE_REALPATH
5602rb_check_realpath_emulate_try(
VALUE arg)
5605 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5609rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5616rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5619 VALUE unresolved_path;
5620 char *resolved_ptr = NULL;
5623 if (mode == RB_REALPATH_DIR) {
5624 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5628 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5629 VALUE paths[2] = {basedir, unresolved_path};
5630 unresolved_path = rb_file_join(2, paths);
5632 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5634 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), NULL)) == NULL) {
5643 if (
errno == ENOTSUP ||
5645 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5646 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5649 if (mode == RB_REALPATH_CHECK) {
5652 rb_sys_fail_path(unresolved_path);
5654 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5657# if !defined(__linux__) && !defined(__APPLE__)
5661 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5662 if (mode == RB_REALPATH_CHECK) {
5665 rb_sys_fail_path(unresolved_path);
5669 if (origenc && origenc != rb_enc_get(resolved)) {
5673 rb_enc_associate(resolved, origenc);
5676 if (is_broken_string(resolved)) {
5677 rb_enc_associate(resolved, rb_filesystem_encoding());
5678 if (is_broken_string(resolved)) {
5679 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5686 if (mode == RB_REALPATH_CHECK) {
5690 arg[2] = (
VALUE)origenc;
5693 rb_check_realpath_emulate_rescue,
Qnil);
5696 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5702rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5704 const enum rb_realpath_mode mode =
5705 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5706 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5712 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5729rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5732 VALUE path = argv[0];
5734 return rb_realpath_internal(basedir, path, 1);
5750rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5753 VALUE path = argv[0];
5755 return rb_realpath_internal(basedir, path, 0);
5759rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5763 const char *s, *last;
5765 if (!e || !l2)
return 0;
5767 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5768 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5769 if (c ==
'.')
return l0;
5774 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5779 if (l1 < l2)
return l1;
5782 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5783#if CASEFOLD_FILESYSTEM
5784#define fncomp strncasecmp
5786#define fncomp strncmp
5788 if (fncomp(s, e, l2) == 0) {
5794static inline const char *
5795enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5797 const char *p, *q, *e, *end;
5800 long len = (alllen ? (size_t)*alllen : strlen(name));
5807 name = skipprefix(name, end, mb_enc, enc);
5808#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5809 const char *root = name;
5812 while (name < end && isdirsep(*name)) {
5819#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5823#ifdef DOSISH_DRIVE_LETTER
5824 else if (*p ==
':') {
5837 p = strrdirsep(name, end, mb_enc, enc);
5842 while (isdirsep(*p)) {
5847 n = ntfs_tail(p, end, mb_enc, enc) - p;
5849 n = chompdirsep(p, end, mb_enc, enc) - p;
5851 for (q = p; q - p < n && *q ==
'.'; q++);
5852 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5853 if (*q ==
'.') e = q;
5873ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5875 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5919 const char *name, *p, *fp = 0;
5925 CheckPath(fname, name);
5929 check_path_encoding(fext);
5931 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5932 enc = rb_str_enc_get(fname);
5935 n = RSTRING_LEN(fname);
5936 if (n <= 0 || !*name) {
5937 return rb_enc_str_new(0, 0, enc);
5940 bool mb_enc = enc_mbclen_needed(enc);
5941 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5947 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5952 if (f == RSTRING_LEN(fname)) {
5957 return rb_enc_str_new(p, f, enc);
5960static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5983rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5989 return rb_file_dirname_n(argv[0], n);
5995 return rb_file_dirname_n(fname, 1);
5999rb_file_dirname_n(
VALUE fname,
int n)
6001 const char *name, *root, *p, *end;
6004 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
6005 CheckPath(fname, name);
6006 end = name + RSTRING_LEN(fname);
6008 bool mb_enc = !rb_str_enc_fastpath(fname);
6011 root = skiproot(name, end);
6013 if (root > name + 1 && isdirsep(*name))
6014 root = skipprefix(name = root - 2, end, mb_enc, enc);
6016 if (root > name + 1)
6019 if (n > (end - root + 1) / 2) {
6025 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
6034 return rb_enc_str_new(
".", 1, enc);
6036#ifdef DOSISH_DRIVE_LETTER
6037 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
6038 const char *top = skiproot(name + 2, end);
6039 dirname = rb_enc_str_new(name, 3, enc);
6044 dirname = rb_enc_str_new(name, p - name, enc);
6045#ifdef DOSISH_DRIVE_LETTER
6046 if (root == name + 2 && p == root && name[1] ==
':')
6052static inline const char *
6053enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
6055 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
6057 p = strrdirsep(name, end, mb_enc, enc);
6061 do name = ++p;
while (isdirsep(*p));
6064 while (*p && *p ==
'.') p++;
6066 if (*p ==
'.' || istrailinggarbage(*p)) {
6068 const char *last = p++, *dot = last;
6069 while (istrailinggarbage(*p)) {
6070 if (*p ==
'.') dot = p;
6073 if (!*p || isADS(*p)) {
6077 if (*last ==
'.' || dot > last) e = dot;
6084 else if (isADS(*p)) {
6088 else if (isdirsep(*p))
6090 Inc(p, end, mb_enc, enc);
6095 if (!e || e == name)
6120 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
6167 CheckPath(fname, name);
6168 long len = RSTRING_LEN(fname);
6171 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
6174 bool mb_enc = !rb_str_enc_fastpath(fname);
6177 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
6178 return rb_enc_str_new(ext,
len, enc);
6202 return rb_get_path(fname);
6234file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6236 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6237 return rb_file_join_ary(arg);
6241rb_file_join_ary(
VALUE ary)
6245 const char *name, *tail;
6255 check_path_encoding(tmp);
6256 len += RSTRING_LEN(tmp);
6264 RBASIC_CLEAR_CLASS(result);
6267 switch (OBJ_BUILTIN_TYPE(tmp)) {
6269 if (!checked) check_path_encoding(tmp);
6274 rb_raise(rb_eArgError,
"recursive array");
6286 rb_enc_copy(result, tmp);
6289 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6290 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6293 else if (tail == name +
len) {
6297 enc = fs_enc_check(result, tmp);
6299 rb_enc_associate(result, enc);
6307rb_file_join_fastpath(
long argc,
VALUE *args)
6312 for (i = 0; i < argc; i++) {
6313 VALUE tmp = args[i];
6315 size += RSTRING_LEN(tmp);
6328 const char *name = RSTRING_PTR(result);
6329 for (i = 1; i < argc; i++) {
6330 VALUE tmp = args[i];
6331 long len = RSTRING_LEN(result);
6337 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6340 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6345 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6352 rb_enc_associate(result, new_enc);
6353 encidx = rb_enc_to_index(new_enc);
6359 rb_str_null_check(result);
6364rb_file_join(
long argc,
VALUE *args)
6366 if (RB_UNLIKELY(argc == 0)) {
6370 VALUE result = rb_file_join_fastpath(argc, args);
6371 if (RB_LIKELY(result)) {
6391rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6393 return rb_file_join(argc, argv);
6396#if defined(HAVE_TRUNCATE)
6397struct truncate_arg {
6403nogvl_truncate(
void *ptr)
6405 struct truncate_arg *ta = ptr;
6406 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6438 struct truncate_arg ta;
6443 path = rb_str_encode_ospath(path);
6446 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6448 rb_sys_fail_path(path);
6452#define rb_file_s_truncate rb_f_notimplement
6455#if defined(HAVE_FTRUNCATE)
6456struct ftruncate_arg {
6462nogvl_ftruncate(
void *ptr)
6464 struct ftruncate_arg *fa = ptr;
6466 return (
VALUE)ftruncate(fa->fd, fa->pos);
6507 struct ftruncate_arg fa;
6514 rb_io_flush_raw(obj, 0);
6516 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6517 rb_sys_fail_path(fptr->
pathv);
6522#define rb_file_truncate rb_f_notimplement
6539#include <winerror.h>
6543rb_thread_flock(
void *data)
6546 int old_errno =
errno;
6548 int *op = data, ret = flock(op[0], op[1]);
6551 if (GetLastError() == ERROR_NOT_LOCKED) {
6609 op[1] = op1 =
NUM2INT(operation);
6614 rb_io_flush_raw(obj, 0);
6616 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6621#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6624 if (op1 & LOCK_NB)
return Qfalse;
6627 time.tv_usec = 100 * 1000;
6633#if defined(ERESTART)
6639 rb_syserr_fail_path(e, fptr->
pathv);
6646test_check(
int n,
int argc,
VALUE *argv)
6652 for (i=1; i<n; i++) {
6659#define CHECK(n) test_check((n), argc, argv)
6751 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6755 return rb_file_blockdev_p(0, argv[1]);
6758 return rb_file_chardev_p(0, argv[1]);
6761 return rb_file_directory_p(0, argv[1]);
6764 return rb_file_exist_p(0, argv[1]);
6767 return rb_file_file_p(0, argv[1]);
6770 return rb_file_sgid_p(0, argv[1]);
6773 return rb_file_grpowned_p(0, argv[1]);
6776 return rb_file_sticky_p(0, argv[1]);
6779 return rb_file_symlink_p(0, argv[1]);
6782 return rb_file_owned_p(0, argv[1]);
6785 return rb_file_rowned_p(0, argv[1]);
6788 return rb_file_pipe_p(0, argv[1]);
6791 return rb_file_readable_p(0, argv[1]);
6794 return rb_file_readable_real_p(0, argv[1]);
6797 return rb_file_size_p(0, argv[1]);
6800 return rb_file_socket_p(0, argv[1]);
6803 return rb_file_suid_p(0, argv[1]);
6806 return rb_file_writable_p(0, argv[1]);
6809 return rb_file_writable_real_p(0, argv[1]);
6812 return rb_file_executable_p(0, argv[1]);
6815 return rb_file_executable_real_p(0, argv[1]);
6818 return rb_file_zero_p(0, argv[1]);
6822 if (strchr(
"MAC", cmd)) {
6824 VALUE fname = argv[1];
6827 if (
rb_stat(fname, &st) == -1) {
6830 rb_syserr_fail_path(e, fname);
6835 return stat_atime(&st);
6837 return stat_mtime(&st);
6839 return stat_ctime(&st);
6845 return rb_file_identical_p(0, argv[1], argv[2]);
6848 if (strchr(
"=<>", cmd)) {
6849 struct stat st1, st2;
6856 t1 = stat_mtimespec(&st1);
6857 t2 = stat_mtimespec(&st2);
6861 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6865 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6866 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6870 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6871 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6878 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6881 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6930rb_stat_s_alloc(
VALUE klass)
6933 stat_alloc(klass, &obj);
6959 fname = rb_str_encode_ospath(fname);
6961 rb_sys_fail_path(fname);
6968 rb_st->initialized =
true;
6985 *copy_rb_st = *orig_rb_st;
7031rb_stat_ftype(
VALUE obj)
7033 return rb_file_ftype(get_stat(obj)->ST_(mode));
7055 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
7082 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
7119 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
7151 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
7181 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
7209 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7244rb_stat_owned(
VALUE obj)
7246 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7251rb_stat_rowned(
VALUE obj)
7253 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7280rb_stat_grpowned(
VALUE obj)
7283 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7312 rb_io_stat_data *st = get_stat(obj);
7315 if (geteuid() == 0)
return Qtrue;
7318 if (rb_stat_owned(obj))
7319 return RBOOL(st->ST_(mode) & S_IRUSR);
7322 if (rb_stat_grpowned(obj))
7323 return RBOOL(st->ST_(mode) & S_IRGRP);
7326 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7344 rb_io_stat_data *st = get_stat(obj);
7347 if (getuid() == 0)
return Qtrue;
7350 if (rb_stat_rowned(obj))
7351 return RBOOL(st->ST_(mode) & S_IRUSR);
7354 if (rb_group_member(get_stat(obj)->ST_(gid)))
7355 return RBOOL(st->ST_(mode) & S_IRGRP);
7358 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7386rb_stat_wr(
VALUE obj)
7389 rb_io_stat_data *st = get_stat(obj);
7390 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7391 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7423 rb_io_stat_data *st = get_stat(obj);
7426 if (geteuid() == 0)
return Qtrue;
7429 if (rb_stat_owned(obj))
7430 return RBOOL(st->ST_(mode) & S_IWUSR);
7433 if (rb_stat_grpowned(obj))
7434 return RBOOL(st->ST_(mode) & S_IWGRP);
7437 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7458 rb_io_stat_data *st = get_stat(obj);
7461 if (getuid() == 0)
return Qtrue;
7464 if (rb_stat_rowned(obj))
7465 return RBOOL(st->ST_(mode) & S_IWUSR);
7468 if (rb_group_member(get_stat(obj)->ST_(gid)))
7469 return RBOOL(st->ST_(mode) & S_IWGRP);
7472 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7501rb_stat_ww(
VALUE obj)
7504 rb_io_stat_data *st = get_stat(obj);
7505 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7506 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7537 rb_io_stat_data *st = get_stat(obj);
7540 if (geteuid() == 0) {
7541 return RBOOL(st->ST_(mode) & S_IXUGO);
7545 if (rb_stat_owned(obj))
7546 return RBOOL(st->ST_(mode) & S_IXUSR);
7549 if (rb_stat_grpowned(obj))
7550 return RBOOL(st->ST_(mode) & S_IXGRP);
7553 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7570 rb_io_stat_data *st = get_stat(obj);
7573 if (getuid() == 0) {
7574 return RBOOL(st->ST_(mode) & S_IXUGO);
7578 if (rb_stat_rowned(obj))
7579 return RBOOL(st->ST_(mode) & S_IXUSR);
7582 if (rb_group_member(get_stat(obj)->ST_(gid)))
7583 return RBOOL(st->ST_(mode) & S_IXGRP);
7586 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7610 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7655 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7687 rb_off_t size = get_stat(obj)->ST_(size);
7689 if (size == 0)
return Qnil;
7722rb_stat_suid(
VALUE obj)
7725 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7759rb_stat_sgid(
VALUE obj)
7762 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7796rb_stat_sticky(
VALUE obj)
7799 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7804#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7805#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7816nogvl_mkfifo(
void *ptr)
7818 struct mkfifo_arg *ma = ptr;
7820 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7848 struct mkfifo_arg ma;
7857 path = rb_str_encode_ospath(path);
7858 ma.path = RSTRING_PTR(path);
7859 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7860 rb_sys_fail_path(path);
7865#define rb_file_s_mkfifo rb_f_notimplement
7868static VALUE rb_mFConst;
7871rb_file_const(
const char *name,
VALUE value)
7873 rb_define_const(rb_mFConst, name, value);
7877rb_is_absolute_path(
const char *path)
7879#ifdef DOSISH_DRIVE_LETTER
7880 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7883 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7886 if (path[0] ==
'/')
return 1;
7892ruby_is_fd_loadable(
int fd)
7899 if (fstat(fd, &st) < 0)
7902 if (S_ISREG(st.st_mode))
7905 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7908 if (S_ISDIR(st.st_mode))
7919rb_file_load_ok(
const char *path)
7926 int mode = (O_RDONLY |
7927#if defined O_NONBLOCK
7929#elif defined O_NDELAY
7935 if (!rb_gc_for_fd(
errno))
return 0;
7937 if (fd < 0)
return 0;
7940 ret = ruby_is_fd_loadable(fd);
7947is_explicit_relative(
const char *path)
7949 if (*path++ !=
'.')
return 0;
7950 if (*path ==
'.') path++;
7951 return isdirsep(*path);
7957 int encidx = rb_enc_get_index(orig);
7958 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7959 encidx = rb_filesystem_encindex();
7960 rb_enc_associate_index(path, encidx);
7968nav_component_p(
const char *s,
const char *send)
7970 if ((send - s) >= 2 && s[0] ==
'.') {
7971 return s[1] ==
'.' || isdirsep(s[1]);
7977fname_need_expansion_p(
VALUE fname)
7979 const char *s = RSTRING_PTR(fname);
7980 const long len = RSTRING_LEN(fname);
7981 const char *send = s +
len;
7983 if (nav_component_p(s, send)) {
7988 bool mbenc = enc_mbclen_needed(enc);
7990 s = enc_path_next(s, send, mbenc, enc);
7992 if (nav_component_p(s, send)) {
7996 s = enc_path_next(s, send, mbenc, enc);
8002expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
8004 long dname_len = RSTRING_LEN(dname);
8005 const char *dname_ptr = RSTRING_PTR(dname);
8009 if (need_expansion || dname_ptr[0] ==
'~') {
8010 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
8015 if (!isdirsep(dname_ptr[dname_len - 1])) {
8024rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
8027 VALUE fname = *filep;
8031 if (!ext[0])
return 0;
8034 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
8035 f = RSTRING_PTR(fname);
8040 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
8041 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
8042 fnlen = RSTRING_LEN(fname);
8043 for (i=0; ext[i]; i++) {
8045 if (rb_file_load_ok(RSTRING_PTR(fname))) {
8046 *filep = copy_path_class(fname, *filep);
8054 long expanded_load_path_maxlen;
8055 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8056 if (!load_path)
return 0;
8059 RBASIC_CLEAR_CLASS(fname);
8060 fnlen = RSTRING_LEN(fname);
8061 bool need_expansion = fname_need_expansion_p(fname);
8064 rb_enc_associate_index(tmp, rb_usascii_encindex());
8066 for (j=0; ext[j]; j++) {
8068 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
8070 if (!RSTRING_LEN(dname))
continue;
8071 expand_feature(fname, dname, tmp, need_expansion);
8073 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8074 *filep = copy_path_class(tmp, *filep);
8080 rb_str_resize(tmp, 0);
8093 path = copy_path_class(file_expand_path_1(path, 0), path);
8094 f = RSTRING_PTR(path);
8098 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
8099 if (!rb_file_load_ok(f))
return 0;
8101 path = copy_path_class(file_expand_path_1(path, 0), path);
8105 long expanded_load_path_maxlen;
8106 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8109 bool need_expansion = fname_need_expansion_p(path);
8111 rb_enc_associate_index(tmp, rb_usascii_encindex());
8112 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
8114 if (!RSTRING_LEN(dname))
continue;
8115 expand_feature(path, dname, tmp, need_expansion);
8117 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8118 return copy_path_class(tmp, path);
8121 rb_str_resize(tmp, 0);
8129#define define_filetest_function(name, func, argc) do { \
8130 rb_define_module_function(rb_mFileTest, name, func, argc); \
8131 rb_define_singleton_method(rb_cFile, name, func, argc); \
8134const char ruby_null_device[] =
8937#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8938 rb_CFString_class_initialize_before_fork();
8946 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8947 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8948 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8949 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8950 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8951 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8952 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8953 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8954 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8955 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8956 define_filetest_function(
"file?", rb_file_file_p, 1);
8957 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8958 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8959 define_filetest_function(
"size?", rb_file_size_p, 1);
8960 define_filetest_function(
"size", rb_file_s_size, 1);
8961 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8962 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8964 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8965 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8966 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8968 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8969 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8971 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8972 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8973 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8975 define_filetest_function(
"identical?", rb_file_identical_p, 2);
9013 separator = rb_fstring_lit(
"/");
9015 rb_define_const(
rb_cFile,
"Separator", separator);
9017 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9366 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9369 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9371 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9373 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9375 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9377 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9379 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9380#if defined(O_NDELAY) || defined(O_NONBLOCK)
9382# define O_NONBLOCK O_NDELAY
9385 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9388 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9391 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9397 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9398#ifndef O_SHARE_DELETE
9399# define O_SHARE_DELETE 0
9402 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9405 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9409 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9413 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9417 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9421 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9425 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9429 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9433 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9435 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9437 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9439 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9442 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_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#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.
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(long len)
Allocates a "temporary" string.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
#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.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_cat(VALUE dst, const char *src, long 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.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
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).
#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.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
#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.
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.