ckfiles 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 17f9e5e46d09082ee214c6a6960723fb488ece8a
4
+ data.tar.gz: 26f5b9ca6eb40eac984baabf730b92666587f3f0
5
+ SHA512:
6
+ metadata.gz: fdc62bcd94914e0edb30ea507e3ca47bcdf09b39a5ab536551a165b6d212fe418443ed1734c730beb5366182044a7e372a11519c9ede3e24fea9291aa96c1eee
7
+ data.tar.gz: 7e7fb47bbc7e42ab80f6d55255bb93e3ab4ff72a23e3da59fd85c62e8ffa8b68150ff46a530427bd3beca3dfbb6940499814595ba525a23921c12c9fdac3f774
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Mac Ma
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ ckfiles
2
+ ==========================
3
+ General file integrity checker, can check recursively. Support SFV, MD5, ZIP, CBZ, 7Z, GZ, BZ2, LHA, LZH, ARJ, CHM, XZ, RAR, CBR and media file containing CRC32 sum in filename with extension with AVI, MKV, MP4, OGM, ASF, RM, RAM, WEBM. Require 7zip and Unrar for checking archive file. Supports Linux / Mac OS X / Windows.
4
+
5
+ Example filename: Big Bunny [c001beef].avi
6
+
7
+ #### Installation: ####
8
+ Linux:
9
+ 1. sudo apt-get install ruby1.9.1-full ruby1.9.1-dev p7zip-full p7zip-rar
10
+ 2. sudo gem install ckfiles
11
+
12
+ Mac OS X:
13
+ 1. Install [MacPorts](http://www.macports.org)
14
+ 2. sudo port install ruby20 rb-rubygems p7zip
15
+ 3. sudo gem install ckfiles
16
+
17
+ Windows: (NTFS Only)
18
+ 1. Install Ruby v2.* from [RubyInstaller](http://rubyinstaller.org)
19
+ 2. Install Development Kit for v2.0 from same site as above
20
+ 2. gem install ckfiles
21
+ 3. Download and install 7z from [7-Zip](http://www.7-zip.org), copy 7z.exe and 7z.dll to bin path (e.g. C:\Ruby200\bin )
22
+
23
+ All OS:
24
+ Download and install [WinRAR/unrar](http://rarlab.com), copy unrar(.exe) to bin path
25
+
26
+ #### Caveat/Notes: ####
27
+ This program uses filesystem's extended attributes, if the filesystem doesn't support the extended attribute or isn't enabled, then you can only use the read only function. This also applies to any network drive.
28
+
29
+ #### Usage: ####
30
+ ckfiles \[arguments\] \[directory\] \<directory2\> \<directory3\>
31
+
32
+ #### Arguments: ####
33
+ --recursive -r Recursively check all files
34
+ --noop -n Not moving corrupted files
35
+ --read-only -x Read Only, do not write attribute (imply -n)
36
+ --expiry -e Expiry time since last check, can be in seconds, or m/h/d/w/mo/y
37
+ --outfile -o Output error to a file
38
+ --help -h Display current help message
@@ -0,0 +1,319 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ =begin
5
+ #
6
+ # General file integrity checker, can check recursively. Support SFV, MD5, ZIP, CBZ, 7Z, GZ, BZ2, LHA, LZH, ARJ, CHM, XZ, RAR, CBR and media file containing CRC32 sum in filename with extension with AVI, MKV, MP4, OGM, ASF, RM, RAM, WEBM. Require 7zip and Unrar for checking archive file. Supports Linux / Mac OS X / Windows.
7
+ #
8
+
9
+
10
+ # attribute format
11
+ v1
12
+ ckfiles.result attribute version, unix epoxy time
13
+ example
14
+ ckfiles.result 1, 1385724368
15
+ =end
16
+
17
+ # making sure program runs in UTF-8 encoding in any environment
18
+ Encoding.default_external = Encoding::UTF_8
19
+ Encoding.default_internal = Encoding::UTF_8
20
+
21
+
22
+ # make print behave as print
23
+ STDOUT.sync = true
24
+
25
+ # executable program sanity check
26
+ if `7z -h` =~ /Igor Pavlov/
27
+ $HAVE_BIN_7Z = true
28
+ else
29
+ $HAVE_BIN_7Z = false
30
+ puts "***** NOTICE: 7z not installed *****"
31
+ end
32
+
33
+ if `unrar -v` =~ /Alexander Roshal/
34
+ $HAVE_BIN_UNRAR = true
35
+ else
36
+ $HAVE_BIN_UNRAR = false
37
+ puts "***** NOTICE: unrar not installed *****"
38
+ end
39
+
40
+
41
+ require 'ffi-xattr'
42
+ require 'fileutils'
43
+ require 'ckfiles/libs'
44
+ require 'digest/md5'
45
+ require 'getoptlong'
46
+
47
+ if RUBY_PLATFORM =~ /mingw/
48
+ # Windows support
49
+ # using Windows API to directly list files and directory
50
+ # otherwise unicode filenames will be be supported, it will all be in ?????? characters (hex falue 3F)
51
+
52
+ # ensure command prompt runs in unicode
53
+ `chcp 65001`
54
+
55
+ co = `chcp`.scan(/[0-9]+/).join.to_i
56
+ if co != 65001
57
+ puts 'Fail to run in unicode mode!'
58
+ exit
59
+ end
60
+
61
+ require 'win32ole'
62
+ $fso = WIN32OLE.new('Scripting.FileSystemObject')
63
+ end
64
+
65
+
66
+ # start up environment
67
+ $noop = false
68
+ $read_only = false
69
+ $expiry = 3600*24*90 # expiry time default to 90 days
70
+ $recursive = false
71
+ $outfile = false
72
+
73
+ def tpass( f )
74
+ if $read_only == false
75
+ begin
76
+ fd = Xattr.new( f )
77
+
78
+ param = 'ckfiles.result'
79
+ param = 'user.' + param if RUBY_PLATFORM =~ /linux/
80
+ fd.set(param, "1, #{Time.now.to_i.to_s}")
81
+
82
+ puts "pass - #{ f }"
83
+ rescue => errmsg
84
+ puts "pass - set attr failed: #{ f }"
85
+ end
86
+ else
87
+ puts "pass - #{ f }"
88
+ end
89
+ end
90
+
91
+ def tfail( f )
92
+ s = "fail - #{ f }"
93
+ puts s
94
+ File.open($outfile, 'ab') { |io| io << s + "\r\n" } if $outfile # write failed result to file
95
+
96
+ if $noop == false and $read_only == false
97
+ newdir = File.dirname(f) + '/Corrupted'
98
+ Dir.mkdir( newdir ) if ! File.directory?( newdir )
99
+ File.rename( f , "#{ newdir }/#{ File.basename(f) }" )
100
+ end
101
+ end
102
+
103
+ # is file already passed?
104
+ def File.passed?( file, expiry=$expiry )
105
+ fd = Xattr.new( file )
106
+ param = 'ckfiles.result'
107
+ param = 'user.' + param if RUBY_PLATFORM =~ /linux/
108
+ begin
109
+ iresult = fd.get(param).split(', ')
110
+ iver = iresult[0]
111
+ itime = iresult[1].to_i
112
+
113
+ # file already passed, skip
114
+ if ( Time.now.to_i - itime ) < expiry
115
+ return true
116
+ end
117
+ rescue
118
+ # attribute not found, so continue
119
+ end
120
+ return false
121
+ end
122
+
123
+
124
+ def show_help
125
+ puts \
126
+ %Q{Usage:
127
+ ckfiles [arguments] [dir] <dir2> <dir3> ...
128
+
129
+ Arguments:
130
+ --recursive -r Recursively check all files
131
+ --noop -n Not moving corrupted files
132
+ --read-only -x Read Only, do not write attribute (imply -n)
133
+ --expiry -e Expiry time since last check, can be in seconds, or m/h/d/w/mo/y
134
+ --outfile -o Output error to a file
135
+ --help -h Display current help message}
136
+ end
137
+
138
+
139
+ #<main>
140
+ opts = GetoptLong.new(
141
+ [ '--recursive', '-r', GetoptLong::NO_ARGUMENT ],
142
+ [ '--noop', '-n', GetoptLong::NO_ARGUMENT ],
143
+ [ '--read-only', '-x', GetoptLong::NO_ARGUMENT ],
144
+ [ '--expiry', '-e', GetoptLong::REQUIRED_ARGUMENT ],
145
+ [ '--outfile', '-o', GetoptLong::REQUIRED_ARGUMENT ],
146
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
147
+ )
148
+
149
+ opts.each { |opt,arg|
150
+ case opt
151
+ when '--help'
152
+ show_help
153
+ exit
154
+ when '--recursive'
155
+ $recursive = true
156
+ when '--noop'
157
+ $noop = true
158
+ when '--read-only'
159
+ $noop = true
160
+ $read_only = true
161
+ when '--expiry'
162
+ $expiry = arg.timelength_to_i
163
+ when '--outfile'
164
+ $outfile = arg
165
+ end
166
+ }
167
+
168
+
169
+ dirs = []
170
+ if ARGV.length > 0
171
+ for dir in ARGV
172
+ if File.directory?( dir )
173
+ dirs << File.expand_path( dir )
174
+ else
175
+ puts "dir don't exists #{dir}"
176
+ end
177
+ end
178
+ else
179
+ show_help
180
+ exit
181
+ end
182
+
183
+
184
+ def chkDir(dir)
185
+ files = []
186
+
187
+ if RUBY_PLATFORM =~ /mingw/
188
+ # Windows API call for listing files and folders
189
+
190
+ gf = $fso.GetFolder(dir)
191
+ for file in gf.Files
192
+ files << file.path
193
+ end
194
+
195
+ for folder in gf.SubFolders
196
+ files << folder.path
197
+ end
198
+ else
199
+ # POSIX supported file/directory listing
200
+
201
+ files = Dir.entries(dir)
202
+ files.delete_if { |f| f[0] == '.' and f[-1] == '.' }
203
+ files.collect! { |f| dir + '/' + f }
204
+ end
205
+
206
+ for f in files
207
+ # skip restricted file/directory
208
+ next if Dir.restricted?( f )
209
+
210
+ if File.file?( f )
211
+ # skip unwanted files or non-regular file
212
+ next if File.basename( f ) =~ /^\._.+/
213
+ next if File.basename( f ) =~ /^\..+$/
214
+ next if File.exists?( f + '.lftp-pget-status' )
215
+
216
+ # skip already passed file
217
+ next if File.passed?( f )
218
+
219
+
220
+ if f =~ /\.(zip|cbz|7z|gz|bz2|lha|lzh|arj|chm|xz)$/i
221
+ # skip if 7z executable isn't found
222
+ next unless $HAVE_BIN_7Z
223
+
224
+ # to go around "invalid byte sequence in UTF-8" error
225
+ result = `7z t -pAAA "#{f}" 2>&1`.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
226
+
227
+ if result =~ /Everything is Ok/
228
+ tpass( f )
229
+ else
230
+ tfail( f )
231
+ end
232
+
233
+ elsif f =~ /\.(rar|cbr)$/i
234
+ # skip if unrar executable isn't found
235
+ next unless $HAVE_BIN_UNRAR
236
+
237
+ if f =~ /\.part[0-9]+\.rar$/i
238
+ # skip file if the part rar file isn't the first one
239
+ next if (f =~ /\.part[0]+1\.rar$/i) == nil
240
+ end
241
+
242
+ if `unrar t -p- "#{f}" 2>&1` =~ /All OK/
243
+ tpass( f )
244
+ else
245
+ tfail( f )
246
+ end
247
+
248
+ elsif f =~ /(\[|\()([0-9a-fA-F]{8})(\]|\))\.(avi|mkv|mp4|ogm|asf|rm|ram|webm)$/i
249
+ # check media file containing crc32 sum at end of filename
250
+
251
+ fcrc = File.crc32(f)
252
+
253
+ if f.downcase.include?(fcrc)
254
+ tpass( f )
255
+ else
256
+ tfail( f )
257
+ end
258
+
259
+ elsif f =~ /\.sfv$/i
260
+ # check files contained in sfv
261
+
262
+ ck_files = load_cksum_list( 'sfv', f )
263
+
264
+ for file, sum in ck_files
265
+ file = File.dirname( f ) + '/' + file
266
+
267
+ if ! File.file?( file )
268
+ puts "SFV: missing #{file}"
269
+ next
270
+ end
271
+
272
+ next if File.passed?( file )
273
+
274
+ fcrc = File.crc32(file)
275
+
276
+ if sum.downcase == fcrc.downcase
277
+ tpass( file )
278
+ else
279
+ tfail( file )
280
+ end
281
+ end
282
+ elsif f =~ /\.md5$/i
283
+ ck_files = load_cksum_list( 'md5', f )
284
+
285
+ for file, sum in ck_files
286
+ next unless file and sum
287
+
288
+ file = File.dirname( f ) + '/' + file
289
+
290
+ if ! File.file?( file )
291
+ puts "MD5: missing #{file}"
292
+ next
293
+ end
294
+
295
+ next if File.passed?( file )
296
+
297
+ fmd5 = File.md5( file )
298
+
299
+ if sum.downcase == fmd5.downcase
300
+ tpass( file )
301
+ else
302
+ tfail( file )
303
+ end
304
+ end
305
+ end
306
+
307
+ elsif File.directory?( f ) and $recursive
308
+ chkDir(f)
309
+ end
310
+ end
311
+ end
312
+
313
+
314
+ for dir in dirs
315
+ chkDir(dir)
316
+ end
317
+
318
+ puts 'all done'
319
+ #</main>
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/ckfiles/version', __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'ckfiles'
7
+ s.version = Ckfiles::VERSION
8
+
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.description = "General file integrity checker, can check recursively. Support SFV, MD5, ZIP, CBZ, 7Z, GZ, BZ2, LHA, LZH, ARJ, CHM, XZ, RAR, CBR and media file containing CRC32 sum in filename with extension with AVI, MKV, MP4, OGM, ASF, RM, RAM, WEBM. Require 7zip and Unrar for checking archive file. Supports Linux / Mac OS X / Windows."
11
+ s.summary = s.description
12
+ s.authors = ["Mac Ma"]
13
+ s.homepage = 'https://github.org/comomac/ckfiles'
14
+ s.license = 'MIT'
15
+
16
+ s.add_dependency 'ffi-xattr', '>= 0.0.5'
17
+
18
+ s.require_paths = ["lib"]
19
+ s.files = `git ls-files`.split($\)
20
+ s.executables = s.files.grep(%r{^bin/}).map { |f| File.basename(f) }
21
+ end
@@ -0,0 +1,129 @@
1
+ require 'zlib'
2
+
3
+ # check if directed is restricted
4
+ def Dir.restricted?( dir, excludes=[] )
5
+ rdirs = [
6
+ /\.Trash/, /\.Trashes/, /\.fseventsd/, /\.Spotlight-V100/, /\.DocumentRevisions-V100/,
7
+ /\.git/,
8
+ /\/\./,
9
+ /\.\$EXTEND/,
10
+ /_SYNCAPP/,
11
+ /Corrupted/,
12
+ /System Volume Information/, /RECYCLER/,
13
+ /backup/i,
14
+ /\/Library\//,
15
+ /\.sparsebundle/,
16
+ /\.tmpdir/, /\.tmp7z/,
17
+ /\.AppleDouble/
18
+ ]
19
+
20
+ for word in rdirs - excludes
21
+ return true if dir.match( word )
22
+ end
23
+ return false
24
+ end
25
+
26
+ def load_cksum_list( sumtype, file )
27
+ # returns { file1=>sum, file2=>sum, file3=>sum ... fileN=>sum }
28
+
29
+ if sumtype == 'sfv'
30
+ file_ext = 'sfv'
31
+ elsif sumtype == 'md5'
32
+ file_ext = 'md5'
33
+ else
34
+ raise 'not supported cksum type'
35
+ end
36
+
37
+ sfs = File.new( file ).readlines
38
+ sfs.collect! { |y| y.chomp }
39
+ begin
40
+ sfs = sfs.collect { |y| y.gsub(/\\/,'/') }
41
+ rescue => errmsg
42
+ puts "ERROR: #{errmsg} FILE: #{file}"
43
+ end
44
+ sfs.flatten!
45
+ sfs.compact!
46
+ sfs.delete_if { |y| y == '' }
47
+ sfs.delete_if { |y| y[0..0] == '#' }
48
+ sfs.delete_if { |y| y[0..0] == ';' }
49
+ sfs.delete_if { |y| y[0..4] == 'MD5 (' } #this format is a pain so drop
50
+
51
+ n = {}
52
+ if sumtype == 'sfv'
53
+ for s in sfs
54
+ n[s[0..-10]] = s[-8..-1]
55
+ end
56
+ elsif sumtype == 'md5'
57
+ for s in sfs
58
+ n[s[34..-1]] = s[0..31]
59
+ end
60
+ end
61
+ n.each { |f,sum| puts "#{sum} #{f}" } if $debug
62
+
63
+ if n.length > 0
64
+ n
65
+ else
66
+ {}
67
+ end
68
+ end
69
+
70
+ # return crc32 sum for file
71
+ def File.crc32(f)
72
+ ptr_f = File.new(f,'rb')
73
+
74
+ r = 0
75
+ while ! ptr_f.eof?
76
+ r = Zlib.crc32( ptr_f.read(1024**2) , r )
77
+ end
78
+ ptr_f.close
79
+
80
+ r.to_s(16).rjust(8).gsub(' ','0').downcase
81
+ end
82
+
83
+ # return md5sum for file
84
+ def File.md5(f)
85
+ Digest::MD5.file(f).hexdigest
86
+ end
87
+
88
+ class String
89
+ # translate length of time to integer format
90
+ # e.g. 3m -> 180 (seconds)
91
+ def timelength_to_i
92
+ units = {
93
+ 'm'=>60,
94
+ 'h'=>60*60,
95
+ 'd'=>60*60*24,
96
+ 'w'=>60*60*24*7,
97
+ 'mo'=>60*60*24*30,
98
+ 'y'=>60*60*24*365
99
+ }
100
+
101
+ u = 1
102
+ n = self.to_f
103
+ for unit, num in units
104
+ if self =~ /#{unit}$/i
105
+ u = num
106
+ break
107
+ end
108
+ end
109
+
110
+ return (u*n).to_i
111
+ end
112
+
113
+ def escape_glob
114
+ # escape any character that will affect Dir.glob
115
+ self.gsub(/([\[\]\{\}\*\?\\])/, '\\\\\1')
116
+ end
117
+
118
+ def ascii
119
+ # return only ascii characters in array
120
+ regex = /[A-Za-z0-9\ \%\;\'\@\~\-\(\)\[\]\&\_\{\}\+\.\/\!\,\#]/
121
+ self.scan(regex)
122
+ end
123
+
124
+ def not_ascii
125
+ # return only none ascii characters in array
126
+ regex = /[^A-Za-z0-9\ \%\;\'\@\~\-\(\)\[\]\&\_\{\}\+\.\/\!\,\#]/
127
+ self.scan(regex)
128
+ end
129
+ end
@@ -0,0 +1,3 @@
1
+ class Ckfiles
2
+ VERSION = "0.9.3"
3
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ckfiles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.3
5
+ platform: ruby
6
+ authors:
7
+ - Mac Ma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi-xattr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.5
27
+ description: General file integrity checker, can check recursively. Support SFV, MD5,
28
+ ZIP, CBZ, 7Z, GZ, BZ2, LHA, LZH, ARJ, CHM, XZ, RAR, CBR and media file containing
29
+ CRC32 sum in filename with extension with AVI, MKV, MP4, OGM, ASF, RM, RAM, WEBM.
30
+ Require 7zip and Unrar for checking archive file. Supports Linux / Mac OS X / Windows.
31
+ email:
32
+ executables:
33
+ - ckfiles
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - LICENSE
38
+ - README.md
39
+ - bin/ckfiles
40
+ - ckfiles.gemspec
41
+ - lib/ckfiles/libs.rb
42
+ - lib/ckfiles/version.rb
43
+ homepage: https://github.org/comomac/ckfiles
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.6.12
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: General file integrity checker, can check recursively. Support SFV, MD5,
67
+ ZIP, CBZ, 7Z, GZ, BZ2, LHA, LZH, ARJ, CHM, XZ, RAR, CBR and media file containing
68
+ CRC32 sum in filename with extension with AVI, MKV, MP4, OGM, ASF, RM, RAM, WEBM.
69
+ Require 7zip and Unrar for checking archive file. Supports Linux / Mac OS X / Windows.
70
+ test_files: []