mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/pathname.rb (realpath): make ELOOP check bit more robust.
(children): prepend self by default. (chroot): obsoleted. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
345f1ac53d
commit
839ef7bfbb
2 changed files with 45 additions and 9 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Tue Oct 14 04:43:55 2003 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
|
* lib/pathname.rb (realpath): make ELOOP check bit more robust.
|
||||||
|
(children): prepend self by default.
|
||||||
|
(chroot): obsoleted.
|
||||||
|
|
||||||
Tue Oct 14 02:29:31 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Tue Oct 14 02:29:31 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* eval.c (rb_require_safe): segfault after loading .so.
|
* eval.c (rb_require_safe): segfault after loading .so.
|
||||||
|
|
|
@ -111,11 +111,20 @@ class Pathname
|
||||||
# it may return relative pathname.
|
# it may return relative pathname.
|
||||||
# Otherwise it returns absolute pathname.
|
# Otherwise it returns absolute pathname.
|
||||||
def realpath(force_absolute=true)
|
def realpath(force_absolute=true)
|
||||||
|
# Check file existence at first by File.stat.
|
||||||
|
# This test detects ELOOP.
|
||||||
|
#
|
||||||
|
# /tmp/a -> a
|
||||||
|
# /tmp/b -> b/b
|
||||||
|
# /tmp/c -> ./c
|
||||||
|
# /tmp/d -> ../tmp/d
|
||||||
|
|
||||||
File.stat(@path)
|
File.stat(@path)
|
||||||
|
|
||||||
top = %r{\A/} =~ @path ? '/' : ''
|
top = %r{\A/} =~ @path ? '/' : ''
|
||||||
unresolved = @path.scan(%r{[^/]+})
|
unresolved = @path.scan(%r{[^/]+})
|
||||||
resolved = []
|
resolved = []
|
||||||
|
checked_path = {}
|
||||||
|
|
||||||
until unresolved.empty?
|
until unresolved.empty?
|
||||||
case unresolved.last
|
case unresolved.last
|
||||||
|
@ -125,7 +134,9 @@ class Pathname
|
||||||
resolved.unshift unresolved.pop
|
resolved.unshift unresolved.pop
|
||||||
else
|
else
|
||||||
path = top + unresolved.join('/')
|
path = top + unresolved.join('/')
|
||||||
if FileTest.symlink? path
|
raise Errno::ELOOP.new(path) if checked_path[path]
|
||||||
|
checked_path[path] = true
|
||||||
|
if File.lstat(path).symlink?
|
||||||
link = File.readlink(path)
|
link = File.readlink(path)
|
||||||
if %r{\A/} =~ link
|
if %r{\A/} =~ link
|
||||||
top = '/'
|
top = '/'
|
||||||
|
@ -211,14 +222,27 @@ class Pathname
|
||||||
end
|
end
|
||||||
|
|
||||||
# Pathname#children returns the children of the directory as an array of
|
# Pathname#children returns the children of the directory as an array of
|
||||||
# pathnames. I.e. it is similar to Pathname#entries except '.' and '..'
|
# pathnames.
|
||||||
# is not returned.
|
#
|
||||||
|
# By default, self is prepended to each pathname in the result.
|
||||||
|
# It is disabled if false is given for the optional argument
|
||||||
|
# prepend_directory.
|
||||||
|
#
|
||||||
|
# Note that the result never contain '.' and '..' because they are not
|
||||||
|
# child.
|
||||||
#
|
#
|
||||||
# This method is exist since 1.8.1.
|
# This method is exist since 1.8.1.
|
||||||
def children
|
def children(prepend_directory=true)
|
||||||
Dir.entries(@path).map {|f|
|
result = []
|
||||||
f == '.' || f == '..' ? nil : Pathname.new(f)
|
Dir.foreach(@path) {|e|
|
||||||
}.compact
|
next if e == '.' || e == '..'
|
||||||
|
if prepend_directory
|
||||||
|
result << Pathname.new(File.join(@path, e))
|
||||||
|
else
|
||||||
|
result << Pathname.new(e)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
# Pathname#relative_path_from returns a relative path from the argument to
|
# Pathname#relative_path_from returns a relative path from the argument to
|
||||||
|
@ -355,14 +379,20 @@ class Pathname
|
||||||
def Pathname.getwd() Pathname.new(Dir.getwd) end
|
def Pathname.getwd() Pathname.new(Dir.getwd) end
|
||||||
class << self; alias pwd getwd end
|
class << self; alias pwd getwd end
|
||||||
|
|
||||||
# This method is obsoleted at 1.8.1.
|
# Pathname#chdir is obsoleted at 1.8.1.
|
||||||
#
|
#
|
||||||
def chdir(&block) # compatibility to 1.8.0.
|
def chdir(&block) # compatibility to 1.8.0.
|
||||||
warn "Pathname#chdir is obsoleted. Use Dir.chdir."
|
warn "Pathname#chdir is obsoleted. Use Dir.chdir."
|
||||||
Dir.chdir(@path, &block)
|
Dir.chdir(@path, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def chroot() Dir.chroot(@path) end
|
# Pathname#chroot is obsoleted at 1.8.1.
|
||||||
|
#
|
||||||
|
def chroot # compatibility to 1.8.0.
|
||||||
|
warn "Pathname#chroot is obsoleted. Use Dir.chroot."
|
||||||
|
Dir.chroot(@path)
|
||||||
|
end
|
||||||
|
|
||||||
def rmdir() Dir.rmdir(@path) end
|
def rmdir() Dir.rmdir(@path) end
|
||||||
def entries() Dir.entries(@path).map {|f| Pathname.new(f) } end
|
def entries() Dir.entries(@path).map {|f| Pathname.new(f) } end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue