On 8/30/04 2:22 PM Pacific Time, Joao Prado Maia (jpm@stripped) wrote:
> Since the locks directory is needed in order for Eventum to be able to
> send emails out, I think we need to add a warning a missing locks
> directory. Similar to how the check for the "templates_c" directory is
> being done now.
This locking issues are fixed in the attached patch. Also included is proper
error handling as it is done throughout the rest of Eventum. Finally, the
setup script is updated with this patch to check for the existence of the
/misc/locks directory.
-Clay
Note: I hand over copyright of the code included in this patch to MySQL AB
for use in the Eventum Issue Tracking System project.
--
Killersoft.com
Attachment: [application/applefile] locks_dir.patch.txt
Index: misc/download_emails.php
===================================================================
--- misc/download_emails.php (revision 355)
+++ misc/download_emails.php (working copy)
@@ -28,6 +28,7 @@
// @(#) $Id: s.download_emails.php 1.4 03/04/15 14:50:39-00:00 jpm $
//
include("../config.inc.php");
+include_once(APP_INC_PATH . "class.error_handler.php");
include_once(APP_INC_PATH . "class.support.php");
include_once(APP_INC_PATH . "class.setup.php");
include_once(APP_INC_PATH . "db_access.php");
@@ -36,60 +37,78 @@
// check for the required parameters
if (@count($HTTP_SERVER_VARS['argv']) < 3 && @$HTTP_SERVER_VARS['argv'][1] != '--fix-lock') {
- echo "Error: Wrong number of parameters given. Expected parameters related to the email account:\n";
- echo " 1 - username\n";
- echo " 2 - hostname\n";
- echo " 3 - mailbox (only required if IMAP account)\n";
- echo "Example: php -q download_emails.php user example.com INBOX\n";
+ $error_msg = "Error: Wrong number of parameters given. Expected parameters related to the email account:\n";
+ $error_msg .= " 1 - username\n";
+ $error_msg .= " 2 - hostname\n";
+ $error_msg .= " 3 - mailbox (only required if IMAP account)\n";
+ $error_msg .= "Example: php -q download_emails.php user example.com INBOX\n";
+ $error_msg .= '(' . join(' ', $HTTP_SERVER_VARS['argv']) . ")\n";
+ Error_Handler::logError($error_msg, __FILE__, __LINE__);
+ echo $error_msg;
exit;
}
// get the account ID since we need it for locking.
$account_id = Email_Account::getAccountID(@$HTTP_SERVER_VARS["argv"][1], @$HTTP_SERVER_VARS["argv"][2], @$HTTP_SERVER_VARS["argv"][3]);
if ($account_id == 0 && !in_array('--fix-lock', @$HTTP_SERVER_VARS['argv'])) {
- echo "Error: Could not find a email account with the parameter provided. Please verify your email account settings and try again.\n";
+ $error_msg = "Error: Could not find a email account with the parameters provided. Please verify your email account settings and try again.\n";
+ $error_msg .= '(' . join(' ', $HTTP_SERVER_VARS['argv']) . ")\n";
+ Error_Handler::logError($error_msg, __FILE__, __LINE__);
+ echo $error_msg;
exit;
}
+// Make sure locks directory exists
+$lock_dir = APP_PATH . "misc/locks";
+if (!is_writable($lock_dir)) {
+ $error_msg = "The $lock_dir directory does not exist or is not writable. Please correct this and try again.";
+ Error_Handler::logError($error_msg, __FILE__, __LINE__);
+ echo $error_msg;
+ exit;
+}
+
+// Lockfile for this account_id, for this script
+$lock_file = $lock_dir . '/download_emails' . $account_id . '.lock';
+
if (in_array('--fix-lock', @$HTTP_SERVER_VARS['argv'])) {
- $setup = Setup::load();
// if there is no account id, unlock all accounts
if (empty($account_id)) {
- if (!is_array($setup['downloading_emails'])) {
- $setup['downloading_emails'] = array();
- } else {
- foreach ($setup['downloading_emails'] as $key => $val) {
- $setup['downloading_emails'][$key] = 'no';
+ $d = dir($lock_dir);
+ while (false !== ($entry = $d->read())) {
+ if (substr($entry, 0, 15) == 'download_emails' && substr($entry, -5) == '.lock') {
+ unlink($lock_dir . "/$entry");
}
}
+ $d->close();
} else {
- $setup['downloading_emails'][$account_id] = 'no';
+ @unlink($lock_file);
}
- Setup::save($setup);
echo "The lock key was fixed successfully.\n";
exit;
}
// check if there is another instance of this script already running
-$setup = Setup::load();
-if (isset($setup['downloading_emails'][$account_id]) && @$setup['downloading_emails'][$account_id] == 'yes') {
- echo "Error: Another instance of the script is still running for the specified account. " .
+if (file_exists($lock_file)) {
+ $error_msg = "Error: Another instance of the script is still running for the specified account. " .
"If this is not accurate, you may fix it by running this script with '--fix-lock' " .
"as the 4th parameter or you may unlock ALL accounts by running this script with '--fix-lock' " .
"as the only parameter.\n";
+ $error_msg .= '(' . join(' ', $HTTP_SERVER_VARS['argv']) . ")\n";
+ Error_Handler::logError($error_msg, __FILE__, __LINE__);
+ echo $error_msg;
exit;
} else {
- if (!is_array($setup['downloading_emails'])) {
- $setup['downloading_emails'] = array();
- }
- $setup['downloading_emails'][$account_id] = 'yes';
- Setup::save($setup);
+ touch($lock_file);
}
$account = Email_Account::getDetails($account_id);
$mbox = Support::connectEmailServer($account);
if ($mbox == false) {
- echo "Error: Could not connect to the email server. Please verify your email account settings and try again.\n";
+ $error_msg = "Error: Could not connect to the email server. Please verify your email account settings and try again.\n";
+ $error_msg .= '(' . join(' ', $HTTP_SERVER_VARS['argv']) . ")\n";
+ Error_Handler::logError($error_msg, __FILE__, __LINE__);
+ echo $error_msg;
+ @unlink($lock_file);
exit;
} else {
$total_emails = Support::getTotalEmails($mbox);
@@ -102,7 +121,5 @@
}
// clear the "lock" key
-$setup = Setup::load();
-$setup['downloading_emails'][$account_id] = 'no';
-Setup::save($setup);
+@unlink($lock_file);
?>
Index: setup/index.php
===================================================================
--- setup/index.php (revision 355)
+++ setup/index.php (working copy)
@@ -119,6 +119,10 @@
if (!empty($error)) {
$errors[] = $error;
}
+ $error = checkPermissions('../misc/locks', "Directory 'misc/locks'", TRUE);
+ if (!empty($error)) {
+ $errors[] = $error;
+ }
$error = checkPermissions('../config.inc.php', "File 'config.inc.php'");
if (!empty($error)) {
$errors[] = $error;