@@ -152,7 +152,7 @@ WriteRequest.prototype.onwrite = function (err, e) {
152152 }
153153
154154 if ( ! err ) {
155- this . file . updateSize ( e . currentTarget . length , this . truncating )
155+ this . file . updateSize ( e . currentTarget . length )
156156 }
157157
158158 if ( this . truncating ) {
@@ -166,6 +166,7 @@ WriteRequest.prototype.onwrite = function (err, e) {
166166
167167WriteRequest . prototype . truncate = function ( ) {
168168 this . truncating = true
169+ this . file . truncate ( )
169170 this . writer . truncate ( this . req . offset )
170171}
171172
@@ -176,21 +177,23 @@ WriteRequest.prototype.lock = function () {
176177}
177178
178179WriteRequest . prototype . run = function ( req ) {
179- var file = this . file
180+ this . file . getWritableFile ( ( err , file ) => {
181+ if ( err ) return req . callback ( err )
180182
181- this . req = req
182- if ( ! this . writer || this . writer . length !== file . size ) return this . makeWriter ( )
183+ this . req = req
184+ if ( ! this . writer || this . writer . length !== file . size ) return this . makeWriter ( )
183185
184- const end = req . offset + req . size
185- if ( end > file . size && ! this . lock ( ) ) return
186+ const end = req . offset + req . size
187+ if ( end > file . size && ! this . lock ( ) ) return
186188
187- if ( req . offset > this . writer . length ) {
188- if ( req . offset > file . size ) return this . truncate ( )
189- return this . makeWriter ( )
190- }
189+ if ( req . offset > this . writer . length ) {
190+ if ( req . offset > file . size ) return this . truncate ( )
191+ return this . makeWriter ( )
192+ }
191193
192- this . writer . seek ( req . offset )
193- this . writer . write ( new Blob ( [ req . data ] , TYPE ) )
194+ this . writer . seek ( req . offset )
195+ this . writer . write ( new Blob ( [ req . data ] , TYPE ) )
196+ } )
194197}
195198
196199function Mutex ( ) {
@@ -246,12 +249,6 @@ ReadRequest.prototype.onread = function (err, buf) {
246249 const req = this . req
247250
248251 if ( err && this . retry ) {
249- if ( err . name === 'NotReadableError' ) {
250- this . file . clearFile ( )
251- this . run ( req )
252- return
253- }
254-
255252 this . retry = false
256253 if ( this . lock ( this ) ) {
257254 this . file . clearFile ( )
@@ -289,44 +286,53 @@ class EntryFile {
289286 this . _lock = mutexify ( )
290287 this . _file = null
291288 this . _size = 0
292- }
293-
294- get locked ( ) {
295- return this . _lock . locked
289+ this . _truncated = false
296290 }
297291
298292 get size ( ) {
299293 return this . _size
300294 }
301295
302- updateSize ( size , truncating = false ) {
303- if ( truncating || size > this . _size ) {
296+ updateSize ( size ) {
297+ if ( ! this . _truncated ) {
304298 this . _size = size
305299 }
306300
307301 this . clearFile ( )
308302 }
309303
304+ truncate ( ) {
305+ this . _truncated = true
306+ }
307+
310308 clearFile ( ) {
311309 this . _file = null
312310 }
313311
314312 get ( cb ) {
315- if ( this . _file ) {
316- cb ( null , this . _file )
317- return
313+ if ( this . _file && ! this . _truncated ) {
314+ return cb ( null , this . _file )
318315 }
319316
320317 this . _lock ( release => {
321- if ( this . _file ) {
318+ if ( this . _file && ! this . _truncated ) {
322319 return release ( cb , null , this . _file )
323320 }
324321
325322 this . _entry . file ( file => {
323+ this . _truncated = false
326324 this . _file = file
327325 this . _size = file . size
328326 release ( cb , null , file )
329327 } , err => release ( cb , err ) )
330328 } )
331329 }
330+
331+ getWritableFile ( cb ) {
332+ if ( ! this . _truncated ) {
333+ return cb ( null , this )
334+ }
335+
336+ this . get ( cb )
337+ }
332338}
0 commit comments