The ETag header/hash property always includes the MD5.
[pithos] / pithos / api / util.py
index d909ed0..6b513b5 100644 (file)
@@ -741,12 +741,26 @@ def put_object_block(request, hashmap, data, offset):
         hashmap.append(request.backend.put_block(('\x00' * bo) + data[:bl]))
     return bl # Return ammount of data written.
 
-def hashmap_hash(request, hashmap):
-    """Produce the root hash, treating the hashmap as a Merkle-like tree."""
-    
-    map = HashMap(request.backend.block_size, request.backend.hash_algorithm)
-    map.extend([unhexlify(x) for x in hashmap])
-    return hexlify(map.hash())
+#def hashmap_hash(request, hashmap):
+#    """Produce the root hash, treating the hashmap as a Merkle-like tree."""
+#    
+#    map = HashMap(request.backend.block_size, request.backend.hash_algorithm)
+#    map.extend([unhexlify(x) for x in hashmap])
+#    return hexlify(map.hash())
+
+def hashmap_md5(request, hashmap, size):
+    """Produce the MD5 sum from the data in the hashmap."""
+    
+    # TODO: Search backend for the MD5 of another object with the same hashmap and size...
+    md5 = hashlib.md5()
+    bs = request.backend.block_size
+    for bi, hash in enumerate(hashmap):
+        data = request.backend.get_block(hash)
+        if bi == len(hashmap) - 1:
+            bs = size % bs
+        pad = bs - min(len(data), bs)
+        md5.update(data + ('\x00' * pad))
+    return md5.hexdigest().lower()
 
 def update_request_headers(request):
     # Handle URL-encoded keys and values.