class MogileFS::Bigfile::Filter

Filter class to wrap IO objects and uncompress DEFLATE'd files

This is used for reading “bigfile” objects generated by the (deprecated) mogtool(1)

Constants

GZIP_HEADER
INFLATABLE_TYPES

Attributes

flushed_bytes[R]

Public Class Methods

new(io, info, opts) click to toggle source
# File lib/mogilefs/bigfile/filter.rb, line 13
def initialize(io, info, opts)
  @io = io
  @info = info
  @md5 = opts[:verify] ? Digest::MD5.new : nil
  @zi = nil
  @flushed_bytes = 0
end

Public Instance Methods

flush() click to toggle source
# File lib/mogilefs/bigfile/filter.rb, line 29
def flush
  @flushed_bytes = @io.write(@zi.finish) if @zi
  @io.flush
end
md5_check!(expect) click to toggle source
# File lib/mogilefs/bigfile/filter.rb, line 21
def md5_check!(expect)
  return unless @md5
  current = @md5.hexdigest
  current == expect or
    raise MogileFS::ChecksumMismatchError, "#{current} != #{expect}"
  @md5.reset
end
write(buf) click to toggle source
# File lib/mogilefs/bigfile/filter.rb, line 34
def write(buf)
  if nil == @zi
    if @info[:compressed] &&
       INFLATABLE_TYPES.include?(@info[:type]) &&
       buf.bytesize >= 2 &&
       buf[0,2] != GZIP_HEADER

      @zi = Zlib::Inflate.new

      # mogtool(1) seems to have a bug that causes it to generate bogus
      # MD5s if zlib deflate is used.  Don't trust those MD5s for now...
      @md5 = nil
    else
      @zi = false
    end
  end
  if @zi
    buf = @zi.inflate(buf)
  else
    @md5 << buf
  end
  @io.write(buf)
end