2011-03-01 04:41:32 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-02-01 19:11:34 -05:00
|
|
|
# frozen_string_literal: true
|
2008-03-31 18:40:06 -04:00
|
|
|
#++
|
2011-03-01 04:41:32 -05:00
|
|
|
# Copyright (C) 2004 Mauricio Julio Fernández Pradier
|
2008-03-31 18:40:06 -04:00
|
|
|
# See LICENSE.txt for additional licensing information.
|
|
|
|
#--
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Class for reading entries out of a tar file
|
2008-03-31 18:40:06 -04:00
|
|
|
|
|
|
|
class Gem::Package::TarReader::Entry
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Header for this tar entry
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
attr_reader :header
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Creates a new tar entry for +header+ that will be read from +io+
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def initialize(header, io)
|
|
|
|
@closed = false
|
|
|
|
@header = header
|
|
|
|
@io = io
|
|
|
|
@orig_pos = @io.pos
|
|
|
|
@read = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_closed # :nodoc:
|
|
|
|
raise IOError, "closed #{self.class}" if closed?
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Number of bytes read out of the tar entry
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def bytes_read
|
|
|
|
@read
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Closes the tar entry
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def close
|
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Is the tar entry closed?
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def closed?
|
|
|
|
@closed
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Are we at the end of the tar entry?
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def eof?
|
|
|
|
check_closed
|
|
|
|
|
|
|
|
@read >= @header.size
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Full name of the tar entry
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def full_name
|
|
|
|
if @header.prefix != "" then
|
|
|
|
File.join @header.prefix, @header.name
|
|
|
|
else
|
|
|
|
@header.name
|
|
|
|
end
|
2011-01-31 22:11:34 -05:00
|
|
|
rescue ArgumentError => e
|
|
|
|
raise unless e.message == 'string contains null byte'
|
|
|
|
raise Gem::Package::TarInvalidError,
|
|
|
|
'tar is corrupt, name contains null byte'
|
2008-03-31 18:40:06 -04:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Read one byte from the tar entry
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def getc
|
|
|
|
check_closed
|
|
|
|
|
|
|
|
return nil if @read >= @header.size
|
|
|
|
|
|
|
|
ret = @io.getc
|
|
|
|
@read += 1 if ret
|
|
|
|
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Is this tar entry a directory?
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def directory?
|
|
|
|
@header.typeflag == "5"
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Is this tar entry a file?
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def file?
|
|
|
|
@header.typeflag == "0"
|
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
##
|
|
|
|
# Is this tar entry a symlink?
|
|
|
|
|
|
|
|
def symlink?
|
|
|
|
@header.typeflag == "2"
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# The position in the tar entry
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def pos
|
|
|
|
check_closed
|
|
|
|
|
|
|
|
bytes_read
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Reads +len+ bytes from the tar file entry, or the rest of the entry if
|
|
|
|
# nil
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def read(len = nil)
|
|
|
|
check_closed
|
|
|
|
|
|
|
|
return nil if @read >= @header.size
|
|
|
|
|
|
|
|
len ||= @header.size - @read
|
|
|
|
max_read = [len, @header.size - @read].min
|
|
|
|
|
|
|
|
ret = @io.read max_read
|
|
|
|
@read += ret.size
|
|
|
|
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
alias readpartial read # :nodoc:
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Rewinds to the beginning of the tar file entry
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
def rewind
|
|
|
|
check_closed
|
|
|
|
|
|
|
|
@io.pos = @orig_pos
|
|
|
|
@read = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|