Skip to content

Commit 106ddaa

Browse files
authored
Merge pull request #33 from zverok/update-docs
Update the documentation content and formatting
2 parents 400574d + b701353 commit 106ddaa

File tree

1 file changed

+93
-74
lines changed

1 file changed

+93
-74
lines changed

lib/fileutils.rb

Lines changed: 93 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,56 @@
2424
#
2525
# require 'fileutils'
2626
#
27-
# FileUtils.cd(dir, options)
28-
# FileUtils.cd(dir, options) {|dir| block }
27+
# FileUtils.cd(dir, **options)
28+
# FileUtils.cd(dir, **options) {|dir| block }
2929
# FileUtils.pwd()
30-
# FileUtils.mkdir(dir, options)
31-
# FileUtils.mkdir(list, options)
32-
# FileUtils.mkdir_p(dir, options)
33-
# FileUtils.mkdir_p(list, options)
34-
# FileUtils.rmdir(dir, options)
35-
# FileUtils.rmdir(list, options)
36-
# FileUtils.ln(target, link, options)
37-
# FileUtils.ln(targets, dir, options)
38-
# FileUtils.ln_s(target, link, options)
39-
# FileUtils.ln_s(targets, dir, options)
40-
# FileUtils.ln_sf(target, link, options)
41-
# FileUtils.cp(src, dest, options)
42-
# FileUtils.cp(list, dir, options)
43-
# FileUtils.cp_r(src, dest, options)
44-
# FileUtils.cp_r(list, dir, options)
45-
# FileUtils.mv(src, dest, options)
46-
# FileUtils.mv(list, dir, options)
47-
# FileUtils.rm(list, options)
48-
# FileUtils.rm_r(list, options)
49-
# FileUtils.rm_rf(list, options)
50-
# FileUtils.install(src, dest, options)
51-
# FileUtils.chmod(mode, list, options)
52-
# FileUtils.chmod_R(mode, list, options)
53-
# FileUtils.chown(user, group, list, options)
54-
# FileUtils.chown_R(user, group, list, options)
55-
# FileUtils.touch(list, options)
30+
# FileUtils.mkdir(dir, **options)
31+
# FileUtils.mkdir(list, **options)
32+
# FileUtils.mkdir_p(dir, **options)
33+
# FileUtils.mkdir_p(list, **options)
34+
# FileUtils.rmdir(dir, **options)
35+
# FileUtils.rmdir(list, **options)
36+
# FileUtils.ln(target, link, **options)
37+
# FileUtils.ln(targets, dir, **options)
38+
# FileUtils.ln_s(target, link, **options)
39+
# FileUtils.ln_s(targets, dir, **options)
40+
# FileUtils.ln_sf(target, link, **options)
41+
# FileUtils.cp(src, dest, **options)
42+
# FileUtils.cp(list, dir, **options)
43+
# FileUtils.cp_r(src, dest, **options)
44+
# FileUtils.cp_r(list, dir, **options)
45+
# FileUtils.mv(src, dest, **options)
46+
# FileUtils.mv(list, dir, **options)
47+
# FileUtils.rm(list, **options)
48+
# FileUtils.rm_r(list, **options)
49+
# FileUtils.rm_rf(list, **options)
50+
# FileUtils.install(src, dest, **options)
51+
# FileUtils.chmod(mode, list, **options)
52+
# FileUtils.chmod_R(mode, list, **options)
53+
# FileUtils.chown(user, group, list, **options)
54+
# FileUtils.chown_R(user, group, list, **options)
55+
# FileUtils.touch(list, **options)
5656
#
57-
# The <tt>options</tt> parameter is a hash of options, taken from the list
58-
# <tt>:force</tt>, <tt>:noop</tt>, <tt>:preserve</tt>, and <tt>:verbose</tt>.
59-
# <tt>:noop</tt> means that no changes are made. The other three are obvious.
60-
# Each method documents the options that it honours.
57+
# Possible <tt>options</tt> are:
58+
#
59+
# <tt>:force</tt> :: forced operation (rewrite files if exist, remove
60+
# directories if not empty, etc.);
61+
# <tt>:verbose</tt> :: print command to be run, in bash syntax, before
62+
# performing it;
63+
# <tt>:preserve</tt> :: preserve object's group, user and modification
64+
# time on copying;
65+
# <tt>:noop</tt> :: no changes are made (usable in combination with
66+
# <tt>:verbose</tt> which will print the command to run)
67+
#
68+
# Each method documents the options that it honours. See also ::commands,
69+
# ::options and ::options_of methods to introspect which command have which
70+
# options.
6171
#
6272
# All methods that have the concept of a "source" file or directory can take
6373
# either one file or a list of files in that argument. See the method
6474
# documentation for examples.
6575
#
66-
# There are some `low level' methods, which do not accept any option:
76+
# There are some `low level' methods, which do not accept keyword arguments:
6777
#
6878
# FileUtils.copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false)
6979
# FileUtils.copy_file(src, dest, preserve = false, dereference = true)
@@ -119,7 +129,7 @@ def pwd
119129
#
120130
# FileUtils.cd('/') # change directory
121131
#
122-
# FileUtils.cd('/', :verbose => true) # change directory and report it
132+
# FileUtils.cd('/', verbose: true) # change directory and report it
123133
#
124134
# FileUtils.cd('/') do # change directory
125135
# # ... # do something
@@ -164,9 +174,9 @@ def remove_trailing_slash(dir) #:nodoc:
164174
# Creates one or more directories.
165175
#
166176
# FileUtils.mkdir 'test'
167-
# FileUtils.mkdir %w( tmp data )
168-
# FileUtils.mkdir 'notexist', :noop => true # Does not really create.
169-
# FileUtils.mkdir 'tmp', :mode => 0700
177+
# FileUtils.mkdir %w(tmp data)
178+
# FileUtils.mkdir 'notexist', noop: true # Does not really create.
179+
# FileUtils.mkdir 'tmp', mode: 0700
170180
#
171181
def mkdir(list, mode: nil, noop: nil, verbose: nil)
172182
list = fu_list(list)
@@ -185,7 +195,7 @@ def mkdir(list, mode: nil, noop: nil, verbose: nil)
185195
#
186196
# FileUtils.mkdir_p '/usr/local/lib/ruby'
187197
#
188-
# causes to make following directories, if it does not exist.
198+
# causes to make following directories, if they do not exist.
189199
#
190200
# * /usr
191201
# * /usr/local
@@ -249,7 +259,7 @@ def fu_mkdir(path, mode) #:nodoc:
249259
# FileUtils.rmdir 'somedir'
250260
# FileUtils.rmdir %w(somedir anydir otherdir)
251261
# # Does not really remove directory; outputs message.
252-
# FileUtils.rmdir 'somedir', :verbose => true, :noop => true
262+
# FileUtils.rmdir 'somedir', verbose: true, noop: true
253263
#
254264
def rmdir(list, parents: nil, noop: nil, verbose: nil)
255265
list = fu_list(list)
@@ -278,7 +288,7 @@ def rmdir(list, parents: nil, noop: nil, verbose: nil)
278288
#
279289
# In the first form, creates a hard link +link+ which points to +target+.
280290
# If +link+ already exists, raises Errno::EEXIST.
281-
# But if the :force option is set, overwrites +link+.
291+
# But if the +force+ option is set, overwrites +link+.
282292
#
283293
# FileUtils.ln 'gcc', 'cc', verbose: true
284294
# FileUtils.ln '/usr/bin/emacs21', '/usr/bin/emacs'
@@ -304,23 +314,23 @@ def ln(src, dest, force: nil, noop: nil, verbose: nil)
304314
alias link ln
305315
module_function :link
306316

