1
0
Fork 0

Avoid warning due to invalid cache dir during init, wait for first use, fixes #10125

pull/10158/head
Jordi Boggiano 2021-10-14 10:23:41 +02:00
parent 131d6ade5d
commit 79727d35c4
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 23 additions and 21 deletions

View File

@ -30,8 +30,8 @@ class Cache
private $io; private $io;
/** @var string */ /** @var string */
private $root; private $root;
/** @var bool */ /** @var ?bool */
private $enabled = true; private $enabled = null;
/** @var string */ /** @var string */
private $allowlist; private $allowlist;
/** @var Filesystem */ /** @var Filesystem */
@ -56,16 +56,6 @@ class Cache
if (!self::isUsable($cacheDir)) { if (!self::isUsable($cacheDir)) {
$this->enabled = false; $this->enabled = false;
return;
}
if (
(!is_dir($this->root) && !Silencer::call('mkdir', $this->root, 0777, true))
|| !is_writable($this->root)
) {
$this->io->writeError('<warning>Cannot create cache directory ' . $this->root . ', or directory is not writable. Proceeding without cache</warning>');
$this->enabled = false;
} }
} }
@ -92,6 +82,18 @@ class Cache
public function isEnabled() public function isEnabled()
{ {
if ($this->enabled === null) {
$this->enabled = true;
if (
(!is_dir($this->root) && !Silencer::call('mkdir', $this->root, 0777, true))
|| !is_writable($this->root)
) {
$this->io->writeError('<warning>Cannot create cache directory ' . $this->root . ', or directory is not writable. Proceeding without cache</warning>');
$this->enabled = false;
}
}
return $this->enabled; return $this->enabled;
} }
@ -102,7 +104,7 @@ class Cache
public function read($file) public function read($file)
{ {
if ($this->enabled) { if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file); $file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) { if (file_exists($this->root . $file)) {
$this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG); $this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG);
@ -116,7 +118,7 @@ class Cache
public function write($file, $contents) public function write($file, $contents)
{ {
if ($this->enabled && !$this->readOnly) { if ($this->isEnabled() && !$this->readOnly) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file); $file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
$this->io->writeError('Writing '.$this->root . $file.' into cache', true, IOInterface::DEBUG); $this->io->writeError('Writing '.$this->root . $file.' into cache', true, IOInterface::DEBUG);
@ -155,7 +157,7 @@ class Cache
*/ */
public function copyFrom($file, $source) public function copyFrom($file, $source)
{ {
if ($this->enabled && !$this->readOnly) { if ($this->isEnabled() && !$this->readOnly) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file); $file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
$this->filesystem->ensureDirectoryExists(dirname($this->root . $file)); $this->filesystem->ensureDirectoryExists(dirname($this->root . $file));
@ -176,7 +178,7 @@ class Cache
*/ */
public function copyTo($file, $target) public function copyTo($file, $target)
{ {
if ($this->enabled) { if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file); $file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) { if (file_exists($this->root . $file)) {
try { try {
@ -216,7 +218,7 @@ class Cache
public function remove($file) public function remove($file)
{ {
if ($this->enabled) { if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file); $file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) { if (file_exists($this->root . $file)) {
return $this->filesystem->unlink($this->root . $file); return $this->filesystem->unlink($this->root . $file);
@ -228,7 +230,7 @@ class Cache
public function clear() public function clear()
{ {
if ($this->enabled) { if ($this->isEnabled()) {
$this->filesystem->emptyDirectory($this->root); $this->filesystem->emptyDirectory($this->root);
return true; return true;
@ -239,7 +241,7 @@ class Cache
public function gc($ttl, $maxSize) public function gc($ttl, $maxSize)
{ {
if ($this->enabled) { if ($this->isEnabled()) {
$expire = new \DateTime(); $expire = new \DateTime();
$expire->modify('-'.$ttl.' seconds'); $expire->modify('-'.$ttl.' seconds');
@ -269,7 +271,7 @@ class Cache
public function sha1($file) public function sha1($file)
{ {
if ($this->enabled) { if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file); $file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) { if (file_exists($this->root . $file)) {
return sha1_file($this->root . $file); return sha1_file($this->root . $file);
@ -281,7 +283,7 @@ class Cache
public function sha256($file) public function sha256($file)
{ {
if ($this->enabled) { if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file); $file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) { if (file_exists($this->root . $file)) {
return hash_file('sha256', $this->root . $file); return hash_file('sha256', $this->root . $file);