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
1136rb_stat_atime(
VALUE self)
1138 return stat_time(statx_atimespec(get_stat(self)));
1152rb_stat_mtime(
VALUE self)
1154 return stat_time(statx_mtimespec(get_stat(self)));
1172rb_stat_ctime(
VALUE self)
1174 return stat_time(statx_ctimespec(get_stat(self)));
1177#if defined(HAVE_STAT_BIRTHTIME)
1199rb_stat_birthtime(
VALUE self)
1201 return statx_birthtime(get_stat(self));
1204# define rb_stat_birthtime rb_f_notimplement
1223rb_stat_inspect(
VALUE self)
1227 static const struct {
1231 {
"dev", rb_stat_dev},
1232 {
"ino", rb_stat_ino},
1233 {
"mode", rb_stat_mode},
1234 {
"nlink", rb_stat_nlink},
1235 {
"uid", rb_stat_uid},
1236 {
"gid", rb_stat_gid},
1237 {
"rdev", rb_stat_rdev},
1238 {
"size", rb_stat_size},
1239 {
"blksize", rb_stat_blksize},
1240 {
"blocks", rb_stat_blocks},
1241 {
"atime", rb_stat_atime},
1242 {
"mtime", rb_stat_mtime},
1243 {
"ctime", rb_stat_ctime},
1244#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1245 {
"birthtime", rb_stat_birthtime},
1251 if (!rb_st->initialized) {
1259 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1267 v = (*member[i].func)(self);
1269 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1271 else if (i == 0 || i == 6) {
1272 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1292no_gvl_fstat(
void *data)
1295 return (
VALUE)fstat(arg->file.fd, arg->st);
1299fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1303 data.file.fd = fptr->
fd;
1306 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1310no_gvl_stat(
void * data)
1313 return (
void *)(
VALUE)STAT(arg->file.path, arg->st);
1317stat_without_gvl(
const char *path,
struct stat *st)
1321 data.file.path = path;
1324 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1327#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1328 defined(HAVE_STRUCT_STATX_STX_BTIME)
1330# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1333# ifdef HAVE_SYSCALL_H
1334# include <syscall.h>
1335# elif defined HAVE_SYS_SYSCALL_H
1336# include <sys/syscall.h>
1338# if defined __linux__
1339# include <linux/stat.h>
1341statx(
int dirfd,
const char *pathname,
int flags,
1342 unsigned int mask,
struct statx *statxbuf)
1344 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1349typedef struct no_gvl_rb_io_stat_data {
1355} no_gvl_rb_io_stat_data;
1358io_blocking_statx(
void *data)
1360 no_gvl_rb_io_stat_data *arg = data;
1361 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1365no_gvl_statx(
void *data)
1367 return (
void *)io_blocking_statx(data);
1371statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1373 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1376 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1380lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1382 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1385 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1389fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1391 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1394 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1397#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1400rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1405 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1410 result = fstatx_without_gvl(fptr, stx, mask);
1415 file = rb_str_encode_ospath(file);
1416 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1422# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1424NORETURN(
static void statx_notimplement(
const char *field_name));
1429statx_notimplement(
const char *field_name)
1432 "%s is unimplemented on this filesystem",
1437statx_birthtime(
const rb_io_stat_data *stx)
1439 if (!statx_has_birthtime(stx)) {
1441 statx_notimplement(
"birthtime");
1443 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1448# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1449# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1450# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1451# define rb_statx(file, stx, mask) rb_stat(file, stx)
1452# define STATX(path, st, mask) STAT(path, st)
1454#if defined(HAVE_STAT_BIRTHTIME)
1455# define statx_has_birthtime(st) 1
1457# define statx_has_birthtime(st) 0
1464# define FSTAT(fd, st) fstat(fd, st)
1473 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1478 result = fstat_without_gvl(fptr, st);
1483 file = rb_str_encode_ospath(file);
1484 result = stat_without_gvl(RSTRING_PTR(file), st);
1506 fname = rb_str_encode_ospath(fname);
1507 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1508 rb_sys_fail_path(fname);
1510 return rb_statx_new(&st);
1529rb_io_stat(
VALUE obj)
1535 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1536 rb_sys_fail_path(fptr->
pathv);
1538 return rb_statx_new(&st);
1543no_gvl_lstat(
void *ptr)
1546 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1550lstat_without_gvl(
const char *path,
struct stat *st)
1554 data.file.path = path;
1557 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1594 fname = rb_str_encode_ospath(fname);
1595 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1596 rb_sys_fail_path(fname);
1598 return rb_statx_new(&st);
1600 return rb_file_s_stat(klass, fname);
1619rb_file_lstat(
VALUE obj)
1628 path = rb_str_encode_ospath(fptr->
pathv);
1629 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1630 rb_sys_fail_path(fptr->
pathv);
1632 return rb_statx_new(&st);
1634 return rb_io_stat(obj);
1639rb_group_member(GETGROUPS_T gid)
1641#if defined(_WIN32) || !defined(HAVE_GETGROUPS)
1650 if (getgid() == gid || getegid() == gid)
1653 groups = getgroups(0, NULL);
1654 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1655 anum = getgroups(groups, gary);
1656 while (--anum >= 0) {
1657 if (gary[anum] == gid) {
1670# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1673#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1674#define USE_GETEUID 1
1679eaccess(
const char *path,
int mode)
1688 if (getuid() == euid && getgid() == getegid())
1689 return access(path, mode);
1691 if (STAT(path, &st) < 0)
1701 if (st.st_mode & S_IXUGO)
1707 if (st.st_uid == euid)
1709 else if (rb_group_member(st.st_gid))
1712 if ((
int)(st.st_mode & mode) == mode)
return 0;
1716 return access(path, mode);
1727nogvl_eaccess(
void *ptr)
1731 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1735rb_eaccess(
VALUE fname,
int mode)
1740 fname = rb_str_encode_ospath(fname);
1744 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1748nogvl_access(
void *ptr)
1752 return (
void *)(
VALUE)access(aa->path, aa->mode);
1756rb_access(
VALUE fname,
int mode)
1761 fname = rb_str_encode_ospath(fname);
1765 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1800# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1806 if (S_ISDIR(st.st_mode))
return Qtrue;
1827# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1833 if (S_ISFIFO(st.st_mode))
return Qtrue;
1866# define S_ISLNK(m) _S_ISLNK(m)
1869# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1872# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1882 fname = rb_str_encode_ospath(fname);
1884 if (S_ISLNK(st.st_mode))
return Qtrue;
1907# define S_ISSOCK(m) _S_ISSOCK(m)
1910# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1913# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1923 if (S_ISSOCK(st.st_mode))
return Qtrue;
1945# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1947# define S_ISBLK(m) (0)
1955 if (S_ISBLK(st.st_mode))
return Qtrue;
1975# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
1981 if (S_ISCHR(st.st_mode))
return Qtrue;
2020 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2035rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2037 return RBOOL(rb_access(fname, R_OK) >= 0);
2041# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2045# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2065rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2071 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2072 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2092 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2107rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2109 return RBOOL(rb_access(fname, W_OK) >= 0);
2129rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2135 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2136 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2160 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2179rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2181 return RBOOL(rb_access(fname, X_OK) >= 0);
2185# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2206 return RBOOL(S_ISREG(st.st_mode));
2225 return RBOOL(st.st_size == 0);
2244 if (st.st_size == 0)
return Qnil;
2265 return RBOOL(st.st_uid == geteuid());
2274 return RBOOL(st.st_uid == getuid());
2306 if (rb_group_member(st.st_gid))
return Qtrue;
2311#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2313check3rdbyte(
VALUE fname,
int mode)
2318 return RBOOL(st.st_mode & mode);
2335 return check3rdbyte(fname, S_ISUID);
2354 return check3rdbyte(fname, S_ISGID);
2373 return check3rdbyte(fname, S_ISVTX);
2402 struct stat st1, st2;
2406 if (st1.st_dev != st2.st_dev)
return Qfalse;
2407 if (st1.st_ino != st2.st_ino)
return Qfalse;
2411 return rb_w32_file_identical_p(fname1, fname2);
2429 if (
rb_stat(fname, &st) < 0) {
2432 rb_syserr_fail_path(e, fname);
2438rb_file_ftype(mode_t mode)
2442 if (S_ISREG(mode)) {
2445 else if (S_ISDIR(mode)) {
2448 else if (S_ISCHR(mode)) {
2449 t =
"characterSpecial";
2452 else if (S_ISBLK(mode)) {
2457 else if (S_ISFIFO(mode)) {
2462 else if (S_ISLNK(mode)) {
2467 else if (S_ISSOCK(mode)) {
2475 return rb_fstring_cstr(t);
2517 fname = rb_str_encode_ospath(fname);
2519 rb_sys_fail_path(fname);
2522 return rb_file_ftype(st.st_mode);
2553 if (
rb_stat(fname, &st) < 0) {
2556 rb_syserr_fail_path(e, fname);
2558 return stat_time(stat_atimespec(&st));
2582rb_file_atime(
VALUE obj)
2588 if (fstat(fptr->
fd, &st) == -1) {
2589 rb_sys_fail_path(fptr->
pathv);
2591 return stat_time(stat_atimespec(&st));
2611 if (
rb_stat(fname, &st) < 0) {
2614 rb_syserr_fail_path(e, fname);
2616 return stat_time(stat_mtimespec(&st));
2630rb_file_mtime(
VALUE obj)
2636 if (fstat(fptr->
fd, &st) == -1) {
2637 rb_sys_fail_path(fptr->
pathv);
2639 return stat_time(stat_mtimespec(&st));
2663 if (
rb_stat(fname, &st) < 0) {
2666 rb_syserr_fail_path(e, fname);
2668 return stat_time(stat_ctimespec(&st));
2685rb_file_ctime(
VALUE obj)
2691 if (fstat(fptr->
fd, &st) == -1) {
2692 rb_sys_fail_path(fptr->
pathv);
2694 return stat_time(stat_ctimespec(&st));
2697#if defined(HAVE_STAT_BIRTHTIME)
2722 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
2725 rb_syserr_fail_path(e, fname);
2727 return statx_birthtime(&st);
2730# define rb_file_s_birthtime rb_f_notimplement
2733#if defined(HAVE_STAT_BIRTHTIME)
2754rb_file_birthtime(
VALUE obj)
2760 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
2761 rb_sys_fail_path(fptr->
pathv);
2763 return statx_birthtime(&st);
2766# define rb_file_birthtime rb_f_notimplement
2778 rb_io_flush_raw(file, 0);
2781 if (fstat(fptr->
fd, &st) == -1) {
2782 rb_sys_fail_path(fptr->
pathv);
2803file_size(
VALUE self)
2805 return OFFT2NUM(rb_file_size(self));
2814nogvl_chmod(
void *ptr)
2817 int ret = chmod(data->path, data->mode);
2818 return (
void *)(
VALUE)ret;
2822rb_chmod(
const char *path, mode_t mode)
2828 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
2832chmod_internal(
const char *path,
void *mode)
2834 return chmod(path, *(mode_t *)mode);
2858 return apply2files(chmod_internal, argc, argv, &mode);
2862struct nogvl_fchmod_data {
2868io_blocking_fchmod(
void *ptr)
2870 struct nogvl_fchmod_data *data = ptr;
2871 int ret = fchmod(data->fd, data->mode);
2876rb_fchmod(
struct rb_io* io, mode_t mode)
2879 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
2880 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
2902#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2910 if (rb_fchmod(fptr, mode) == -1) {
2911 if (HAVE_FCHMOD ||
errno != ENOSYS)
2912 rb_sys_fail_path(fptr->
pathv);
2915 if (!HAVE_FCHMOD)
return INT2FIX(0);
2918#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2920 path = rb_str_encode_ospath(fptr->
pathv);
2921 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
2922 rb_sys_fail_path(fptr->
pathv);
2928#if defined(HAVE_LCHMOD)
2930lchmod_internal(
const char *path,
void *mode)
2932 return lchmod(path, *(mode_t *)mode);
2969 return apply2files(lchmod_internal, argc, argv, &mode);
2972#define rb_file_s_lchmod rb_f_notimplement
2975static inline rb_uid_t
2979 return (rb_uid_t)-1;
2984static inline rb_gid_t
2988 return (rb_gid_t)-1;
2999chown_internal(
const char *path,
void *arg)
3002 return chown(path, args->owner, args->group);
3026 arg.owner = to_uid(*argv++);
3027 arg.group = to_gid(*argv++);
3029 return apply2files(chown_internal, argc, argv, &arg);
3041nogvl_chown(
void *ptr)
3044 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
3048rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
3051 .as = {.path = path},
3052 .new = {.owner = owner, .group = group},
3054 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
3059nogvl_fchown(
void *ptr)
3062 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
3066rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3071 .new = {.owner = owner, .group = group},
3073 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3107 path = rb_str_encode_ospath(fptr->
pathv);
3108 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3109 rb_sys_fail_path(fptr->
pathv);
3111 if (rb_fchown(fptr->
fd, o, g) == -1)
3112 rb_sys_fail_path(fptr->
pathv);
3118#if defined(HAVE_LCHOWN)
3120lchown_internal(
const char *path,
void *arg)
3123 return lchown(path, args->owner, args->group);
3179 arg.owner = to_uid(*argv++);
3180 arg.group = to_gid(*argv++);
3182 return apply2files(lchown_internal, argc, argv, &arg);
3185#define rb_file_s_lchown rb_f_notimplement
3195NORETURN(
static void utime_failed(
struct apply_arg *));
3201 VALUE path = aa->fn[aa->i].path;
3204 if (ua->tsp && e == EINVAL) {
3207 VALUE atime = ua->atime;
3208 VALUE mtime = ua->mtime;
3210 if (!
NIL_P(atime)) {
3213 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3216 if (
NIL_P(a)) e[0] = m;
3232 rb_syserr_fail_path(e, path);
3236#if defined(HAVE_UTIMES)
3238# if !defined(HAVE_UTIMENSAT)
3240# elif defined(__APPLE__) && \
3241 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3243# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3244typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3248static inline utimensat_func *
3255# define utimensat rb_utimensat()
3262utime_internal(
const char *path,
void *arg)
3265 const struct timespec *tsp = v->tsp;
3266 struct timeval tvbuf[2], *tvp = NULL;
3268#if defined(HAVE_UTIMENSAT)
3269# if defined(__APPLE__)
3270 const int try_utimensat = utimensat != NULL;
3271 const int try_utimensat_follow = utimensat != NULL;
3273# define TRY_UTIMENSAT 1
3274 static int try_utimensat = 1;
3275# ifdef AT_SYMLINK_NOFOLLOW
3276 static int try_utimensat_follow = 1;
3278 const int try_utimensat_follow = 0;
3283 if (v->follow ? try_utimensat_follow : try_utimensat) {
3284# ifdef AT_SYMLINK_NOFOLLOW
3286 flags = AT_SYMLINK_NOFOLLOW;
3290 int result = utimensat(AT_FDCWD, path, tsp, flags);
3291# ifdef TRY_UTIMENSAT
3292 if (result < 0 &&
errno == ENOSYS) {
3293# ifdef AT_SYMLINK_NOFOLLOW
3294 try_utimensat_follow = 0;
3306 tvbuf[0].tv_sec = tsp[0].tv_sec;
3307 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3308 tvbuf[1].tv_sec = tsp[1].tv_sec;
3309 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3313 if (v->follow)
return lutimes(path, tvp);
3315 return utimes(path, tvp);
3320#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3328utime_internal(
const char *path,
void *arg)
3332 struct utimbuf utbuf, *utp = NULL;
3334 utbuf.actime = tsp[0].tv_sec;
3335 utbuf.modtime = tsp[1].tv_sec;
3338 return utime(path, utp);
3343utime_internal_i(
int argc,
VALUE *argv,
int follow)
3346 struct timespec tss[2], *tsp = NULL;
3349 args.atime = *argv++;
3350 args.mtime = *argv++;
3352 args.follow = follow;
3354 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3357 if (args.atime == args.mtime)
3364 return apply2files(utime_internal, argc, argv, &args);
3381 return utime_internal_i(argc, argv, FALSE);
3384#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3441 return utime_internal_i(argc, argv, TRUE);
3444#define rb_file_s_lutime rb_f_notimplement
3447#ifdef RUBY_FUNCTION_NAME_STRING
3448# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3450# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3452#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3453NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3455syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3459 const int max_pathlen = MAX_PATH;
3461 const int max_pathlen = MAXPATHLEN;
3472#ifdef RUBY_FUNCTION_NAME_STRING
3473 rb_syserr_fail_path_in(func, e, str);
3475 rb_syserr_fail_path(e, str);
3510 from = rb_str_encode_ospath(from);
3511 to = rb_str_encode_ospath(to);
3514 sys_fail2(from, to);
3519#define rb_file_s_link rb_f_notimplement
3552 from = rb_str_encode_ospath(from);
3553 to = rb_str_encode_ospath(to);
3556 sys_fail2(from, to);
3561#define rb_file_s_symlink rb_f_notimplement
3588 return rb_readlink(path, rb_filesystem_encoding());
3591struct readlink_arg {
3598nogvl_readlink(
void *ptr)
3600 struct readlink_arg *ra = ptr;
3602 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
3606readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
3608 struct readlink_arg ra;
3610 ra.path = RSTRING_PTR(path);
3611 ra.buf = RSTRING_PTR(buf);
3614 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
3625 path = rb_str_encode_ospath(path);
3626 v = rb_enc_str_new(0, size, enc);
3627 while ((rv = readlink_without_gvl(path, v, size)) == size
3629 || (rv < 0 &&
errno == ERANGE)
3638 rb_str_resize(v, 0);
3639 rb_syserr_fail_path(e, path);
3641 rb_str_resize(v, rv);
3646#define rb_file_s_readlink rb_f_notimplement
3650unlink_internal(
const char *path,
void *arg)
3652 return unlink(path);
3672rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
3674 return apply2files(unlink_internal, argc, argv, 0);
3683no_gvl_rename(
void *ptr)
3687 return (
void *)(
VALUE)rename(ra->src, ra->dst);
3708 f = rb_str_encode_ospath(from);
3709 t = rb_str_encode_ospath(to);
3712#if defined __CYGWIN__
3715 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
3720 if (chmod(ra.dst, 0666) == 0 &&
3721 unlink(ra.dst) == 0 &&
3722 rename(ra.src, ra.dst) == 0)
3726 syserr_fail2(e, from, to);
3761 rb_error_arity(argc, 0, 1);
3769#if defined __CYGWIN__ || defined DOSISH
3771#define DOSISH_DRIVE_LETTER
3772#define FILE_ALT_SEPARATOR '\\'
3774#ifdef FILE_ALT_SEPARATOR
3775#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
3777static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
3780#define isdirsep(x) ((x) == '/')
3793# define USE_NTFS_ADS 1
3795# define USE_NTFS_ADS 0
3800#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
3802#define istrailinggarbage(x) 0
3806# define isADS(x) ((x) == ':')
3811#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
3813#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
3814#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
3816#if defined(DOSISH_UNC)
3817#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
3819#define has_unc(buf) 0
3822#ifdef DOSISH_DRIVE_LETTER
3824has_drive_letter(
const char *buf)
3826 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
3851 if (chdir(drive) == 0) {
3852 drvcwd = rb_dir_getwd_ospath();
3864not_same_drive(
VALUE path,
int drive)
3866 const char *p = RSTRING_PTR(path);
3867 if (RSTRING_LEN(path) < 2)
return 0;
3868 if (has_drive_letter(p)) {
3879skiproot(
const char *path,
const char *end)
3881#ifdef DOSISH_DRIVE_LETTER
3882 if (path + 2 <= end && has_drive_letter(path)) path += 2;
3884 while (path < end && isdirsep(*path)) path++;
3885 return (
char *)path;
3889enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
3891 while (s < e && !isdirsep(*s)) {
3892 Inc(s, e, mb_enc, enc);
3897#define nextdirsep rb_enc_path_next
3901 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
3904#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3905#define skipprefix enc_path_skip_prefix
3907#define skipprefix(path, end, mb_enc, enc) (path)
3910enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3912#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3914 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
3916 while (path < end && isdirsep(*path)) path++;
3917 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
3918 path + 2 <= end && !isdirsep(path[1])) {
3919 path = enc_path_next(path + 1, end, mb_enc, enc);
3921 return (
char *)path;
3924#ifdef DOSISH_DRIVE_LETTER
3925 if (path + 2 <= end && has_drive_letter(path))
3926 return (
char *)(path + 2);
3929 return (
char *)path;
3933rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
3935 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
3939skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
3941#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3942 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
3943 while (p < end && isdirsep(*p)) p++;
3946 return skiproot(path, end);
3951rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
3953 return skipprefixroot(path, end, enc);
3957enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3960 while (path < end) {
3961 if (isdirsep(*path)) {
3962 const char *tmp = path++;
3963 while (path < end && isdirsep(*path)) path++;
3964 if (path >= end)
break;
3968 Inc(path, end, mb_enc, enc);
3974rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
3976 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
3980strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3982 if (RB_UNLIKELY(mb_enc)) {
3983 return enc_path_last_separator(path, end, mb_enc, enc);
3986 const char *cursor = end - 1;
3988 while (isdirsep(cursor[0])) {
3992 while (cursor >= path) {
3993 if (isdirsep(cursor[0])) {
3994 while (cursor > path && isdirsep(cursor[-1])) {
3997 return (
char *)cursor;
4005chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
4007 while (path < end) {
4008 if (isdirsep(*path)) {
4009 const char *last = path++;
4010 while (path < end && isdirsep(*path)) path++;
4011 if (path >= end)
return (
char *)last;
4014 Inc(path, end, mb_enc, enc);
4017 return (
char *)path;
4023 if (path < end && isdirsep(*path)) path++;
4024 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
4030 rb_encoding *enc = rb_enc_check_str(path1, path2);
4031 int encidx = rb_enc_to_index(enc);
4032 if (encidx == ENCINDEX_US_ASCII) {
4033 encidx = rb_enc_get_index(path1);
4034 if (encidx == ENCINDEX_US_ASCII)
4035 encidx = rb_enc_get_index(path2);
4036 enc = rb_enc_from_index(encidx);
4043ntfs_tail(
const char *path,
const char *end,
rb_encoding *enc)
4045 bool mb_enc = enc_mbclen_needed(enc);
4046 while (path < end && *path ==
'.') path++;
4047 while (path < end && !isADS(*path)) {
4048 if (istrailinggarbage(*path)) {
4049 const char *last = path++;
4050 while (path < end && istrailinggarbage(*path)) path++;
4051 if (path >= end || isADS(*path))
return (
char *)last;
4053 else if (isdirsep(*path)) {
4054 const char *last = path++;
4055 while (path < end && isdirsep(*path)) path++;
4056 if (path >= end)
return (
char *)last;
4057 if (isADS(*path)) path++;
4060 Inc(path, end, mb_enc, enc);
4063 return (
char *)path;
4067#define BUFCHECK(cond) do {\
4070 do {buflen *= 2;} while (cond);\
4071 rb_str_resize(result, buflen);\
4072 buf = RSTRING_PTR(result);\
4074 pend = buf + buflen;\
4078#define BUFINIT(result, buf, p, pend) do {\
4079 if (!result) { result = rb_usascii_str_new(0, 1); } \
4080 p = buf = RSTRING_PTR(result); \
4081 buflen = RSTRING_LEN(result); \
4082 pend = p + buflen; \
4086# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
4088# define SKIPPATHSEP(p) 1
4091#define BUFCOPY(srcptr, srclen) do { \
4092 const int skip = SKIPPATHSEP(p); \
4093 rb_str_set_len(result, p-buf+skip); \
4094 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
4096 memcpy(p, (srcptr), (srclen)); \
4100#define WITH_ROOTDIFF(stmt) do { \
4101 long rootdiff = root - buf; \
4103 root = buf + rootdiff; \
4107copy_home_path(
VALUE result,
const char *dir)
4113 dirlen = strlen(dir);
4114 rb_str_resize(result, dirlen);
4115 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
4116 encidx = rb_filesystem_encindex();
4117 rb_enc_associate_index(result, encidx);
4118#if defined FILE_ALT_SEPARATOR
4120 bool mb_enc = enc_mbclen_needed(enc);
4121 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
4122 if (*p == FILE_ALT_SEPARATOR) {
4134 VALUE dirname = rb_getpwdirnam_for_login(user);
4135 if (dirname ==
Qnil) {
4136 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4138 const char *dir = RSTRING_PTR(dirname);
4140 extern char *getlogin(
void);
4141 const char *pwPtr = 0;
4143 # define endpwent() ((void)0)
4144 const char *dir, *username = RSTRING_PTR(user);
4155 if ((login = getlogin()) && strcasecmp(username, login) == 0)
4156 dir = pwPtr = getenv(
"HOME");
4158 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
4161 copy_home_path(result, dir);
4167rb_default_home_dir(
VALUE result)
4169 const char *dir = getenv(
"HOME");
4171#if defined HAVE_PWD_H
4185 VALUE login_name = rb_getlogin();
4187# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4192 if (
NIL_P(login_name)) {
4193 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4197 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4198 if (
NIL_P(pw_dir)) {
4199 pw_dir = rb_getpwdiruid();
4200 if (
NIL_P(pw_dir)) {
4201 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4206 copy_home_path(result, RSTRING_PTR(pw_dir));
4207 rb_str_resize(pw_dir, 0);
4212 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4214 return copy_home_path(result, dir);
4220#if NORMALIZE_UTF8PATH
4221 VALUE path = rb_str_normalize_ospath(ptr,
len);
4222 rb_enc_associate(path, fsenc);
4225 return rb_enc_str_new(ptr,
len, fsenc);
4232 if (RB_UNLIKELY(!rb_enc_asciicompat(fsenc) || rb_enc_str_coderange(dirname) !=
ENC_CODERANGE_7BIT)) {
4234 rb_enc_associate(dirname, fsenc);
4238 size_t dirlen = RSTRING_LEN(dirname);
4241 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4242 if (!rb_enc_compatible(fname, dirname)) {
4245 rb_enc_check(fname, dirname);
4246 rb_bug(
"unreachable");
4248 rb_encoding *direnc = fs_enc_check(fname, dirname);
4249 if (direnc != fsenc) {
4256 do {buflen *= 2;}
while (dirlen > buflen);
4257 rb_str_resize(result, buflen);
4258 buf = RSTRING_PTR(result);
4259 memcpy(buf, cwdp, dirlen);
4260 rb_enc_associate(result, *enc);
4261 return buf + dirlen;
4265rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4267 const char *s, *b, *fend;
4268 char *buf, *p, *pend, *root;
4269 size_t buflen, bdiff;
4270 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4273 fend = s + RSTRING_LEN(fname);
4274 enc = rb_str_enc_get(fname);
4275 bool mb_enc = enc_mbclen_needed(enc);
4276 if (!mb_enc &&
RTEST(dname)) {
4277 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4280 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4281 BUFINIT(result, buf, p, pend);
4284 if (s + 1 == fend || isdirsep(s[1])) {
4288 if (++s < fend) ++s;
4289 rb_default_home_dir(result);
4292 s = nextdirsep(b = s, fend, enc);
4295 BUFCHECK(bdiff + userlen >= buflen);
4296 memcpy(p, b, userlen);
4299 rb_enc_associate(result, enc);
4300 rb_home_dir_of(result, result);
4304 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4306 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4307 (
int)userlen, b, fname);
4310 rb_raise(rb_eArgError,
"non-absolute home");
4313 BUFINIT(result, buf, p, pend);
4316#ifdef DOSISH_DRIVE_LETTER
4318 else if (s + 1 < fend && has_drive_letter(s)) {
4319 BUFINIT(result, buf, p, pend);
4321 if (s + 2 < fend && isdirsep(s[2])) {
4324 BUFCHECK(bdiff + 2 >= buflen);
4328 rb_enc_copy(result, fname);
4333 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4334 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4335 BUFINIT(result, buf, p, pend);
4342 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4343 BUFINIT(result, buf, p, pend);
4347 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4350 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4355 else if (s == fend || !rb_is_absolute_path(s)) {
4357 if (!
NIL_P(dname)) {
4359 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4363 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4365 if (RB_UNLIKELY(RSTRING_LEN(result) > RSTRING_LEN(dname))) {
4370 result = resized_result;
4374 rb_enc_associate(result, fs_enc_check(result, fname));
4375 BUFINIT(result, buf, p, pend);
4379 VALUE cwd = rb_dir_getwd_ospath();
4383 char *e = append_fspath(result, fname, rb_dir_getwd_ospath(), &enc, fsenc);
4384 BUFINIT(result, buf, p, pend);
4387#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4388 if (s < fend && isdirsep(*s)) {
4391 p = skipprefix(buf, p, mb_enc, enc);
4395 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4398 BUFINIT(result, buf, p, pend);
4402 do s++;
while (s < fend && isdirsep(*s));
4405 BUFCHECK(bdiff >= buflen);
4406 memset(buf,
'/',
len);
4408 rb_enc_associate(result, fs_enc_check(result, fname));
4410 if (p > buf && p[-1] ==
'/')
4414 BUFCHECK(bdiff + 1 >= buflen);
4419 BUFCHECK(bdiff + 1 >= buflen);
4421 root = skipprefix(buf, p+1, mb_enc, enc);
4434 if (s+1 == fend || isdirsep(*(s+1))) {
4438 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
4448 do ++s;
while (s < fend && istrailinggarbage(*s));
4453#if defined FILE_ALT_SEPARATOR
4454 case FILE_ALT_SEPARATOR:
4468 while (s < fend && istrailinggarbage(*s)) s++;
4478#if defined FILE_ALT_SEPARATOR
4479 case FILE_ALT_SEPARATOR:
4482 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4490 int n = ignored_char_p(s, fend, enc);
4493 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4501 Inc(s, fend, mb_enc, enc);
4509 static const char prime[] =
":$DATA";
4510 enum {prime_len =
sizeof(prime) -1};
4514 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) {
4517 if (isADS(*(s - (prime_len+1)))) {
4520 else if (memchr(b,
':', s - prime_len - b)) {
4529 if (p == skiproot(buf, p + !!*p) - 1) p++;
4533 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s,
"*?")) {
4538 WIN32_FIND_DATAW wfd;
4541#ifdef HAVE_CYGWIN_CONV_PATH
4542 char *w32buf = NULL;
4543 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
4545 char w32buf[MAXPATHLEN];
4549 int lnk_added = 0, is_symlink = 0;
4553 if (lstat_without_gvl(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
4559 path = *buf ? buf :
"/";
4560#ifdef HAVE_CYGWIN_CONV_PATH
4561 bufsize = cygwin_conv_path(flags, path, NULL, 0);
4564 if (lnk_added) bufsize += 4;
4566 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
4571 bufsize = MAXPATHLEN;
4572 if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
4576 if (is_symlink && b == w32buf) {
4578 strlcat(w32buf, p, bufsize);
4580 strlcat(w32buf,
".lnk", bufsize);
4591 if (encidx != ENCINDEX_UTF_8 && !is_ascii_string(result)) {
4592 tmp = rb_str_encode_ospath(result);
4594 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0);
4596 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr,
len);
4598 h = FindFirstFileW(wstr, &wfd);
4600 if (h != INVALID_HANDLE_VALUE) {
4603 len = lstrlenW(wfd.cFileName);
4605 if (lnk_added &&
len > 4 &&
4606 wcscasecmp(wfd.cFileName +
len - 4, L
".lnk") == 0) {
4607 wfd.cFileName[
len -= 4] = L
'\0';
4614 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL);
4615 if (tmp == result) {
4616 BUFCHECK(bdiff +
len >= buflen);
4617 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p,
len + 1, NULL, NULL);
4621 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, RSTRING_PTR(tmp),
len + 1, NULL, NULL);
4622 rb_str_cat_conv_enc_opts(result, bdiff, RSTRING_PTR(tmp),
len,
4623 rb_utf8_encoding(), 0,
Qnil);
4624 BUFINIT(result, buf, p, pend);
4625 rb_str_resize(tmp, 0);
4638 rb_enc_check(fname, result);
4645str_shrink(
VALUE str)
4647 rb_str_resize(str, RSTRING_LEN(str));
4651#define expand_path(fname, dname, abs_mode, long_name, result) \
4652 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
4654#define check_expand_path_args(fname, dname) \
4655 (((fname) = rb_get_path(fname)), \
4656 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
4659file_expand_path_1(
VALUE fname,
long extra_capa)
4662 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, buffer);
4668 check_expand_path_args(fname, dname);
4669 return expand_path(fname, dname, 0, 1,
Qfalse);
4673rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
4675 return expand_path(fname, dname, 0, 0,
Qfalse);
4679rb_file_s_expand_path(
int argc,
const VALUE *argv)
4682 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4728 return rb_file_s_expand_path(c, v);
4734 check_expand_path_args(fname, dname);
4735 return expand_path(fname, dname, 1, 1,
Qfalse);
4739rb_file_s_absolute_path(
int argc,
const VALUE *argv)
4742 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4785 return rb_file_s_absolute_path(c, v);
4813 VALUE path = rb_get_path(fname);
4815 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
4819enum rb_realpath_mode {
4823 RB_REALPATH_MODE_MAX
4827realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
4828 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
4830 const char *pend = unresolved + strlen(unresolved);
4834 while (unresolved < pend) {
4835 const char *testname = unresolved;
4836 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
4837 long testnamelen = unresolved_firstsep - unresolved;
4838 const char *unresolved_nextname = unresolved_firstsep;
4839 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
4840 unresolved_nextname++;
4841 unresolved = unresolved_nextname;
4842 if (testnamelen == 1 && testname[0] ==
'.') {
4844 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
4845 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
4846 bool mb_enc = enc_mbclen_needed(enc);
4847 const char *resolved_str = RSTRING_PTR(*resolvedp);
4848 const char *resolved_names = resolved_str + *prefixlenp;
4849 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
4850 long len = lastsep ? lastsep - resolved_names : 0;
4851 rb_str_resize(*resolvedp, *prefixlenp +
len);
4857 if (*prefixlenp < RSTRING_LEN(testpath))
4859#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4860 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
4861 const char *prefix = RSTRING_PTR(testpath);
4862 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
4867 checkval = rb_hash_aref(loopcheck, testpath);
4868 if (!
NIL_P(checkval)) {
4869 if (checkval ==
ID2SYM(resolving)) {
4870 if (mode == RB_REALPATH_CHECK) {
4874 rb_syserr_fail_path(ELOOP, testpath);
4883 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
4886 if (e == ENOENT && !
NIL_P(fallback)) {
4887 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
4892 if (mode == RB_REALPATH_CHECK)
return -1;
4894 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
4895 rb_syserr_fail_path(e, testpath);
4896 *resolvedp = testpath;
4900 rb_syserr_fail_path(e, testpath);
4904 if (S_ISLNK(sbuf.st_mode)) {
4907 const char *link_prefix, *link_names;
4908 long link_prefixlen;
4909 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
4910 link = rb_readlink(testpath, enc);
4911 link_prefix = RSTRING_PTR(link);
4912 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
4913 link_prefixlen = link_names - link_prefix;
4914 if (link_prefixlen > 0) {
4918 tmpenc = fs_enc_check(*resolvedp, link);
4921 *prefixlenp = link_prefixlen;
4923 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
4924 loopcheck, mode, !*unresolved_firstsep))
4933 rb_hash_aset(loopcheck, s, s);
4934 *resolvedp = testpath;
4943rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4947 VALUE unresolved_path;
4952 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
4953 char *ptr, *prefixptr = NULL, *pend;
4958 if (!
NIL_P(basedir)) {
4963 enc = rb_enc_get(unresolved_path);
4964 unresolved_path = TO_OSPATH(unresolved_path);
4966 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
4967 if (ptr != path_names) {
4968 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
4972 if (!
NIL_P(basedir)) {
4974 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
4975 if (ptr != basedir_names) {
4981 curdir = rb_dir_getwd_ospath();
4983 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
4988 pend = prefixptr + prefixlen;
4989 bool mb_enc = enc_mbclen_needed(enc);
4990 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
4992 prefixlen = ++ptr - prefixptr;
4995#ifdef FILE_ALT_SEPARATOR
4996 while (prefixptr < ptr) {
4997 if (*prefixptr == FILE_ALT_SEPARATOR) {
5000 Inc(prefixptr, pend, mb_enc, enc);
5004 switch (rb_enc_to_index(enc)) {
5005 case ENCINDEX_ASCII_8BIT:
5006 case ENCINDEX_US_ASCII:
5007 rb_enc_associate_index(resolved, rb_filesystem_encindex());
5010 loopcheck = rb_hash_new();
5012 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
5015 if (basedir_names) {
5016 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
5019 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
5022 if (origenc && origenc != rb_enc_get(resolved)) {
5024 rb_enc_associate(resolved, origenc);
5036static VALUE rb_file_join(
long argc,
VALUE *args);
5038#ifndef HAVE_REALPATH
5040rb_check_realpath_emulate_try(
VALUE arg)
5043 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
5047rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
5051#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
5052 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
5054# include <sys/syslimits.h>
5055# define NEEDS_REALPATH_BUFFER 1
5059rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
5062 VALUE unresolved_path;
5063 char *resolved_ptr = NULL;
5065# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
5066 char resolved_buffer[PATH_MAX];
5068 char *
const resolved_buffer = NULL;
5071 if (mode == RB_REALPATH_DIR) {
5072 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5076 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
5077 VALUE paths[2] = {basedir, unresolved_path};
5078 unresolved_path = rb_file_join(2, paths);
5080 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
5082 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
5091 if (
errno == ENOTSUP ||
5093 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
5094 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5097 if (mode == RB_REALPATH_CHECK) {
5100 rb_sys_fail_path(unresolved_path);
5102 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
5103# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
5107# if !defined(__linux__) && !defined(__APPLE__)
5111 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
5112 if (mode == RB_REALPATH_CHECK) {
5115 rb_sys_fail_path(unresolved_path);
5119 if (origenc && origenc != rb_enc_get(resolved)) {
5123 rb_enc_associate(resolved, origenc);
5126 if (is_broken_string(resolved)) {
5127 rb_enc_associate(resolved, rb_filesystem_encoding());
5128 if (is_broken_string(resolved)) {
5129 rb_enc_associate(resolved, rb_ascii8bit_encoding());
5136 if (mode == RB_REALPATH_CHECK) {
5140 arg[2] = (
VALUE)origenc;
5143 rb_check_realpath_emulate_rescue,
Qnil);
5146 return rb_check_realpath_emulate(basedir, path, origenc, mode);
5152rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
5154 const enum rb_realpath_mode mode =
5155 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
5156 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
5162 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
5179rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
5182 VALUE path = argv[0];
5184 return rb_realpath_internal(basedir, path, 1);
5200rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
5203 VALUE path = argv[0];
5205 return rb_realpath_internal(basedir, path, 0);
5209rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
5213 const char *s, *last;
5215 if (!e || !l2)
return 0;
5217 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
5218 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
5219 if (c ==
'.')
return l0;
5224 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
5229 if (l1 < l2)
return l1;
5232 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
5233#if CASEFOLD_FILESYSTEM
5234#define fncomp strncasecmp
5236#define fncomp strncmp
5238 if (fncomp(s, e, l2) == 0) {
5244static inline const char *
5245enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
5247 const char *p, *q, *e, *end;
5250 long len = (alllen ? (size_t)*alllen : strlen(name));
5257 name = skipprefix(name, end, mb_enc, enc);
5258#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5259 const char *root = name;
5262 while (name < end && isdirsep(*name)) {
5269#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5273#ifdef DOSISH_DRIVE_LETTER
5274 else if (*p ==
':') {
5287 p = strrdirsep(name, end, mb_enc, enc);
5292 while (isdirsep(*p)) {
5297 n = ntfs_tail(p, end, enc) - p;
5299 n = chompdirsep(p, end, mb_enc, enc) - p;
5301 for (q = p; q - p < n && *q ==
'.'; q++);
5302 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5303 if (*q ==
'.') e = q;
5323ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5325 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5369 const char *name, *p, *fp = 0;
5375 CheckPath(fname, name);
5379 check_path_encoding(fext);
5381 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5382 enc = rb_str_enc_get(fname);
5385 n = RSTRING_LEN(fname);
5386 if (n <= 0 || !*name) {
5387 return rb_enc_str_new(0, 0, enc);
5390 bool mb_enc = enc_mbclen_needed(enc);
5391 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5397 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5402 if (f == RSTRING_LEN(fname)) {
5407 return rb_enc_str_new(p, f, enc);
5410static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5432rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5438 return rb_file_dirname_n(argv[0], n);
5444 return rb_file_dirname_n(fname, 1);
5448rb_file_dirname_n(
VALUE fname,
int n)
5450 const char *name, *root, *p, *end;
5453 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5454 CheckPath(fname, name);
5455 end = name + RSTRING_LEN(fname);
5457 bool mb_enc = !rb_str_enc_fastpath(fname);
5460 root = skiproot(name, end);
5462 if (root > name + 1 && isdirsep(*name))
5463 root = skipprefix(name = root - 2, end, mb_enc, enc);
5465 if (root > name + 1)
5468 if (n > (end - root + 1) / 2) {
5474 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5483 return rb_enc_str_new(
".", 1, enc);
5485#ifdef DOSISH_DRIVE_LETTER
5486 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5487 const char *top = skiproot(name + 2, end);
5488 dirname = rb_enc_str_new(name, 3, enc);
5493 dirname = rb_enc_str_new(name, p - name, enc);
5494#ifdef DOSISH_DRIVE_LETTER
5495 if (root == name + 2 && p == root && name[1] ==
':')
5501static inline const char *
5502enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5504 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5506 p = strrdirsep(name, end, mb_enc, enc);
5510 do name = ++p;
while (isdirsep(*p));
5513 while (*p && *p ==
'.') p++;
5515 if (*p ==
'.' || istrailinggarbage(*p)) {
5517 const char *last = p++, *dot = last;
5518 while (istrailinggarbage(*p)) {
5519 if (*p ==
'.') dot = p;
5522 if (!*p || isADS(*p)) {
5526 if (*last ==
'.' || dot > last) e = dot;
5533 else if (isADS(*p)) {
5537 else if (isdirsep(*p))
5539 Inc(p, end, mb_enc, enc);
5544 if (!e || e == name)
5569 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
5616 CheckPath(fname, name);
5617 long len = RSTRING_LEN(fname);
5620 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
5623 bool mb_enc = !rb_str_enc_fastpath(fname);
5626 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
5627 return rb_enc_str_new(ext,
len, enc);
5660 return rb_get_path(fname);
5684file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
5686 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
5687 return rb_file_join_ary(arg);
5691rb_file_join_ary(
VALUE ary)
5695 const char *name, *tail;
5705 check_path_encoding(tmp);
5706 len += RSTRING_LEN(tmp);
5714 RBASIC_CLEAR_CLASS(result);
5717 switch (OBJ_BUILTIN_TYPE(tmp)) {
5719 if (!checked) check_path_encoding(tmp);
5724 rb_raise(rb_eArgError,
"recursive array");
5736 rb_enc_copy(result, tmp);
5739 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
5740 if (RSTRING_LEN(tmp) > 0 && isdirsep(RSTRING_PTR(tmp)[0])) {
5743 else if (tail == name +
len) {
5747 enc = fs_enc_check(result, tmp);
5749 rb_enc_associate(result, enc);
5757rb_file_join_fastpath(
long argc,
VALUE *args)
5762 for (i = 0; i < argc; i++) {
5763 VALUE tmp = args[i];
5765 size += RSTRING_LEN(tmp);
5778 const char *name = RSTRING_PTR(result);
5779 for (i = 1; i < argc; i++) {
5780 VALUE tmp = args[i];
5781 long len = RSTRING_LEN(result);
5787 if (tmp_len > 0 && isdirsep(tmp_s[0])) {
5790 while (chomp > 0 && isdirsep(name[chomp - 1])) {
5795 else if (
len < 1 || !isdirsep(name[
len - 1])) {
5802 rb_enc_associate(result, new_enc);
5803 encidx = rb_enc_to_index(new_enc);
5809 rb_str_null_check(result);
5814rb_file_join(
long argc,
VALUE *args)
5816 if (RB_UNLIKELY(argc == 0)) {
5820 VALUE result = rb_file_join_fastpath(argc, args);
5821 if (RB_LIKELY(result)) {
5841rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
5843 return rb_file_join(argc, argv);
5846#if defined(HAVE_TRUNCATE)
5847struct truncate_arg {
5853nogvl_truncate(
void *ptr)
5855 struct truncate_arg *ta = ptr;
5856 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
5883 struct truncate_arg ta;
5888 path = rb_str_encode_ospath(path);
5891 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
5893 rb_sys_fail_path(path);
5897#define rb_file_s_truncate rb_f_notimplement
5900#if defined(HAVE_FTRUNCATE)
5901struct ftruncate_arg {
5907nogvl_ftruncate(
void *ptr)
5909 struct ftruncate_arg *fa = ptr;
5911 return (
VALUE)ftruncate(fa->fd, fa->pos);
5932 struct ftruncate_arg fa;
5939 rb_io_flush_raw(obj, 0);
5941 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
5942 rb_sys_fail_path(fptr->
pathv);
5947#define rb_file_truncate rb_f_notimplement
5964#include <winerror.h>
5968rb_thread_flock(
void *data)
5971 int old_errno =
errno;
5973 int *op = data, ret = flock(op[0], op[1]);
5976 if (GetLastError() == ERROR_NOT_LOCKED) {
6034 op[1] = op1 =
NUM2INT(operation);
6039 rb_io_flush_raw(obj, 0);
6041 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
6046#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
6049 if (op1 & LOCK_NB)
return Qfalse;
6052 time.tv_usec = 100 * 1000;
6058#if defined(ERESTART)
6064 rb_syserr_fail_path(e, fptr->
pathv);
6071test_check(
int n,
int argc,
VALUE *argv)
6077 for (i=1; i<n; i++) {
6084#define CHECK(n) test_check((n), argc, argv)
6176 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
6180 return rb_file_blockdev_p(0, argv[1]);
6183 return rb_file_chardev_p(0, argv[1]);
6186 return rb_file_directory_p(0, argv[1]);
6189 return rb_file_exist_p(0, argv[1]);
6192 return rb_file_file_p(0, argv[1]);
6195 return rb_file_sgid_p(0, argv[1]);
6198 return rb_file_grpowned_p(0, argv[1]);
6201 return rb_file_sticky_p(0, argv[1]);
6204 return rb_file_symlink_p(0, argv[1]);
6207 return rb_file_owned_p(0, argv[1]);
6210 return rb_file_rowned_p(0, argv[1]);
6213 return rb_file_pipe_p(0, argv[1]);
6216 return rb_file_readable_p(0, argv[1]);
6219 return rb_file_readable_real_p(0, argv[1]);
6222 return rb_file_size_p(0, argv[1]);
6225 return rb_file_socket_p(0, argv[1]);
6228 return rb_file_suid_p(0, argv[1]);
6231 return rb_file_writable_p(0, argv[1]);
6234 return rb_file_writable_real_p(0, argv[1]);
6237 return rb_file_executable_p(0, argv[1]);
6240 return rb_file_executable_real_p(0, argv[1]);
6243 return rb_file_zero_p(0, argv[1]);
6247 if (strchr(
"MAC", cmd)) {
6249 VALUE fname = argv[1];
6252 if (
rb_stat(fname, &st) == -1) {
6255 rb_syserr_fail_path(e, fname);
6260 return stat_atime(&st);
6262 return stat_mtime(&st);
6264 return stat_ctime(&st);
6270 return rb_file_identical_p(0, argv[1], argv[2]);
6273 if (strchr(
"=<>", cmd)) {
6274 struct stat st1, st2;
6281 t1 = stat_mtimespec(&st1);
6282 t2 = stat_mtimespec(&st2);
6286 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6290 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6291 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6295 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6296 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6303 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6306 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6360rb_stat_s_alloc(
VALUE klass)
6381 fname = rb_str_encode_ospath(fname);
6383 rb_sys_fail_path(fname);
6390 rb_st->initialized =
true;
6407 *copy_rb_st = *orig_rb_st;
6449rb_stat_ftype(
VALUE obj)
6451 return rb_file_ftype(get_stat(obj)->ST_(mode));
6468 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6484 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
6516 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
6537 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
6560 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
6581 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
6599rb_stat_owned(
VALUE obj)
6601 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
6606rb_stat_rowned(
VALUE obj)
6608 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
6629rb_stat_grpowned(
VALUE obj)
6632 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
6651 rb_io_stat_data *st = get_stat(obj);
6654 if (geteuid() == 0)
return Qtrue;
6657 if (rb_stat_owned(obj))
6658 return RBOOL(st->ST_(mode) & S_IRUSR);
6661 if (rb_stat_grpowned(obj))
6662 return RBOOL(st->ST_(mode) & S_IRGRP);
6665 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6684 rb_io_stat_data *st = get_stat(obj);
6687 if (getuid() == 0)
return Qtrue;
6690 if (rb_stat_rowned(obj))
6691 return RBOOL(st->ST_(mode) & S_IRUSR);
6694 if (rb_group_member(get_stat(obj)->ST_(gid)))
6695 return RBOOL(st->ST_(mode) & S_IRGRP);
6698 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6717rb_stat_wr(
VALUE obj)
6720 rb_io_stat_data *st = get_stat(obj);
6721 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
6722 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6742 rb_io_stat_data *st = get_stat(obj);
6745 if (geteuid() == 0)
return Qtrue;
6748 if (rb_stat_owned(obj))
6749 return RBOOL(st->ST_(mode) & S_IWUSR);
6752 if (rb_stat_grpowned(obj))
6753 return RBOOL(st->ST_(mode) & S_IWGRP);
6756 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6775 rb_io_stat_data *st = get_stat(obj);
6778 if (getuid() == 0)
return Qtrue;
6781 if (rb_stat_rowned(obj))
6782 return RBOOL(st->ST_(mode) & S_IWUSR);
6785 if (rb_group_member(get_stat(obj)->ST_(gid)))
6786 return RBOOL(st->ST_(mode) & S_IWGRP);
6789 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6808rb_stat_ww(
VALUE obj)
6811 rb_io_stat_data *st = get_stat(obj);
6812 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
6813 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6835 rb_io_stat_data *st = get_stat(obj);
6838 if (geteuid() == 0) {
6839 return RBOOL(st->ST_(mode) & S_IXUGO);
6843 if (rb_stat_owned(obj))
6844 return RBOOL(st->ST_(mode) & S_IXUSR);
6847 if (rb_stat_grpowned(obj))
6848 return RBOOL(st->ST_(mode) & S_IXGRP);
6851 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6867 rb_io_stat_data *st = get_stat(obj);
6870 if (getuid() == 0) {
6871 return RBOOL(st->ST_(mode) & S_IXUGO);
6875 if (rb_stat_rowned(obj))
6876 return RBOOL(st->ST_(mode) & S_IXUSR);
6879 if (rb_group_member(get_stat(obj)->ST_(gid)))
6880 return RBOOL(st->ST_(mode) & S_IXGRP);
6883 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6902 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
6920 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
6939 rb_off_t size = get_stat(obj)->ST_(size);
6941 if (size == 0)
return Qnil;
6957rb_stat_suid(
VALUE obj)
6960 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
6978rb_stat_sgid(
VALUE obj)
6981 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
6999rb_stat_sticky(
VALUE obj)
7002 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
7007#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
7008#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
7019nogvl_mkfifo(
void *ptr)
7021 struct mkfifo_arg *ma = ptr;
7023 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
7040 struct mkfifo_arg ma;
7049 path = rb_str_encode_ospath(path);
7050 ma.path = RSTRING_PTR(path);
7051 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
7052 rb_sys_fail_path(path);
7057#define rb_file_s_mkfifo rb_f_notimplement
7060static VALUE rb_mFConst;
7063rb_file_const(
const char *name,
VALUE value)
7065 rb_define_const(rb_mFConst, name, value);
7069rb_is_absolute_path(
const char *path)
7071#ifdef DOSISH_DRIVE_LETTER
7072 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
7075 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
7078 if (path[0] ==
'/')
return 1;
7084ruby_is_fd_loadable(
int fd)
7091 if (fstat(fd, &st) < 0)
7094 if (S_ISREG(st.st_mode))
7097 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
7100 if (S_ISDIR(st.st_mode))
7111rb_file_load_ok(
const char *path)
7118 int mode = (O_RDONLY |
7119#if defined O_NONBLOCK
7121#elif defined O_NDELAY
7127 if (!rb_gc_for_fd(
errno))
return 0;
7129 if (fd < 0)
return 0;
7132 ret = ruby_is_fd_loadable(fd);
7139is_explicit_relative(
const char *path)
7141 if (*path++ !=
'.')
return 0;
7142 if (*path ==
'.') path++;
7143 return isdirsep(*path);
7149 int encidx = rb_enc_get_index(orig);
7150 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
7151 encidx = rb_filesystem_encindex();
7152 rb_enc_associate_index(path, encidx);
7160nav_component_p(
const char *s,
const char *send)
7162 if ((send - s) >= 2 && s[0] ==
'.') {
7163 return s[1] ==
'.' || isdirsep(s[1]);
7169fname_need_expansion_p(
VALUE fname)
7171 const char *s = RSTRING_PTR(fname);
7172 const long len = RSTRING_LEN(fname);
7173 const char *send = s +
len;
7175 if (nav_component_p(s, send)) {
7180 bool mbenc = enc_mbclen_needed(enc);
7182 s = enc_path_next(s, send, mbenc, enc);
7184 if (nav_component_p(s, send)) {
7188 s = enc_path_next(s, send, mbenc, enc);
7194expand_feature(
VALUE fname,
VALUE dname,
VALUE buffer,
bool need_expansion)
7196 long dname_len = RSTRING_LEN(dname);
7197 const char *dname_ptr = RSTRING_PTR(dname);
7201 if (need_expansion || dname_ptr[0] ==
'~') {
7202 rb_file_expand_path_internal(fname, dname, 0, 0, buffer);
7207 if (!isdirsep(dname_ptr[dname_len - 1])) {
7216rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
7219 VALUE fname = *filep;
7223 if (!ext[0])
return 0;
7226 fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7227 f = RSTRING_PTR(fname);
7232 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7233 if (!expanded) fname = file_expand_path_1(fname, DLEXT_MAXLEN);
7234 fnlen = RSTRING_LEN(fname);
7235 for (i=0; ext[i]; i++) {
7237 if (rb_file_load_ok(RSTRING_PTR(fname))) {
7238 *filep = copy_path_class(fname, *filep);
7246 long expanded_load_path_maxlen;
7247 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7248 if (!load_path)
return 0;
7251 RBASIC_CLEAR_CLASS(fname);
7252 fnlen = RSTRING_LEN(fname);
7253 bool need_expansion = fname_need_expansion_p(fname);
7256 rb_enc_associate_index(tmp, rb_usascii_encindex());
7258 for (j=0; ext[j]; j++) {
7260 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
7262 if (!RSTRING_LEN(dname))
continue;
7263 expand_feature(fname, dname, tmp, need_expansion);
7265 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7266 *filep = copy_path_class(tmp, *filep);
7272 rb_str_resize(tmp, 0);
7285 path = copy_path_class(file_expand_path_1(path, 0), path);
7286 f = RSTRING_PTR(path);
7290 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
7291 if (!rb_file_load_ok(f))
return 0;
7293 path = copy_path_class(file_expand_path_1(path, 0), path);
7297 long expanded_load_path_maxlen;
7298 VALUE load_path = rb_get_expanded_load_path(&expanded_load_path_maxlen);
7301 bool need_expansion = fname_need_expansion_p(path);
7303 rb_enc_associate_index(tmp, rb_usascii_encindex());
7304 for (
long i = 0; i <
RARRAY_LEN(load_path); i++) {
7306 if (!RSTRING_LEN(dname))
continue;
7307 expand_feature(path, dname, tmp, need_expansion);
7309 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
7310 return copy_path_class(tmp, path);
7313 rb_str_resize(tmp, 0);
7321#define define_filetest_function(name, func, argc) do { \
7322 rb_define_module_function(rb_mFileTest, name, func, argc); \
7323 rb_define_singleton_method(rb_cFile, name, func, argc); \
7326const char ruby_null_device[] =
7329#elif defined AMIGA || defined __amigaos__
8133#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
8134 rb_CFString_class_initialize_before_fork();
8142 define_filetest_function(
"directory?", rb_file_directory_p, 1);
8143 define_filetest_function(
"exist?", rb_file_exist_p, 1);
8144 define_filetest_function(
"readable?", rb_file_readable_p, 1);
8145 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
8146 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
8147 define_filetest_function(
"writable?", rb_file_writable_p, 1);
8148 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
8149 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
8150 define_filetest_function(
"executable?", rb_file_executable_p, 1);
8151 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
8152 define_filetest_function(
"file?", rb_file_file_p, 1);
8153 define_filetest_function(
"zero?", rb_file_zero_p, 1);
8154 define_filetest_function(
"empty?", rb_file_zero_p, 1);
8155 define_filetest_function(
"size?", rb_file_size_p, 1);
8156 define_filetest_function(
"size", rb_file_s_size, 1);
8157 define_filetest_function(
"owned?", rb_file_owned_p, 1);
8158 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
8160 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
8161 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
8162 define_filetest_function(
"socket?", rb_file_socket_p, 1);
8164 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
8165 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
8167 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
8168 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
8169 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
8171 define_filetest_function(
"identical?", rb_file_identical_p, 2);
8209 separator = rb_fstring_lit(
"/");
8211 rb_define_const(
rb_cFile,
"Separator", separator);
8213 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
8562 rb_mFConst = rb_define_module_under(
rb_cFile,
"Constants");
8565 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
8567 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
8569 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
8571 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
8573 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
8575 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
8576#if defined(O_NDELAY) || defined(O_NONBLOCK)
8578# define O_NONBLOCK O_NDELAY
8581 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
8584 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
8587 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
8593 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
8594#ifndef O_SHARE_DELETE
8595# define O_SHARE_DELETE 0
8598 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
8601 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
8605 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
8609 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
8613 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
8617 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
8621 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
8625 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
8629 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
8631 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
8633 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
8635 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
8638 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.