main.js
function syncToTmp(tmpfd, msgnumber, cb) {

        // Pass the last msg
        if (msgnumber > messages.offsets.length) cb();

        // Skip deleted messages
        else if (messages.offsets[msgnumber] === undefined) syncToTmp(tmpfd, msgnumber + 1, cb);
        else {

            var buffer = new Buffer(omessages.sizes[msgnumber]);

            fs.read(fd, buffer, 0, omessages.sizes[msgnumber], messages.offsets[msgnumber], function(err, bytesRead, buffer) {

                fs.write(tmpfd, buffer, 0, bytesRead, null, function(err, written, buffer) {

                    syncToTmp(tmpfd, msgnumber + 1, cb);

                });
            });
        }
    }
function syncToTmp(tmpfd, msgnumber, cb) {

        // Pass the last msg
        if (msgnumber > messages.offsets.length) cb();

        // Skip deleted messages
        else if (messages.offsets[msgnumber] === undefined) syncToTmp(tmpfd, msgnumber + 1, cb);
        else {

            var buffer = new Buffer(omessages.sizes[msgnumber]);

            fs.read(fd, buffer, 0, omessages.sizes[msgnumber], messages.offsets[msgnumber], function(err, bytesRead, buffer) {

                fs.write(tmpfd, buffer, 0, bytesRead, null, function(err, written, buffer) {

                    syncToTmp(tmpfd, msgnumber + 1, cb);

                });
            });
        }
    }
function syncToTmp ( tmpfd , msgnumber , cb ) { // Pass the last msg if ( msgnumber > messages . offsets . length ) cb ( ) ; // Skip deleted messages else if ( messages . offsets [ msgnumber ] === undefined ) syncToTmp ( tmpfd , msgnumber + 1 , cb ) ; else { var buffer = new Buffer ( omessages . sizes [ msgnumber ] ) ; fs . read ( fd , buffer , 0 , omessages . sizes [ msgnumber ] , messages . offsets [ msgnumber ] , function ( err , bytesRead , buffer ) { fs . write ( tmpfd , buffer , 0 , bytesRead , null , function ( err , written , buffer ) { syncToTmp ( tmpfd , msgnumber + 1 , cb ) ; } ) ; } ) ; } }
Private methods follow Write modifications to temp file
Private methods follow Write modifications to temp file