class Fluent::NewTailInput::PositionFile

Constants

UNWATCHED_POSITION

Public Class Methods

compact(file) click to toggle source

Clean up unwatched file entries

# File lib/fluent/plugin/in_tail.rb, line 709
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
new(file, map, last_pos) click to toggle source
# File lib/fluent/plugin/in_tail.rb, line 670
def initialize(file, map, last_pos)
  @file = file
  @map = map
  @last_pos = last_pos
end
parse(file) click to toggle source
# File lib/fluent/plugin/in_tail.rb, line 691
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]
    pos = m[2].to_i(16)
    ino = m[3].to_i(16)
    seek = file.pos - line.bytesize + path.bytesize + 1
    map[path] = FilePositionEntry.new(file, seek)
  }
  new(file, map, file.pos)
end

Public Instance Methods

[](path) click to toggle source
# File lib/fluent/plugin/in_tail.rb, line 676
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