Changeset - 5673cf6b1207
[Not reviewed]
default
0 1 0
Laman - 8 years ago 2017-05-08 00:09:58

fixed tree load
1 file changed with 1 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/hashtree.py
Show inline comments
 
@@ -16,48 +16,49 @@ class HashTree:
 
	@classmethod
 
	def fromFile(cls,fd):
 
		stat=os.fstat(fd.fileno())
 
		size=stat.st_size # !! symlinks
 
		leafCount=(size-1)//HashTree.BLOCK_SIZE+1 # number of leaf blocks
 
		res=cls(leafCount)
 
		
 
		for i in range(leafCount):
 
			data=fd.read(HashTree.BLOCK_SIZE)
 
			res.insertLeaf(hashlib.sha256(data).digest()[HashTree.HASH_LEN:])
 
		res.buildTree()
 
		
 
		return res
 

	
 
	@classmethod
 
	def load(cls,filename):
 
		with open(filename,"rb") as f:
 
			stat=os.fstat(f.fileno())
 
			size=stat.st_size
 
			nodeCount=size//HashTree.HASH_LEN
 
			res=cls((nodeCount+1)//2)
 

	
 
			for i in range(nodeCount):
 
				res.store[i]=f.read(HashTree.HASH_LEN)
 
		return res
 

	
 
	def save(self,filename):
 
		with open(filename,"wb") as f:
 
			for h in self.store:
 
				f.write(h)
 

	
 
		
 
	## Inserts a leaf at the first empty position.
 
	#	
 
	#	Useful and used only during the tree construction.
 
	def insertLeaf(self,h):
 
		self.store[self.index]=h
 
		self.index+=1
 
		
 
	## Updates a hash stored in the leaf.
 
	def updateLeaf(self,index,h):
 
		if index<self.leafStart: raise IndexError()
 
		
 
		self.store[index]=h
 
		self.updateNode((index-1)//2)
 
	
 
	## Updates the node at index and all its ancestors.
 
	def updateNode(self,index):
 
		while index>=0:
0 comments (0 inline, 0 general)