0

fixed error handling code. Now more robust against corrupt chunks!

This commit is contained in:
Andrew Brown
2012-03-18 13:52:59 -04:00
parent e9cf747513
commit ff0f4fe46e
3 changed files with 22 additions and 19 deletions

View File

@@ -127,7 +127,8 @@ class LRUCache(object):
def __delitem__(self, key): def __delitem__(self, key):
# Used to flush the cache of this key # Used to flush the cache of this key
link = self.cache[key] cache = self.cache
link = cache[key]
del cache[key] del cache[key]
link.left.right = link.right link.left.right = link.right
link.right.left = link.left link.right.left = link.left

View File

@@ -42,7 +42,17 @@ def load_region(fileobj):
for accessing the chunks inside.""" for accessing the chunks inside."""
return MCRFileReader(fileobj) return MCRFileReader(fileobj)
class CorruptNBTError(Exception):
class CorruptionError(Exception):
pass
class CorruptRegionError(CorruptionError):
"""An exception raised when the MCRFileReader class encounters an
error during region file parsing.
"""
pass
class CorruptChunkError(CorruptionError):
pass
class CorruptNBTError(CorruptionError):
"""An exception raised when the NBTFileReader class encounters """An exception raised when the NBTFileReader class encounters
something unexpected in an NBT file.""" something unexpected in an NBT file."""
pass pass
@@ -180,16 +190,6 @@ class NBTFileReader(object):
except (struct.error, ValueError), e: except (struct.error, ValueError), e:
raise CorruptNBTError("could not parse nbt: %s" % (str(e),)) raise CorruptNBTError("could not parse nbt: %s" % (str(e),))
class CorruptionError(Exception):
pass
class CorruptRegionError(CorruptionError):
"""An exception raised when the MCRFileReader class encounters an
error during region file parsing.
"""
pass
class CorruptChunkError(CorruptionError):
pass
# For reference, the MCR format is outlined at # For reference, the MCR format is outlined at
# <http://www.minecraftwiki.net/wiki/Beta_Level_Format> # <http://www.minecraftwiki.net/wiki/Beta_Level_Format>
class MCRFileReader(object): class MCRFileReader(object):
@@ -289,7 +289,7 @@ class MCRFileReader(object):
is_gzip = False is_gzip = False
else: else:
# unsupported! # unsupported!
raise CorruptRegionError("unsupported chunk compression type: %i" % (compression)) raise CorruptRegionError("unsupported chunk compression type: %i (should be 1 or 2)" % (compression,))
# turn the rest of the data into a StringIO object # turn the rest of the data into a StringIO object
# (using data_length - 1, as we already read 1 byte for compression) # (using data_length - 1, as we already read 1 byte for compression)
@@ -300,5 +300,7 @@ class MCRFileReader(object):
try: try:
return NBTFileReader(data, is_gzip=is_gzip).read_all() return NBTFileReader(data, is_gzip=is_gzip).read_all()
except CorruptionError:
raise
except Exception, e: except Exception, e:
raise CorruptChunkError("Misc error parsing chunk: " + str(e)) raise CorruptChunkError("Misc error parsing chunk: " + str(e))

View File

@@ -246,7 +246,7 @@ class RegionSet(object):
# This is populated below. It is a mapping from (x,y) region coords to filename # This is populated below. It is a mapping from (x,y) region coords to filename
self.regionfiles = {} self.regionfiles = {}
# This holds up to 16 open regionfile objects # This holds a cache of open regionfile objects
self.regioncache = cache.LRUCache(size=16, destructor=lambda regionobj: regionobj.close()) self.regioncache = cache.LRUCache(size=16, destructor=lambda regionobj: regionobj.close())
for x, y, regionfile in self._iterate_regionfiles(): for x, y, regionfile in self._iterate_regionfiles():
@@ -319,8 +319,8 @@ class RegionSet(object):
if regionfile is None: if regionfile is None:
raise ChunkDoesntExist("Chunk %s,%s doesn't exist (and neither does its region)" % (x,z)) raise ChunkDoesntExist("Chunk %s,%s doesn't exist (and neither does its region)" % (x,z))
# Try 3 times to load and parse this chunk before giving up and raising # Try a few times to load and parse this chunk before giving up and
# an error # raising an error
tries = 5 tries = 5
while True: while True:
try: try:
@@ -337,8 +337,8 @@ class RegionSet(object):
time.sleep(0.5) time.sleep(0.5)
continue continue
else: else:
logging.warning("Tried %d times to read chunk %d,%d but I got error %s", logging.warning("Tried several times to read chunk %d,%d. Giving up.",
tries, x, z, e) x, z, e)
logging.debug("Full traceback:", exc_info=1) logging.debug("Full traceback:", exc_info=1)
# Let this exception propagate out through the C code into # Let this exception propagate out through the C code into
# tileset.py, where it is caught and gracefully continues # tileset.py, where it is caught and gracefully continues