Skip to content

Commit

Permalink
ZP-1626 . Re-added SafeGetContents. Released under the Affero GNU Gen…
Browse files Browse the repository at this point in the history
…eral Public License (AGPL) version 3.
  • Loading branch information
Michele Lunardi committed Jun 11, 2021
1 parent f313344 commit 5badeab
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib/utils/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,35 @@ public static function CheckAndFixEncodingInHeaders(&$mail, $message) {
$message->headers["from"] = Utils::convertRawHeader2Utf8($rawheaders["from"], $message->headers["from"]);
}

/**
* Tries to load the content of a file from disk with retries in case of file system returns an empty file.
*
* @param $filename
* $filename is the name of the file to be opened
*
* @param $functName
* $functName is the name of the caller function. Usefull to be printed into the log file
*
* @param $suppressWarnings
* $suppressWarnings boolean. True if file_get_contents function has to be called with suppress warnings enabled, False otherwise
*
* @access private
* @return string
*/
public static function SafeGetContents($filename, $functName, $suppressWarnings) {
$attempts = (defined('FILE_STATE_ATTEMPTS') ? FILE_STATE_ATTEMPTS : 3);
$sleep_time = (defined('FILE_STATE_SLEEP') ? FILE_STATE_SLEEP : 100);
$i = 1;
while (($i <= $attempts) && (($filecontents = ($suppressWarnings ? @file_get_contents($filename) : file_get_contents($filename))) === '')) {
ZLog::Write(LOGLEVEL_WARN, sprintf("FileStateMachine->%s(): Failed on reading filename '%s' - attempt: %d", $functName, $filename, $i));
$i++;
usleep($sleep_time * 1000);
}
if ($i > $attempts)
ZLog::Write(LOGLEVEL_FATAL, sprintf("FileStateMachine->%s(): Unable to read filename '%s' after %d retries",$functName, $filename, --$i));

return $filecontents;
}
/**
* Tries to load the content of a file from disk with retries in case of file system returns an empty file.
* In case of non empty files it tries to unserialize them.
Expand Down

0 comments on commit 5badeab

Please sign in to comment.