@@ -39,12 +39,20 @@ function isValidUnixId(id) {
3939 return true ;
4040}
4141
42- function isFatalOverwriteError ( err , flag ) {
42+ function getFlags ( options ) {
43+ var flags = ! options . append ? 'w' : 'a' ;
44+ if ( ! options . overwrite ) {
45+ flags += 'x' ;
46+ }
47+ return flags ;
48+ }
49+
50+ function isFatalOverwriteError ( err , flags ) {
4351 if ( ! err ) {
4452 return false ;
4553 }
4654
47- if ( err . code === 'EEXIST' && flag === 'wx ' ) {
55+ if ( err . code === 'EEXIST' && flags [ 1 ] === 'x ' ) {
4856 // Handle scenario for file overwrite failures.
4957 return false ;
5058 }
@@ -274,7 +282,7 @@ function updateMetadata(fd, file, callback) {
274282function symlink ( srcPath , destPath , opts , callback ) {
275283 // Because fs.symlink does not allow atomic overwrite option with flags, we
276284 // delete and recreate if the link already exists and overwrite is true.
277- if ( opts . flag === 'w' ) {
285+ if ( opts . flags === 'w' ) {
278286 // TODO What happens when we call unlink with windows junctions?
279287 fs . unlink ( destPath , onUnlink ) ;
280288 } else {
@@ -289,7 +297,7 @@ function symlink(srcPath, destPath, opts, callback) {
289297 }
290298
291299 function onSymlink ( symlinkErr ) {
292- if ( isFatalOverwriteError ( symlinkErr , opts . flag ) ) {
300+ if ( isFatalOverwriteError ( symlinkErr , opts . flags ) ) {
293301 return callback ( symlinkErr ) ;
294302 }
295303 callback ( ) ;
@@ -317,10 +325,10 @@ function writeFile(filepath, data, options, callback) {
317325
318326 // Default the same as node
319327 var mode = options . mode || constants . DEFAULT_FILE_MODE ;
320- var flag = options . flag || 'w' ;
321- var position = APPEND_MODE_REGEXP . test ( flag ) ? null : 0 ;
328+ var flags = options . flags || 'w' ;
329+ var position = APPEND_MODE_REGEXP . test ( flags ) ? null : 0 ;
322330
323- fs . open ( filepath , flag , mode , onOpen ) ;
331+ fs . open ( filepath , flags , mode , onOpen ) ;
324332
325333 function onOpen ( openErr , fd ) {
326334 if ( openErr ) {
@@ -357,7 +365,7 @@ function WriteStream(path, options, flush) {
357365 this . path = path ;
358366
359367 this . mode = options . mode || constants . DEFAULT_FILE_MODE ;
360- this . flag = options . flag || 'w' ;
368+ this . flags = options . flags || 'w' ;
361369
362370 // Used by node's `fs.WriteStream`
363371 this . fd = null ;
@@ -374,7 +382,7 @@ util.inherits(WriteStream, FlushWriteStream);
374382WriteStream . prototype . open = function ( ) {
375383 var self = this ;
376384
377- fs . open ( this . path , this . flag , this . mode , onOpen ) ;
385+ fs . open ( this . path , this . flags , this . mode , onOpen ) ;
378386
379387 function onOpen ( openErr , fd ) {
380388 if ( openErr ) {
@@ -435,6 +443,7 @@ function cleanup(callback) {
435443module . exports = {
436444 closeFd : closeFd ,
437445 isValidUnixId : isValidUnixId ,
446+ getFlags : getFlags ,
438447 isFatalOverwriteError : isFatalOverwriteError ,
439448 isFatalUnlinkError : isFatalUnlinkError ,
440449 getModeDiff : getModeDiff ,
0 commit comments