@ -495,16 +495,31 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
read ( ) ;
} ) ;
this . forceClose = function ( ) {
this . forceClose = function ( cb ) {
this . readable = false ;
fs . close ( this . fd , function ( err ) {
function close ( ) {
fs . close ( self . fd , function ( err ) {
if ( err ) {
if ( cb ) {
cb ( err ) ;
}
self . emit ( 'error' , err ) ;
return ;
}
if ( cb ) {
cb ( null ) ;
}
self . emit ( 'close' ) ;
} ) ;
}
if ( this . fd ) {
close ( ) ;
} else {
this . addListener ( 'open' , close ) ;
}
} ;
this . pause = function ( ) {
@ -546,7 +561,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
queue = [ ] ,
busy = false ;
queue . push ( [ fs . open , this . path , this . flags , this . mode ] ) ;
queue . push ( [ fs . open , this . path , this . flags , this . mode , undefined ] ) ;
function flush ( ) {
if ( busy ) {
@ -560,29 +575,40 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
busy = true ;
var method = args . shift ( ) ;
var
method = args . shift ( ) ,
cb = args . pop ( ) ;
args . push ( function ( err ) {
busy = false ;
if ( err ) {
self . writeable = false ;
if ( cb ) {
cb ( err ) ;
}
self . emit ( 'error' , err ) ;
return ;
}
// save reference for file pointer
if ( method === fs . open ) {
self . fd = arguments [ 1 ] ;
self . emit ( 'open' , self . fd ) ;
}
// stop flushing after close
if ( method === fs . close ) {
if ( cb ) {
cb ( null ) ;
}
self . emit ( 'close' ) ;
return ;
}
// save reference for file pointer
if ( method === fs . open ) {
self . fd = arguments [ 1 ] ;
self . emit ( 'open' , self . fd ) ;
} else if ( cb ) {
// write callback
cb ( null , arguments [ 1 ] ) ;
}
flush ( ) ;
} ) ;
@ -594,30 +620,37 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
method . apply ( null , args ) ;
} ;
this . write = function ( data ) {
this . write = function ( data , cb ) {
if ( ! this . writeable ) {
throw new Error ( 'stream not writeable' ) ;
}
queue . push ( [ fs . write , data , undefined , this . encoding ] ) ;
queue . push ( [ fs . write , data , undefined , this . encoding , cb ] ) ;
flush ( ) ;
return false ;
} ;
this . close = function ( ) {
this . close = function ( cb ) {
this . writeable = false ;
queue . push ( [ fs . close , ] ) ;
queue . push ( [ fs . close , cb ] ) ;
flush ( ) ;
} ;
this . forceClose = function ( ) {
this . forceClose = function ( cb ) {
this . writeable = false ;
fs . close ( self . fd , function ( err ) {
if ( err ) {
if ( cb ) {
cb ( err ) ;
}
self . emit ( 'error' , err ) ;
return ;
}
if ( cb ) {
cb ( null ) ;
}
self . emit ( 'close' ) ;
} ) ;
} ;