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);
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);
1581 fname = rb_str_encode_ospath(fname);
1582 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1583 rb_sys_fail_path(fname);
1585 return rb_statx_new(&st);
1587 return rb_file_s_stat(klass, fname);
1606rb_file_lstat(
VALUE obj)
1615 path = rb_str_encode_ospath(fptr->
pathv);
1616 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1617 rb_sys_fail_path(fptr->
pathv);
1619 return rb_statx_new(&st);
1621 return rb_io_stat(obj);
1626rb_group_member(GETGROUPS_T gid)
1628#if defined(_WIN32) || !defined(HAVE_GETGROUPS)
1637 if (getgid() == gid || getegid() == gid)
1640 groups = getgroups(0, NULL);
1641 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1642 anum = getgroups(groups, gary);
1643 while (--anum >= 0) {
1644 if (gary[anum] == gid) {
1657# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1660#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1661#define USE_GETEUID 1
1666eaccess(
const char *path,
int mode)
1675 if (getuid() == euid && getgid() == getegid())
1676 return access(path, mode);
1678 if (STAT(path, &st) < 0)
1688 if (st.st_mode & S_IXUGO)
1694 if (st.st_uid == euid)
1696 else if (rb_group_member(st.st_gid))
1699 if ((
int)(st.st_mode & mode) == mode)
return 0;
1703 return access(path, mode);
1714nogvl_eaccess(
void *ptr)
1718 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1722rb_eaccess(
VALUE fname,
int mode)
1727 fname = rb_str_encode_ospath(fname);
1731 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1735nogvl_access(
void *ptr)
1739 return (
void *)(
VALUE)access(aa->path, aa->mode);
1743rb_access(
VALUE fname,
int mode)
1748 fname = rb_str_encode_ospath(fname);
1752 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1787# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1793 if (S_ISDIR(st.st_mode))
return Qtrue;
1814# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1820 if (S_ISFIFO(st.st_mode))
return Qtrue;
1843# define S_ISLNK(m) _S_ISLNK(m)
1846# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1849# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1859 fname = rb_str_encode_ospath(fname);
1861 if (S_ISLNK(st.st_mode))
return Qtrue;
1884# define S_ISSOCK(m) _S_ISSOCK(m)
1887# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1890# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1900 if (S_ISSOCK(st.st_mode))
return Qtrue;
1922# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1924# define S_ISBLK(m) (0)
1932 if (S_ISBLK(st.st_mode))
return Qtrue;
1952# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
1958 if (S_ISCHR(st.st_mode))
return Qtrue;
1997 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2012rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2014 return RBOOL(rb_access(fname, R_OK) >= 0);
2018# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2022# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2042rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2048 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2049 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2069 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2084rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2086 return RBOOL(rb_access(fname, W_OK) >= 0);
2106rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2112 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2113 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2137 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2156rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2158 return RBOOL(rb_access(fname, X_OK) >= 0);
2162# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2183 return RBOOL(S_ISREG(st.st_mode));
2202 return RBOOL(st.st_size == 0);
2221 if (st.st_size == 0)
return Qnil;
2242 return RBOOL(st.st_uid == geteuid());
2251 return RBOOL(st.st_uid == getuid());
2272 if (rb_group_member(st.st_gid))
return Qtrue;
2277#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2279check3rdbyte(
VALUE fname,
int mode)
2284 return RBOOL(st.st_mode & mode);
2301 return check3rdbyte(fname, S_ISUID);
2320 return check3rdbyte(fname, S_ISGID);
2339 return check3rdbyte(fname, S_ISVTX);
2368 struct stat st1, st2;
2372 if (st1.st_dev != st2.st_dev)
return Qfalse;
2373 if (st1.st_ino != st2.st_ino)
return Qfalse;
2377 return rb_w32_file_identical_p(fname1, fname2);
2395 if (
rb_stat(fname, &st) < 0) {
2398 rb_syserr_fail_path(e, fname);
2404rb_file_ftype(mode_t mode)
2408 if (S_ISREG(mode)) {
2411 else if (S_ISDIR(mode)) {
2414 else if (S_ISCHR(mode)) {
2415 t =
"characterSpecial";
2418 else if (S_ISBLK(mode)) {
2423 else if (S_ISFIFO(mode)) {
2428 else if (S_ISLNK(mode)) {
2433 else if (S_ISSOCK(mode)) {
2441 return rb_fstring_cstr(t);
2465 fname = rb_str_encode_ospath(fname);
2467 rb_sys_fail_path(fname);
2470 return rb_file_ftype(st.st_mode);
2501 if (
rb_stat(fname, &st) < 0) {
2504 rb_syserr_fail_path(e, fname);
2506 return stat_time(stat_atimespec(&st));
2530rb_file_atime(
VALUE obj)
2536 if (fstat(fptr->
fd, &st) == -1) {
2537 rb_sys_fail_path(fptr->
pathv);
2539 return stat_time(stat_atimespec(&st));
2559 if (
rb_stat(fname, &st) < 0) {
2562 rb_syserr_fail_path(e, fname);
2564 return stat_time(stat_mtimespec(&st));
2578rb_file_mtime(
VALUE obj)
2584 if (fstat(fptr->
fd, &st) == -1) {
2585 rb_sys_fail_path(fptr->
pathv);
2587 return stat_time(stat_mtimespec(&st));
2611 if (
rb_stat(fname, &st) < 0) {
2614 rb_syserr_fail_path(e, fname);
2616 return stat_time(stat_ctimespec(&st));
2633rb_file_ctime(
VALUE obj)
2639 if (fstat(fptr->
fd, &st) == -1) {
2640 rb_sys_fail_path(fptr->
pathv);
2642 return stat_time(stat_ctimespec(&st));
2645#if defined(HAVE_STAT_BIRTHTIME)
2670 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
2673 rb_syserr_fail_path(e, fname);
2675 return statx_birthtime(&st);
2678# define rb_file_s_birthtime rb_f_notimplement
2681#if defined(HAVE_STAT_BIRTHTIME)
2702rb_file_birthtime(
VALUE obj)
2708 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
2709 rb_sys_fail_path(fptr->
pathv);
2711 return statx_birthtime(&st);
2714# define rb_file_birthtime rb_f_notimplement
2726 rb_io_flush_raw(file, 0);
2729 if (fstat(fptr->
fd, &st) == -1) {
2730 rb_sys_fail_path(fptr->
pathv);
2751file_size(
VALUE self)
2753 return OFFT2NUM(rb_file_size(self));
2762nogvl_chmod(
void *ptr)
2765 int ret = chmod(data->path, data->mode);
2766 return (
void *)(
VALUE)ret;
2770rb_chmod(
const char *path, mode_t mode)
2776 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
2780chmod_internal(
const char *path,
void *mode)
2782 return chmod(path, *(mode_t *)mode);
2806 return apply2files(chmod_internal, argc, argv, &mode);
2810struct nogvl_fchmod_data {
2816io_blocking_fchmod(
void *ptr)
2818 struct nogvl_fchmod_data *data = ptr;
2819 int ret = fchmod(data->fd, data->mode);
2824rb_fchmod(
struct rb_io* io, mode_t mode)
2827 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
2828 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
2850#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2858 if (rb_fchmod(fptr, mode) == -1) {
2859 if (HAVE_FCHMOD ||
errno != ENOSYS)
2860 rb_sys_fail_path(fptr->
pathv);
2863 if (!HAVE_FCHMOD)
return INT2FIX(0);
2866#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2868 path = rb_str_encode_ospath(fptr->
pathv);
2869 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
2870 rb_sys_fail_path(fptr->
pathv);
2876#if defined(HAVE_LCHMOD)
2878lchmod_internal(
const char *path,
void *mode)
2880 return lchmod(path, *(mode_t *)mode);
2901 return apply2files(lchmod_internal, argc, argv, &mode);
2904#define rb_file_s_lchmod rb_f_notimplement
2907static inline rb_uid_t
2911 return (rb_uid_t)-1;
2916static inline rb_gid_t
2920 return (rb_gid_t)-1;
2931chown_internal(
const char *path,
void *arg)
2934 return chown(path, args->owner, args->group);
2958 arg.owner = to_uid(*argv++);
2959 arg.group = to_gid(*argv++);
2961 return apply2files(chown_internal, argc, argv, &arg);
2973nogvl_chown(
void *ptr)
2976 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
2980rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
2983 .as = {.path = path},
2984 .new = {.owner = owner, .group = group},
2986 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
2991nogvl_fchown(
void *ptr)
2994 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
2998rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
3003 .new = {.owner = owner, .group = group},
3005 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3039 path = rb_str_encode_ospath(fptr->
pathv);
3040 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3041 rb_sys_fail_path(fptr->
pathv);
3043 if (rb_fchown(fptr->
fd, o, g) == -1)
3044 rb_sys_fail_path(fptr->
pathv);
3050#if defined(HAVE_LCHOWN)
3052lchown_internal(
const char *path,
void *arg)
3055 return lchown(path, args->owner, args->group);
3075 arg.owner = to_uid(*argv++);
3076 arg.group = to_gid(*argv++);
3078 return apply2files(lchown_internal, argc, argv, &arg);
3081#define rb_file_s_lchown rb_f_notimplement
3091NORETURN(
static void utime_failed(
struct apply_arg *));
3097 VALUE path = aa->fn[aa->i].path;
3100 if (ua->tsp && e == EINVAL) {
3103 VALUE atime = ua->atime;
3104 VALUE mtime = ua->mtime;
3106 if (!
NIL_P(atime)) {
3109 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3112 if (
NIL_P(a)) e[0] = m;
3128 rb_syserr_fail_path(e, path);
3132#if defined(HAVE_UTIMES)
3134# if !defined(HAVE_UTIMENSAT)
3136# elif defined(__APPLE__) && \
3137 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3139# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3140typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3144static inline utimensat_func *
3151# define utimensat rb_utimensat()
3158utime_internal(
const char *path,
void *arg)
3161 const struct timespec *tsp = v->tsp;
3162 struct timeval tvbuf[2], *tvp = NULL;
3164#if defined(HAVE_UTIMENSAT)
3165# if defined(__APPLE__)
3166 const int try_utimensat = utimensat != NULL;
3167 const int try_utimensat_follow = utimensat != NULL;
3169# define TRY_UTIMENSAT 1
3170 static int try_utimensat = 1;
3171# ifdef AT_SYMLINK_NOFOLLOW
3172 static int try_utimensat_follow = 1;
3174 const int try_utimensat_follow = 0;
3179 if (v->follow ? try_utimensat_follow : try_utimensat) {
3180# ifdef AT_SYMLINK_NOFOLLOW
3182 flags = AT_SYMLINK_NOFOLLOW;
3186 int result = utimensat(AT_FDCWD, path, tsp, flags);
3187# ifdef TRY_UTIMENSAT
3188 if (result < 0 &&
errno == ENOSYS) {
3189# ifdef AT_SYMLINK_NOFOLLOW
3190 try_utimensat_follow = 0;
3202 tvbuf[0].tv_sec = tsp[0].tv_sec;
3203 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3204 tvbuf[1].tv_sec = tsp[1].tv_sec;
3205 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3209 if (v->follow)
return lutimes(path, tvp);
3211 return utimes(path, tvp);
3216#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3224utime_internal(
const char *path,
void *arg)
3228 struct utimbuf utbuf, *utp = NULL;
3230 utbuf.actime = tsp[0].tv_sec;
3231 utbuf.modtime = tsp[1].tv_sec;
3234 return utime(path, utp);
3239utime_internal_i(
int argc,
VALUE *argv,
int follow)
3242 struct timespec tss[2], *tsp = NULL;
3245 args.atime = *argv++;
3246 args.mtime = *argv++;
3248 args.follow = follow;
3250 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3253 if (args.atime == args.mtime)
3260 return apply2files(utime_internal, argc, argv, &args);
3277 return utime_internal_i(argc, argv, FALSE);
3280#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3296 return utime_internal_i(argc, argv, TRUE);
3299#define rb_file_s_lutime rb_f_notimplement
3302#ifdef RUBY_FUNCTION_NAME_STRING
3303# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3305# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3307#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3308NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3310syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3314 const int max_pathlen = MAX_PATH;
3316 const int max_pathlen = MAXPATHLEN;
3327#ifdef RUBY_FUNCTION_NAME_STRING
3328 rb_syserr_fail_path_in(func, e, str);
3330 rb_syserr_fail_path(e, str);
3352 from = rb_str_encode_ospath(from);
3353 to = rb_str_encode_ospath(to);
3356 sys_fail2(from, to);
3361#define rb_file_s_link rb_f_notimplement
3382 from = rb_str_encode_ospath(from);
3383 to = rb_str_encode_ospath(to);
3386 sys_fail2(from, to);
3391#define rb_file_s_symlink rb_f_notimplement
3409 return rb_readlink(path, rb_filesystem_encoding());
3412struct readlink_arg {
3419nogvl_readlink(
void *ptr)
3421 struct readlink_arg *ra = ptr;
3423 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
3427readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
3429 struct readlink_arg ra;
3431 ra.path = RSTRING_PTR(path);
3432 ra.buf = RSTRING_PTR(buf);
3435 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
3446 path = rb_str_encode_ospath(path);
3447 v = rb_enc_str_new(0, size, enc);
3448 while ((rv = readlink_without_gvl(path, v, size)) == size
3450 || (rv < 0 &&
errno == ERANGE)
3459 rb_str_resize(v, 0);
3460 rb_syserr_fail_path(e, path);
3462 rb_str_resize(v, rv);
3467#define rb_file_s_readlink rb_f_notimplement
3471unlink_internal(
const char *path,
void *arg)
3473 return unlink(path);
3493rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
3495 return apply2files(unlink_internal, argc, argv, 0);
3504no_gvl_rename(
void *ptr)
3508 return (
void *)(
VALUE)rename(ra->src, ra->dst);
3529 f = rb_str_encode_ospath(from);
3530 t = rb_str_encode_ospath(to);
3533#if defined __CYGWIN__
3536 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
3541 if (chmod(ra.dst, 0666) == 0 &&
3542 unlink(ra.dst) == 0 &&
3543 rename(ra.src, ra.dst) == 0)
3547 syserr_fail2(e, from, to);
3582 rb_error_arity(argc, 0, 1);
3590#if defined __CYGWIN__ || defined DOSISH
3592#define DOSISH_DRIVE_LETTER
3593#define FILE_ALT_SEPARATOR '\\'
3595#ifdef FILE_ALT_SEPARATOR
3596#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
3598static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
3601#define isdirsep(x) ((x) == '/')
3614# define USE_NTFS_ADS 1
3616# define USE_NTFS_ADS 0
3621#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
3623#define istrailinggarbage(x) 0
3627# define isADS(x) ((x) == ':')
3632#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
3634#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
3635#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
3637#if defined(DOSISH_UNC)
3638#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
3640#define has_unc(buf) 0
3643#ifdef DOSISH_DRIVE_LETTER
3645has_drive_letter(
const char *buf)
3647 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
3660 char *drvcwd, *oldcwd;
3671 if (chdir(drive) == 0) {
3684not_same_drive(
VALUE path,
int drive)
3686 const char *p = RSTRING_PTR(path);
3687 if (RSTRING_LEN(path) < 2)
return 0;
3688 if (has_drive_letter(p)) {
3699skiproot(
const char *path,
const char *end)
3701#ifdef DOSISH_DRIVE_LETTER
3702 if (path + 2 <= end && has_drive_letter(path)) path += 2;
3704 while (path < end && isdirsep(*path)) path++;
3705 return (
char *)path;
3709enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
3711 while (s < e && !isdirsep(*s)) {
3712 Inc(s, e, mb_enc, enc);
3717#define nextdirsep rb_enc_path_next
3721 return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
3724#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3725#define skipprefix enc_path_skip_prefix
3727#define skipprefix(path, end, mb_enc, enc) (path)
3730enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3732#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3734 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
3736 while (path < end && isdirsep(*path)) path++;
3737 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
3738 path + 2 <= end && !isdirsep(path[1])) {
3739 path = enc_path_next(path + 1, end, mb_enc, enc);
3741 return (
char *)path;
3744#ifdef DOSISH_DRIVE_LETTER
3745 if (path + 2 <= end && has_drive_letter(path))
3746 return (
char *)(path + 2);
3749 return (
char *)path;
3753rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
3755 return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
3759skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
3761#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3762 char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
3763 while (p < end && isdirsep(*p)) p++;
3766 return skiproot(path, end);
3771rb_enc_path_skip_prefix_root(
const char *path,
const char *end,
rb_encoding *enc)
3773 return skipprefixroot(path, end, enc);
3777enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3780 while (path < end) {
3781 if (isdirsep(*path)) {
3782 const char *tmp = path++;
3783 while (path < end && isdirsep(*path)) path++;
3784 if (path >= end)
break;
3788 Inc(path, end, mb_enc, enc);
3794rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
3796 return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
3800strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3802 if (RB_UNLIKELY(mb_enc)) {
3803 return enc_path_last_separator(path, end, mb_enc, enc);
3806 const char *cursor = end - 1;
3808 while (isdirsep(cursor[0])) {
3812 while (cursor >= path) {
3813 if (isdirsep(cursor[0])) {
3814 while (cursor > path && isdirsep(cursor[-1])) {
3817 return (
char *)cursor;
3825chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3827 while (path < end) {
3828 if (isdirsep(*path)) {
3829 const char *last = path++;
3830 while (path < end && isdirsep(*path)) path++;
3831 if (path >= end)
return (
char *)last;
3834 Inc(path, end, mb_enc, enc);
3837 return (
char *)path;
3843 if (path < end && isdirsep(*path)) path++;
3844 return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
3850 rb_encoding *enc = rb_enc_check_str(path1, path2);
3851 int encidx = rb_enc_to_index(enc);
3852 if (encidx == ENCINDEX_US_ASCII) {
3853 encidx = rb_enc_get_index(path1);
3854 if (encidx == ENCINDEX_US_ASCII)
3855 encidx = rb_enc_get_index(path2);
3856 enc = rb_enc_from_index(encidx);
3863ntfs_tail(
const char *path,
const char *end,
rb_encoding *enc)
3865 bool mb_enc = enc_mbclen_needed(enc);
3866 while (path < end && *path ==
'.') path++;
3867 while (path < end && !isADS(*path)) {
3868 if (istrailinggarbage(*path)) {
3869 const char *last = path++;
3870 while (path < end && istrailinggarbage(*path)) path++;
3871 if (path >= end || isADS(*path))
return (
char *)last;
3873 else if (isdirsep(*path)) {
3874 const char *last = path++;
3875 while (path < end && isdirsep(*path)) path++;
3876 if (path >= end)
return (
char *)last;
3877 if (isADS(*path)) path++;
3880 Inc(path, end, mb_enc, enc);
3883 return (
char *)path;
3887#define BUFCHECK(cond) do {\
3890 do {buflen *= 2;} while (cond);\
3891 rb_str_resize(result, buflen);\
3892 buf = RSTRING_PTR(result);\
3894 pend = buf + buflen;\
3899 p = buf = RSTRING_PTR(result),\
3900 buflen = RSTRING_LEN(result),\
3904# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
3906# define SKIPPATHSEP(p) 1
3909#define BUFCOPY(srcptr, srclen) do { \
3910 const int skip = SKIPPATHSEP(p); \
3911 rb_str_set_len(result, p-buf+skip); \
3912 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
3914 memcpy(p, (srcptr), (srclen)); \
3918#define WITH_ROOTDIFF(stmt) do { \
3919 long rootdiff = root - buf; \
3921 root = buf + rootdiff; \
3925copy_home_path(
VALUE result,
const char *dir)
3931 dirlen = strlen(dir);
3932 rb_str_resize(result, dirlen);
3933 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
3934 encidx = rb_filesystem_encindex();
3935 rb_enc_associate_index(result, encidx);
3936#if defined FILE_ALT_SEPARATOR
3938 bool mb_enc = enc_mbclen_needed(enc);
3939 for (
char *p = buf, *bend = p + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
3940 if (*p == FILE_ALT_SEPARATOR) {
3952 VALUE dirname = rb_getpwdirnam_for_login(user);
3953 if (dirname ==
Qnil) {
3954 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3956 const char *dir = RSTRING_PTR(dirname);
3958 extern char *getlogin(
void);
3959 const char *pwPtr = 0;
3961 # define endpwent() ((void)0)
3962 const char *dir, *username = RSTRING_PTR(user);
3973 if ((login = getlogin()) && strcasecmp(username, login) == 0)
3974 dir = pwPtr = getenv(
"HOME");
3976 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3979 copy_home_path(result, dir);
3985rb_default_home_dir(
VALUE result)
3987 const char *dir = getenv(
"HOME");
3989#if defined HAVE_PWD_H
4003 VALUE login_name = rb_getlogin();
4005# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
4010 if (
NIL_P(login_name)) {
4011 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
4015 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
4016 if (
NIL_P(pw_dir)) {
4017 pw_dir = rb_getpwdiruid();
4018 if (
NIL_P(pw_dir)) {
4019 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
4024 copy_home_path(result, RSTRING_PTR(pw_dir));
4025 rb_str_resize(pw_dir, 0);
4030 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4032 return copy_home_path(result, dir);
4038#if NORMALIZE_UTF8PATH
4039 VALUE path = rb_str_normalize_ospath(ptr,
len);
4040 rb_enc_associate(path, fsenc);
4043 return rb_enc_str_new(ptr,
len, fsenc);
4050 char *buf, *cwdp = dir;
4054 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4055 dirname = ospath_new(dir, dirlen, fsenc);
4056 if (!rb_enc_compatible(fname, dirname)) {
4060 rb_enc_check(fname, dirname);
4061 rb_bug(
"unreachable");
4063 rb_encoding *direnc = fs_enc_check(fname, dirname);
4064 if (direnc != fsenc) {
4068 else if (NORMALIZE_UTF8PATH) {
4073 do {buflen *= 2;}
while (dirlen > buflen);
4074 rb_str_resize(result, buflen);
4075 buf = RSTRING_PTR(result);
4076 memcpy(buf, cwdp, dirlen);
4078 if (!
NIL_P(dirname)) rb_str_resize(dirname, 0);
4079 rb_enc_associate(result, *enc);
4080 return buf + dirlen;
4084rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4086 const char *s, *b, *fend;
4087 char *buf, *p, *pend, *root;
4088 size_t buflen, bdiff;
4089 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4092 fend = s + RSTRING_LEN(fname);
4093 enc = rb_str_enc_get(fname);
4094 bool mb_enc = enc_mbclen_needed(enc);
4095 if (!mb_enc &&
RTEST(dname)) {
4096 mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
4101 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4103 if (s + 1 == fend || isdirsep(s[1])) {
4107 if (++s < fend) ++s;
4108 rb_default_home_dir(result);
4111 s = nextdirsep(b = s, fend, enc);
4114 BUFCHECK(bdiff + userlen >= buflen);
4115 memcpy(p, b, userlen);
4118 rb_enc_associate(result, enc);
4119 rb_home_dir_of(result, result);
4123 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4125 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4126 (
int)userlen, b, fname);
4129 rb_raise(rb_eArgError,
"non-absolute home");
4135#ifdef DOSISH_DRIVE_LETTER
4137 else if (s + 1 < fend && has_drive_letter(s)) {
4138 if (s + 2 < fend && isdirsep(s[2])) {
4141 BUFCHECK(bdiff + 2 >= buflen);
4145 rb_enc_copy(result, fname);
4150 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4151 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4159 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4164 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4167 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4172 else if (s == fend || !rb_is_absolute_path(s)) {
4173 if (!
NIL_P(dname)) {
4174 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4175 rb_enc_associate(result, fs_enc_check(result, fname));
4180 char *e = append_fspath(result, fname,
ruby_getcwd(), &enc, fsenc);
4184#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4185 if (s < fend && isdirsep(*s)) {
4188 p = skipprefix(buf, p, mb_enc, enc);
4192 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4197 do s++;
while (s < fend && isdirsep(*s));
4200 BUFCHECK(bdiff >= buflen);
4201 memset(buf,
'/',
len);
4203 rb_enc_associate(result, fs_enc_check(result, fname));
4205 if (p > buf && p[-1] ==
'/')
4209 BUFCHECK(bdiff + 1 >= buflen);
4214 BUFCHECK(bdiff + 1 >= buflen);
4216 root = skipprefix(buf, p+1, mb_enc, enc);
4229 if (s+1 == fend || isdirsep(*(s+1))) {
4233 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
4243 do ++s;
while (s < fend && istrailinggarbage(*s));
4248#if defined FILE_ALT_SEPARATOR
4249 case FILE_ALT_SEPARATOR:
4263 while (s < fend && istrailinggarbage(*s)) s++;
4273#if defined FILE_ALT_SEPARATOR
4274 case FILE_ALT_SEPARATOR:
4277 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4285 int n = ignored_char_p(s, fend, enc);
4288 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4296 Inc(s, fend, mb_enc, enc);
4304 static const char prime[] =
":$DATA";
4305 enum {prime_len =
sizeof(prime) -1};
4309 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) {
4312 if (isADS(*(s - (prime_len+1)))) {
4315 else if (memchr(b,
':', s - prime_len - b)) {
4324 if (p == skiproot(buf, p + !!*p) - 1) p++;
4328 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s,
"*?")) {
4333 WIN32_FIND_DATAW wfd;
4336#ifdef HAVE_CYGWIN_CONV_PATH
4337 char *w32buf = NULL;
4338 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
4340 char w32buf[MAXPATHLEN];
4344 int lnk_added = 0, is_symlink = 0;
4348 if (lstat_without_gvl(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
4354 path = *buf ? buf :
"/";
4355#ifdef HAVE_CYGWIN_CONV_PATH
4356 bufsize = cygwin_conv_path(flags, path, NULL, 0);
4359 if (lnk_added) bufsize += 4;
4361 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
4366 bufsize = MAXPATHLEN;
4367 if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
4371 if (is_symlink && b == w32buf) {
4373 strlcat(w32buf, p, bufsize);
4375 strlcat(w32buf,
".lnk", bufsize);
4386 if (encidx != ENCINDEX_UTF_8 && !is_ascii_string(result)) {
4387 tmp = rb_str_encode_ospath(result);
4389 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0);
4391 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr,
len);
4393 h = FindFirstFileW(wstr, &wfd);
4395 if (h != INVALID_HANDLE_VALUE) {
4398 len = lstrlenW(wfd.cFileName);
4400 if (lnk_added &&
len > 4 &&
4401 wcscasecmp(wfd.cFileName +
len - 4, L
".lnk") == 0) {
4402 wfd.cFileName[
len -= 4] = L
'\0';
4409 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL);
4410 if (tmp == result) {
4411 BUFCHECK(bdiff +
len >= buflen);
4412 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p,
len + 1, NULL, NULL);
4416 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, RSTRING_PTR(tmp),
len + 1, NULL, NULL);
4417 rb_str_cat_conv_enc_opts(result, bdiff, RSTRING_PTR(tmp),
len,
4418 rb_utf8_encoding(), 0,
Qnil);
4420 rb_str_resize(tmp, 0);
4433 rb_enc_check(fname, result);
4439#define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, 1)
4442str_shrink(
VALUE str)
4444 rb_str_resize(str, RSTRING_LEN(str));
4448#define expand_path(fname, dname, abs_mode, long_name, result) \
4449 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
4451#define check_expand_path_args(fname, dname) \
4452 (((fname) = rb_get_path(fname)), \
4453 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
4456file_expand_path_1(
VALUE fname)
4458 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, EXPAND_PATH_BUFFER());
4464 check_expand_path_args(fname, dname);
4465 return expand_path(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
4469rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
4471 return expand_path(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
4475rb_file_s_expand_path(
int argc,
const VALUE *argv)
4478 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4512 return rb_file_s_expand_path(c, v);
4518 check_expand_path_args(fname, dname);
4519 return expand_path(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
4523rb_file_s_absolute_path(
int argc,
const VALUE *argv)
4526 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4545 return rb_file_s_absolute_path(c, v);
4561 VALUE path = rb_get_path(fname);
4563 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
4567enum rb_realpath_mode {
4571 RB_REALPATH_MODE_MAX
4575realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
4576 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
4578 const char *pend = unresolved + strlen(unresolved);
4582 while (unresolved < pend) {
4583 const char *testname = unresolved;
4584 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
4585 long testnamelen = unresolved_firstsep - unresolved;
4586 const char *unresolved_nextname = unresolved_firstsep;
4587 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
4588 unresolved_nextname++;
4589 unresolved = unresolved_nextname;
4590 if (testnamelen == 1 && testname[0] ==
'.') {
4592 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
4593 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
4594 bool mb_enc = enc_mbclen_needed(enc);
4595 const char *resolved_str = RSTRING_PTR(*resolvedp);
4596 const char *resolved_names = resolved_str + *prefixlenp;
4597 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
4598 long len = lastsep ? lastsep - resolved_names : 0;
4599 rb_str_resize(*resolvedp, *prefixlenp +
len);
4605 if (*prefixlenp < RSTRING_LEN(testpath))
4607#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4608 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
4609 const char *prefix = RSTRING_PTR(testpath);
4610 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
4615 checkval = rb_hash_aref(loopcheck, testpath);
4616 if (!
NIL_P(checkval)) {
4617 if (checkval ==
ID2SYM(resolving)) {
4618 if (mode == RB_REALPATH_CHECK) {
4622 rb_syserr_fail_path(ELOOP, testpath);
4631 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
4634 if (e == ENOENT && !
NIL_P(fallback)) {
4635 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
4640 if (mode == RB_REALPATH_CHECK)
return -1;
4642 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
4643 rb_syserr_fail_path(e, testpath);
4644 *resolvedp = testpath;
4648 rb_syserr_fail_path(e, testpath);
4652 if (S_ISLNK(sbuf.st_mode)) {
4655 const char *link_prefix, *link_names;
4656 long link_prefixlen;
4657 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
4658 link = rb_readlink(testpath, enc);
4659 link_prefix = RSTRING_PTR(link);
4660 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
4661 link_prefixlen = link_names - link_prefix;
4662 if (link_prefixlen > 0) {
4666 tmpenc = fs_enc_check(*resolvedp, link);
4669 *prefixlenp = link_prefixlen;
4671 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
4672 loopcheck, mode, !*unresolved_firstsep))
4681 rb_hash_aset(loopcheck, s, s);
4682 *resolvedp = testpath;
4691rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4695 VALUE unresolved_path;
4700 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
4701 char *ptr, *prefixptr = NULL, *pend;
4706 if (!
NIL_P(basedir)) {
4711 enc = rb_enc_get(unresolved_path);
4712 unresolved_path = TO_OSPATH(unresolved_path);
4714 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
4715 if (ptr != path_names) {
4716 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
4720 if (!
NIL_P(basedir)) {
4722 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
4723 if (ptr != basedir_names) {
4729 curdir = rb_dir_getwd_ospath();
4731 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
4736 pend = prefixptr + prefixlen;
4737 bool mb_enc = enc_mbclen_needed(enc);
4738 ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
4740 prefixlen = ++ptr - prefixptr;
4743#ifdef FILE_ALT_SEPARATOR
4744 while (prefixptr < ptr) {
4745 if (*prefixptr == FILE_ALT_SEPARATOR) {
4748 Inc(prefixptr, pend, mb_enc, enc);
4752 switch (rb_enc_to_index(enc)) {
4753 case ENCINDEX_ASCII_8BIT:
4754 case ENCINDEX_US_ASCII:
4755 rb_enc_associate_index(resolved, rb_filesystem_encindex());
4758 loopcheck = rb_hash_new();
4760 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
4763 if (basedir_names) {
4764 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
4767 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
4770 if (origenc && origenc != rb_enc_get(resolved)) {
4772 rb_enc_associate(resolved, origenc);
4784static VALUE rb_file_join(
long argc,
VALUE *args);
4786#ifndef HAVE_REALPATH
4788rb_check_realpath_emulate_try(
VALUE arg)
4791 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
4795rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
4799#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
4800 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
4802# include <sys/syslimits.h>
4803# define NEEDS_REALPATH_BUFFER 1
4807rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4810 VALUE unresolved_path;
4811 char *resolved_ptr = NULL;
4813# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
4814 char resolved_buffer[PATH_MAX];
4816 char *
const resolved_buffer = NULL;
4819 if (mode == RB_REALPATH_DIR) {
4820 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4824 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
4825 VALUE paths[2] = {basedir, unresolved_path};
4826 unresolved_path = rb_file_join(2, paths);
4828 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
4830 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
4839 if (
errno == ENOTSUP ||
4841 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
4842 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4845 if (mode == RB_REALPATH_CHECK) {
4848 rb_sys_fail_path(unresolved_path);
4850 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
4851# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
4855# if !defined(__linux__) && !defined(__APPLE__)
4859 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
4860 if (mode == RB_REALPATH_CHECK) {
4863 rb_sys_fail_path(unresolved_path);
4867 if (origenc && origenc != rb_enc_get(resolved)) {
4871 rb_enc_associate(resolved, origenc);
4874 if (is_broken_string(resolved)) {
4875 rb_enc_associate(resolved, rb_filesystem_encoding());
4876 if (is_broken_string(resolved)) {
4877 rb_enc_associate(resolved, rb_ascii8bit_encoding());
4884 if (mode == RB_REALPATH_CHECK) {
4888 arg[2] = (
VALUE)origenc;
4891 rb_check_realpath_emulate_rescue,
Qnil);
4894 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4900rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
4902 const enum rb_realpath_mode mode =
4903 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
4904 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
4910 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
4927rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
4930 VALUE path = argv[0];
4932 return rb_realpath_internal(basedir, path, 1);
4948rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
4951 VALUE path = argv[0];
4953 return rb_realpath_internal(basedir, path, 0);
4957rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
4961 const char *s, *last;
4963 if (!e || !l2)
return 0;
4965 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
4966 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
4967 if (c ==
'.')
return l0;
4972 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
4977 if (l1 < l2)
return l1;
4980 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
4981#if CASEFOLD_FILESYSTEM
4982#define fncomp strncasecmp
4984#define fncomp strncmp
4986 if (fncomp(s, e, l2) == 0) {
4992static inline const char *
4993enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
4995 const char *p, *q, *e, *end;
4998 long len = (alllen ? (size_t)*alllen : strlen(name));
5005 name = skipprefix(name, end, mb_enc, enc);
5006#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5007 const char *root = name;
5010 while (name < end && isdirsep(*name)) {
5017#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
5021#ifdef DOSISH_DRIVE_LETTER
5022 else if (*p ==
':') {
5035 p = strrdirsep(name, end, mb_enc, enc);
5040 while (isdirsep(*p)) {
5045 n = ntfs_tail(p, end, enc) - p;
5047 n = chompdirsep(p, end, mb_enc, enc) - p;
5049 for (q = p; q - p < n && *q ==
'.'; q++);
5050 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5051 if (*q ==
'.') e = q;
5071ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5073 return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
5117 const char *name, *p, *fp = 0;
5123 CheckPath(fname, name);
5127 check_path_encoding(fext);
5129 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5130 enc = rb_str_enc_get(fname);
5133 n = RSTRING_LEN(fname);
5134 if (n <= 0 || !*name) {
5135 return rb_enc_str_new(0, 0, enc);
5138 bool mb_enc = enc_mbclen_needed(enc);
5139 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5145 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5150 if (f == RSTRING_LEN(fname)) {
5155 return rb_enc_str_new(p, f, enc);
5158static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5180rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5186 return rb_file_dirname_n(argv[0], n);
5192 return rb_file_dirname_n(fname, 1);
5196rb_file_dirname_n(
VALUE fname,
int n)
5198 const char *name, *root, *p, *end;
5201 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5202 CheckPath(fname, name);
5203 end = name + RSTRING_LEN(fname);
5205 bool mb_enc = !rb_str_enc_fastpath(fname);
5208 root = skiproot(name, end);
5210 if (root > name + 1 && isdirsep(*name))
5211 root = skipprefix(name = root - 2, end, mb_enc, enc);
5213 if (root > name + 1)
5216 if (n > (end - root + 1) / 2) {
5222 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5231 return rb_enc_str_new(
".", 1, enc);
5233#ifdef DOSISH_DRIVE_LETTER
5234 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5235 const char *top = skiproot(name + 2, end);
5236 dirname = rb_enc_str_new(name, 3, enc);
5241 dirname = rb_enc_str_new(name, p - name, enc);
5242#ifdef DOSISH_DRIVE_LETTER
5243 if (root == name + 2 && p == root && name[1] ==
':')
5249static inline const char *
5250enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5252 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5254 p = strrdirsep(name, end, mb_enc, enc);
5258 do name = ++p;
while (isdirsep(*p));
5261 while (*p && *p ==
'.') p++;
5263 if (*p ==
'.' || istrailinggarbage(*p)) {
5265 const char *last = p++, *dot = last;
5266 while (istrailinggarbage(*p)) {
5267 if (*p ==
'.') dot = p;
5270 if (!*p || isADS(*p)) {
5274 if (*last ==
'.' || dot > last) e = dot;
5281 else if (isADS(*p)) {
5285 else if (isdirsep(*p))
5287 Inc(p, end, mb_enc, enc);
5292 if (!e || e == name)
5317 return enc_find_extname(name,
len, enc_mbclen_needed(enc), enc);
5350 CheckPath(fname, name);
5351 long len = RSTRING_LEN(fname);
5354 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
5357 bool mb_enc = !rb_str_enc_fastpath(fname);
5360 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
5361 return rb_enc_str_new(ext,
len, enc);
5394 return rb_get_path(fname);
5418file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
5420 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
5421 return rb_file_join_ary(arg);
5425rb_file_join_ary(
VALUE ary)
5429 const char *name, *tail;
5439 check_path_encoding(tmp);
5440 len += RSTRING_LEN(tmp);
5448 RBASIC_CLEAR_CLASS(result);
5451 switch (OBJ_BUILTIN_TYPE(tmp)) {
5453 if (!checked) check_path_encoding(tmp);
5458 rb_raise(rb_eArgError,
"recursive array");
5470 rb_enc_copy(result, tmp);
5473 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
5474 if (RSTRING_PTR(tmp) && isdirsep(RSTRING_PTR(tmp)[0])) {
5481 enc = fs_enc_check(result, tmp);
5483 rb_enc_associate(result, enc);
5491rb_file_join_fastpath(
long argc,
VALUE *args)
5496 for (i = 0; i < argc; i++) {
5497 VALUE tmp = args[i];
5499 size += RSTRING_LEN(tmp);
5512 const char *name = RSTRING_PTR(result);
5513 for (i = 1; i < argc; i++) {
5514 VALUE tmp = args[i];
5515 long len = RSTRING_LEN(result);
5521 if (isdirsep(tmp_s[0])) {
5523 long trailing_seps = 0;
5524 while (isdirsep(name[
len - trailing_seps - 1])) {
5529 else if (!isdirsep(name[
len - 1])) {
5536 rb_enc_associate(result, new_enc);
5537 encidx = rb_enc_to_index(new_enc);
5543 rb_str_null_check(result);
5548rb_file_join(
long argc,
VALUE *args)
5550 if (RB_UNLIKELY(argc == 0)) {
5554 VALUE result = rb_file_join_fastpath(argc, args);
5555 if (RB_LIKELY(result)) {
5573rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
5575 return rb_file_join(argc, argv);
5578#if defined(HAVE_TRUNCATE)
5579struct truncate_arg {
5585nogvl_truncate(
void *ptr)
5587 struct truncate_arg *ta = ptr;
5588 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
5609 struct truncate_arg ta;
5614 path = rb_str_encode_ospath(path);
5617 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
5619 rb_sys_fail_path(path);
5623#define rb_file_s_truncate rb_f_notimplement
5626#if defined(HAVE_FTRUNCATE)
5627struct ftruncate_arg {
5633nogvl_ftruncate(
void *ptr)
5635 struct ftruncate_arg *fa = ptr;
5637 return (
VALUE)ftruncate(fa->fd, fa->pos);
5658 struct ftruncate_arg fa;
5665 rb_io_flush_raw(obj, 0);
5667 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
5668 rb_sys_fail_path(fptr->
pathv);
5673#define rb_file_truncate rb_f_notimplement
5690#include <winerror.h>
5694rb_thread_flock(
void *data)
5697 int old_errno =
errno;
5699 int *op = data, ret = flock(op[0], op[1]);
5702 if (GetLastError() == ERROR_NOT_LOCKED) {
5760 op[1] = op1 =
NUM2INT(operation);
5765 rb_io_flush_raw(obj, 0);
5767 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
5772#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
5775 if (op1 & LOCK_NB)
return Qfalse;
5778 time.tv_usec = 100 * 1000;
5784#if defined(ERESTART)
5790 rb_syserr_fail_path(e, fptr->
pathv);
5797test_check(
int n,
int argc,
VALUE *argv)
5803 for (i=1; i<n; i++) {
5810#define CHECK(n) test_check((n), argc, argv)
5902 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
5906 return rb_file_blockdev_p(0, argv[1]);
5909 return rb_file_chardev_p(0, argv[1]);
5912 return rb_file_directory_p(0, argv[1]);
5915 return rb_file_exist_p(0, argv[1]);
5918 return rb_file_file_p(0, argv[1]);
5921 return rb_file_sgid_p(0, argv[1]);
5924 return rb_file_grpowned_p(0, argv[1]);
5927 return rb_file_sticky_p(0, argv[1]);
5930 return rb_file_symlink_p(0, argv[1]);
5933 return rb_file_owned_p(0, argv[1]);
5936 return rb_file_rowned_p(0, argv[1]);
5939 return rb_file_pipe_p(0, argv[1]);
5942 return rb_file_readable_p(0, argv[1]);
5945 return rb_file_readable_real_p(0, argv[1]);
5948 return rb_file_size_p(0, argv[1]);
5951 return rb_file_socket_p(0, argv[1]);
5954 return rb_file_suid_p(0, argv[1]);
5957 return rb_file_writable_p(0, argv[1]);
5960 return rb_file_writable_real_p(0, argv[1]);
5963 return rb_file_executable_p(0, argv[1]);
5966 return rb_file_executable_real_p(0, argv[1]);
5969 return rb_file_zero_p(0, argv[1]);
5973 if (strchr(
"MAC", cmd)) {
5975 VALUE fname = argv[1];
5978 if (
rb_stat(fname, &st) == -1) {
5981 rb_syserr_fail_path(e, fname);
5986 return stat_atime(&st);
5988 return stat_mtime(&st);
5990 return stat_ctime(&st);
5996 return rb_file_identical_p(0, argv[1], argv[2]);
5999 if (strchr(
"=<>", cmd)) {
6000 struct stat st1, st2;
6007 t1 = stat_mtimespec(&st1);
6008 t2 = stat_mtimespec(&st2);
6012 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
6016 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
6017 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
6021 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
6022 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
6029 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
6032 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6086rb_stat_s_alloc(
VALUE klass)
6107 fname = rb_str_encode_ospath(fname);
6109 rb_sys_fail_path(fname);
6116 rb_st->initialized =
true;
6133 *copy_rb_st = *orig_rb_st;
6152rb_stat_ftype(
VALUE obj)
6154 return rb_file_ftype(get_stat(obj)->ST_(mode));
6171 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6187 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
6213 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
6234 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
6257 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
6278 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
6296rb_stat_owned(
VALUE obj)
6298 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
6303rb_stat_rowned(
VALUE obj)
6305 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
6322rb_stat_grpowned(
VALUE obj)
6325 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
6344 rb_io_stat_data *st = get_stat(obj);
6347 if (geteuid() == 0)
return Qtrue;
6350 if (rb_stat_owned(obj))
6351 return RBOOL(st->ST_(mode) & S_IRUSR);
6354 if (rb_stat_grpowned(obj))
6355 return RBOOL(st->ST_(mode) & S_IRGRP);
6358 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6377 rb_io_stat_data *st = get_stat(obj);
6380 if (getuid() == 0)
return Qtrue;
6383 if (rb_stat_rowned(obj))
6384 return RBOOL(st->ST_(mode) & S_IRUSR);
6387 if (rb_group_member(get_stat(obj)->ST_(gid)))
6388 return RBOOL(st->ST_(mode) & S_IRGRP);
6391 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6410rb_stat_wr(
VALUE obj)
6413 rb_io_stat_data *st = get_stat(obj);
6414 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
6415 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6435 rb_io_stat_data *st = get_stat(obj);
6438 if (geteuid() == 0)
return Qtrue;
6441 if (rb_stat_owned(obj))
6442 return RBOOL(st->ST_(mode) & S_IWUSR);
6445 if (rb_stat_grpowned(obj))
6446 return RBOOL(st->ST_(mode) & S_IWGRP);
6449 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6468 rb_io_stat_data *st = get_stat(obj);
6471 if (getuid() == 0)
return Qtrue;
6474 if (rb_stat_rowned(obj))
6475 return RBOOL(st->ST_(mode) & S_IWUSR);
6478 if (rb_group_member(get_stat(obj)->ST_(gid)))
6479 return RBOOL(st->ST_(mode) & S_IWGRP);
6482 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6501rb_stat_ww(
VALUE obj)
6504 rb_io_stat_data *st = get_stat(obj);
6505 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
6506 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6528 rb_io_stat_data *st = get_stat(obj);
6531 if (geteuid() == 0) {
6532 return RBOOL(st->ST_(mode) & S_IXUGO);
6536 if (rb_stat_owned(obj))
6537 return RBOOL(st->ST_(mode) & S_IXUSR);
6540 if (rb_stat_grpowned(obj))
6541 return RBOOL(st->ST_(mode) & S_IXGRP);
6544 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6560 rb_io_stat_data *st = get_stat(obj);
6563 if (getuid() == 0) {
6564 return RBOOL(st->ST_(mode) & S_IXUGO);
6568 if (rb_stat_rowned(obj))
6569 return RBOOL(st->ST_(mode) & S_IXUSR);
6572 if (rb_group_member(get_stat(obj)->ST_(gid)))
6573 return RBOOL(st->ST_(mode) & S_IXGRP);
6576 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6595 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
6613 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
6632 rb_off_t size = get_stat(obj)->ST_(size);
6634 if (size == 0)
return Qnil;
6650rb_stat_suid(
VALUE obj)
6653 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
6671rb_stat_sgid(
VALUE obj)
6674 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
6692rb_stat_sticky(
VALUE obj)
6695 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
6700#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
6701#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
6712nogvl_mkfifo(
void *ptr)
6714 struct mkfifo_arg *ma = ptr;
6716 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
6733 struct mkfifo_arg ma;
6742 path = rb_str_encode_ospath(path);
6743 ma.path = RSTRING_PTR(path);
6744 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
6745 rb_sys_fail_path(path);
6750#define rb_file_s_mkfifo rb_f_notimplement
6753static VALUE rb_mFConst;
6756rb_file_const(
const char *name,
VALUE value)
6758 rb_define_const(rb_mFConst, name, value);
6762rb_is_absolute_path(
const char *path)
6764#ifdef DOSISH_DRIVE_LETTER
6765 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
6768 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
6771 if (path[0] ==
'/')
return 1;
6777ruby_is_fd_loadable(
int fd)
6784 if (fstat(fd, &st) < 0)
6787 if (S_ISREG(st.st_mode))
6790 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
6793 if (S_ISDIR(st.st_mode))
6804rb_file_load_ok(
const char *path)
6811 int mode = (O_RDONLY |
6812#if defined O_NONBLOCK
6814#elif defined O_NDELAY
6820 if (!rb_gc_for_fd(
errno))
return 0;
6822 if (fd < 0)
return 0;
6825 ret = ruby_is_fd_loadable(fd);
6832is_explicit_relative(
const char *path)
6834 if (*path++ !=
'.')
return 0;
6835 if (*path ==
'.') path++;
6836 return isdirsep(*path);
6842 int encidx = rb_enc_get_index(orig);
6843 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
6844 encidx = rb_filesystem_encindex();
6845 rb_enc_associate_index(path, encidx);
6853rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
6856 VALUE fname = *filep, load_path, tmp;
6860 if (!ext[0])
return 0;
6863 fname = file_expand_path_1(fname);
6864 f = RSTRING_PTR(fname);
6869 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6870 if (!expanded) fname = file_expand_path_1(fname);
6871 fnlen = RSTRING_LEN(fname);
6872 for (i=0; ext[i]; i++) {
6874 if (rb_file_load_ok(RSTRING_PTR(fname))) {
6875 *filep = copy_path_class(fname, *filep);
6883 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6884 if (!load_path)
return 0;
6887 RBASIC_CLEAR_CLASS(fname);
6888 fnlen = RSTRING_LEN(fname);
6890 rb_enc_associate_index(tmp, rb_usascii_encindex());
6891 for (j=0; ext[j]; j++) {
6893 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6897 if (RSTRING_LEN(str) == 0)
continue;
6898 rb_file_expand_path_internal(fname, str, 0, 0, tmp);
6899 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
6900 *filep = copy_path_class(tmp, *filep);
6906 rb_str_resize(tmp, 0);
6914 VALUE tmp, load_path;
6919 tmp = file_expand_path_1(path);
6920 path = copy_path_class(tmp, path);
6921 f = RSTRING_PTR(path);
6925 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6926 if (!rb_file_load_ok(f))
return 0;
6928 path = copy_path_class(file_expand_path_1(path), path);
6932 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6937 rb_enc_associate_index(tmp, rb_usascii_encindex());
6938 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6941 if (RSTRING_LEN(str) > 0) {
6942 rb_file_expand_path_internal(path, str, 0, 0, tmp);
6943 f = RSTRING_PTR(tmp);
6944 if (rb_file_load_ok(f))
goto found;
6947 rb_str_resize(tmp, 0);
6955 return copy_path_class(tmp, path);
6958#define define_filetest_function(name, func, argc) do { \
6959 rb_define_module_function(rb_mFileTest, name, func, argc); \
6960 rb_define_singleton_method(rb_cFile, name, func, argc); \
6963const char ruby_null_device[] =
6966#elif defined AMIGA || defined __amigaos__
7770#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
7771 rb_CFString_class_initialize_before_fork();
7779 define_filetest_function(
"directory?", rb_file_directory_p, 1);
7780 define_filetest_function(
"exist?", rb_file_exist_p, 1);
7781 define_filetest_function(
"readable?", rb_file_readable_p, 1);
7782 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
7783 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
7784 define_filetest_function(
"writable?", rb_file_writable_p, 1);
7785 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
7786 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
7787 define_filetest_function(
"executable?", rb_file_executable_p, 1);
7788 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
7789 define_filetest_function(
"file?", rb_file_file_p, 1);
7790 define_filetest_function(
"zero?", rb_file_zero_p, 1);
7791 define_filetest_function(
"empty?", rb_file_zero_p, 1);
7792 define_filetest_function(
"size?", rb_file_size_p, 1);
7793 define_filetest_function(
"size", rb_file_s_size, 1);
7794 define_filetest_function(
"owned?", rb_file_owned_p, 1);
7795 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
7797 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
7798 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
7799 define_filetest_function(
"socket?", rb_file_socket_p, 1);
7801 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
7802 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
7804 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
7805 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
7806 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
7808 define_filetest_function(
"identical?", rb_file_identical_p, 2);
7846 separator = rb_fstring_lit(
"/");
7848 rb_define_const(
rb_cFile,
"Separator", separator);
7850 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
8202 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
8204 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
8206 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
8208 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
8210 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
8212 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
8213#if defined(O_NDELAY) || defined(O_NONBLOCK)
8215# define O_NONBLOCK O_NDELAY
8218 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
8221 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
8224 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
8230 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
8231#ifndef O_SHARE_DELETE
8232# define O_SHARE_DELETE 0
8235 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
8238 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
8242 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
8246 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
8250 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
8254 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
8258 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
8262 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
8266 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
8268 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
8270 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
8272 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
8275 rb_define_const(rb_mFConst,
"NULL", rb_fstring_cstr(ruby_null_device));
#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.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_define_module(const char *name)
Defines a top-level module.
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
#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.
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.
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 strdup(s)
Just another name of ruby_strdup.
#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 RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
#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.