diff --git a/backup/util/loggers/file_logger.class.php b/backup/util/loggers/file_logger.class.php index 98ff74acf39..97ae661339a 100644 --- a/backup/util/loggers/file_logger.class.php +++ b/backup/util/loggers/file_logger.class.php @@ -50,11 +50,18 @@ class file_logger extends base_logger { } public function __destruct() { - @fclose($this->fhandle); // Blindy close the file handler (no exceptions in destruct) + if (is_resource($this->fhandle)) { + // Blindy close the file handler (no exceptions in destruct). + @fclose($this->fhandle); + } } public function __sleep() { - @fclose($this->fhandle); // Blindy close the file handler before serialization + if (is_resource($this->fhandle)) { + // Blindy close the file handler before serialization. + @fclose($this->fhandle); + $this->fhandle = null; + } return array('level', 'showdate', 'showlevel', 'next', 'fullpath'); } @@ -90,7 +97,7 @@ class file_logger extends base_logger { } else { $content = $prefix . str_repeat('  ', $depth) . htmlentities($message, ENT_QUOTES, 'UTF-8') . '
' . PHP_EOL; } - if (false === fwrite($this->fhandle, $content)) { + if (!is_resource($this->fhandle) || (false === fwrite($this->fhandle, $content))) { throw new base_logger_exception('error_writing_file', $this->fullpath); } return true; diff --git a/cache/stores/file/lib.php b/cache/stores/file/lib.php index dd5e05825e4..0c08710d551 100644 --- a/cache/stores/file/lib.php +++ b/cache/stores/file/lib.php @@ -399,7 +399,7 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i public function delete($key) { $filename = $key.'.cache'; $file = $this->file_path_for_key($key); - if (@unlink($file)) { + if (file_exists($file) && @unlink($file)) { unset($this->keys[$filename]); return true; } diff --git a/lib/classes/task/file_temp_cleanup_task.php b/lib/classes/task/file_temp_cleanup_task.php index 05bfd5cccbd..d4971dc84d3 100644 --- a/lib/classes/task/file_temp_cleanup_task.php +++ b/lib/classes/task/file_temp_cleanup_task.php @@ -90,7 +90,11 @@ class file_temp_cleanup_task extends scheduled_task { } else { // Return the time modified to the original date only for real files. if ($iter->isDir() && !$iter->isDot()) { - touch($node, $modifieddateobject[$node]); + try { + @touch($node, $modifieddateobject[$node]); + } catch (\Throwable $t) { + null; + } } } } diff --git a/lib/filestorage/file_system_filedir.php b/lib/filestorage/file_system_filedir.php index 103dcca41e5..8c26da3b6dd 100644 --- a/lib/filestorage/file_system_filedir.php +++ b/lib/filestorage/file_system_filedir.php @@ -378,7 +378,9 @@ class file_system_filedir extends file_system { // Let's try to prevent some race conditions. $prev = ignore_user_abort(true); - @unlink($hashfile.'.tmp'); + if (file_exists($hashfile.'.tmp')) { + @unlink($hashfile.'.tmp'); + } if (!copy($pathname, $hashfile.'.tmp')) { // Borked permissions or out of disk space. @unlink($hashfile.'.tmp'); @@ -393,7 +395,10 @@ class file_system_filedir extends file_system { } rename($hashfile.'.tmp', $hashfile); chmod($hashfile, $this->filepermissions); // Fix permissions if needed. - @unlink($hashfile.'.tmp'); // Just in case anything fails in a weird way. + if (file_exists($hashfile.'.tmp')) { + // Just in case anything fails in a weird way. + @unlink($hashfile.'.tmp'); + } ignore_user_abort($prev); return array($contenthash, $filesize, $newfile); @@ -467,7 +472,10 @@ class file_system_filedir extends file_system { } rename($hashfile.'.tmp', $hashfile); chmod($hashfile, $this->filepermissions); // Fix permissions if needed. - @unlink($hashfile.'.tmp'); // Just in case anything fails in a weird way. + if (file_exists($hashfile.'.tmp')) { + // Just in case anything fails in a weird way. + @unlink($hashfile.'.tmp'); + } ignore_user_abort($prev); return array($contenthash, $filesize, $newfile);