From e9195e9d14d679f1dad7edef82ab2dcd0ee0e6df Mon Sep 17 00:00:00 2001 From: Stavros Sachtouris Date: Fri, 10 Jan 2014 12:04:11 +0200 Subject: [PATCH] Remove fstat from clients.utils.readall Refs: #4871 Fstat was used to compute the actual file size in order to distinguish if an empty read was because the file ended. It now considers an empty read to be an end of file. This allows reading non-regular files. --- kamaki/clients/pithos/__init__.py | 8 +++++--- kamaki/clients/utils/__init__.py | 13 ++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/kamaki/clients/pithos/__init__.py b/kamaki/clients/pithos/__init__.py index ed88ede..3bc7b0a 100644 --- a/kamaki/clients/pithos/__init__.py +++ b/kamaki/clients/pithos/__init__.py @@ -315,17 +315,19 @@ class PithosClient(PithosRestClient): hash_gen = hash_cb(nblocks) hash_gen.next() - for i in range(nblocks): + for i in xrange(nblocks): block = readall(fileobj, min(blocksize, size - offset)) bytes = len(block) + if bytes <= 0: + break hash = _pithos_hash(block, blockhash) hashes.append(hash) hmap[hash] = (offset, bytes) offset += bytes if hash_cb: hash_gen.next() - msg = ('Failed to calculate uploaded blocks:' - ' Offset and object size do not match') + msg = ('Failed to calculate uploading blocks: ' + 'read bytes(%s) != requested size (%s)' % (offset, size)) assert offset == size, msg def _upload_missing_blocks(self, missing, hmap, fileobj, upload_gen=None): diff --git a/kamaki/clients/utils/__init__.py b/kamaki/clients/utils/__init__.py index 744595a..b7d400b 100644 --- a/kamaki/clients/utils/__init__.py +++ b/kamaki/clients/utils/__init__.py @@ -94,15 +94,14 @@ def path4url(*args): def readall(openfile, size, retries=7): """Read a file until size is reached""" - from os import fstat - actual_size = fstat(openfile.fileno()).st_size - openfile.tell() - size = actual_size if actual_size < size else size remains = size if size > 0 else 0 buf = '' for i in range(retries): - buf += openfile.read(remains) - remains = size - len(buf) - if remains: - continue + tmp_buf = openfile.read(remains) + if tmp_buf: + buf += tmp_buf + remains -= len(tmp_buf) + if remains > 0: + continue return buf raise IOError('Failed to read %s bytes from file' % size) -- 1.7.10.4