1
/**********************************************************************
6
$Date: 2007-03-04 23:51:31 +0900 (Sun, 04 Mar 2007) $
7
created at: Mon Nov 15 12:24:34 JST 1993
9
Copyright (C) 1993-2003 Yukihiro Matsumoto
10
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
11
Copyright (C) 2000 Information-technology Promotion Agency, Japan
13
**********************************************************************/
16
#include "missing/file.h"
29
#ifdef HAVE_SYS_FILE_H
30
# include <sys/file.h>
32
int flock _((int, int));
35
#ifdef HAVE_SYS_PARAM_H
36
# include <sys/param.h>
39
# define MAXPATHLEN 1024
44
VALUE rb_time_new _((time_t, time_t));
48
#elif defined HAVE_SYS_UTIME_H
49
#include <sys/utime.h>
57
char *strrchr _((const char*,const char));
60
#include <sys/types.h>
63
#ifdef HAVE_SYS_MKDEV_H
64
#include <sys/mkdev.h>
67
#if !defined HAVE_LSTAT && !defined lstat
70
#if !HAVE_FSEEKO && !defined(fseeko)
74
#ifdef __BEOS__ /* should not change ID if -1 */
76
be_chown(const char *path, uid_t owner, gid_t group)
78
if (owner == -1 || group == -1) {
80
if (stat(path, &st) < 0) return -1;
81
if (owner == -1) owner = st.st_uid;
82
if (group == -1) group = st.st_gid;
84
return chown(path, owner, group);
86
#define chown be_chown
88
be_fchown(int fd, uid_t owner, gid_t group)
90
if (owner == -1 || group == -1) {
92
if (fstat(fd, &st) < 0) return -1;
93
if (owner == -1) owner = st.st_uid;
94
if (group == -1) group = st.st_gid;
96
return fchown(fd, owner, group);
98
#define fchown be_fchown
105
static long apply2files _((void (*)(const char *, void *), VALUE, void *));
107
apply2files(func, vargs, arg)
108
void (*func)_((const char *, void *));
114
struct RArray *args = RARRAY(vargs);
116
for (i=0; i<args->len; i++) {
118
SafeStringValue(path);
119
(*func)(StringValueCStr(path), arg);
127
* file.path -> filename
129
* Returns the pathname used to create <i>file</i> as a string. Does
130
* not normalize the name.
132
* File.new("testfile").path #=> "testfile"
133
* File.new("/tmp/../tmp/xxx", "w").path #=> "/tmp/../tmp/xxx"
143
fptr = RFILE(rb_io_taint_check(obj))->fptr;
144
rb_io_check_initialized(fptr);
145
if (!fptr->path) return Qnil;
146
return rb_tainted_str_new2(fptr->path);
150
stat_new_0(klass, st)
154
struct stat *nst = 0;
157
nst = ALLOC(struct stat);
160
return Data_Wrap_Struct(klass, NULL, free, nst);
167
return stat_new_0(rb_cStat, st);
175
Data_Get_Struct(self, struct stat, st);
176
if (!st) rb_raise(rb_eTypeError, "uninitialized File::Stat");
182
* stat <=> other_stat => -1, 0, 1
184
* Compares <code>File::Stat</code> objects by comparing their
185
* respective modification times.
187
* f1 = File.new("f1", "w")
189
* f2 = File.new("f2", "w")
190
* f1.stat <=> f2.stat #=> -1
194
rb_stat_cmp(self, other)
197
if (rb_obj_is_kind_of(other, rb_obj_class(self))) {
198
time_t t1 = get_stat(self)->st_mtime;
199
time_t t2 = get_stat(other)->st_mtime;
210
static VALUE rb_stat_dev _((VALUE));
211
static VALUE rb_stat_ino _((VALUE));
212
static VALUE rb_stat_mode _((VALUE));
213
static VALUE rb_stat_nlink _((VALUE));
214
static VALUE rb_stat_uid _((VALUE));
215
static VALUE rb_stat_gid _((VALUE));
216
static VALUE rb_stat_rdev _((VALUE));
217
static VALUE rb_stat_size _((VALUE));
218
static VALUE rb_stat_blksize _((VALUE));
219
static VALUE rb_stat_blocks _((VALUE));
220
static VALUE rb_stat_atime _((VALUE));
221
static VALUE rb_stat_mtime _((VALUE));
222
static VALUE rb_stat_ctime _((VALUE));
228
* Returns an integer representing the device on which <i>stat</i>
231
* File.stat("testfile").dev #=> 774
238
return INT2NUM(get_stat(self)->st_dev);
243
* stat.dev_major => fixnum
245
* Returns the major part of <code>File_Stat#dev</code> or
248
* File.stat("/dev/fd1").dev_major #=> 2
249
* File.stat("/dev/tty").dev_major #=> 5
253
rb_stat_dev_major(self)
257
long dev = get_stat(self)->st_dev;
258
return ULONG2NUM(major(dev));
266
* stat.dev_minor => fixnum
268
* Returns the minor part of <code>File_Stat#dev</code> or
271
* File.stat("/dev/fd1").dev_minor #=> 1
272
* File.stat("/dev/tty").dev_minor #=> 0
276
rb_stat_dev_minor(self)
280
long dev = get_stat(self)->st_dev;
281
return ULONG2NUM(minor(dev));
292
* Returns the inode number for <i>stat</i>.
294
* File.stat("testfile").ino #=> 1083669
303
return ULL2NUM(get_stat(self)->st_ino);
305
return ULONG2NUM(get_stat(self)->st_ino);
311
* stat.mode => fixnum
313
* Returns an integer representing the permission bits of
314
* <i>stat</i>. The meaning of the bits is platform dependent; on
315
* Unix systems, see <code>stat(2)</code>.
317
* File.chmod(0644, "testfile") #=> 1
318
* s = File.stat("testfile")
319
* sprintf("%o", s.mode) #=> "100644"
327
return UINT2NUM((unsigned short)(get_stat(self)->st_mode));
329
return UINT2NUM(get_stat(self)->st_mode);
335
* stat.nlink => fixnum
337
* Returns the number of hard links to <i>stat</i>.
339
* File.stat("testfile").nlink #=> 1
340
* File.link("testfile", "testfile.bak") #=> 0
341
* File.stat("testfile").nlink #=> 2
349
return UINT2NUM(get_stat(self)->st_nlink);
357
* Returns the numeric user id of the owner of <i>stat</i>.
359
* File.stat("testfile").uid #=> 501
367
return UINT2NUM(get_stat(self)->st_uid);
374
* Returns the numeric group id of the owner of <i>stat</i>.
376
* File.stat("testfile").gid #=> 500
384
return UINT2NUM(get_stat(self)->st_gid);
390
* stat.rdev => fixnum or nil
392
* Returns an integer representing the device type on which
393
* <i>stat</i> resides. Returns <code>nil</code> if the operating
394
* system doesn't support this feature.
396
* File.stat("/dev/fd1").rdev #=> 513
397
* File.stat("/dev/tty").rdev #=> 1280
405
return ULONG2NUM(get_stat(self)->st_rdev);
413
* stat.rdev_major => fixnum
415
* Returns the major part of <code>File_Stat#rdev</code> or
418
* File.stat("/dev/fd1").rdev_major #=> 2
419
* File.stat("/dev/tty").rdev_major #=> 5
423
rb_stat_rdev_major(self)
426
#if defined(HAVE_ST_RDEV) && defined(major)
427
long rdev = get_stat(self)->st_rdev;
428
return ULONG2NUM(major(rdev));
436
* stat.rdev_minor => fixnum
438
* Returns the minor part of <code>File_Stat#rdev</code> or
441
* File.stat("/dev/fd1").rdev_minor #=> 1
442
* File.stat("/dev/tty").rdev_minor #=> 0
446
rb_stat_rdev_minor(self)
449
#if defined(HAVE_ST_RDEV) && defined(minor)
450
long rdev = get_stat(self)->st_rdev;
451
return ULONG2NUM(minor(rdev));
459
* stat.size => fixnum
461
* Returns the size of <i>stat</i> in bytes.
463
* File.stat("testfile").size #=> 66
470
return OFFT2NUM(get_stat(self)->st_size);
475
* stat.blksize => integer or nil
477
* Returns the native file system's block size. Will return <code>nil</code>
478
* on platforms that don't support this information.
480
* File.stat("testfile").blksize #=> 4096
485
rb_stat_blksize(self)
488
#ifdef HAVE_ST_BLKSIZE
489
return ULONG2NUM(get_stat(self)->st_blksize);
497
* stat.blocks => integer or nil
499
* Returns the number of native file system blocks allocated for this
500
* file, or <code>nil</code> if the operating system doesn't
501
* support this feature.
503
* File.stat("testfile").blocks #=> 2
510
#ifdef HAVE_ST_BLOCKS
511
return ULONG2NUM(get_stat(self)->st_blocks);
522
* Returns the last access time for this file as an object of class
525
* File.stat("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
533
return rb_time_new(get_stat(self)->st_atime, 0);
538
* stat.mtime -> aTime
540
* Returns the modification time of <i>stat</i>.
542
* File.stat("testfile").mtime #=> Wed Apr 09 08:53:14 CDT 2003
550
return rb_time_new(get_stat(self)->st_mtime, 0);
555
* stat.ctime -> aTime
557
* Returns the change time for <i>stat</i> (that is, the time
558
* directory information about the file was changed, not the file
561
* File.stat("testfile").ctime #=> Wed Apr 09 08:53:14 CDT 2003
569
return rb_time_new(get_stat(self)->st_ctime, 0);
574
* stat.inspect => string
576
* Produce a nicely formatted description of <i>stat</i>.
578
* File.stat("/etc/passwd").inspect
579
* #=> "#<File::Stat dev=0xe000005, ino=1078078, mode=0100644,
580
* nlink=1, uid=0, gid=0, rdev=0x0, size=1374, blksize=4096,
581
* blocks=8, atime=Wed Dec 10 10:16:12 CST 2003,
582
* mtime=Fri Sep 12 15:41:41 CDT 2003,
583
* ctime=Mon Oct 27 11:20:27 CST 2003>"
587
rb_stat_inspect(self)
592
static const struct {
594
VALUE (*func)_((VALUE));
596
{"dev", rb_stat_dev},
597
{"ino", rb_stat_ino},
598
{"mode", rb_stat_mode},
599
{"nlink", rb_stat_nlink},
600
{"uid", rb_stat_uid},
601
{"gid", rb_stat_gid},
602
{"rdev", rb_stat_rdev},
603
{"size", rb_stat_size},
604
{"blksize", rb_stat_blksize},
605
{"blocks", rb_stat_blocks},
606
{"atime", rb_stat_atime},
607
{"mtime", rb_stat_mtime},
608
{"ctime", rb_stat_ctime},
611
str = rb_str_buf_new2("#<");
612
rb_str_buf_cat2(str, rb_obj_classname(self));
613
rb_str_buf_cat2(str, " ");
615
for (i = 0; i < sizeof(member)/sizeof(member[0]); i++) {
619
rb_str_buf_cat2(str, ", ");
621
rb_str_buf_cat2(str, member[i].name);
622
rb_str_buf_cat2(str, "=");
623
v = (*member[i].func)(self);
624
if (i == 2) { /* mode */
627
sprintf(buf, "0%lo", NUM2ULONG(v));
628
rb_str_buf_cat2(str, buf);
630
else if (i == 0 || i == 6) { /* dev/rdev */
633
sprintf(buf, "0x%lx", NUM2ULONG(v));
634
rb_str_buf_cat2(str, buf);
637
rb_str_append(str, rb_inspect(v));
640
rb_str_buf_cat2(str, ">");
641
OBJ_INFECT(str, self);
653
tmp = rb_check_convert_type(file, T_FILE, "IO", "to_io");
658
GetOpenFile(tmp, fptr);
659
return fstat(fileno(fptr->f), st);
661
SafeStringValue(file);
662
return stat(StringValueCStr(file), st);
667
w32_io_info(file, st)
669
BY_HANDLE_FILE_INFORMATION *st;
674
tmp = rb_check_convert_type(*file, T_FILE, "IO", "to_io");
678
GetOpenFile(tmp, fptr);
679
f = (HANDLE)rb_w32_get_osfhandle(fileno(fptr->f));
680
if (f == (HANDLE)-1) return INVALID_HANDLE_VALUE;
683
SafeStringValue(*file);
684
f = CreateFile(StringValueCStr(*file), 0,
685
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
686
rb_w32_iswin95() ? 0 : FILE_FLAG_BACKUP_SEMANTICS, NULL);
687
if (f == INVALID_HANDLE_VALUE) return f;
690
if (GetFileType(f) == FILE_TYPE_DISK) {
691
ZeroMemory(st, sizeof(*st));
692
if (GetFileInformationByHandle(f, st)) return ret;
694
if (ret) CloseHandle(ret);
695
return INVALID_HANDLE_VALUE;
701
* File.stat(file_name) => stat
703
* Returns a <code>File::Stat</code> object for the named file (see
704
* <code>File::Stat</code>).
706
* File.stat("testfile").mtime #=> Tue Apr 08 12:58:04 CDT 2003
711
rb_file_s_stat(klass, fname)
716
SafeStringValue(fname);
717
if (rb_stat(fname, &st) < 0) {
718
rb_sys_fail(StringValueCStr(fname));
720
return stat_new(&st);
727
* Returns status information for <em>ios</em> as an object of type
728
* <code>File::Stat</code>.
730
* f = File.new("testfile")
732
* "%o" % s.mode #=> "100644"
734
* s.atime #=> Wed Apr 09 08:53:54 CDT 2003
745
GetOpenFile(obj, fptr);
746
if (fstat(fileno(fptr->f), &st) == -1) {
747
rb_sys_fail(fptr->path);
749
return stat_new(&st);
754
* File.lstat(file_name) => stat
756
* Same as <code>File::stat</code>, but does not follow the last symbolic
757
* link. Instead, reports on the link itself.
759
* File.symlink("testfile", "link2test") #=> 0
760
* File.stat("testfile").size #=> 66
761
* File.lstat("link2test").size #=> 8
762
* File.stat("link2test").size #=> 66
767
rb_file_s_lstat(klass, fname)
773
SafeStringValue(fname);
774
if (lstat(StringValueCStr(fname), &st) == -1) {
775
rb_sys_fail(RSTRING(fname)->ptr);
777
return stat_new(&st);
779
return rb_file_s_stat(klass, fname);
788
* Same as <code>IO#stat</code>, but does not follow the last symbolic
789
* link. Instead, reports on the link itself.
791
* File.symlink("testfile", "link2test") #=> 0
792
* File.stat("testfile").size #=> 66
793
* f = File.new("link2test")
807
GetOpenFile(obj, fptr);
808
if (!fptr->path) return Qnil;
809
if (lstat(fptr->path, &st) == -1) {
810
rb_sys_fail(fptr->path);
812
return stat_new(&st);
814
return rb_io_stat(obj);
823
if (getgid() == gid || getegid() == gid)
826
# ifdef HAVE_GETGROUPS
829
# define NGROUPS NGROUPS_MAX
835
GETGROUPS_T gary[NGROUPS];
838
anum = getgroups(NGROUPS, gary);
840
if (gary[anum] == gid)
849
# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
852
#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
853
#define USE_GETEUID 1
866
if (stat(path, &st) < 0) return -1;
871
/* Root can read or write any file. */
875
/* Root can execute any file that has any one of the execute
877
if (st.st_mode & S_IXUGO)
883
if (st.st_uid == euid) /* owner */
885
else if (group_member(st.st_gid))
888
if ((st.st_mode & mode) == mode) return 0;
892
# if _MSC_VER >= 1400
895
return access(path, mode);
902
* Document-class: FileTest
904
* <code>FileTest</code> implements file test operations similar to
905
* those used in <code>File::Stat</code>. It exists as a standalone
906
* module, and its methods are also insinuated into the <code>File</code>
907
* class. (Note that this is not done by inclusion: the interpreter cheats).
914
* File.directory?(file_name) => true or false
916
* Returns <code>true</code> if the named file is a directory,
917
* <code>false</code> otherwise.
919
* File.directory?(".")
927
# define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
932
if (rb_stat(fname, &st) < 0) return Qfalse;
933
if (S_ISDIR(st.st_mode)) return Qtrue;
939
* File.pipe?(file_name) => true or false
941
* Returns <code>true</code> if the named file is a pipe.
950
# define S_ISFIFO(m) ((m & S_IFMT) == S_IFIFO)
955
if (rb_stat(fname, &st) < 0) return Qfalse;
956
if (S_ISFIFO(st.st_mode)) return Qtrue;
964
* File.symlink?(file_name) => true or false
966
* Returns <code>true</code> if the named file is a symbolic link.
975
# define S_ISLNK(m) _S_ISLNK(m)
976
# elif defined __BORLANDC__
978
# define S_ISLNK(m) (((unsigned short)(m) & S_IFMT) == _S_IFLNK)
981
# define S_ISLNK(m) (((unsigned short)(m) & S_IFMT) == S_IFLNK)
986
# define S_ISLNK(m) ((m & S_IFMT) == _S_IFLNK)
989
# define S_ISLNK(m) ((m & S_IFMT) == S_IFLNK)
998
SafeStringValue(fname);
999
if (lstat(StringValueCStr(fname), &st) < 0) return Qfalse;
1000
if (S_ISLNK(st.st_mode)) return Qtrue;
1008
* File.socket?(file_name) => true or false
1010
* Returns <code>true</code> if the named file is a socket.
1019
# define S_ISSOCK(m) _S_ISSOCK(m)
1020
# elif defined __BORLANDC__
1022
# define S_ISSOCK(m) (((unsigned short)(m) & S_IFMT) == _S_IFSOCK)
1025
# define S_ISSOCK(m) (((unsigned short)(m) & S_IFMT) == S_IFSOCK)
1030
# define S_ISSOCK(m) ((m & S_IFMT) == _S_IFSOCK)
1033
# define S_ISSOCK(m) ((m & S_IFMT) == S_IFSOCK)
1042
if (rb_stat(fname, &st) < 0) return Qfalse;
1043
if (S_ISSOCK(st.st_mode)) return Qtrue;
1051
* File.blockdev?(file_name) => true or false
1053
* Returns <code>true</code> if the named file is a block device.
1062
# define S_ISBLK(m) ((m & S_IFMT) == S_IFBLK)
1064
# define S_ISBLK(m) (0) /* anytime false */
1071
if (rb_stat(fname, &st) < 0) return Qfalse;
1072
if (S_ISBLK(st.st_mode)) return Qtrue;
1080
* File.chardev?(file_name) => true or false
1082
* Returns <code>true</code> if the named file is a character device.
1089
# define S_ISCHR(m) ((m & S_IFMT) == S_IFCHR)
1094
if (rb_stat(fname, &st) < 0) return Qfalse;
1095
if (S_ISCHR(st.st_mode)) return Qtrue;
1103
* File.exist?(file_name) => true or false
1104
* File.exists?(file_name) => true or false (obsolete)
1106
* Return <code>true</code> if the named file exists.
1115
if (rb_stat(fname, &st) < 0) return Qfalse;
1121
* File.readable?(file_name) => true or false
1123
* Returns <code>true</code> if the named file is readable by the effective
1124
* user id of this process.
1131
SafeStringValue(fname);
1132
if (eaccess(StringValueCStr(fname), R_OK) < 0) return Qfalse;
1138
* File.readable_real?(file_name) => true or false
1140
* Returns <code>true</code> if the named file is readable by the real
1141
* user id of this process.
1148
SafeStringValue(fname);
1149
if (access(StringValueCStr(fname), R_OK) < 0) return Qfalse;
1156
* File.writable?(file_name) => true or false
1158
* Returns <code>true</code> if the named file is writable by the effective
1159
* user id of this process.
1166
SafeStringValue(fname);
1167
if (eaccess(StringValueCStr(fname), W_OK) < 0) return Qfalse;
1173
* File.writable_real?(file_name) => true or false
1175
* Returns <code>true</code> if the named file is writable by the real
1176
* user id of this process.
1183
SafeStringValue(fname);
1184
if (access(StringValueCStr(fname), W_OK) < 0) return Qfalse;
1190
* File.executable?(file_name) => true or false
1192
* Returns <code>true</code> if the named file is executable by the effective
1193
* user id of this process.
1200
SafeStringValue(fname);
1201
if (eaccess(StringValueCStr(fname), X_OK) < 0) return Qfalse;
1207
* File.executable_real?(file_name) => true or false
1209
* Returns <code>true</code> if the named file is executable by the real
1210
* user id of this process.
1217
SafeStringValue(fname);
1218
if (access(StringValueCStr(fname), X_OK) < 0) return Qfalse;
1223
# define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
1228
* File.file?(file_name) => true or false
1230
* Returns <code>true</code> if the named file exists and is a
1240
if (rb_stat(fname, &st) < 0) return Qfalse;
1241
if (S_ISREG(st.st_mode)) return Qtrue;
1247
* File.zero?(file_name) => true or false
1249
* Returns <code>true</code> if the named file exists and has
1259
if (rb_stat(fname, &st) < 0) return Qfalse;
1260
if (st.st_size == 0) return Qtrue;
1266
* File.size?(file_name) => Integer or nil
1268
* Returns +nil+ if +file_name+ doesn't exist or has zero size, the size of the
1278
if (rb_stat(fname, &st) < 0) return Qnil;
1279
if (st.st_size == 0) return Qnil;
1280
return OFFT2NUM(st.st_size);
1285
* File.owned?(file_name) => true or false
1287
* Returns <code>true</code> if the named file exists and the
1288
* effective used id of the calling process is the owner of
1293
test_owned(obj, fname)
1298
if (rb_stat(fname, &st) < 0) return Qfalse;
1299
if (st.st_uid == geteuid()) return Qtrue;
1304
test_rowned(obj, fname)
1309
if (rb_stat(fname, &st) < 0) return Qfalse;
1310
if (st.st_uid == getuid()) return Qtrue;
1316
* File.grpowned?(file_name) => true or false
1318
* Returns <code>true</code> if the named file exists and the
1319
* effective group id of the calling process is the owner of
1320
* the file. Returns <code>false</code> on Windows.
1324
test_grpowned(obj, fname)
1330
if (rb_stat(fname, &st) < 0) return Qfalse;
1331
if (group_member(st.st_gid)) return Qtrue;
1336
#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
1338
check3rdbyte(fname, mode)
1344
SafeStringValue(fname);
1345
if (stat(StringValueCStr(fname), &st) < 0) return Qfalse;
1346
if (st.st_mode & mode) return Qtrue;
1353
* File.setuid?(file_name) => true or false
1355
* Returns <code>true</code> if the named file has the setuid bit set.
1359
test_suid(obj, fname)
1363
return check3rdbyte(fname, S_ISUID);
1371
* File.setgid?(file_name) => true or false
1373
* Returns <code>true</code> if the named file has the setgid bit set.
1377
test_sgid(obj, fname)
1381
return check3rdbyte(fname, S_ISGID);
1389
* File.sticky?(file_name) => true or false
1391
* Returns <code>true</code> if the named file has the sticky bit set.
1395
test_sticky(obj, fname)
1399
return check3rdbyte(fname, S_ISVTX);
1407
* File.identical?(file_1, file_2) => true or false
1409
* Returns <code>true</code> if the named files are identical.
1412
* p File.identical?("a", "a") #=> true
1413
* p File.identical?("a", "./a") #=> true
1414
* File.link("a", "b")
1415
* p File.identical?("a", "b") #=> true
1416
* File.symlink("a", "c")
1417
* p File.identical?("a", "c") #=> true
1419
* p File.identical?("a", "d") #=> false
1423
test_identical(obj, fname1, fname2)
1424
VALUE obj, fname1, fname2;
1427
struct stat st1, st2;
1429
if (rb_stat(fname1, &st1) < 0) return Qfalse;
1430
if (rb_stat(fname2, &st2) < 0) return Qfalse;
1431
if (st1.st_dev != st2.st_dev) return Qfalse;
1432
if (st1.st_ino != st2.st_ino) return Qfalse;
1435
BY_HANDLE_FILE_INFORMATION st1, st2;
1436
HANDLE f1 = 0, f2 = 0;
1441
f1 = w32_io_info(&fname1, &st1);
1442
if (f1 == INVALID_HANDLE_VALUE) return Qfalse;
1443
f2 = w32_io_info(&fname2, &st2);
1444
if (f1) CloseHandle(f1);
1445
if (f2 == INVALID_HANDLE_VALUE) return Qfalse;
1446
if (f2) CloseHandle(f2);
1448
if (st1.dwVolumeSerialNumber == st2.dwVolumeSerialNumber &&
1449
st1.nFileIndexHigh == st2.nFileIndexHigh &&
1450
st1.nFileIndexLow == st2.nFileIndexLow)
1452
if (!f1 || !f2) return Qfalse;
1453
if (rb_w32_iswin95()) return Qfalse;
1455
SafeStringValue(fname1);
1456
fname1 = rb_str_new4(fname1);
1457
SafeStringValue(fname2);
1458
if (access(RSTRING(fname1)->ptr, 0)) return Qfalse;
1459
if (access(RSTRING(fname2)->ptr, 0)) return Qfalse;
1461
fname1 = rb_file_expand_path(fname1, Qnil);
1462
fname2 = rb_file_expand_path(fname2, Qnil);
1463
if (RSTRING(fname1)->len != RSTRING(fname2)->len) return Qfalse;
1464
if (rb_memcicmp(RSTRING(fname1)->ptr, RSTRING(fname2)->ptr, RSTRING(fname1)->len))
1472
* File.size(file_name) => integer
1474
* Returns the size of <code>file_name</code>.
1478
rb_file_s_size(klass, fname)
1483
if (rb_stat(fname, &st) < 0)
1484
rb_sys_fail(StringValueCStr(fname));
1485
return OFFT2NUM(st.st_size);
1494
if (S_ISREG(st->st_mode)) {
1497
else if (S_ISDIR(st->st_mode)) {
1500
else if (S_ISCHR(st->st_mode)) {
1501
t = "characterSpecial";
1504
else if (S_ISBLK(st->st_mode)) {
1509
else if (S_ISFIFO(st->st_mode)) {
1514
else if (S_ISLNK(st->st_mode)) {
1519
else if (S_ISSOCK(st->st_mode)) {
1527
return rb_str_new2(t);
1532
* File.ftype(file_name) => string
1534
* Identifies the type of the named file; the return string is one of
1535
* ``<code>file</code>'', ``<code>directory</code>'',
1536
* ``<code>characterSpecial</code>'', ``<code>blockSpecial</code>'',
1537
* ``<code>fifo</code>'', ``<code>link</code>'',
1538
* ``<code>socket</code>'', or ``<code>unknown</code>''.
1540
* File.ftype("testfile") #=> "file"
1541
* File.ftype("/dev/tty") #=> "characterSpecial"
1542
* File.ftype("/tmp/.X11-unix/X0") #=> "socket"
1546
rb_file_s_ftype(klass, fname)
1551
SafeStringValue(fname);
1552
if (lstat(StringValueCStr(fname), &st) == -1) {
1553
rb_sys_fail(RSTRING(fname)->ptr);
1556
return rb_file_ftype(&st);
1561
* File.atime(file_name) => time
1563
* Returns the last access time for the named file as a Time object).
1565
* File.atime("testfile") #=> Wed Apr 09 08:51:48 CDT 2003
1570
rb_file_s_atime(klass, fname)
1575
if (rb_stat(fname, &st) < 0)
1576
rb_sys_fail(StringValueCStr(fname));
1577
return rb_time_new(st.st_atime, 0);
1582
* file.atime => time
1584
* Returns the last access time (a <code>Time</code> object)
1585
* for <i>file</i>, or epoch if <i>file</i> has not been accessed.
1587
* File.new("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
1598
GetOpenFile(obj, fptr);
1599
if (fstat(fileno(fptr->f), &st) == -1) {
1600
rb_sys_fail(fptr->path);
1602
return rb_time_new(st.st_atime, 0);
1607
* File.mtime(file_name) => time
1609
* Returns the modification time for the named file as a Time object.
1611
* File.mtime("testfile") #=> Tue Apr 08 12:58:04 CDT 2003
1616
rb_file_s_mtime(klass, fname)
1621
if (rb_stat(fname, &st) < 0)
1622
rb_sys_fail(RSTRING(fname)->ptr);
1623
return rb_time_new(st.st_mtime, 0);
1628
* file.mtime -> time
1630
* Returns the modification time for <i>file</i>.
1632
* File.new("testfile").mtime #=> Wed Apr 09 08:53:14 CDT 2003
1643
GetOpenFile(obj, fptr);
1644
if (fstat(fileno(fptr->f), &st) == -1) {
1645
rb_sys_fail(fptr->path);
1647
return rb_time_new(st.st_mtime, 0);
1652
* File.ctime(file_name) => time
1654
* Returns the change time for the named file (the time at which
1655
* directory information about the file was changed, not the file
1658
* File.ctime("testfile") #=> Wed Apr 09 08:53:13 CDT 2003
1663
rb_file_s_ctime(klass, fname)
1668
if (rb_stat(fname, &st) < 0)
1669
rb_sys_fail(RSTRING(fname)->ptr);
1670
return rb_time_new(st.st_ctime, 0);
1675
* file.ctime -> time
1677
* Returns the change time for <i>file</i> (that is, the time directory
1678
* information about the file was changed, not the file itself).
1680
* File.new("testfile").ctime #=> Wed Apr 09 08:53:14 CDT 2003
1691
GetOpenFile(obj, fptr);
1692
if (fstat(fileno(fptr->f), &st) == -1) {
1693
rb_sys_fail(fptr->path);
1695
return rb_time_new(st.st_ctime, 0);
1698
static void chmod_internal _((const char *, void *));
1700
chmod_internal(path, mode)
1704
if (chmod(path, *(int *)mode) < 0)
1710
* File.chmod(mode_int, file_name, ... ) -> integer
1712
* Changes permission bits on the named file(s) to the bit pattern
1713
* represented by <i>mode_int</i>. Actual effects are operating system
1714
* dependent (see the beginning of this section). On Unix systems, see
1715
* <code>chmod(2)</code> for details. Returns the number of files
1718
* File.chmod(0644, "testfile", "out") #=> 2
1722
rb_file_s_chmod(argc, argv)
1732
rb_scan_args(argc, argv, "1*", &vmode, &rest);
1733
mode = NUM2INT(vmode);
1735
n = apply2files(chmod_internal, rest, &mode);
1741
* file.chmod(mode_int) => 0
1743
* Changes permission bits on <i>file</i> to the bit pattern
1744
* represented by <i>mode_int</i>. Actual effects are platform
1745
* dependent; on Unix systems, see <code>chmod(2)</code> for details.
1746
* Follows symbolic links. Also see <code>File#lchmod</code>.
1748
* f = File.new("out", "w");
1749
* f.chmod(0644) #=> 0
1753
rb_file_chmod(obj, vmode)
1760
mode = NUM2INT(vmode);
1762
GetOpenFile(obj, fptr);
1764
if (fchmod(fileno(fptr->f), mode) == -1)
1765
rb_sys_fail(fptr->path);
1767
if (!fptr->path) return Qnil;
1768
if (chmod(fptr->path, mode) == -1)
1769
rb_sys_fail(fptr->path);
1775
#if defined(HAVE_LCHMOD)
1776
static void lchmod_internal _((const char *, void *));
1778
lchmod_internal(path, mode)
1782
if (lchmod(path, (int)mode) < 0)
1788
* File.lchmod(mode_int, file_name, ...) => integer
1790
* Equivalent to <code>File::chmod</code>, but does not follow symbolic
1791
* links (so it will change the permissions associated with the link,
1792
* not the file referenced by the link). Often not available.
1797
rb_file_s_lchmod(argc, argv)
1806
rb_scan_args(argc, argv, "1*", &vmode, &rest);
1807
mode = NUM2INT(vmode);
1809
n = apply2files(lchmod_internal, rest, (void *)(long)mode);
1814
rb_file_s_lchmod(argc, argv)
1819
return Qnil; /* not reached */
1827
static void chown_internal _((const char *, void *));
1829
chown_internal(path, argp)
1833
struct chown_args *args = (struct chown_args *)argp;
1834
if (chown(path, args->owner, args->group) < 0)
1840
* File.chown(owner_int, group_int, file_name,... ) -> integer
1842
* Changes the owner and group of the named file(s) to the given
1843
* numeric owner and group id's. Only a process with superuser
1844
* privileges may change the owner of a file. The current owner of a
1845
* file may change the file's group to any group to which the owner
1846
* belongs. A <code>nil</code> or -1 owner or group id is ignored.
1847
* Returns the number of files processed.
1849
* File.chown(nil, 100, "testfile")
1854
rb_file_s_chown(argc, argv)
1859
struct chown_args arg;
1863
rb_scan_args(argc, argv, "2*", &o, &g, &rest);
1868
arg.owner = NUM2INT(o);
1874
arg.group = NUM2INT(g);
1877
n = apply2files(chown_internal, rest, &arg);
1883
* file.chown(owner_int, group_int ) => 0
1885
* Changes the owner and group of <i>file</i> to the given numeric
1886
* owner and group id's. Only a process with superuser privileges may
1887
* change the owner of a file. The current owner of a file may change
1888
* the file's group to any group to which the owner belongs. A
1889
* <code>nil</code> or -1 owner or group id is ignored. Follows
1890
* symbolic links. See also <code>File#lchown</code>.
1892
* File.new("testfile").chown(502, 1000)
1897
rb_file_chown(obj, owner, group)
1898
VALUE obj, owner, group;
1904
o = NIL_P(owner) ? -1 : NUM2INT(owner);
1905
g = NIL_P(group) ? -1 : NUM2INT(group);
1906
GetOpenFile(obj, fptr);
1907
#if defined(DJGPP) || defined(__CYGWIN32__) || defined(_WIN32) || defined(__EMX__)
1908
if (!fptr->path) return Qnil;
1909
if (chown(fptr->path, o, g) == -1)
1910
rb_sys_fail(fptr->path);
1912
if (fchown(fileno(fptr->f), o, g) == -1)
1913
rb_sys_fail(fptr->path);
1919
#if defined(HAVE_LCHOWN) && !defined(__CHECKER__)
1920
static void lchown_internal _((const char *, void *));
1922
lchown_internal(path, argp)
1926
struct chown_args *args = (struct chown_args *)argp;
1927
if (lchown(path, args->owner, args->group) < 0)
1934
* file.lchown(owner_int, group_int, file_name,..) => integer
1936
* Equivalent to <code>File::chown</code>, but does not follow symbolic
1937
* links (so it will change the owner associated with the link, not the
1938
* file referenced by the link). Often not available. Returns number
1939
* of files in the argument list.
1944
rb_file_s_lchown(argc, argv)
1949
struct chown_args arg;
1953
rb_scan_args(argc, argv, "2*", &o, &g, &rest);
1958
arg.owner = NUM2INT(o);
1964
arg.group = NUM2INT(g);
1967
n = apply2files(lchown_internal, rest, &arg);
1972
rb_file_s_lchown(argc, argv)
1980
struct timeval rb_time_timeval();
1982
static void utime_internal _((const char *, void *));
1984
#if defined(HAVE_UTIMES) && !defined(__CHECKER__)
1987
utime_internal(path, arg)
1991
struct timeval *tvp = arg;
1992
if (utimes(path, tvp) < 0)
1998
* File.utime(atime, mtime, file_name,...) => integer
2000
* Sets the access and modification times of each
2001
* named file to the first two arguments. Returns
2002
* the number of file names in the argument list.
2006
rb_file_s_utime(argc, argv)
2010
VALUE atime, mtime, rest;
2011
struct timeval tvs[2], *tvp = NULL;
2014
rb_scan_args(argc, argv, "2*", &atime, &mtime, &rest);
2016
if (!NIL_P(atime) || !NIL_P(mtime)) {
2018
tvp[0] = rb_time_timeval(atime);
2019
tvp[1] = rb_time_timeval(mtime);
2022
n = apply2files(utime_internal, rest, tvp);
2028
#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
2036
utime_internal(path, arg)
2040
struct utimbuf *utp = arg;
2041
if (utime(path, utp) < 0)
2046
rb_file_s_utime(argc, argv)
2050
VALUE atime, mtime, rest;
2053
struct utimbuf utbuf, *utp = NULL;
2055
rb_scan_args(argc, argv, "2*", &atime, &mtime, &rest);
2057
if (!NIL_P(atime) || !NIL_P(mtime)) {
2059
tv = rb_time_timeval(atime);
2060
utp->actime = tv.tv_sec;
2061
tv = rb_time_timeval(mtime);
2062
utp->modtime = tv.tv_sec;
2065
n = apply2files(utime_internal, rest, utp);
2071
NORETURN(static void sys_fail2 _((VALUE,VALUE)));
2079
len = RSTRING(s1)->len + RSTRING(s2)->len + 5;
2080
buf = ALLOCA_N(char, len);
2081
snprintf(buf, len, "%s or %s", RSTRING(s1)->ptr, RSTRING(s2)->ptr);
2087
* File.link(old_name, new_name) => 0
2089
* Creates a new name for an existing file using a hard link. Will not
2090
* overwrite <i>new_name</i> if it already exists (raising a subclass
2091
* of <code>SystemCallError</code>). Not available on all platforms.
2093
* File.link("testfile", ".testfile") #=> 0
2094
* IO.readlines(".testfile")[0] #=> "This is line one\n"
2098
rb_file_s_link(klass, from, to)
2099
VALUE klass, from, to;
2102
SafeStringValue(from);
2103
SafeStringValue(to);
2105
if (link(StringValueCStr(from), StringValueCStr(to)) < 0) {
2106
sys_fail2(from, to);
2111
return Qnil; /* not reached */
2117
* File.symlink(old_name, new_name) => 0
2119
* Creates a symbolic link called <i>new_name</i> for the existing file
2120
* <i>old_name</i>. Raises a <code>NotImplemented</code> exception on
2121
* platforms that do not support symbolic links.
2123
* File.symlink("testfile", "link2test") #=> 0
2128
rb_file_s_symlink(klass, from, to)
2129
VALUE klass, from, to;
2132
SafeStringValue(from);
2133
SafeStringValue(to);
2135
if (symlink(StringValueCStr(from), StringValueCStr(to)) < 0) {
2136
sys_fail2(from, to);
2141
return Qnil; /* not reached */
2147
* File.readlink(link_name) -> file_name
2149
* Returns the name of the file referenced by the given link.
2150
* Not available on all platforms.
2152
* File.symlink("testfile", "link2test") #=> 0
2153
* File.readlink("link2test") #=> "testfile"
2157
rb_file_s_readlink(klass, path)
2160
#ifdef HAVE_READLINK
2166
SafeStringValue(path);
2167
buf = xmalloc(size);
2168
while ((rv = readlink(RSTRING(path)->ptr, buf, size)) == size
2170
|| (rv < 0 && errno == ERANGE) /* quirky behavior of GPFS */
2174
buf = xrealloc(buf, size);
2178
rb_sys_fail(RSTRING(path)->ptr);
2180
v = rb_tainted_str_new(buf, rv);
2186
return Qnil; /* not reached */
2190
static void unlink_internal _((const char *, void *));
2192
unlink_internal(path, arg)
2196
if (unlink(path) < 0)
2202
* File.delete(file_name, ...) => integer
2203
* File.unlink(file_name, ...) => integer
2205
* Deletes the named files, returning the number of names
2206
* passed as arguments. Raises an exception on any error.
2207
* See also <code>Dir::rmdir</code>.
2211
rb_file_s_unlink(klass, args)
2217
n = apply2files(unlink_internal, args, 0);
2223
* File.rename(old_name, new_name) => 0
2225
* Renames the given file to the new name. Raises a
2226
* <code>SystemCallError</code> if the file cannot be renamed.
2228
* File.rename("afile", "afile.bak") #=> 0
2232
rb_file_s_rename(klass, from, to)
2233
VALUE klass, from, to;
2235
const char *src, *dst;
2236
SafeStringValue(from);
2237
SafeStringValue(to);
2239
src = StringValueCStr(from);
2240
dst = StringValueCStr(to);
2241
#if defined __CYGWIN__
2244
if (rename(src, dst) < 0) {
2245
#if defined __CYGWIN__
2246
extern unsigned long __attribute__((stdcall)) GetLastError(void);
2247
if (errno == 0) { /* This is a bug of old Cygwin */
2248
/* incorrect as cygwin errno, but the last resort */
2249
errno = GetLastError();
2251
#elif defined DOSISH && !defined _WIN32
2253
#if defined (__EMX__)
2257
if (chmod(dst, 0666) == 0 &&
2259
rename(src, dst) == 0)
2263
sys_fail2(from, to);
2271
* File.umask() => integer
2272
* File.umask(integer) => integer
2274
* Returns the current umask value for this process. If the optional
2275
* argument is given, set the umask to that value and return the
2276
* previous value. Umask values are <em>subtracted</em> from the
2277
* default permissions, so a umask of <code>0222</code> would make a
2278
* file read-only for everyone.
2280
* File.umask(0006) #=> 18
2285
rb_file_s_umask(argc, argv)
2296
else if (argc == 1) {
2297
omask = umask(NUM2INT(argv[0]));
2300
rb_raise(rb_eArgError, "wrong number of arguments");
2302
return INT2FIX(omask);
2307
#define isdirsep(x) ((x) == '/' || (x) == '\\')
2309
#define isdirsep(x) ((x) == '/')
2311
#ifndef CharNext /* defined as CharNext[AW] on Windows. */
2313
# define CharNext(p) ((p) + mblen(p, MB_CUR_MAX))
2315
# define CharNext(p) ((p) + 1)
2322
#define DOSISH_DRIVE_LETTER
2325
#ifdef DOSISH_DRIVE_LETTER
2327
has_drive_letter(buf)
2330
if (ISALPHA(buf[0]) && buf[1] == ':') {
2343
char *drvcwd, *oldcwd;
2349
/* the only way that I know to get the current directory
2350
of a particular drive is to change chdir() to that drive,
2351
so save the old cwd before chdir()
2353
oldcwd = my_getcwd();
2354
if (chdir(drive) == 0) {
2355
drvcwd = my_getcwd();
2360
/* perhaps the drive is not exist. we return only drive letter */
2361
drvcwd = strdup(drive);
2367
static inline char *
2371
#ifdef DOSISH_DRIVE_LETTER
2372
if (has_drive_letter(path)) path += 2;
2374
while (isdirsep(*path)) path++;
2375
return (char *)path;
2378
#define nextdirsep rb_path_next
2383
while (*s && !isdirsep(*s)) {
2389
#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
2390
#define skipprefix rb_path_skip_prefix
2392
#define skipprefix(path) (path)
2395
rb_path_skip_prefix(path)
2398
#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
2400
if (isdirsep(path[0]) && isdirsep(path[1])) {
2402
while (isdirsep(*path)) path++;
2403
if (*(path = nextdirsep(path)) && path[1] && !isdirsep(path[1]))
2404
path = nextdirsep(path + 1);
2405
return (char *)path;
2408
#ifdef DOSISH_DRIVE_LETTER
2409
if (has_drive_letter(path))
2410
return (char *)(path + 2);
2413
return (char *)path;
2416
#define strrdirsep rb_path_last_separator
2418
rb_path_last_separator(path)
2423
if (isdirsep(*path)) {
2424
const char *tmp = path++;
2425
while (isdirsep(*path)) path++;
2430
path = CharNext(path);
2441
if (isdirsep(*path)) {
2442
const char *last = path++;
2443
while (isdirsep(*path)) path++;
2444
if (!*path) return (char *)last;
2447
path = CharNext(path);
2450
return (char *)path;
2457
if (isdirsep(*path)) path++;
2458
return chompdirsep(path);
2461
#define BUFCHECK(cond) do {\
2462
long bdiff = p - buf;\
2466
rb_str_resize(result, buflen);\
2467
buf = RSTRING(result)->ptr;\
2469
pend = buf + buflen;\
2472
#define BUFINIT() (\
2473
p = buf = RSTRING(result)->ptr,\
2474
buflen = RSTRING(result)->len,\
2477
#if !defined(TOLOWER)
2478
#define TOLOWER(c) (ISUPPER(c) ? tolower(c) : (c))
2481
static int is_absolute_path _((const char*));
2484
file_expand_path(fname, dname, result)
2485
VALUE fname, dname, result;
2487
char *s, *buf, *b, *p, *pend, *root;
2488
long buflen, dirlen;
2491
s = StringValuePtr(fname);
2493
tainted = OBJ_TAINTED(fname);
2496
if (isdirsep(s[1]) || s[1] == '\0') {
2497
char *dir = getenv("HOME");
2500
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `%s'", s);
2502
dirlen = strlen(dir);
2503
BUFCHECK(dirlen > buflen);
2505
#if defined DOSISH || defined __CYGWIN__
2506
for (p = buf; *p; p = CharNext(p)) {
2512
p = buf + strlen(dir);
2519
struct passwd *pwPtr;
2522
s = nextdirsep(b = s);
2523
BUFCHECK(bdiff + (s-b) >= buflen);
2528
pwPtr = getpwnam(buf);
2531
rb_raise(rb_eArgError, "user %s doesn't exist", buf);
2533
dirlen = strlen(pwPtr->pw_dir);
2534
BUFCHECK(dirlen > buflen);
2535
strcpy(buf, pwPtr->pw_dir);
2536
p = buf + strlen(pwPtr->pw_dir);
2541
#ifdef DOSISH_DRIVE_LETTER
2542
/* skip drive letter */
2543
else if (has_drive_letter(s)) {
2544
if (isdirsep(s[2])) {
2545
/* specified drive letter, and full path */
2546
/* skip drive letter */
2547
BUFCHECK(bdiff + 2 >= buflen);
2553
/* specified drive, but not full path */
2555
if (!NIL_P(dname)) {
2556
file_expand_path(dname, Qnil, result);
2558
if (has_drive_letter(p) && TOLOWER(p[0]) == TOLOWER(s[0])) {
2559
/* ok, same drive */
2564
char *dir = getcwdofdrv(*s);
2567
dirlen = strlen(dir);
2568
BUFCHECK(dirlen > buflen);
2572
p = chompdirsep(skiproot(buf));
2577
else if (!is_absolute_path(s)) {
2578
if (!NIL_P(dname)) {
2579
file_expand_path(dname, Qnil, result);
2583
char *dir = my_getcwd();
2586
dirlen = strlen(dir);
2587
BUFCHECK(dirlen > buflen);
2591
#if defined DOSISH || defined __CYGWIN__
2593
/* specified full path, but not drive letter nor UNC */
2594
/* we need to get the drive letter or UNC share name */
2595
p = skipprefix(buf);
2599
p = chompdirsep(skiproot(buf));
2603
do s++; while (isdirsep(*s));
2605
BUFCHECK(bdiff >= buflen);
2606
memset(buf, '/', p - buf);
2608
if (p > buf && p[-1] == '/')
2614
root = skipprefix(buf);
2620
if (b == s++) { /* beginning of path element */
2626
if (*(s+1) == '\0' || isdirsep(*(s+1))) {
2627
/* We must go back to the parent */
2629
if (!(b = strrdirsep(root))) {
2639
#if defined DOSISH || defined __CYGWIN__
2645
/* ordinary path element, beginning don't move */
2651
#if defined DOSISH || defined __CYGWIN__
2655
long rootdiff = root - buf;
2656
BUFCHECK(bdiff + (s-b+1) >= buflen);
2657
root = buf + rootdiff;
2658
memcpy(++p, b, s-b);
2671
BUFCHECK(bdiff + (s-b) >= buflen);
2672
memcpy(++p, b, s-b);
2675
if (p == skiproot(buf) - 1) p++;
2677
if (tainted) OBJ_TAINT(result);
2678
RSTRING(result)->len = p - buf;
2684
rb_file_expand_path(fname, dname)
2687
return file_expand_path(fname, dname, rb_str_new(0, MAXPATHLEN + 2));
2692
* File.expand_path(file_name [, dir_string] ) -> abs_file_name
2694
* Converts a pathname to an absolute pathname. Relative paths are
2695
* referenced from the current working directory of the process unless
2696
* <i>dir_string</i> is given, in which case it will be used as the
2697
* starting point. The given pathname may start with a
2698
* ``<code>~</code>'', which expands to the process owner's home
2699
* directory (the environment variable <code>HOME</code> must be set
2700
* correctly). ``<code>~</code><i>user</i>'' expands to the named
2701
* user's home directory.
2703
* File.expand_path("~oracle/bin") #=> "/home/oracle/bin"
2704
* File.expand_path("../../bin", "/tmp/x") #=> "/bin"
2708
rb_file_s_expand_path(argc, argv)
2715
return rb_file_expand_path(argv[0], Qnil);
2717
rb_scan_args(argc, argv, "11", &fname, &dname);
2719
return rb_file_expand_path(fname, dname);
2730
l1 = chompdirsep(p) - p;
2732
if (l2 == 2 && e[1] == '*') {
2737
if (l1 < l2) return l1;
2739
if (strncmp(p+l1-l2, e, l2) == 0) {
2747
* File.basename(file_name [, suffix] ) -> base_name
2749
* Returns the last component of the filename given in <i>file_name</i>,
2750
* which must be formed using forward slashes (``<code>/</code>'')
2751
* regardless of the separator used on the local file system. If
2752
* <i>suffix</i> is given and present at the end of <i>file_name</i>,
2755
* File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb"
2756
* File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
2760
rb_file_s_basename(argc, argv)
2764
VALUE fname, fext, basename;
2766
#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
2771
if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) {
2775
if (RSTRING(fname)->len == 0 || !*(name = RSTRING(fname)->ptr))
2777
name = skipprefix(name);
2778
#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
2781
while (isdirsep(*name))
2786
#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
2790
#ifdef DOSISH_DRIVE_LETTER
2791
else if (*p == ':') {
2803
else if (!(p = strrdirsep(name))) {
2804
if (NIL_P(fext) || !(f = rmext(name, StringValueCStr(fext)))) {
2805
f = chompdirsep(name) - name;
2806
if (f == RSTRING(fname)->len) return fname;
2811
while (isdirsep(*p)) p++; /* skip last / */
2812
if (NIL_P(fext) || !(f = rmext(p, StringValueCStr(fext)))) {
2813
f = chompdirsep(p) - p;
2816
basename = rb_str_new(p, f);
2817
OBJ_INFECT(basename, fname);
2823
* File.dirname(file_name ) -> dir_name
2825
* Returns all components of the filename given in <i>file_name</i>
2826
* except the last one. The filename must be formed using forward
2827
* slashes (``<code>/</code>'') regardless of the separator used on the
2828
* local file system.
2830
* File.dirname("/home/gumby/work/ruby.rb") #=> "/home/gumby/work"
2834
rb_file_s_dirname(klass, fname)
2837
const char *name, *root, *p;
2840
name = StringValueCStr(fname);
2841
root = skiproot(name);
2843
if (root > name + 1 && isdirsep(*name))
2844
root = skipprefix(name = root - 2);
2846
if (root > name + 1)
2849
p = strrdirsep(root);
2854
return rb_str_new2(".");
2855
#ifdef DOSISH_DRIVE_LETTER
2856
if (has_drive_letter(name) && isdirsep(*(name + 2))) {
2857
const char *top = skiproot(name + 2);
2858
dirname = rb_str_new(name, 3);
2859
rb_str_cat(dirname, top, p - top);
2863
dirname = rb_str_new(name, p - name);
2864
#ifdef DOSISH_DRIVE_LETTER
2865
if (has_drive_letter(name) && root == name + 2 && p - name == 2)
2866
rb_str_cat(dirname, ".", 1);
2868
OBJ_INFECT(dirname, fname);
2874
* File.extname(path) -> string
2876
* Returns the extension (the portion of file name in <i>path</i>
2877
* after the period).
2879
* File.extname("test.rb") #=> ".rb"
2880
* File.extname("a/b/d/test.rb") #=> ".rb"
2881
* File.extname("test") #=> ""
2882
* File.extname(".profile") #=> ""
2887
rb_file_s_extname(klass, fname)
2893
name = StringValueCStr(fname);
2894
p = strrdirsep(name); /* get the last path component */
2900
e = strrchr(p, '.'); /* get the last dot of the last component */
2901
if (!e || e == p || !e[1]) /* no dot, or the only dot is first or end? */
2902
return rb_str_new2("");
2903
extname = rb_str_new(e, chompdirsep(e) - e); /* keep the dot, too! */
2904
OBJ_INFECT(extname, fname);
2910
* File.split(file_name) => array
2912
* Splits the given string into a directory and a file component and
2913
* returns them in a two-element array. See also
2914
* <code>File::dirname</code> and <code>File::basename</code>.
2916
* File.split("/home/gumby/.profile") #=> ["/home/gumby", ".profile"]
2920
rb_file_s_split(klass, path)
2923
StringValue(path); /* get rid of converting twice */
2924
return rb_assoc_new(rb_file_s_dirname(Qnil, path), rb_file_s_basename(1,&path));
2927
static VALUE separator;
2929
static VALUE rb_file_join _((VALUE ary, VALUE sep));
2932
file_inspect_join(ary, arg)
2936
return rb_file_join(arg[0], arg[1]);
2940
rb_file_join(ary, sep)
2948
if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
2949
if (OBJ_TAINTED(ary)) taint = 1;
2950
if (OBJ_TAINTED(sep)) taint = 1;
2953
for (i=0; i<RARRAY(ary)->len; i++) {
2954
if (TYPE(RARRAY(ary)->ptr[i]) == T_STRING) {
2955
len += RSTRING(RARRAY(ary)->ptr[i])->len;
2961
if (!NIL_P(sep) && TYPE(sep) == T_STRING) {
2962
len += RSTRING(sep)->len * RARRAY(ary)->len - 1;
2964
result = rb_str_buf_new(len);
2965
for (i=0; i<RARRAY(ary)->len; i++) {
2966
tmp = RARRAY(ary)->ptr[i];
2967
switch (TYPE(tmp)) {
2971
if (rb_inspecting_p(tmp)) {
2972
tmp = rb_str_new2("[...]");
2979
tmp = rb_protect_inspect(file_inspect_join, ary, (VALUE)args);
2983
StringValueCStr(tmp);
2985
name = StringValueCStr(result);
2986
if (i > 0 && !NIL_P(sep)) {
2987
tail = chompdirsep(name);
2988
if (RSTRING(tmp)->ptr && isdirsep(RSTRING(tmp)->ptr[0])) {
2989
RSTRING(result)->len = tail - name;
2992
rb_str_buf_append(result, sep);
2995
rb_str_buf_append(result, tmp);
2996
if (OBJ_TAINTED(tmp)) taint = 1;
2999
if (taint) OBJ_TAINT(result);
3005
* File.join(string, ...) -> path
3007
* Returns a new string formed by joining the strings using
3008
* <code>File::SEPARATOR</code>.
3010
* File.join("usr", "mail", "gumby") #=> "usr/mail/gumby"
3015
rb_file_s_join(klass, args)
3018
return rb_file_join(args, separator);
3023
* File.truncate(file_name, integer) => 0
3025
* Truncates the file <i>file_name</i> to be at most <i>integer</i>
3026
* bytes long. Not available on all platforms.
3028
* f = File.new("out", "w")
3029
* f.write("1234567890") #=> 10
3031
* File.truncate("out", 5) #=> 0
3032
* File.size("out") #=> 5
3037
rb_file_s_truncate(klass, path, len)
3038
VALUE klass, path, len;
3043
pos = NUM2OFFT(len);
3044
SafeStringValue(path);
3046
#ifdef HAVE_TRUNCATE
3047
if (truncate(StringValueCStr(path), pos) < 0)
3048
rb_sys_fail(RSTRING(path)->ptr);
3055
if ((tmpfd = open(StringValueCStr(path), O_RDWR)) < 0) {
3056
rb_sys_fail(RSTRING(path)->ptr);
3059
if ((tmpfd = open(StringValueCStr(path), 0)) < 0) {
3060
rb_sys_fail(RSTRING(path)->ptr);
3063
if (chsize(tmpfd, pos) < 0) {
3065
rb_sys_fail(RSTRING(path)->ptr);
3078
* file.truncate(integer) => 0
3080
* Truncates <i>file</i> to at most <i>integer</i> bytes. The file
3081
* must be opened for writing. Not available on all platforms.
3083
* f = File.new("out", "w")
3084
* f.syswrite("1234567890") #=> 10
3085
* f.truncate(5) #=> 0
3087
* File.size("out") #=> 5
3091
rb_file_truncate(obj, len)
3099
pos = NUM2OFFT(len);
3100
GetOpenFile(obj, fptr);
3101
if (!(fptr->mode & FMODE_WRITABLE)) {
3102
rb_raise(rb_eIOError, "not opened for writing");
3104
f = GetWriteFile(fptr);
3106
fseeko(f, (off_t)0, SEEK_CUR);
3107
#ifdef HAVE_TRUNCATE
3108
if (ftruncate(fileno(f), pos) < 0)
3109
rb_sys_fail(fptr->path);
3112
if (chsize(fileno(f), pos) < 0)
3113
rb_sys_fail(fptr->path);
3136
#include <winerror.h>
3137
cygwin_flock(int fd, int op)
3139
int old_errno = errno;
3140
int ret = flock(fd, op);
3141
if (GetLastError() == ERROR_NOT_LOCKED) {
3147
# define flock(fd, op) cygwin_flock(fd, op)
3151
rb_thread_flock(fd, op, fptr)
3155
if (rb_thread_alone() || (op & LOCK_NB)) {
3158
ret = flock(fd, op);
3163
while (flock(fd, op) < 0) {
3167
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3170
rb_thread_polling(); /* busy wait */
3171
rb_io_check_closed(fptr);
3182
#define flock(fd, op) rb_thread_flock(fd, op, fptr)
3186
* file.flock (locking_constant ) => 0 or false
3188
* Locks or unlocks a file according to <i>locking_constant</i> (a
3189
* logical <em>or</em> of the values in the table below).
3190
* Returns <code>false</code> if <code>File::LOCK_NB</code> is
3191
* specified and the operation would otherwise have blocked. Not
3192
* available on all platforms.
3194
* Locking constants (in class File):
3196
* LOCK_EX | Exclusive lock. Only one process may hold an
3197
* | exclusive lock for a given file at a time.
3198
* ----------+------------------------------------------------
3199
* LOCK_NB | Don't block when locking. May be combined
3200
* | with other lock options using logical or.
3201
* ----------+------------------------------------------------
3202
* LOCK_SH | Shared lock. Multiple processes may each hold a
3203
* | shared lock for a given file at the same time.
3204
* ----------+------------------------------------------------
3209
* File.new("testfile").flock(File::LOCK_UN) #=> 0
3214
rb_file_flock(obj, operation)
3223
op = NUM2INT(operation);
3224
GetOpenFile(obj, fptr);
3226
if (fptr->mode & FMODE_WRITABLE) {
3227
fflush(GetWriteFile(fptr));
3230
if (flock(fileno(fptr->f), op) < 0) {
3234
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3239
#if defined(ERESTART)
3244
rb_sys_fail(fptr->path);
3252
test_check(n, argc, argv)
3259
if (n != argc) rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, n);
3260
for (i=1; i<n; i++) {
3261
switch (TYPE(argv[i])) {
3264
SafeStringValue(argv[i]);
3272
#define CHECK(n) test_check((n), argc, argv)
3276
* test(int_cmd, file1 [, file2] ) => obj
3278
* Uses the integer <i>aCmd</i> to perform various tests on
3279
* <i>file1</i> (first table below) or on <i>file1</i> and
3280
* <i>file2</i> (second table).
3282
* File tests on a single file:
3284
* Test Returns Meaning
3285
* ?A | Time | Last access time for file1
3286
* ?b | boolean | True if file1 is a block device
3287
* ?c | boolean | True if file1 is a character device
3288
* ?C | Time | Last change time for file1
3289
* ?d | boolean | True if file1 exists and is a directory
3290
* ?e | boolean | True if file1 exists
3291
* ?f | boolean | True if file1 exists and is a regular file
3292
* ?g | boolean | True if file1 has the \CF{setgid} bit
3293
* | | set (false under NT)
3294
* ?G | boolean | True if file1 exists and has a group
3295
* | | ownership equal to the caller's group
3296
* ?k | boolean | True if file1 exists and has the sticky bit set
3297
* ?l | boolean | True if file1 exists and is a symbolic link
3298
* ?M | Time | Last modification time for file1
3299
* ?o | boolean | True if file1 exists and is owned by
3300
* | | the caller's effective uid
3301
* ?O | boolean | True if file1 exists and is owned by
3302
* | | the caller's real uid
3303
* ?p | boolean | True if file1 exists and is a fifo
3304
* ?r | boolean | True if file1 is readable by the effective
3305
* | | uid/gid of the caller
3306
* ?R | boolean | True if file is readable by the real
3307
* | | uid/gid of the caller
3308
* ?s | int/nil | If file1 has nonzero size, return the size,
3309
* | | otherwise return nil
3310
* ?S | boolean | True if file1 exists and is a socket
3311
* ?u | boolean | True if file1 has the setuid bit set
3312
* ?w | boolean | True if file1 exists and is writable by
3313
* | | the effective uid/gid
3314
* ?W | boolean | True if file1 exists and is writable by
3315
* | | the real uid/gid
3316
* ?x | boolean | True if file1 exists and is executable by
3317
* | | the effective uid/gid
3318
* ?X | boolean | True if file1 exists and is executable by
3319
* | | the real uid/gid
3320
* ?z | boolean | True if file1 exists and has a zero length
3322
* Tests that take two files:
3324
* ?- | boolean | True if file1 and file2 are identical
3325
* ?= | boolean | True if the modification times of file1
3326
* | | and file2 are equal
3327
* ?< | boolean | True if the modification time of file1
3328
* | | is prior to that of file2
3329
* ?> | boolean | True if the modification time of file1
3330
* | | is after that of file2
3334
rb_f_test(argc, argv)
3340
if (argc == 0) rb_raise(rb_eArgError, "wrong number of arguments");
3341
#if 0 /* 1.7 behavior? */
3343
return RTEST(argv[0]) ? Qtrue : Qfalse;
3346
cmd = NUM2CHR(argv[0]);
3347
if (cmd == 0) return Qfalse;
3348
if (strchr("bcdefgGkloOprRsSuwWxXz", cmd)) {
3352
return test_b(0, argv[1]);
3355
return test_c(0, argv[1]);
3358
return test_d(0, argv[1]);
3362
return test_e(0, argv[1]);
3365
return test_f(0, argv[1]);
3368
return test_sgid(0, argv[1]);
3371
return test_grpowned(0, argv[1]);
3374
return test_sticky(0, argv[1]);
3377
return test_l(0, argv[1]);
3380
return test_owned(0, argv[1]);
3383
return test_rowned(0, argv[1]);
3386
return test_p(0, argv[1]);
3389
return test_r(0, argv[1]);
3392
return test_R(0, argv[1]);
3395
return test_s(0, argv[1]);
3398
return test_S(0, argv[1]);
3401
return test_suid(0, argv[1]);
3404
return test_w(0, argv[1]);
3407
return test_W(0, argv[1]);
3410
return test_x(0, argv[1]);
3413
return test_X(0, argv[1]);
3416
return test_z(0, argv[1]);
3420
if (strchr("MAC", cmd)) {
3424
if (rb_stat(argv[1], &st) == -1) {
3425
rb_sys_fail(RSTRING(argv[1])->ptr);
3430
return rb_time_new(st.st_atime, 0);
3432
return rb_time_new(st.st_mtime, 0);
3434
return rb_time_new(st.st_ctime, 0);
3440
return test_identical(0, argv[1], argv[2]);
3443
if (strchr("=<>", cmd)) {
3444
struct stat st1, st2;
3447
if (rb_stat(argv[1], &st1) < 0) return Qfalse;
3448
if (rb_stat(argv[2], &st2) < 0) return Qfalse;
3452
if (st1.st_mtime == st2.st_mtime) return Qtrue;
3456
if (st1.st_mtime > st2.st_mtime) return Qtrue;
3460
if (st1.st_mtime < st2.st_mtime) return Qtrue;
3464
/* unknown command */
3465
rb_raise(rb_eArgError, "unknown command ?%c", cmd);
3466
return Qnil; /* not reached */
3472
* Document-class: File::Stat
3474
* Objects of class <code>File::Stat</code> encapsulate common status
3475
* information for <code>File</code> objects. The information is
3476
* recorded at the moment the <code>File::Stat</code> object is
3477
* created; changes made to the file after that point will not be
3478
* reflected. <code>File::Stat</code> objects are returned by
3479
* <code>IO#stat</code>, <code>File::stat</code>,
3480
* <code>File#lstat</code>, and <code>File::lstat</code>. Many of these
3481
* methods return platform-specific values, and not all values are
3482
* meaningful on all systems. See also <code>Kernel#test</code>.
3485
static VALUE rb_stat_s_alloc _((VALUE));
3487
rb_stat_s_alloc(klass)
3490
return stat_new_0(klass, 0);
3496
* File::Stat.new(file_name) => stat
3498
* Create a File::Stat object for the given file name (raising an
3499
* exception if the file doesn't exist).
3503
rb_stat_init(obj, fname)
3506
struct stat st, *nst;
3508
SafeStringValue(fname);
3510
if (stat(StringValueCStr(fname), &st) == -1) {
3511
rb_sys_fail(RSTRING(fname)->ptr);
3513
if (DATA_PTR(obj)) {
3514
free(DATA_PTR(obj));
3515
DATA_PTR(obj) = NULL;
3517
nst = ALLOC(struct stat);
3519
DATA_PTR(obj) = nst;
3526
rb_stat_init_copy(copy, orig)
3531
if (copy == orig) return orig;
3532
rb_check_frozen(copy);
3533
/* need better argument type check */
3534
if (!rb_obj_is_instance_of(orig, rb_obj_class(copy))) {
3535
rb_raise(rb_eTypeError, "wrong argument class");
3537
if (DATA_PTR(copy)) {
3538
free(DATA_PTR(copy));
3541
if (DATA_PTR(orig)) {
3542
nst = ALLOC(struct stat);
3543
*nst = *(struct stat*)DATA_PTR(orig);
3544
DATA_PTR(copy) = nst;
3552
* stat.ftype => string
3554
* Identifies the type of <i>stat</i>. The return string is one of:
3555
* ``<code>file</code>'', ``<code>directory</code>'',
3556
* ``<code>characterSpecial</code>'', ``<code>blockSpecial</code>'',
3557
* ``<code>fifo</code>'', ``<code>link</code>'',
3558
* ``<code>socket</code>'', or ``<code>unknown</code>''.
3560
* File.stat("/dev/tty").ftype #=> "characterSpecial"
3568
return rb_file_ftype(get_stat(obj));
3573
* stat.directory? => true or false
3575
* Returns <code>true</code> if <i>stat</i> is a directory,
3576
* <code>false</code> otherwise.
3578
* File.stat("testfile").directory? #=> false
3579
* File.stat(".").directory? #=> true
3586
if (S_ISDIR(get_stat(obj)->st_mode)) return Qtrue;
3592
* stat.pipe? => true or false
3594
* Returns <code>true</code> if the operating system supports pipes and
3595
* <i>stat</i> is a pipe; <code>false</code> otherwise.
3603
if (S_ISFIFO(get_stat(obj)->st_mode)) return Qtrue;
3611
* stat.symlink? => true or false
3613
* Returns <code>true</code> if <i>stat</i> is a symbolic link,
3614
* <code>false</code> if it isn't or if the operating system doesn't
3615
* support this feature. As <code>File::stat</code> automatically
3616
* follows symbolic links, <code>symlink?</code> will always be
3617
* <code>false</code> for an object returned by
3618
* <code>File::stat</code>.
3620
* File.symlink("testfile", "alink") #=> 0
3621
* File.stat("alink").symlink? #=> false
3622
* File.lstat("alink").symlink? #=> true
3631
if (S_ISLNK(get_stat(obj)->st_mode)) return Qtrue;
3638
* stat.socket? => true or false
3640
* Returns <code>true</code> if <i>stat</i> is a socket,
3641
* <code>false</code> if it isn't or if the operating system doesn't
3642
* support this feature.
3644
* File.stat("testfile").socket? #=> false
3653
if (S_ISSOCK(get_stat(obj)->st_mode)) return Qtrue;
3661
* stat.blockdev? => true or false
3663
* Returns <code>true</code> if the file is a block device,
3664
* <code>false</code> if it isn't or if the operating system doesn't
3665
* support this feature.
3667
* File.stat("testfile").blockdev? #=> false
3668
* File.stat("/dev/hda1").blockdev? #=> true
3677
if (S_ISBLK(get_stat(obj)->st_mode)) return Qtrue;
3685
* stat.chardev? => true or false
3687
* Returns <code>true</code> if the file is a character device,
3688
* <code>false</code> if it isn't or if the operating system doesn't
3689
* support this feature.
3691
* File.stat("/dev/tty").chardev? #=> true
3699
if (S_ISCHR(get_stat(obj)->st_mode)) return Qtrue;
3706
* stat.owned? => true or false
3708
* Returns <code>true</code> if the effective user id of the process is
3709
* the same as the owner of <i>stat</i>.
3711
* File.stat("testfile").owned? #=> true
3712
* File.stat("/etc/passwd").owned? #=> false
3720
if (get_stat(obj)->st_uid == geteuid()) return Qtrue;
3728
if (get_stat(obj)->st_uid == getuid()) return Qtrue;
3734
* stat.grpowned? => true or false
3736
* Returns true if the effective group id of the process is the same as
3737
* the group id of <i>stat</i>. On Windows NT, returns <code>false</code>.
3739
* File.stat("testfile").grpowned? #=> true
3740
* File.stat("/etc/passwd").grpowned? #=> false
3745
rb_stat_grpowned(obj)
3749
if (group_member(get_stat(obj)->st_gid)) return Qtrue;
3756
* stat.readable? => true or false
3758
* Returns <code>true</code> if <i>stat</i> is readable by the
3759
* effective user id of this process.
3761
* File.stat("testfile").readable? #=> true
3769
struct stat *st = get_stat(obj);
3772
if (geteuid() == 0) return Qtrue;
3775
if (rb_stat_owned(obj))
3776
return st->st_mode & S_IRUSR ? Qtrue : Qfalse;
3779
if (rb_stat_grpowned(obj))
3780
return st->st_mode & S_IRGRP ? Qtrue : Qfalse;
3783
if (!(st->st_mode & S_IROTH)) return Qfalse;
3792
* stat.readable_real? -> true or false
3794
* Returns <code>true</code> if <i>stat</i> is readable by the real
3795
* user id of this process.
3797
* File.stat("testfile").readable_real? #=> true
3805
struct stat *st = get_stat(obj);
3808
if (getuid() == 0) return Qtrue;
3811
if (rb_stat_rowned(obj))
3812
return st->st_mode & S_IRUSR ? Qtrue : Qfalse;
3815
if (group_member(get_stat(obj)->st_gid))
3816
return st->st_mode & S_IRGRP ? Qtrue : Qfalse;
3819
if (!(st->st_mode & S_IROTH)) return Qfalse;
3826
* stat.writable? -> true or false
3828
* Returns <code>true</code> if <i>stat</i> is writable by the
3829
* effective user id of this process.
3831
* File.stat("testfile").writable? #=> true
3839
struct stat *st = get_stat(obj);
3842
if (geteuid() == 0) return Qtrue;
3845
if (rb_stat_owned(obj))
3846
return st->st_mode & S_IWUSR ? Qtrue : Qfalse;
3849
if (rb_stat_grpowned(obj))
3850
return st->st_mode & S_IWGRP ? Qtrue : Qfalse;
3853
if (!(st->st_mode & S_IWOTH)) return Qfalse;
3860
* stat.writable_real? -> true or false
3862
* Returns <code>true</code> if <i>stat</i> is writable by the real
3863
* user id of this process.
3865
* File.stat("testfile").writable_real? #=> true
3873
struct stat *st = get_stat(obj);
3876
if (getuid() == 0) return Qtrue;
3879
if (rb_stat_rowned(obj))
3880
return st->st_mode & S_IWUSR ? Qtrue : Qfalse;
3883
if (group_member(get_stat(obj)->st_gid))
3884
return st->st_mode & S_IWGRP ? Qtrue : Qfalse;
3887
if (!(st->st_mode & S_IWOTH)) return Qfalse;
3894
* stat.executable? => true or false
3896
* Returns <code>true</code> if <i>stat</i> is executable or if the
3897
* operating system doesn't distinguish executable files from
3898
* nonexecutable files. The tests are made using the effective owner of
3901
* File.stat("testfile").executable? #=> false
3909
struct stat *st = get_stat(obj);
3912
if (geteuid() == 0) {
3913
return st->st_mode & S_IXUGO ? Qtrue : Qfalse;
3917
if (rb_stat_owned(obj))
3918
return st->st_mode & S_IXUSR ? Qtrue : Qfalse;
3921
if (rb_stat_grpowned(obj))
3922
return st->st_mode & S_IXGRP ? Qtrue : Qfalse;
3925
if (!(st->st_mode & S_IXOTH)) return Qfalse;
3932
* stat.executable_real? => true or false
3934
* Same as <code>executable?</code>, but tests using the real owner of
3943
struct stat *st = get_stat(obj);
3946
if (getuid() == 0) {
3947
return st->st_mode & S_IXUGO ? Qtrue : Qfalse;
3951
if (rb_stat_rowned(obj))
3952
return st->st_mode & S_IXUSR ? Qtrue : Qfalse;
3955
if (group_member(get_stat(obj)->st_gid))
3956
return st->st_mode & S_IXGRP ? Qtrue : Qfalse;
3959
if (!(st->st_mode & S_IXOTH)) return Qfalse;
3966
* stat.file? => true or false
3968
* Returns <code>true</code> if <i>stat</i> is a regular file (not
3969
* a device file, pipe, socket, etc.).
3971
* File.stat("testfile").file? #=> true
3979
if (S_ISREG(get_stat(obj)->st_mode)) return Qtrue;
3985
* stat.zero? => true or false
3987
* Returns <code>true</code> if <i>stat</i> is a zero-length file;
3988
* <code>false</code> otherwise.
3990
* File.stat("testfile").zero? #=> false
3998
if (get_stat(obj)->st_size == 0) return Qtrue;
4005
* state.size => integer
4007
* Returns the size of <i>stat</i> in bytes.
4009
* File.stat("testfile").size #=> 66
4017
off_t size = get_stat(obj)->st_size;
4019
if (size == 0) return Qnil;
4020
return OFFT2NUM(size);
4025
* stat.setuid? => true or false
4027
* Returns <code>true</code> if <i>stat</i> has the set-user-id
4028
* permission bit set, <code>false</code> if it doesn't or if the
4029
* operating system doesn't support this feature.
4031
* File.stat("/bin/su").setuid? #=> true
4039
if (get_stat(obj)->st_mode & S_ISUID) return Qtrue;
4046
* stat.setgid? => true or false
4048
* Returns <code>true</code> if <i>stat</i> has the set-group-id
4049
* permission bit set, <code>false</code> if it doesn't or if the
4050
* operating system doesn't support this feature.
4052
* File.stat("/usr/sbin/lpc").setgid? #=> true
4061
if (get_stat(obj)->st_mode & S_ISGID) return Qtrue;
4068
* stat.sticky? => true or false
4070
* Returns <code>true</code> if <i>stat</i> has its sticky bit set,
4071
* <code>false</code> if it doesn't or if the operating system doesn't
4072
* support this feature.
4074
* File.stat("testfile").sticky? #=> false
4083
if (get_stat(obj)->st_mode & S_ISVTX) return Qtrue;
4091
rb_file_const(name, value)
4095
rb_define_const(rb_mFConst, name, value);
4099
is_absolute_path(path)
4102
#ifdef DOSISH_DRIVE_LETTER
4103
if (has_drive_letter(path) && isdirsep(path[2])) return 1;
4106
if (isdirsep(path[0]) && isdirsep(path[1])) return 1;
4109
if (path[0] == '/') return 1;
4114
#ifndef ENABLE_PATH_CHECK
4115
# if defined DOSISH || defined __CYGWIN__
4116
# define ENABLE_PATH_CHECK 0
4118
# define ENABLE_PATH_CHECK 1
4122
#if ENABLE_PATH_CHECK
4124
path_check_0(fpath, execpath)
4129
char *p0 = StringValueCStr(fpath);
4132
if (!is_absolute_path(p0)) {
4133
char *buf = my_getcwd();
4136
newpath = rb_str_new2(buf);
4139
rb_str_cat2(newpath, "/");
4140
rb_str_cat2(newpath, p0);
4141
p0 = RSTRING(fpath = newpath)->ptr;
4145
# define S_IWOTH 002
4147
if (stat(p0, &st) == 0 && S_ISDIR(st.st_mode) && (st.st_mode & S_IWOTH)
4149
&& !(p && execpath && (st.st_mode & S_ISVTX))
4152
rb_warn("Insecure world writable dir %s in %sPATH, mode 0%o",
4153
p0, (execpath ? "" : "LOAD_"), st.st_mode);
4159
if (!s || s == p0) return 1;
4170
#if ENABLE_PATH_CHECK
4171
return path_check_0(rb_str_new2(path), Qfalse);
4181
#if ENABLE_PATH_CHECK
4182
char *p0, *p, *pend;
4183
const char sep = PATH_SEP_CHAR;
4185
if (!path) return 1;
4187
pend = path + strlen(path);
4189
p = strchr(path, sep);
4193
if (!path_check_0(rb_str_new(p0, p - p0), Qtrue)) {
4194
return 0; /* not safe */
4197
if (p0 > pend) break;
4198
p = strchr(p0, sep);
4205
#if defined(__MACOS__) || defined(riscos)
4207
is_macos_native_path(path)
4210
if (strchr(path, ':')) return 1;
4221
if (!file) return 0;
4222
f = fopen(file, "r");
4223
if (f == NULL) return 0;
4228
extern VALUE rb_load_path;
4231
rb_find_file_ext(filep, ext)
4233
const char * const *ext;
4236
char *f = RSTRING(*filep)->ptr;
4241
fname = rb_file_expand_path(*filep, Qnil);
4242
if (rb_safe_level() >= 2 && OBJ_TAINTED(fname)) {
4243
rb_raise(rb_eSecurityError, "loading from unsafe file %s", f);
4246
f = StringValueCStr(fname);
4250
if (is_absolute_path(f)) {
4251
for (i=0; ext[i]; i++) {
4252
fname = rb_str_dup(*filep);
4253
rb_str_cat2(fname, ext[i]);
4255
if (file_load_ok(StringValueCStr(fname))) {
4263
if (!rb_load_path) return 0;
4265
Check_Type(rb_load_path, T_ARRAY);
4266
for (i=0;i<RARRAY(rb_load_path)->len;i++) {
4267
VALUE str = RARRAY(rb_load_path)->ptr[i];
4269
SafeStringValue(str);
4270
if (RSTRING(str)->len == 0) continue;
4271
path = RSTRING(str)->ptr;
4272
for (j=0; ext[j]; j++) {
4273
fname = rb_str_dup(*filep);
4274
rb_str_cat2(fname, ext[j]);
4276
found = dln_find_file(StringValueCStr(fname), path);
4277
if (found && file_load_ok(found)) {
4291
char *f = StringValueCStr(path);
4295
path = rb_file_expand_path(path, Qnil);
4296
if (rb_safe_level() >= 1 && OBJ_TAINTED(path)) {
4297
rb_raise(rb_eSecurityError, "loading from unsafe path %s", f);
4300
f = StringValueCStr(path);
4303
#if defined(__MACOS__) || defined(riscos)
4304
if (is_macos_native_path(f)) {
4305
if (rb_safe_level() >= 1 && !fpath_check(f)) {
4306
rb_raise(rb_eSecurityError, "loading from unsafe file %s", f);
4308
if (file_load_ok(f)) return path;
4312
if (is_absolute_path(f)) {
4313
if (rb_safe_level() >= 1 && !fpath_check(f)) {
4314
rb_raise(rb_eSecurityError, "loading from unsafe file %s", f);
4316
if (file_load_ok(f)) return path;
4319
if (rb_safe_level() >= 4) {
4320
rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f);
4326
Check_Type(rb_load_path, T_ARRAY);
4328
for (i=0;i<RARRAY(rb_load_path)->len;i++) {
4329
VALUE str = RARRAY(rb_load_path)->ptr[i];
4330
SafeStringValue(str);
4331
if (RSTRING(str)->len > 0) {
4332
rb_ary_push(tmp, str);
4335
tmp = rb_ary_join(tmp, rb_str_new2(PATH_SEP));
4336
if (RSTRING(tmp)->len == 0) {
4340
lpath = RSTRING(tmp)->ptr;
4348
return 0; /* no path, no load */
4350
if (!(f = dln_find_file(f, lpath))) {
4353
if (rb_safe_level() >= 1 && !fpath_check(f)) {
4354
rb_raise(rb_eSecurityError, "loading from unsafe file %s", f);
4356
if (file_load_ok(f)) {
4357
tmp = rb_str_new2(f);
4365
define_filetest_function(name, func, argc)
4370
rb_define_module_function(rb_mFileTest, name, func, argc);
4371
rb_define_singleton_method(rb_cFile, name, func, argc);
4376
* A <code>File</code> is an abstraction of any file object accessible
4377
* by the program and is closely associated with class <code>IO</code>
4378
* <code>File</code> includes the methods of module
4379
* <code>FileTest</code> as class methods, allowing you to write (for
4380
* example) <code>File.exist?("foo")</code>.
4382
* In the description of File methods,
4383
* <em>permission bits</em> are a platform-specific
4384
* set of bits that indicate permissions of a file. On Unix-based
4385
* systems, permissions are viewed as a set of three octets, for the
4386
* owner, the group, and the rest of the world. For each of these
4387
* entities, permissions may be set to read, write, or execute the
4390
* The permission bits <code>0644</code> (in octal) would thus be
4391
* interpreted as read/write for owner, and read-only for group and
4392
* other. Higher-order bits may also be used to indicate the type of
4393
* file (plain, directory, pipe, socket, and so on) and various other
4394
* special features. If the permissions are for a directory, the
4395
* meaning of the execute bit changes; when set the directory can be
4398
* On non-Posix operating systems, there may be only the ability to
4399
* make a file read-only or read-write. In this case, the remaining
4400
* permission bits will be synthesized to resemble typical values. For
4401
* instance, on Windows NT the default permission bits are
4402
* <code>0644</code>, which means read/write for owner, read-only for
4403
* all others. The only change that can be made is to make the file
4404
* read-only, which is reported as <code>0444</code>.
4410
rb_mFileTest = rb_define_module("FileTest");
4411
rb_cFile = rb_define_class("File", rb_cIO);
4413
define_filetest_function("directory?", test_d, 1);
4414
define_filetest_function("exist?", test_e, 1);
4415
define_filetest_function("exists?", test_e, 1); /* temporary */
4416
define_filetest_function("readable?", test_r, 1);
4417
define_filetest_function("readable_real?", test_R, 1);
4418
define_filetest_function("writable?", test_w, 1);
4419
define_filetest_function("writable_real?", test_W, 1);
4420
define_filetest_function("executable?", test_x, 1);
4421
define_filetest_function("executable_real?", test_X, 1);
4422
define_filetest_function("file?", test_f, 1);
4423
define_filetest_function("zero?", test_z, 1);
4424
define_filetest_function("size?", test_s, 1);
4425
define_filetest_function("size", rb_file_s_size, 1);
4426
define_filetest_function("owned?", test_owned, 1);
4427
define_filetest_function("grpowned?", test_grpowned, 1);
4429
define_filetest_function("pipe?", test_p, 1);
4430
define_filetest_function("symlink?", test_l, 1);
4431
define_filetest_function("socket?", test_S, 1);
4433
define_filetest_function("blockdev?", test_b, 1);
4434
define_filetest_function("chardev?", test_c, 1);
4436
define_filetest_function("setuid?", test_suid, 1);
4437
define_filetest_function("setgid?", test_sgid, 1);
4438
define_filetest_function("sticky?", test_sticky, 1);
4440
define_filetest_function("identical?", test_identical, 2);
4442
rb_define_singleton_method(rb_cFile, "stat", rb_file_s_stat, 1);
4443
rb_define_singleton_method(rb_cFile, "lstat", rb_file_s_lstat, 1);
4444
rb_define_singleton_method(rb_cFile, "ftype", rb_file_s_ftype, 1);
4446
rb_define_singleton_method(rb_cFile, "atime", rb_file_s_atime, 1);
4447
rb_define_singleton_method(rb_cFile, "mtime", rb_file_s_mtime, 1);
4448
rb_define_singleton_method(rb_cFile, "ctime", rb_file_s_ctime, 1);
4450
rb_define_singleton_method(rb_cFile, "utime", rb_file_s_utime, -1);
4451
rb_define_singleton_method(rb_cFile, "chmod", rb_file_s_chmod, -1);
4452
rb_define_singleton_method(rb_cFile, "chown", rb_file_s_chown, -1);
4453
rb_define_singleton_method(rb_cFile, "lchmod", rb_file_s_lchmod, -1);
4454
rb_define_singleton_method(rb_cFile, "lchown", rb_file_s_lchown, -1);
4456
rb_define_singleton_method(rb_cFile, "link", rb_file_s_link, 2);
4457
rb_define_singleton_method(rb_cFile, "symlink", rb_file_s_symlink, 2);
4458
rb_define_singleton_method(rb_cFile, "readlink", rb_file_s_readlink, 1);
4460
rb_define_singleton_method(rb_cFile, "unlink", rb_file_s_unlink, -2);
4461
rb_define_singleton_method(rb_cFile, "delete", rb_file_s_unlink, -2);
4462
rb_define_singleton_method(rb_cFile, "rename", rb_file_s_rename, 2);
4463
rb_define_singleton_method(rb_cFile, "umask", rb_file_s_umask, -1);
4464
rb_define_singleton_method(rb_cFile, "truncate", rb_file_s_truncate, 2);
4465
rb_define_singleton_method(rb_cFile, "expand_path", rb_file_s_expand_path, -1);
4466
rb_define_singleton_method(rb_cFile, "basename", rb_file_s_basename, -1);
4467
rb_define_singleton_method(rb_cFile, "dirname", rb_file_s_dirname, 1);
4468
rb_define_singleton_method(rb_cFile, "extname", rb_file_s_extname, 1);
4470
separator = rb_obj_freeze(rb_str_new2("/"));
4471
rb_define_const(rb_cFile, "Separator", separator);
4472
rb_define_const(rb_cFile, "SEPARATOR", separator);
4473
rb_define_singleton_method(rb_cFile, "split", rb_file_s_split, 1);
4474
rb_define_singleton_method(rb_cFile, "join", rb_file_s_join, -2);
4477
rb_define_const(rb_cFile, "ALT_SEPARATOR", rb_obj_freeze(rb_str_new2("\\")));
4479
rb_define_const(rb_cFile, "ALT_SEPARATOR", Qnil);
4481
rb_define_const(rb_cFile, "PATH_SEPARATOR", rb_obj_freeze(rb_str_new2(PATH_SEP)));
4483
rb_define_method(rb_cIO, "stat", rb_io_stat, 0); /* this is IO's method */
4484
rb_define_method(rb_cFile, "lstat", rb_file_lstat, 0);
4486
rb_define_method(rb_cFile, "atime", rb_file_atime, 0);
4487
rb_define_method(rb_cFile, "mtime", rb_file_mtime, 0);
4488
rb_define_method(rb_cFile, "ctime", rb_file_ctime, 0);
4490
rb_define_method(rb_cFile, "chmod", rb_file_chmod, 1);
4491
rb_define_method(rb_cFile, "chown", rb_file_chown, 2);
4492
rb_define_method(rb_cFile, "truncate", rb_file_truncate, 1);
4494
rb_define_method(rb_cFile, "flock", rb_file_flock, 1);
4496
rb_mFConst = rb_define_module_under(rb_cFile, "Constants");
4497
rb_include_module(rb_cIO, rb_mFConst);
4498
rb_file_const("LOCK_SH", INT2FIX(LOCK_SH));
4499
rb_file_const("LOCK_EX", INT2FIX(LOCK_EX));
4500
rb_file_const("LOCK_UN", INT2FIX(LOCK_UN));
4501
rb_file_const("LOCK_NB", INT2FIX(LOCK_NB));
4503
rb_define_method(rb_cFile, "path", rb_file_path, 0);
4504
rb_define_global_function("test", rb_f_test, -1);
4506
rb_cStat = rb_define_class_under(rb_cFile, "Stat", rb_cObject);
4507
rb_define_alloc_func(rb_cStat, rb_stat_s_alloc);
4508
rb_define_method(rb_cStat, "initialize", rb_stat_init, 1);
4509
rb_define_method(rb_cStat, "initialize_copy", rb_stat_init_copy, 1);
4511
rb_include_module(rb_cStat, rb_mComparable);
4513
rb_define_method(rb_cStat, "<=>", rb_stat_cmp, 1);
4515
rb_define_method(rb_cStat, "dev", rb_stat_dev, 0);
4516
rb_define_method(rb_cStat, "dev_major", rb_stat_dev_major, 0);
4517
rb_define_method(rb_cStat, "dev_minor", rb_stat_dev_minor, 0);
4518
rb_define_method(rb_cStat, "ino", rb_stat_ino, 0);
4519
rb_define_method(rb_cStat, "mode", rb_stat_mode, 0);
4520
rb_define_method(rb_cStat, "nlink", rb_stat_nlink, 0);
4521
rb_define_method(rb_cStat, "uid", rb_stat_uid, 0);
4522
rb_define_method(rb_cStat, "gid", rb_stat_gid, 0);
4523
rb_define_method(rb_cStat, "rdev", rb_stat_rdev, 0);
4524
rb_define_method(rb_cStat, "rdev_major", rb_stat_rdev_major, 0);
4525
rb_define_method(rb_cStat, "rdev_minor", rb_stat_rdev_minor, 0);
4526
rb_define_method(rb_cStat, "size", rb_stat_size, 0);
4527
rb_define_method(rb_cStat, "blksize", rb_stat_blksize, 0);
4528
rb_define_method(rb_cStat, "blocks", rb_stat_blocks, 0);
4529
rb_define_method(rb_cStat, "atime", rb_stat_atime, 0);
4530
rb_define_method(rb_cStat, "mtime", rb_stat_mtime, 0);
4531
rb_define_method(rb_cStat, "ctime", rb_stat_ctime, 0);
4533
rb_define_method(rb_cStat, "inspect", rb_stat_inspect, 0);
4535
rb_define_method(rb_cStat, "ftype", rb_stat_ftype, 0);
4537
rb_define_method(rb_cStat, "directory?", rb_stat_d, 0);
4538
rb_define_method(rb_cStat, "readable?", rb_stat_r, 0);
4539
rb_define_method(rb_cStat, "readable_real?", rb_stat_R, 0);
4540
rb_define_method(rb_cStat, "writable?", rb_stat_w, 0);
4541
rb_define_method(rb_cStat, "writable_real?", rb_stat_W, 0);
4542
rb_define_method(rb_cStat, "executable?", rb_stat_x, 0);
4543
rb_define_method(rb_cStat, "executable_real?", rb_stat_X, 0);
4544
rb_define_method(rb_cStat, "file?", rb_stat_f, 0);
4545
rb_define_method(rb_cStat, "zero?", rb_stat_z, 0);
4546
rb_define_method(rb_cStat, "size?", rb_stat_s, 0);
4547
rb_define_method(rb_cStat, "owned?", rb_stat_owned, 0);
4548
rb_define_method(rb_cStat, "grpowned?", rb_stat_grpowned, 0);
4550
rb_define_method(rb_cStat, "pipe?", rb_stat_p, 0);
4551
rb_define_method(rb_cStat, "symlink?", rb_stat_l, 0);
4552
rb_define_method(rb_cStat, "socket?", rb_stat_S, 0);
4554
rb_define_method(rb_cStat, "blockdev?", rb_stat_b, 0);
4555
rb_define_method(rb_cStat, "chardev?", rb_stat_c, 0);
4557
rb_define_method(rb_cStat, "setuid?", rb_stat_suid, 0);
4558
rb_define_method(rb_cStat, "setgid?", rb_stat_sgid, 0);
4559
rb_define_method(rb_cStat, "sticky?", rb_stat_sticky, 0);