Index: tarfile.py =================================================================== --- tarfile.py (revision 46040) +++ tarfile.py (working copy) @@ -283,6 +283,9 @@ class StreamError(TarError): """Exception for unsupported operations on stream-like TarFiles.""" pass +class HeaderError(TarError): + """Exception for invalid headers.""" + pass #--------------------------- # internal stream interface @@ -822,9 +825,9 @@ """Construct a TarInfo object from a 512 byte string buffer. """ if len(buf) != BLOCKSIZE: - raise ValueError("truncated header") + raise HeaderError("truncated header") if buf.count(NUL) == BLOCKSIZE: - raise ValueError("empty header") + raise HeaderError("empty header") tarinfo = cls() tarinfo.buf = buf @@ -844,7 +847,7 @@ tarinfo.prefix = buf[345:500] if tarinfo.chksum not in calc_chksums(buf): - raise ValueError("invalid header") + raise HeaderError("invalid header") return tarinfo def tobuf(self, posix=False): @@ -1753,13 +1756,6 @@ try: tarinfo = TarInfo.frombuf(buf) - - # We shouldn't rely on this checksum, because some tar programs - # calculate it differently and it is merely validating the - # header block. We could just as well skip this part, which would - # have a slight effect on performance... - if tarinfo.chksum not in calc_chksums(buf): - self._dbg(1, "tarfile: Bad Checksum %r" % tarinfo.name) # Set the TarInfo object's offset to the current position of the # TarFile and set self.offset to the position where the data blocks @@ -1769,16 +1765,14 @@ tarinfo = self.proc_member(tarinfo) - except ValueError, e: + except HeaderError, e: if self.ignore_zeros: - self._dbg(2, "0x%X: empty or invalid block: %s" % - (self.offset, e)) + self._dbg(2, "0x%X: %s" % (self.offset, e)) self.offset += BLOCKSIZE continue else: if self.offset == 0: - raise ReadError("empty, unreadable or compressed " - "file: %s" % e) + raise ReadError(str(e)) return None break