Class: FileDigests
- Inherits:
-
Object
- Object
- FileDigests
- Defined in:
- lib/file-digests.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(files_path, digest_database_path, options = {}) ⇒ FileDigests
constructor
A new instance of FileDigests.
- #initialize_database(path) ⇒ Object
- #perform_check ⇒ Object
- #show_duplicates ⇒ Object
Constructor Details
#initialize(files_path, digest_database_path, options = {}) ⇒ FileDigests
Returns a new instance of FileDigests.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/file-digests.rb', line 25 def initialize files_path, digest_database_path, = {} = @files_path = cleanup_path(files_path || ".") @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 cleanup_path(digest_database_path) 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 ensure_dir_exists @digest_database_path.dirname # Please do not use this flag, support for sha512 is here for backward compatibility, and one day it will be removed. if File.exist?(@digest_database_path.dirname + '.file-digests.sha512') @use_sha512 = true end initialize_database @digest_database_path end |
Class Method Details
.perform_check ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/file-digests.rb', line 10 def self.perform_check = { auto: (ENV["AUTO"] == "true"), quiet: (ENV["QUIET"] == "true"), test_only: (ENV["TEST_ONLY"] == "true") } file_digests = self.new ARGV[0], ARGV[1], file_digests.perform_check end |
.show_duplicates ⇒ Object
20 21 22 23 |
# File 'lib/file-digests.rb', line 20 def self.show_duplicates file_digests = self.new ARGV[0], ARGV[1] file_digests.show_duplicates end |
Instance Method Details
#initialize_database(path) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/file-digests.rb', line 57 def initialize_database path @db = SQLite3::Database.new path.to_s @db.results_as_hash = true execute 'PRAGMA journal_mode = "WAL"' execute 'PRAGMA synchronous = "NORMAL"' execute 'PRAGMA locking_mode = "EXCLUSIVE"' execute 'PRAGMA cache_size = "5000"' unless execute("SELECT name FROM sqlite_master WHERE type='table' AND name = 'digests'").length == 1 execute 'PRAGMA encoding = "UTF-8"' execute "CREATE TABLE digests ( id INTEGER PRIMARY KEY, filename TEXT, mtime TEXT, digest TEXT, digest_check_time TEXT)" execute "CREATE UNIQUE INDEX digests_filename ON digests(filename)" end prepare_method :insert, "INSERT INTO digests (filename, mtime, digest, digest_check_time) VALUES (?, ?, ?, datetime('now'))" prepare_method :find_by_filename, "SELECT id, mtime, digest FROM digests WHERE filename = ?" prepare_method :touch_digest_check_time, "UPDATE digests SET digest_check_time = datetime('now') WHERE id = ?" prepare_method :update_mtime_and_digest, "UPDATE digests SET mtime = ?, digest = ?, digest_check_time = datetime('now') WHERE id = ?" prepare_method :update_mtime, "UPDATE digests SET mtime = ?, digest_check_time = datetime('now') WHERE id = ?" prepare_method :delete_by_filename, "DELETE FROM digests WHERE filename = ?" prepare_method :query_duplicates, "SELECT digest, filename FROM digests WHERE digest IN (SELECT digest FROM digests GROUP BY digest HAVING count(*) > 1) ORDER BY digest, filename;" end |
#perform_check ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/file-digests.rb', line 86 def perform_check @counters = {good: 0, updated: 0, new: 0, missing: 0, renamed: 0, likely_damaged: 0, exceptions: 0} @missing_files = Hash[@db.prepare("SELECT filename, digest FROM digests").execute!] @new_files = {} measure_time do walk_files do |filename| process_file filename end end track_renames if any_missing_files? print_missing_files if ![:test_only] && ([:auto] || confirm("Remove missing files from the database")) remove_missing_files end end if @counters[:likely_damaged] > 0 || @counters[:exceptions] > 0 STDERR.puts "ERRORS WERE OCCURRED" end puts @counters.inspect end |
#show_duplicates ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/file-digests.rb', line 113 def show_duplicates current_digest = nil result = query_duplicates while found = result.next_hash do if current_digest != found['digest'] puts "" if current_digest current_digest = found['digest'] puts "#{found['digest']}:" end puts " #{found['filename']}" end end |