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);
1523 fname = rb_str_encode_ospath(fname);
1524 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1525 rb_sys_fail_path(fname);
1527 return rb_statx_new(&st);
1546rb_io_stat(
VALUE obj)
1552 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1553 rb_sys_fail_path(fptr->
pathv);
1555 return rb_statx_new(&st);
1560no_gvl_lstat(
void *ptr)
1563 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1567lstat_without_gvl(
const char *path,
struct stat *st)
1571 data.file.path = path;
1574 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1611 fname = rb_str_encode_ospath(fname);
1612 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1613 rb_sys_fail_path(fname);
1615 return rb_statx_new(&st);
1617 return rb_file_s_stat(klass, fname);
1636rb_file_lstat(
VALUE obj)
1645 path = rb_str_encode_ospath(fptr->
pathv);
1646 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1647 rb_sys_fail_path(fptr->
pathv);
1649 return rb_statx_new(&st);
1651 return rb_io_stat(obj);
1656rb_group_member(GETGROUPS_T gid)
1658#if defined(_WIN32) || !defined(HAVE_GETGROUPS)
1667 if (getgid() == gid || getegid() == gid)
1670 groups = getgroups(0, NULL);
1671 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1672 anum = getgroups(groups, gary);
1673 while (--anum >= 0) {
1674 if (gary[anum] == gid) {
1687# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1690#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1691#define USE_GETEUID 1
1696eaccess(
const char *path,
int mode)
1705 if (getuid() == euid && getgid() == getegid())
1706 return access(path, mode);
1708 if (STAT(path, &st) < 0)
1718 if (st.st_mode & S_IXUGO)
1724 if (st.st_uid == euid)
1726 else if (rb_group_member(st.st_gid))
1729 if ((
int)(st.st_mode & mode) == mode)
return 0;
1733 return access(path, mode);
1744nogvl_eaccess(
void *ptr)
1748 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1752rb_eaccess(
VALUE fname,
int mode)
1757 fname = rb_str_encode_ospath(fname);
1761 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1765nogvl_access(
void *ptr)
1769 return (
void *)(
VALUE)access(aa->path, aa->mode);
1773rb_access(
VALUE fname,
int mode)
1778 fname = rb_str_encode_ospath(fname);
1782 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1825# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1831 if (S_ISDIR(st.st_mode))
return Qtrue;
1852# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1858 if (S_ISFIFO(st.st_mode))
return Qtrue;
1891# define S_ISLNK(m) _S_ISLNK(m)
1894# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1897# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1907 fname = rb_str_encode_ospath(fname);
1909 if (S_ISLNK(st.st_mode))
return Qtrue;
1932# define S_ISSOCK(m) _S_ISSOCK(m)
1935# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1938# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1948 if (S_ISSOCK(st.st_mode))
return Qtrue;
1976# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1978# define S_ISBLK(m) (0)
1986 if (S_ISBLK(st.st_mode))
return Qtrue;
2013# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
2019 if (S_ISCHR(st.st_mode))
return Qtrue;
2067 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2082rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2084 return RBOOL(rb_access(fname, R_OK) >= 0);
2088# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2092# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2112rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2118 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2119 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2139 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2154rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2156 return RBOOL(rb_access(fname, W_OK) >= 0);
2176rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2182 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2183 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2226 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2245rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2247 return RBOOL(rb_access(fname, X_OK) >= 0);
2251# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2283 return RBOOL(S_ISREG(st.st_mode));
2329 return RBOOL(st.st_size == 0);
2348 if (st.st_size == 0)
return Qnil;
2369 return RBOOL(st.st_uid == geteuid());
2378 return RBOOL(st.st_uid == getuid());
2410 if (rb_group_member(st.st_gid))
return Qtrue;
2415#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2417check3rdbyte(
VALUE fname,
int mode)
2422 return RBOOL(st.st_mode & mode);
2439 return check3rdbyte(fname, S_ISUID);
2458 return check3rdbyte(fname, S_ISGID);
2477 return check3rdbyte(fname, S_ISVTX);
2514 struct stat st1, st2;
2518 if (st1.st_dev != st2.st_dev)
return Qfalse;
2519 if (st1.st_ino != st2.st_ino)
return Qfalse;
2523 return rb_w32_file_identical_p(fname1, fname2);
2541 if (
rb_stat(fname, &st) < 0) {
2544 rb_syserr_fail_path(e, fname);
2550rb_file_ftype(mode_t mode)
2554 if (S_ISREG(mode)) {
2557 else if (S_ISDIR(mode)) {
2560 else if (S_ISCHR(mode)) {
2561 t =
"characterSpecial";
2564 else if (S_ISBLK(mode)) {
2569 else if (S_ISFIFO(mode)) {
2574 else if (S_ISLNK(mode)) {
2579 else if (S_ISSOCK(mode)) {
2587 return rb_fstring_cstr(t);
2629 fname = rb_str_encode_ospath(fname);
2631 rb_sys_fail_path(fname);
2634 return rb_file_ftype(st.st_mode);
2682 if (
rb_stat(fname, &st) < 0) {
2685 rb_syserr_fail_path(e, fname);
2687 return stat_time(stat_atimespec(&st));
2714rb_file_atime(
VALUE obj)
2720 if (fstat(fptr->
fd, &st) == -1) {
2721 rb_sys_fail_path(fptr->
pathv);
2723 return stat_time(stat_atimespec(&st));
2743 if (
rb_stat(fname, &st) < 0) {
2746 rb_syserr_fail_path(e, fname);
2748 return stat_time(stat_mtimespec(&st));
2762rb_file_mtime(
VALUE obj)
2768 if (fstat(fptr->
fd, &st) == -1) {
2769 rb_sys_fail_path(fptr->
pathv);
2771 return stat_time(stat_mtimespec(&st));
2814 if (
rb_stat(fname, &st) < 0) {
2817 rb_syserr_fail_path(e, fname);
2819 return stat_time(stat_ctimespec(&st));
2836rb_file_ctime(
VALUE obj)
2842 if (fstat(fptr->
fd, &st) == -1) {
2843 rb_sys_fail_path(fptr->
pathv);
2845 return stat_time(stat_ctimespec(&st));
2848#if defined(HAVE_STAT_BIRTHTIME)
2879 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
2882 rb_syserr_fail_path(e, fname);
2884 return statx_birthtime(&st);
2887# define rb_file_s_birthtime rb_f_notimplement
2890#if defined(HAVE_STAT_BIRTHTIME)
2911rb_file_birthtime(
VALUE obj)
2917 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
2918 rb_sys_fail_path(fptr->
pathv);
2920 return statx_birthtime(&st);
2923# define rb_file_birthtime rb_f_notimplement
2935 rb_io_flush_raw(file, 0);
2938 if (fstat(fptr->
fd, &st) == -1) {
2939 rb_sys_fail_path(fptr->
pathv);
2960file_size(
VALUE self)
2962 return OFFT2NUM(rb_file_size(self));
2971nogvl_chmod(
void *ptr)
2974 int ret = chmod(data->path, data->mode);
2975 return (
void *)(
VALUE)ret;
2979rb_chmod(
const char *path, mode_t mode)
2985 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
2989chmod_internal(
const char *path,
void *mode)
2991 return chmod(path, *(mode_t *)mode);
3029 return apply2files(chmod_internal, argc, argv, &mode);
3033struct nogvl_fchmod_data {
3039io_blocking_fchmod(
void *ptr)
3041 struct nogvl_fchmod_data *data = ptr;
3042 int ret = fchmod(data->fd, data->mode);
3047rb_fchmod(
struct rb_io* io, mode_t mode)
3050 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
3051 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
3083#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3091 if (rb_fchmod(fptr, mode) == -1) {
3092 if (HAVE_FCHMOD ||
errno != ENOSYS)
3093 rb_sys_fail_path(fptr->
pathv);
3096 if (!HAVE_FCHMOD)
return INT2FIX(0);
3099#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
3101 path = rb_str_encode_ospath(fptr->
pathv);
3102 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
3103 rb_sys_fail_path(fptr->
pathv);
3109#if defined(HAVE_LCHMOD)
3111lchmod_internal(
const char *path,
void *mode)
3113 return lchmod(path, *(mode_t *)mode);
3150 return apply2files(lchmod_internal, argc, argv, &mode);
3153#define rb_file_s_lchmod rb_f_notimplement
3156static inline rb_uid_t
3160 return (rb_uid_t)-1;
3165static inline rb_gid_t
3169 return (rb_gid_t)-1;
3180chown_internal(
const char *path,
void *arg)
3183 return chown(path, args->owner, args->group);
3237 arg.owner = to_uid(*argv++);
3238 arg.group = to_gid(*argv++);
3240 return apply2files(chown_internal, argc, argv, &arg);
3252nogvl_chown(
void *ptr)
3255 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3259rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3262 .as = {.path = path},
3263 .new = {.owner = owner, .group = group},
3265 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3270nogvl_fchown(
void *ptr)
3273 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3277rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3282 .new = {.owner = owner, .group = group},
3284 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3318 path = rb_str_encode_ospath(fptr->
pathv);
3319 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3320 rb_sys_fail_path(fptr->
pathv);
3322 if (rb_fchown(fptr->
fd, o, g) == -1)
3323 rb_sys_fail_path(fptr->
pathv);
3329#if defined(HAVE_LCHOWN)
3331lchown_internal(
const char *path,
void *arg)
3334 return lchown(path, args->owner, args->group);
3390 arg.owner = to_uid(*argv++);
3391 arg.group = to_gid(*argv++);
3393 return apply2files(lchown_internal, argc, argv, &arg);
3396#define rb_file_s_lchown rb_f_notimplement
3406NORETURN(
static void utime_failed(
struct apply_arg *));
3412 VALUE path = aa->fn[aa->i].path;
3415 if (ua->tsp && e == EINVAL) {
3418 VALUE atime = ua->atime;
3419 VALUE mtime = ua->mtime;
3421 if (!
NIL_P(atime)) {
3424 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3427 if (
NIL_P(a)) e[0] = m;
3443 rb_syserr_fail_path(e, path);
3447#if defined(HAVE_UTIMES)
3449# if !defined(HAVE_UTIMENSAT)
3451# elif defined(__APPLE__) && \
3452 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3454# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3455typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3459static inline utimensat_func *
3466# define utimensat rb_utimensat()
3473utime_internal(
const char *path,
void *arg)
3476 const struct timespec *tsp = v->tsp;
3477 struct timeval tvbuf[2], *tvp = NULL;
3479#if defined(HAVE_UTIMENSAT)
3480# if defined(__APPLE__)
3481 const int try_utimensat = utimensat != NULL;
3482 const int try_utimensat_follow = utimensat != NULL;
3484# define TRY_UTIMENSAT 1
3485 static int try_utimensat = 1;
3486# ifdef AT_SYMLINK_NOFOLLOW
3487 static int try_utimensat_follow = 1;
3489 const int try_utimensat_follow = 0;
3494 if (v->follow ? try_utimensat_follow : try_utimensat) {
3495# ifdef AT_SYMLINK_NOFOLLOW
3497 flags = AT_SYMLINK_NOFOLLOW;
3501 int result = utimensat(AT_FDCWD, path, tsp, flags);
3502# ifdef TRY_UTIMENSAT
3503 if (result < 0 &&
errno == ENOSYS) {
3504# ifdef AT_SYMLINK_NOFOLLOW
3505 try_utimensat_follow = 0;
3517 tvbuf[0].tv_sec = tsp[0].tv_sec;
3518 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3519 tvbuf[1].tv_sec = tsp[1].tv_sec;
3520 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3524 if (v->follow)
return lutimes(path, tvp);
3526 return utimes(path, tvp);
3531#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3539utime_internal(
const char *path,
void *arg)
3543 struct utimbuf utbuf, *utp = NULL;
3545 utbuf.actime = tsp[0].tv_sec;
3546 utbuf.modtime = tsp[1].tv_sec;
3549 return utime(path, utp);
3554utime_internal_i(
int argc,
VALUE *argv,
int follow)
3557 struct timespec tss[2], *tsp = NULL;
3560 args.atime = *argv++;
3561 args.mtime = *argv++;
3563 args.follow = follow;
3565 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3568 if (args.atime == args.mtime)
3575 return apply2files(utime_internal, argc, argv, &args);
3592 return utime_internal_i(argc, argv, FALSE);
3595#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3652 return utime_internal_i(argc, argv, TRUE);
3655#define rb_file_s_lutime rb_f_notimplement
3658#ifdef RUBY_FUNCTION_NAME_STRING
3659# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3661# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3663#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3664NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3666syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3670 const int max_pathlen = MAX_PATH;
3672 const int max_pathlen = MAXPATHLEN;
3683#ifdef RUBY_FUNCTION_NAME_STRING
3684 rb_syserr_fail_path_in(func, e, str);
3686 rb_syserr_fail_path(e, str);
3721 from = rb_str_encode_ospath(from);
3722 to = rb_str_encode_ospath(to);
3725 sys_fail2(from, to);
3730#define rb_file_s_link rb_f_notimplement
3763 from = rb_str_encode_ospath(from);
3764 to = rb_str_encode_ospath(to);
3767 sys_fail2(from, to);
3772#define rb_file_s_symlink rb_f_notimplement
3799 return rb_readlink(path, rb_filesystem_encoding());
3802struct readlink_arg {
3809nogvl_readlink(
void *ptr)
3811 struct readlink_arg *ra = ptr;
3813 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
3817readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
3819 struct readlink_arg ra;
3821 ra.path = RSTRING_PTR(path);
3822 ra.buf = RSTRING_PTR(buf);
3825 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
3836 path = rb_str_encode_ospath(path);
3837 v = rb_enc_str_new(0, size, enc);
3838 while ((rv = readlink_without_gvl(path, v, size)) == size
3840 || (rv < 0 &&
errno == ERANGE)
3849 rb_str_resize(v, 0);
3850 rb_syserr_fail_path(e, path);
3852 rb_str_resize(v, rv);
3857#define rb_file_s_readlink rb_f_notimplement
3861unlink_internal(
const char *path,
void *arg)
3863 return unlink(path);
3883rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
3885 return apply2files(unlink_internal, argc, argv, 0);
3894no_gvl_rename(
void *ptr)
3898 return (
void *)(
VALUE)rename(ra->src, ra->dst);
3919 f = rb_str_encode_ospath(from);
3920 t = rb_str_encode_ospath(to);
3923#if defined __CYGWIN__
3926 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
3931 if (chmod(ra.dst, 0666) == 0 &&
3932 unlink(ra.dst) == 0 &&
3933 rename(ra.src, ra.dst) == 0)
3937 syserr_fail2(e, from, to);
3972 rb_error_arity(argc, 0, 1);
3980#if defined __CYGWIN__ || defined DOSISH
3982#define DOSISH_DRIVE_LETTER
3983#define FILE_ALT_SEPARATOR '\\'
3985#ifdef FILE_ALT_SEPARATOR
3986#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
3988static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
3991#define isdirsep(x) ((x) == '/')
4004# define USE_NTFS_ADS 1
4006# define USE_NTFS_ADS 0
4011#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
4013#define istrailinggarbage(x) 0
4017# define isADS(x) ((x) == ':')
4022#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
4024#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
4025#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
4027#if defined(DOSISH_UNC)
4028#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
4030#define has_unc(buf) 0
4033#ifdef DOSISH_DRIVE_LETTER
4035has_drive_letter(
const char *buf)
4037 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
4062 if (chdir(drive) == 0) {
4063 drvcwd = rb_dir_getwd_ospath();
4075not_same_drive(
VALUE path,
int drive)
4077 const char *p = RSTRING_PTR(path);
4078 if (RSTRING_LEN(path) < 2)
return 0;
4079 if (has_drive_letter(p)) {
4090skiproot(
const char *path,
const char *end)
4092#ifdef DOSISH_DRIVE_LETTER
4093 if (path + 2 <= end && has_drive_letter(path)) path += 2;
4095 while (path < end && isdirsep(*path)) path++;
4096 return (
char *)path;
4100enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
4102 while (s < e && !isdirsep(*s)) {
4103 Inc(s, e, mb_enc, enc);
4108#define nextdirsep rb_enc_path_next
4112 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
4115#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4116#define skipprefix enc_path_skip_prefix
4118#define skipprefix(path, end, mb_enc, enc) (path)
4121enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4123#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4125 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
4127 while (path < end && isdirsep(*path)) path++;
4128 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
4129 path + 2 <= end && !isdirsep(path[1])) {
4130 path = enc_path_next(path + 1, end, mb_enc, enc);
4132 return (
char *)path;
4135#ifdef DOSISH_DRIVE_LETTER
4136 if (path + 2 <= end && has_drive_letter(path))
4137 return (
char *)(path + 2);
4140 return (
char *)path;
4144rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
4146 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
4150skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
4152#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4153 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
4154 while (p < end && isdirsep(*p)) p++;
4157 return skiproot(path, end);
4162rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
4164 return skipprefixroot(path, end, enc);
4168enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4171 while (path < end) {
4172 if (isdirsep(*path)) {
4173 const char *tmp = path++;
4174 while (path < end && isdirsep(*path)) path++;
4175 if (path >= end)
break;
4179 Inc(path, end, mb_enc, enc);
4185rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
4187 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
4191strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4193 if (RB_UNLIKELY(mb_enc)) {
4194 return enc_path_last_separator(path, end, mb_enc, enc);
4197 const char *cursor = end - 1;
4199 while (isdirsep(cursor[0])) {
4203 while (cursor >= path) {
4204 if (isdirsep(cursor[0])) {
4205 while (cursor > path && isdirsep(cursor[-1])) {
4208 return (
char *)cursor;
4216chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4218 while (path < end) {
4219 if (isdirsep(*path)) {
4220 const char *last = path++;
4221 while (path < end && isdirsep(*path)) path++;
4222 if (path >= end)
return (
char *)last;
4225 Inc(path, end, mb_enc, enc);
4228 return (
char *)path;
4234 if (path < end && isdirsep(*path)) path++;
4235 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4241 rb_encoding *enc = rb_enc_check_str(path1, path2);
4242 int encidx = rb_enc_to_index(enc);
4243 if (encidx == ENCINDEX_US_ASCII) {
4244 encidx = rb_enc_get_index(path1);
4245 if (encidx == ENCINDEX_US_ASCII)
4246 encidx = rb_enc_get_index(path2);
4247 enc = rb_enc_from_index(encidx);
4254ntfs_tail(
const char *path,
const char *end,
rb_encoding *enc)
4256 bool mb_enc = enc_mbclen_needed(enc);
4257 while (path < end && *path ==
'.') path++;
4258 while (path < end && !isADS(*path)) {
4259 if (istrailinggarbage(*path)) {
4260 const char *last = path++;
4261 while (path < end && istrailinggarbage(*path)) path++;
4262 if (path >= end || isADS(*path))
return (
char *)last;
4264 else if (isdirsep(*path)) {
4265 const char *last = path++;
4266 while (path < end && isdirsep(*path)) path++;
4267 if (path >= end)
return (
char *)last;
4268 if (isADS(*path)) path++;
4271 Inc(path, end, mb_enc, enc);
4274 return (
char *)path;
4278#define BUFCHECK(cond) do {\
4281 do {buflen *= 2;} while (cond);\
4282 rb_str_resize(result, buflen);\
4283 buf = RSTRING_PTR(result);\
4285 pend = buf + buflen;\
4289#define BUFINIT(result, buf, p, pend) do {\
4290 if (!result) { result = rb_usascii_str_new(0, 1); } \
4291 p = buf = RSTRING_PTR(result); \
4292 buflen = RSTRING_LEN(result); \
4293 pend = p + buflen; \
4297# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4299# define SKIPPATHSEP(p) 1
4302#define BUFCOPY(srcptr, srclen) do { \
4303 const int skip = SKIPPATHSEP(p); \
4304 rb_str_set_len(result, p-buf+skip); \
4305 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4307 memcpy(p, (srcptr), (srclen)); \
4311#define WITH_ROOTDIFF(stmt) do { \
4312 long rootdiff = root - buf; \
4314 root = buf + rootdiff; \
4318copy_home_path(
VALUE result,
const char *dir)
4324 dirlen = strlen(dir);
4325 rb_str_resize(result, dirlen);
4326 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4327 encidx = rb_filesystem_encindex();
4328 rb_enc_associate_index(result, encidx);
4329#if defined FILE_ALT_SEPARATOR
4331 bool mb_enc = enc_mbclen_needed(enc);
4332 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4333 if (*p == FILE_ALT_SEPARATOR) {
4345 VALUE dirname = rb_getpwdirnam_for_login(user);
4346 if (dirname ==
Qnil) {
4347 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4349 const char *dir = RSTRING_PTR(dirname);
4351 extern char *getlogin(
void);
4352 const char *pwPtr = 0;
4354 # define endpwent() ((void)0)
4355 const char *dir, *username = RSTRING_PTR(user);
4366 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4367 dir = pwPtr = getenv(
"HOME");
4369 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4372 copy_home_path(result, dir);
4378rb_default_home_dir(
VALUE result)
4380 const char *dir = getenv(
"HOME");
4382#if defined HAVE_PWD_H
4396 VALUE login_name = rb_getlogin();
4398# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4403 if (
NIL_P(login_name)) {
4404 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4408 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4409 if (
NIL_P(pw_dir)) {
4410 pw_dir = rb_getpwdiruid();
4411 if (
NIL_P(pw_dir)) {
4412 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4417 copy_home_path(result, RSTRING_PTR(pw_dir));
4418 rb_str_resize(pw_dir, 0);
4423 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4425 return copy_home_path(result, dir);
4431#if NORMALIZE_UTF8PATH
4432 VALUE path = rb_str_normalize_ospath(ptr,
len);
4433 rb_enc_associate(path, fsenc);
4436 return rb_enc_str_new(ptr,
len, fsenc);
4443 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4445 rb_enc_associate(dirname, fsenc);
4449 size_t dirlen = RSTRING_LEN(dirname);
4452 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4453 if (!rb_enc_compatible(fname, dirname)) {
4456 rb_enc_check(fname, dirname);
4457 rb_bug(
"unreachable");
4459 rb_encoding *direnc = fs_enc_check(fname, dirname);
4460 if (direnc != fsenc) {
4467 do {buflen *= 2;}
while (dirlen > buflen);
4468 rb_str_resize(result, buflen);
4469 buf = RSTRING_PTR(result);
4470 memcpy(buf, cwdp, dirlen);
4471 rb_enc_associate(result, *enc);
4472 return buf + dirlen;
4476rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4478 const char *s, *b, *fend;
4479 char *buf, *p, *pend, *root;
4480 size_t buflen, bdiff;
4481 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4484 fend = s + RSTRING_LEN(fname);
4485 enc = rb_str_enc_get(fname);
4486 bool mb_enc = enc_mbclen_needed(enc);
4487 if (!mb_enc &&
RTEST(dname)) {
4488 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4491 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4492 BUFINIT(result, buf, p, pend);
4495 if (s + 1 == fend || isdirsep(s[1])) {
4499 if (++s < fend) ++s;
4500 rb_default_home_dir(result);
4503 s = nextdirsep(b = s, fend, enc);
4506 BUFCHECK(bdiff + userlen >= buflen);
4507 memcpy(p, b, userlen);
4510 rb_enc_associate(result, enc);
4511 rb_home_dir_of(result, result);
4515 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4517 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4518 (
int)userlen, b, fname);
4521 rb_raise(rb_eArgError,
"non-absolute home");
4524 BUFINIT(result, buf, p, pend);
4527#ifdef DOSISH_DRIVE_LETTER
4529 else if (s + 1 < fend && has_drive_letter(s)) {
4530 BUFINIT(result, buf, p, pend);
4532 if (s + 2 < fend && isdirsep(s[2])) {
4535 BUFCHECK(bdiff + 2 >= buflen);
4539 rb_enc_copy(result, fname);
4544 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4545 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4546 BUFINIT(result, buf, p, pend);
4553 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4554 BUFINIT(result, buf, p, pend);
4558 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4561 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4566 else if (s == fend || !rb_is_absolute_path(s)) {
4568 if (!
NIL_P(dname)) {
4570 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4574 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4576 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
4581 result = resized_result;
4585 rb_enc_associate(result, fs_enc_check(result, fname));
4586 BUFINIT(result, buf, p, pend);
4590 VALUE cwd = rb_dir_getwd_ospath();
4594 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
4595 BUFINIT(result, buf, p, pend);
4598#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4599 if (s < fend && isdirsep(*s)) {
4602 p = skipprefix(buf, p, mb_enc, enc);
4606 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4609 BUFINIT(result, buf, p, pend);
4613 do s++;
while (s < fend && isdirsep(*s));
4616 BUFCHECK(bdiff >= buflen);
4617 memset(buf,
'/',
len);
4619 rb_enc_associate(result, fs_enc_check(result, fname));
4621 if (p > buf && p[-1] ==
'/')
4625 BUFCHECK(bdiff + 1 >= buflen);
4630 BUFCHECK(bdiff + 1 >= buflen);
4632 root = skipprefix(buf, p+1, mb_enc, enc);
4645 if (s+1 == fend || isdirsep(*(s+1))) {
4649 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
4659 do ++s;
while (s < fend && istrailinggarbage(*s));
4664#if defined FILE_ALT_SEPARATOR
4665 case FILE_ALT_SEPARATOR:
4679 while (s < fend && istrailinggarbage(*s)) s++;
4689#if defined FILE_ALT_SEPARATOR
4690 case FILE_ALT_SEPARATOR:
4693 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4701 int n = ignored_char_p(s, fend, enc);
4704 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4712 Inc(s, fend, mb_enc, enc);
4720 static const char prime[] =
":$DATA";
4721 enum {prime_len =
sizeof(prime) -1};
4725 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) {
4728 if (isADS(*(s - (prime_len+1)))) {
4731 else if (memchr(b,
':', s - prime_len - b)) {
4740 if (p == skiproot(buf, p + !!*p) - 1) p++;
4744 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s,
"*?")) {
4749 WIN32_FIND_DATAW wfd;
4752#ifdef HAVE_CYGWIN_CONV_PATH
4753 char *w32buf = NULL;
4754 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
4756 char w32buf[MAXPATHLEN];
4760 int lnk_added = 0, is_symlink = 0;
4764 if (lstat_without_gvl(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
4770 path = *buf ? buf :
"/";
4771#ifdef HAVE_CYGWIN_CONV_PATH
4772 bufsize = cygwin_conv_path(flags, path, NULL, 0);
4775 if (lnk_added) bufsize += 4;
4777 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
4782 bufsize = MAXPATHLEN;
4783 if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
4787 if (is_symlink && b == w32buf) {
4789 strlcat(w32buf, p, bufsize);
4791 strlcat(w32buf,
".lnk", bufsize);
4802 if (encidx != ENCINDEX_UTF_8 && !is_ascii_string(result)) {
4803 tmp = rb_str_encode_ospath(result);
4805 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0);
4807 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr,
len);
4809 h = FindFirstFileW(wstr, &wfd);
4811 if (h != INVALID_HANDLE_VALUE) {
4814 len = lstrlenW(wfd.cFileName);
4816 if (lnk_added &&
len > 4 &&
4817 wcscasecmp(wfd.cFileName +
len - 4, L
".lnk") == 0) {
4818 wfd.cFileName[
len -= 4] = L
'\0';
4825 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL);
4826 if (tmp == result) {
4827 BUFCHECK(bdiff +
len >= buflen);
4828 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p,
len + 1, NULL, NULL);
4832 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, RSTRING_PTR(tmp),
len + 1, NULL, NULL);
4833 rb_str_cat_conv_enc_opts(result, bdiff, RSTRING_PTR(tmp),
len,
4834 rb_utf8_encoding(), 0,
Qnil);
4835 BUFINIT(result, buf, p, pend);
4836 rb_str_resize(tmp, 0);
4849 rb_enc_check(fname, result);
4856str_shrink(
VALUE str)
4858 rb_str_resize(str, RSTRING_LEN(str));
4862#define expand_path(fname, dname, abs_mode, long_name, result) \
4863 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
4865#define check_expand_path_args(fname, dname) \
4866 (((fname) = rb_get_path(fname)), \
4867 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
4870file_expand_path_1(
VALUE fname,
long extra_capa)
4873 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
4879 check_expand_path_args(fname, dname);
4880 return expand_path(fname, dname, 0, 1,
Qfalse);
4884rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
4886 return expand_path(fname, dname, 0, 0,
Qfalse);
4890rb_file_s_expand_path(
int argc,
const VALUE *argv)
4893 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4939 return rb_file_s_expand_path(c, v);
4945 check_expand_path_args(fname, dname);
4946 return expand_path(fname, dname, 1, 1,
Qfalse);
4950rb_file_s_absolute_path(
int argc,
const VALUE *argv)
4953 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4996 return rb_file_s_absolute_path(c, v);
5024 VALUE path = rb_get_path(fname);
5026 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
5030enum rb_realpath_mode {
5034 RB_REALPATH_MODE_MAX
5038realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
5039 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
5041 const char *pend = unresolved + strlen(unresolved);
5045 while (unresolved < pend) {
5046 const char *testname = unresolved;
5047 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
5048 long testnamelen = unresolved_firstsep - unresolved;
5049 const char *unresolved_nextname = unresolved_firstsep;
5050 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
5051 unresolved_nextname++;
5052 unresolved = unresolved_nextname;
5053 if (testnamelen == 1 && testname[0] ==
'.') {
5055 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
5056 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
5057 bool mb_enc = enc_mbclen_needed(enc);
5058 const char *resolved_str = RSTRING_PTR(*resolvedp);
5059 const char *resolved_names = resolved_str + *prefixlenp;
5060 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
5061 long len = lastsep ? lastsep - resolved_names : 0;
5062 rb_str_resize(*resolvedp, *prefixlenp +
len);
5068 if (*prefixlenp < RSTRING_LEN(testpath))
5070#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
5071 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
5072 const char *prefix = RSTRING_PTR(testpath);
5073 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
5078 checkval = rb_hash_aref(loopcheck, testpath);
5079 if (!
NIL_P(checkval)) {
5080 if (checkval ==
ID2SYM(resolving)) {
5081 if (mode == RB_REALPATH_CHECK) {
5085 rb_syserr_fail_path(ELOOP, testpath);
5094 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
5097 if (e == ENOENT && !
NIL_P(fallback)) {
5098 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
5103 if (mode == RB_REALPATH_CHECK)
return -1;
5105 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
5106 rb_syserr_fail_path(e, testpath);
5107 *resolvedp = testpath;
5111 rb_syserr_fail_path(e, testpath);
5115 if (S_ISLNK(sbuf.st_mode)) {
5118 const char *link_prefix, *link_names;
5119 long link_prefixlen;
5120 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
5121 link = rb_readlink(testpath, enc);
5122 link_prefix = RSTRING_PTR(link);
5123 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
5124 link_prefixlen = link_names - link_prefix;
5125 if (link_prefixlen > 0) {
5129 tmpenc = fs_enc_check(*resolvedp, link);
5132 *prefixlenp = link_prefixlen;
5134 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
5135 loopcheck, mode, !*unresolved_firstsep))
5144 rb_hash_aset(loopcheck, s, s);
5145 *resolvedp = testpath;
5154rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5158 VALUE unresolved_path;
5163 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
5164 char *ptr, *prefixptr = NULL, *pend;
5169 if (!
NIL_P(basedir)) {
5174 enc = rb_enc_get(unresolved_path);
5175 unresolved_path = TO_OSPATH(unresolved_path);
5177 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
5178 if (ptr != path_names) {
5179 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
5183 if (!
NIL_P(basedir)) {
5185 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
5186 if (ptr != basedir_names) {
5192 curdir = rb_dir_getwd_ospath();
5194 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
5199 pend = prefixptr + prefixlen;
5200 bool mb_enc = enc_mbclen_needed(enc);
5201 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
5203 prefixlen = ++ptr - prefixptr;
5206#ifdef FILE_ALT_SEPARATOR
5207 while (prefixptr < ptr) {
5208 if (*prefixptr == FILE_ALT_SEPARATOR) {
5211 Inc(prefixptr, pend, mb_enc, enc);
5215 switch (rb_enc_to_index(enc)) {
5216 case ENCINDEX_ASCII_8BIT:
5217 case ENCINDEX_US_ASCII:
5218 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5221 loopcheck = rb_hash_new();
5223 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5226 if (basedir_names) {
5227 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5230 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5233 if (origenc && origenc != rb_enc_get(resolved)) {
5235 rb_enc_associate(resolved, origenc);
5247static VALUE rb_file_join(
long argc,
VALUE *args);
5249#ifndef HAVE_REALPATH
5251rb_check_realpath_emulate_try(
VALUE arg)
5254 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5258rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5262#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5263 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5265# include <sys/syslimits.h>
5266# define NEEDS_REALPATH_BUFFER 1
5270rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5273 VALUE unresolved_path;
5274 char *resolved_ptr = NULL;
5276# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5277 char resolved_buffer[PATH_MAX];
5279 char *
const resolved_buffer = NULL;
5282 if (mode == RB_REALPATH_DIR) {
5283 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5287 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5288 VALUE paths[2] = {basedir, unresolved_path};
5289 unresolved_path = rb_file_join(2, paths);
5291 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5293 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5302 if (
errno == ENOTSUP ||
5304 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5305 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5308 if (mode == RB_REALPATH_CHECK) {
5311 rb_sys_fail_path(unresolved_path);
5313 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5314# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5318# if !defined(__linux__) && !defined(__APPLE__)
5322 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5323 if (mode == RB_REALPATH_CHECK) {
5326 rb_sys_fail_path(unresolved_path);
5330 if (origenc && origenc != rb_enc_get(resolved)) {
5334 rb_enc_associate(resolved, origenc);
5337 if (is_broken_string(resolved)) {
5338 rb_enc_associate(resolved, rb_filesystem_encoding());
5339 if (is_broken_string(resolved)) {
5340 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5347 if (mode == RB_REALPATH_CHECK) {
5351 arg[2] = (
VALUE)origenc;
5354 rb_check_realpath_emulate_rescue,
Qnil);
5357 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5363rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5365 const enum rb_realpath_mode mode =
5366 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5367 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5373 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5390rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5393 VALUE path = argv[0];
5395 return rb_realpath_internal(basedir, path, 1);
5411rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5414 VALUE path = argv[0];
5416 return rb_realpath_internal(basedir, path, 0);
5420rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5424 const char *s, *last;
5426 if (!e || !l2)
return 0;
5428 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5429 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5430 if (c ==
'.')
return l0;
5435 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5440 if (l1 < l2)
return l1;
5443 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5444#if CASEFOLD_FILESYSTEM
5445#define fncomp strncasecmp
5447#define fncomp strncmp
5449 if (fncomp(s, e, l2) == 0) {
5455static inline const char *
5456enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5458 const char *p, *q, *e, *end;
5461 long len = (alllen ? (size_t)*alllen : strlen(name));
5468 name = skipprefix(name, end, mb_enc, enc);
5469#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5470 const char *root = name;
5473 while (name < end && isdirsep(*name)) {
5480#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5484#ifdef DOSISH_DRIVE_LETTER
5485 else if (*p ==
':') {
5498 p = strrdirsep(name, end, mb_enc, enc);
5503 while (isdirsep(*p)) {
5508 n = ntfs_tail(p, end, enc) - p;
5510 n = chompdirsep(p, end, mb_enc, enc) - p;
5512 for (q = p; q - p < n && *q ==
'.'; q++);
5513 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5514 if (*q ==
'.') e = q;
5534ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5536 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5580 const char *name, *p, *fp = 0;
5586 CheckPath(fname, name);
5590 check_path_encoding(fext);
5592 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5593 enc = rb_str_enc_get(fname);
5596 n = RSTRING_LEN(fname);
5597 if (n <= 0 || !*name) {
5598 return rb_enc_str_new(0, 0, enc);
5601 bool mb_enc = enc_mbclen_needed(enc);
5602 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5608 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5613 if (f == RSTRING_LEN(fname)) {
5618 return rb_enc_str_new(p, f, enc);
5621static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5644rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5650 return rb_file_dirname_n(argv[0], n);
5656 return rb_file_dirname_n(fname, 1);
5660rb_file_dirname_n(
VALUE fname,
int n)
5662 const char *name, *root, *p, *end;
5665 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5666 CheckPath(fname, name);
5667 end = name + RSTRING_LEN(fname);
5669 bool mb_enc = !rb_str_enc_fastpath(fname);
5672 root = skiproot(name, end);
5674 if (root > name + 1 && isdirsep(*name))
5675 root = skipprefix(name = root - 2, end, mb_enc, enc);
5677 if (root > name + 1)
5680 if (n > (end - root + 1) / 2) {
5686 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5695 return rb_enc_str_new(
".", 1, enc);
5697#ifdef DOSISH_DRIVE_LETTER
5698 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5699 const char *top = skiproot(name + 2, end);
5700 dirname = rb_enc_str_new(name, 3, enc);
5705 dirname = rb_enc_str_new(name, p - name, enc);
5706#ifdef DOSISH_DRIVE_LETTER
5707 if (root == name + 2 && p == root && name[1] ==
':')
5713static inline const char *
5714enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5716 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5718 p = strrdirsep(name, end, mb_enc, enc);
5722 do name = ++p;
while (isdirsep(*p));
5725 while (*p && *p ==
'.') p++;
5727 if (*p ==
'.' || istrailinggarbage(*p)) {
5729 const char *last = p++, *dot = last;
5730 while (istrailinggarbage(*p)) {
5731 if (*p ==
'.') dot = p;
5734 if (!*p || isADS(*p)) {
5738 if (*last ==
'.' || dot > last) e = dot;
5745 else if (isADS(*p)) {
5749 else if (isdirsep(*p))
5751 Inc(p, end, mb_enc, enc);
5756 if (!e || e == name)
5781 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
5828 CheckPath(fname, name);
5829 long len = RSTRING_LEN(fname);
5832 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
5835 bool mb_enc = !rb_str_enc_fastpath(fname);
5838 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
5839 return rb_enc_str_new(ext,
len, enc);
5872 return rb_get_path(fname);
5896file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
5898 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
5899 return rb_file_join_ary(arg);
5903rb_file_join_ary(
VALUE ary)
5907 const char *name, *tail;
5917 check_path_encoding(tmp);
5918 len += RSTRING_LEN(tmp);
5926 RBASIC_CLEAR_CLASS(result);
5929 switch (OBJ_BUILTIN_TYPE(tmp)) {
5931 if (!checked) check_path_encoding(tmp);
5936 rb_raise(rb_eArgError,
"recursive array");
5948 rb_enc_copy(result, tmp);
5951 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
5952 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
5955 else if (tail == name +
len) {
5959 enc = fs_enc_check(result, tmp);
5961 rb_enc_associate(result, enc);
5969rb_file_join_fastpath(
long argc,
VALUE *args)
5974 for (i = 0; i < argc; i++) {
5975 VALUE tmp = args[i];
5977 size += RSTRING_LEN(tmp);
5990 const char *name = RSTRING_PTR(result);
5991 for (i = 1; i < argc; i++) {
5992 VALUE tmp = args[i];
5993 long len = RSTRING_LEN(result);
5999 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
6002 while (chomp > 0 && isdirsep(name[chomp - 1])) {
6007 else if (
len < 1 || !isdirsep(name[
len - 1])) {
6014 rb_enc_associate(result, new_enc);
6015 encidx = rb_enc_to_index(new_enc);
6021 rb_str_null_check(result);
6026rb_file_join(
long argc,
VALUE *args)
6028 if (RB_UNLIKELY(argc == 0)) {
6032 VALUE result = rb_file_join_fastpath(argc, args);
6033 if (RB_LIKELY(result)) {
6053rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
6055 return rb_file_join(argc, argv);
6058#if defined(HAVE_TRUNCATE)
6059struct truncate_arg {
6065nogvl_truncate(
void *ptr)
6067 struct truncate_arg *ta = ptr;
6068 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
6095 struct truncate_arg ta;
6100 path = rb_str_encode_ospath(path);
6103 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
6105 rb_sys_fail_path(path);
6109#define rb_file_s_truncate rb_f_notimplement
6112#if defined(HAVE_FTRUNCATE)
6113struct ftruncate_arg {
6119nogvl_ftruncate(
void *ptr)
6121 struct ftruncate_arg *fa = ptr;
6123 return (
VALUE)ftruncate(fa->fd, fa->pos);
6144 struct ftruncate_arg fa;
6151 rb_io_flush_raw(obj, 0);
6153 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
6154 rb_sys_fail_path(fptr->
pathv);
6159#define rb_file_truncate rb_f_notimplement
6176#include <winerror.h>
6180rb_thread_flock(
void *data)
6183 int old_errno =
errno;
6185 int *op = data, ret = flock(op[0], op[1]);
6188 if (GetLastError() == ERROR_NOT_LOCKED) {
6246 op[1] = op1 =
NUM2INT(operation);
6251 rb_io_flush_raw(obj, 0);
6253 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6258#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6261 if (op1 & LOCK_NB)
return Qfalse;
6264 time.tv_usec = 100 * 1000;
6270#if defined(ERESTART)
6276 rb_syserr_fail_path(e, fptr->
pathv);
6283test_check(
int n,
int argc,
VALUE *argv)
6289 for (i=1; i<n; i++) {
6296#define CHECK(n) test_check((n), argc, argv)
6388 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6392 return rb_file_blockdev_p(0, argv[1]);
6395 return rb_file_chardev_p(0, argv[1]);
6398 return rb_file_directory_p(0, argv[1]);
6401 return rb_file_exist_p(0, argv[1]);
6404 return rb_file_file_p(0, argv[1]);
6407 return rb_file_sgid_p(0, argv[1]);
6410 return rb_file_grpowned_p(0, argv[1]);
6413 return rb_file_sticky_p(0, argv[1]);
6416 return rb_file_symlink_p(0, argv[1]);
6419 return rb_file_owned_p(0, argv[1]);
6422 return rb_file_rowned_p(0, argv[1]);
6425 return rb_file_pipe_p(0, argv[1]);
6428 return rb_file_readable_p(0, argv[1]);
6431 return rb_file_readable_real_p(0, argv[1]);
6434 return rb_file_size_p(0, argv[1]);
6437 return rb_file_socket_p(0, argv[1]);
6440 return rb_file_suid_p(0, argv[1]);
6443 return rb_file_writable_p(0, argv[1]);
6446 return rb_file_writable_real_p(0, argv[1]);
6449 return rb_file_executable_p(0, argv[1]);
6452 return rb_file_executable_real_p(0, argv[1]);
6455 return rb_file_zero_p(0, argv[1]);
6459 if (strchr(
"MAC", cmd)) {
6461 VALUE fname = argv[1];
6464 if (
rb_stat(fname, &st) == -1) {
6467 rb_syserr_fail_path(e, fname);
6472 return stat_atime(&st);
6474 return stat_mtime(&st);
6476 return stat_ctime(&st);
6482 return rb_file_identical_p(0, argv[1], argv[2]);
6485 if (strchr(
"=<>", cmd)) {
6486 struct stat st1, st2;
6493 t1 = stat_mtimespec(&st1);
6494 t2 = stat_mtimespec(&st2);
6498 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6502 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6503 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6507 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6508 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6515 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6518 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6572rb_stat_s_alloc(
VALUE klass)
6593 fname = rb_str_encode_ospath(fname);
6595 rb_sys_fail_path(fname);
6602 rb_st->initialized =
true;
6619 *copy_rb_st = *orig_rb_st;
6661rb_stat_ftype(
VALUE obj)
6663 return rb_file_ftype(get_stat(obj)->ST_(mode));
6680 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6696 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
6728 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
6749 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
6772 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
6793 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
6811rb_stat_owned(
VALUE obj)
6813 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
6818rb_stat_rowned(
VALUE obj)
6820 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
6841rb_stat_grpowned(
VALUE obj)
6844 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
6863 rb_io_stat_data *st = get_stat(obj);
6866 if (geteuid() == 0)
return Qtrue;
6869 if (rb_stat_owned(obj))
6870 return RBOOL(st->ST_(mode) & S_IRUSR);
6873 if (rb_stat_grpowned(obj))
6874 return RBOOL(st->ST_(mode) & S_IRGRP);
6877 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6896 rb_io_stat_data *st = get_stat(obj);
6899 if (getuid() == 0)
return Qtrue;
6902 if (rb_stat_rowned(obj))
6903 return RBOOL(st->ST_(mode) & S_IRUSR);
6906 if (rb_group_member(get_stat(obj)->ST_(gid)))
6907 return RBOOL(st->ST_(mode) & S_IRGRP);
6910 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6929rb_stat_wr(
VALUE obj)
6932 rb_io_stat_data *st = get_stat(obj);
6933 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
6934 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6954 rb_io_stat_data *st = get_stat(obj);
6957 if (geteuid() == 0)
return Qtrue;
6960 if (rb_stat_owned(obj))
6961 return RBOOL(st->ST_(mode) & S_IWUSR);
6964 if (rb_stat_grpowned(obj))
6965 return RBOOL(st->ST_(mode) & S_IWGRP);
6968 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6987 rb_io_stat_data *st = get_stat(obj);
6990 if (getuid() == 0)
return Qtrue;
6993 if (rb_stat_rowned(obj))
6994 return RBOOL(st->ST_(mode) & S_IWUSR);
6997 if (rb_group_member(get_stat(obj)->ST_(gid)))
6998 return RBOOL(st->ST_(mode) & S_IWGRP);
7001 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
7020rb_stat_ww(
VALUE obj)
7023 rb_io_stat_data *st = get_stat(obj);
7024 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
7025 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
7067 rb_io_stat_data *st = get_stat(obj);
7070 if (geteuid() == 0) {
7071 return RBOOL(st->ST_(mode) & S_IXUGO);
7075 if (rb_stat_owned(obj))
7076 return RBOOL(st->ST_(mode) & S_IXUSR);
7079 if (rb_stat_grpowned(obj))
7080 return RBOOL(st->ST_(mode) & S_IXGRP);
7083 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7099 rb_io_stat_data *st = get_stat(obj);
7102 if (getuid() == 0) {
7103 return RBOOL(st->ST_(mode) & S_IXUGO);
7107 if (rb_stat_rowned(obj))
7108 return RBOOL(st->ST_(mode) & S_IXUSR);
7111 if (rb_group_member(get_stat(obj)->ST_(gid)))
7112 return RBOOL(st->ST_(mode) & S_IXGRP);
7115 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
7138 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
7156 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
7175 rb_off_t size = get_stat(obj)->ST_(size);
7177 if (size == 0)
return Qnil;
7193rb_stat_suid(
VALUE obj)
7196 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
7214rb_stat_sgid(
VALUE obj)
7217 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
7235rb_stat_sticky(
VALUE obj)
7238 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7243#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7244#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7255nogvl_mkfifo(
void *ptr)
7257 struct mkfifo_arg *ma = ptr;
7259 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7276 struct mkfifo_arg ma;
7285 path = rb_str_encode_ospath(path);
7286 ma.path = RSTRING_PTR(path);
7287 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7288 rb_sys_fail_path(path);
7293#define rb_file_s_mkfifo rb_f_notimplement
7296static VALUE rb_mFConst;
7299rb_file_const(
const char *name,
VALUE value)
7301 rb_define_const(rb_mFConst, name, value);
7305rb_is_absolute_path(
const char *path)
7307#ifdef DOSISH_DRIVE_LETTER
7308 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7311 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7314 if (path[0] ==
'/')
return 1;
7320ruby_is_fd_loadable(
int fd)
7327 if (fstat(fd, &st) < 0)
7330 if (S_ISREG(st.st_mode))
7333 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7336 if (S_ISDIR(st.st_mode))
7347rb_file_load_ok(
const char *path)
7354 int mode = (O_RDONLY |
7355#if defined O_NONBLOCK
7357#elif defined O_NDELAY
7363 if (!rb_gc_for_fd(
errno))
return 0;
7365 if (fd < 0)
return 0;
7368 ret = ruby_is_fd_loadable(fd);
7375is_explicit_relative(
const char *path)
7377 if (*path++ !=
'.')
return 0;
7378 if (*path ==
'.') path++;
7379 return isdirsep(*path);
7385 int encidx = rb_enc_get_index(orig);
7386 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7387 encidx = rb_filesystem_encindex();
7388 rb_enc_associate_index(path, encidx);
7396nav_component_p(
const char *s,
const char *send)
7398 if ((send - s) >= 2 && s[0] ==
'.') {
7399 return s[1] ==
'.' || isdirsep(s[1]);
7405fname_need_expansion_p(
VALUE fname)
7407 const char *s = RSTRING_PTR(fname);
7408 const long len = RSTRING_LEN(fname);
7409 const char *send = s +
len;
7411 if (nav_component_p(s, send)) {
7416 bool mbenc = enc_mbclen_needed(enc);
7418 s = enc_path_next(s, send, mbenc, enc);
7420 if (nav_component_p(s, send)) {
7424 s = enc_path_next(s, send, mbenc, enc);
7430expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7432 long dname_len = RSTRING_LEN(dname);
7433 const char *dname_ptr = RSTRING_PTR(dname);
7437 if (need_expansion || dname_ptr[0] ==
'~') {
7438 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7443 if (!isdirsep(dname_ptr[dname_len - 1])) {
7452rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
7455 VALUE fname = *filep;
7459 if (!ext[0])
return 0;
7462 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7463 f = RSTRING_PTR(fname);
7468 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7469 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7470 fnlen = RSTRING_LEN(fname);
7471 for (i=0; ext[i]; i++) {
7473 if (rb_file_load_ok(RSTRING_PTR(fname))) {
7474 *filep = copy_path_class(fname, *filep);
7482 long expanded_load_path_maxlen;
7483 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7484 if (!load_path)
return 0;
7487 RBASIC_CLEAR_CLASS(fname);
7488 fnlen = RSTRING_LEN(fname);
7489 bool need_expansion = fname_need_expansion_p(fname);
7492 rb_enc_associate_index(tmp, rb_usascii_encindex());
7494 for (j=0; ext[j]; j++) {
7496 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
7498 if (!RSTRING_LEN(dname))
continue;
7499 expand_feature(fname, dname, tmp, need_expansion);
7501 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7502 *filep = copy_path_class(tmp, *filep);
7508 rb_str_resize(tmp, 0);
7521 path = copy_path_class(file_expand_path_1(path, 0), path);
7522 f = RSTRING_PTR(path);
7526 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7527 if (!rb_file_load_ok(f))
return 0;
7529 path = copy_path_class(file_expand_path_1(path, 0), path);
7533 long expanded_load_path_maxlen;
7534 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7537 bool need_expansion = fname_need_expansion_p(path);
7539 rb_enc_associate_index(tmp, rb_usascii_encindex());
7540 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
7542 if (!RSTRING_LEN(dname))
continue;
7543 expand_feature(path, dname, tmp, need_expansion);
7545 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7546 return copy_path_class(tmp, path);
7549 rb_str_resize(tmp, 0);
7557#define define_filetest_function(name, func, argc) do { \
7558 rb_define_module_function(rb_mFileTest, name, func, argc); \
7559 rb_define_singleton_method(rb_cFile, name, func, argc); \
7562const char ruby_null_device[] =
7565#elif defined AMIGA || defined __amigaos__
8369#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8370 rb_CFString_class_initialize_before_fork();
8378 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8379 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8380 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8381 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8382 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8383 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8384 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8385 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8386 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8387 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8388 define_filetest_function(
"file?", rb_file_file_p, 1);
8389 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8390 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8391 define_filetest_function(
"size?", rb_file_size_p, 1);
8392 define_filetest_function(
"size", rb_file_s_size, 1);
8393 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8394 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8396 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8397 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8398 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8400 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8401 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8403 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8404 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8405 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8407 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8445 separator = rb_fstring_lit(
"/");
8447 rb_define_const(
rb_cFile,
"Separator", separator);
8449 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
8798 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
8801 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
8803 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
8805 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
8807 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
8809 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
8811 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
8812#if defined(O_NDELAY) || defined(O_NONBLOCK)
8814# define O_NONBLOCK O_NDELAY
8817 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
8820 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
8823 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
8829 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
8830#ifndef O_SHARE_DELETE
8831# define O_SHARE_DELETE 0
8834 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
8837 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
8841 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
8845 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
8849 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
8853 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
8857 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
8861 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
8865 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
8867 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
8869 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
8871 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
8874 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.