PHP 8.2.31
Preview: ftools.rb Size: 6.17 KB
/proc/thread-self/root/opt/alt/ruby18/lib64/ruby/1.8/ftools.rb

# 
# = 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.
#
# == Description
#
# 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 
#
# When you
#
#   require '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
# more comprehensive.
#
class File
end

class << File

  BUFSIZE = 8 * 1024

  #
  # 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)
    if directory? to
      join to.sub(%r([/\\]$), ''), basename(from)
    else
      to
    end
  end

  #
  # Copies a file +from+ to +to+. If +to+ is a directory, copies +from+
  # to <tt>to/from</tt>.
  #
  def syscopy(from, to)
    to = catname(from, to)

    fmode = stat(from).mode
    tpath = to
    not_exist = !exist?(tpath)

    from = open(from, "rb")
    to = open(to, "wb")

    begin
      while true
	to.syswrite from.sysread(BUFSIZE)
      end
    rescue EOFError
      ret = true
    rescue
      ret = false
    ensure
      to.close
      from.close
    end
    chmod(fmode, tpath) if not_exist
    ret
  end

  #
  # 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)
    $stderr.print from, " -> ", catname(from, to), "\n" if verbose
    syscopy from, to
  end

  alias cp copy

  #
  # 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 ->
  # to</tt> is printed.
  #
  def move(from, to, verbose = false)
    to = catname(from, to)
    $stderr.print from, " -> ", to, "\n" if verbose

    if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
      unlink to
    end
    fstat = stat(from)
    begin
      rename from, to
    rescue
      begin
        symlink readlink(from), to and unlink from
      rescue
	from_stat = stat(from)
	syscopy from, to and unlink from
	utime(from_stat.atime, from_stat.mtime, to)
	begin
	  chown(fstat.uid, fstat.gid, to)
	rescue
	end
      end
    end
  end

  alias mv move

  #
  # 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

    from = open(from, "rb")
    to = open(to, "rb")

    ret = false
    fr = tr = ''

    begin
      while fr == tr
	fr = from.read(BUFSIZE)
	if fr
	  tr = to.read(fr.size)
	else
	  ret = to.read(BUFSIZE)
	  ret = !ret || ret.length == 0
	  break
	end
      end
    rescue
      ret = false
    ensure
      to.close
      from.close
    end
    ret
  end

  alias cmp compare

  #
  # 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.
  #
  def safe_unlink(*files)
    verbose = if files[-1].is_a? String then false else files.pop end
    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
    end
  end

  alias rm_f safe_unlink

  #
  # 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
  # parameter isn't a String, verbose mode will be enabled.
  #
  def makedirs(*dirs)
    verbose = if dirs[-1].is_a? String then false else dirs.pop end
    mode = 0755
    for dir in dirs
      parent = dirname(dir)
      next if parent == dir or directory? dir
      makedirs parent unless directory? parent
      $stderr.print "mkdir ", dir, "\n" if verbose
      if basename(dir) != ""
        begin
          Dir.mkdir dir, mode
        rescue SystemCallError
          raise unless directory? dir
        end
      end
    end
  end

  alias mkpath makedirs

  alias o_chmod chmod

  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
  # be enabled.
  #
  #   File.chmod 0755, 'somecommand'
  #   File.chmod 0644, 'my.rb', 'your.rb', true
  #
  def chmod(mode, *files)
    verbose = if files[-1].is_a? String then false else files.pop end
    $stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose
    o_chmod mode, *files
  end
  $VERBOSE = vsave

  #
  # 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)
    to = catname(from, to)
    unless exist? to and cmp from, to
      safe_unlink to if exist? to
      cp from, to, verbose
      chmod mode, to, verbose if mode
    end
  end

end

# vi:set sw=2:

Directory Contents

Dirs: 27 × Files: 83

