mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* lib/ftools.rb (File.safe_unlink): do not modify a symlinked file.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8355 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									e313b6a9f1
								
							
						
					
					
						commit
						98cc039220
					
				
					 2 changed files with 35 additions and 22 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,7 @@
 | 
			
		|||
Tue Apr 19 23:37:09 2005  WATANABE Hirofumi  <eban@ruby-lang.org>
 | 
			
		||||
 | 
			
		||||
	* lib/ftools.rb (File.safe_unlink): do not modify a symlinked file.
 | 
			
		||||
 | 
			
		||||
Tue Apr 19 23:02:40 2005  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		||||
 | 
			
		||||
	* eval.c (search_required): deal with features with path too.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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+, <tt>from <=> to</tt> 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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue