<commit_before><?php

namespace App\Metadata;

//use RecursiveDirectoryIterator;
//use RecursiveIteratorIterator;

class DiskUsage
{
	/**
	 * Returns true if the current system is windows.
	 *
	 * @return bool
	 */
	public function is_win()
	{
		$os = strtoupper(substr(PHP_OS, 0, 3));

		return $os === 'WIN';
	}

	/**
	 * From https://www.php.net/manual/en/function.disk-total-space.php.
	 *
	 * @param $bytes
	 *
	 * @return string
	 */
	public function getSymbolByQuantity($bytes)
	{
		$symbols = array(
			'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
		);
		$exp = intval(floor(log($bytes) / log(1024)));

		return sprintf('%.2f %s', ($bytes / pow(1024, $exp)),
			$symbols[$exp]);
	}

	/**
	 * from https://stackoverflow.com/questions/478121/how-to-get-directory-size-in-php.
	 *
	 * @param $dir
	 *
	 * @return bool|false|int|string
	 */
	public function getTotalSize($dir)
	{
		$dir = rtrim(str_replace('\\', '/', $dir), '/');

		if (is_dir($dir) === true) {
			// If on a Unix Host (Linux, Mac OS)
			if (!$this->is_win()) {
				$io = popen("ls -ltrR {$dir} |awk '{print $5}'|awk 'BEGIN{sum=0} {sum=sum+$1} END {print sum}'", 'r');
				$size = fgets($io, 80);
				pclose($io);
//				$io = popen('/usr/bin/du -sk ' . $dir, 'r');
//				if ($io !== false) {
//					$size = fgets($io, 4096);
//					$size = substr($size, 0, strpos($size, "\t"));
//					pclose($io);
//					return intval($size) * 1024;
//				}
				return intval($size);
			} // If on a Windows Host (WIN32, WINNT, Windows)
			else {
				if (extension_loaded('com_dotnet')) {
					$obj = new \COM('scripting.filesystemobject');
					if (is_object($obj)) {
						$ref = $obj->getfolder($dir);
						$totalSize = $ref->size;
						$obj = null;

						return $totalSize;
					}<commit_msg>use exec instead of popen<commit_after><?php

namespace App\Metadata;

//use RecursiveDirectoryIterator;
//use RecursiveIteratorIterator;

class DiskUsage
{
	/**
	 * Returns true if the current system is windows.
	 *
	 * @return bool
	 */
	public function is_win()
	{
		$os = strtoupper(substr(PHP_OS, 0, 3));

		return $os === 'WIN';
	}

	/**
	 * From https://www.php.net/manual/en/function.disk-total-space.php.
	 *
	 * @param $bytes
	 *
	 * @return string
	 */
	public function getSymbolByQuantity($bytes)
	{
		$symbols = array(
			'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
		);
		$exp = intval(floor(log($bytes) / log(1024)));

		return sprintf('%.2f %s', ($bytes / pow(1024, $exp)),
			$symbols[$exp]);
	}

	/**
	 * from https://stackoverflow.com/questions/478121/how-to-get-directory-size-in-php.
	 *
	 * @param $dir
	 *
	 * @return bool|false|int|string
	 */
	public function getTotalSize($dir)
	{
		$dir = rtrim(str_replace('\\', '/', $dir), '/');

		if (is_dir($dir) === true) {
			// If on a Unix Host (Linux, Mac OS)
			if (!$this->is_win()) {
				$command = "ls -ltrR {$dir} |awk '{print $5}'|awk 'BEGIN{sum=0} {sum=sum+$1} END {print sum}' 2>&1";
				exec($command, $output);
				$size = $output[0] ?? 0;
//				$io = popen("ls -ltrR {$dir} |awk '{print $5}'|awk 'BEGIN{sum=0} {sum=sum+$1} END {print sum}'", 'r');
//				$size = fgets($io, 80);
//				pclose($io);
//				$io = popen('/usr/bin/du -sk ' . $dir, 'r');
//				if ($io !== false) {
//					$size = fgets($io, 4096);
//					$size = substr($size, 0, strpos($size, "\t"));
//					pclose($io);
//					return intval($size) * 1024;
//				}
				return intval($size);
			} // If on a Windows Host (WIN32, WINNT, Windows)
			else {
				if (extension_loaded('com_dotnet')) {
					$obj = new \COM('scripting.filesystemobject');
					if (is_object($obj)) {
						$ref = $obj->getfolder($dir);
						$totalSize = $ref->size;
						$obj = null;

						return $totalSize;
					}
{'fraction_non_alphanumeric': 0.22270153680237262, 'fraction_numerical': 0.019681854947425182, 'mean_word_length': 2.8686131386861313, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '3680525', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1418, 'n_words': 316}