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)
2207rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2213 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2214 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2234 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2249rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2251 return RBOOL(rb_access(fname, W_OK) >= 0);
2271rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2277 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2278 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2321 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2340rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2342 return RBOOL(rb_access(fname, X_OK) >= 0);
2346# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2378 return RBOOL(S_ISREG(st.st_mode));
2424 return RBOOL(st.st_size == 0);
2462 if (st.st_size == 0)
return Qnil;
2496 return RBOOL(st.st_uid == geteuid());
2505 return RBOOL(st.st_uid == getuid());
2537 if (rb_group_member(st.st_gid))
return Qtrue;
2542#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2544check3rdbyte(
VALUE fname,
int mode)
2549 return RBOOL(st.st_mode & mode);
2582 return check3rdbyte(fname, S_ISUID);
2617 return check3rdbyte(fname, S_ISGID);
2653 return check3rdbyte(fname, S_ISVTX);
2690 struct stat st1, st2;
2694 if (st1.st_dev != st2.st_dev)
return Qfalse;
2695 if (st1.st_ino != st2.st_ino)
return Qfalse;
2699 return rb_w32_file_identical_p(fname1, fname2);
2725 if (
rb_stat(fname, &st) < 0) {
2728 rb_syserr_fail_path(e, fname);
2734rb_file_ftype(mode_t mode)
2738 if (S_ISREG(mode)) {
2741 else if (S_ISDIR(mode)) {
2744 else if (S_ISCHR(mode)) {
2745 t =
"characterSpecial";
2748 else if (S_ISBLK(mode)) {
2753 else if (S_ISFIFO(mode)) {
2758 else if (S_ISLNK(mode)) {
2763 else if (S_ISSOCK(mode)) {
2771 return rb_fstring_cstr(t);
2813 fname = rb_str_encode_ospath(fname);
2815 rb_sys_fail_path(fname);
2818 return rb_file_ftype(st.st_mode);
2866 if (
rb_stat(fname, &st) < 0) {
2869 rb_syserr_fail_path(e, fname);
2871 return stat_time(stat_atimespec(&st));
2898rb_file_atime(
VALUE obj)
2904 if (fstat(fptr->
fd, &st) == -1) {
2905 rb_sys_fail_path(fptr->
pathv);
2907 return stat_time(stat_atimespec(&st));
2947 if (
rb_stat(fname, &st) < 0) {
2950 rb_syserr_fail_path(e, fname);
2952 return stat_time(stat_mtimespec(&st));
2977rb_file_mtime(
VALUE obj)
2983 if (fstat(fptr->
fd, &st) == -1) {
2984 rb_sys_fail_path(fptr->
pathv);
2986 return stat_time(stat_mtimespec(&st));
3029 if (
rb_stat(fname, &st) < 0) {
3032 rb_syserr_fail_path(e, fname);
3034 return stat_time(stat_ctimespec(&st));
3051rb_file_ctime(
VALUE obj)
3057 if (fstat(fptr->
fd, &st) == -1) {
3058 rb_sys_fail_path(fptr->
pathv);
3060 return stat_time(stat_ctimespec(&st));
3063#if defined(HAVE_STAT_BIRTHTIME)
3094 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3097 rb_syserr_fail_path(e, fname);
3099 return statx_birthtime(&st);
3102# define rb_file_s_birthtime rb_f_notimplement
3105#if defined(HAVE_STAT_BIRTHTIME)
3126rb_file_birthtime(
VALUE obj)
3132 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3133 rb_sys_fail_path(fptr->
pathv);
3135 return statx_birthtime(&st);
3138# define rb_file_birthtime rb_f_notimplement
3150 rb_io_flush_raw(file, 0);
3153 if (fstat(fptr->
fd, &st) == -1) {
3154 rb_sys_fail_path(fptr->
pathv);
3180file_size(
VALUE self)
3182 return OFFT2NUM(rb_file_size(self));
3191nogvl_chmod(
void *ptr)
3194 int ret = chmod(data->path, data->mode);
3195 return (
void *)(
VALUE)ret;
3199rb_chmod(
const char *path, mode_t mode)
3205 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3209chmod_internal(
const char *path,
void *mode)
3211 return chmod(path, *(mode_t *)mode);
3249 return apply2files(chmod_internal, argc, argv, &mode);
3253struct nogvl_fchmod_data {
3259io_blocking_fchmod(
void *ptr)
3261 struct nogvl_fchmod_data *data = ptr;
3262 int ret = fchmod(data->fd, data->mode);
3267rb_fchmod(
struct rb_io* io, mode_t mode)
3270 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3271 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3303#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3311 if (rb_fchmod(fptr, mode) == -1) {
3312 if (HAVE_FCHMOD ||
errno != ENOSYS)
3313 rb_sys_fail_path(fptr->
pathv);
3316 if (!HAVE_FCHMOD)
return INT2FIX(0);
3319#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3321 path = rb_str_encode_ospath(fptr->
pathv);
3322 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3323 rb_sys_fail_path(fptr->
pathv);
3329#if defined(HAVE_LCHMOD)
3331lchmod_internal(
const char *path,
void *mode)
3333 return lchmod(path, *(mode_t *)mode);
3370 return apply2files(lchmod_internal, argc, argv, &mode);
3373#define rb_file_s_lchmod rb_f_notimplement
3376static inline rb_uid_t
3380 return (rb_uid_t)-1;
3385static inline rb_gid_t
3389 return (rb_gid_t)-1;
3400chown_internal(
const char *path,
void *arg)
3403 return chown(path, args->owner, args->group);
3457 arg.owner = to_uid(*argv++);
3458 arg.group = to_gid(*argv++);
3460 return apply2files(chown_internal, argc, argv, &arg);
3472nogvl_chown(
void *ptr)
3475 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3479rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3482 .as = {.path = path},
3483 .new = {.owner = owner, .group = group},
3485 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3490nogvl_fchown(
void *ptr)
3493 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3497rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3502 .new = {.owner = owner, .group = group},
3504 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3538 path = rb_str_encode_ospath(fptr->
pathv);
3539 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3540 rb_sys_fail_path(fptr->
pathv);
3542 if (rb_fchown(fptr->
fd, o, g) == -1)
3543 rb_sys_fail_path(fptr->
pathv);
3549#if defined(HAVE_LCHOWN)
3551lchown_internal(
const char *path,
void *arg)
3554 return lchown(path, args->owner, args->group);
3611 arg.owner = to_uid(*argv++);
3612 arg.group = to_gid(*argv++);
3614 return apply2files(lchown_internal, argc, argv, &arg);
3617#define rb_file_s_lchown rb_f_notimplement
3627NORETURN(
static void utime_failed(
struct apply_arg *));
3633 VALUE path = aa->fn[aa->i].path;
3636 if (ua->tsp && e == EINVAL) {
3639 VALUE atime = ua->atime;
3640 VALUE mtime = ua->mtime;
3642 if (!
NIL_P(atime)) {
3645 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3648 if (
NIL_P(a)) e[0] = m;
3664 rb_syserr_fail_path(e, path);
3668#if defined(HAVE_UTIMES)
3670# if !defined(HAVE_UTIMENSAT)
3672# elif defined(__APPLE__) && \
3673 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3675# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3676typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3680static inline utimensat_func *
3687# define utimensat rb_utimensat()
3694utime_internal(
const char *path,
void *arg)
3697 const struct timespec *tsp = v->tsp;
3698 struct timeval tvbuf[2], *tvp = NULL;
3700#if defined(HAVE_UTIMENSAT)
3701# if defined(__APPLE__)
3702 const int try_utimensat = utimensat != NULL;
3703 const int try_utimensat_follow = utimensat != NULL;
3705# define TRY_UTIMENSAT 1
3706 static int try_utimensat = 1;
3707# ifdef AT_SYMLINK_NOFOLLOW
3708 static int try_utimensat_follow = 1;
3710 const int try_utimensat_follow = 0;
3715 if (v->follow ? try_utimensat_follow : try_utimensat) {
3716# ifdef AT_SYMLINK_NOFOLLOW
3718 flags = AT_SYMLINK_NOFOLLOW;
3722 int result = utimensat(AT_FDCWD, path, tsp, flags);
3723# ifdef TRY_UTIMENSAT
3724 if (result < 0 &&
errno == ENOSYS) {
3725# ifdef AT_SYMLINK_NOFOLLOW
3726 try_utimensat_follow = 0;
3738 tvbuf[0].tv_sec = tsp[0].tv_sec;
3739 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3740 tvbuf[1].tv_sec = tsp[1].tv_sec;
3741 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3745 if (v->follow)
return lutimes(path, tvp);
3747 return utimes(path, tvp);
3752#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3760utime_internal(
const char *path,
void *arg)
3764 struct utimbuf utbuf, *utp = NULL;
3766 utbuf.actime = tsp[0].tv_sec;
3767 utbuf.modtime = tsp[1].tv_sec;
3770 return utime(path, utp);
3775utime_internal_i(
int argc,
VALUE *argv,
int follow)
3778 struct timespec tss[2], *tsp = NULL;
3781 args.atime = *argv++;
3782 args.mtime = *argv++;
3784 args.follow = follow;
3786 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3789 if (args.atime == args.mtime)
3796 return apply2files(utime_internal, argc, argv, &args);
3813 return utime_internal_i(argc, argv, FALSE);
3816#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3874 return utime_internal_i(argc, argv, TRUE);
3877#define rb_file_s_lutime rb_f_notimplement
3880#ifdef RUBY_FUNCTION_NAME_STRING
3881# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3883# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3885#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3886NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3888syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3892 const int max_pathlen = MAX_PATH;
3894 const int max_pathlen = MAXPATHLEN;
3905#ifdef RUBY_FUNCTION_NAME_STRING
3906 rb_syserr_fail_path_in(func, e, str);
3908 rb_syserr_fail_path(e, str);
3943 from = rb_str_encode_ospath(from);
3944 to = rb_str_encode_ospath(to);
3947 sys_fail2(from, to);
3952#define rb_file_s_link rb_f_notimplement
3993 from = rb_str_encode_ospath(from);
3994 to = rb_str_encode_ospath(to);
3997 sys_fail2(from, to);
4002#define rb_file_s_symlink rb_f_notimplement
4030 return rb_readlink(path, rb_filesystem_encoding());
4033struct readlink_arg {
4040nogvl_readlink(
void *ptr)
4042 struct readlink_arg *ra = ptr;
4044 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4048readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4050 struct readlink_arg ra;
4052 ra.path = RSTRING_PTR(path);
4053 ra.buf = RSTRING_PTR(buf);
4056 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4067 path = rb_str_encode_ospath(path);
4068 v = rb_enc_str_new(0, size, enc);
4069 while ((rv = readlink_without_gvl(path, v, size)) == size
4071 || (rv < 0 &&
errno == ERANGE)
4080 rb_str_resize(v, 0);
4081 rb_syserr_fail_path(e, path);
4083 rb_str_resize(v, rv);
4088#define rb_file_s_readlink rb_f_notimplement
4092unlink_internal(
const char *path,
void *arg)
4094 return unlink(path);
4123rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4125 return apply2files(unlink_internal, argc, argv, 0);
4134no_gvl_rename(
void *ptr)
4138 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4216 f = rb_str_encode_ospath(from);
4217 t = rb_str_encode_ospath(to);
4220#if defined __CYGWIN__
4223 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4228 if (chmod(ra.dst, 0666) == 0 &&
4229 unlink(ra.dst) == 0 &&
4230 rename(ra.src, ra.dst) == 0)
4234 syserr_fail2(e, from, to);
4268 rb_error_arity(argc, 0, 1);
4276#if defined __CYGWIN__ || defined DOSISH
4278#define DOSISH_DRIVE_LETTER
4279#define FILE_ALT_SEPARATOR '\\'
4281#ifdef FILE_ALT_SEPARATOR
4282#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4284static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4287#define isdirsep(x) ((x) == '/')
4299#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4300#define isADS(x) ((x) == ':')
4302#define istrailinggarbage(x) 0
4305#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4307#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4308#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4310#if defined(DOSISH_UNC)
4311#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4313#define has_unc(buf) 0
4316#ifdef DOSISH_DRIVE_LETTER
4318has_drive_letter(
const char *buf)
4320 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4345 if (chdir(drive) == 0) {
4346 drvcwd = rb_dir_getwd_ospath();
4358not_same_drive(
VALUE path,
int drive)
4360 const char *p = RSTRING_PTR(path);
4361 if (RSTRING_LEN(path) < 2)
return 0;
4362 if (has_drive_letter(p)) {
4373skiproot(
const char *path,
const char *end)
4375#ifdef DOSISH_DRIVE_LETTER
4376 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4378 while (path < end && isdirsep(*path)) path++;
4379 return (
char *)path;
4383enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4385 while (s < e && !isdirsep(*s)) {
4386 Inc(s, e, mb_enc, enc);
4391#define nextdirsep rb_enc_path_next
4395 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4398#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4399#define skipprefix enc_path_skip_prefix
4401#define skipprefix(path, end, mb_enc, enc) (path)
4404enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4406#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4408 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4410 while (path < end && isdirsep(*path)) path++;
4411 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4412 path + 2 <= end && !isdirsep(path[1])) {
4413 path = enc_path_next(path + 1, end, mb_enc, enc);
4415 return (
char *)path;
4418#ifdef DOSISH_DRIVE_LETTER
4419 if (path + 2 <= end && has_drive_letter(path))
4420 return (
char *)(path + 2);
4423 return (
char *)path;
4427rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4429 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4433skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4435#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4436 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4437 while (p < end && isdirsep(*p)) p++;
4440 return skiproot(path, end);
4445rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4447 return skipprefixroot(path, end, enc);
4451enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4454 while (path < end) {
4455 if (isdirsep(*path)) {
4456 const char *tmp = path++;
4457 while (path < end && isdirsep(*path)) path++;
4458 if (path >= end)
break;
4462 Inc(path, end, mb_enc, enc);
4468rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4470 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4474strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4476 if (RB_UNLIKELY(mb_enc)) {
4477 return enc_path_last_separator(path, end, mb_enc, enc);
4480 const char *cursor = end - 1;
4482 while (cursor >= path && isdirsep(cursor[0])) {
4486 while (cursor >= path) {
4487 if (isdirsep(cursor[0])) {
4488 while (cursor > path && isdirsep(cursor[-1])) {
4491 return (
char *)cursor;
4499chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4501 while (path < end) {
4502 if (isdirsep(*path)) {
4503 const char *last = path++;
4504 while (path < end && isdirsep(*path)) path++;
4505 if (path >= end)
return (
char *)last;
4508 Inc(path, end, mb_enc, enc);
4511 return (
char *)path;
4517 if (path < end && isdirsep(*path)) path++;
4518 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4524 rb_encoding *enc = rb_enc_check_str(path1, path2);
4525 int encidx = rb_enc_to_index(enc);
4526 if (encidx == ENCINDEX_US_ASCII) {
4527 encidx = rb_enc_get_index(path1);
4528 if (encidx == ENCINDEX_US_ASCII)
4529 encidx = rb_enc_get_index(path2);
4530 enc = rb_enc_from_index(encidx);
4537ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4539 while (path < end && *path ==
'.') path++;
4540 while (path < end && !isADS(*path)) {
4541 if (istrailinggarbage(*path)) {
4542 const char *last = path++;
4543 while (path < end && istrailinggarbage(*path)) path++;
4544 if (path >= end || isADS(*path))
return (
char *)last;
4546 else if (isdirsep(*path)) {
4547 const char *last = path++;
4548 while (path < end && isdirsep(*path)) path++;
4549 if (path >= end)
return (
char *)last;
4550 if (isADS(*path)) path++;
4553 Inc(path, end, mb_enc, enc);
4556 return (
char *)path;
4560#define BUFCHECK(cond) do {\
4563 do {buflen *= 2;} while (cond);\
4564 rb_str_resize(result, buflen);\
4565 buf = RSTRING_PTR(result);\
4567 pend = buf + buflen;\
4571#define BUFINIT(result, buf, p, pend) do {\
4572 if (!result) { result = rb_usascii_str_new(0, 1); } \
4573 p = buf = RSTRING_PTR(result); \
4574 buflen = RSTRING_LEN(result); \
4575 pend = p + buflen; \
4579# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4581# define SKIPPATHSEP(p) 1
4584#define BUFCOPY(srcptr, srclen) do { \
4585 const int skip = SKIPPATHSEP(p); \
4586 rb_str_set_len(result, p-buf+skip); \
4587 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4589 memcpy(p, (srcptr), (srclen)); \
4593#define WITH_ROOTDIFF(stmt) do { \
4594 long rootdiff = root - buf; \
4596 root = buf + rootdiff; \
4600copy_home_path(
VALUE result,
const char *dir)
4606 dirlen = strlen(dir);
4607 rb_str_resize(result, dirlen);
4608 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4609 encidx = rb_filesystem_encindex();
4610 rb_enc_associate_index(result, encidx);
4611#if defined FILE_ALT_SEPARATOR
4613 bool mb_enc = enc_mbclen_needed(enc);
4614 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4615 if (*p == FILE_ALT_SEPARATOR) {
4627 VALUE dirname = rb_getpwdirnam_for_login(user);
4628 if (dirname ==
Qnil) {
4629 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4631 const char *dir = RSTRING_PTR(dirname);
4633 extern char *getlogin(
void);
4634 const char *pwPtr = 0;
4636 # define endpwent() ((void)0)
4637 const char *dir, *username = RSTRING_PTR(user);
4648 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4649 dir = pwPtr = getenv(
"HOME");
4651 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4654 copy_home_path(result, dir);
4660rb_default_home_dir(
VALUE result)
4662 const char *dir = getenv(
"HOME");
4664#if defined HAVE_PWD_H
4678 VALUE login_name = rb_getlogin();
4680# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4685 if (
NIL_P(login_name)) {
4686 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4690 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4691 if (
NIL_P(pw_dir)) {
4692 pw_dir = rb_getpwdiruid();
4693 if (
NIL_P(pw_dir)) {
4694 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4699 copy_home_path(result, RSTRING_PTR(pw_dir));
4700 rb_str_resize(pw_dir, 0);
4705 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4707 return copy_home_path(result, dir);
4713#if NORMALIZE_UTF8PATH
4714 VALUE path = rb_str_normalize_ospath(ptr,
len);
4715 rb_enc_associate(path, fsenc);
4718 return rb_enc_str_new(ptr,
len, fsenc);
4725 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4727 rb_enc_associate(dirname, fsenc);
4731 size_t dirlen = RSTRING_LEN(dirname);
4734 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4735 if (!rb_enc_compatible(fname, dirname)) {
4738 rb_enc_check(fname, dirname);
4739 rb_bug(
"unreachable");
4741 rb_encoding *direnc = fs_enc_check(fname, dirname);
4742 if (direnc != fsenc) {
4749 do {buflen *= 2;}
while (dirlen > buflen);
4750 rb_str_resize(result, buflen);
4751 buf = RSTRING_PTR(result);
4752 memcpy(buf, cwdp, dirlen);
4753 rb_enc_associate(result, *enc);
4754 return buf + dirlen;
4758rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4760 const char *s, *b, *fend;
4761 char *buf, *p, *pend, *root;
4762 size_t buflen, bdiff;
4763 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4766 fend = s + RSTRING_LEN(fname);
4767 enc = rb_str_enc_get(fname);
4768 bool mb_enc = enc_mbclen_needed(enc);
4769 if (!mb_enc &&
RTEST(dname)) {
4770 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4773 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4774 BUFINIT(result, buf, p, pend);
4777 if (s + 1 == fend || isdirsep(s[1])) {
4781 if (++s < fend) ++s;
4782 rb_default_home_dir(result);
4785 s = nextdirsep(b = s, fend, enc);
4788 BUFCHECK(bdiff + userlen >= buflen);
4789 memcpy(p, b, userlen);
4792 rb_enc_associate(result, enc);
4793 rb_home_dir_of(result, result);
4797 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4799 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4800 (
int)userlen, b, fname);
4803 rb_raise(rb_eArgError,
"non-absolute home");
4806 BUFINIT(result, buf, p, pend);
4809#ifdef DOSISH_DRIVE_LETTER
4811 else if (s + 1 < fend && has_drive_letter(s)) {
4812 BUFINIT(result, buf, p, pend);
4814 if (s + 2 < fend && isdirsep(s[2])) {
4817 BUFCHECK(bdiff + 2 >= buflen);
4821 rb_enc_copy(result, fname);
4826 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4827 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4828 BUFINIT(result, buf, p, pend);
4835 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4836 BUFINIT(result, buf, p, pend);
4840 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4843 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4848 else if (s == fend || !rb_is_absolute_path(s)) {
4850 if (!
NIL_P(dname)) {
4852 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4856 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4858 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
4863 result = resized_result;
4867 rb_enc_associate(result, fs_enc_check(result, fname));
4868 BUFINIT(result, buf, p, pend);
4872 VALUE cwd = rb_dir_getwd_ospath();
4876 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
4877 BUFINIT(result, buf, p, pend);
4880#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4881 if (s < fend && isdirsep(*s)) {
4884 p = skipprefix(buf, p, mb_enc, enc);
4888 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4891 BUFINIT(result, buf, p, pend);
4895 do s++;
while (s < fend && isdirsep(*s));
4898 BUFCHECK(bdiff >= buflen);
4899 memset(buf,
'/',
len);
4901 rb_enc_associate(result, fs_enc_check(result, fname));
4903 if (p > buf && p[-1] ==
'/')
4907 BUFCHECK(bdiff + 1 >= buflen);
4912 BUFCHECK(bdiff + 1 >= buflen);
4914 root = skipprefix(buf, p+1, mb_enc, enc);
4927 if (s+1 == fend || isdirsep(*(s+1))) {
4931 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
4941#if defined FILE_ALT_SEPARATOR
4942 case FILE_ALT_SEPARATOR:
4953#if defined FILE_ALT_SEPARATOR
4954 case FILE_ALT_SEPARATOR:
4957 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4965 int n = ignored_char_p(s, fend, enc);
4968 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4976 Inc(s, fend, mb_enc, enc);
4985 if (p == skiproot(buf, p + !!*p) - 1) p++;
4988 rb_enc_check(fname, result);
4995str_shrink(
VALUE str)
4997 rb_str_resize(str, RSTRING_LEN(str));
5001#define expand_path(fname, dname, abs_mode, long_name, result) \
5002 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5004#define check_expand_path_args(fname, dname) \
5005 (((fname) = rb_get_path(fname)), \
5006 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5009file_expand_path_1(
VALUE fname,
long extra_capa)
5012 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5018 check_expand_path_args(fname, dname);
5019 return expand_path(fname, dname, 0, 1,
Qfalse);
5023rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5025 return expand_path(fname, dname, 0, 0,
Qfalse);
5029rb_file_s_expand_path(
int argc,
const VALUE *argv)
5032 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5078 return rb_file_s_expand_path(c, v);
5084 check_expand_path_args(fname, dname);
5085 return expand_path(fname, dname, 1, 1,
Qfalse);
5089rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5092 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5135 return rb_file_s_absolute_path(c, v);
5163 VALUE path = rb_get_path(fname);
5165 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5169enum rb_realpath_mode {
5173 RB_REALPATH_MODE_MAX
5177realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5178 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5180 const char *pend = unresolved + strlen(unresolved);
5184 while (unresolved < pend) {
5185 const char *testname = unresolved;
5186 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5187 long testnamelen = unresolved_firstsep - unresolved;
5188 const char *unresolved_nextname = unresolved_firstsep;
5189 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5190 unresolved_nextname++;
5191 unresolved = unresolved_nextname;
5192 if (testnamelen == 1 && testname[0] ==
'.') {
5194 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5195 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5196 bool mb_enc = enc_mbclen_needed(enc);
5197 const char *resolved_str = RSTRING_PTR(*resolvedp);
5198 const char *resolved_names = resolved_str + *prefixlenp;
5199 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5200 long len = lastsep ? lastsep - resolved_names : 0;
5201 rb_str_resize(*resolvedp, *prefixlenp +
len);
5207 if (*prefixlenp < RSTRING_LEN(testpath))
5209#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5210 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5211 const char *prefix = RSTRING_PTR(testpath);
5212 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5217 checkval = rb_hash_aref(loopcheck, testpath);
5218 if (!
NIL_P(checkval)) {
5219 if (checkval ==
ID2SYM(resolving)) {
5220 if (mode == RB_REALPATH_CHECK) {
5224 rb_syserr_fail_path(ELOOP, testpath);
5233 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5236 if (e == ENOENT && !
NIL_P(fallback)) {
5237 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5242 if (mode == RB_REALPATH_CHECK)
return -1;
5244 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5245 rb_syserr_fail_path(e, testpath);
5246 *resolvedp = testpath;
5250 rb_syserr_fail_path(e, testpath);
5254 if (S_ISLNK(sbuf.st_mode)) {
5257 const char *link_prefix, *link_names;
5258 long link_prefixlen;
5259 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5260 link = rb_readlink(testpath, enc);
5261 link_prefix = RSTRING_PTR(link);
5262 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5263 link_prefixlen = link_names - link_prefix;
5264 if (link_prefixlen > 0) {
5268 tmpenc = fs_enc_check(*resolvedp, link);
5271 *prefixlenp = link_prefixlen;
5273 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5274 loopcheck, mode, !*unresolved_firstsep))
5283 rb_hash_aset(loopcheck, s, s);
5284 *resolvedp = testpath;
5293rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5297 VALUE unresolved_path;
5302 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5303 char *ptr, *prefixptr = NULL, *pend;
5308 if (!
NIL_P(basedir)) {
5313 enc = rb_enc_get(unresolved_path);
5314 unresolved_path = TO_OSPATH(unresolved_path);
5316 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5317 if (ptr != path_names) {
5318 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5322 if (!
NIL_P(basedir)) {
5324 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5325 if (ptr != basedir_names) {
5331 curdir = rb_dir_getwd_ospath();
5333 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5338 pend = prefixptr + prefixlen;
5339 bool mb_enc = enc_mbclen_needed(enc);
5340 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5342 prefixlen = ++ptr - prefixptr;
5345#ifdef FILE_ALT_SEPARATOR
5346 while (prefixptr < ptr) {
5347 if (*prefixptr == FILE_ALT_SEPARATOR) {
5350 Inc(prefixptr, pend, mb_enc, enc);
5354 switch (rb_enc_to_index(enc)) {
5355 case ENCINDEX_ASCII_8BIT:
5356 case ENCINDEX_US_ASCII:
5357 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5360 loopcheck = rb_hash_new();
5362 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5365 if (basedir_names) {
5366 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5369 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5372 if (origenc && origenc != rb_enc_get(resolved)) {
5374 rb_enc_associate(resolved, origenc);
5386static VALUE rb_file_join(
long argc,
VALUE *args);
5388#ifndef HAVE_REALPATH
5390rb_check_realpath_emulate_try(
VALUE arg)
5393 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5397rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5401#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5402 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5404# include <sys/syslimits.h>
5405# define NEEDS_REALPATH_BUFFER 1
5409rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5412 VALUE unresolved_path;
5413 char *resolved_ptr = NULL;
5415# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5416 char resolved_buffer[PATH_MAX];
5418 char *
const resolved_buffer = NULL;
5421 if (mode == RB_REALPATH_DIR) {
5422 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5426 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5427 VALUE paths[2] = {basedir, unresolved_path};
5428 unresolved_path = rb_file_join(2, paths);
5430 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5432 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5441 if (
errno == ENOTSUP ||
5443 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5444 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5447 if (mode == RB_REALPATH_CHECK) {
5450 rb_sys_fail_path(unresolved_path);
5452 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5453# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5457# if !defined(__linux__) && !defined(__APPLE__)
5461 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5462 if (mode == RB_REALPATH_CHECK) {
5465 rb_sys_fail_path(unresolved_path);
5469 if (origenc && origenc != rb_enc_get(resolved)) {
5473 rb_enc_associate(resolved, origenc);
5476 if (is_broken_string(resolved)) {
5477 rb_enc_associate(resolved, rb_filesystem_encoding());
5478 if (is_broken_string(resolved)) {
5479 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5486 if (mode == RB_REALPATH_CHECK) {
5490 arg[2] = (
VALUE)origenc;
5493 rb_check_realpath_emulate_rescue,
Qnil);
5496 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5502rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5504 const enum rb_realpath_mode mode =
5505 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5506 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5512 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5529rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5532 VALUE path = argv[0];
5534 return rb_realpath_internal(basedir, path, 1);
5550rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5553 VALUE path = argv[0];
5555 return rb_realpath_internal(basedir, path, 0);
5559rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5563 const char *s, *last;
5565 if (!e || !l2)
return 0;
5567 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5568 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5569 if (c ==
'.')
return l0;
5574 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5579 if (l1 < l2)
return l1;
5582 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5583#if CASEFOLD_FILESYSTEM
5584#define fncomp strncasecmp
5586#define fncomp strncmp
5588 if (fncomp(s, e, l2) == 0) {
5594static inline const char *
5595enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5597 const char *p, *q, *e, *end;
5600 long len = (alllen ? (size_t)*alllen : strlen(name));
5607 name = skipprefix(name, end, mb_enc, enc);
5608#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5609 const char *root = name;
5612 while (name < end && isdirsep(*name)) {
5619#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5623#ifdef DOSISH_DRIVE_LETTER
5624 else if (*p ==
':') {
5637 p = strrdirsep(name, end, mb_enc, enc);
5642 while (isdirsep(*p)) {
5647 n = ntfs_tail(p, end, mb_enc, enc) - p;
5649 n = chompdirsep(p, end, mb_enc, enc) - p;
5651 for (q = p; q - p < n && *q ==
'.'; q++);
5652 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5653 if (*q ==
'.') e = q;
5673ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5675 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5719 const char *name, *p, *fp = 0;
5725 CheckPath(fname, name);
5729 check_path_encoding(fext);
5731 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5732 enc = rb_str_enc_get(fname);
5735 n = RSTRING_LEN(fname);
5736 if (n <= 0 || !*name) {
5737 return rb_enc_str_new(0, 0, enc);
5740 bool mb_enc = enc_mbclen_needed(enc);
5741 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5747 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5752 if (f == RSTRING_LEN(fname)) {
5757 return rb_enc_str_new(p, f, enc);
5760static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5783rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5789 return rb_file_dirname_n(argv[0], n);
5795 return rb_file_dirname_n(fname, 1);
5799rb_file_dirname_n(
VALUE fname,
int n)
5801 const char *name, *root, *p, *end;
5804 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5805 CheckPath(fname, name);
5806 end = name + RSTRING_LEN(fname);
5808 bool mb_enc = !rb_str_enc_fastpath(fname);
5811 root = skiproot(name, end);
5813 if (root > name + 1 && isdirsep(*name))
5814 root = skipprefix(name = root - 2, end, mb_enc, enc);
5816 if (root > name + 1)
5819 if (n > (end - root + 1) / 2) {
5825 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5834 return rb_enc_str_new(
".", 1, enc);
5836#ifdef DOSISH_DRIVE_LETTER
5837 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5838 const char *top = skiproot(name + 2, end);
5839 dirname = rb_enc_str_new(name, 3, enc);
5844 dirname = rb_enc_str_new(name, p - name, enc);
5845#ifdef DOSISH_DRIVE_LETTER
5846 if (root == name + 2 && p == root && name[1] ==
':')
5852static inline const char *
5853enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5855 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5857 p = strrdirsep(name, end, mb_enc, enc);
5861 do name = ++p;
while (isdirsep(*p));
5864 while (*p && *p ==
'.') p++;
5866 if (*p ==
'.' || istrailinggarbage(*p)) {
5868 const char *last = p++, *dot = last;
5869 while (istrailinggarbage(*p)) {
5870 if (*p ==
'.') dot = p;
5873 if (!*p || isADS(*p)) {
5877 if (*last ==
'.' || dot > last) e = dot;
5884 else if (isADS(*p)) {
5888 else if (isdirsep(*p))
5890 Inc(p, end, mb_enc, enc);
5895 if (!e || e == name)
5920 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
5967 CheckPath(fname, name);
5968 long len = RSTRING_LEN(fname);
5971 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
5974 bool mb_enc = !rb_str_enc_fastpath(fname);
5977 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
5978 return rb_enc_str_new(ext,
len, enc);
6002 return rb_get_path(fname);
6034file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6036 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6037 return rb_file_join_ary(arg);
6041rb_file_join_ary(
VALUE ary)
6045 const char *name, *tail;
6055 check_path_encoding(tmp);
6056 len += RSTRING_LEN(tmp);
6064 RBASIC_CLEAR_CLASS(result);
6067 switch (OBJ_BUILTIN_TYPE(tmp)) {
6069 if (!checked) check_path_encoding(tmp);
6074 rb_raise(rb_eArgError,
"recursive array");
6086 rb_enc_copy(result, tmp);
6089 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6090 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6093 else if (tail == name +
len) {
6097 enc = fs_enc_check(result, tmp);
6099 rb_enc_associate(result, enc);
6107rb_file_join_fastpath(
long argc,
VALUE *args)
6112 for (i = 0; i < argc; i++) {
6113 VALUE tmp = args[i];
6115 size += RSTRING_LEN(tmp);
6128 const char *name = RSTRING_PTR(result);
6129 for (i = 1; i < argc; i++) {
6130 VALUE tmp = args[i];
6131 long len = RSTRING_LEN(result);
6137 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6140 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6145 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6152 rb_enc_associate(result, new_enc);
6153 encidx = rb_enc_to_index(new_enc);
6159 rb_str_null_check(result);
6164rb_file_join(
long argc,
VALUE *args)
6166 if (RB_UNLIKELY(argc == 0)) {
6170 VALUE result = rb_file_join_fastpath(argc, args);
6171 if (RB_LIKELY(result)) {
6191rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6193 return rb_file_join(argc, argv);
6196#if defined(HAVE_TRUNCATE)
6197struct truncate_arg {
6203nogvl_truncate(
void *ptr)
6205 struct truncate_arg *ta = ptr;
6206 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6238 struct truncate_arg ta;
6243 path = rb_str_encode_ospath(path);
6246 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6248 rb_sys_fail_path(path);
6252#define rb_file_s_truncate rb_f_notimplement
6255#if defined(HAVE_FTRUNCATE)
6256struct ftruncate_arg {
6262nogvl_ftruncate(
void *ptr)
6264 struct ftruncate_arg *fa = ptr;
6266 return (
VALUE)ftruncate(fa->fd, fa->pos);
6307 struct ftruncate_arg fa;
6314 rb_io_flush_raw(obj, 0);
6316 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6317 rb_sys_fail_path(fptr->
pathv);
6322#define rb_file_truncate rb_f_notimplement
6339#include <winerror.h>
6343rb_thread_flock(
void *data)
6346 int old_errno =
errno;
6348 int *op = data, ret = flock(op[0], op[1]);
6351 if (GetLastError() == ERROR_NOT_LOCKED) {
6409 op[1] = op1 =
NUM2INT(operation);
6414 rb_io_flush_raw(obj, 0);
6416 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6421#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6424 if (op1 & LOCK_NB)
return Qfalse;
6427 time.tv_usec = 100 * 1000;
6433#if defined(ERESTART)
6439 rb_syserr_fail_path(e, fptr->
pathv);
6446test_check(
int n,
int argc,
VALUE *argv)
6452 for (i=1; i<n; i++) {
6459#define CHECK(n) test_check((n), argc, argv)
6551 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6555 return rb_file_blockdev_p(0, argv[1]);
6558 return rb_file_chardev_p(0, argv[1]);
6561 return rb_file_directory_p(0, argv[1]);
6564 return rb_file_exist_p(0, argv[1]);
6567 return rb_file_file_p(0, argv[1]);
6570 return rb_file_sgid_p(0, argv[1]);
6573 return rb_file_grpowned_p(0, argv[1]);
6576 return rb_file_sticky_p(0, argv[1]);
6579 return rb_file_symlink_p(0, argv[1]);
6582 return rb_file_owned_p(0, argv[1]);
6585 return rb_file_rowned_p(0, argv[1]);
6588 return rb_file_pipe_p(0, argv[1]);
6591 return rb_file_readable_p(0, argv[1]);
6594 return rb_file_readable_real_p(0, argv[1]);
6597 return rb_file_size_p(0, argv[1]);
6600 return rb_file_socket_p(0, argv[1]);
6603 return rb_file_suid_p(0, argv[1]);
6606 return rb_file_writable_p(0, argv[1]);
6609 return rb_file_writable_real_p(0, argv[1]);
6612 return rb_file_executable_p(0, argv[1]);
6615 return rb_file_executable_real_p(0, argv[1]);
6618 return rb_file_zero_p(0, argv[1]);
6622 if (strchr(
"MAC", cmd)) {
6624 VALUE fname = argv[1];
6627 if (
rb_stat(fname, &st) == -1) {
6630 rb_syserr_fail_path(e, fname);
6635 return stat_atime(&st);
6637 return stat_mtime(&st);
6639 return stat_ctime(&st);
6645 return rb_file_identical_p(0, argv[1], argv[2]);
6648 if (strchr(
"=<>", cmd)) {
6649 struct stat st1, st2;
6656 t1 = stat_mtimespec(&st1);
6657 t2 = stat_mtimespec(&st2);
6661 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6665 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6666 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6670 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6671 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6678 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6681 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6735rb_stat_s_alloc(
VALUE klass)
6756 fname = rb_str_encode_ospath(fname);
6758 rb_sys_fail_path(fname);
6765 rb_st->initialized =
true;
6782 *copy_rb_st = *orig_rb_st;
6824rb_stat_ftype(
VALUE obj)
6826 return rb_file_ftype(get_stat(obj)->ST_(mode));
6842 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6869 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
6906 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
6938 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
6960 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
6980 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7015rb_stat_owned(
VALUE obj)
7017 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7022rb_stat_rowned(
VALUE obj)
7024 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7045rb_stat_grpowned(
VALUE obj)
7048 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7077 rb_io_stat_data *st = get_stat(obj);
7080 if (geteuid() == 0)
return Qtrue;
7083 if (rb_stat_owned(obj))
7084 return RBOOL(st->ST_(mode) & S_IRUSR);
7087 if (rb_stat_grpowned(obj))
7088 return RBOOL(st->ST_(mode) & S_IRGRP);
7091 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7109 rb_io_stat_data *st = get_stat(obj);
7112 if (getuid() == 0)
return Qtrue;
7115 if (rb_stat_rowned(obj))
7116 return RBOOL(st->ST_(mode) & S_IRUSR);
7119 if (rb_group_member(get_stat(obj)->ST_(gid)))
7120 return RBOOL(st->ST_(mode) & S_IRGRP);
7123 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7142rb_stat_wr(
VALUE obj)
7145 rb_io_stat_data *st = get_stat(obj);
7146 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7147 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7167 rb_io_stat_data *st = get_stat(obj);
7170 if (geteuid() == 0)
return Qtrue;
7173 if (rb_stat_owned(obj))
7174 return RBOOL(st->ST_(mode) & S_IWUSR);
7177 if (rb_stat_grpowned(obj))
7178 return RBOOL(st->ST_(mode) & S_IWGRP);
7181 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7200 rb_io_stat_data *st = get_stat(obj);
7203 if (getuid() == 0)
return Qtrue;
7206 if (rb_stat_rowned(obj))
7207 return RBOOL(st->ST_(mode) & S_IWUSR);
7210 if (rb_group_member(get_stat(obj)->ST_(gid)))
7211 return RBOOL(st->ST_(mode) & S_IWGRP);
7214 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7233rb_stat_ww(
VALUE obj)
7236 rb_io_stat_data *st = get_stat(obj);
7237 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7238 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7280 rb_io_stat_data *st = get_stat(obj);
7283 if (geteuid() == 0) {
7284 return RBOOL(st->ST_(mode) & S_IXUGO);
7288 if (rb_stat_owned(obj))
7289 return RBOOL(st->ST_(mode) & S_IXUSR);
7292 if (rb_stat_grpowned(obj))
7293 return RBOOL(st->ST_(mode) & S_IXGRP);
7296 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7312 rb_io_stat_data *st = get_stat(obj);
7315 if (getuid() == 0) {
7316 return RBOOL(st->ST_(mode) & S_IXUGO);
7320 if (rb_stat_rowned(obj))
7321 return RBOOL(st->ST_(mode) & S_IXUSR);
7324 if (rb_group_member(get_stat(obj)->ST_(gid)))
7325 return RBOOL(st->ST_(mode) & S_IXGRP);
7328 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7351 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7368 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7400 rb_off_t size = get_stat(obj)->ST_(size);
7402 if (size == 0)
return Qnil;
7435rb_stat_suid(
VALUE obj)
7438 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7472rb_stat_sgid(
VALUE obj)
7475 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7509rb_stat_sticky(
VALUE obj)
7512 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7517#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7518#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7529nogvl_mkfifo(
void *ptr)
7531 struct mkfifo_arg *ma = ptr;
7533 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7561 struct mkfifo_arg ma;
7570 path = rb_str_encode_ospath(path);
7571 ma.path = RSTRING_PTR(path);
7572 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7573 rb_sys_fail_path(path);
7578#define rb_file_s_mkfifo rb_f_notimplement
7581static VALUE rb_mFConst;
7584rb_file_const(
const char *name,
VALUE value)
7586 rb_define_const(rb_mFConst, name, value);
7590rb_is_absolute_path(
const char *path)
7592#ifdef DOSISH_DRIVE_LETTER
7593 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7596 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7599 if (path[0] ==
'/')
return 1;
7605ruby_is_fd_loadable(
int fd)
7612 if (fstat(fd, &st) < 0)
7615 if (S_ISREG(st.st_mode))
7618 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7621 if (S_ISDIR(st.st_mode))
7632rb_file_load_ok(
const char *path)
7639 int mode = (O_RDONLY |
7640#if defined O_NONBLOCK
7642#elif defined O_NDELAY
7648 if (!rb_gc_for_fd(
errno))
return 0;
7650 if (fd < 0)
return 0;
7653 ret = ruby_is_fd_loadable(fd);
7660is_explicit_relative(
const char *path)
7662 if (*path++ !=
'.')
return 0;
7663 if (*path ==
'.') path++;
7664 return isdirsep(*path);
7670 int encidx = rb_enc_get_index(orig);
7671 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7672 encidx = rb_filesystem_encindex();
7673 rb_enc_associate_index(path, encidx);
7681nav_component_p(
const char *s,
const char *send)
7683 if ((send - s) >= 2 && s[0] ==
'.') {
7684 return s[1] ==
'.' || isdirsep(s[1]);
7690fname_need_expansion_p(
VALUE fname)
7692 const char *s = RSTRING_PTR(fname);
7693 const long len = RSTRING_LEN(fname);
7694 const char *send = s +
len;
7696 if (nav_component_p(s, send)) {
7701 bool mbenc = enc_mbclen_needed(enc);
7703 s = enc_path_next(s, send, mbenc, enc);
7705 if (nav_component_p(s, send)) {
7709 s = enc_path_next(s, send, mbenc, enc);
7715expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7717 long dname_len = RSTRING_LEN(dname);
7718 const char *dname_ptr = RSTRING_PTR(dname);
7722 if (need_expansion || dname_ptr[0] ==
'~') {
7723 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7728 if (!isdirsep(dname_ptr[dname_len - 1])) {
7737rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
7740 VALUE fname = *filep;
7744 if (!ext[0])
return 0;
7747 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7748 f = RSTRING_PTR(fname);
7753 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7754 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7755 fnlen = RSTRING_LEN(fname);
7756 for (i=0; ext[i]; i++) {
7758 if (rb_file_load_ok(RSTRING_PTR(fname))) {
7759 *filep = copy_path_class(fname, *filep);
7767 long expanded_load_path_maxlen;
7768 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7769 if (!load_path)
return 0;
7772 RBASIC_CLEAR_CLASS(fname);
7773 fnlen = RSTRING_LEN(fname);
7774 bool need_expansion = fname_need_expansion_p(fname);
7777 rb_enc_associate_index(tmp, rb_usascii_encindex());
7779 for (j=0; ext[j]; j++) {
7781 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
7783 if (!RSTRING_LEN(dname))
continue;
7784 expand_feature(fname, dname, tmp, need_expansion);
7786 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7787 *filep = copy_path_class(tmp, *filep);
7793 rb_str_resize(tmp, 0);
7806 path = copy_path_class(file_expand_path_1(path, 0), path);
7807 f = RSTRING_PTR(path);
7811 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7812 if (!rb_file_load_ok(f))
return 0;
7814 path = copy_path_class(file_expand_path_1(path, 0), path);
7818 long expanded_load_path_maxlen;
7819 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7822 bool need_expansion = fname_need_expansion_p(path);
7824 rb_enc_associate_index(tmp, rb_usascii_encindex());
7825 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
7827 if (!RSTRING_LEN(dname))
continue;
7828 expand_feature(path, dname, tmp, need_expansion);
7830 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7831 return copy_path_class(tmp, path);
7834 rb_str_resize(tmp, 0);
7842#define define_filetest_function(name, func, argc) do { \
7843 rb_define_module_function(rb_mFileTest, name, func, argc); \
7844 rb_define_singleton_method(rb_cFile, name, func, argc); \
7847const char ruby_null_device[] =
7850#elif defined AMIGA || defined __amigaos__
8654#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8655 rb_CFString_class_initialize_before_fork();
8663 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8664 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8665 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8666 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8667 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8668 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8669 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8670 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8671 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8672 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8673 define_filetest_function(
"file?", rb_file_file_p, 1);
8674 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8675 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8676 define_filetest_function(
"size?", rb_file_size_p, 1);
8677 define_filetest_function(
"size", rb_file_s_size, 1);
8678 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8679 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8681 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8682 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8683 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8685 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8686 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8688 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8689 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8690 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8692 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8730 separator = rb_fstring_lit(
"/");
8732 rb_define_const(
rb_cFile,
"Separator", separator);
8734 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9083 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9086 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9088 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9090 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9092 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9094 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9096 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9097#if defined(O_NDELAY) || defined(O_NONBLOCK)
9099# define O_NONBLOCK O_NDELAY
9102 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9105 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9108 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9114 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9115#ifndef O_SHARE_DELETE
9116# define O_SHARE_DELETE 0
9119 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9122 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9126 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9130 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9134 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9138 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9142 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9146 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9150 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9152 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9154 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9156 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9159 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.