Skip to content

Commit

Permalink
Better resource guarding.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maikuolan committed Sep 18, 2023
1 parent 1bcd6ee commit 7c745cf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ __*Why "v3.0.0" instead of "v1.0.0?"*__ Prior to phpMussel v3, the "phpMussel Co
[2023.09.04; Maikuolan]: Added colouration to phpMussel's CLI mode (some code has been added to the core to facilitate this). The atHit method has been migrated from the Loader class to the Scanner class.

[2023.09.16~18; Maikuolan]: Significantly refactored all L10N data.

[2023.09.18; Maikuolan]: Better resource guarding.
28 changes: 10 additions & 18 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* License: GNU/GPLv2
* @see LICENSE.txt
*
* This file: The loader (last modified: 2023.09.17).
* This file: The loader (last modified: 2023.09.18).
*/

namespace phpMussel\Core;
Expand Down Expand Up @@ -410,12 +410,12 @@ public function __construct(
$WriteMode = 'ab';
$Data = $this->InstanceCache['PendingErrorLogData'];
}
$Handle = fopen($File, $WriteMode);
if (is_resource($Handle)) {
fwrite($Handle, $Data);
fclose($Handle);
$this->logRotation($this->Configuration['core']['error_log']);
if (!is_resource($Handle = fopen($File, $WriteMode))) {
return false;
}
fwrite($Handle, $Data);
fclose($Handle);
$this->logRotation($this->Configuration['core']['error_log']);
return true;
});

Expand Down Expand Up @@ -476,8 +476,7 @@ public function readFile(string $File): string
return '';
}

$Handle = fopen($File, 'rb');
if (!is_resource($Handle)) {
if (!is_resource($Handle = fopen($File, 'rb'))) {
return '';
}
$Data = fread($Handle, $Filesize);
Expand Down Expand Up @@ -960,8 +959,7 @@ public function readFileContentGZ(string $File): string

$Data = '';
if ($BlocksToRead > 0) {
$Handle = gzopen($File, 'rb');
if (!is_resource($Handle)) {
if (!is_resource($Handle = gzopen($File, 'rb'))) {
return '';
}
$Done = 0;
Expand Down Expand Up @@ -1041,12 +1039,7 @@ public function gZCompressFile(string $File): bool
return false;
}

$Handle = fopen($File, 'rb');
if (!is_resource($Handle)) {
return false;
}
$HandleGZ = gzopen($File . '.gz', 'wb');
if (!is_resource($HandleGZ)) {
if (!is_resource($Handle = fopen($File, 'rb')) || !is_resource($HandleGZ = gzopen($File . '.gz', 'wb'))) {
return false;
}
while (!feof($Handle)) {
Expand Down Expand Up @@ -1237,8 +1230,7 @@ public function updateConfiguration(): bool
} else {
return false;
}
$Handle = fopen($this->ConfigurationPath, 'wb');
if (!is_resource($Handle)) {
if (!is_resource($Handle = fopen($this->ConfigurationPath, 'wb'))) {
return false;
}
$Err = fwrite($Handle, $Reconstructed);
Expand Down
12 changes: 9 additions & 3 deletions src/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* License: GNU/GPLv2
* @see LICENSE.txt
*
* This file: The scanner (last modified: 2023.09.04).
* This file: The scanner (last modified: 2023.09.18).
*/

namespace phpMussel\Core;
Expand Down Expand Up @@ -108,7 +108,10 @@ public function __construct(\phpMussel\Core\Loader &$Loader)
]) . "\n";
$Truncate = $this->Loader->readBytes($this->Loader->Configuration['core']['truncate']);
$WriteMode = (!file_exists($File) || ($Truncate > 0 && filesize($File) >= $Truncate)) ? 'wb' : 'ab';
$Stream = fopen($File, $WriteMode);
if (!is_resource($Stream = fopen($File, $WriteMode))) {
trigger_error('The "writeToSerialLog" event failed to open "' . $File . '" for writing.');
return false;
}
fwrite($Stream, $Data);
fclose($Stream);
$this->Loader->logRotation($this->Loader->Configuration['core']['scan_log_serialized']);
Expand Down Expand Up @@ -146,7 +149,10 @@ public function __construct(\phpMussel\Core\Loader &$Loader)
$Truncate = $this->Loader->readBytes($this->Loader->Configuration['core']['truncate']);
$WriteMode = ($Truncate > 0 && filesize($File) >= $Truncate) ? 'wb' : 'ab';
}
$Handle = fopen($File, 'ab');
if (!is_resource($Handle = fopen($File, 'ab'))) {
trigger_error('The "writeToScanLog" event failed to open "' . $File . '" for writing.');
return false;
}
fwrite($Handle, $Results);
fclose($Handle);
$this->Loader->logRotation($this->Loader->Configuration['core']['scan_log']);
Expand Down

0 comments on commit 7c745cf

Please sign in to comment.