Commit 917e4bdf authored by Chrysostomos Nanakos's avatar Chrysostomos Nanakos
Browse files

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.
parent 9bebf114
......@@ -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
......
......@@ -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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment