classification
Title: tarfile: opening an empty tar file fails
Type: behavior Stage:
Components: Extension Modules Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: lars.gustaebel Nosy List: evanj, lars.gustaebel
Priority: normal Keywords: patch

Created on 2009-05-27 12:29 by evanj, last changed 2009-11-22 19:10 by lars.gustaebel. This issue is now closed.

Files
File name Uploaded Description Edit
tarfile-empty.diff evanj, 2009-05-27 12:29 Patch that fixes the bug for me (probably not correct)
Messages (3)
msg88416 - (view) Author: Evan Jones (evanj) Date: 2009-05-27 12:29
tarfile.open() with an empty tar archive fails with a ReadError
exception. GNU tar refuses to create empty archives, but tarfile allows
it. See the following code which reproduces the error. I used the
version of tarfile.py from subversion (revision 72458) with Python 2.5
on Linux.

Exception:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    tar = tarfile.open(fileobj=data)
  File "/home/evanj/taskmgr/tarfile.py", line 1649, in open
    raise ReadError("file could not be opened successfully")
tarfile.ReadError: file could not be opened successfully

The problem seems to be that when TarFile.next() is called, it raises
the following exception for the empty tar file:

  File "/home/evanj/taskmgr/tarfile.py", line 2310, in next
    tarinfo = self.tarinfo.fromtarfile(self)
  File "/home/evanj/taskmgr/tarfile.py", line 1235, in fromtarfile
    obj = cls.frombuf(buf)
  File "/home/evanj/taskmgr/tarfile.py", line 1190, in frombuf
    raise HeaderError("empty header")


The attached patch works for me, but no guarantees that it doesn't cause
other problems!


Sample code:

import cStringIO
import tarfile

# Create an empty tar file
data = cStringIO.StringIO()
tar = tarfile.open(mode="w", fileobj=data)
tar.close()
print "empty tar file; length:", len(data.getvalue())

# Open the tar file
data.seek(0)
tar = tarfile.open(fileobj=data)
print tar
msg88941 - (view) Author: Lars Gustäbel (lars.gustaebel) * (Python committer) Date: 2009-06-05 13:03
Thanks for the report. Empty archives are perfectly valid and tarfile
should be able to read them without error. I will take care of this
issue soon.
msg95607 - (view) Author: Lars Gustäbel (lars.gustaebel) * (Python committer) Date: 2009-11-22 19:10
I have checked in a fix for this problem: trunk (r76443) and py3k (r76444).

Thank you very much for your report. Sorry that it took that long to get
it fixed.
History
Date User Action Args
2009-11-22 19:10:32lars.gustaebelsetstatus: open -> closed
resolution: accepted
messages: + msg95607
2009-06-05 13:03:50lars.gustaebelsetassignee: lars.gustaebel

messages: + msg88941
nosy: + lars.gustaebel
2009-05-27 12:29:21evanjsettitle: tarfile: -> tarfile: opening an empty tar file fails
2009-05-27 12:29:06evanjcreate