[前][次][番号順一覧][スレッド一覧]

ruby-changes:71787

From: Akinori <ko1@a...>
Date: Thu, 21 Apr 2022 17:10:31 +0900 (JST)
Subject: [ruby-changes:71787] cb02324c4e (master): [ruby/pathname] Implement Pathname#lutime

https://git.ruby-lang.org/ruby.git/commit/?id=cb02324c4e

From cb02324c4e5c7aae0add0a5c4e5adbf637d9acb0 Mon Sep 17 00:00:00 2001
From: Akinori MUSHA <knu@i...>
Date: Mon, 4 Apr 2022 02:03:09 +0900
Subject: [ruby/pathname] Implement Pathname#lutime

https://github.com/ruby/pathname/commit/268cb5acff
---
 ext/pathname/pathname.c        | 17 +++++++++++++++++
 test/pathname/test_pathname.rb | 19 +++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c
index 00ca85205b..e2c3c36dbf 100644
--- a/ext/pathname/pathname.c
+++ b/ext/pathname/pathname.c
@@ -35,6 +35,7 @@ static ID id_lchmod; https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L35
 static ID id_lchown;
 static ID id_link;
 static ID id_lstat;
+static ID id_lutime;
 static ID id_mkdir;
 static ID id_mtime;
 static ID id_open;
@@ -764,6 +765,19 @@ path_utime(VALUE self, VALUE atime, VALUE mtime) https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L765
     return rb_funcall(rb_cFile, id_utime, 3, atime, mtime, get_strpath(self));
 }
 
+/*
+ * Update the access and modification times of the file.
+ *
+ * Same as Pathname#utime, but does not follow symbolic links.
+ *
+ * See File.lutime.
+ */
+static VALUE
+path_lutime(VALUE self, VALUE atime, VALUE mtime)
+{
+    return rb_funcall(rb_cFile, id_lutime, 3, atime, mtime, get_strpath(self));
+}
+
 /*
  * Returns the last component of the path.
  *
@@ -1465,6 +1479,7 @@ path_f_pathname(VALUE self, VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L1479
  * - #make_symlink(old)
  * - #truncate(length)
  * - #utime(atime, mtime)
+ * - #lutime(atime, mtime)
  * - #basename(*args)
  * - #dirname
  * - #extname
@@ -1563,6 +1578,7 @@ Init_pathname(void) https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L1578
     rb_define_method(rb_cPathname, "make_symlink", path_make_symlink, 1);
     rb_define_method(rb_cPathname, "truncate", path_truncate, 1);
     rb_define_method(rb_cPathname, "utime", path_utime, 2);
+    rb_define_method(rb_cPathname, "lutime", path_lutime, 2);
     rb_define_method(rb_cPathname, "basename", path_basename, -1);
     rb_define_method(rb_cPathname, "dirname", path_dirname, 0);
     rb_define_method(rb_cPathname, "extname", path_extname, 0);
@@ -1646,6 +1662,7 @@ InitVM_pathname(void) https://github.com/ruby/ruby/blob/trunk/ext/pathname/pathname.c#L1662
     id_lchown = rb_intern("lchown");
     id_link = rb_intern("link");
     id_lstat = rb_intern("lstat");
+    id_lutime = rb_intern("lutime");
     id_mkdir = rb_intern("mkdir");
     id_mtime = rb_intern("mtime");
     id_open = rb_intern("open");
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index c971597602..a23dc21ae3 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -1043,6 +1043,25 @@ class TestPathname < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/pathname/test_pathname.rb#L1043
     }
   end
 
+  def test_lutime
+    return if !has_symlink?
+    with_tmpchdir('rubytest-pathname') {|dir|
+      open("a", "w") {|f| f.write "abc" }
+      atime = File.atime("a")
+      mtime = File.mtime("a")
+      latime = Time.utc(2000)
+      lmtime = Time.utc(1999)
+      File.symlink("a", "l")
+      Pathname("l").utime(latime, lmtime)
+      s = File.lstat("a")
+      ls = File.lstat("l")
+      assert_equal(atime, s.atime)
+      assert_equal(mtime, s.mtime)
+      assert_equal(latime, ls.atime)
+      assert_equal(lmtime, ls.mtime)
+    }
+  end
+
   def test_basename
     assert_equal(Pathname("basename"), Pathname("dirname/basename").basename)
     assert_equal(Pathname("bar"), Pathname("foo/bar.x").basename(".x"))
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]