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));
2257 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2274rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2276 return RBOOL(rb_access(fname, W_OK) >= 0);
2305rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2311 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2312 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2355 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2374rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2376 return RBOOL(rb_access(fname, X_OK) >= 0);
2380# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2412 return RBOOL(S_ISREG(st.st_mode));
2472 return RBOOL(st.st_size == 0);
2510 if (st.st_size == 0)
return Qnil;
2544 return RBOOL(st.st_uid == geteuid());
2553 return RBOOL(st.st_uid == getuid());
2585 if (rb_group_member(st.st_gid))
return Qtrue;
2590#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2592check3rdbyte(
VALUE fname,
int mode)
2597 return RBOOL(st.st_mode & mode);
2630 return check3rdbyte(fname, S_ISUID);
2665 return check3rdbyte(fname, S_ISGID);
2701 return check3rdbyte(fname, S_ISVTX);
2738 struct stat st1, st2;
2742 if (st1.st_dev != st2.st_dev)
return Qfalse;
2743 if (st1.st_ino != st2.st_ino)
return Qfalse;
2747 return rb_w32_file_identical_p(fname1, fname2);
2773 if (
rb_stat(fname, &st) < 0) {
2776 rb_syserr_fail_path(e, fname);
2782rb_file_ftype(mode_t mode)
2786 if (S_ISREG(mode)) {
2789 else if (S_ISDIR(mode)) {
2792 else if (S_ISCHR(mode)) {
2793 t =
"characterSpecial";
2796 else if (S_ISBLK(mode)) {
2801 else if (S_ISFIFO(mode)) {
2806 else if (S_ISLNK(mode)) {
2811 else if (S_ISSOCK(mode)) {
2819 return rb_fstring_cstr(t);
2861 fname = rb_str_encode_ospath(fname);
2863 rb_sys_fail_path(fname);
2866 return rb_file_ftype(st.st_mode);
2914 if (
rb_stat(fname, &st) < 0) {
2917 rb_syserr_fail_path(e, fname);
2919 return stat_time(stat_atimespec(&st));
2946rb_file_atime(
VALUE obj)
2952 if (fstat(fptr->
fd, &st) == -1) {
2953 rb_sys_fail_path(fptr->
pathv);
2955 return stat_time(stat_atimespec(&st));
2995 if (
rb_stat(fname, &st) < 0) {
2998 rb_syserr_fail_path(e, fname);
3000 return stat_time(stat_mtimespec(&st));
3025rb_file_mtime(
VALUE obj)
3031 if (fstat(fptr->
fd, &st) == -1) {
3032 rb_sys_fail_path(fptr->
pathv);
3034 return stat_time(stat_mtimespec(&st));
3077 if (
rb_stat(fname, &st) < 0) {
3080 rb_syserr_fail_path(e, fname);
3082 return stat_time(stat_ctimespec(&st));
3099rb_file_ctime(
VALUE obj)
3105 if (fstat(fptr->
fd, &st) == -1) {
3106 rb_sys_fail_path(fptr->
pathv);
3108 return stat_time(stat_ctimespec(&st));
3111#if defined(HAVE_STAT_BIRTHTIME)
3142 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3145 rb_syserr_fail_path(e, fname);
3147 return statx_birthtime(&st);
3150# define rb_file_s_birthtime rb_f_notimplement
3153#if defined(HAVE_STAT_BIRTHTIME)
3174rb_file_birthtime(
VALUE obj)
3180 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3181 rb_sys_fail_path(fptr->
pathv);
3183 return statx_birthtime(&st);
3186# define rb_file_birthtime rb_f_notimplement
3198 rb_io_flush_raw(file, 0);
3201 if (fstat(fptr->
fd, &st) == -1) {
3202 rb_sys_fail_path(fptr->
pathv);
3228file_size(
VALUE self)
3230 return OFFT2NUM(rb_file_size(self));
3239nogvl_chmod(
void *ptr)
3242 int ret = chmod(data->path, data->mode);
3243 return (
void *)(
VALUE)ret;
3247rb_chmod(
const char *path, mode_t mode)
3253 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3257chmod_internal(
const char *path,
void *mode)
3259 return chmod(path, *(mode_t *)mode);
3297 return apply2files(chmod_internal, argc, argv, &mode);
3301struct nogvl_fchmod_data {
3307io_blocking_fchmod(
void *ptr)
3309 struct nogvl_fchmod_data *data = ptr;
3310 int ret = fchmod(data->fd, data->mode);
3315rb_fchmod(
struct rb_io* io, mode_t mode)
3318 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3319 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3351#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3359 if (rb_fchmod(fptr, mode) == -1) {
3360 if (HAVE_FCHMOD ||
errno != ENOSYS)
3361 rb_sys_fail_path(fptr->
pathv);
3364 if (!HAVE_FCHMOD)
return INT2FIX(0);
3367#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3369 path = rb_str_encode_ospath(fptr->
pathv);
3370 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3371 rb_sys_fail_path(fptr->
pathv);
3377#if defined(HAVE_LCHMOD)
3379lchmod_internal(
const char *path,
void *mode)
3381 return lchmod(path, *(mode_t *)mode);
3418 return apply2files(lchmod_internal, argc, argv, &mode);
3421#define rb_file_s_lchmod rb_f_notimplement
3424static inline rb_uid_t
3428 return (rb_uid_t)-1;
3433static inline rb_gid_t
3437 return (rb_gid_t)-1;
3448chown_internal(
const char *path,
void *arg)
3451 return chown(path, args->owner, args->group);
3505 arg.owner = to_uid(*argv++);
3506 arg.group = to_gid(*argv++);
3508 return apply2files(chown_internal, argc, argv, &arg);
3520nogvl_chown(
void *ptr)
3523 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3527rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3530 .as = {.path = path},
3531 .new = {.owner = owner, .group = group},
3533 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3538nogvl_fchown(
void *ptr)
3541 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3545rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3550 .new = {.owner = owner, .group = group},
3552 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3586 path = rb_str_encode_ospath(fptr->
pathv);
3587 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3588 rb_sys_fail_path(fptr->
pathv);
3590 if (rb_fchown(fptr->
fd, o, g) == -1)
3591 rb_sys_fail_path(fptr->
pathv);
3597#if defined(HAVE_LCHOWN)
3599lchown_internal(
const char *path,
void *arg)
3602 return lchown(path, args->owner, args->group);
3659 arg.owner = to_uid(*argv++);
3660 arg.group = to_gid(*argv++);
3662 return apply2files(lchown_internal, argc, argv, &arg);
3665#define rb_file_s_lchown rb_f_notimplement
3675NORETURN(
static void utime_failed(
struct apply_arg *));
3681 VALUE path = aa->fn[aa->i].path;
3684 if (ua->tsp && e == EINVAL) {
3687 VALUE atime = ua->atime;
3688 VALUE mtime = ua->mtime;
3690 if (!
NIL_P(atime)) {
3693 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3696 if (
NIL_P(a)) e[0] = m;
3712 rb_syserr_fail_path(e, path);
3716#if defined(HAVE_UTIMES)
3718# if !defined(HAVE_UTIMENSAT)
3720# elif defined(__APPLE__) && \
3721 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3723# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3724typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3728static inline utimensat_func *
3735# define utimensat rb_utimensat()
3742utime_internal(
const char *path,
void *arg)
3745 const struct timespec *tsp = v->tsp;
3746 struct timeval tvbuf[2], *tvp = NULL;
3748#if defined(HAVE_UTIMENSAT)
3749# if defined(__APPLE__)
3750 const int try_utimensat = utimensat != NULL;
3751 const int try_utimensat_follow = utimensat != NULL;
3753# define TRY_UTIMENSAT 1
3754 static int try_utimensat = 1;
3755# ifdef AT_SYMLINK_NOFOLLOW
3756 static int try_utimensat_follow = 1;
3758 const int try_utimensat_follow = 0;
3763 if (v->follow ? try_utimensat_follow : try_utimensat) {
3764# ifdef AT_SYMLINK_NOFOLLOW
3766 flags = AT_SYMLINK_NOFOLLOW;
3770 int result = utimensat(AT_FDCWD, path, tsp, flags);
3771# ifdef TRY_UTIMENSAT
3772 if (result < 0 &&
errno == ENOSYS) {
3773# ifdef AT_SYMLINK_NOFOLLOW
3774 try_utimensat_follow = 0;
3786 tvbuf[0].tv_sec = tsp[0].tv_sec;
3787 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3788 tvbuf[1].tv_sec = tsp[1].tv_sec;
3789 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3793 if (v->follow)
return lutimes(path, tvp);
3795 return utimes(path, tvp);
3800#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3808utime_internal(
const char *path,
void *arg)
3812 struct utimbuf utbuf, *utp = NULL;
3814 utbuf.actime = tsp[0].tv_sec;
3815 utbuf.modtime = tsp[1].tv_sec;
3818 return utime(path, utp);
3823utime_internal_i(
int argc,
VALUE *argv,
int follow)
3826 struct timespec tss[2], *tsp = NULL;
3829 args.atime = *argv++;
3830 args.mtime = *argv++;
3832 args.follow = follow;
3834 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3837 if (args.atime == args.mtime)
3844 return apply2files(utime_internal, argc, argv, &args);
3861 return utime_internal_i(argc, argv, FALSE);
3864#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3922 return utime_internal_i(argc, argv, TRUE);
3925#define rb_file_s_lutime rb_f_notimplement
3928#ifdef RUBY_FUNCTION_NAME_STRING
3929# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3931# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3933#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3934NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3936syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3940 const int max_pathlen = MAX_PATH;
3942 const int max_pathlen = MAXPATHLEN;
3953#ifdef RUBY_FUNCTION_NAME_STRING
3954 rb_syserr_fail_path_in(func, e, str);
3956 rb_syserr_fail_path(e, str);
3991 from = rb_str_encode_ospath(from);
3992 to = rb_str_encode_ospath(to);
3995 sys_fail2(from, to);
4000#define rb_file_s_link rb_f_notimplement
4041 from = rb_str_encode_ospath(from);
4042 to = rb_str_encode_ospath(to);
4045 sys_fail2(from, to);
4050#define rb_file_s_symlink rb_f_notimplement
4078 return rb_readlink(path, rb_filesystem_encoding());
4081struct readlink_arg {
4088nogvl_readlink(
void *ptr)
4090 struct readlink_arg *ra = ptr;
4092 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4096readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4098 struct readlink_arg ra;
4100 ra.path = RSTRING_PTR(path);
4101 ra.buf = RSTRING_PTR(buf);
4104 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4115 path = rb_str_encode_ospath(path);
4116 v = rb_enc_str_new(0, size, enc);
4117 while ((rv = readlink_without_gvl(path, v, size)) == size
4119 || (rv < 0 &&
errno == ERANGE)
4128 rb_str_resize(v, 0);
4129 rb_syserr_fail_path(e, path);
4131 rb_str_resize(v, rv);
4136#define rb_file_s_readlink rb_f_notimplement
4140unlink_internal(
const char *path,
void *arg)
4142 return unlink(path);
4185rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4187 return apply2files(unlink_internal, argc, argv, 0);
4196no_gvl_rename(
void *ptr)
4200 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4278 f = rb_str_encode_ospath(from);
4279 t = rb_str_encode_ospath(to);
4282#if defined __CYGWIN__
4285 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4290 if (chmod(ra.dst, 0666) == 0 &&
4291 unlink(ra.dst) == 0 &&
4292 rename(ra.src, ra.dst) == 0)
4296 syserr_fail2(e, from, to);
4330 rb_error_arity(argc, 0, 1);
4338#if defined __CYGWIN__ || defined DOSISH
4340#define DOSISH_DRIVE_LETTER
4341#define FILE_ALT_SEPARATOR '\\'
4343#ifdef FILE_ALT_SEPARATOR
4344#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4346static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4349#define isdirsep(x) ((x) == '/')
4361#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4362#define isADS(x) ((x) == ':')
4364#define istrailinggarbage(x) 0
4367#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4369#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4370#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4372#if defined(DOSISH_UNC)
4373#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4375#define has_unc(buf) 0
4378#ifdef DOSISH_DRIVE_LETTER
4380has_drive_letter(
const char *buf)
4382 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4407 if (chdir(drive) == 0) {
4408 drvcwd = rb_dir_getwd_ospath();
4420not_same_drive(
VALUE path,
int drive)
4422 const char *p = RSTRING_PTR(path);
4423 if (RSTRING_LEN(path) < 2)
return 0;
4424 if (has_drive_letter(p)) {
4435skiproot(
const char *path,
const char *end)
4437#ifdef DOSISH_DRIVE_LETTER
4438 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4440 while (path < end && isdirsep(*path)) path++;
4441 return (
char *)path;
4445enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4447 while (s < e && !isdirsep(*s)) {
4448 Inc(s, e, mb_enc, enc);
4453#define nextdirsep rb_enc_path_next
4457 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4460#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4461#define skipprefix enc_path_skip_prefix
4463#define skipprefix(path, end, mb_enc, enc) (path)
4466enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4468#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4470 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4472 while (path < end && isdirsep(*path)) path++;
4473 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4474 path + 2 <= end && !isdirsep(path[1])) {
4475 path = enc_path_next(path + 1, end, mb_enc, enc);
4477 return (
char *)path;
4480#ifdef DOSISH_DRIVE_LETTER
4481 if (path + 2 <= end && has_drive_letter(path))
4482 return (
char *)(path + 2);
4485 return (
char *)path;
4489rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4491 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4495skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4497#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4498 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4499 while (p < end && isdirsep(*p)) p++;
4502 return skiproot(path, end);
4507rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4509 return skipprefixroot(path, end, enc);
4513enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4516 while (path < end) {
4517 if (isdirsep(*path)) {
4518 const char *tmp = path++;
4519 while (path < end && isdirsep(*path)) path++;
4520 if (path >= end)
break;
4524 Inc(path, end, mb_enc, enc);
4530rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4532 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4536strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4538 if (RB_UNLIKELY(mb_enc)) {
4539 return enc_path_last_separator(path, end, mb_enc, enc);
4542 const char *cursor = end - 1;
4544 while (cursor >= path && isdirsep(cursor[0])) {
4548 while (cursor >= path) {
4549 if (isdirsep(cursor[0])) {
4550 while (cursor > path && isdirsep(cursor[-1])) {
4553 return (
char *)cursor;
4561chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4563 while (path < end) {
4564 if (isdirsep(*path)) {
4565 const char *last = path++;
4566 while (path < end && isdirsep(*path)) path++;
4567 if (path >= end)
return (
char *)last;
4570 Inc(path, end, mb_enc, enc);
4573 return (
char *)path;
4579 if (path < end && isdirsep(*path)) path++;
4580 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4586 rb_encoding *enc = rb_enc_check_str(path1, path2);
4587 int encidx = rb_enc_to_index(enc);
4588 if (encidx == ENCINDEX_US_ASCII) {
4589 encidx = rb_enc_get_index(path1);
4590 if (encidx == ENCINDEX_US_ASCII)
4591 encidx = rb_enc_get_index(path2);
4592 enc = rb_enc_from_index(encidx);
4599ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4601 while (path < end && *path ==
'.') path++;
4602 while (path < end && !isADS(*path)) {
4603 if (istrailinggarbage(*path)) {
4604 const char *last = path++;
4605 while (path < end && istrailinggarbage(*path)) path++;
4606 if (path >= end || isADS(*path))
return (
char *)last;
4608 else if (isdirsep(*path)) {
4609 const char *last = path++;
4610 while (path < end && isdirsep(*path)) path++;
4611 if (path >= end)
return (
char *)last;
4612 if (isADS(*path)) path++;
4615 Inc(path, end, mb_enc, enc);
4618 return (
char *)path;
4622#define BUFCHECK(cond) do {\
4625 do {buflen *= 2;} while (cond);\
4626 rb_str_resize(result, buflen);\
4627 buf = RSTRING_PTR(result);\
4629 pend = buf + buflen;\
4633#define BUFINIT(result, buf, p, pend) do {\
4634 if (!result) { result = rb_usascii_str_new(0, 1); } \
4635 p = buf = RSTRING_PTR(result); \
4636 buflen = RSTRING_LEN(result); \
4637 pend = p + buflen; \
4641# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4643# define SKIPPATHSEP(p) 1
4646#define BUFCOPY(srcptr, srclen) do { \
4647 const int skip = SKIPPATHSEP(p); \
4648 rb_str_set_len(result, p-buf+skip); \
4649 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4651 memcpy(p, (srcptr), (srclen)); \
4655#define WITH_ROOTDIFF(stmt) do { \
4656 long rootdiff = root - buf; \
4658 root = buf + rootdiff; \
4662copy_home_path(
VALUE result,
const char *dir)
4668 dirlen = strlen(dir);
4669 rb_str_resize(result, dirlen);
4670 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4671 encidx = rb_filesystem_encindex();
4672 rb_enc_associate_index(result, encidx);
4673#if defined FILE_ALT_SEPARATOR
4675 bool mb_enc = enc_mbclen_needed(enc);
4676 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4677 if (*p == FILE_ALT_SEPARATOR) {
4689 VALUE dirname = rb_getpwdirnam_for_login(user);
4690 if (dirname ==
Qnil) {
4691 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4693 const char *dir = RSTRING_PTR(dirname);
4695 extern char *getlogin(
void);
4696 const char *pwPtr = 0;
4698 # define endpwent() ((void)0)
4699 const char *dir, *username = RSTRING_PTR(user);
4710 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4711 dir = pwPtr = getenv(
"HOME");
4713 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4716 copy_home_path(result, dir);
4722rb_default_home_dir(
VALUE result)
4724 const char *dir = getenv(
"HOME");
4726#if defined HAVE_PWD_H
4740 VALUE login_name = rb_getlogin();
4742# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4747 if (
NIL_P(login_name)) {
4748 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4752 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4753 if (
NIL_P(pw_dir)) {
4754 pw_dir = rb_getpwdiruid();
4755 if (
NIL_P(pw_dir)) {
4756 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4761 copy_home_path(result, RSTRING_PTR(pw_dir));
4762 rb_str_resize(pw_dir, 0);
4767 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4769 return copy_home_path(result, dir);
4775#if NORMALIZE_UTF8PATH
4776 VALUE path = rb_str_normalize_ospath(ptr,
len);
4777 rb_enc_associate(path, fsenc);
4780 return rb_enc_str_new(ptr,
len, fsenc);
4787 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4789 rb_enc_associate(dirname, fsenc);
4793 size_t dirlen = RSTRING_LEN(dirname);
4796 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4797 if (!rb_enc_compatible(fname, dirname)) {
4800 rb_enc_check(fname, dirname);
4801 rb_bug(
"unreachable");
4803 rb_encoding *direnc = fs_enc_check(fname, dirname);
4804 if (direnc != fsenc) {
4811 do {buflen *= 2;}
while (dirlen > buflen);
4812 rb_str_resize(result, buflen);
4813 buf = RSTRING_PTR(result);
4814 memcpy(buf, cwdp, dirlen);
4815 rb_enc_associate(result, *enc);
4816 return buf + dirlen;
4820rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4822 const char *s, *b, *fend;
4823 char *buf, *p, *pend, *root;
4824 size_t buflen, bdiff;
4825 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4828 fend = s + RSTRING_LEN(fname);
4829 enc = rb_str_enc_get(fname);
4830 bool mb_enc = enc_mbclen_needed(enc);
4831 if (!mb_enc &&
RTEST(dname)) {
4832 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4835 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4836 BUFINIT(result, buf, p, pend);
4839 if (s + 1 == fend || isdirsep(s[1])) {
4843 if (++s < fend) ++s;
4844 rb_default_home_dir(result);
4847 s = nextdirsep(b = s, fend, enc);
4850 BUFCHECK(bdiff + userlen >= buflen);
4851 memcpy(p, b, userlen);
4854 rb_enc_associate(result, enc);
4855 rb_home_dir_of(result, result);
4859 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4861 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4862 (
int)userlen, b, fname);
4865 rb_raise(rb_eArgError,
"non-absolute home");
4868 BUFINIT(result, buf, p, pend);
4871#ifdef DOSISH_DRIVE_LETTER
4873 else if (s + 1 < fend && has_drive_letter(s)) {
4874 BUFINIT(result, buf, p, pend);
4876 if (s + 2 < fend && isdirsep(s[2])) {
4879 BUFCHECK(bdiff + 2 >= buflen);
4883 rb_enc_copy(result, fname);
4888 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4889 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4890 BUFINIT(result, buf, p, pend);
4897 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4898 BUFINIT(result, buf, p, pend);
4902 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4905 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4910 else if (s == fend || !rb_is_absolute_path(s)) {
4912 if (!
NIL_P(dname)) {
4914 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4918 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4920 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
4925 result = resized_result;
4929 rb_enc_associate(result, fs_enc_check(result, fname));
4930 BUFINIT(result, buf, p, pend);
4934 VALUE cwd = rb_dir_getwd_ospath();
4938 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
4939 BUFINIT(result, buf, p, pend);
4942#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4943 if (s < fend && isdirsep(*s)) {
4946 p = skipprefix(buf, p, mb_enc, enc);
4950 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4953 BUFINIT(result, buf, p, pend);
4957 do s++;
while (s < fend && isdirsep(*s));
4960 BUFCHECK(bdiff >= buflen);
4961 memset(buf,
'/',
len);
4963 rb_enc_associate(result, fs_enc_check(result, fname));
4965 if (p > buf && p[-1] ==
'/')
4969 BUFCHECK(bdiff + 1 >= buflen);
4974 BUFCHECK(bdiff + 1 >= buflen);
4976 root = skipprefix(buf, p+1, mb_enc, enc);
4989 if (s+1 == fend || isdirsep(*(s+1))) {
4993 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
5003#if defined FILE_ALT_SEPARATOR
5004 case FILE_ALT_SEPARATOR:
5015#if defined FILE_ALT_SEPARATOR
5016 case FILE_ALT_SEPARATOR:
5019 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5027 int n = ignored_char_p(s, fend, enc);
5030 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5038 Inc(s, fend, mb_enc, enc);
5047 if (p == skiproot(buf, p + !!*p) - 1) p++;
5050 rb_enc_check(fname, result);
5057str_shrink(
VALUE str)
5059 rb_str_resize(str, RSTRING_LEN(str));
5063#define expand_path(fname, dname, abs_mode, long_name, result) \
5064 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5066#define check_expand_path_args(fname, dname) \
5067 (((fname) = rb_get_path(fname)), \
5068 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5071file_expand_path_1(
VALUE fname,
long extra_capa)
5074 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5080 check_expand_path_args(fname, dname);
5081 return expand_path(fname, dname, 0, 1,
Qfalse);
5085rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5087 return expand_path(fname, dname, 0, 0,
Qfalse);
5091rb_file_s_expand_path(
int argc,
const VALUE *argv)
5094 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5140 return rb_file_s_expand_path(c, v);
5146 check_expand_path_args(fname, dname);
5147 return expand_path(fname, dname, 1, 1,
Qfalse);
5151rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5154 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5197 return rb_file_s_absolute_path(c, v);
5225 VALUE path = rb_get_path(fname);
5227 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5231enum rb_realpath_mode {
5235 RB_REALPATH_MODE_MAX
5239realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5240 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5242 const char *pend = unresolved + strlen(unresolved);
5246 while (unresolved < pend) {
5247 const char *testname = unresolved;
5248 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5249 long testnamelen = unresolved_firstsep - unresolved;
5250 const char *unresolved_nextname = unresolved_firstsep;
5251 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5252 unresolved_nextname++;
5253 unresolved = unresolved_nextname;
5254 if (testnamelen == 1 && testname[0] ==
'.') {
5256 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5257 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5258 bool mb_enc = enc_mbclen_needed(enc);
5259 const char *resolved_str = RSTRING_PTR(*resolvedp);
5260 const char *resolved_names = resolved_str + *prefixlenp;
5261 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5262 long len = lastsep ? lastsep - resolved_names : 0;
5263 rb_str_resize(*resolvedp, *prefixlenp +
len);
5269 if (*prefixlenp < RSTRING_LEN(testpath))
5271#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5272 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5273 const char *prefix = RSTRING_PTR(testpath);
5274 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5279 checkval = rb_hash_aref(loopcheck, testpath);
5280 if (!
NIL_P(checkval)) {
5281 if (checkval ==
ID2SYM(resolving)) {
5282 if (mode == RB_REALPATH_CHECK) {
5286 rb_syserr_fail_path(ELOOP, testpath);
5295 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5298 if (e == ENOENT && !
NIL_P(fallback)) {
5299 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5304 if (mode == RB_REALPATH_CHECK)
return -1;
5306 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5307 rb_syserr_fail_path(e, testpath);
5308 *resolvedp = testpath;
5312 rb_syserr_fail_path(e, testpath);
5316 if (S_ISLNK(sbuf.st_mode)) {
5319 const char *link_prefix, *link_names;
5320 long link_prefixlen;
5321 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5322 link = rb_readlink(testpath, enc);
5323 link_prefix = RSTRING_PTR(link);
5324 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5325 link_prefixlen = link_names - link_prefix;
5326 if (link_prefixlen > 0) {
5330 tmpenc = fs_enc_check(*resolvedp, link);
5333 *prefixlenp = link_prefixlen;
5335 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5336 loopcheck, mode, !*unresolved_firstsep))
5345 rb_hash_aset(loopcheck, s, s);
5346 *resolvedp = testpath;
5355rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5359 VALUE unresolved_path;
5364 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5365 char *ptr, *prefixptr = NULL, *pend;
5370 if (!
NIL_P(basedir)) {
5375 enc = rb_enc_get(unresolved_path);
5376 unresolved_path = TO_OSPATH(unresolved_path);
5378 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5379 if (ptr != path_names) {
5380 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5384 if (!
NIL_P(basedir)) {
5386 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5387 if (ptr != basedir_names) {
5393 curdir = rb_dir_getwd_ospath();
5395 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5400 pend = prefixptr + prefixlen;
5401 bool mb_enc = enc_mbclen_needed(enc);
5402 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5404 prefixlen = ++ptr - prefixptr;
5407#ifdef FILE_ALT_SEPARATOR
5408 while (prefixptr < ptr) {
5409 if (*prefixptr == FILE_ALT_SEPARATOR) {
5412 Inc(prefixptr, pend, mb_enc, enc);
5416 switch (rb_enc_to_index(enc)) {
5417 case ENCINDEX_ASCII_8BIT:
5418 case ENCINDEX_US_ASCII:
5419 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5422 loopcheck = rb_hash_new();
5424 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5427 if (basedir_names) {
5428 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5431 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5434 if (origenc && origenc != rb_enc_get(resolved)) {
5436 rb_enc_associate(resolved, origenc);
5448static VALUE rb_file_join(
long argc,
VALUE *args);
5450#ifndef HAVE_REALPATH
5452rb_check_realpath_emulate_try(
VALUE arg)
5455 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5459rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5463#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5464 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5466# include <sys/syslimits.h>
5467# define NEEDS_REALPATH_BUFFER 1
5471rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5474 VALUE unresolved_path;
5475 char *resolved_ptr = NULL;
5477# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5478 char resolved_buffer[PATH_MAX];
5480 char *
const resolved_buffer = NULL;
5483 if (mode == RB_REALPATH_DIR) {
5484 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5488 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5489 VALUE paths[2] = {basedir, unresolved_path};
5490 unresolved_path = rb_file_join(2, paths);
5492 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5494 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5503 if (
errno == ENOTSUP ||
5505 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5506 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5509 if (mode == RB_REALPATH_CHECK) {
5512 rb_sys_fail_path(unresolved_path);
5514 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5515# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5519# if !defined(__linux__) && !defined(__APPLE__)
5523 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5524 if (mode == RB_REALPATH_CHECK) {
5527 rb_sys_fail_path(unresolved_path);
5531 if (origenc && origenc != rb_enc_get(resolved)) {
5535 rb_enc_associate(resolved, origenc);
5538 if (is_broken_string(resolved)) {
5539 rb_enc_associate(resolved, rb_filesystem_encoding());
5540 if (is_broken_string(resolved)) {
5541 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5548 if (mode == RB_REALPATH_CHECK) {
5552 arg[2] = (
VALUE)origenc;
5555 rb_check_realpath_emulate_rescue,
Qnil);
5558 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5564rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5566 const enum rb_realpath_mode mode =
5567 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5568 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5574 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5591rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5594 VALUE path = argv[0];
5596 return rb_realpath_internal(basedir, path, 1);
5612rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5615 VALUE path = argv[0];
5617 return rb_realpath_internal(basedir, path, 0);
5621rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5625 const char *s, *last;
5627 if (!e || !l2)
return 0;
5629 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5630 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5631 if (c ==
'.')
return l0;
5636 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5641 if (l1 < l2)
return l1;
5644 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5645#if CASEFOLD_FILESYSTEM
5646#define fncomp strncasecmp
5648#define fncomp strncmp
5650 if (fncomp(s, e, l2) == 0) {
5656static inline const char *
5657enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5659 const char *p, *q, *e, *end;
5662 long len = (alllen ? (size_t)*alllen : strlen(name));
5669 name = skipprefix(name, end, mb_enc, enc);
5670#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5671 const char *root = name;
5674 while (name < end && isdirsep(*name)) {
5681#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5685#ifdef DOSISH_DRIVE_LETTER
5686 else if (*p ==
':') {
5699 p = strrdirsep(name, end, mb_enc, enc);
5704 while (isdirsep(*p)) {
5709 n = ntfs_tail(p, end, mb_enc, enc) - p;
5711 n = chompdirsep(p, end, mb_enc, enc) - p;
5713 for (q = p; q - p < n && *q ==
'.'; q++);
5714 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5715 if (*q ==
'.') e = q;
5735ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5737 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5781 const char *name, *p, *fp = 0;
5787 CheckPath(fname, name);
5791 check_path_encoding(fext);
5793 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5794 enc = rb_str_enc_get(fname);
5797 n = RSTRING_LEN(fname);
5798 if (n <= 0 || !*name) {
5799 return rb_enc_str_new(0, 0, enc);
5802 bool mb_enc = enc_mbclen_needed(enc);
5803 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5809 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5814 if (f == RSTRING_LEN(fname)) {
5819 return rb_enc_str_new(p, f, enc);
5822static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5845rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5851 return rb_file_dirname_n(argv[0], n);
5857 return rb_file_dirname_n(fname, 1);
5861rb_file_dirname_n(
VALUE fname,
int n)
5863 const char *name, *root, *p, *end;
5866 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5867 CheckPath(fname, name);
5868 end = name + RSTRING_LEN(fname);
5870 bool mb_enc = !rb_str_enc_fastpath(fname);
5873 root = skiproot(name, end);
5875 if (root > name + 1 && isdirsep(*name))
5876 root = skipprefix(name = root - 2, end, mb_enc, enc);
5878 if (root > name + 1)
5881 if (n > (end - root + 1) / 2) {
5887 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5896 return rb_enc_str_new(
".", 1, enc);
5898#ifdef DOSISH_DRIVE_LETTER
5899 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5900 const char *top = skiproot(name + 2, end);
5901 dirname = rb_enc_str_new(name, 3, enc);
5906 dirname = rb_enc_str_new(name, p - name, enc);
5907#ifdef DOSISH_DRIVE_LETTER
5908 if (root == name + 2 && p == root && name[1] ==
':')
5914static inline const char *
5915enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5917 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5919 p = strrdirsep(name, end, mb_enc, enc);
5923 do name = ++p;
while (isdirsep(*p));
5926 while (*p && *p ==
'.') p++;
5928 if (*p ==
'.' || istrailinggarbage(*p)) {
5930 const char *last = p++, *dot = last;
5931 while (istrailinggarbage(*p)) {
5932 if (*p ==
'.') dot = p;
5935 if (!*p || isADS(*p)) {
5939 if (*last ==
'.' || dot > last) e = dot;
5946 else if (isADS(*p)) {
5950 else if (isdirsep(*p))
5952 Inc(p, end, mb_enc, enc);
5957 if (!e || e == name)
5982 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
6029 CheckPath(fname, name);
6030 long len = RSTRING_LEN(fname);
6033 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
6036 bool mb_enc = !rb_str_enc_fastpath(fname);
6039 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
6040 return rb_enc_str_new(ext,
len, enc);
6064 return rb_get_path(fname);
6096file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6098 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6099 return rb_file_join_ary(arg);
6103rb_file_join_ary(
VALUE ary)
6107 const char *name, *tail;
6117 check_path_encoding(tmp);
6118 len += RSTRING_LEN(tmp);
6126 RBASIC_CLEAR_CLASS(result);
6129 switch (OBJ_BUILTIN_TYPE(tmp)) {
6131 if (!checked) check_path_encoding(tmp);
6136 rb_raise(rb_eArgError,
"recursive array");
6148 rb_enc_copy(result, tmp);
6151 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6152 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6155 else if (tail == name +
len) {
6159 enc = fs_enc_check(result, tmp);
6161 rb_enc_associate(result, enc);
6169rb_file_join_fastpath(
long argc,
VALUE *args)
6174 for (i = 0; i < argc; i++) {
6175 VALUE tmp = args[i];
6177 size += RSTRING_LEN(tmp);
6190 const char *name = RSTRING_PTR(result);
6191 for (i = 1; i < argc; i++) {
6192 VALUE tmp = args[i];
6193 long len = RSTRING_LEN(result);
6199 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6202 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6207 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6214 rb_enc_associate(result, new_enc);
6215 encidx = rb_enc_to_index(new_enc);
6221 rb_str_null_check(result);
6226rb_file_join(
long argc,
VALUE *args)
6228 if (RB_UNLIKELY(argc == 0)) {
6232 VALUE result = rb_file_join_fastpath(argc, args);
6233 if (RB_LIKELY(result)) {
6253rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6255 return rb_file_join(argc, argv);
6258#if defined(HAVE_TRUNCATE)
6259struct truncate_arg {
6265nogvl_truncate(
void *ptr)
6267 struct truncate_arg *ta = ptr;
6268 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6300 struct truncate_arg ta;
6305 path = rb_str_encode_ospath(path);
6308 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6310 rb_sys_fail_path(path);
6314#define rb_file_s_truncate rb_f_notimplement
6317#if defined(HAVE_FTRUNCATE)
6318struct ftruncate_arg {
6324nogvl_ftruncate(
void *ptr)
6326 struct ftruncate_arg *fa = ptr;
6328 return (
VALUE)ftruncate(fa->fd, fa->pos);
6369 struct ftruncate_arg fa;
6376 rb_io_flush_raw(obj, 0);
6378 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6379 rb_sys_fail_path(fptr->
pathv);
6384#define rb_file_truncate rb_f_notimplement
6401#include <winerror.h>
6405rb_thread_flock(
void *data)
6408 int old_errno =
errno;
6410 int *op = data, ret = flock(op[0], op[1]);
6413 if (GetLastError() == ERROR_NOT_LOCKED) {
6471 op[1] = op1 =
NUM2INT(operation);
6476 rb_io_flush_raw(obj, 0);
6478 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6483#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6486 if (op1 & LOCK_NB)
return Qfalse;
6489 time.tv_usec = 100 * 1000;
6495#if defined(ERESTART)
6501 rb_syserr_fail_path(e, fptr->
pathv);
6508test_check(
int n,
int argc,
VALUE *argv)
6514 for (i=1; i<n; i++) {
6521#define CHECK(n) test_check((n), argc, argv)
6613 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6617 return rb_file_blockdev_p(0, argv[1]);
6620 return rb_file_chardev_p(0, argv[1]);
6623 return rb_file_directory_p(0, argv[1]);
6626 return rb_file_exist_p(0, argv[1]);
6629 return rb_file_file_p(0, argv[1]);
6632 return rb_file_sgid_p(0, argv[1]);
6635 return rb_file_grpowned_p(0, argv[1]);
6638 return rb_file_sticky_p(0, argv[1]);
6641 return rb_file_symlink_p(0, argv[1]);
6644 return rb_file_owned_p(0, argv[1]);
6647 return rb_file_rowned_p(0, argv[1]);
6650 return rb_file_pipe_p(0, argv[1]);
6653 return rb_file_readable_p(0, argv[1]);
6656 return rb_file_readable_real_p(0, argv[1]);
6659 return rb_file_size_p(0, argv[1]);
6662 return rb_file_socket_p(0, argv[1]);
6665 return rb_file_suid_p(0, argv[1]);
6668 return rb_file_writable_p(0, argv[1]);
6671 return rb_file_writable_real_p(0, argv[1]);
6674 return rb_file_executable_p(0, argv[1]);
6677 return rb_file_executable_real_p(0, argv[1]);
6680 return rb_file_zero_p(0, argv[1]);
6684 if (strchr(
"MAC", cmd)) {
6686 VALUE fname = argv[1];
6689 if (
rb_stat(fname, &st) == -1) {
6692 rb_syserr_fail_path(e, fname);
6697 return stat_atime(&st);
6699 return stat_mtime(&st);
6701 return stat_ctime(&st);
6707 return rb_file_identical_p(0, argv[1], argv[2]);
6710 if (strchr(
"=<>", cmd)) {
6711 struct stat st1, st2;
6718 t1 = stat_mtimespec(&st1);
6719 t2 = stat_mtimespec(&st2);
6723 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6727 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6728 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6732 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6733 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6740 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6743 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6797rb_stat_s_alloc(
VALUE klass)
6818 fname = rb_str_encode_ospath(fname);
6820 rb_sys_fail_path(fname);
6827 rb_st->initialized =
true;
6844 *copy_rb_st = *orig_rb_st;
6886rb_stat_ftype(
VALUE obj)
6888 return rb_file_ftype(get_stat(obj)->ST_(mode));
6904 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6931 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
6968 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
7000 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
7022 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
7042 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7077rb_stat_owned(
VALUE obj)
7079 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7084rb_stat_rowned(
VALUE obj)
7086 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7107rb_stat_grpowned(
VALUE obj)
7110 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7139 rb_io_stat_data *st = get_stat(obj);
7142 if (geteuid() == 0)
return Qtrue;
7145 if (rb_stat_owned(obj))
7146 return RBOOL(st->ST_(mode) & S_IRUSR);
7149 if (rb_stat_grpowned(obj))
7150 return RBOOL(st->ST_(mode) & S_IRGRP);
7153 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7171 rb_io_stat_data *st = get_stat(obj);
7174 if (getuid() == 0)
return Qtrue;
7177 if (rb_stat_rowned(obj))
7178 return RBOOL(st->ST_(mode) & S_IRUSR);
7181 if (rb_group_member(get_stat(obj)->ST_(gid)))
7182 return RBOOL(st->ST_(mode) & S_IRGRP);
7185 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7213rb_stat_wr(
VALUE obj)
7216 rb_io_stat_data *st = get_stat(obj);
7217 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7218 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7250 rb_io_stat_data *st = get_stat(obj);
7253 if (geteuid() == 0)
return Qtrue;
7256 if (rb_stat_owned(obj))
7257 return RBOOL(st->ST_(mode) & S_IWUSR);
7260 if (rb_stat_grpowned(obj))
7261 return RBOOL(st->ST_(mode) & S_IWGRP);
7264 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7285 rb_io_stat_data *st = get_stat(obj);
7288 if (getuid() == 0)
return Qtrue;
7291 if (rb_stat_rowned(obj))
7292 return RBOOL(st->ST_(mode) & S_IWUSR);
7295 if (rb_group_member(get_stat(obj)->ST_(gid)))
7296 return RBOOL(st->ST_(mode) & S_IWGRP);
7299 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7328rb_stat_ww(
VALUE obj)
7331 rb_io_stat_data *st = get_stat(obj);
7332 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7333 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7375 rb_io_stat_data *st = get_stat(obj);
7378 if (geteuid() == 0) {
7379 return RBOOL(st->ST_(mode) & S_IXUGO);
7383 if (rb_stat_owned(obj))
7384 return RBOOL(st->ST_(mode) & S_IXUSR);
7387 if (rb_stat_grpowned(obj))
7388 return RBOOL(st->ST_(mode) & S_IXGRP);
7391 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7407 rb_io_stat_data *st = get_stat(obj);
7410 if (getuid() == 0) {
7411 return RBOOL(st->ST_(mode) & S_IXUGO);
7415 if (rb_stat_rowned(obj))
7416 return RBOOL(st->ST_(mode) & S_IXUSR);
7419 if (rb_group_member(get_stat(obj)->ST_(gid)))
7420 return RBOOL(st->ST_(mode) & S_IXGRP);
7423 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7446 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7491 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7523 rb_off_t size = get_stat(obj)->ST_(size);
7525 if (size == 0)
return Qnil;
7558rb_stat_suid(
VALUE obj)
7561 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7595rb_stat_sgid(
VALUE obj)
7598 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7632rb_stat_sticky(
VALUE obj)
7635 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7640#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7641#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7652nogvl_mkfifo(
void *ptr)
7654 struct mkfifo_arg *ma = ptr;
7656 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7684 struct mkfifo_arg ma;
7693 path = rb_str_encode_ospath(path);
7694 ma.path = RSTRING_PTR(path);
7695 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7696 rb_sys_fail_path(path);
7701#define rb_file_s_mkfifo rb_f_notimplement
7704static VALUE rb_mFConst;
7707rb_file_const(
const char *name,
VALUE value)
7709 rb_define_const(rb_mFConst, name, value);
7713rb_is_absolute_path(
const char *path)
7715#ifdef DOSISH_DRIVE_LETTER
7716 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7719 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7722 if (path[0] ==
'/')
return 1;
7728ruby_is_fd_loadable(
int fd)
7735 if (fstat(fd, &st) < 0)
7738 if (S_ISREG(st.st_mode))
7741 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7744 if (S_ISDIR(st.st_mode))
7755rb_file_load_ok(
const char *path)
7762 int mode = (O_RDONLY |
7763#if defined O_NONBLOCK
7765#elif defined O_NDELAY
7771 if (!rb_gc_for_fd(
errno))
return 0;
7773 if (fd < 0)
return 0;
7776 ret = ruby_is_fd_loadable(fd);
7783is_explicit_relative(
const char *path)
7785 if (*path++ !=
'.')
return 0;
7786 if (*path ==
'.') path++;
7787 return isdirsep(*path);
7793 int encidx = rb_enc_get_index(orig);
7794 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7795 encidx = rb_filesystem_encindex();
7796 rb_enc_associate_index(path, encidx);
7804nav_component_p(
const char *s,
const char *send)
7806 if ((send - s) >= 2 && s[0] ==
'.') {
7807 return s[1] ==
'.' || isdirsep(s[1]);
7813fname_need_expansion_p(
VALUE fname)
7815 const char *s = RSTRING_PTR(fname);
7816 const long len = RSTRING_LEN(fname);
7817 const char *send = s +
len;
7819 if (nav_component_p(s, send)) {
7824 bool mbenc = enc_mbclen_needed(enc);
7826 s = enc_path_next(s, send, mbenc, enc);
7828 if (nav_component_p(s, send)) {
7832 s = enc_path_next(s, send, mbenc, enc);
7838expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7840 long dname_len = RSTRING_LEN(dname);
7841 const char *dname_ptr = RSTRING_PTR(dname);
7845 if (need_expansion || dname_ptr[0] ==
'~') {
7846 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7851 if (!isdirsep(dname_ptr[dname_len - 1])) {
7860rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
7863 VALUE fname = *filep;
7867 if (!ext[0])
return 0;
7870 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7871 f = RSTRING_PTR(fname);
7876 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7877 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7878 fnlen = RSTRING_LEN(fname);
7879 for (i=0; ext[i]; i++) {
7881 if (rb_file_load_ok(RSTRING_PTR(fname))) {
7882 *filep = copy_path_class(fname, *filep);
7890 long expanded_load_path_maxlen;
7891 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7892 if (!load_path)
return 0;
7895 RBASIC_CLEAR_CLASS(fname);
7896 fnlen = RSTRING_LEN(fname);
7897 bool need_expansion = fname_need_expansion_p(fname);
7900 rb_enc_associate_index(tmp, rb_usascii_encindex());
7902 for (j=0; ext[j]; j++) {
7904 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
7906 if (!RSTRING_LEN(dname))
continue;
7907 expand_feature(fname, dname, tmp, need_expansion);
7909 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7910 *filep = copy_path_class(tmp, *filep);
7916 rb_str_resize(tmp, 0);
7929 path = copy_path_class(file_expand_path_1(path, 0), path);
7930 f = RSTRING_PTR(path);
7934 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7935 if (!rb_file_load_ok(f))
return 0;
7937 path = copy_path_class(file_expand_path_1(path, 0), path);
7941 long expanded_load_path_maxlen;
7942 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7945 bool need_expansion = fname_need_expansion_p(path);
7947 rb_enc_associate_index(tmp, rb_usascii_encindex());
7948 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
7950 if (!RSTRING_LEN(dname))
continue;
7951 expand_feature(path, dname, tmp, need_expansion);
7953 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7954 return copy_path_class(tmp, path);
7957 rb_str_resize(tmp, 0);
7965#define define_filetest_function(name, func, argc) do { \
7966 rb_define_module_function(rb_mFileTest, name, func, argc); \
7967 rb_define_singleton_method(rb_cFile, name, func, argc); \
7970const char ruby_null_device[] =
8773#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8774 rb_CFString_class_initialize_before_fork();
8782 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8783 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8784 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8785 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8786 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8787 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8788 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8789 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8790 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8791 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8792 define_filetest_function(
"file?", rb_file_file_p, 1);
8793 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8794 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8795 define_filetest_function(
"size?", rb_file_size_p, 1);
8796 define_filetest_function(
"size", rb_file_s_size, 1);
8797 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8798 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8800 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8801 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8802 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8804 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8805 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8807 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8808 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8809 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8811 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8849 separator = rb_fstring_lit(
"/");
8851 rb_define_const(
rb_cFile,
"Separator", separator);
8853 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9202 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9205 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9207 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9209 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9211 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9213 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9215 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9216#if defined(O_NDELAY) || defined(O_NONBLOCK)
9218# define O_NONBLOCK O_NDELAY
9221 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9224 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9227 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9233 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9234#ifndef O_SHARE_DELETE
9235# define O_SHARE_DELETE 0
9238 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9241 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9245 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9249 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9253 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9257 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9261 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9265 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9269 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9271 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9273 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9275 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9278 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.