<commit_before>
'use strict';

/**
 * Kalabox disk utility module.
 * @module kbox.util.disk
 */

// Native modules
var os = require('os');

// Npm modules
var diskspace = require('diskspace');
var fs = require('fs-extra');
var Promise = require('bluebird');

// Kalabox modules
var deps = require('../core/deps.js');
var shell = require('./shell.js');

/**
 * Gets the UUID of the Max OSX volume
 * @arg {boolean} callback.uuid - UUID of the Volume
 * @example
 * return kbox.util.disk.getMacVolume()
 *
 * .then(function(uuid) {
 *   return uuid;
 * })
 */
exports.getMacVolume = function() {

  // Set up our OSX command to check this
  var cmd = 'diskutil info /';

  // Run our command to check if we are blocking all ports
  return Promise.fromNode(function(cb) {
    shell.exec(cmd, cb);
  })

  // Return true if blocked all is disabled
  .then(function(data) {
    return data;
  });

};

/**
 * Gets the temp directory and creates it if needed
 * @arg {boolean} callback.uuid - UUID of the Volume
 * @example
 * var downloadDir = kbox.util.disk.getTempDir();
 */
exports.getTempDir = function() {
  return deps.call(function(globalConfig) {
    var dir = globalConfig.downloadsRoot;
    if (!fs.existsSync(dir)) {
      fs.mkdirpSync(dir);
    }
    return dir;
  });
};

/*
 * Return a result from a diskspace.check
 */
var getDiskProp = function(prop) {

  // Choose a drive based on platform
  // @todo: can you not have a C drive on windows?
  var volume = (process.platform === 'win32') ? 'C' : '/';

  // Get some info about our disk
  return Promise.fromNode(function(cb) {
    diskspace.check(volume, cb);
  })

  // Return the piece we want
  .then(function(data) {

    // Return the status
    if (prop === 'status') {
      return data[2];
    }

    // Or the free space
    else if (prop === 'free') {
      return data[1];
    }

    // Or the total space
    else {
      return data[0];
    }

  })

  // Catch any errors
  .catch(function(err) {
    throw new Error(err);
  });
};

/**
 * Gets the FREE space on the volume
 * @arg {boolean} callback.freeSpace - freespace on the Volume
 * @example
 * return kbox.util.disk.getDiskFreeSpace()<commit_msg>#447: Need to trim disk status reply for windows compat<commit_after>
'use strict';

/**
 * Kalabox disk utility module.
 * @module kbox.util.disk
 */

// Native modules
var os = require('os');

// Npm modules
var diskspace = require('diskspace');
var fs = require('fs-extra');
var Promise = require('bluebird');

// Kalabox modules
var deps = require('../core/deps.js');
var shell = require('./shell.js');

/**
 * Gets the UUID of the Max OSX volume
 * @arg {boolean} callback.uuid - UUID of the Volume
 * @example
 * return kbox.util.disk.getMacVolume()
 *
 * .then(function(uuid) {
 *   return uuid;
 * })
 */
exports.getMacVolume = function() {

  // Set up our OSX command to check this
  var cmd = 'diskutil info /';

  // Run our command to check if we are blocking all ports
  return Promise.fromNode(function(cb) {
    shell.exec(cmd, cb);
  })

  // Return true if blocked all is disabled
  .then(function(data) {
    return data;
  });

};

/**
 * Gets the temp directory and creates it if needed
 * @arg {boolean} callback.uuid - UUID of the Volume
 * @example
 * var downloadDir = kbox.util.disk.getTempDir();
 */
exports.getTempDir = function() {
  return deps.call(function(globalConfig) {
    var dir = globalConfig.downloadsRoot;
    if (!fs.existsSync(dir)) {
      fs.mkdirpSync(dir);
    }
    return dir;
  });
};

/*
 * Return a result from a diskspace.check
 */
var getDiskProp = function(prop) {

  // Choose a drive based on platform
  // @todo: can you not have a C drive on windows?
  var volume = (process.platform === 'win32') ? 'C' : '/';

  // Get some info about our disk
  return Promise.fromNode(function(cb) {
    diskspace.check(volume, cb);
  })

  // Return the piece we want
  .then(function(data) {

    // Return the status
    if (prop === 'status') {
      return data[2].trim();
    }

    // Or the free space
    else if (prop === 'free') {
      return data[1];
    }

    // Or the total space
    else {
      return data[0];
    }

  })

  // Catch any errors
  .catch(function(err) {
    throw new Error(err);
  });
};

/**
 * Gets the FREE space on the volume
 * @arg {boolean} callback.freeSpace - freespace on the Volume
 * @example
 * return kbox.util.disk.getDiskFreeSpace()
{'fraction_non_alphanumeric': 0.14486510995239174, 'fraction_numerical': 0.0029471775107685334, 'mean_word_length': 2.8001722652885443, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6670743', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1423, 'n_words': 448}