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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db5f9c1faeeed556960cd8c1d6a448d0b860ff5145eefaa04890847222c04b87
4
- data.tar.gz: b1858913665bd3ce971fd53fe605c5f5e83c3f54310cff0b57f9b4b08f3fddce
3
+ metadata.gz: 27ef5c07b544bb7e63a8b9ca9d8b99d0b46588b45311ef940d7431691178b99c
4
+ data.tar.gz: b1febf1fbdabab014eca65a86e3beee4cfaf4478ead18a5da071b045ae0ab56a
5
5
  SHA512:
6
- metadata.gz: 303c02c2d5374c4438829dbabb3ac479e77d20f3f0051d86c7b0ecb01ea0ff1cb50db40b692c9749c5a7da5d77dc54012881df92d309bb12700a57c232a50c32
7
- data.tar.gz: 78fc7896d1090778d4a0fea0642c22208a8de1bf89506a3ca5e5efc7949b3bb2b2e528a2b0e4a070ea6c66d66cee3a6b6c9850664225e6c303b0e8abdc273591
6
+ metadata.gz: f4d0db5b5ca2a29adaac1fa9aaefd276977f39d0c6e7e17e5432586d63a91a5bb20bd4af8c955e4e5c625460102ae1431b33947e5c40cc9351c5bb858f28974b
7
+ data.tar.gz: 3dc1ec4ac2224a84d1cc914b81314ccfba5580a11ca179ac9b4ca9201a599f57a52edb997af8adc369861124a733d1030f5dcb2a234dbf3c10a68b03690a5b3a
@@ -1,8 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- QUIET = (ENV["QUIET"] == "true")
4
- TEST_ONLY = (ENV["TEST_ONLY"] == "true")
5
-
6
3
  require 'file-digests'
7
4
 
8
5
  FileDigests.perform_check
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- QUIET = (ENV["QUIET"] == "true")
4
- TEST_ONLY = true
3
+ ENV["TEST_ONLY"] = "true"
5
4
 
6
5
  require 'file-digests'
7
6
 
@@ -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
- checker = Checker.new ARGV[0], ARGV[1]
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 QUIET
59
- unless TEST_ONLY
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 QUIET
73
- unless TEST_ONLY
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 QUIET
81
- unless TEST_ONLY
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 process_missing_files counters
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 TEST_ONLY
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
- if (counters[:missing] = @missing_files.length) > 0
100
- puts "\nMISSING FILES:"
101
- @missing_files.sort.to_h.each do |filename, digest|
102
- puts filename
103
- end
104
- unless TEST_ONLY
105
- puts "Remove missing files from the database (y/n)?"
106
- if STDIN.gets.strip.downcase == "y"
107
- @db.transaction do
108
- @missing_files.each do |filename, digest|
109
- delete_by_filename filename
110
- end
111
- end
112
- end
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.process_missing_files @counters
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 QUIET
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
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.16
4
+ version: 0.0.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stanislav Senotrusov