From 15dabe82163717bbdadab82d72e657f5fa731e99 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 3 Mar 2007 13:37:02 +0000 Subject: [PATCH] * file.c (rb_file_s_utime): allow nil to set the current time. * lib/fileutils.rb (touch): ditto, and added :mtime and :nocreate options. fixed: [ruby-talk:219037] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11973 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ file.c | 11 +++++++---- lib/fileutils.rb | 17 ++++++++++++++--- version.h | 2 ++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index d0b820f505..fab8fed3a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sat Mar 3 22:37:02 2007 Nobuyoshi Nakada + + * file.c (rb_file_s_utime): allow nil to set the current time. + + * lib/fileutils.rb (touch): ditto, and added :mtime and :nocreate + options. fixed: [ruby-talk:219037] + Sat Mar 3 15:52:26 2007 Akinori MUSHA * object.c (instance_variable_get): Restore rdoc markups lost in diff --git a/file.c b/file.c index cbda4dd36f..843b170553 100644 --- a/file.c +++ b/file.c @@ -1989,13 +1989,16 @@ static VALUE rb_file_s_utime(int argc, VALUE *argv) { VALUE atime, mtime, rest; - struct timeval tvp[2]; + struct timeval tvs[2], *tvp = NULL; long n; rb_scan_args(argc, argv, "2*", &atime, &mtime, &rest); - tvp[0] = rb_time_timeval(atime); - tvp[1] = rb_time_timeval(mtime); + if (!NIL_P(atime) || !NIL_P(mtime)) { + tvp = tvs; + tvp[0] = rb_time_timeval(atime); + tvp[1] = rb_time_timeval(mtime); + } n = apply2files(utime_internal, rest, tvp); return LONG2FIX(n); @@ -2024,7 +2027,7 @@ rb_file_s_utime(int argc, VALUE *argv) VALUE atime, mtime, rest; long n; struct timeval tv; - struct utimbuf utbuf; + struct utimbuf utbuf, *utp = NULL; rb_scan_args(argc, argv, "2*", &atime, &mtime, &rest); diff --git a/lib/fileutils.rb b/lib/fileutils.rb index 976b838838..4da9b56fb6 100644 --- a/lib/fileutils.rb +++ b/lib/fileutils.rb @@ -1007,22 +1007,33 @@ module FileUtils def touch(list, options = {}) fu_check_options options, OPT_TABLE['touch'] list = fu_list(list) - fu_output_message "touch #{list.join ' '}" if options[:verbose] + created = nocreate = options[:nocreate] + t = options[:mtime] + if options[:verbose] + fu_output_message "touch #{ + nocreate ? ' -c' : '' + }#{ + t ? t.strftime(' -t %Y%m%d%H%M.%S') : '' + }#{list.join ' '}" + end return if options[:noop] - t = Time.now list.each do |path| + created = nocreate begin File.utime(t, t, path) rescue Errno::ENOENT + raise if created File.open(path, 'a') { ; } + created = true + retry if t end end end module_function :touch - OPT_TABLE['touch'] = [:noop, :verbose] + OPT_TABLE['touch'] = [:noop, :verbose, :mtime, :nocreate] private diff --git a/version.h b/version.h index 78f407ea13..90f2886a29 100644 --- a/version.h +++ b/version.h @@ -11,7 +11,9 @@ #define RUBY_RELEASE_MONTH 3 #define RUBY_RELEASE_DAY 3 +#ifdef RUBY_EXTERN RUBY_EXTERN const char ruby_version[]; RUBY_EXTERN const char ruby_release_date[]; RUBY_EXTERN const char ruby_platform[]; RUBY_EXTERN const int ruby_patchlevel; +#endif