From 77923d719fcdab79ea39c79de96d264caf445f99 Mon Sep 17 00:00:00 2001 From: aamine Date: Fri, 21 Feb 2003 11:57:31 +0000 Subject: [PATCH] * lib/fileutils.rb: new method FileUtils#copy_stream. * lib/fileutils.rb: new method FileUtils#compare_file. * lib/fileutils.rb: new method FileUtils#compare_stream. * lib/fileutils.rb: new method FileUtils#rmtree (alias of rm_rf). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3526 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 +++++ lib/fileutils.rb | 114 +++++++++++++++++++++++++++-------------------- 2 files changed, 76 insertions(+), 48 deletions(-) diff --git a/ChangeLog b/ChangeLog index 383d20543f..e1aefa7a04 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Fri Feb 21 21:03:41 2003 Minero Aoki + + * lib/fileutils.rb: new method FileUtils#copy_stream. + + * lib/fileutils.rb: new method FileUtils#compare_file. + + * lib/fileutils.rb: new method FileUtils#compare_stream. + + * lib/fileutils.rb: new method FileUtils#rmtree (alias of rm_rf). + Fri Feb 21 17:19:27 2003 WATANABE Hirofumi * eval.c (rb_f_require): do not need to abort if a DLEXT file diff --git a/lib/fileutils.rb b/lib/fileutils.rb index e42b74f9ae..775b2bb535 100644 --- a/lib/fileutils.rb +++ b/lib/fileutils.rb @@ -14,7 +14,7 @@ # # cd( dir, *options ) # cd( dir, *options ) {|dir| .... } -# uptodate?( newer, older_list, *options ) +# pwd() # mkdir( dir, *options ) # mkdir_p( dir, *options ) # rmdir( dir, *options ) @@ -32,7 +32,6 @@ # rm( list, *options ) # rm_r( list, *options ) # rm_rf( list, *options ) -# cmp( file_a, file_b, *options ) # install( src, dest, mode = , *options ) # chmod( mode, list, *options ) # touch( list, *options ) @@ -46,6 +45,14 @@ # either one file or a list of files in that argument. See the method # documentation for examples. # +# There are some `low level' methods, which does not accept any option: +# +# uptodate?( file, cmp_list ) +# copy_file( srcfilename, destfilename ) +# copy_stream( srcstream, deststream ) +# compare_file( file_a, file_b ) +# compare_stream( stream_a, stream_b ) +# # == module FileUtils::Verbose # # This module has all methods of FileUtils module, but it outputs messages @@ -88,7 +95,7 @@ module FileUtils def cd( dir, *options, &block ) # :yield: dir noop, verbose, = fu_parseargs(options, :noop, :verbose) fu_output_message "cd #{dir}" if verbose - Dir.chdir dir, &block unless noop + Dir.chdir(dir, &block) unless noop fu_output_message 'cd -' if verbose and block end @@ -380,19 +387,30 @@ module FileUtils end private :fu_preserve_attr - def copy_file( src, dest ) #:nodoc: - bsize = fu_blksize(File.stat(src).blksize) + # + # Copies file +src+ to +dest+. + # Both of +src+ and +dest+ must be a filename. + # + def copy_file( src, dest ) File.open(src, 'rb') {|r| File.open(dest, 'wb') {|w| - begin - while true - w.syswrite r.sysread(bsize) - end - rescue EOFError - end + copy_stream r, w } } end + # + # Copies stream +src+ to +dest+. + # Both of +src+ and +dest+ must be a IO. + # + def copy_stream( src, dest ) + bsize = fu_stream_blksize(src, dest) + begin + while true + dest.syswrite src.sysread(bsize) + end + rescue EOFError + end + end # # Options: noop verbose @@ -521,6 +539,8 @@ module FileUtils rm_r list, :force, *options end + alias rmtree rm_rf + def remove_file( fname, force = false ) #:nodoc: first_time_p = true begin @@ -557,42 +577,40 @@ module FileUtils # - # Options: (none) - # # Returns true if the contents of a file A and a file B are identical. # - # FileUtils.cmp 'somefile', 'somefile' #=> true - # FileUtils.cmp '/bin/cp', '/bin/mv' #=> maybe false + # FileUtils.compare_file('somefile', 'somefile') #=> true + # FileUtils.compare_file('/bin/cp', '/bin/mv') #=> maybe false # - def cmp( filea, fileb, *options ) - raise ArgumentError, 'cmp does not accept any option' unless options.empty? - - sa = sb = nil - st = File.stat(filea) - bsize = fu_blksize(st.blksize) - return false unless File.size(fileb) == st.size - - File.open(filea, 'rb') {|a| - File.open(fileb, 'rb') {|b| - begin - while sa == sb - sa = a.read(bsize) - sb = b.read(bsize) - unless sa and sb - if sa.nil? and sb.nil? - return true - end - end - end - rescue EOFError - ; - end + def compare_file( a, b ) + return false unless File.size(a) == File.size(b) + File.open(a, 'rb') {|fa| + File.open(b, 'rb') {|fb| + return compare_stream(fa, fb) } } + end + alias identical? compare_file + alias cmp compare_file + + # + # Returns true if the contents of a stream +a+ and +b+ are identical. + # + def compare_stream( a, b ) + bsize = fu_stream_blksize(a, b) + sa = sb = nil + while sa == sb + sa = a.read(bsize) + sb = b.read(bsize) + unless sa and sb + if sa.nil? and sb.nil? + return true + end + end + end false end - alias identical? cmp # # Options: noop verbose @@ -636,6 +654,7 @@ module FileUtils File.chmod mode, *list end + # # Options: noop verbose # @@ -702,12 +721,13 @@ module FileUtils end end - def fu_blksize( size ) - if size.nil? or size.zero? - 2048 - else - size + def fu_stream_blksize( *streams ) + streams.each do |s| + next unless s.respond_to?(:stat) + size = s.stat.blksize + return size unless size.nil? and size.zero? end + 1024 end @@ -729,11 +749,9 @@ module FileUtils 'cd' => %w( noop verbose ), 'chdir' => %w( noop verbose ), 'chmod' => %w( noop verbose ), - 'cmp' => %w(), 'copy' => %w( preserve noop verbose ), 'cp' => %w( preserve noop verbose ), 'cp_r' => %w( preserve noop verbose ), - 'identical?' => %w(), 'install' => %w( noop verbose ), 'link' => %w( force noop verbose ), 'ln' => %w( force noop verbose ), @@ -750,11 +768,11 @@ module FileUtils 'rm_f' => %w( noop verbose ), 'rm_r' => %w( force noop verbose ), 'rm_rf' => %w( noop verbose ), + 'rmtree' => %w( noop verbose ), 'rmdir' => %w( noop verbose ), 'safe_unlink' => %w( noop verbose ), 'symlink' => %w( force noop verbose ), - 'touch' => %w( noop verbose ), - 'uptodate?' => %w() + 'touch' => %w( noop verbose ) }