1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/fileutils.rb: stat.blksize might become 0/nil.

* lib/fileutils.rb: change coding style.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2962 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2002-10-17 03:54:25 +00:00
parent 67f14c022b
commit 370e4a2c47

View file

@ -269,7 +269,7 @@ module FileUtils
return false unless FileTest.exist? new return false unless FileTest.exist? new
new_time = File.ctime(new) new_time = File.ctime(new)
args.each do |old| args.each do |old|
if FileTest.exist? old then if FileTest.exist? old
return false unless new_time > File.mtime(old) return false unless new_time > File.mtime(old)
end end
end end
@ -294,14 +294,14 @@ module FileUtils
fu_output_message "mkdir -p #{list.join ' '}" if verbose fu_output_message "mkdir -p #{list.join ' '}" if verbose
return *list if noop return *list if noop
list.collect {|n| File.expand_path(n) }.each do |dir| list.map {|n| File.expand_path(n) }.each do |dir|
stack = [] stack = []
until FileTest.directory? dir do until FileTest.directory? dir
stack.push dir stack.push dir
dir = File.dirname(dir) dir = File.dirname(dir)
end end
stack.reverse_each do |dir| stack.reverse_each do |n|
Dir.mkdir dir Dir.mkdir n
end end
end end
@ -329,7 +329,7 @@ module FileUtils
fu_output_message "ln #{argv.join ' '}" if verbose fu_output_message "ln #{argv.join ' '}" if verbose
return if noop return if noop
fu_each_src_dest( src, dest ) do |s,d| fu_each_src_dest(src, dest) do |s,d|
remove_file d, true if force remove_file d, true if force
File.link s, d File.link s, d
end end
@ -342,7 +342,7 @@ module FileUtils
fu_output_message "ln -s#{force ? 'f' : ''} #{[src,dest].flatten.join ' '}" if verbose fu_output_message "ln -s#{force ? 'f' : ''} #{[src,dest].flatten.join ' '}" if verbose
return if noop return if noop
fu_each_src_dest( src, dest ) do |s,d| fu_each_src_dest(src, dest) do |s,d|
remove_file d, true if force remove_file d, true if force
File.symlink s, d File.symlink s, d
end end
@ -361,7 +361,7 @@ module FileUtils
fu_output_message "cp#{preserve ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if verbose fu_output_message "cp#{preserve ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if verbose
return if noop return if noop
fu_each_src_dest( src, dest ) do |s,d| fu_each_src_dest(src, dest) do |s,d|
fu_preserve_attr(preserve, s, d) { fu_preserve_attr(preserve, s, d) {
copy_file s, d copy_file s, d
} }
@ -375,8 +375,8 @@ module FileUtils
fu_output_message "cp -r #{[src,dest].flatten.join ' '}" if verbose fu_output_message "cp -r #{[src,dest].flatten.join ' '}" if verbose
return if noop return if noop
fu_each_src_dest( src, dest ) do |s,d| fu_each_src_dest(src, dest) do |s,d|
if FileTest.directory? s then if FileTest.directory? s
fu_copy_dir s, d, '.', preserve fu_copy_dir s, d, '.', preserve
else else
fu_p_copy s, d, preserve fu_p_copy s, d, preserve
@ -391,7 +391,7 @@ module FileUtils
Dir.mkdir dir unless FileTest.directory? dir Dir.mkdir dir unless FileTest.directory? dir
} }
Dir.entries( "#{src}/#{rel}" ).each do |fn| Dir.entries( "#{src}/#{rel}" ).each do |fn|
if FileTest.directory? File.join(src,rel,fn) then if FileTest.directory? File.join(src,rel,fn)
next if /\A\.\.?\z/ === fn next if /\A\.\.?\z/ === fn
fu_copy_dir src, dest, "#{rel}/#{fn}", preserve fu_copy_dir src, dest, "#{rel}/#{fn}", preserve
else else
@ -409,7 +409,7 @@ module FileUtils
private :fu_p_copy private :fu_p_copy
def fu_preserve_attr( really, src, dest ) def fu_preserve_attr( really, src, dest )
unless really then unless really
yield src, dest yield src, dest
return return
end end
@ -418,7 +418,7 @@ module FileUtils
yield src, dest yield src, dest
File.utime st.atime, st.mtime, dest File.utime st.atime, st.mtime, dest
begin begin
File.chown st.uid, st.gid File.chown st.uid, st.gid, dest
rescue Errno::EPERM rescue Errno::EPERM
File.chmod st.mode & 01777, dest # clear setuid/setgid File.chmod st.mode & 01777, dest # clear setuid/setgid
else else
@ -428,14 +428,12 @@ module FileUtils
private :fu_preserve_attr private :fu_preserve_attr
def copy_file( src, dest ) def copy_file( src, dest )
st = r = w = nil bsize = fu_blksize(File.stat(src).blksize)
File.open(src, 'rb') {|r|
File.open( src, 'rb' ) {|r| File.open(dest, 'wb') {|w|
File.open( dest, 'wb' ) {|w|
st = r.stat
begin begin
while true do while true
w.write r.sysread(st.blksize) w.syswrite r.sysread(bsize)
end end
rescue EOFError rescue EOFError
end end
@ -448,16 +446,15 @@ module FileUtils
fu_output_message "mv #{[src,dest].flatten.join ' '}" if verbose fu_output_message "mv #{[src,dest].flatten.join ' '}" if verbose
return if noop return if noop
fu_each_src_dest( src, dest ) do |s,d| fu_each_src_dest(src, dest) do |s,d|
if /djgpp|cygwin|mswin32/ === RUBY_PLATFORM and if cannot_overwrite_file? and FileTest.file? d
FileTest.file? d then File.unlink d
File.unlink d
end end
begin begin
File.rename s, d File.rename s, d
rescue rescue
if FileTest.symlink? s then if FileTest.symlink? s
File.symlink File.readlink(s), dest File.symlink File.readlink(s), dest
File.unlink s File.unlink s
else else
@ -477,6 +474,10 @@ module FileUtils
alias move mv alias move mv
def cannot_overwrite_file?
/djgpp|cygwin|mswin32/ === RUBY_PLATFORM
end
def rm( list, *options ) def rm( list, *options )
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose) force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
@ -531,7 +532,7 @@ module FileUtils
rescue rescue
# rescue dos? # rescue dos?
begin begin
if first then if first
first = false first = false
File.chmod 0777, fn File.chmod 0777, fn
retry retry
@ -542,7 +543,7 @@ module FileUtils
end end
def remove_dir( dir, force = false ) def remove_dir( dir, force = false )
Dir.foreach( dir ) do |file| Dir.foreach(dir) do |file|
next if /\A\.\.?\z/ === file next if /\A\.\.?\z/ === file
path = "#{dir}/#{file}" path = "#{dir}/#{file}"
if FileTest.directory? path then remove_dir path, force if FileTest.directory? path then remove_dir path, force
@ -563,16 +564,17 @@ module FileUtils
sa = sb = nil sa = sb = nil
st = File.stat(filea) st = File.stat(filea)
bsize = fu_blksize(st.blksize)
File.size(fileb) == st.size or return true File.size(fileb) == st.size or return true
File.open( filea, 'rb' ) {|a| File.open(filea, 'rb') {|a|
File.open( fileb, 'rb' ) {|b| File.open(fileb, 'rb') {|b|
begin begin
while sa == sb do while sa == sb
sa = a.read( st.blksize ) sa = a.read(bsize)
sb = b.read( st.blksize ) sb = b.read(bsize)
unless sa and sb then unless sa and sb
if sa.nil? and sb.nil? then if sa.nil? and sb.nil?
return true return true
end end
end end
@ -593,7 +595,7 @@ module FileUtils
return if noop return if noop
fu_each_src_dest( src, dest ) do |s,d| fu_each_src_dest( src, dest ) do |s,d|
unless FileTest.exist? d and cmp(s,d) then unless FileTest.exist? d and cmp(s,d)
remove_file d, true remove_file d, true
copy_file s, d copy_file s, d
File.chmod mode, d if mode File.chmod mode, d if mode
@ -631,10 +633,10 @@ module FileUtils
def fu_parseargs( opts, *flagdecl ) def fu_parseargs( opts, *flagdecl )
tab = {} tab = {}
if opts.last == true or opts.last == false then if opts.last == true or opts.last == false
tab[:verbose] = opts.pop tab[:verbose] = opts.pop
end end
while Symbol === opts.last do while Symbol === opts.last
tab[opts.pop] = true tab[opts.pop] = true
end end
@ -650,7 +652,7 @@ module FileUtils
end end
def fu_each_src_dest( src, dest ) def fu_each_src_dest( src, dest )
unless Array === src then unless Array === src
yield src, fu_dest_filename(src, dest) yield src, fu_dest_filename(src, dest)
else else
dir = dest dir = dest
@ -663,13 +665,21 @@ module FileUtils
end end
def fu_dest_filename( src, dest ) def fu_dest_filename( src, dest )
if FileTest.directory? dest then if FileTest.directory? dest
(dest[-1,1] == '/' ? dest : dest + '/') + File.basename(src) (dest[-1,1] == '/' ? dest : dest + '/') + File.basename(src)
else else
dest dest
end end
end end
def fu_blksize( size )
if size.nil? or size.zero?
2048
else
size
end
end
@fileutils_output = $stderr @fileutils_output = $stderr
@fileutils_label = 'fileutils.' @fileutils_label = 'fileutils.'
@ -729,11 +739,11 @@ module FileUtils
next unless opts.include? 'verbose' next unless opts.include? 'verbose'
module_eval <<-End, __FILE__, __LINE__ + 1 module_eval <<-End, __FILE__, __LINE__ + 1
def #{name}( *args ) def #{name}( *args )
unless defined? @fileutils_verbose then unless defined? @fileutils_verbose
@fileutils_verbose = true @fileutils_verbose = true
end end
args.push :verbose if @fileutils_verbose args.push :verbose if @fileutils_verbose
super( *args ) super(*args)
end end
End End
end end
@ -755,11 +765,11 @@ module FileUtils
next unless opts.include? 'noop' next unless opts.include? 'noop'
module_eval <<-End, __FILE__, __LINE__ + 1 module_eval <<-End, __FILE__, __LINE__ + 1
def #{name}( *args ) def #{name}( *args )
unless defined? @fileutils_nowrite then unless defined? @fileutils_nowrite
@fileutils_nowrite = true @fileutils_nowrite = true
end end
args.push :noop if @fileutils_nowrite args.push :noop if @fileutils_nowrite
super( *args ) super(*args)
end end
End End
end end
@ -790,7 +800,7 @@ module FileUtils
module_eval <<-End, __FILE__, __LINE__ + 1 module_eval <<-End, __FILE__, __LINE__ + 1
def #{name}( *args ) def #{name}( *args )
#{s} #{s}
super( *args ) super(*args)
end end
End End
end end