Class: Snoopit::FileInfo
- Inherits:
-
Object
- Object
- Snoopit::FileInfo
- Defined in:
- lib/snoopit/file_info.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#init_stat ⇒ Object
Returns the value of attribute init_stat.
-
#last_line ⇒ Object
Returns the value of attribute last_line.
-
#line_no ⇒ Object
Returns the value of attribute line_no.
-
#mtime ⇒ Object
Returns the value of attribute mtime.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #as_json ⇒ Object
- #from_hash(hash) ⇒ Object
- #from_json(json_str) ⇒ Object
- #get_last_line(file_handle) ⇒ Object
-
#initialize(file = nil) ⇒ FileInfo
constructor
A new instance of FileInfo.
- #new_file?(file_handle, stat) ⇒ Boolean
- #read_from_last?(file_handle, stat) ⇒ Boolean
- #to_json(*args) ⇒ Object
-
#updated?(file_handle) ⇒ boolean
Update file Info if the file has changed use the file handle to move the file pointer to the character where reading will start.
Constructor Details
#initialize(file = nil) ⇒ FileInfo
Returns a new instance of FileInfo.
8 9 10 11 12 13 14 15 16 |
# File 'lib/snoopit/file_info.rb', line 8 def initialize(file=nil) @file = file @line_no = 0 @offset = 0 @size = 0 @mtime = nil @last_line = nil @init_stat = true end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
6 7 8 |
# File 'lib/snoopit/file_info.rb', line 6 def file @file end |
#init_stat ⇒ Object
Returns the value of attribute init_stat.
6 7 8 |
# File 'lib/snoopit/file_info.rb', line 6 def init_stat @init_stat end |
#last_line ⇒ Object
Returns the value of attribute last_line.
6 7 8 |
# File 'lib/snoopit/file_info.rb', line 6 def last_line @last_line end |
#line_no ⇒ Object
Returns the value of attribute line_no.
6 7 8 |
# File 'lib/snoopit/file_info.rb', line 6 def line_no @line_no end |
#mtime ⇒ Object
Returns the value of attribute mtime.
6 7 8 |
# File 'lib/snoopit/file_info.rb', line 6 def mtime @mtime end |
#offset ⇒ Object
Returns the value of attribute offset.
6 7 8 |
# File 'lib/snoopit/file_info.rb', line 6 def offset @offset end |
#size ⇒ Object
Returns the value of attribute size.
6 7 8 |
# File 'lib/snoopit/file_info.rb', line 6 def size @size end |
Instance Method Details
#as_json ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/snoopit/file_info.rb', line 74 def as_json(*) { file: @file, line_no: @line_no, offset: @offset, size: @size, mtime: @mtime.iso8601, last_line: @last_line } end |
#from_hash(hash) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/snoopit/file_info.rb', line 93 def from_hash(hash) @file = hash['file'] @line_no = hash['line_no'] @offset = hash['offset'] @size = hash['size'] @mtime = Time.parse hash['mtime'] @last_line = hash['last_line'] @init_stat = false end |
#from_json(json_str) ⇒ Object
89 90 91 |
# File 'lib/snoopit/file_info.rb', line 89 def from_json(json_str) from_hash JSON.parse(json_str) end |
#get_last_line(file_handle) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/snoopit/file_info.rb', line 63 def get_last_line(file_handle) line = nil unless @last_line.nil? Snoopit.logger.debug "File point at byte: #{file_handle.tell}" file_handle.seek (-@last_line.bytesize), IO::SEEK_END Snoopit.logger.debug "Seeked to byte: #{file_handle.tell}" line = file_handle.readline end line end |
#new_file?(file_handle, stat) ⇒ Boolean
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/snoopit/file_info.rb', line 41 def new_file?(file_handle, stat) # seek to 0 Snoopit.logger.debug 'FileTracker.updated? file new read from start of file: ' + @file @offset = 0 @size = stat.size @mtime = stat.mtime @last_line = nil file_handle.seek 0, IO::SEEK_SET true end |
#read_from_last?(file_handle, stat) ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/snoopit/file_info.rb', line 52 def read_from_last?(file_handle, stat) # seek to last position + 1 old_size = @size @size = stat.size @mtime = stat.mtime Snoopit.logger.debug "File pointer at byte: #{file_handle.tell}" file_handle.seek old_size, IO::SEEK_SET Snoopit.logger.debug "Starting read from byte: #{file_handle.tell} destination byte #{old_size} new size #{@size}" true end |
#to_json(*args) ⇒ Object
85 86 87 |
# File 'lib/snoopit/file_info.rb', line 85 def to_json(*args) as_json.to_json(*args) end |
#updated?(file_handle) ⇒ boolean
Update file Info if the file has changed use the file handle to move the file pointer to the character where reading will start
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/snoopit/file_info.rb', line 22 def updated?(file_handle) c_stat = File.stat @file if (c_stat.size == @size) && (c_stat.mtime.to_i == @mtime.to_i) && (! @init_stat) Snoopit.logger.debug 'FileTracker.updated? file has not changed: ' + @file updated = false elsif c_stat.size < @size Snoopit.logger.debug 'FileTracker.updated? file size is smaller it is a new new file: ' + @file updated = new_file? file_handle, c_stat elsif (c_stat.size == @size) && (! @mtime.nil?) && (c_stat.mtime.to_i > @mtime.to_i) Snoopit.logger.debug 'FileTracker.updated? file size is same but file time is newer it is a new file: ' + @file updated = new_file? file_handle, c_stat else Snoopit.logger.debug 'FileTracker.updated? reading from last read location: ' + @file updated = read_from_last? file_handle, c_stat end @init_stat = false updated end |