Font : Auto-Install

Font : Auto-Install
Version: 3.5
By: Kain Nobel

Introduction

This system is basically a module which can be setup to automatically install multiple font(s) on a player's machine! The process is quick and seemless, and notification to the player is optional. Also, instead of game crashing when the player doesn't have permissions, it gives a friendly message to run application as Administrator or tells you how to install fonts manually, then continues onto the game like normal.

Features

[list]
[*]Automatically install fonts so the player doesn't have to
[*]Notifies player of successful installs *optional*
[*]Notifies player of failed installs *optional*
[*]Notifies player of not found files *optional*
[*]Stand alone module which doesn't have anything to do with RM
[*]Will call rescue clause with message if player doesn't have permissions
[/list]

Script

FileUtils Spoiler
  1. #
  2. # = fileutils.rb
  3. #
  4. # Copyright (c) 2000-2005 Minero Aoki <[email hidden]>
  5. #
  6. # This program is free software.
  7. # You can distribute/modify this program under the same terms of ruby.
  8. #
  9. # == module FileUtils
  10. #
  11. # Namespace for several file utility methods for copying, moving, removing, etc.
  12. #
  13. # === Module Functions
  14. #
  15. #   cd(dir, options)
  16. #   cd(dir, options) {|dir| .... }
  17. #   pwd()
  18. #   mkdir(dir, options)
  19. #   mkdir(list, options)
  20. #   mkdir_p(dir, options)
  21. #   mkdir_p(list, options)
  22. #   rmdir(dir, options)
  23. #   rmdir(list, options)
  24. #   ln(old, new, options)
  25. #   ln(list, destdir, options)
  26. #   ln_s(old, new, options)
  27. #   ln_s(list, destdir, options)
  28. #   ln_sf(src, dest, options)
  29. #   cp(src, dest, options)
  30. #   cp(list, dir, options)
  31. #   cp_r(src, dest, options)
  32. #   cp_r(list, dir, options)
  33. #   mv(src, dest, options)
  34. #   mv(list, dir, options)
  35. #   rm(list, options)
  36. #   rm_r(list, options)
  37. #   rm_rf(list, options)
  38. #   install(src, dest, mode = <src's>, options)
  39. #   chmod(mode, list, options)
  40. #   chmod_R(mode, list, options)
  41. #   chown(user, group, list, options)
  42. #   chown_R(user, group, list, options)
  43. #   touch(list, options)
  44. #
  45. # The <tt>options</tt> parameter is a hash of options, taken from the list
  46. # <tt>:force</tt>, <tt>:noop</tt>, <tt>:preserve</tt>, and <tt>:verbose</tt>.
  47. # <tt>:noop</tt> means that no changes are made.  The other two are obvious.
  48. # Each method documents the options that it honours.
  49. #
  50. # All methods that have the concept of a "source" file or directory can take
  51. # either one file or a list of files in that argument.  See the method
  52. # documentation for examples.
  53. #
  54. # There are some `low level' methods, which do not accept any option:
  55. #
  56. #   copy_entry(src, dest, preserve = false, dereference = false)
  57. #   copy_file(src, dest, preserve = false, dereference = true)
  58. #   copy_stream(srcstream, deststream)
  59. #   remove_entry(path, force = false)
  60. #   remove_entry_secure(path, force = false)
  61. #   remove_file(path, force = false)
  62. #   compare_file(path_a, path_b)
  63. #   compare_stream(stream_a, stream_b)
  64. #   uptodate?(file, cmp_list)
  65. #
  66. # == module FileUtils::Verbose
  67. #
  68. # This module has all methods of FileUtils module, but it outputs messages
  69. # before acting.  This equates to passing the <tt>:verbose</tt> flag to methods
  70. # in FileUtils.
  71. #
  72. # == module FileUtils::NoWrite
  73. #
  74. # This module has all methods of FileUtils module, but never changes
  75. # files/directories.  This equates to passing the <tt>:noop</tt> flag to methods
  76. # in FileUtils.
  77. #
  78. # == module FileUtils::DryRun
  79. #
  80. # This module has all methods of FileUtils module, but never changes
  81. # files/directories.  This equates to passing the <tt>:noop</tt> and
  82. # <tt>:verbose</tt> flags to methods in FileUtils.
  83. #
  84.  
  85. module FileUtils
  86.  
  87.   def self.private_module_function(name)   #:nodoc:
  88.     module_function name
  89.     private_class_method name
  90.   end
  91.  
  92.   # This hash table holds command options.
  93.   OPT_TABLE = {}   #:nodoc: internal use only
  94.  
  95.   #
  96.   # Options: (none)
  97.   #
  98.   # Returns the name of the current directory.
  99.   #
  100.   def pwd
  101.     Dir.pwd
  102.   end
  103.   module_function :pwd
  104.  
  105.   alias getwd pwd
  106.   module_function :getwd
  107.  
  108.   #
  109.   # Options: verbose
  110.   #
  111.   # Changes the current directory to the directory +dir+.
  112.   #
  113.   # If this method is called with block, resumes to the old
  114.   # working directory after the block execution finished.
  115.   #
  116.   #   FileUtils.cd('/', :verbose => true)   # chdir and report it
  117.   #
  118.   def cd(dir, options = {}, &block) # :yield: dir
  119.     fu_check_options options, :verbose
  120.     fu_output_message "cd #{dir}" if options[:verbose]
  121.     Dir.chdir(dir, &block)
  122.     fu_output_message 'cd -' if options[:verbose] and block
  123.   end
  124.   module_function :cd
  125.  
  126.   alias chdir cd
  127.   module_function :chdir
  128.  
  129.   OPT_TABLE['cd']    =
  130.   OPT_TABLE['chdir'] = %w( verbose )
  131.  
  132.   #
  133.   # Options: (none)
  134.   #
  135.   # Returns true if +newer+ is newer than all +old_list+.
  136.   # Non-existent files are older than any file.
  137.   #
  138.   #   FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \
  139.   #       system 'make hello.o'
  140.   #
  141.   def uptodate?(new, old_list, options = nil)
  142.     raise ArgumentError, 'uptodate? does not accept any option' if options
  143.  
  144.     return false unless File.exist?(new)
  145.     new_time = File.mtime(new)
  146.     old_list.each do |old|
  147.       if File.exist?(old)
  148.         return false unless new_time > File.mtime(old)
  149.       end
  150.     end
  151.     true
  152.   end
  153.   module_function :uptodate?
  154.  
  155.   #
  156.   # Options: mode noop verbose
  157.   #
  158.   # Creates one or more directories.
  159.   #
  160.   #   FileUtils.mkdir 'test'
  161.   #   FileUtils.mkdir %w( tmp data )
  162.   #   FileUtils.mkdir 'notexist', :noop => true  # Does not really create.
  163.   #   FileUtils.mkdir 'tmp', :mode => 0700
  164.   #
  165.   def mkdir(list, options = {})
  166.     fu_check_options options, :mode, :noop, :verbose
  167.     list = fu_list(list)
  168.     fu_output_message "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
  169.     return if options[:noop]
  170.  
  171.     list.each do |dir|
  172.       fu_mkdir dir, options[:mode]
  173.     end
  174.   end
  175.   module_function :mkdir
  176.  
  177.   OPT_TABLE['mkdir'] = %w( noop verbose mode )
  178.  
  179.   #
  180.   # Options: mode noop verbose
  181.   #
  182.   # Creates a directory and all its parent directories.
  183.   # For example,
  184.   #
  185.   #   FileUtils.mkdir_p '/usr/local/lib/ruby'
  186.   #
  187.   # causes to make following directories, if it does not exist.
  188.   #     * /usr
  189.   #     * /usr/local
  190.   #     * /usr/local/lib
  191.   #     * /usr/local/lib/ruby
  192.   #
  193.   # You can pass several directories at a time in a list.
  194.   #
  195.   def mkdir_p(list, options = {})
  196.     fu_check_options options, :mode, :noop, :verbose
  197.     list = fu_list(list)
  198.     fu_output_message "mkdir -p #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
  199.     return *list if options[:noop]
  200.  
  201.     list.map {|path| path.sub(%r</\z>, '') }.each do |path|
  202.       # optimize for the most common case
  203.       begin
  204.         fu_mkdir path, options[:mode]
  205.         next
  206.       rescue SystemCallError
  207.         next if File.directory?(path)
  208.       end
  209.  
  210.       stack = []
  211.       until path == stack.last   # dirname("/")=="/", dirname("C:/")=="C:/"
  212.         stack.push path
  213.         path = File.dirname(path)
  214.       end
  215.       stack.reverse_each do |path|
  216.         begin
  217.           fu_mkdir path, options[:mode]
  218.         rescue SystemCallError => err
  219.           raise unless File.directory?(path)
  220.         end
  221.       end
  222.     end
  223.  
  224.     return *list
  225.   end
  226.   module_function :mkdir_p
  227.  
  228.   alias mkpath    mkdir_p
  229.   alias makedirs  mkdir_p
  230.   module_function :mkpath
  231.   module_function :makedirs
  232.  
  233.   OPT_TABLE['mkdir_p']  =
  234.   OPT_TABLE['mkpath']   =
  235.   OPT_TABLE['makedirs'] = %w( noop verbose )
  236.  
  237.   def fu_mkdir(path, mode)   #:nodoc:
  238.     path = path.sub(%r</\z>, '')
  239.     if mode
  240.       Dir.mkdir path, mode
  241.       File.chmod mode, path
  242.     else
  243.       Dir.mkdir path
  244.     end
  245.   end
  246.   private_module_function :fu_mkdir
  247.  
  248.   #
  249.   # Options: noop, verbose
  250.   #
  251.   # Removes one or more directories.
  252.   #
  253.   #   FileUtils.rmdir 'somedir'
  254.   #   FileUtils.rmdir %w(somedir anydir otherdir)
  255.   #   # Does not really remove directory; outputs message.
  256.   #   FileUtils.rmdir 'somedir', :verbose => true, :noop => true
  257.   #
  258.   def rmdir(list, options = {})
  259.     fu_check_options options, :noop, :verbose
  260.     list = fu_list(list)
  261.     fu_output_message "rmdir #{list.join ' '}" if options[:verbose]
  262.     return if options[:noop]
  263.     list.each do |dir|
  264.       Dir.rmdir dir.sub(%r</\z>, '')
  265.     end
  266.   end
  267.   module_function :rmdir
  268.  
  269.   OPT_TABLE['rmdir'] = %w( noop verbose )
  270.  
  271.   #
  272.   # Options: force noop verbose
  273.   #
  274.   # <b><tt>ln(old, new, options = {})</tt></b>
  275.   #
  276.   # Creates a hard link +new+ which points to +old+.
  277.   # If +new+ already exists and it is a directory, creates a link +new/old+.
  278.   # If +new+ already exists and it is not a directory, raises Errno::EEXIST.
  279.   # But if :force option is set, overwrite +new+.
  280.   #
  281.   #   FileUtils.ln 'gcc', 'cc', :verbose => true
  282.   #   FileUtils.ln '/usr/bin/emacs21', '/usr/bin/emacs'
  283.   #
  284.   # <b><tt>ln(list, destdir, options = {})</tt></b>
  285.   #
  286.   # Creates several hard links in a directory, with each one pointing to the
  287.   # item in +list+.  If +destdir+ is not a directory, raises Errno::ENOTDIR.
  288.   #
  289.   #   include FileUtils
  290.   #   cd '/sbin'
  291.   #   FileUtils.ln %w(cp mv mkdir), '/bin'   # Now /sbin/cp and /bin/cp are linked.
  292.   #
  293.   def ln(src, dest, options = {})
  294.     fu_check_options options, :force, :noop, :verbose
  295.     fu_output_message "ln#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
  296.     return if options[:noop]
  297.     fu_each_src_dest0(src, dest) do |s,d|
  298.       remove_file d, true if options[:force]
  299.       File.link s, d
  300.     end
  301.   end
  302.   module_function :ln
  303.  
  304.   alias link ln
  305.   module_function :link
  306.  
  307.   OPT_TABLE['ln']   =
  308.   OPT_TABLE['link'] = %w( noop verbose force )
  309.  
  310.   #
  311.   # Options: force noop verbose
  312.   #
  313.   # <b><tt>ln_s(old, new, options = {})</tt></b>
  314.   #
  315.   # Creates a symbolic link +new+ which points to +old+.  If +new+ already
  316.   # exists and it is a directory, creates a symbolic link +new/old+.  If +new+
  317.   # already exists and it is not a directory, raises Errno::EEXIST.  But if
  318.   # :force option is set, overwrite +new+.
  319.   #
  320.   #   FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby'
  321.   #   FileUtils.ln_s 'verylongsourcefilename.c', 'c', :force => true
  322.   #
  323.   # <b><tt>ln_s(list, destdir, options = {})</tt></b>
  324.   #
  325.   # Creates several symbolic links in a directory, with each one pointing to the
  326.   # item in +list+.  If +destdir+ is not a directory, raises Errno::ENOTDIR.
  327.   #
  328.   # If +destdir+ is not a directory, raises Errno::ENOTDIR.
  329.   #
  330.   #   FileUtils.ln_s Dir.glob('bin/*.rb'), '/home/aamine/bin'
  331.   #
  332.   def ln_s(src, dest, options = {})
  333.     fu_check_options options, :force, :noop, :verbose
  334.     fu_output_message "ln -s#{options[:force] ? 'f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
  335.     return if options[:noop]
  336.     fu_each_src_dest0(src, dest) do |s,d|
  337.       remove_file d, true if options[:force]
  338.       File.symlink s, d
  339.     end
  340.   end
  341.   module_function :ln_s
  342.  
  343.   alias symlink ln_s
  344.   module_function :symlink
  345.  
  346.   OPT_TABLE['ln_s']    =
  347.   OPT_TABLE['symlink'] = %w( noop verbose force )
  348.  
  349.   #
  350.   # Options: noop verbose
  351.   #
  352.   # Same as
  353.   #   #ln_s(src, dest, :force)
  354.   #
  355.   def ln_sf(src, dest, options = {})
  356.     fu_check_options options, :noop, :verbose
  357.     options = options.dup
  358.     options[:force] = true
  359.     ln_s src, dest, options
  360.   end
  361.   module_function :ln_sf
  362.  
  363.   OPT_TABLE['ln_sf'] = %w( noop verbose )
  364.  
  365.   #
  366.   # Options: preserve noop verbose
  367.   #
  368.   # Copies a file content +src+ to +dest+.  If +dest+ is a directory,
  369.   # copies +src+ to +dest/src+.
  370.   #
  371.   # If +src+ is a list of files, then +dest+ must be a directory.
  372.   #
  373.   #   FileUtils.cp 'eval.c', 'eval.c.org'
  374.   #   FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6'
  375.   #   FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose => true
  376.   #   FileUtils.cp 'symlink', 'dest'   # copy content, "dest" is not a symlink
  377.   #
  378.   def cp(src, dest, options = {})
  379.     fu_check_options options, :preserve, :noop, :verbose
  380.     fu_output_message "cp#{options[:preserve] ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
  381.     return if options[:noop]
  382.     fu_each_src_dest(src, dest) do |s, d|
  383.       copy_file s, d, options[:preserve]
  384.     end
  385.   end
  386.   module_function :cp
  387.  
  388.   alias copy cp
  389.   module_function :copy
  390.  
  391.   OPT_TABLE['cp']   =
  392.   OPT_TABLE['copy'] = %w( noop verbose preserve )
  393.  
  394.   #
  395.   # Options: preserve noop verbose dereference_root
  396.   #
  397.   # Copies +src+ to +dest+. If +src+ is a directory, this method copies
  398.   # all its contents recursively. If +dest+ is a directory, copies
  399.   # +src+ to +dest/src+.
  400.   #
  401.   # +src+ can be a list of files.
  402.   #
  403.   #   # Installing ruby library "mylib" under the site_ruby
  404.   #   FileUtils.rm_r site_ruby + '/mylib', :force
  405.   #   FileUtils.cp_r 'lib/', site_ruby + '/mylib'
  406.   #
  407.   #   # Examples of copying several files to target directory.
  408.   #   FileUtils.cp_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
  409.   #   FileUtils.cp_r Dir.glob('*.rb'), '/home/aamine/lib/ruby', :noop => true, :verbose => true
  410.   #
  411.   #   # If you want to copy all contents of a directory instead of the
  412.   #   # directory itself, c.f. src/x -> dest/x, src/y -> dest/y,
  413.   #   # use following code.
  414.   #   FileUtils.cp_r 'src/.', 'dest'     # cp_r('src', 'dest') makes src/dest,
  415.   #                                      # but this doesn't.
  416.   #
  417.   def cp_r(src, dest, options = {})
  418.     fu_check_options options, :preserve, :noop, :verbose, :dereference_root
  419.     fu_output_message "cp -r#{options[:preserve] ? 'p' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
  420.     return if options[:noop]
  421.     options[:dereference_root] = true unless options.key?(:dereference_root)
  422.     fu_each_src_dest(src, dest) do |s, d|
  423.       copy_entry s, d, options[:preserve], options[:dereference_root]
  424.     end
  425.   end
  426.   module_function :cp_r
  427.  
  428.   OPT_TABLE['cp_r'] = %w( noop verbose preserve dereference_root )
  429.  
  430.   #
  431.   # Copies a file system entry +src+ to +dest+.
  432.   # If +src+ is a directory, this method copies its contents recursively.
  433.   # This method preserves file types, c.f. symlink, directory...
  434.   # (FIFO, device files and etc. are not supported yet)
  435.   #
  436.   # Both of +src+ and +dest+ must be a path name.
  437.   # +src+ must exist, +dest+ must not exist.
  438.   #
  439.   # If +preserve+ is true, this method preserves owner, group, permissions
  440.   # and modified time.
  441.   #
  442.   # If +dereference_root+ is true, this method dereference tree root.
  443.   #
  444.   def copy_entry(src, dest, preserve = false, dereference_root = false)
  445.     Entry_.new(src, nil, dereference_root).traverse do |ent|
  446.       destent = Entry_.new(dest, ent.rel, false)
  447.       ent.copy destent.path
  448.       ent.copy_metadata destent.path if preserve
  449.     end
  450.   end
  451.   module_function :copy_entry
  452.  
  453.   #
  454.   # Copies file contents of +src+ to +dest+.
  455.   # Both of +src+ and +dest+ must be a path name.
  456.   #
  457.   def copy_file(src, dest, preserve = false, dereference = true)
  458.     ent = Entry_.new(src, nil, dereference)
  459.     ent.copy_file dest
  460.     ent.copy_metadata dest if preserve
  461.   end
  462.   module_function :copy_file
  463.  
  464.   #
  465.   # Copies stream +src+ to +dest+.
  466.   # +src+ must respond to #read(n) and
  467.   # +dest+ must respond to #write(str).
  468.   #
  469.   def copy_stream(src, dest)
  470.     fu_copy_stream0 src, dest, fu_stream_blksize(src, dest)
  471.   end
  472.   module_function :copy_stream
  473.  
  474.   #
  475.   # Options: force noop verbose
  476.   #
  477.   # Moves file(s) +src+ to +dest+.  If +file+ and +dest+ exist on the different
  478.   # disk partition, the file is copied instead.
  479.   #
  480.   #   FileUtils.mv 'badname.rb', 'goodname.rb'
  481.   #   FileUtils.mv 'stuff.rb', '/notexist/lib/ruby', :force => true  # no error
  482.   #
  483.   #   FileUtils.mv %w(junk.txt dust.txt), '/home/aamine/.trash/'
  484.   #   FileUtils.mv Dir.glob('test*.rb'), 'test', :noop => true, :verbose => true
  485.   #
  486.   def mv(src, dest, options = {})
  487.     fu_check_options options, :force, :noop, :verbose
  488.     fu_output_message "mv#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
  489.     return if options[:noop]
  490.     fu_each_src_dest(src, dest) do |s, d|
  491.       destent = Entry_.new(d, nil, true)
  492.       begin
  493.         if destent.exist?
  494.           if destent.directory?
  495.             raise Errno::EEXIST, dest
  496.           else
  497.             destent.remove_file if rename_cannot_overwrite_file?
  498.           end
  499.         end
  500.         begin
  501.           File.rename s, d
  502.         rescue Errno::EXDEV
  503.           copy_entry s, d, true
  504.         end
  505.       rescue SystemCallError
  506.         raise unless options[:force]
  507.       end
  508.     end
  509.   end
  510.   module_function :mv
  511.  
  512.   alias move mv
  513.   module_function :move
  514.  
  515.   OPT_TABLE['mv']   =
  516.   OPT_TABLE['move'] = %w( noop verbose force )
  517.  
  518.   def rename_cannot_overwrite_file?   #:nodoc:
  519.     /djgpp|cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
  520.   end
  521.   private_module_function :rename_cannot_overwrite_file?
  522.  
  523.   #
  524.   # Options: force noop verbose
  525.   #
  526.   # Remove file(s) specified in +list+.  This method cannot remove directories.
  527.   # All StandardErrors are ignored when the :force option is set.
  528.   #
  529.   #   FileUtils.rm %w( junk.txt dust.txt )
  530.   #   FileUtils.rm Dir.glob('*.so')
  531.   #   FileUtils.rm 'NotExistFile', :force => true   # never raises exception
  532.   #
  533.   def rm(list, options = {})
  534.     fu_check_options options, :force, :noop, :verbose
  535.     list = fu_list(list)
  536.     fu_output_message "rm#{options[:force] ? ' -f' : ''} #{list.join ' '}" if options[:verbose]
  537.     return if options[:noop]
  538.  
  539.     list.each do |path|
  540.       remove_file path, options[:force]
  541.     end
  542.   end
  543.   module_function :rm
  544.  
  545.   alias remove rm
  546.   module_function :remove
  547.  
  548.   OPT_TABLE['rm']     =
  549.   OPT_TABLE['remove'] = %w( noop verbose force )
  550.  
  551.   #
  552.   # Options: noop verbose
  553.   #
  554.   # Equivalent to
  555.   #
  556.   #   #rm(list, :force => true)
  557.   #
  558.   def rm_f(list, options = {})
  559.     fu_check_options options, :noop, :verbose
  560.     options = options.dup
  561.     options[:force] = true
  562.     rm list, options
  563.   end
  564.   module_function :rm_f
  565.  
  566.   alias safe_unlink rm_f
  567.   module_function :safe_unlink
  568.  
  569.   OPT_TABLE['rm_f']        =
  570.   OPT_TABLE['safe_unlink'] = %w( noop verbose )
  571.  
  572.   #
  573.   # Options: force noop verbose secure
  574.   #
  575.   # remove files +list+[0] +list+[1]... If +list+[n] is a directory,
  576.   # removes its all contents recursively. This method ignores
  577.   # StandardError when :force option is set.
  578.   #
  579.   #   FileUtils.rm_r Dir.glob('/tmp/*')
  580.   #   FileUtils.rm_r '/', :force => true          #  :-)
  581.   #
  582.   # WARNING: This method causes local vulnerability
  583.   # if one of parent directories or removing directory tree are world
  584.   # writable (including /tmp, whose permission is 1777), and the current
  585.   # process has strong privilege such as Unix super user (root), and the
  586.   # system has symbolic link.  For secure removing, read the documentation
  587.   # of #remove_entry_secure carefully, and set :secure option to true.
  588.   # Default is :secure=>false.
  589.   #
  590.   # NOTE: This method calls #remove_entry_secure if :secure option is set.
  591.   # See also #remove_entry_secure.
  592.   #
  593.   def rm_r(list, options = {})
  594.     fu_check_options options, :force, :noop, :verbose, :secure
  595.     # options[:secure] = true unless options.key?(:secure)
  596.     list = fu_list(list)
  597.     fu_output_message "rm -r#{options[:force] ? 'f' : ''} #{list.join ' '}" if options[:verbose]
  598.     return if options[:noop]
  599.     list.each do |path|
  600.       if options[:secure]
  601.         remove_entry_secure path, options[:force]
  602.       else
  603.         remove_entry path, options[:force]
  604.       end
  605.     end
  606.   end
  607.   module_function :rm_r
  608.  
  609.   OPT_TABLE['rm_r'] = %w( noop verbose force secure )
  610.  
  611.   #
  612.   # Options: noop verbose secure
  613.   #
  614.   # Equivalent to
  615.   #
  616.   #   #rm_r(list, :force => true)
  617.   #
  618.   # WARNING: This method causes local vulnerability.
  619.   # Read the documentation of #rm_r first.
  620.   #
  621.   def rm_rf(list, options = {})
  622.     fu_check_options options, :noop, :verbose, :secure
  623.     options = options.dup
  624.     options[:force] = true
  625.     rm_r list, options
  626.   end
  627.   module_function :rm_rf
  628.  
  629.   alias rmtree rm_rf
  630.   module_function :rmtree
  631.  
  632.   OPT_TABLE['rm_rf']  =
  633.   OPT_TABLE['rmtree'] = %w( noop verbose secure )
  634.  
  635.   #
  636.   # This method removes a file system entry +path+.  +path+ shall be a
  637.   # regular file, a directory, or something.  If +path+ is a directory,
  638.   # remove it recursively.  This method is required to avoid TOCTTOU
  639.   # (time-of-check-to-time-of-use) local security vulnerability of #rm_r.
  640.   # #rm_r causes security hole when:
  641.   #
  642.   #   * Parent directory is world writable (including /tmp).
  643.   #   * Removing directory tree includes world writable directory.
  644.   #   * The system has symbolic link.
  645.   #
  646.   # To avoid this security hole, this method applies special preprocess.
  647.   # If +path+ is a directory, this method chown(2) and chmod(2) all
  648.   # removing directories.  This requires the current process is the
  649.   # owner of the removing whole directory tree, or is the super user (root).
  650.   #
  651.   # WARNING: You must ensure that *ALL* parent directories are not
  652.   # world writable.  Otherwise this method does not work.
  653.   # Only exception is temporary directory like /tmp and /var/tmp,
  654.   # whose permission is 1777.
  655.   #
  656.   # WARNING: Only the owner of the removing directory tree, or Unix super
  657.   # user (root) should invoke this method.  Otherwise this method does not
  658.   # work.
  659.   #
  660.   # For details of this security vulnerability, see Perl's case:
  661.   #
  662.   #   http://www.cve.mitre.org/cgi-bin/cvenam ... -2005-0448
  663.   #   http://www.cve.mitre.org/cgi-bin/cvenam ... -2004-0452
  664.   #
  665.   # For fileutils.rb, this vulnerability is reported in [ruby-dev:26100].
  666.   #
  667.   def remove_entry_secure(path, force = false)
  668.     unless fu_have_symlink?
  669.       remove_entry path, force
  670.       return
  671.     end
  672.     fullpath = File.expand_path(path)
  673.     st = File.lstat(fullpath)
  674.     unless st.directory?
  675.       File.unlink fullpath
  676.       return
  677.     end
  678.     # is a directory.
  679.     parent_st = File.stat(File.dirname(fullpath))
  680.     unless fu_world_writable?(parent_st)
  681.       remove_entry path, force
  682.       return
  683.     end
  684.     unless parent_st.sticky?
  685.       raise ArgumentError, "parent directory is world writable, FileUtils#remove_entry_secure does not work; abort: #{path.inspect} (parent directory mode #{'%o' % parent_st.mode})"
  686.     end
  687.     # freeze tree root
  688.     euid = Process.euid
  689.     File.open(fullpath + '/.') {|f|
  690.       unless fu_stat_identical_entry?(st, f.stat)
  691.         # symlink (TOC-to-TOU attack?)
  692.         File.unlink fullpath
  693.         return
  694.       end
  695.       f.chown euid, -1
  696.       f.chmod 0700
  697.     }
  698.     # ---- tree root is frozen ----
  699.     root = Entry_.new(path)
  700.     root.preorder_traverse do |ent|
  701.       if ent.directory?
  702.         ent.chown euid, -1
  703.         ent.chmod 0700
  704.       end
  705.     end
  706.     root.postorder_traverse do |ent|
  707.       begin
  708.         ent.remove
  709.       rescue
  710.         raise unless force
  711.       end
  712.     end
  713.   rescue
  714.     raise unless force
  715.   end
  716.   module_function :remove_entry_secure
  717.  
  718.   def fu_world_writable?(st)
  719.     (st.mode & 0002) != 0
  720.   end
  721.  
  722.   def fu_have_symlink?   #:nodoc
  723.     File.symlink nil, nil
  724.   rescue NotImplementedError
  725.     return false
  726.   rescue
  727.     return true
  728.   end
  729.   private_module_function :fu_have_symlink?
  730.  
  731.   def fu_stat_identical_entry?(a, b)   #:nodoc:
  732.     a.dev == b.dev and a.ino == b.ino
  733.   end
  734.   private_module_function :fu_stat_identical_entry?
  735.  
  736.   #
  737.   # This method removes a file system entry +path+.
  738.   # +path+ might be a regular file, a directory, or something.
  739.   # If +path+ is a directory, remove it recursively.
  740.   #
  741.   # See also #remove_entry_secure.
  742.   #
  743.   def remove_entry(path, force = false)
  744.     Entry_.new(path).postorder_traverse do |ent|
  745.       begin
  746.         ent.remove
  747.       rescue
  748.         raise unless force
  749.       end
  750.     end
  751.   rescue
  752.     raise unless force
  753.   end
  754.   module_function :remove_entry
  755.  
  756.   #
  757.   # Removes a file +path+.
  758.   # This method ignores StandardError if +force+ is true.
  759.   #
  760.   def remove_file(path, force = false)
  761.     Entry_.new(path).remove_file
  762.   rescue
  763.     raise unless force
  764.   end
  765.   module_function :remove_file
  766.  
  767.   #
  768.   # Removes a directory +dir+ and its contents recursively.
  769.   # This method ignores StandardError if +force+ is true.
  770.   #
  771.   def remove_dir(path, force = false)
  772.     remove_entry path, force   # FIXME?? check if it is a directory
  773.   end
  774.   module_function :remove_dir
  775.  
  776.   #
  777.   # Returns true if the contents of a file A and a file B are identical.
  778.   #
  779.   #   FileUtils.compare_file('somefile', 'somefile')  #=> true
  780.   #   FileUtils.compare_file('/bin/cp', '/bin/mv')    #=> maybe false
  781.   #
  782.   def compare_file(a, b)
  783.     return false unless File.size(a) == File.size(b)
  784.     File.open(a, 'rb') {|fa|
  785.       File.open(b, 'rb') {|fb|
  786.         return compare_stream(fa, fb)
  787.       }
  788.     }
  789.   end
  790.   module_function :compare_file
  791.  
  792.   alias identical? compare_file
  793.   alias cmp compare_file
  794.   module_function :identical?
  795.   module_function :cmp
  796.  
  797.   #
  798.   # Returns true if the contents of a stream +a+ and +b+ are identical.
  799.   #
  800.   def compare_stream(a, b)
  801.     bsize = fu_stream_blksize(a, b)
  802.     sa = sb = nil
  803.     while sa == sb
  804.       sa = a.read(bsize)
  805.       sb = b.read(bsize)
  806.       unless sa and sb
  807.         if sa.nil? and sb.nil?
  808.           return true
  809.         end
  810.       end
  811.     end
  812.     false
  813.   end
  814.   module_function :compare_stream
  815.  
  816.   #
  817.   # Options: mode noop verbose
  818.   #
  819.   # If +src+ is not same as +dest+, copies it and changes the permission
  820.   # mode to +mode+.  If +dest+ is a directory, destination is +dest+/+src+.
  821.   #
  822.   #   FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true
  823.   #   FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true
  824.   #
  825.   def install(src, dest, options = {})
  826.     fu_check_options options, :mode, :preserve, :noop, :verbose
  827.     fu_output_message "install -c#{options[:preserve] && ' -p'}#{options[:mode] ? (' -m 0%o' % options[:mode]) : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
  828.     return if options[:noop]
  829.     fu_each_src_dest(src, dest) do |s, d|
  830.       unless File.exist?(d) and compare_file(s, d)
  831.         remove_file d, true
  832.         st = File.stat(s) if options[:preserve]
  833.         copy_file s, d
  834.         File.utime st.atime, st.mtime, d if options[:preserve]
  835.         File.chmod options[:mode], d if options[:mode]
  836.       end
  837.     end
  838.   end
  839.   module_function :install
  840.  
  841.   OPT_TABLE['install'] = %w( noop verbose preserve mode )
  842.  
  843.   #
  844.   # Options: noop verbose
  845.   #
  846.   # Changes permission bits on the named files (in +list+) to the bit pattern
  847.   # represented by +mode+.
  848.   #
  849.   #   FileUtils.chmod 0755, 'somecommand'
  850.   #   FileUtils.chmod 0644, %w(my.rb your.rb his.rb her.rb)
  851.   #   FileUtils.chmod 0755, '/usr/bin/ruby', :verbose => true
  852.   #
  853.   def chmod(mode, list, options = {})
  854.     fu_check_options options, :noop, :verbose
  855.     list = fu_list(list)
  856.     fu_output_message sprintf('chmod %o %s', mode, list.join(' ')) if options[:verbose]
  857.     return if options[:noop]
  858.     list.each do |path|
  859.       Entry_.new(path).chmod mode
  860.     end
  861.   end
  862.   module_function :chmod
  863.  
  864.   OPT_TABLE['chmod'] = %w( noop verbose )
  865.  
  866.   #
  867.   # Options: noop verbose force
  868.   #
  869.   # Changes permission bits on the named files (in +list+)
  870.   # to the bit pattern represented by +mode+.
  871.   #
  872.   #   FileUtils.chmod_R 0700, "/tmp/app.#{$$}"
  873.   #
  874.   def chmod_R(mode, list, options = {})
  875.     fu_check_options options, :noop, :verbose, :force
  876.     list = fu_list(list)
  877.     fu_output_message sprintf('chmod -R%s %o %s',
  878.                               (options[:force] ? 'f' : ''),
  879.                               mode, list.join(' ')) if options[:verbose]
  880.     return if options[:noop]
  881.     list.each do |root|
  882.       Entry_.new(root).traverse do |ent|
  883.         begin
  884.           ent.chmod mode
  885.         rescue
  886.           raise unless options[:force]
  887.         end
  888.       end
  889.     end
  890.   end
  891.   module_function :chmod_R
  892.  
  893.   OPT_TABLE['chmod_R'] = %w( noop verbose )
  894.  
  895.   #
  896.   # Options: noop verbose
  897.   #
  898.   # Changes owner and group on the named files (in +list+)
  899.   # to the user +user+ and the group +group+.  +user+ and +group+
  900.   # may be an ID (Integer/String) or a name (String).
  901.   # If +user+ or +group+ is nil, this method does not change
  902.   # the attribute.
  903.   #
  904.   #   FileUtils.chown 'root', 'staff', '/usr/local/bin/ruby'
  905.   #   FileUtils.chown nil, 'bin', Dir.glob('/usr/bin/*'), :verbose => true
  906.   #
  907.   def chown(user, group, list, options = {})
  908.     fu_check_options options, :noop, :verbose
  909.     list = fu_list(list)
  910.     fu_output_message sprintf('chown %s%s',
  911.                               [user,group].compact.join(':') + ' ',
  912.                               list.join(' ')) if options[:verbose]
  913.     return if options[:noop]
  914.     uid = fu_get_uid(user)
  915.     gid = fu_get_gid(group)
  916.     list.each do |path|
  917.       Entry_.new(path).chown uid, gid
  918.     end
  919.   end
  920.   module_function :chown
  921.  
  922.   OPT_TABLE['chown'] = %w( noop verbose )
  923.  
  924.   #
  925.   # Options: noop verbose force
  926.   #
  927.   # Changes owner and group on the named files (in +list+)
  928.   # to the user +user+ and the group +group+ recursively.
  929.   # +user+ and +group+ may be an ID (Integer/String) or
  930.   # a name (String).  If +user+ or +group+ is nil, this
  931.   # method does not change the attribute.
  932.   #
  933.   #   FileUtils.chown_R 'www', 'www', '/var/www/htdocs'
  934.   #   FileUtils.chown_R 'cvs', 'cvs', '/var/cvs', :verbose => true
  935.   #
  936.   def chown_R(user, group, list, options = {})
  937.     fu_check_options options, :noop, :verbose, :force
  938.     list = fu_list(list)
  939.     fu_output_message sprintf('chown -R%s %s%s',
  940.                               (options[:force] ? 'f' : ''),
  941.                               [user,group].compact.join(':') + ' ',
  942.                               list.join(' ')) if options[:verbose]
  943.     return if options[:noop]
  944.     uid = fu_get_uid(user)
  945.     gid = fu_get_gid(group)
  946.     return unless uid or gid
  947.     list.each do |root|
  948.       Entry_.new(root).traverse do |ent|
  949.         begin
  950.           ent.chown uid, gid
  951.         rescue
  952.           raise unless options[:force]
  953.         end
  954.       end
  955.     end
  956.   end
  957.   module_function :chown_R
  958.  
  959.   OPT_TABLE['chown_R'] = %w( noop verbose )
  960.  
  961.   begin
  962.     require 'etc'
  963.  
  964.     def fu_get_uid(user)   #:nodoc:
  965.       return nil unless user
  966.       user = user.to_s
  967.       if /\A\d+\z/ =~ user
  968.       then user.to_i
  969.       else Etc.getpwnam(user).uid
  970.       end
  971.     end
  972.     private_module_function :fu_get_uid
  973.  
  974.     def fu_get_gid(group)   #:nodoc:
  975.       return nil unless group
  976.       if /\A\d+\z/ =~ group
  977.       then group.to_i
  978.       else Etc.getgrnam(group).gid
  979.       end
  980.     end
  981.     private_module_function :fu_get_gid
  982.  
  983.   rescue LoadError
  984.     # need Win32 support???
  985.  
  986.     def fu_get_uid(user)   #:nodoc:
  987.       user    # FIXME
  988.     end
  989.     private_module_function :fu_get_uid
  990.  
  991.     def fu_get_gid(group)   #:nodoc:
  992.       group   # FIXME
  993.     end
  994.     private_module_function :fu_get_gid
  995.   end
  996.  
  997.   #
  998.   # Options: noop verbose
  999.   #
  1000.   # Updates modification time (mtime) and access time (atime) of file(s) in
  1001.   # +list+.  Files are created if they don't exist.
  1002.   #
  1003.   #   FileUtils.touch 'timestamp'
  1004.   #   FileUtils.touch Dir.glob('*.c');  system 'make'
  1005.   #
  1006.   def touch(list, options = {})
  1007.     fu_check_options options, :noop, :verbose
  1008.     list = fu_list(list)
  1009.     fu_output_message "touch #{list.join ' '}" if options[:verbose]
  1010.     return if options[:noop]
  1011.     t = Time.now
  1012.     list.each do |path|
  1013.       begin
  1014.         File.utime(t, t, path)
  1015.       rescue Errno::ENOENT
  1016.         File.open(path, 'a') {
  1017.           ;
  1018.         }
  1019.       end
  1020.     end
  1021.   end
  1022.   module_function :touch
  1023.  
  1024.   OPT_TABLE['touch'] = %w( noop verbose )
  1025.  
  1026.   private
  1027.  
  1028.   module StreamUtils_
  1029.     private
  1030.  
  1031.     def fu_windows?
  1032.       /mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
  1033.     end
  1034.  
  1035.     def fu_copy_stream0(src, dest, blksize)   #:nodoc:
  1036.       # FIXME: readpartial?
  1037.       while s = src.read(blksize)
  1038.         dest.write s
  1039.       end
  1040.     end
  1041.  
  1042.     def fu_stream_blksize(*streams)
  1043.       streams.each do |s|
  1044.         next unless s.respond_to?(:stat)
  1045.         size = fu_blksize(s.stat)
  1046.         return size if size
  1047.       end
  1048.       fu_default_blksize()
  1049.     end
  1050.  
  1051.     def fu_blksize(st)
  1052.       s = st.blksize
  1053.       return nil unless s
  1054.       return nil if s == 0
  1055.       s
  1056.     end
  1057.  
  1058.     def fu_default_blksize
  1059.       1024
  1060.     end
  1061.   end
  1062.  
  1063.   include StreamUtils_
  1064.   extend StreamUtils_
  1065.  
  1066.   class Entry_   #:nodoc: internal use only
  1067.     include StreamUtils_
  1068.  
  1069.     def initialize(a, b = nil, deref = false)
  1070.       @prefix = @rel = @path = nil
  1071.       if b
  1072.         @prefix = a
  1073.         @rel = b
  1074.       else
  1075.         @path = a
  1076.       end
  1077.       @deref = deref
  1078.       @stat = nil
  1079.       @lstat = nil
  1080.     end
  1081.  
  1082.     def inspect
  1083.       "\#<#{self.class} #{path()}>"
  1084.     end
  1085.  
  1086.     def path
  1087.       if @path
  1088.         @path.to_str
  1089.       else
  1090.         join(@prefix, @rel)
  1091.       end
  1092.     end
  1093.  
  1094.     def prefix
  1095.       @prefix || @path
  1096.     end
  1097.  
  1098.     def rel
  1099.       @rel
  1100.     end
  1101.  
  1102.     def dereference?
  1103.       @deref
  1104.     end
  1105.  
  1106.     def exist?
  1107.       lstat! ? true : false
  1108.     end
  1109.  
  1110.     def file?
  1111.       s = lstat!
  1112.       s and s.file?
  1113.     end
  1114.  
  1115.     def directory?
  1116.       s = lstat!
  1117.       s and s.directory?
  1118.     end
  1119.  
  1120.     def symlink?
  1121.       s = lstat!
  1122.       s and s.symlink?
  1123.     end
  1124.  
  1125.     def chardev?
  1126.       s = lstat!
  1127.       s and s.chardev?
  1128.     end
  1129.  
  1130.     def blockdev?
  1131.       s = lstat!
  1132.       s and s.blockdev?
  1133.     end
  1134.  
  1135.     def socket?
  1136.       s = lstat!
  1137.       s and s.socket?
  1138.     end
  1139.  
  1140.     def pipe?
  1141.       s = lstat!
  1142.       s and s.pipe?
  1143.     end
  1144.  
  1145.     S_IF_DOOR = 0xD000
  1146.  
  1147.     def door?
  1148.       s = lstat!
  1149.       s and (s.mode & 0xF000 == S_IF_DOOR)
  1150.     end
  1151.  
  1152.     def entries
  1153.       Dir.entries(path())\
  1154.           .reject {|n| n == '.' or n == '..' }\
  1155.           .map {|n| Entry_.new(prefix(), join(rel(), n.untaint)) }
  1156.     end
  1157.  
  1158.     def stat
  1159.       return @stat if @stat
  1160.       if lstat() and lstat().symlink?
  1161.         @stat = File.stat(path())
  1162.       else
  1163.         @stat = lstat()
  1164.       end
  1165.       @stat
  1166.     end
  1167.  
  1168.     def stat!
  1169.       return @stat if @stat
  1170.       if lstat! and lstat!.symlink?
  1171.         @stat = File.stat(path())
  1172.       else
  1173.         @stat = lstat!
  1174.       end
  1175.       @stat
  1176.     rescue SystemCallError
  1177.       nil
  1178.     end
  1179.  
  1180.     def lstat
  1181.       if dereference?
  1182.         @lstat ||= File.stat(path())
  1183.       else
  1184.         @lstat ||= File.lstat(path())
  1185.       end
  1186.     end
  1187.  
  1188.     def lstat!
  1189.       lstat()
  1190.     rescue SystemCallError
  1191.       nil
  1192.     end
  1193.  
  1194.     def chmod(mode)
  1195.       if symlink?
  1196.         File.lchmod mode, path() if have_lchmod?
  1197.       else
  1198.         File.chmod mode, path()
  1199.       end
  1200.     end
  1201.  
  1202.     def chown(uid, gid)
  1203.       if symlink?
  1204.         File.lchown uid, gid, path() if have_lchown?
  1205.       else
  1206.         File.chown uid, gid, path()
  1207.       end
  1208.     end
  1209.  
  1210.     def copy(dest)
  1211.       case
  1212.       when file?
  1213.         copy_file dest
  1214.       when directory?
  1215.         begin
  1216.           Dir.mkdir dest
  1217.         rescue
  1218.           raise unless File.directory?(dest)
  1219.         end
  1220.       when symlink?
  1221.         File.symlink File.readlink(path()), dest
  1222.       when chardev?
  1223.         raise "cannot handle device file" unless File.respond_to?(:mknod)
  1224.         mknod dest, ?c, 0666, lstat().rdev
  1225.       when blockdev?
  1226.         raise "cannot handle device file" unless File.respond_to?(:mknod)
  1227.         mknod dest, ?b, 0666, lstat().rdev
  1228.       when socket?
  1229.         raise "cannot handle socket" unless File.respond_to?(:mknod)
  1230.         mknod dest, nil, lstat().mode, 0
  1231.       when pipe?
  1232.         raise "cannot handle FIFO" unless File.respond_to?(:mkfifo)
  1233.         mkfifo dest, 0666
  1234.       when door?
  1235.         raise "cannot handle door: #{path()}"
  1236.       else
  1237.         raise "unknown file type: #{path()}"
  1238.       end
  1239.     end
  1240.  
  1241.     def copy_file(dest)
  1242.       st = stat()
  1243.       File.open(path(),  'rb') {|r|
  1244.         File.open(dest, 'wb', st.mode) {|w|
  1245.           fu_copy_stream0 r, w, (fu_blksize(st) || fu_default_blksize())
  1246.         }
  1247.       }
  1248.     end
  1249.  
  1250.     def copy_metadata(path)
  1251.       st = lstat()
  1252.       File.utime st.atime, st.mtime, path
  1253.       begin
  1254.         File.chown st.uid, st.gid, path
  1255.       rescue Errno::EPERM
  1256.         # clear setuid/setgid
  1257.         File.chmod st.mode & 01777, path
  1258.       else
  1259.         File.chmod st.mode, path
  1260.       end
  1261.     end
  1262.  
  1263.     def remove
  1264.       if directory?
  1265.         remove_dir1
  1266.       else
  1267.         remove_file
  1268.       end
  1269.     end
  1270.  
  1271.     def remove_dir1
  1272.       platform_support {
  1273.         Dir.rmdir path().sub(%r</\z>, '')
  1274.       }
  1275.     end
  1276.  
  1277.     def remove_file
  1278.       platform_support {
  1279.         File.unlink path
  1280.       }
  1281.     end
  1282.  
  1283.     def platform_support
  1284.       return yield unless fu_windows?
  1285.       first_time_p = true
  1286.       begin
  1287.         yield
  1288.       rescue Errno::ENOENT
  1289.         raise
  1290.       rescue => err
  1291.         if first_time_p
  1292.           first_time_p = false
  1293.           begin
  1294.             File.chmod 0700, path()   # Windows does not have symlink
  1295.             retry
  1296.           rescue SystemCallError
  1297.           end
  1298.         end
  1299.         raise err
  1300.       end
  1301.     end
  1302.  
  1303.     def preorder_traverse
  1304.       stack = [self]
  1305.       while ent = stack.pop
  1306.         yield ent
  1307.         stack.concat ent.entries.reverse if ent.directory?
  1308.       end
  1309.     end
  1310.  
  1311.     alias traverse preorder_traverse
  1312.  
  1313.     def postorder_traverse
  1314.       if directory?
  1315.         entries().each do |ent|
  1316.           ent.postorder_traverse do |e|
  1317.             yield e
  1318.           end
  1319.         end
  1320.       end
  1321.       yield self
  1322.     end
  1323.  
  1324.     private
  1325.  
  1326.     $fileutils_rb_have_lchmod = nil
  1327.  
  1328.     def have_lchmod?
  1329.       # This is not MT-safe, but it does not matter.
  1330.       if $fileutils_rb_have_lchmod == nil
  1331.         $fileutils_rb_have_lchmod = check_have_lchmod?
  1332.       end
  1333.       $fileutils_rb_have_lchmod
  1334.     end
  1335.  
  1336.     def check_have_lchmod?
  1337.       return false unless File.respond_to?(:lchmod)
  1338.       File.lchmod 0
  1339.       return true
  1340.     rescue NotImplementedError
  1341.       return false
  1342.     end
  1343.  
  1344.     $fileutils_rb_have_lchown = nil
  1345.  
  1346.     def have_lchown?
  1347.       # This is not MT-safe, but it does not matter.
  1348.       if $fileutils_rb_have_lchown == nil
  1349.         $fileutils_rb_have_lchown = check_have_lchown?
  1350.       end
  1351.       $fileutils_rb_have_lchown
  1352.     end
  1353.  
  1354.     def check_have_lchown?
  1355.       return false unless File.respond_to?(:lchown)
  1356.       File.lchown nil, nil
  1357.       return true
  1358.     rescue NotImplementedError
  1359.       return false
  1360.     end
  1361.  
  1362.     def join(dir, base)
  1363.       return dir.to_str if not base or base == '.'
  1364.       return base.to_str if not dir or dir == '.'
  1365.       File.join(dir, base)
  1366.     end
  1367.   end   # class Entry_
  1368.  
  1369.   def fu_list(arg)   #:nodoc:
  1370.     [arg].flatten.map {|path| path.to_str }
  1371.   end
  1372.   private_module_function :fu_list
  1373.  
  1374.   def fu_each_src_dest(src, dest)   #:nodoc:
  1375.     fu_each_src_dest0(src, dest) do |s, d|
  1376.       raise ArgumentError, "same file: #{s} and #{d}" if fu_same?(s, d)
  1377.       yield s, d
  1378.     end
  1379.   end
  1380.   private_module_function :fu_each_src_dest
  1381.  
  1382.   def fu_each_src_dest0(src, dest)   #:nodoc:
  1383.     if src.is_a?(Array)
  1384.       src.each do |s|
  1385.         s = s.to_str
  1386.         yield s, File.join(dest, File.basename(s))
  1387.       end
  1388.     else
  1389.       src = src.to_str
  1390.       if File.directory?(dest)
  1391.         yield src, File.join(dest, File.basename(src))
  1392.       else
  1393.         yield src, dest.to_str
  1394.       end
  1395.     end
  1396.   end
  1397.   private_module_function :fu_each_src_dest0
  1398.  
  1399.   def fu_same?(a, b)   #:nodoc:
  1400.     if fu_have_st_ino?
  1401.       st1 = File.stat(a)
  1402.       st2 = File.stat(b)
  1403.       st1.dev == st2.dev and st1.ino == st2.ino
  1404.     else
  1405.       File.expand_path(a) == File.expand_path(b)
  1406.     end
  1407.   rescue Errno::ENOENT
  1408.     return false
  1409.   end
  1410.   private_module_function :fu_same?
  1411.  
  1412.   def fu_have_st_ino?   #:nodoc:
  1413.     not fu_windows?
  1414.   end
  1415.   private_module_function :fu_have_st_ino?
  1416.  
  1417.   def fu_check_options(options, *optdecl)   #:nodoc:
  1418.     h = options.dup
  1419.     optdecl.each do |name|
  1420.       h.delete name
  1421.     end
  1422.     raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty?
  1423.   end
  1424.   private_module_function :fu_check_options
  1425.  
  1426.   def fu_update_option(args, new)   #:nodoc:
  1427.     if args.last.is_a?(Hash)
  1428.       args[-1] = args.last.dup.update(new)
  1429.     else
  1430.       args.push new
  1431.     end
  1432.     args
  1433.   end
  1434.   private_module_function :fu_update_option
  1435.  
  1436.   @fileutils_output = $stderr
  1437.   @fileutils_label  = ''
  1438.  
  1439.   def fu_output_message(msg)   #:nodoc:
  1440.     @fileutils_output ||= $stderr
  1441.     @fileutils_label  ||= ''
  1442.     @fileutils_output.puts @fileutils_label + msg
  1443.   end
  1444.   private_module_function :fu_output_message
  1445.  
  1446.   METHODS = singleton_methods() - ['private_module_function']
  1447.  
  1448.   #
  1449.   # Returns an Array of method names which have any options.
  1450.   #
  1451.   #   p FileUtils.commands  #=> ["chmod", "cp", "cp_r", "install", ...]
  1452.   #
  1453.   def FileUtils.commands
  1454.     OPT_TABLE.keys
  1455.   end
  1456.  
  1457.   #
  1458.   # Returns an Array of option names.
  1459.   #
  1460.   #   p FileUtils.options  #=> ["noop", "force", "verbose", "preserve", "mode"]
  1461.   #
  1462.   def FileUtils.options
  1463.     OPT_TABLE.values.flatten.uniq
  1464.   end
  1465.  
  1466.   #
  1467.   # Returns true if the method +mid+ have an option +opt+.
  1468.   #
  1469.   #   p FileUtils.have_option?(:cp, :noop)     #=> true
  1470.   #   p FileUtils.have_option?(:rm, :force)    #=> true
  1471.   #   p FileUtils.have_option?(:rm, :perserve) #=> false
  1472.   #
  1473.   def FileUtils.have_option?(mid, opt)
  1474.     li = OPT_TABLE[mid.to_s] or raise ArgumentError, "no such method: #{mid}"
  1475.     li.include?(opt.to_s)
  1476.   end
  1477.  
  1478.   #
  1479.   # Returns an Array of option names of the method +mid+.
  1480.   #
  1481.   #   p FileUtils.options(:rm)  #=> ["noop", "verbose", "force"]
  1482.   #
  1483.   def FileUtils.options_of(mid)
  1484.     OPT_TABLE[mid.to_s]
  1485.   end
  1486.  
  1487.   #
  1488.   # Returns an Array of method names which have the option +opt+.
  1489.   #
  1490.   #   p FileUtils.collect_method(:preserve) #=> ["cp", "cp_r", "copy", "install"]
  1491.   #
  1492.   def FileUtils.collect_method(opt)
  1493.     OPT_TABLE.keys.select {|m| OPT_TABLE[m].include?(opt.to_s) }
  1494.   end
  1495.  
  1496.   #
  1497.   # This module has all methods of FileUtils module, but it outputs messages
  1498.   # before acting.  This equates to passing the <tt>:verbose</tt> flag to
  1499.   # methods in FileUtils.
  1500.   #
  1501.   module Verbose
  1502.     include FileUtils
  1503.     @fileutils_output  = $stderr
  1504.     @fileutils_label   = ''
  1505.     ::FileUtils.collect_method('verbose').each do |name|
  1506.       module_eval(<<-EOS, __FILE__, __LINE__ + 1)
  1507.         def #{name}(*args)
  1508.           super(*fu_update_option(args, :verbose => true))
  1509.         end
  1510.         private :#{name}
  1511.       EOS
  1512.     end
  1513.     extend self
  1514.     class << self
  1515.       ::FileUtils::METHODS.each do |m|
  1516.         public m
  1517.       end
  1518.     end
  1519.   end
  1520.  
  1521.   #
  1522.   # This module has all methods of FileUtils module, but never changes
  1523.   # files/directories.  This equates to passing the <tt>:noop</tt> flag
  1524.   # to methods in FileUtils.
  1525.   #
  1526.   module NoWrite
  1527.     include FileUtils
  1528.     @fileutils_output  = $stderr
  1529.     @fileutils_label   = ''
  1530.     ::FileUtils.collect_method('noop').each do |name|
  1531.       module_eval(<<-EOS, __FILE__, __LINE__ + 1)
  1532.         def #{name}(*args)
  1533.           super(*fu_update_option(args, :noop => true))
  1534.         end
  1535.         private :#{name}
  1536.       EOS
  1537.     end
  1538.     extend self
  1539.     class << self
  1540.       ::FileUtils::METHODS.each do |m|
  1541.         public m
  1542.       end
  1543.     end
  1544.   end
  1545.  
  1546.   #
  1547.   # This module has all methods of FileUtils module, but never changes
  1548.   # files/directories, with printing message before acting.
  1549.   # This equates to passing the <tt>:noop</tt> and <tt>:verbose</tt> flag
  1550.   # to methods in FileUtils.
  1551.   #
  1552.   module DryRun
  1553.     include FileUtils
  1554.     @fileutils_output  = $stderr
  1555.     @fileutils_label   = ''
  1556.     ::FileUtils.collect_method('noop').each do |name|
  1557.       module_eval(<<-EOS, __FILE__, __LINE__ + 1)
  1558.         def #{name}(*args)
  1559.           super(*fu_update_option(args, :noop => true, :verbose => true))
  1560.         end
  1561.         private :#{name}
  1562.       EOS
  1563.     end
  1564.     extend self
  1565.     class << self
  1566.       ::FileUtils::METHODS.each do |m|
  1567.         public m
  1568.       end
  1569.     end
  1570.   end
  1571. end
