From 0171712a2516e638f1dd2a8e247fc44433babbd8 Mon Sep 17 00:00:00 2001 From: Steve Slaven Date: Thu, 23 Jul 2009 14:34:18 -0700 Subject: File saving works now diff --git a/fusearchive.py b/fusearchive.py index 25fc046..f648235 100755 --- a/fusearchive.py +++ b/fusearchive.py @@ -30,7 +30,7 @@ debug_level = 4 def dmsg(level,message): if level <= debug_level: - print str(level) + ": " + message + print str(level) + ": " + str(message) def flag2mode(flags): md = {os.O_RDONLY: 'r', os.O_WRONLY: 'w', os.O_RDWR: 'w+'} @@ -110,7 +110,7 @@ def load_chunk( key ): # Todo: make a digest -> path function to share with deflate hexdigest = ''.join( [ "%02x" % ord( x ) for x in chars ] ) - dmsg( 3, "Hash is: " + hash + " sub " + seq ) + dmsg( 3, "Hash is: " + str(hexdigest) + " sub " + str(seq) ) subparts = [ "%02x" % ord( x ) for x in chars[ :magic_depth ] ] subpath = '/'.join( subparts ) dmsg( 3, "Subpath: " + subpath ) @@ -397,8 +397,7 @@ class FuseArchive(Fuse): def _load_chunk( self, index ): # Save this chunk if modified - if self.chunk_modified: - self._save_chunk() + self._save_chunk() dmsg( 3, "Loading chunk " + str(index) ) key = None @@ -413,16 +412,23 @@ class FuseArchive(Fuse): self.chunk = load_chunk( key ) else: dmsg( 3, "No chunk at this index, loading nothing" ) - self.chunk = None + self.chunk = '' self.chunk_index = index self.chunk_modified = False - def _save_chunk(): - dmsg( 3, "Saving chunk " + self.chunk_index ) - key = save_chunk( self.chunk ) - self.chunks[ index ] = key - dmsg( 3, "Key was " + str( key ) ) + def _save_chunk(self): + if self.chunk_modified: + dmsg( 3, "Saving chunk " + str( self.chunk_index ) ) + key = save_chunk( self.chunk ) + + # Make sure we have room for this chunk + size = len( self.chunks ) + if self.chunk_index >= size: + self.chunks.extend( [None] * ( self.chunk_index -size + 1 ) ) + + self.chunks[ self.chunk_index ] = key + dmsg( 3, "Key was " + str( key ) ) def read(self, length, offset): dmsg( 3, "Reading from " + self.orig_path + " offset: " + str( offset ) @@ -459,12 +465,29 @@ class FuseArchive(Fuse): dmsg( 3, "Pulling in chunk for writing" ) self._load_chunk( index ) buf_remain = buf_len - buf_offset - if rest < buf_remain: - dmsg( 3, "Writing " + str( rest ) + " bytes, buffer boundry" ) + chunk_remain = self.chunk_size - rest + if chunk_remain < buf_remain: + dmsg( 3, "Writing " + str( chunk_remain ) + " bytes, buffer boundry" ) + this_len = chunk_remain else: dmsg( 3, "Writing final " + str( buf_remain ) + " bytes" ) + this_len = buf_remain + + # Since python doesn't do in-place reassignment like you + # can with splice() we will reconstruct the data by joining + # stuff by offsets (first chars to skip, then our joining + # buf chunk, the everything that would have been after it) + self.chunk = self.chunk[ :rest ] + \ + buf[ buf_offset:this_len ] + \ + self.chunk[ (rest + this_len): ] + + buf_offset += this_len + + # Advance to next block + rest = 0 + index += 1 + self.chunk_modified = True - self.chunk_modified = True return len(buf) # BUG: If you cp -a a file then quickly ls -l sometimes it doesn't show @@ -479,6 +502,23 @@ class FuseArchive(Fuse): if self.wr: dmsg( 3, "_fflush!" ) # Save our main data + self._save_chunk() + + # Figure out our size based on the number of chunks + the + # len of the final chunk + numchunks = len( self.chunks ) + if numchunks > 0: + # Load the last chunk + dmsg( 3, "We have " + str(numchunks) + " chunks, calculating size" ) + self._load_chunk( numchunks - 1 ) + self.size = ( numchunks - 1 ) * self.chunk_size + \ + len( self.chunk ) + else: + dmsg( 3, "No chunks, setting size to zero" ) + self.size = 0 + + dmsg( 3, "Size calculated is: " + str( self.size ) ) + out = gzip.open( "./tree" + self.orig_path, "w" ) pickle.dump( { 'size': self.size, -- cgit v0.10.2