@ -33,6 +33,7 @@ var constants = process.binding('constants');
var fs = exports ;
var Stream = require ( 'stream' ) . Stream ;
var EventEmitter = require ( 'events' ) . EventEmitter ;
var FSReqWrap = binding . FSReqWrap ;
var Readable = Stream . Readable ;
var Writable = Stream . Writable ;
@ -182,7 +183,9 @@ fs.Stats.prototype.isSocket = function() {
fs . exists = function ( path , callback ) {
if ( ! nullCheck ( path , cb ) ) return ;
binding . stat ( pathModule . _ makeLong ( path ) , cb ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = cb ;
binding . stat ( pathModule . _ makeLong ( path ) , req ) ;
function cb ( err , stats ) {
if ( callback ) callback ( err ? false : true ) ;
}
@ -421,7 +424,9 @@ Object.defineProperty(exports, '_stringToFlags', {
// list to make the arguments clear.
fs . close = function ( fd , callback ) {
binding . close ( fd , makeCallback ( callback ) ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = makeCallback ( callback ) ;
binding . close ( fd , req ) ;
} ;
fs . closeSync = function ( fd ) {
@ -443,10 +448,14 @@ fs.open = function(path, flags, mode, callback) {
mode = modeNum ( mode , 438 /*=0666*/ ) ;
if ( ! nullCheck ( path , callback ) ) return ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . open ( pathModule . _ makeLong ( path ) ,
stringToFlags ( flags ) ,
mode ,
callback ) ;
req ) ;
} ;
fs . openSync = function ( path , flags , mode ) {
@ -482,7 +491,10 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
callback && callback ( err , bytesRead || 0 , buffer ) ;
}
binding . read ( fd , buffer , offset , length , position , wrapper ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = wrapper ;
binding . read ( fd , buffer , offset , length , position , req ) ;
} ;
fs . readSync = function ( fd , buffer , offset , length , position ) {
@ -515,6 +527,16 @@ fs.readSync = function(fd, buffer, offset, length, position) {
// OR
// fs.write(fd, string[, position[, encoding]], callback);
fs . write = function ( fd , buffer , offset , length , position , callback ) {
function strWrapper ( err , written ) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback ( err , written || 0 , buffer ) ;
}
function bufWrapper ( err , written ) {
// retain reference to string in case it's external
callback ( err , written || 0 , buffer ) ;
}
if ( util . isBuffer ( buffer ) ) {
// if no position is passed then assume null
if ( util . isFunction ( position ) ) {
@ -522,11 +544,9 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
position = null ;
}
callback = maybeCallback ( callback ) ;
var wrapper = function ( err , written ) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback ( err , written || 0 , buffer ) ;
} ;
return binding . writeBuffer ( fd , buffer , offset , length , position , wrapper ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = strWrapper ;
return binding . writeBuffer ( fd , buffer , offset , length , position , req ) ;
}
if ( util . isString ( buffer ) )
@ -541,11 +561,9 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
length = 'utf8' ;
}
callback = maybeCallback ( position ) ;
position = function ( err , written ) {
// retain reference to string in case it's external
callback ( err , written || 0 , buffer ) ;
} ;
return binding . writeString ( fd , buffer , offset , length , position ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = bufWrapper ;
return binding . writeString ( fd , buffer , offset , length , req ) ;
} ;
// usage:
@ -569,9 +587,11 @@ fs.rename = function(oldPath, newPath, callback) {
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( oldPath , callback ) ) return ;
if ( ! nullCheck ( newPath , callback ) ) return ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . rename ( pathModule . _ makeLong ( oldPath ) ,
pathModule . _ makeLong ( newPath ) ,
callback ) ;
req ) ;
} ;
fs . renameSync = function ( oldPath , newPath ) {
@ -583,8 +603,9 @@ fs.renameSync = function(oldPath, newPath) {
fs . truncate = function ( path , len , callback ) {
if ( util . isNumber ( path ) ) {
// legacy
return fs . ftruncate ( path , len , callback ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
return fs . ftruncate ( path , len , req ) ;
}
if ( util . isFunction ( len ) ) {
callback = len ;
@ -592,14 +613,17 @@ fs.truncate = function(path, len, callback) {
} else if ( util . isUndefined ( len ) ) {
len = 0 ;
}
callback = maybeCallback ( callback ) ;
fs . open ( path , 'r+' , function ( er , fd ) {
if ( er ) return callback ( er ) ;
binding . ftruncate ( fd , len , function ( er ) {
var req = new FSReqWrap ( ) ;
req . oncomplete = function ftruncateCb ( er ) {
fs . close ( fd , function ( er2 ) {
callback ( er || er2 ) ;
} ) ;
} ) ;
} ;
binding . ftruncate ( fd , len , req ) ;
} ) ;
} ;
@ -628,7 +652,9 @@ fs.ftruncate = function(fd, len, callback) {
} else if ( util . isUndefined ( len ) ) {
len = 0 ;
}
binding . ftruncate ( fd , len , makeCallback ( callback ) ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = makeCallback ( callback ) ;
binding . ftruncate ( fd , len , req ) ;
} ;
fs . ftruncateSync = function ( fd , len ) {
@ -639,9 +665,11 @@ fs.ftruncateSync = function(fd, len) {
} ;
fs . rmdir = function ( path , callback ) {
callback = mak eCallback ( callback ) ;
callback = mayb eCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
binding . rmdir ( pathModule . _ makeLong ( path ) , callback ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . rmdir ( pathModule . _ makeLong ( path ) , req ) ;
} ;
fs . rmdirSync = function ( path ) {
@ -650,7 +678,9 @@ fs.rmdirSync = function(path) {
} ;
fs . fdatasync = function ( fd , callback ) {
binding . fdatasync ( fd , makeCallback ( callback ) ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = makeCallback ( callback ) ;
binding . fdatasync ( fd , req ) ;
} ;
fs . fdatasyncSync = function ( fd ) {
@ -658,7 +688,9 @@ fs.fdatasyncSync = function(fd) {
} ;
fs . fsync = function ( fd , callback ) {
binding . fsync ( fd , makeCallback ( callback ) ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = makeCallback ( callback ) ;
binding . fsync ( fd , req ) ;
} ;
fs . fsyncSync = function ( fd ) {
@ -669,9 +701,11 @@ fs.mkdir = function(path, mode, callback) {
if ( util . isFunction ( mode ) ) callback = mode ;
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . mkdir ( pathModule . _ makeLong ( path ) ,
modeNum ( mode , 511 /*=0777*/ ) ,
callback ) ;
req ) ;
} ;
fs . mkdirSync = function ( path , mode ) {
@ -683,7 +717,9 @@ fs.mkdirSync = function(path, mode) {
fs . readdir = function ( path , callback ) {
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
binding . readdir ( pathModule . _ makeLong ( path ) , callback ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . readdir ( pathModule . _ makeLong ( path ) , req ) ;
} ;
fs . readdirSync = function ( path ) {
@ -692,19 +728,25 @@ fs.readdirSync = function(path) {
} ;
fs . fstat = function ( fd , callback ) {
binding . fstat ( fd , makeCallback ( callback ) ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = makeCallback ( callback ) ;
binding . fstat ( fd , req ) ;
} ;
fs . lstat = function ( path , callback ) {
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
binding . lstat ( pathModule . _ makeLong ( path ) , callback ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . lstat ( pathModule . _ makeLong ( path ) , req ) ;
} ;
fs . stat = function ( path , callback ) {
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
binding . stat ( pathModule . _ makeLong ( path ) , callback ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . stat ( pathModule . _ makeLong ( path ) , req ) ;
} ;
fs . fstatSync = function ( fd ) {
@ -724,7 +766,9 @@ fs.statSync = function(path) {
fs . readlink = function ( path , callback ) {
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
binding . readlink ( pathModule . _ makeLong ( path ) , callback ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . readlink ( pathModule . _ makeLong ( path ) , req ) ;
} ;
fs . readlinkSync = function ( path ) {
@ -752,10 +796,13 @@ fs.symlink = function(destination, path, type_, callback) {
if ( ! nullCheck ( destination , callback ) ) return ;
if ( ! nullCheck ( path , callback ) ) return ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . symlink ( preprocessSymlinkDestination ( destination , type ) ,
pathModule . _ makeLong ( path ) ,
type ,
callback ) ;
req ) ;
} ;
fs . symlinkSync = function ( destination , path , type ) {
@ -774,9 +821,12 @@ fs.link = function(srcpath, dstpath, callback) {
if ( ! nullCheck ( srcpath , callback ) ) return ;
if ( ! nullCheck ( dstpath , callback ) ) return ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . link ( pathModule . _ makeLong ( srcpath ) ,
pathModule . _ makeLong ( dstpath ) ,
callback ) ;
req ) ;
} ;
fs . linkSync = function ( srcpath , dstpath ) {
@ -789,7 +839,9 @@ fs.linkSync = function(srcpath, dstpath) {
fs . unlink = function ( path , callback ) {
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
binding . unlink ( pathModule . _ makeLong ( path ) , callback ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . unlink ( pathModule . _ makeLong ( path ) , req ) ;
} ;
fs . unlinkSync = function ( path ) {
@ -798,7 +850,9 @@ fs.unlinkSync = function(path) {
} ;
fs . fchmod = function ( fd , mode , callback ) {
binding . fchmod ( fd , modeNum ( mode ) , makeCallback ( callback ) ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = makeCallback ( callback ) ;
binding . fchmod ( fd , modeNum ( mode ) , req ) ;
} ;
fs . fchmodSync = function ( fd , mode ) {
@ -848,9 +902,11 @@ if (constants.hasOwnProperty('O_SYMLINK')) {
fs . chmod = function ( path , mode , callback ) {
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . chmod ( pathModule . _ makeLong ( path ) ,
modeNum ( mode ) ,
callback ) ;
req ) ;
} ;
fs . chmodSync = function ( path , mode ) {
@ -877,7 +933,9 @@ if (constants.hasOwnProperty('O_SYMLINK')) {
}
fs . fchown = function ( fd , uid , gid , callback ) {
binding . fchown ( fd , uid , gid , makeCallback ( callback ) ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = makeCallback ( callback ) ;
binding . fchown ( fd , uid , gid , req ) ;
} ;
fs . fchownSync = function ( fd , uid , gid ) {
@ -887,7 +945,9 @@ fs.fchownSync = function(fd, uid, gid) {
fs . chown = function ( path , uid , gid , callback ) {
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
binding . chown ( pathModule . _ makeLong ( path ) , uid , gid , callback ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . chown ( pathModule . _ makeLong ( path ) , uid , gid , req ) ;
} ;
fs . chownSync = function ( path , uid , gid ) {
@ -913,10 +973,12 @@ fs._toUnixTimestamp = toUnixTimestamp;
fs . utimes = function ( path , atime , mtime , callback ) {
callback = makeCallback ( callback ) ;
if ( ! nullCheck ( path , callback ) ) return ;
var req = new FSReqWrap ( ) ;
req . oncomplete = callback ;
binding . utimes ( pathModule . _ makeLong ( path ) ,
toUnixTimestamp ( atime ) ,
toUnixTimestamp ( mtime ) ,
callback ) ;
req ) ;
} ;
fs . utimesSync = function ( path , atime , mtime ) {
@ -929,7 +991,9 @@ fs.utimesSync = function(path, atime, mtime) {
fs . futimes = function ( fd , atime , mtime , callback ) {
atime = toUnixTimestamp ( atime ) ;
mtime = toUnixTimestamp ( mtime ) ;
binding . futimes ( fd , atime , mtime , makeCallback ( callback ) ) ;
var req = new FSReqWrap ( ) ;
req . oncomplete = makeCallback ( callback ) ;
binding . futimes ( fd , atime , mtime , req ) ;
} ;
fs . futimesSync = function ( fd , atime , mtime ) {