307-
#
308-
# :call-seq:
309-
# FileUtils.cp_lr(src, dest, noop: nil, verbose: nil, dereference_root: true, remove_destination: false)
310317
#
311318
# Hard link +src+ to +dest+. If +src+ is a directory, this method links
312319
# all its contents recursively. If +dest+ is a directory, links
313320
# +src+ to +dest/src+.
314321
#
315322
# +src+ can be a list of files.
316323
#
317-
# # Installing the library "mylib" under the site_ruby directory.
318-
# FileUtils.rm_r site_ruby + '/mylib', :force => true
324+
# If +dereference_root+ is true, this method dereference tree root.
325+
#
326+
# If +remove_destination+ is true, this method removes each destination file before copy.
327+
#
328+
# FileUtils.rm_r site_ruby + '/mylib', force: true
319329
# FileUtils.cp_lr 'lib/', site_ruby + '/mylib'
320330
#
321331
# # Examples of linking several files to target directory.
322332
# FileUtils.cp_lr %w(mail.rb field.rb debug/), site_ruby + '/tmail'
323-
# FileUtils.cp_lr Dir.glob('*.rb'), '/home/aamine/lib/ruby', :noop => true, :verbose => true
333+
# FileUtils.cp_lr Dir.glob('*.rb'), '/home/aamine/lib/ruby', noop: true, verbose: true
324334
#
325335
# # If you want to link all contents of a directory instead of the
326336
# # directory itself, c.f. src/x -> dest/x, src/y -> dest/y,
@@ -345,7 +355,7 @@ def cp_lr(src, dest, noop: nil, verbose: nil,
345355
#
346356
# In the first form, creates a symbolic link +link+ which points to +target+.
347357
# If +link+ already exists, raises Errno::EEXIST.
348-
# But if the :force option is set, overwrites +link+.
358+
# But if the <tt>force</tt> option is set, overwrites +link+.
349359
#
350360
# FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby'
351361
# FileUtils.ln_s 'verylongsourcefilename.c', 'c', force: true
@@ -411,7 +421,7 @@ def link_entry(src, dest, dereference_root = false, remove_destination = false)
411421
#
412422
# FileUtils.cp 'eval.c', 'eval.c.org'
413423
# FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6'
414-
# FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose => true
424+
# FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', verbose: true
415425
# FileUtils.cp 'symlink', 'dest' # copy content, "dest" is not a symlink
416426
#
417427
def cp(src, dest, preserve: nil, noop: nil, verbose: nil)
@@ -433,13 +443,17 @@ def cp(src, dest, preserve: nil, noop: nil, verbose: nil)
433443
#
434444
# +src+ can be a list of files.
435445
#
446+
# If +dereference_root+ is true, this method dereference tree root.
447+
#
448+
# If +remove_destination+ is true, this method removes each destination file before copy.
449+
#
436450
# # Installing Ruby library "mylib" under the site_ruby
437-
# FileUtils.rm_r site_ruby + '/mylib', :force
451+
# FileUtils.rm_r site_ruby + '/mylib', force: true
438452
# FileUtils.cp_r 'lib/', site_ruby + '/mylib'
439453
#
440454
# # Examples of copying several files to target directory.
441455
# FileUtils.cp_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
442-
# FileUtils.cp_r Dir.glob('*.rb'), '/home/foo/lib/ruby', :noop => true, :verbose => true
456+
# FileUtils.cp_r Dir.glob('*.rb'), '/home/foo/lib/ruby', noop: true, verbose: true
443457
#
444458
# # If you want to copy all contents of a directory instead of the
445459
# # directory itself, c.f. src/x -> dest/x, src/y -> dest/y,
@@ -511,10 +525,10 @@ def copy_stream(src, dest)
511525
# disk partition, the file is copied then the original file is removed.
512526
#
513527
# FileUtils.mv 'badname.rb', 'goodname.rb'
514-
# FileUtils.mv 'stuff.rb', '/notexist/lib/ruby', :force => true # no error
528+
# FileUtils.mv 'stuff.rb', '/notexist/lib/ruby', force: true # no error
515529
#
516530
# FileUtils.mv %w(junk.txt dust.txt), '/home/foo/.trash/'
517-
# FileUtils.mv Dir.glob('test*.rb'), 'test', :noop => true, :verbose => true
531+
# FileUtils.mv Dir.glob('test*.rb'), 'test', noop: true, verbose: true
518532
#
519533
def mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil)
520534
fu_output_message "mv#{force ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if verbose
@@ -554,7 +568,7 @@ def mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil)
554568
#
555569
# FileUtils.rm %w( junk.txt dust.txt )
556570
# FileUtils.rm Dir.glob('*.so')
557-
# FileUtils.rm 'NotExistFile', :force => true # never raises exception
571+
# FileUtils.rm 'NotExistFile', force: true # never raises exception
558572
#
559573
def rm(list, force: nil, noop: nil, verbose: nil)
560574
list = fu_list(list)
@@ -573,7 +587,7 @@ def rm(list, force: nil, noop: nil, verbose: nil)
573587
#
574588
# Equivalent to
575589
#
576-
# FileUtils.rm(list, :force => true)
590+
# FileUtils.rm(list, force: true)
577591
#
578592
def rm_f(list, noop: nil, verbose: nil)
579593
rm list, force: true, noop: noop, verbose: verbose
@@ -589,18 +603,18 @@ def rm_f(list, noop: nil, verbose: nil)
589603
# StandardError when :force option is set.
590604
#
591605
# FileUtils.rm_r Dir.glob('/tmp/*')
592-
# FileUtils.rm_r 'some_dir', :force => true
606+
# FileUtils.rm_r 'some_dir', force: true
593607
#
594608
# WARNING: This method causes local vulnerability
595609
# if one of parent directories or removing directory tree are world
596610
# writable (including /tmp, whose permission is 1777), and the current
597611
# process has strong privilege such as Unix super user (root), and the
598612
# system has symbolic link. For secure removing, read the documentation
599-
# of #remove_entry_secure carefully, and set :secure option to true.
600-
# Default is :secure=>false.
613+
# of remove_entry_secure carefully, and set :secure option to true.
614+
# Default is <tt>secure: false</tt>.
601615
#
602-
# NOTE: This method calls #remove_entry_secure if :secure option is set.
603-
# See also #remove_entry_secure.
616+
# NOTE: This method calls remove_entry_secure if :secure option is set.
617+
# See also remove_entry_secure.
604618
#
605619
def rm_r(list, force: nil, noop: nil, verbose: nil, secure: nil)
606620
list = fu_list(list)
@@ -619,10 +633,10 @@ def rm_r(list, force: nil, noop: nil, verbose: nil, secure: nil)
619633
#
620634
# Equivalent to
621635
#
622-
# FileUtils.rm_r(list, :force => true)
636+
# FileUtils.rm_r(list, force: true)
623637
#
624638
# WARNING: This method causes local vulnerability.
625-
# Read the documentation of #rm_r first.
639+
# Read the documentation of rm_r first.
626640
#
627641
def rm_rf(list, noop: nil, verbose: nil, secure: nil)
628642
rm_r list, force: true, noop: noop, verbose: verbose, secure: secure
@@ -636,7 +650,7 @@ def rm_rf(list, noop: nil, verbose: nil, secure: nil)
636650
# This method removes a file system entry +path+. +path+ shall be a
637651
# regular file, a directory, or something. If +path+ is a directory,
638652
# remove it recursively. This method is required to avoid TOCTTOU
639-
# (time-of-check-to-time-of-use) local security vulnerability of #rm_r.
653+
# (time-of-check-to-time-of-use) local security vulnerability of rm_r.
640654
# #rm_r causes security hole when:
641655
#
642656
# * Parent directory is world writable (including /tmp).
@@ -755,7 +769,7 @@ def fu_stat_identical_entry?(a, b) #:nodoc:
755769
# +path+ might be a regular file, a directory, or something.
756770
# If +path+ is a directory, remove it recursively.
757771
#
758-
# See also #remove_entry_secure.
772+
# See also remove_entry_secure.
759773
#
760774
def remove_entry(path, force = false)
761775
Entry_.new(path).postorder_traverse do |ent|
@@ -839,8 +853,8 @@ def compare_stream(a, b)
839853
# mode to +mode+. If +dest+ is a directory, destination is +dest+/+src+.
840854
# This method removes destination before copy.
841855
#
842-
# FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true
843-
# FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true
856+
# FileUtils.install 'ruby', '/usr/local/bin/ruby', mode: 0755, verbose: true
857+
# FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', verbose: true
844858
#
845859
def install(src, dest, mode: nil, owner: nil, group: nil, preserve: nil,
846860
noop: nil, verbose: nil)
@@ -970,12 +984,12 @@ def mode_to_s(mode) #:nodoc:
970984
# Absolute mode is
971985
# FileUtils.chmod 0755, 'somecommand'
972986
# FileUtils.chmod 0644, %w(my.rb your.rb his.rb her.rb)
973-
# FileUtils.chmod 0755, '/usr/bin/ruby', :verbose => true
987+
# FileUtils.chmod 0755, '/usr/bin/ruby', verbose: true
974988
#
975989
# Symbolic mode is
976990
# FileUtils.chmod "u=wrx,go=rx", 'somecommand'
977991
# FileUtils.chmod "u=wr,go=rr", %w(my.rb your.rb his.rb her.rb)
978-
# FileUtils.chmod "u=wrx,go=rx", '/usr/bin/ruby', :verbose => true
992+
# FileUtils.chmod "u=wrx,go=rx", '/usr/bin/ruby', verbose: true
979993
#
980994
# "a" :: is user, group, other mask.
981995
# "u" :: is user's mask.
@@ -1035,7 +1049,7 @@ def chmod_R(mode, list, noop: nil, verbose: nil, force: nil)
10351049
# the attribute.
10361050
#
10371051
# FileUtils.chown 'root', 'staff', '/usr/local/bin/ruby'
1038-
# FileUtils.chown nil, 'bin', Dir.glob('/usr/bin/*'), :verbose => true
1052+
# FileUtils.chown nil, 'bin', Dir.glob('/usr/bin/*'), verbose: true
10391053
#
10401054
def chown(user, group, list, noop: nil, verbose: nil)
10411055
list = fu_list(list)
@@ -1059,7 +1073,7 @@ def chown(user, group, list, noop: nil, verbose: nil)
10591073
# method does not change the attribute.
10601074
#
10611075
# FileUtils.chown_R 'www', 'www', '/var/www/htdocs'
1062-
# FileUtils.chown_R 'cvs', 'cvs', '/var/cvs', :verbose => true
1076+
# FileUtils.chown_R 'cvs', 'cvs', '/var/cvs', verbose: true
10631077
#
10641078
def chown_R(user, group, list, noop: nil, verbose: nil, force: nil)
10651079
list = fu_list(list)
@@ -1605,8 +1619,11 @@ def fu_output_message(msg) #:nodoc:
16051619
tbl
16061620
}
16071621

