/**
 * Copy a file.
 * @function lib.file.copyFile
 * @param {string} src - File path to copy from.
 * @param {string} dest - File path to copy to.
 * @author Taka Okunishi
 *
 */
var fs = require('fs');
exports = module.exports = function (src, dest, callback) {
    var readStream = fs.createReadStream(src),
        writeStream = fs.createWriteStream(dest);

    var errorHandler = exports._errorHandler(callback);
    readStream.on('error', errorHandler);
    writeStream.on('error', errorHandler);
    writeStream.on('close', function () {
        callback(null);
    });
    readStream.pipe(writeStream);
};

exports._errorHandler = function (callback) {
    return function (err) {
        callback && callback(err);
        callback = null;
    };
};