Handle invalid tar layers and add tests

This commit is contained in:
Joseph Schorr 2014-11-10 18:26:37 -05:00
parent cc9d5798b3
commit 611bc895e1
2 changed files with 55 additions and 0 deletions

View file

@ -1,6 +1,11 @@
import os
import tarfile
class TarLayerReadException(Exception):
""" Exception raised when reading a layer has failed. """
pass
class TarLayerFormat(object):
""" Class which creates a generator of the combined TAR data. """
def __init__(self, tar_iterator):
@ -10,9 +15,14 @@ class TarLayerFormat(object):
for current_tar in self.tar_iterator():
# Read the current TAR. If it is empty, we just continue
# to the next one.
tar_file = None
try:
tar_file = tarfile.open(mode='r|*', fileobj=current_tar)
except tarfile.ReadError as re:
if re.message != 'empty file':
raise TarLayerReadException('Could not read layer')
if not tar_file:
continue
# For each of the tar entries, yield them IF and ONLY IF we have not