From b71cc1c68969d20c1f6cfacf553a4e4d34c03d1e Mon Sep 17 00:00:00 2001 From: Michael Usher Date: Wed, 17 Oct 2018 19:51:07 +1100 Subject: [PATCH] #32221 Set permissions on log file creation instead of every write. (#32222) * Set permissions on log file creation instead of every write. * Code style fix and removed unnecessary else condition --- lib/private/Log/Owncloud.php | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/private/Log/Owncloud.php b/lib/private/Log/Owncloud.php index da972100d820..fb728d1da59b 100644 --- a/lib/private/Log/Owncloud.php +++ b/lib/private/Log/Owncloud.php @@ -51,14 +51,8 @@ public static function init() { * Fall back to default log file if specified logfile does not exist * and can not be created. */ - if (!\file_exists(self::$logFile)) { - if (!\is_writable(\dirname(self::$logFile))) { - self::$logFile = $defaultLogFile; - } else { - if (!\touch(self::$logFile)) { - self::$logFile = $defaultLogFile; - } - } + if (!self::createLogFile(self::$logFile)) { + self::$logFile = $defaultLogFile; } } @@ -125,11 +119,11 @@ public static function writeExtra($app, $message, $level, $conditionalLogFile, $ if ($conditionalLogFile[0] !== '/') { $conditionalLogFile = \OC::$server->getConfig()->getSystemValue('datadirectory') . "/" . $conditionalLogFile; } + self::createLogFile($conditionalLogFile); $handle = @\fopen($conditionalLogFile, 'a'); - @\chmod($conditionalLogFile, 0640); } else { + self::createLogFile(self::$logFile); $handle = @\fopen(self::$logFile, 'a'); - @\chmod(self::$logFile, 0640); } if ($handle) { \fwrite($handle, $entry."\n"); @@ -143,6 +137,22 @@ public static function writeExtra($app, $message, $level, $conditionalLogFile, $ } } + /** + * create a log file and chmod it to the correct permissions + * @param string $logFile + * @return boolean + */ + public static function createLogFile($logFile) { + if (\file_exists($logFile)) { + return true; + } + if (\is_writable(\dirname($logFile)) && \touch($logFile)) { + @\chmod($logFile, 0640); + return true; + } + return false; + } + /** * @return string */