# = ftools.rb: Extra tools for the File class
# Author:: WATANABE, Hirofumi
# Documentation:: Zachary Landau
# This library can be distributed under the terms of the Ruby license.
# You can freely distribute/modify this library.
# It is included in the Ruby standard library.
# ftools adds several (class, not instance) methods to the File class, for
# copying, moving, deleting, installing, and comparing files, as well as
# creating a directory path. See the File class for details.
# FileUtils contains all or nearly all the same functionality and more, and
# is a recommended option over ftools
# then the File class aquires some utility methods for copying, moving, and
# deleting files, and more.
# See the method descriptions below, and consider using FileUtils as it is
# If +to+ is a valid directory, +from+ will be appended to +to+, adding
# and escaping backslashes as necessary. Otherwise, +to+ will be returned.
# Useful for appending +from+ to +to+ only if the filename was not specified
join to.sub(%r([/\\]$), ''), basename(from)
# Copies a file +from+ to +to+. If +to+ is a directory, copies +from+
not_exist = !exist?(tpath)
to.syswrite from.sysread(BUFSIZE)
chmod(fmode, tpath) if not_exist
# Copies a file +from+ to +to+ using #syscopy. If +to+ is a directory,
# copies +from+ to <tt>to/from</tt>. If +verbose+ is true, <tt>from -> to</tt>
def copy(from, to, verbose = false)
$stderr.print from, " -> ", catname(from, to), "\n" if verbose
# Moves a file +from+ to +to+ using #syscopy. If +to+ is a directory,
# copies from +from+ to <tt>to/from</tt>. If +verbose+ is true, <tt>from ->
def move(from, to, verbose = false)
$stderr.print from, " -> ", to, "\n" if verbose
if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
symlink readlink(from), to and unlink from
syscopy from, to and unlink from
utime(from_stat.atime, from_stat.mtime, to)
chown(fstat.uid, fstat.gid, to)
# Returns +true+ if and only if the contents of files +from+ and +to+ are
# identical. If +verbose+ is +true+, <tt>from <=> to</tt> is printed.
def compare(from, to, verbose = false)
$stderr.print from, " <=> ", to, "\n" if verbose
return false if stat(from).size != stat(to).size
ret = !ret || ret.length == 0
# Removes a list of files. Each parameter should be the name of the file to
# delete. If the last parameter isn't a String, verbose mode will be enabled.
# Returns the number of files deleted.
verbose = if files[-1].is_a? String then false else files.pop end
$stderr.print "removing ", file, "\n" if verbose
rescue Errno::EACCES # for Windows
continue if symlink? file
o_chmod mode | 0200, file
$stderr.print "removing ", file, "\n" if verbose
o_chmod mode, file rescue nil
# Creates a directory and all its parent directories.
# File.makedirs '/usr/lib/ruby'
# causes the following directories to be made, if they do not exist.
# You can pass several directories, each as a parameter. If the last
# parameter isn't a String, verbose mode will be enabled.
verbose = if dirs[-1].is_a? String then false else dirs.pop end
next if parent == dir or directory? dir
makedirs parent unless directory? parent
$stderr.print "mkdir ", dir, "\n" if verbose
raise unless directory? dir
vsave, $VERBOSE = $VERBOSE, false
# Changes permission bits on +files+ to the bit pattern represented
# by +mode+. If the last parameter isn't a String, verbose mode will
# File.chmod 0755, 'somecommand'
# File.chmod 0644, 'my.rb', 'your.rb', true
verbose = if files[-1].is_a? String then false else files.pop end
$stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose
# If +src+ is not the same as +dest+, copies it and changes the permission
# mode to +mode+. If +dest+ is a directory, destination is <tt>dest/src</tt>.
# If +mode+ is not set, default is used. If +verbose+ is set to true, the
# name of each file copied will be printed.
def install(from, to, mode = nil, verbose = false)
unless exist? to and cmp from, to
safe_unlink to if exist? to
chmod mode, to, verbose if mode