forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
6215 lines (5568 loc) · 146 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>
#include <wchar.h>
#endif
#ifdef __APPLE__
#include <CoreFoundation/CFString.h>
#endif
#include "id.h"
#include "internal.h"
#include "ruby/io.h"
#include "ruby/util.h"
#include "dln.h"
#include "encindex.h"
#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
#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
#ifdef HAVE_SYS_SYSMACROS_H
#include <sys/sysmacros.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#if defined(__native_client__)
# if defined(NACL_NEWLIB)
# include "nacl/utime.h"
# include "nacl/stat.h"
# include "nacl/unistd.h"
# else
# undef HAVE_UTIMENSAT
# endif
#endif
#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_ustati64((p), (s))
#undef lstat
#define lstat(p, s) rb_w32_ulstati64((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 utime
#define utime(p, t) rb_w32_uutime((p), (t))
#undef link
#define link(f, t) rb_w32_ulink((f), (t))
#undef unlink
#define unlink(p) rb_w32_uunlink(p)
#undef rename
#define rename(f, t) rb_w32_urename((f), (t))
#undef symlink
#define symlink(s, l) rb_w32_usymlink((s), (l))
#else
#define STAT(p, s) stat((p), (s))
#endif
#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
VALUE rb_cFile;
VALUE rb_mFileTest;
VALUE rb_cStat;
#define insecure_obj_p(obj, level) ((level) > 0 && OBJ_TAINTED(obj))
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 != 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, int level)
{
VALUE tmp;
ID to_path;
if (insecure_obj_p(obj, level)) {
rb_insecure_operation();
}
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, VALUE tmp, int level)
{
tmp = file_path_convert(tmp);
if (obj != tmp && insecure_obj_p(tmp, level)) {
rb_insecure_operation();
}
check_path_encoding(tmp);
if (!rb_str_to_cstr(tmp)) {
rb_raise(rb_eArgError, "path name contains null byte");
}
return rb_str_new4(tmp);
}
VALUE
rb_get_path_check(VALUE obj, int level)
{
VALUE tmp = rb_get_path_check_to_string(obj, level);
return rb_get_path_check_convert(obj, tmp, level);
}
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, rb_safe_level());
}
VALUE
rb_str_encode_ospath(VALUE path)
{
#if USE_OSPATH
int encidx = ENCODING_GET(path);
#ifdef _WIN32
if (encidx == ENCINDEX_ASCII) {
encidx = rb_filesystem_encindex();
}
#endif
if (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
return path;
}
#ifdef __APPLE__
# define NORMALIZE_UTF8PATH 1
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
# define NORMALIZE_UTF8PATH 0
#endif
#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
static VALUE
apply2files(void (*func)(const char *, VALUE, void *), int argc, VALUE *argv, void *arg)
{
long i;
volatile VALUE path;
for (i=0; i<argc; i++) {
const char *s;
path = rb_get_path(argv[i]);
path = rb_str_encode_ospath(path);
s = RSTRING_PTR(path);
(*func)(s, path, arg);
}
return LONG2FIX(argc);
}
/*
* call-seq:
* file.path -> filename
* file.to_path -> filename
*
* Returns the pathname used to create <i>file</i> as a string. Does
* not normalize the name.
*
* The pathname may not point the file corresponding to <i>file</i>.
* e.g. file has been moved, deleted, or created with <code>File::TMPFILE</code> option.
*
* 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 size_t
stat_memsize(const void *p)
{
return sizeof(struct stat);
}
static const rb_data_type_t stat_data_type = {
"stat",
{NULL, RUBY_TYPED_DEFAULT_FREE, stat_memsize,},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};
static VALUE
stat_new_0(VALUE klass, const struct stat *st)
{
struct stat *nst = 0;
VALUE obj = TypedData_Wrap_Struct(klass, &stat_data_type, 0);
if (st) {
nst = ALLOC(struct stat);
*nst = *st;
RTYPEDDATA_DATA(obj) = nst;
}
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 stat* st;
TypedData_Get_Struct(self, struct stat, &stat_data_type, 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, 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)
{
return DEVT2NUM(get_stat(self)->st_dev);
}
/*
* 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 DEVT2NUM(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 DEVT2NUM(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 _WIN32
struct stat *st = get_stat(self);
unsigned short *p2 = (unsigned short *)st;
unsigned int *p4 = (unsigned int *)st;
uint64_t r;
r = p2[2];
r <<= 16;
r |= p2[7];
r <<= 32;
r |= p4[5];
return ULL2NUM(r);
#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)
{
return UINT2NUM(get_stat(self)->st_nlink);
}
/*
* 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
return DEVT2NUM(get_stat(self)->st_rdev);
#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 DEVT2NUM(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 DEVT2NUM(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(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_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 = (long)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 = (long)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);
}
#define HAVE_STAT_BIRTHTIME
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
static VALUE
stat_birthtime(struct stat *st)
{
struct timespec *ts = &st->st_birthtimespec;
return rb_time_nano_new(ts->tv_sec, ts->tv_nsec);
}
#elif defined(_WIN32)
# define stat_birthtime stat_ctime
#else
# undef HAVE_STAT_BIRTHTIME
#endif
/*
* 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).
*
* Note that on Windows (NTFS), returns creation time (birth time).
*
* 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));
}
#if defined(HAVE_STAT_BIRTHTIME)
/*
* call-seq:
* stat.birthtime -> aTime
*
* Returns the birth time for <i>stat</i>.
*
* If the platform doesn't have birthtime, raises NotImplementedError.
*
* File.write("testfile", "foo")
* sleep 10
* File.write("testfile", "bar")
* sleep 10
* File.chmod(0644, "testfile")
* sleep 10
* File.read("testfile")
* File.stat("testfile").birthtime #=> 2014-02-24 11:19:17 +0900
* File.stat("testfile").mtime #=> 2014-02-24 11:19:27 +0900
* File.stat("testfile").ctime #=> 2014-02-24 11:19:37 +0900
* File.stat("testfile").atime #=> 2014-02-24 11:19:47 +0900
*
*/
static VALUE
rb_stat_birthtime(VALUE self)
{
return stat_birthtime(get_stat(self));
}
#else
# define rb_stat_birthtime rb_f_notimplement
#endif
/*
* 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,
* # birthtime=Mon Aug 04 08:13:49 CDT 2003>"
*/
static VALUE
rb_stat_inspect(VALUE self)
{
VALUE str;
size_t 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},
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
{"birthtime", rb_stat_birthtime},
#endif
};
struct stat* st;
TypedData_Get_Struct(self, struct stat, &stat_data_type, 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, "=");