| Module | FileUtils |
| In: |
lib/fileutils.rb
|
Copyright © 2000-2004 Minero Aoki <aamine@loveruby.net>
This program is free software. You can distribute/modify this program under the same terms of ruby.
Namespace for several file utility methods for copying, moving, removing, etc.
cd(dir, options)
cd(dir, options) {|dir| .... }
pwd()
mkdir(dir, options)
mkdir_p(dir, options)
rmdir(dir, options)
ln(old, new, options)
ln(list, destdir, options)
ln_s(old, new, options)
ln_s(list, destdir, options)
ln_sf(src, dest, options)
cp(src, dest, options)
cp(list, dir, options)
cp_r(src, dest, options)
cp_r(list, dir, options)
mv(src, dest, options)
mv(list, dir, options)
rm(list, options)
rm_r(list, options)
rm_rf(list, options)
install(src, dest, mode = <src's>, options)
chmod(mode, list, options)
touch(list, options)
The options parameter is a hash of options, taken from the list :force, :noop, :preserve, and :verbose. :noop means that no changes are made. The other two are obvious. Each method documents the options that it honours.
All methods that have the concept of a "source" file or directory can take either one file or a list of files in that argument. See the method documentation for examples.
There are some `low level’ methods, which does not accept any option:
uptodate?(file, cmp_list) copy_entry(src, dest, preserve = false, dereference = false) copy_file(src, dest, preserve = false, dereference = true) copy_stream(srcstream, deststream) compare_file(path_a, path_b) compare_stream(stream_a, stream_b)
This module has all methods of FileUtils module, but it outputs messages before acting. This equates to passing the :verbose flag to methods in FileUtils.
This module has all methods of FileUtils module, but never changes files/directories. This equates to passing the :noop flag to methods in FileUtils.
This module has all methods of FileUtils module, but never changes files/directories. This equates to passing the :noop and :verbose flags to methods in FileUtils.
| OPT_TABLE | = | { 'cd' => %w( noop verbose ), 'chdir' => %w( noop verbose ), 'chmod' => %w( noop verbose ), 'copy' => %w( noop verbose preserve ), 'cp' => %w( noop verbose preserve ), 'cp_r' => %w( noop verbose preserve ), 'install' => %w( noop verbose preserve mode ), 'link' => %w( noop verbose force ), 'ln' => %w( noop verbose force ), 'ln_s' => %w( noop verbose force ), 'ln_sf' => %w( noop verbose ), 'makedirs' => %w( noop verbose ), 'mkdir' => %w( noop verbose mode ), 'mkdir_p' => %w( noop verbose mode ), 'mkpath' => %w( noop verbose ), 'move' => %w( noop verbose force ), 'mv' => %w( noop verbose force ), 'remove' => %w( noop verbose force ), 'rm' => %w( noop verbose force ), 'rm_f' => %w( noop verbose ), 'rm_r' => %w( noop verbose force ), 'rm_rf' => %w( noop verbose ), 'rmtree' => %w( noop verbose ), 'rmdir' => %w( noop verbose ), 'safe_unlink' => %w( noop verbose ), 'symlink' => %w( noop verbose force ), 'touch' => %w( noop verbose ) |
Options: noop verbose
Changes the current directory to the directory dir.
If this method is called with block, resumes to the old working directory after the block execution finished.
FileUtils.cd('/', :verbose => true) # chdir and report it
# File lib/fileutils.rb, line 101
101: def cd(dir, options = {}, &block) # :yield: dir
102: fu_check_options options, :noop, :verbose
103: fu_output_message "cd #{dir}" if options[:verbose]
104: Dir.chdir(dir, &block) unless options[:noop]
105: fu_output_message 'cd -' if options[:verbose] and block
106: end
Options: noop verbose
Changes permission bits on the named files (in list) to the bit pattern represented by mode.
FileUtils.chmod 0755, 'somecommand' FileUtils.chmod 0644, %w(my.rb your.rb his.rb her.rb) FileUtils.chmod 0755, '/usr/bin/ruby', :verbose => true
# File lib/fileutils.rb, line 794
794: def chmod(mode, list, options = {})
795: fu_check_options options, :noop, :verbose
796: list = fu_list(list)
797: fu_output_message sprintf('chmod %o %s', mode, list.join(' ')) if options[:verbose]
798: return if options[:noop]
799: File.chmod mode, *list
800: end
Returns true if the contents of a file A and a file B are identical.
FileUtils.compare_file('somefile', 'somefile') #=> true
FileUtils.compare_file('/bin/cp', '/bin/mv') #=> maybe false
# File lib/fileutils.rb, line 727
727: def compare_file(a, b)
728: return false unless File.size(a) == File.size(b)
729: File.open(a, 'rb') {|fa|
730: File.open(b, 'rb') {|fb|
731: return compare_stream(fa, fb)
732: }
733: }
734: end
Returns true if the contents of a stream a and b are identical.
# File lib/fileutils.rb, line 742
742: def compare_stream(a, b)
743: bsize = fu_stream_blksize(a, b)
744: sa = sb = nil
745: while sa == sb
746: sa = a.read(bsize)
747: sb = b.read(bsize)
748: unless sa and sb
749: if sa.nil? and sb.nil?
750: return true
751: end
752: end
753: end
754: false
755: end
Copies a file system entry src to dest. This method preserves file types, c.f. FIFO, device files, directory.…
Both of src and dest must be a path name. src must exist, dest must not exist.
If preserve is true, this method preserves owner, group and permissions. If dereference is true, this method copies a target of symbolic link instead of a symbolic link itself.
# File lib/fileutils.rb, line 415
415: def copy_entry(src, dest, preserve = false, dereference = false)
416: CopyContext_.new(preserve, dereference).copy_entry src, dest
417: end
Copies file contents of src to dest. Both of src and dest must be a path name.
# File lib/fileutils.rb, line 423
423: def copy_file(src, dest, preserve = false, dereference = true)
424: CopyContext_.new(preserve, dereference).copy_content src, dest
425: end
Options: preserve noop verbose
Copies a file src to dest. If dest is a directory, copies src to +dest/src+.
If src is a list of files, then dest must be a directory.
FileUtils.cp 'eval.c', 'eval.c.org' FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6' FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose => true
# File lib/fileutils.rb, line 336
336: def cp(src, dest, options = {})
337: fu_check_options options, :preserve, :noop, :verbose
338: fu_output_message "cp#{options[:preserve] ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
339: return if options[:noop]
340:
341: fu_each_src_dest(src, dest) do |s,d|
342: copy_file s, d, options[:preserve]
343: end
344: end
Options: preserve noop verbose
Copies src to dest. If src is a directory, this method copies all its contents recursively. If dest is a directory, copies src to +dest/src+.
src can be a list of files.
# Installing ruby library "mylib" under the site_ruby
FileUtils.rm_r site_ruby + '/mylib', :force
FileUtils.cp_r 'lib/', site_ruby + '/mylib'
# Examples of copying several files to target directory.
FileUtils.cp_r %w(mail.rb field.rb debug/), site_ruby + '/tmail'
FileUtils.cp_r Dir.glob('*.rb'), '/home/aamine/lib/ruby', :noop, :verbose
# If you want to copy all contents of a directory instead of the
# directory itself, c.f. src/x -> dest/x, src/y -> dest/y,
# use following code.
FileUtils.cp_r 'src/.', 'dest' # cp_r('src', 'dest') makes src/dest,
# but this doesn't.
# File lib/fileutils.rb, line 371
371: def cp_r(src, dest, options = {})
372: fu_check_options options, :preserve, :noop, :verbose
373: fu_output_message "cp -r#{options[:preserve] ? 'p' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
374: return if options[:noop]
375:
376: fu_each_src_dest(src, dest) do |s,d|
377: if File.directory?(s)
378: fu_traverse(s) {|rel, deref, st|
379: ctx = CopyContext_.new(options[:preserve], deref, st)
380: ctx.copy_entry "#{s}/#{rel}", "#{d}/#{rel}"
381: }
382: else
383: copy_file s, d, options[:preserve]
384: end
385: end
386: end
Options: mode noop verbose
If src is not same as dest, copies it and changes the permission mode to mode. If dest is a directory, destination is dest/src.
FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true
# File lib/fileutils.rb, line 767
767: def install(src, dest, options = {})
768: fu_check_options options, :mode, :preserve, :noop, :verbose
769: fu_output_message "install -c#{options[:preserve] && ' -p'}#{options[:mode] ? (' -m 0%o' % options[:mode]) : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
770: return if options[:noop]
771:
772: fu_each_src_dest(src, dest) do |s,d|
773: unless File.exist?(d) and compare_file(s,d)
774: remove_file d, true
775: st = File.stat(s) if options[:preserve]
776: copy_file s, d
777: File.utime st.atime, st.mtime, d if options[:preserve]
778: File.chmod options[:mode], d if options[:mode]
779: end
780: end
781: end
Options: force noop verbose
ln( old, new, options = {} )
Creates a hard link new which points to old. If new already exists and it is a directory, creates a symbolic link +new/old+. If new already exists and it is not a directory, raises Errno::EEXIST. But if :force option is set, overwrite new.
FileUtils.ln 'gcc', 'cc', :verbose => true FileUtils.ln '/usr/bin/emacs21', '/usr/bin/emacs'
ln( list, destdir, options = {} )
Creates several hard links in a directory, with each one pointing to the item in list. If destdir is not a directory, raises Errno::ENOTDIR.
include FileUtils cd '/bin' ln %w(cp mv mkdir), '/usr/bin' # Now /usr/bin/cp and /bin/cp are linked.
# File lib/fileutils.rb, line 262
262: def ln(src, dest, options = {})
263: fu_check_options options, :force, :noop, :verbose
264: fu_output_message "ln#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
265: return if options[:noop]
266:
267: fu_each_src_dest0(src, dest) do |s,d|
268: remove_file d, true if options[:force]
269: File.link s, d
270: end
271: end
Options: force noop verbose
ln_s( old, new, options = {} )
Creates a symbolic link new which points to old. If new already exists and it is a directory, creates a symbolic link +new/old+. If new already exists and it is not a directory, raises Errno::EEXIST. But if :force option is set, overwrite new.
FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby' FileUtils.ln_s 'verylongsourcefilename.c', 'c', :force => true
ln_s( list, destdir, options = {} )
Creates several symbolic links in a directory, with each one pointing to the item in list. If destdir is not a directory, raises Errno::ENOTDIR.
If destdir is not a directory, raises Errno::ENOTDIR.
FileUtils.ln_s Dir.glob('bin/*.rb'), '/home/aamine/bin'
# File lib/fileutils.rb, line 297
297: def ln_s(src, dest, options = {})
298: fu_check_options options, :force, :noop, :verbose
299: fu_output_message "ln -s#{options[:force] ? 'f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
300: return if options[:noop]
301:
302: fu_each_src_dest0(src, dest) do |s,d|
303: remove_file d, true if options[:force]
304: File.symlink s, d
305: end
306: end
Options: noop verbose
Same as
#ln_s(src, dest, :force)
# File lib/fileutils.rb, line 316
316: def ln_sf(src, dest, options = {})
317: fu_check_options options, :noop, :verbose
318: options = options.dup
319: options[:force] = true
320: ln_s src, dest, options
321: end
Options: mode noop verbose
Creates one or more directories.
FileUtils.mkdir 'test' FileUtils.mkdir %w( tmp data ) FileUtils.mkdir 'notexist', :noop => true # Does not really create. FileUtils.mkdir 'tmp', :mode => 0700
# File lib/fileutils.rb, line 144
144: def mkdir(list, options = {})
145: fu_check_options options, :mode, :noop, :verbose
146: list = fu_list(list)
147: fu_output_message "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
148: return if options[:noop]
149:
150: list.each do |dir|
151: fu_mkdir dir, options[:mode]
152: end
153: end
Options: mode noop verbose
Creates a directory and all its parent directories. For example,
FileUtils.mkdir_p '/usr/local/lib/ruby'
causes to make following directories, if it does not exist.
* /usr
* /usr/local
* /usr/local/lib
* /usr/local/lib/ruby
You can pass several directories at a time in a list.
# File lib/fileutils.rb, line 171
171: def mkdir_p(list, options = {})
172: fu_check_options options, :mode, :noop, :verbose
173: list = fu_list(list)
174: fu_output_message "mkdir -p #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
175: return *list if options[:noop]
176:
177: list.map {|path| path.sub(%r</\z>, '') }.each do |path|
178: # optimize for the most common case
179: begin
180: fu_mkdir path, options[:mode]
181: next
182: rescue SystemCallError
183: next if File.directory?(path)
184: end
185:
186: stack = []
187: until path == stack.last # dirname("/")=="/", dirname("C:/")=="C:/"
188: stack.push path
189: path = File.dirname(path)
190: end
191: stack.reverse_each do |path|
192: begin
193: fu_mkdir path, options[:mode]
194: rescue SystemCallError => err
195: raise unless File.directory?(path)
196: end
197: end
198: end
199:
200: return *list
201: end
Options: force noop verbose
Moves file(s) src to dest. If file and dest exist on the different disk partition, the file is copied instead.
FileUtils.mv 'badname.rb', 'goodname.rb'
FileUtils.mv 'stuff.rb', '/notexist/lib/ruby', :force => true # no error
FileUtils.mv %w(junk.txt dust.txt), '/home/aamine/.trash/'
FileUtils.mv Dir.glob('test*.rb'), 'test', :noop => true, :verbose => true
# File lib/fileutils.rb, line 567
567: def mv(src, dest, options = {})
568: fu_check_options options, :force, :noop, :verbose
569: fu_output_message "mv#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
570: return if options[:noop]
571:
572: fu_each_src_dest(src, dest) do |s,d|
573: if rename_cannot_overwrite_file? and File.file?(d)
574: begin
575: File.unlink d
576: rescue SystemCallError
577: raise unless options[:force]
578: end
579: end
580: begin
581: File.rename s, d
582: rescue SystemCallError
583: begin
584: copy_entry s, d, true
585: File.unlink s
586: rescue SystemCallError
587: raise unless options[:force]
588: end
589: end
590: end
591: end
Options: (none)
Returns the name of the current directory.
# File lib/fileutils.rb, line 85
85: def pwd
86: Dir.pwd
87: end
Options: force noop verbose
Remove file(s) specified in list. This method cannot remove directories. All StandardErrors are ignored when the :force option is set.
FileUtils.rm %w( junk.txt dust.txt )
FileUtils.rm Dir.glob('*.so')
FileUtils.rm 'NotExistFile', :force => true # never raises exception
# File lib/fileutils.rb, line 611
611: def rm(list, options = {})
612: fu_check_options options, :force, :noop, :verbose
613: list = fu_list(list)
614: fu_output_message "rm#{options[:force] ? ' -f' : ''} #{list.join ' '}" if options[:verbose]
615: return if options[:noop]
616:
617: list.each do |fname|
618: remove_file fname, options[:force]
619: end
620: end
Options: noop verbose
Same as
#rm(list, :force)
# File lib/fileutils.rb, line 630
630: def rm_f(list, options = {})
631: fu_check_options options, :noop, :verbose
632: options = options.dup
633: options[:force] = true
634: rm list, options
635: end
Options: force noop verbose
remove files list[0] list[1]… If list[n] is a directory, removes its all contents recursively. This method ignores StandardError when :force option is set.
FileUtils.rm_r Dir.glob('/tmp/*')
FileUtils.rm_r '/', :force => true # :-)
# File lib/fileutils.rb, line 649
649: def rm_r(list, options = {})
650: fu_check_options options, :force, :noop, :verbose
651: list = fu_list(list)
652: fu_output_message "rm -r#{options[:force] ? 'f' : ''} #{list.join ' '}" if options[:verbose]
653: return if options[:noop]
654:
655: list.each do |fname|
656: begin
657: st = File.lstat(fname)
658: rescue
659: next if options[:force]
660: raise
661: end
662: if st.symlink? then remove_file fname, options[:force]
663: elsif st.directory? then remove_dir fname, options[:force]
664: else remove_file fname, options[:force]
665: end
666: end
667: end
Options: noop verbose
Same as
#rm_r(list, :force => true)
# File lib/fileutils.rb, line 675
675: def rm_rf(list, options = {})
676: fu_check_options options, :noop, :verbose
677: options = options.dup
678: options[:force] = true
679: rm_r list, options
680: end
Options: noop, verbose
Removes one or more directories.
FileUtils.rmdir 'somedir' FileUtils.rmdir %w(somedir anydir otherdir) # Does not really remove directory; outputs message. FileUtils.rmdir 'somedir', :verbose => true, :noop => true
# File lib/fileutils.rb, line 228
228: def rmdir(list, options = {})
229: fu_check_options options, :noop, :verbose
230: list = fu_list(list)
231: fu_output_message "rmdir #{list.join ' '}" if options[:verbose]
232: return if options[:noop]
233:
234: list.each do |dir|
235: Dir.rmdir dir.sub(%r</\z>, '')
236: end
237: end
Options: noop verbose
Updates modification time (mtime) and access time (atime) of file(s) in list. Files are created if they don’t exist.
FileUtils.touch 'timestamp'
FileUtils.touch Dir.glob('*.c'); system 'make'
# File lib/fileutils.rb, line 812
812: def touch(list, options = {})
813: fu_check_options options, :noop, :verbose
814: list = fu_list(list)
815: fu_output_message "touch #{list.join ' '}" if options[:verbose]
816: return if options[:noop]
817:
818: t = Time.now
819: list.each do |fname|
820: begin
821: File.utime(t, t, fname)
822: rescue Errno::ENOENT
823: File.open(fname, 'a') {
824: ;
825: }
826: end
827: end
828: end
Options: (none)
Returns true if newer is newer than all old_list. Non-existent files are older than any file.
FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or # system 'make hello.o'
# File lib/fileutils.rb, line 120
120: def uptodate?(new, old_list, options = nil)
121: raise ArgumentError, 'uptodate? does not accept any option' if options
122:
123: return false unless File.exist?(new)
124: new_time = File.mtime(new)
125: old_list.each do |old|
126: if File.exist?(old)
127: return false unless new_time > File.mtime(old)
128: end
129: end
130: true
131: end
# File lib/fileutils.rb, line 890
890: def fu_blksize(st)
891: s = st.blksize
892: return nil unless s
893: return nil if s == 0
894: s
895: end
# File lib/fileutils.rb, line 832
832: def fu_check_options(options, *optdecl)
833: h = options.dup
834: optdecl.each do |name|
835: h.delete name
836: end
837: raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty?
838: end
# File lib/fileutils.rb, line 844
844: def fu_each_src_dest(src, dest)
845: fu_each_src_dest0(src, dest) do |s, d|
846: raise ArgumentError, "same file: #{s} and #{d}" if fu_same?(s, d)
847: yield s, d
848: end
849: end
# File lib/fileutils.rb, line 851
851: def fu_each_src_dest0(src, dest)
852: if src.is_a?(Array)
853: src.each do |s|
854: yield s.to_str, File.join(dest, File.basename(s))
855: end
856: else
857: if File.directory?(dest)
858: yield src.to_str, File.join(dest, File.basename(src))
859: else
860: yield src.to_str, dest.to_str
861: end
862: end
863: end
# File lib/fileutils.rb, line 840
840: def fu_list(arg)
841: [arg].flatten.map {|path| path.to_str }
842: end
# File lib/fileutils.rb, line 206
206: def fu_mkdir(path, mode)
207: path = path.sub(%r</\z>, '')
208: if mode
209: Dir.mkdir path, mode
210: File.chmod mode, path
211: else
212: Dir.mkdir path
213: end
214: end
# File lib/fileutils.rb, line 904
904: def fu_output_message(msg)
905: @fileutils_output ||= $stderr
906: @fileutils_label ||= ''
907: @fileutils_output.puts @fileutils_label + msg
908: end
# File lib/fileutils.rb, line 865
865: def fu_same?(a, b)
866: if have_st_ino?
867: st1 = File.stat(a)
868: st2 = File.stat(b)
869: st1.dev == st2.dev and st1.ino == st2.ino
870: else
871: File.expand_path(a) == File.expand_path(b)
872: end
873: rescue Errno::ENOENT
874: return false
875: end
# File lib/fileutils.rb, line 881
881: def fu_stream_blksize(*streams)
882: streams.each do |s|
883: next unless s.respond_to?(:stat)
884: size = fu_blksize(s.stat)
885: return size if size
886: end
887: fu_default_blksize()
888: end
# File lib/fileutils.rb, line 910
910: def fu_update_option(args, new)
911: if args.last.is_a?(Hash)
912: args.last.update new
913: else
914: args.push new
915: end
916: args
917: end