Skip to content

Instantly share code, notes, and snippets.

@yanmhlv
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanmhlv/9344953 to your computer and use it in GitHub Desktop.
Save yanmhlv/9344953 to your computer and use it in GitHub Desktop.
Получение списка хешей, состоящего из пути, sha1-хеша и информации о файле.
require 'digest'
def get_hash(string=nil, file=nil, method='md5')
raise RuntimeError if string.nil? && file.nil?
method = method.upcase
method = Digest.class_eval(method)
if string
hash = method.hexdigest(string)
elsif file
hash = method.file(file)
end
hash
end
def fileinfo(path, full=true)
error = nil
begin
stat = File.stat(path)
hash = get_hash(path) if full
rescue
error = true
hash = nil
end
return {stat: stat, hash: hash, error: error, path: path}
end
root_dir = ARGV[0] ? Dir.exists?(ARGV[0]) : Dir.getwd
files = Dir.glob("#{root_dir}/*/**").select do |item|
File.file? item
end
files = files.map {|f| fileinfo(f, false)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment