@@ -161,35 +161,6 @@ def _set_cache_(self, attr):
161161 in the single attribute."""
162162 pass
163163
164-
165- class FDStreamWrapper (object ):
166- """A simple wrapper providing the most basic functions on a file descriptor
167- with the fileobject interface. Cannot use os.fdopen as the resulting stream
168- takes ownership"""
169- __slots__ = ("_fd" , '_pos' )
170- def __init__ (self , fd ):
171- self ._fd = fd
172- self ._pos = 0
173-
174- def write (self , data ):
175- self ._pos += len (data )
176- os .write (self ._fd , data )
177-
178- def read (self , count = 0 ):
179- if count == 0 :
180- count = os .path .getsize (self ._filepath )
181- # END handle read everything
182-
183- bytes = os .read (self ._fd , count )
184- self ._pos += len (bytes )
185- return bytes
186-
187- def fileno (self ):
188- return self ._fd
189-
190- def tell (self ):
191- return self ._pos
192-
193164
194165class LockedFD (object ):
195166 """This class facilitates a safe read and write operation to a file on disk.
@@ -240,7 +211,7 @@ def open(self, write=False, stream=False):
240211 binary = getattr (os , 'O_BINARY' , 0 )
241212 lockmode = os .O_WRONLY | os .O_CREAT | os .O_EXCL | binary
242213 try :
243- fd = os .open (self ._lockfilepath (), lockmode )
214+ fd = os .open (self ._lockfilepath (), lockmode , 0600 )
244215 if not write :
245216 os .close (fd )
246217 else :
@@ -253,11 +224,19 @@ def open(self, write=False, stream=False):
253224 # open actual file if required
254225 if self ._fd is None :
255226 # we could specify exlusive here, as we obtained the lock anyway
256- self ._fd = os .open (self ._filepath , os .O_RDONLY | binary )
227+ try :
228+ self ._fd = os .open (self ._filepath , os .O_RDONLY | binary )
229+ except :
230+ # assure we release our lockfile
231+ os .remove (self ._lockfilepath ())
232+ raise
233+ # END handle lockfile
257234 # END open descriptor for reading
258235
259236 if stream :
260- return FDStreamWrapper (self ._fd )
237+ # need delayed import
238+ from stream import FDStream
239+ return FDStream (self ._fd )
261240 else :
262241 return self ._fd
263242 # END handle stream
0 commit comments