Last active
August 29, 2015 13:56
-
-
Save yanmhlv/9344953 to your computer and use it in GitHub Desktop.
Получение списка хешей, состоящего из пути, sha1-хеша и информации о файле.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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