Expand to see the code.

Font : Auto-Install Spoiler
  1. #===============================================================================
  2. # ** Font : Auto-Install
  3. #-------------------------------------------------------------------------------
  4. # Author      : Kain Nobel © (Credit to Woratana for origional concept)
  5. # Version     : 3.5
  6. # Last Update : 2009.06.20
  7. # Requirement : "FileUtil" Ruby class (by Minero Aoki <[email hidden]>)
  8. #===============================================================================
  9. # * Description
  10. #-------------------------------------------------------------------------------
  11. #   Automatically installs one or more fonts when the game is ran, so the player
  12. #   doesn't have to be bothered with it. The process is not only quick but also
  13. #   transparent, meaning the player won't notice it (unless you set Notify true)
  14. #
  15. #   Notification can be sent allowing the player to know whats going on. A list
  16. #   of successful, failed to install and not found fonts will be displayed for
  17. #   fonts that aren't already present on the user's machine. If all the fonts
  18. #   are already installed, then everything is good to go and no message is sent.
  19. #
  20. #   Lastly, if an error occurs where the player doesn't have permission to
  21. #   install fonts on their machine, a prompt will come up letting them know
  22. #   that they must log in as administrator, or point them to the directory of
  23. #   the fonts and tell them what to do.
  24. #===============================================================================
  25. # * Version History
  26. #-------------------------------------------------------------------------------
  27. #   Version 1.1 (2006.05.26)
  28. #     - Woratana's origional version (found in Master of the Wind Arc III)
  29. #   Version 2.0 (2007.12.16)
  30. #     - Changed the common name of "Fonts" module to "FontAutoInstall" in order
  31. #       to prevent confusion with possible scripts that have their own "Fonts"
  32. #       module.
  33. #     - Replaced "Filenames" and "Names" array constants with single "Fonts"
  34. #       hash constant.
  35. #   Version 2.5 (2008.05.13)
  36. #     - Removed Scene_Title additional coding, imported it to be handled through
  37. #       "FontAutoInstall" module directly.
  38. #     - Aliased Graphics.update to call FontAutoInstall.install once.
  39. #   Version 3.0 (2008.12.17)
  40. #     - Fixed "Critical Address at..." error that occured when installing files
  41. #       with extensions other than '.ttf'.
  42. #     - For fonts that are already installed, you no longer get a "Not Found"
  43. #       error message just because the font/directory was deleted.
  44. #     - Fixed error with font file keys and their 'frozen' status.
  45. #   Version 3.5 (2009.06.21)
  46. #     - Deleted and re-scripted entire system from scratch again! ;)
  47. #     - System should be compatible on most Ruby environments, not just RM! :)
  48. #===============================================================================
  49. # * Install
  50. #-------------------------------------------------------------------------------
  51. #   Place the script above "main" and below the "FileUtils" module if you're not
  52. # using 'require' for the actual FileUtils.rb file.
  53. #===============================================================================
  54.  
  55. #-------------------------------------------------------------------------------
  56. # * SDK Log
  57. #-------------------------------------------------------------------------------
  58. if Object.const_defined?(:SDK)
  59.   SDK.log('Font.Auto-Install', 'Kain Nobel ©', 3.5, '2009.06.17')
  60. end
  61.  
  62. #===============================================================================
  63. # ** FontAutoInstall
  64. #===============================================================================
  65.  
  66. module FontAutoInstall
  67. #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
  68. # ~**                CUSTOMIZABLE CONSTANTS - BEGIN                        **~ #
  69. #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
  70.   #-----------------------------------------------------------------------------
  71.   # * Fonts to be installed (example Fonts['filename.extension'] = 'font name')
  72.   #-----------------------------------------------------------------------------
  73.   Fonts = Hash.new
  74.   Fonts['FIRESTARTER.ttf']           = "Firestarter"
  75.   #-----------------------------------------------------------------------------
  76.   # * Do you want to print a message when font has been installed?
  77.   #-----------------------------------------------------------------------------
  78.   Notify  = true
  79.   #-----------------------------------------------------------------------------
  80.   # * Automatically reset game if new fonts were installed?
  81.   #-----------------------------------------------------------------------------
  82.   Reload  = true
  83.   #-----------------------------------------------------------------------------
  84.   # * Directory (within project's folder) to find the font file(s).
  85.   #-----------------------------------------------------------------------------
  86.   Source = 'Fonts/'
  87. #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
  88. # ~**                 CUSTOMIZABLE CONSTANTS - END                         **~ #
  89. #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
  90.   #-----------------------------------------------------------------------------
  91.   # * Module Function
  92.   #-----------------------------------------------------------------------------
  93.   module_function
  94.   #-----------------------------------------------------------------------------
  95.   # * Location to Install the Font.
  96.   #-----------------------------------------------------------------------------
  97.   Path = "#{ENV['SystemRoot']}\\Fonts\\"
  98.   #-----------------------------------------------------------------------------
  99.   # * Win32API Declaration
  100.   #-----------------------------------------------------------------------------
  101.   AddFontResource    = Win32API.new('gdi32',   'AddFontResource',   'P',   'L')
  102.   WriteProfileString = Win32API.new('kernel32','WriteProfileString','PPP', 'L')
  103.   SendMessage        = Win32API.new('user32',  'SendMessage',       'LLLL','L')
  104.   #-----------------------------------------------------------------------------
  105.   # * Module Function
  106.   #-----------------------------------------------------------------------------
  107.   module_function
  108.   #-----------------------------------------------------------------------------
  109.   # * FontAutoInstall.install
  110.   #-----------------------------------------------------------------------------
  111.   def install
  112.     if Object.const_defined?(:FileUtils)
  113.       install_start
  114.       install_notify
  115.       install_finish
  116.       return
  117.     end
  118.     error = "There was an error loading required script..."
  119.     error += "\n\n  -> FileUtils\n\n...Now disabling Auto Font Installer."
  120.     print error
  121.   end
  122.   #-----------------------------------------------------------------------------
  123.   # * FontAutoInstall.install_start
  124.   #-----------------------------------------------------------------------------
  125.   def install_start
  126.     @success = Array.new
  127.     @failure = Array.new
  128.     @nofound = Array.new
  129.     @present = Array.new
  130.     Fonts.each_key do |filename|
  131.       if FileTest.exist?("#{Path}#{filename}")
  132.         @present << filename
  133.       end
  134.     end
  135.     Fonts.each_key {|filename| install_font(filename)}
  136.   end
  137.   #-----------------------------------------------------------------------------
  138.   # * FontAutoInstall.install_font
  139.   #-----------------------------------------------------------------------------
  140.   def install_font(file)
  141.     filename = file.dup
  142.     realname = Fonts[file].dup
  143.     unless FileTest.exists?("#{Path}#{filename}")
  144.       if FileTest.exists?("#{Source}#{filename}")
  145.         FileUtils.cp("#{Source}#{filename}", "#{Path}#{realname}")
  146.         AddFontResource.call("#{Path}#{filename}")
  147.         WriteProfileString.call('Fonts',"#{realname} (TrueType)", filename)
  148.         SendMessage.call(0xffff, 0x001D, 0, 0)
  149.         if FileTest.exist?("#{Path}#{filename}")
  150.           @success << [filename, realname]
  151.         else
  152.           @failure  << [filename, realname]
  153.         end
  154.       else
  155.         @nofound << [filename, realname]
  156.       end
  157.     end
  158.   end  
  159.   #-----------------------------------------------------------------------------
  160.   # * FontAutoInstall.install_notify
  161.   #-----------------------------------------------------------------------------
  162.   def install_notify
  163.     return unless Notify
  164.     @present.each do |font|
  165.       @success.delete(font)
  166.       @failure.delete(font)
  167.       @nofound.delete(font)
  168.     end
  169.     message = String.new
  170.     unless @success.empty?
  171.       message += "The following Font(s) were successfully installed...\n\n"
  172.       @success.sort.each {|font| message += "  -> '#{font[0]}' (#{font[1]})\n"}
  173.     end
  174.     unless @failure.empty?
  175.       message += "The following Font(s) failed to install...\n\n"
  176.       @failure.sort.each {|font| message += "  -> '#{font[0]}' (#{font[1]})\n"}
  177.     end
  178.     unless @nofound.empty?
  179.       message += "The following Font(s) weren't found...\n\n"
  180.       @nofound.sort.each {|font| message += "  -> \"#{font[0]}\" (#{font[1]})\n"}
  181.     end
  182.     unless message.empty?
  183.       print message
  184.       install_finish
  185.     end
  186.   end
  187.   #-----------------------------------------------------------------------------
  188.   # * FontAutoInstall.install_finish
  189.   #-----------------------------------------------------------------------------
  190.   def install_finish
  191.     unless @success.empty?
  192.       print "The game must be reset for installed fonts to display."
  193.       if Reload
  194.         Thread.new{system ("Game")}
  195.         exit
  196.       end
  197.     end
  198.   end
  199. end
  200.  
  201. #-------------------------------------------------------------------------------
  202. # * Call Installation
  203. #-------------------------------------------------------------------------------
  204.  
  205. begin
  206.   FontAutoInstall.install
  207. rescue Errno::EACCES
  208.   error =  "There was an error using :FontAutoInstall.install:\n\n User does no"
  209.   error += "t have permissions, please close the application and log in as admi"
  210.   error += "nistarator. If you happen to be running Windows Vista, then you can"
  211.   error += " right-click the application and choose \"Run As Administrator\", b"
  212.   error += "ut a password might be required for this option.\n\nThe fonts are s"
  213.   error += "tored in the following directory within the project...\n\n"
  214.   error += "\"#{Dir.getwd}/#{FontAutoInstall::Source}\"\n\nIf the source folder"
  215.   error += " for the fonts is accessible, you can try right-clicking the font f"
  216.   error += "ile and selecting the \"Install\" option in the drop-down box."
  217.   print error
  218. end
Expand to see the code.


Instructions

To be placed Below the FileUtils module, obviously above Main if you didn't already know! Don't forget to check out the Setup section, it explains itself nicely, you'll have to tell it which fonts to install, and what the system will name them.

Compatibility

This script should be compatible in any Ruby environment not just limited to XP and VX, so long as it is ran on a Windows operating system and not Linux or anything else. Since RM is a Windows-based program and can't be used/played in other OS's then this shouldn't be an issue at all :P

FAQ

Whats the difference between yours and Woratana's? Spoiler
The only real *major* difference is that mine doesn't have anything to do with Scene_Title while woratana's did. I basically made it so that it'd run in any other Ruby application besides a Maker, thats all...

That and alot of improved setup, coding and rescue clauses, I've prettymuch gave the script the makeover it ever-so-badly needed. All in all, it stays true to its origional functionality though, minus the fact that it did run in Scene_Title origionally :P


Credits and Thanks

Minero Aoki : Author of Ruby's FileUtils library which this script requires to move font to system folder.
Woratana : What little evidence is left, this script is kinda based off of his origional version still.
www.dafont.com : I made frequent visits to this place during testing, it has alot of pretty good free fonts!
Master of the Wind : Project I found Woratana's version in.

Author's Notes

This system works great, but there is issue when trying to install certain fonts which I'm unsure why. I've tested it with like 22 different fonts and there was 2 that it couldn't install, but every font I tried was of the same kind, .ttf (True Type Font) files so... yeah, there's a 1% chance that it won't install a certain font :P

If anybody knows why certain fonts won't auto-install with this script, then that'd be cool if you tell me. But in general, 99% of the fonts should install with these.

Terms and Conditions

Another "Free to use in commercial and non-commercial games" script, credit to both Woratana and [/i]Kain Nobel[/i] is required though, thanks :)
No posts? I'll be the first! Great job once again Kain. I love all of your scripts. I personally hated installing font styles...

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=65684