file-digests 0.0.16 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/file-digests +0 -3
- data/bin/file-digests-test +1 -2
- data/lib/file-digests.rb +55 -29
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27ef5c07b544bb7e63a8b9ca9d8b99d0b46588b45311ef940d7431691178b99c
|
4
|
+
data.tar.gz: b1febf1fbdabab014eca65a86e3beee4cfaf4478ead18a5da071b045ae0ab56a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4d0db5b5ca2a29adaac1fa9aaefd276977f39d0c6e7e17e5432586d63a91a5bb20bd4af8c955e4e5c625460102ae1431b33947e5c40cc9351c5bb858f28974b
|
7
|
+
data.tar.gz: 3dc1ec4ac2224a84d1cc914b81314ccfba5580a11ca179ac9b4ca9201a599f57a52edb997af8adc369861124a733d1030f5dcb2a234dbf3c10a68b03690a5b3a
|
data/bin/file-digests
CHANGED
data/bin/file-digests-test
CHANGED
data/lib/file-digests.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
require 'date'
|
3
2
|
require 'set'
|
4
3
|
require 'digest'
|
@@ -9,12 +8,19 @@ require 'sqlite3'
|
|
9
8
|
module FileDigests
|
10
9
|
|
11
10
|
def self.perform_check
|
12
|
-
|
11
|
+
options = {
|
12
|
+
auto: (ENV["AUTO"] == "true"),
|
13
|
+
quiet: (ENV["QUIET"] == "true"),
|
14
|
+
test_only: (ENV["TEST_ONLY"] == "true")
|
15
|
+
}
|
16
|
+
checker = Checker.new ARGV[0], ARGV[1], options
|
13
17
|
checker.perform_check
|
14
18
|
end
|
15
19
|
|
16
20
|
class DigestDatabase
|
17
|
-
def initialize path
|
21
|
+
def initialize path, options = {}
|
22
|
+
@options = options
|
23
|
+
|
18
24
|
@db = SQLite3::Database.new path.to_s
|
19
25
|
@db.results_as_hash = true
|
20
26
|
|
@@ -55,8 +61,8 @@ module FileDigests
|
|
55
61
|
|
56
62
|
if found['digest'] == digest
|
57
63
|
counters[:good] += 1
|
58
|
-
# puts "GOOD: #{file_path}" unless
|
59
|
-
unless
|
64
|
+
# puts "GOOD: #{file_path}" unless @options[:quiet]
|
65
|
+
unless @options[:test_only]
|
60
66
|
if found['mtime'] == mtime
|
61
67
|
touch_digest_check_time found['id']
|
62
68
|
else
|
@@ -69,47 +75,50 @@ module FileDigests
|
|
69
75
|
STDERR.puts "LIKELY DAMAGED: #{file_path}"
|
70
76
|
else
|
71
77
|
counters[:updated] += 1
|
72
|
-
puts "UPDATED: #{file_path}" unless
|
73
|
-
unless
|
78
|
+
puts "UPDATED: #{file_path}" unless @options[:quiet]
|
79
|
+
unless @options[:test_only]
|
74
80
|
update_mtime_and_digest mtime, digest, found['id']
|
75
81
|
end
|
76
82
|
end
|
77
83
|
end
|
78
84
|
else
|
79
85
|
counters[:new] += 1
|
80
|
-
puts "NEW: #{file_path}" unless
|
81
|
-
unless
|
86
|
+
puts "NEW: #{file_path}" unless @options[:quiet]
|
87
|
+
unless @options[:test_only]
|
82
88
|
@new_files[file_path] = digest
|
83
89
|
insert file_path, mtime, digest
|
84
90
|
end
|
85
91
|
end
|
86
92
|
end
|
87
93
|
|
88
|
-
def
|
94
|
+
def track_renames counters
|
89
95
|
@missing_files.delete_if do |filename, digest|
|
90
96
|
if @new_files.value?(digest)
|
91
97
|
counters[:renamed] += 1
|
92
|
-
unless
|
98
|
+
unless @options[:test_only]
|
93
99
|
delete_by_filename filename
|
94
100
|
end
|
95
101
|
true
|
96
102
|
end
|
97
103
|
end
|
104
|
+
counters[:missing] = @missing_files.length
|
105
|
+
end
|
98
106
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
107
|
+
def any_missing_files?
|
108
|
+
@missing_files.length > 0
|
109
|
+
end
|
110
|
+
|
111
|
+
def print_missing_files
|
112
|
+
puts "\nMISSING FILES:"
|
113
|
+
@missing_files.sort.to_h.each do |filename, digest|
|
114
|
+
puts filename
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def remove_missing_files
|
119
|
+
@db.transaction do
|
120
|
+
@missing_files.each do |filename, digest|
|
121
|
+
delete_by_filename filename
|
113
122
|
end
|
114
123
|
end
|
115
124
|
end
|
@@ -130,7 +139,8 @@ module FileDigests
|
|
130
139
|
end
|
131
140
|
|
132
141
|
class Checker
|
133
|
-
def initialize files_path, digest_database_path
|
142
|
+
def initialize files_path, digest_database_path, options = {}
|
143
|
+
@options = options
|
134
144
|
@files_path = cleanup_path(files_path || ".")
|
135
145
|
@prefix_to_remove = @files_path.to_s + '/'
|
136
146
|
|
@@ -152,11 +162,12 @@ module FileDigests
|
|
152
162
|
|
153
163
|
ensure_dir_exists @digest_database_path.dirname
|
154
164
|
|
165
|
+
# Please do not use this flag, support for sha512 is here for backward compatibility, and one day it will be removed.
|
155
166
|
if File.exist?(@digest_database_path.dirname + '.file-digests.sha512')
|
156
167
|
@use_sha512 = true
|
157
168
|
end
|
158
169
|
|
159
|
-
@digest_database = DigestDatabase.new @digest_database_path
|
170
|
+
@digest_database = DigestDatabase.new @digest_database_path, @options
|
160
171
|
@counters = {good: 0, updated: 0, new: 0, missing: 0, renamed: 0, likely_damaged: 0, exceptions: 0}
|
161
172
|
end
|
162
173
|
|
@@ -167,7 +178,14 @@ module FileDigests
|
|
167
178
|
end
|
168
179
|
end
|
169
180
|
|
170
|
-
@digest_database.
|
181
|
+
@digest_database.track_renames @counters
|
182
|
+
|
183
|
+
if @digest_database.any_missing_files?
|
184
|
+
@digest_database.print_missing_files
|
185
|
+
if !@options[:test_only] && (@options[:auto] || confirm("Remove missing files from the database"))
|
186
|
+
@digest_database.remove_missing_files
|
187
|
+
end
|
188
|
+
end
|
171
189
|
|
172
190
|
if @counters[:likely_damaged] > 0 || @counters[:exceptions] > 0
|
173
191
|
STDERR.puts "ERRORS WERE OCCURRED"
|
@@ -178,6 +196,14 @@ module FileDigests
|
|
178
196
|
|
179
197
|
private
|
180
198
|
|
199
|
+
|
200
|
+
def confirm text
|
201
|
+
if STDIN.tty? && STDOUT.tty?
|
202
|
+
puts "#{text} (y/n)?"
|
203
|
+
STDIN.gets.strip.downcase == "y"
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
181
207
|
def process_file filename
|
182
208
|
return if File.symlink? filename
|
183
209
|
|
@@ -250,7 +276,7 @@ module FileDigests
|
|
250
276
|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
251
277
|
yield
|
252
278
|
elapsed = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start).to_i
|
253
|
-
puts "Elapsed time: #{elapsed / 3600}h #{(elapsed % 3600) / 60}m #{elapsed % 60}s" unless
|
279
|
+
puts "Elapsed time: #{elapsed / 3600}h #{(elapsed % 3600) / 60}m #{elapsed % 60}s" unless @options[:quiet]
|
254
280
|
end
|
255
281
|
|
256
282
|
end
|