diff options
author | Steve Slaven <bpk@hoopajoo.net> | 2009-08-06 23:23:10 (GMT) |
---|---|---|
committer | Steve Slaven <bpk@hoopajoo.net> | 2009-08-06 23:23:10 (GMT) |
commit | 48c3c17f734f2e548b6c8d0d75a0060306a75ee6 (patch) | |
tree | f4829510a82181fb62c7f324859a6cc18ca1ec60 /FuseArchive/Chunk.py | |
parent | 2a98d3391ca347317284347e5cd1c3ecbadc3d7e (diff) | |
download | fusearchive-48c3c17f734f2e548b6c8d0d75a0060306a75ee6.zip fusearchive-48c3c17f734f2e548b6c8d0d75a0060306a75ee6.tar.gz fusearchive-48c3c17f734f2e548b6c8d0d75a0060306a75ee6.tar.bz2 |
Add reference counting so we can delete unused chunks
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 ) |