Class: Fluent::Plugin::TailInput::PositionFile

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/in_tail.rb

Constant Summary collapse

UNWATCHED_POSITION =
0xffffffffffffffff

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, map, last_pos) ⇒ PositionFile

Returns a new instance of PositionFile.



784
785
786
787
788
# File 'lib/fluent/plugin/in_tail.rb', line 784

def initialize(file, map, last_pos)
  @file = file
  @map = map
  @last_pos = last_pos
end

Class Method Details

.compact(file) ⇒ Object

Clean up unwatched file entries



821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/fluent/plugin/in_tail.rb', line 821

def self.compact(file)
  file.pos = 0
  existent_entries = file.each_line.map { |line|
    m = /^([^\t]+)\t([0-9a-fA-F]+)\t([0-9a-fA-F]+)/.match(line)
    next unless m
    path = m[1]
    pos = m[2].to_i(16)
    ino = m[3].to_i(16)
    # 32bit inode converted to 64bit at this phase
    pos == UNWATCHED_POSITION ? nil : ("%s\t%016x\t%016x\n" % [path, pos, ino])
  }.compact

  file.pos = 0
  file.truncate(0)
  file.write(existent_entries.join)
end

.parse(file) ⇒ Object



805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/fluent/plugin/in_tail.rb', line 805

def self.parse(file)
  compact(file)

  map = {}
  file.pos = 0
  file.each_line {|line|
    m = /^([^\t]+)\t([0-9a-fA-F]+)\t([0-9a-fA-F]+)/.match(line)
    next unless m
    path = m[1]
    seek = file.pos - line.bytesize + path.bytesize + 1
    map[path] = FilePositionEntry.new(file, seek)
  }
  new(file, map, file.pos)
end

Instance Method Details

#[](path) ⇒ Object



790
791
792
793
794
795
796
797
798
799
800
801
802
803
# File 'lib/fluent/plugin/in_tail.rb', line 790

def [](path)
  if m = @map[path]
    return m
  end

  @file.pos = @last_pos
  @file.write path
  @file.write "\t"
  seek = @file.pos
  @file.write "0000000000000000\t0000000000000000\n"
  @last_pos = @file.pos

  @map[path] = FilePositionEntry.new(@file, seek)
end