Module | FileUtils |
In: |
lib/fileutils.rb
lib/un.rb |
Copyright (c) 2000-2006 Minero Aoki
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(list, options) mkdir_p(dir, options) mkdir_p(list, options) rmdir(dir, options) rmdir(list, 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) chmod_R(mode, list, options) chown(user, group, list, options) chown_R(user, group, 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 do not accept any option:
copy_entry(src, dest, preserve = false, dereference = false) copy_file(src, dest, preserve = false, dereference = true) copy_stream(srcstream, deststream) remove_entry(path, force = false) remove_entry_secure(path, force = false) remove_file(path, force = false) compare_file(path_a, path_b) compare_stream(stream_a, stream_b) uptodate?(file, cmp_list)
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.
METHODS | = | singleton_methods() - %w( private_module_function commands options have_option? options_of collect_method ) |
Returns true if the method mid have an option opt.
p FileUtils.have_option?(:cp, :noop) #=> true p FileUtils.have_option?(:rm, :force) #=> true p FileUtils.have_option?(:rm, :perserve) #=> false
# File lib/fileutils.rb, line 1496 1496: def FileUtils.have_option?(mid, opt) 1497: li = OPT_TABLE[mid.to_s] or raise ArgumentError, "no such method: #{mid}" 1498: li.include?(opt) 1499: end
Options: 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 118 118: def cd(dir, options = {}, &block) # :yield: dir 119: fu_check_options options, OPT_TABLE['cd'] 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
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 870 870: def chmod(mode, list, options = {}) 871: fu_check_options options, OPT_TABLE['chmod'] 872: list = fu_list(list) 873: fu_output_message sprintf('chmod %o %s', mode, list.join(' ')) if options[:verbose] 874: return if options[:noop] 875: list.each do |path| 876: Entry_.new(path).chmod mode 877: end 878: end
Options: noop verbose force
Changes permission bits on the named files (in list) to the bit pattern represented by mode.
FileUtils.chmod_R 0700, "/tmp/app.#{$$}"
# File lib/fileutils.rb, line 891 891: def chmod_R(mode, list, options = {}) 892: fu_check_options options, OPT_TABLE['chmod_R'] 893: list = fu_list(list) 894: fu_output_message sprintf('chmod -R%s %o %s', 895: (options[:force] ? 'f' : ''), 896: mode, list.join(' ')) if options[:verbose] 897: return if options[:noop] 898: list.each do |root| 899: Entry_.new(root).traverse do |ent| 900: begin 901: ent.chmod mode 902: rescue 903: raise unless options[:force] 904: end 905: end 906: end 907: end
Options: noop verbose
Changes owner and group on the named files (in list) to the user user and the group group. user and group may be an ID (Integer/String) or a name (String). If user or group is nil, this method does not change the attribute.
FileUtils.chown 'root', 'staff', '/usr/local/bin/ruby' FileUtils.chown nil, 'bin', Dir.glob('/usr/bin/*'), :verbose => true
# File lib/fileutils.rb, line 924 924: def chown(user, group, list, options = {}) 925: fu_check_options options, OPT_TABLE['chown'] 926: list = fu_list(list) 927: fu_output_message sprintf('chown %s%s', 928: [user,group].compact.join(':') + ' ', 929: list.join(' ')) if options[:verbose] 930: return if options[:noop] 931: uid = fu_get_uid(user) 932: gid = fu_get_gid(group) 933: list.each do |path| 934: Entry_.new(path).chown uid, gid 935: end 936: end
Options: noop verbose force
Changes owner and group on the named files (in list) to the user user and the group group recursively. user and group may be an ID (Integer/String) or a name (String). If user or group is nil, this method does not change the attribute.
FileUtils.chown_R 'www', 'www', '/var/www/htdocs' FileUtils.chown_R 'cvs', 'cvs', '/var/cvs', :verbose => true
# File lib/fileutils.rb, line 953 953: def chown_R(user, group, list, options = {}) 954: fu_check_options options, OPT_TABLE['chown_R'] 955: list = fu_list(list) 956: fu_output_message sprintf('chown -R%s %s%s', 957: (options[:force] ? 'f' : ''), 958: [user,group].compact.join(':') + ' ', 959: list.join(' ')) if options[:verbose] 960: return if options[:noop] 961: uid = fu_get_uid(user) 962: gid = fu_get_gid(group) 963: return unless uid or gid 964: list.each do |root| 965: Entry_.new(root).traverse do |ent| 966: begin 967: ent.chown uid, gid 968: rescue 969: raise unless options[:force] 970: end 971: end 972: end 973: 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 798 798: def compare_file(a, b) 799: return false unless File.size(a) == File.size(b) 800: File.open(a, 'rb') {|fa| 801: File.open(b, 'rb') {|fb| 802: return compare_stream(fa, fb) 803: } 804: } 805: end
Returns true if the contents of a stream a and b are identical.
# File lib/fileutils.rb, line 816 816: def compare_stream(a, b) 817: bsize = fu_stream_blksize(a, b) 818: sa = sb = nil 819: while sa == sb 820: sa = a.read(bsize) 821: sb = b.read(bsize) 822: unless sa and sb 823: if sa.nil? and sb.nil? 824: return true 825: end 826: end 827: end 828: false 829: end
Copies a file system entry src to dest. If src is a directory, this method copies its contents recursively. This method preserves file types, c.f. symlink, directory… (FIFO, device files and etc. are not supported yet)
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, permissions and modified time.
If dereference_root is true, this method dereference tree root.
If remove_destination is true, this method removes each destination file before copy.
# File lib/fileutils.rb, line 448 448: def copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false) 449: Entry_.new(src, nil, dereference_root).traverse do |ent| 450: destent = Entry_.new(dest, ent.rel, false) 451: File.unlink destent.path if remove_destination && File.file?(destent.path) 452: ent.copy destent.path 453: ent.copy_metadata destent.path if preserve 454: end 455: end
Copies file contents of src to dest. Both of src and dest must be a path name.
# File lib/fileutils.rb, line 462 462: def copy_file(src, dest, preserve = false, dereference = true) 463: ent = Entry_.new(src, nil, dereference) 464: ent.copy_file dest 465: ent.copy_metadata dest if preserve 466: end
Copies stream src to dest. src must respond to read(n) and dest must respond to write(str).
# File lib/fileutils.rb, line 474 474: def copy_stream(src, dest) 475: fu_copy_stream0 src, dest, fu_stream_blksize(src, dest) 476: end
Options: preserve noop verbose
Copies a file content 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 FileUtils.cp 'symlink', 'dest' # copy content, "dest" is not a symlink
# File lib/fileutils.rb, line 378 378: def cp(src, dest, options = {}) 379: fu_check_options options, OPT_TABLE['cp'] 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
Options: preserve noop verbose dereference_root remove_destination
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 => true, :verbose => true # 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 417 417: def cp_r(src, dest, options = {}) 418: fu_check_options options, OPT_TABLE['cp_r'] 419: fu_output_message "cp -r#{options[:preserve] ? 'p' : ''}#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose] 420: return if options[:noop] 421: options = options.dup 422: options[:dereference_root] = true unless options.key?(:dereference_root) 423: fu_each_src_dest(src, dest) do |s, d| 424: copy_entry s, d, options[:preserve], options[:dereference_root], options[:remove_destination] 425: end 426: end
# File lib/fileutils.rb, line 738 738: def fu_have_symlink? #:nodoc 739: File.symlink nil, nil 740: rescue NotImplementedError 741: return false 742: rescue 743: return true 744: end
# File lib/fileutils.rb, line 733 733: def fu_world_writable?(st) 734: (st.mode & 0002) != 0 735: end
Options: mode preserve 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. This method removes destination before copy.
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 842 842: def install(src, dest, options = {}) 843: fu_check_options options, OPT_TABLE['install'] 844: fu_output_message "install -c#{options[:preserve] && ' -p'}#{options[:mode] ? (' -m 0%o' % options[:mode]) : ''} #{[src,dest].flatten.join ' '}" if options[:verbose] 845: return if options[:noop] 846: fu_each_src_dest(src, dest) do |s, d| 847: unless File.exist?(d) and compare_file(s, d) 848: remove_file d, true 849: st = File.stat(s) if options[:preserve] 850: copy_file s, d 851: File.utime st.atime, st.mtime, d if options[:preserve] 852: File.chmod options[:mode], d if options[:mode] 853: end 854: end 855: end
Options: force noop verbose
Creates a hard link new which points to old. If new already exists and it is a directory, creates a 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 '/sbin' FileUtils.ln %w(cp mv mkdir), '/bin' # Now /sbin/cp and /bin/cp are linked.
# File lib/fileutils.rb, line 293 293: def ln(src, dest, options = {}) 294: fu_check_options options, OPT_TABLE['ln'] 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
Options: force noop verbose
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 332 332: def ln_s(src, dest, options = {}) 333: fu_check_options options, OPT_TABLE['ln_s'] 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
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 165 165: def mkdir(list, options = {}) 166: fu_check_options options, OPT_TABLE['mkdir'] 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
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 195 195: def mkdir_p(list, options = {}) 196: fu_check_options options, OPT_TABLE['mkdir_p'] 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
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 491 491: def mv(src, dest, options = {}) 492: fu_check_options options, OPT_TABLE['mv'] 493: fu_output_message "mv#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose] 494: return if options[:noop] 495: fu_each_src_dest(src, dest) do |s, d| 496: destent = Entry_.new(d, nil, true) 497: begin 498: if destent.exist? 499: if destent.directory? 500: raise Errno::EEXIST, dest 501: else 502: destent.remove_file if rename_cannot_overwrite_file? 503: end 504: end 505: begin 506: File.rename s, d 507: rescue Errno::EXDEV 508: copy_entry s, d, true 509: if options[:secure] 510: remove_entry_secure s, options[:force] 511: else 512: remove_entry s, options[:force] 513: end 514: end 515: rescue SystemCallError 516: raise unless options[:force] 517: end 518: end 519: end
Removes a directory dir and its contents recursively. This method ignores StandardError if force is true.
# File lib/fileutils.rb, line 787 787: def remove_dir(path, force = false) 788: remove_entry path, force # FIXME?? check if it is a directory 789: end
This method removes a file system entry path. path might be a regular file, a directory, or something. If path is a directory, remove it recursively.
See also remove_entry_secure.
# File lib/fileutils.rb, line 759 759: def remove_entry(path, force = false) 760: Entry_.new(path).postorder_traverse do |ent| 761: begin 762: ent.remove 763: rescue 764: raise unless force 765: end 766: end 767: rescue 768: raise unless force 769: end
This method removes a file system entry path. path shall be a regular file, a directory, or something. If path is a directory, remove it recursively. This method is required to avoid TOCTTOU (time-of-check-to-time-of-use) local security vulnerability of rm_r. rm_r causes security hole when:
* Parent directory is world writable (including /tmp). * Removing directory tree includes world writable directory. * The system has symbolic link.
To avoid this security hole, this method applies special preprocess. If path is a directory, this method chown(2) and chmod(2) all removing directories. This requires the current process is the owner of the removing whole directory tree, or is the super user (root).
WARNING: You must ensure that ALL parent directories cannot be moved by other untrusted users. For example, parent directories should not be owned by untrusted users, and should not be world writable except when the sticky bit set.
WARNING: Only the owner of the removing directory tree, or Unix super user (root) should invoke this method. Otherwise this method does not work.
For details of this security vulnerability, see Perl‘s case:
http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CAN-2005-0448 http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CAN-2004-0452
For fileutils.rb, this vulnerability is reported in [ruby-dev:26100].
# File lib/fileutils.rb, line 677 677: def remove_entry_secure(path, force = false) 678: unless fu_have_symlink? 679: remove_entry path, force 680: return 681: end 682: fullpath = File.expand_path(path) 683: st = File.lstat(fullpath) 684: unless st.directory? 685: File.unlink fullpath 686: return 687: end 688: # is a directory. 689: parent_st = File.stat(File.dirname(fullpath)) 690: unless fu_world_writable?(parent_st) 691: remove_entry path, force 692: return 693: end 694: unless parent_st.sticky? 695: raise ArgumentError, "parent directory is world writable, FileUtils#remove_entry_secure does not work; abort: #{path.inspect} (parent directory mode #{'%o' % parent_st.mode})" 696: end 697: # freeze tree root 698: euid = Process.euid 699: File.open(fullpath + '/.') {|f| 700: unless fu_stat_identical_entry?(st, f.stat) 701: # symlink (TOC-to-TOU attack?) 702: File.unlink fullpath 703: return 704: end 705: f.chown euid, -1 706: f.chmod 0700 707: unless fu_stat_identical_entry?(st, File.lstat(fullpath)) 708: # TOC-to-TOU attack? 709: File.unlink fullpath 710: return 711: end 712: } 713: # ---- tree root is frozen ---- 714: root = Entry_.new(path) 715: root.preorder_traverse do |ent| 716: if ent.directory? 717: ent.chown euid, -1 718: ent.chmod 0700 719: end 720: end 721: root.postorder_traverse do |ent| 722: begin 723: ent.remove 724: rescue 725: raise unless force 726: end 727: end 728: rescue 729: raise unless force 730: end
Removes a file path. This method ignores StandardError if force is true.
# File lib/fileutils.rb, line 776 776: def remove_file(path, force = false) 777: Entry_.new(path).remove_file 778: rescue 779: raise unless force 780: 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 543 543: def rm(list, options = {}) 544: fu_check_options options, OPT_TABLE['rm'] 545: list = fu_list(list) 546: fu_output_message "rm#{options[:force] ? ' -f' : ''} #{list.join ' '}" if options[:verbose] 547: return if options[:noop] 548: 549: list.each do |path| 550: remove_file path, options[:force] 551: end 552: end
Options: force noop verbose secure
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 # :-)
WARNING: This method causes local vulnerability if one of parent directories or removing directory tree are world writable (including /tmp, whose permission is 1777), and the current process has strong privilege such as Unix super user (root), and the system has symbolic link. For secure removing, read the documentation of remove_entry_secure carefully, and set :secure option to true. Default is :secure=>false.
NOTE: This method calls remove_entry_secure if :secure option is set. See also remove_entry_secure.
# File lib/fileutils.rb, line 603 603: def rm_r(list, options = {}) 604: fu_check_options options, OPT_TABLE['rm_r'] 605: # options[:secure] = true unless options.key?(:secure) 606: list = fu_list(list) 607: fu_output_message "rm -r#{options[:force] ? 'f' : ''} #{list.join ' '}" if options[:verbose] 608: return if options[:noop] 609: list.each do |path| 610: if options[:secure] 611: remove_entry_secure path, options[:force] 612: else 613: remove_entry path, options[:force] 614: end 615: end 616: end
Options: noop verbose secure
Equivalent to
#rm_r(list, :force => true)
WARNING: This method causes local vulnerability. Read the documentation of rm_r first.
# File lib/fileutils.rb, line 631 631: def rm_rf(list, options = {}) 632: fu_check_options options, OPT_TABLE['rm_rf'] 633: options = options.dup 634: options[:force] = true 635: rm_r list, options 636: 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 258 258: def rmdir(list, options = {}) 259: fu_check_options options, OPT_TABLE['rmdir'] 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
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 1024 1024: def touch(list, options = {}) 1025: fu_check_options options, OPT_TABLE['touch'] 1026: list = fu_list(list) 1027: created = nocreate = options[:nocreate] 1028: t = options[:mtime] 1029: if options[:verbose] 1030: fu_output_message "touch #{nocreate ? ' -c' : ''}#{t ? t.strftime(' -t %Y%m%d%H%M.%S') : ''}#{list.join ' '}" 1031: end 1032: return if options[:noop] 1033: list.each do |path| 1034: created = nocreate 1035: begin 1036: File.utime(t, t, path) 1037: rescue Errno::ENOENT 1038: raise if created 1039: File.open(path, 'a') { 1040: ; 1041: } 1042: created = true 1043: retry if t 1044: end 1045: end 1046: 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 141 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