file-digests 0.0.11 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/file-digests.rb +42 -32
- 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: db5f9c1faeeed556960cd8c1d6a448d0b860ff5145eefaa04890847222c04b87
|
4
|
+
data.tar.gz: b1858913665bd3ce971fd53fe605c5f5e83c3f54310cff0b57f9b4b08f3fddce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 303c02c2d5374c4438829dbabb3ac479e77d20f3f0051d86c7b0ecb01ea0ff1cb50db40b692c9749c5a7da5d77dc54012881df92d309bb12700a57c232a50c32
|
7
|
+
data.tar.gz: 78fc7896d1090778d4a0fea0642c22208a8de1bf89506a3ca5e5efc7949b3bb2b2e528a2b0e4a070ea6c66d66cee3a6b6c9850664225e6c303b0e8abdc273591
|
data/lib/file-digests.rb
CHANGED
@@ -8,27 +8,6 @@ require 'sqlite3'
|
|
8
8
|
|
9
9
|
module FileDigests
|
10
10
|
|
11
|
-
def self.ensure_dir_exists path
|
12
|
-
if File.exist?(path)
|
13
|
-
unless File.directory?(path)
|
14
|
-
raise "#{path} is not a directory"
|
15
|
-
end
|
16
|
-
else
|
17
|
-
FileUtils.mkdir_p path
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.measure_time
|
22
|
-
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
23
|
-
yield
|
24
|
-
elapsed = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start).to_i
|
25
|
-
puts "Elapsed time: #{elapsed / 3600}h #{(elapsed % 3600) / 60}m #{elapsed % 60}s" unless QUIET
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.patch_path_string path
|
29
|
-
Gem.win_platform? ? path.gsub(/\\/, '/') : path
|
30
|
-
end
|
31
|
-
|
32
11
|
def self.perform_check
|
33
12
|
checker = Checker.new ARGV[0], ARGV[1]
|
34
13
|
checker.perform_check
|
@@ -152,22 +131,26 @@ module FileDigests
|
|
152
131
|
|
153
132
|
class Checker
|
154
133
|
def initialize files_path, digest_database_path
|
155
|
-
@files_path =
|
134
|
+
@files_path = cleanup_path(files_path || ".")
|
156
135
|
@prefix_to_remove = @files_path.to_s + '/'
|
157
136
|
|
158
137
|
raise "Files path must be a readable directory" unless (File.directory?(@files_path) && File.readable?(@files_path))
|
159
138
|
|
160
139
|
@digest_database_path = if digest_database_path
|
161
|
-
|
140
|
+
cleanup_path(digest_database_path)
|
162
141
|
else
|
163
142
|
@files_path + '.file-digests.sqlite'
|
164
143
|
end
|
165
144
|
|
145
|
+
if File.directory?(@digest_database_path)
|
146
|
+
@digest_database_path = @digest_database_path + '.file-digests.sqlite'
|
147
|
+
end
|
148
|
+
|
166
149
|
if @files_path == @digest_database_path.dirname
|
167
150
|
@skip_file_digests_sqlite = true
|
168
151
|
end
|
169
152
|
|
170
|
-
|
153
|
+
ensure_dir_exists @digest_database_path.dirname
|
171
154
|
|
172
155
|
if File.exist?(@digest_database_path.dirname + '.file-digests.sha512')
|
173
156
|
@use_sha512 = true
|
@@ -178,7 +161,7 @@ module FileDigests
|
|
178
161
|
end
|
179
162
|
|
180
163
|
def perform_check
|
181
|
-
|
164
|
+
measure_time do
|
182
165
|
walk_files do |filename|
|
183
166
|
process_file filename
|
184
167
|
end
|
@@ -193,11 +176,7 @@ module FileDigests
|
|
193
176
|
puts @counters.inspect
|
194
177
|
end
|
195
178
|
|
196
|
-
|
197
|
-
Dir.glob(@files_path + '**' + '*', File::FNM_DOTMATCH) do |filename|
|
198
|
-
yield filename
|
199
|
-
end
|
200
|
-
end
|
179
|
+
private
|
201
180
|
|
202
181
|
def process_file filename
|
203
182
|
return if File.symlink? filename
|
@@ -222,14 +201,38 @@ module FileDigests
|
|
222
201
|
end
|
223
202
|
|
224
203
|
@digest_database.insert_or_update(
|
225
|
-
filename.delete_prefix(@prefix_to_remove).unicode_normalize(:nfkc),
|
204
|
+
filename.delete_prefix(@prefix_to_remove).encode('utf-8', universal_newline: true).unicode_normalize(:nfkc),
|
226
205
|
stat.mtime.utc.strftime('%Y-%m-%d %H:%M:%S'),
|
227
206
|
get_file_digest(filename),
|
228
207
|
@counters
|
229
208
|
)
|
230
209
|
rescue => exception
|
231
210
|
@counters[:exceptions] += 1
|
232
|
-
STDERR.puts "EXCEPTION: #{filename}: #{exception.message}"
|
211
|
+
STDERR.puts "EXCEPTION: #{filename.encode('utf-8', universal_newline: true)}: #{exception.message}"
|
212
|
+
end
|
213
|
+
|
214
|
+
def patch_path_string path
|
215
|
+
Gem.win_platform? ? path.gsub(/\\/, '/') : path
|
216
|
+
end
|
217
|
+
|
218
|
+
def cleanup_path path
|
219
|
+
Pathname.new(patch_path_string(path)).cleanpath
|
220
|
+
end
|
221
|
+
|
222
|
+
def ensure_dir_exists path
|
223
|
+
if File.exist?(path)
|
224
|
+
unless File.directory?(path)
|
225
|
+
raise "#{path} is not a directory"
|
226
|
+
end
|
227
|
+
else
|
228
|
+
FileUtils.mkdir_p path
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def walk_files
|
233
|
+
Dir.glob(@files_path + '**' + '*', File::FNM_DOTMATCH) do |filename|
|
234
|
+
yield filename
|
235
|
+
end
|
233
236
|
end
|
234
237
|
|
235
238
|
def get_file_digest filename
|
@@ -243,5 +246,12 @@ module FileDigests
|
|
243
246
|
end
|
244
247
|
end
|
245
248
|
|
249
|
+
def measure_time
|
250
|
+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
251
|
+
yield
|
252
|
+
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 QUIET
|
254
|
+
end
|
255
|
+
|
246
256
|
end
|
247
257
|
end
|