From a5d09ee942fe078c5b8f63ec7d2462253687fefe Mon Sep 17 00:00:00 2001 From: eban Date: Tue, 19 Apr 2005 14:49:43 +0000 Subject: * lib/ftools.rb (File.safe_unlink): do not modify a symlinked file. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@8355 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/ftools.rb | 53 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/ftools.rb b/lib/ftools.rb index 0441020b05..a99c7e713f 100644 --- a/lib/ftools.rb +++ b/lib/ftools.rb @@ -1,7 +1,7 @@ # # = ftools.rb: Extra tools for the File class # -# Author:: WANTANABE, Hirofumi +# Author:: WATANABE, Hirofumi # Documentation:: Zachary Landau # # This library can be distributed under the terms of the Ruby license. @@ -45,8 +45,8 @@ class << File # in +to+. # def catname(from, to) - if FileTest.directory? to - File.join to.sub(%r([/\\]$), ''), basename(from) + if directory? to + join to.sub(%r([/\\]$), ''), basename(from) else to end @@ -88,7 +88,7 @@ class << File # is printed. # def copy(from, to, verbose = false) - $deferr.print from, " -> ", catname(from, to), "\n" if verbose + $stderr.print from, " -> ", catname(from, to), "\n" if verbose syscopy from, to end @@ -101,9 +101,9 @@ class << File # def move(from, to, verbose = false) to = catname(from, to) - $deferr.print from, " -> ", to, "\n" if verbose + $stderr.print from, " -> ", to, "\n" if verbose - if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and FileTest.file? to + if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to unlink to end fstat = stat(from) @@ -111,7 +111,7 @@ class << File rename from, to rescue begin - symlink File.readlink(from), to and unlink from + symlink readlink(from), to and unlink from rescue from_stat = stat(from) syscopy from, to and unlink from @@ -131,7 +131,7 @@ class << File # identical. If +verbose+ is +true+, from <=> to is printed. # def compare(from, to, verbose = false) - $deferr.print from, " <=> ", to, "\n" if verbose + $stderr.print from, " <=> ", to, "\n" if verbose return false if stat(from).size != stat(to).size @@ -170,12 +170,22 @@ class << File # def safe_unlink(*files) verbose = if files[-1].is_a? String then false else files.pop end - begin - $deferr.print files.join(" "), "\n" if verbose - chmod 0777, *files - unlink(*files) - rescue -# $deferr.print "warning: Couldn't unlink #{files.join ' '}\n" + files.each do |file| + begin + unlink file + $stderr.print "removing ", file, "\n" if verbose + rescue Errno::EACCES # for Windows + continue if symlink? file + begin + mode = stat(file).mode + o_chmod mode | 0200, file + unlink file + $stderr.print "removing ", file, "\n" if verbose + rescue + o_chmod mode, file rescue nil + end + rescue + end end end @@ -197,18 +207,17 @@ class << File # def makedirs(*dirs) verbose = if dirs[-1].is_a? String then false else dirs.pop end -# mode = if dirs[-1].is_a? Fixnum then dirs.pop else 0755 end mode = 0755 for dir in dirs parent = dirname(dir) - next if parent == dir or FileTest.directory? dir - makedirs parent unless FileTest.directory? parent - $deferr.print "mkdir ", dir, "\n" if verbose + next if parent == dir or directory? dir + makedirs parent unless directory? parent + $stderr.print "mkdir ", dir, "\n" if verbose if basename(dir) != "" begin Dir.mkdir dir, mode rescue SystemCallError - raise unless File.directory? dir + raise unless directory? dir end end end @@ -230,7 +239,7 @@ class << File # def chmod(mode, *files) verbose = if files[-1].is_a? String then false else files.pop end - $deferr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose + $stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose o_chmod mode, *files end $VERBOSE = vsave @@ -243,8 +252,8 @@ class << File # def install(from, to, mode = nil, verbose = false) to = catname(from, to) - unless FileTest.exist? to and cmp from, to - safe_unlink to if FileTest.exist? to + unless exist? to and cmp from, to + safe_unlink to if exist? to cp from, to, verbose chmod mode, to, verbose if mode end -- cgit v1.2.3