14#include "ruby/internal/config.h"
18# include "missing/file.h"
30# if !(defined(__has_feature) && defined(__has_attribute))
35# define API_AVAILABLE(...)
36# define API_DEPRECATED(...)
38# include <CoreFoundation/CFString.h>
55#ifdef HAVE_SYS_PARAM_H
56# include <sys/param.h>
59# define MAXPATHLEN 1024
64#elif defined HAVE_SYS_UTIME_H
65# include <sys/utime.h>
72#ifdef HAVE_SYS_SYSMACROS_H
73# include <sys/sysmacros.h>
79#ifdef HAVE_SYS_MKDEV_H
80# include <sys/mkdev.h>
83#if defined(HAVE_FCNTL_H)
87#if defined(HAVE_SYS_TIME_H)
91#if !defined HAVE_LSTAT && !defined lstat
97# include "win32/file.h"
99# define chmod(p, m) rb_w32_uchmod((p), (m))
101# define chown(p, o, g) rb_w32_uchown((p), (o), (g))
103# define lchown(p, o, g) rb_w32_ulchown((p), (o), (g))
105# define link(f, t) rb_w32_ulink((f), (t))
107# define readlink(f, t, l) rb_w32_ureadlink((f), (t), (l))
109# define symlink(s, l) rb_w32_usymlink((s), (l))
118#ifdef HAVE_STRUCT_STATX_STX_BTIME
119# define ST_(name) stx_ ## name
122# define ST_(name) st_ ## name
126#if defined _WIN32 || defined __APPLE__
128# define TO_OSPATH(str) rb_str_encode_ospath(str)
131# define TO_OSPATH(str) (str)
135#if defined DOSISH || defined __CYGWIN__
140#if defined HAVE_REALPATH && defined __sun && defined __SVR4
153#include "internal/compilers.h"
154#include "internal/dir.h"
155#include "internal/encoding.h"
156#include "internal/error.h"
157#include "internal/file.h"
158#include "internal/io.h"
159#include "internal/load.h"
160#include "internal/object.h"
161#include "internal/process.h"
162#include "internal/thread.h"
163#include "internal/vm.h"
168#define UIANY2NUM(x) \
169 ((sizeof(x) <= sizeof(unsigned int)) ? \
170 UINT2NUM((unsigned)(x)) : \
171 (sizeof(x) <= sizeof(unsigned long)) ? \
172 ULONG2NUM((unsigned long)(x)) : \
173 ULL2NUM((unsigned LONG_LONG)(x)))
180file_path_convert(
VALUE name)
185 if (ENCINDEX_US_ASCII != fname_encidx &&
186 ENCINDEX_ASCII_8BIT != fname_encidx &&
187 (fs_encidx = rb_filesystem_encindex()) != fname_encidx &&
188 rb_default_internal_encoding() &&
192 rb_encoding *fname_encoding = rb_enc_from_index(fname_encidx);
193 rb_encoding *fs_encoding = rb_enc_from_index(fs_encidx);
201check_path_encoding(
VALUE str)
203 if (RB_UNLIKELY(!rb_str_enc_fastpath(str))) {
205 if (!rb_enc_asciicompat(enc)) {
213rb_get_path_check_to_string(
VALUE obj)
222 tmp = rb_check_funcall_default(obj, to_path, 0, 0, obj);
228rb_get_path_check_convert(
VALUE obj)
230 obj = file_path_convert(obj);
231 rb_get_path_check_no_convert(obj);
237rb_get_path_check_no_convert(
VALUE obj)
239 check_path_encoding(obj);
240 if (!rb_str_to_cstr(obj)) {
241 rb_raise(rb_eArgError,
"path name contains null byte");
250 return rb_get_path(obj);
256 return rb_get_path_check_convert(rb_get_path_check_to_string(obj));
260check_path(
VALUE obj,
const char **cstr)
262 VALUE str = rb_get_path_check_convert(rb_get_path_check_to_string(obj));
266 *cstr = RSTRING_PTR(str);
270#define CheckPath(str, cstr) RB_GC_GUARD(str) = check_path(str, &cstr);
277 if (encidx != ENCINDEX_ASCII_8BIT && encidx != ENCINDEX_UTF_8) {
287# define NORMALIZE_UTF8PATH 1
289# ifdef HAVE_WORKING_FORK
290static CFMutableStringRef
291mutable_CFString_new(CFStringRef *s,
const char *ptr,
long len)
293 const CFAllocatorRef alloc = kCFAllocatorDefault;
294 *s = CFStringCreateWithBytesNoCopy(alloc, (
const UInt8 *)ptr,
len,
295 kCFStringEncodingUTF8, FALSE,
297 return CFStringCreateMutableCopy(alloc,
len, *s);
300# define mutable_CFString_release(m, s) (CFRelease(m), CFRelease(s))
303rb_CFString_class_initialize_before_fork(
void)
326 const char small_str[] =
"/";
327 long len =
sizeof(small_str) - 1;
335 for (
int i = 0; i < 2; i++) {
336 CFMutableStringRef m = mutable_CFString_new(&s, small_str,
len);
337 mutable_CFString_release(m, s);
343rb_str_append_normalized_ospath(
VALUE str,
const char *ptr,
long len)
348 CFMutableStringRef m = mutable_CFString_new(&s, ptr,
len);
349 long oldlen = RSTRING_LEN(str);
351 CFStringNormalize(m, kCFStringNormalizationFormC);
352 all = CFRangeMake(0, CFStringGetLength(m));
353 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE, NULL, 0, &buflen);
355 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE,
356 (UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
358 mutable_CFString_release(m, s);
363rb_str_normalize_ospath(
const char *ptr,
long len)
366 const char *e = ptr +
len;
375 rb_enc_associate(str, enc);
380 int r = rb_enc_precise_mbclen(p, e, enc);
384 rb_str_append_normalized_ospath(str, p1, p-p1);
391 c = rb_enc_mbc_to_codepoint(p, e, enc);
392 if ((0x2000 <= c && c <= 0x2FFF) || (0xF900 <= c && c <= 0xFAFF) ||
393 (0x2F800 <= c && c <= 0x2FAFF)) {
395 rb_str_append_normalized_ospath(str, p1, p-p1);
406 rb_str_append_normalized_ospath(str, p1, p-p1);
413ignored_char_p(
const char *p,
const char *e,
rb_encoding *enc)
416 if (p+3 > e)
return 0;
417 switch ((
unsigned char)*p) {
419 switch ((
unsigned char)p[1]) {
421 c = (
unsigned char)p[2];
423 if (c >= 0x8c && c <= 0x8f)
return 3;
425 if (c >= 0xaa && c <= 0xae)
return 3;
428 c = (
unsigned char)p[2];
430 if (c >= 0xaa && c <= 0xaf)
return 3;
436 if ((
unsigned char)p[1] == 0xbb &&
437 (
unsigned char)p[2] == 0xbf)
444# define NORMALIZE_UTF8PATH 0
447#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
458 int (*func)(
const char *,
void *);
464no_gvl_apply2files(
void *ptr)
468 for (aa->i = 0; aa->i < aa->argc; aa->i++) {
469 if (aa->func(aa->fn[aa->i].ptr, aa->arg) < 0) {
478NORETURN(
static void utime_failed(
struct apply_arg *));
479static int utime_internal(
const char *,
void *);
483apply2files(
int (*func)(
const char *,
void *),
int argc,
VALUE *argv,
void *arg)
487 const long len = (long)(offsetof(
struct apply_arg, fn) + (size * argc));
495 for (aa->i = 0; aa->i < argc; aa->i++) {
496 VALUE path = rb_get_path(argv[aa->i]);
498 path = rb_str_encode_ospath(path);
499 aa->fn[aa->i].ptr = RSTRING_PTR(path);
500 aa->fn[aa->i].path = path;
503 IO_WITHOUT_GVL(no_gvl_apply2files, aa);
506 if (func == utime_internal) {
510 rb_syserr_fail_path(aa->errnum, aa->fn[aa->i].path);
529 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
533 rb_io_stat_data stat;
546rb_stat_new(
const struct stat *st)
552# define CP(m) .stx_ ## m = st->st_ ## m
553# define CP_32(m) .stx_ ## m = (uint32_t)st->st_ ## m
554# define CP_TS(m) .stx_ ## m = stat_ ## m ## spec(st)
555 rb_st->stat = (
struct statx){
556 .stx_mask = STATX_BASIC_STATS,
573 rb_st->initialized =
true;
581rb_statx_new(
const rb_io_stat_data *st)
587 rb_st->initialized =
true;
593static rb_io_stat_data*
598 if (!rb_st->initialized) rb_raise(
rb_eTypeError,
"uninitialized File::Stat");
604statx_mtimespec(
const rb_io_stat_data *st)
606 return st->stx_mtime;
609# define statx_mtimespec stat_mtimespec
647 if (ts1.tv_sec == ts2.tv_sec) {
648 if (ts1.tv_nsec == ts2.tv_nsec)
return INT2FIX(0);
649 if (ts1.tv_nsec < ts2.tv_nsec)
return INT2FIX(-1);
652 if (ts1.tv_sec < ts2.tv_sec)
return INT2FIX(-1);
658#define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
661# define NUM2DEVT(v) NUM2UINT(v)
664# define DEVT2NUM(v) UINT2NUM(v)
666#ifndef PRI_DEVT_PREFIX
667# define PRI_DEVT_PREFIX ""
681rb_stat_dev(
VALUE self)
684 unsigned int m = get_stat(self)->stx_dev_major;
685 unsigned int n = get_stat(self)->stx_dev_minor;
687#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
688 return DEVT2NUM(get_stat(self)->st_dev);
689#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
690 return ULONG2NUM(get_stat(self)->st_dev);
692 return ULL2NUM(get_stat(self)->st_dev);
707rb_stat_dev_major(
VALUE self)
710 return UINT2NUM(get_stat(self)->stx_dev_major);
712 return UINT2NUM(major(get_stat(self)->st_dev));
729rb_stat_dev_minor(
VALUE self)
732 return UINT2NUM(get_stat(self)->stx_dev_minor);
734 return UINT2NUM(minor(get_stat(self)->st_dev));
751rb_stat_ino(
VALUE self)
753 rb_io_stat_data *ptr = get_stat(self);
754#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
756 return rb_integer_unpack(&ptr->st_ino, 2,
757 SIZEOF_STRUCT_STAT_ST_INO, 0,
761 return UIANY2NUM(ptr->ST_(ino));
779rb_stat_mode(
VALUE self)
781 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
797rb_stat_nlink(
VALUE self)
800 const rb_io_stat_data *ptr = get_stat(self);
802 return UIANY2NUM(ptr->ST_(nlink));
816rb_stat_uid(
VALUE self)
818 return UIDT2NUM(get_stat(self)->ST_(uid));
832rb_stat_gid(
VALUE self)
834 return GIDT2NUM(get_stat(self)->ST_(gid));
850rb_stat_rdev(
VALUE self)
853 unsigned int m = get_stat(self)->stx_rdev_major;
854 unsigned int n = get_stat(self)->stx_rdev_minor;
856#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
858#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
859 return DEVT2NUM(get_stat(self)->ST_(rdev));
860#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
861 return ULONG2NUM(get_stat(self)->ST_(rdev));
863 return ULL2NUM(get_stat(self)->ST_(rdev));
878rb_stat_rdev_major(
VALUE self)
881 return UINT2NUM(get_stat(self)->stx_rdev_major);
882#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
883 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
900rb_stat_rdev_minor(
VALUE self)
903 return UINT2NUM(get_stat(self)->stx_rdev_minor);
904#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
905 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
941rb_stat_size(
VALUE self)
943 return OFFT2NUM(get_stat(self)->ST_(size));
958rb_stat_blksize(
VALUE self)
960#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
961 return ULONG2NUM(get_stat(self)->ST_(blksize));
979rb_stat_blocks(
VALUE self)
981#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
982# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
983 return ULL2NUM(get_stat(self)->ST_(blocks));
985 return ULONG2NUM(get_stat(self)->ST_(blocks));
993stat_atimespec(
const struct stat *st)
996 ts.tv_sec = st->st_atime;
997#if defined(HAVE_STRUCT_STAT_ST_ATIM)
998 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
999#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1000 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
1001#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
1002 ts.tv_nsec = (uint32_t)st->st_atimensec;
1011statx_atimespec(
const rb_io_stat_data *st)
1013 return st->stx_atime;
1016# define statx_atimespec stat_atimespec
1026stat_atime(
const struct stat *st)
1028 return stat_time(stat_atimespec(st));
1032stat_mtimespec(
const struct stat *st)
1035 ts.tv_sec = st->st_mtime;
1036#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1037 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1038#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1039 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1040#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1041 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1049stat_mtime(
const struct stat *st)
1051 return stat_time(stat_mtimespec(st));
1055stat_ctimespec(
const struct stat *st)
1058 ts.tv_sec = st->st_ctime;
1059#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1060 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1061#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1062 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1063#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1064 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1073statx_ctimespec(
const rb_io_stat_data *st)
1075 return st->stx_ctime;
1078# define statx_ctimespec stat_ctimespec
1082stat_ctime(
const struct stat *st)
1084 return stat_time(stat_ctimespec(st));
1087#define HAVE_STAT_BIRTHTIME
1088#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1090statx_birthtime(
const rb_io_stat_data *st)
1095#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1096static VALUE statx_birthtime(
const rb_io_stat_data *st);
1097#elif defined(_WIN32)
1098# define statx_birthtime stat_ctime
1100# undef HAVE_STAT_BIRTHTIME
1147rb_stat_atime(
VALUE self)
1149 return stat_time(statx_atimespec(get_stat(self)));
1180rb_stat_mtime(
VALUE self)
1182 return stat_time(statx_mtimespec(get_stat(self)));
1200rb_stat_ctime(
VALUE self)
1202 return stat_time(statx_ctimespec(get_stat(self)));
1205#if defined(HAVE_STAT_BIRTHTIME)
1227rb_stat_birthtime(
VALUE self)
1229 return statx_birthtime(get_stat(self));
1232# define rb_stat_birthtime rb_f_notimplement
1251rb_stat_inspect(
VALUE self)
1255 static const struct {
1259 {
"dev", rb_stat_dev},
1260 {
"ino", rb_stat_ino},
1261 {
"mode", rb_stat_mode},
1262 {
"nlink", rb_stat_nlink},
1263 {
"uid", rb_stat_uid},
1264 {
"gid", rb_stat_gid},
1265 {
"rdev", rb_stat_rdev},
1266 {
"size", rb_stat_size},
1267 {
"blksize", rb_stat_blksize},
1268 {
"blocks", rb_stat_blocks},
1269 {
"atime", rb_stat_atime},
1270 {
"mtime", rb_stat_mtime},
1271 {
"ctime", rb_stat_ctime},
1272#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1273 {
"birthtime", rb_stat_birthtime},
1279 if (!rb_st->initialized) {
1287 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1295 v = (*member[i].func)(self);
1297 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1299 else if (i == 0 || i == 6) {
1300 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1320no_gvl_fstat(
void *data)
1323 return (
VALUE)fstat(arg->file.fd, arg->st);
1327fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1331 data.file.fd = fptr->
fd;
1334 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1338no_gvl_stat(
void * data)
1341 return (
void *)(
VALUE)stat(arg->file.path, arg->st);
1345stat_without_gvl(
const char *path,
struct stat *st)
1349 data.file.path = path;
1352 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1355#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1356 defined(HAVE_STRUCT_STATX_STX_BTIME)
1358# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1361# ifdef HAVE_SYSCALL_H
1362# include <syscall.h>
1363# elif defined HAVE_SYS_SYSCALL_H
1364# include <sys/syscall.h>
1366# if defined __linux__
1367# include <linux/stat.h>
1369statx(
int dirfd,
const char *pathname,
int flags,
1370 unsigned int mask,
struct statx *statxbuf)
1372 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1377typedef struct no_gvl_rb_io_stat_data {
1383} no_gvl_rb_io_stat_data;
1386io_blocking_statx(
void *data)
1388 no_gvl_rb_io_stat_data *arg = data;
1389 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1393no_gvl_statx(
void *data)
1395 return (
void *)io_blocking_statx(data);
1399statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1401 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1404 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1408lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1410 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1413 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1417fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1419 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1422 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1425#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1428rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1433 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1438 result = fstatx_without_gvl(fptr, stx, mask);
1443 file = rb_str_encode_ospath(file);
1444 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1450# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1452NORETURN(
static void statx_notimplement(
const char *field_name));
1457statx_notimplement(
const char *field_name)
1460 "%s is unimplemented on this filesystem",
1465statx_birthtime(
const rb_io_stat_data *stx)
1467 if (!statx_has_birthtime(stx)) {
1469 statx_notimplement(
"birthtime");
1471 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1476# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1477# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1478# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1479# define rb_statx(file, stx, mask) rb_stat(file, stx)
1480# define STATX(path, st, mask) stat(path, st)
1482#if defined(HAVE_STAT_BIRTHTIME)
1483# define statx_has_birthtime(st) 1
1485# define statx_has_birthtime(st) 0
1492# define FSTAT(fd, st) fstat(fd, st)
1501 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1506 result = fstat_without_gvl(fptr, st);
1511 file = rb_str_encode_ospath(file);
1512 result = stat_without_gvl(RSTRING_PTR(file), st);
1550 fname = rb_str_encode_ospath(fname);
1551 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1552 rb_sys_fail_path(fname);
1554 return rb_statx_new(&st);
1586rb_io_stat(
VALUE obj)
1592 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1593 rb_sys_fail_path(fptr->
pathv);
1595 return rb_statx_new(&st);
1600no_gvl_lstat(
void *ptr)
1603 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1607lstat_without_gvl(
const char *path,
struct stat *st)
1611 data.file.path = path;
1614 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1651 fname = rb_str_encode_ospath(fname);
1652 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1653 rb_sys_fail_path(fname);
1655 return rb_statx_new(&st);
1657 return rb_file_s_stat(klass, fname);
1690rb_file_lstat(
VALUE obj)
1699 path = rb_str_encode_ospath(fptr->
pathv);
1700 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1701 rb_sys_fail_path(fptr->
pathv);
1703 return rb_statx_new(&st);
1705 return rb_io_stat(obj);
1710rb_group_member(GETGROUPS_T gid)
1712#if !defined(HAVE_GETGROUPS)
1721 if (getgid() == gid || getegid() == gid)
1724 groups = getgroups(0, NULL);
1725 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1726 anum = getgroups(groups, gary);
1727 while (--anum >= 0) {
1728 if (gary[anum] == gid) {
1741# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1744#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1745#define USE_GETEUID 1
1750eaccess(
const char *path,
int mode)
1759 if (getuid() == euid && getgid() == getegid())
1760 return access(path, mode);
1762 if (stat(path, &st) < 0)
1772 if (st.st_mode & S_IXUGO)
1778 if (st.st_uid == euid)
1780 else if (rb_group_member(st.st_gid))
1783 if ((
int)(st.st_mode & mode) == mode)
return 0;
1787 return access(path, mode);
1798nogvl_eaccess(
void *ptr)
1802 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1806rb_eaccess(
VALUE fname,
int mode)
1811 fname = rb_str_encode_ospath(fname);
1815 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1819nogvl_access(
void *ptr)
1823 return (
void *)(
VALUE)access(aa->path, aa->mode);
1827rb_access(
VALUE fname,
int mode)
1832 fname = rb_str_encode_ospath(fname);
1836 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1879# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1885 if (S_ISDIR(st.st_mode))
return Qtrue;
1914# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1920 if (S_ISFIFO(st.st_mode))
return Qtrue;
1952# define S_ISLNK(m) _S_ISLNK(m)
1955# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1958# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1968 fname = rb_str_encode_ospath(fname);
1970 if (S_ISLNK(st.st_mode))
return Qtrue;
2005# define S_ISSOCK(m) _S_ISSOCK(m)
2008# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
2011# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
2021 if (S_ISSOCK(st.st_mode))
return Qtrue;
2049# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
2051# define S_ISBLK(m) (0)
2059 if (S_ISBLK(st.st_mode))
return Qtrue;
2086# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2092 if (S_ISCHR(st.st_mode))
return Qtrue;
2162 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2176rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2178 return RBOOL(rb_access(fname, R_OK) >= 0);
2182# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2186# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2215rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2221 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2222 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2256 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2273rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2275 return RBOOL(rb_access(fname, W_OK) >= 0);
2304rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2310 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2311 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2354 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2373rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2375 return RBOOL(rb_access(fname, X_OK) >= 0);
2379# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2411 return RBOOL(S_ISREG(st.st_mode));
2471 return RBOOL(st.st_size == 0);
2509 if (st.st_size == 0)
return Qnil;
2543 return RBOOL(st.st_uid == geteuid());
2552 return RBOOL(st.st_uid == getuid());
2584 if (rb_group_member(st.st_gid))
return Qtrue;
2589#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2591check3rdbyte(
VALUE fname,
int mode)
2596 return RBOOL(st.st_mode & mode);
2629 return check3rdbyte(fname, S_ISUID);
2664 return check3rdbyte(fname, S_ISGID);
2700 return check3rdbyte(fname, S_ISVTX);
2737 struct stat st1, st2;
2741 if (st1.st_dev != st2.st_dev)
return Qfalse;
2742 if (st1.st_ino != st2.st_ino)
return Qfalse;
2746 return rb_w32_file_identical_p(fname1, fname2);
2772 if (
rb_stat(fname, &st) < 0) {
2775 rb_syserr_fail_path(e, fname);
2781rb_file_ftype(mode_t mode)
2785 if (S_ISREG(mode)) {
2788 else if (S_ISDIR(mode)) {
2791 else if (S_ISCHR(mode)) {
2792 t =
"characterSpecial";
2795 else if (S_ISBLK(mode)) {
2800 else if (S_ISFIFO(mode)) {
2805 else if (S_ISLNK(mode)) {
2810 else if (S_ISSOCK(mode)) {
2818 return rb_fstring_cstr(t);
2860 fname = rb_str_encode_ospath(fname);
2862 rb_sys_fail_path(fname);
2865 return rb_file_ftype(st.st_mode);
2913 if (
rb_stat(fname, &st) < 0) {
2916 rb_syserr_fail_path(e, fname);
2918 return stat_time(stat_atimespec(&st));
2945rb_file_atime(
VALUE obj)
2951 if (fstat(fptr->
fd, &st) == -1) {
2952 rb_sys_fail_path(fptr->
pathv);
2954 return stat_time(stat_atimespec(&st));
2994 if (
rb_stat(fname, &st) < 0) {
2997 rb_syserr_fail_path(e, fname);
2999 return stat_time(stat_mtimespec(&st));
3024rb_file_mtime(
VALUE obj)
3030 if (fstat(fptr->
fd, &st) == -1) {
3031 rb_sys_fail_path(fptr->
pathv);
3033 return stat_time(stat_mtimespec(&st));
3076 if (
rb_stat(fname, &st) < 0) {
3079 rb_syserr_fail_path(e, fname);
3081 return stat_time(stat_ctimespec(&st));
3098rb_file_ctime(
VALUE obj)
3104 if (fstat(fptr->
fd, &st) == -1) {
3105 rb_sys_fail_path(fptr->
pathv);
3107 return stat_time(stat_ctimespec(&st));
3110#if defined(HAVE_STAT_BIRTHTIME)
3141 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
3144 rb_syserr_fail_path(e, fname);
3146 return statx_birthtime(&st);
3149# define rb_file_s_birthtime rb_f_notimplement
3152#if defined(HAVE_STAT_BIRTHTIME)
3173rb_file_birthtime(
VALUE obj)
3179 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
3180 rb_sys_fail_path(fptr->
pathv);
3182 return statx_birthtime(&st);
3185# define rb_file_birthtime rb_f_notimplement
3197 rb_io_flush_raw(file, 0);
3200 if (fstat(fptr->
fd, &st) == -1) {
3201 rb_sys_fail_path(fptr->
pathv);
3227file_size(
VALUE self)
3229 return OFFT2NUM(rb_file_size(self));
3238nogvl_chmod(
void *ptr)
3241 int ret = chmod(data->path, data->mode);
3242 return (
void *)(
VALUE)ret;
3246rb_chmod(
const char *path, mode_t mode)
3252 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3256chmod_internal(
const char *path,
void *mode)
3258 return chmod(path, *(mode_t *)mode);
3296 return apply2files(chmod_internal, argc, argv, &mode);
3300struct nogvl_fchmod_data {
3306io_blocking_fchmod(
void *ptr)
3308 struct nogvl_fchmod_data *data = ptr;
3309 int ret = fchmod(data->fd, data->mode);
3314rb_fchmod(
struct rb_io* io, mode_t mode)
3317 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3318 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3350#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3358 if (rb_fchmod(fptr, mode) == -1) {
3359 if (HAVE_FCHMOD ||
errno != ENOSYS)
3360 rb_sys_fail_path(fptr->
pathv);
3363 if (!HAVE_FCHMOD)
return INT2FIX(0);
3366#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3368 path = rb_str_encode_ospath(fptr->
pathv);
3369 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3370 rb_sys_fail_path(fptr->
pathv);
3376#if defined(HAVE_LCHMOD)
3378lchmod_internal(
const char *path,
void *mode)
3380 return lchmod(path, *(mode_t *)mode);
3417 return apply2files(lchmod_internal, argc, argv, &mode);
3420#define rb_file_s_lchmod rb_f_notimplement
3423static inline rb_uid_t
3427 return (rb_uid_t)-1;
3432static inline rb_gid_t
3436 return (rb_gid_t)-1;
3447chown_internal(
const char *path,
void *arg)
3450 return chown(path, args->owner, args->group);
3504 arg.owner = to_uid(*argv++);
3505 arg.group = to_gid(*argv++);
3507 return apply2files(chown_internal, argc, argv, &arg);
3519nogvl_chown(
void *ptr)
3522 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3526rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3529 .as = {.path = path},
3530 .new = {.owner = owner, .group = group},
3532 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3537nogvl_fchown(
void *ptr)
3540 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3544rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3549 .new = {.owner = owner, .group = group},
3551 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3585 path = rb_str_encode_ospath(fptr->
pathv);
3586 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3587 rb_sys_fail_path(fptr->
pathv);
3589 if (rb_fchown(fptr->
fd, o, g) == -1)
3590 rb_sys_fail_path(fptr->
pathv);
3596#if defined(HAVE_LCHOWN)
3598lchown_internal(
const char *path,
void *arg)
3601 return lchown(path, args->owner, args->group);
3658 arg.owner = to_uid(*argv++);
3659 arg.group = to_gid(*argv++);
3661 return apply2files(lchown_internal, argc, argv, &arg);
3664#define rb_file_s_lchown rb_f_notimplement
3674NORETURN(
static void utime_failed(
struct apply_arg *));
3680 VALUE path = aa->fn[aa->i].path;
3683 if (ua->tsp && e == EINVAL) {
3686 VALUE atime = ua->atime;
3687 VALUE mtime = ua->mtime;
3689 if (!
NIL_P(atime)) {
3692 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3695 if (
NIL_P(a)) e[0] = m;
3711 rb_syserr_fail_path(e, path);
3715#if defined(HAVE_UTIMES)
3717# if !defined(HAVE_UTIMENSAT)
3719# elif defined(__APPLE__) && \
3720 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3722# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3723typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3727static inline utimensat_func *
3734# define utimensat rb_utimensat()
3741utime_internal(
const char *path,
void *arg)
3744 const struct timespec *tsp = v->tsp;
3745 struct timeval tvbuf[2], *tvp = NULL;
3747#if defined(HAVE_UTIMENSAT)
3748# if defined(__APPLE__)
3749 const int try_utimensat = utimensat != NULL;
3750 const int try_utimensat_follow = utimensat != NULL;
3752# define TRY_UTIMENSAT 1
3753 static int try_utimensat = 1;
3754# ifdef AT_SYMLINK_NOFOLLOW
3755 static int try_utimensat_follow = 1;
3757 const int try_utimensat_follow = 0;
3762 if (v->follow ? try_utimensat_follow : try_utimensat) {
3763# ifdef AT_SYMLINK_NOFOLLOW
3765 flags = AT_SYMLINK_NOFOLLOW;
3769 int result = utimensat(AT_FDCWD, path, tsp, flags);
3770# ifdef TRY_UTIMENSAT
3771 if (result < 0 &&
errno == ENOSYS) {
3772# ifdef AT_SYMLINK_NOFOLLOW
3773 try_utimensat_follow = 0;
3785 tvbuf[0].tv_sec = tsp[0].tv_sec;
3786 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3787 tvbuf[1].tv_sec = tsp[1].tv_sec;
3788 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3792 if (v->follow)
return lutimes(path, tvp);
3794 return utimes(path, tvp);
3799#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3807utime_internal(
const char *path,
void *arg)
3811 struct utimbuf utbuf, *utp = NULL;
3813 utbuf.actime = tsp[0].tv_sec;
3814 utbuf.modtime = tsp[1].tv_sec;
3817 return utime(path, utp);
3822utime_internal_i(
int argc,
VALUE *argv,
int follow)
3825 struct timespec tss[2], *tsp = NULL;
3828 args.atime = *argv++;
3829 args.mtime = *argv++;
3831 args.follow = follow;
3833 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3836 if (args.atime == args.mtime)
3843 return apply2files(utime_internal, argc, argv, &args);
3889 return utime_internal_i(argc, argv, FALSE);
3892#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3950 return utime_internal_i(argc, argv, TRUE);
3953#define rb_file_s_lutime rb_f_notimplement
3956#ifdef RUBY_FUNCTION_NAME_STRING
3957# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3959# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3961#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3962NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3964syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3968 const int max_pathlen = MAX_PATH;
3970 const int max_pathlen = MAXPATHLEN;
3981#ifdef RUBY_FUNCTION_NAME_STRING
3982 rb_syserr_fail_path_in(func, e, str);
3984 rb_syserr_fail_path(e, str);
4019 from = rb_str_encode_ospath(from);
4020 to = rb_str_encode_ospath(to);
4023 sys_fail2(from, to);
4028#define rb_file_s_link rb_f_notimplement
4069 from = rb_str_encode_ospath(from);
4070 to = rb_str_encode_ospath(to);
4073 sys_fail2(from, to);
4078#define rb_file_s_symlink rb_f_notimplement
4106 return rb_readlink(path, rb_filesystem_encoding());
4109struct readlink_arg {
4116nogvl_readlink(
void *ptr)
4118 struct readlink_arg *ra = ptr;
4120 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
4124readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
4126 struct readlink_arg ra;
4128 ra.path = RSTRING_PTR(path);
4129 ra.buf = RSTRING_PTR(buf);
4132 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
4143 path = rb_str_encode_ospath(path);
4144 v = rb_enc_str_new(0, size, enc);
4145 while ((rv = readlink_without_gvl(path, v, size)) == size
4147 || (rv < 0 &&
errno == ERANGE)
4156 rb_str_resize(v, 0);
4157 rb_syserr_fail_path(e, path);
4159 rb_str_resize(v, rv);
4164#define rb_file_s_readlink rb_f_notimplement
4168unlink_internal(
const char *path,
void *arg)
4170 return unlink(path);
4213rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
4215 return apply2files(unlink_internal, argc, argv, 0);
4224no_gvl_rename(
void *ptr)
4228 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4306 f = rb_str_encode_ospath(from);
4307 t = rb_str_encode_ospath(to);
4310#if defined __CYGWIN__
4313 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4318 if (chmod(ra.dst, 0666) == 0 &&
4319 unlink(ra.dst) == 0 &&
4320 rename(ra.src, ra.dst) == 0)
4324 syserr_fail2(e, from, to);
4358 rb_error_arity(argc, 0, 1);
4366#if defined __CYGWIN__ || defined DOSISH
4368#define DOSISH_DRIVE_LETTER
4369#define FILE_ALT_SEPARATOR '\\'
4371#ifdef FILE_ALT_SEPARATOR
4372#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4374static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4377#define isdirsep(x) ((x) == '/')
4389#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4390#define isADS(x) ((x) == ':')
4392#define istrailinggarbage(x) 0
4395#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4397#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4398#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4400#if defined(DOSISH_UNC)
4401#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4403#define has_unc(buf) 0
4406#ifdef DOSISH_DRIVE_LETTER
4408has_drive_letter(
const char *buf)
4410 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4435 if (chdir(drive) == 0) {
4436 drvcwd = rb_dir_getwd_ospath();
4448not_same_drive(
VALUE path,
int drive)
4450 const char *p = RSTRING_PTR(path);
4451 if (RSTRING_LEN(path) < 2)
return 0;
4452 if (has_drive_letter(p)) {
4463skiproot(
const char *path,
const char *end)
4465#ifdef DOSISH_DRIVE_LETTER
4466 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4468 while (path < end && isdirsep(*path)) path++;
4469 return (
char *)path;
4473enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4475 while (s < e && !isdirsep(*s)) {
4476 Inc(s, e, mb_enc, enc);
4481#define nextdirsep rb_enc_path_next
4485 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4488#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4489#define skipprefix enc_path_skip_prefix
4491#define skipprefix(path, end, mb_enc, enc) (path)
4494enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4496#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4498 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4500 while (path < end && isdirsep(*path)) path++;
4501 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4502 path + 2 <= end && !isdirsep(path[1])) {
4503 path = enc_path_next(path + 1, end, mb_enc, enc);
4505 return (
char *)path;
4508#ifdef DOSISH_DRIVE_LETTER
4509 if (path + 2 <= end && has_drive_letter(path))
4510 return (
char *)(path + 2);
4513 return (
char *)path;
4517rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4519 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4523skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4525#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4526 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4527 while (p < end && isdirsep(*p)) p++;
4530 return skiproot(path, end);
4535rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4537 return skipprefixroot(path, end, enc);
4541enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4544 while (path < end) {
4545 if (isdirsep(*path)) {
4546 const char *tmp = path++;
4547 while (path < end && isdirsep(*path)) path++;
4548 if (path >= end)
break;
4552 Inc(path, end, mb_enc, enc);
4558rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4560 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4564strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4566 if (RB_UNLIKELY(mb_enc)) {
4567 return enc_path_last_separator(path, end, mb_enc, enc);
4570 const char *cursor = end - 1;
4572 while (cursor >= path && isdirsep(cursor[0])) {
4576 while (cursor >= path) {
4577 if (isdirsep(cursor[0])) {
4578 while (cursor > path && isdirsep(cursor[-1])) {
4581 return (
char *)cursor;
4589chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4591 while (path < end) {
4592 if (isdirsep(*path)) {
4593 const char *last = path++;
4594 while (path < end && isdirsep(*path)) path++;
4595 if (path >= end)
return (
char *)last;
4598 Inc(path, end, mb_enc, enc);
4601 return (
char *)path;
4607 if (path < end && isdirsep(*path)) path++;
4608 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4614 rb_encoding *enc = rb_enc_check_str(path1, path2);
4615 int encidx = rb_enc_to_index(enc);
4616 if (encidx == ENCINDEX_US_ASCII) {
4617 encidx = rb_enc_get_index(path1);
4618 if (encidx == ENCINDEX_US_ASCII)
4619 encidx = rb_enc_get_index(path2);
4620 enc = rb_enc_from_index(encidx);
4627ntfs_tail(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4629 while (path < end && *path ==
'.') path++;
4630 while (path < end && !isADS(*path)) {
4631 if (istrailinggarbage(*path)) {
4632 const char *last = path++;
4633 while (path < end && istrailinggarbage(*path)) path++;
4634 if (path >= end || isADS(*path))
return (
char *)last;
4636 else if (isdirsep(*path)) {
4637 const char *last = path++;
4638 while (path < end && isdirsep(*path)) path++;
4639 if (path >= end)
return (
char *)last;
4640 if (isADS(*path)) path++;
4643 Inc(path, end, mb_enc, enc);
4646 return (
char *)path;
4650#define BUFCHECK(cond) do {\
4653 do {buflen *= 2;} while (cond);\
4654 rb_str_resize(result, buflen);\
4655 buf = RSTRING_PTR(result);\
4657 pend = buf + buflen;\
4661#define BUFINIT(result, buf, p, pend) do {\
4662 if (!result) { result = rb_usascii_str_new(0, 1); } \
4663 p = buf = RSTRING_PTR(result); \
4664 buflen = RSTRING_LEN(result); \
4665 pend = p + buflen; \
4669# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4671# define SKIPPATHSEP(p) 1
4674#define BUFCOPY(srcptr, srclen) do { \
4675 const int skip = SKIPPATHSEP(p); \
4676 rb_str_set_len(result, p-buf+skip); \
4677 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4679 memcpy(p, (srcptr), (srclen)); \
4683#define WITH_ROOTDIFF(stmt) do { \
4684 long rootdiff = root - buf; \
4686 root = buf + rootdiff; \
4690copy_home_path(
VALUE result,
const char *dir)
4696 dirlen = strlen(dir);
4697 rb_str_resize(result, dirlen);
4698 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4699 encidx = rb_filesystem_encindex();
4700 rb_enc_associate_index(result, encidx);
4701#if defined FILE_ALT_SEPARATOR
4703 bool mb_enc = enc_mbclen_needed(enc);
4704 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4705 if (*p == FILE_ALT_SEPARATOR) {
4717 VALUE dirname = rb_getpwdirnam_for_login(user);
4718 if (dirname ==
Qnil) {
4719 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4721 const char *dir = RSTRING_PTR(dirname);
4723 extern char *getlogin(
void);
4724 const char *pwPtr = 0;
4726 # define endpwent() ((void)0)
4727 const char *dir, *username = RSTRING_PTR(user);
4738 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4739 dir = pwPtr = getenv(
"HOME");
4741 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4744 copy_home_path(result, dir);
4750rb_default_home_dir(
VALUE result)
4752 const char *dir = getenv(
"HOME");
4754#if defined HAVE_PWD_H
4768 VALUE login_name = rb_getlogin();
4770# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4775 if (
NIL_P(login_name)) {
4776 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4780 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4781 if (
NIL_P(pw_dir)) {
4782 pw_dir = rb_getpwdiruid();
4783 if (
NIL_P(pw_dir)) {
4784 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4789 copy_home_path(result, RSTRING_PTR(pw_dir));
4790 rb_str_resize(pw_dir, 0);
4795 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4797 return copy_home_path(result, dir);
4803#if NORMALIZE_UTF8PATH
4804 VALUE path = rb_str_normalize_ospath(ptr,
len);
4805 rb_enc_associate(path, fsenc);
4808 return rb_enc_str_new(ptr,
len, fsenc);
4815 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4817 rb_enc_associate(dirname, fsenc);
4821 size_t dirlen = RSTRING_LEN(dirname);
4824 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4825 if (!rb_enc_compatible(fname, dirname)) {
4828 rb_enc_check(fname, dirname);
4829 rb_bug(
"unreachable");
4831 rb_encoding *direnc = fs_enc_check(fname, dirname);
4832 if (direnc != fsenc) {
4839 do {buflen *= 2;}
while (dirlen > buflen);
4840 rb_str_resize(result, buflen);
4841 buf = RSTRING_PTR(result);
4842 memcpy(buf, cwdp, dirlen);
4843 rb_enc_associate(result, *enc);
4844 return buf + dirlen;
4848rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4850 const char *s, *b, *fend;
4851 char *buf, *p, *pend, *root;
4852 size_t buflen, bdiff;
4853 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4856 fend = s + RSTRING_LEN(fname);
4857 enc = rb_str_enc_get(fname);
4858 bool mb_enc = enc_mbclen_needed(enc);
4859 if (!mb_enc &&
RTEST(dname)) {
4860 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4863 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4864 BUFINIT(result, buf, p, pend);
4867 if (s + 1 == fend || isdirsep(s[1])) {
4871 if (++s < fend) ++s;
4872 rb_default_home_dir(result);
4875 s = nextdirsep(b = s, fend, enc);
4878 BUFCHECK(bdiff + userlen >= buflen);
4879 memcpy(p, b, userlen);
4882 rb_enc_associate(result, enc);
4883 rb_home_dir_of(result, result);
4887 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4889 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4890 (
int)userlen, b, fname);
4893 rb_raise(rb_eArgError,
"non-absolute home");
4896 BUFINIT(result, buf, p, pend);
4899#ifdef DOSISH_DRIVE_LETTER
4901 else if (s + 1 < fend && has_drive_letter(s)) {
4902 BUFINIT(result, buf, p, pend);
4904 if (s + 2 < fend && isdirsep(s[2])) {
4907 BUFCHECK(bdiff + 2 >= buflen);
4911 rb_enc_copy(result, fname);
4916 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4917 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4918 BUFINIT(result, buf, p, pend);
4925 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4926 BUFINIT(result, buf, p, pend);
4930 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4933 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4938 else if (s == fend || !rb_is_absolute_path(s)) {
4940 if (!
NIL_P(dname)) {
4942 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4946 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4948 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
4953 result = resized_result;
4957 rb_enc_associate(result, fs_enc_check(result, fname));
4958 BUFINIT(result, buf, p, pend);
4962 VALUE cwd = rb_dir_getwd_ospath();
4966 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
4967 BUFINIT(result, buf, p, pend);
4970#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4971 if (s < fend && isdirsep(*s)) {
4974 p = skipprefix(buf, p, mb_enc, enc);
4978 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4981 BUFINIT(result, buf, p, pend);
4985 do s++;
while (s < fend && isdirsep(*s));
4988 BUFCHECK(bdiff >= buflen);
4989 memset(buf,
'/',
len);
4991 rb_enc_associate(result, fs_enc_check(result, fname));
4993 if (p > buf && p[-1] ==
'/')
4997 BUFCHECK(bdiff + 1 >= buflen);
5002 BUFCHECK(bdiff + 1 >= buflen);
5004 root = skipprefix(buf, p+1, mb_enc, enc);
5017 if (s+1 == fend || isdirsep(*(s+1))) {
5021 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
5031#if defined FILE_ALT_SEPARATOR
5032 case FILE_ALT_SEPARATOR:
5043#if defined FILE_ALT_SEPARATOR
5044 case FILE_ALT_SEPARATOR:
5047 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5055 int n = ignored_char_p(s, fend, enc);
5058 WITH_ROOTDIFF(BUFCOPY(b, s-b));
5066 Inc(s, fend, mb_enc, enc);
5075 if (p == skiproot(buf, p + !!*p) - 1) p++;
5078 rb_enc_check(fname, result);
5085str_shrink(
VALUE str)
5087 rb_str_resize(str, RSTRING_LEN(str));
5091#define expand_path(fname, dname, abs_mode, long_name, result) \
5092 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
5094#define check_expand_path_args(fname, dname) \
5095 (((fname) = rb_get_path(fname)), \
5096 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
5099file_expand_path_1(
VALUE fname,
long extra_capa)
5102 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
5108 check_expand_path_args(fname, dname);
5109 return expand_path(fname, dname, 0, 1,
Qfalse);
5113rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
5115 return expand_path(fname, dname, 0, 0,
Qfalse);
5119rb_file_s_expand_path(
int argc,
const VALUE *argv)
5122 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5168 return rb_file_s_expand_path(c, v);
5174 check_expand_path_args(fname, dname);
5175 return expand_path(fname, dname, 1, 1,
Qfalse);
5179rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5182 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5225 return rb_file_s_absolute_path(c, v);
5253 VALUE path = rb_get_path(fname);
5255 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5259enum rb_realpath_mode {
5263 RB_REALPATH_MODE_MAX
5267realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5268 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5270 const char *pend = unresolved + strlen(unresolved);
5274 while (unresolved < pend) {
5275 const char *testname = unresolved;
5276 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5277 long testnamelen = unresolved_firstsep - unresolved;
5278 const char *unresolved_nextname = unresolved_firstsep;
5279 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5280 unresolved_nextname++;
5281 unresolved = unresolved_nextname;
5282 if (testnamelen == 1 && testname[0] ==
'.') {
5284 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5285 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5286 bool mb_enc = enc_mbclen_needed(enc);
5287 const char *resolved_str = RSTRING_PTR(*resolvedp);
5288 const char *resolved_names = resolved_str + *prefixlenp;
5289 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5290 long len = lastsep ? lastsep - resolved_names : 0;
5291 rb_str_resize(*resolvedp, *prefixlenp +
len);
5297 if (*prefixlenp < RSTRING_LEN(testpath))
5299#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5300 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5301 const char *prefix = RSTRING_PTR(testpath);
5302 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5307 checkval = rb_hash_aref(loopcheck, testpath);
5308 if (!
NIL_P(checkval)) {
5309 if (checkval ==
ID2SYM(resolving)) {
5310 if (mode == RB_REALPATH_CHECK) {
5314 rb_syserr_fail_path(ELOOP, testpath);
5323 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5326 if (e == ENOENT && !
NIL_P(fallback)) {
5327 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5332 if (mode == RB_REALPATH_CHECK)
return -1;
5334 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5335 rb_syserr_fail_path(e, testpath);
5336 *resolvedp = testpath;
5340 rb_syserr_fail_path(e, testpath);
5344 if (S_ISLNK(sbuf.st_mode)) {
5347 const char *link_prefix, *link_names;
5348 long link_prefixlen;
5349 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5350 link = rb_readlink(testpath, enc);
5351 link_prefix = RSTRING_PTR(link);
5352 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5353 link_prefixlen = link_names - link_prefix;
5354 if (link_prefixlen > 0) {
5358 tmpenc = fs_enc_check(*resolvedp, link);
5361 *prefixlenp = link_prefixlen;
5363 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5364 loopcheck, mode, !*unresolved_firstsep))
5373 rb_hash_aset(loopcheck, s, s);
5374 *resolvedp = testpath;
5382#ifdef DOSISH_DRIVE_LETTER
5386ospath_for_expand(
VALUE path)
5389 case ENCINDEX_ASCII_8BIT:
5390 case ENCINDEX_US_ASCII:
5391 return rb_enc_associate_index(
rb_str_dup(path), rb_filesystem_encindex());
5393 return TO_OSPATH(path);
5398rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5402 VALUE unresolved_path;
5407 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5408 char *ptr, *prefixptr = NULL, *pend;
5411#ifdef DOSISH_DRIVE_LETTER
5412 VALUE rootdir = path;
5414 if (!
NIL_P(basedir) && skipprefixroot(ptr, ptr +
len, rb_enc_get(path)) == ptr) {
5419 if (
len >= 2 && has_drive_letter(ptr) && (
len == 2 || !isdirsep(ptr[2]))) {
5422 if (!
NIL_P(basedir)) basedir = ospath_for_expand(rb_get_path(basedir));
5423 path = rb_file_absolute_path(ospath_for_expand(path), basedir);
5430 if (!
NIL_P(basedir)) {
5435 enc = rb_enc_get(unresolved_path);
5436 unresolved_path = TO_OSPATH(unresolved_path);
5438 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5439 if (ptr != path_names) {
5440 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5444 if (!
NIL_P(basedir)) {
5446 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5447 if (ptr != basedir_names) {
5453 curdir = rb_dir_getwd_ospath();
5455 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5460 pend = prefixptr + prefixlen;
5461 bool mb_enc = enc_mbclen_needed(enc);
5462 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5464 prefixlen = ++ptr - prefixptr;
5467#ifdef FILE_ALT_SEPARATOR
5468 while (prefixptr < ptr) {
5469 if (*prefixptr == FILE_ALT_SEPARATOR) {
5472 Inc(prefixptr, pend, mb_enc, enc);
5476 switch (rb_enc_to_index(enc)) {
5477 case ENCINDEX_ASCII_8BIT:
5478 case ENCINDEX_US_ASCII:
5479 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5482 loopcheck = rb_hash_new();
5484 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5487 if (basedir_names) {
5488 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5491 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5494 if (origenc && origenc != rb_enc_get(resolved)) {
5496 rb_enc_associate(resolved, origenc);
5508static VALUE rb_file_join(
long argc,
VALUE *args);
5510#ifndef HAVE_REALPATH
5512rb_check_realpath_emulate_try(
VALUE arg)
5515 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5519rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5523#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5524 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5526# include <sys/syslimits.h>
5527# define NEEDS_REALPATH_BUFFER 1
5531rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5534 VALUE unresolved_path;
5535 char *resolved_ptr = NULL;
5537# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5538 char resolved_buffer[PATH_MAX];
5540 char *
const resolved_buffer = NULL;
5543 if (mode == RB_REALPATH_DIR) {
5544 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5548 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5549 VALUE paths[2] = {basedir, unresolved_path};
5550 unresolved_path = rb_file_join(2, paths);
5552 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5554 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5563 if (
errno == ENOTSUP ||
5565 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5566 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5569 if (mode == RB_REALPATH_CHECK) {
5572 rb_sys_fail_path(unresolved_path);
5574 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5575# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5579# if !defined(__linux__) && !defined(__APPLE__)
5583 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5584 if (mode == RB_REALPATH_CHECK) {
5587 rb_sys_fail_path(unresolved_path);
5591 if (origenc && origenc != rb_enc_get(resolved)) {
5595 rb_enc_associate(resolved, origenc);
5598 if (is_broken_string(resolved)) {
5599 rb_enc_associate(resolved, rb_filesystem_encoding());
5600 if (is_broken_string(resolved)) {
5601 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5608 if (mode == RB_REALPATH_CHECK) {
5612 arg[2] = (
VALUE)origenc;
5615 rb_check_realpath_emulate_rescue,
Qnil);
5618 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5624rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5626 const enum rb_realpath_mode mode =
5627 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5628 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5634 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5651rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5654 VALUE path = argv[0];
5656 return rb_realpath_internal(basedir, path, 1);
5672rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5675 VALUE path = argv[0];
5677 return rb_realpath_internal(basedir, path, 0);
5681rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5685 const char *s, *last;
5687 if (!e || !l2)
return 0;
5689 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5690 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5691 if (c ==
'.')
return l0;
5696 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5701 if (l1 < l2)
return l1;
5704 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5705#if CASEFOLD_FILESYSTEM
5706#define fncomp strncasecmp
5708#define fncomp strncmp
5710 if (fncomp(s, e, l2) == 0) {
5716static inline const char *
5717enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5719 const char *p, *q, *e, *end;
5722 long len = (alllen ? (size_t)*alllen : strlen(name));
5729 name = skipprefix(name, end, mb_enc, enc);
5730#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5731 const char *root = name;
5734 while (name < end && isdirsep(*name)) {
5741#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5745#ifdef DOSISH_DRIVE_LETTER
5746 else if (*p ==
':') {
5759 p = strrdirsep(name, end, mb_enc, enc);
5764 while (isdirsep(*p)) {
5769 n = ntfs_tail(p, end, mb_enc, enc) - p;
5771 n = chompdirsep(p, end, mb_enc, enc) - p;
5773 for (q = p; q - p < n && *q ==
'.'; q++);
5774 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5775 if (*q ==
'.') e = q;
5795ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5797 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5841 const char *name, *p, *fp = 0;
5847 CheckPath(fname, name);
5851 check_path_encoding(fext);
5853 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5854 enc = rb_str_enc_get(fname);
5857 n = RSTRING_LEN(fname);
5858 if (n <= 0 || !*name) {
5859 return rb_enc_str_new(0, 0, enc);
5862 bool mb_enc = enc_mbclen_needed(enc);
5863 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5869 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5874 if (f == RSTRING_LEN(fname)) {
5879 return rb_enc_str_new(p, f, enc);
5882static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5905rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5911 return rb_file_dirname_n(argv[0], n);
5917 return rb_file_dirname_n(fname, 1);
5921rb_file_dirname_n(
VALUE fname,
int n)
5923 const char *name, *root, *p, *end;
5926 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5927 CheckPath(fname, name);
5928 end = name + RSTRING_LEN(fname);
5930 bool mb_enc = !rb_str_enc_fastpath(fname);
5933 root = skiproot(name, end);
5935 if (root > name + 1 && isdirsep(*name))
5936 root = skipprefix(name = root - 2, end, mb_enc, enc);
5938 if (root > name + 1)
5941 if (n > (end - root + 1) / 2) {
5947 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5956 return rb_enc_str_new(
".", 1, enc);
5958#ifdef DOSISH_DRIVE_LETTER
5959 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5960 const char *top = skiproot(name + 2, end);
5961 dirname = rb_enc_str_new(name, 3, enc);
5966 dirname = rb_enc_str_new(name, p - name, enc);
5967#ifdef DOSISH_DRIVE_LETTER
5968 if (root == name + 2 && p == root && name[1] ==
':')
5974static inline const char *
5975enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5977 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5979 p = strrdirsep(name, end, mb_enc, enc);
5983 do name = ++p;
while (isdirsep(*p));
5986 while (*p && *p ==
'.') p++;
5988 if (*p ==
'.' || istrailinggarbage(*p)) {
5990 const char *last = p++, *dot = last;
5991 while (istrailinggarbage(*p)) {
5992 if (*p ==
'.') dot = p;
5995 if (!*p || isADS(*p)) {
5999 if (*last ==
'.' || dot > last) e = dot;
6006 else if (isADS(*p)) {
6010 else if (isdirsep(*p))
6012 Inc(p, end, mb_enc, enc);
6017 if (!e || e == name)
6042 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
6089 CheckPath(fname, name);
6090 long len = RSTRING_LEN(fname);
6093 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
6096 bool mb_enc = !rb_str_enc_fastpath(fname);
6099 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
6100 return rb_enc_str_new(ext,
len, enc);
6124 return rb_get_path(fname);
6156file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
6158 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
6159 return rb_file_join_ary(arg);
6163rb_file_join_ary(
VALUE ary)
6167 const char *name, *tail;
6177 check_path_encoding(tmp);
6178 len += RSTRING_LEN(tmp);
6186 RBASIC_CLEAR_CLASS(result);
6189 switch (OBJ_BUILTIN_TYPE(tmp)) {
6191 if (!checked) check_path_encoding(tmp);
6196 rb_raise(rb_eArgError,
"recursive array");
6208 rb_enc_copy(result, tmp);
6211 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6212 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6215 else if (tail == name +
len) {
6219 enc = fs_enc_check(result, tmp);
6221 rb_enc_associate(result, enc);
6229rb_file_join_fastpath(
long argc,
VALUE *args)
6234 for (i = 0; i < argc; i++) {
6235 VALUE tmp = args[i];
6237 size += RSTRING_LEN(tmp);
6250 const char *name = RSTRING_PTR(result);
6251 for (i = 1; i < argc; i++) {
6252 VALUE tmp = args[i];
6253 long len = RSTRING_LEN(result);
6259 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6262 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6267 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6274 rb_enc_associate(result, new_enc);
6275 encidx = rb_enc_to_index(new_enc);
6281 rb_str_null_check(result);
6286rb_file_join(
long argc,
VALUE *args)
6288 if (RB_UNLIKELY(argc == 0)) {
6292 VALUE result = rb_file_join_fastpath(argc, args);
6293 if (RB_LIKELY(result)) {
6313rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6315 return rb_file_join(argc, argv);
6318#if defined(HAVE_TRUNCATE)
6319struct truncate_arg {
6325nogvl_truncate(
void *ptr)
6327 struct truncate_arg *ta = ptr;
6328 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6360 struct truncate_arg ta;
6365 path = rb_str_encode_ospath(path);
6368 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6370 rb_sys_fail_path(path);
6374#define rb_file_s_truncate rb_f_notimplement
6377#if defined(HAVE_FTRUNCATE)
6378struct ftruncate_arg {
6384nogvl_ftruncate(
void *ptr)
6386 struct ftruncate_arg *fa = ptr;
6388 return (
VALUE)ftruncate(fa->fd, fa->pos);
6429 struct ftruncate_arg fa;
6436 rb_io_flush_raw(obj, 0);
6438 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6439 rb_sys_fail_path(fptr->
pathv);
6444#define rb_file_truncate rb_f_notimplement
6461#include <winerror.h>
6465rb_thread_flock(
void *data)
6468 int old_errno =
errno;
6470 int *op = data, ret = flock(op[0], op[1]);
6473 if (GetLastError() == ERROR_NOT_LOCKED) {
6531 op[1] = op1 =
NUM2INT(operation);
6536 rb_io_flush_raw(obj, 0);
6538 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6543#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6546 if (op1 & LOCK_NB)
return Qfalse;
6549 time.tv_usec = 100 * 1000;
6555#if defined(ERESTART)
6561 rb_syserr_fail_path(e, fptr->
pathv);
6568test_check(
int n,
int argc,
VALUE *argv)
6574 for (i=1; i<n; i++) {
6581#define CHECK(n) test_check((n), argc, argv)
6673 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6677 return rb_file_blockdev_p(0, argv[1]);
6680 return rb_file_chardev_p(0, argv[1]);
6683 return rb_file_directory_p(0, argv[1]);
6686 return rb_file_exist_p(0, argv[1]);
6689 return rb_file_file_p(0, argv[1]);
6692 return rb_file_sgid_p(0, argv[1]);
6695 return rb_file_grpowned_p(0, argv[1]);
6698 return rb_file_sticky_p(0, argv[1]);
6701 return rb_file_symlink_p(0, argv[1]);
6704 return rb_file_owned_p(0, argv[1]);
6707 return rb_file_rowned_p(0, argv[1]);
6710 return rb_file_pipe_p(0, argv[1]);
6713 return rb_file_readable_p(0, argv[1]);
6716 return rb_file_readable_real_p(0, argv[1]);
6719 return rb_file_size_p(0, argv[1]);
6722 return rb_file_socket_p(0, argv[1]);
6725 return rb_file_suid_p(0, argv[1]);
6728 return rb_file_writable_p(0, argv[1]);
6731 return rb_file_writable_real_p(0, argv[1]);
6734 return rb_file_executable_p(0, argv[1]);
6737 return rb_file_executable_real_p(0, argv[1]);
6740 return rb_file_zero_p(0, argv[1]);
6744 if (strchr(
"MAC", cmd)) {
6746 VALUE fname = argv[1];
6749 if (
rb_stat(fname, &st) == -1) {
6752 rb_syserr_fail_path(e, fname);
6757 return stat_atime(&st);
6759 return stat_mtime(&st);
6761 return stat_ctime(&st);
6767 return rb_file_identical_p(0, argv[1], argv[2]);
6770 if (strchr(
"=<>", cmd)) {
6771 struct stat st1, st2;
6778 t1 = stat_mtimespec(&st1);
6779 t2 = stat_mtimespec(&st2);
6783 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6787 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6788 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6792 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6793 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6800 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6803 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6857rb_stat_s_alloc(
VALUE klass)
6878 fname = rb_str_encode_ospath(fname);
6880 rb_sys_fail_path(fname);
6887 rb_st->initialized =
true;
6904 *copy_rb_st = *orig_rb_st;
6946rb_stat_ftype(
VALUE obj)
6948 return rb_file_ftype(get_stat(obj)->ST_(mode));
6964 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6991 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
7028 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
7060 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
7082 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
7102 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
7137rb_stat_owned(
VALUE obj)
7139 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
7144rb_stat_rowned(
VALUE obj)
7146 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
7167rb_stat_grpowned(
VALUE obj)
7170 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
7199 rb_io_stat_data *st = get_stat(obj);
7202 if (geteuid() == 0)
return Qtrue;
7205 if (rb_stat_owned(obj))
7206 return RBOOL(st->ST_(mode) & S_IRUSR);
7209 if (rb_stat_grpowned(obj))
7210 return RBOOL(st->ST_(mode) & S_IRGRP);
7213 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7231 rb_io_stat_data *st = get_stat(obj);
7234 if (getuid() == 0)
return Qtrue;
7237 if (rb_stat_rowned(obj))
7238 return RBOOL(st->ST_(mode) & S_IRUSR);
7241 if (rb_group_member(get_stat(obj)->ST_(gid)))
7242 return RBOOL(st->ST_(mode) & S_IRGRP);
7245 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7273rb_stat_wr(
VALUE obj)
7276 rb_io_stat_data *st = get_stat(obj);
7277 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7278 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7310 rb_io_stat_data *st = get_stat(obj);
7313 if (geteuid() == 0)
return Qtrue;
7316 if (rb_stat_owned(obj))
7317 return RBOOL(st->ST_(mode) & S_IWUSR);
7320 if (rb_stat_grpowned(obj))
7321 return RBOOL(st->ST_(mode) & S_IWGRP);
7324 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7345 rb_io_stat_data *st = get_stat(obj);
7348 if (getuid() == 0)
return Qtrue;
7351 if (rb_stat_rowned(obj))
7352 return RBOOL(st->ST_(mode) & S_IWUSR);
7355 if (rb_group_member(get_stat(obj)->ST_(gid)))
7356 return RBOOL(st->ST_(mode) & S_IWGRP);
7359 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7388rb_stat_ww(
VALUE obj)
7391 rb_io_stat_data *st = get_stat(obj);
7392 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7393 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7435 rb_io_stat_data *st = get_stat(obj);
7438 if (geteuid() == 0) {
7439 return RBOOL(st->ST_(mode) & S_IXUGO);
7443 if (rb_stat_owned(obj))
7444 return RBOOL(st->ST_(mode) & S_IXUSR);
7447 if (rb_stat_grpowned(obj))
7448 return RBOOL(st->ST_(mode) & S_IXGRP);
7451 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7467 rb_io_stat_data *st = get_stat(obj);
7470 if (getuid() == 0) {
7471 return RBOOL(st->ST_(mode) & S_IXUGO);
7475 if (rb_stat_rowned(obj))
7476 return RBOOL(st->ST_(mode) & S_IXUSR);
7479 if (rb_group_member(get_stat(obj)->ST_(gid)))
7480 return RBOOL(st->ST_(mode) & S_IXGRP);
7483 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7506 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7551 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7583 rb_off_t size = get_stat(obj)->ST_(size);
7585 if (size == 0)
return Qnil;
7618rb_stat_suid(
VALUE obj)
7621 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7655rb_stat_sgid(
VALUE obj)
7658 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7692rb_stat_sticky(
VALUE obj)
7695 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7700#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7701#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7712nogvl_mkfifo(
void *ptr)
7714 struct mkfifo_arg *ma = ptr;
7716 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7744 struct mkfifo_arg ma;
7753 path = rb_str_encode_ospath(path);
7754 ma.path = RSTRING_PTR(path);
7755 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7756 rb_sys_fail_path(path);
7761#define rb_file_s_mkfifo rb_f_notimplement
7764static VALUE rb_mFConst;
7767rb_file_const(
const char *name,
VALUE value)
7769 rb_define_const(rb_mFConst, name, value);
7773rb_is_absolute_path(
const char *path)
7775#ifdef DOSISH_DRIVE_LETTER
7776 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7779 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7782 if (path[0] ==
'/')
return 1;
7788ruby_is_fd_loadable(
int fd)
7795 if (fstat(fd, &st) < 0)
7798 if (S_ISREG(st.st_mode))
7801 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7804 if (S_ISDIR(st.st_mode))
7815rb_file_load_ok(
const char *path)
7822 int mode = (O_RDONLY |
7823#if defined O_NONBLOCK
7825#elif defined O_NDELAY
7831 if (!rb_gc_for_fd(
errno))
return 0;
7833 if (fd < 0)
return 0;
7836 ret = ruby_is_fd_loadable(fd);
7843is_explicit_relative(
const char *path)
7845 if (*path++ !=
'.')
return 0;
7846 if (*path ==
'.') path++;
7847 return isdirsep(*path);
7853 int encidx = rb_enc_get_index(orig);
7854 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7855 encidx = rb_filesystem_encindex();
7856 rb_enc_associate_index(path, encidx);
7864nav_component_p(
const char *s,
const char *send)
7866 if ((send - s) >= 2 && s[0] ==
'.') {
7867 return s[1] ==
'.' || isdirsep(s[1]);
7873fname_need_expansion_p(
VALUE fname)
7875 const char *s = RSTRING_PTR(fname);
7876 const long len = RSTRING_LEN(fname);
7877 const char *send = s +
len;
7879 if (nav_component_p(s, send)) {
7884 bool mbenc = enc_mbclen_needed(enc);
7886 s = enc_path_next(s, send, mbenc, enc);
7888 if (nav_component_p(s, send)) {
7892 s = enc_path_next(s, send, mbenc, enc);
7898expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7900 long dname_len = RSTRING_LEN(dname);
7901 const char *dname_ptr = RSTRING_PTR(dname);
7905 if (need_expansion || dname_ptr[0] ==
'~') {
7906 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7911 if (!isdirsep(dname_ptr[dname_len - 1])) {
7920rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
7923 VALUE fname = *filep;
7927 if (!ext[0])
return 0;
7930 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7931 f = RSTRING_PTR(fname);
7936 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7937 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7938 fnlen = RSTRING_LEN(fname);
7939 for (i=0; ext[i]; i++) {
7941 if (rb_file_load_ok(RSTRING_PTR(fname))) {
7942 *filep = copy_path_class(fname, *filep);
7950 long expanded_load_path_maxlen;
7951 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7952 if (!load_path)
return 0;
7955 RBASIC_CLEAR_CLASS(fname);
7956 fnlen = RSTRING_LEN(fname);
7957 bool need_expansion = fname_need_expansion_p(fname);
7960 rb_enc_associate_index(tmp, rb_usascii_encindex());
7962 for (j=0; ext[j]; j++) {
7964 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
7966 if (!RSTRING_LEN(dname))
continue;
7967 expand_feature(fname, dname, tmp, need_expansion);
7969 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7970 *filep = copy_path_class(tmp, *filep);
7976 rb_str_resize(tmp, 0);
7989 path = copy_path_class(file_expand_path_1(path, 0), path);
7990 f = RSTRING_PTR(path);
7994 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7995 if (!rb_file_load_ok(f))
return 0;
7997 path = copy_path_class(file_expand_path_1(path, 0), path);
8001 long expanded_load_path_maxlen;
8002 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
8005 bool need_expansion = fname_need_expansion_p(path);
8007 rb_enc_associate_index(tmp, rb_usascii_encindex());
8008 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
8010 if (!RSTRING_LEN(dname))
continue;
8011 expand_feature(path, dname, tmp, need_expansion);
8013 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
8014 return copy_path_class(tmp, path);
8017 rb_str_resize(tmp, 0);
8025#define define_filetest_function(name, func, argc) do { \
8026 rb_define_module_function(rb_mFileTest, name, func, argc); \
8027 rb_define_singleton_method(rb_cFile, name, func, argc); \
8030const char ruby_null_device[] =
8833#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8834 rb_CFString_class_initialize_before_fork();
8842 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8843 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8844 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8845 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8846 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8847 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8848 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8849 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8850 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8851 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8852 define_filetest_function(
"file?", rb_file_file_p, 1);
8853 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8854 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8855 define_filetest_function(
"size?", rb_file_size_p, 1);
8856 define_filetest_function(
"size", rb_file_s_size, 1);
8857 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8858 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8860 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8861 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8862 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8864 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8865 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8867 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8868 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8869 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8871 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8909 separator = rb_fstring_lit(
"/");
8911 rb_define_const(
rb_cFile,
"Separator", separator);
8913 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
9262 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
9265 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
9267 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
9269 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
9271 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
9273 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
9275 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
9276#if defined(O_NDELAY) || defined(O_NONBLOCK)
9278# define O_NONBLOCK O_NDELAY
9281 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
9284 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
9287 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
9293 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
9294#ifndef O_SHARE_DELETE
9295# define O_SHARE_DELETE 0
9298 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
9301 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
9305 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
9309 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
9313 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
9317 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
9321 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
9325 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
9329 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
9331 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
9333 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
9335 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
9338 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.