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);
249 check_path_encoding(obj);
250 if (!rb_str_to_cstr(obj)) {
251 rb_raise(rb_eArgError,
"path name contains null byte");
260 return rb_get_path(obj);
266 return rb_get_path_check_convert(rb_get_path_check_to_string(obj));
270check_path(
VALUE obj,
const char **cstr)
272 VALUE str = rb_get_path_check_convert(rb_get_path_check_to_string(obj));
276 *cstr = RSTRING_PTR(str);
280#define CheckPath(str, cstr) RB_GC_GUARD(str) = check_path(str, &cstr);
287#if 0 && defined _WIN32
288 if (encidx == ENCINDEX_ASCII_8BIT) {
289 encidx = rb_filesystem_encindex();
292 if (encidx != ENCINDEX_ASCII_8BIT && encidx != ENCINDEX_UTF_8) {
302# define NORMALIZE_UTF8PATH 1
304# ifdef HAVE_WORKING_FORK
305static CFMutableStringRef
306mutable_CFString_new(CFStringRef *s,
const char *ptr,
long len)
308 const CFAllocatorRef alloc = kCFAllocatorDefault;
309 *s = CFStringCreateWithBytesNoCopy(alloc, (
const UInt8 *)ptr,
len,
310 kCFStringEncodingUTF8, FALSE,
312 return CFStringCreateMutableCopy(alloc,
len, *s);
315# define mutable_CFString_release(m, s) (CFRelease(m), CFRelease(s))
318rb_CFString_class_initialize_before_fork(
void)
341 const char small_str[] =
"/";
342 long len =
sizeof(small_str) - 1;
350 for (
int i = 0; i < 2; i++) {
351 CFMutableStringRef m = mutable_CFString_new(&s, small_str,
len);
352 mutable_CFString_release(m, s);
358rb_str_append_normalized_ospath(
VALUE str,
const char *ptr,
long len)
363 CFMutableStringRef m = mutable_CFString_new(&s, ptr,
len);
364 long oldlen = RSTRING_LEN(str);
366 CFStringNormalize(m, kCFStringNormalizationFormC);
367 all = CFRangeMake(0, CFStringGetLength(m));
368 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE, NULL, 0, &buflen);
370 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE,
371 (UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
373 mutable_CFString_release(m, s);
378rb_str_normalize_ospath(
const char *ptr,
long len)
381 const char *e = ptr +
len;
390 rb_enc_associate(str, enc);
395 int r = rb_enc_precise_mbclen(p, e, enc);
399 rb_str_append_normalized_ospath(str, p1, p-p1);
406 c = rb_enc_mbc_to_codepoint(p, e, enc);
407 if ((0x2000 <= c && c <= 0x2FFF) || (0xF900 <= c && c <= 0xFAFF) ||
408 (0x2F800 <= c && c <= 0x2FAFF)) {
410 rb_str_append_normalized_ospath(str, p1, p-p1);
421 rb_str_append_normalized_ospath(str, p1, p-p1);
428ignored_char_p(
const char *p,
const char *e,
rb_encoding *enc)
431 if (p+3 > e)
return 0;
432 switch ((
unsigned char)*p) {
434 switch ((
unsigned char)p[1]) {
436 c = (
unsigned char)p[2];
438 if (c >= 0x8c && c <= 0x8f)
return 3;
440 if (c >= 0xaa && c <= 0xae)
return 3;
443 c = (
unsigned char)p[2];
445 if (c >= 0xaa && c <= 0xaf)
return 3;
451 if ((
unsigned char)p[1] == 0xbb &&
452 (
unsigned char)p[2] == 0xbf)
459# define NORMALIZE_UTF8PATH 0
462#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
473 int (*func)(
const char *,
void *);
479no_gvl_apply2files(
void *ptr)
483 for (aa->i = 0; aa->i < aa->argc; aa->i++) {
484 if (aa->func(aa->fn[aa->i].ptr, aa->arg) < 0) {
493NORETURN(
static void utime_failed(
struct apply_arg *));
494static int utime_internal(
const char *,
void *);
498apply2files(
int (*func)(
const char *,
void *),
int argc,
VALUE *argv,
void *arg)
502 const long len = (long)(offsetof(
struct apply_arg, fn) + (size * argc));
510 for (aa->i = 0; aa->i < argc; aa->i++) {
511 VALUE path = rb_get_path(argv[aa->i]);
513 path = rb_str_encode_ospath(path);
514 aa->fn[aa->i].ptr = RSTRING_PTR(path);
515 aa->fn[aa->i].path = path;
518 IO_WITHOUT_GVL(no_gvl_apply2files, aa);
521 if (func == utime_internal) {
525 rb_syserr_fail_path(aa->errnum, aa->fn[aa->i].path);
548 rb_io_stat_data stat;
561rb_stat_new(
const struct stat *st)
567# define CP(m) .stx_ ## m = st->st_ ## m
568# define CP_32(m) .stx_ ## m = (uint32_t)st->st_ ## m
569# define CP_TS(m) .stx_ ## m = stat_ ## m ## spec(st)
570 rb_st->stat = (
struct statx){
571 .stx_mask = STATX_BASIC_STATS,
588 rb_st->initialized =
true;
596rb_statx_new(
const rb_io_stat_data *st)
602 rb_st->initialized =
true;
608static rb_io_stat_data*
613 if (!rb_st->initialized) rb_raise(
rb_eTypeError,
"uninitialized File::Stat");
619statx_mtimespec(
const rb_io_stat_data *st)
621 return st->stx_mtime;
624# define statx_mtimespec stat_mtimespec
662 if (ts1.tv_sec == ts2.tv_sec) {
663 if (ts1.tv_nsec == ts2.tv_nsec)
return INT2FIX(0);
664 if (ts1.tv_nsec < ts2.tv_nsec)
return INT2FIX(-1);
667 if (ts1.tv_sec < ts2.tv_sec)
return INT2FIX(-1);
673#define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
676# define NUM2DEVT(v) NUM2UINT(v)
679# define DEVT2NUM(v) UINT2NUM(v)
681#ifndef PRI_DEVT_PREFIX
682# define PRI_DEVT_PREFIX ""
696rb_stat_dev(
VALUE self)
699 unsigned int m = get_stat(self)->stx_dev_major;
700 unsigned int n = get_stat(self)->stx_dev_minor;
702#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
703 return DEVT2NUM(get_stat(self)->st_dev);
704#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
705 return ULONG2NUM(get_stat(self)->st_dev);
707 return ULL2NUM(get_stat(self)->st_dev);
723rb_stat_dev_major(
VALUE self)
726 return UINT2NUM(get_stat(self)->stx_dev_major);
728 return UINT2NUM(major(get_stat(self)->st_dev));
746rb_stat_dev_minor(
VALUE self)
749 return UINT2NUM(get_stat(self)->stx_dev_minor);
751 return UINT2NUM(minor(get_stat(self)->st_dev));
768rb_stat_ino(
VALUE self)
770 rb_io_stat_data *ptr = get_stat(self);
771#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
773 return rb_integer_unpack(&ptr->st_ino, 2,
774 SIZEOF_STRUCT_STAT_ST_INO, 0,
778 return UIANY2NUM(ptr->ST_(ino));
796rb_stat_mode(
VALUE self)
798 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
814rb_stat_nlink(
VALUE self)
817 const rb_io_stat_data *ptr = get_stat(self);
819 return UIANY2NUM(ptr->ST_(nlink));
833rb_stat_uid(
VALUE self)
835 return UIDT2NUM(get_stat(self)->ST_(uid));
849rb_stat_gid(
VALUE self)
851 return GIDT2NUM(get_stat(self)->ST_(gid));
867rb_stat_rdev(
VALUE self)
870 unsigned int m = get_stat(self)->stx_rdev_major;
871 unsigned int n = get_stat(self)->stx_rdev_minor;
873#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
875#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
876 return DEVT2NUM(get_stat(self)->ST_(rdev));
877#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
878 return ULONG2NUM(get_stat(self)->ST_(rdev));
880 return ULL2NUM(get_stat(self)->ST_(rdev));
896rb_stat_rdev_major(
VALUE self)
899 return UINT2NUM(get_stat(self)->stx_rdev_major);
900#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
901 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
919rb_stat_rdev_minor(
VALUE self)
922 return UINT2NUM(get_stat(self)->stx_rdev_minor);
923#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
924 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
940rb_stat_size(
VALUE self)
942 return OFFT2NUM(get_stat(self)->ST_(size));
957rb_stat_blksize(
VALUE self)
959#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
960 return ULONG2NUM(get_stat(self)->ST_(blksize));
978rb_stat_blocks(
VALUE self)
980#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
981# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
982 return ULL2NUM(get_stat(self)->ST_(blocks));
984 return ULONG2NUM(get_stat(self)->ST_(blocks));
992stat_atimespec(
const struct stat *st)
995 ts.tv_sec = st->st_atime;
996#if defined(HAVE_STRUCT_STAT_ST_ATIM)
997 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
998#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
999 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
1000#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
1001 ts.tv_nsec = (uint32_t)st->st_atimensec;
1010statx_atimespec(
const rb_io_stat_data *st)
1012 return st->stx_atime;
1015# define statx_atimespec stat_atimespec
1025stat_atime(
const struct stat *st)
1027 return stat_time(stat_atimespec(st));
1031stat_mtimespec(
const struct stat *st)
1034 ts.tv_sec = st->st_mtime;
1035#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1036 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1037#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1038 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1039#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1040 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1048stat_mtime(
const struct stat *st)
1050 return stat_time(stat_mtimespec(st));
1054stat_ctimespec(
const struct stat *st)
1057 ts.tv_sec = st->st_ctime;
1058#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1059 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1060#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1061 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1062#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1063 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1072statx_ctimespec(
const rb_io_stat_data *st)
1074 return st->stx_ctime;
1077# define statx_ctimespec stat_ctimespec
1081stat_ctime(
const struct stat *st)
1083 return stat_time(stat_ctimespec(st));
1086#define HAVE_STAT_BIRTHTIME
1087#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1089statx_birthtime(
const rb_io_stat_data *st)
1094#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1095static VALUE statx_birthtime(
const rb_io_stat_data *st);
1096#elif defined(_WIN32)
1097# define statx_birthtime stat_ctime
1099# undef HAVE_STAT_BIRTHTIME
1128rb_stat_atime(
VALUE self)
1130 return stat_time(statx_atimespec(get_stat(self)));
1144rb_stat_mtime(
VALUE self)
1146 return stat_time(statx_mtimespec(get_stat(self)));
1164rb_stat_ctime(
VALUE self)
1166 return stat_time(statx_ctimespec(get_stat(self)));
1169#if defined(HAVE_STAT_BIRTHTIME)
1193rb_stat_birthtime(
VALUE self)
1195 return statx_birthtime(get_stat(self));
1198# define rb_stat_birthtime rb_f_notimplement
1217rb_stat_inspect(
VALUE self)
1221 static const struct {
1225 {
"dev", rb_stat_dev},
1226 {
"ino", rb_stat_ino},
1227 {
"mode", rb_stat_mode},
1228 {
"nlink", rb_stat_nlink},
1229 {
"uid", rb_stat_uid},
1230 {
"gid", rb_stat_gid},
1231 {
"rdev", rb_stat_rdev},
1232 {
"size", rb_stat_size},
1233 {
"blksize", rb_stat_blksize},
1234 {
"blocks", rb_stat_blocks},
1235 {
"atime", rb_stat_atime},
1236 {
"mtime", rb_stat_mtime},
1237 {
"ctime", rb_stat_ctime},
1238#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1239 {
"birthtime", rb_stat_birthtime},
1245 if (!rb_st->initialized) {
1253 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1261 v = (*member[i].func)(self);
1263 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1265 else if (i == 0 || i == 6) {
1266 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1286no_gvl_fstat(
void *data)
1289 return (
VALUE)fstat(arg->file.fd, arg->st);
1293fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1297 data.file.fd = fptr->
fd;
1300 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1304no_gvl_stat(
void * data)
1307 return (
void *)(
VALUE)STAT(arg->file.path, arg->st);
1311stat_without_gvl(
const char *path,
struct stat *st)
1315 data.file.path = path;
1318 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1321#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1322 defined(HAVE_STRUCT_STATX_STX_BTIME)
1324# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1327# ifdef HAVE_SYSCALL_H
1328# include <syscall.h>
1329# elif defined HAVE_SYS_SYSCALL_H
1330# include <sys/syscall.h>
1332# if defined __linux__
1333# include <linux/stat.h>
1335statx(
int dirfd,
const char *pathname,
int flags,
1336 unsigned int mask,
struct statx *statxbuf)
1338 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1343typedef struct no_gvl_rb_io_stat_data {
1349} no_gvl_rb_io_stat_data;
1352io_blocking_statx(
void *data)
1354 no_gvl_rb_io_stat_data *arg = data;
1355 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1359no_gvl_statx(
void *data)
1361 return (
void *)io_blocking_statx(data);
1365statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1367 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1370 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1374lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1376 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1379 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1383fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1385 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1388 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1391#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1394rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1399 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1404 result = fstatx_without_gvl(fptr, stx, mask);
1409 file = rb_str_encode_ospath(file);
1410 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1416# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1418NORETURN(
static void statx_notimplement(
const char *field_name));
1423statx_notimplement(
const char *field_name)
1426 "%s is unimplemented on this filesystem",
1431statx_birthtime(
const rb_io_stat_data *stx)
1433 if (!statx_has_birthtime(stx)) {
1435 statx_notimplement(
"birthtime");
1437 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1442# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1443# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1444# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1445# define rb_statx(file, stx, mask) rb_stat(file, stx)
1446# define STATX(path, st, mask) STAT(path, st)
1448#if defined(HAVE_STAT_BIRTHTIME)
1449# define statx_has_birthtime(st) 1
1451# define statx_has_birthtime(st) 0
1458# define FSTAT(fd, st) fstat(fd, st)
1467 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1472 result = fstat_without_gvl(fptr, st);
1477 file = rb_str_encode_ospath(file);
1478 result = stat_without_gvl(RSTRING_PTR(file), st);
1500 fname = rb_str_encode_ospath(fname);
1501 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1502 rb_sys_fail_path(fname);
1504 return rb_statx_new(&st);
1523rb_io_stat(
VALUE obj)
1529 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1530 rb_sys_fail_path(fptr->
pathv);
1532 return rb_statx_new(&st);
1537no_gvl_lstat(
void *ptr)
1540 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1544lstat_without_gvl(
const char *path,
struct stat *st)
1548 data.file.path = path;
1551 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1575 fname = rb_str_encode_ospath(fname);
1576 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1577 rb_sys_fail_path(fname);
1579 return rb_statx_new(&st);
1581 return rb_file_s_stat(klass, fname);
1600rb_file_lstat(
VALUE obj)
1609 path = rb_str_encode_ospath(fptr->
pathv);
1610 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1611 rb_sys_fail_path(fptr->
pathv);
1613 return rb_statx_new(&st);
1615 return rb_io_stat(obj);
1620rb_group_member(GETGROUPS_T gid)
1622#if defined(_WIN32) || !defined(HAVE_GETGROUPS)
1631 if (getgid() == gid || getegid() == gid)
1634 groups = getgroups(0, NULL);
1635 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1636 anum = getgroups(groups, gary);
1637 while (--anum >= 0) {
1638 if (gary[anum] == gid) {
1651# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1654#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1655#define USE_GETEUID 1
1660eaccess(
const char *path,
int mode)
1669 if (getuid() == euid && getgid() == getegid())
1670 return access(path, mode);
1672 if (STAT(path, &st) < 0)
1682 if (st.st_mode & S_IXUGO)
1688 if (st.st_uid == euid)
1690 else if (rb_group_member(st.st_gid))
1693 if ((
int)(st.st_mode & mode) == mode)
return 0;
1697 return access(path, mode);
1708nogvl_eaccess(
void *ptr)
1712 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1716rb_eaccess(
VALUE fname,
int mode)
1721 fname = rb_str_encode_ospath(fname);
1725 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1729nogvl_access(
void *ptr)
1733 return (
void *)(
VALUE)access(aa->path, aa->mode);
1737rb_access(
VALUE fname,
int mode)
1742 fname = rb_str_encode_ospath(fname);
1746 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1781# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1787 if (S_ISDIR(st.st_mode))
return Qtrue;
1808# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1814 if (S_ISFIFO(st.st_mode))
return Qtrue;
1837# define S_ISLNK(m) _S_ISLNK(m)
1840# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1843# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1853 fname = rb_str_encode_ospath(fname);
1855 if (S_ISLNK(st.st_mode))
return Qtrue;
1878# define S_ISSOCK(m) _S_ISSOCK(m)
1881# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1884# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1894 if (S_ISSOCK(st.st_mode))
return Qtrue;
1916# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1918# define S_ISBLK(m) (0)
1926 if (S_ISBLK(st.st_mode))
return Qtrue;
1946# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
1952 if (S_ISCHR(st.st_mode))
return Qtrue;
1991 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
2006rb_file_readable_real_p(
VALUE obj,
VALUE fname)
2008 return RBOOL(rb_access(fname, R_OK) >= 0);
2012# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
2016# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2036rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2042 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2043 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2063 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2078rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2080 return RBOOL(rb_access(fname, W_OK) >= 0);
2100rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2106 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2107 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2131 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2150rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2152 return RBOOL(rb_access(fname, X_OK) >= 0);
2156# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2177 return RBOOL(S_ISREG(st.st_mode));
2196 return RBOOL(st.st_size == 0);
2215 if (st.st_size == 0)
return Qnil;
2236 return RBOOL(st.st_uid == geteuid());
2245 return RBOOL(st.st_uid == getuid());
2266 if (rb_group_member(st.st_gid))
return Qtrue;
2271#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2273check3rdbyte(
VALUE fname,
int mode)
2278 return RBOOL(st.st_mode & mode);
2295 return check3rdbyte(fname, S_ISUID);
2314 return check3rdbyte(fname, S_ISGID);
2333 return check3rdbyte(fname, S_ISVTX);
2362 struct stat st1, st2;
2366 if (st1.st_dev != st2.st_dev)
return Qfalse;
2367 if (st1.st_ino != st2.st_ino)
return Qfalse;
2371 return rb_w32_file_identical_p(fname1, fname2);
2389 if (
rb_stat(fname, &st) < 0) {
2392 rb_syserr_fail_path(e, fname);
2398rb_file_ftype(mode_t mode)
2402 if (S_ISREG(mode)) {
2405 else if (S_ISDIR(mode)) {
2408 else if (S_ISCHR(mode)) {
2409 t =
"characterSpecial";
2412 else if (S_ISBLK(mode)) {
2417 else if (S_ISFIFO(mode)) {
2422 else if (S_ISLNK(mode)) {
2427 else if (S_ISSOCK(mode)) {
2435 return rb_fstring_cstr(t);
2459 fname = rb_str_encode_ospath(fname);
2461 rb_sys_fail_path(fname);
2464 return rb_file_ftype(st.st_mode);
2494 if (
rb_stat(fname, &st) < 0) {
2497 rb_syserr_fail_path(e, fname);
2499 return stat_time(stat_atimespec(&st));
2522rb_file_atime(
VALUE obj)
2528 if (fstat(fptr->
fd, &st) == -1) {
2529 rb_sys_fail_path(fptr->
pathv);
2531 return stat_time(stat_atimespec(&st));
2551 if (
rb_stat(fname, &st) < 0) {
2554 rb_syserr_fail_path(e, fname);
2556 return stat_time(stat_mtimespec(&st));
2570rb_file_mtime(
VALUE obj)
2576 if (fstat(fptr->
fd, &st) == -1) {
2577 rb_sys_fail_path(fptr->
pathv);
2579 return stat_time(stat_mtimespec(&st));
2603 if (
rb_stat(fname, &st) < 0) {
2606 rb_syserr_fail_path(e, fname);
2608 return stat_time(stat_ctimespec(&st));
2625rb_file_ctime(
VALUE obj)
2631 if (fstat(fptr->
fd, &st) == -1) {
2632 rb_sys_fail_path(fptr->
pathv);
2634 return stat_time(stat_ctimespec(&st));
2637#if defined(HAVE_STAT_BIRTHTIME)
2657 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
2660 rb_syserr_fail_path(e, fname);
2662 return statx_birthtime(&st);
2665# define rb_file_s_birthtime rb_f_notimplement
2668#if defined(HAVE_STAT_BIRTHTIME)
2682rb_file_birthtime(
VALUE obj)
2688 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
2689 rb_sys_fail_path(fptr->
pathv);
2691 return statx_birthtime(&st);
2694# define rb_file_birthtime rb_f_notimplement
2706 rb_io_flush_raw(file, 0);
2709 if (fstat(fptr->
fd, &st) == -1) {
2710 rb_sys_fail_path(fptr->
pathv);
2731file_size(
VALUE self)
2733 return OFFT2NUM(rb_file_size(self));
2742nogvl_chmod(
void *ptr)
2745 int ret = chmod(data->path, data->mode);
2746 return (
void *)(
VALUE)ret;
2750rb_chmod(
const char *path, mode_t mode)
2756 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
2760chmod_internal(
const char *path,
void *mode)
2762 return chmod(path, *(mode_t *)mode);
2786 return apply2files(chmod_internal, argc, argv, &mode);
2790struct nogvl_fchmod_data {
2796io_blocking_fchmod(
void *ptr)
2798 struct nogvl_fchmod_data *data = ptr;
2799 int ret = fchmod(data->fd, data->mode);
2804rb_fchmod(
struct rb_io* io, mode_t mode)
2807 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
2808 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
2830#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2838 if (rb_fchmod(fptr, mode) == -1) {
2839 if (HAVE_FCHMOD ||
errno != ENOSYS)
2840 rb_sys_fail_path(fptr->
pathv);
2843 if (!HAVE_FCHMOD)
return INT2FIX(0);
2846#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2848 path = rb_str_encode_ospath(fptr->
pathv);
2849 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
2850 rb_sys_fail_path(fptr->
pathv);
2856#if defined(HAVE_LCHMOD)
2858lchmod_internal(
const char *path,
void *mode)
2860 return lchmod(path, *(mode_t *)mode);
2881 return apply2files(lchmod_internal, argc, argv, &mode);
2884#define rb_file_s_lchmod rb_f_notimplement
2887static inline rb_uid_t
2891 return (rb_uid_t)-1;
2896static inline rb_gid_t
2900 return (rb_gid_t)-1;
2911chown_internal(
const char *path,
void *arg)
2914 return chown(path, args->owner, args->group);
2938 arg.owner = to_uid(*argv++);
2939 arg.group = to_gid(*argv++);
2941 return apply2files(chown_internal, argc, argv, &arg);
2953nogvl_chown(
void *ptr)
2956 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
2960rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
2963 .as = {.path = path},
2964 .new = {.owner = owner, .group = group},
2966 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
2971nogvl_fchown(
void *ptr)
2974 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
2978rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
2983 .new = {.owner = owner, .group = group},
2985 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
3019 path = rb_str_encode_ospath(fptr->
pathv);
3020 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
3021 rb_sys_fail_path(fptr->
pathv);
3023 if (rb_fchown(fptr->
fd, o, g) == -1)
3024 rb_sys_fail_path(fptr->
pathv);
3030#if defined(HAVE_LCHOWN)
3032lchown_internal(
const char *path,
void *arg)
3035 return lchown(path, args->owner, args->group);
3055 arg.owner = to_uid(*argv++);
3056 arg.group = to_gid(*argv++);
3058 return apply2files(lchown_internal, argc, argv, &arg);
3061#define rb_file_s_lchown rb_f_notimplement
3071NORETURN(
static void utime_failed(
struct apply_arg *));
3077 VALUE path = aa->fn[aa->i].path;
3080 if (ua->tsp && e == EINVAL) {
3083 VALUE atime = ua->atime;
3084 VALUE mtime = ua->mtime;
3086 if (!
NIL_P(atime)) {
3089 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3092 if (
NIL_P(a)) e[0] = m;
3108 rb_syserr_fail_path(e, path);
3112#if defined(HAVE_UTIMES)
3114# if !defined(HAVE_UTIMENSAT)
3116# elif defined(__APPLE__) && \
3117 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3119# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3120typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3124static inline utimensat_func *
3131# define utimensat rb_utimensat()
3138utime_internal(
const char *path,
void *arg)
3141 const struct timespec *tsp = v->tsp;
3142 struct timeval tvbuf[2], *tvp = NULL;
3144#if defined(HAVE_UTIMENSAT)
3145# if defined(__APPLE__)
3146 const int try_utimensat = utimensat != NULL;
3147 const int try_utimensat_follow = utimensat != NULL;
3149# define TRY_UTIMENSAT 1
3150 static int try_utimensat = 1;
3151# ifdef AT_SYMLINK_NOFOLLOW
3152 static int try_utimensat_follow = 1;
3154 const int try_utimensat_follow = 0;
3159 if (v->follow ? try_utimensat_follow : try_utimensat) {
3160# ifdef AT_SYMLINK_NOFOLLOW
3162 flags = AT_SYMLINK_NOFOLLOW;
3166 int result = utimensat(AT_FDCWD, path, tsp, flags);
3167# ifdef TRY_UTIMENSAT
3168 if (result < 0 &&
errno == ENOSYS) {
3169# ifdef AT_SYMLINK_NOFOLLOW
3170 try_utimensat_follow = 0;
3182 tvbuf[0].tv_sec = tsp[0].tv_sec;
3183 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3184 tvbuf[1].tv_sec = tsp[1].tv_sec;
3185 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3189 if (v->follow)
return lutimes(path, tvp);
3191 return utimes(path, tvp);
3196#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3204utime_internal(
const char *path,
void *arg)
3208 struct utimbuf utbuf, *utp = NULL;
3210 utbuf.actime = tsp[0].tv_sec;
3211 utbuf.modtime = tsp[1].tv_sec;
3214 return utime(path, utp);
3219utime_internal_i(
int argc,
VALUE *argv,
int follow)
3222 struct timespec tss[2], *tsp = NULL;
3225 args.atime = *argv++;
3226 args.mtime = *argv++;
3228 args.follow = follow;
3230 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3233 if (args.atime == args.mtime)
3240 return apply2files(utime_internal, argc, argv, &args);
3257 return utime_internal_i(argc, argv, FALSE);
3260#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3276 return utime_internal_i(argc, argv, TRUE);
3279#define rb_file_s_lutime rb_f_notimplement
3282#ifdef RUBY_FUNCTION_NAME_STRING
3283# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3285# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3287#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3288NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3290syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3294 const int max_pathlen = MAX_PATH;
3296 const int max_pathlen = MAXPATHLEN;
3307#ifdef RUBY_FUNCTION_NAME_STRING
3308 rb_syserr_fail_path_in(func, e, str);
3310 rb_syserr_fail_path(e, str);
3332 from = rb_str_encode_ospath(from);
3333 to = rb_str_encode_ospath(to);
3336 sys_fail2(from, to);
3341#define rb_file_s_link rb_f_notimplement
3362 from = rb_str_encode_ospath(from);
3363 to = rb_str_encode_ospath(to);
3366 sys_fail2(from, to);
3371#define rb_file_s_symlink rb_f_notimplement
3389 return rb_readlink(path, rb_filesystem_encoding());
3392struct readlink_arg {
3399nogvl_readlink(
void *ptr)
3401 struct readlink_arg *ra = ptr;
3403 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
3407readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
3409 struct readlink_arg ra;
3411 ra.path = RSTRING_PTR(path);
3412 ra.buf = RSTRING_PTR(buf);
3415 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
3426 path = rb_str_encode_ospath(path);
3427 v = rb_enc_str_new(0, size, enc);
3428 while ((rv = readlink_without_gvl(path, v, size)) == size
3430 || (rv < 0 &&
errno == ERANGE)
3439 rb_str_resize(v, 0);
3440 rb_syserr_fail_path(e, path);
3442 rb_str_resize(v, rv);
3447#define rb_file_s_readlink rb_f_notimplement
3451unlink_internal(
const char *path,
void *arg)
3453 return unlink(path);
3473rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
3475 return apply2files(unlink_internal, argc, argv, 0);
3484no_gvl_rename(
void *ptr)
3488 return (
void *)(
VALUE)rename(ra->src, ra->dst);
3509 f = rb_str_encode_ospath(from);
3510 t = rb_str_encode_ospath(to);
3513#if defined __CYGWIN__
3516 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
3521 if (chmod(ra.dst, 0666) == 0 &&
3522 unlink(ra.dst) == 0 &&
3523 rename(ra.src, ra.dst) == 0)
3527 syserr_fail2(e, from, to);
3562 rb_error_arity(argc, 0, 1);
3570#if defined __CYGWIN__ || defined DOSISH
3572#define DOSISH_DRIVE_LETTER
3573#define FILE_ALT_SEPARATOR '\\'
3575#ifdef FILE_ALT_SEPARATOR
3576#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
3578static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
3581#define isdirsep(x) ((x) == '/')
3594# define USE_NTFS_ADS 1
3596# define USE_NTFS_ADS 0
3601#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
3603#define istrailinggarbage(x) 0
3607# define isADS(x) ((x) == ':')
3612#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
3613#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
3615#if defined(DOSISH_UNC)
3616#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
3618#define has_unc(buf) 0
3621#ifdef DOSISH_DRIVE_LETTER
3623has_drive_letter(
const char *buf)
3625 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
3638 char *drvcwd, *oldcwd;
3649 if (chdir(drive) == 0) {
3662not_same_drive(
VALUE path,
int drive)
3664 const char *p = RSTRING_PTR(path);
3665 if (RSTRING_LEN(path) < 2)
return 0;
3666 if (has_drive_letter(p)) {
3677skiproot(
const char *path,
const char *end)
3679#ifdef DOSISH_DRIVE_LETTER
3680 if (path + 2 <= end && has_drive_letter(path)) path += 2;
3682 while (path < end && isdirsep(*path)) path++;
3683 return (
char *)path;
3687enc_path_next(
const char *s,
const char *e,
bool mb_enc,
rb_encoding *enc)
3689 while (s < e && !isdirsep(*s)) {
3690 Inc(s, e, mb_enc, enc);
3695#define nextdirsep rb_enc_path_next
3699 return enc_path_next(s, e,
true, enc);
3702#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3703#define skipprefix enc_path_skip_prefix
3705#define skipprefix(path, end, mb_enc, enc) (path)
3708enc_path_skip_prefix(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3710#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3712 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
3714 while (path < end && isdirsep(*path)) path++;
3715 if ((path = enc_path_next(path, end, mb_enc, enc)) < end &&
3716 path + 2 <= end && !isdirsep(path[1])) {
3717 path = enc_path_next(path + 1, end, mb_enc, enc);
3719 return (
char *)path;
3722#ifdef DOSISH_DRIVE_LETTER
3723 if (path + 2 <= end && has_drive_letter(path))
3724 return (
char *)(path + 2);
3727 return (
char *)path;
3731rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
3733 return enc_path_skip_prefix(path, end,
true, enc);
3737skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
3739#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3740 char *p = skipprefix(path, end,
true, enc);
3741 while (isdirsep(*p)) p++;
3744 return skiproot(path, end);
3750enc_path_last_separator(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3753 while (path < end) {
3754 if (isdirsep(*path)) {
3755 const char *tmp = path++;
3756 while (path < end && isdirsep(*path)) path++;
3757 if (path >= end)
break;
3761 Inc(path, end, mb_enc, enc);
3767rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
3769 return enc_path_last_separator(path, end,
true, enc);
3773strrdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3775 if (RB_UNLIKELY(mb_enc)) {
3776 return enc_path_last_separator(path, end, mb_enc, enc);
3779 const char *cursor = end - 1;
3781 while (isdirsep(cursor[0])) {
3785 while (cursor >= path) {
3786 if (isdirsep(cursor[0])) {
3787 while (cursor > path && isdirsep(cursor[-1])) {
3790 return (
char *)cursor;
3798chompdirsep(
const char *path,
const char *end,
bool mb_enc,
rb_encoding *enc)
3800 while (path < end) {
3801 if (isdirsep(*path)) {
3802 const char *last = path++;
3803 while (path < end && isdirsep(*path)) path++;
3804 if (path >= end)
return (
char *)last;
3807 Inc(path, end, mb_enc, enc);
3810 return (
char *)path;
3816 if (path < end && isdirsep(*path)) path++;
3817 return chompdirsep(path, end,
true, enc);
3823 rb_encoding *enc = rb_enc_check_str(path1, path2);
3824 int encidx = rb_enc_to_index(enc);
3825 if (encidx == ENCINDEX_US_ASCII) {
3826 encidx = rb_enc_get_index(path1);
3827 if (encidx == ENCINDEX_US_ASCII)
3828 encidx = rb_enc_get_index(path2);
3829 enc = rb_enc_from_index(encidx);
3836ntfs_tail(
const char *path,
const char *end,
rb_encoding *enc)
3838 while (path < end && *path ==
'.') path++;
3839 while (path < end && !isADS(*path)) {
3840 if (istrailinggarbage(*path)) {
3841 const char *last = path++;
3842 while (path < end && istrailinggarbage(*path)) path++;
3843 if (path >= end || isADS(*path))
return (
char *)last;
3845 else if (isdirsep(*path)) {
3846 const char *last = path++;
3847 while (path < end && isdirsep(*path)) path++;
3848 if (path >= end)
return (
char *)last;
3849 if (isADS(*path)) path++;
3852 Inc(path, end,
true, enc);
3855 return (
char *)path;
3859#define BUFCHECK(cond) do {\
3862 do {buflen *= 2;} while (cond);\
3863 rb_str_resize(result, buflen);\
3864 buf = RSTRING_PTR(result);\
3866 pend = buf + buflen;\
3871 p = buf = RSTRING_PTR(result),\
3872 buflen = RSTRING_LEN(result),\
3876# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
3878# define SKIPPATHSEP(p) 1
3881#define BUFCOPY(srcptr, srclen) do { \
3882 const int skip = SKIPPATHSEP(p); \
3883 rb_str_set_len(result, p-buf+skip); \
3884 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
3886 memcpy(p, (srcptr), (srclen)); \
3890#define WITH_ROOTDIFF(stmt) do { \
3891 long rootdiff = root - buf; \
3893 root = buf + rootdiff; \
3897copy_home_path(
VALUE result,
const char *dir)
3900#if defined DOSISH || defined __CYGWIN__
3907 dirlen = strlen(dir);
3908 rb_str_resize(result, dirlen);
3909 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
3910 encidx = rb_filesystem_encindex();
3911 rb_enc_associate_index(result, encidx);
3912#if defined DOSISH || defined __CYGWIN__
3913 enc = rb_enc_from_index(encidx);
3914 for (bend = (p = buf) + dirlen; p < bend; Inc(p, bend,
true, enc)) {
3927 VALUE dirname = rb_getpwdirnam_for_login(user);
3928 if (dirname ==
Qnil) {
3929 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3931 const char *dir = RSTRING_PTR(dirname);
3933 extern char *getlogin(
void);
3934 const char *pwPtr = 0;
3936 # define endpwent() ((void)0)
3937 const char *dir, *username = RSTRING_PTR(user);
3948 if ((login = getlogin()) && strcasecmp(username, login) == 0)
3949 dir = pwPtr = getenv(
"HOME");
3951 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3954 copy_home_path(result, dir);
3960rb_default_home_dir(
VALUE result)
3962 const char *dir = getenv(
"HOME");
3964#if defined HAVE_PWD_H
3978 VALUE login_name = rb_getlogin();
3980# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
3985 if (
NIL_P(login_name)) {
3986 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
3990 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
3991 if (
NIL_P(pw_dir)) {
3992 pw_dir = rb_getpwdiruid();
3993 if (
NIL_P(pw_dir)) {
3994 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
3999 copy_home_path(result, RSTRING_PTR(pw_dir));
4000 rb_str_resize(pw_dir, 0);
4005 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
4007 return copy_home_path(result, dir);
4013#if NORMALIZE_UTF8PATH
4014 VALUE path = rb_str_normalize_ospath(ptr,
len);
4015 rb_enc_associate(path, fsenc);
4018 return rb_enc_str_new(ptr,
len, fsenc);
4025 char *buf, *cwdp = dir;
4029 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
4030 dirname = ospath_new(dir, dirlen, fsenc);
4031 if (!rb_enc_compatible(fname, dirname)) {
4035 rb_enc_check(fname, dirname);
4036 rb_bug(
"unreachable");
4038 rb_encoding *direnc = fs_enc_check(fname, dirname);
4039 if (direnc != fsenc) {
4043 else if (NORMALIZE_UTF8PATH) {
4048 do {buflen *= 2;}
while (dirlen > buflen);
4049 rb_str_resize(result, buflen);
4050 buf = RSTRING_PTR(result);
4051 memcpy(buf, cwdp, dirlen);
4053 if (!
NIL_P(dirname)) rb_str_resize(dirname, 0);
4054 rb_enc_associate(result, *enc);
4055 return buf + dirlen;
4059rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
4061 const char *s, *b, *fend;
4062 char *buf, *p, *pend, *root;
4063 size_t buflen, bdiff;
4064 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
4067 fend = s + RSTRING_LEN(fname);
4068 enc = rb_str_enc_get(fname);
4069 bool mb_enc = !rb_str_encindex_fastpath(rb_enc_to_index(enc));
4070 if (!mb_enc &&
RTEST(dname)) {
4071 mb_enc = !rb_str_encindex_fastpath(rb_enc_to_index(rb_str_enc_get(dname)));
4076 if (s < fend && s[0] ==
'~' && abs_mode == 0) {
4078 if (s + 1 == fend || isdirsep(s[1])) {
4082 if (++s < fend) ++s;
4083 rb_default_home_dir(result);
4086 s = nextdirsep(b = s, fend, enc);
4089 BUFCHECK(bdiff + userlen >= buflen);
4090 memcpy(p, b, userlen);
4093 rb_enc_associate(result, enc);
4094 rb_home_dir_of(result, result);
4098 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
4100 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
4101 (
int)userlen, b, fname);
4104 rb_raise(rb_eArgError,
"non-absolute home");
4110#ifdef DOSISH_DRIVE_LETTER
4112 else if (s + 1 < fend && has_drive_letter(s)) {
4113 if (s + 2 < fend && isdirsep(s[2])) {
4116 BUFCHECK(bdiff + 2 >= buflen);
4120 rb_enc_copy(result, fname);
4125 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4126 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4134 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4139 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4142 p = chompdirsep(skiproot(buf, p), p,
true, enc);
4147 else if (s == fend || !rb_is_absolute_path(s)) {
4148 if (!
NIL_P(dname)) {
4149 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4150 rb_enc_associate(result, fs_enc_check(result, fname));
4155 char *e = append_fspath(result, fname,
ruby_getcwd(), &enc, fsenc);
4159#if defined DOSISH || defined __CYGWIN__
4160 if (s < fend && isdirsep(*s)) {
4163 p = skipprefix(buf, p,
true, enc);
4167 p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
4172 do s++;
while (s < fend && isdirsep(*s));
4175 BUFCHECK(bdiff >= buflen);
4176 memset(buf,
'/',
len);
4178 rb_enc_associate(result, fs_enc_check(result, fname));
4180 if (p > buf && p[-1] ==
'/')
4184 BUFCHECK(bdiff + 1 >= buflen);
4189 BUFCHECK(bdiff + 1 >= buflen);
4191 root = skipprefix(buf, p+1, mb_enc, enc);
4204 if (s+1 == fend || isdirsep(*(s+1))) {
4208 if (!(n = strrdirsep(root, p, mb_enc, enc))) {
4218 do ++s;
while (s < fend && istrailinggarbage(*s));
4223#if defined DOSISH || defined __CYGWIN__
4238 while (s < fend && istrailinggarbage(*s)) s++;
4248#if defined DOSISH || defined __CYGWIN__
4252 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4260 int n = ignored_char_p(s, fend, enc);
4263 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4271 Inc(s, fend, mb_enc, enc);
4279 static const char prime[] =
":$DATA";
4280 enum {prime_len =
sizeof(prime) -1};
4284 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) {
4287 if (isADS(*(s - (prime_len+1)))) {
4290 else if (memchr(b,
':', s - prime_len - b)) {
4299 if (p == skiproot(buf, p + !!*p) - 1) p++;
4303 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s,
"*?")) {
4308 WIN32_FIND_DATAW wfd;
4311#ifdef HAVE_CYGWIN_CONV_PATH
4312 char *w32buf = NULL;
4313 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
4315 char w32buf[MAXPATHLEN];
4319 int lnk_added = 0, is_symlink = 0;
4323 if (lstat_without_gvl(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
4329 path = *buf ? buf :
"/";
4330#ifdef HAVE_CYGWIN_CONV_PATH
4331 bufsize = cygwin_conv_path(flags, path, NULL, 0);
4334 if (lnk_added) bufsize += 4;
4336 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
4341 bufsize = MAXPATHLEN;
4342 if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
4346 if (is_symlink && b == w32buf) {
4348 strlcat(w32buf, p, bufsize);
4350 strlcat(w32buf,
".lnk", bufsize);
4361 if (encidx != ENCINDEX_UTF_8 && !is_ascii_string(result)) {
4362 tmp = rb_str_encode_ospath(result);
4364 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0);
4366 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr,
len);
4368 h = FindFirstFileW(wstr, &wfd);
4370 if (h != INVALID_HANDLE_VALUE) {
4373 len = lstrlenW(wfd.cFileName);
4375 if (lnk_added &&
len > 4 &&
4376 wcscasecmp(wfd.cFileName +
len - 4, L
".lnk") == 0) {
4377 wfd.cFileName[
len -= 4] = L
'\0';
4384 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL);
4385 if (tmp == result) {
4386 BUFCHECK(bdiff +
len >= buflen);
4387 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p,
len + 1, NULL, NULL);
4391 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, RSTRING_PTR(tmp),
len + 1, NULL, NULL);
4392 rb_str_cat_conv_enc_opts(result, bdiff, RSTRING_PTR(tmp),
len,
4393 rb_utf8_encoding(), 0,
Qnil);
4395 rb_str_resize(tmp, 0);
4408 rb_enc_check(fname, result);
4414#define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, 1)
4417str_shrink(
VALUE str)
4419 rb_str_resize(str, RSTRING_LEN(str));
4423#define expand_path(fname, dname, abs_mode, long_name, result) \
4424 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
4426#define check_expand_path_args(fname, dname) \
4427 (((fname) = rb_get_path(fname)), \
4428 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
4431file_expand_path_1(
VALUE fname)
4433 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, EXPAND_PATH_BUFFER());
4439 check_expand_path_args(fname, dname);
4440 return expand_path(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
4444rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
4446 return expand_path(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
4450rb_file_s_expand_path(
int argc,
const VALUE *argv)
4453 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4487 return rb_file_s_expand_path(c, v);
4493 check_expand_path_args(fname, dname);
4494 return expand_path(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
4498rb_file_s_absolute_path(
int argc,
const VALUE *argv)
4501 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4520 return rb_file_s_absolute_path(c, v);
4536 VALUE path = rb_get_path(fname);
4538 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
4542enum rb_realpath_mode {
4546 RB_REALPATH_MODE_MAX
4550realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
4551 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
4553 const char *pend = unresolved + strlen(unresolved);
4557 while (unresolved < pend) {
4558 const char *testname = unresolved;
4559 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
4560 long testnamelen = unresolved_firstsep - unresolved;
4561 const char *unresolved_nextname = unresolved_firstsep;
4562 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
4563 unresolved_nextname++;
4564 unresolved = unresolved_nextname;
4565 if (testnamelen == 1 && testname[0] ==
'.') {
4567 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
4568 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
4569 const char *resolved_str = RSTRING_PTR(*resolvedp);
4570 const char *resolved_names = resolved_str + *prefixlenp;
4571 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp),
true, enc);
4572 long len = lastsep ? lastsep - resolved_names : 0;
4573 rb_str_resize(*resolvedp, *prefixlenp +
len);
4579 if (*prefixlenp < RSTRING_LEN(testpath))
4581#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4582 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
4583 const char *prefix = RSTRING_PTR(testpath);
4584 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
4589 checkval = rb_hash_aref(loopcheck, testpath);
4590 if (!
NIL_P(checkval)) {
4591 if (checkval ==
ID2SYM(resolving)) {
4592 if (mode == RB_REALPATH_CHECK) {
4596 rb_syserr_fail_path(ELOOP, testpath);
4605 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
4608 if (e == ENOENT && !
NIL_P(fallback)) {
4609 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
4614 if (mode == RB_REALPATH_CHECK)
return -1;
4616 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
4617 rb_syserr_fail_path(e, testpath);
4618 *resolvedp = testpath;
4622 rb_syserr_fail_path(e, testpath);
4626 if (S_ISLNK(sbuf.st_mode)) {
4629 const char *link_prefix, *link_names;
4630 long link_prefixlen;
4631 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
4632 link = rb_readlink(testpath, enc);
4633 link_prefix = RSTRING_PTR(link);
4634 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
4635 link_prefixlen = link_names - link_prefix;
4636 if (link_prefixlen > 0) {
4640 tmpenc = fs_enc_check(*resolvedp, link);
4643 *prefixlenp = link_prefixlen;
4645 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
4646 loopcheck, mode, !*unresolved_firstsep))
4655 rb_hash_aset(loopcheck, s, s);
4656 *resolvedp = testpath;
4665rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4669 VALUE unresolved_path;
4674 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
4675 char *ptr, *prefixptr = NULL, *pend;
4680 if (!
NIL_P(basedir)) {
4685 enc = rb_enc_get(unresolved_path);
4686 unresolved_path = TO_OSPATH(unresolved_path);
4688 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
4689 if (ptr != path_names) {
4690 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
4694 if (!
NIL_P(basedir)) {
4696 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
4697 if (ptr != basedir_names) {
4703 curdir = rb_dir_getwd_ospath();
4705 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
4710 pend = prefixptr + prefixlen;
4711 ptr = chompdirsep(prefixptr, pend,
true, enc);
4713 prefixlen = ++ptr - prefixptr;
4716#ifdef FILE_ALT_SEPARATOR
4717 while (prefixptr < ptr) {
4718 if (*prefixptr == FILE_ALT_SEPARATOR) {
4721 Inc(prefixptr, pend,
true, enc);
4725 switch (rb_enc_to_index(enc)) {
4726 case ENCINDEX_ASCII_8BIT:
4727 case ENCINDEX_US_ASCII:
4728 rb_enc_associate_index(resolved, rb_filesystem_encindex());
4731 loopcheck = rb_hash_new();
4733 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
4736 if (basedir_names) {
4737 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
4740 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
4743 if (origenc && origenc != rb_enc_get(resolved)) {
4745 rb_enc_associate(resolved, origenc);
4757static VALUE rb_file_join(
long argc,
VALUE *args);
4759#ifndef HAVE_REALPATH
4761rb_check_realpath_emulate_try(
VALUE arg)
4764 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
4768rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
4772#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
4773 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
4775# include <sys/syslimits.h>
4776# define NEEDS_REALPATH_BUFFER 1
4780rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4783 VALUE unresolved_path;
4784 char *resolved_ptr = NULL;
4786# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
4787 char resolved_buffer[PATH_MAX];
4789 char *
const resolved_buffer = NULL;
4792 if (mode == RB_REALPATH_DIR) {
4793 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4797 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
4798 VALUE paths[2] = {basedir, unresolved_path};
4799 unresolved_path = rb_file_join(2, paths);
4801 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
4803 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
4812 if (
errno == ENOTSUP ||
4814 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
4815 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4818 if (mode == RB_REALPATH_CHECK) {
4821 rb_sys_fail_path(unresolved_path);
4823 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
4824# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
4828# if !defined(__linux__) && !defined(__APPLE__)
4832 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
4833 if (mode == RB_REALPATH_CHECK) {
4836 rb_sys_fail_path(unresolved_path);
4840 if (origenc && origenc != rb_enc_get(resolved)) {
4844 rb_enc_associate(resolved, origenc);
4847 if (is_broken_string(resolved)) {
4848 rb_enc_associate(resolved, rb_filesystem_encoding());
4849 if (is_broken_string(resolved)) {
4850 rb_enc_associate(resolved, rb_ascii8bit_encoding());
4857 if (mode == RB_REALPATH_CHECK) {
4861 arg[2] = (
VALUE)origenc;
4864 rb_check_realpath_emulate_rescue,
Qnil);
4867 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4873rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
4875 const enum rb_realpath_mode mode =
4876 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
4877 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
4883 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
4900rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
4903 VALUE path = argv[0];
4905 return rb_realpath_internal(basedir, path, 1);
4921rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
4924 VALUE path = argv[0];
4926 return rb_realpath_internal(basedir, path, 0);
4930rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
4934 const char *s, *last;
4936 if (!e || !l2)
return 0;
4938 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
4939 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
4940 if (c ==
'.')
return l0;
4945 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
4950 if (l1 < l2)
return l1;
4953 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
4954#if CASEFOLD_FILESYSTEM
4955#define fncomp strncasecmp
4957#define fncomp strncmp
4959 if (fncomp(s, e, l2) == 0) {
4965static inline const char *
4966enc_find_basename(
const char *name,
long *baselen,
long *alllen,
bool mb_enc,
rb_encoding *enc)
4968 const char *p, *q, *e, *end;
4969#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4974 long len = (alllen ? (size_t)*alllen : strlen(name));
4981 name = skipprefix(name, end, mb_enc, enc);
4982#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4986 while (name < end && isdirsep(*name)) {
4993#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4997#ifdef DOSISH_DRIVE_LETTER
4998 else if (*p ==
':') {
5011 p = strrdirsep(name, end, mb_enc, enc);
5016 while (isdirsep(*p)) {
5021 n = ntfs_tail(p, end, enc) - p;
5023 n = chompdirsep(p, end, mb_enc, enc) - p;
5025 for (q = p; q - p < n && *q ==
'.'; q++);
5026 for (e = 0; q - p < n; Inc(q, end, mb_enc, enc)) {
5027 if (*q ==
'.') e = q;
5047ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
5049 return enc_find_basename(name, baselen, alllen,
true, enc);
5073 const char *name, *p, *fp = 0;
5079 CheckPath(fname, name);
5083 check_path_encoding(fext);
5085 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
5086 enc = rb_str_enc_get(fname);
5089 n = RSTRING_LEN(fname);
5090 if (n <= 0 || !*name) {
5091 return rb_enc_str_new(0, 0, enc);
5094 bool mb_enc = !rb_str_encindex_fastpath(rb_enc_to_index(enc));
5095 p = enc_find_basename(name, &f, &n, mb_enc, enc);
5101 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
5106 if (f == RSTRING_LEN(fname)) {
5111 return rb_enc_str_new(p, f, enc);
5114static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5136rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5142 return rb_file_dirname_n(argv[0], n);
5148 return rb_file_dirname_n(fname, 1);
5152rb_file_dirname_n(
VALUE fname,
int n)
5154 const char *name, *root, *p, *end;
5157 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5158 CheckPath(fname, name);
5159 end = name + RSTRING_LEN(fname);
5161 bool mb_enc = !rb_str_enc_fastpath(fname);
5164 root = skiproot(name, end);
5166 if (root > name + 1 && isdirsep(*name))
5167 root = skipprefix(name = root - 2, end, mb_enc, enc);
5169 if (root > name + 1)
5172 if (n > (end - root + 1) / 2) {
5178 if (!(p = strrdirsep(root, p, mb_enc, enc))) {
5187 return rb_enc_str_new(
".", 1, enc);
5189#ifdef DOSISH_DRIVE_LETTER
5190 if (name + 3 < end && has_drive_letter(name) && isdirsep(*(name + 2))) {
5191 const char *top = skiproot(name + 2, end);
5192 dirname = rb_enc_str_new(name, 3, enc);
5197 dirname = rb_enc_str_new(name, p - name, enc);
5198#ifdef DOSISH_DRIVE_LETTER
5199 if (root == name + 2 && p == root && name[1] ==
':')
5205static inline const char *
5206enc_find_extname(
const char *name,
long *
len,
bool mb_enc,
rb_encoding *enc)
5208 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5210 p = strrdirsep(name, end, mb_enc, enc);
5214 do name = ++p;
while (isdirsep(*p));
5217 while (*p && *p ==
'.') p++;
5219 if (*p ==
'.' || istrailinggarbage(*p)) {
5221 const char *last = p++, *dot = last;
5222 while (istrailinggarbage(*p)) {
5223 if (*p ==
'.') dot = p;
5226 if (!*p || isADS(*p)) {
5230 if (*last ==
'.' || dot > last) e = dot;
5237 else if (isADS(*p)) {
5241 else if (isdirsep(*p))
5243 Inc(p, end, mb_enc, enc);
5248 if (!e || e == name)
5273 return enc_find_extname(name,
len,
true, enc);
5306 CheckPath(fname, name);
5307 long len = RSTRING_LEN(fname);
5310 return rb_enc_str_new(0, 0, rb_str_enc_get(fname));
5313 bool mb_enc = !rb_str_enc_fastpath(fname);
5316 const char *ext = enc_find_extname(name, &
len, mb_enc, enc);
5317 return rb_enc_str_new(ext,
len, enc);
5350 return rb_get_path(fname);
5374file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
5376 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
5377 return rb_file_join_ary(arg);
5381rb_file_join_ary(
VALUE ary)
5385 const char *name, *tail;
5395 check_path_encoding(tmp);
5396 len += RSTRING_LEN(tmp);
5404 RBASIC_CLEAR_CLASS(result);
5407 switch (OBJ_BUILTIN_TYPE(tmp)) {
5409 if (!checked) check_path_encoding(tmp);
5414 rb_raise(rb_eArgError,
"recursive array");
5426 rb_enc_copy(result, tmp);
5429 tail = chompdirsep(name, name +
len,
true, rb_enc_get(result));
5430 if (RSTRING_PTR(tmp) && isdirsep(RSTRING_PTR(tmp)[0])) {
5437 enc = fs_enc_check(result, tmp);
5439 rb_enc_associate(result, enc);
5447rb_file_join_fastpath(
long argc,
VALUE *args)
5452 for (i = 0; i < argc; i++) {
5453 VALUE tmp = args[i];
5455 size += RSTRING_LEN(tmp);
5468 const char *name = RSTRING_PTR(result);
5469 for (i = 1; i < argc; i++) {
5470 VALUE tmp = args[i];
5471 long len = RSTRING_LEN(result);
5477 if (isdirsep(tmp_s[0])) {
5479 long trailing_seps = 0;
5480 while (isdirsep(name[
len - trailing_seps - 1])) {
5485 else if (!isdirsep(name[
len - 1])) {
5492 rb_enc_associate(result, new_enc);
5493 encidx = rb_enc_to_index(new_enc);
5499 rb_str_null_check(result);
5504rb_file_join(
long argc,
VALUE *args)
5506 if (RB_UNLIKELY(argc == 0)) {
5510 VALUE result = rb_file_join_fastpath(argc, args);
5511 if (RB_LIKELY(result)) {
5529rb_file_s_join(
int argc,
VALUE *argv,
VALUE klass)
5531 return rb_file_join(argc, argv);
5534#if defined(HAVE_TRUNCATE)
5535struct truncate_arg {
5541nogvl_truncate(
void *ptr)
5543 struct truncate_arg *ta = ptr;
5544 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
5565 struct truncate_arg ta;
5570 path = rb_str_encode_ospath(path);
5573 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
5575 rb_sys_fail_path(path);
5579#define rb_file_s_truncate rb_f_notimplement
5582#if defined(HAVE_FTRUNCATE)
5583struct ftruncate_arg {
5589nogvl_ftruncate(
void *ptr)
5591 struct ftruncate_arg *fa = ptr;
5593 return (
VALUE)ftruncate(fa->fd, fa->pos);
5614 struct ftruncate_arg fa;
5621 rb_io_flush_raw(obj, 0);
5623 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
5624 rb_sys_fail_path(fptr->
pathv);
5629#define rb_file_truncate rb_f_notimplement
5646#include <winerror.h>
5650rb_thread_flock(
void *data)
5653 int old_errno =
errno;
5655 int *op = data, ret = flock(op[0], op[1]);
5658 if (GetLastError() == ERROR_NOT_LOCKED) {
5716 op[1] = op1 =
NUM2INT(operation);
5721 rb_io_flush_raw(obj, 0);
5723 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
5728#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
5731 if (op1 & LOCK_NB)
return Qfalse;
5734 time.tv_usec = 100 * 1000;
5740#if defined(ERESTART)
5746 rb_syserr_fail_path(e, fptr->
pathv);
5753test_check(
int n,
int argc,
VALUE *argv)
5759 for (i=1; i<n; i++) {
5766#define CHECK(n) test_check((n), argc, argv)
5858 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
5862 return rb_file_blockdev_p(0, argv[1]);
5865 return rb_file_chardev_p(0, argv[1]);
5868 return rb_file_directory_p(0, argv[1]);
5871 return rb_file_exist_p(0, argv[1]);
5874 return rb_file_file_p(0, argv[1]);
5877 return rb_file_sgid_p(0, argv[1]);
5880 return rb_file_grpowned_p(0, argv[1]);
5883 return rb_file_sticky_p(0, argv[1]);
5886 return rb_file_symlink_p(0, argv[1]);
5889 return rb_file_owned_p(0, argv[1]);
5892 return rb_file_rowned_p(0, argv[1]);
5895 return rb_file_pipe_p(0, argv[1]);
5898 return rb_file_readable_p(0, argv[1]);
5901 return rb_file_readable_real_p(0, argv[1]);
5904 return rb_file_size_p(0, argv[1]);
5907 return rb_file_socket_p(0, argv[1]);
5910 return rb_file_suid_p(0, argv[1]);
5913 return rb_file_writable_p(0, argv[1]);
5916 return rb_file_writable_real_p(0, argv[1]);
5919 return rb_file_executable_p(0, argv[1]);
5922 return rb_file_executable_real_p(0, argv[1]);
5925 return rb_file_zero_p(0, argv[1]);
5929 if (strchr(
"MAC", cmd)) {
5931 VALUE fname = argv[1];
5934 if (
rb_stat(fname, &st) == -1) {
5937 rb_syserr_fail_path(e, fname);
5942 return stat_atime(&st);
5944 return stat_mtime(&st);
5946 return stat_ctime(&st);
5952 return rb_file_identical_p(0, argv[1], argv[2]);
5955 if (strchr(
"=<>", cmd)) {
5956 struct stat st1, st2;
5963 t1 = stat_mtimespec(&st1);
5964 t2 = stat_mtimespec(&st2);
5968 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
5972 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
5973 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
5977 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
5978 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
5985 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
5988 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
6042rb_stat_s_alloc(
VALUE klass)
6063 fname = rb_str_encode_ospath(fname);
6065 rb_sys_fail_path(fname);
6072 rb_st->initialized =
true;
6089 *copy_rb_st = *orig_rb_st;
6108rb_stat_ftype(
VALUE obj)
6110 return rb_file_ftype(get_stat(obj)->ST_(mode));
6127 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
6143 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
6169 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
6190 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
6213 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
6234 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
6252rb_stat_owned(
VALUE obj)
6254 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
6259rb_stat_rowned(
VALUE obj)
6261 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
6278rb_stat_grpowned(
VALUE obj)
6281 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
6300 rb_io_stat_data *st = get_stat(obj);
6303 if (geteuid() == 0)
return Qtrue;
6306 if (rb_stat_owned(obj))
6307 return RBOOL(st->ST_(mode) & S_IRUSR);
6310 if (rb_stat_grpowned(obj))
6311 return RBOOL(st->ST_(mode) & S_IRGRP);
6314 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6333 rb_io_stat_data *st = get_stat(obj);
6336 if (getuid() == 0)
return Qtrue;
6339 if (rb_stat_rowned(obj))
6340 return RBOOL(st->ST_(mode) & S_IRUSR);
6343 if (rb_group_member(get_stat(obj)->ST_(gid)))
6344 return RBOOL(st->ST_(mode) & S_IRGRP);
6347 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6366rb_stat_wr(
VALUE obj)
6369 rb_io_stat_data *st = get_stat(obj);
6370 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
6371 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6391 rb_io_stat_data *st = get_stat(obj);
6394 if (geteuid() == 0)
return Qtrue;
6397 if (rb_stat_owned(obj))
6398 return RBOOL(st->ST_(mode) & S_IWUSR);
6401 if (rb_stat_grpowned(obj))
6402 return RBOOL(st->ST_(mode) & S_IWGRP);
6405 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6424 rb_io_stat_data *st = get_stat(obj);
6427 if (getuid() == 0)
return Qtrue;
6430 if (rb_stat_rowned(obj))
6431 return RBOOL(st->ST_(mode) & S_IWUSR);
6434 if (rb_group_member(get_stat(obj)->ST_(gid)))
6435 return RBOOL(st->ST_(mode) & S_IWGRP);
6438 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6457rb_stat_ww(
VALUE obj)
6460 rb_io_stat_data *st = get_stat(obj);
6461 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
6462 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6484 rb_io_stat_data *st = get_stat(obj);
6487 if (geteuid() == 0) {
6488 return RBOOL(st->ST_(mode) & S_IXUGO);
6492 if (rb_stat_owned(obj))
6493 return RBOOL(st->ST_(mode) & S_IXUSR);
6496 if (rb_stat_grpowned(obj))
6497 return RBOOL(st->ST_(mode) & S_IXGRP);
6500 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6516 rb_io_stat_data *st = get_stat(obj);
6519 if (getuid() == 0) {
6520 return RBOOL(st->ST_(mode) & S_IXUGO);
6524 if (rb_stat_rowned(obj))
6525 return RBOOL(st->ST_(mode) & S_IXUSR);
6528 if (rb_group_member(get_stat(obj)->ST_(gid)))
6529 return RBOOL(st->ST_(mode) & S_IXGRP);
6532 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6551 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
6569 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
6588 rb_off_t size = get_stat(obj)->ST_(size);
6590 if (size == 0)
return Qnil;
6606rb_stat_suid(
VALUE obj)
6609 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
6627rb_stat_sgid(
VALUE obj)
6630 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
6648rb_stat_sticky(
VALUE obj)
6651 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
6656#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
6657#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
6668nogvl_mkfifo(
void *ptr)
6670 struct mkfifo_arg *ma = ptr;
6672 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
6689 struct mkfifo_arg ma;
6698 path = rb_str_encode_ospath(path);
6699 ma.path = RSTRING_PTR(path);
6700 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
6701 rb_sys_fail_path(path);
6706#define rb_file_s_mkfifo rb_f_notimplement
6709static VALUE rb_mFConst;
6712rb_file_const(
const char *name,
VALUE value)
6714 rb_define_const(rb_mFConst, name, value);
6718rb_is_absolute_path(
const char *path)
6720#ifdef DOSISH_DRIVE_LETTER
6721 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
6724 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
6727 if (path[0] ==
'/')
return 1;
6733ruby_is_fd_loadable(
int fd)
6740 if (fstat(fd, &st) < 0)
6743 if (S_ISREG(st.st_mode))
6746 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
6749 if (S_ISDIR(st.st_mode))
6760rb_file_load_ok(
const char *path)
6767 int mode = (O_RDONLY |
6768#if defined O_NONBLOCK
6770#elif defined O_NDELAY
6776 if (!rb_gc_for_fd(
errno))
return 0;
6778 if (fd < 0)
return 0;
6781 ret = ruby_is_fd_loadable(fd);
6788is_explicit_relative(
const char *path)
6790 if (*path++ !=
'.')
return 0;
6791 if (*path ==
'.') path++;
6792 return isdirsep(*path);
6798 int encidx = rb_enc_get_index(orig);
6799 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
6800 encidx = rb_filesystem_encindex();
6801 rb_enc_associate_index(path, encidx);
6809rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
6812 VALUE fname = *filep, load_path, tmp;
6816 if (!ext[0])
return 0;
6819 fname = file_expand_path_1(fname);
6820 f = RSTRING_PTR(fname);
6825 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6826 if (!expanded) fname = file_expand_path_1(fname);
6827 fnlen = RSTRING_LEN(fname);
6828 for (i=0; ext[i]; i++) {
6830 if (rb_file_load_ok(RSTRING_PTR(fname))) {
6831 *filep = copy_path_class(fname, *filep);
6839 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6840 if (!load_path)
return 0;
6843 RBASIC_CLEAR_CLASS(fname);
6844 fnlen = RSTRING_LEN(fname);
6846 rb_enc_associate_index(tmp, rb_usascii_encindex());
6847 for (j=0; ext[j]; j++) {
6849 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6853 if (RSTRING_LEN(str) == 0)
continue;
6854 rb_file_expand_path_internal(fname, str, 0, 0, tmp);
6855 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
6856 *filep = copy_path_class(tmp, *filep);
6862 rb_str_resize(tmp, 0);
6870 VALUE tmp, load_path;
6875 tmp = file_expand_path_1(path);
6876 path = copy_path_class(tmp, path);
6877 f = RSTRING_PTR(path);
6881 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6882 if (!rb_file_load_ok(f))
return 0;
6884 path = copy_path_class(file_expand_path_1(path), path);
6888 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6893 rb_enc_associate_index(tmp, rb_usascii_encindex());
6894 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6897 if (RSTRING_LEN(str) > 0) {
6898 rb_file_expand_path_internal(path, str, 0, 0, tmp);
6899 f = RSTRING_PTR(tmp);
6900 if (rb_file_load_ok(f))
goto found;
6903 rb_str_resize(tmp, 0);
6911 return copy_path_class(tmp, path);
6914#define define_filetest_function(name, func, argc) do { \
6915 rb_define_module_function(rb_mFileTest, name, func, argc); \
6916 rb_define_singleton_method(rb_cFile, name, func, argc); \
6919const char ruby_null_device[] =
6922#elif defined AMIGA || defined __amigaos__
7726#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
7727 rb_CFString_class_initialize_before_fork();
7735 define_filetest_function(
"directory?", rb_file_directory_p, 1);
7736 define_filetest_function(
"exist?", rb_file_exist_p, 1);
7737 define_filetest_function(
"readable?", rb_file_readable_p, 1);
7738 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
7739 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
7740 define_filetest_function(
"writable?", rb_file_writable_p, 1);
7741 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
7742 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
7743 define_filetest_function(
"executable?", rb_file_executable_p, 1);
7744 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
7745 define_filetest_function(
"file?", rb_file_file_p, 1);
7746 define_filetest_function(
"zero?", rb_file_zero_p, 1);
7747 define_filetest_function(
"empty?", rb_file_zero_p, 1);
7748 define_filetest_function(
"size?", rb_file_size_p, 1);
7749 define_filetest_function(
"size", rb_file_s_size, 1);
7750 define_filetest_function(
"owned?", rb_file_owned_p, 1);
7751 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
7753 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
7754 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
7755 define_filetest_function(
"socket?", rb_file_socket_p, 1);
7757 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
7758 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
7760 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
7761 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
7762 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
7764 define_filetest_function(
"identical?", rb_file_identical_p, 2);
7802 separator = rb_fstring_lit(
"/");
7804 rb_define_const(
rb_cFile,
"Separator", separator);
7806 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
8158 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
8160 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
8162 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
8164 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
8166 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
8168 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
8169#if defined(O_NDELAY) || defined(O_NONBLOCK)
8171# define O_NONBLOCK O_NDELAY
8174 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
8177 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
8180 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
8186 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
8187#ifndef O_SHARE_DELETE
8188# define O_SHARE_DELETE 0
8191 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
8194 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
8198 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
8202 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
8206 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
8210 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
8214 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
8218 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
8222 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
8224 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
8226 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
8228 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
8231 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_coderange(VALUE str)
Scans the passed string to collect its code range.
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.