lib/safeFile.js
function safeWriteFileSync (fileName, data, options) {
    verifyFileName (fileName);
    
    // if fileName exists, verify it is a file
    var info = getFileInfo (fileName);
    if ((info.exists) && (info.isFile === false)) {
        var message = format (cc.MSG_IS_NOT_A_FILE, fileName);
        throw new SafeFileError (cc.IS_NOT_A_FILE, message);
    }

    // get current file system state, and auto-recover if necessary
    var state = getState (fileName);

    // store data in well defined ephemeral file to allow manual recovery
    // If file already exists, remove it (failed prior recovery).
    if (state.ephemeral.exists) {
        fs.unlinkSync (state.ephemeral.name);
    }

    writeFileSync (state.ephemeral.name, data, options);
    state.ephemeral.exists = true;

    // if ready state file already exists, recover prior state
    if (state.ready.exists) {
        performRecovery (state, false);
    }
    
    fs.renameSync (state.ephemeral.name, state.ready.name);

    // refresh state and process recovery to set file system state
    state = getState (fileName);
    performRecovery (state, true);
}
function safeWriteFileSync (fileName, data, options) {
    verifyFileName (fileName);
    
    // if fileName exists, verify it is a file
    var info = getFileInfo (fileName);
    if ((info.exists) && (info.isFile === false)) {
        var message = format (cc.MSG_IS_NOT_A_FILE, fileName);
        throw new SafeFileError (cc.IS_NOT_A_FILE, message);
    }

    // get current file system state, and auto-recover if necessary
    var state = getState (fileName);

    // store data in well defined ephemeral file to allow manual recovery
    // If file already exists, remove it (failed prior recovery).
    if (state.ephemeral.exists) {
        fs.unlinkSync (state.ephemeral.name);
    }

    writeFileSync (state.ephemeral.name, data, options);
    state.ephemeral.exists = true;

    // if ready state file already exists, recover prior state
    if (state.ready.exists) {
        performRecovery (state, false);
    }
    
    fs.renameSync (state.ephemeral.name, state.ready.name);

    // refresh state and process recovery to set file system state
    state = getState (fileName);
    performRecovery (state, true);
}
function safeWriteFileSync ( fileName , data , options ) { verifyFileName ( fileName ) ; // if fileName exists, verify it is a file var info = getFileInfo ( fileName ) ; if ( ( info . exists ) && ( info . isFile === false ) ) { var message = format ( cc . MSG_IS_NOT_A_FILE , fileName ) ; throw new SafeFileError ( cc . IS_NOT_A_FILE , message ) ; } // get current file system state, and auto-recover if necessary var state = getState ( fileName ) ; // store data in well defined ephemeral file to allow manual recovery // If file already exists, remove it (failed prior recovery). if ( state . ephemeral . exists ) { fs . unlinkSync ( state . ephemeral . name ) ; } writeFileSync ( state . ephemeral . name , data , options ) ; state . ephemeral . exists = true ; // if ready state file already exists, recover prior state if ( state . ready . exists ) { performRecovery ( state , false ) ; } fs . renameSync ( state . ephemeral . name , state . ready . name ) ; // refresh state and process recovery to set file system state state = getState ( fileName ) ; performRecovery ( state , true ) ; }
Write data to a file using a recoverable process.
@param {String} fileName Name of file (path optional).
@param {String} data Data to write.
@throws SafeFileError
Write data to a file using a recoverable process .