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));
777rb_stat_ino(
VALUE self)
779 rb_io_stat_data *ptr = get_stat(self);
780#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
782 return rb_integer_unpack(&ptr->st_ino, 2,
783 SIZEOF_STRUCT_STAT_ST_INO, 0,
787 return UIANY2NUM(ptr->ST_(ino));
805rb_stat_mode(
VALUE self)
807 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
823rb_stat_nlink(
VALUE self)
826 const rb_io_stat_data *ptr = get_stat(self);
828 return UIANY2NUM(ptr->ST_(nlink));
842rb_stat_uid(
VALUE self)
844 return UIDT2NUM(get_stat(self)->ST_(uid));
869rb_stat_gid(
VALUE self)
871 return GIDT2NUM(get_stat(self)->ST_(gid));
887rb_stat_rdev(
VALUE self)
890 unsigned int m = get_stat(self)->stx_rdev_major;
891 unsigned int n = get_stat(self)->stx_rdev_minor;
893#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
895#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
896 return DEVT2NUM(get_stat(self)->ST_(rdev));
897#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
898 return ULONG2NUM(get_stat(self)->ST_(rdev));
900 return ULL2NUM(get_stat(self)->ST_(rdev));
915rb_stat_rdev_major(
VALUE self)
918 return UINT2NUM(get_stat(self)->stx_rdev_major);
919#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
920 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
937rb_stat_rdev_minor(
VALUE self)
940 return UINT2NUM(get_stat(self)->stx_rdev_minor);
941#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
942 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
978rb_stat_size(
VALUE self)
980 return OFFT2NUM(get_stat(self)->ST_(size));
1002rb_stat_blksize(
VALUE self)
1004#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1005 return ULONG2NUM(get_stat(self)->ST_(blksize));
1029rb_stat_blocks(
VALUE self)
1031#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1032# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
1033 return ULL2NUM(get_stat(self)->ST_(blocks));
1035 return ULONG2NUM(get_stat(self)->ST_(blocks));
1043stat_atimespec(
const struct stat *st)
1046 ts.tv_sec = st->st_atime;
1047#if defined(HAVE_STRUCT_STAT_ST_ATIM)
1048 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
1049#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1050 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
1051#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
1052 ts.tv_nsec = (uint32_t)st->st_atimensec;
1061statx_atimespec(
const rb_io_stat_data *st)
1063 return st->stx_atime;
1066# define statx_atimespec stat_atimespec
1076stat_atime(
const struct stat *st)
1078 return stat_time(stat_atimespec(st));
1082stat_mtimespec(
const struct stat *st)
1085 ts.tv_sec = st->st_mtime;
1086#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1087 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1088#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1089 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1090#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1091 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1099stat_mtime(
const struct stat *st)
1101 return stat_time(stat_mtimespec(st));
1105stat_ctimespec(
const struct stat *st)
1108 ts.tv_sec = st->st_ctime;
1109#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1110 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1111#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1112 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1113#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1114 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1123statx_ctimespec(
const rb_io_stat_data *st)
1125 return st->stx_ctime;
1128# define statx_ctimespec stat_ctimespec
1132stat_ctime(
const struct stat *st)
1134 return stat_time(stat_ctimespec(st));
1137#define HAVE_STAT_BIRTHTIME
1138#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1140statx_birthtime(
const rb_io_stat_data *st)
1145#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1146static VALUE statx_birthtime(
const rb_io_stat_data *st);
1147#elif defined(_WIN32)
1148# define statx_birthtime stat_ctime
1150# undef HAVE_STAT_BIRTHTIME
1199rb_stat_atime(
VALUE self)
1201 return stat_time(statx_atimespec(get_stat(self)));
1232rb_stat_mtime(
VALUE self)
1234 return stat_time(statx_mtimespec(get_stat(self)));
1252rb_stat_ctime(
VALUE self)
1254 return stat_time(statx_ctimespec(get_stat(self)));
1257#if defined(HAVE_STAT_BIRTHTIME)
1280rb_stat_birthtime(
VALUE self)
1282 return statx_birthtime(get_stat(self));
1285# define rb_stat_birthtime rb_f_notimplement
1304rb_stat_inspect(
VALUE self)
1308 static const struct {
1312 {
"dev", rb_stat_dev},
1313 {
"ino", rb_stat_ino},
1314 {
"mode", rb_stat_mode},
1315 {
"nlink", rb_stat_nlink},
1316 {
"uid", rb_stat_uid},
1317 {
"gid", rb_stat_gid},
1318 {
"rdev", rb_stat_rdev},
1319 {
"size", rb_stat_size},
1320 {
"blksize", rb_stat_blksize},
1321 {
"blocks", rb_stat_blocks},
1322 {
"atime", rb_stat_atime},
1323 {
"mtime", rb_stat_mtime},
1324 {
"ctime", rb_stat_ctime},
1325#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1326 {
"birthtime", rb_stat_birthtime},
1332 if (!rb_st->initialized) {
1340 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1348 v = (*member[i].func)(self);
1350 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1352 else if (i == 0 || i == 6) {
1353 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1373no_gvl_fstat(
void *data)
1376 return (
VALUE)fstat(arg->file.fd, arg->st);
1380fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1384 data.file.fd = fptr->
fd;
1387 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1391no_gvl_stat(
void * data)
1394 return (
void *)(
VALUE)stat(arg->file.path, arg->st);
1398stat_without_gvl(
const char *path,
struct stat *st)
1402 data.file.path = path;
1405 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1408#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1409 defined(HAVE_STRUCT_STATX_STX_BTIME)
1411# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1414# ifdef HAVE_SYSCALL_H
1415# include <syscall.h>
1416# elif defined HAVE_SYS_SYSCALL_H
1417# include <sys/syscall.h>
1419# if defined __linux__
1420# include <linux/stat.h>
1422statx(
int dirfd,
const char *pathname,
int flags,
1423 unsigned int mask,
struct statx *statxbuf)
1425 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1430typedef struct no_gvl_rb_io_stat_data {
1436} no_gvl_rb_io_stat_data;
1439io_blocking_statx(
void *data)
1441 no_gvl_rb_io_stat_data *arg = data;
1442 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1446no_gvl_statx(
void *data)
1448 return (
void *)io_blocking_statx(data);
1452statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1454 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1457 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1461lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1463 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1466 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1470fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1472 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1475 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1478#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1481rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1486 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1491 result = fstatx_without_gvl(fptr, stx, mask);
1496 file = rb_str_encode_ospath(file);
1497 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1503# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1505NORETURN(
static void statx_notimplement(
const char *field_name));
1510statx_notimplement(
const char *field_name)
1513 "%s is unimplemented on this filesystem",
1518statx_birthtime(
const rb_io_stat_data *stx)
1520 if (!statx_has_birthtime(stx)) {
1522 statx_notimplement(
"birthtime");
1524 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1529# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1530# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1531# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1532# define rb_statx(file, stx, mask) rb_stat(file, stx)
1533# define STATX(path, st, mask) stat(path, st)
1535#if defined(HAVE_STAT_BIRTHTIME)
1536# define statx_has_birthtime(st) 1
1538# define statx_has_birthtime(st) 0
1545# define FSTAT(fd, st) fstat(fd, st)
1554 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1559 result = fstat_without_gvl(fptr, st);
1564 file = rb_str_encode_ospath(file);
1565 result = stat_without_gvl(RSTRING_PTR(file), st);
1603 fname = rb_str_encode_ospath(fname);
1604 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1605 rb_sys_fail_path(fname);
1607 return rb_statx_new(&st);
1639rb_io_stat(
VALUE obj)
1645 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1646 rb_sys_fail_path(fptr->
pathv);
1648 return rb_statx_new(&st);
1653no_gvl_lstat(
void *ptr)
1656 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1660lstat_without_gvl(
const char *path,
struct stat *st)
1664 data.file.path = path;
1667 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1704 fname = rb_str_encode_ospath(fname);
1705 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1706 rb_sys_fail_path(fname);
1708 return rb_statx_new(&st);
1710 return rb_file_s_stat(klass, fname);
1743rb_file_lstat(
VALUE obj)
1752 path = rb_str_encode_ospath(fptr->
pathv);
1753 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1754 rb_sys_fail_path(fptr->
pathv);
1756 return rb_statx_new(&st);
1758 return rb_io_stat(obj);
1763rb_group_member(GETGROUPS_T gid)
1765#if !defined(HAVE_GETGROUPS)
1774 if (getgid() == gid || getegid() == gid)
1777 groups = getgroups(0, NULL);
1778 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1779 anum = getgroups(groups, gary);
1780 while (--anum >= 0) {
1781 if (gary[anum] == gid) {
1794# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1797#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1798#define USE_GETEUID 1
1803eaccess(
const char *path,
int mode)
1812 if (getuid() == euid && getgid() == getegid())
1813 return access(path, mode);
1815 if (stat(path, &st) < 0)
1825 if (st.st_mode & S_IXUGO)
1831 if (st.st_uid == euid)
1833 else if (rb_group_member(st.st_gid))
1836 if ((
int)(st.st_mode & mode) == mode)
return 0;
1840 return access(path, mode);
1851nogvl_eaccess(
void *ptr)
1855 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1859rb_eaccess(
VALUE fname,
int mode)
1864 fname = rb_str_encode_ospath(fname);
1868 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1872nogvl_access(
void *ptr)
1876 return (
void *)(
VALUE)access(aa->path, aa->mode);
1880rb_access(
VALUE fname,
int mode)
1885 fname = rb_str_encode_ospath(fname);
1889 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1924# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1930 if (S_ISDIR(st.st_mode))
return Qtrue;
1959# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1965 if (S_ISFIFO(st.st_mode))
return Qtrue;
1997# define S_ISLNK(m) _S_ISLNK(m)
2000# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
2003# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
2013 fname = rb_str_encode_ospath(fname);
2015 if (S_ISLNK(st.st_mode))
return Qtrue;
2050# define S_ISSOCK(m) _S_ISSOCK(m)
2053# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
2056# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
2066 if (S_ISSOCK(st.st_mode))
return Qtrue;
2098# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
2100# define S_ISBLK(m) (0)
2108 if (S_ISBLK(st.st_mode))
return Qtrue;
2135# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2141 if (S_ISCHR(st.st_mode))
return Qtrue;
2211 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2225rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2227 return RBOOL(rb_access(fname, R_OK) >= 0);
2231# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2235# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2264rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2270 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2271 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2305 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2322rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2324 return RBOOL(rb_access(fname, W_OK) >= 0);
2353rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2359 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2360 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2396 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2415rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2417 return RBOOL(rb_access(fname, X_OK) >= 0);
2421# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2449 return RBOOL(S_ISREG(st.st_mode));
2509 return RBOOL(st.st_size == 0);
2547 if (st.st_size == 0)
return Qnil;
2581 return RBOOL(st.st_uid == geteuid());
2590 return RBOOL(st.st_uid == getuid());
2626 if (rb_group_member(st.st_gid))
return Qtrue;
2631#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2633check3rdbyte(
VALUE fname,
int mode)
2638 return RBOOL(st.st_mode & mode);
2671 return check3rdbyte(fname, S_ISUID);
2706 return check3rdbyte(fname, S_ISGID);
2742 return check3rdbyte(fname, S_ISVTX);
2779 struct stat st1, st2;
2783 if (st1.st_dev != st2.st_dev)
return Qfalse;
2784 if (st1.st_ino != st2.st_ino)
return Qfalse;
2788 return rb_w32_file_identical_p(fname1, fname2);
2814 if (
rb_stat(fname, &st) < 0) {
2817 rb_syserr_fail_path(e, fname);
2823rb_file_ftype(mode_t mode)
2827 if (S_ISREG(mode)) {
2830 else if (S_ISDIR(mode)) {
2833 else if (S_ISCHR(mode)) {
2834 t =
"characterSpecial";
2837 else if (S_ISBLK(mode)) {
2842 else if (S_ISFIFO(mode)) {
2847 else if (S_ISLNK(mode)) {
2852 else if (S_ISSOCK(mode)) {
2860 return rb_fstring_cstr(t);
2909 fname = rb_str_encode_ospath(fname);
2911 rb_sys_fail_path(fname);
2914 return rb_file_ftype(st.st_mode);
2970 if (
rb_stat(fname, &st) < 0) {
2973 rb_syserr_fail_path(e, fname);
2975 return stat_time(stat_atimespec(&st));
3007rb_file_atime(
VALUE obj)
3013 if (fstat(fptr->
fd, &st) == -1) {
3014 rb_sys_fail_path(fptr->
pathv);
3016 return stat_time(stat_atimespec(&st));
3056 if (
rb_stat(fname, &st) < 0) {
3059 rb_syserr_fail_path(e, fname);
3061 return stat_time(stat_mtimespec(&st));
3086rb_file_mtime(
VALUE obj)
3092 if (fstat(fptr->
fd, &st) == -1) {
3093 rb_sys_fail_path(fptr->
pathv);
3095 return stat_time(stat_mtimespec(&st));
3138 if (
rb_stat(fname, &st) < 0) {
3141 rb_syserr_fail_path(e, fname);
3143 return stat_time(stat_ctimespec(&st));
3160rb_file_ctime(
VALUE obj)
3166 if (fstat(fptr->
fd, &st) == -1) {
3167 rb_sys_fail_path(fptr->
pathv);
3169 return stat_time(stat_ctimespec(&st));
3172#if defined(HAVE_STAT_BIRTHTIME)
3198 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3201 rb_syserr_fail_path(e, fname);
3203 return statx_birthtime(&st);
3206# define rb_file_s_birthtime rb_f_notimplement
3209#if defined(HAVE_STAT_BIRTHTIME)
3230rb_file_birthtime(
VALUE obj)
3236 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3237 rb_sys_fail_path(fptr->
pathv);
3239 return statx_birthtime(&st);
3242# define rb_file_birthtime rb_f_notimplement
3254 rb_io_flush_raw(file, 0);
3257 if (fstat(fptr->
fd, &st) == -1) {
3258 rb_sys_fail_path(fptr->
pathv);
3284file_size(
VALUE self)
3286 return OFFT2NUM(rb_file_size(self));
3295nogvl_chmod(
void *ptr)
3298 int ret = chmod(data->path, data->mode);
3299 return (
void *)(
VALUE)ret;
3303rb_chmod(
const char *path, mode_t mode)
3309 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3313chmod_internal(
const char *path,
void *mode)
3315 return chmod(path, *(mode_t *)mode);
3348 return apply2files(chmod_internal, argc, argv, &mode);
3352struct nogvl_fchmod_data {
3358io_blocking_fchmod(
void *ptr)
3360 struct nogvl_fchmod_data *data = ptr;
3361 int ret = fchmod(data->fd, data->mode);
3366rb_fchmod(
struct rb_io* io, mode_t mode)
3369 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3370 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3399#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3407 if (rb_fchmod(fptr, mode) == -1) {
3408 if (HAVE_FCHMOD ||
errno != ENOSYS)
3409 rb_sys_fail_path(fptr->
pathv);
3412 if (!HAVE_FCHMOD)
return INT2FIX(0);
3415#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3417 path = rb_str_encode_ospath(fptr->
pathv);
3418 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3419 rb_sys_fail_path(fptr->
pathv);
3425#if defined(HAVE_LCHMOD)
3427lchmod_internal(
const char *path,
void *mode)
3429 return lchmod(path, *(mode_t *)mode);
3466 return apply2files(lchmod_internal, argc, argv, &mode);
3469#define rb_file_s_lchmod rb_f_notimplement
3472static inline rb_uid_t
3476 return (rb_uid_t)-1;
3481static inline rb_gid_t
3485 return (rb_gid_t)-1;
3496chown_internal(
const char *path,
void *arg)
3499 return chown(path, args->owner, args->group);
3553 arg.owner = to_uid(*argv++);
3554 arg.group = to_gid(*argv++);
3556 return apply2files(chown_internal, argc, argv, &arg);
3568nogvl_chown(
void *ptr)
3571 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3575rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3578 .as = {.path = path},
3579 .new = {.owner = owner, .group = group},
3581 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3586nogvl_fchown(
void *ptr)
3589 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3593rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3598 .new = {.owner = owner, .group = group},
3600 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3634 path = rb_str_encode_ospath(fptr->
pathv);
3635 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3636 rb_sys_fail_path(fptr->
pathv);
3638 if (rb_fchown(fptr->
fd, o, g) == -1)
3639 rb_sys_fail_path(fptr->
pathv);
3645#if defined(HAVE_LCHOWN)
3647lchown_internal(
const char *path,
void *arg)
3650 return lchown(path, args->owner, args->group);
3707 arg.owner = to_uid(*argv++);
3708 arg.group = to_gid(*argv++);
3710 return apply2files(lchown_internal, argc, argv, &arg);
3713#define rb_file_s_lchown rb_f_notimplement
3723NORETURN(
static void utime_failed(
struct apply_arg *));
3729 VALUE path = aa->fn[aa->i].path;
3732 if (ua->tsp && e == EINVAL) {
3735 VALUE atime = ua->atime;
3736 VALUE mtime = ua->mtime;
3738 if (!
NIL_P(atime)) {
3741 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3744 if (
NIL_P(a)) e[0] = m;
3760 rb_syserr_fail_path(e, path);
3764#if defined(HAVE_UTIMES)
3766# if !defined(HAVE_UTIMENSAT)
3768# elif defined(__APPLE__) && \
3769 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3771# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3772typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3776static inline utimensat_func *
3783# define utimensat rb_utimensat()
3790utime_internal(
const char *path,
void *arg)
3793 const struct timespec *tsp = v->tsp;
3794 struct timeval tvbuf[2], *tvp = NULL;
3796#if defined(HAVE_UTIMENSAT)
3797# if defined(__APPLE__)
3798 const int try_utimensat = utimensat != NULL;
3799 const int try_utimensat_follow = utimensat != NULL;
3801# define TRY_UTIMENSAT 1
3802 static int try_utimensat = 1;
3803# ifdef AT_SYMLINK_NOFOLLOW
3804 static int try_utimensat_follow = 1;
3806 const int try_utimensat_follow = 0;
3811 if (v->follow ? try_utimensat_follow : try_utimensat) {
3812# ifdef AT_SYMLINK_NOFOLLOW
3814 flags = AT_SYMLINK_NOFOLLOW;
3818 int result = utimensat(AT_FDCWD, path, tsp, flags);
3819# ifdef TRY_UTIMENSAT
3820 if (result < 0 &&
errno == ENOSYS) {
3821# ifdef AT_SYMLINK_NOFOLLOW
3822 try_utimensat_follow = 0;
3834 tvbuf[0].tv_sec = tsp[0].tv_sec;
3835 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3836 tvbuf[1].tv_sec = tsp[1].tv_sec;
3837 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3841 if (v->follow)
return lutimes(path, tvp);
3843 return utimes(path, tvp);
3848#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3856utime_internal(
const char *path,
void *arg)
3860 struct utimbuf utbuf, *utp = NULL;
3862 utbuf.actime = tsp[0].tv_sec;
3863 utbuf.modtime = tsp[1].tv_sec;
3866 return utime(path, utp);
3871utime_internal_i(
int argc,
VALUE *argv,
int follow)
3874 struct timespec tss[2], *tsp = NULL;
3877 args.atime = *argv++;
3878 args.mtime = *argv++;
3880 args.follow = follow;
3882 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3885 if (args.atime == args.mtime)
3892 return apply2files(utime_internal, argc, argv, &args);
3938 return utime_internal_i(argc, argv, FALSE);
3941#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3999 return utime_internal_i(argc, argv, TRUE);
4002#define rb_file_s_lutime rb_f_notimplement
4005#ifdef RUBY_FUNCTION_NAME_STRING
4006# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
4008# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
4010#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
4011NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
4013syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
4017 const int max_pathlen = MAX_PATH;
4019 const int max_pathlen = MAXPATHLEN;
4030#ifdef RUBY_FUNCTION_NAME_STRING
4031 rb_syserr_fail_path_in(func, e, str);
4033 rb_syserr_fail_path(e, str);
4068 from = rb_str_encode_ospath(from);
4069 to = rb_str_encode_ospath(to);
4072 sys_fail2(from, to);
4077#define rb_file_s_link rb_f_notimplement
4118 from = rb_str_encode_ospath(from);
4119 to = rb_str_encode_ospath(to);
4122 sys_fail2(from, to);
4127#define rb_file_s_symlink rb_f_notimplement
4155 return rb_readlink(path, rb_filesystem_encoding());
4158struct readlink_arg {
4165nogvl_readlink(
void *ptr)
4167 struct readlink_arg *ra = ptr;
4169 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4173readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4175 struct readlink_arg ra;
4177 ra.path = RSTRING_PTR(path);
4178 ra.buf = RSTRING_PTR(buf);
4181 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4192 path = rb_str_encode_ospath(path);
4193 v = rb_enc_str_new(0, size, enc);
4194 while ((rv = readlink_without_gvl(path, v, size)) == size
4196 || (rv < 0 &&
errno == ERANGE)
4205 rb_str_resize(v, 0);
4206 rb_syserr_fail_path(e, path);
4208 rb_str_resize(v, rv);
4213#define rb_file_s_readlink rb_f_notimplement
4217unlink_internal(
const char *path,
void *arg)
4219 return unlink(path);
4262rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4264 return apply2files(unlink_internal, argc, argv, 0);
4273no_gvl_rename(
void *ptr)
4277 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4355 f = rb_str_encode_ospath(from);
4356 t = rb_str_encode_ospath(to);
4359#if defined __CYGWIN__
4362 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4367 if (chmod(ra.dst, 0666) == 0 &&
4368 unlink(ra.dst) == 0 &&
4369 rename(ra.src, ra.dst) == 0)
4373 syserr_fail2(e, from, to);
4407 rb_error_arity(argc, 0, 1);
4415#if defined __CYGWIN__ || defined DOSISH
4417#define DOSISH_DRIVE_LETTER
4418#define FILE_ALT_SEPARATOR '\\'
4420#ifdef FILE_ALT_SEPARATOR
4421#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4423static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4426#define isdirsep(x) ((x) == '/')
4438#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4439#define isADS(x) ((x) == ':')
4441#define istrailinggarbage(x) 0
4444#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4446#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4447#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4449#if defined(DOSISH_UNC)
4450#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4452#define has_unc(buf) 0
4455#ifdef DOSISH_DRIVE_LETTER
4457has_drive_letter(
const char *buf)
4459 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4484 if (chdir(drive) == 0) {
4485 drvcwd = rb_dir_getwd_ospath();
4497not_same_drive(
VALUE path,
int drive)
4499 const char *p = RSTRING_PTR(path);
4500 if (RSTRING_LEN(path) < 2)
return 0;
4501 if (has_drive_letter(p)) {
4512skiproot(
const char *path,
const char *end)
4514#ifdef DOSISH_DRIVE_LETTER
4515 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4517 while (path < end && isdirsep(*path)) path++;
4518 return (
char *)path;
4522enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4524 while (s < e && !isdirsep(*s)) {
4525 Inc(s, e, mb_enc, enc);
4530#define nextdirsep rb_enc_path_next
4534 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4537#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4538#define skipprefix enc_path_skip_prefix
4540#define skipprefix(path, end, mb_enc, enc) (path)
4543enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4545#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4547 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4549 while (path < end && isdirsep(*path)) path++;
4550 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4551 path + 2 <= end && !isdirsep(path[1])) {
4552 path = enc_path_next(path + 1, end, mb_enc, enc);
4554 return (
char *)path;
4557#ifdef DOSISH_DRIVE_LETTER
4558 if (path + 2 <= end && has_drive_letter(path))
4559 return (
char *)(path + 2);
4562 return (
char *)path;
4566rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4568 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4572skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4574#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4575 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4576 while (p < end && isdirsep(*p)) p++;
4579 return skiproot(path, end);
4584rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4586 return skipprefixroot(path, end, enc);
4590enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4593 while (path < end) {
4594 if (isdirsep(*path)) {
4595 const char *tmp = path++;
4596 while (path < end && isdirsep(*path)) path++;
4597 if (path >= end)
break;
4601 Inc(path, end, mb_enc, enc);
4607rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4609 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4613strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4615 if (RB_UNLIKELY(mb_enc)) {
4616 return enc_path_last_separator(path, end, mb_enc, enc);
4619 const char *cursor = end - 1;
4621 while (cursor >= path && isdirsep(cursor[0])) {
4625 while (cursor >= path) {
4626 if (isdirsep(cursor[0])) {
4627 while (cursor > path && isdirsep(cursor[-1])) {
4630 return (
char *)cursor;
4638chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4640 while (path < end) {
4641 if (isdirsep(*path)) {
4642 const char *last = path++;
4643 while (path < end && isdirsep(*path)) path++;
4644 if (path >= end)
return (
char *)last;
4647 Inc(path, end, mb_enc, enc);
4650 return (
char *)path;
4656 if (path < end && isdirsep(*path)) path++;
4657 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4663 rb_encoding *enc = rb_enc_check_str(path1, path2);
4664 int encidx = rb_enc_to_index(enc);
4665 if (encidx == ENCINDEX_US_ASCII) {
4666 encidx = rb_enc_get_index(path1);
4667 if (encidx == ENCINDEX_US_ASCII)
4668 encidx = rb_enc_get_index(path2);
4669 enc = rb_enc_from_index(encidx);
4676ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4678 while (path < end && *path ==
'.') path++;
4679 while (path < end && !isADS(*path)) {
4680 if (istrailinggarbage(*path)) {
4681 const char *last = path++;
4682 while (path < end && istrailinggarbage(*path)) path++;
4683 if (path >= end || isADS(*path))
return (
char *)last;
4685 else if (isdirsep(*path)) {
4686 const char *last = path++;
4687 while (path < end && isdirsep(*path)) path++;
4688 if (path >= end)
return (
char *)last;
4689 if (isADS(*path)) path++;
4692 Inc(path, end, mb_enc, enc);
4695 return (
char *)path;
4699#define BUFCHECK(cond) do {\
4702 do {buflen *= 2;} while (cond);\
4703 rb_str_resize(result, buflen);\
4704 buf = RSTRING_PTR(result);\
4706 pend = buf + buflen;\
4710#define BUFINIT(result, buf, p, pend) do {\
4711 if (!result) { result = rb_usascii_str_new(0, 1); } \
4712 p = buf = RSTRING_PTR(result); \
4713 buflen = RSTRING_LEN(result); \
4714 pend = p + buflen; \
4718# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4720# define SKIPPATHSEP(p) 1
4723#define BUFCOPY(srcptr, srclen) do { \
4724 const int skip = SKIPPATHSEP(p); \
4725 rb_str_set_len(result, p-buf+skip); \
4726 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4728 memcpy(p, (srcptr), (srclen)); \
4732#define WITH_ROOTDIFF(stmt) do { \
4733 long rootdiff = root - buf; \
4735 root = buf + rootdiff; \
4739copy_home_path(
VALUE result,
const char *dir)
4745 dirlen = strlen(dir);
4746 rb_str_resize(result, dirlen);
4747 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4748 encidx = rb_filesystem_encindex();
4749 rb_enc_associate_index(result, encidx);
4750#if defined FILE_ALT_SEPARATOR
4752 bool mb_enc = enc_mbclen_needed(enc);
4753 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4754 if (*p == FILE_ALT_SEPARATOR) {
4766 VALUE dirname = rb_getpwdirnam_for_login(user);
4767 if (dirname ==
Qnil) {
4768 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4770 const char *dir = RSTRING_PTR(dirname);
4772 extern char *getlogin(
void);
4773 const char *pwPtr = 0;
4775 # define endpwent() ((void)0)
4776 const char *dir, *username = RSTRING_PTR(user);
4787 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4788 dir = pwPtr = getenv(
"HOME");
4790 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4793 copy_home_path(result, dir);
4799rb_default_home_dir(
VALUE result)
4801 const char *dir = getenv(
"HOME");
4803#if defined HAVE_PWD_H
4817 VALUE login_name = rb_getlogin();
4819# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4824 if (
NIL_P(login_name)) {
4825 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4829 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4830 if (
NIL_P(pw_dir)) {
4831 pw_dir = rb_getpwdiruid();
4832 if (
NIL_P(pw_dir)) {
4833 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4838 copy_home_path(result, RSTRING_PTR(pw_dir));
4839 rb_str_resize(pw_dir, 0);
4844 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4846 return copy_home_path(result, dir);
4852#if NORMALIZE_UTF8PATH
4853 VALUE path = rb_str_normalize_ospath(ptr,
len);
4854 rb_enc_associate(path, fsenc);
4857 return rb_enc_str_new(ptr,
len, fsenc);
4864 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4866 rb_enc_associate(dirname, fsenc);
4870 size_t dirlen = RSTRING_LEN(dirname);
4873 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4874 if (!rb_enc_compatible(fname, dirname)) {
4877 rb_enc_check(fname, dirname);
4878 rb_bug(
"unreachable");
4880 rb_encoding *direnc = fs_enc_check(fname, dirname);
4881 if (direnc != fsenc) {
4888 do {buflen *= 2;}
while (dirlen > buflen);
4889 rb_str_resize(result, buflen);
4890 buf = RSTRING_PTR(result);
4891 memcpy(buf, cwdp, dirlen);
4892 rb_enc_associate(result, *enc);
4893 return buf + dirlen;
4897rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4899 const char *s, *b, *fend;
4900 char *buf, *p, *pend, *root;
4901 size_t buflen, bdiff;
4902 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4905 fend = s + RSTRING_LEN(fname);
4906 enc = rb_str_enc_get(fname);
4907 bool mb_enc = enc_mbclen_needed(enc);
4908 if (!mb_enc &&
RTEST(dname)) {
4909 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4912 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4913 BUFINIT(result, buf, p, pend);
4916 if (s + 1 == fend || isdirsep(s[1])) {
4920 if (++s < fend) ++s;
4921 rb_default_home_dir(result);
4924 s = nextdirsep(b = s, fend, enc);
4927 BUFCHECK(bdiff + userlen >= buflen);
4928 memcpy(p, b, userlen);
4931 rb_enc_associate(result, enc);
4932 rb_home_dir_of(result, result);
4936 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4938 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4939 (
int)userlen, b, fname);
4942 rb_raise(rb_eArgError,
"non-absolute home");
4945 BUFINIT(result, buf, p, pend);
4948#ifdef DOSISH_DRIVE_LETTER
4950 else if (s + 1 < fend && has_drive_letter(s)) {
4951 BUFINIT(result, buf, p, pend);
4953 if (s + 2 < fend && isdirsep(s[2])) {
4956 BUFCHECK(bdiff + 2 >= buflen);
4960 rb_enc_copy(result, fname);
4965 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4966 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4967 BUFINIT(result, buf, p, pend);
4974 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4975 BUFINIT(result, buf, p, pend);
4979 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4982 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4987 else if (s == fend || !rb_is_absolute_path(s)) {
4989 if (!
NIL_P(dname)) {
4991 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4995 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4997 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
5002 result = resized_result;
5006 rb_enc_associate(result, fs_enc_check(result, fname));
5007 BUFINIT(result, buf, p, pend);
5011 VALUE cwd = rb_dir_getwd_ospath();
5015 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
5016 BUFINIT(result, buf, p, pend);
5019#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5020 if (s < fend && isdirsep(*s)) {
5023 p = skipprefix(buf, p, mb_enc, enc);
5027 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
5030 BUFINIT(result, buf, p, pend);
5034 do s++;
while (s < fend && isdirsep(*s));
5037 BUFCHECK(bdiff >= buflen);
5038 memset(buf,
'/',
len);
5040 rb_enc_associate(result, fs_enc_check(result, fname));
5042 if (p > buf && p[-1] ==
'/')
5046 BUFCHECK(bdiff + 1 >= buflen);
5051 BUFCHECK(bdiff + 1 >= buflen);
5053 root = skipprefix(buf, p+1, mb_enc, enc);
5066 if (s+1 == fend || isdirsep(*(s+1))) {
5070 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
5080#if defined FILE_ALT_SEPARATOR
5081 case FILE_ALT_SEPARATOR:
5092#if defined FILE_ALT_SEPARATOR
5093 case FILE_ALT_SEPARATOR:
5096 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5104 int n = ignored_char_p(s, fend, enc);
5107 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5115 Inc(s, fend, mb_enc, enc);
5124 if (p == skiproot(buf, p + !!*p) - 1) p++;
5127 rb_enc_check(fname, result);
5134str_shrink(
VALUE str)
5136 rb_str_resize(str, RSTRING_LEN(str));
5140#define expand_path(fname, dname, abs_mode, long_name, result) \
5141 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5143#define check_expand_path_args(fname, dname) \
5144 (((fname) = rb_get_path(fname)), \
5145 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5148file_expand_path_1(
VALUE fname,
long extra_capa)
5151 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5157 check_expand_path_args(fname, dname);
5158 return expand_path(fname, dname, 0, 1,
Qfalse);
5162rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5164 return expand_path(fname, dname, 0, 0,
Qfalse);
5168rb_file_s_expand_path(
int argc,
const VALUE *argv)
5171 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5217 return rb_file_s_expand_path(c, v);
5223 check_expand_path_args(fname, dname);
5224 return expand_path(fname, dname, 1, 1,
Qfalse);
5228rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5231 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5274 return rb_file_s_absolute_path(c, v);
5302 VALUE path = rb_get_path(fname);
5304 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5308enum rb_realpath_mode {
5312 RB_REALPATH_MODE_MAX
5316realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5317 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5319 const char *pend = unresolved + strlen(unresolved);
5323 while (unresolved < pend) {
5324 const char *testname = unresolved;
5325 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5326 long testnamelen = unresolved_firstsep - unresolved;
5327 const char *unresolved_nextname = unresolved_firstsep;
5328 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5329 unresolved_nextname++;
5330 unresolved = unresolved_nextname;
5331 if (testnamelen == 1 && testname[0] ==
'.') {
5333 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5334 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5335 bool mb_enc = enc_mbclen_needed(enc);
5336 const char *resolved_str = RSTRING_PTR(*resolvedp);
5337 const char *resolved_names = resolved_str + *prefixlenp;
5338 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5339 long len = lastsep ? lastsep - resolved_names : 0;
5340 rb_str_resize(*resolvedp, *prefixlenp +
len);
5346 if (*prefixlenp < RSTRING_LEN(testpath))
5348#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5349 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5350 const char *prefix = RSTRING_PTR(testpath);
5351 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5356 checkval = rb_hash_aref(loopcheck, testpath);
5357 if (!
NIL_P(checkval)) {
5358 if (checkval ==
ID2SYM(resolving)) {
5359 if (mode == RB_REALPATH_CHECK) {
5363 rb_syserr_fail_path(ELOOP, testpath);
5372 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5375 if (e == ENOENT && !
NIL_P(fallback)) {
5376 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5381 if (mode == RB_REALPATH_CHECK)
return -1;
5383 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5384 rb_syserr_fail_path(e, testpath);
5385 *resolvedp = testpath;
5389 rb_syserr_fail_path(e, testpath);
5393 if (S_ISLNK(sbuf.st_mode)) {
5396 const char *link_prefix, *link_names;
5397 long link_prefixlen;
5398 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5399 link = rb_readlink(testpath, enc);
5400 link_prefix = RSTRING_PTR(link);
5401 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5402 link_prefixlen = link_names - link_prefix;
5403 if (link_prefixlen > 0) {
5407 tmpenc = fs_enc_check(*resolvedp, link);
5410 *prefixlenp = link_prefixlen;
5412 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5413 loopcheck, mode, !*unresolved_firstsep))
5422 rb_hash_aset(loopcheck, s, s);
5423 *resolvedp = testpath;
5431#ifdef DOSISH_DRIVE_LETTER
5435ospath_for_expand(
VALUE path)
5438 case ENCINDEX_ASCII_8BIT:
5439 case ENCINDEX_US_ASCII:
5440 return rb_enc_associate_index(
rb_str_dup(path), rb_filesystem_encindex());
5442 return TO_OSPATH(path);
5447rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5451 VALUE unresolved_path;
5456 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5457 char *ptr, *prefixptr = NULL, *pend;
5460#ifdef DOSISH_DRIVE_LETTER
5461 VALUE rootdir = path;
5463 if (!
NIL_P(basedir) && skipprefixroot(ptr, ptr +
len, rb_enc_get(path)) == ptr) {
5468 if (
len >= 2 && has_drive_letter(ptr) && (
len == 2 || !isdirsep(ptr[2]))) {
5471 if (!
NIL_P(basedir)) basedir = ospath_for_expand(rb_get_path(basedir));
5472 path = rb_file_absolute_path(ospath_for_expand(path), basedir);
5479 if (!
NIL_P(basedir)) {
5484 enc = rb_enc_get(unresolved_path);
5485 unresolved_path = TO_OSPATH(unresolved_path);
5487 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5488 if (ptr != path_names) {
5489 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5493 if (!
NIL_P(basedir)) {
5495 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5496 if (ptr != basedir_names) {
5502 curdir = rb_dir_getwd_ospath();
5504 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5509 pend = prefixptr + prefixlen;
5510 bool mb_enc = enc_mbclen_needed(enc);
5511 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5513 prefixlen = ++ptr - prefixptr;
5516#ifdef FILE_ALT_SEPARATOR
5517 while (prefixptr < ptr) {
5518 if (*prefixptr == FILE_ALT_SEPARATOR) {
5521 Inc(prefixptr, pend, mb_enc, enc);
5525 switch (rb_enc_to_index(enc)) {
5526 case ENCINDEX_ASCII_8BIT:
5527 case ENCINDEX_US_ASCII:
5528 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5531 loopcheck = rb_hash_new();
5533 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5536 if (basedir_names) {
5537 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5540 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5543 if (origenc && origenc != rb_enc_get(resolved)) {
5545 rb_enc_associate(resolved, origenc);
5557static VALUE rb_file_join(
long argc,
VALUE *args);
5559#ifndef HAVE_REALPATH
5561rb_check_realpath_emulate_try(
VALUE arg)
5564 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5568rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5572#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5573 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5575# include <sys/syslimits.h>
5576# define NEEDS_REALPATH_BUFFER 1
5580rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5583 VALUE unresolved_path;
5584 char *resolved_ptr = NULL;
5586# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5587 char resolved_buffer[PATH_MAX];
5589 char *
const resolved_buffer = NULL;
5592 if (mode == RB_REALPATH_DIR) {
5593 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5597 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5598 VALUE paths[2] = {basedir, unresolved_path};
5599 unresolved_path = rb_file_join(2, paths);
5601 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5603 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5612 if (
errno == ENOTSUP ||
5614 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5615 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5618 if (mode == RB_REALPATH_CHECK) {
5621 rb_sys_fail_path(unresolved_path);
5623 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5624# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5628# if !defined(__linux__) && !defined(__APPLE__)
5632 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5633 if (mode == RB_REALPATH_CHECK) {
5636 rb_sys_fail_path(unresolved_path);
5640 if (origenc && origenc != rb_enc_get(resolved)) {
5644 rb_enc_associate(resolved, origenc);
5647 if (is_broken_string(resolved)) {
5648 rb_enc_associate(resolved, rb_filesystem_encoding());
5649 if (is_broken_string(resolved)) {
5650 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5657 if (mode == RB_REALPATH_CHECK) {
5661 arg[2] = (
VALUE)origenc;
5664 rb_check_realpath_emulate_rescue,
Qnil);
5667 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5673rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5675 const enum rb_realpath_mode mode =
5676 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5677 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5683 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5700rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5703 VALUE path = argv[0];
5705 return rb_realpath_internal(basedir, path, 1);
5721rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5724 VALUE path = argv[0];
5726 return rb_realpath_internal(basedir, path, 0);
5730rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5734 const char *s, *last;
5736 if (!e || !l2)
return 0;
5738 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5739 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5740 if (c ==
'.')
return l0;
5745 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5750 if (l1 < l2)
return l1;
5753 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5754#if CASEFOLD_FILESYSTEM
5755#define fncomp strncasecmp
5757#define fncomp strncmp
5759 if (fncomp(s, e, l2) == 0) {
5765static inline const char *
5766enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5768 const char *p, *q, *e, *end;
5771 long len = (alllen ? (size_t)*alllen : strlen(name));
5778 name = skipprefix(name, end, mb_enc, enc);
5779#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5780 const char *root = name;
5783 while (name < end && isdirsep(*name)) {
5790#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5794#ifdef DOSISH_DRIVE_LETTER
5795 else if (*p ==
':') {
5808 p = strrdirsep(name, end, mb_enc, enc);
5813 while (isdirsep(*p)) {
5818 n = ntfs_tail(p, end, mb_enc, enc) - p;
5820 n = chompdirsep(p, end, mb_enc, enc) - p;
5822 for (q = p; q - p < n && *q ==
'.'; q++);
5823 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5824 if (*q ==
'.') e = q;
5844ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5846 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5890 const char *name, *p, *fp = 0;
5896 CheckPath(fname, name);
5900 check_path_encoding(fext);
5902 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5903 enc = rb_str_enc_get(fname);
5906 n = RSTRING_LEN(fname);
5907 if (n <= 0 || !*name) {
5908 return rb_enc_str_new(0, 0, enc);
5911 bool mb_enc = enc_mbclen_needed(enc);
5912 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5918 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5923 if (f == RSTRING_LEN(fname)) {
5928 return rb_enc_str_new(p, f, enc);
5931static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5954rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5960 return rb_file_dirname_n(argv[0], n);
5966 return rb_file_dirname_n(fname, 1);
5970rb_file_dirname_n(
VALUE fname,
int n)
5972 const char *name, *root, *p, *end;
5975 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5976 CheckPath(fname, name);
5977 end = name + RSTRING_LEN(fname);
5979 bool mb_enc = !rb_str_enc_fastpath(fname);
5982 root = skiproot(name, end);
5984 if (root > name + 1 && isdirsep(*name))
5985 root = skipprefix(name = root - 2, end, mb_enc, enc);
5987 if (root > name + 1)
5990 if (n > (end - root + 1) / 2) {
5996 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
6005 return rb_enc_str_new(
".", 1, enc);
6007#ifdef DOSISH_DRIVE_LETTER
6008 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
6009 const char *top = skiproot(name + 2, end);
6010 dirname = rb_enc_str_new(name, 3, enc);
6015 dirname = rb_enc_str_new(name, p - name, enc);
6016#ifdef DOSISH_DRIVE_LETTER
6017 if (root == name + 2 && p == root && name[1] ==
':')
6023static inline const char *
6024enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
6026 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
6028 p = strrdirsep(name, end, mb_enc, enc);
6032 do name = ++p;
while (isdirsep(*p));
6035 while (*p && *p ==
'.') p++;
6037 if (*p ==
'.' || istrailinggarbage(*p)) {
6039 const char *last = p++, *dot = last;
6040 while (istrailinggarbage(*p)) {
6041 if (*p ==
'.') dot = p;
6044 if (!*p || isADS(*p)) {
6048 if (*last ==
'.' || dot > last) e = dot;
6055 else if (isADS(*p)) {
6059 else if (isdirsep(*p))
6061 Inc(p, end, mb_enc, enc);
6066 if (!e || e == name)
6091 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
6138 CheckPath(fname, name);
6139 long len = RSTRING_LEN(fname);
6142 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
6145 bool mb_enc = !rb_str_enc_fastpath(fname);
6148 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
6149 return rb_enc_str_new(ext,
len, enc);
6173 return rb_get_path(fname);
6205file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6207 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6208 return rb_file_join_ary(arg);
6212rb_file_join_ary(
VALUE ary)
6216 const char *name, *tail;
6226 check_path_encoding(tmp);
6227 len += RSTRING_LEN(tmp);
6235 RBASIC_CLEAR_CLASS(result);
6238 switch (OBJ_BUILTIN_TYPE(tmp)) {
6240 if (!checked) check_path_encoding(tmp);
6245 rb_raise(rb_eArgError,
"recursive array");
6257 rb_enc_copy(result, tmp);
6260 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6261 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6264 else if (tail == name +
len) {
6268 enc = fs_enc_check(result, tmp);
6270 rb_enc_associate(result, enc);
6278rb_file_join_fastpath(
long argc,
VALUE *args)
6283 for (i = 0; i < argc; i++) {
6284 VALUE tmp = args[i];
6286 size += RSTRING_LEN(tmp);
6299 const char *name = RSTRING_PTR(result);
6300 for (i = 1; i < argc; i++) {
6301 VALUE tmp = args[i];
6302 long len = RSTRING_LEN(result);
6308 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6311 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6316 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6323 rb_enc_associate(result, new_enc);
6324 encidx = rb_enc_to_index(new_enc);
6330 rb_str_null_check(result);
6335rb_file_join(
long argc,
VALUE *args)
6337 if (RB_UNLIKELY(argc == 0)) {
6341 VALUE result = rb_file_join_fastpath(argc, args);
6342 if (RB_LIKELY(result)) {
6362rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6364 return rb_file_join(argc, argv);
6367#if defined(HAVE_TRUNCATE)
6368struct truncate_arg {
6374nogvl_truncate(
void *ptr)
6376 struct truncate_arg *ta = ptr;
6377 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6409 struct truncate_arg ta;
6414 path = rb_str_encode_ospath(path);
6417 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6419 rb_sys_fail_path(path);
6423#define rb_file_s_truncate rb_f_notimplement
6426#if defined(HAVE_FTRUNCATE)
6427struct ftruncate_arg {
6433nogvl_ftruncate(
void *ptr)
6435 struct ftruncate_arg *fa = ptr;
6437 return (
VALUE)ftruncate(fa->fd, fa->pos);
6478 struct ftruncate_arg fa;
6485 rb_io_flush_raw(obj, 0);
6487 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6488 rb_sys_fail_path(fptr->
pathv);
6493#define rb_file_truncate rb_f_notimplement
6510#include <winerror.h>
6514rb_thread_flock(
void *data)
6517 int old_errno =
errno;
6519 int *op = data, ret = flock(op[0], op[1]);
6522 if (GetLastError() == ERROR_NOT_LOCKED) {
6580 op[1] = op1 =
NUM2INT(operation);
6585 rb_io_flush_raw(obj, 0);
6587 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6592#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6595 if (op1 & LOCK_NB)
return Qfalse;
6598 time.tv_usec = 100 * 1000;
6604#if defined(ERESTART)
6610 rb_syserr_fail_path(e, fptr->
pathv);
6617test_check(
int n,
int argc,
VALUE *argv)
6623 for (i=1; i<n; i++) {
6630#define CHECK(n) test_check((n), argc, argv)
6722 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6726 return rb_file_blockdev_p(0, argv[1]);
6729 return rb_file_chardev_p(0, argv[1]);
6732 return rb_file_directory_p(0, argv[1]);
6735 return rb_file_exist_p(0, argv[1]);
6738 return rb_file_file_p(0, argv[1]);
6741 return rb_file_sgid_p(0, argv[1]);
6744 return rb_file_grpowned_p(0, argv[1]);
6747 return rb_file_sticky_p(0, argv[1]);
6750 return rb_file_symlink_p(0, argv[1]);
6753 return rb_file_owned_p(0, argv[1]);
6756 return rb_file_rowned_p(0, argv[1]);
6759 return rb_file_pipe_p(0, argv[1]);
6762 return rb_file_readable_p(0, argv[1]);
6765 return rb_file_readable_real_p(0, argv[1]);
6768 return rb_file_size_p(0, argv[1]);
6771 return rb_file_socket_p(0, argv[1]);
6774 return rb_file_suid_p(0, argv[1]);
6777 return rb_file_writable_p(0, argv[1]);
6780 return rb_file_writable_real_p(0, argv[1]);
6783 return rb_file_executable_p(0, argv[1]);
6786 return rb_file_executable_real_p(0, argv[1]);
6789 return rb_file_zero_p(0, argv[1]);
6793 if (strchr(
"MAC", cmd)) {
6795 VALUE fname = argv[1];
6798 if (
rb_stat(fname, &st) == -1) {
6801 rb_syserr_fail_path(e, fname);
6806 return stat_atime(&st);
6808 return stat_mtime(&st);
6810 return stat_ctime(&st);
6816 return rb_file_identical_p(0, argv[1], argv[2]);
6819 if (strchr(
"=<>", cmd)) {
6820 struct stat st1, st2;
6827 t1 = stat_mtimespec(&st1);
6828 t2 = stat_mtimespec(&st2);
6832 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6836 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6837 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6841 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6842 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6849 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6852 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6901rb_stat_s_alloc(
VALUE klass)
6930 fname = rb_str_encode_ospath(fname);
6932 rb_sys_fail_path(fname);
6939 rb_st->initialized =
true;
6956 *copy_rb_st = *orig_rb_st;
7002rb_stat_ftype(
VALUE obj)
7004 return rb_file_ftype(get_stat(obj)->ST_(mode));
7026 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
7053 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
7090 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
7122 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
7152 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
7180 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7215rb_stat_owned(
VALUE obj)
7217 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7222rb_stat_rowned(
VALUE obj)
7224 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7251rb_stat_grpowned(
VALUE obj)
7254 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7283 rb_io_stat_data *st = get_stat(obj);
7286 if (geteuid() == 0)
return Qtrue;
7289 if (rb_stat_owned(obj))
7290 return RBOOL(st->ST_(mode) & S_IRUSR);
7293 if (rb_stat_grpowned(obj))
7294 return RBOOL(st->ST_(mode) & S_IRGRP);
7297 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7315 rb_io_stat_data *st = get_stat(obj);
7318 if (getuid() == 0)
return Qtrue;
7321 if (rb_stat_rowned(obj))
7322 return RBOOL(st->ST_(mode) & S_IRUSR);
7325 if (rb_group_member(get_stat(obj)->ST_(gid)))
7326 return RBOOL(st->ST_(mode) & S_IRGRP);
7329 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7357rb_stat_wr(
VALUE obj)
7360 rb_io_stat_data *st = get_stat(obj);
7361 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7362 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7394 rb_io_stat_data *st = get_stat(obj);
7397 if (geteuid() == 0)
return Qtrue;
7400 if (rb_stat_owned(obj))
7401 return RBOOL(st->ST_(mode) & S_IWUSR);
7404 if (rb_stat_grpowned(obj))
7405 return RBOOL(st->ST_(mode) & S_IWGRP);
7408 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7429 rb_io_stat_data *st = get_stat(obj);
7432 if (getuid() == 0)
return Qtrue;
7435 if (rb_stat_rowned(obj))
7436 return RBOOL(st->ST_(mode) & S_IWUSR);
7439 if (rb_group_member(get_stat(obj)->ST_(gid)))
7440 return RBOOL(st->ST_(mode) & S_IWGRP);
7443 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7472rb_stat_ww(
VALUE obj)
7475 rb_io_stat_data *st = get_stat(obj);
7476 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7477 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7512 rb_io_stat_data *st = get_stat(obj);
7515 if (geteuid() == 0) {
7516 return RBOOL(st->ST_(mode) & S_IXUGO);
7520 if (rb_stat_owned(obj))
7521 return RBOOL(st->ST_(mode) & S_IXUSR);
7524 if (rb_stat_grpowned(obj))
7525 return RBOOL(st->ST_(mode) & S_IXGRP);
7528 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7544 rb_io_stat_data *st = get_stat(obj);
7547 if (getuid() == 0) {
7548 return RBOOL(st->ST_(mode) & S_IXUGO);
7552 if (rb_stat_rowned(obj))
7553 return RBOOL(st->ST_(mode) & S_IXUSR);
7556 if (rb_group_member(get_stat(obj)->ST_(gid)))
7557 return RBOOL(st->ST_(mode) & S_IXGRP);
7560 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7584 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7629 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7661 rb_off_t size = get_stat(obj)->ST_(size);
7663 if (size == 0)
return Qnil;
7696rb_stat_suid(
VALUE obj)
7699 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7733rb_stat_sgid(
VALUE obj)
7736 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7770rb_stat_sticky(
VALUE obj)
7773 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7778#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7779#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7790nogvl_mkfifo(
void *ptr)
7792 struct mkfifo_arg *ma = ptr;
7794 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7822 struct mkfifo_arg ma;
7831 path = rb_str_encode_ospath(path);
7832 ma.path = RSTRING_PTR(path);
7833 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7834 rb_sys_fail_path(path);
7839#define rb_file_s_mkfifo rb_f_notimplement
7842static VALUE rb_mFConst;
7845rb_file_const(
const char *name,
VALUE value)
7847 rb_define_const(rb_mFConst, name, value);
7851rb_is_absolute_path(
const char *path)
7853#ifdef DOSISH_DRIVE_LETTER
7854 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7857 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7860 if (path[0] ==
'/')
return 1;
7866ruby_is_fd_loadable(
int fd)
7873 if (fstat(fd, &st) < 0)
7876 if (S_ISREG(st.st_mode))
7879 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7882 if (S_ISDIR(st.st_mode))
7893rb_file_load_ok(
const char *path)
7900 int mode = (O_RDONLY |
7901#if defined O_NONBLOCK
7903#elif defined O_NDELAY
7909 if (!rb_gc_for_fd(
errno))
return 0;
7911 if (fd < 0)
return 0;
7914 ret = ruby_is_fd_loadable(fd);
7921is_explicit_relative(
const char *path)
7923 if (*path++ !=
'.')
return 0;
7924 if (*path ==
'.') path++;
7925 return isdirsep(*path);
7931 int encidx = rb_enc_get_index(orig);
7932 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7933 encidx = rb_filesystem_encindex();
7934 rb_enc_associate_index(path, encidx);
7942nav_component_p(
const char *s,
const char *send)
7944 if ((send - s) >= 2 && s[0] ==
'.') {
7945 return s[1] ==
'.' || isdirsep(s[1]);
7951fname_need_expansion_p(
VALUE fname)
7953 const char *s = RSTRING_PTR(fname);
7954 const long len = RSTRING_LEN(fname);
7955 const char *send = s +
len;
7957 if (nav_component_p(s, send)) {
7962 bool mbenc = enc_mbclen_needed(enc);
7964 s = enc_path_next(s, send, mbenc, enc);
7966 if (nav_component_p(s, send)) {
7970 s = enc_path_next(s, send, mbenc, enc);
7976expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7978 long dname_len = RSTRING_LEN(dname);
7979 const char *dname_ptr = RSTRING_PTR(dname);
7983 if (need_expansion || dname_ptr[0] ==
'~') {
7984 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7989 if (!isdirsep(dname_ptr[dname_len - 1])) {
7998rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
8001 VALUE fname = *filep;
8005 if (!ext[0])
return 0;
8008 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
8009 f = RSTRING_PTR(fname);
8014 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
8015 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
8016 fnlen = RSTRING_LEN(fname);
8017 for (i=0; ext[i]; i++) {
8019 if (rb_file_load_ok(RSTRING_PTR(fname))) {
8020 *filep = copy_path_class(fname, *filep);
8028 long expanded_load_path_maxlen;
8029 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8030 if (!load_path)
return 0;
8033 RBASIC_CLEAR_CLASS(fname);
8034 fnlen = RSTRING_LEN(fname);
8035 bool need_expansion = fname_need_expansion_p(fname);
8038 rb_enc_associate_index(tmp, rb_usascii_encindex());
8040 for (j=0; ext[j]; j++) {
8042 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
8044 if (!RSTRING_LEN(dname))
continue;
8045 expand_feature(fname, dname, tmp, need_expansion);
8047 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8048 *filep = copy_path_class(tmp, *filep);
8054 rb_str_resize(tmp, 0);
8067 path = copy_path_class(file_expand_path_1(path, 0), path);
8068 f = RSTRING_PTR(path);
8072 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
8073 if (!rb_file_load_ok(f))
return 0;
8075 path = copy_path_class(file_expand_path_1(path, 0), path);
8079 long expanded_load_path_maxlen;
8080 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8083 bool need_expansion = fname_need_expansion_p(path);
8085 rb_enc_associate_index(tmp, rb_usascii_encindex());
8086 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
8088 if (!RSTRING_LEN(dname))
continue;
8089 expand_feature(path, dname, tmp, need_expansion);
8091 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8092 return copy_path_class(tmp, path);
8095 rb_str_resize(tmp, 0);
8103#define define_filetest_function(name, func, argc) do { \
8104 rb_define_module_function(rb_mFileTest, name, func, argc); \
8105 rb_define_singleton_method(rb_cFile, name, func, argc); \
8108const char ruby_null_device[] =
8911#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8912 rb_CFString_class_initialize_before_fork();
8920 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8921 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8922 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8923 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8924 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8925 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8926 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8927 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8928 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8929 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8930 define_filetest_function(
"file?", rb_file_file_p, 1);
8931 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8932 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8933 define_filetest_function(
"size?", rb_file_size_p, 1);
8934 define_filetest_function(
"size", rb_file_s_size, 1);
8935 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8936 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8938 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8939 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8940 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8942 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8943 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8945 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8946 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8947 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8949 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8987 separator = rb_fstring_lit(
"/");
8989 rb_define_const(
rb_cFile,
"Separator", separator);
8991 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9340 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9343 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9345 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9347 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9349 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9351 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9353 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9354#if defined(O_NDELAY) || defined(O_NONBLOCK)
9356# define O_NONBLOCK O_NDELAY
9359 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9362 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9365 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9371 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9372#ifndef O_SHARE_DELETE
9373# define O_SHARE_DELETE 0
9376 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9379 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9383 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9387 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9391 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9395 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9399 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9403 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9407 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9409 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9411 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9413 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9416 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.