lib/safeFile.js
function performRecovery (state, removeEphemeral) {
    // if ephemeral flag true, and ephemeral file exists, remove it
    if ((removeEphemeral) && (state.ephemeral.exists)) {
        fs.unlinkSync (state.ephemeral.name);
    }

    // if only backups exist, restore from backup
    var baseAvailable = state.base.exists || state.ready.exists;
    if (baseAvailable === false) {
        if (state.tertiary.exists) {
            if (state.backup.exists) {
                fs.renameSync (state.backup.name, state.base.name);
                fs.renameSync (state.tertiary.name, state.backup.name);
            } else {
                fs.renameSync (state.tertiary.name, state.base.name);
            }
        } else if (state.backup.exists) {
            fs.renameSync (state.backup.name, state.base.name);
        }
        
        return;
    }

    // if tertiary state file exists, remove it
    if (state.tertiary.exists) {
        fs.unlinkSync (state.tertiary.name);
    }

    // if ready state file exists, update ready, base and backup files
    if (state.ready.exists) {
        var removeTertiary = false;
        

        // if base and backup exist, rename to tertiary temporarily
        if ((state.base.exists) && (state.backup.exists)) {
            fs.renameSync (state.backup.name, state.tertiary.name);
            removeTertiary = true;
        }

        // if base exists, rename to backup
        if (state.base.exists) {
            fs.renameSync (state.base.name, state.backup.name);
        }

        // place ready state file in base and delete temporary tertiary file
        fs.renameSync (state.ready.name, state.base.name);

        // if temporary tertiary created, remove it
        if (removeTertiary) {
            fs.unlinkSync (state.tertiary.name);
        }
    }
}
function performRecovery (state, removeEphemeral) {
    // if ephemeral flag true, and ephemeral file exists, remove it
    if ((removeEphemeral) && (state.ephemeral.exists)) {
        fs.unlinkSync (state.ephemeral.name);
    }

    // if only backups exist, restore from backup
    var baseAvailable = state.base.exists || state.ready.exists;
    if (baseAvailable === false) {
        if (state.tertiary.exists) {
            if (state.backup.exists) {
                fs.renameSync (state.backup.name, state.base.name);
                fs.renameSync (state.tertiary.name, state.backup.name);
            } else {
                fs.renameSync (state.tertiary.name, state.base.name);
            }
        } else if (state.backup.exists) {
            fs.renameSync (state.backup.name, state.base.name);
        }
        
        return;
    }

    // if tertiary state file exists, remove it
    if (state.tertiary.exists) {
        fs.unlinkSync (state.tertiary.name);
    }

    // if ready state file exists, update ready, base and backup files
    if (state.ready.exists) {
        var removeTertiary = false;
        

        // if base and backup exist, rename to tertiary temporarily
        if ((state.base.exists) && (state.backup.exists)) {
            fs.renameSync (state.backup.name, state.tertiary.name);
            removeTertiary = true;
        }

        // if base exists, rename to backup
        if (state.base.exists) {
            fs.renameSync (state.base.name, state.backup.name);
        }

        // place ready state file in base and delete temporary tertiary file
        fs.renameSync (state.ready.name, state.base.name);

        // if temporary tertiary created, remove it
        if (removeTertiary) {
            fs.unlinkSync (state.tertiary.name);
        }
    }
}
function performRecovery ( state , removeEphemeral ) { // if ephemeral flag true, and ephemeral file exists, remove it if ( ( removeEphemeral ) && ( state . ephemeral . exists ) ) { fs . unlinkSync ( state . ephemeral . name ) ; } // if only backups exist, restore from backup var baseAvailable = state . base . exists || state . ready . exists ; if ( baseAvailable === false ) { if ( state . tertiary . exists ) { if ( state . backup . exists ) { fs . renameSync ( state . backup . name , state . base . name ) ; fs . renameSync ( state . tertiary . name , state . backup . name ) ; } else { fs . renameSync ( state . tertiary . name , state . base . name ) ; } } else if ( state . backup . exists ) { fs . renameSync ( state . backup . name , state . base . name ) ; } return ; } // if tertiary state file exists, remove it if ( state . tertiary . exists ) { fs . unlinkSync ( state . tertiary . name ) ; } // if ready state file exists, update ready, base and backup files if ( state . ready . exists ) { var removeTertiary = false ; // if base and backup exist, rename to tertiary temporarily if ( ( state . base . exists ) && ( state . backup . exists ) ) { fs . renameSync ( state . backup . name , state . tertiary . name ) ; removeTertiary = true ; } // if base exists, rename to backup if ( state . base . exists ) { fs . renameSync ( state . base . name , state . backup . name ) ; } // place ready state file in base and delete temporary tertiary file fs . renameSync ( state . ready . name , state . base . name ) ; // if temporary tertiary created, remove it if ( removeTertiary ) { fs . unlinkSync ( state . tertiary . name ) ; } } }
Evaluate save state, initiating recovery if necessary.
@param {Object} state State object with file names and existence flags
Evaluate save state initiating recovery if necessary .