Skip to content

Commit

Permalink
Optimization when creating dirs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Sep 3, 2017
1 parent 8a205ea commit 68df1a5
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/ObjectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ private function createFileDirIfNotExists($filename)
} elseif (is_file($pathinfo['dirname'])) {
return false;
} else {
return mkdir($pathinfo['dirname'], 0777, true);
return $this->createDirIfNotExists($pathinfo['dirname']);
}
}
return false;
Expand All @@ -847,8 +847,21 @@ private function createFileDirIfNotExists($filename)
private function createDirIfNotExists($dir)
{
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
try {
set_error_handler(function($errno, $errstr, $errfile, $errline) {
restore_error_handler();
throw new \IvoPetkov\ObjectStorage\ErrorException($errstr, 0, $errno, $errfile, $errline);
});
$result = mkdir($dir, 0777, true);
restore_error_handler();
return $result;
} catch (\IvoPetkov\ObjectStorage\ErrorException $e) {
if ($e->getMessage() !== 'mkdir(): File exists') { // The directory may be just created in other process.
throw $e;
}
}
}
return true;
}

/**
Expand Down

0 comments on commit 68df1a5

Please sign in to comment.