-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
4819 lines (4254 loc) · 107 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
file.c -
$Author$
created at: Mon Nov 15 12:24:34 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#ifdef _WIN32
#include "missing/file.h"
#endif
#ifdef __CYGWIN__
#include <windows.h>
#include <sys/cygwin.h>
#endif
#include "ruby/ruby.h"
#include "ruby/io.h"
#include "ruby/util.h"
#include "dln.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#else
int flock(int, int);
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifndef MAXPATHLEN
# define MAXPATHLEN 1024
#endif
#include <ctype.h>
#include <time.h>
#ifdef HAVE_UTIME_H
#include <utime.h>
#elif defined HAVE_SYS_UTIME_H
#include <sys/utime.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_MKDEV_H
#include <sys/mkdev.h>
#endif
#if !defined HAVE_LSTAT && !defined lstat
#define lstat stat
#endif
#ifdef __BEOS__ /* should not change ID if -1 */
static int
be_chown(const char *path, uid_t owner, gid_t group)
{
if (owner == -1 || group == -1) {
struct stat st;
if (stat(path, &st) < 0) return -1;
if (owner == -1) owner = st.st_uid;
if (group == -1) group = st.st_gid;
}
return chown(path, owner, group);
}
#define chown be_chown
static int
be_fchown(int fd, uid_t owner, gid_t group)
{
if (owner == -1 || group == -1) {
struct stat st;
if (fstat(fd, &st) < 0) return -1;
if (owner == -1) owner = st.st_uid;
if (group == -1) group = st.st_gid;
}
return fchown(fd, owner, group);
}
#define fchown be_fchown
#endif /* __BEOS__ */
VALUE rb_cFile;
VALUE rb_mFileTest;
VALUE rb_cStat;
static VALUE
rb_get_path_check(VALUE obj, int check)
{
VALUE tmp;
ID to_path;
if (check) rb_check_safe_obj(obj);
tmp = rb_check_string_type(obj);
if (!NIL_P(tmp)) goto exit;
CONST_ID(to_path, "to_path");
if (rb_respond_to(obj, to_path)) {
tmp = rb_funcall(obj, to_path, 0, 0);
}
else {
tmp = obj;
}
exit:
StringValueCStr(tmp);
if (check && obj != tmp) {
rb_check_safe_obj(tmp);
}
return rb_str_new4(tmp);
}
VALUE
rb_get_path_no_checksafe(VALUE obj)
{
return rb_get_path_check(obj, 0);
}
VALUE
rb_get_path(VALUE obj)
{
return rb_get_path_check(obj, 1);
}
static long
apply2files(void (*func)(const char *, void *), VALUE vargs, void *arg)
{
long i;
volatile VALUE path;
rb_secure(4);
for (i=0; i<RARRAY_LEN(vargs); i++) {
path = rb_get_path(RARRAY_PTR(vargs)[i]);
(*func)(StringValueCStr(path), arg);
}
return RARRAY_LEN(vargs);
}
/*
* call-seq:
* file.path -> filename
*
* Returns the pathname used to create <i>file</i> as a string. Does
* not normalize the name.
*
* File.new("testfile").path #=> "testfile"
* File.new("/tmp/../tmp/xxx", "w").path #=> "/tmp/../tmp/xxx"
*
*/
static VALUE
rb_file_path(VALUE obj)
{
rb_io_t *fptr;
fptr = RFILE(rb_io_taint_check(obj))->fptr;
rb_io_check_initialized(fptr);
if (NIL_P(fptr->pathv)) return Qnil;
return rb_obj_taint(rb_str_dup(fptr->pathv));
}
static VALUE
stat_new_0(VALUE klass, struct stat *st)
{
struct stat *nst = 0;
if (st) {
nst = ALLOC(struct stat);
*nst = *st;
}
return Data_Wrap_Struct(klass, NULL, -1, nst);
}
static VALUE
stat_new(struct stat *st)
{
return stat_new_0(rb_cStat, st);
}
static struct stat*
get_stat(VALUE self)
{
struct stat* st;
Data_Get_Struct(self, struct stat, st);
if (!st) rb_raise(rb_eTypeError, "uninitialized File::Stat");
return st;
}
static struct timespec stat_mtimespec(struct stat *st);
/*
* call-seq:
* stat <=> other_stat => -1, 0, 1
*
* Compares <code>File::Stat</code> objects by comparing their
* respective modification times.
*
* f1 = File.new("f1", "w")
* sleep 1
* f2 = File.new("f2", "w")
* f1.stat <=> f2.stat #=> -1
*/
static VALUE
rb_stat_cmp(VALUE self, VALUE other)
{
if (rb_obj_is_kind_of(other, rb_obj_class(self))) {
struct timespec ts1 = stat_mtimespec(get_stat(self));
struct timespec ts2 = stat_mtimespec(get_stat(other));
if (ts1.tv_sec == ts2.tv_sec) {
if (ts1.tv_nsec == ts2.tv_nsec) return INT2FIX(0);
if (ts1.tv_nsec < ts2.tv_nsec) return INT2FIX(-1);
return INT2FIX(1);
}
if (ts1.tv_sec < ts2.tv_sec) return INT2FIX(-1);
return INT2FIX(1);
}
return Qnil;
}
#define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
/*
* call-seq:
* stat.dev => fixnum
*
* Returns an integer representing the device on which <i>stat</i>
* resides.
*
* File.stat("testfile").dev #=> 774
*/
static VALUE
rb_stat_dev(VALUE self)
{
return INT2NUM(get_stat(self)->st_dev);
}
/*
* call-seq:
* stat.dev_major => fixnum
*
* Returns the major part of <code>File_Stat#dev</code> or
* <code>nil</code>.
*
* File.stat("/dev/fd1").dev_major #=> 2
* File.stat("/dev/tty").dev_major #=> 5
*/
static VALUE
rb_stat_dev_major(VALUE self)
{
#if defined(major)
long dev = get_stat(self)->st_dev;
return ULONG2NUM(major(dev));
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.dev_minor => fixnum
*
* Returns the minor part of <code>File_Stat#dev</code> or
* <code>nil</code>.
*
* File.stat("/dev/fd1").dev_minor #=> 1
* File.stat("/dev/tty").dev_minor #=> 0
*/
static VALUE
rb_stat_dev_minor(VALUE self)
{
#if defined(minor)
long dev = get_stat(self)->st_dev;
return ULONG2NUM(minor(dev));
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.ino => fixnum
*
* Returns the inode number for <i>stat</i>.
*
* File.stat("testfile").ino #=> 1083669
*
*/
static VALUE
rb_stat_ino(VALUE self)
{
#ifdef HUGE_ST_INO
return ULL2NUM(get_stat(self)->st_ino);
#else
return ULONG2NUM(get_stat(self)->st_ino);
#endif
}
/*
* call-seq:
* stat.mode => fixnum
*
* Returns an integer representing the permission bits of
* <i>stat</i>. The meaning of the bits is platform dependent; on
* Unix systems, see <code>stat(2)</code>.
*
* File.chmod(0644, "testfile") #=> 1
* s = File.stat("testfile")
* sprintf("%o", s.mode) #=> "100644"
*/
static VALUE
rb_stat_mode(VALUE self)
{
return UINT2NUM(ST2UINT(get_stat(self)->st_mode));
}
/*
* call-seq:
* stat.nlink => fixnum
*
* Returns the number of hard links to <i>stat</i>.
*
* File.stat("testfile").nlink #=> 1
* File.link("testfile", "testfile.bak") #=> 0
* File.stat("testfile").nlink #=> 2
*
*/
static VALUE
rb_stat_nlink(VALUE self)
{
return UINT2NUM(get_stat(self)->st_nlink);
}
/*
* call-seq:
* stat.uid => fixnum
*
* Returns the numeric user id of the owner of <i>stat</i>.
*
* File.stat("testfile").uid #=> 501
*
*/
static VALUE
rb_stat_uid(VALUE self)
{
return UIDT2NUM(get_stat(self)->st_uid);
}
/*
* call-seq:
* stat.gid => fixnum
*
* Returns the numeric group id of the owner of <i>stat</i>.
*
* File.stat("testfile").gid #=> 500
*
*/
static VALUE
rb_stat_gid(VALUE self)
{
return GIDT2NUM(get_stat(self)->st_gid);
}
/*
* call-seq:
* stat.rdev => fixnum or nil
*
* Returns an integer representing the device type on which
* <i>stat</i> resides. Returns <code>nil</code> if the operating
* system doesn't support this feature.
*
* File.stat("/dev/fd1").rdev #=> 513
* File.stat("/dev/tty").rdev #=> 1280
*/
static VALUE
rb_stat_rdev(VALUE self)
{
#ifdef HAVE_ST_RDEV
return ULONG2NUM(get_stat(self)->st_rdev);
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.rdev_major => fixnum
*
* Returns the major part of <code>File_Stat#rdev</code> or
* <code>nil</code>.
*
* File.stat("/dev/fd1").rdev_major #=> 2
* File.stat("/dev/tty").rdev_major #=> 5
*/
static VALUE
rb_stat_rdev_major(VALUE self)
{
#if defined(HAVE_ST_RDEV) && defined(major)
long rdev = get_stat(self)->st_rdev;
return ULONG2NUM(major(rdev));
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.rdev_minor => fixnum
*
* Returns the minor part of <code>File_Stat#rdev</code> or
* <code>nil</code>.
*
* File.stat("/dev/fd1").rdev_minor #=> 1
* File.stat("/dev/tty").rdev_minor #=> 0
*/
static VALUE
rb_stat_rdev_minor(VALUE self)
{
#if defined(HAVE_ST_RDEV) && defined(minor)
long rdev = get_stat(self)->st_rdev;
return ULONG2NUM(minor(rdev));
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.size => fixnum
*
* Returns the size of <i>stat</i> in bytes.
*
* File.stat("testfile").size #=> 66
*/
static VALUE
rb_stat_size(VALUE self)
{
return OFFT2NUM(get_stat(self)->st_size);
}
/*
* call-seq:
* stat.blksize => integer or nil
*
* Returns the native file system's block size. Will return <code>nil</code>
* on platforms that don't support this information.
*
* File.stat("testfile").blksize #=> 4096
*
*/
static VALUE
rb_stat_blksize(VALUE self)
{
#ifdef HAVE_ST_BLKSIZE
return ULONG2NUM(get_stat(self)->st_blksize);
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.blocks => integer or nil
*
* Returns the number of native file system blocks allocated for this
* file, or <code>nil</code> if the operating system doesn't
* support this feature.
*
* File.stat("testfile").blocks #=> 2
*/
static VALUE
rb_stat_blocks(VALUE self)
{
#ifdef HAVE_ST_BLOCKS
return ULONG2NUM(get_stat(self)->st_blocks);
#else
return Qnil;
#endif
}
static struct timespec
stat_atimespec(struct stat *st)
{
struct timespec ts;
ts.tv_sec = st->st_atime;
#if defined(HAVE_STRUCT_STAT_ST_ATIM)
ts.tv_nsec = st->st_atim.tv_nsec;
#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
ts.tv_nsec = st->st_atimespec.tv_nsec;
#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
ts.tv_nsec = st->st_atimensec;
#else
ts.tv_nsec = 0;
#endif
return ts;
}
static VALUE
stat_atime(struct stat *st)
{
struct timespec ts = stat_atimespec(st);
return rb_time_nano_new(ts.tv_sec, ts.tv_nsec);
}
static struct timespec
stat_mtimespec(struct stat *st)
{
struct timespec ts;
ts.tv_sec = st->st_mtime;
#if defined(HAVE_STRUCT_STAT_ST_MTIM)
ts.tv_nsec = st->st_mtim.tv_nsec;
#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
ts.tv_nsec = st->st_mtimespec.tv_nsec;
#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
ts.tv_nsec = st->st_mtimensec;
#else
ts.tv_nsec = 0;
#endif
return ts;
}
static VALUE
stat_mtime(struct stat *st)
{
struct timespec ts = stat_mtimespec(st);
return rb_time_nano_new(ts.tv_sec, ts.tv_nsec);
}
static struct timespec
stat_ctimespec(struct stat *st)
{
struct timespec ts;
ts.tv_sec = st->st_ctime;
#if defined(HAVE_STRUCT_STAT_ST_CTIM)
ts.tv_nsec = st->st_ctim.tv_nsec;
#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
ts.tv_nsec = st->st_ctimespec.tv_nsec;
#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
ts.tv_nsec = st->st_ctimensec;
#else
ts.tv_nsec = 0;
#endif
return ts;
}
static VALUE
stat_ctime(struct stat *st)
{
struct timespec ts = stat_ctimespec(st);
return rb_time_nano_new(ts.tv_sec, ts.tv_nsec);
}
/*
* call-seq:
* stat.atime => time
*
* Returns the last access time for this file as an object of class
* <code>Time</code>.
*
* File.stat("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
*
*/
static VALUE
rb_stat_atime(VALUE self)
{
return stat_atime(get_stat(self));
}
/*
* call-seq:
* stat.mtime -> aTime
*
* Returns the modification time of <i>stat</i>.
*
* File.stat("testfile").mtime #=> Wed Apr 09 08:53:14 CDT 2003
*
*/
static VALUE
rb_stat_mtime(VALUE self)
{
return stat_mtime(get_stat(self));
}
/*
* call-seq:
* stat.ctime -> aTime
*
* Returns the change time for <i>stat</i> (that is, the time
* directory information about the file was changed, not the file
* itself).
*
* File.stat("testfile").ctime #=> Wed Apr 09 08:53:14 CDT 2003
*
*/
static VALUE
rb_stat_ctime(VALUE self)
{
return stat_ctime(get_stat(self));
}
/*
* call-seq:
* stat.inspect => string
*
* Produce a nicely formatted description of <i>stat</i>.
*
* File.stat("/etc/passwd").inspect
* #=> "#<File::Stat dev=0xe000005, ino=1078078, mode=0100644,
* # nlink=1, uid=0, gid=0, rdev=0x0, size=1374, blksize=4096,
* # blocks=8, atime=Wed Dec 10 10:16:12 CST 2003,
* # mtime=Fri Sep 12 15:41:41 CDT 2003,
* # ctime=Mon Oct 27 11:20:27 CST 2003>"
*/
static VALUE
rb_stat_inspect(VALUE self)
{
VALUE str;
int i;
static const struct {
const char *name;
VALUE (*func)(VALUE);
} member[] = {
{"dev", rb_stat_dev},
{"ino", rb_stat_ino},
{"mode", rb_stat_mode},
{"nlink", rb_stat_nlink},
{"uid", rb_stat_uid},
{"gid", rb_stat_gid},
{"rdev", rb_stat_rdev},
{"size", rb_stat_size},
{"blksize", rb_stat_blksize},
{"blocks", rb_stat_blocks},
{"atime", rb_stat_atime},
{"mtime", rb_stat_mtime},
{"ctime", rb_stat_ctime},
};
struct stat* st;
Data_Get_Struct(self, struct stat, st);
if (!st) {
return rb_sprintf("#<%s: uninitialized>", rb_obj_classname(self));
}
str = rb_str_buf_new2("#<");
rb_str_buf_cat2(str, rb_obj_classname(self));
rb_str_buf_cat2(str, " ");
for (i = 0; i < sizeof(member)/sizeof(member[0]); i++) {
VALUE v;
if (i > 0) {
rb_str_buf_cat2(str, ", ");
}
rb_str_buf_cat2(str, member[i].name);
rb_str_buf_cat2(str, "=");
v = (*member[i].func)(self);
if (i == 2) { /* mode */
rb_str_catf(str, "0%lo", NUM2ULONG(v));
}
else if (i == 0 || i == 6) { /* dev/rdev */
rb_str_catf(str, "0x%lx", NUM2ULONG(v));
}
else {
rb_str_append(str, rb_inspect(v));
}
}
rb_str_buf_cat2(str, ">");
OBJ_INFECT(str, self);
return str;
}
static int
rb_stat(VALUE file, struct stat *st)
{
VALUE tmp;
rb_secure(2);
tmp = rb_check_convert_type(file, T_FILE, "IO", "to_io");
if (!NIL_P(tmp)) {
rb_io_t *fptr;
GetOpenFile(tmp, fptr);
return fstat(fptr->fd, st);
}
FilePathValue(file);
return stat(StringValueCStr(file), st);
}
#ifdef _WIN32
static HANDLE
w32_io_info(VALUE *file, BY_HANDLE_FILE_INFORMATION *st)
{
VALUE tmp;
HANDLE f, ret = 0;
tmp = rb_check_convert_type(*file, T_FILE, "IO", "to_io");
if (!NIL_P(tmp)) {
rb_io_t *fptr;
GetOpenFile(tmp, fptr);
f = (HANDLE)rb_w32_get_osfhandle(fptr->fd);
if (f == (HANDLE)-1) return INVALID_HANDLE_VALUE;
}
else {
FilePathValue(*file);
f = CreateFile(StringValueCStr(*file), 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
rb_w32_iswin95() ? 0 : FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (f == INVALID_HANDLE_VALUE) return f;
ret = f;
}
if (GetFileType(f) == FILE_TYPE_DISK) {
ZeroMemory(st, sizeof(*st));
if (GetFileInformationByHandle(f, st)) return ret;
}
if (ret) CloseHandle(ret);
return INVALID_HANDLE_VALUE;
}
#endif
/*
* call-seq:
* File.stat(file_name) => stat
*
* Returns a <code>File::Stat</code> object for the named file (see
* <code>File::Stat</code>).
*
* File.stat("testfile").mtime #=> Tue Apr 08 12:58:04 CDT 2003
*
*/
static VALUE
rb_file_s_stat(VALUE klass, VALUE fname)
{
struct stat st;
rb_secure(4);
FilePathValue(fname);
if (rb_stat(fname, &st) < 0) {
FilePathValue(fname);
rb_sys_fail(RSTRING_PTR(fname));
}
return stat_new(&st);
}
/*
* call-seq:
* ios.stat => stat
*
* Returns status information for <em>ios</em> as an object of type
* <code>File::Stat</code>.
*
* f = File.new("testfile")
* s = f.stat
* "%o" % s.mode #=> "100644"
* s.blksize #=> 4096
* s.atime #=> Wed Apr 09 08:53:54 CDT 2003
*
*/
static VALUE
rb_io_stat(VALUE obj)
{
rb_io_t *fptr;
struct stat st;
#define rb_sys_fail_path(path) rb_sys_fail(NIL_P(path) ? 0 : RSTRING_PTR(path))
GetOpenFile(obj, fptr);
if (fstat(fptr->fd, &st) == -1) {
rb_sys_fail_path(fptr->pathv);
}
return stat_new(&st);
}
/*
* call-seq:
* File.lstat(file_name) => stat
*
* Same as <code>File::stat</code>, but does not follow the last symbolic
* link. Instead, reports on the link itself.
*
* File.symlink("testfile", "link2test") #=> 0
* File.stat("testfile").size #=> 66
* File.lstat("link2test").size #=> 8
* File.stat("link2test").size #=> 66
*
*/
static VALUE
rb_file_s_lstat(VALUE klass, VALUE fname)
{
#ifdef HAVE_LSTAT
struct stat st;
rb_secure(2);
FilePathValue(fname);
if (lstat(StringValueCStr(fname), &st) == -1) {
rb_sys_fail(RSTRING_PTR(fname));
}
return stat_new(&st);
#else
return rb_file_s_stat(klass, fname);
#endif
}
/*
* call-seq:
* file.lstat => stat
*
* Same as <code>IO#stat</code>, but does not follow the last symbolic
* link. Instead, reports on the link itself.
*
* File.symlink("testfile", "link2test") #=> 0
* File.stat("testfile").size #=> 66
* f = File.new("link2test")
* f.lstat.size #=> 8
* f.stat.size #=> 66
*/
static VALUE
rb_file_lstat(VALUE obj)
{
#ifdef HAVE_LSTAT
rb_io_t *fptr;
struct stat st;
rb_secure(2);
GetOpenFile(obj, fptr);
if (NIL_P(fptr->pathv)) return Qnil;
if (lstat(RSTRING_PTR(fptr->pathv), &st) == -1) {
rb_sys_fail_path(fptr->pathv);
}
return stat_new(&st);
#else
return rb_io_stat(obj);
#endif
}
#ifndef HAVE_GROUP_MEMBER
static int
group_member(GETGROUPS_T gid)
{
#ifndef _WIN32
if (getgid() == gid || getegid() == gid)
return Qtrue;
# ifdef HAVE_GETGROUPS
# ifndef NGROUPS
# ifdef NGROUPS_MAX
# define NGROUPS NGROUPS_MAX
# else
# define NGROUPS 32
# endif
# endif
{
GETGROUPS_T gary[NGROUPS];
int anum;
anum = getgroups(NGROUPS, gary);
while (--anum >= 0)
if (gary[anum] == gid)
return Qtrue;
}
# endif
#endif
return Qfalse;
}
#endif
#ifndef S_IXUGO
# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
#endif
#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
#define USE_GETEUID 1
#endif
#ifndef HAVE_EACCESS
int
eaccess(const char *path, int mode)
{
#ifdef USE_GETEUID
struct stat st;
rb_uid_t euid;
if (stat(path, &st) < 0) return -1;
euid = geteuid();
if (euid == 0) {
/* Root can read or write any file. */
if (!(mode & X_OK))
return 0;
/* Root can execute any file that has any one of the execute
bits set. */
if (st.st_mode & S_IXUGO)
return 0;
return -1;
}
if (st.st_uid == euid) /* owner */
mode <<= 6;
else if (group_member(st.st_gid))
mode <<= 3;
if ((st.st_mode & mode) == mode) return 0;
return -1;
#else
# if defined(_MSC_VER) || defined(__MINGW32__)
mode &= ~1;
# endif
return access(path, mode);
#endif
}
#endif
/*
* Document-class: FileTest
*
* <code>FileTest</code> implements file test operations similar to
* those used in <code>File::Stat</code>. It exists as a standalone
* module, and its methods are also insinuated into the <code>File</code>
* class. (Note that this is not done by inclusion: the interpreter cheats).
*
*/
/*
* File.directory?(file_name) => true or false
* File.directory?(file_name) => true or false
*
* Returns <code>true</code> if the named file is a directory,
* <code>false</code> otherwise.
*
* File.directory?(".")
*/
/*
* Document-method: exist?
*
* call-seq:
* Dir.exist?(file_name) => true or false
* Dir.exists?(file_name) => true or false
*
* Returns <code>true</code> if the named file is a directory,
* <code>false</code> otherwise.
*
*/
/*
* Document-method: directory?
*
* call-seq:
* File.directory?(file_name) => true or false
*
* Returns <code>true</code> if the named file is a directory,
* <code>false</code> otherwise.
*
* File.directory?(".")