14#include "ruby/internal/config.h"
18# include "missing/file.h"
27# include <sys/cygwin.h>
32# if !(defined(__has_feature) && defined(__has_attribute))
37# define API_AVAILABLE(...)
38# define API_DEPRECATED(...)
40# include <CoreFoundation/CFString.h>
57#ifdef HAVE_SYS_PARAM_H
58# include <sys/param.h>
61# define MAXPATHLEN 1024
66#elif defined HAVE_SYS_UTIME_H
67# include <sys/utime.h>
74#ifdef HAVE_SYS_SYSMACROS_H
75# include <sys/sysmacros.h>
81#ifdef HAVE_SYS_MKDEV_H
82# include <sys/mkdev.h>
85#if defined(HAVE_FCNTL_H)
89#if defined(HAVE_SYS_TIME_H)
93#if !defined HAVE_LSTAT && !defined lstat
99# include "win32/file.h"
100# define STAT(p, s) rb_w32_ustati128((p), (s))
102# define lstat(p, s) rb_w32_ulstati128((p), (s))
104# define access(p, m) rb_w32_uaccess((p), (m))
106# define truncate(p, n) rb_w32_utruncate((p), (n))
108# define chmod(p, m) rb_w32_uchmod((p), (m))
110# define chown(p, o, g) rb_w32_uchown((p), (o), (g))
112# define lchown(p, o, g) rb_w32_ulchown((p), (o), (g))
114# define utimensat(s, p, t, f) rb_w32_uutimensat((s), (p), (t), (f))
116# define link(f, t) rb_w32_ulink((f), (t))
118# define unlink(p) rb_w32_uunlink(p)
120# define readlink(f, t, l) rb_w32_ureadlink((f), (t), (l))
122# define rename(f, t) rb_w32_urename((f), (t))
124# define symlink(s, l) rb_w32_usymlink((s), (l))
132# define STAT(p, s) stat((p), (s))
135#ifdef HAVE_STRUCT_STATX_STX_BTIME
136# define ST_(name) stx_ ## name
139# define ST_(name) st_ ## name
143#if defined _WIN32 || defined __APPLE__
145# define TO_OSPATH(str) rb_str_encode_ospath(str)
148# define TO_OSPATH(str) (str)
152#if defined DOSISH || defined __CYGWIN__
157#if defined HAVE_REALPATH && defined __sun && defined __SVR4
170#include "internal/compilers.h"
171#include "internal/dir.h"
172#include "internal/encoding.h"
173#include "internal/error.h"
174#include "internal/file.h"
175#include "internal/io.h"
176#include "internal/load.h"
177#include "internal/object.h"
178#include "internal/process.h"
179#include "internal/thread.h"
180#include "internal/vm.h"
185#define UIANY2NUM(x) \
186 ((sizeof(x) <= sizeof(unsigned int)) ? \
187 UINT2NUM((unsigned)(x)) : \
188 (sizeof(x) <= sizeof(unsigned long)) ? \
189 ULONG2NUM((unsigned long)(x)) : \
190 ULL2NUM((unsigned LONG_LONG)(x)))
197file_path_convert(
VALUE name)
202 if (ENCINDEX_US_ASCII != fname_encidx &&
203 ENCINDEX_ASCII_8BIT != fname_encidx &&
204 (fs_encidx = rb_filesystem_encindex()) != fname_encidx &&
205 rb_default_internal_encoding() &&
209 rb_encoding *fname_encoding = rb_enc_from_index(fname_encidx);
210 rb_encoding *fs_encoding = rb_enc_from_index(fs_encidx);
218check_path_encoding(
VALUE str)
220 if (RB_UNLIKELY(!rb_str_enc_fastpath(str))) {
222 if (!rb_enc_asciicompat(enc)) {
230rb_get_path_check_to_string(
VALUE obj)
239 tmp = rb_check_funcall_default(obj, to_path, 0, 0, obj);
245rb_get_path_check_convert(
VALUE obj)
247 obj = file_path_convert(obj);
248 rb_get_path_check_no_convert(obj);
254rb_get_path_check_no_convert(
VALUE obj)
256 check_path_encoding(obj);
257 if (!rb_str_to_cstr(obj)) {
258 rb_raise(rb_eArgError,
"path name contains null byte");
267 return rb_get_path(obj);
273 return rb_get_path_check_convert(rb_get_path_check_to_string(obj));
277check_path(
VALUE obj,
const char **cstr)
279 VALUE str = rb_get_path_check_convert(rb_get_path_check_to_string(obj));
283 *cstr = RSTRING_PTR(str);
287#define CheckPath(str, cstr) RB_GC_GUARD(str) = check_path(str, &cstr);
294#if 0 && defined _WIN32
295 if (encidx == ENCINDEX_ASCII_8BIT) {
296 encidx = rb_filesystem_encindex();
299 if (encidx != ENCINDEX_ASCII_8BIT && encidx != ENCINDEX_UTF_8) {
309# define NORMALIZE_UTF8PATH 1
311# ifdef HAVE_WORKING_FORK
312static CFMutableStringRef
313mutable_CFString_new(CFStringRef *s,
const char *ptr,
long len)
315 const CFAllocatorRef alloc = kCFAllocatorDefault;
316 *s = CFStringCreateWithBytesNoCopy(alloc, (
const UInt8 *)ptr,
len,
317 kCFStringEncodingUTF8, FALSE,
319 return CFStringCreateMutableCopy(alloc,
len, *s);
322# define mutable_CFString_release(m, s) (CFRelease(m), CFRelease(s))
325rb_CFString_class_initialize_before_fork(
void)
348 const char small_str[] =
"/";
349 long len =
sizeof(small_str) - 1;
357 for (
int i = 0; i < 2; i++) {
358 CFMutableStringRef m = mutable_CFString_new(&s, small_str,
len);
359 mutable_CFString_release(m, s);
365rb_str_append_normalized_ospath(
VALUE str,
const char *ptr,
long len)
370 CFMutableStringRef m = mutable_CFString_new(&s, ptr,
len);
371 long oldlen = RSTRING_LEN(str);
373 CFStringNormalize(m, kCFStringNormalizationFormC);
374 all = CFRangeMake(0, CFStringGetLength(m));
375 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE, NULL, 0, &buflen);
377 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE,
378 (UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
380 mutable_CFString_release(m, s);
385rb_str_normalize_ospath(
const char *ptr,
long len)
388 const char *e = ptr +
len;
397 rb_enc_associate(str, enc);
402 int r = rb_enc_precise_mbclen(p, e, enc);
406 rb_str_append_normalized_ospath(str, p1, p-p1);
413 c = rb_enc_mbc_to_codepoint(p, e, enc);
414 if ((0x2000 <= c && c <= 0x2FFF) || (0xF900 <= c && c <= 0xFAFF) ||
415 (0x2F800 <= c && c <= 0x2FAFF)) {
417 rb_str_append_normalized_ospath(str, p1, p-p1);
428 rb_str_append_normalized_ospath(str, p1, p-p1);
435ignored_char_p(
const char *p,
const char *e,
rb_encoding *enc)
438 if (p+3 > e)
return 0;
439 switch ((
unsigned char)*p) {
441 switch ((
unsigned char)p[1]) {
443 c = (
unsigned char)p[2];
445 if (c >= 0x8c && c <= 0x8f)
return 3;
447 if (c >= 0xaa && c <= 0xae)
return 3;
450 c = (
unsigned char)p[2];
452 if (c >= 0xaa && c <= 0xaf)
return 3;
458 if ((
unsigned char)p[1] == 0xbb &&
459 (
unsigned char)p[2] == 0xbf)
466# define NORMALIZE_UTF8PATH 0
469#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
480 int (*func)(
const char *,
void *);
486no_gvl_apply2files(
void *ptr)
490 for (aa->i = 0; aa->i < aa->argc; aa->i++) {
491 if (aa->func(aa->fn[aa->i].ptr, aa->arg) < 0) {
500NORETURN(
static void utime_failed(
struct apply_arg *));
501static int utime_internal(
const char *,
void *);
505apply2files(
int (*func)(
const char *,
void *),
int argc,
VALUE *argv,
void *arg)
509 const long len = (long)(offsetof(
struct apply_arg, fn) + (size * argc));
517 for (aa->i = 0; aa->i < argc; aa->i++) {
518 VALUE path = rb_get_path(argv[aa->i]);
520 path = rb_str_encode_ospath(path);
521 aa->fn[aa->i].ptr = RSTRING_PTR(path);
522 aa->fn[aa->i].path = path;
525 IO_WITHOUT_GVL(no_gvl_apply2files, aa);
528 if (func == utime_internal) {
532 rb_syserr_fail_path(aa->errnum, aa->fn[aa->i].path);
551 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
555 rb_io_stat_data stat;
568rb_stat_new(
const struct stat *st)
574# define CP(m) .stx_ ## m = st->st_ ## m
575# define CP_32(m) .stx_ ## m = (uint32_t)st->st_ ## m
576# define CP_TS(m) .stx_ ## m = stat_ ## m ## spec(st)
577 rb_st->stat = (
struct statx){
578 .stx_mask = STATX_BASIC_STATS,
595 rb_st->initialized =
true;
603rb_statx_new(
const rb_io_stat_data *st)
609 rb_st->initialized =
true;
615static rb_io_stat_data*
620 if (!rb_st->initialized) rb_raise(
rb_eTypeError,
"uninitialized File::Stat");
626statx_mtimespec(
const rb_io_stat_data *st)
628 return st->stx_mtime;
631# define statx_mtimespec stat_mtimespec
669 if (ts1.tv_sec == ts2.tv_sec) {
670 if (ts1.tv_nsec == ts2.tv_nsec)
return INT2FIX(0);
671 if (ts1.tv_nsec < ts2.tv_nsec)
return INT2FIX(-1);
674 if (ts1.tv_sec < ts2.tv_sec)
return INT2FIX(-1);
680#define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
683# define NUM2DEVT(v) NUM2UINT(v)
686# define DEVT2NUM(v) UINT2NUM(v)
688#ifndef PRI_DEVT_PREFIX
689# define PRI_DEVT_PREFIX ""
703rb_stat_dev(
VALUE self)
706 unsigned int m = get_stat(self)->stx_dev_major;
707 unsigned int n = get_stat(self)->stx_dev_minor;
709#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
710 return DEVT2NUM(get_stat(self)->st_dev);
711#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
712 return ULONG2NUM(get_stat(self)->st_dev);
714 return ULL2NUM(get_stat(self)->st_dev);
730rb_stat_dev_major(
VALUE self)
733 return UINT2NUM(get_stat(self)->stx_dev_major);
735 return UINT2NUM(major(get_stat(self)->st_dev));
753rb_stat_dev_minor(
VALUE self)
756 return UINT2NUM(get_stat(self)->stx_dev_minor);
758 return UINT2NUM(minor(get_stat(self)->st_dev));
775rb_stat_ino(
VALUE self)
777 rb_io_stat_data *ptr = get_stat(self);
778#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
780 return rb_integer_unpack(&ptr->st_ino, 2,
781 SIZEOF_STRUCT_STAT_ST_INO, 0,
785 return UIANY2NUM(ptr->ST_(ino));
803rb_stat_mode(
VALUE self)
805 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
821rb_stat_nlink(
VALUE self)
824 const rb_io_stat_data *ptr = get_stat(self);
826 return UIANY2NUM(ptr->ST_(nlink));
840rb_stat_uid(
VALUE self)
842 return UIDT2NUM(get_stat(self)->ST_(uid));
856rb_stat_gid(
VALUE self)
858 return GIDT2NUM(get_stat(self)->ST_(gid));
874rb_stat_rdev(
VALUE self)
877 unsigned int m = get_stat(self)->stx_rdev_major;
878 unsigned int n = get_stat(self)->stx_rdev_minor;
880#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
882#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
883 return DEVT2NUM(get_stat(self)->ST_(rdev));
884#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
885 return ULONG2NUM(get_stat(self)->ST_(rdev));
887 return ULL2NUM(get_stat(self)->ST_(rdev));
903rb_stat_rdev_major(
VALUE self)
906 return UINT2NUM(get_stat(self)->stx_rdev_major);
907#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
908 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
926rb_stat_rdev_minor(
VALUE self)
929 return UINT2NUM(get_stat(self)->stx_rdev_minor);
930#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
931 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
947rb_stat_size(
VALUE self)
949 return OFFT2NUM(get_stat(self)->ST_(size));
964rb_stat_blksize(
VALUE self)
966#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
967 return ULONG2NUM(get_stat(self)->ST_(blksize));
985rb_stat_blocks(
VALUE self)
987#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
988# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
989 return ULL2NUM(get_stat(self)->ST_(blocks));
991 return ULONG2NUM(get_stat(self)->ST_(blocks));
999stat_atimespec(
const struct stat *st)
1002 ts.tv_sec = st->st_atime;
1003#if defined(HAVE_STRUCT_STAT_ST_ATIM)
1004 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
1005#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
1006 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
1007#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
1008 ts.tv_nsec = (uint32_t)st->st_atimensec;
1017statx_atimespec(
const rb_io_stat_data *st)
1019 return st->stx_atime;
1022# define statx_atimespec stat_atimespec
1032stat_atime(
const struct stat *st)
1034 return stat_time(stat_atimespec(st));
1038stat_mtimespec(
const struct stat *st)
1041 ts.tv_sec = st->st_mtime;
1042#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1043 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1044#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1045 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1046#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1047 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1055stat_mtime(
const struct stat *st)
1057 return stat_time(stat_mtimespec(st));
1061stat_ctimespec(
const struct stat *st)
1064 ts.tv_sec = st->st_ctime;
1065#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1066 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1067#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1068 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1069#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1070 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1079statx_ctimespec(
const rb_io_stat_data *st)
1081 return st->stx_ctime;
1084# define statx_ctimespec stat_ctimespec
1088stat_ctime(
const struct stat *st)
1090 return stat_time(stat_ctimespec(st));
1093#define HAVE_STAT_BIRTHTIME
1094#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1096statx_birthtime(
const rb_io_stat_data *st)
1101#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1102static VALUE statx_birthtime(
const rb_io_stat_data *st);
1103#elif defined(_WIN32)
1104# define statx_birthtime stat_ctime
1106# undef HAVE_STAT_BIRTHTIME
1153rb_stat_atime(
VALUE self)
1155 return stat_time(statx_atimespec(get_stat(self)));
1169rb_stat_mtime(
VALUE self)
1171 return stat_time(statx_mtimespec(get_stat(self)));
1189rb_stat_ctime(
VALUE self)
1191 return stat_time(statx_ctimespec(get_stat(self)));
1194#if defined(HAVE_STAT_BIRTHTIME)
1216rb_stat_birthtime(
VALUE self)
1218 return statx_birthtime(get_stat(self));
1221# define rb_stat_birthtime rb_f_notimplement
1240rb_stat_inspect(
VALUE self)
1244 static const struct {
1248 {
"dev", rb_stat_dev},
1249 {
"ino", rb_stat_ino},
1250 {
"mode", rb_stat_mode},
1251 {
"nlink", rb_stat_nlink},
1252 {
"uid", rb_stat_uid},
1253 {
"gid", rb_stat_gid},
1254 {
"rdev", rb_stat_rdev},
1255 {
"size", rb_stat_size},
1256 {
"blksize", rb_stat_blksize},
1257 {
"blocks", rb_stat_blocks},
1258 {
"atime", rb_stat_atime},
1259 {
"mtime", rb_stat_mtime},
1260 {
"ctime", rb_stat_ctime},
1261#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1262 {
"birthtime", rb_stat_birthtime},
1268 if (!rb_st->initialized) {
1276 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1284 v = (*member[i].func)(self);
1286 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1288 else if (i == 0 || i == 6) {
1289 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1309no_gvl_fstat(
void *data)
1312 return (
VALUE)fstat(arg->file.fd, arg->st);
1316fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1320 data.file.fd = fptr->
fd;
1323 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1327no_gvl_stat(
void * data)
1330 return (
void *)(
VALUE)STAT(arg->file.path, arg->st);
1334stat_without_gvl(
const char *path,
struct stat *st)
1338 data.file.path = path;
1341 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1344#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1345 defined(HAVE_STRUCT_STATX_STX_BTIME)
1347# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1350# ifdef HAVE_SYSCALL_H
1351# include <syscall.h>
1352# elif defined HAVE_SYS_SYSCALL_H
1353# include <sys/syscall.h>
1355# if defined __linux__
1356# include <linux/stat.h>
1358statx(
int dirfd,
const char *pathname,
int flags,
1359 unsigned int mask,
struct statx *statxbuf)
1361 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1366typedef struct no_gvl_rb_io_stat_data {
1372} no_gvl_rb_io_stat_data;
1375io_blocking_statx(
void *data)
1377 no_gvl_rb_io_stat_data *arg = data;
1378 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1382no_gvl_statx(
void *data)
1384 return (
void *)io_blocking_statx(data);
1388statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1390 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1393 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1397lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1399 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1402 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1406fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1408 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1411 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1414#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1417rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1422 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1427 result = fstatx_without_gvl(fptr, stx, mask);
1432 file = rb_str_encode_ospath(file);
1433 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1439# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1441NORETURN(
static void statx_notimplement(
const char *field_name));
1446statx_notimplement(
const char *field_name)
1449 "%s is unimplemented on this filesystem",
1454statx_birthtime(
const rb_io_stat_data *stx)
1456 if (!statx_has_birthtime(stx)) {
1458 statx_notimplement(
"birthtime");
1460 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1465# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1466# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1467# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1468# define rb_statx(file, stx, mask) rb_stat(file, stx)
1469# define STATX(path, st, mask) STAT(path, st)
1471#if defined(HAVE_STAT_BIRTHTIME)
1472# define statx_has_birthtime(st) 1
1474# define statx_has_birthtime(st) 0
1481# define FSTAT(fd, st) fstat(fd, st)
1490 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1495 result = fstat_without_gvl(fptr, st);
1500 file = rb_str_encode_ospath(file);
1501 result = stat_without_gvl(RSTRING_PTR(file), st);
1539 fname = rb_str_encode_ospath(fname);
1540 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1541 rb_sys_fail_path(fname);
1543 return rb_statx_new(&st);
1562rb_io_stat(
VALUE obj)
1568 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1569 rb_sys_fail_path(fptr->
pathv);
1571 return rb_statx_new(&st);
1576no_gvl_lstat(
void *ptr)
1579 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1583lstat_without_gvl(
const char *path,
struct stat *st)
1587 data.file.path = path;
1590 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1627 fname = rb_str_encode_ospath(fname);
1628 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1629 rb_sys_fail_path(fname);
1631 return rb_statx_new(&st);
1633 return rb_file_s_stat(klass, fname);
1652rb_file_lstat(
VALUE obj)
1661 path = rb_str_encode_ospath(fptr->
pathv);
1662 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1663 rb_sys_fail_path(fptr->
pathv);
1665 return rb_statx_new(&st);
1667 return rb_io_stat(obj);
1672rb_group_member(GETGROUPS_T gid)
1674#if defined(_WIN32) || !defined(HAVE_GETGROUPS)
1683 if (getgid() == gid || getegid() == gid)
1686 groups = getgroups(0, NULL);
1687 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1688 anum = getgroups(groups, gary);
1689 while (--anum >= 0) {
1690 if (gary[anum] == gid) {
1703# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1706#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1707#define USE_GETEUID 1
1712eaccess(
const char *path,
int mode)
1721 if (getuid() == euid && getgid() == getegid())
1722 return access(path, mode);
1724 if (STAT(path, &st) < 0)
1734 if (st.st_mode & S_IXUGO)
1740 if (st.st_uid == euid)
1742 else if (rb_group_member(st.st_gid))
1745 if ((
int)(st.st_mode & mode) == mode)
return 0;
1749 return access(path, mode);
1760nogvl_eaccess(
void *ptr)
1764 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1768rb_eaccess(
VALUE fname,
int mode)
1773 fname = rb_str_encode_ospath(fname);
1777 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1781nogvl_access(
void *ptr)
1785 return (
void *)(
VALUE)access(aa->path, aa->mode);
1789rb_access(
VALUE fname,
int mode)
1794 fname = rb_str_encode_ospath(fname);
1798 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1841# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1847 if (S_ISDIR(st.st_mode))
return Qtrue;
1868# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1874 if (S_ISFIFO(st.st_mode))
return Qtrue;
1905# define S_ISLNK(m) _S_ISLNK(m)
1908# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1911# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1921 fname = rb_str_encode_ospath(fname);
1923 if (S_ISLNK(st.st_mode))
return Qtrue;
1946# define S_ISSOCK(m) _S_ISSOCK(m)
1949# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1952# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1962 if (S_ISSOCK(st.st_mode))
return Qtrue;
1990# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1992# define S_ISBLK(m) (0)
2000 if (S_ISBLK(st.st_mode))
return Qtrue;
2027# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2033 if (S_ISCHR(st.st_mode))
return Qtrue;
2081 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2096rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2098 return RBOOL(rb_access(fname, R_OK) >= 0);
2102# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2106# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2126rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2132 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2133 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2153 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2168rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2170 return RBOOL(rb_access(fname, W_OK) >= 0);
2190rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2196 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2197 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2240 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2259rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2261 return RBOOL(rb_access(fname, X_OK) >= 0);
2265# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2297 return RBOOL(S_ISREG(st.st_mode));
2343 return RBOOL(st.st_size == 0);
2362 if (st.st_size == 0)
return Qnil;
2383 return RBOOL(st.st_uid == geteuid());
2392 return RBOOL(st.st_uid == getuid());
2424 if (rb_group_member(st.st_gid))
return Qtrue;
2429#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2431check3rdbyte(
VALUE fname,
int mode)
2436 return RBOOL(st.st_mode & mode);
2453 return check3rdbyte(fname, S_ISUID);
2472 return check3rdbyte(fname, S_ISGID);
2491 return check3rdbyte(fname, S_ISVTX);
2528 struct stat st1, st2;
2532 if (st1.st_dev != st2.st_dev)
return Qfalse;
2533 if (st1.st_ino != st2.st_ino)
return Qfalse;
2537 return rb_w32_file_identical_p(fname1, fname2);
2555 if (
rb_stat(fname, &st) < 0) {
2558 rb_syserr_fail_path(e, fname);
2564rb_file_ftype(mode_t mode)
2568 if (S_ISREG(mode)) {
2571 else if (S_ISDIR(mode)) {
2574 else if (S_ISCHR(mode)) {
2575 t =
"characterSpecial";
2578 else if (S_ISBLK(mode)) {
2583 else if (S_ISFIFO(mode)) {
2588 else if (S_ISLNK(mode)) {
2593 else if (S_ISSOCK(mode)) {
2601 return rb_fstring_cstr(t);
2643 fname = rb_str_encode_ospath(fname);
2645 rb_sys_fail_path(fname);
2648 return rb_file_ftype(st.st_mode);
2696 if (
rb_stat(fname, &st) < 0) {
2699 rb_syserr_fail_path(e, fname);
2701 return stat_time(stat_atimespec(&st));
2728rb_file_atime(
VALUE obj)
2734 if (fstat(fptr->
fd, &st) == -1) {
2735 rb_sys_fail_path(fptr->
pathv);
2737 return stat_time(stat_atimespec(&st));
2757 if (
rb_stat(fname, &st) < 0) {
2760 rb_syserr_fail_path(e, fname);
2762 return stat_time(stat_mtimespec(&st));
2776rb_file_mtime(
VALUE obj)
2782 if (fstat(fptr->
fd, &st) == -1) {
2783 rb_sys_fail_path(fptr->
pathv);
2785 return stat_time(stat_mtimespec(&st));
2828 if (
rb_stat(fname, &st) < 0) {
2831 rb_syserr_fail_path(e, fname);
2833 return stat_time(stat_ctimespec(&st));
2850rb_file_ctime(
VALUE obj)
2856 if (fstat(fptr->
fd, &st) == -1) {
2857 rb_sys_fail_path(fptr->
pathv);
2859 return stat_time(stat_ctimespec(&st));
2862#if defined(HAVE_STAT_BIRTHTIME)
2893 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
2896 rb_syserr_fail_path(e, fname);
2898 return statx_birthtime(&st);
2901# define rb_file_s_birthtime rb_f_notimplement
2904#if defined(HAVE_STAT_BIRTHTIME)
2925rb_file_birthtime(
VALUE obj)
2931 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
2932 rb_sys_fail_path(fptr->
pathv);
2934 return statx_birthtime(&st);
2937# define rb_file_birthtime rb_f_notimplement
2949 rb_io_flush_raw(file, 0);
2952 if (fstat(fptr->
fd, &st) == -1) {
2953 rb_sys_fail_path(fptr->
pathv);
2974file_size(
VALUE self)
2976 return OFFT2NUM(rb_file_size(self));
2985nogvl_chmod(
void *ptr)
2988 int ret = chmod(data->path, data->mode);
2989 return (
void *)(
VALUE)ret;
2993rb_chmod(
const char *path, mode_t mode)
2999 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
3003chmod_internal(
const char *path,
void *mode)
3005 return chmod(path, *(mode_t *)mode);
3043 return apply2files(chmod_internal, argc, argv, &mode);
3047struct nogvl_fchmod_data {
3053io_blocking_fchmod(
void *ptr)
3055 struct nogvl_fchmod_data *data = ptr;
3056 int ret = fchmod(data->fd, data->mode);
3061rb_fchmod(
struct rb_io* io, mode_t mode)
3064 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3065 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3097#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3105 if (rb_fchmod(fptr, mode) == -1) {
3106 if (HAVE_FCHMOD ||
errno != ENOSYS)
3107 rb_sys_fail_path(fptr->
pathv);
3110 if (!HAVE_FCHMOD)
return INT2FIX(0);
3113#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3115 path = rb_str_encode_ospath(fptr->
pathv);
3116 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3117 rb_sys_fail_path(fptr->
pathv);
3123#if defined(HAVE_LCHMOD)
3125lchmod_internal(
const char *path,
void *mode)
3127 return lchmod(path, *(mode_t *)mode);
3164 return apply2files(lchmod_internal, argc, argv, &mode);
3167#define rb_file_s_lchmod rb_f_notimplement
3170static inline rb_uid_t
3174 return (rb_uid_t)-1;
3179static inline rb_gid_t
3183 return (rb_gid_t)-1;
3194chown_internal(
const char *path,
void *arg)
3197 return chown(path, args->owner, args->group);
3251 arg.owner = to_uid(*argv++);
3252 arg.group = to_gid(*argv++);
3254 return apply2files(chown_internal, argc, argv, &arg);
3266nogvl_chown(
void *ptr)
3269 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3273rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3276 .as = {.path = path},
3277 .new = {.owner = owner, .group = group},
3279 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3284nogvl_fchown(
void *ptr)
3287 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3291rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3296 .new = {.owner = owner, .group = group},
3298 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3332 path = rb_str_encode_ospath(fptr->
pathv);
3333 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3334 rb_sys_fail_path(fptr->
pathv);
3336 if (rb_fchown(fptr->
fd, o, g) == -1)
3337 rb_sys_fail_path(fptr->
pathv);
3343#if defined(HAVE_LCHOWN)
3345lchown_internal(
const char *path,
void *arg)
3348 return lchown(path, args->owner, args->group);
3405 arg.owner = to_uid(*argv++);
3406 arg.group = to_gid(*argv++);
3408 return apply2files(lchown_internal, argc, argv, &arg);
3411#define rb_file_s_lchown rb_f_notimplement
3421NORETURN(
static void utime_failed(
struct apply_arg *));
3427 VALUE path = aa->fn[aa->i].path;
3430 if (ua->tsp && e == EINVAL) {
3433 VALUE atime = ua->atime;
3434 VALUE mtime = ua->mtime;
3436 if (!
NIL_P(atime)) {
3439 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3442 if (
NIL_P(a)) e[0] = m;
3458 rb_syserr_fail_path(e, path);
3462#if defined(HAVE_UTIMES)
3464# if !defined(HAVE_UTIMENSAT)
3466# elif defined(__APPLE__) && \
3467 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3469# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3470typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3474static inline utimensat_func *
3481# define utimensat rb_utimensat()
3488utime_internal(
const char *path,
void *arg)
3491 const struct timespec *tsp = v->tsp;
3492 struct timeval tvbuf[2], *tvp = NULL;
3494#if defined(HAVE_UTIMENSAT)
3495# if defined(__APPLE__)
3496 const int try_utimensat = utimensat != NULL;
3497 const int try_utimensat_follow = utimensat != NULL;
3499# define TRY_UTIMENSAT 1
3500 static int try_utimensat = 1;
3501# ifdef AT_SYMLINK_NOFOLLOW
3502 static int try_utimensat_follow = 1;
3504 const int try_utimensat_follow = 0;
3509 if (v->follow ? try_utimensat_follow : try_utimensat) {
3510# ifdef AT_SYMLINK_NOFOLLOW
3512 flags = AT_SYMLINK_NOFOLLOW;
3516 int result = utimensat(AT_FDCWD, path, tsp, flags);
3517# ifdef TRY_UTIMENSAT
3518 if (result < 0 &&
errno == ENOSYS) {
3519# ifdef AT_SYMLINK_NOFOLLOW
3520 try_utimensat_follow = 0;
3532 tvbuf[0].tv_sec = tsp[0].tv_sec;
3533 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3534 tvbuf[1].tv_sec = tsp[1].tv_sec;
3535 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3539 if (v->follow)
return lutimes(path, tvp);
3541 return utimes(path, tvp);
3546#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3554utime_internal(
const char *path,
void *arg)
3558 struct utimbuf utbuf, *utp = NULL;
3560 utbuf.actime = tsp[0].tv_sec;
3561 utbuf.modtime = tsp[1].tv_sec;
3564 return utime(path, utp);
3569utime_internal_i(
int argc,
VALUE *argv,
int follow)
3572 struct timespec tss[2], *tsp = NULL;
3575 args.atime = *argv++;
3576 args.mtime = *argv++;
3578 args.follow = follow;
3580 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3583 if (args.atime == args.mtime)
3590 return apply2files(utime_internal, argc, argv, &args);
3607 return utime_internal_i(argc, argv, FALSE);
3610#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3668 return utime_internal_i(argc, argv, TRUE);
3671#define rb_file_s_lutime rb_f_notimplement
3674#ifdef RUBY_FUNCTION_NAME_STRING
3675# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3677# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3679#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3680NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3682syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3686 const int max_pathlen = MAX_PATH;
3688 const int max_pathlen = MAXPATHLEN;
3699#ifdef RUBY_FUNCTION_NAME_STRING
3700 rb_syserr_fail_path_in(func, e, str);
3702 rb_syserr_fail_path(e, str);
3737 from = rb_str_encode_ospath(from);
3738 to = rb_str_encode_ospath(to);
3741 sys_fail2(from, to);
3746#define rb_file_s_link rb_f_notimplement
3783 from = rb_str_encode_ospath(from);
3784 to = rb_str_encode_ospath(to);
3787 sys_fail2(from, to);
3792#define rb_file_s_symlink rb_f_notimplement
3820 return rb_readlink(path, rb_filesystem_encoding());
3823struct readlink_arg {
3830nogvl_readlink(
void *ptr)
3832 struct readlink_arg *ra = ptr;
3834 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
3838readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
3840 struct readlink_arg ra;
3842 ra.path = RSTRING_PTR(path);
3843 ra.buf = RSTRING_PTR(buf);
3846 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
3857 path = rb_str_encode_ospath(path);
3858 v = rb_enc_str_new(0, size, enc);
3859 while ((rv = readlink_without_gvl(path, v, size)) == size
3861 || (rv < 0 &&
errno == ERANGE)
3870 rb_str_resize(v, 0);
3871 rb_syserr_fail_path(e, path);
3873 rb_str_resize(v, rv);
3878#define rb_file_s_readlink rb_f_notimplement
3882unlink_internal(
const char *path,
void *arg)
3884 return unlink(path);
3913rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
3915 return apply2files(unlink_internal, argc, argv, 0);
3924no_gvl_rename(
void *ptr)
3928 return (
void *)(
VALUE)rename(ra->src, ra->dst);
4006 f = rb_str_encode_ospath(from);
4007 t = rb_str_encode_ospath(to);
4010#if defined __CYGWIN__
4013 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
4018 if (chmod(ra.dst, 0666) == 0 &&
4019 unlink(ra.dst) == 0 &&
4020 rename(ra.src, ra.dst) == 0)
4024 syserr_fail2(e, from, to);
4059 rb_error_arity(argc, 0, 1);
4067#if defined __CYGWIN__ || defined DOSISH
4069#define DOSISH_DRIVE_LETTER
4070#define FILE_ALT_SEPARATOR '\\'
4072#ifdef FILE_ALT_SEPARATOR
4073#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
4075static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
4078#define isdirsep(x) ((x) == '/')
4091# define USE_NTFS_ADS 1
4093# define USE_NTFS_ADS 0
4098#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4100#define istrailinggarbage(x) 0
4104# define isADS(x) ((x) == ':')
4109#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4111#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4112#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4114#if defined(DOSISH_UNC)
4115#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4117#define has_unc(buf) 0
4120#ifdef DOSISH_DRIVE_LETTER
4122has_drive_letter(
const char *buf)
4124 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4149 if (chdir(drive) == 0) {
4150 drvcwd = rb_dir_getwd_ospath();
4162not_same_drive(
VALUE path,
int drive)
4164 const char *p = RSTRING_PTR(path);
4165 if (RSTRING_LEN(path) < 2)
return 0;
4166 if (has_drive_letter(p)) {
4177skiproot(
const char *path,
const char *end)
4179#ifdef DOSISH_DRIVE_LETTER
4180 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4182 while (path < end && isdirsep(*path)) path++;
4183 return (
char *)path;
4187enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4189 while (s < e && !isdirsep(*s)) {
4190 Inc(s, e, mb_enc, enc);
4195#define nextdirsep rb_enc_path_next
4199 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4202#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4203#define skipprefix enc_path_skip_prefix
4205#define skipprefix(path, end, mb_enc, enc) (path)
4208enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4210#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4212 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4214 while (path < end && isdirsep(*path)) path++;
4215 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4216 path + 2 <= end && !isdirsep(path[1])) {
4217 path = enc_path_next(path + 1, end, mb_enc, enc);
4219 return (
char *)path;
4222#ifdef DOSISH_DRIVE_LETTER
4223 if (path + 2 <= end && has_drive_letter(path))
4224 return (
char *)(path + 2);
4227 return (
char *)path;
4231rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4233 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4237skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4239#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4240 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4241 while (p < end && isdirsep(*p)) p++;
4244 return skiproot(path, end);
4249rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4251 return skipprefixroot(path, end, enc);
4255enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4258 while (path < end) {
4259 if (isdirsep(*path)) {
4260 const char *tmp = path++;
4261 while (path < end && isdirsep(*path)) path++;
4262 if (path >= end)
break;
4266 Inc(path, end, mb_enc, enc);
4272rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4274 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4278strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4280 if (RB_UNLIKELY(mb_enc)) {
4281 return enc_path_last_separator(path, end, mb_enc, enc);
4284 const char *cursor = end - 1;
4286 while (isdirsep(cursor[0])) {
4290 while (cursor >= path) {
4291 if (isdirsep(cursor[0])) {
4292 while (cursor > path && isdirsep(cursor[-1])) {
4295 return (
char *)cursor;
4303chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4305 while (path < end) {
4306 if (isdirsep(*path)) {
4307 const char *last = path++;
4308 while (path < end && isdirsep(*path)) path++;
4309 if (path >= end)
return (
char *)last;
4312 Inc(path, end, mb_enc, enc);
4315 return (
char *)path;
4321 if (path < end && isdirsep(*path)) path++;
4322 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4328 rb_encoding *enc = rb_enc_check_str(path1, path2);
4329 int encidx = rb_enc_to_index(enc);
4330 if (encidx == ENCINDEX_US_ASCII) {
4331 encidx = rb_enc_get_index(path1);
4332 if (encidx == ENCINDEX_US_ASCII)
4333 encidx = rb_enc_get_index(path2);
4334 enc = rb_enc_from_index(encidx);
4341ntfs_tail(
const char *path,
const char *end,
rb_encoding *enc)
4343 bool mb_enc = enc_mbclen_needed(enc);
4344 while (path < end && *path ==
'.') path++;
4345 while (path < end && !isADS(*path)) {
4346 if (istrailinggarbage(*path)) {
4347 const char *last = path++;
4348 while (path < end && istrailinggarbage(*path)) path++;
4349 if (path >= end || isADS(*path))
return (
char *)last;
4351 else if (isdirsep(*path)) {
4352 const char *last = path++;
4353 while (path < end && isdirsep(*path)) path++;
4354 if (path >= end)
return (
char *)last;
4355 if (isADS(*path)) path++;
4358 Inc(path, end, mb_enc, enc);
4361 return (
char *)path;
4365#define BUFCHECK(cond) do {\
4368 do {buflen *= 2;} while (cond);\
4369 rb_str_resize(result, buflen);\
4370 buf = RSTRING_PTR(result);\
4372 pend = buf + buflen;\
4376#define BUFINIT(result, buf, p, pend) do {\
4377 if (!result) { result = rb_usascii_str_new(0, 1); } \
4378 p = buf = RSTRING_PTR(result); \
4379 buflen = RSTRING_LEN(result); \
4380 pend = p + buflen; \
4384# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4386# define SKIPPATHSEP(p) 1
4389#define BUFCOPY(srcptr, srclen) do { \
4390 const int skip = SKIPPATHSEP(p); \
4391 rb_str_set_len(result, p-buf+skip); \
4392 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4394 memcpy(p, (srcptr), (srclen)); \
4398#define WITH_ROOTDIFF(stmt) do { \
4399 long rootdiff = root - buf; \
4401 root = buf + rootdiff; \
4405copy_home_path(
VALUE result,
const char *dir)
4411 dirlen = strlen(dir);
4412 rb_str_resize(result, dirlen);
4413 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4414 encidx = rb_filesystem_encindex();
4415 rb_enc_associate_index(result, encidx);
4416#if defined FILE_ALT_SEPARATOR
4418 bool mb_enc = enc_mbclen_needed(enc);
4419 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4420 if (*p == FILE_ALT_SEPARATOR) {
4432 VALUE dirname = rb_getpwdirnam_for_login(user);
4433 if (dirname ==
Qnil) {
4434 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4436 const char *dir = RSTRING_PTR(dirname);
4438 extern char *getlogin(
void);
4439 const char *pwPtr = 0;
4441 # define endpwent() ((void)0)
4442 const char *dir, *username = RSTRING_PTR(user);
4453 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4454 dir = pwPtr = getenv(
"HOME");
4456 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4459 copy_home_path(result, dir);
4465rb_default_home_dir(
VALUE result)
4467 const char *dir = getenv(
"HOME");
4469#if defined HAVE_PWD_H
4483 VALUE login_name = rb_getlogin();
4485# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4490 if (
NIL_P(login_name)) {
4491 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4495 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4496 if (
NIL_P(pw_dir)) {
4497 pw_dir = rb_getpwdiruid();
4498 if (
NIL_P(pw_dir)) {
4499 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4504 copy_home_path(result, RSTRING_PTR(pw_dir));
4505 rb_str_resize(pw_dir, 0);
4510 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4512 return copy_home_path(result, dir);
4518#if NORMALIZE_UTF8PATH
4519 VALUE path = rb_str_normalize_ospath(ptr,
len);
4520 rb_enc_associate(path, fsenc);
4523 return rb_enc_str_new(ptr,
len, fsenc);
4530 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4532 rb_enc_associate(dirname, fsenc);
4536 size_t dirlen = RSTRING_LEN(dirname);
4539 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4540 if (!rb_enc_compatible(fname, dirname)) {
4543 rb_enc_check(fname, dirname);
4544 rb_bug(
"unreachable");
4546 rb_encoding *direnc = fs_enc_check(fname, dirname);
4547 if (direnc != fsenc) {
4554 do {buflen *= 2;}
while (dirlen > buflen);
4555 rb_str_resize(result, buflen);
4556 buf = RSTRING_PTR(result);
4557 memcpy(buf, cwdp, dirlen);
4558 rb_enc_associate(result, *enc);
4559 return buf + dirlen;
4563rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4565 const char *s, *b, *fend;
4566 char *buf, *p, *pend, *root;
4567 size_t buflen, bdiff;
4568 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4571 fend = s + RSTRING_LEN(fname);
4572 enc = rb_str_enc_get(fname);
4573 bool mb_enc = enc_mbclen_needed(enc);
4574 if (!mb_enc &&
RTEST(dname)) {
4575 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4578 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4579 BUFINIT(result, buf, p, pend);
4582 if (s + 1 == fend || isdirsep(s[1])) {
4586 if (++s < fend) ++s;
4587 rb_default_home_dir(result);
4590 s = nextdirsep(b = s, fend, enc);
4593 BUFCHECK(bdiff + userlen >= buflen);
4594 memcpy(p, b, userlen);
4597 rb_enc_associate(result, enc);
4598 rb_home_dir_of(result, result);
4602 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4604 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4605 (
int)userlen, b, fname);
4608 rb_raise(rb_eArgError,
"non-absolute home");
4611 BUFINIT(result, buf, p, pend);
4614#ifdef DOSISH_DRIVE_LETTER
4616 else if (s + 1 < fend && has_drive_letter(s)) {
4617 BUFINIT(result, buf, p, pend);
4619 if (s + 2 < fend && isdirsep(s[2])) {
4622 BUFCHECK(bdiff + 2 >= buflen);
4626 rb_enc_copy(result, fname);
4631 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4632 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4633 BUFINIT(result, buf, p, pend);
4640 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4641 BUFINIT(result, buf, p, pend);
4645 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4648 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4653 else if (s == fend || !rb_is_absolute_path(s)) {
4655 if (!
NIL_P(dname)) {
4657 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4661 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4663 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
4668 result = resized_result;
4672 rb_enc_associate(result, fs_enc_check(result, fname));
4673 BUFINIT(result, buf, p, pend);
4677 VALUE cwd = rb_dir_getwd_ospath();
4681 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
4682 BUFINIT(result, buf, p, pend);
4685#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4686 if (s < fend && isdirsep(*s)) {
4689 p = skipprefix(buf, p, mb_enc, enc);
4693 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4696 BUFINIT(result, buf, p, pend);
4700 do s++;
while (s < fend && isdirsep(*s));
4703 BUFCHECK(bdiff >= buflen);
4704 memset(buf,
'/',
len);
4706 rb_enc_associate(result, fs_enc_check(result, fname));
4708 if (p > buf && p[-1] ==
'/')
4712 BUFCHECK(bdiff + 1 >= buflen);
4717 BUFCHECK(bdiff + 1 >= buflen);
4719 root = skipprefix(buf, p+1, mb_enc, enc);
4732 if (s+1 == fend || isdirsep(*(s+1))) {
4736 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
4746 do ++s;
while (s < fend && istrailinggarbage(*s));
4751#if defined FILE_ALT_SEPARATOR
4752 case FILE_ALT_SEPARATOR:
4766 while (s < fend && istrailinggarbage(*s)) s++;
4776#if defined FILE_ALT_SEPARATOR
4777 case FILE_ALT_SEPARATOR:
4780 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4788 int n = ignored_char_p(s, fend, enc);
4791 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4799 Inc(s, fend, mb_enc, enc);
4807 static const char prime[] =
":$DATA";
4808 enum {prime_len =
sizeof(prime) -1};
4812 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) {
4815 if (isADS(*(s - (prime_len+1)))) {
4818 else if (memchr(b,
':', s - prime_len - b)) {
4827 if (p == skiproot(buf, p + !!*p) - 1) p++;
4831 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s,
"*?")) {
4836 WIN32_FIND_DATAW wfd;
4839#ifdef HAVE_CYGWIN_CONV_PATH
4840 char *w32buf = NULL;
4841 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
4843 char w32buf[MAXPATHLEN];
4847 int lnk_added = 0, is_symlink = 0;
4851 if (lstat_without_gvl(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
4857 path = *buf ? buf :
"/";
4858#ifdef HAVE_CYGWIN_CONV_PATH
4859 bufsize = cygwin_conv_path(flags, path, NULL, 0);
4862 if (lnk_added) bufsize += 4;
4864 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
4869 bufsize = MAXPATHLEN;
4870 if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
4874 if (is_symlink && b == w32buf) {
4876 strlcat(w32buf, p, bufsize);
4878 strlcat(w32buf,
".lnk", bufsize);
4889 if (encidx != ENCINDEX_UTF_8 && !is_ascii_string(result)) {
4890 tmp = rb_str_encode_ospath(result);
4892 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0);
4894 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr,
len);
4896 h = FindFirstFileW(wstr, &wfd);
4898 if (h != INVALID_HANDLE_VALUE) {
4901 len = lstrlenW(wfd.cFileName);
4903 if (lnk_added &&
len > 4 &&
4904 wcscasecmp(wfd.cFileName +
len - 4, L
".lnk") == 0) {
4905 wfd.cFileName[
len -= 4] = L
'\0';
4912 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL);
4913 if (tmp == result) {
4914 BUFCHECK(bdiff +
len >= buflen);
4915 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p,
len + 1, NULL, NULL);
4919 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, RSTRING_PTR(tmp),
len + 1, NULL, NULL);
4920 rb_str_cat_conv_enc_opts(result, bdiff, RSTRING_PTR(tmp),
len,
4921 rb_utf8_encoding(), 0,
Qnil);
4922 BUFINIT(result, buf, p, pend);
4923 rb_str_resize(tmp, 0);
4936 rb_enc_check(fname, result);
4943str_shrink(
VALUE str)
4945 rb_str_resize(str, RSTRING_LEN(str));
4949#define expand_path(fname, dname, abs_mode, long_name, result) \
4950 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
4952#define check_expand_path_args(fname, dname) \
4953 (((fname) = rb_get_path(fname)), \
4954 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
4957file_expand_path_1(
VALUE fname,
long extra_capa)
4960 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
4966 check_expand_path_args(fname, dname);
4967 return expand_path(fname, dname, 0, 1,
Qfalse);
4971rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
4973 return expand_path(fname, dname, 0, 0,
Qfalse);
4977rb_file_s_expand_path(
int argc,
const VALUE *argv)
4980 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5026 return rb_file_s_expand_path(c, v);
5032 check_expand_path_args(fname, dname);
5033 return expand_path(fname, dname, 1, 1,
Qfalse);
5037rb_file_s_absolute_path(
int argc,
const VALUE *argv)
5040 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
5083 return rb_file_s_absolute_path(c, v);
5111 VALUE path = rb_get_path(fname);
5113 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5117enum rb_realpath_mode {
5121 RB_REALPATH_MODE_MAX
5125realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5126 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5128 const char *pend = unresolved + strlen(unresolved);
5132 while (unresolved < pend) {
5133 const char *testname = unresolved;
5134 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5135 long testnamelen = unresolved_firstsep - unresolved;
5136 const char *unresolved_nextname = unresolved_firstsep;
5137 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5138 unresolved_nextname++;
5139 unresolved = unresolved_nextname;
5140 if (testnamelen == 1 && testname[0] ==
'.') {
5142 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5143 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5144 bool mb_enc = enc_mbclen_needed(enc);
5145 const char *resolved_str = RSTRING_PTR(*resolvedp);
5146 const char *resolved_names = resolved_str + *prefixlenp;
5147 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5148 long len = lastsep ? lastsep - resolved_names : 0;
5149 rb_str_resize(*resolvedp, *prefixlenp +
len);
5155 if (*prefixlenp < RSTRING_LEN(testpath))
5157#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5158 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5159 const char *prefix = RSTRING_PTR(testpath);
5160 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5165 checkval = rb_hash_aref(loopcheck, testpath);
5166 if (!
NIL_P(checkval)) {
5167 if (checkval ==
ID2SYM(resolving)) {
5168 if (mode == RB_REALPATH_CHECK) {
5172 rb_syserr_fail_path(ELOOP, testpath);
5181 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5184 if (e == ENOENT && !
NIL_P(fallback)) {
5185 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5190 if (mode == RB_REALPATH_CHECK)
return -1;
5192 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5193 rb_syserr_fail_path(e, testpath);
5194 *resolvedp = testpath;
5198 rb_syserr_fail_path(e, testpath);
5202 if (S_ISLNK(sbuf.st_mode)) {
5205 const char *link_prefix, *link_names;
5206 long link_prefixlen;
5207 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5208 link = rb_readlink(testpath, enc);
5209 link_prefix = RSTRING_PTR(link);
5210 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5211 link_prefixlen = link_names - link_prefix;
5212 if (link_prefixlen > 0) {
5216 tmpenc = fs_enc_check(*resolvedp, link);
5219 *prefixlenp = link_prefixlen;
5221 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5222 loopcheck, mode, !*unresolved_firstsep))
5231 rb_hash_aset(loopcheck, s, s);
5232 *resolvedp = testpath;
5241rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5245 VALUE unresolved_path;
5250 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5251 char *ptr, *prefixptr = NULL, *pend;
5256 if (!
NIL_P(basedir)) {
5261 enc = rb_enc_get(unresolved_path);
5262 unresolved_path = TO_OSPATH(unresolved_path);
5264 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5265 if (ptr != path_names) {
5266 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5270 if (!
NIL_P(basedir)) {
5272 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5273 if (ptr != basedir_names) {
5279 curdir = rb_dir_getwd_ospath();
5281 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5286 pend = prefixptr + prefixlen;
5287 bool mb_enc = enc_mbclen_needed(enc);
5288 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5290 prefixlen = ++ptr - prefixptr;
5293#ifdef FILE_ALT_SEPARATOR
5294 while (prefixptr < ptr) {
5295 if (*prefixptr == FILE_ALT_SEPARATOR) {
5298 Inc(prefixptr, pend, mb_enc, enc);
5302 switch (rb_enc_to_index(enc)) {
5303 case ENCINDEX_ASCII_8BIT:
5304 case ENCINDEX_US_ASCII:
5305 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5308 loopcheck = rb_hash_new();
5310 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5313 if (basedir_names) {
5314 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5317 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5320 if (origenc && origenc != rb_enc_get(resolved)) {
5322 rb_enc_associate(resolved, origenc);
5334static VALUE rb_file_join(
long argc,
VALUE *args);
5336#ifndef HAVE_REALPATH
5338rb_check_realpath_emulate_try(
VALUE arg)
5341 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5345rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5349#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5350 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5352# include <sys/syslimits.h>
5353# define NEEDS_REALPATH_BUFFER 1
5357rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5360 VALUE unresolved_path;
5361 char *resolved_ptr = NULL;
5363# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5364 char resolved_buffer[PATH_MAX];
5366 char *
const resolved_buffer = NULL;
5369 if (mode == RB_REALPATH_DIR) {
5370 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5374 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5375 VALUE paths[2] = {basedir, unresolved_path};
5376 unresolved_path = rb_file_join(2, paths);
5378 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5380 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5389 if (
errno == ENOTSUP ||
5391 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5392 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5395 if (mode == RB_REALPATH_CHECK) {
5398 rb_sys_fail_path(unresolved_path);
5400 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5401# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5405# if !defined(__linux__) && !defined(__APPLE__)
5409 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5410 if (mode == RB_REALPATH_CHECK) {
5413 rb_sys_fail_path(unresolved_path);
5417 if (origenc && origenc != rb_enc_get(resolved)) {
5421 rb_enc_associate(resolved, origenc);
5424 if (is_broken_string(resolved)) {
5425 rb_enc_associate(resolved, rb_filesystem_encoding());
5426 if (is_broken_string(resolved)) {
5427 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5434 if (mode == RB_REALPATH_CHECK) {
5438 arg[2] = (
VALUE)origenc;
5441 rb_check_realpath_emulate_rescue,
Qnil);
5444 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5450rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5452 const enum rb_realpath_mode mode =
5453 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5454 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5460 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5477rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5480 VALUE path = argv[0];
5482 return rb_realpath_internal(basedir, path, 1);
5498rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5501 VALUE path = argv[0];
5503 return rb_realpath_internal(basedir, path, 0);
5507rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5511 const char *s, *last;
5513 if (!e || !l2)
return 0;
5515 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5516 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5517 if (c ==
'.')
return l0;
5522 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5527 if (l1 < l2)
return l1;
5530 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5531#if CASEFOLD_FILESYSTEM
5532#define fncomp strncasecmp
5534#define fncomp strncmp
5536 if (fncomp(s, e, l2) == 0) {
5542static inline const char *
5543enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5545 const char *p, *q, *e, *end;
5548 long len = (alllen ? (size_t)*alllen : strlen(name));
5555 name = skipprefix(name, end, mb_enc, enc);
5556#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5557 const char *root = name;
5560 while (name < end && isdirsep(*name)) {
5567#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5571#ifdef DOSISH_DRIVE_LETTER
5572 else if (*p ==
':') {
5585 p = strrdirsep(name, end, mb_enc, enc);
5590 while (isdirsep(*p)) {
5595 n = ntfs_tail(p, end, enc) - p;
5597 n = chompdirsep(p, end, mb_enc, enc) - p;
5599 for (q = p; q - p < n && *q ==
'.'; q++);
5600 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5601 if (*q ==
'.') e = q;
5621ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5623 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5667 const char *name, *p, *fp = 0;
5673 CheckPath(fname, name);
5677 check_path_encoding(fext);
5679 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5680 enc = rb_str_enc_get(fname);
5683 n = RSTRING_LEN(fname);
5684 if (n <= 0 || !*name) {
5685 return rb_enc_str_new(0, 0, enc);
5688 bool mb_enc = enc_mbclen_needed(enc);
5689 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5695 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5700 if (f == RSTRING_LEN(fname)) {
5705 return rb_enc_str_new(p, f, enc);
5708static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5731rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5737 return rb_file_dirname_n(argv[0], n);
5743 return rb_file_dirname_n(fname, 1);
5747rb_file_dirname_n(
VALUE fname,
int n)
5749 const char *name, *root, *p, *end;
5752 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5753 CheckPath(fname, name);
5754 end = name + RSTRING_LEN(fname);
5756 bool mb_enc = !rb_str_enc_fastpath(fname);
5759 root = skiproot(name, end);
5761 if (root > name + 1 && isdirsep(*name))
5762 root = skipprefix(name = root - 2, end, mb_enc, enc);
5764 if (root > name + 1)
5767 if (n > (end - root + 1) / 2) {
5773 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5782 return rb_enc_str_new(
".", 1, enc);
5784#ifdef DOSISH_DRIVE_LETTER
5785 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5786 const char *top = skiproot(name + 2, end);
5787 dirname = rb_enc_str_new(name, 3, enc);
5792 dirname = rb_enc_str_new(name, p - name, enc);
5793#ifdef DOSISH_DRIVE_LETTER
5794 if (root == name + 2 && p == root && name[1] ==
':')
5800static inline const char *
5801enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5803 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5805 p = strrdirsep(name, end, mb_enc, enc);
5809 do name = ++p;
while (isdirsep(*p));
5812 while (*p && *p ==
'.') p++;
5814 if (*p ==
'.' || istrailinggarbage(*p)) {
5816 const char *last = p++, *dot = last;
5817 while (istrailinggarbage(*p)) {
5818 if (*p ==
'.') dot = p;
5821 if (!*p || isADS(*p)) {
5825 if (*last ==
'.' || dot > last) e = dot;
5832 else if (isADS(*p)) {
5836 else if (isdirsep(*p))
5838 Inc(p, end, mb_enc, enc);
5843 if (!e || e == name)
5868 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
5915 CheckPath(fname, name);
5916 long len = RSTRING_LEN(fname);
5919 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
5922 bool mb_enc = !rb_str_enc_fastpath(fname);
5925 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
5926 return rb_enc_str_new(ext,
len, enc);
5959 return rb_get_path(fname);
5983file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
5985 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
5986 return rb_file_join_ary(arg);
5990rb_file_join_ary(
VALUE ary)
5994 const char *name, *tail;
6004 check_path_encoding(tmp);
6005 len += RSTRING_LEN(tmp);
6013 RBASIC_CLEAR_CLASS(result);
6016 switch (OBJ_BUILTIN_TYPE(tmp)) {
6018 if (!checked) check_path_encoding(tmp);
6023 rb_raise(rb_eArgError,
"recursive array");
6035 rb_enc_copy(result, tmp);
6038 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
6039 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
6042 else if (tail == name +
len) {
6046 enc = fs_enc_check(result, tmp);
6048 rb_enc_associate(result, enc);
6056rb_file_join_fastpath(
long argc,
VALUE *args)
6061 for (i = 0; i < argc; i++) {
6062 VALUE tmp = args[i];
6064 size += RSTRING_LEN(tmp);
6077 const char *name = RSTRING_PTR(result);
6078 for (i = 1; i < argc; i++) {
6079 VALUE tmp = args[i];
6080 long len = RSTRING_LEN(result);
6086 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6089 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6094 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6101 rb_enc_associate(result, new_enc);
6102 encidx = rb_enc_to_index(new_enc);
6108 rb_str_null_check(result);
6113rb_file_join(
long argc,
VALUE *args)
6115 if (RB_UNLIKELY(argc == 0)) {
6119 VALUE result = rb_file_join_fastpath(argc, args);
6120 if (RB_LIKELY(result)) {
6140rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6142 return rb_file_join(argc, argv);
6145#if defined(HAVE_TRUNCATE)
6146struct truncate_arg {
6152nogvl_truncate(
void *ptr)
6154 struct truncate_arg *ta = ptr;
6155 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6182 struct truncate_arg ta;
6187 path = rb_str_encode_ospath(path);
6190 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6192 rb_sys_fail_path(path);
6196#define rb_file_s_truncate rb_f_notimplement
6199#if defined(HAVE_FTRUNCATE)
6200struct ftruncate_arg {
6206nogvl_ftruncate(
void *ptr)
6208 struct ftruncate_arg *fa = ptr;
6210 return (
VALUE)ftruncate(fa->fd, fa->pos);
6231 struct ftruncate_arg fa;
6238 rb_io_flush_raw(obj, 0);
6240 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6241 rb_sys_fail_path(fptr->
pathv);
6246#define rb_file_truncate rb_f_notimplement
6263#include <winerror.h>
6267rb_thread_flock(
void *data)
6270 int old_errno =
errno;
6272 int *op = data, ret = flock(op[0], op[1]);
6275 if (GetLastError() == ERROR_NOT_LOCKED) {
6333 op[1] = op1 =
NUM2INT(operation);
6338 rb_io_flush_raw(obj, 0);
6340 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6345#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6348 if (op1 & LOCK_NB)
return Qfalse;
6351 time.tv_usec = 100 * 1000;
6357#if defined(ERESTART)
6363 rb_syserr_fail_path(e, fptr->
pathv);
6370test_check(
int n,
int argc,
VALUE *argv)
6376 for (i=1; i<n; i++) {
6383#define CHECK(n) test_check((n), argc, argv)
6475 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6479 return rb_file_blockdev_p(0, argv[1]);
6482 return rb_file_chardev_p(0, argv[1]);
6485 return rb_file_directory_p(0, argv[1]);
6488 return rb_file_exist_p(0, argv[1]);
6491 return rb_file_file_p(0, argv[1]);
6494 return rb_file_sgid_p(0, argv[1]);
6497 return rb_file_grpowned_p(0, argv[1]);
6500 return rb_file_sticky_p(0, argv[1]);
6503 return rb_file_symlink_p(0, argv[1]);
6506 return rb_file_owned_p(0, argv[1]);
6509 return rb_file_rowned_p(0, argv[1]);
6512 return rb_file_pipe_p(0, argv[1]);
6515 return rb_file_readable_p(0, argv[1]);
6518 return rb_file_readable_real_p(0, argv[1]);
6521 return rb_file_size_p(0, argv[1]);
6524 return rb_file_socket_p(0, argv[1]);
6527 return rb_file_suid_p(0, argv[1]);
6530 return rb_file_writable_p(0, argv[1]);
6533 return rb_file_writable_real_p(0, argv[1]);
6536 return rb_file_executable_p(0, argv[1]);
6539 return rb_file_executable_real_p(0, argv[1]);
6542 return rb_file_zero_p(0, argv[1]);
6546 if (strchr(
"MAC", cmd)) {
6548 VALUE fname = argv[1];
6551 if (
rb_stat(fname, &st) == -1) {
6554 rb_syserr_fail_path(e, fname);
6559 return stat_atime(&st);
6561 return stat_mtime(&st);
6563 return stat_ctime(&st);
6569 return rb_file_identical_p(0, argv[1], argv[2]);
6572 if (strchr(
"=<>", cmd)) {
6573 struct stat st1, st2;
6580 t1 = stat_mtimespec(&st1);
6581 t2 = stat_mtimespec(&st2);
6585 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6589 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6590 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6594 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6595 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6602 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6605 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6659rb_stat_s_alloc(
VALUE klass)
6680 fname = rb_str_encode_ospath(fname);
6682 rb_sys_fail_path(fname);
6689 rb_st->initialized =
true;
6706 *copy_rb_st = *orig_rb_st;
6748rb_stat_ftype(
VALUE obj)
6750 return rb_file_ftype(get_stat(obj)->ST_(mode));
6767 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6783 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
6814 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
6835 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
6858 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
6879 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
6897rb_stat_owned(
VALUE obj)
6899 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
6904rb_stat_rowned(
VALUE obj)
6906 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
6927rb_stat_grpowned(
VALUE obj)
6930 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
6949 rb_io_stat_data *st = get_stat(obj);
6952 if (geteuid() == 0)
return Qtrue;
6955 if (rb_stat_owned(obj))
6956 return RBOOL(st->ST_(mode) & S_IRUSR);
6959 if (rb_stat_grpowned(obj))
6960 return RBOOL(st->ST_(mode) & S_IRGRP);
6963 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6982 rb_io_stat_data *st = get_stat(obj);
6985 if (getuid() == 0)
return Qtrue;
6988 if (rb_stat_rowned(obj))
6989 return RBOOL(st->ST_(mode) & S_IRUSR);
6992 if (rb_group_member(get_stat(obj)->ST_(gid)))
6993 return RBOOL(st->ST_(mode) & S_IRGRP);
6996 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
7015rb_stat_wr(
VALUE obj)
7018 rb_io_stat_data *st = get_stat(obj);
7019 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
7020 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7040 rb_io_stat_data *st = get_stat(obj);
7043 if (geteuid() == 0)
return Qtrue;
7046 if (rb_stat_owned(obj))
7047 return RBOOL(st->ST_(mode) & S_IWUSR);
7050 if (rb_stat_grpowned(obj))
7051 return RBOOL(st->ST_(mode) & S_IWGRP);
7054 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7073 rb_io_stat_data *st = get_stat(obj);
7076 if (getuid() == 0)
return Qtrue;
7079 if (rb_stat_rowned(obj))
7080 return RBOOL(st->ST_(mode) & S_IWUSR);
7083 if (rb_group_member(get_stat(obj)->ST_(gid)))
7084 return RBOOL(st->ST_(mode) & S_IWGRP);
7087 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7106rb_stat_ww(
VALUE obj)
7109 rb_io_stat_data *st = get_stat(obj);
7110 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7111 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7153 rb_io_stat_data *st = get_stat(obj);
7156 if (geteuid() == 0) {
7157 return RBOOL(st->ST_(mode) & S_IXUGO);
7161 if (rb_stat_owned(obj))
7162 return RBOOL(st->ST_(mode) & S_IXUSR);
7165 if (rb_stat_grpowned(obj))
7166 return RBOOL(st->ST_(mode) & S_IXGRP);
7169 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7185 rb_io_stat_data *st = get_stat(obj);
7188 if (getuid() == 0) {
7189 return RBOOL(st->ST_(mode) & S_IXUGO);
7193 if (rb_stat_rowned(obj))
7194 return RBOOL(st->ST_(mode) & S_IXUSR);
7197 if (rb_group_member(get_stat(obj)->ST_(gid)))
7198 return RBOOL(st->ST_(mode) & S_IXGRP);
7201 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7224 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7242 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7261 rb_off_t size = get_stat(obj)->ST_(size);
7263 if (size == 0)
return Qnil;
7279rb_stat_suid(
VALUE obj)
7282 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7300rb_stat_sgid(
VALUE obj)
7303 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7321rb_stat_sticky(
VALUE obj)
7324 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7329#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7330#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7341nogvl_mkfifo(
void *ptr)
7343 struct mkfifo_arg *ma = ptr;
7345 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7362 struct mkfifo_arg ma;
7371 path = rb_str_encode_ospath(path);
7372 ma.path = RSTRING_PTR(path);
7373 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7374 rb_sys_fail_path(path);
7379#define rb_file_s_mkfifo rb_f_notimplement
7382static VALUE rb_mFConst;
7385rb_file_const(
const char *name,
VALUE value)
7387 rb_define_const(rb_mFConst, name, value);
7391rb_is_absolute_path(
const char *path)
7393#ifdef DOSISH_DRIVE_LETTER
7394 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7397 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7400 if (path[0] ==
'/')
return 1;
7406ruby_is_fd_loadable(
int fd)
7413 if (fstat(fd, &st) < 0)
7416 if (S_ISREG(st.st_mode))
7419 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7422 if (S_ISDIR(st.st_mode))
7433rb_file_load_ok(
const char *path)
7440 int mode = (O_RDONLY |
7441#if defined O_NONBLOCK
7443#elif defined O_NDELAY
7449 if (!rb_gc_for_fd(
errno))
return 0;
7451 if (fd < 0)
return 0;
7454 ret = ruby_is_fd_loadable(fd);
7461is_explicit_relative(
const char *path)
7463 if (*path++ !=
'.')
return 0;
7464 if (*path ==
'.') path++;
7465 return isdirsep(*path);
7471 int encidx = rb_enc_get_index(orig);
7472 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7473 encidx = rb_filesystem_encindex();
7474 rb_enc_associate_index(path, encidx);
7482nav_component_p(
const char *s,
const char *send)
7484 if ((send - s) >= 2 && s[0] ==
'.') {
7485 return s[1] ==
'.' || isdirsep(s[1]);
7491fname_need_expansion_p(
VALUE fname)
7493 const char *s = RSTRING_PTR(fname);
7494 const long len = RSTRING_LEN(fname);
7495 const char *send = s +
len;
7497 if (nav_component_p(s, send)) {
7502 bool mbenc = enc_mbclen_needed(enc);
7504 s = enc_path_next(s, send, mbenc, enc);
7506 if (nav_component_p(s, send)) {
7510 s = enc_path_next(s, send, mbenc, enc);
7516expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7518 long dname_len = RSTRING_LEN(dname);
7519 const char *dname_ptr = RSTRING_PTR(dname);
7523 if (need_expansion || dname_ptr[0] ==
'~') {
7524 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7529 if (!isdirsep(dname_ptr[dname_len - 1])) {
7538rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
7541 VALUE fname = *filep;
7545 if (!ext[0])
return 0;
7548 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7549 f = RSTRING_PTR(fname);
7554 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7555 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7556 fnlen = RSTRING_LEN(fname);
7557 for (i=0; ext[i]; i++) {
7559 if (rb_file_load_ok(RSTRING_PTR(fname))) {
7560 *filep = copy_path_class(fname, *filep);
7568 long expanded_load_path_maxlen;
7569 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7570 if (!load_path)
return 0;
7573 RBASIC_CLEAR_CLASS(fname);
7574 fnlen = RSTRING_LEN(fname);
7575 bool need_expansion = fname_need_expansion_p(fname);
7578 rb_enc_associate_index(tmp, rb_usascii_encindex());
7580 for (j=0; ext[j]; j++) {
7582 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
7584 if (!RSTRING_LEN(dname))
continue;
7585 expand_feature(fname, dname, tmp, need_expansion);
7587 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7588 *filep = copy_path_class(tmp, *filep);
7594 rb_str_resize(tmp, 0);
7607 path = copy_path_class(file_expand_path_1(path, 0), path);
7608 f = RSTRING_PTR(path);
7612 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7613 if (!rb_file_load_ok(f))
return 0;
7615 path = copy_path_class(file_expand_path_1(path, 0), path);
7619 long expanded_load_path_maxlen;
7620 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7623 bool need_expansion = fname_need_expansion_p(path);
7625 rb_enc_associate_index(tmp, rb_usascii_encindex());
7626 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
7628 if (!RSTRING_LEN(dname))
continue;
7629 expand_feature(path, dname, tmp, need_expansion);
7631 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7632 return copy_path_class(tmp, path);
7635 rb_str_resize(tmp, 0);
7643#define define_filetest_function(name, func, argc) do { \
7644 rb_define_module_function(rb_mFileTest, name, func, argc); \
7645 rb_define_singleton_method(rb_cFile, name, func, argc); \
7648const char ruby_null_device[] =
7651#elif defined AMIGA || defined __amigaos__
8455#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8456 rb_CFString_class_initialize_before_fork();
8464 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8465 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8466 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8467 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8468 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8469 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8470 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8471 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8472 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8473 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8474 define_filetest_function(
"file?", rb_file_file_p, 1);
8475 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8476 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8477 define_filetest_function(
"size?", rb_file_size_p, 1);
8478 define_filetest_function(
"size", rb_file_s_size, 1);
8479 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8480 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8482 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8483 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8484 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8486 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8487 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8489 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8490 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8491 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8493 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8531 separator = rb_fstring_lit(
"/");
8533 rb_define_const(
rb_cFile,
"Separator", separator);
8535 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
8884 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
8887 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
8889 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
8891 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
8893 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
8895 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
8897 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
8898#if defined(O_NDELAY) || defined(O_NONBLOCK)
8900# define O_NONBLOCK O_NDELAY
8903 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
8906 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
8909 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
8915 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
8916#ifndef O_SHARE_DELETE
8917# define O_SHARE_DELETE 0
8920 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
8923 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
8927 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
8931 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
8935 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
8939 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
8943 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
8947 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
8951 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
8953 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
8955 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
8957 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
8960 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 STRCASECMP
Old name of st_locale_insensitive_strcasecmp.
#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)
Just calls rb_obj_freeze_inline() inside.
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 ALLOCA_N(type, n)
#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.