| Module | FileUtils |
| In: |
lib/fileutils.rb
|
Copyright © 2000-2005 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(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() - ['private_module_function'] |
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 1473
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
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, :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
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 853
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
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 874
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
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 907
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
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 936
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
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 782
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
Returns true if the contents of a stream a and b are identical.
# File lib/fileutils.rb, line 800
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
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.
# File lib/fileutils.rb, line 444
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
Copies file contents of src to dest. Both of src and dest must be a path name.
# File lib/fileutils.rb, line 457
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
Copies stream src to dest. src must respond to read(n) and dest must respond to write(str).
# File lib/fileutils.rb, line 469
469: def copy_stream(src, dest)
470: fu_copy_stream0 src, dest, fu_stream_blksize(src, dest)
471: 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, :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
Options: preserve noop verbose dereference_root
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, :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
# File lib/fileutils.rb, line 722
722: def fu_have_symlink? #:nodoc
723: File.symlink nil, nil
724: rescue NotImplementedError
725: return false
726: rescue
727: return true
728: end
# File lib/fileutils.rb, line 718
718: def fu_world_writable?(st)
719: (st.mode & 0002) != 0
720: 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 825
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
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 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, :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
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 332
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
Options: noop verbose
Same as
#ln_s(src, dest, :force)
# File lib/fileutils.rb, line 355
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
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, :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
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, :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
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 486
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
Options: (none)
Returns the name of the current directory.
# File lib/fileutils.rb, line 100
100: def pwd
101: Dir.pwd
102: end
Removes a directory dir and its contents recursively. This method ignores StandardError if force is true.
# File lib/fileutils.rb, line 771
771: def remove_dir(path, force = false)
772: remove_entry path, force # FIXME?? check if it is a directory
773: 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 743
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
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 667
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
Removes a file path. This method ignores StandardError if force is true.
# File lib/fileutils.rb, line 760
760: def remove_file(path, force = false)
761: Entry_.new(path).remove_file
762: rescue
763: raise unless force
764: 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 533
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
Options: noop verbose
Equivalent to
#rm(list, :force => true)
# File lib/fileutils.rb, line 558
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
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 593
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
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 621
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
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, :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
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 1006
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
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