docs: function mkfile(path, options, realCallback) {
          var meta = {};
          var called;
          var callback = function (err) {
              if (called) {
                  if (err) {
                      if (meta.stream) meta.stream.emit("error", err);
                      else console.error(err.stack);
                  }
                  else if (meta.stream) meta.stream.emit("saved");
                  return;
              }
              called = true;
              return realCallback(err, meta);
          };
  
          if (options.stream && !options.stream.readable) {
              return callback(new TypeError("options.stream must be readable."));
          }
  
          // Pause the input for now since we're not ready to write quite yet
          var readable = options.stream;
          if (readable) {
              if (readable.pause) readable.pause();
              var buffer = [];
              readable.on("data", onData);
              readable.on("end", onEnd);
          }
  
          function onData(chunk) {
              buffer.push(["data", chunk]);
          }
          function onEnd() {
              buffer.push(["end"]);
          }
          function error(err) {
              resume();
              if (tempPath) {
                  fs.unlink(tempPath, callback.bind(null, err));
              }
              else
                  return callback(err);
          }
          
          function resume() {
              if (readable) {
                  // Stop buffering events and playback anything that happened.
                  readable.removeListener("data", onData);
                  readable.removeListener("end", onEnd);
  
                  buffer.forEach(function (event) {
                      readable.emit.apply(readable, event);
                  });
                  // Resume the input stream if possible
                  if (readable.resume) readable.resume();
              }
          }
          
          var tempPath;
          var resolvedPath = "";
  
          mkdir();
  
          function mkdir() {
              if (options.parents) {
                  mkdirP(dirname(path), {}, function(err) {
                      if (err) return error(err);
                      resolve();
                  });
              }
              else {
                  resolve();
              }
          }
  
          // Make sure the user has access to the directory and get the real path.
          function resolve() {
              resolvePath(path, function (err, _resolvedPath) {
                  if (err) {
                      if (err.code !== "ENOENT") {
                          return error(err);
                      }
                      // If checkSymlinks is on we'll get an ENOENT when creating a new file.
                      // In that case, just resolve the parent path and go from there.
                      resolvePath(dirname(path), function (err, dir) {
                          if (err) return error(err);
                          resolvedPath = join(dir, basename(path));
                          createTempFile();
                      });
                      return;
                  }
                  
                  resolvedPath = _resolvedPath;
                  createTempFile();
              });
          }
          
          
          function createTempFile() {
              tempPath = tmpFile(dirname(resolvedPath), "." + basename(resolvedPath) + "-", "~");
              
              var mode = options.mode || umask & 0666;
              fs.stat(resolvedPath, function(err, stat) {
                  if (err && err.code !== "ENOENT") return error(err);
                  
                  var uid = process.getuid ? process.getuid() : 0;
                  var gid = process.getgid ? process.getgid() : 0;
                  
                  if (stat) {
                      mode = stat.mode & 0777;
                      uid = stat.uid;
                      gid = stat.gid;
                  }
  
                  // node 0.8.x adds a "wx" shortcut, but since it's not in 0.6.x we use the
                  // longhand here.
                  var flags = constants.O_CREAT | constants.O_WRONLY | constants.O_EXCL;
                  fs.open(tempPath, flags, mode, function (err, fd) {
                      if (err) return error(err);
                      
                      fs.fchown(fd, uid, gid, function(err) {
                          fs.close(fd);
                          if (err) return error(err);
                          
                          pipe(fs.WriteStream(tempPath, {
                              encoding: options.encoding || null,
                              mode: mode
                          }));
                      });
                  });
              });
          }
  
          function pipe(writable) {
              var hadError;
              
              if (readable) {
                  readable.pipe(writable);
              }
              else {
                  writable.on('open', function () {
                      if (hadError) return;
                      meta.stream = writable;
                      callback();
                  });
              }
              writable.on('error', function (err) {
                  hadError = true;
                  error(err);
              });
              writable.on('close', function () {
                  if (hadError) return;
                  swap();
              });
  
              resume();
          }
          
          function swap() {
              fs.rename(tempPath, resolvedPath, function (err) {
                  if (err) return error(err);
                  callback();
              });
          }
      }