Name Size Perms Modified Actions
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
cgi DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
date DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
digest DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
dl DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
drb DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
io DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
irb DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
net DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
openssl DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
optparse DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
racc DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
rdoc DIR
- drwxr-xr-x 2024-03-03 22:48:17
Edit Download
rexml DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
rinda DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
rss DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
runit DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
shell DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
soap DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
test DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
uri DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
webrick DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
wsdl DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
- drwxr-xr-x 2024-03-03 22:50:34
Edit Download
xmlrpc DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
xsd DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
yaml DIR
- drwxr-xr-x 2024-03-03 22:48:14
Edit Download
2.50 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
3.37 KB lrw-r--r-- 2007-07-16 15:47:16
Edit Download
17.73 KB lrw-r--r-- 2008-02-10 15:24:56
Edit Download
6.89 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
73.74 KB lrw-r--r-- 2009-12-14 02:40:07
Edit Download
12.84 KB lrw-r--r-- 2009-08-03 05:59:38
Edit Download
24.46 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
53.02 KB lrw-r--r-- 2010-06-08 04:45:42
Edit Download
128 B lrw-r--r-- 2007-02-12 23:01:19
Edit Download
20.61 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
8.81 KB lrw-r--r-- 2009-07-16 00:35:14
Edit Download
1.12 KB lrw-r--r-- 2007-07-28 00:40:58
Edit Download
19 B lrw-r--r-- 2007-02-12 23:01:19
Edit Download
4.04 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
5.60 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
274 B lrw-r--r-- 2007-02-12 23:01:19
Edit Download
21.38 KB lrw-r--r-- 2009-02-23 17:44:50
Edit Download
487 B lrw-r--r-- 2007-02-12 23:01:19
Edit Download
633 B lrw-r--r-- 2008-02-18 01:17:44
Edit Download
42.23 KB lrw-r--r-- 2011-05-20 22:29:13
Edit Download
5.38 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
1.84 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
6.16 KB lrw-r--r-- 2008-06-06 08:05:24
Edit Download
6.17 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
8.10 KB lrw-r--r-- 2008-04-10 10:52:50
Edit Download
14.88 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
2.25 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
6.43 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
590 B lrw-r--r-- 2007-02-12 23:01:19
Edit Download
21.96 KB lrw-r--r-- 2008-07-12 15:08:29
Edit Download
7.43 KB lrw-r--r-- 2009-08-09 08:44:15
Edit Download
4.30 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
8.12 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
17.59 KB lrw-r--r-- 2011-05-30 02:08:57
Edit Download
1.28 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
5.42 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
27.21 KB lrw-r--r-- 2009-01-24 15:02:50
Edit Download
411 B lrw-r--r-- 2007-03-06 10:09:51
Edit Download
50.65 KB lrw-r--r-- 2010-12-04 06:34:10
Edit Download
7.93 KB lrw-r--r-- 2009-11-25 07:45:29
Edit Download
2.07 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
5.15 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
20.49 KB lrw-r--r-- 2007-10-08 11:16:54
Edit Download
2.10 KB lrw-r--r-- 2009-12-14 04:28:06
Edit Download
575 B lrw-r--r-- 2010-11-22 07:21:45
Edit Download
47.12 KB lrw-r--r-- 2009-02-20 11:43:35
Edit Download
3.35 KB lrw-r--r-- 2009-05-26 12:06:21
Edit Download
1.55 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
1.33 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
29.39 KB lrw-r--r-- 2010-11-23 08:21:08
Edit Download
1.48 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
15.97 KB lrw-r--r-- 2007-06-07 10:06:41
Edit Download
18.33 KB lrw-r--r-- 2007-02-16 19:53:09
Edit Download
90 B lrw-r--r-- 2007-02-12 23:01:19
Edit Download
1.59 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
11.15 KB lrw-r--r-- 2008-06-06 08:05:24
Edit Download
12.05 KB lrw-r--r-- 2010-06-08 05:02:31
Edit Download
835 B lrw-r--r-- 2007-02-12 23:01:19
Edit Download
1.55 KB lrw-r--r-- 2008-08-08 01:58:40
Edit Download
56.83 KB lrw-r--r-- 2010-12-23 03:22:57
Edit Download
504 B lrw-r--r-- 2007-10-21 12:19:43
Edit Download
180 B lrw-r--r-- 2007-02-12 23:01:19
Edit Download
20.63 KB lrw-r--r-- 2009-01-20 03:23:46
Edit Download
4.27 KB lrw-r--r-- 2011-12-10 12:17:33
Edit Download
27.08 KB lrw-r--r-- 2008-06-09 09:20:43
Edit Download
418 B lrw-r--r-- 2007-03-06 10:09:51
Edit Download
4.66 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
3.99 KB lrw-r--r-- 2008-03-21 12:16:15
Edit Download
8.08 KB lrw-r--r-- 2008-07-03 11:14:50
Edit Download
6.09 KB lrw-r--r-- 2009-02-19 16:41:12
Edit Download
4.86 KB lrw-r--r-- 2008-04-21 09:43:44
Edit Download
104 B lrw-r--r-- 2007-02-13 19:39:32
Edit Download
4.32 KB lrw-r--r-- 2010-06-08 07:08:15
Edit Download
31.58 KB lrw-r--r-- 2008-06-06 08:05:24
Edit Download
3.00 KB lrw-r--r-- 2010-06-08 06:24:25
Edit Download
3.69 KB lrw-r--r-- 2009-01-26 02:12:10
Edit Download
2.73 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
7.99 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
4.54 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
710 B lrw-r--r-- 2008-04-15 09:41:47
Edit Download
2.68 KB lrw-r--r-- 2007-02-12 23:01:19
Edit Download
811 B lrw-r--r-- 2007-02-12 23:01:19
Edit Download
12.36 KB lrw-r--r-- 2008-04-19 11:45:39
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).