2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
# = ftools.rb: Extra tools for the File class
|
|
|
|
#
|
2005-04-19 18:49:43 +04:00
|
|
|
# Author:: WATANABE, Hirofumi
|
2004-02-14 10:10:21 +03:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# == Description
|
|
|
|
#
|
2006-08-04 22:05:50 +04:00
|
|
|
# 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.
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
2006-08-04 22:05:50 +04:00
|
|
|
# FileUtils contains all or nearly all the same functionality and more, and
|
|
|
|
# is a recommended option over ftools
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
# When you
|
|
|
|
#
|
|
|
|
# require 'ftools'
|
|
|
|
#
|
2006-08-04 22:05:50 +04:00
|
|
|
# then the File class aquires some utility methods for copying, moving, and
|
|
|
|
# deleting files, and more.
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
2006-08-04 22:05:50 +04:00
|
|
|
# See the method descriptions below, and consider using FileUtils as it is
|
|
|
|
# more comprehensive.
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
class File
|
|
|
|
end
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
class << File
|
|
|
|
|
2002-06-20 12:21:00 +04:00
|
|
|
BUFSIZE = 8 * 1024
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# in +to+.
|
|
|
|
#
|
|
|
|
def catname(from, to)
|
2005-04-19 18:49:43 +04:00
|
|
|
if directory? to
|
|
|
|
join to.sub(%r([/\\]$), ''), basename(from)
|
1998-01-16 15:19:09 +03:00
|
|
|
else
|
|
|
|
to
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
# Copies a file +from+ to +to+. If +to+ is a directory, copies +from+
|
|
|
|
# to <tt>to/from</tt>.
|
|
|
|
#
|
|
|
|
def syscopy(from, to)
|
1998-01-16 15:19:09 +03:00
|
|
|
to = catname(from, to)
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
fmode = stat(from).mode
|
|
|
|
tpath = to
|
2001-05-07 10:57:40 +04:00
|
|
|
not_exist = !exist?(tpath)
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2002-06-20 12:21:00 +04:00
|
|
|
from = open(from, "rb")
|
|
|
|
to = open(to, "wb")
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
begin
|
1999-01-20 07:59:39 +03:00
|
|
|
while true
|
2002-06-20 12:21:00 +04:00
|
|
|
to.syswrite from.sysread(BUFSIZE)
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
rescue EOFError
|
1999-01-20 07:59:39 +03:00
|
|
|
ret = true
|
1998-01-16 15:19:09 +03:00
|
|
|
rescue
|
1999-01-20 07:59:39 +03:00
|
|
|
ret = false
|
1998-01-16 15:19:09 +03:00
|
|
|
ensure
|
|
|
|
to.close
|
|
|
|
from.close
|
|
|
|
end
|
2001-05-07 10:57:40 +04:00
|
|
|
chmod(fmode, tpath) if not_exist
|
1998-01-16 15:19:09 +03:00
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
# 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>
|
|
|
|
# is printed.
|
|
|
|
#
|
|
|
|
def copy(from, to, verbose = false)
|
2005-04-19 18:49:43 +04:00
|
|
|
$stderr.print from, " -> ", catname(from, to), "\n" if verbose
|
1998-01-16 15:19:09 +03:00
|
|
|
syscopy from, to
|
|
|
|
end
|
|
|
|
|
|
|
|
alias cp copy
|
|
|
|
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
# Moves a file +from+ to +to+ using #syscopy. If +to+ is a directory,
|
2006-08-04 22:05:50 +04:00
|
|
|
# copies from +from+ to <tt>to/from</tt>. If +verbose+ is true, <tt>from ->
|
|
|
|
# to</tt> is printed.
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
def move(from, to, verbose = false)
|
1998-01-16 15:19:09 +03:00
|
|
|
to = catname(from, to)
|
2005-04-19 18:49:43 +04:00
|
|
|
$stderr.print from, " -> ", to, "\n" if verbose
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2005-04-19 18:49:43 +04:00
|
|
|
if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
|
1998-01-16 15:19:09 +03:00
|
|
|
unlink to
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
fstat = stat(from)
|
1998-01-16 15:19:09 +03:00
|
|
|
begin
|
|
|
|
rename from, to
|
|
|
|
rescue
|
1999-08-13 09:45:20 +04:00
|
|
|
begin
|
2005-04-19 18:49:43 +04:00
|
|
|
symlink readlink(from), to and unlink from
|
1999-08-13 09:45:20 +04:00
|
|
|
rescue
|
|
|
|
from_stat = stat(from)
|
|
|
|
syscopy from, to and unlink from
|
|
|
|
utime(from_stat.atime, from_stat.mtime, to)
|
|
|
|
begin
|
2000-08-05 12:40:11 +04:00
|
|
|
chown(fstat.uid, fstat.gid, to)
|
1999-08-13 09:45:20 +04:00
|
|
|
rescue
|
|
|
|
end
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias mv move
|
|
|
|
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
2006-08-04 22:05:50 +04:00
|
|
|
# Returns +true+ if and only if the contents of files +from+ and +to+ are
|
2004-02-14 10:10:21 +03:00
|
|
|
# identical. If +verbose+ is +true+, <tt>from <=> to</tt> is printed.
|
|
|
|
#
|
|
|
|
def compare(from, to, verbose = false)
|
2005-04-19 18:49:43 +04:00
|
|
|
$stderr.print from, " <=> ", to, "\n" if verbose
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2002-06-20 12:59:59 +04:00
|
|
|
return false if stat(from).size != stat(to).size
|
|
|
|
|
2002-06-20 12:21:00 +04:00
|
|
|
from = open(from, "rb")
|
|
|
|
to = open(to, "rb")
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ret = false
|
1998-01-16 15:19:09 +03:00
|
|
|
fr = tr = ''
|
|
|
|
|
|
|
|
begin
|
|
|
|
while fr == tr
|
2002-06-20 12:21:00 +04:00
|
|
|
fr = from.read(BUFSIZE)
|
2000-08-05 12:40:11 +04:00
|
|
|
if fr
|
1998-01-16 15:19:09 +03:00
|
|
|
tr = to.read(fr.size)
|
|
|
|
else
|
2002-06-20 12:21:00 +04:00
|
|
|
ret = to.read(BUFSIZE)
|
1999-08-13 09:45:20 +04:00
|
|
|
ret = !ret || ret.length == 0
|
1998-01-16 15:19:09 +03:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
1999-01-20 07:59:39 +03:00
|
|
|
ret = false
|
1998-01-16 15:19:09 +03:00
|
|
|
ensure
|
|
|
|
to.close
|
|
|
|
from.close
|
|
|
|
end
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
alias cmp compare
|
|
|
|
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def safe_unlink(*files)
|
1999-01-20 07:59:39 +03:00
|
|
|
verbose = if files[-1].is_a? String then false else files.pop end
|
2005-04-19 18:49:43 +04:00
|
|
|
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
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias rm_f safe_unlink
|
|
|
|
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
# Creates a directory and all its parent directories.
|
|
|
|
# For example,
|
|
|
|
#
|
|
|
|
# File.makedirs '/usr/lib/ruby'
|
|
|
|
#
|
|
|
|
# causes the following directories to be made, if they do not exist.
|
|
|
|
# * /usr
|
|
|
|
# * /usr/lib
|
|
|
|
# * /usr/lib/ruby
|
|
|
|
#
|
|
|
|
# You can pass several directories, each as a parameter. If the last
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-19 03:19:47 +04:00
|
|
|
# parameter isn't a String, verbose mode will be enabled.
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def makedirs(*dirs)
|
1999-01-20 07:59:39 +03:00
|
|
|
verbose = if dirs[-1].is_a? String then false else dirs.pop end
|
1998-01-16 15:19:09 +03:00
|
|
|
mode = 0755
|
|
|
|
for dir in dirs
|
|
|
|
parent = dirname(dir)
|
2005-04-19 18:49:43 +04:00
|
|
|
next if parent == dir or directory? dir
|
|
|
|
makedirs parent unless directory? parent
|
|
|
|
$stderr.print "mkdir ", dir, "\n" if verbose
|
1999-08-13 09:45:20 +04:00
|
|
|
if basename(dir) != ""
|
2003-12-11 14:08:16 +03:00
|
|
|
begin
|
|
|
|
Dir.mkdir dir, mode
|
|
|
|
rescue SystemCallError
|
2005-04-19 18:49:43 +04:00
|
|
|
raise unless directory? dir
|
2003-12-11 14:08:16 +03:00
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias mkpath makedirs
|
|
|
|
|
|
|
|
alias o_chmod chmod
|
|
|
|
|
2000-08-05 12:40:11 +04:00
|
|
|
vsave, $VERBOSE = $VERBOSE, false
|
2004-02-14 10:10:21 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Changes permission bits on +files+ to the bit pattern represented
|
|
|
|
# by +mode+. If the last parameter isn't a String, verbose mode will
|
|
|
|
# be enabled.
|
|
|
|
#
|
|
|
|
# File.chmod 0755, 'somecommand'
|
|
|
|
# File.chmod 0644, 'my.rb', 'your.rb', true
|
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
def chmod(mode, *files)
|
1999-01-20 07:59:39 +03:00
|
|
|
verbose = if files[-1].is_a? String then false else files.pop end
|
2005-04-19 18:49:43 +04:00
|
|
|
$stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose
|
1998-01-16 15:19:09 +03:00
|
|
|
o_chmod mode, *files
|
|
|
|
end
|
2000-08-05 12:40:11 +04:00
|
|
|
$VERBOSE = vsave
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2004-02-14 10:10:21 +03:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
def install(from, to, mode = nil, verbose = false)
|
1998-01-16 15:19:09 +03:00
|
|
|
to = catname(from, to)
|
2005-04-19 18:49:43 +04:00
|
|
|
unless exist? to and cmp from, to
|
|
|
|
safe_unlink to if exist? to
|
1998-01-16 15:19:09 +03:00
|
|
|
cp from, to, verbose
|
|
|
|
chmod mode, to, verbose if mode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2004-02-14 10:10:21 +03:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
# vi:set sw=2:
|