Class: FileDigests::Checker
- Inherits:
-
Object
- Object
- FileDigests::Checker
- Defined in:
- lib/file-digests.rb
Instance Method Summary collapse
- #get_file_digest(filename) ⇒ Object
-
#initialize(files_path, digest_database_path) ⇒ Checker
constructor
A new instance of Checker.
- #perform_check ⇒ Object
- #process_file(filename) ⇒ Object
- #walk_files ⇒ Object
Constructor Details
#initialize(files_path, digest_database_path) ⇒ Checker
Returns a new instance of Checker.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/file-digests.rb', line 154 def initialize files_path, digest_database_path @files_path = Pathname.new(FileDigests::patch_path_string(files_path || ".")).cleanpath @prefix_to_remove = @files_path.to_s + '/' raise "Files path must be a readable directory" unless (File.directory?(@files_path) && File.readable?(@files_path)) @digest_database_path = if digest_database_path Pathname.new(FileDigests::patch_path_string(digest_database_path)).cleanpath else @files_path + '.file-digests.sqlite' end if File.directory?(@digest_database_path) @digest_database_path = @digest_database_path + '.file-digests.sqlite' end if @files_path == @digest_database_path.dirname @skip_file_digests_sqlite = true end FileDigests::ensure_dir_exists @digest_database_path.dirname if File.exist?(@digest_database_path.dirname + '.file-digests.sha512') @use_sha512 = true end @digest_database = DigestDatabase.new @digest_database_path @counters = {good: 0, updated: 0, new: 0, missing: 0, renamed: 0, likely_damaged: 0, exceptions: 0} end |
Instance Method Details
#get_file_digest(filename) ⇒ Object
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/file-digests.rb', line 239 def get_file_digest filename File.open(filename, 'rb') do |io| digest = (@use_sha512 ? Digest::SHA512 : Digest::SHA256).new buffer = "" while io.read(40960, buffer) digest.update(buffer) end return digest.hexdigest end end |
#perform_check ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/file-digests.rb', line 184 def perform_check FileDigests::measure_time do walk_files do |filename| process_file filename end end @digest_database.process_missing_files @counters if @counters[:likely_damaged] > 0 || @counters[:exceptions] > 0 STDERR.puts "ERRORS WERE OCCURRED" end puts @counters.inspect end |
#process_file(filename) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/file-digests.rb', line 206 def process_file filename return if File.symlink? filename stat = File.stat filename return if stat.blockdev? return if stat.chardev? return if stat.directory? return if stat.pipe? unless stat.readable? raise "File is not readable" end return if stat.socket? if @skip_file_digests_sqlite basename = File.basename(filename) return if basename == '.file-digests.sha512' return if basename == '.file-digests.sqlite' return if basename == '.file-digests.sqlite-wal' return if basename == '.file-digests.sqlite-shm' end @digest_database.insert_or_update( filename.delete_prefix(@prefix_to_remove).encode('utf-8', universal_newline: true).unicode_normalize(:nfkc), stat.mtime.utc.strftime('%Y-%m-%d %H:%M:%S'), get_file_digest(filename), @counters ) rescue => exception @counters[:exceptions] += 1 STDERR.puts "EXCEPTION: #{filename}: #{exception.message}" end |
#walk_files ⇒ Object
200 201 202 203 204 |
# File 'lib/file-digests.rb', line 200 def walk_files Dir.glob(@files_path + '**' + '*', File::FNM_DOTMATCH) do |filename| yield filename end end |