1622+
public
1623+
16081624
#
1609-
# Returns an Array of method names which have any options.
1625+
# Returns an Array of names of high-level methods that accept any keyword
1626+
# arguments.
16101627
#
16111628
# p FileUtils.commands #=> ["chmod", "cp", "cp_r", "install", ...]
16121629
#
@@ -1645,22 +1662,24 @@ def self.options_of(mid)
16451662
end
16461663

16471664
#
1648-
# Returns an Array of method names which have the option +opt+.
1665+
# Returns an Array of methods names which have the option +opt+.
16491666
#
16501667
# p FileUtils.collect_method(:preserve) #=> ["cp", "cp_r", "copy", "install"]
16511668
#
16521669
def self.collect_method(opt)
16531670
OPT_TABLE.keys.select {|m| OPT_TABLE[m].include?(opt) }
16541671
end
16551672

1656-
LOW_METHODS = singleton_methods(false) - collect_method(:noop).map(&:intern)
1657-
module LowMethods
1673+
private
1674+
1675+
LOW_METHODS = singleton_methods(false) - collect_method(:noop).map(&:intern) # :nodoc:
1676+
module LowMethods # :nodoc: internal use only
16581677
private
16591678
def _do_nothing(*)end
16601679
::FileUtils::LOW_METHODS.map {|name| alias_method name, :_do_nothing}
16611680
end
16621681

1663-
METHODS = singleton_methods() - [:private_module_function,
1682+
METHODS = singleton_methods() - [:private_module_function, # :nodoc:
16641683
:commands, :options, :have_option?, :options_of, :collect_method]
16651684

16661685
#

0 commit comments

Comments
 (0)