Filesystem: capture error information from attempted lock file creation

This commit is contained in:
Dylan K. Taylor 2022-12-06 14:06:08 +00:00
parent a3306914cc
commit b5e6dec0c6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -187,9 +187,10 @@ final class Filesystem{
* @throws \InvalidArgumentException if the lock file path is invalid (e.g. parent directory doesn't exist, permission denied) * @throws \InvalidArgumentException if the lock file path is invalid (e.g. parent directory doesn't exist, permission denied)
*/ */
public static function createLockFile(string $lockFilePath) : ?int{ public static function createLockFile(string $lockFilePath) : ?int{
$resource = fopen($lockFilePath, "a+b"); try{
if($resource === false){ $resource = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => fopen($lockFilePath, "a+b"));
throw new \InvalidArgumentException("Invalid lock file path or read/write permissions denied"); }catch(\ErrorException $e){
throw new \InvalidArgumentException("Failed to open lock file: " . $e->getMessage(), 0, $e);
} }
if(!flock($resource, LOCK_EX | LOCK_NB)){ if(!flock($resource, LOCK_EX | LOCK_NB)){
//wait for a shared lock to avoid race conditions if two servers started at the same time - this makes sure the //wait for a shared lock to avoid race conditions if two servers started at the same time - this makes sure the