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))
117#define STAT(p, s) stat((p), (s))
119#ifdef HAVE_STRUCT_STATX_STX_BTIME
120# define ST_(name) stx_ ## name
123# define ST_(name) st_ ## name
127#if defined _WIN32 || defined __APPLE__
129# define TO_OSPATH(str) rb_str_encode_ospath(str)
132# define TO_OSPATH(str) (str)
136#if defined DOSISH || defined __CYGWIN__
141#if defined HAVE_REALPATH && defined __sun && defined __SVR4
154#include "internal/compilers.h"
155#include "internal/dir.h"
156#include "internal/encoding.h"
157#include "internal/error.h"
158#include "internal/file.h"
159#include "internal/io.h"
160#include "internal/load.h"
161#include "internal/object.h"
162#include "internal/process.h"
163#include "internal/thread.h"
164#include "internal/vm.h"
169#define UIANY2NUM(x) \
170 ((sizeof(x) <= sizeof(unsigned int)) ? \
171 UINT2NUM((unsigned)(x)) : \
172 (sizeof(x) <= sizeof(unsigned long)) ? \
173 ULONG2NUM((unsigned long)(x)) : \
174 ULL2NUM((unsigned LONG_LONG)(x)))
181file_path_convert(
VALUE name)
186 if (ENCINDEX_US_ASCII != fname_encidx &&
187 ENCINDEX_ASCII_8BIT != fname_encidx &&
188 (fs_encidx = rb_filesystem_encindex()) != fname_encidx &&
189 rb_default_internal_encoding() &&
193 rb_encoding *fname_encoding = rb_enc_from_index(fname_encidx);
194 rb_encoding *fs_encoding = rb_enc_from_index(fs_encidx);
202check_path_encoding(
VALUE str)
204 if (RB_UNLIKELY(!rb_str_enc_fastpath(str))) {
206 if (!rb_enc_asciicompat(enc)) {
214rb_get_path_check_to_string(
VALUE obj)
223 tmp = rb_check_funcall_default(obj, to_path, 0, 0, obj);
229rb_get_path_check_convert(
VALUE obj)
231 obj = file_path_convert(obj);
232 rb_get_path_check_no_convert(obj);
238rb_get_path_check_no_convert(
VALUE obj)
240 check_path_encoding(obj);
241 if (!rb_str_to_cstr(obj)) {
242 rb_raise(rb_eArgError,
"path name contains null byte");
251 return rb_get_path(obj);
257 return rb_get_path_check_convert(rb_get_path_check_to_string(obj));
261check_path(
VALUE obj,
const char **cstr)
263 VALUE str = rb_get_path_check_convert(rb_get_path_check_to_string(obj));
267 *cstr = RSTRING_PTR(str);
271#define CheckPath(str, cstr) RB_GC_GUARD(str) = check_path(str, &cstr);
278 if (encidx != ENCINDEX_ASCII_8BIT && encidx != ENCINDEX_UTF_8) {
288# define NORMALIZE_UTF8PATH 1
290# ifdef HAVE_WORKING_FORK
291static CFMutableStringRef
292mutable_CFString_new(CFStringRef *s,
const char *ptr,
long len)
294 const CFAllocatorRef alloc = kCFAllocatorDefault;
295 *s = CFStringCreateWithBytesNoCopy(alloc, (
const UInt8 *)ptr,
len,
296 kCFStringEncodingUTF8, FALSE,
298 return CFStringCreateMutableCopy(alloc,
len, *s);
301# define mutable_CFString_release(m, s) (CFRelease(m), CFRelease(s))
304rb_CFString_class_initialize_before_fork(
void)
327 const char small_str[] =
"/";
328 long len =
sizeof(small_str) - 1;
336 for (
int i = 0; i < 2; i++) {
337 CFMutableStringRef m = mutable_CFString_new(&s, small_str,
len);
338 mutable_CFString_release(m, s);
344rb_str_append_normalized_ospath(
VALUE str,
const char *ptr,
long len)
349 CFMutableStringRef m = mutable_CFString_new(&s, ptr,
len);
350 long oldlen = RSTRING_LEN(str);
352 CFStringNormalize(m, kCFStringNormalizationFormC);
353 all = CFRangeMake(0, CFStringGetLength(m));
354 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE, NULL, 0, &buflen);
356 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE,
357 (UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
359 mutable_CFString_release(m, s);
364rb_str_normalize_ospath(
const char *ptr,
long len)
367 const char *e = ptr +
len;
376 rb_enc_associate(str, enc);
381 int r = rb_enc_precise_mbclen(p, e, enc);
385 rb_str_append_normalized_ospath(str, p1, p-p1);
392 c = rb_enc_mbc_to_codepoint(p, e, enc);
393 if ((0x2000 <= c && c <= 0x2FFF) || (0xF900 <= c && c <= 0xFAFF) ||
394 (0x2F800 <= c && c <= 0x2FAFF)) {
396 rb_str_append_normalized_ospath(str, p1, p-p1);
407 rb_str_append_normalized_ospath(str, p1, p-p1);
414ignored_char_p(
const char *p,
const char *e,
rb_encoding *enc)
417 if (p+3 > e)
return 0;
418 switch ((
unsigned char)*p) {
420 switch ((
unsigned char)p[1]) {
422 c = (
unsigned char)p[2];
424 if (c >= 0x8c && c <= 0x8f)
return 3;
426 if (c >= 0xaa && c <= 0xae)
return 3;
429 c = (
unsigned char)p[2];
431 if (c >= 0xaa && c <= 0xaf)
return 3;
437 if ((
unsigned char)p[1] == 0xbb &&
438 (
unsigned char)p[2] == 0xbf)
445# define NORMALIZE_UTF8PATH 0
448#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
459 int (*func)(
const char *,
void *);
465no_gvl_apply2files(
void *ptr)
469 for (aa->i = 0; aa->i < aa->argc; aa->i++) {
470 if (aa->func(aa->fn[aa->i].ptr, aa->arg) < 0) {
479NORETURN(
static void utime_failed(
struct apply_arg *));
480static int utime_internal(
const char *,
void *);
484apply2files(
int (*func)(
const char *,
void *),
int argc,
VALUE *argv,
void *arg)
488 const long len = (long)(offsetof(
struct apply_arg, fn) + (size * argc));
496 for (aa->i = 0; aa->i < argc; aa->i++) {
497 VALUE path = rb_get_path(argv[aa->i]);
499 path = rb_str_encode_ospath(path);
500 aa->fn[aa->i].ptr = RSTRING_PTR(path);
501 aa->fn[aa->i].path = path;
504 IO_WITHOUT_GVL(no_gvl_apply2files, aa);
507 if (func == utime_internal) {
511 rb_syserr_fail_path(aa->errnum, aa->fn[aa->i].path);
530 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
534 rb_io_stat_data stat;
547rb_stat_new(
const struct stat *st)
553# define CP(m) .stx_ ## m = st->st_ ## m
554# define CP_32(m) .stx_ ## m = (uint32_t)st->st_ ## m
555# define CP_TS(m) .stx_ ## m = stat_ ## m ## spec(st)
556 rb_st->stat = (
struct statx){
557 .stx_mask = STATX_BASIC_STATS,
574 rb_st->initialized =
true;
582rb_statx_new(
const rb_io_stat_data *st)
588 rb_st->initialized =
true;
594static rb_io_stat_data*
599 if (!rb_st->initialized) rb_raise(
rb_eTypeError,
"uninitialized File::Stat");
605statx_mtimespec(
const rb_io_stat_data *st)
607 return st->stx_mtime;
610# define statx_mtimespec stat_mtimespec
648 if (ts1.tv_sec == ts2.tv_sec) {
649 if (ts1.tv_nsec == ts2.tv_nsec)
return INT2FIX(0);
650 if (ts1.tv_nsec < ts2.tv_nsec)
return INT2FIX(-1);
653 if (ts1.tv_sec < ts2.tv_sec)
return INT2FIX(-1);
659#define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
662# define NUM2DEVT(v) NUM2UINT(v)
665# define DEVT2NUM(v) UINT2NUM(v)
667#ifndef PRI_DEVT_PREFIX
668# define PRI_DEVT_PREFIX ""
682rb_stat_dev(
VALUE self)
685 unsigned int m = get_stat(self)->stx_dev_major;
686 unsigned int n = get_stat(self)->stx_dev_minor;
688#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
689 return DEVT2NUM(get_stat(self)->st_dev);
690#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
691 return ULONG2NUM(get_stat(self)->st_dev);
693 return ULL2NUM(get_stat(self)->st_dev);
708rb_stat_dev_major(
VALUE self)
711 return UINT2NUM(get_stat(self)->stx_dev_major);
713 return UINT2NUM(major(get_stat(self)->st_dev));
730rb_stat_dev_minor(
VALUE self)
733 return UINT2NUM(get_stat(self)->stx_dev_minor);
735 return UINT2NUM(minor(get_stat(self)->st_dev));
752rb_stat_ino(
VALUE self)
754 rb_io_stat_data *ptr = get_stat(self);
755#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
757 return rb_integer_unpack(&ptr->st_ino, 2,
758 SIZEOF_STRUCT_STAT_ST_INO, 0,
762 return UIANY2NUM(ptr->ST_(ino));
780rb_stat_mode(
VALUE self)
782 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
798rb_stat_nlink(
VALUE self)
801 const rb_io_stat_data *ptr = get_stat(self);
803 return UIANY2NUM(ptr->ST_(nlink));
817rb_stat_uid(
VALUE self)
819 return UIDT2NUM(get_stat(self)->ST_(uid));
833rb_stat_gid(
VALUE self)
835 return GIDT2NUM(get_stat(self)->ST_(gid));
851rb_stat_rdev(
VALUE self)
854 unsigned int m = get_stat(self)->stx_rdev_major;
855 unsigned int n = get_stat(self)->stx_rdev_minor;
857#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
859#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
860 return DEVT2NUM(get_stat(self)->ST_(rdev));
861#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
862 return ULONG2NUM(get_stat(self)->ST_(rdev));
864 return ULL2NUM(get_stat(self)->ST_(rdev));
879rb_stat_rdev_major(
VALUE self)
882 return UINT2NUM(get_stat(self)->stx_rdev_major);
883#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
884 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
901rb_stat_rdev_minor(
VALUE self)
904 return UINT2NUM(get_stat(self)->stx_rdev_minor);
905#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
906 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
942rb_stat_size(
VALUE self)
944 return OFFT2NUM(get_stat(self)->ST_(size));
959rb_stat_blksize(
VALUE self)
961#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
962 return ULONG2NUM(get_stat(self)->ST_(blksize));
980rb_stat_blocks(
VALUE self)
982#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
983# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
984 return ULL2NUM(get_stat(self)->ST_(blocks));
986 return ULONG2NUM(get_stat(self)->ST_(blocks));
994stat_atimespec(
const struct stat *st)
997 ts.tv_sec = st->st_atime;
998#if defined(HAVE_STRUCT_STAT_ST_ATIM)
999 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
1000#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1001 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
1002#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
1003 ts.tv_nsec = (uint32_t)st->st_atimensec;
1012statx_atimespec(
const rb_io_stat_data *st)
1014 return st->stx_atime;
1017# define statx_atimespec stat_atimespec
1027stat_atime(
const struct stat *st)
1029 return stat_time(stat_atimespec(st));
1033stat_mtimespec(
const struct stat *st)
1036 ts.tv_sec = st->st_mtime;
1037#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1038 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1039#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1040 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1041#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1042 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1050stat_mtime(
const struct stat *st)
1052 return stat_time(stat_mtimespec(st));
1056stat_ctimespec(
const struct stat *st)
1059 ts.tv_sec = st->st_ctime;
1060#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1061 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1062#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1063 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1064#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1065 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1074statx_ctimespec(
const rb_io_stat_data *st)
1076 return st->stx_ctime;
1079# define statx_ctimespec stat_ctimespec
1083stat_ctime(
const struct stat *st)
1085 return stat_time(stat_ctimespec(st));
1088#define HAVE_STAT_BIRTHTIME
1089#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1091statx_birthtime(
const rb_io_stat_data *st)
1096#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1097static VALUE statx_birthtime(
const rb_io_stat_data *st);
1098#elif defined(_WIN32)
1099# define statx_birthtime stat_ctime
1101# undef HAVE_STAT_BIRTHTIME
1148rb_stat_atime(
VALUE self)
1150 return stat_time(statx_atimespec(get_stat(self)));
1181rb_stat_mtime(
VALUE self)
1183 return stat_time(statx_mtimespec(get_stat(self)));
1201rb_stat_ctime(
VALUE self)
1203 return stat_time(statx_ctimespec(get_stat(self)));
1206#if defined(HAVE_STAT_BIRTHTIME)
1228rb_stat_birthtime(
VALUE self)
1230 return statx_birthtime(get_stat(self));
1233# define rb_stat_birthtime rb_f_notimplement
1252rb_stat_inspect(
VALUE self)
1256 static const struct {
1260 {
"dev", rb_stat_dev},
1261 {
"ino", rb_stat_ino},
1262 {
"mode", rb_stat_mode},
1263 {
"nlink", rb_stat_nlink},
1264 {
"uid", rb_stat_uid},
1265 {
"gid", rb_stat_gid},
1266 {
"rdev", rb_stat_rdev},
1267 {
"size", rb_stat_size},
1268 {
"blksize", rb_stat_blksize},
1269 {
"blocks", rb_stat_blocks},
1270 {
"atime", rb_stat_atime},
1271 {
"mtime", rb_stat_mtime},
1272 {
"ctime", rb_stat_ctime},
1273#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1274 {
"birthtime", rb_stat_birthtime},
1280 if (!rb_st->initialized) {
1288 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1296 v = (*member[i].func)(self);
1298 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1300 else if (i == 0 || i == 6) {
1301 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1321no_gvl_fstat(
void *data)
1324 return (
VALUE)fstat(arg->file.fd, arg->st);
1328fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1332 data.file.fd = fptr->
fd;
1335 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1339no_gvl_stat(
void * data)
1342 return (
void *)(
VALUE)STAT(arg->file.path, arg->st);
1346stat_without_gvl(
const char *path,
struct stat *st)
1350 data.file.path = path;
1353 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1356#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1357 defined(HAVE_STRUCT_STATX_STX_BTIME)
1359# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1362# ifdef HAVE_SYSCALL_H
1363# include <syscall.h>
1364# elif defined HAVE_SYS_SYSCALL_H
1365# include <sys/syscall.h>
1367# if defined __linux__
1368# include <linux/stat.h>
1370statx(
int dirfd,
const char *pathname,
int flags,
1371 unsigned int mask,
struct statx *statxbuf)
1373 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1378typedef struct no_gvl_rb_io_stat_data {
1384} no_gvl_rb_io_stat_data;
1387io_blocking_statx(
void *data)
1389 no_gvl_rb_io_stat_data *arg = data;
1390 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1394no_gvl_statx(
void *data)
1396 return (
void *)io_blocking_statx(data);
1400statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1402 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1405 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1409lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1411 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1414 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1418fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1420 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1423 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1426#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1429rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1434 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1439 result = fstatx_without_gvl(fptr, stx, mask);
1444 file = rb_str_encode_ospath(file);
1445 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1451# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1453NORETURN(
static void statx_notimplement(
const char *field_name));
1458statx_notimplement(
const char *field_name)
1461 "%s is unimplemented on this filesystem",
1466statx_birthtime(
const rb_io_stat_data *stx)
1468 if (!statx_has_birthtime(stx)) {
1470 statx_notimplement(
"birthtime");
1472 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1477# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1478# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1479# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1480# define rb_statx(file, stx, mask) rb_stat(file, stx)
1481# define STATX(path, st, mask) STAT(path, st)
1483#if defined(HAVE_STAT_BIRTHTIME)
1484# define statx_has_birthtime(st) 1
1486# define statx_has_birthtime(st) 0
1493# define FSTAT(fd, st) fstat(fd, st)
1502 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1507 result = fstat_without_gvl(fptr, st);
1512 file = rb_str_encode_ospath(file);
1513 result = stat_without_gvl(RSTRING_PTR(file), st);
1551 fname = rb_str_encode_ospath(fname);
1552 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1553 rb_sys_fail_path(fname);
1555 return rb_statx_new(&st);
1587rb_io_stat(
VALUE obj)
1593 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1594 rb_sys_fail_path(fptr->
pathv);
1596 return rb_statx_new(&st);
1601no_gvl_lstat(
void *ptr)
1604 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1608lstat_without_gvl(
const char *path,
struct stat *st)
1612 data.file.path = path;
1615 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1652 fname = rb_str_encode_ospath(fname);
1653 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1654 rb_sys_fail_path(fname);
1656 return rb_statx_new(&st);
1658 return rb_file_s_stat(klass, fname);
1691rb_file_lstat(
VALUE obj)
1700 path = rb_str_encode_ospath(fptr->
pathv);
1701 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1702 rb_sys_fail_path(fptr->
pathv);
1704 return rb_statx_new(&st);
1706 return rb_io_stat(obj);
1711rb_group_member(GETGROUPS_T gid)
1713#if !defined(HAVE_GETGROUPS)
1722 if (getgid() == gid || getegid() == gid)
1725 groups = getgroups(0, NULL);
1726 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1727 anum = getgroups(groups, gary);
1728 while (--anum >= 0) {
1729 if (gary[anum] == gid) {
1742# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1745#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1746#define USE_GETEUID 1
1751eaccess(
const char *path,
int mode)
1760 if (getuid() == euid && getgid() == getegid())
1761 return access(path, mode);
1763 if (STAT(path, &st) < 0)
1773 if (st.st_mode & S_IXUGO)
1779 if (st.st_uid == euid)
1781 else if (rb_group_member(st.st_gid))
1784 if ((
int)(st.st_mode & mode) == mode)
return 0;
1788 return access(path, mode);
1799nogvl_eaccess(
void *ptr)
1803 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1807rb_eaccess(
VALUE fname,
int mode)
1812 fname = rb_str_encode_ospath(fname);
1816 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1820nogvl_access(
void *ptr)
1824 return (
void *)(
VALUE)access(aa->path, aa->mode);
1828rb_access(
VALUE fname,
int mode)
1833 fname = rb_str_encode_ospath(fname);
1837 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1880# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1886 if (S_ISDIR(st.st_mode))
return Qtrue;
1915# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1921 if (S_ISFIFO(st.st_mode))
return Qtrue;
1953# define S_ISLNK(m) _S_ISLNK(m)
1956# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1959# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1969 fname = rb_str_encode_ospath(fname);
1971 if (S_ISLNK(st.st_mode))
return Qtrue;
2006# define S_ISSOCK(m) _S_ISSOCK(m)
2009# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
2012# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
2022 if (S_ISSOCK(st.st_mode))
return Qtrue;
2050# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
2052# define S_ISBLK(m) (0)
2060 if (S_ISBLK(st.st_mode))
return Qtrue;
2087# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2093 if (S_ISCHR(st.st_mode))
return Qtrue;
2163 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2177rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2179 return RBOOL(rb_access(fname, R_OK) >= 0);
2183# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2187# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2216rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2222 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2223 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2245 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2262rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2264 return RBOOL(rb_access(fname, W_OK) >= 0);
2293rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2299 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2300 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2343 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2362rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2364 return RBOOL(rb_access(fname, X_OK) >= 0);
2368# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2400 return RBOOL(S_ISREG(st.st_mode));
2460 return RBOOL(st.st_size == 0);
2498 if (st.st_size == 0)
return Qnil;
2532 return RBOOL(st.st_uid == geteuid());
2541 return RBOOL(st.st_uid == getuid());
2573 if (rb_group_member(st.st_gid))
return Qtrue;
2578#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2580check3rdbyte(
VALUE fname,
int mode)
2585 return RBOOL(st.st_mode & mode);
2618 return check3rdbyte(fname, S_ISUID);
2653 return check3rdbyte(fname, S_ISGID);
2689 return check3rdbyte(fname, S_ISVTX);
2726 struct stat st1, st2;
2730 if (st1.st_dev != st2.st_dev)
return Qfalse;
2731 if (st1.st_ino != st2.st_ino)
return Qfalse;
2735 return rb_w32_file_identical_p(fname1, fname2);
2761 if (
rb_stat(fname, &st) < 0) {
2764 rb_syserr_fail_path(e, fname);
2770rb_file_ftype(mode_t mode)
2774 if (S_ISREG(mode)) {
2777 else if (S_ISDIR(mode)) {
2780 else if (S_ISCHR(mode)) {
2781 t =
"characterSpecial";
2784 else if (S_ISBLK(mode)) {
2789 else if (S_ISFIFO(mode)) {
2794 else if (S_ISLNK(mode)) {
2799 else if (S_ISSOCK(mode)) {
2807 return rb_fstring_cstr(t);
2849 fname = rb_str_encode_ospath(fname);
2851 rb_sys_fail_path(fname);
2854 return rb_file_ftype(st.st_mode);
2902 if (
rb_stat(fname, &st) < 0) {
2905 rb_syserr_fail_path(e, fname);
2907 return stat_time(stat_atimespec(&st));
2934rb_file_atime(
VALUE obj)
2940 if (fstat(fptr->
fd, &st) == -1) {
2941 rb_sys_fail_path(fptr->
pathv);
2943 return stat_time(stat_atimespec(&st));
2983 if (
rb_stat(fname, &st) < 0) {
2986 rb_syserr_fail_path(e, fname);
2988 return stat_time(stat_mtimespec(&st));
3013rb_file_mtime(
VALUE obj)
3019 if (fstat(fptr->
fd, &st) == -1) {
3020 rb_sys_fail_path(fptr->
pathv);
3022 return stat_time(stat_mtimespec(&st));
3065 if (
rb_stat(fname, &st) < 0) {
3068 rb_syserr_fail_path(e, fname);
3070 return stat_time(stat_ctimespec(&st));
3087rb_file_ctime(
VALUE obj)
3093 if (fstat(fptr->
fd, &st) == -1) {
3094 rb_sys_fail_path(fptr->
pathv);
3096 return stat_time(stat_ctimespec(&st));
3099#if defined(HAVE_STAT_BIRTHTIME)
3130 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3133 rb_syserr_fail_path(e, fname);
3135 return statx_birthtime(&st);
3138# define rb_file_s_birthtime rb_f_notimplement
3141#if defined(HAVE_STAT_BIRTHTIME)
3162rb_file_birthtime(
VALUE obj)
3168 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3169 rb_sys_fail_path(fptr->
pathv);
3171 return statx_birthtime(&st);
3174# define rb_file_birthtime rb_f_notimplement
3186 rb_io_flush_raw(file, 0);
3189 if (fstat(fptr->
fd, &st) == -1) {
3190 rb_sys_fail_path(fptr->
pathv);
3216file_size(
VALUE self)
3218 return OFFT2NUM(rb_file_size(self));
3227nogvl_chmod(
void *ptr)
3230 int ret = chmod(data->path, data->mode);
3231 return (
void *)(
VALUE)ret;
3235rb_chmod(
const char *path, mode_t mode)
3241 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3245chmod_internal(
const char *path,
void *mode)
3247 return chmod(path, *(mode_t *)mode);
3285 return apply2files(chmod_internal, argc, argv, &mode);
3289struct nogvl_fchmod_data {
3295io_blocking_fchmod(
void *ptr)
3297 struct nogvl_fchmod_data *data = ptr;
3298 int ret = fchmod(data->fd, data->mode);
3303rb_fchmod(
struct rb_io* io, mode_t mode)
3306 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3307 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3339#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3347 if (rb_fchmod(fptr, mode) == -1) {
3348 if (HAVE_FCHMOD ||
errno != ENOSYS)
3349 rb_sys_fail_path(fptr->
pathv);
3352 if (!HAVE_FCHMOD)
return INT2FIX(0);
3355#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3357 path = rb_str_encode_ospath(fptr->
pathv);
3358 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3359 rb_sys_fail_path(fptr->
pathv);
3365#if defined(HAVE_LCHMOD)
3367lchmod_internal(
const char *path,
void *mode)
3369 return lchmod(path, *(mode_t *)mode);
3406 return apply2files(lchmod_internal, argc, argv, &mode);
3409#define rb_file_s_lchmod rb_f_notimplement
3412static inline rb_uid_t
3416 return (rb_uid_t)-1;
3421static inline rb_gid_t
3425 return (rb_gid_t)-1;
3436chown_internal(
const char *path,
void *arg)
3439 return chown(path, args->owner, args->group);
3493 arg.owner = to_uid(*argv++);
3494 arg.group = to_gid(*argv++);
3496 return apply2files(chown_internal, argc, argv, &arg);
3508nogvl_chown(
void *ptr)
3511 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3515rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3518 .as = {.path = path},
3519 .new = {.owner = owner, .group = group},
3521 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3526nogvl_fchown(
void *ptr)
3529 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3533rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3538 .new = {.owner = owner, .group = group},
3540 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3574 path = rb_str_encode_ospath(fptr->
pathv);
3575 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3576 rb_sys_fail_path(fptr->
pathv);
3578 if (rb_fchown(fptr->
fd, o, g) == -1)
3579 rb_sys_fail_path(fptr->
pathv);
3585#if defined(HAVE_LCHOWN)
3587lchown_internal(
const char *path,
void *arg)
3590 return lchown(path, args->owner, args->group);
3647 arg.owner = to_uid(*argv++);
3648 arg.group = to_gid(*argv++);
3650 return apply2files(lchown_internal, argc, argv, &arg);
3653#define rb_file_s_lchown rb_f_notimplement
3663NORETURN(
static void utime_failed(
struct apply_arg *));
3669 VALUE path = aa->fn[aa->i].path;
3672 if (ua->tsp && e == EINVAL) {
3675 VALUE atime = ua->atime;
3676 VALUE mtime = ua->mtime;
3678 if (!
NIL_P(atime)) {
3681 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3684 if (
NIL_P(a)) e[0] = m;
3700 rb_syserr_fail_path(e, path);
3704#if defined(HAVE_UTIMES)
3706# if !defined(HAVE_UTIMENSAT)
3708# elif defined(__APPLE__) && \
3709 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3711# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3712typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3716static inline utimensat_func *
3723# define utimensat rb_utimensat()
3730utime_internal(
const char *path,
void *arg)
3733 const struct timespec *tsp = v->tsp;
3734 struct timeval tvbuf[2], *tvp = NULL;
3736#if defined(HAVE_UTIMENSAT)
3737# if defined(__APPLE__)
3738 const int try_utimensat = utimensat != NULL;
3739 const int try_utimensat_follow = utimensat != NULL;
3741# define TRY_UTIMENSAT 1
3742 static int try_utimensat = 1;
3743# ifdef AT_SYMLINK_NOFOLLOW
3744 static int try_utimensat_follow = 1;
3746 const int try_utimensat_follow = 0;
3751 if (v->follow ? try_utimensat_follow : try_utimensat) {
3752# ifdef AT_SYMLINK_NOFOLLOW
3754 flags = AT_SYMLINK_NOFOLLOW;
3758 int result = utimensat(AT_FDCWD, path, tsp, flags);
3759# ifdef TRY_UTIMENSAT
3760 if (result < 0 &&
errno == ENOSYS) {
3761# ifdef AT_SYMLINK_NOFOLLOW
3762 try_utimensat_follow = 0;
3774 tvbuf[0].tv_sec = tsp[0].tv_sec;
3775 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3776 tvbuf[1].tv_sec = tsp[1].tv_sec;
3777 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3781 if (v->follow)
return lutimes(path, tvp);
3783 return utimes(path, tvp);
3788#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3796utime_internal(
const char *path,
void *arg)
3800 struct utimbuf utbuf, *utp = NULL;
3802 utbuf.actime = tsp[0].tv_sec;
3803 utbuf.modtime = tsp[1].tv_sec;
3806 return utime(path, utp);
3811utime_internal_i(
int argc,
VALUE *argv,
int follow)
3814 struct timespec tss[2], *tsp = NULL;
3817 args.atime = *argv++;
3818 args.mtime = *argv++;
3820 args.follow = follow;
3822 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3825 if (args.atime == args.mtime)
3832 return apply2files(utime_internal, argc, argv, &args);
3849 return utime_internal_i(argc, argv, FALSE);
3852#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3910 return utime_internal_i(argc, argv, TRUE);
3913#define rb_file_s_lutime rb_f_notimplement
3916#ifdef RUBY_FUNCTION_NAME_STRING
3917# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3919# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3921#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3922NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3924syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3928 const int max_pathlen = MAX_PATH;
3930 const int max_pathlen = MAXPATHLEN;
3941#ifdef RUBY_FUNCTION_NAME_STRING
3942 rb_syserr_fail_path_in(func, e, str);
3944 rb_syserr_fail_path(e, str);
3979 from = rb_str_encode_ospath(from);
3980 to = rb_str_encode_ospath(to);
3983 sys_fail2(from, to);
3988#define rb_file_s_link rb_f_notimplement
4029 from = rb_str_encode_ospath(from);
4030 to = rb_str_encode_ospath(to);
4033 sys_fail2(from, to);
4038#define rb_file_s_symlink rb_f_notimplement
4066 return rb_readlink(path, rb_filesystem_encoding());
4069struct readlink_arg {
4076nogvl_readlink(
void *ptr)
4078 struct readlink_arg *ra = ptr;
4080 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4084readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4086 struct readlink_arg ra;
4088 ra.path = RSTRING_PTR(path);
4089 ra.buf = RSTRING_PTR(buf);
4092 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4103 path = rb_str_encode_ospath(path);
4104 v = rb_enc_str_new(0, size, enc);
4105 while ((rv = readlink_without_gvl(path, v, size)) == size
4107 || (rv < 0 &&
errno == ERANGE)
4116 rb_str_resize(v, 0);
4117 rb_syserr_fail_path(e, path);
4119 rb_str_resize(v, rv);
4124#define rb_file_s_readlink rb_f_notimplement
4128unlink_internal(
const char *path,
void *arg)
4130 return unlink(path);
4173rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4175 return apply2files(unlink_internal, argc, argv, 0);
4184no_gvl_rename(
void *ptr)
4188 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4266 f = rb_str_encode_ospath(from);
4267 t = rb_str_encode_ospath(to);
4270#if defined __CYGWIN__
4273 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4278 if (chmod(ra.dst, 0666) == 0 &&
4279 unlink(ra.dst) == 0 &&
4280 rename(ra.src, ra.dst) == 0)
4284 syserr_fail2(e, from, to);
4318 rb_error_arity(argc, 0, 1);
4326#if defined __CYGWIN__ || defined DOSISH
4328#define DOSISH_DRIVE_LETTER
4329#define FILE_ALT_SEPARATOR '\\'
4331#ifdef FILE_ALT_SEPARATOR
4332#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4334static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4337#define isdirsep(x) ((x) == '/')
4349#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4350#define isADS(x) ((x) == ':')
4352#define istrailinggarbage(x) 0
4355#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4357#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4358#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4360#if defined(DOSISH_UNC)
4361#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4363#define has_unc(buf) 0
4366#ifdef DOSISH_DRIVE_LETTER
4368has_drive_letter(
const char *buf)
4370 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4395 if (chdir(drive) == 0) {
4396 drvcwd = rb_dir_getwd_ospath();
4408not_same_drive(
VALUE path,
int drive)
4410 const char *p = RSTRING_PTR(path);
4411 if (RSTRING_LEN(path) < 2)
return 0;
4412 if (has_drive_letter(p)) {
4423skiproot(
const char *path,
const char *end)
4425#ifdef DOSISH_DRIVE_LETTER
4426 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4428 while (path < end && isdirsep(*path)) path++;
4429 return (
char *)path;
4433enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4435 while (s < e && !isdirsep(*s)) {
4436 Inc(s, e, mb_enc, enc);
4441#define nextdirsep rb_enc_path_next
4445 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4448#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4449#define skipprefix enc_path_skip_prefix
4451#define skipprefix(path, end, mb_enc, enc) (path)
4454enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4456#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4458 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4460 while (path < end && isdirsep(*path)) path++;
4461 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4462 path + 2 <= end && !isdirsep(path[1])) {
4463 path = enc_path_next(path + 1, end, mb_enc, enc);
4465 return (
char *)path;
4468#ifdef DOSISH_DRIVE_LETTER
4469 if (path + 2 <= end && has_drive_letter(path))
4470 return (
char *)(path + 2);
4473 return (
char *)path;
4477rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4479 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4483skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4485#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4486 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4487 while (p < end && isdirsep(*p)) p++;
4490 return skiproot(path, end);
4495rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4497 return skipprefixroot(path, end, enc);
4501enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4504 while (path < end) {
4505 if (isdirsep(*path)) {
4506 const char *tmp = path++;
4507 while (path < end && isdirsep(*path)) path++;
4508 if (path >= end)
break;
4512 Inc(path, end, mb_enc, enc);
4518rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4520 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4524strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4526 if (RB_UNLIKELY(mb_enc)) {
4527 return enc_path_last_separator(path, end, mb_enc, enc);
4530 const char *cursor = end - 1;
4532 while (cursor >= path && isdirsep(cursor[0])) {
4536 while (cursor >= path) {
4537 if (isdirsep(cursor[0])) {
4538 while (cursor > path && isdirsep(cursor[-1])) {
4541 return (
char *)cursor;
4549chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4551 while (path < end) {
4552 if (isdirsep(*path)) {
4553 const char *last = path++;
4554 while (path < end && isdirsep(*path)) path++;
4555 if (path >= end)
return (
char *)last;
4558 Inc(path, end, mb_enc, enc);
4561 return (
char *)path;
4567 if (path < end && isdirsep(*path)) path++;
4568 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4574 rb_encoding *enc = rb_enc_check_str(path1, path2);
4575 int encidx = rb_enc_to_index(enc);
4576 if (encidx == ENCINDEX_US_ASCII) {
4577 encidx = rb_enc_get_index(path1);
4578 if (encidx == ENCINDEX_US_ASCII)
4579 encidx = rb_enc_get_index(path2);
4580 enc = rb_enc_from_index(encidx);
4587ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4589 while (path < end && *path ==
'.') path++;
4590 while (path < end && !isADS(*path)) {
4591 if (istrailinggarbage(*path)) {
4592 const char *last = path++;
4593 while (path < end && istrailinggarbage(*path)) path++;
4594 if (path >= end || isADS(*path))
return (
char *)last;
4596 else if (isdirsep(*path)) {
4597 const char *last = path++;
4598 while (path < end && isdirsep(*path)) path++;
4599 if (path >= end)
return (
char *)last;
4600 if (isADS(*path)) path++;
4603 Inc(path, end, mb_enc, enc);
4606 return (
char *)path;
4610#define BUFCHECK(cond) do {\
4613 do {buflen *= 2;} while (cond);\
4614 rb_str_resize(result, buflen);\
4615 buf = RSTRING_PTR(result);\
4617 pend = buf + buflen;\
4621#define BUFINIT(result, buf, p, pend) do {\
4622 if (!result) { result = rb_usascii_str_new(0, 1); } \
4623 p = buf = RSTRING_PTR(result); \
4624 buflen = RSTRING_LEN(result); \
4625 pend = p + buflen; \
4629# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4631# define SKIPPATHSEP(p) 1
4634#define BUFCOPY(srcptr, srclen) do { \
4635 const int skip = SKIPPATHSEP(p); \
4636 rb_str_set_len(result, p-buf+skip); \
4637 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4639 memcpy(p, (srcptr), (srclen)); \
4643#define WITH_ROOTDIFF(stmt) do { \
4644 long rootdiff = root - buf; \
4646 root = buf + rootdiff; \
4650copy_home_path(
VALUE result,
const char *dir)
4656 dirlen = strlen(dir);
4657 rb_str_resize(result, dirlen);
4658 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4659 encidx = rb_filesystem_encindex();
4660 rb_enc_associate_index(result, encidx);
4661#if defined FILE_ALT_SEPARATOR
4663 bool mb_enc = enc_mbclen_needed(enc);
4664 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4665 if (*p == FILE_ALT_SEPARATOR) {
4677 VALUE dirname = rb_getpwdirnam_for_login(user);
4678 if (dirname ==
Qnil) {
4679 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4681 const char *dir = RSTRING_PTR(dirname);
4683 extern char *getlogin(
void);
4684 const char *pwPtr = 0;
4686 # define endpwent() ((void)0)
4687 const char *dir, *username = RSTRING_PTR(user);
4698 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4699 dir = pwPtr = getenv(
"HOME");
4701 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4704 copy_home_path(result, dir);
4710rb_default_home_dir(
VALUE result)
4712 const char *dir = getenv(
"HOME");
4714#if defined HAVE_PWD_H
4728 VALUE login_name = rb_getlogin();
4730# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4735 if (
NIL_P(login_name)) {
4736 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4740 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4741 if (
NIL_P(pw_dir)) {
4742 pw_dir = rb_getpwdiruid();
4743 if (
NIL_P(pw_dir)) {
4744 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4749 copy_home_path(result, RSTRING_PTR(pw_dir));
4750 rb_str_resize(pw_dir, 0);
4755 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4757 return copy_home_path(result, dir);
4763#if NORMALIZE_UTF8PATH
4764 VALUE path = rb_str_normalize_ospath(ptr,
len);
4765 rb_enc_associate(path, fsenc);
4768 return rb_enc_str_new(ptr,
len, fsenc);
4775 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4777 rb_enc_associate(dirname, fsenc);
4781 size_t dirlen = RSTRING_LEN(dirname);
4784 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4785 if (!rb_enc_compatible(fname, dirname)) {
4788 rb_enc_check(fname, dirname);
4789 rb_bug(
"unreachable");
4791 rb_encoding *direnc = fs_enc_check(fname, dirname);
4792 if (direnc != fsenc) {
4799 do {buflen *= 2;}
while (dirlen > buflen);
4800 rb_str_resize(result, buflen);
4801 buf = RSTRING_PTR(result);
4802 memcpy(buf, cwdp, dirlen);
4803 rb_enc_associate(result, *enc);
4804 return buf + dirlen;
4808rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4810 const char *s, *b, *fend;
4811 char *buf, *p, *pend, *root;
4812 size_t buflen, bdiff;
4813 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4816 fend = s + RSTRING_LEN(fname);
4817 enc = rb_str_enc_get(fname);
4818 bool mb_enc = enc_mbclen_needed(enc);
4819 if (!mb_enc &&
RTEST(dname)) {
4820 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4823 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4824 BUFINIT(result, buf, p, pend);
4827 if (s + 1 == fend || isdirsep(s[1])) {
4831 if (++s < fend) ++s;
4832 rb_default_home_dir(result);
4835 s = nextdirsep(b = s, fend, enc);
4838 BUFCHECK(bdiff + userlen >= buflen);
4839 memcpy(p, b, userlen);
4842 rb_enc_associate(result, enc);
4843 rb_home_dir_of(result, result);
4847 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4849 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4850 (
int)userlen, b, fname);
4853 rb_raise(rb_eArgError,
"non-absolute home");
4856 BUFINIT(result, buf, p, pend);
4859#ifdef DOSISH_DRIVE_LETTER
4861 else if (s + 1 < fend && has_drive_letter(s)) {
4862 BUFINIT(result, buf, p, pend);
4864 if (s + 2 < fend && isdirsep(s[2])) {
4867 BUFCHECK(bdiff + 2 >= buflen);
4871 rb_enc_copy(result, fname);
4876 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4877 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4878 BUFINIT(result, buf, p, pend);
4885 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4886 BUFINIT(result, buf, p, pend);
4890 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4893 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4898 else if (s == fend || !rb_is_absolute_path(s)) {
4900 if (!
NIL_P(dname)) {
4902 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4906 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4908 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
4913 result = resized_result;
4917 rb_enc_associate(result, fs_enc_check(result, fname));
4918 BUFINIT(result, buf, p, pend);
4922 VALUE cwd = rb_dir_getwd_ospath();
4926 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
4927 BUFINIT(result, buf, p, pend);
4930#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4931 if (s < fend && isdirsep(*s)) {
4934 p = skipprefix(buf, p, mb_enc, enc);
4938 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4941 BUFINIT(result, buf, p, pend);
4945 do s++;
while (s < fend && isdirsep(*s));
4948 BUFCHECK(bdiff >= buflen);
4949 memset(buf,
'/',
len);
4951 rb_enc_associate(result, fs_enc_check(result, fname));
4953 if (p > buf && p[-1] ==
'/')
4957 BUFCHECK(bdiff + 1 >= buflen);
4962 BUFCHECK(bdiff + 1 >= buflen);
4964 root = skipprefix(buf, p+1, mb_enc, enc);
4977 if (s+1 == fend || isdirsep(*(s+1))) {
4981 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
4991#if defined FILE_ALT_SEPARATOR
4992 case FILE_ALT_SEPARATOR:
5003#if defined FILE_ALT_SEPARATOR
5004 case FILE_ALT_SEPARATOR:
5007 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5015 int n = ignored_char_p(s, fend, enc);
5018 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5026 Inc(s, fend, mb_enc, enc);
5035 if (p == skiproot(buf, p + !!*p) - 1) p++;
5038 rb_enc_check(fname, result);
5045str_shrink(
VALUE str)
5047 rb_str_resize(str, RSTRING_LEN(str));
5051#define expand_path(fname, dname, abs_mode, long_name, result) \
5052 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5054#define check_expand_path_args(fname, dname) \
5055 (((fname) = rb_get_path(fname)), \
5056 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5059file_expand_path_1(
VALUE fname,
long extra_capa)
5062 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5068 check_expand_path_args(fname, dname);
5069 return expand_path(fname, dname, 0, 1,
Qfalse);
5073rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5075 return expand_path(fname, dname, 0, 0,
Qfalse);
5079rb_file_s_expand_path(
int argc,
const VALUE *argv)
5082 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5128 return rb_file_s_expand_path(c, v);
5134 check_expand_path_args(fname, dname);
5135 return expand_path(fname, dname, 1, 1,
Qfalse);
5139rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5142 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5185 return rb_file_s_absolute_path(c, v);
5213 VALUE path = rb_get_path(fname);
5215 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5219enum rb_realpath_mode {
5223 RB_REALPATH_MODE_MAX
5227realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5228 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5230 const char *pend = unresolved + strlen(unresolved);
5234 while (unresolved < pend) {
5235 const char *testname = unresolved;
5236 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5237 long testnamelen = unresolved_firstsep - unresolved;
5238 const char *unresolved_nextname = unresolved_firstsep;
5239 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5240 unresolved_nextname++;
5241 unresolved = unresolved_nextname;
5242 if (testnamelen == 1 && testname[0] ==
'.') {
5244 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5245 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5246 bool mb_enc = enc_mbclen_needed(enc);
5247 const char *resolved_str = RSTRING_PTR(*resolvedp);
5248 const char *resolved_names = resolved_str + *prefixlenp;
5249 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5250 long len = lastsep ? lastsep - resolved_names : 0;
5251 rb_str_resize(*resolvedp, *prefixlenp +
len);
5257 if (*prefixlenp < RSTRING_LEN(testpath))
5259#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5260 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5261 const char *prefix = RSTRING_PTR(testpath);
5262 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5267 checkval = rb_hash_aref(loopcheck, testpath);
5268 if (!
NIL_P(checkval)) {
5269 if (checkval ==
ID2SYM(resolving)) {
5270 if (mode == RB_REALPATH_CHECK) {
5274 rb_syserr_fail_path(ELOOP, testpath);
5283 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5286 if (e == ENOENT && !
NIL_P(fallback)) {
5287 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5292 if (mode == RB_REALPATH_CHECK)
return -1;
5294 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5295 rb_syserr_fail_path(e, testpath);
5296 *resolvedp = testpath;
5300 rb_syserr_fail_path(e, testpath);
5304 if (S_ISLNK(sbuf.st_mode)) {
5307 const char *link_prefix, *link_names;
5308 long link_prefixlen;
5309 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5310 link = rb_readlink(testpath, enc);
5311 link_prefix = RSTRING_PTR(link);
5312 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5313 link_prefixlen = link_names - link_prefix;
5314 if (link_prefixlen > 0) {
5318 tmpenc = fs_enc_check(*resolvedp, link);
5321 *prefixlenp = link_prefixlen;
5323 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5324 loopcheck, mode, !*unresolved_firstsep))
5333 rb_hash_aset(loopcheck, s, s);
5334 *resolvedp = testpath;
5343rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5347 VALUE unresolved_path;
5352 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5353 char *ptr, *prefixptr = NULL, *pend;
5358 if (!
NIL_P(basedir)) {
5363 enc = rb_enc_get(unresolved_path);
5364 unresolved_path = TO_OSPATH(unresolved_path);
5366 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5367 if (ptr != path_names) {
5368 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5372 if (!
NIL_P(basedir)) {
5374 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5375 if (ptr != basedir_names) {
5381 curdir = rb_dir_getwd_ospath();
5383 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5388 pend = prefixptr + prefixlen;
5389 bool mb_enc = enc_mbclen_needed(enc);
5390 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5392 prefixlen = ++ptr - prefixptr;
5395#ifdef FILE_ALT_SEPARATOR
5396 while (prefixptr < ptr) {
5397 if (*prefixptr == FILE_ALT_SEPARATOR) {
5400 Inc(prefixptr, pend, mb_enc, enc);
5404 switch (rb_enc_to_index(enc)) {
5405 case ENCINDEX_ASCII_8BIT:
5406 case ENCINDEX_US_ASCII:
5407 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5410 loopcheck = rb_hash_new();
5412 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5415 if (basedir_names) {
5416 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5419 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5422 if (origenc && origenc != rb_enc_get(resolved)) {
5424 rb_enc_associate(resolved, origenc);
5436static VALUE rb_file_join(
long argc,
VALUE *args);
5438#ifndef HAVE_REALPATH
5440rb_check_realpath_emulate_try(
VALUE arg)
5443 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5447rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5451#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5452 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5454# include <sys/syslimits.h>
5455# define NEEDS_REALPATH_BUFFER 1
5459rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5462 VALUE unresolved_path;
5463 char *resolved_ptr = NULL;
5465# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5466 char resolved_buffer[PATH_MAX];
5468 char *
const resolved_buffer = NULL;
5471 if (mode == RB_REALPATH_DIR) {
5472 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5476 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5477 VALUE paths[2] = {basedir, unresolved_path};
5478 unresolved_path = rb_file_join(2, paths);
5480 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5482 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5491 if (
errno == ENOTSUP ||
5493 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5494 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5497 if (mode == RB_REALPATH_CHECK) {
5500 rb_sys_fail_path(unresolved_path);
5502 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5503# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5507# if !defined(__linux__) && !defined(__APPLE__)
5511 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5512 if (mode == RB_REALPATH_CHECK) {
5515 rb_sys_fail_path(unresolved_path);
5519 if (origenc && origenc != rb_enc_get(resolved)) {
5523 rb_enc_associate(resolved, origenc);
5526 if (is_broken_string(resolved)) {
5527 rb_enc_associate(resolved, rb_filesystem_encoding());
5528 if (is_broken_string(resolved)) {
5529 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5536 if (mode == RB_REALPATH_CHECK) {
5540 arg[2] = (
VALUE)origenc;
5543 rb_check_realpath_emulate_rescue,
Qnil);
5546 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5552rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5554 const enum rb_realpath_mode mode =
5555 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5556 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5562 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5579rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5582 VALUE path = argv[0];
5584 return rb_realpath_internal(basedir, path, 1);
5600rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5603 VALUE path = argv[0];
5605 return rb_realpath_internal(basedir, path, 0);
5609rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5613 const char *s, *last;
5615 if (!e || !l2)
return 0;
5617 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5618 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5619 if (c ==
'.')
return l0;
5624 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5629 if (l1 < l2)
return l1;
5632 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5633#if CASEFOLD_FILESYSTEM
5634#define fncomp strncasecmp
5636#define fncomp strncmp
5638 if (fncomp(s, e, l2) == 0) {
5644static inline const char *
5645enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5647 const char *p, *q, *e, *end;
5650 long len = (alllen ? (size_t)*alllen : strlen(name));
5657 name = skipprefix(name, end, mb_enc, enc);
5658#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5659 const char *root = name;
5662 while (name < end && isdirsep(*name)) {
5669#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5673#ifdef DOSISH_DRIVE_LETTER
5674 else if (*p ==
':') {
5687 p = strrdirsep(name, end, mb_enc, enc);
5692 while (isdirsep(*p)) {
5697 n = ntfs_tail(p, end, mb_enc, enc) - p;
5699 n = chompdirsep(p, end, mb_enc, enc) - p;
5701 for (q = p; q - p < n && *q ==
'.'; q++);
5702 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5703 if (*q ==
'.') e = q;
5723ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5725 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5769 const char *name, *p, *fp = 0;
5775 CheckPath(fname, name);
5779 check_path_encoding(fext);
5781 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5782 enc = rb_str_enc_get(fname);
5785 n = RSTRING_LEN(fname);
5786 if (n <= 0 || !*name) {
5787 return rb_enc_str_new(0, 0, enc);
5790 bool mb_enc = enc_mbclen_needed(enc);
5791 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5797 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5802 if (f == RSTRING_LEN(fname)) {
5807 return rb_enc_str_new(p, f, enc);
5810static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5833rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5839 return rb_file_dirname_n(argv[0], n);
5845 return rb_file_dirname_n(fname, 1);
5849rb_file_dirname_n(
VALUE fname,
int n)
5851 const char *name, *root, *p, *end;
5854 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5855 CheckPath(fname, name);
5856 end = name + RSTRING_LEN(fname);
5858 bool mb_enc = !rb_str_enc_fastpath(fname);
5861 root = skiproot(name, end);
5863 if (root > name + 1 && isdirsep(*name))
5864 root = skipprefix(name = root - 2, end, mb_enc, enc);
5866 if (root > name + 1)
5869 if (n > (end - root + 1) / 2) {
5875 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5884 return rb_enc_str_new(
".", 1, enc);
5886#ifdef DOSISH_DRIVE_LETTER
5887 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5888 const char *top = skiproot(name + 2, end);
5889 dirname = rb_enc_str_new(name, 3, enc);
5894 dirname = rb_enc_str_new(name, p - name, enc);
5895#ifdef DOSISH_DRIVE_LETTER
5896 if (root == name + 2 && p == root && name[1] ==
':')
5902static inline const char *
5903enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5905 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5907 p = strrdirsep(name, end, mb_enc, enc);
5911 do name = ++p;
while (isdirsep(*p));
5914 while (*p && *p ==
'.') p++;
5916 if (*p ==
'.' || istrailinggarbage(*p)) {
5918 const char *last = p++, *dot = last;
5919 while (istrailinggarbage(*p)) {
5920 if (*p ==
'.') dot = p;
5923 if (!*p || isADS(*p)) {
5927 if (*last ==
'.' || dot > last) e = dot;
5934 else if (isADS(*p)) {
5938 else if (isdirsep(*p))
5940 Inc(p, end, mb_enc, enc);
5945 if (!e || e == name)
5970 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
6017 CheckPath(fname, name);
6018 long len = RSTRING_LEN(fname);
6021 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
6024 bool mb_enc = !rb_str_enc_fastpath(fname);
6027 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
6028 return rb_enc_str_new(ext,
len, enc);
6052 return rb_get_path(fname);
6084file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6086 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6087 return rb_file_join_ary(arg);
6091rb_file_join_ary(
VALUE ary)
6095 const char *name, *tail;
6105 check_path_encoding(tmp);
6106 len += RSTRING_LEN(tmp);
6114 RBASIC_CLEAR_CLASS(result);
6117 switch (OBJ_BUILTIN_TYPE(tmp)) {
6119 if (!checked) check_path_encoding(tmp);
6124 rb_raise(rb_eArgError,
"recursive array");
6136 rb_enc_copy(result, tmp);
6139 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6140 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6143 else if (tail == name +
len) {
6147 enc = fs_enc_check(result, tmp);
6149 rb_enc_associate(result, enc);
6157rb_file_join_fastpath(
long argc,
VALUE *args)
6162 for (i = 0; i < argc; i++) {
6163 VALUE tmp = args[i];
6165 size += RSTRING_LEN(tmp);
6178 const char *name = RSTRING_PTR(result);
6179 for (i = 1; i < argc; i++) {
6180 VALUE tmp = args[i];
6181 long len = RSTRING_LEN(result);
6187 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6190 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6195 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6202 rb_enc_associate(result, new_enc);
6203 encidx = rb_enc_to_index(new_enc);
6209 rb_str_null_check(result);
6214rb_file_join(
long argc,
VALUE *args)
6216 if (RB_UNLIKELY(argc == 0)) {
6220 VALUE result = rb_file_join_fastpath(argc, args);
6221 if (RB_LIKELY(result)) {
6241rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6243 return rb_file_join(argc, argv);
6246#if defined(HAVE_TRUNCATE)
6247struct truncate_arg {
6253nogvl_truncate(
void *ptr)
6255 struct truncate_arg *ta = ptr;
6256 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6288 struct truncate_arg ta;
6293 path = rb_str_encode_ospath(path);
6296 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6298 rb_sys_fail_path(path);
6302#define rb_file_s_truncate rb_f_notimplement
6305#if defined(HAVE_FTRUNCATE)
6306struct ftruncate_arg {
6312nogvl_ftruncate(
void *ptr)
6314 struct ftruncate_arg *fa = ptr;
6316 return (
VALUE)ftruncate(fa->fd, fa->pos);
6357 struct ftruncate_arg fa;
6364 rb_io_flush_raw(obj, 0);
6366 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6367 rb_sys_fail_path(fptr->
pathv);
6372#define rb_file_truncate rb_f_notimplement
6389#include <winerror.h>
6393rb_thread_flock(
void *data)
6396 int old_errno =
errno;
6398 int *op = data, ret = flock(op[0], op[1]);
6401 if (GetLastError() == ERROR_NOT_LOCKED) {
6459 op[1] = op1 =
NUM2INT(operation);
6464 rb_io_flush_raw(obj, 0);
6466 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6471#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6474 if (op1 & LOCK_NB)
return Qfalse;
6477 time.tv_usec = 100 * 1000;
6483#if defined(ERESTART)
6489 rb_syserr_fail_path(e, fptr->
pathv);
6496test_check(
int n,
int argc,
VALUE *argv)
6502 for (i=1; i<n; i++) {
6509#define CHECK(n) test_check((n), argc, argv)
6601 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6605 return rb_file_blockdev_p(0, argv[1]);
6608 return rb_file_chardev_p(0, argv[1]);
6611 return rb_file_directory_p(0, argv[1]);
6614 return rb_file_exist_p(0, argv[1]);
6617 return rb_file_file_p(0, argv[1]);
6620 return rb_file_sgid_p(0, argv[1]);
6623 return rb_file_grpowned_p(0, argv[1]);
6626 return rb_file_sticky_p(0, argv[1]);
6629 return rb_file_symlink_p(0, argv[1]);
6632 return rb_file_owned_p(0, argv[1]);
6635 return rb_file_rowned_p(0, argv[1]);
6638 return rb_file_pipe_p(0, argv[1]);
6641 return rb_file_readable_p(0, argv[1]);
6644 return rb_file_readable_real_p(0, argv[1]);
6647 return rb_file_size_p(0, argv[1]);
6650 return rb_file_socket_p(0, argv[1]);
6653 return rb_file_suid_p(0, argv[1]);
6656 return rb_file_writable_p(0, argv[1]);
6659 return rb_file_writable_real_p(0, argv[1]);
6662 return rb_file_executable_p(0, argv[1]);
6665 return rb_file_executable_real_p(0, argv[1]);
6668 return rb_file_zero_p(0, argv[1]);
6672 if (strchr(
"MAC", cmd)) {
6674 VALUE fname = argv[1];
6677 if (
rb_stat(fname, &st) == -1) {
6680 rb_syserr_fail_path(e, fname);
6685 return stat_atime(&st);
6687 return stat_mtime(&st);
6689 return stat_ctime(&st);
6695 return rb_file_identical_p(0, argv[1], argv[2]);
6698 if (strchr(
"=<>", cmd)) {
6699 struct stat st1, st2;
6706 t1 = stat_mtimespec(&st1);
6707 t2 = stat_mtimespec(&st2);
6711 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6715 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6716 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6720 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6721 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6728 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6731 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6785rb_stat_s_alloc(
VALUE klass)
6806 fname = rb_str_encode_ospath(fname);
6808 rb_sys_fail_path(fname);
6815 rb_st->initialized =
true;
6832 *copy_rb_st = *orig_rb_st;
6874rb_stat_ftype(
VALUE obj)
6876 return rb_file_ftype(get_stat(obj)->ST_(mode));
6892 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6919 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
6956 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
6988 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
7010 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
7030 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7065rb_stat_owned(
VALUE obj)
7067 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7072rb_stat_rowned(
VALUE obj)
7074 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7095rb_stat_grpowned(
VALUE obj)
7098 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7127 rb_io_stat_data *st = get_stat(obj);
7130 if (geteuid() == 0)
return Qtrue;
7133 if (rb_stat_owned(obj))
7134 return RBOOL(st->ST_(mode) & S_IRUSR);
7137 if (rb_stat_grpowned(obj))
7138 return RBOOL(st->ST_(mode) & S_IRGRP);
7141 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7159 rb_io_stat_data *st = get_stat(obj);
7162 if (getuid() == 0)
return Qtrue;
7165 if (rb_stat_rowned(obj))
7166 return RBOOL(st->ST_(mode) & S_IRUSR);
7169 if (rb_group_member(get_stat(obj)->ST_(gid)))
7170 return RBOOL(st->ST_(mode) & S_IRGRP);
7173 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7201rb_stat_wr(
VALUE obj)
7204 rb_io_stat_data *st = get_stat(obj);
7205 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7206 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7226 rb_io_stat_data *st = get_stat(obj);
7229 if (geteuid() == 0)
return Qtrue;
7232 if (rb_stat_owned(obj))
7233 return RBOOL(st->ST_(mode) & S_IWUSR);
7236 if (rb_stat_grpowned(obj))
7237 return RBOOL(st->ST_(mode) & S_IWGRP);
7240 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7261 rb_io_stat_data *st = get_stat(obj);
7264 if (getuid() == 0)
return Qtrue;
7267 if (rb_stat_rowned(obj))
7268 return RBOOL(st->ST_(mode) & S_IWUSR);
7271 if (rb_group_member(get_stat(obj)->ST_(gid)))
7272 return RBOOL(st->ST_(mode) & S_IWGRP);
7275 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7304rb_stat_ww(
VALUE obj)
7307 rb_io_stat_data *st = get_stat(obj);
7308 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7309 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7351 rb_io_stat_data *st = get_stat(obj);
7354 if (geteuid() == 0) {
7355 return RBOOL(st->ST_(mode) & S_IXUGO);
7359 if (rb_stat_owned(obj))
7360 return RBOOL(st->ST_(mode) & S_IXUSR);
7363 if (rb_stat_grpowned(obj))
7364 return RBOOL(st->ST_(mode) & S_IXGRP);
7367 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7383 rb_io_stat_data *st = get_stat(obj);
7386 if (getuid() == 0) {
7387 return RBOOL(st->ST_(mode) & S_IXUGO);
7391 if (rb_stat_rowned(obj))
7392 return RBOOL(st->ST_(mode) & S_IXUSR);
7395 if (rb_group_member(get_stat(obj)->ST_(gid)))
7396 return RBOOL(st->ST_(mode) & S_IXGRP);
7399 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7422 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7467 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7499 rb_off_t size = get_stat(obj)->ST_(size);
7501 if (size == 0)
return Qnil;
7534rb_stat_suid(
VALUE obj)
7537 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7571rb_stat_sgid(
VALUE obj)
7574 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7608rb_stat_sticky(
VALUE obj)
7611 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7616#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7617#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7628nogvl_mkfifo(
void *ptr)
7630 struct mkfifo_arg *ma = ptr;
7632 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7660 struct mkfifo_arg ma;
7669 path = rb_str_encode_ospath(path);
7670 ma.path = RSTRING_PTR(path);
7671 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7672 rb_sys_fail_path(path);
7677#define rb_file_s_mkfifo rb_f_notimplement
7680static VALUE rb_mFConst;
7683rb_file_const(
const char *name,
VALUE value)
7685 rb_define_const(rb_mFConst, name, value);
7689rb_is_absolute_path(
const char *path)
7691#ifdef DOSISH_DRIVE_LETTER
7692 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7695 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7698 if (path[0] ==
'/')
return 1;
7704ruby_is_fd_loadable(
int fd)
7711 if (fstat(fd, &st) < 0)
7714 if (S_ISREG(st.st_mode))
7717 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7720 if (S_ISDIR(st.st_mode))
7731rb_file_load_ok(
const char *path)
7738 int mode = (O_RDONLY |
7739#if defined O_NONBLOCK
7741#elif defined O_NDELAY
7747 if (!rb_gc_for_fd(
errno))
return 0;
7749 if (fd < 0)
return 0;
7752 ret = ruby_is_fd_loadable(fd);
7759is_explicit_relative(
const char *path)
7761 if (*path++ !=
'.')
return 0;
7762 if (*path ==
'.') path++;
7763 return isdirsep(*path);
7769 int encidx = rb_enc_get_index(orig);
7770 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7771 encidx = rb_filesystem_encindex();
7772 rb_enc_associate_index(path, encidx);
7780nav_component_p(
const char *s,
const char *send)
7782 if ((send - s) >= 2 && s[0] ==
'.') {
7783 return s[1] ==
'.' || isdirsep(s[1]);
7789fname_need_expansion_p(
VALUE fname)
7791 const char *s = RSTRING_PTR(fname);
7792 const long len = RSTRING_LEN(fname);
7793 const char *send = s +
len;
7795 if (nav_component_p(s, send)) {
7800 bool mbenc = enc_mbclen_needed(enc);
7802 s = enc_path_next(s, send, mbenc, enc);
7804 if (nav_component_p(s, send)) {
7808 s = enc_path_next(s, send, mbenc, enc);
7814expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7816 long dname_len = RSTRING_LEN(dname);
7817 const char *dname_ptr = RSTRING_PTR(dname);
7821 if (need_expansion || dname_ptr[0] ==
'~') {
7822 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7827 if (!isdirsep(dname_ptr[dname_len - 1])) {
7836rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
7839 VALUE fname = *filep;
7843 if (!ext[0])
return 0;
7846 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7847 f = RSTRING_PTR(fname);
7852 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7853 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7854 fnlen = RSTRING_LEN(fname);
7855 for (i=0; ext[i]; i++) {
7857 if (rb_file_load_ok(RSTRING_PTR(fname))) {
7858 *filep = copy_path_class(fname, *filep);
7866 long expanded_load_path_maxlen;
7867 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7868 if (!load_path)
return 0;
7871 RBASIC_CLEAR_CLASS(fname);
7872 fnlen = RSTRING_LEN(fname);
7873 bool need_expansion = fname_need_expansion_p(fname);
7876 rb_enc_associate_index(tmp, rb_usascii_encindex());
7878 for (j=0; ext[j]; j++) {
7880 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
7882 if (!RSTRING_LEN(dname))
continue;
7883 expand_feature(fname, dname, tmp, need_expansion);
7885 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7886 *filep = copy_path_class(tmp, *filep);
7892 rb_str_resize(tmp, 0);
7905 path = copy_path_class(file_expand_path_1(path, 0), path);
7906 f = RSTRING_PTR(path);
7910 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7911 if (!rb_file_load_ok(f))
return 0;
7913 path = copy_path_class(file_expand_path_1(path, 0), path);
7917 long expanded_load_path_maxlen;
7918 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7921 bool need_expansion = fname_need_expansion_p(path);
7923 rb_enc_associate_index(tmp, rb_usascii_encindex());
7924 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
7926 if (!RSTRING_LEN(dname))
continue;
7927 expand_feature(path, dname, tmp, need_expansion);
7929 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7930 return copy_path_class(tmp, path);
7933 rb_str_resize(tmp, 0);
7941#define define_filetest_function(name, func, argc) do { \
7942 rb_define_module_function(rb_mFileTest, name, func, argc); \
7943 rb_define_singleton_method(rb_cFile, name, func, argc); \
7946const char ruby_null_device[] =
8749#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8750 rb_CFString_class_initialize_before_fork();
8758 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8759 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8760 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8761 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8762 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8763 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8764 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8765 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8766 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8767 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8768 define_filetest_function(
"file?", rb_file_file_p, 1);
8769 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8770 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8771 define_filetest_function(
"size?", rb_file_size_p, 1);
8772 define_filetest_function(
"size", rb_file_s_size, 1);
8773 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8774 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8776 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8777 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8778 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8780 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8781 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8783 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8784 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8785 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8787 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8825 separator = rb_fstring_lit(
"/");
8827 rb_define_const(
rb_cFile,
"Separator", separator);
8829 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9178 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9181 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9183 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9185 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9187 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9189 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9191 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9192#if defined(O_NDELAY) || defined(O_NONBLOCK)
9194# define O_NONBLOCK O_NDELAY
9197 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9200 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9203 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9209 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9210#ifndef O_SHARE_DELETE
9211# define O_SHARE_DELETE 0
9214 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9217 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9221 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9225 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9229 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9233 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9237 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9241 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9245 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9247 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9249 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9251 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9254 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.