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

ruby-changes:43438

From: nobu <ko1@a...>
Date: Mon, 27 Jun 2016 16:55:23 +0900 (JST)
Subject: [ruby-changes:43438] nobu:r55512 (trunk): FileUtils#install: owner/group options

nobu	2016-06-27 16:55:17 +0900 (Mon, 27 Jun 2016)

  New Revision: 55512

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55512

  Log:
    FileUtils#install: owner/group options
    
    * lib/fileutils.rb (FileUtils#install): add owner and group
      options.

  Modified files:
    trunk/ChangeLog
    trunk/lib/fileutils.rb
    trunk/lib/un.rb
    trunk/test/fileutils/test_fileutils.rb
Index: test/fileutils/test_fileutils.rb
===================================================================
--- test/fileutils/test_fileutils.rb	(revision 55511)
+++ test/fileutils/test_fileutils.rb	(revision 55512)
@@ -968,6 +968,22 @@ class TestFileUtils < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L968
     }
   end
 
+  def test_install_owner_option
+    File.open('tmp/aaa', 'w') {|f| f.puts 'aaa' }
+    File.open('tmp/bbb', 'w') {|f| f.puts 'bbb' }
+    assert_nothing_raised {
+      install 'tmp/aaa', 'tmp/bbb', :owner => "nobody", :noop => true
+    }
+  end
+
+  def test_install_group_option
+    File.open('tmp/aaa', 'w') {|f| f.puts 'aaa' }
+    File.open('tmp/bbb', 'w') {|f| f.puts 'bbb' }
+    assert_nothing_raised {
+      install 'tmp/aaa', 'tmp/bbb', :group => "nobody", :noop => true
+    }
+  end
+
   def test_chmod
     check_singleton :chmod
 
Index: lib/un.rb
===================================================================
--- lib/un.rb	(revision 55511)
+++ lib/un.rb	(revision 55512)
@@ -189,13 +189,17 @@ end https://github.com/ruby/ruby/blob/trunk/lib/un.rb#L189
 #   -p          apply access/modification times of SOURCE files to
 #               corresponding destination files
 #   -m          set permission mode (as in chmod), instead of 0755
+#   -o          set owner user id, instead of the current owner
+#   -g          set owner group id, instead of the current group
 #   -v          verbose
 #
 
 def install
-  setup("pm:") do |argv, options|
+  setup("pm:o:g:") do |argv, options|
     options[:mode] = (mode = options.delete :m) ? mode.oct : 0755
     options[:preserve] = true if options.delete :p
+    (owner = options.delete :o) and options[:owner] = owner
+    (group = options.delete :g) and options[:group] = group
     dest = argv.pop
     argv = argv[0] if argv.size == 1
     FileUtils.install argv, dest, options
Index: lib/fileutils.rb
===================================================================
--- lib/fileutils.rb	(revision 55511)
+++ lib/fileutils.rb	(revision 55512)
@@ -754,9 +754,20 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L754
   #   FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true
   #   FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true
   #
-  def install(src, dest, mode: nil, preserve: nil, noop: nil, verbose: nil)
-    fu_output_message "install -c#{preserve && ' -p'}#{mode ? (' -m 0%o' % mode) : ''} #{[src,dest].flatten.join ' '}" if verbose
+  def install(src, dest, mode: nil, owner: nil, group: nil, preserve: nil,
+              noop: nil, verbose: nil)
+    if verbose
+      msg = +"install -c"
+      msg << ' -p' if preserve
+      msg << ' -m 0%o' % mode if mode
+      msg << " -o #{owner}" if owner
+      msg << " -g #{group}" if group
+      msg << ' ' << [src,dest].flatten.join(' ')
+      fu_output_message msg
+    end
     return if noop
+    uid = fu_get_uid(owner)
+    gid = fu_get_gid(group)
     fu_each_src_dest(src, dest) do |s, d|
       st = File.stat(s)
       unless File.exist?(d) and compare_file(s, d)
@@ -764,6 +775,7 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L775
         copy_file s, d
         File.utime st.atime, st.mtime, d if preserve
         File.chmod mode, d if mode
+        File.chown uid, gid, d if uid or gid
       end
     end
   end
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55511)
+++ ChangeLog	(revision 55512)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jun 27 16:55:14 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/fileutils.rb (FileUtils#install): add owner and group
+	  options.
+
 Mon Jun 27 08:56:55 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* compile.c (ADD_TRACE): ignore trace instruction on non-positive

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

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