Project

General

Profile

Feature #9857 ยป pathname_birthtime.patch

znz (Kazuhiro NISHIYAMA), 06/28/2014 10:32 AM

View differences:

ext/pathname/extconf.rb
1 1
require 'mkmf'
2
have_struct_member("struct stat", "st_birthtimespec", "sys/stat.h")
2 3
create_makefile('pathname')
ext/pathname/pathname.c
442 442

  
443 443
/*
444 444
 * call-seq:
445
 *   pathname.birthtime	-> time
446
 *
447
 * Returns the birth time for the file.
448
 * If the platform doesn't have birthtime, returns <i>ctime</i>.
449
 *
450
 * See File.birthtime.
451
 */
452
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
453
static VALUE
454
path_birthtime(VALUE self)
455
{
456
    return rb_funcall(rb_cFile, rb_intern("birthtime"), 1, get_strpath(self));
457
}
458
#else
459
# define path_birthtime rb_f_notimplement
460
#endif
461

  
462
/*
463
 * call-seq:
445 464
 *   pathname.ctime	-> time
446 465
 *
447 466
 * Returns the last change time, using directory information, not the file itself.
......
1288 1307
 *
1289 1308
 * These methods are a facade for File:
1290 1309
 * - #atime
1310
 * - #birthtime
1291 1311
 * - #ctime
1292 1312
 * - #mtime
1293 1313
 * - #chmod(mode)
......
1380 1400
    rb_define_method(rb_cPathname, "binwrite", path_binwrite, -1);
1381 1401
    rb_define_method(rb_cPathname, "sysopen", path_sysopen, -1);
1382 1402
    rb_define_method(rb_cPathname, "atime", path_atime, 0);
1403
    rb_define_method(rb_cPathname, "birthtime", path_birthtime, 0);
1383 1404
    rb_define_method(rb_cPathname, "ctime", path_ctime, 0);
1384 1405
    rb_define_method(rb_cPathname, "mtime", path_mtime, 0);
1385 1406
    rb_define_method(rb_cPathname, "chmod", path_chmod, 1);
pathname_birthtime.patch
1
diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c
2
index 3db97fc..cfdaac9 100644
3
--- a/ext/pathname/pathname.c
4
+++ b/ext/pathname/pathname.c
5
@@ -442,6 +442,25 @@ path_atime(VALUE self)
6
 
7
 /*
8
  * call-seq:
9
+ *   pathname.birthtime	-> time
10
+ *
11
+ * Returns the birth time for the file.
12
+ * If the platform doesn't have birthtime, returns <i>ctime</i>.
13
+ *
14
+ * See File.birthtime.
15
+ */
16
+#if defined(HAVE_STAT_BIRTHTIME)
17
+static VALUE
18
+path_birthtime(VALUE self)
19
+{
20
+    return rb_funcall(rb_cFile, rb_intern("birthtime"), 1, get_strpath(self));
21
+}
22
+#else
23
+# define path_birthtime rb_f_notimplement
24
+#endif
25
+
26
+/*
27
+ * call-seq:
28
  *   pathname.ctime	-> time
29
  *
30
  * Returns the last change time, using directory information, not the file itself.
31
@@ -1288,6 +1307,7 @@ path_f_pathname(VALUE self, VALUE str)
32
  *
33
  * These methods are a facade for File:
34
  * - #atime
35
+ * - #birthtime
36
  * - #ctime
37
  * - #mtime
38
  * - #chmod(mode)
39
@@ -1380,6 +1400,7 @@ Init_pathname()
40
     rb_define_method(rb_cPathname, "binwrite", path_binwrite, -1);
41
     rb_define_method(rb_cPathname, "sysopen", path_sysopen, -1);
42
     rb_define_method(rb_cPathname, "atime", path_atime, 0);
43
+    rb_define_method(rb_cPathname, "birthtime", path_birthtime, 0);
44
     rb_define_method(rb_cPathname, "ctime", path_ctime, 0);
45
     rb_define_method(rb_cPathname, "mtime", path_mtime, 0);
46
     rb_define_method(rb_cPathname, "chmod", path_chmod, 1);
47
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
48
index 110b782..413cb4c 100644
49
--- a/test/pathname/test_pathname.rb
50
+++ b/test/pathname/test_pathname.rb
51
@@ -771,6 +771,11 @@ class TestPathname < Test::Unit::TestCase
52
     assert_kind_of(Time, Pathname(__FILE__).atime)
53
   end
54
 
55
+  def test_birthtime
56
+    assert_kind_of(Time, Pathname(__FILE__).birthtime)
57
+  rescue NotImplementedError
58
+  end
59
+
60
   def test_ctime
61
     assert_kind_of(Time, Pathname(__FILE__).ctime)
62
   end
test/pathname/test_pathname.rb
771 771
    assert_kind_of(Time, Pathname(__FILE__).atime)
772 772
  end
773 773

  
774
  def test_birthtime
775
    assert_kind_of(Time, Pathname(__FILE__).birthtime)
776
  rescue NotImplementedError
777
    assert_raise(NotImplementedError) do
778
      File.birthtime(__FILE__)
779
    end
780
  end
781

  
774 782
  def test_ctime
775 783
    assert_kind_of(Time, Pathname(__FILE__).ctime)
776 784
  end