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
1152rb_stat_atime(
VALUE self)
1154 return stat_time(statx_atimespec(get_stat(self)));
1185rb_stat_mtime(
VALUE self)
1187 return stat_time(statx_mtimespec(get_stat(self)));
1205rb_stat_ctime(
VALUE self)
1207 return stat_time(statx_ctimespec(get_stat(self)));
1210#if defined(HAVE_STAT_BIRTHTIME)
1232rb_stat_birthtime(
VALUE self)
1234 return statx_birthtime(get_stat(self));
1237# define rb_stat_birthtime rb_f_notimplement
1256rb_stat_inspect(
VALUE self)
1260 static const struct {
1264 {
"dev", rb_stat_dev},
1265 {
"ino", rb_stat_ino},
1266 {
"mode", rb_stat_mode},
1267 {
"nlink", rb_stat_nlink},
1268 {
"uid", rb_stat_uid},
1269 {
"gid", rb_stat_gid},
1270 {
"rdev", rb_stat_rdev},
1271 {
"size", rb_stat_size},
1272 {
"blksize", rb_stat_blksize},
1273 {
"blocks", rb_stat_blocks},
1274 {
"atime", rb_stat_atime},
1275 {
"mtime", rb_stat_mtime},
1276 {
"ctime", rb_stat_ctime},
1277#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1278 {
"birthtime", rb_stat_birthtime},
1284 if (!rb_st->initialized) {
1292 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1300 v = (*member[i].func)(self);
1302 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1304 else if (i == 0 || i == 6) {
1305 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1325no_gvl_fstat(
void *data)
1328 return (
VALUE)fstat(arg->file.fd, arg->st);
1332fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1336 data.file.fd = fptr->
fd;
1339 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1343no_gvl_stat(
void * data)
1346 return (
void *)(
VALUE)stat(arg->file.path, arg->st);
1350stat_without_gvl(
const char *path,
struct stat *st)
1354 data.file.path = path;
1357 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1360#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1361 defined(HAVE_STRUCT_STATX_STX_BTIME)
1363# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1366# ifdef HAVE_SYSCALL_H
1367# include <syscall.h>
1368# elif defined HAVE_SYS_SYSCALL_H
1369# include <sys/syscall.h>
1371# if defined __linux__
1372# include <linux/stat.h>
1374statx(
int dirfd,
const char *pathname,
int flags,
1375 unsigned int mask,
struct statx *statxbuf)
1377 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1382typedef struct no_gvl_rb_io_stat_data {
1388} no_gvl_rb_io_stat_data;
1391io_blocking_statx(
void *data)
1393 no_gvl_rb_io_stat_data *arg = data;
1394 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1398no_gvl_statx(
void *data)
1400 return (
void *)io_blocking_statx(data);
1404statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1406 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1409 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1413lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1415 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1418 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1422fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1424 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1427 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1430#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1433rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1438 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1443 result = fstatx_without_gvl(fptr, stx, mask);
1448 file = rb_str_encode_ospath(file);
1449 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1455# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1457NORETURN(
static void statx_notimplement(
const char *field_name));
1462statx_notimplement(
const char *field_name)
1465 "%s is unimplemented on this filesystem",
1470statx_birthtime(
const rb_io_stat_data *stx)
1472 if (!statx_has_birthtime(stx)) {
1474 statx_notimplement(
"birthtime");
1476 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1481# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1482# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1483# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1484# define rb_statx(file, stx, mask) rb_stat(file, stx)
1485# define STATX(path, st, mask) stat(path, st)
1487#if defined(HAVE_STAT_BIRTHTIME)
1488# define statx_has_birthtime(st) 1
1490# define statx_has_birthtime(st) 0
1497# define FSTAT(fd, st) fstat(fd, st)
1506 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1511 result = fstat_without_gvl(fptr, st);
1516 file = rb_str_encode_ospath(file);
1517 result = stat_without_gvl(RSTRING_PTR(file), st);
1555 fname = rb_str_encode_ospath(fname);
1556 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1557 rb_sys_fail_path(fname);
1559 return rb_statx_new(&st);
1591rb_io_stat(
VALUE obj)
1597 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1598 rb_sys_fail_path(fptr->
pathv);
1600 return rb_statx_new(&st);
1605no_gvl_lstat(
void *ptr)
1608 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1612lstat_without_gvl(
const char *path,
struct stat *st)
1616 data.file.path = path;
1619 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1656 fname = rb_str_encode_ospath(fname);
1657 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1658 rb_sys_fail_path(fname);
1660 return rb_statx_new(&st);
1662 return rb_file_s_stat(klass, fname);
1695rb_file_lstat(
VALUE obj)
1704 path = rb_str_encode_ospath(fptr->
pathv);
1705 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1706 rb_sys_fail_path(fptr->
pathv);
1708 return rb_statx_new(&st);
1710 return rb_io_stat(obj);
1715rb_group_member(GETGROUPS_T gid)
1717#if !defined(HAVE_GETGROUPS)
1726 if (getgid() == gid || getegid() == gid)
1729 groups = getgroups(0, NULL);
1730 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1731 anum = getgroups(groups, gary);
1732 while (--anum >= 0) {
1733 if (gary[anum] == gid) {
1746# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1749#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1750#define USE_GETEUID 1
1755eaccess(
const char *path,
int mode)
1764 if (getuid() == euid && getgid() == getegid())
1765 return access(path, mode);
1767 if (stat(path, &st) < 0)
1777 if (st.st_mode & S_IXUGO)
1783 if (st.st_uid == euid)
1785 else if (rb_group_member(st.st_gid))
1788 if ((
int)(st.st_mode & mode) == mode)
return 0;
1792 return access(path, mode);
1803nogvl_eaccess(
void *ptr)
1807 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1811rb_eaccess(
VALUE fname,
int mode)
1816 fname = rb_str_encode_ospath(fname);
1820 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1824nogvl_access(
void *ptr)
1828 return (
void *)(
VALUE)access(aa->path, aa->mode);
1832rb_access(
VALUE fname,
int mode)
1837 fname = rb_str_encode_ospath(fname);
1841 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1884# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1890 if (S_ISDIR(st.st_mode))
return Qtrue;
1919# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1925 if (S_ISFIFO(st.st_mode))
return Qtrue;
1957# define S_ISLNK(m) _S_ISLNK(m)
1960# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1963# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1973 fname = rb_str_encode_ospath(fname);
1975 if (S_ISLNK(st.st_mode))
return Qtrue;
2010# define S_ISSOCK(m) _S_ISSOCK(m)
2013# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
2016# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
2026 if (S_ISSOCK(st.st_mode))
return Qtrue;
2054# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
2056# define S_ISBLK(m) (0)
2064 if (S_ISBLK(st.st_mode))
return Qtrue;
2091# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2097 if (S_ISCHR(st.st_mode))
return Qtrue;
2167 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2181rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2183 return RBOOL(rb_access(fname, R_OK) >= 0);
2187# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2191# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2220rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2226 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2227 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2261 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2278rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2280 return RBOOL(rb_access(fname, W_OK) >= 0);
2309rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2315 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2316 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2352 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2371rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2373 return RBOOL(rb_access(fname, X_OK) >= 0);
2377# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2409 return RBOOL(S_ISREG(st.st_mode));
2469 return RBOOL(st.st_size == 0);
2507 if (st.st_size == 0)
return Qnil;
2541 return RBOOL(st.st_uid == geteuid());
2550 return RBOOL(st.st_uid == getuid());
2582 if (rb_group_member(st.st_gid))
return Qtrue;
2587#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2589check3rdbyte(
VALUE fname,
int mode)
2594 return RBOOL(st.st_mode & mode);
2627 return check3rdbyte(fname, S_ISUID);
2662 return check3rdbyte(fname, S_ISGID);
2698 return check3rdbyte(fname, S_ISVTX);
2735 struct stat st1, st2;
2739 if (st1.st_dev != st2.st_dev)
return Qfalse;
2740 if (st1.st_ino != st2.st_ino)
return Qfalse;
2744 return rb_w32_file_identical_p(fname1, fname2);
2770 if (
rb_stat(fname, &st) < 0) {
2773 rb_syserr_fail_path(e, fname);
2779rb_file_ftype(mode_t mode)
2783 if (S_ISREG(mode)) {
2786 else if (S_ISDIR(mode)) {
2789 else if (S_ISCHR(mode)) {
2790 t =
"characterSpecial";
2793 else if (S_ISBLK(mode)) {
2798 else if (S_ISFIFO(mode)) {
2803 else if (S_ISLNK(mode)) {
2808 else if (S_ISSOCK(mode)) {
2816 return rb_fstring_cstr(t);
2858 fname = rb_str_encode_ospath(fname);
2860 rb_sys_fail_path(fname);
2863 return rb_file_ftype(st.st_mode);
2911 if (
rb_stat(fname, &st) < 0) {
2914 rb_syserr_fail_path(e, fname);
2916 return stat_time(stat_atimespec(&st));
2943rb_file_atime(
VALUE obj)
2949 if (fstat(fptr->
fd, &st) == -1) {
2950 rb_sys_fail_path(fptr->
pathv);
2952 return stat_time(stat_atimespec(&st));
2992 if (
rb_stat(fname, &st) < 0) {
2995 rb_syserr_fail_path(e, fname);
2997 return stat_time(stat_mtimespec(&st));
3022rb_file_mtime(
VALUE obj)
3028 if (fstat(fptr->
fd, &st) == -1) {
3029 rb_sys_fail_path(fptr->
pathv);
3031 return stat_time(stat_mtimespec(&st));
3074 if (
rb_stat(fname, &st) < 0) {
3077 rb_syserr_fail_path(e, fname);
3079 return stat_time(stat_ctimespec(&st));
3096rb_file_ctime(
VALUE obj)
3102 if (fstat(fptr->
fd, &st) == -1) {
3103 rb_sys_fail_path(fptr->
pathv);
3105 return stat_time(stat_ctimespec(&st));
3108#if defined(HAVE_STAT_BIRTHTIME)
3139 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3142 rb_syserr_fail_path(e, fname);
3144 return statx_birthtime(&st);
3147# define rb_file_s_birthtime rb_f_notimplement
3150#if defined(HAVE_STAT_BIRTHTIME)
3171rb_file_birthtime(
VALUE obj)
3177 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3178 rb_sys_fail_path(fptr->
pathv);
3180 return statx_birthtime(&st);
3183# define rb_file_birthtime rb_f_notimplement
3195 rb_io_flush_raw(file, 0);
3198 if (fstat(fptr->
fd, &st) == -1) {
3199 rb_sys_fail_path(fptr->
pathv);
3225file_size(
VALUE self)
3227 return OFFT2NUM(rb_file_size(self));
3236nogvl_chmod(
void *ptr)
3239 int ret = chmod(data->path, data->mode);
3240 return (
void *)(
VALUE)ret;
3244rb_chmod(
const char *path, mode_t mode)
3250 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3254chmod_internal(
const char *path,
void *mode)
3256 return chmod(path, *(mode_t *)mode);
3289 return apply2files(chmod_internal, argc, argv, &mode);
3293struct nogvl_fchmod_data {
3299io_blocking_fchmod(
void *ptr)
3301 struct nogvl_fchmod_data *data = ptr;
3302 int ret = fchmod(data->fd, data->mode);
3307rb_fchmod(
struct rb_io* io, mode_t mode)
3310 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3311 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3340#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3348 if (rb_fchmod(fptr, mode) == -1) {
3349 if (HAVE_FCHMOD ||
errno != ENOSYS)
3350 rb_sys_fail_path(fptr->
pathv);
3353 if (!HAVE_FCHMOD)
return INT2FIX(0);
3356#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3358 path = rb_str_encode_ospath(fptr->
pathv);
3359 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3360 rb_sys_fail_path(fptr->
pathv);
3366#if defined(HAVE_LCHMOD)
3368lchmod_internal(
const char *path,
void *mode)
3370 return lchmod(path, *(mode_t *)mode);
3407 return apply2files(lchmod_internal, argc, argv, &mode);
3410#define rb_file_s_lchmod rb_f_notimplement
3413static inline rb_uid_t
3417 return (rb_uid_t)-1;
3422static inline rb_gid_t
3426 return (rb_gid_t)-1;
3437chown_internal(
const char *path,
void *arg)
3440 return chown(path, args->owner, args->group);
3494 arg.owner = to_uid(*argv++);
3495 arg.group = to_gid(*argv++);
3497 return apply2files(chown_internal, argc, argv, &arg);
3509nogvl_chown(
void *ptr)
3512 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3516rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3519 .as = {.path = path},
3520 .new = {.owner = owner, .group = group},
3522 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3527nogvl_fchown(
void *ptr)
3530 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3534rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3539 .new = {.owner = owner, .group = group},
3541 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3575 path = rb_str_encode_ospath(fptr->
pathv);
3576 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3577 rb_sys_fail_path(fptr->
pathv);
3579 if (rb_fchown(fptr->
fd, o, g) == -1)
3580 rb_sys_fail_path(fptr->
pathv);
3586#if defined(HAVE_LCHOWN)
3588lchown_internal(
const char *path,
void *arg)
3591 return lchown(path, args->owner, args->group);
3648 arg.owner = to_uid(*argv++);
3649 arg.group = to_gid(*argv++);
3651 return apply2files(lchown_internal, argc, argv, &arg);
3654#define rb_file_s_lchown rb_f_notimplement
3664NORETURN(
static void utime_failed(
struct apply_arg *));
3670 VALUE path = aa->fn[aa->i].path;
3673 if (ua->tsp && e == EINVAL) {
3676 VALUE atime = ua->atime;
3677 VALUE mtime = ua->mtime;
3679 if (!
NIL_P(atime)) {
3682 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3685 if (
NIL_P(a)) e[0] = m;
3701 rb_syserr_fail_path(e, path);
3705#if defined(HAVE_UTIMES)
3707# if !defined(HAVE_UTIMENSAT)
3709# elif defined(__APPLE__) && \
3710 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3712# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3713typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3717static inline utimensat_func *
3724# define utimensat rb_utimensat()
3731utime_internal(
const char *path,
void *arg)
3734 const struct timespec *tsp = v->tsp;
3735 struct timeval tvbuf[2], *tvp = NULL;
3737#if defined(HAVE_UTIMENSAT)
3738# if defined(__APPLE__)
3739 const int try_utimensat = utimensat != NULL;
3740 const int try_utimensat_follow = utimensat != NULL;
3742# define TRY_UTIMENSAT 1
3743 static int try_utimensat = 1;
3744# ifdef AT_SYMLINK_NOFOLLOW
3745 static int try_utimensat_follow = 1;
3747 const int try_utimensat_follow = 0;
3752 if (v->follow ? try_utimensat_follow : try_utimensat) {
3753# ifdef AT_SYMLINK_NOFOLLOW
3755 flags = AT_SYMLINK_NOFOLLOW;
3759 int result = utimensat(AT_FDCWD, path, tsp, flags);
3760# ifdef TRY_UTIMENSAT
3761 if (result < 0 &&
errno == ENOSYS) {
3762# ifdef AT_SYMLINK_NOFOLLOW
3763 try_utimensat_follow = 0;
3775 tvbuf[0].tv_sec = tsp[0].tv_sec;
3776 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3777 tvbuf[1].tv_sec = tsp[1].tv_sec;
3778 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3782 if (v->follow)
return lutimes(path, tvp);
3784 return utimes(path, tvp);
3789#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3797utime_internal(
const char *path,
void *arg)
3801 struct utimbuf utbuf, *utp = NULL;
3803 utbuf.actime = tsp[0].tv_sec;
3804 utbuf.modtime = tsp[1].tv_sec;
3807 return utime(path, utp);
3812utime_internal_i(
int argc,
VALUE *argv,
int follow)
3815 struct timespec tss[2], *tsp = NULL;
3818 args.atime = *argv++;
3819 args.mtime = *argv++;
3821 args.follow = follow;
3823 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3826 if (args.atime == args.mtime)
3833 return apply2files(utime_internal, argc, argv, &args);
3879 return utime_internal_i(argc, argv, FALSE);
3882#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3940 return utime_internal_i(argc, argv, TRUE);
3943#define rb_file_s_lutime rb_f_notimplement
3946#ifdef RUBY_FUNCTION_NAME_STRING
3947# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3949# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3951#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3952NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3954syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3958 const int max_pathlen = MAX_PATH;
3960 const int max_pathlen = MAXPATHLEN;
3971#ifdef RUBY_FUNCTION_NAME_STRING
3972 rb_syserr_fail_path_in(func, e, str);
3974 rb_syserr_fail_path(e, str);
4009 from = rb_str_encode_ospath(from);
4010 to = rb_str_encode_ospath(to);
4013 sys_fail2(from, to);
4018#define rb_file_s_link rb_f_notimplement
4059 from = rb_str_encode_ospath(from);
4060 to = rb_str_encode_ospath(to);
4063 sys_fail2(from, to);
4068#define rb_file_s_symlink rb_f_notimplement
4096 return rb_readlink(path, rb_filesystem_encoding());
4099struct readlink_arg {
4106nogvl_readlink(
void *ptr)
4108 struct readlink_arg *ra = ptr;
4110 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4114readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4116 struct readlink_arg ra;
4118 ra.path = RSTRING_PTR(path);
4119 ra.buf = RSTRING_PTR(buf);
4122 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4133 path = rb_str_encode_ospath(path);
4134 v = rb_enc_str_new(0, size, enc);
4135 while ((rv = readlink_without_gvl(path, v, size)) == size
4137 || (rv < 0 &&
errno == ERANGE)
4146 rb_str_resize(v, 0);
4147 rb_syserr_fail_path(e, path);
4149 rb_str_resize(v, rv);
4154#define rb_file_s_readlink rb_f_notimplement
4158unlink_internal(
const char *path,
void *arg)
4160 return unlink(path);
4203rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4205 return apply2files(unlink_internal, argc, argv, 0);
4214no_gvl_rename(
void *ptr)
4218 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4296 f = rb_str_encode_ospath(from);
4297 t = rb_str_encode_ospath(to);
4300#if defined __CYGWIN__
4303 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4308 if (chmod(ra.dst, 0666) == 0 &&
4309 unlink(ra.dst) == 0 &&
4310 rename(ra.src, ra.dst) == 0)
4314 syserr_fail2(e, from, to);
4348 rb_error_arity(argc, 0, 1);
4356#if defined __CYGWIN__ || defined DOSISH
4358#define DOSISH_DRIVE_LETTER
4359#define FILE_ALT_SEPARATOR '\\'
4361#ifdef FILE_ALT_SEPARATOR
4362#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4364static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4367#define isdirsep(x) ((x) == '/')
4379#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4380#define isADS(x) ((x) == ':')
4382#define istrailinggarbage(x) 0
4385#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4387#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4388#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4390#if defined(DOSISH_UNC)
4391#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4393#define has_unc(buf) 0
4396#ifdef DOSISH_DRIVE_LETTER
4398has_drive_letter(
const char *buf)
4400 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4425 if (chdir(drive) == 0) {
4426 drvcwd = rb_dir_getwd_ospath();
4438not_same_drive(
VALUE path,
int drive)
4440 const char *p = RSTRING_PTR(path);
4441 if (RSTRING_LEN(path) < 2)
return 0;
4442 if (has_drive_letter(p)) {
4453skiproot(
const char *path,
const char *end)
4455#ifdef DOSISH_DRIVE_LETTER
4456 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4458 while (path < end && isdirsep(*path)) path++;
4459 return (
char *)path;
4463enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4465 while (s < e && !isdirsep(*s)) {
4466 Inc(s, e, mb_enc, enc);
4471#define nextdirsep rb_enc_path_next
4475 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4478#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4479#define skipprefix enc_path_skip_prefix
4481#define skipprefix(path, end, mb_enc, enc) (path)
4484enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4486#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4488 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4490 while (path < end && isdirsep(*path)) path++;
4491 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4492 path + 2 <= end && !isdirsep(path[1])) {
4493 path = enc_path_next(path + 1, end, mb_enc, enc);
4495 return (
char *)path;
4498#ifdef DOSISH_DRIVE_LETTER
4499 if (path + 2 <= end && has_drive_letter(path))
4500 return (
char *)(path + 2);
4503 return (
char *)path;
4507rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4509 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4513skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4515#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4516 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4517 while (p < end && isdirsep(*p)) p++;
4520 return skiproot(path, end);
4525rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4527 return skipprefixroot(path, end, enc);
4531enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4534 while (path < end) {
4535 if (isdirsep(*path)) {
4536 const char *tmp = path++;
4537 while (path < end && isdirsep(*path)) path++;
4538 if (path >= end)
break;
4542 Inc(path, end, mb_enc, enc);
4548rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4550 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4554strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4556 if (RB_UNLIKELY(mb_enc)) {
4557 return enc_path_last_separator(path, end, mb_enc, enc);
4560 const char *cursor = end - 1;
4562 while (cursor >= path && isdirsep(cursor[0])) {
4566 while (cursor >= path) {
4567 if (isdirsep(cursor[0])) {
4568 while (cursor > path && isdirsep(cursor[-1])) {
4571 return (
char *)cursor;
4579chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4581 while (path < end) {
4582 if (isdirsep(*path)) {
4583 const char *last = path++;
4584 while (path < end && isdirsep(*path)) path++;
4585 if (path >= end)
return (
char *)last;
4588 Inc(path, end, mb_enc, enc);
4591 return (
char *)path;
4597 if (path < end && isdirsep(*path)) path++;
4598 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4604 rb_encoding *enc = rb_enc_check_str(path1, path2);
4605 int encidx = rb_enc_to_index(enc);
4606 if (encidx == ENCINDEX_US_ASCII) {
4607 encidx = rb_enc_get_index(path1);
4608 if (encidx == ENCINDEX_US_ASCII)
4609 encidx = rb_enc_get_index(path2);
4610 enc = rb_enc_from_index(encidx);
4617ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4619 while (path < end && *path ==
'.') path++;
4620 while (path < end && !isADS(*path)) {
4621 if (istrailinggarbage(*path)) {
4622 const char *last = path++;
4623 while (path < end && istrailinggarbage(*path)) path++;
4624 if (path >= end || isADS(*path))
return (
char *)last;
4626 else if (isdirsep(*path)) {
4627 const char *last = path++;
4628 while (path < end && isdirsep(*path)) path++;
4629 if (path >= end)
return (
char *)last;
4630 if (isADS(*path)) path++;
4633 Inc(path, end, mb_enc, enc);
4636 return (
char *)path;
4640#define BUFCHECK(cond) do {\
4643 do {buflen *= 2;} while (cond);\
4644 rb_str_resize(result, buflen);\
4645 buf = RSTRING_PTR(result);\
4647 pend = buf + buflen;\
4651#define BUFINIT(result, buf, p, pend) do {\
4652 if (!result) { result = rb_usascii_str_new(0, 1); } \
4653 p = buf = RSTRING_PTR(result); \
4654 buflen = RSTRING_LEN(result); \
4655 pend = p + buflen; \
4659# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4661# define SKIPPATHSEP(p) 1
4664#define BUFCOPY(srcptr, srclen) do { \
4665 const int skip = SKIPPATHSEP(p); \
4666 rb_str_set_len(result, p-buf+skip); \
4667 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4669 memcpy(p, (srcptr), (srclen)); \
4673#define WITH_ROOTDIFF(stmt) do { \
4674 long rootdiff = root - buf; \
4676 root = buf + rootdiff; \
4680copy_home_path(
VALUE result,
const char *dir)
4686 dirlen = strlen(dir);
4687 rb_str_resize(result, dirlen);
4688 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4689 encidx = rb_filesystem_encindex();
4690 rb_enc_associate_index(result, encidx);
4691#if defined FILE_ALT_SEPARATOR
4693 bool mb_enc = enc_mbclen_needed(enc);
4694 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4695 if (*p == FILE_ALT_SEPARATOR) {
4707 VALUE dirname = rb_getpwdirnam_for_login(user);
4708 if (dirname ==
Qnil) {
4709 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4711 const char *dir = RSTRING_PTR(dirname);
4713 extern char *getlogin(
void);
4714 const char *pwPtr = 0;
4716 # define endpwent() ((void)0)
4717 const char *dir, *username = RSTRING_PTR(user);
4728 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4729 dir = pwPtr = getenv(
"HOME");
4731 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4734 copy_home_path(result, dir);
4740rb_default_home_dir(
VALUE result)
4742 const char *dir = getenv(
"HOME");
4744#if defined HAVE_PWD_H
4758 VALUE login_name = rb_getlogin();
4760# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4765 if (
NIL_P(login_name)) {
4766 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4770 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4771 if (
NIL_P(pw_dir)) {
4772 pw_dir = rb_getpwdiruid();
4773 if (
NIL_P(pw_dir)) {
4774 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4779 copy_home_path(result, RSTRING_PTR(pw_dir));
4780 rb_str_resize(pw_dir, 0);
4785 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4787 return copy_home_path(result, dir);
4793#if NORMALIZE_UTF8PATH
4794 VALUE path = rb_str_normalize_ospath(ptr,
len);
4795 rb_enc_associate(path, fsenc);
4798 return rb_enc_str_new(ptr,
len, fsenc);
4805 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4807 rb_enc_associate(dirname, fsenc);
4811 size_t dirlen = RSTRING_LEN(dirname);
4814 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4815 if (!rb_enc_compatible(fname, dirname)) {
4818 rb_enc_check(fname, dirname);
4819 rb_bug(
"unreachable");
4821 rb_encoding *direnc = fs_enc_check(fname, dirname);
4822 if (direnc != fsenc) {
4829 do {buflen *= 2;}
while (dirlen > buflen);
4830 rb_str_resize(result, buflen);
4831 buf = RSTRING_PTR(result);
4832 memcpy(buf, cwdp, dirlen);
4833 rb_enc_associate(result, *enc);
4834 return buf + dirlen;
4838rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4840 const char *s, *b, *fend;
4841 char *buf, *p, *pend, *root;
4842 size_t buflen, bdiff;
4843 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4846 fend = s + RSTRING_LEN(fname);
4847 enc = rb_str_enc_get(fname);
4848 bool mb_enc = enc_mbclen_needed(enc);
4849 if (!mb_enc &&
RTEST(dname)) {
4850 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4853 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4854 BUFINIT(result, buf, p, pend);
4857 if (s + 1 == fend || isdirsep(s[1])) {
4861 if (++s < fend) ++s;
4862 rb_default_home_dir(result);
4865 s = nextdirsep(b = s, fend, enc);
4868 BUFCHECK(bdiff + userlen >= buflen);
4869 memcpy(p, b, userlen);
4872 rb_enc_associate(result, enc);
4873 rb_home_dir_of(result, result);
4877 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4879 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4880 (
int)userlen, b, fname);
4883 rb_raise(rb_eArgError,
"non-absolute home");
4886 BUFINIT(result, buf, p, pend);
4889#ifdef DOSISH_DRIVE_LETTER
4891 else if (s + 1 < fend && has_drive_letter(s)) {
4892 BUFINIT(result, buf, p, pend);
4894 if (s + 2 < fend && isdirsep(s[2])) {
4897 BUFCHECK(bdiff + 2 >= buflen);
4901 rb_enc_copy(result, fname);
4906 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4907 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4908 BUFINIT(result, buf, p, pend);
4915 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4916 BUFINIT(result, buf, p, pend);
4920 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4923 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4928 else if (s == fend || !rb_is_absolute_path(s)) {
4930 if (!
NIL_P(dname)) {
4932 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4936 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4938 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
4943 result = resized_result;
4947 rb_enc_associate(result, fs_enc_check(result, fname));
4948 BUFINIT(result, buf, p, pend);
4952 VALUE cwd = rb_dir_getwd_ospath();
4956 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
4957 BUFINIT(result, buf, p, pend);
4960#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4961 if (s < fend && isdirsep(*s)) {
4964 p = skipprefix(buf, p, mb_enc, enc);
4968 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4971 BUFINIT(result, buf, p, pend);
4975 do s++;
while (s < fend && isdirsep(*s));
4978 BUFCHECK(bdiff >= buflen);
4979 memset(buf,
'/',
len);
4981 rb_enc_associate(result, fs_enc_check(result, fname));
4983 if (p > buf && p[-1] ==
'/')
4987 BUFCHECK(bdiff + 1 >= buflen);
4992 BUFCHECK(bdiff + 1 >= buflen);
4994 root = skipprefix(buf, p+1, mb_enc, enc);
5007 if (s+1 == fend || isdirsep(*(s+1))) {
5011 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
5021#if defined FILE_ALT_SEPARATOR
5022 case FILE_ALT_SEPARATOR:
5033#if defined FILE_ALT_SEPARATOR
5034 case FILE_ALT_SEPARATOR:
5037 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5045 int n = ignored_char_p(s, fend, enc);
5048 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5056 Inc(s, fend, mb_enc, enc);
5065 if (p == skiproot(buf, p + !!*p) - 1) p++;
5068 rb_enc_check(fname, result);
5075str_shrink(
VALUE str)
5077 rb_str_resize(str, RSTRING_LEN(str));
5081#define expand_path(fname, dname, abs_mode, long_name, result) \
5082 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5084#define check_expand_path_args(fname, dname) \
5085 (((fname) = rb_get_path(fname)), \
5086 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5089file_expand_path_1(
VALUE fname,
long extra_capa)
5092 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5098 check_expand_path_args(fname, dname);
5099 return expand_path(fname, dname, 0, 1,
Qfalse);
5103rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5105 return expand_path(fname, dname, 0, 0,
Qfalse);
5109rb_file_s_expand_path(
int argc,
const VALUE *argv)
5112 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5158 return rb_file_s_expand_path(c, v);
5164 check_expand_path_args(fname, dname);
5165 return expand_path(fname, dname, 1, 1,
Qfalse);
5169rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5172 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5215 return rb_file_s_absolute_path(c, v);
5243 VALUE path = rb_get_path(fname);
5245 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5249enum rb_realpath_mode {
5253 RB_REALPATH_MODE_MAX
5257realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5258 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5260 const char *pend = unresolved + strlen(unresolved);
5264 while (unresolved < pend) {
5265 const char *testname = unresolved;
5266 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5267 long testnamelen = unresolved_firstsep - unresolved;
5268 const char *unresolved_nextname = unresolved_firstsep;
5269 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5270 unresolved_nextname++;
5271 unresolved = unresolved_nextname;
5272 if (testnamelen == 1 && testname[0] ==
'.') {
5274 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5275 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5276 bool mb_enc = enc_mbclen_needed(enc);
5277 const char *resolved_str = RSTRING_PTR(*resolvedp);
5278 const char *resolved_names = resolved_str + *prefixlenp;
5279 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5280 long len = lastsep ? lastsep - resolved_names : 0;
5281 rb_str_resize(*resolvedp, *prefixlenp +
len);
5287 if (*prefixlenp < RSTRING_LEN(testpath))
5289#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5290 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5291 const char *prefix = RSTRING_PTR(testpath);
5292 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5297 checkval = rb_hash_aref(loopcheck, testpath);
5298 if (!
NIL_P(checkval)) {
5299 if (checkval ==
ID2SYM(resolving)) {
5300 if (mode == RB_REALPATH_CHECK) {
5304 rb_syserr_fail_path(ELOOP, testpath);
5313 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5316 if (e == ENOENT && !
NIL_P(fallback)) {
5317 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5322 if (mode == RB_REALPATH_CHECK)
return -1;
5324 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5325 rb_syserr_fail_path(e, testpath);
5326 *resolvedp = testpath;
5330 rb_syserr_fail_path(e, testpath);
5334 if (S_ISLNK(sbuf.st_mode)) {
5337 const char *link_prefix, *link_names;
5338 long link_prefixlen;
5339 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5340 link = rb_readlink(testpath, enc);
5341 link_prefix = RSTRING_PTR(link);
5342 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5343 link_prefixlen = link_names - link_prefix;
5344 if (link_prefixlen > 0) {
5348 tmpenc = fs_enc_check(*resolvedp, link);
5351 *prefixlenp = link_prefixlen;
5353 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5354 loopcheck, mode, !*unresolved_firstsep))
5363 rb_hash_aset(loopcheck, s, s);
5364 *resolvedp = testpath;
5372#ifdef DOSISH_DRIVE_LETTER
5376ospath_for_expand(
VALUE path)
5379 case ENCINDEX_ASCII_8BIT:
5380 case ENCINDEX_US_ASCII:
5381 return rb_enc_associate_index(
rb_str_dup(path), rb_filesystem_encindex());
5383 return TO_OSPATH(path);
5388rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5392 VALUE unresolved_path;
5397 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5398 char *ptr, *prefixptr = NULL, *pend;
5401#ifdef DOSISH_DRIVE_LETTER
5402 VALUE rootdir = path;
5404 if (!
NIL_P(basedir) && skipprefixroot(ptr, ptr +
len, rb_enc_get(path)) == ptr) {
5409 if (
len >= 2 && has_drive_letter(ptr) && (
len == 2 || !isdirsep(ptr[2]))) {
5412 if (!
NIL_P(basedir)) basedir = ospath_for_expand(rb_get_path(basedir));
5413 path = rb_file_absolute_path(ospath_for_expand(path), basedir);
5420 if (!
NIL_P(basedir)) {
5425 enc = rb_enc_get(unresolved_path);
5426 unresolved_path = TO_OSPATH(unresolved_path);
5428 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5429 if (ptr != path_names) {
5430 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5434 if (!
NIL_P(basedir)) {
5436 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5437 if (ptr != basedir_names) {
5443 curdir = rb_dir_getwd_ospath();
5445 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5450 pend = prefixptr + prefixlen;
5451 bool mb_enc = enc_mbclen_needed(enc);
5452 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5454 prefixlen = ++ptr - prefixptr;
5457#ifdef FILE_ALT_SEPARATOR
5458 while (prefixptr < ptr) {
5459 if (*prefixptr == FILE_ALT_SEPARATOR) {
5462 Inc(prefixptr, pend, mb_enc, enc);
5466 switch (rb_enc_to_index(enc)) {
5467 case ENCINDEX_ASCII_8BIT:
5468 case ENCINDEX_US_ASCII:
5469 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5472 loopcheck = rb_hash_new();
5474 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5477 if (basedir_names) {
5478 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5481 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5484 if (origenc && origenc != rb_enc_get(resolved)) {
5486 rb_enc_associate(resolved, origenc);
5498static VALUE rb_file_join(
long argc,
VALUE *args);
5500#ifndef HAVE_REALPATH
5502rb_check_realpath_emulate_try(
VALUE arg)
5505 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5509rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5513#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5514 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5516# include <sys/syslimits.h>
5517# define NEEDS_REALPATH_BUFFER 1
5521rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5524 VALUE unresolved_path;
5525 char *resolved_ptr = NULL;
5527# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5528 char resolved_buffer[PATH_MAX];
5530 char *
const resolved_buffer = NULL;
5533 if (mode == RB_REALPATH_DIR) {
5534 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5538 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5539 VALUE paths[2] = {basedir, unresolved_path};
5540 unresolved_path = rb_file_join(2, paths);
5542 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5544 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5553 if (
errno == ENOTSUP ||
5555 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5556 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5559 if (mode == RB_REALPATH_CHECK) {
5562 rb_sys_fail_path(unresolved_path);
5564 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5565# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5569# if !defined(__linux__) && !defined(__APPLE__)
5573 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5574 if (mode == RB_REALPATH_CHECK) {
5577 rb_sys_fail_path(unresolved_path);
5581 if (origenc && origenc != rb_enc_get(resolved)) {
5585 rb_enc_associate(resolved, origenc);
5588 if (is_broken_string(resolved)) {
5589 rb_enc_associate(resolved, rb_filesystem_encoding());
5590 if (is_broken_string(resolved)) {
5591 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5598 if (mode == RB_REALPATH_CHECK) {
5602 arg[2] = (
VALUE)origenc;
5605 rb_check_realpath_emulate_rescue,
Qnil);
5608 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5614rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5616 const enum rb_realpath_mode mode =
5617 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5618 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5624 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5641rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5644 VALUE path = argv[0];
5646 return rb_realpath_internal(basedir, path, 1);
5662rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5665 VALUE path = argv[0];
5667 return rb_realpath_internal(basedir, path, 0);
5671rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5675 const char *s, *last;
5677 if (!e || !l2)
return 0;
5679 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5680 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5681 if (c ==
'.')
return l0;
5686 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5691 if (l1 < l2)
return l1;
5694 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5695#if CASEFOLD_FILESYSTEM
5696#define fncomp strncasecmp
5698#define fncomp strncmp
5700 if (fncomp(s, e, l2) == 0) {
5706static inline const char *
5707enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5709 const char *p, *q, *e, *end;
5712 long len = (alllen ? (size_t)*alllen : strlen(name));
5719 name = skipprefix(name, end, mb_enc, enc);
5720#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5721 const char *root = name;
5724 while (name < end && isdirsep(*name)) {
5731#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5735#ifdef DOSISH_DRIVE_LETTER
5736 else if (*p ==
':') {
5749 p = strrdirsep(name, end, mb_enc, enc);
5754 while (isdirsep(*p)) {
5759 n = ntfs_tail(p, end, mb_enc, enc) - p;
5761 n = chompdirsep(p, end, mb_enc, enc) - p;
5763 for (q = p; q - p < n && *q ==
'.'; q++);
5764 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5765 if (*q ==
'.') e = q;
5785ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5787 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5831 const char *name, *p, *fp = 0;
5837 CheckPath(fname, name);
5841 check_path_encoding(fext);
5843 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5844 enc = rb_str_enc_get(fname);
5847 n = RSTRING_LEN(fname);
5848 if (n <= 0 || !*name) {
5849 return rb_enc_str_new(0, 0, enc);
5852 bool mb_enc = enc_mbclen_needed(enc);
5853 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5859 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5864 if (f == RSTRING_LEN(fname)) {
5869 return rb_enc_str_new(p, f, enc);
5872static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5895rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5901 return rb_file_dirname_n(argv[0], n);
5907 return rb_file_dirname_n(fname, 1);
5911rb_file_dirname_n(
VALUE fname,
int n)
5913 const char *name, *root, *p, *end;
5916 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5917 CheckPath(fname, name);
5918 end = name + RSTRING_LEN(fname);
5920 bool mb_enc = !rb_str_enc_fastpath(fname);
5923 root = skiproot(name, end);
5925 if (root > name + 1 && isdirsep(*name))
5926 root = skipprefix(name = root - 2, end, mb_enc, enc);
5928 if (root > name + 1)
5931 if (n > (end - root + 1) / 2) {
5937 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5946 return rb_enc_str_new(
".", 1, enc);
5948#ifdef DOSISH_DRIVE_LETTER
5949 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5950 const char *top = skiproot(name + 2, end);
5951 dirname = rb_enc_str_new(name, 3, enc);
5956 dirname = rb_enc_str_new(name, p - name, enc);
5957#ifdef DOSISH_DRIVE_LETTER
5958 if (root == name + 2 && p == root && name[1] ==
':')
5964static inline const char *
5965enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5967 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5969 p = strrdirsep(name, end, mb_enc, enc);
5973 do name = ++p;
while (isdirsep(*p));
5976 while (*p && *p ==
'.') p++;
5978 if (*p ==
'.' || istrailinggarbage(*p)) {
5980 const char *last = p++, *dot = last;
5981 while (istrailinggarbage(*p)) {
5982 if (*p ==
'.') dot = p;
5985 if (!*p || isADS(*p)) {
5989 if (*last ==
'.' || dot > last) e = dot;
5996 else if (isADS(*p)) {
6000 else if (isdirsep(*p))
6002 Inc(p, end, mb_enc, enc);
6007 if (!e || e == name)
6032 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
6079 CheckPath(fname, name);
6080 long len = RSTRING_LEN(fname);
6083 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
6086 bool mb_enc = !rb_str_enc_fastpath(fname);
6089 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
6090 return rb_enc_str_new(ext,
len, enc);
6114 return rb_get_path(fname);
6146file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6148 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6149 return rb_file_join_ary(arg);
6153rb_file_join_ary(
VALUE ary)
6157 const char *name, *tail;
6167 check_path_encoding(tmp);
6168 len += RSTRING_LEN(tmp);
6176 RBASIC_CLEAR_CLASS(result);
6179 switch (OBJ_BUILTIN_TYPE(tmp)) {
6181 if (!checked) check_path_encoding(tmp);
6186 rb_raise(rb_eArgError,
"recursive array");
6198 rb_enc_copy(result, tmp);
6201 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6202 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6205 else if (tail == name +
len) {
6209 enc = fs_enc_check(result, tmp);
6211 rb_enc_associate(result, enc);
6219rb_file_join_fastpath(
long argc,
VALUE *args)
6224 for (i = 0; i < argc; i++) {
6225 VALUE tmp = args[i];
6227 size += RSTRING_LEN(tmp);
6240 const char *name = RSTRING_PTR(result);
6241 for (i = 1; i < argc; i++) {
6242 VALUE tmp = args[i];
6243 long len = RSTRING_LEN(result);
6249 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6252 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6257 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6264 rb_enc_associate(result, new_enc);
6265 encidx = rb_enc_to_index(new_enc);
6271 rb_str_null_check(result);
6276rb_file_join(
long argc,
VALUE *args)
6278 if (RB_UNLIKELY(argc == 0)) {
6282 VALUE result = rb_file_join_fastpath(argc, args);
6283 if (RB_LIKELY(result)) {
6303rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6305 return rb_file_join(argc, argv);
6308#if defined(HAVE_TRUNCATE)
6309struct truncate_arg {
6315nogvl_truncate(
void *ptr)
6317 struct truncate_arg *ta = ptr;
6318 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6350 struct truncate_arg ta;
6355 path = rb_str_encode_ospath(path);
6358 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6360 rb_sys_fail_path(path);
6364#define rb_file_s_truncate rb_f_notimplement
6367#if defined(HAVE_FTRUNCATE)
6368struct ftruncate_arg {
6374nogvl_ftruncate(
void *ptr)
6376 struct ftruncate_arg *fa = ptr;
6378 return (
VALUE)ftruncate(fa->fd, fa->pos);
6419 struct ftruncate_arg fa;
6426 rb_io_flush_raw(obj, 0);
6428 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6429 rb_sys_fail_path(fptr->
pathv);
6434#define rb_file_truncate rb_f_notimplement
6451#include <winerror.h>
6455rb_thread_flock(
void *data)
6458 int old_errno =
errno;
6460 int *op = data, ret = flock(op[0], op[1]);
6463 if (GetLastError() == ERROR_NOT_LOCKED) {
6521 op[1] = op1 =
NUM2INT(operation);
6526 rb_io_flush_raw(obj, 0);
6528 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6533#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6536 if (op1 & LOCK_NB)
return Qfalse;
6539 time.tv_usec = 100 * 1000;
6545#if defined(ERESTART)
6551 rb_syserr_fail_path(e, fptr->
pathv);
6558test_check(
int n,
int argc,
VALUE *argv)
6564 for (i=1; i<n; i++) {
6571#define CHECK(n) test_check((n), argc, argv)
6663 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6667 return rb_file_blockdev_p(0, argv[1]);
6670 return rb_file_chardev_p(0, argv[1]);
6673 return rb_file_directory_p(0, argv[1]);
6676 return rb_file_exist_p(0, argv[1]);
6679 return rb_file_file_p(0, argv[1]);
6682 return rb_file_sgid_p(0, argv[1]);
6685 return rb_file_grpowned_p(0, argv[1]);
6688 return rb_file_sticky_p(0, argv[1]);
6691 return rb_file_symlink_p(0, argv[1]);
6694 return rb_file_owned_p(0, argv[1]);
6697 return rb_file_rowned_p(0, argv[1]);
6700 return rb_file_pipe_p(0, argv[1]);
6703 return rb_file_readable_p(0, argv[1]);
6706 return rb_file_readable_real_p(0, argv[1]);
6709 return rb_file_size_p(0, argv[1]);
6712 return rb_file_socket_p(0, argv[1]);
6715 return rb_file_suid_p(0, argv[1]);
6718 return rb_file_writable_p(0, argv[1]);
6721 return rb_file_writable_real_p(0, argv[1]);
6724 return rb_file_executable_p(0, argv[1]);
6727 return rb_file_executable_real_p(0, argv[1]);
6730 return rb_file_zero_p(0, argv[1]);
6734 if (strchr(
"MAC", cmd)) {
6736 VALUE fname = argv[1];
6739 if (
rb_stat(fname, &st) == -1) {
6742 rb_syserr_fail_path(e, fname);
6747 return stat_atime(&st);
6749 return stat_mtime(&st);
6751 return stat_ctime(&st);
6757 return rb_file_identical_p(0, argv[1], argv[2]);
6760 if (strchr(
"=<>", cmd)) {
6761 struct stat st1, st2;
6768 t1 = stat_mtimespec(&st1);
6769 t2 = stat_mtimespec(&st2);
6773 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6777 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6778 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6782 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6783 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6790 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6793 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6842rb_stat_s_alloc(
VALUE klass)
6871 fname = rb_str_encode_ospath(fname);
6873 rb_sys_fail_path(fname);
6880 rb_st->initialized =
true;
6897 *copy_rb_st = *orig_rb_st;
6939rb_stat_ftype(
VALUE obj)
6941 return rb_file_ftype(get_stat(obj)->ST_(mode));
6957 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6984 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
7021 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
7053 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
7075 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
7095 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7130rb_stat_owned(
VALUE obj)
7132 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7137rb_stat_rowned(
VALUE obj)
7139 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7160rb_stat_grpowned(
VALUE obj)
7163 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7192 rb_io_stat_data *st = get_stat(obj);
7195 if (geteuid() == 0)
return Qtrue;
7198 if (rb_stat_owned(obj))
7199 return RBOOL(st->ST_(mode) & S_IRUSR);
7202 if (rb_stat_grpowned(obj))
7203 return RBOOL(st->ST_(mode) & S_IRGRP);
7206 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7224 rb_io_stat_data *st = get_stat(obj);
7227 if (getuid() == 0)
return Qtrue;
7230 if (rb_stat_rowned(obj))
7231 return RBOOL(st->ST_(mode) & S_IRUSR);
7234 if (rb_group_member(get_stat(obj)->ST_(gid)))
7235 return RBOOL(st->ST_(mode) & S_IRGRP);
7238 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7266rb_stat_wr(
VALUE obj)
7269 rb_io_stat_data *st = get_stat(obj);
7270 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7271 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7303 rb_io_stat_data *st = get_stat(obj);
7306 if (geteuid() == 0)
return Qtrue;
7309 if (rb_stat_owned(obj))
7310 return RBOOL(st->ST_(mode) & S_IWUSR);
7313 if (rb_stat_grpowned(obj))
7314 return RBOOL(st->ST_(mode) & S_IWGRP);
7317 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7338 rb_io_stat_data *st = get_stat(obj);
7341 if (getuid() == 0)
return Qtrue;
7344 if (rb_stat_rowned(obj))
7345 return RBOOL(st->ST_(mode) & S_IWUSR);
7348 if (rb_group_member(get_stat(obj)->ST_(gid)))
7349 return RBOOL(st->ST_(mode) & S_IWGRP);
7352 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7381rb_stat_ww(
VALUE obj)
7384 rb_io_stat_data *st = get_stat(obj);
7385 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7386 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7421 rb_io_stat_data *st = get_stat(obj);
7424 if (geteuid() == 0) {
7425 return RBOOL(st->ST_(mode) & S_IXUGO);
7429 if (rb_stat_owned(obj))
7430 return RBOOL(st->ST_(mode) & S_IXUSR);
7433 if (rb_stat_grpowned(obj))
7434 return RBOOL(st->ST_(mode) & S_IXGRP);
7437 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7453 rb_io_stat_data *st = get_stat(obj);
7456 if (getuid() == 0) {
7457 return RBOOL(st->ST_(mode) & S_IXUGO);
7461 if (rb_stat_rowned(obj))
7462 return RBOOL(st->ST_(mode) & S_IXUSR);
7465 if (rb_group_member(get_stat(obj)->ST_(gid)))
7466 return RBOOL(st->ST_(mode) & S_IXGRP);
7469 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7492 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7537 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7569 rb_off_t size = get_stat(obj)->ST_(size);
7571 if (size == 0)
return Qnil;
7604rb_stat_suid(
VALUE obj)
7607 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7641rb_stat_sgid(
VALUE obj)
7644 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7678rb_stat_sticky(
VALUE obj)
7681 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7686#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7687#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7698nogvl_mkfifo(
void *ptr)
7700 struct mkfifo_arg *ma = ptr;
7702 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7730 struct mkfifo_arg ma;
7739 path = rb_str_encode_ospath(path);
7740 ma.path = RSTRING_PTR(path);
7741 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7742 rb_sys_fail_path(path);
7747#define rb_file_s_mkfifo rb_f_notimplement
7750static VALUE rb_mFConst;
7753rb_file_const(
const char *name,
VALUE value)
7755 rb_define_const(rb_mFConst, name, value);
7759rb_is_absolute_path(
const char *path)
7761#ifdef DOSISH_DRIVE_LETTER
7762 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7765 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7768 if (path[0] ==
'/')
return 1;
7774ruby_is_fd_loadable(
int fd)
7781 if (fstat(fd, &st) < 0)
7784 if (S_ISREG(st.st_mode))
7787 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7790 if (S_ISDIR(st.st_mode))
7801rb_file_load_ok(
const char *path)
7808 int mode = (O_RDONLY |
7809#if defined O_NONBLOCK
7811#elif defined O_NDELAY
7817 if (!rb_gc_for_fd(
errno))
return 0;
7819 if (fd < 0)
return 0;
7822 ret = ruby_is_fd_loadable(fd);
7829is_explicit_relative(
const char *path)
7831 if (*path++ !=
'.')
return 0;
7832 if (*path ==
'.') path++;
7833 return isdirsep(*path);
7839 int encidx = rb_enc_get_index(orig);
7840 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7841 encidx = rb_filesystem_encindex();
7842 rb_enc_associate_index(path, encidx);
7850nav_component_p(
const char *s,
const char *send)
7852 if ((send - s) >= 2 && s[0] ==
'.') {
7853 return s[1] ==
'.' || isdirsep(s[1]);
7859fname_need_expansion_p(
VALUE fname)
7861 const char *s = RSTRING_PTR(fname);
7862 const long len = RSTRING_LEN(fname);
7863 const char *send = s +
len;
7865 if (nav_component_p(s, send)) {
7870 bool mbenc = enc_mbclen_needed(enc);
7872 s = enc_path_next(s, send, mbenc, enc);
7874 if (nav_component_p(s, send)) {
7878 s = enc_path_next(s, send, mbenc, enc);
7884expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7886 long dname_len = RSTRING_LEN(dname);
7887 const char *dname_ptr = RSTRING_PTR(dname);
7891 if (need_expansion || dname_ptr[0] ==
'~') {
7892 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7897 if (!isdirsep(dname_ptr[dname_len - 1])) {
7906rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
7909 VALUE fname = *filep;
7913 if (!ext[0])
return 0;
7916 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7917 f = RSTRING_PTR(fname);
7922 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7923 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7924 fnlen = RSTRING_LEN(fname);
7925 for (i=0; ext[i]; i++) {
7927 if (rb_file_load_ok(RSTRING_PTR(fname))) {
7928 *filep = copy_path_class(fname, *filep);
7936 long expanded_load_path_maxlen;
7937 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7938 if (!load_path)
return 0;
7941 RBASIC_CLEAR_CLASS(fname);
7942 fnlen = RSTRING_LEN(fname);
7943 bool need_expansion = fname_need_expansion_p(fname);
7946 rb_enc_associate_index(tmp, rb_usascii_encindex());
7948 for (j=0; ext[j]; j++) {
7950 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
7952 if (!RSTRING_LEN(dname))
continue;
7953 expand_feature(fname, dname, tmp, need_expansion);
7955 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7956 *filep = copy_path_class(tmp, *filep);
7962 rb_str_resize(tmp, 0);
7975 path = copy_path_class(file_expand_path_1(path, 0), path);
7976 f = RSTRING_PTR(path);
7980 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7981 if (!rb_file_load_ok(f))
return 0;
7983 path = copy_path_class(file_expand_path_1(path, 0), path);
7987 long expanded_load_path_maxlen;
7988 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7991 bool need_expansion = fname_need_expansion_p(path);
7993 rb_enc_associate_index(tmp, rb_usascii_encindex());
7994 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
7996 if (!RSTRING_LEN(dname))
continue;
7997 expand_feature(path, dname, tmp, need_expansion);
7999 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8000 return copy_path_class(tmp, path);
8003 rb_str_resize(tmp, 0);
8011#define define_filetest_function(name, func, argc) do { \
8012 rb_define_module_function(rb_mFileTest, name, func, argc); \
8013 rb_define_singleton_method(rb_cFile, name, func, argc); \
8016const char ruby_null_device[] =
8819#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8820 rb_CFString_class_initialize_before_fork();
8828 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8829 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8830 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8831 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8832 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8833 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8834 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8835 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8836 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8837 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8838 define_filetest_function(
"file?", rb_file_file_p, 1);
8839 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8840 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8841 define_filetest_function(
"size?", rb_file_size_p, 1);
8842 define_filetest_function(
"size", rb_file_s_size, 1);
8843 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8844 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8846 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8847 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8848 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8850 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8851 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8853 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8854 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8855 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8857 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8895 separator = rb_fstring_lit(
"/");
8897 rb_define_const(
rb_cFile,
"Separator", separator);
8899 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9248 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9251 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9253 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9255 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9257 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9259 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9261 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9262#if defined(O_NDELAY) || defined(O_NONBLOCK)
9264# define O_NONBLOCK O_NDELAY
9267 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9270 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9273 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9279 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9280#ifndef O_SHARE_DELETE
9281# define O_SHARE_DELETE 0
9284 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9287 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9291 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9295 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9299 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9303 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9307 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9311 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9315 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9317 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9319 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9321 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9324 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.