handle edge cases with file locking: file needs to exist before it can be locked, fixes #803

This commit is contained in:
El RIDO 2021-06-05 05:48:17 +02:00
parent ffe48092fe
commit edb8e5e078
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92

View file

@ -90,12 +90,15 @@ abstract class AbstractPersistence
} }
$file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess'; $file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
if (!is_file($file)) { if (!is_file($file)) {
$writtenBytes = @file_put_contents( $writtenBytes = 0;
$file, if ($fileCreated = @touch($file)) {
'Require all denied' . PHP_EOL, $writtenBytes = @file_put_contents(
LOCK_EX $file,
); 'Require all denied' . PHP_EOL,
if ($writtenBytes === false || $writtenBytes < 19) { LOCK_EX
);
}
if ($fileCreated === false || $writtenBytes === false || $writtenBytes < 19) {
throw new Exception('unable to write to file ' . $file, 11); throw new Exception('unable to write to file ' . $file, 11);
} }
} }
@ -114,9 +117,16 @@ abstract class AbstractPersistence
protected static function _store($filename, $data) protected static function _store($filename, $data)
{ {
self::_initialize(); self::_initialize();
$file = self::$_path . DIRECTORY_SEPARATOR . $filename; $file = self::$_path . DIRECTORY_SEPARATOR . $filename;
$writtenBytes = @file_put_contents($file, $data, LOCK_EX); $fileCreated = true;
if ($writtenBytes === false || $writtenBytes < strlen($data)) { $writtenBytes = 0;
if (!is_file($file)) {
$fileCreated = @touch($file);
}
if ($fileCreated) {
$writtenBytes = @file_put_contents($file, $data, LOCK_EX);
}
if ($fileCreated === false || $writtenBytes === false || $writtenBytes < strlen($data)) {
throw new Exception('unable to write to file ' . $file, 13); throw new Exception('unable to write to file ' . $file, 13);
} }
@chmod($file, 0640); // protect file access @chmod($file, 0640); // protect file access