file-digests 0.0.10 → 0.0.15

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/file-digests.rb +61 -45
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a167cb6f59c4cdae02d1a84636e81a2a2deab97f1f23f6c9a47a3ff875da554
4
- data.tar.gz: 193fb14076bfb7bc66928bbb4d1b6c4ff58c8c9ec9ba3d22bfcbda74960b9416
3
+ metadata.gz: 573cd697002f30d083625d586b4d03caf8c0f2cb763bed75e2bc9ac0a8b03612
4
+ data.tar.gz: 7cfbef9cdf7a9110c6b84b27591a498960515842675fabf6bf5512a78d7e54bc
5
5
  SHA512:
6
- metadata.gz: 20cfd65569fc3919ba8957ced4afca0572044c745a3d31f14f64872ea267599a911bb9e9f2ac203f25d1069c6bc50d4d24600a779d42f49e91a39f701eeaeee6
7
- data.tar.gz: d6cee295615ce3ae115c20fde66533e547148ba7626ebcbc78bf30e6cd5e2d5b3f391ca4068f50e79892ae695636a00c0395f63226a6d9cf5ba4de9d27bcff24
6
+ metadata.gz: 4916e667dccfb630a31fdaa74271a9c85a5f28a81f28143a4f5b406784e0e78652a29ff8e3fd9ff6507a5d90a4d9baacd2385f7de169f2c9dda6dc0f3b58866b
7
+ data.tar.gz: be5ae679122bbcffc703940b113e8d1e1057e46f7ce61ead1f52664f6b147bbd9c201d56b7faeafef29da30297cfde2e40f38c8e9400d615b9bbc6e630557259
@@ -8,32 +8,9 @@ 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
- files_path = Pathname.new patch_path_string(ARGV[0] || ".")
34
- digest_database_path = Pathname.new patch_path_string(ARGV[1]) if ARGV[1]
35
- checker = Checker.new files_path, digest_database_path
36
- checker.check
12
+ checker = Checker.new ARGV[0], ARGV[1]
13
+ checker.perform_check
37
14
  end
38
15
 
39
16
  class DigestDatabase
@@ -154,27 +131,37 @@ module FileDigests
154
131
 
155
132
  class Checker
156
133
  def initialize files_path, digest_database_path
157
- @counters = {good: 0, updated: 0, new: 0, missing: 0, renamed: 0, likely_damaged: 0, exceptions: 0}
158
- @files_path = files_path
134
+ @files_path = cleanup_path(files_path || ".")
159
135
  @prefix_to_remove = @files_path.to_s + '/'
160
136
 
161
- unless digest_database_path
162
- digest_database_path = @files_path + '.file-digests.sqlite'
137
+ raise "Files path must be a readable directory" unless (File.directory?(@files_path) && File.readable?(@files_path))
138
+
139
+ @digest_database_path = if digest_database_path
140
+ cleanup_path(digest_database_path)
141
+ else
142
+ @files_path + '.file-digests.sqlite'
143
+ end
144
+
145
+ if File.directory?(@digest_database_path)
146
+ @digest_database_path = @digest_database_path + '.file-digests.sqlite'
147
+ end
148
+
149
+ if @files_path == @digest_database_path.dirname
163
150
  @skip_file_digests_sqlite = true
164
151
  end
165
152
 
166
- FileDigests::ensure_dir_exists @files_path
167
- FileDigests::ensure_dir_exists digest_database_path.dirname
153
+ ensure_dir_exists @digest_database_path.dirname
168
154
 
169
- if File.exist?(digest_database_path.dirname + '.file-digests.sha512')
155
+ if File.exist?(@digest_database_path.dirname + '.file-digests.sha512')
170
156
  @use_sha512 = true
171
157
  end
172
158
 
173
- @digest_database = DigestDatabase.new digest_database_path
159
+ @digest_database = DigestDatabase.new @digest_database_path
160
+ @counters = {good: 0, updated: 0, new: 0, missing: 0, renamed: 0, likely_damaged: 0, exceptions: 0}
174
161
  end
175
162
 
176
- def check
177
- FileDigests::measure_time do
163
+ def perform_check
164
+ measure_time do
178
165
  walk_files do |filename|
179
166
  process_file filename
180
167
  end
@@ -189,11 +176,7 @@ module FileDigests
189
176
  puts @counters.inspect
190
177
  end
191
178
 
192
- def walk_files
193
- Dir.glob(@files_path + '**' + '*', File::FNM_DOTMATCH) do |filename|
194
- yield filename
195
- end
196
- end
179
+ private
197
180
 
198
181
  def process_file filename
199
182
  return if File.symlink? filename
@@ -210,20 +193,46 @@ module FileDigests
210
193
  return if stat.socket?
211
194
 
212
195
  if @skip_file_digests_sqlite
213
- return if filename == '.file-digests.sqlite'
214
- return if filename == '.file-digests.sqlite-wal'
215
- return if filename == '.file-digests.sqlite-shm'
196
+ basename = File.basename(filename)
197
+ return if basename == '.file-digests.sha512'
198
+ return if basename == '.file-digests.sqlite'
199
+ return if basename == '.file-digests.sqlite-wal'
200
+ return if basename == '.file-digests.sqlite-shm'
216
201
  end
217
202
 
218
203
  @digest_database.insert_or_update(
219
- 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),
220
205
  stat.mtime.utc.strftime('%Y-%m-%d %H:%M:%S'),
221
206
  get_file_digest(filename),
222
207
  @counters
223
208
  )
224
209
  rescue => exception
225
210
  @counters[:exceptions] += 1
226
- 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(digest_database_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
227
236
  end
228
237
 
229
238
  def get_file_digest filename
@@ -237,5 +246,12 @@ module FileDigests
237
246
  end
238
247
  end
239
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
+
240
256
  end
241
257
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file-digests
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stanislav Senotrusov