@@ -18,34 +18,40 @@ def handle_specifically(watched_file)
1818 # fast forward through the lines until we reach unseen content?
1919 # meaning that we can quit in the middle of a zip file
2020 key = watched_file . sincedb_key
21- begin
22- file_stream = FileInputStream . new ( watched_file . path )
23- gzip_stream = GZIPInputStream . new ( file_stream )
24- decoder = InputStreamReader . new ( gzip_stream , "UTF-8" )
25- buffered = BufferedReader . new ( decoder )
26- while ( line = buffered . readLine ( false ) )
27- watched_file . listener . accept ( line )
28- # can't quit, if we did then we would incorrectly write a 'completed' sincedb entry
29- # what do we do about quit when we have just begun reading the zipped file (e.g. pipeline reloading)
30- # should we track lines read in the sincedb and
31- # fast forward through the lines until we reach unseen content?
32- # meaning that we can quit in the middle of a zip file
33- end
34- watched_file . listener . eof
35- rescue ZipException => e
36- logger . error ( "Cannot decompress the gzip file at path: #{ watched_file . path } " )
37- watched_file . listener . error
38- else
39- sincedb_collection . store_last_read ( key , watched_file . last_stat_size )
40- sincedb_collection . request_disk_flush
41- watched_file . listener . deleted
21+
22+ if @settings . check_archive_validity && corrupted? ( watched_file )
4223 watched_file . unwatch
43- ensure
44- # rescue each close individually so all close attempts are tried
45- close_and_ignore_ioexception ( buffered ) unless buffered . nil?
46- close_and_ignore_ioexception ( decoder ) unless decoder . nil?
47- close_and_ignore_ioexception ( gzip_stream ) unless gzip_stream . nil?
48- close_and_ignore_ioexception ( file_stream ) unless file_stream . nil?
24+ else
25+ begin
26+ file_stream = FileInputStream . new ( watched_file . path )
27+ gzip_stream = GZIPInputStream . new ( file_stream )
28+ decoder = InputStreamReader . new ( gzip_stream , "UTF-8" )
29+ buffered = BufferedReader . new ( decoder )
30+ while ( line = buffered . readLine ( false ) )
31+ watched_file . listener . accept ( line )
32+ # can't quit, if we did then we would incorrectly write a 'completed' sincedb entry
33+ # what do we do about quit when we have just begun reading the zipped file (e.g. pipeline reloading)
34+ # should we track lines read in the sincedb and
35+ # fast forward through the lines until we reach unseen content?
36+ # meaning that we can quit in the middle of a zip file
37+ end
38+ watched_file . listener . eof
39+ rescue ZipException => e
40+ logger . error ( "Cannot decompress the gzip file at path: #{ watched_file . path } " , :exception => e . class ,
41+ :message => e . message , :backtrace => e . backtrace )
42+ watched_file . listener . error
43+ else
44+ sincedb_collection . store_last_read ( key , watched_file . last_stat_size )
45+ sincedb_collection . request_disk_flush
46+ watched_file . listener . deleted
47+ watched_file . unwatch
48+ ensure
49+ # rescue each close individually so all close attempts are tried
50+ close_and_ignore_ioexception ( buffered ) unless buffered . nil?
51+ close_and_ignore_ioexception ( decoder ) unless decoder . nil?
52+ close_and_ignore_ioexception ( gzip_stream ) unless gzip_stream . nil?
53+ close_and_ignore_ioexception ( file_stream ) unless file_stream . nil?
54+ end
4955 end
5056 sincedb_collection . clear_watched_file ( key )
5157 end
@@ -56,7 +62,28 @@ def close_and_ignore_ioexception(closeable)
5662 begin
5763 closeable . close
5864 rescue Exception => e # IOException can be thrown by any of the Java classes that implement the Closable interface.
59- logger . warn ( "Ignoring an IOException when closing an instance of #{ closeable . class . name } " , "exception" => e )
65+ logger . warn ( "Ignoring an IOException when closing an instance of #{ closeable . class . name } " ,
66+ :exception => e . class , :message => e . message , :backtrace => e . backtrace )
67+ end
68+ end
69+
70+ def corrupted? ( watched_file )
71+ begin
72+ file_stream = FileInputStream . new ( watched_file . path )
73+ gzip_stream = GZIPInputStream . new ( file_stream )
74+ buffer = Java ::byte [ 8192 ] . new
75+ start = Time . new
76+ until gzip_stream . read ( buffer ) == -1
77+ end
78+ return false
79+ rescue ZipException => e
80+ duration = Time . now - start
81+ logger . warn ( "Detected corrupted archive #{ watched_file . path } file won't be processed" , :message => e . message ,
82+ :duration => duration . round ( 3 ) )
83+ return true
84+ ensure
85+ close_and_ignore_ioexception ( gzip_stream ) unless gzip_stream . nil?
86+ close_and_ignore_ioexception ( file_stream ) unless file_stream . nil?
6087 end
6188 end
6289 end
0 commit comments