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:
parent
67f14c022b
commit
370e4a2c47
1 changed files with 54 additions and 44 deletions
|
@ -269,7 +269,7 @@ module FileUtils
|
|||
return false unless FileTest.exist? new
|
||||
new_time = File.ctime(new)
|
||||
args.each do |old|
|
||||
if FileTest.exist? old then
|
||||
if FileTest.exist? old
|
||||
return false unless new_time > File.mtime(old)
|
||||
end
|
||||
end
|
||||
|
@ -294,14 +294,14 @@ module FileUtils
|
|||
fu_output_message "mkdir -p #{list.join ' '}" if verbose
|
||||
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 = []
|
||||
until FileTest.directory? dir do
|
||||
until FileTest.directory? dir
|
||||
stack.push dir
|
||||
dir = File.dirname(dir)
|
||||
end
|
||||
stack.reverse_each do |dir|
|
||||
Dir.mkdir dir
|
||||
stack.reverse_each do |n|
|
||||
Dir.mkdir n
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -376,7 +376,7 @@ module FileUtils
|
|||
return if noop
|
||||
|
||||
fu_each_src_dest(src, dest) do |s,d|
|
||||
if FileTest.directory? s then
|
||||
if FileTest.directory? s
|
||||
fu_copy_dir s, d, '.', preserve
|
||||
else
|
||||
fu_p_copy s, d, preserve
|
||||
|
@ -391,7 +391,7 @@ module FileUtils
|
|||
Dir.mkdir dir unless FileTest.directory? dir
|
||||
}
|
||||
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
|
||||
fu_copy_dir src, dest, "#{rel}/#{fn}", preserve
|
||||
else
|
||||
|
@ -409,7 +409,7 @@ module FileUtils
|
|||
private :fu_p_copy
|
||||
|
||||
def fu_preserve_attr( really, src, dest )
|
||||
unless really then
|
||||
unless really
|
||||
yield src, dest
|
||||
return
|
||||
end
|
||||
|
@ -418,7 +418,7 @@ module FileUtils
|
|||
yield src, dest
|
||||
File.utime st.atime, st.mtime, dest
|
||||
begin
|
||||
File.chown st.uid, st.gid
|
||||
File.chown st.uid, st.gid, dest
|
||||
rescue Errno::EPERM
|
||||
File.chmod st.mode & 01777, dest # clear setuid/setgid
|
||||
else
|
||||
|
@ -428,14 +428,12 @@ module FileUtils
|
|||
private :fu_preserve_attr
|
||||
|
||||
def copy_file( src, dest )
|
||||
st = r = w = nil
|
||||
|
||||
bsize = fu_blksize(File.stat(src).blksize)
|
||||
File.open(src, 'rb') {|r|
|
||||
File.open(dest, 'wb') {|w|
|
||||
st = r.stat
|
||||
begin
|
||||
while true do
|
||||
w.write r.sysread(st.blksize)
|
||||
while true
|
||||
w.syswrite r.sysread(bsize)
|
||||
end
|
||||
rescue EOFError
|
||||
end
|
||||
|
@ -449,15 +447,14 @@ module FileUtils
|
|||
return if noop
|
||||
|
||||
fu_each_src_dest(src, dest) do |s,d|
|
||||
if /djgpp|cygwin|mswin32/ === RUBY_PLATFORM and
|
||||
FileTest.file? d then
|
||||
if cannot_overwrite_file? and FileTest.file? d
|
||||
File.unlink d
|
||||
end
|
||||
|
||||
begin
|
||||
File.rename s, d
|
||||
rescue
|
||||
if FileTest.symlink? s then
|
||||
if FileTest.symlink? s
|
||||
File.symlink File.readlink(s), dest
|
||||
File.unlink s
|
||||
else
|
||||
|
@ -477,6 +474,10 @@ module FileUtils
|
|||
|
||||
alias move mv
|
||||
|
||||
def cannot_overwrite_file?
|
||||
/djgpp|cygwin|mswin32/ === RUBY_PLATFORM
|
||||
end
|
||||
|
||||
|
||||
def rm( list, *options )
|
||||
force, noop, verbose, = fu_parseargs(options, :force, :noop, :verbose)
|
||||
|
@ -531,7 +532,7 @@ module FileUtils
|
|||
rescue
|
||||
# rescue dos?
|
||||
begin
|
||||
if first then
|
||||
if first
|
||||
first = false
|
||||
File.chmod 0777, fn
|
||||
retry
|
||||
|
@ -563,16 +564,17 @@ module FileUtils
|
|||
|
||||
sa = sb = nil
|
||||
st = File.stat(filea)
|
||||
bsize = fu_blksize(st.blksize)
|
||||
File.size(fileb) == st.size or return true
|
||||
|
||||
File.open(filea, 'rb') {|a|
|
||||
File.open(fileb, 'rb') {|b|
|
||||
begin
|
||||
while sa == sb do
|
||||
sa = a.read( st.blksize )
|
||||
sb = b.read( st.blksize )
|
||||
unless sa and sb then
|
||||
if sa.nil? and sb.nil? then
|
||||
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
|
||||
|
@ -593,7 +595,7 @@ module FileUtils
|
|||
return if noop
|
||||
|
||||
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
|
||||
copy_file s, d
|
||||
File.chmod mode, d if mode
|
||||
|
@ -631,10 +633,10 @@ module FileUtils
|
|||
|
||||
def fu_parseargs( opts, *flagdecl )
|
||||
tab = {}
|
||||
if opts.last == true or opts.last == false then
|
||||
if opts.last == true or opts.last == false
|
||||
tab[:verbose] = opts.pop
|
||||
end
|
||||
while Symbol === opts.last do
|
||||
while Symbol === opts.last
|
||||
tab[opts.pop] = true
|
||||
end
|
||||
|
||||
|
@ -650,7 +652,7 @@ module FileUtils
|
|||
end
|
||||
|
||||
def fu_each_src_dest( src, dest )
|
||||
unless Array === src then
|
||||
unless Array === src
|
||||
yield src, fu_dest_filename(src, dest)
|
||||
else
|
||||
dir = dest
|
||||
|
@ -663,13 +665,21 @@ module FileUtils
|
|||
end
|
||||
|
||||
def fu_dest_filename( src, dest )
|
||||
if FileTest.directory? dest then
|
||||
if FileTest.directory? dest
|
||||
(dest[-1,1] == '/' ? dest : dest + '/') + File.basename(src)
|
||||
else
|
||||
dest
|
||||
end
|
||||
end
|
||||
|
||||
def fu_blksize( size )
|
||||
if size.nil? or size.zero?
|
||||
2048
|
||||
else
|
||||
size
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@fileutils_output = $stderr
|
||||
@fileutils_label = 'fileutils.'
|
||||
|
@ -729,7 +739,7 @@ module FileUtils
|
|||
next unless opts.include? 'verbose'
|
||||
module_eval <<-End, __FILE__, __LINE__ + 1
|
||||
def #{name}( *args )
|
||||
unless defined? @fileutils_verbose then
|
||||
unless defined? @fileutils_verbose
|
||||
@fileutils_verbose = true
|
||||
end
|
||||
args.push :verbose if @fileutils_verbose
|
||||
|
@ -755,7 +765,7 @@ module FileUtils
|
|||
next unless opts.include? 'noop'
|
||||
module_eval <<-End, __FILE__, __LINE__ + 1
|
||||
def #{name}( *args )
|
||||
unless defined? @fileutils_nowrite then
|
||||
unless defined? @fileutils_nowrite
|
||||
@fileutils_nowrite = true
|
||||
end
|
||||
args.push :noop if @fileutils_nowrite
|
||||
|
|
Loading…
Reference in a new issue