commit 6f08f063ce9b6d926b749c0ce0883f387542b59a
Author: Chrysostomos Nanakos <cnanakos@grnet.gr>
Date:   Mon Feb 3 16:04:50 2014 +0200

    pithos: Add new read/write file object functions
    
    Add new functions for reading and writing file objects based
    on the new file access mode introduced in the file context manager.

diff --git a/snf-pithos-backend/pithos/backends/lib/hashfiler/fileblocker.py b/snf-pithos-backend/pithos/backends/lib/hashfiler/fileblocker.py
index 77a8906..0e07d0f 100644
--- a/snf-pithos-backend/pithos/backends/lib/hashfiler/fileblocker.py
+++ b/snf-pithos-backend/pithos/backends/lib/hashfiler/fileblocker.py
@@ -37,6 +37,7 @@ from hashlib import new as newhasher
 from binascii import hexlify
 
 from context_file import ContextFile, file_sync_read_chunks
+from os import O_RDONLY, O_WRONLY
 
 
 class FileBlocker(object):
@@ -78,13 +79,19 @@ class FileBlocker(object):
     def _pad(self, block):
         return block + ('\x00' * (self.blocksize - len(block)))
 
-    def _get_rear_block(self, blkhash, create=0):
+    def _read_rear_block(self, blkhash):
+        filename = hexlify(blkhash)
+        dir = join(self.blockpath, filename[0:2], filename[2:4], filename[4:6])
+        name = join(dir, filename)
+        return ContextFile(name, O_RDONLY)
+
+    def _write_rear_block(self, blkhash):
         filename = hexlify(blkhash)
         dir = join(self.blockpath, filename[0:2], filename[2:4], filename[4:6])
         if not exists(dir):
             makedirs(dir)
         name = join(dir, filename)
-        return ContextFile(name, create)
+        return ContextFile(name, O_WRONLY)
 
     def _check_rear_block(self, blkhash):
         filename = hexlify(blkhash)
@@ -122,7 +129,7 @@ class FileBlocker(object):
             if h == self.emptyhash:
                 append(self._pad(''))
                 continue
-            with self._get_rear_block(h, 0) as rbl:
+            with self._read_rear_block(h) as rbl:
                 if not rbl:
                     break
                 for block in rbl.sync_read_chunks(blocksize, 1, 0):
@@ -144,7 +151,7 @@ class FileBlocker(object):
         missing = [i for i, h in enumerate(hashlist) if not
                    self._check_rear_block(h)]
         for i in missing:
-            with self._get_rear_block(hashlist[i], 1) as rbl:
+            with self._write_rear_block(hashlist[i]) as rbl:
                 rbl.sync_write(blocklist[i])  # XXX: verify?
 
         return hashlist, missing
diff --git a/snf-pithos-backend/pithos/backends/lib/hashfiler/filemapper.py b/snf-pithos-backend/pithos/backends/lib/hashfiler/filemapper.py
index 6f12f61..8d1d4c8 100644
--- a/snf-pithos-backend/pithos/backends/lib/hashfiler/filemapper.py
+++ b/snf-pithos-backend/pithos/backends/lib/hashfiler/filemapper.py
@@ -36,6 +36,7 @@ from os.path import isdir, realpath, exists, join
 from binascii import hexlify
 
 from context_file import ContextFile
+from os import O_RDONLY, O_WRONLY
 
 
 class FileMapper(object):
@@ -58,13 +59,21 @@ class FileMapper(object):
                                  (mappath,))
         self.mappath = mappath
 
-    def _get_rear_map(self, maphash, create=0):
+    def _read_rear_map(self, maphash):
         filename = hexlify(maphash)
         dir = join(self.mappath, filename[0:2], filename[2:4], filename[4:6])
         if not exists(dir):
             makedirs(dir)
         name = join(dir, filename)
-        return ContextFile(name, create)
+        return ContextFile(name, O_RDONLY)
+
+    def _write_rear_map(self, maphash):
+        filename = hexlify(maphash)
+        dir = join(self.mappath, filename[0:2], filename[2:4], filename[4:6])
+        if not exists(dir):
+            makedirs(dir)
+        name = join(dir, filename)
+        return ContextFile(name, O_WRONLY)
 
     def _check_rear_map(self, maphash):
         filename = hexlify(maphash)
@@ -80,15 +89,15 @@ class FileMapper(object):
         namelen = self.namelen
         hashes = ()
 
-        with self._get_rear_map(maphash, 0) as rmap:
+        with self._read_rear_map(maphash) as rmap:
             if rmap:
                 hashes = list(rmap.sync_read_chunks(namelen, nr, blkoff))
         return hashes
 
-    def map_stor(self, maphash, hashes=(), blkoff=0, create=1):
+    def map_stor(self, maphash, hashes=(), blkoff=0):
         """Store hashes in the given hashes map."""
         namelen = self.namelen
         if self._check_rear_map(maphash):
             return
-        with self._get_rear_map(maphash, 1) as rmap:
+        with self._write_rear_map(maphash) as rmap:
             rmap.sync_write_chunks(namelen, blkoff, hashes, None)
