@@ -70,6 +70,29 @@ int flock(int, int);
70
70
#define lstat stat
71
71
#endif
72
72
73
+ /* define system APIs */
74
+ #ifdef _WIN32
75
+ #define STAT (p , s ) rb_w32_wstati64((WCHAR *)(p), s)
76
+ #define LSTAT (p , s ) rb_w32_wstati64((WCHAR *)(p), s)
77
+ #define ACCESS (p , m ) _waccess((WCHAR *)(p), m)
78
+ #define CHMOD (p , m ) _wchmod((WCHAR *)(p), m)
79
+ #define CHOWN (p , o , g ) rb_w32_wchown((WCHAR *)(p), o, g)
80
+ #define UTIME (p , t ) rb_w32_wutime((WCHAR *)(p), t)
81
+ #define LINK (f , t ) rb_w32_wlink((WCHAR *)(f), (WCHAR *)(t))
82
+ #define UNLINK (p ) rb_w32_wunlink((WCHAR *)(p))
83
+ #define RENAME (f , t ) _wrename((WCHAR *)(f), (WCHAR *)(t))
84
+ #else
85
+ #define STAT (p , s ) stat(p, s)
86
+ #define LSTAT (p , s ) lstat(p, s)
87
+ #define ACCESS (p , m ) access(p, m)
88
+ #define CHMOD (p , m ) chmod(p, m)
89
+ #define CHOWN (p , o , g ) chown(p, o, g)
90
+ #define UTIME (p , t ) utime(p, t)
91
+ #define LINK (f , t ) link(f, t)
92
+ #define UNLINK (p ) unlink(p)
93
+ #define RENAME (f , t ) rename(f, t)
94
+ #endif
95
+
73
96
#ifdef __BEOS__ /* should not change ID if -1 */
74
97
static int
75
98
be_chown (const char * path , uid_t owner , gid_t group )
@@ -772,11 +795,7 @@ rb_stat(VALUE file, struct stat *st)
772
795
}
773
796
FilePathValue (file );
774
797
file = rb_str_conv_for_path (file );
775
- #ifdef _WIN32
776
- return rb_w32_wstati64 ((WCHAR * )RSTRING_PTR (file ), st );
777
- #else
778
- return stat (RSTRING_PTR (file ), st );
779
- #endif
798
+ return STAT (RSTRING_PTR (file ), st );
780
799
}
781
800
782
801
#ifdef _WIN32
@@ -925,7 +944,7 @@ rb_file_lstat(VALUE obj)
925
944
GetOpenFile (obj , fptr );
926
945
if (NIL_P (fptr -> pathv )) return Qnil ;
927
946
path = rb_str_conv_for_path (fptr -> pathv );
928
- if (lstat (RSTRING_PTR (path ), & st ) == -1 ) {
947
+ if (LSTAT (RSTRING_PTR (path ), & st ) == -1 ) {
929
948
rb_sys_fail_path (fptr -> pathv );
930
949
}
931
950
return stat_new (& st );
@@ -981,11 +1000,7 @@ eaccess(const char *path, int mode)
981
1000
struct stat st ;
982
1001
rb_uid_t euid ;
983
1002
984
- #ifdef _WIN32
985
- if (_wstat ((WCHAR * )path , & st ) < 0 )
986
- #else
987
- if (stat (path , & st ) < 0 )
988
- #endif
1003
+ if (STAT (path , & st ) < 0 )
989
1004
return -1 ;
990
1005
991
1006
euid = geteuid ();
@@ -1015,19 +1030,15 @@ eaccess(const char *path, int mode)
1015
1030
# if defined(_MSC_VER ) || defined(__MINGW32__ )
1016
1031
mode &= ~1 ;
1017
1032
# endif
1018
- return access (path , mode );
1033
+ return ACCESS (path , mode );
1019
1034
#endif
1020
1035
}
1021
1036
#endif
1022
1037
1023
1038
static inline int
1024
1039
access_internal (const char * path , int mode )
1025
1040
{
1026
- #ifdef _WIN32
1027
- return _waccess ((WCHAR * )path , mode );
1028
- #else
1029
- return access (path , mode );
1030
- #endif
1041
+ return ACCESS (path , mode );
1031
1042
}
1032
1043
1033
1044
@@ -1745,11 +1756,7 @@ rb_file_s_ftype(VALUE klass, VALUE fname)
1745
1756
rb_secure (2 );
1746
1757
FilePathValue (fname );
1747
1758
fname = rb_str_conv_for_path (fname );
1748
- #ifdef _WIN32
1749
- if (rb_w32_wstati64 ((WCHAR * )RSTRING_PTR (fname ), & st ) == -1 )
1750
- #else
1751
- if (lstat (RSTRING_PTR (fname ), & st ) == -1 )
1752
- #endif
1759
+ if (LSTAT (RSTRING_PTR (fname ), & st ) == -1 )
1753
1760
rb_sys_fail (RSTRING_PTR (fname ));
1754
1761
1755
1762
return rb_file_ftype (& st );
@@ -1923,11 +1930,7 @@ rb_file_size(VALUE obj)
1923
1930
static void
1924
1931
chmod_internal (const char * path , void * mode )
1925
1932
{
1926
- #ifdef _WIN32
1927
- if (_wchmod ((WCHAR * )path , * (int * )mode ) < 0 )
1928
- #else
1929
- if (chmod (path , * (int * )mode ) < 0 )
1930
- #endif
1933
+ if (CHMOD (path , * (int * )mode ) < 0 )
1931
1934
rb_sys_fail (path );
1932
1935
}
1933
1936
@@ -1992,11 +1995,7 @@ rb_file_chmod(VALUE obj, VALUE vmode)
1992
1995
#else
1993
1996
if (NIL_P (fptr -> pathv )) return Qnil ;
1994
1997
path = rb_str_conv_for_path (fptr -> pathv );
1995
- # ifdef _WIN32
1996
- if (_wchmod ((WCHAR * )RSTRING_PTR (path ), mode ) == -1 )
1997
- # else
1998
- if (chmod (RSTRING_PTR (path ), mode ) == -1 )
1999
- # endif
1998
+ if (CHMOD (RSTRING_PTR (path ), mode ) == -1 )
2000
1999
rb_sys_fail_path (fptr -> pathv );
2001
2000
#endif
2002
2001
@@ -2048,11 +2047,7 @@ static void
2048
2047
chown_internal (const char * path , void * arg )
2049
2048
{
2050
2049
struct chown_args * args = arg ;
2051
- #ifdef _WIN32
2052
- if (rb_w32_wchown ((WCHAR * )path , args -> owner , args -> group ) < 0 )
2053
- #else
2054
2050
if (chown (path , args -> owner , args -> group ) < 0 )
2055
- #endif
2056
2051
rb_sys_fail (path );
2057
2052
}
2058
2053
@@ -2128,11 +2123,7 @@ rb_file_chown(VALUE obj, VALUE owner, VALUE group)
2128
2123
#ifndef HAVE_FCHOWN
2129
2124
if (NIL_P (fptr -> pathv )) return Qnil ;
2130
2125
path = rb_str_conv_for_path (fptr -> pathv );
2131
- # ifdef _WIN32
2132
- if (rb_w32_wchown ((WCHAR * )RSTRING_PTR (path ), o , g ) == -1 )
2133
- # else
2134
- if (chown (RSTRING_PTR (path ), o , g ) == -1 )
2135
- # endif
2126
+ if (CHOWN (RSTRING_PTR (path ), o , g ) == -1 )
2136
2127
rb_sys_fail_path (fptr -> pathv );
2137
2128
#else
2138
2129
if (fchown (fptr -> fd , o , g ) == -1 )
@@ -2292,11 +2283,7 @@ utime_internal(const char *path, void *arg)
2292
2283
utbuf .modtime = tsp [1 ].tv_sec ;
2293
2284
utp = & utbuf ;
2294
2285
}
2295
- #ifdef _WIN32
2296
- if (rb_w32_wutime ((WCHAR * )path , utp ) < 0 )
2297
- #else
2298
- if (utime (path , utp ) < 0 )
2299
- #endif
2286
+ if (UTIME (path , utp ) < 0 )
2300
2287
utime_failed (path , tsp , v -> atime , v -> mtime );
2301
2288
}
2302
2289
@@ -2388,11 +2375,7 @@ rb_file_s_link(VALUE klass, VALUE from, VALUE to)
2388
2375
from = rb_str_conv_for_path (from );
2389
2376
to = rb_str_conv_for_path (to );
2390
2377
2391
- #ifdef _WIN32
2392
- if (rb_w32_wlink ((WCHAR * )RSTRING_PTR (from ), (WCHAR * )RSTRING_PTR (to )) < 0 )
2393
- #else
2394
2378
if (link (RSTRING_PTR (from ), RSTRING_PTR (to )) < 0 )
2395
- #endif
2396
2379
sys_fail2 (from , to );
2397
2380
return INT2FIX (0 );
2398
2381
}
@@ -2479,11 +2462,7 @@ rb_file_s_readlink(VALUE klass, VALUE path)
2479
2462
static void
2480
2463
unlink_internal (const char * path , void * arg )
2481
2464
{
2482
- #ifdef _WIN32
2483
- if (rb_w32_wunlink ((WCHAR * )path ) < 0 )
2484
- #else
2485
- if (unlink (path ) < 0 )
2486
- #endif
2465
+ if (UNLINK (path ) < 0 )
2487
2466
rb_sys_fail (path );
2488
2467
}
2489
2468
@@ -2532,11 +2511,7 @@ rb_file_s_rename(VALUE klass, VALUE from, VALUE to)
2532
2511
#if defined __CYGWIN__
2533
2512
errno = 0 ;
2534
2513
#endif
2535
- #ifdef _WIN32
2536
- if (_wrename ((WCHAR * )src , (WCHAR * )dst ) < 0 ) {
2537
- #else
2538
- if (rename (src , dst ) < 0 ) {
2539
- #endif
2514
+ if (RENAME (src , dst ) < 0 ) {
2540
2515
#if defined DOSISH && !defined _WIN32
2541
2516
switch (errno ) {
2542
2517
case EEXIST :
@@ -3549,19 +3524,20 @@ rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
3549
3524
rb_secure (2 );
3550
3525
pos = NUM2OFFT (len );
3551
3526
FilePathValue (path );
3527
+ path = rb_str_conv_for_path (path );
3552
3528
#ifdef HAVE_TRUNCATE
3553
- if (truncate (StringValueCStr (path ), pos ) < 0 )
3529
+ if (truncate (RSTRING_PTR (path ), pos ) < 0 )
3554
3530
rb_sys_fail (RSTRING_PTR (path ));
3555
3531
#else /* defined(HAVE_CHSIZE) */
3556
3532
{
3557
3533
int tmpfd ;
3558
3534
3559
3535
# ifdef _WIN32
3560
- if ((tmpfd = open ( StringValueCStr (path ), O_RDWR )) < 0 ) {
3536
+ if ((tmpfd = rb_w32_wopen (( WCHAR * ) RSTRING_PTR (path ), O_RDWR )) < 0 ) {
3561
3537
rb_sys_fail (RSTRING_PTR (path ));
3562
3538
}
3563
3539
# else
3564
- if ((tmpfd = open (StringValueCStr (path ), 0 )) < 0 ) {
3540
+ if ((tmpfd = open (RSTRING_PTR (path ), 0 )) < 0 ) {
3565
3541
rb_sys_fail (RSTRING_PTR (path ));
3566
3542
}
3567
3543
# endif
@@ -3993,7 +3969,8 @@ rb_stat_init(VALUE obj, VALUE fname)
3993
3969
3994
3970
rb_secure (2 );
3995
3971
FilePathValue (fname );
3996
- if (stat (StringValueCStr (fname ), & st ) == -1 ) {
3972
+ fname = rb_str_conv_for_path (fname );
3973
+ if (STAT (RSTRING_PTR (fname ), & st ) == -1 ) {
3997
3974
rb_sys_fail (RSTRING_PTR (fname ));
3998
3975
}
3999
3976
if (DATA_PTR (obj )) {
0 commit comments