[#33000] [Ruby 1.9-Bug#4014][Open] Case-Sensitivity of Property Names Depends on Regexp Encoding — Run Paint Run Run <redmine@...>

Bug #4014: Case-Sensitivity of Property Names Depends on Regexp Encoding

11 messages 2010/11/01

[#33021] Re: [Ruby 1.9-Feature#4015][Open] File::DIRECT Constant for O_DIRECT — Yukihiro Matsumoto <matz@...>

Hi,

15 messages 2010/11/02

[#33139] [Ruby 1.9-Bug#4044][Open] Regex matching errors when using \W character class and /i option — Ben Hoskings <redmine@...>

Bug #4044: Regex matching errors when using \W character class and /i option

8 messages 2010/11/11

[#33162] Windows Unicode (chcp 65001) Generates incorrect output — Luis Lavena <luislavena@...>

Hello,

10 messages 2010/11/14

[#33246] [Ruby 1.9-Feature#4068][Open] Replace current standard Date/DateTime library with home_run — Jeremy Evans <redmine@...>

Feature #4068: Replace current standard Date/DateTime library with home_run

40 messages 2010/11/17

[#33255] [Ruby 1.9-Feature#4071][Open] support basic auth for Net::HTTP.get requests — "coderrr ." <redmine@...>

Feature #4071: support basic auth for Net::HTTP.get requests

23 messages 2010/11/19

[#33322] [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Shugo Maeda <redmine@...>

Feature #4085: Refinements and nested methods

94 messages 2010/11/24
[#33345] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Yusuke ENDOH <mame@...> 2010/11/25

Hi,

[#33356] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Shugo Maeda <shugo@...> 2010/11/25

Hi,

[#33375] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Yusuke ENDOH <mame@...> 2010/11/25

Hi,

[#33381] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Shugo Maeda <shugo@...> 2010/11/25

Hi,

[#33387] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Magnus Holm <judofyr@...> 2010/11/25

Woah, this is very nice stuff! Some comments/questions:

[#33487] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Charles Oliver Nutter <headius@...> 2010/11/30

This is a long response, and for that I apologize. I want to make sure

[#33535] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Yusuke ENDOH <mame@...> 2010/12/03

Hi,

[#33519] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Shugo Maeda <shugo@...> 2010/12/02

Hi,

[#33523] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Yusuke ENDOH <mame@...> 2010/12/02

Hi,

[#33539] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Shugo Maeda <shugo@...> 2010/12/03

Hi,

[#33543] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Yusuke ENDOH <mame@...> 2010/12/03

Hi,

[#33546] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Shugo Maeda <shugo@...> 2010/12/03

Hi,

[#33548] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Yusuke ENDOH <mame@...> 2010/12/03

Hi,

[#33567] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Shugo Maeda <shugo@...> 2010/12/04

Hi,

[#33595] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods — Charles Oliver Nutter <headius@...> 2010/12/06

On Sat, Dec 4, 2010 at 6:32 AM, Shugo Maeda <shugo@ruby-lang.org> wrote:

[#33367] Planning to release 1.8.7 fixes on 12/25 (Japanese timezone) — Urabe Shyouhei <shyouhei@...>

Hello,

20 messages 2010/11/25
[#33439] Re: Planning to release 1.8.7 fixes on 12/25 (Japanese timezone) — Luis Lavena <luislavena@...> 2010/11/27

2010/11/25 Urabe Shyouhei <shyouhei@ruby-lang.org>:

[#33456] [Request for Comment] avoid timer thread — SASADA Koichi <ko1@...>

Hi,

25 messages 2010/11/29
[#35152] Re: [Request for Comment] avoid timer thread — Mark Somerville <mark@...> 2011/02/08

On Mon, Nov 29, 2010 at 11:53:03AM +0900, SASADA Koichi wrote:

[#36077] Re: [Request for Comment] avoid timer thread — Mark Somerville <mark@...> 2011/05/09

On Tue, Feb 08, 2011 at 09:24:13PM +0900, Mark Somerville wrote:

[#36952] Re: [Request for Comment] avoid timer thread — Eric Wong <normalperson@...> 2011/06/10

Mark Somerville <mark@scottishclimbs.com> wrote:

[#37080] Re: [Request for Comment] avoid timer thread — Mark Somerville <mark@...> 2011/06/13

On Sat, Jun 11, 2011 at 05:57:11AM +0900, Eric Wong wrote:

[#37103] Re: [Request for Comment] avoid timer thread — Eric Wong <normalperson@...> 2011/06/13

Mark Somerville <mark@scottishclimbs.com> wrote:

[#37187] Re: [Request for Comment] avoid timer thread — SASADA Koichi <ko1@...> 2011/06/16

(2011/06/14 3:37), Eric Wong wrote:

[#37195] Re: [Request for Comment] avoid timer thread — Eric Wong <normalperson@...> 2011/06/17

SASADA Koichi <ko1@atdot.net> wrote:

[#37205] Re: [Request for Comment] avoid timer thread — Eric Wong <normalperson@...> 2011/06/17

Eric Wong <normalperson@yhbt.net> wrote:

[#33469] [Ruby 1.9-Feature#4100][Open] Improve Net::HTTP documentation — Eric Hodel <redmine@...>

Feature #4100: Improve Net::HTTP documentation

12 messages 2010/11/29

[ruby-core:33163] [Ruby 1.9-Feature#4052][Open] File.lutime Patch

From: Run Paint Run Run <redmine@...>
Date: 2010-11-14 01:23:19 UTC
List: ruby-core #33163
Feature #4052: File.lutime Patch
http://redmine.ruby-lang.org/issues/show/4052

Author: Run Paint Run Run
Status: Open, Priority: Normal
Category: core

This patch implements File.lutime, which behaves as File.utime except if given a symlink it acts upon the link itself rather than the referent. The naming convention follows that of the other singleton methods of File which treat symlinks specially. This functionality could be implemented by a gem, but given we already define File.lchmod, File.lchown, and File.lstat, it is inconsistent not to also define File.lutime.

This functionality is provided by either lutimes(3), or utimensat(2) with the AT_SYMLINK_NOFOLLOW flag. The latter is standardised by POSIX <http://goo.gl/38pHx>

Platform support is as follows:
* FreeBSD - Has lutimes(2) since version 3. <http://goo.gl/9OWRF>
* Mac OS - Has lutimes since at least version 10.5. <http://goo.gl/pSvM1>
* OpenBSD - Appears to lack both APIs.
* Linux - Supported lutimes(3) and utimensat(2) since version 2.6.22. (lutimes() is just a wrapper for utimensat()). <http://goo.gl/p29ja>
* Windows - No support.

For the status of esoteric platforms, consult http://goo.gl/Bf4Qm and http://goo.gl/7h6cj .

On platforms that don't support either function, File.lutime raises a NotImplementedError.

The portability logic is a bit hairy, so I'll summarise:

1 - File.lutime requires either lutimes() to be defined or both utimensat() and AT_SYMLINK_NOFOLLOW. I'm not aware of a practical case where utimensat() would be defined without AT_SYMLINK_NOFOLLOW. Further, File.lutime requires utimes(). This isn't strictly necessary, but simplifies the logic: it seems most unlikely that a platform would support either lutimes() or utimensat() without also supporting utimes().
2 - Like File.utime, we prefer to use utimensat() as it provides better resolution than utimes(). 
3 - If utimensat(..., AT_SYMLINK_NOFOLLOW) sets errno to ENOSYS, there are two possibilities: either the platform doesn't support the system call, or, like Linux 2.6.22 (<http://goo.gl/Bf4Qm>), utimensat() is defined but raises ENOSYS when given this flag. If utimensat() gives ENOSYS when it wasn't passed AT_SYMLINK_NOFOLLOW, we assume the syscall really doesn't exist, and bypass it for all future invocations of File.utime and File.lutime. If utimensat() gives ENOSYS when passed AT_SYMLINK_NOFOLLOW, or AT_SYMLINK_NOFOLLOW_ isn't defined, we bypass utimensat() for all future File.lutime invocations, but still consider it for future File.utime invocations.
4 - If we can't use utimensat() we fall back on lutimes().
5 - If a platform defines utimensat() and AT_SYMLINK_NOFOLLOW, but the former gives ENOSYS when passed the latter, we also fall back on lutimes(). However, if the platform doesn't define lutimes(), we raise NotImplementedError.

Tests are attached for File.utime when given a symlink and File.lutime.


----------------------------------------
http://redmine.ruby-lang.org

Attachments (1)

lutime.patch (6.77 KB, text/x-diff)
diff --git a/configure.in b/configure.in
index af5274d..d41d70b 100644
--- a/configure.in
+++ b/configure.in
@@ -1291,7 +1291,7 @@ else
 fi
 AC_CHECK_FUNCS(fmod killpg wait4 waitpid fork spawnv syscall chroot getcwd eaccess\
 	      truncate ftruncate chsize times utimes utimensat fcntl lockf lstat\
-	      link symlink readlink readdir_r fsync fdatasync fchown\
+	      link lutimes symlink readlink readdir_r fsync fdatasync fchown\
 	      setitimer setruid seteuid setreuid setresuid setproctitle socketpair\
 	      setrgid setegid setregid setresgid issetugid pause lchown lchmod\
 	      getpgrp setpgrp getpgid setpgid initgroups getgroups setgroups\
diff --git a/file.c b/file.c
index dc9f830..0e80731 100644
--- a/file.c
+++ b/file.c
@@ -2195,6 +2195,7 @@ struct timespec rb_time_timespec(VALUE time);
 struct utime_args {
     const struct timespec* tsp;
     VALUE atime, mtime;
+    int follow; /* Whether to act on symlinks (1) or their referent (0) */
 };
 
 #if defined DOSISH || defined __CYGWIN__
@@ -2246,11 +2247,24 @@ utime_internal(const char *path, void *arg)
 
 #ifdef HAVE_UTIMENSAT
     static int try_utimensat = 1;
+    static int try_utimensat_follow = 1;
+    int flags = 0;
 
-    if (try_utimensat) {
-        if (utimensat(AT_FDCWD, path, tsp, 0) < 0) {
+    if ((v->follow && try_utimensat_follow) || (!v->follow && try_utimensat)) {
+	if (v->follow) {
+#ifdef AT_SYMLINK_NOFOLLOW
+	    flags = AT_SYMLINK_NOFOLLOW;
+#else
+	    try_utimensat_follow = 0;
+	    goto no_utimensat;
+#endif
+	}
+
+        if (utimensat(AT_FDCWD, path, tsp, flags) < 0) {
             if (errno == ENOSYS) {
-                try_utimensat = 0;
+		try_utimensat_follow = 0;
+                if (!v->follow)
+		    try_utimensat = 0;
                 goto no_utimensat;
             }
             utime_failed(path, tsp, v->atime, v->mtime);
@@ -2267,7 +2281,16 @@ no_utimensat:
         tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
         tvp = tvbuf;
     }
-    if (utimes(path, tvp) < 0)
+
+    if (v->follow) {
+#ifdef HAVE_LUTIMES
+	if (lutimes(path, tvp) < 0)
+	    utime_failed(path, tsp, v->atime, v->mtime);
+#else
+	rb_notimplement();
+#endif
+    }
+    else if (utimes(path, tvp) < 0)
 	utime_failed(path, tsp, v->atime, v->mtime);
 }
 
@@ -2297,17 +2320,8 @@ utime_internal(const char *path, void *arg)
 
 #endif
 
-/*
- * call-seq:
- *  File.utime(atime, mtime, file_name,...)   ->  integer
- *
- * Sets the access and modification times of each
- * named file to the first two arguments. Returns
- * the number of file names in the argument list.
- */
-
 static VALUE
-rb_file_s_utime(int argc, VALUE *argv)
+utime_internal_i(int argc, VALUE *argv, int follow)
 {
     VALUE rest;
     struct utime_args args;
@@ -2317,6 +2331,8 @@ rb_file_s_utime(int argc, VALUE *argv)
     rb_secure(2);
     rb_scan_args(argc, argv, "2*", &args.atime, &args.mtime, &rest);
 
+    args.follow = follow;
+
     if (!NIL_P(args.atime) || !NIL_P(args.mtime)) {
 	tsp = tss;
 	tsp[0] = rb_time_timespec(args.atime);
@@ -2328,6 +2344,45 @@ rb_file_s_utime(int argc, VALUE *argv)
     return LONG2FIX(n);
 }
 
+/*
+ * call-seq:
+ *  File.utime(atime, mtime, file_name,...)   ->  integer
+ *
+ * Sets the access and modification times of each named file to the
+ * first two arguments. If a file is a symlink, this method acts upon
+ * its referent rather than the link itself; for the inverse
+ * behavior see <code>File.lutime</code>. Returns the number of file
+ * names in the argument list.
+ */
+
+static VALUE
+rb_file_s_utime(int argc, VALUE *argv)
+{
+    return utime_internal_i(argc, argv, 0);
+}
+
+#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
+
+/*
+ * call-seq:
+ *  File.lutime(atime, mtime, file_name,...)   ->  integer
+ *
+ * Sets the access and modification times of each named file to the
+ * first two arguments. If a file is a symlink, this method acts upon
+ * the link itself as opposed to its referent; for the inverse
+ * behavior, see <code>File.utime</code>. Returns the number of file
+ * names in the argument list.
+ */
+
+static VALUE
+rb_file_s_lutime(int argc, VALUE *argv)
+{
+    return utime_internal_i(argc, argv, 1);
+}
+#else
+#define rb_file_s_lutime rb_f_notimplement
+#endif
+
 NORETURN(static void sys_fail2(VALUE,VALUE));
 static void
 sys_fail2(VALUE s1, VALUE s2)
@@ -5319,6 +5374,7 @@ Init_File(void)
     rb_define_singleton_method(rb_cFile, "chown", rb_file_s_chown, -1);
     rb_define_singleton_method(rb_cFile, "lchmod", rb_file_s_lchmod, -1);
     rb_define_singleton_method(rb_cFile, "lchown", rb_file_s_lchown, -1);
+    rb_define_singleton_method(rb_cFile, "lutime", rb_file_s_lutime, -1);
 
     rb_define_singleton_method(rb_cFile, "link", rb_file_s_link, 2);
     rb_define_singleton_method(rb_cFile, "symlink", rb_file_s_symlink, 2);
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index 1d45b97..1d16c3d 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -346,6 +346,41 @@ class TestFileExhaustive < Test::Unit::TestCase
     File.utime(t + 1, t + 2, @zerofile)
     assert_equal(t + 1, File.atime(@zerofile))
     assert_equal(t + 2, File.mtime(@zerofile))
+    skip("File.symlink not implemented") unless (File.respond_to?(:symlink) and @symlinkfile)
+    stat_ln = File.lstat(@symlinkfile)
+    assert_equal(File.utime(t, t, @symlinkfile), 1)
+    assert_equal(File.stat(@file).atime, t)
+    assert_equal(File.stat(@file).mtime, t)
+    assert_equal(File.lstat(@symlinkfile).atime, stat_ln.atime)
+    assert_equal(File.lstat(@symlinkfile).mtime, stat_ln.mtime)
+  end
+
+  def test_lutime
+    skip("File.lutime not implemented") unless File.respond_to?(:lutime)
+    Dir.mktmpdir('rubytest-lutime') do |tmp|
+      Dir.chdir(tmp) do
+        open('f',?w){}
+        times = [Time.now - 24*100, Time.now - 24*1_000].map(&:round)
+        assert_equal(File.lutime(times[0], times[0], ?f), 1)
+        assert_equal(File.stat(?f).atime.round, times[0])
+        assert_equal(File.stat(?f).mtime.round, times[0])
+
+        # Assume that a platform supporting changing utimens on symlinks
+        # also supports symlinks
+        File.symlink(?f, ?l)
+        assert_equal(File.lutime(times[1], times[1], ?l), 1)
+        assert_equal(File.stat(?f).atime.round, times[0])
+        assert_equal(File.stat(?f).mtime.round, times[0])
+        assert_equal(File.lstat(?l).atime.round, times[1])
+        assert_equal(File.lstat(?l).mtime.round, times[1])
+
+        assert_equal(File.lutime(times[1], times[0], ?l, ?f), 2)
+        assert_equal(File.stat(?f).atime.round, times[1])
+        assert_equal(File.stat(?f).mtime.round, times[0])
+        assert_equal(File.lstat(?l).atime.round, times[1])
+        assert_equal(File.lstat(?l).mtime.round, times[0])
+      end
+    end
   end
 
   def test_hardlink

In This Thread

Prev Next