diff options
Diffstat (limited to 'FuseArchive/Chunk.py')
-rw-r--r-- | FuseArchive/Chunk.py | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/FuseArchive/Chunk.py b/FuseArchive/Chunk.py index 9bb05bd..082df68 100644 --- a/FuseArchive/Chunk.py +++ b/FuseArchive/Chunk.py @@ -8,11 +8,11 @@ import struct, zlib, logging hformat = 'HLBL48x' compress_level = 6 -header_length = 64 +_header_length = 64 -assert struct.calcsize( hformat ) == header_length, \ - "Header struct must be 64 bytes not %d bytes" % \ - struct.calcsize( hformat ) +assert struct.calcsize( hformat ) == _header_length, \ + "Header struct must be %d bytes not %d bytes" % \ + ( _header_length, struct.calcsize( hformat ) ) # This handles serialization and deserialization of compressed chunks with # some header data @@ -59,15 +59,15 @@ class Chunk: @staticmethod def deserialize(data): logging.debug( "Deserializing data of length %d" % len( data ) ) - hd = Chunk.parse_header( data[ :header_length ] ) + hd = Chunk.parse_header( data[ :_header_length ] ) obj = Chunk() obj.count = hd[ 'count' ] compression = hd[ 'compression' ] if compression == 0: - obj.chunk = data[ header_length: ] + obj.chunk = data[ _header_length: ] elif compression == 1: - obj.chunk = zlib.decompress( data[ header_length: ] ) + obj.chunk = zlib.decompress( data[ _header_length: ] ) else: raise ValueError( "Invalid compression type: %d" % compression ) @@ -87,3 +87,15 @@ class Chunk: 'count': fields[ 3 ] } + # This is for updating header info, returns a tuple with the new count + # + the data + @staticmethod + def inc_header_ref(data, count): + logging.debug( "Incrementing ref count by %d" % count ) + fields = list( struct.unpack( hformat, data ) ) + fields[ 3 ] += count + return( fields[ 3 ], struct.pack( hformat, *fields ) ) + + @staticmethod + def header_length(): + return( _header_length ) |