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 ""
686rb_stat_dev(
VALUE self)
689 unsigned int m = get_stat(self)->stx_dev_major;
690 unsigned int n = get_stat(self)->stx_dev_minor;
692#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
693 return DEVT2NUM(get_stat(self)->st_dev);
694#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
695 return ULONG2NUM(get_stat(self)->st_dev);
697 return ULL2NUM(get_stat(self)->st_dev);
712rb_stat_dev_major(
VALUE self)
715 return UINT2NUM(get_stat(self)->stx_dev_major);
717 return UINT2NUM(major(get_stat(self)->st_dev));
734rb_stat_dev_minor(
VALUE self)
737 return UINT2NUM(get_stat(self)->stx_dev_minor);
739 return UINT2NUM(minor(get_stat(self)->st_dev));
756rb_stat_ino(
VALUE self)
758 rb_io_stat_data *ptr = get_stat(self);
759#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
761 return rb_integer_unpack(&ptr->st_ino, 2,
762 SIZEOF_STRUCT_STAT_ST_INO, 0,
766 return UIANY2NUM(ptr->ST_(ino));
784rb_stat_mode(
VALUE self)
786 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
802rb_stat_nlink(
VALUE self)
805 const rb_io_stat_data *ptr = get_stat(self);
807 return UIANY2NUM(ptr->ST_(nlink));
821rb_stat_uid(
VALUE self)
823 return UIDT2NUM(get_stat(self)->ST_(uid));
837rb_stat_gid(
VALUE self)
839 return GIDT2NUM(get_stat(self)->ST_(gid));
855rb_stat_rdev(
VALUE self)
858 unsigned int m = get_stat(self)->stx_rdev_major;
859 unsigned int n = get_stat(self)->stx_rdev_minor;
861#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
863#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
864 return DEVT2NUM(get_stat(self)->ST_(rdev));
865#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
866 return ULONG2NUM(get_stat(self)->ST_(rdev));
868 return ULL2NUM(get_stat(self)->ST_(rdev));
883rb_stat_rdev_major(
VALUE self)
886 return UINT2NUM(get_stat(self)->stx_rdev_major);
887#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
888 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
905rb_stat_rdev_minor(
VALUE self)
908 return UINT2NUM(get_stat(self)->stx_rdev_minor);
909#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
910 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
946rb_stat_size(
VALUE self)
948 return OFFT2NUM(get_stat(self)->ST_(size));
963rb_stat_blksize(
VALUE self)
965#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
966 return ULONG2NUM(get_stat(self)->ST_(blksize));
984rb_stat_blocks(
VALUE self)
986#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
987# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
988 return ULL2NUM(get_stat(self)->ST_(blocks));
990 return ULONG2NUM(get_stat(self)->ST_(blocks));
998stat_atimespec(
const struct stat *st)
1001 ts.tv_sec = st->st_atime;
1002#if defined(HAVE_STRUCT_STAT_ST_ATIM)
1003 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
1004#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1005 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
1006#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
1007 ts.tv_nsec = (uint32_t)st->st_atimensec;
1016statx_atimespec(
const rb_io_stat_data *st)
1018 return st->stx_atime;
1021# define statx_atimespec stat_atimespec
1031stat_atime(
const struct stat *st)
1033 return stat_time(stat_atimespec(st));
1037stat_mtimespec(
const struct stat *st)
1040 ts.tv_sec = st->st_mtime;
1041#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1042 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1043#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1044 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1045#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1046 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1054stat_mtime(
const struct stat *st)
1056 return stat_time(stat_mtimespec(st));
1060stat_ctimespec(
const struct stat *st)
1063 ts.tv_sec = st->st_ctime;
1064#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1065 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1066#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1067 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1068#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1069 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1078statx_ctimespec(
const rb_io_stat_data *st)
1080 return st->stx_ctime;
1083# define statx_ctimespec stat_ctimespec
1087stat_ctime(
const struct stat *st)
1089 return stat_time(stat_ctimespec(st));
1092#define HAVE_STAT_BIRTHTIME
1093#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1095statx_birthtime(
const rb_io_stat_data *st)
1100#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1101static VALUE statx_birthtime(
const rb_io_stat_data *st);
1102#elif defined(_WIN32)
1103# define statx_birthtime stat_ctime
1105# undef HAVE_STAT_BIRTHTIME
1154rb_stat_atime(
VALUE self)
1156 return stat_time(statx_atimespec(get_stat(self)));
1187rb_stat_mtime(
VALUE self)
1189 return stat_time(statx_mtimespec(get_stat(self)));
1207rb_stat_ctime(
VALUE self)
1209 return stat_time(statx_ctimespec(get_stat(self)));
1212#if defined(HAVE_STAT_BIRTHTIME)
1234rb_stat_birthtime(
VALUE self)
1236 return statx_birthtime(get_stat(self));
1239# define rb_stat_birthtime rb_f_notimplement
1258rb_stat_inspect(
VALUE self)
1262 static const struct {
1266 {
"dev", rb_stat_dev},
1267 {
"ino", rb_stat_ino},
1268 {
"mode", rb_stat_mode},
1269 {
"nlink", rb_stat_nlink},
1270 {
"uid", rb_stat_uid},
1271 {
"gid", rb_stat_gid},
1272 {
"rdev", rb_stat_rdev},
1273 {
"size", rb_stat_size},
1274 {
"blksize", rb_stat_blksize},
1275 {
"blocks", rb_stat_blocks},
1276 {
"atime", rb_stat_atime},
1277 {
"mtime", rb_stat_mtime},
1278 {
"ctime", rb_stat_ctime},
1279#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1280 {
"birthtime", rb_stat_birthtime},
1286 if (!rb_st->initialized) {
1294 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1302 v = (*member[i].func)(self);
1304 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1306 else if (i == 0 || i == 6) {
1307 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1327no_gvl_fstat(
void *data)
1330 return (
VALUE)fstat(arg->file.fd, arg->st);
1334fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1338 data.file.fd = fptr->
fd;
1341 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1345no_gvl_stat(
void * data)
1348 return (
void *)(
VALUE)stat(arg->file.path, arg->st);
1352stat_without_gvl(
const char *path,
struct stat *st)
1356 data.file.path = path;
1359 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1362#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1363 defined(HAVE_STRUCT_STATX_STX_BTIME)
1365# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1368# ifdef HAVE_SYSCALL_H
1369# include <syscall.h>
1370# elif defined HAVE_SYS_SYSCALL_H
1371# include <sys/syscall.h>
1373# if defined __linux__
1374# include <linux/stat.h>
1376statx(
int dirfd,
const char *pathname,
int flags,
1377 unsigned int mask,
struct statx *statxbuf)
1379 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1384typedef struct no_gvl_rb_io_stat_data {
1390} no_gvl_rb_io_stat_data;
1393io_blocking_statx(
void *data)
1395 no_gvl_rb_io_stat_data *arg = data;
1396 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1400no_gvl_statx(
void *data)
1402 return (
void *)io_blocking_statx(data);
1406statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1408 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1411 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1415lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1417 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1420 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1424fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1426 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1429 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1432#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1435rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1440 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1445 result = fstatx_without_gvl(fptr, stx, mask);
1450 file = rb_str_encode_ospath(file);
1451 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1457# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1459NORETURN(
static void statx_notimplement(
const char *field_name));
1464statx_notimplement(
const char *field_name)
1467 "%s is unimplemented on this filesystem",
1472statx_birthtime(
const rb_io_stat_data *stx)
1474 if (!statx_has_birthtime(stx)) {
1476 statx_notimplement(
"birthtime");
1478 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1483# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1484# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1485# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1486# define rb_statx(file, stx, mask) rb_stat(file, stx)
1487# define STATX(path, st, mask) stat(path, st)
1489#if defined(HAVE_STAT_BIRTHTIME)
1490# define statx_has_birthtime(st) 1
1492# define statx_has_birthtime(st) 0
1499# define FSTAT(fd, st) fstat(fd, st)
1508 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1513 result = fstat_without_gvl(fptr, st);
1518 file = rb_str_encode_ospath(file);
1519 result = stat_without_gvl(RSTRING_PTR(file), st);
1557 fname = rb_str_encode_ospath(fname);
1558 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1559 rb_sys_fail_path(fname);
1561 return rb_statx_new(&st);
1593rb_io_stat(
VALUE obj)
1599 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1600 rb_sys_fail_path(fptr->
pathv);
1602 return rb_statx_new(&st);
1607no_gvl_lstat(
void *ptr)
1610 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1614lstat_without_gvl(
const char *path,
struct stat *st)
1618 data.file.path = path;
1621 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1658 fname = rb_str_encode_ospath(fname);
1659 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1660 rb_sys_fail_path(fname);
1662 return rb_statx_new(&st);
1664 return rb_file_s_stat(klass, fname);
1697rb_file_lstat(
VALUE obj)
1706 path = rb_str_encode_ospath(fptr->
pathv);
1707 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1708 rb_sys_fail_path(fptr->
pathv);
1710 return rb_statx_new(&st);
1712 return rb_io_stat(obj);
1717rb_group_member(GETGROUPS_T gid)
1719#if !defined(HAVE_GETGROUPS)
1728 if (getgid() == gid || getegid() == gid)
1731 groups = getgroups(0, NULL);
1732 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1733 anum = getgroups(groups, gary);
1734 while (--anum >= 0) {
1735 if (gary[anum] == gid) {
1748# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1751#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1752#define USE_GETEUID 1
1757eaccess(
const char *path,
int mode)
1766 if (getuid() == euid && getgid() == getegid())
1767 return access(path, mode);
1769 if (stat(path, &st) < 0)
1779 if (st.st_mode & S_IXUGO)
1785 if (st.st_uid == euid)
1787 else if (rb_group_member(st.st_gid))
1790 if ((
int)(st.st_mode & mode) == mode)
return 0;
1794 return access(path, mode);
1805nogvl_eaccess(
void *ptr)
1809 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1813rb_eaccess(
VALUE fname,
int mode)
1818 fname = rb_str_encode_ospath(fname);
1822 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1826nogvl_access(
void *ptr)
1830 return (
void *)(
VALUE)access(aa->path, aa->mode);
1834rb_access(
VALUE fname,
int mode)
1839 fname = rb_str_encode_ospath(fname);
1843 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1886# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1892 if (S_ISDIR(st.st_mode))
return Qtrue;
1921# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1927 if (S_ISFIFO(st.st_mode))
return Qtrue;
1959# define S_ISLNK(m) _S_ISLNK(m)
1962# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1965# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1975 fname = rb_str_encode_ospath(fname);
1977 if (S_ISLNK(st.st_mode))
return Qtrue;
2012# define S_ISSOCK(m) _S_ISSOCK(m)
2015# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
2018# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
2028 if (S_ISSOCK(st.st_mode))
return Qtrue;
2056# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
2058# define S_ISBLK(m) (0)
2066 if (S_ISBLK(st.st_mode))
return Qtrue;
2093# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2099 if (S_ISCHR(st.st_mode))
return Qtrue;
2169 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2183rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2185 return RBOOL(rb_access(fname, R_OK) >= 0);
2189# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2193# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2222rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2228 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2229 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2263 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2280rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2282 return RBOOL(rb_access(fname, W_OK) >= 0);
2311rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2317 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2318 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2354 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2373rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2375 return RBOOL(rb_access(fname, X_OK) >= 0);
2379# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2411 return RBOOL(S_ISREG(st.st_mode));
2471 return RBOOL(st.st_size == 0);
2509 if (st.st_size == 0)
return Qnil;
2543 return RBOOL(st.st_uid == geteuid());
2552 return RBOOL(st.st_uid == getuid());
2584 if (rb_group_member(st.st_gid))
return Qtrue;
2589#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2591check3rdbyte(
VALUE fname,
int mode)
2596 return RBOOL(st.st_mode & mode);
2629 return check3rdbyte(fname, S_ISUID);
2664 return check3rdbyte(fname, S_ISGID);
2700 return check3rdbyte(fname, S_ISVTX);
2737 struct stat st1, st2;
2741 if (st1.st_dev != st2.st_dev)
return Qfalse;
2742 if (st1.st_ino != st2.st_ino)
return Qfalse;
2746 return rb_w32_file_identical_p(fname1, fname2);
2772 if (
rb_stat(fname, &st) < 0) {
2775 rb_syserr_fail_path(e, fname);
2781rb_file_ftype(mode_t mode)
2785 if (S_ISREG(mode)) {
2788 else if (S_ISDIR(mode)) {
2791 else if (S_ISCHR(mode)) {
2792 t =
"characterSpecial";
2795 else if (S_ISBLK(mode)) {
2800 else if (S_ISFIFO(mode)) {
2805 else if (S_ISLNK(mode)) {
2810 else if (S_ISSOCK(mode)) {
2818 return rb_fstring_cstr(t);
2860 fname = rb_str_encode_ospath(fname);
2862 rb_sys_fail_path(fname);
2865 return rb_file_ftype(st.st_mode);
2921 if (
rb_stat(fname, &st) < 0) {
2924 rb_syserr_fail_path(e, fname);
2926 return stat_time(stat_atimespec(&st));
2958rb_file_atime(
VALUE obj)
2964 if (fstat(fptr->
fd, &st) == -1) {
2965 rb_sys_fail_path(fptr->
pathv);
2967 return stat_time(stat_atimespec(&st));
3007 if (
rb_stat(fname, &st) < 0) {
3010 rb_syserr_fail_path(e, fname);
3012 return stat_time(stat_mtimespec(&st));
3037rb_file_mtime(
VALUE obj)
3043 if (fstat(fptr->
fd, &st) == -1) {
3044 rb_sys_fail_path(fptr->
pathv);
3046 return stat_time(stat_mtimespec(&st));
3089 if (
rb_stat(fname, &st) < 0) {
3092 rb_syserr_fail_path(e, fname);
3094 return stat_time(stat_ctimespec(&st));
3111rb_file_ctime(
VALUE obj)
3117 if (fstat(fptr->
fd, &st) == -1) {
3118 rb_sys_fail_path(fptr->
pathv);
3120 return stat_time(stat_ctimespec(&st));
3123#if defined(HAVE_STAT_BIRTHTIME)
3154 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3157 rb_syserr_fail_path(e, fname);
3159 return statx_birthtime(&st);
3162# define rb_file_s_birthtime rb_f_notimplement
3165#if defined(HAVE_STAT_BIRTHTIME)
3186rb_file_birthtime(
VALUE obj)
3192 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3193 rb_sys_fail_path(fptr->
pathv);
3195 return statx_birthtime(&st);
3198# define rb_file_birthtime rb_f_notimplement
3210 rb_io_flush_raw(file, 0);
3213 if (fstat(fptr->
fd, &st) == -1) {
3214 rb_sys_fail_path(fptr->
pathv);
3240file_size(
VALUE self)
3242 return OFFT2NUM(rb_file_size(self));
3251nogvl_chmod(
void *ptr)
3254 int ret = chmod(data->path, data->mode);
3255 return (
void *)(
VALUE)ret;
3259rb_chmod(
const char *path, mode_t mode)
3265 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3269chmod_internal(
const char *path,
void *mode)
3271 return chmod(path, *(mode_t *)mode);
3304 return apply2files(chmod_internal, argc, argv, &mode);
3308struct nogvl_fchmod_data {
3314io_blocking_fchmod(
void *ptr)
3316 struct nogvl_fchmod_data *data = ptr;
3317 int ret = fchmod(data->fd, data->mode);
3322rb_fchmod(
struct rb_io* io, mode_t mode)
3325 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3326 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3355#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3363 if (rb_fchmod(fptr, mode) == -1) {
3364 if (HAVE_FCHMOD ||
errno != ENOSYS)
3365 rb_sys_fail_path(fptr->
pathv);
3368 if (!HAVE_FCHMOD)
return INT2FIX(0);
3371#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3373 path = rb_str_encode_ospath(fptr->
pathv);
3374 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3375 rb_sys_fail_path(fptr->
pathv);
3381#if defined(HAVE_LCHMOD)
3383lchmod_internal(
const char *path,
void *mode)
3385 return lchmod(path, *(mode_t *)mode);
3422 return apply2files(lchmod_internal, argc, argv, &mode);
3425#define rb_file_s_lchmod rb_f_notimplement
3428static inline rb_uid_t
3432 return (rb_uid_t)-1;
3437static inline rb_gid_t
3441 return (rb_gid_t)-1;
3452chown_internal(
const char *path,
void *arg)
3455 return chown(path, args->owner, args->group);
3509 arg.owner = to_uid(*argv++);
3510 arg.group = to_gid(*argv++);
3512 return apply2files(chown_internal, argc, argv, &arg);
3524nogvl_chown(
void *ptr)
3527 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3531rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3534 .as = {.path = path},
3535 .new = {.owner = owner, .group = group},
3537 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3542nogvl_fchown(
void *ptr)
3545 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3549rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3554 .new = {.owner = owner, .group = group},
3556 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3590 path = rb_str_encode_ospath(fptr->
pathv);
3591 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3592 rb_sys_fail_path(fptr->
pathv);
3594 if (rb_fchown(fptr->
fd, o, g) == -1)
3595 rb_sys_fail_path(fptr->
pathv);
3601#if defined(HAVE_LCHOWN)
3603lchown_internal(
const char *path,
void *arg)
3606 return lchown(path, args->owner, args->group);
3663 arg.owner = to_uid(*argv++);
3664 arg.group = to_gid(*argv++);
3666 return apply2files(lchown_internal, argc, argv, &arg);
3669#define rb_file_s_lchown rb_f_notimplement
3679NORETURN(
static void utime_failed(
struct apply_arg *));
3685 VALUE path = aa->fn[aa->i].path;
3688 if (ua->tsp && e == EINVAL) {
3691 VALUE atime = ua->atime;
3692 VALUE mtime = ua->mtime;
3694 if (!
NIL_P(atime)) {
3697 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3700 if (
NIL_P(a)) e[0] = m;
3716 rb_syserr_fail_path(e, path);
3720#if defined(HAVE_UTIMES)
3722# if !defined(HAVE_UTIMENSAT)
3724# elif defined(__APPLE__) && \
3725 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3727# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3728typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3732static inline utimensat_func *
3739# define utimensat rb_utimensat()
3746utime_internal(
const char *path,
void *arg)
3749 const struct timespec *tsp = v->tsp;
3750 struct timeval tvbuf[2], *tvp = NULL;
3752#if defined(HAVE_UTIMENSAT)
3753# if defined(__APPLE__)
3754 const int try_utimensat = utimensat != NULL;
3755 const int try_utimensat_follow = utimensat != NULL;
3757# define TRY_UTIMENSAT 1
3758 static int try_utimensat = 1;
3759# ifdef AT_SYMLINK_NOFOLLOW
3760 static int try_utimensat_follow = 1;
3762 const int try_utimensat_follow = 0;
3767 if (v->follow ? try_utimensat_follow : try_utimensat) {
3768# ifdef AT_SYMLINK_NOFOLLOW
3770 flags = AT_SYMLINK_NOFOLLOW;
3774 int result = utimensat(AT_FDCWD, path, tsp, flags);
3775# ifdef TRY_UTIMENSAT
3776 if (result < 0 &&
errno == ENOSYS) {
3777# ifdef AT_SYMLINK_NOFOLLOW
3778 try_utimensat_follow = 0;
3790 tvbuf[0].tv_sec = tsp[0].tv_sec;
3791 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3792 tvbuf[1].tv_sec = tsp[1].tv_sec;
3793 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3797 if (v->follow)
return lutimes(path, tvp);
3799 return utimes(path, tvp);
3804#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3812utime_internal(
const char *path,
void *arg)
3816 struct utimbuf utbuf, *utp = NULL;
3818 utbuf.actime = tsp[0].tv_sec;
3819 utbuf.modtime = tsp[1].tv_sec;
3822 return utime(path, utp);
3827utime_internal_i(
int argc,
VALUE *argv,
int follow)
3830 struct timespec tss[2], *tsp = NULL;
3833 args.atime = *argv++;
3834 args.mtime = *argv++;
3836 args.follow = follow;
3838 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3841 if (args.atime == args.mtime)
3848 return apply2files(utime_internal, argc, argv, &args);
3894 return utime_internal_i(argc, argv, FALSE);
3897#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3955 return utime_internal_i(argc, argv, TRUE);
3958#define rb_file_s_lutime rb_f_notimplement
3961#ifdef RUBY_FUNCTION_NAME_STRING
3962# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3964# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3966#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3967NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3969syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3973 const int max_pathlen = MAX_PATH;
3975 const int max_pathlen = MAXPATHLEN;
3986#ifdef RUBY_FUNCTION_NAME_STRING
3987 rb_syserr_fail_path_in(func, e, str);
3989 rb_syserr_fail_path(e, str);
4024 from = rb_str_encode_ospath(from);
4025 to = rb_str_encode_ospath(to);
4028 sys_fail2(from, to);
4033#define rb_file_s_link rb_f_notimplement
4074 from = rb_str_encode_ospath(from);
4075 to = rb_str_encode_ospath(to);
4078 sys_fail2(from, to);
4083#define rb_file_s_symlink rb_f_notimplement
4111 return rb_readlink(path, rb_filesystem_encoding());
4114struct readlink_arg {
4121nogvl_readlink(
void *ptr)
4123 struct readlink_arg *ra = ptr;
4125 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4129readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4131 struct readlink_arg ra;
4133 ra.path = RSTRING_PTR(path);
4134 ra.buf = RSTRING_PTR(buf);
4137 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4148 path = rb_str_encode_ospath(path);
4149 v = rb_enc_str_new(0, size, enc);
4150 while ((rv = readlink_without_gvl(path, v, size)) == size
4152 || (rv < 0 &&
errno == ERANGE)
4161 rb_str_resize(v, 0);
4162 rb_syserr_fail_path(e, path);
4164 rb_str_resize(v, rv);
4169#define rb_file_s_readlink rb_f_notimplement
4173unlink_internal(
const char *path,
void *arg)
4175 return unlink(path);
4218rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4220 return apply2files(unlink_internal, argc, argv, 0);
4229no_gvl_rename(
void *ptr)
4233 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4311 f = rb_str_encode_ospath(from);
4312 t = rb_str_encode_ospath(to);
4315#if defined __CYGWIN__
4318 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4323 if (chmod(ra.dst, 0666) == 0 &&
4324 unlink(ra.dst) == 0 &&
4325 rename(ra.src, ra.dst) == 0)
4329 syserr_fail2(e, from, to);
4363 rb_error_arity(argc, 0, 1);
4371#if defined __CYGWIN__ || defined DOSISH
4373#define DOSISH_DRIVE_LETTER
4374#define FILE_ALT_SEPARATOR '\\'
4376#ifdef FILE_ALT_SEPARATOR
4377#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4379static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4382#define isdirsep(x) ((x) == '/')
4394#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4395#define isADS(x) ((x) == ':')
4397#define istrailinggarbage(x) 0
4400#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4402#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4403#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4405#if defined(DOSISH_UNC)
4406#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4408#define has_unc(buf) 0
4411#ifdef DOSISH_DRIVE_LETTER
4413has_drive_letter(
const char *buf)
4415 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4440 if (chdir(drive) == 0) {
4441 drvcwd = rb_dir_getwd_ospath();
4453not_same_drive(
VALUE path,
int drive)
4455 const char *p = RSTRING_PTR(path);
4456 if (RSTRING_LEN(path) < 2)
return 0;
4457 if (has_drive_letter(p)) {
4468skiproot(
const char *path,
const char *end)
4470#ifdef DOSISH_DRIVE_LETTER
4471 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4473 while (path < end && isdirsep(*path)) path++;
4474 return (
char *)path;
4478enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4480 while (s < e && !isdirsep(*s)) {
4481 Inc(s, e, mb_enc, enc);
4486#define nextdirsep rb_enc_path_next
4490 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4493#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4494#define skipprefix enc_path_skip_prefix
4496#define skipprefix(path, end, mb_enc, enc) (path)
4499enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4501#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4503 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4505 while (path < end && isdirsep(*path)) path++;
4506 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4507 path + 2 <= end && !isdirsep(path[1])) {
4508 path = enc_path_next(path + 1, end, mb_enc, enc);
4510 return (
char *)path;
4513#ifdef DOSISH_DRIVE_LETTER
4514 if (path + 2 <= end && has_drive_letter(path))
4515 return (
char *)(path + 2);
4518 return (
char *)path;
4522rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4524 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4528skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4530#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4531 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4532 while (p < end && isdirsep(*p)) p++;
4535 return skiproot(path, end);
4540rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4542 return skipprefixroot(path, end, enc);
4546enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4549 while (path < end) {
4550 if (isdirsep(*path)) {
4551 const char *tmp = path++;
4552 while (path < end && isdirsep(*path)) path++;
4553 if (path >= end)
break;
4557 Inc(path, end, mb_enc, enc);
4563rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4565 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4569strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4571 if (RB_UNLIKELY(mb_enc)) {
4572 return enc_path_last_separator(path, end, mb_enc, enc);
4575 const char *cursor = end - 1;
4577 while (cursor >= path && isdirsep(cursor[0])) {
4581 while (cursor >= path) {
4582 if (isdirsep(cursor[0])) {
4583 while (cursor > path && isdirsep(cursor[-1])) {
4586 return (
char *)cursor;
4594chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4596 while (path < end) {
4597 if (isdirsep(*path)) {
4598 const char *last = path++;
4599 while (path < end && isdirsep(*path)) path++;
4600 if (path >= end)
return (
char *)last;
4603 Inc(path, end, mb_enc, enc);
4606 return (
char *)path;
4612 if (path < end && isdirsep(*path)) path++;
4613 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4619 rb_encoding *enc = rb_enc_check_str(path1, path2);
4620 int encidx = rb_enc_to_index(enc);
4621 if (encidx == ENCINDEX_US_ASCII) {
4622 encidx = rb_enc_get_index(path1);
4623 if (encidx == ENCINDEX_US_ASCII)
4624 encidx = rb_enc_get_index(path2);
4625 enc = rb_enc_from_index(encidx);
4632ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4634 while (path < end && *path ==
'.') path++;
4635 while (path < end && !isADS(*path)) {
4636 if (istrailinggarbage(*path)) {
4637 const char *last = path++;
4638 while (path < end && istrailinggarbage(*path)) path++;
4639 if (path >= end || isADS(*path))
return (
char *)last;
4641 else if (isdirsep(*path)) {
4642 const char *last = path++;
4643 while (path < end && isdirsep(*path)) path++;
4644 if (path >= end)
return (
char *)last;
4645 if (isADS(*path)) path++;
4648 Inc(path, end, mb_enc, enc);
4651 return (
char *)path;
4655#define BUFCHECK(cond) do {\
4658 do {buflen *= 2;} while (cond);\
4659 rb_str_resize(result, buflen);\
4660 buf = RSTRING_PTR(result);\
4662 pend = buf + buflen;\
4666#define BUFINIT(result, buf, p, pend) do {\
4667 if (!result) { result = rb_usascii_str_new(0, 1); } \
4668 p = buf = RSTRING_PTR(result); \
4669 buflen = RSTRING_LEN(result); \
4670 pend = p + buflen; \
4674# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4676# define SKIPPATHSEP(p) 1
4679#define BUFCOPY(srcptr, srclen) do { \
4680 const int skip = SKIPPATHSEP(p); \
4681 rb_str_set_len(result, p-buf+skip); \
4682 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4684 memcpy(p, (srcptr), (srclen)); \
4688#define WITH_ROOTDIFF(stmt) do { \
4689 long rootdiff = root - buf; \
4691 root = buf + rootdiff; \
4695copy_home_path(
VALUE result,
const char *dir)
4701 dirlen = strlen(dir);
4702 rb_str_resize(result, dirlen);
4703 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4704 encidx = rb_filesystem_encindex();
4705 rb_enc_associate_index(result, encidx);
4706#if defined FILE_ALT_SEPARATOR
4708 bool mb_enc = enc_mbclen_needed(enc);
4709 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4710 if (*p == FILE_ALT_SEPARATOR) {
4722 VALUE dirname = rb_getpwdirnam_for_login(user);
4723 if (dirname ==
Qnil) {
4724 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4726 const char *dir = RSTRING_PTR(dirname);
4728 extern char *getlogin(
void);
4729 const char *pwPtr = 0;
4731 # define endpwent() ((void)0)
4732 const char *dir, *username = RSTRING_PTR(user);
4743 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4744 dir = pwPtr = getenv(
"HOME");
4746 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4749 copy_home_path(result, dir);
4755rb_default_home_dir(
VALUE result)
4757 const char *dir = getenv(
"HOME");
4759#if defined HAVE_PWD_H
4773 VALUE login_name = rb_getlogin();
4775# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4780 if (
NIL_P(login_name)) {
4781 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4785 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4786 if (
NIL_P(pw_dir)) {
4787 pw_dir = rb_getpwdiruid();
4788 if (
NIL_P(pw_dir)) {
4789 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4794 copy_home_path(result, RSTRING_PTR(pw_dir));
4795 rb_str_resize(pw_dir, 0);
4800 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4802 return copy_home_path(result, dir);
4808#if NORMALIZE_UTF8PATH
4809 VALUE path = rb_str_normalize_ospath(ptr,
len);
4810 rb_enc_associate(path, fsenc);
4813 return rb_enc_str_new(ptr,
len, fsenc);
4820 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4822 rb_enc_associate(dirname, fsenc);
4826 size_t dirlen = RSTRING_LEN(dirname);
4829 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4830 if (!rb_enc_compatible(fname, dirname)) {
4833 rb_enc_check(fname, dirname);
4834 rb_bug(
"unreachable");
4836 rb_encoding *direnc = fs_enc_check(fname, dirname);
4837 if (direnc != fsenc) {
4844 do {buflen *= 2;}
while (dirlen > buflen);
4845 rb_str_resize(result, buflen);
4846 buf = RSTRING_PTR(result);
4847 memcpy(buf, cwdp, dirlen);
4848 rb_enc_associate(result, *enc);
4849 return buf + dirlen;
4853rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4855 const char *s, *b, *fend;
4856 char *buf, *p, *pend, *root;
4857 size_t buflen, bdiff;
4858 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4861 fend = s + RSTRING_LEN(fname);
4862 enc = rb_str_enc_get(fname);
4863 bool mb_enc = enc_mbclen_needed(enc);
4864 if (!mb_enc &&
RTEST(dname)) {
4865 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4868 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4869 BUFINIT(result, buf, p, pend);
4872 if (s + 1 == fend || isdirsep(s[1])) {
4876 if (++s < fend) ++s;
4877 rb_default_home_dir(result);
4880 s = nextdirsep(b = s, fend, enc);
4883 BUFCHECK(bdiff + userlen >= buflen);
4884 memcpy(p, b, userlen);
4887 rb_enc_associate(result, enc);
4888 rb_home_dir_of(result, result);
4892 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4894 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4895 (
int)userlen, b, fname);
4898 rb_raise(rb_eArgError,
"non-absolute home");
4901 BUFINIT(result, buf, p, pend);
4904#ifdef DOSISH_DRIVE_LETTER
4906 else if (s + 1 < fend && has_drive_letter(s)) {
4907 BUFINIT(result, buf, p, pend);
4909 if (s + 2 < fend && isdirsep(s[2])) {
4912 BUFCHECK(bdiff + 2 >= buflen);
4916 rb_enc_copy(result, fname);
4921 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4922 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4923 BUFINIT(result, buf, p, pend);
4930 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4931 BUFINIT(result, buf, p, pend);
4935 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4938 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4943 else if (s == fend || !rb_is_absolute_path(s)) {
4945 if (!
NIL_P(dname)) {
4947 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4951 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4953 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
4958 result = resized_result;
4962 rb_enc_associate(result, fs_enc_check(result, fname));
4963 BUFINIT(result, buf, p, pend);
4967 VALUE cwd = rb_dir_getwd_ospath();
4971 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
4972 BUFINIT(result, buf, p, pend);
4975#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4976 if (s < fend && isdirsep(*s)) {
4979 p = skipprefix(buf, p, mb_enc, enc);
4983 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4986 BUFINIT(result, buf, p, pend);
4990 do s++;
while (s < fend && isdirsep(*s));
4993 BUFCHECK(bdiff >= buflen);
4994 memset(buf,
'/',
len);
4996 rb_enc_associate(result, fs_enc_check(result, fname));
4998 if (p > buf && p[-1] ==
'/')
5002 BUFCHECK(bdiff + 1 >= buflen);
5007 BUFCHECK(bdiff + 1 >= buflen);
5009 root = skipprefix(buf, p+1, mb_enc, enc);
5022 if (s+1 == fend || isdirsep(*(s+1))) {
5026 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
5036#if defined FILE_ALT_SEPARATOR
5037 case FILE_ALT_SEPARATOR:
5048#if defined FILE_ALT_SEPARATOR
5049 case FILE_ALT_SEPARATOR:
5052 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5060 int n = ignored_char_p(s, fend, enc);
5063 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5071 Inc(s, fend, mb_enc, enc);
5080 if (p == skiproot(buf, p + !!*p) - 1) p++;
5083 rb_enc_check(fname, result);
5090str_shrink(
VALUE str)
5092 rb_str_resize(str, RSTRING_LEN(str));
5096#define expand_path(fname, dname, abs_mode, long_name, result) \
5097 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5099#define check_expand_path_args(fname, dname) \
5100 (((fname) = rb_get_path(fname)), \
5101 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5104file_expand_path_1(
VALUE fname,
long extra_capa)
5107 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5113 check_expand_path_args(fname, dname);
5114 return expand_path(fname, dname, 0, 1,
Qfalse);
5118rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5120 return expand_path(fname, dname, 0, 0,
Qfalse);
5124rb_file_s_expand_path(
int argc,
const VALUE *argv)
5127 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5173 return rb_file_s_expand_path(c, v);
5179 check_expand_path_args(fname, dname);
5180 return expand_path(fname, dname, 1, 1,
Qfalse);
5184rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5187 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5230 return rb_file_s_absolute_path(c, v);
5258 VALUE path = rb_get_path(fname);
5260 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5264enum rb_realpath_mode {
5268 RB_REALPATH_MODE_MAX
5272realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5273 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5275 const char *pend = unresolved + strlen(unresolved);
5279 while (unresolved < pend) {
5280 const char *testname = unresolved;
5281 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5282 long testnamelen = unresolved_firstsep - unresolved;
5283 const char *unresolved_nextname = unresolved_firstsep;
5284 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5285 unresolved_nextname++;
5286 unresolved = unresolved_nextname;
5287 if (testnamelen == 1 && testname[0] ==
'.') {
5289 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5290 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5291 bool mb_enc = enc_mbclen_needed(enc);
5292 const char *resolved_str = RSTRING_PTR(*resolvedp);
5293 const char *resolved_names = resolved_str + *prefixlenp;
5294 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5295 long len = lastsep ? lastsep - resolved_names : 0;
5296 rb_str_resize(*resolvedp, *prefixlenp +
len);
5302 if (*prefixlenp < RSTRING_LEN(testpath))
5304#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5305 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5306 const char *prefix = RSTRING_PTR(testpath);
5307 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5312 checkval = rb_hash_aref(loopcheck, testpath);
5313 if (!
NIL_P(checkval)) {
5314 if (checkval ==
ID2SYM(resolving)) {
5315 if (mode == RB_REALPATH_CHECK) {
5319 rb_syserr_fail_path(ELOOP, testpath);
5328 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5331 if (e == ENOENT && !
NIL_P(fallback)) {
5332 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5337 if (mode == RB_REALPATH_CHECK)
return -1;
5339 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5340 rb_syserr_fail_path(e, testpath);
5341 *resolvedp = testpath;
5345 rb_syserr_fail_path(e, testpath);
5349 if (S_ISLNK(sbuf.st_mode)) {
5352 const char *link_prefix, *link_names;
5353 long link_prefixlen;
5354 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5355 link = rb_readlink(testpath, enc);
5356 link_prefix = RSTRING_PTR(link);
5357 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5358 link_prefixlen = link_names - link_prefix;
5359 if (link_prefixlen > 0) {
5363 tmpenc = fs_enc_check(*resolvedp, link);
5366 *prefixlenp = link_prefixlen;
5368 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5369 loopcheck, mode, !*unresolved_firstsep))
5378 rb_hash_aset(loopcheck, s, s);
5379 *resolvedp = testpath;
5387#ifdef DOSISH_DRIVE_LETTER
5391ospath_for_expand(
VALUE path)
5394 case ENCINDEX_ASCII_8BIT:
5395 case ENCINDEX_US_ASCII:
5396 return rb_enc_associate_index(
rb_str_dup(path), rb_filesystem_encindex());
5398 return TO_OSPATH(path);
5403rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5407 VALUE unresolved_path;
5412 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5413 char *ptr, *prefixptr = NULL, *pend;
5416#ifdef DOSISH_DRIVE_LETTER
5417 VALUE rootdir = path;
5419 if (!
NIL_P(basedir) && skipprefixroot(ptr, ptr +
len, rb_enc_get(path)) == ptr) {
5424 if (
len >= 2 && has_drive_letter(ptr) && (
len == 2 || !isdirsep(ptr[2]))) {
5427 if (!
NIL_P(basedir)) basedir = ospath_for_expand(rb_get_path(basedir));
5428 path = rb_file_absolute_path(ospath_for_expand(path), basedir);
5435 if (!
NIL_P(basedir)) {
5440 enc = rb_enc_get(unresolved_path);
5441 unresolved_path = TO_OSPATH(unresolved_path);
5443 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5444 if (ptr != path_names) {
5445 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5449 if (!
NIL_P(basedir)) {
5451 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5452 if (ptr != basedir_names) {
5458 curdir = rb_dir_getwd_ospath();
5460 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5465 pend = prefixptr + prefixlen;
5466 bool mb_enc = enc_mbclen_needed(enc);
5467 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5469 prefixlen = ++ptr - prefixptr;
5472#ifdef FILE_ALT_SEPARATOR
5473 while (prefixptr < ptr) {
5474 if (*prefixptr == FILE_ALT_SEPARATOR) {
5477 Inc(prefixptr, pend, mb_enc, enc);
5481 switch (rb_enc_to_index(enc)) {
5482 case ENCINDEX_ASCII_8BIT:
5483 case ENCINDEX_US_ASCII:
5484 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5487 loopcheck = rb_hash_new();
5489 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5492 if (basedir_names) {
5493 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5496 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5499 if (origenc && origenc != rb_enc_get(resolved)) {
5501 rb_enc_associate(resolved, origenc);
5513static VALUE rb_file_join(
long argc,
VALUE *args);
5515#ifndef HAVE_REALPATH
5517rb_check_realpath_emulate_try(
VALUE arg)
5520 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5524rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5528#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5529 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5531# include <sys/syslimits.h>
5532# define NEEDS_REALPATH_BUFFER 1
5536rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5539 VALUE unresolved_path;
5540 char *resolved_ptr = NULL;
5542# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5543 char resolved_buffer[PATH_MAX];
5545 char *
const resolved_buffer = NULL;
5548 if (mode == RB_REALPATH_DIR) {
5549 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5553 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5554 VALUE paths[2] = {basedir, unresolved_path};
5555 unresolved_path = rb_file_join(2, paths);
5557 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5559 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5568 if (
errno == ENOTSUP ||
5570 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5571 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5574 if (mode == RB_REALPATH_CHECK) {
5577 rb_sys_fail_path(unresolved_path);
5579 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5580# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5584# if !defined(__linux__) && !defined(__APPLE__)
5588 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5589 if (mode == RB_REALPATH_CHECK) {
5592 rb_sys_fail_path(unresolved_path);
5596 if (origenc && origenc != rb_enc_get(resolved)) {
5600 rb_enc_associate(resolved, origenc);
5603 if (is_broken_string(resolved)) {
5604 rb_enc_associate(resolved, rb_filesystem_encoding());
5605 if (is_broken_string(resolved)) {
5606 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5613 if (mode == RB_REALPATH_CHECK) {
5617 arg[2] = (
VALUE)origenc;
5620 rb_check_realpath_emulate_rescue,
Qnil);
5623 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5629rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5631 const enum rb_realpath_mode mode =
5632 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5633 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5639 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5656rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5659 VALUE path = argv[0];
5661 return rb_realpath_internal(basedir, path, 1);
5677rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5680 VALUE path = argv[0];
5682 return rb_realpath_internal(basedir, path, 0);
5686rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5690 const char *s, *last;
5692 if (!e || !l2)
return 0;
5694 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5695 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5696 if (c ==
'.')
return l0;
5701 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5706 if (l1 < l2)
return l1;
5709 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5710#if CASEFOLD_FILESYSTEM
5711#define fncomp strncasecmp
5713#define fncomp strncmp
5715 if (fncomp(s, e, l2) == 0) {
5721static inline const char *
5722enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5724 const char *p, *q, *e, *end;
5727 long len = (alllen ? (size_t)*alllen : strlen(name));
5734 name = skipprefix(name, end, mb_enc, enc);
5735#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5736 const char *root = name;
5739 while (name < end && isdirsep(*name)) {
5746#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5750#ifdef DOSISH_DRIVE_LETTER
5751 else if (*p ==
':') {
5764 p = strrdirsep(name, end, mb_enc, enc);
5769 while (isdirsep(*p)) {
5774 n = ntfs_tail(p, end, mb_enc, enc) - p;
5776 n = chompdirsep(p, end, mb_enc, enc) - p;
5778 for (q = p; q - p < n && *q ==
'.'; q++);
5779 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5780 if (*q ==
'.') e = q;
5800ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5802 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5846 const char *name, *p, *fp = 0;
5852 CheckPath(fname, name);
5856 check_path_encoding(fext);
5858 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5859 enc = rb_str_enc_get(fname);
5862 n = RSTRING_LEN(fname);
5863 if (n <= 0 || !*name) {
5864 return rb_enc_str_new(0, 0, enc);
5867 bool mb_enc = enc_mbclen_needed(enc);
5868 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5874 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5879 if (f == RSTRING_LEN(fname)) {
5884 return rb_enc_str_new(p, f, enc);
5887static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5910rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5916 return rb_file_dirname_n(argv[0], n);
5922 return rb_file_dirname_n(fname, 1);
5926rb_file_dirname_n(
VALUE fname,
int n)
5928 const char *name, *root, *p, *end;
5931 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5932 CheckPath(fname, name);
5933 end = name + RSTRING_LEN(fname);
5935 bool mb_enc = !rb_str_enc_fastpath(fname);
5938 root = skiproot(name, end);
5940 if (root > name + 1 && isdirsep(*name))
5941 root = skipprefix(name = root - 2, end, mb_enc, enc);
5943 if (root > name + 1)
5946 if (n > (end - root + 1) / 2) {
5952 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5961 return rb_enc_str_new(
".", 1, enc);
5963#ifdef DOSISH_DRIVE_LETTER
5964 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5965 const char *top = skiproot(name + 2, end);
5966 dirname = rb_enc_str_new(name, 3, enc);
5971 dirname = rb_enc_str_new(name, p - name, enc);
5972#ifdef DOSISH_DRIVE_LETTER
5973 if (root == name + 2 && p == root && name[1] ==
':')
5979static inline const char *
5980enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5982 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5984 p = strrdirsep(name, end, mb_enc, enc);
5988 do name = ++p;
while (isdirsep(*p));
5991 while (*p && *p ==
'.') p++;
5993 if (*p ==
'.' || istrailinggarbage(*p)) {
5995 const char *last = p++, *dot = last;
5996 while (istrailinggarbage(*p)) {
5997 if (*p ==
'.') dot = p;
6000 if (!*p || isADS(*p)) {
6004 if (*last ==
'.' || dot > last) e = dot;
6011 else if (isADS(*p)) {
6015 else if (isdirsep(*p))
6017 Inc(p, end, mb_enc, enc);
6022 if (!e || e == name)
6047 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
6094 CheckPath(fname, name);
6095 long len = RSTRING_LEN(fname);
6098 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
6101 bool mb_enc = !rb_str_enc_fastpath(fname);
6104 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
6105 return rb_enc_str_new(ext,
len, enc);
6129 return rb_get_path(fname);
6161file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6163 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6164 return rb_file_join_ary(arg);
6168rb_file_join_ary(
VALUE ary)
6172 const char *name, *tail;
6182 check_path_encoding(tmp);
6183 len += RSTRING_LEN(tmp);
6191 RBASIC_CLEAR_CLASS(result);
6194 switch (OBJ_BUILTIN_TYPE(tmp)) {
6196 if (!checked) check_path_encoding(tmp);
6201 rb_raise(rb_eArgError,
"recursive array");
6213 rb_enc_copy(result, tmp);
6216 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6217 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6220 else if (tail == name +
len) {
6224 enc = fs_enc_check(result, tmp);
6226 rb_enc_associate(result, enc);
6234rb_file_join_fastpath(
long argc,
VALUE *args)
6239 for (i = 0; i < argc; i++) {
6240 VALUE tmp = args[i];
6242 size += RSTRING_LEN(tmp);
6255 const char *name = RSTRING_PTR(result);
6256 for (i = 1; i < argc; i++) {
6257 VALUE tmp = args[i];
6258 long len = RSTRING_LEN(result);
6264 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6267 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6272 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6279 rb_enc_associate(result, new_enc);
6280 encidx = rb_enc_to_index(new_enc);
6286 rb_str_null_check(result);
6291rb_file_join(
long argc,
VALUE *args)
6293 if (RB_UNLIKELY(argc == 0)) {
6297 VALUE result = rb_file_join_fastpath(argc, args);
6298 if (RB_LIKELY(result)) {
6318rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6320 return rb_file_join(argc, argv);
6323#if defined(HAVE_TRUNCATE)
6324struct truncate_arg {
6330nogvl_truncate(
void *ptr)
6332 struct truncate_arg *ta = ptr;
6333 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6365 struct truncate_arg ta;
6370 path = rb_str_encode_ospath(path);
6373 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6375 rb_sys_fail_path(path);
6379#define rb_file_s_truncate rb_f_notimplement
6382#if defined(HAVE_FTRUNCATE)
6383struct ftruncate_arg {
6389nogvl_ftruncate(
void *ptr)
6391 struct ftruncate_arg *fa = ptr;
6393 return (
VALUE)ftruncate(fa->fd, fa->pos);
6434 struct ftruncate_arg fa;
6441 rb_io_flush_raw(obj, 0);
6443 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6444 rb_sys_fail_path(fptr->
pathv);
6449#define rb_file_truncate rb_f_notimplement
6466#include <winerror.h>
6470rb_thread_flock(
void *data)
6473 int old_errno =
errno;
6475 int *op = data, ret = flock(op[0], op[1]);
6478 if (GetLastError() == ERROR_NOT_LOCKED) {
6536 op[1] = op1 =
NUM2INT(operation);
6541 rb_io_flush_raw(obj, 0);
6543 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6548#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6551 if (op1 & LOCK_NB)
return Qfalse;
6554 time.tv_usec = 100 * 1000;
6560#if defined(ERESTART)
6566 rb_syserr_fail_path(e, fptr->
pathv);
6573test_check(
int n,
int argc,
VALUE *argv)
6579 for (i=1; i<n; i++) {
6586#define CHECK(n) test_check((n), argc, argv)
6678 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6682 return rb_file_blockdev_p(0, argv[1]);
6685 return rb_file_chardev_p(0, argv[1]);
6688 return rb_file_directory_p(0, argv[1]);
6691 return rb_file_exist_p(0, argv[1]);
6694 return rb_file_file_p(0, argv[1]);
6697 return rb_file_sgid_p(0, argv[1]);
6700 return rb_file_grpowned_p(0, argv[1]);
6703 return rb_file_sticky_p(0, argv[1]);
6706 return rb_file_symlink_p(0, argv[1]);
6709 return rb_file_owned_p(0, argv[1]);
6712 return rb_file_rowned_p(0, argv[1]);
6715 return rb_file_pipe_p(0, argv[1]);
6718 return rb_file_readable_p(0, argv[1]);
6721 return rb_file_readable_real_p(0, argv[1]);
6724 return rb_file_size_p(0, argv[1]);
6727 return rb_file_socket_p(0, argv[1]);
6730 return rb_file_suid_p(0, argv[1]);
6733 return rb_file_writable_p(0, argv[1]);
6736 return rb_file_writable_real_p(0, argv[1]);
6739 return rb_file_executable_p(0, argv[1]);
6742 return rb_file_executable_real_p(0, argv[1]);
6745 return rb_file_zero_p(0, argv[1]);
6749 if (strchr(
"MAC", cmd)) {
6751 VALUE fname = argv[1];
6754 if (
rb_stat(fname, &st) == -1) {
6757 rb_syserr_fail_path(e, fname);
6762 return stat_atime(&st);
6764 return stat_mtime(&st);
6766 return stat_ctime(&st);
6772 return rb_file_identical_p(0, argv[1], argv[2]);
6775 if (strchr(
"=<>", cmd)) {
6776 struct stat st1, st2;
6783 t1 = stat_mtimespec(&st1);
6784 t2 = stat_mtimespec(&st2);
6788 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6792 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6793 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6797 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6798 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6805 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6808 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6857rb_stat_s_alloc(
VALUE klass)
6886 fname = rb_str_encode_ospath(fname);
6888 rb_sys_fail_path(fname);
6895 rb_st->initialized =
true;
6912 *copy_rb_st = *orig_rb_st;
6954rb_stat_ftype(
VALUE obj)
6956 return rb_file_ftype(get_stat(obj)->ST_(mode));
6972 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6999 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
7036 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
7068 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
7090 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
7110 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7145rb_stat_owned(
VALUE obj)
7147 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7152rb_stat_rowned(
VALUE obj)
7154 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7175rb_stat_grpowned(
VALUE obj)
7178 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7207 rb_io_stat_data *st = get_stat(obj);
7210 if (geteuid() == 0)
return Qtrue;
7213 if (rb_stat_owned(obj))
7214 return RBOOL(st->ST_(mode) & S_IRUSR);
7217 if (rb_stat_grpowned(obj))
7218 return RBOOL(st->ST_(mode) & S_IRGRP);
7221 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7239 rb_io_stat_data *st = get_stat(obj);
7242 if (getuid() == 0)
return Qtrue;
7245 if (rb_stat_rowned(obj))
7246 return RBOOL(st->ST_(mode) & S_IRUSR);
7249 if (rb_group_member(get_stat(obj)->ST_(gid)))
7250 return RBOOL(st->ST_(mode) & S_IRGRP);
7253 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7281rb_stat_wr(
VALUE obj)
7284 rb_io_stat_data *st = get_stat(obj);
7285 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7286 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7318 rb_io_stat_data *st = get_stat(obj);
7321 if (geteuid() == 0)
return Qtrue;
7324 if (rb_stat_owned(obj))
7325 return RBOOL(st->ST_(mode) & S_IWUSR);
7328 if (rb_stat_grpowned(obj))
7329 return RBOOL(st->ST_(mode) & S_IWGRP);
7332 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7353 rb_io_stat_data *st = get_stat(obj);
7356 if (getuid() == 0)
return Qtrue;
7359 if (rb_stat_rowned(obj))
7360 return RBOOL(st->ST_(mode) & S_IWUSR);
7363 if (rb_group_member(get_stat(obj)->ST_(gid)))
7364 return RBOOL(st->ST_(mode) & S_IWGRP);
7367 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7396rb_stat_ww(
VALUE obj)
7399 rb_io_stat_data *st = get_stat(obj);
7400 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7401 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7436 rb_io_stat_data *st = get_stat(obj);
7439 if (geteuid() == 0) {
7440 return RBOOL(st->ST_(mode) & S_IXUGO);
7444 if (rb_stat_owned(obj))
7445 return RBOOL(st->ST_(mode) & S_IXUSR);
7448 if (rb_stat_grpowned(obj))
7449 return RBOOL(st->ST_(mode) & S_IXGRP);
7452 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7468 rb_io_stat_data *st = get_stat(obj);
7471 if (getuid() == 0) {
7472 return RBOOL(st->ST_(mode) & S_IXUGO);
7476 if (rb_stat_rowned(obj))
7477 return RBOOL(st->ST_(mode) & S_IXUSR);
7480 if (rb_group_member(get_stat(obj)->ST_(gid)))
7481 return RBOOL(st->ST_(mode) & S_IXGRP);
7484 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7507 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7552 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7584 rb_off_t size = get_stat(obj)->ST_(size);
7586 if (size == 0)
return Qnil;
7619rb_stat_suid(
VALUE obj)
7622 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7656rb_stat_sgid(
VALUE obj)
7659 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7693rb_stat_sticky(
VALUE obj)
7696 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7701#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7702#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7713nogvl_mkfifo(
void *ptr)
7715 struct mkfifo_arg *ma = ptr;
7717 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7745 struct mkfifo_arg ma;
7754 path = rb_str_encode_ospath(path);
7755 ma.path = RSTRING_PTR(path);
7756 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7757 rb_sys_fail_path(path);
7762#define rb_file_s_mkfifo rb_f_notimplement
7765static VALUE rb_mFConst;
7768rb_file_const(
const char *name,
VALUE value)
7770 rb_define_const(rb_mFConst, name, value);
7774rb_is_absolute_path(
const char *path)
7776#ifdef DOSISH_DRIVE_LETTER
7777 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7780 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7783 if (path[0] ==
'/')
return 1;
7789ruby_is_fd_loadable(
int fd)
7796 if (fstat(fd, &st) < 0)
7799 if (S_ISREG(st.st_mode))
7802 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7805 if (S_ISDIR(st.st_mode))
7816rb_file_load_ok(
const char *path)
7823 int mode = (O_RDONLY |
7824#if defined O_NONBLOCK
7826#elif defined O_NDELAY
7832 if (!rb_gc_for_fd(
errno))
return 0;
7834 if (fd < 0)
return 0;
7837 ret = ruby_is_fd_loadable(fd);
7844is_explicit_relative(
const char *path)
7846 if (*path++ !=
'.')
return 0;
7847 if (*path ==
'.') path++;
7848 return isdirsep(*path);
7854 int encidx = rb_enc_get_index(orig);
7855 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7856 encidx = rb_filesystem_encindex();
7857 rb_enc_associate_index(path, encidx);
7865nav_component_p(
const char *s,
const char *send)
7867 if ((send - s) >= 2 && s[0] ==
'.') {
7868 return s[1] ==
'.' || isdirsep(s[1]);
7874fname_need_expansion_p(
VALUE fname)
7876 const char *s = RSTRING_PTR(fname);
7877 const long len = RSTRING_LEN(fname);
7878 const char *send = s +
len;
7880 if (nav_component_p(s, send)) {
7885 bool mbenc = enc_mbclen_needed(enc);
7887 s = enc_path_next(s, send, mbenc, enc);
7889 if (nav_component_p(s, send)) {
7893 s = enc_path_next(s, send, mbenc, enc);
7899expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7901 long dname_len = RSTRING_LEN(dname);
7902 const char *dname_ptr = RSTRING_PTR(dname);
7906 if (need_expansion || dname_ptr[0] ==
'~') {
7907 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7912 if (!isdirsep(dname_ptr[dname_len - 1])) {
7921rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
7924 VALUE fname = *filep;
7928 if (!ext[0])
return 0;
7931 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7932 f = RSTRING_PTR(fname);
7937 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7938 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7939 fnlen = RSTRING_LEN(fname);
7940 for (i=0; ext[i]; i++) {
7942 if (rb_file_load_ok(RSTRING_PTR(fname))) {
7943 *filep = copy_path_class(fname, *filep);
7951 long expanded_load_path_maxlen;
7952 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7953 if (!load_path)
return 0;
7956 RBASIC_CLEAR_CLASS(fname);
7957 fnlen = RSTRING_LEN(fname);
7958 bool need_expansion = fname_need_expansion_p(fname);
7961 rb_enc_associate_index(tmp, rb_usascii_encindex());
7963 for (j=0; ext[j]; j++) {
7965 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
7967 if (!RSTRING_LEN(dname))
continue;
7968 expand_feature(fname, dname, tmp, need_expansion);
7970 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7971 *filep = copy_path_class(tmp, *filep);
7977 rb_str_resize(tmp, 0);
7990 path = copy_path_class(file_expand_path_1(path, 0), path);
7991 f = RSTRING_PTR(path);
7995 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7996 if (!rb_file_load_ok(f))
return 0;
7998 path = copy_path_class(file_expand_path_1(path, 0), path);
8002 long expanded_load_path_maxlen;
8003 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8006 bool need_expansion = fname_need_expansion_p(path);
8008 rb_enc_associate_index(tmp, rb_usascii_encindex());
8009 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
8011 if (!RSTRING_LEN(dname))
continue;
8012 expand_feature(path, dname, tmp, need_expansion);
8014 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8015 return copy_path_class(tmp, path);
8018 rb_str_resize(tmp, 0);
8026#define define_filetest_function(name, func, argc) do { \
8027 rb_define_module_function(rb_mFileTest, name, func, argc); \
8028 rb_define_singleton_method(rb_cFile, name, func, argc); \
8031const char ruby_null_device[] =
8834#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8835 rb_CFString_class_initialize_before_fork();
8843 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8844 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8845 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8846 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8847 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8848 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8849 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8850 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8851 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8852 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8853 define_filetest_function(
"file?", rb_file_file_p, 1);
8854 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8855 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8856 define_filetest_function(
"size?", rb_file_size_p, 1);
8857 define_filetest_function(
"size", rb_file_s_size, 1);
8858 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8859 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8861 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8862 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8863 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8865 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8866 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8868 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8869 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8870 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8872 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8910 separator = rb_fstring_lit(
"/");
8912 rb_define_const(
rb_cFile,
"Separator", separator);
8914 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9263 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9266 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9268 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9270 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9272 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9274 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9276 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9277#if defined(O_NDELAY) || defined(O_NONBLOCK)
9279# define O_NONBLOCK O_NDELAY
9282 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9285 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9288 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9294 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9295#ifndef O_SHARE_DELETE
9296# define O_SHARE_DELETE 0
9299 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9302 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9306 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9310 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9314 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9318 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9322 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9326 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9330 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9332 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9334 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9336 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9339 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.