Module: FileUtils
- Included in:
- BaseAction, SFTP
- Defined in:
- lib/lockr/fileutils.rb
Class Method Summary collapse
-
.calculate_sha512_hash(filename) ⇒ Object
calculate the sha512 hash of a file.
-
.copy(file_src, file_target) ⇒ Object
copy file_src to file_target.
-
.load_obj_yaml(file) ⇒ Object
load an yaml object from file.
-
.rotate_file(file, limit) ⇒ Object
rotate the provided file with a maximum of ‘limit’ backups renamed filed will be named file_0, file_1, …
-
.store_obj_yaml(file, object) ⇒ Object
store an object as yaml to file.
Class Method Details
.calculate_sha512_hash(filename) ⇒ Object
calculate the sha512 hash of a file
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/lockr/fileutils.rb', line 59 def FileUtils.calculate_sha512_hash( filename) sha512 = OpenSSL::Digest::SHA512.new File.open( filename) do |file| buffer = '' # Read the file 512 bytes at a time while not file.eof file.read(512, buffer) sha512.update(buffer) end end sha512.to_s end |
.copy(file_src, file_target) ⇒ Object
copy file_src to file_target
26 27 28 29 30 31 32 33 34 |
# File 'lib/lockr/fileutils.rb', line 26 def FileUtils.copy( file_src, file_target) return unless File.exists?( file_src) dst = File.new( file_target, 'w') File.open( file_src, 'r') do |src| dst.write( src.read) end dst.close end |
.load_obj_yaml(file) ⇒ Object
load an yaml object from file
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lockr/fileutils.rb', line 44 def FileUtils.load_obj_yaml( file) object = {} unless File.exist?( file) return object end File.open( file, 'r') do |f| object = YAML::load(f) end object end |
.rotate_file(file, limit) ⇒ Object
rotate the provided file with a maximum of ‘limit’ backups renamed filed will be named file_0, file_1, …
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/lockr/fileutils.rb', line 5 def FileUtils.rotate_file( file, limit) return unless File.exists?(file) # move old files first max_files = limit - 1 max_files.downto( 0) { |i| if i == 0 FileUtils.copy( file, "#{file}_#{i}") else j = i - 1 if File.exists?("#{file}_#{j}") FileUtils.copy( "#{file}_#{j}", "#{file}_#{i}") end end } puts "Rotated local vault file(s)" end |
.store_obj_yaml(file, object) ⇒ Object
store an object as yaml to file
37 38 39 40 41 |
# File 'lib/lockr/fileutils.rb', line 37 def FileUtils.store_obj_yaml( file, object) File.open( file, 'w') do |f| f.write( object.to_yaml) end end |