From 14914fce4441c919e702328c6891e191c53cf25b Mon Sep 17 00:00:00 2001 From: Bobselp Date: Mon, 23 Sep 2019 21:08:16 +0200 Subject: [PATCH] Get mailbox size with maildirsize file --- lib/Froxlor/Cron/System/MailboxsizeCron.php | 49 +++++++++++++-------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/Froxlor/Cron/System/MailboxsizeCron.php b/lib/Froxlor/Cron/System/MailboxsizeCron.php index 154b5685..65749b6a 100644 --- a/lib/Froxlor/Cron/System/MailboxsizeCron.php +++ b/lib/Froxlor/Cron/System/MailboxsizeCron.php @@ -38,26 +38,37 @@ class MailboxsizeCron extends \Froxlor\Cron\FroxlorCron $_maildir = \Froxlor\FileDir::makeCorrectDir($maildir['maildirpath']); if (file_exists($_maildir) && is_dir($_maildir)) { - // mail-address allows many special characters, see http://en.wikipedia.org/wiki/Email_address#Local_part - $return = false; - $back = \Froxlor\FileDir::safe_exec('du -sk ' . escapeshellarg($_maildir), $return, array( - '|', - '&', - '`', - '$', - '~', - '?' - )); - foreach ($back as $backrow) { - $emailusage = explode(' ', $backrow); + // 1. Check if maildirsize exists (usually when quota is enabled) + $maildirsize = $_maildir.'maildirsize'; + if (file_exists($maildirsize)) { + \Froxlor\FroxlorLogger::getInstanceOf()->logAction(\Froxlor\FroxlorLogger::CRON_ACTION, LOG_NOTICE, 'found maildirsize file in ' . $_maildir); + $file = file($maildirsize, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + if (!empty($file[1])) { + $emailusage = floatval(explode(' ', $file[1])[0]); + } + } else { + // 2. If maildirsize file does not exist, compute with du + // mail-address allows many special characters, see http://en.wikipedia.org/wiki/Email_address#Local_part + $return = false; + $back = \Froxlor\FileDir::safe_exec('du -sk ' . escapeshellarg($_maildir), $return, array( + '|', + '&', + '`', + '$', + '~', + '?' + )); + foreach ($back as $backrow) { + $emailusage = explode(' ', $backrow); + } + $emailusage = floatval($emailusage['0']); + + // as freebsd does not have the -b flag for 'du' which gives + // the size in bytes, we use "-sk" for all and calculate from KiB + $emailusage *= 1024; + + unset($back); } - $emailusage = floatval($emailusage['0']); - - // as freebsd does not have the -b flag for 'du' which gives - // the size in bytes, we use "-sk" for all and calculate from KiB - $emailusage *= 1024; - - unset($back); \Froxlor\Database\Database::pexecute($upd_stmt, array( 'size' => $emailusage, 'id' => $maildir['id']