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 1490 1490: def FileUtils.have_option?(mid, opt) 1491: li = OPT_TABLE[mid.to_s] or raise ArgumentError, "no such method: #{mid}" 1492: li.include?(opt) 1493: 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 864 864: def chmod(mode, list, options = {}) 865: fu_check_options options, OPT_TABLE['chmod'] 866: list = fu_list(list) 867: fu_output_message sprintf('chmod %o %s', mode, list.join(' ')) if options[:verbose] 868: return if options[:noop] 869: list.each do |path| 870: Entry_.new(path).chmod mode 871: end 872: 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 885 885: def chmod_R(mode, list, options = {}) 886: fu_check_options options, OPT_TABLE['chmod_R'] 887: list = fu_list(list) 888: fu_output_message sprintf('chmod -R%s %o %s', 889: (options[:force] ? 'f' : ''), 890: mode, list.join(' ')) if options[:verbose] 891: return if options[:noop] 892: list.each do |root| 893: Entry_.new(root).traverse do |ent| 894: begin 895: ent.chmod mode 896: rescue 897: raise unless options[:force] 898: end 899: end 900: end 901: 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 918 918: def chown(user, group, list, options = {}) 919: fu_check_options options, OPT_TABLE['chown'] 920: list = fu_list(list) 921: fu_output_message sprintf('chown %s%s', 922: [user,group].compact.join(':') + ' ', 923: list.join(' ')) if options[:verbose] 924: return if options[:noop] 925: uid = fu_get_uid(user) 926: gid = fu_get_gid(group) 927: list.each do |path| 928: Entry_.new(path).chown uid, gid 929: end 930: 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 947 947: def chown_R(user, group, list, options = {}) 948: fu_check_options options, OPT_TABLE['chown_R'] 949: list = fu_list(list) 950: fu_output_message sprintf('chown -R%s %s%s', 951: (options[:force] ? 'f' : ''), 952: [user,group].compact.join(':') + ' ', 953: list.join(' ')) if options[:verbose] 954: return if options[:noop] 955: uid = fu_get_uid(user) 956: gid = fu_get_gid(group) 957: return unless uid or gid 958: list.each do |root| 959: Entry_.new(root).traverse do |ent| 960: begin 961: ent.chown uid, gid 962: rescue 963: raise unless options[:force] 964: end 965: end 966: end 967: 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 792 792: def compare_file(a, b) 793: return false unless File.size(a) == File.size(b) 794: File.open(a, 'rb') {|fa| 795: File.open(b, 'rb') {|fb| 796: return compare_stream(fa, fb) 797: } 798: } 799: end
Returns true if the contents of a stream a and b are identical.
# File lib/fileutils.rb, line 810 810: def compare_stream(a, b) 811: bsize = fu_stream_blksize(a, b) 812: sa = sb = nil 813: while sa == sb 814: sa = a.read(bsize) 815: sb = b.read(bsize) 816: unless sa and sb 817: if sa.nil? and sb.nil? 818: return true 819: end 820: end 821: end 822: false 823: 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 447 447: def copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false) 448: Entry_.new(src, nil, dereference_root).traverse do |ent| 449: destent = Entry_.new(dest, ent.rel, false) 450: File.unlink destent.path if remove_destination && File.file?(destent.path) 451: ent.copy destent.path 452: ent.copy_metadata destent.path if preserve 453: end 454: end
Copies file contents of src to dest. Both of src and dest must be a path name.
# File lib/fileutils.rb, line 461 461: def copy_file(src, dest, preserve = false, dereference = true) 462: ent = Entry_.new(src, nil, dereference) 463: ent.copy_file dest 464: ent.copy_metadata dest if preserve 465: end
Copies stream src to dest. src must respond to read(n) and dest must respond to write(str).
# File lib/fileutils.rb, line 473 473: def copy_stream(src, dest) 474: fu_copy_stream0 src, dest, fu_stream_blksize(src, dest) 475: 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[: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], options[:remove_destination] 424: end 425: end
# File lib/fileutils.rb, line 732 732: def fu_have_symlink? #:nodoc 733: File.symlink nil, nil 734: rescue NotImplementedError 735: return false 736: rescue 737: return true 738: end
# File lib/fileutils.rb, line 727 727: def fu_world_writable?(st) 728: (st.mode & 0002) != 0 729: 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 836 836: def install(src, dest, options = {}) 837: fu_check_options options, OPT_TABLE['install'] 838: fu_output_message "install -c#{options[:preserve] && ' -p'}#{options[:mode] ? (' -m 0%o' % options[:mode]) : ''} #{[src,dest].flatten.join ' '}" if options[:verbose] 839: return if options[:noop] 840: fu_each_src_dest(src, dest) do |s, d| 841: unless File.exist?(d) and compare_file(s, d) 842: remove_file d, true 843: st = File.stat(s) if options[:preserve] 844: copy_file s, d 845: File.utime st.atime, st.mtime, d if options[:preserve] 846: File.chmod options[:mode], d if options[:mode] 847: end 848: end 849: 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 490 490: def mv(src, dest, options = {}) 491: fu_check_options options, OPT_TABLE['mv'] 492: fu_output_message "mv#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose] 493: return if options[:noop] 494: fu_each_src_dest(src, dest) do |s, d| 495: destent = Entry_.new(d, nil, true) 496: begin 497: if destent.exist? 498: if destent.directory? 499: raise Errno::EEXIST, dest 500: else 501: destent.remove_file if rename_cannot_overwrite_file? 502: end 503: end 504: begin 505: File.rename s, d 506: rescue Errno::EXDEV 507: copy_entry s, d, true 508: if options[:secure] 509: remove_entry_secure s, options[:force] 510: else 511: remove_entry s, options[:force] 512: end 513: end 514: rescue SystemCallError 515: raise unless options[:force] 516: end 517: end 518: end
Removes a directory dir and its contents recursively. This method ignores StandardError if force is true.
# File lib/fileutils.rb, line 781 781: def remove_dir(path, force = false) 782: remove_entry path, force # FIXME?? check if it is a directory 783: 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 753 753: def remove_entry(path, force = false) 754: Entry_.new(path).postorder_traverse do |ent| 755: begin 756: ent.remove 757: rescue 758: raise unless force 759: end 760: end 761: rescue 762: raise unless force 763: 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 are not world writable. Otherwise this method does not work. Only exception is temporary directory like /tmp and /var/tmp, whose permission is 1777.
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 676 676: def remove_entry_secure(path, force = false) 677: unless fu_have_symlink? 678: remove_entry path, force 679: return 680: end 681: fullpath = File.expand_path(path) 682: st = File.lstat(fullpath) 683: unless st.directory? 684: File.unlink fullpath 685: return 686: end 687: # is a directory. 688: parent_st = File.stat(File.dirname(fullpath)) 689: unless fu_world_writable?(parent_st) 690: remove_entry path, force 691: return 692: end 693: unless parent_st.sticky? 694: raise ArgumentError, "parent directory is world writable, FileUtils#remove_entry_secure does not work; abort: #{path.inspect} (parent directory mode #{'%o' % parent_st.mode})" 695: end 696: # freeze tree root 697: euid = Process.euid 698: File.open(fullpath + '/.') {|f| 699: unless fu_stat_identical_entry?(st, f.stat) 700: # symlink (TOC-to-TOU attack?) 701: File.unlink fullpath 702: return 703: end 704: f.chown euid, -1 705: f.chmod 0700 706: } 707: # ---- tree root is frozen ---- 708: root = Entry_.new(path) 709: root.preorder_traverse do |ent| 710: if ent.directory? 711: ent.chown euid, -1 712: ent.chmod 0700 713: end 714: end 715: root.postorder_traverse do |ent| 716: begin 717: ent.remove 718: rescue 719: raise unless force 720: end 721: end 722: rescue 723: raise unless force 724: end
Removes a file path. This method ignores StandardError if force is true.
# File lib/fileutils.rb, line 770 770: def remove_file(path, force = false) 771: Entry_.new(path).remove_file 772: rescue 773: raise unless force 774: 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 542 542: def rm(list, options = {}) 543: fu_check_options options, OPT_TABLE['rm'] 544: list = fu_list(list) 545: fu_output_message "rm#{options[:force] ? ' -f' : ''} #{list.join ' '}" if options[:verbose] 546: return if options[:noop] 547: 548: list.each do |path| 549: remove_file path, options[:force] 550: end 551: 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 602 602: def rm_r(list, options = {}) 603: fu_check_options options, OPT_TABLE['rm_r'] 604: # options[:secure] = true unless options.key?(:secure) 605: list = fu_list(list) 606: fu_output_message "rm -r#{options[:force] ? 'f' : ''} #{list.join ' '}" if options[:verbose] 607: return if options[:noop] 608: list.each do |path| 609: if options[:secure] 610: remove_entry_secure path, options[:force] 611: else 612: remove_entry path, options[:force] 613: end 614: end 615: 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 630 630: def rm_rf(list, options = {}) 631: fu_check_options options, OPT_TABLE['rm_rf'] 632: options = options.dup 633: options[:force] = true 634: rm_r list, options 635: 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 1018 1018: def touch(list, options = {}) 1019: fu_check_options options, OPT_TABLE['touch'] 1020: list = fu_list(list) 1021: created = nocreate = options[:nocreate] 1022: t = options[:mtime] 1023: if options[:verbose] 1024: fu_output_message "touch #{nocreate ? ' -c' : ''}#{t ? t.strftime(' -t %Y%m%d%H%M.%S') : ''}#{list.join ' '}" 1025: end 1026: return if options[:noop] 1027: list.each do |path| 1028: created = nocreate 1029: begin 1030: File.utime(t, t, path) 1031: rescue Errno::ENOENT 1032: raise if created 1033: File.open(path, 'a') { 1034: ; 1035: } 1036: created = true 1037: retry if t 1038: end 1039: end 1040: 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