зеркало из https://github.com/github/ruby.git
* 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:
Родитель
e313b6a9f1
Коммит
98cc039220
|
@ -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>
|
Tue Apr 19 23:02:40 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* eval.c (search_required): deal with features with path too.
|
* eval.c (search_required): deal with features with path too.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#
|
#
|
||||||
# = ftools.rb: Extra tools for the File class
|
# = ftools.rb: Extra tools for the File class
|
||||||
#
|
#
|
||||||
# Author:: WANTANABE, Hirofumi
|
# Author:: WATANABE, Hirofumi
|
||||||
# Documentation:: Zachary Landau
|
# Documentation:: Zachary Landau
|
||||||
#
|
#
|
||||||
# This library can be distributed under the terms of the Ruby license.
|
# This library can be distributed under the terms of the Ruby license.
|
||||||
|
@ -45,8 +45,8 @@ class << File
|
||||||
# in +to+.
|
# in +to+.
|
||||||
#
|
#
|
||||||
def catname(from, to)
|
def catname(from, to)
|
||||||
if FileTest.directory? to
|
if directory? to
|
||||||
File.join to.sub(%r([/\\]$), ''), basename(from)
|
join to.sub(%r([/\\]$), ''), basename(from)
|
||||||
else
|
else
|
||||||
to
|
to
|
||||||
end
|
end
|
||||||
|
@ -88,7 +88,7 @@ class << File
|
||||||
# is printed.
|
# is printed.
|
||||||
#
|
#
|
||||||
def copy(from, to, verbose = false)
|
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
|
syscopy from, to
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -101,9 +101,9 @@ class << File
|
||||||
#
|
#
|
||||||
def move(from, to, verbose = false)
|
def move(from, to, verbose = false)
|
||||||
to = catname(from, to)
|
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
|
unlink to
|
||||||
end
|
end
|
||||||
fstat = stat(from)
|
fstat = stat(from)
|
||||||
|
@ -111,7 +111,7 @@ class << File
|
||||||
rename from, to
|
rename from, to
|
||||||
rescue
|
rescue
|
||||||
begin
|
begin
|
||||||
symlink File.readlink(from), to and unlink from
|
symlink readlink(from), to and unlink from
|
||||||
rescue
|
rescue
|
||||||
from_stat = stat(from)
|
from_stat = stat(from)
|
||||||
syscopy from, to and unlink from
|
syscopy from, to and unlink from
|
||||||
|
@ -131,7 +131,7 @@ class << File
|
||||||
# identical. If +verbose+ is +true+, <tt>from <=> to</tt> is printed.
|
# identical. If +verbose+ is +true+, <tt>from <=> to</tt> is printed.
|
||||||
#
|
#
|
||||||
def compare(from, to, verbose = false)
|
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
|
return false if stat(from).size != stat(to).size
|
||||||
|
|
||||||
|
@ -170,12 +170,22 @@ class << File
|
||||||
#
|
#
|
||||||
def safe_unlink(*files)
|
def safe_unlink(*files)
|
||||||
verbose = if files[-1].is_a? String then false else files.pop end
|
verbose = if files[-1].is_a? String then false else files.pop end
|
||||||
begin
|
files.each do |file|
|
||||||
$deferr.print files.join(" "), "\n" if verbose
|
begin
|
||||||
chmod 0777, *files
|
unlink file
|
||||||
unlink(*files)
|
$stderr.print "removing ", file, "\n" if verbose
|
||||||
rescue
|
rescue Errno::EACCES # for Windows
|
||||||
# $deferr.print "warning: Couldn't unlink #{files.join ' '}\n"
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -197,18 +207,17 @@ class << File
|
||||||
#
|
#
|
||||||
def makedirs(*dirs)
|
def makedirs(*dirs)
|
||||||
verbose = if dirs[-1].is_a? String then false else dirs.pop end
|
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
|
mode = 0755
|
||||||
for dir in dirs
|
for dir in dirs
|
||||||
parent = dirname(dir)
|
parent = dirname(dir)
|
||||||
next if parent == dir or FileTest.directory? dir
|
next if parent == dir or directory? dir
|
||||||
makedirs parent unless FileTest.directory? parent
|
makedirs parent unless directory? parent
|
||||||
$deferr.print "mkdir ", dir, "\n" if verbose
|
$stderr.print "mkdir ", dir, "\n" if verbose
|
||||||
if basename(dir) != ""
|
if basename(dir) != ""
|
||||||
begin
|
begin
|
||||||
Dir.mkdir dir, mode
|
Dir.mkdir dir, mode
|
||||||
rescue SystemCallError
|
rescue SystemCallError
|
||||||
raise unless File.directory? dir
|
raise unless directory? dir
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -230,7 +239,7 @@ class << File
|
||||||
#
|
#
|
||||||
def chmod(mode, *files)
|
def chmod(mode, *files)
|
||||||
verbose = if files[-1].is_a? String then false else files.pop end
|
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
|
o_chmod mode, *files
|
||||||
end
|
end
|
||||||
$VERBOSE = vsave
|
$VERBOSE = vsave
|
||||||
|
@ -243,8 +252,8 @@ class << File
|
||||||
#
|
#
|
||||||
def install(from, to, mode = nil, verbose = false)
|
def install(from, to, mode = nil, verbose = false)
|
||||||
to = catname(from, to)
|
to = catname(from, to)
|
||||||
unless FileTest.exist? to and cmp from, to
|
unless exist? to and cmp from, to
|
||||||
safe_unlink to if FileTest.exist? to
|
safe_unlink to if exist? to
|
||||||
cp from, to, verbose
|
cp from, to, verbose
|
||||||
chmod mode, to, verbose if mode
|
chmod mode, to, verbose if mode
|
||||||
end
|
end
|
||||||
|
|
Загрузка…
Ссылка в новой задаче