Class: Snoopit::FileInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/snoopit/file_info.rb

Overview

Holds and manages the file read information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ FileInfo

Returns a new instance of FileInfo.



10
11
12
13
14
15
16
17
18
# File 'lib/snoopit/file_info.rb', line 10

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

#fileObject

Returns the value of attribute file.



8
9
10
# File 'lib/snoopit/file_info.rb', line 8

def file
  @file
end

#init_statObject

Returns the value of attribute init_stat.



8
9
10
# File 'lib/snoopit/file_info.rb', line 8

def init_stat
  @init_stat
end

#last_lineObject

Returns the value of attribute last_line.



8
9
10
# File 'lib/snoopit/file_info.rb', line 8

def last_line
  @last_line
end

#line_noObject

Returns the value of attribute line_no.



8
9
10
# File 'lib/snoopit/file_info.rb', line 8

def line_no
  @line_no
end

#mtimeObject

Returns the value of attribute mtime.



8
9
10
# File 'lib/snoopit/file_info.rb', line 8

def mtime
  @mtime
end

#offsetObject

Returns the value of attribute offset.



8
9
10
# File 'lib/snoopit/file_info.rb', line 8

def offset
  @offset
end

#sizeObject

Returns the value of attribute size.



8
9
10
# File 'lib/snoopit/file_info.rb', line 8

def size
  @size
end

Instance Method Details

#as_jsonObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/snoopit/file_info.rb', line 80

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



99
100
101
102
103
104
105
106
107
# File 'lib/snoopit/file_info.rb', line 99

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



95
96
97
# File 'lib/snoopit/file_info.rb', line 95

def from_json(json_str)
  from_hash JSON.parse(json_str)
end

#get_last_line(file_handle) ⇒ Object

Get the last line of the file



69
70
71
72
73
74
75
76
77
78
# File 'lib/snoopit/file_info.rb', line 69

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

This is a new file reset or start reading from the beginning

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
# File 'lib/snoopit/file_info.rb', line 45

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

Continue reading from last location, which maybe on new file on initialization

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
# File 'lib/snoopit/file_info.rb', line 57

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



91
92
93
# File 'lib/snoopit/file_info.rb', line 91

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

Parameters:

  • file_handle (File)

Returns:

  • (boolean)

    true if updated false not updated



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/snoopit/file_info.rb', line 25

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