root / snf-pithos-backend / pithos / backends / lib / hashfiler / context_file.py @ 4ab486a3
History | View | Annotate | Download (5.5 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from os import ( |
35 |
SEEK_CUR, |
36 |
SEEK_SET, |
37 |
O_RDONLY, |
38 |
O_WRONLY, |
39 |
O_RDWR |
40 |
) |
41 |
|
42 |
_zeros = ''
|
43 |
|
44 |
|
45 |
def zeros(nr): |
46 |
global _zeros
|
47 |
size = len(_zeros)
|
48 |
if nr == size:
|
49 |
return _zeros
|
50 |
|
51 |
if nr > size:
|
52 |
_zeros += '\0' * (nr - size)
|
53 |
return _zeros
|
54 |
|
55 |
if nr < size:
|
56 |
_zeros = _zeros[:nr] |
57 |
return _zeros
|
58 |
|
59 |
|
60 |
def file_sync_write_chunks(openfile, chunksize, offset, chunks, size=None): |
61 |
"""Write given chunks to the given buffered file object.
|
62 |
Writes never span across chunk boundaries.
|
63 |
If size is given stop after or pad until size bytes have been written.
|
64 |
"""
|
65 |
fwrite = openfile.write |
66 |
seek = openfile.seek |
67 |
padding = 0
|
68 |
|
69 |
try:
|
70 |
seek(offset * chunksize) |
71 |
except IOError: |
72 |
seek = None
|
73 |
for x in xrange(offset): |
74 |
fwrite(zeros(chunksize)) |
75 |
|
76 |
cursize = offset * chunksize |
77 |
|
78 |
for chunk in chunks: |
79 |
if padding:
|
80 |
if seek:
|
81 |
seek(padding - 1, SEEK_CUR)
|
82 |
fwrite("\x00")
|
83 |
else:
|
84 |
fwrite(buffer(zeros(chunksize), 0, padding)) |
85 |
if size is not None and cursize + chunksize >= size: |
86 |
chunk = chunk[:chunksize - (cursize - size)] |
87 |
fwrite(chunk) |
88 |
cursize += len(chunk)
|
89 |
break
|
90 |
fwrite(chunk) |
91 |
padding = chunksize - len(chunk)
|
92 |
|
93 |
padding = size - cursize if size is not None else 0 |
94 |
if padding <= 0: |
95 |
return
|
96 |
|
97 |
q, r = divmod(padding, chunksize)
|
98 |
for x in xrange(q): |
99 |
fwrite(zeros(chunksize)) |
100 |
fwrite(buffer(zeros(chunksize), 0, r)) |
101 |
|
102 |
|
103 |
def file_sync_read_chunks(openfile, chunksize, nr, offset=0): |
104 |
"""Read and yield groups of chunks from a buffered file object at offset.
|
105 |
Reads never span accros chunksize boundaries.
|
106 |
"""
|
107 |
fread = openfile.read |
108 |
remains = offset * chunksize |
109 |
seek = openfile.seek |
110 |
try:
|
111 |
seek(remains) |
112 |
except IOError: |
113 |
seek = None
|
114 |
while 1: |
115 |
s = fread(remains) |
116 |
remains -= len(s)
|
117 |
if remains <= 0: |
118 |
break
|
119 |
|
120 |
while nr:
|
121 |
remains = chunksize |
122 |
chunk = ''
|
123 |
while 1: |
124 |
s = fread(remains) |
125 |
if not s: |
126 |
if chunk:
|
127 |
yield chunk
|
128 |
return
|
129 |
chunk += s |
130 |
remains -= len(s)
|
131 |
if remains <= 0: |
132 |
break
|
133 |
yield chunk
|
134 |
nr -= 1
|
135 |
|
136 |
|
137 |
class ContextFile(object): |
138 |
__slots__ = ("name", "fdesc", "oflag") |
139 |
|
140 |
def __init__(self, name, oflag): |
141 |
self.name = name
|
142 |
self.fdesc = None |
143 |
self.oflag = oflag
|
144 |
#self.dirty = 0
|
145 |
|
146 |
def __enter__(self): |
147 |
name = self.name
|
148 |
if self.oflag == O_RDONLY: |
149 |
fdesc = open(name, 'rb') |
150 |
elif self.oflag == O_WRONLY: |
151 |
fdesc = open(name, 'wb') |
152 |
elif self.oflag == O_RDWR: |
153 |
fdesc = open(name, 'wb+') |
154 |
else:
|
155 |
raise Exception("Wrong file acccess mode.") |
156 |
|
157 |
self.fdesc = fdesc
|
158 |
return self |
159 |
|
160 |
def __exit__(self, exc, arg, trace): |
161 |
fdesc = self.fdesc
|
162 |
if fdesc is not None: |
163 |
#if self.dirty:
|
164 |
# fsync(fdesc.fileno())
|
165 |
fdesc.close() |
166 |
return False # propagate exceptions |
167 |
|
168 |
def seek(self, offset, whence=SEEK_SET): |
169 |
return self.fdesc.seek(offset, whence) |
170 |
|
171 |
def tell(self): |
172 |
return self.fdesc.tell() |
173 |
|
174 |
def truncate(self, size): |
175 |
self.fdesc.truncate(size)
|
176 |
|
177 |
def sync_write(self, data): |
178 |
#self.dirty = 1
|
179 |
self.fdesc.write(data)
|
180 |
|
181 |
def sync_write_chunks(self, chunksize, offset, chunks, size=None): |
182 |
#self.dirty = 1
|
183 |
return file_sync_write_chunks(self.fdesc, chunksize, offset, chunks, |
184 |
size) |
185 |
|
186 |
def sync_read(self, size): |
187 |
read = self.fdesc.read
|
188 |
data = ''
|
189 |
while 1: |
190 |
s = read(size) |
191 |
if not s: |
192 |
break
|
193 |
data += s |
194 |
return data
|
195 |
|
196 |
def sync_read_chunks(self, chunksize, nr, offset=0): |
197 |
return file_sync_read_chunks(self.fdesc, chunksize, nr, offset) |