From 3c85831742d01fd4dbf35b819038900018bb3a5d Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Thu, 9 Jul 2009 00:10:58 +0000 Subject: - Fix connection controller errors with nil strings (bindings?), fixing Issue #331 - Make the "optional" socket more optional: add a list of common socket file locations that are checked (instead of just /tmp/mysql.sock), including MAMP to address a common use case --- Source/CMMCPConnection.m | 37 +++++++++++++++++++++++++++++++++++++ Source/SPConnectionController.m | 3 +++ 2 files changed, 40 insertions(+) diff --git a/Source/CMMCPConnection.m b/Source/CMMCPConnection.m index df71ff82..0e3e085a 100644 --- a/Source/CMMCPConnection.m +++ b/Source/CMMCPConnection.m @@ -81,6 +81,9 @@ static void forcePingTimeout(int signalNumber); mConnectionFlags = kMCPConnectionDefaultOption; + if (!host) host = @""; + if (!login) login = @""; + connectionHost = [[NSString alloc] initWithString:host]; connectionLogin = [[NSString alloc] initWithString:login]; connectionPort = port; @@ -101,6 +104,12 @@ static void forcePingTimeout(int signalNumber); } mConnectionFlags = kMCPConnectionDefaultOption; + + if (!socket || ![socket length]) { + socket = [self findSocketPath]; + if (!socket) socket = @""; + } + if (!login) login = @""; connectionHost = nil; connectionLogin = [[NSString alloc] initWithString:login]; @@ -162,6 +171,8 @@ static void forcePingTimeout(int signalNumber); if (connectionKeychainName) [connectionKeychainName release], connectionKeychainName = nil; if (connectionKeychainAccount) [connectionKeychainAccount release], connectionKeychainAccount = nil; + if (!thePassword) thePassword = @""; + connectionPassword = [[NSString alloc] initWithString:thePassword]; return YES; @@ -1307,6 +1318,32 @@ static void forcePingTimeout(int signalNumber) return(!mysql_query(mConnection, "SET GLOBAL max_allowed_packet = @@global.max_allowed_packet")); } +/* + * Check some common locations for the presence of a MySQL socket file, returning + * it if successful. + */ +- (NSString *)findSocketPath +{ + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSArray *possibleSocketLocations = [NSArray arrayWithObjects: + @"/tmp/mysql.sock", // Default + @"/var/run/mysqld/mysqld.sock", // As used on Debian/Gentoo + @"/var/tmp/mysql.sock", // As used on FreeBSD + @"/var/lib/mysql/mysql.sock", // As used by Fedora + @"/opt/local/lib/mysql/mysql.sock", // Alternate fedora + @"/opt/local/var/run/mysqld/mysqld.sock", // Darwinports MySQL + @"/opt/local/var/run/mysql4/mysqld.sock", // Darwinports MySQL 4 + @"/opt/local/var/run/mysql5/mysqld.sock", // Darwinports MySQL 5 + @"/Applications/MAMP/tmp/mysql/mysql.sock", // MAMP default location + nil]; + + for (int i = 0; i < [possibleSocketLocations count]; i++) { + if ([fileManager fileExistsAtPath:[possibleSocketLocations objectAtIndex:i]]) + return [possibleSocketLocations objectAtIndex:i]; + } + + return nil; +} - (void) dealloc { diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 71f075c1..b5c53aba 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -311,6 +311,9 @@ } else if ([mySQLConnection getLastErrorID] == 1045) { // "Access denied" error errorMessage = [NSString stringWithFormat:NSLocalizedString(@"Unable to connect to host %@ because access was denied.\n\nDouble-check your username and password and ensure that access from your current location is permitted.\n\nMySQL said: %@", @"message of panel when connection to host failed due to access denied error"), [self host], [mySQLConnection getLastErrorMessage]]; [self failConnectionWithTitle:NSLocalizedString(@"Access denied!", @"connection failed due to access denied title") errorMessage:errorMessage detail:nil]; + } else if ([self type] == SP_CONNECTION_SOCKET && (![self socket] || ![[self socket] length]) && ![mySQLConnection findSocketPath]) { + errorMessage = [NSString stringWithFormat:NSLocalizedString(@"The socket file could not be found in any common location. Please supply the correct socket location.\n\nMySQL said: %@", @"message of panel when connection to socket failed because optional socket could not be found"), [mySQLConnection getLastErrorMessage]]; + [self failConnectionWithTitle:NSLocalizedString(@"Socket not found!", @"socket not found title") errorMessage:errorMessage detail:nil]; } else { errorMessage = [NSString stringWithFormat:NSLocalizedString(@"Unable to connect to host %@, or the request timed out.\n\nBe sure that the address is correct and that you have the necessary privileges, or try increasing the connection timeout (currently %i seconds).\n\nMySQL said: %@", @"message of panel when connection to host failed"), [self host], [[prefs objectForKey:@"ConnectionTimeoutValue"] intValue], [mySQLConnection getLastErrorMessage]]; [self failConnectionWithTitle:NSLocalizedString(@"Connection failed!", @"connection failed title") errorMessage:errorMessage detail:nil]; -- cgit v1.2.3