Add some class methods to the FileUtils class to make some routine directory/file tests and management a bit easier.
Return true
if the given directory name is absolute.
# File buryspam.rb, line 435 def dir_absolute?(dir) dir[0] == // end
Create the given directory if it doesn't already exist.
# File buryspam.rb, line 429 def ensure_dir_exists(dir) return if File.directory?(dir) mkdir_p(dir) end
Return true
if there is sufficient disk space available in
your home directory, according to the min_disk_free
configuration parameter setting
# File buryspam.rb, line 458 def free_space? return %x{/bin/df -Pk #{ENV['HOME']}}. split(/\n/). pop.split[3]. to_i * 1024 > Buryspam::Config.min_disk_free end
Rename the file to the proposed new name. Ensure new file name is unique by appending a four digit sequence number, if necessary. Assumes old_filename and directory of new_filename both exist.
# File buryspam.rb, line 442 def rename_file_uniq(old_filename, new_filename) Logger.debug("Renaming file '#{old_filename}' to '#{new_filename}'") seq = 1 while File.exist?(new_filename) Logger.info("'#{new_filename}' already exists.") ext = ".%04d" % seq new_filename.gsub!(/\.\d+$/, ext) || new_filename << ext seq += 1 end File.rename(old_filename, new_filename) Logger.info("'#{old_filename}' renamed to '#{new_filename}'") end