Revision e9195e9d kamaki/clients/utils/__init__.py
b/kamaki/clients/utils/__init__.py | ||
---|---|---|
94 | 94 |
|
95 | 95 |
def readall(openfile, size, retries=7): |
96 | 96 |
"""Read a file until size is reached""" |
97 |
from os import fstat |
|
98 |
actual_size = fstat(openfile.fileno()).st_size - openfile.tell() |
|
99 |
size = actual_size if actual_size < size else size |
|
100 | 97 |
remains = size if size > 0 else 0 |
101 | 98 |
buf = '' |
102 | 99 |
for i in range(retries): |
103 |
buf += openfile.read(remains) |
|
104 |
remains = size - len(buf) |
|
105 |
if remains: |
|
106 |
continue |
|
100 |
tmp_buf = openfile.read(remains) |
|
101 |
if tmp_buf: |
|
102 |
buf += tmp_buf |
|
103 |
remains -= len(tmp_buf) |
|
104 |
if remains > 0: |
|
105 |
continue |
|
107 | 106 |
return buf |
108 | 107 |
raise IOError('Failed to read %s bytes from file' % size) |
Also available in: Unified diff