aboutsummaryrefslogtreecommitdiffstats
path: root/FuseArchive/Storage/FileSystem.py
diff options
context:
space:
mode:
Diffstat (limited to 'FuseArchive/Storage/FileSystem.py')
-rw-r--r--FuseArchive/Storage/FileSystem.py58
1 files changed, 44 insertions, 14 deletions
diff --git a/FuseArchive/Storage/FileSystem.py b/FuseArchive/Storage/FileSystem.py
index a6687b5..04c6bb9 100644
--- a/FuseArchive/Storage/FileSystem.py
+++ b/FuseArchive/Storage/FileSystem.py
@@ -4,6 +4,47 @@ from FuseArchive.Chunk import Chunk
magic_depth = 5
+def inc_chunk( key, count ):
+ # Increment this chunk header reference
+ path = _key_to_path( key )
+ logging.debug( "Updating header on %s, ref count + %d" % ( path, count ) )
+ f = open( path, "r+" )
+ data = f.read( Chunk.header_length() )
+ newcount, data = Chunk.inc_header_ref( data, count )
+ f.seek( 0 )
+ f.write( data )
+ f.close()
+ logging.debug( "Count is now: %d" % newcount )
+
+ assert newcount >= 0, "Count is negative?!!"
+
+ if newcount == 0:
+ logging.debug( "Freeing chunk" )
+ os.unlink( path )
+
+def lock_chunk( key ):
+ inc_chunk( key, 1 )
+
+def unlock_chunk( key ):
+ inc_chunk( key, -1 )
+
+def _key_to_path( key ):
+ logging.debug( "Converting key to path" )
+ ( thash, seq ) = key
+ chars = list( thash )
+ logging.debug( chars )
+
+ # Todo: make a digest -> path function to share with deflate
+ hexdigest = ''.join( [ "%02x" % ord( x ) for x in chars ] )
+ logging.debug( "Hash is: %s sub %d" % ( hexdigest, seq ) )
+ subparts = [ "%02x" % ord( x ) for x in chars[ :magic_depth ] ]
+ subpath = '/'.join( subparts )
+ logging.debug( "Subpath: " + subpath )
+
+ subpath += "/%s_%d" % ( hexdigest, seq )
+ return( "./storage/" + subpath )
+
+
# This will write out a data block, it will return a key that can get this
# data back later
def save_chunk( chunk ):
@@ -77,26 +118,15 @@ def load_chunk( key ):
if FuseArchive.magic_profiling:
return ''
- ( thash, seq ) = key
logging.debug( "Begin load_chunk" )
- chars = list( thash )
- logging.debug( chars )
-
- # Todo: make a digest -> path function to share with deflate
- hexdigest = ''.join( [ "%02x" % ord( x ) for x in chars ] )
- logging.debug( "Hash is: %s sub %d" % ( hexdigest, seq ) )
- subparts = [ "%02x" % ord( x ) for x in chars[ :magic_depth ] ]
- subpath = '/'.join( subparts )
- logging.debug( "Subpath: " + subpath )
-
- subpath += "/%s_%d" % ( hexdigest, seq )
+ subpath = _key_to_path( key )
logging.debug( "Chunk path: " + subpath )
- if os.path.exists( "./storage/" + subpath ):
+ if os.path.exists( subpath ):
logging.debug( "Exporting chunk" )
- readchunk = open( "./storage/" + subpath )
+ readchunk = open( subpath )
chunk = Chunk.deserialize( readchunk.read() ).chunk
readchunk.close()
else: