function writeFile(path, data, options, callback) {
        var encoding = options.encoding || "utf8",
            preserveBOM = options.preserveBOM;

        function _finishWrite(created) {
            appshell.fs.writeFile(path, data, encoding, preserveBOM, function (err) {
                if (err) {
                    callback(_mapError(err));
                } else {
                    stat(path, function (err, stat) {
                        callback(err, stat, created);
                    });
                }
            });
        }

        stat(path, function (err, stats) {
            if (err) {
                switch (err) {
                case FileSystemError.NOT_FOUND:
                    _finishWrite(true);
                    break;
                default:
                    callback(err);
                }
                return;
            }

            if (options.hasOwnProperty("expectedHash") && options.expectedHash !== stats._hash) {
                console.error("Blind write attempted: ", path, stats._hash, options.expectedHash);

                if (options.hasOwnProperty("expectedContents")) {
                    appshell.fs.readFile(path, encoding, function (_err, _data) {
                        if (_err || _data !== options.expectedContents) {
                            callback(FileSystemError.CONTENTS_MODIFIED);
                            return;
                        }

                        _finishWrite(false);
                    });
                    return;
                } else {
                    callback(FileSystemError.CONTENTS_MODIFIED);
                    return;
                }
            }

            _finishWrite(false);
        });
    }
Write data to the file at the given path, calling back asynchronously with
either a FileSystemError string or the FileSystemStats object associated
with the written file and a boolean that indicates whether the file was
created by the write (true) or not (false). If no file exists at the
given path, a new file will be created. The options parameter can be used
to specify an encoding (default "utf8"), an octal mode (default
unspecified and implementation dependent), and a consistency hash, which
is used to the current state of the file before overwriting it. If a
consistency hash is provided but does not match the hash of the file on
disk, a FileSystemError.CONTENTS_MODIFIED error is passed to the callback.

@param {string} path
@param {string} data
@param {{encoding : string=, mode : number=, expectedHash : object=, expectedContents : string=}} options
@param {function(?string, FileSystemStats=, boolean)} callback