-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
7881 lines (7136 loc) · 208 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
**********************************************************************/
#include "ruby/internal/config.h"
#ifdef _WIN32
# include "missing/file.h"
# include "ruby.h"
#endif
#include <ctype.h>
#include <time.h>
#ifdef __CYGWIN__
# include <windows.h>
# include <sys/cygwin.h>
# include <wchar.h>
#endif
#ifdef __APPLE__
# if !(defined(__has_feature) && defined(__has_attribute))
/* Maybe a bug in SDK of Xcode 10.2.1 */
/* In this condition, <os/availability.h> does not define
* API_AVAILABLE and similar, but __API_AVAILABLE and similar which
* are defined in <Availability.h> */
# define API_AVAILABLE(...)
# define API_DEPRECATED(...)
# endif
# include <CoreFoundation/CFString.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.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
#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
#ifdef HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_MKDEV_H
# include <sys/mkdev.h>
#endif
#if defined(HAVE_FCNTL_H)
# include <fcntl.h>
#endif
#if defined(HAVE_SYS_TIME_H)
# include <sys/time.h>
#endif
#if !defined HAVE_LSTAT && !defined lstat
# define lstat stat
#endif
/* define system APIs */
#ifdef _WIN32
# include "win32/file.h"
# define STAT(p, s) rb_w32_ustati128((p), (s))
# undef lstat
# define lstat(p, s) rb_w32_ulstati128((p), (s))
# undef access
# define access(p, m) rb_w32_uaccess((p), (m))
# undef truncate
# define truncate(p, n) rb_w32_utruncate((p), (n))
# undef chmod
# define chmod(p, m) rb_w32_uchmod((p), (m))
# undef chown
# define chown(p, o, g) rb_w32_uchown((p), (o), (g))
# undef lchown
# define lchown(p, o, g) rb_w32_ulchown((p), (o), (g))
# undef utimensat
# define utimensat(s, p, t, f) rb_w32_uutimensat((s), (p), (t), (f))
# undef link
# define link(f, t) rb_w32_ulink((f), (t))
# undef unlink
# define unlink(p) rb_w32_uunlink(p)
# undef readlink
# define readlink(f, t, l) rb_w32_ureadlink((f), (t), (l))
# undef rename
# define rename(f, t) rb_w32_urename((f), (t))
# undef symlink
# define symlink(s, l) rb_w32_usymlink((s), (l))
# ifdef HAVE_REALPATH
/* Don't use native realpath(3) on Windows, as the check for
absolute paths does not work for drive letters. */
# undef HAVE_REALPATH
# endif
#else
# define STAT(p, s) stat((p), (s))
#endif /* _WIN32 */
#if defined _WIN32 || defined __APPLE__
# define USE_OSPATH 1
# define TO_OSPATH(str) rb_str_encode_ospath(str)
#else
# define USE_OSPATH 0
# define TO_OSPATH(str) (str)
#endif
/* utime may fail if time is out-of-range for the FS [ruby-dev:38277] */
#if defined DOSISH || defined __CYGWIN__
# define UTIME_EINVAL
#endif
/* Solaris 10 realpath(3) doesn't support File.realpath */
#if defined HAVE_REALPATH && defined __sun && defined __SVR4
#undef HAVE_REALPATH
#endif
#ifdef HAVE_REALPATH
# include <limits.h>
# include <stdlib.h>
#endif
#include "dln.h"
#include "encindex.h"
#include "id.h"
#include "internal.h"
#include "internal/compilers.h"
#include "internal/dir.h"
#include "internal/error.h"
#include "internal/file.h"
#include "internal/io.h"
#include "internal/load.h"
#include "internal/object.h"
#include "internal/process.h"
#include "internal/thread.h"
#include "internal/vm.h"
#include "ruby/encoding.h"
#include "ruby/thread.h"
#include "ruby/util.h"
VALUE rb_cFile;
VALUE rb_mFileTest;
VALUE rb_cStat;
static VALUE
file_path_convert(VALUE name)
{
#ifndef _WIN32 /* non Windows == Unix */
int fname_encidx = ENCODING_GET(name);
int fs_encidx;
if (ENCINDEX_US_ASCII != fname_encidx &&
ENCINDEX_ASCII_8BIT != fname_encidx &&
(fs_encidx = rb_filesystem_encindex()) != fname_encidx &&
rb_default_internal_encoding() &&
!rb_enc_str_asciionly_p(name)) {
/* Don't call rb_filesystem_encoding() before US-ASCII and ASCII-8BIT */
/* fs_encoding should be ascii compatible */
rb_encoding *fname_encoding = rb_enc_from_index(fname_encidx);
rb_encoding *fs_encoding = rb_enc_from_index(fs_encidx);
name = rb_str_conv_enc(name, fname_encoding, fs_encoding);
}
#endif
return name;
}
static rb_encoding *
check_path_encoding(VALUE str)
{
rb_encoding *enc = rb_enc_get(str);
if (!rb_enc_asciicompat(enc)) {
rb_raise(rb_eEncCompatError, "path name must be ASCII-compatible (%s): %"PRIsVALUE,
rb_enc_name(enc), rb_str_inspect(str));
}
return enc;
}
VALUE
rb_get_path_check_to_string(VALUE obj)
{
VALUE tmp;
ID to_path;
if (RB_TYPE_P(obj, T_STRING)) {
return obj;
}
CONST_ID(to_path, "to_path");
tmp = rb_check_funcall_default(obj, to_path, 0, 0, obj);
StringValue(tmp);
return tmp;
}
VALUE
rb_get_path_check_convert(VALUE obj)
{
obj = file_path_convert(obj);
check_path_encoding(obj);
if (!rb_str_to_cstr(obj)) {
rb_raise(rb_eArgError, "path name contains null byte");
}
return rb_str_new4(obj);
}
VALUE
rb_get_path_no_checksafe(VALUE obj)
{
return rb_get_path(obj);
}
VALUE
rb_get_path(VALUE obj)
{
return rb_get_path_check_convert(rb_get_path_check_to_string(obj));
}
VALUE
rb_str_encode_ospath(VALUE path)
{
#if USE_OSPATH
int encidx = ENCODING_GET(path);
#if 0 && defined _WIN32
if (encidx == ENCINDEX_ASCII_8BIT) {
encidx = rb_filesystem_encindex();
}
#endif
if (encidx != ENCINDEX_ASCII_8BIT && encidx != ENCINDEX_UTF_8) {
rb_encoding *enc = rb_enc_from_index(encidx);
rb_encoding *utf8 = rb_utf8_encoding();
path = rb_str_conv_enc(path, enc, utf8);
}
#endif /* USE_OSPATH */
return path;
}
#ifdef __APPLE__
# define NORMALIZE_UTF8PATH 1
# ifdef HAVE_WORKING_FORK
static void
rb_CFString_class_initialize_before_fork(void)
{
/*
* Since macOS 13, CFString family API used in
* rb_str_append_normalized_ospath may internally use Objective-C classes
* (NSTaggedPointerString and NSPlaceholderMutableString) for small strings.
*
* On the other hand, Objective-C classes should not be used for the first
* time in a fork()'ed but not exec()'ed process. Violations for this rule
* can result deadlock during class initialization, so Objective-C runtime
* conservatively crashes on such cases by default.
*
* Therefore, we need to use CFString API to initialize Objective-C classes
* used internally *before* fork().
*
* For future changes, please note that this initialization process cannot
* be done in ctor because NSTaggedPointerString in CoreFoundation is enabled
* after CFStringInitializeTaggedStrings(), which is called during loading
* Objective-C runtime after ctor.
* For more details, see https://bugs.ruby-lang.org/issues/18912
*/
/* Enough small but non-empty ASCII string to fit in NSTaggedPointerString. */
const char small_str[] = "/";
long len = sizeof(small_str) - 1;
const CFAllocatorRef alloc = kCFAllocatorDefault;
CFStringRef s = CFStringCreateWithBytesNoCopy(alloc,
(const UInt8 *)small_str,
len, kCFStringEncodingUTF8,
FALSE, kCFAllocatorNull);
CFMutableStringRef m = CFStringCreateMutableCopy(alloc, len, s);
CFRelease(m);
CFRelease(s);
}
# endif /* HAVE_WORKING_FORK */
static VALUE
rb_str_append_normalized_ospath(VALUE str, const char *ptr, long len)
{
CFIndex buflen = 0;
CFRange all;
CFStringRef s = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault,
(const UInt8 *)ptr, len,
kCFStringEncodingUTF8, FALSE,
kCFAllocatorNull);
CFMutableStringRef m = CFStringCreateMutableCopy(kCFAllocatorDefault, len, s);
long oldlen = RSTRING_LEN(str);
CFStringNormalize(m, kCFStringNormalizationFormC);
all = CFRangeMake(0, CFStringGetLength(m));
CFStringGetBytes(m, all, kCFStringEncodingUTF8, '?', FALSE, NULL, 0, &buflen);
rb_str_modify_expand(str, buflen);
CFStringGetBytes(m, all, kCFStringEncodingUTF8, '?', FALSE,
(UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
rb_str_set_len(str, oldlen + buflen);
CFRelease(m);
CFRelease(s);
return str;
}
VALUE
rb_str_normalize_ospath(const char *ptr, long len)
{
const char *p = ptr;
const char *e = ptr + len;
const char *p1 = p;
VALUE str = rb_str_buf_new(len);
rb_encoding *enc = rb_utf8_encoding();
rb_enc_associate(str, enc);
while (p < e) {
int l, c;
int r = rb_enc_precise_mbclen(p, e, enc);
if (!MBCLEN_CHARFOUND_P(r)) {
/* invalid byte shall not happen but */
static const char invalid[3] = "\xEF\xBF\xBD";
rb_str_append_normalized_ospath(str, p1, p-p1);
rb_str_cat(str, invalid, sizeof(invalid));
p += 1;
p1 = p;
continue;
}
l = MBCLEN_CHARFOUND_LEN(r);
c = rb_enc_mbc_to_codepoint(p, e, enc);
if ((0x2000 <= c && c <= 0x2FFF) || (0xF900 <= c && c <= 0xFAFF) ||
(0x2F800 <= c && c <= 0x2FAFF)) {
if (p - p1 > 0) {
rb_str_append_normalized_ospath(str, p1, p-p1);
}
rb_str_cat(str, p, l);
p += l;
p1 = p;
}
else {
p += l;
}
}
if (p - p1 > 0) {
rb_str_append_normalized_ospath(str, p1, p-p1);
}
return str;
}
static int
ignored_char_p(const char *p, const char *e, rb_encoding *enc)
{
unsigned char c;
if (p+3 > e) return 0;
switch ((unsigned char)*p) {
case 0xe2:
switch ((unsigned char)p[1]) {
case 0x80:
c = (unsigned char)p[2];
/* c >= 0x200c && c <= 0x200f */
if (c >= 0x8c && c <= 0x8f) return 3;
/* c >= 0x202a && c <= 0x202e */
if (c >= 0xaa && c <= 0xae) return 3;
return 0;
case 0x81:
c = (unsigned char)p[2];
/* c >= 0x206a && c <= 0x206f */
if (c >= 0xaa && c <= 0xaf) return 3;
return 0;
}
break;
case 0xef:
/* c == 0xfeff */
if ((unsigned char)p[1] == 0xbb &&
(unsigned char)p[2] == 0xbf)
return 3;
break;
}
return 0;
}
#else /* !__APPLE__ */
# define NORMALIZE_UTF8PATH 0
#endif /* __APPLE__ */
#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
struct apply_filename {
const char *ptr;
VALUE path;
};
struct apply_arg {
int i;
int argc;
int errnum;
int (*func)(const char *, void *);
void *arg;
struct apply_filename fn[FLEX_ARY_LEN];
};
static void *
no_gvl_apply2files(void *ptr)
{
struct apply_arg *aa = ptr;
for (aa->i = 0; aa->i < aa->argc; aa->i++) {
if (aa->func(aa->fn[aa->i].ptr, aa->arg) < 0) {
aa->errnum = errno;
break;
}
}
return 0;
}
#ifdef UTIME_EINVAL
NORETURN(static void utime_failed(struct apply_arg *));
static int utime_internal(const char *, void *);
#endif
static VALUE
apply2files(int (*func)(const char *, void *), int argc, VALUE *argv, void *arg)
{
VALUE v;
const size_t size = sizeof(struct apply_filename);
const long len = (long)(offsetof(struct apply_arg, fn) + (size * argc));
struct apply_arg *aa = ALLOCV(v, len);
aa->errnum = 0;
aa->argc = argc;
aa->arg = arg;
aa->func = func;
for (aa->i = 0; aa->i < argc; aa->i++) {
VALUE path = rb_get_path(argv[aa->i]);
path = rb_str_encode_ospath(path);
aa->fn[aa->i].ptr = RSTRING_PTR(path);
aa->fn[aa->i].path = path;
}
rb_thread_call_without_gvl(no_gvl_apply2files, aa, RUBY_UBF_IO, 0);
if (aa->errnum) {
#ifdef UTIME_EINVAL
if (func == utime_internal) {
utime_failed(aa);
}
#endif
rb_syserr_fail_path(aa->errnum, aa->fn[aa->i].path);
}
if (v) {
ALLOCV_END(v);
}
return LONG2FIX(argc);
}
static const rb_data_type_t stat_data_type = {
"stat",
{
NULL,
RUBY_TYPED_DEFAULT_FREE,
NULL, // No external memory to report
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
};
struct rb_stat {
struct stat stat;
bool initialized;
};
static VALUE
stat_new_0(VALUE klass, const struct stat *st)
{
struct rb_stat *rb_st;
VALUE obj = TypedData_Make_Struct(klass, struct rb_stat, &stat_data_type, rb_st);
if (st) {
rb_st->stat = *st;
rb_st->initialized = true;
}
return obj;
}
VALUE
rb_stat_new(const struct stat *st)
{
return stat_new_0(rb_cStat, st);
}
static struct stat*
get_stat(VALUE self)
{
struct rb_stat* rb_st;
TypedData_Get_Struct(self, struct rb_stat, &stat_data_type, rb_st);
if (!rb_st->initialized) rb_raise(rb_eTypeError, "uninitialized File::Stat");
return &rb_st->stat;
}
static struct timespec stat_mtimespec(const struct stat *st);
/*
* call-seq:
* stat <=> other_stat -> -1, 0, 1, nil
*
* Compares File::Stat objects by comparing their respective modification
* times.
*
* +nil+ is returned if +other_stat+ is not a File::Stat object
*
* 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)))
#ifndef NUM2DEVT
# define NUM2DEVT(v) NUM2UINT(v)
#endif
#ifndef DEVT2NUM
# define DEVT2NUM(v) UINT2NUM(v)
#endif
#ifndef PRI_DEVT_PREFIX
# define PRI_DEVT_PREFIX ""
#endif
/*
* call-seq:
* stat.dev -> integer
*
* Returns an integer representing the device on which <i>stat</i>
* resides.
*
* File.stat("testfile").dev #=> 774
*/
static VALUE
rb_stat_dev(VALUE self)
{
#if SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
return DEVT2NUM(get_stat(self)->st_dev);
#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
return ULONG2NUM(get_stat(self)->st_dev);
#else
return ULL2NUM(get_stat(self)->st_dev);
#endif
}
/*
* call-seq:
* stat.dev_major -> integer
*
* 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)
return UINT2NUM(major(get_stat(self)->st_dev));
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.dev_minor -> integer
*
* 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)
return UINT2NUM(minor(get_stat(self)->st_dev));
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.ino -> integer
*
* Returns the inode number for <i>stat</i>.
*
* File.stat("testfile").ino #=> 1083669
*
*/
static VALUE
rb_stat_ino(VALUE self)
{
#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
/* assume INTEGER_PACK_LSWORD_FIRST and st_inohigh is just next of st_ino */
return rb_integer_unpack(&get_stat(self)->st_ino, 2,
SIZEOF_STRUCT_STAT_ST_INO, 0,
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER|
INTEGER_PACK_2COMP);
#elif SIZEOF_STRUCT_STAT_ST_INO > SIZEOF_LONG
return ULL2NUM(get_stat(self)->st_ino);
#else
return ULONG2NUM(get_stat(self)->st_ino);
#endif
}
/*
* call-seq:
* stat.mode -> integer
*
* 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 -> integer
*
* 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)
{
/* struct stat::st_nlink is nlink_t in POSIX. Not the case for Windows. */
const struct stat *ptr = get_stat(self);
if (sizeof(ptr->st_nlink) <= sizeof(int)) {
return UINT2NUM((unsigned)ptr->st_nlink);
}
else if (sizeof(ptr->st_nlink) == sizeof(long)) {
return ULONG2NUM((unsigned long)ptr->st_nlink);
}
else if (sizeof(ptr->st_nlink) == sizeof(LONG_LONG)) {
return ULL2NUM((unsigned LONG_LONG)ptr->st_nlink);
}
else {
rb_bug(":FIXME: don't know what to do");
}
}
/*
* call-seq:
* stat.uid -> integer
*
* 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 -> integer
*
* 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 -> integer 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_STRUCT_STAT_ST_RDEV
# if SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
return DEVT2NUM(get_stat(self)->st_rdev);
# elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
return ULONG2NUM(get_stat(self)->st_rdev);
# else
return ULL2NUM(get_stat(self)->st_rdev);
# endif
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.rdev_major -> integer
*
* 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_STRUCT_STAT_ST_RDEV) && defined(major)
return UINT2NUM(major(get_stat(self)->st_rdev));
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.rdev_minor -> integer
*
* 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_STRUCT_STAT_ST_RDEV) && defined(minor)
return UINT2NUM(minor(get_stat(self)->st_rdev));
#else
return Qnil;
#endif
}
/*
* call-seq:
* stat.size -> integer
*
* 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_STRUCT_STAT_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_STRUCT_STAT_ST_BLOCKS
# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
return ULL2NUM(get_stat(self)->st_blocks);
# else
return ULONG2NUM(get_stat(self)->st_blocks);
# endif
#else
return Qnil;
#endif
}
static struct timespec
stat_atimespec(const 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 = (long)st->st_atimensec;
#else
ts.tv_nsec = 0;
#endif
return ts;
}
static VALUE
stat_time(const struct timespec ts)
{
return rb_time_nano_new(ts.tv_sec, ts.tv_nsec);
}
static VALUE
stat_atime(const struct stat *st)
{
return stat_time(stat_atimespec(st));
}
static struct timespec
stat_mtimespec(const 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 = (long)st->st_mtimensec;
#else
ts.tv_nsec = 0;
#endif
return ts;
}
static VALUE
stat_mtime(const struct stat *st)
{
return stat_time(stat_mtimespec(st));
}
static struct timespec
stat_ctimespec(const 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 = (long)st->st_ctimensec;
#else
ts.tv_nsec = 0;
#endif
return ts;
}
static VALUE
stat_ctime(const struct stat *st)
{
return stat_time(stat_ctimespec(st));
}
#define HAVE_STAT_BIRTHTIME
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
typedef struct stat statx_data;
static VALUE
stat_birthtime(const struct stat *st)
{
const struct timespec *ts = &st->st_birthtimespec;
return rb_time_nano_new(ts->tv_sec, ts->tv_nsec);
}
#elif defined(_WIN32)
typedef struct stat statx_data;
# define stat_birthtime stat_ctime
#else
# undef HAVE_STAT_BIRTHTIME
#endif /* defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) */
/*
* call-seq:
* stat.atime -> time
*
* Returns the last access time for this file as an object of class
* Time.
*
* 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 -> time
*
* 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));
}