# Object-Oriented Pathname Class
# Author:: Tanaka Akira <akr@m17n.org>
# Documentation:: Author and Gavin Sinclair
# For documentation, see class Pathname.
# to_path is implemented so Pathname objects are usable with File.open, etc.
SAME_PATHS = if File::FNM_SYSCASE.nonzero?
# Avoid #zero? here because #casecmp can return nil.
proc {|a, b| a.casecmp(b) == 0}
SEPARATOR_LIST = "#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}"
SEPARATOR_PAT = /[#{SEPARATOR_LIST}]/
SEPARATOR_LIST = "#{Regexp.quote File::SEPARATOR}"
SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/
# chop_basename(path) -> [pre-basename, basename] or nil
def chop_basename(path) # :nodoc:
base = File.basename(path)
if /\A#{SEPARATOR_PAT}?\z/o =~ base
return path[0, path.rindex(base)], base
# split_names(path) -> prefix, [name, ...]
def split_names(path) # :nodoc:
while r = chop_basename(path)
def prepend_prefix(prefix, relpath) # :nodoc:
elsif /#{SEPARATOR_PAT}/o =~ prefix
prefix = File.dirname(prefix)
prefix = File.join(prefix, "") if File.basename(prefix + 'a') != 'a'
# Returns clean pathname of +self+ with consecutive slashes and useless dots
# removed. The filesystem is not accessed.
# If +consider_symlink+ is +true+, then a more conservative algorithm is used
# to avoid breaking symbolic linkages. This may retain more +..+
# entries than absolutely necessary, but without accessing the filesystem,
def cleanpath(consider_symlink=false)
# Clean the path simply by resolving and removing excess +.+ and +..+ entries.
# Nothing more, nothing less.
def cleanpath_aggressive # :nodoc:
while r = chop_basename(pre)
pre.tr!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
if /#{SEPARATOR_PAT}/o =~ File.basename(pre)
names.shift while names[0] == '..'
self.class.new(prepend_prefix(pre, File.join(*names)))
private :cleanpath_aggressive
# has_trailing_separator?(path) -> bool
def has_trailing_separator?(path) # :nodoc:
if r = chop_basename(path)
pre.length + basename.length < path.length
private :has_trailing_separator?
# add_trailing_separator(path) -> path
def add_trailing_separator(path) # :nodoc:
if File.basename(path + 'a') == 'a'
File.join(path, "") # xxx: Is File.join is appropriate to add separator?
private :add_trailing_separator
def del_trailing_separator(path) # :nodoc:
if r = chop_basename(path)
elsif /#{SEPARATOR_PAT}+\z/o =~ path
$` + File.dirname(path)[/#{SEPARATOR_PAT}*\z/o]
private :del_trailing_separator
def cleanpath_conservative # :nodoc:
while r = chop_basename(pre)
names.unshift base if base != '.'
pre.tr!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
if /#{SEPARATOR_PAT}/o =~ File.basename(pre)
names.shift while names[0] == '..'
self.class.new(File.dirname(pre))
if names.last != '..' && File.basename(path) == '.'
result = prepend_prefix(pre, File.join(*names))
if /\A(?:\.|\.\.)\z/ !~ names.last && has_trailing_separator?(path)
self.class.new(add_trailing_separator(result))
private :cleanpath_conservative
# Returns the parent directory.
# This is same as <code>self + '..'</code>.
# Returns +true+ if +self+ points to a mountpoint.
stat2 = self.parent.lstat
stat1.dev == stat2.dev && stat1.ino == stat2.ino ||
# Predicate method for root directories. Returns +true+ if the
# pathname consists of consecutive slashes.
# It doesn't access the filesystem. So it may return +false+ for some
# pathnames which points to roots such as <tt>/usr/..</tt>.
!!(chop_basename(@path) == nil && /#{SEPARATOR_PAT}/o =~ @path)
# Predicate method for testing whether a path is absolute.
# It returns +true+ if the pathname begins with a slash.
# p = Pathname.new('/im/sure')
# p = Pathname.new('not/so/sure')
# The opposite of Pathname#absolute?
# It returns +false+ if the pathname begins with a slash.
# p = Pathname.new('/im/sure')
# p = Pathname.new('not/so/sure')
while r = chop_basename(path)
# Iterates over each component of the path.
# Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
# # yields "usr", "bin", and "ruby".
# Returns an Enumerator if no block was given.
# enum = Pathname.new("/usr/bin/ruby").each_filename
# # yields "usr", "bin", and "ruby".
def each_filename # :yield: filename
return to_enum(__method__) unless block_given?
_, names = split_names(@path)
names.each {|filename| yield filename }
# Iterates over and yields a new Pathname object
# for each element in the given path in descending order.
# Pathname.new('/path/to/some/file.rb').descend {|v| p v}
# #<Pathname:/path/to/some>
# #<Pathname:/path/to/some/file.rb>
# Pathname.new('path/to/some/file.rb').descend {|v| p v}
# #<Pathname:path/to/some>
# #<Pathname:path/to/some/file.rb>
# It doesn't access the filesystem.
vs.reverse_each {|v| yield v }
# Iterates over and yields a new Pathname object
# for each element in the given path in ascending order.
# Pathname.new('/path/to/some/file.rb').ascend {|v| p v}
# #<Pathname:/path/to/some/file.rb>
# #<Pathname:/path/to/some>
# Pathname.new('path/to/some/file.rb').ascend {|v| p v}
# #<Pathname:path/to/some/file.rb>
# #<Pathname:path/to/some>
# It doesn't access the filesystem.
while r = chop_basename(path)
yield self.class.new(del_trailing_separator(path))
# Appends a pathname fragment to +self+ to produce a new Pathname object.
# p1 = Pathname.new("/usr") # Pathname:/usr
# p2 = p1 + "bin/ruby" # Pathname:/usr/bin/ruby
# p3 = p1 + "/etc/passwd" # Pathname:/etc/passwd
# p4 = p1 / "bin/ruby" # Pathname:/usr/bin/ruby
# p5 = p1 / "/etc/passwd" # Pathname:/etc/passwd
# This method doesn't access the file system; it is pure string manipulation.
other = Pathname.new(other) unless Pathname === other
Pathname.new(plus(@path, other.to_s))
def plus(path1, path2) # -> path # :nodoc:
while r2 = chop_basename(prefix2)
index_list2.unshift prefix2.length
basename_list2.unshift basename2
return path2 if prefix2 != ''
while !basename_list2.empty? && basename_list2.first == '.'
break unless r1 = chop_basename(prefix1)
if basename1 == '..' || basename_list2.empty? || basename_list2.first != '..'
prefix1 = prefix1 + basename1
r1 = chop_basename(prefix1)
if !r1 && /#{SEPARATOR_PAT}/o =~ File.basename(prefix1)
while !basename_list2.empty? && basename_list2.first == '..'
if !basename_list2.empty?
suffix2 = path2[index_list2.first..-1]
r1 ? File.join(prefix1, suffix2) : prefix1 + suffix2
r1 ? prefix1 : File.dirname(prefix1)
# Joins the given pathnames onto +self+ to create a new Pathname object.
# path0 = Pathname.new("/usr") # Pathname:/usr
# path0 = path0.join("bin/ruby") # Pathname:/usr/bin/ruby
# path1 = Pathname.new("/usr") + "bin/ruby" # Pathname:/usr/bin/ruby
return self if args.empty?
result = Pathname.new(result) unless Pathname === result
return result if result.absolute?
arg = Pathname.new(arg) unless Pathname === arg
return result if result.absolute?
# Returns the children of the directory (files and subdirectories, not
# recursive) as an array of Pathname objects.
# By default, the returned pathnames will have enough information to access
# the files. If you set +with_directory+ to +false+, then the returned
# pathnames will contain the filename only.
# pn = Pathname("/usr/lib/ruby/1.8")
# # -> [ Pathname:/usr/lib/ruby/1.8/English.rb,
# Pathname:/usr/lib/ruby/1.8/Env.rb,
# Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ]
# # -> [ Pathname:English.rb, Pathname:Env.rb, Pathname:abbrev.rb, ... ]
# Note that the results never contain the entries +.+ and +..+ in
# the directory because they are not children.
def children(with_directory=true)
with_directory = false if @path == '.'
next if e == '.' || e == '..'
result << self.class.new(File.join(@path, e))
result << self.class.new(e)
# Iterates over the children of the directory
# (files and subdirectories, not recursive).
# It yields Pathname object for each child.
# By default, the yielded pathnames will have enough information to access
# If you set +with_directory+ to +false+, then the returned pathnames will
# contain the filename only.
# Pathname("/usr/local").each_child {|f| p f }
# #=> #<Pathname:/usr/local/share>
# # #<Pathname:/usr/local/bin>
# # #<Pathname:/usr/local/games>
# # #<Pathname:/usr/local/lib>
# # #<Pathname:/usr/local/include>
# # #<Pathname:/usr/local/sbin>
# # #<Pathname:/usr/local/src>
# # #<Pathname:/usr/local/man>
# Pathname("/usr/local").each_child(false) {|f| p f }
# Note that the results never contain the entries +.+ and +..+ in
# the directory because they are not children.
def each_child(with_directory=true, &b)
children(with_directory).each(&b)
# Returns a relative path from the given +base_directory+ to the receiver.
# If +self+ is absolute, then +base_directory+ must be absolute too.
# If +self+ is relative, then +base_directory+ must be relative too.
# This method doesn't access the filesystem. It assumes no symlinks.
# ArgumentError is raised when it cannot find a relative path.
def relative_path_from(base_directory)
dest_directory = self.cleanpath.to_s
base_directory = base_directory.cleanpath.to_s
dest_prefix = dest_directory
while r = chop_basename(dest_prefix)
dest_prefix, basename = r
dest_names.unshift basename if basename != '.'