Skip to content

Commit 55d2b1f

Browse files
committed
Changelog excerpt: - Fixed a bottleneck in the scan process caused by the readFileBlocks method.
1 parent 4a12611 commit 55d2b1f

File tree

3 files changed

+33
-46
lines changed

3 files changed

+33
-46
lines changed

Changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,5 @@ __*Why "v3.0.0" instead of "v1.0.0?"*__ Prior to phpMussel v3, the "phpMussel Co
7777
[2022.02.21; Maikuolan]: Added a default caching prefix.
7878

7979
[2022.03.11; Bug-fix; Maikuolan]: Shorthand data was quoted, but needed to be defined as literals in order to avoid being unentitised prior to being parsed as regular expression partials, in order to avoid breaking those expressions and potentially triggering fatal errors; Fixed.
80+
81+
[2022.03.24; Bug-fix; Maikuolan]: Fixed a bottleneck in the scan process caused by the readFileBlocks method (phpMussel/phpMussel#231).

src/Loader.php

+16-37
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The loader (last modified: 2022.02.21).
11+
* This file: The loader (last modified: 2022.03.24).
1212
*/
1313

1414
namespace phpMussel\Core;
@@ -169,7 +169,7 @@ class Loader
169169
private $Channels = [];
170170

171171
/**
172-
* @var int The default blocksize for readFileBlocks and readFileBlocksGZ.
172+
* @var int The default blocksize for readFileContent and readFileContentGZ.
173173
*/
174174
private $Blocksize = 131072;
175175

@@ -337,7 +337,7 @@ public function __construct(
337337
$this->Request = new \Maikuolan\Common\Request();
338338
$this->Request->DefaultTimeout = $this->Configuration['core']['default_timeout'];
339339
$ChannelsDataArray = [];
340-
$this->YAML->process($this->readFileBlocks($this->AssetsPath . 'channels.yaml'), $ChannelsDataArray);
340+
$this->YAML->process($this->readFileContent($this->AssetsPath . 'channels.yaml'), $ChannelsDataArray);
341341
$this->Request->Channels = $ChannelsDataArray ?: [];
342342
unset($ChannelsDataArray);
343343
if (!isset($this->Request->Channels['Triggers'])) {
@@ -850,58 +850,37 @@ public function substrAfterLast(string $h, string $n): string
850850
}
851851

852852
/**
853-
* Returns the contents of files.
853+
* Reads and returns the contents of files.
854854
*
855-
* @param string $File The file to read.
856-
* @param int $BlocksToRead The number of blocks to read from the file.
857-
* @return string The file's contents (an empty string on failure).
855+
* @param string $File The path and the name of the file to read.
856+
* @return string The file's content, or an empty string on failure.
858857
*/
859-
public function readFileBlocks(string $File, int $BlocksToRead = 0): string
858+
public function readFileContent(string $File): string
860859
{
861860
/** Guard. */
862-
if (!is_file($File) || !is_readable($File) || !$Filesize = filesize($File)) {
861+
if (!strlen($File) || !is_file($File) || !is_readable($File)) {
863862
return '';
864863
}
865864

866-
/** Calculate this file's blocks to read. */
867-
if (!$BlocksToRead) {
868-
$BlocksToRead = ($Filesize && $this->Blocksize) ? ceil($Filesize / $this->Blocksize) : 0;
869-
}
870-
871-
$Data = '';
872-
if ($BlocksToRead > 0) {
873-
$Handle = fopen($File, 'rb');
874-
if (!is_resource($Handle)) {
875-
return '';
876-
}
877-
$Done = 0;
878-
while ($Done < $BlocksToRead) {
879-
$Data .= fread($Handle, $this->Blocksize);
880-
$Done++;
881-
}
882-
fclose($Handle);
883-
}
884-
return $Data;
865+
$Data = file_get_contents($File);
866+
return is_string($Data) ? $Data : '';
885867
}
886868

887869
/**
888-
* Returns the contents of GZ-compressed files.
870+
* Reads and returns the contents of GZ-compressed files.
889871
*
890872
* @param string $File The file to read.
891-
* @param int $BlocksToRead The number of blocks to read from the file.
892-
* @return string The file's contents (an empty string on failure).
873+
* @return string The file's content, or an empty string on failure.
893874
*/
894-
public function readFileBlocksGZ(string $File, int $BlocksToRead = 0): string
875+
public function readFileContentGZ(string $File): string
895876
{
896877
/** Guard. */
897-
if (!is_file($File) || !is_readable($File) || !$Filesize = filesize($File)) {
878+
if (!strlen($File) || !is_file($File) || !is_readable($File) || !$Filesize = filesize($File)) {
898879
return '';
899880
}
900881

901882
/** Calculate this file's blocks to read. */
902-
if (!$BlocksToRead) {
903-
$BlocksToRead = ($Filesize && $this->Blocksize) ? ceil($Filesize / $this->Blocksize) : 0;
904-
}
883+
$BlocksToRead = ($Filesize && $this->Blocksize) ? ceil($Filesize / $this->Blocksize) : 0;
905884

906885
$Data = '';
907886
if ($BlocksToRead > 0) {
@@ -982,7 +961,7 @@ public function arrayify(&$Input): void
982961
public function gZCompressFile(string $File): bool
983962
{
984963
/** Guard. */
985-
if (!is_file($File) || !is_readable($File)|| !$Filesize = filesize($File)) {
964+
if (!strlen($File) || !is_file($File) || !is_readable($File)) {
986965
return false;
987966
}
988967

src/Scanner.php

+15-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The scanner (last modified: 2022.02.01).
11+
* This file: The scanner (last modified: 2022.03.24).
1212
*/
1313

1414
namespace phpMussel\Core;
@@ -839,10 +839,15 @@ private function recursor($Files = '', int $Depth = -1): void
839839
}
840840

841841
/** Read in the file to be scanned. */
842-
$In = $this->Loader->readFileBlocks($Files, (
843-
$this->Loader->Configuration['files']['scannable_threshold'] > 0 &&
844-
$fS > $this->Loader->readBytes($this->Loader->Configuration['files']['scannable_threshold'])
845-
) ? $this->Loader->readBytes($this->Loader->Configuration['files']['scannable_threshold']) : $fS, true);
842+
$In = $this->Loader->readFileContent($Files);
843+
844+
/** Enforce scannable threshold. */
845+
if (
846+
($ScannableThreshold = $this->Loader->readBytes($this->Loader->Configuration['files']['scannable_threshold'])) > 0 &&
847+
strlen($In) > $ScannableThreshold
848+
) {
849+
$In = substr($In, 0, $ScannableThreshold);
850+
}
846851

847852
/** Generate CRC for the file to be scanned. */
848853
$fdCRC = hash('crc32b', $In);
@@ -1146,11 +1151,12 @@ private function dataHandler(string $str = '', int $Depth = 0, string $OriginalF
11461151
$this->statsIncrement('API-Scanned', 1);
11471152
}
11481153

1154+
/** Enforce scannable threshold. */
11491155
if (
1150-
$this->Loader->Configuration['files']['scannable_threshold'] > 0 &&
1151-
$StringLength > $this->Loader->readBytes($this->Loader->Configuration['files']['scannable_threshold'])
1156+
($ScannableThreshold = $this->Loader->readBytes($this->Loader->Configuration['files']['scannable_threshold'])) > 0 &&
1157+
$StringLength > $ScannableThreshold
11521158
) {
1153-
$StringLength = $this->Loader->readBytes($this->Loader->Configuration['files']['scannable_threshold']);
1159+
$StringLength = $ScannableThreshold;
11541160
$str = substr($str, 0, $StringLength);
11551161
$str_cut = 1;
11561162
} else {
@@ -1740,7 +1746,7 @@ private function dataHandler(string $str = '', int $Depth = 0, string $OriginalF
17401746
continue;
17411747
}
17421748
if (!isset($this->Loader->InstanceCache[$SigFile])) {
1743-
$this->Loader->InstanceCache[$SigFile] = $this->Loader->readFileBlocks($this->Loader->SignaturesPath . $SigFile);
1749+
$this->Loader->InstanceCache[$SigFile] = $this->Loader->readFileContent($this->Loader->SignaturesPath . $SigFile);
17441750
}
17451751

17461752
/** Fire event: "beforeSigFile". */

0 commit comments

Comments
 (0)