<commit_before>'use strict';

var fs = require('graceful-fs');

var streamFile = require('../../src/getContents/streamFile');
var futimes    = require('../../futimes');

function writeStream(writePath, file, cb) {
  var stat = file.stat;

  var opt = {
    mode: stat.mode,
    flag: file.flag
  };

  var outStream = fs.createWriteStream(writePath, opt);
  var outFD;

  file.contents.once('error', complete);
  outStream.once('error', complete);
  outStream.once('open', open );
  outStream.once('finish', success);

  file.contents.pipe(outStream);

  function open(fd) {
    outFD = fd;
  }

  function success() {
    streamFile(file, {}, function(error) {
      if (error) {
        return complete(error);
      }

      futimes(outFD, stat, complete);
    });
  }

  // cleanup
  function complete(err) {
    file.contents.removeListener('error', complete);
    outStream.removeListener('error', complete);
    outStream.removeListener('open', open);
    outStream.removeListener('finish', success);
    cb(err);
  }
}

module.exports = writeStream;<commit_msg>Fix: Refactor writeStream to prevent issue with futimes<commit_after>'use strict';

var fs = require('graceful-fs');

var streamFile = require('../../src/getContents/streamFile');
var futimes    = require('../../futimes');

function writeStream(writePath, file, cb) {
  var stat = file.stat;
  var outStream;
  var outFD;

  fs.open(writePath, 'w', file.stat.mode, function (err, fd) {
    if (err) {
      cb(err);
    }

    outFD = fd;
    outStream = fs.createWriteStream(null, {fd: fd});

    file.contents.once('error', complete);
    file.contents.once('end', readStreamEnd);
    outStream.once('error', complete);
    outStream.once('finish', complete);

    // Streams are piped with end disabled, this prevents the
    // WriteStream from closing the file descriptor after all
    // data is written.
    file.contents.pipe(outStream, {end: false});
  });

  function readStreamEnd() {
    streamFile(file, {}, function(error) {
      if (error) {
        return complete(error);
      }

      futimes(outFD, stat, function (error) {
        if (error) {
          return complete(error);
        }

        // All finished with WriteStream, close and clean up
        outStream.end();
      });
    });
  }

  // cleanup
  function complete(err) {
    file.contents.removeListener('error', complete);
    file.contents.removeListener('end', readStreamEnd);
    if (outStream) {
      outStream.removeListener('error', complete);
      outStream.removeListener('finish', complete);
    }
    cb(err);
  }
}

module.exports = writeStream;
{'fraction_non_alphanumeric': 0.15093618647306076, 'fraction_numerical': 0.0, 'mean_word_length': 3.090625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4538328', 'n_tokens_mistral': 811, 'n_tokens_neox': 795, 'n_words': 183}