Skip to content

Commit

Permalink
Merge branch '5.6.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
martinknafve committed Apr 28, 2016
2 parents 1c9c33e + b8e45c5 commit 9b60c8b
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 16 deletions.
2 changes: 1 addition & 1 deletion hmailserver/source/Server/COM/InterfaceDomain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ STDMETHODIMP InterfaceDomain::Save()
return S_OK;
}

return COMError::GenerateError(sErrorMessage);
return COMError::GenerateError("Failed to save object. " + sErrorMessage);
}
catch (...)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ namespace HM

// Since the files are now compressed, we can deleted
// the data backup directory
if (!FileUtilities::DeleteDirectory(sDataBackupDir))
if (!FileUtilities::DeleteDirectory(sDataBackupDir, true))
{
Application::Instance()->GetBackupManager()->OnBackupFailed("Could not delete files from the destination directory.");
return false;
Expand Down Expand Up @@ -383,7 +383,7 @@ namespace HM
{
// The temporary directory we created while
// unzipping should be deleted now.
FileUtilities::DeleteDirectory(sExtractedFilesDirectory);
FileUtilities::DeleteDirectory(sExtractedFilesDirectory, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace HM
}

// We were successful in copying the data directory for the domain in question. Now drop the old structure.
FileUtilities::DeleteDirectory(oldDirectory);
FileUtilities::DeleteDirectory(oldDirectory, true);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ namespace HM
bool bRet = Application::Instance()->GetDBManager()->Execute(deleteCommand);

// Delete folder from data directory
String sAccountFolder = IniFileSettings::Instance()->GetDataDirectory() + "\\" + StringParser::ExtractDomain(pAccount->GetAddress()) + "\\" + StringParser::ExtractAddress(pAccount->GetAddress());
FileUtilities::DeleteDirectory(sAccountFolder);
String sDomainName = StringParser::ExtractDomain(pAccount->GetAddress());
String sMailbox = StringParser::ExtractAddress(pAccount->GetAddress());

if (!sDomainName.IsEmpty() && !sMailbox.IsEmpty())
{
String sAccountFolder = IniFileSettings::Instance()->GetDataDirectory() + "\\" + sDomainName + "\\" + sMailbox;
FileUtilities::DeleteDirectory(sAccountFolder, false);
}

// Refresh caches.
Cache<Account>::Instance()->RemoveObject(pAccount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ namespace HM

SQLCommand command("delete from hm_domains where domainid = @DOMAINID");
command.AddParameter("@DOMAINID", iDomainID);

if (!Application::Instance()->GetDBManager()->Execute(command))
return false;

// Refresh the BO cache
CacheContainer::Instance()->RemoveDomain(pDomain);

// Delete folder from data directory
String sDomainFolder = IniFileSettings::Instance()->GetDataDirectory() + "\\" + pDomain->GetName();
FileUtilities::DeleteDirectory(sDomainFolder);
if (!pDomain->GetName().IsEmpty())
{
String sDomainFolder = IniFileSettings::Instance()->GetDataDirectory() + "\\" + pDomain->GetName();
FileUtilities::DeleteDirectory(sDomainFolder, false);
}

return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ namespace HM
return false;
}

if (!StringParser::IsValidEmailAddress(account->GetAddress()))
{
resultDescription = "The account address is not a valid email address.";
return false;
}

std::shared_ptr<Domain> domain = GetDomain(account->GetDomainID());

Expand Down
46 changes: 44 additions & 2 deletions hmailserver/source/Server/Common/Util/FileUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,24 @@ namespace HM
}

bool
FileUtilities::DeleteDirectory(const String &sDirName)
FileUtilities::DeleteDirectory(const String &sDirName, bool force)
{
boost::system::error_code error_code;
if (!force)
{
if (!boost::filesystem::is_directory(sDirName))
{
// The directory is already gone.
return true;
}

if (GetDirectoryContainsFileRecursive(sDirName))
{
// Directory is not empty.
return false;
}
}

boost::system::error_code error_code;
boost::filesystem::remove_all(sDirName, error_code);

if (error_code)
Expand Down Expand Up @@ -452,6 +466,34 @@ namespace HM

}

bool
FileUtilities::GetDirectoryContainsFileRecursive(const String &sDirectoryName)
{
if (!boost::filesystem::is_directory(sDirectoryName))
return false;

std::vector<FileInfo> result;

for (boost::filesystem::directory_iterator file(sDirectoryName); file != boost::filesystem::directory_iterator(); ++file)
{
boost::filesystem::path current(file->path());

if(boost::filesystem::is_directory(current))
{
auto result = GetDirectoryContainsFileRecursive(current.wstring().c_str());

if (result)
return true;
}
else
{
return true;
}
}

return false;
}


bool
FileUtilities::DeleteDirectoriesInDirectory(const String &sDirName)
Expand Down
4 changes: 2 additions & 2 deletions hmailserver/source/Server/Common/Util/FileUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ namespace HM
static bool CreateDirectory(const String &sName);

static bool CopyDirectory(String sFrom, String sTo, String &errorMessage);
static bool DeleteDirectory(const String &sDirName);
static bool DeleteDirectory(const String &sDirName, bool force);
static bool DeleteFilesInDirectory(const String &sDirName);
static bool DeleteDirectoriesInDirectory(const String &sDirName);

static std::vector<FileInfo> GetFilesInDirectory(const String &sDirectoryName, const String &regularExpressionTest);

static bool GetDirectoryContainsFileRecursive(const String &sDirectoryName);
static bool IsUNCPath(const String &sPath);
static bool IsValidUNCFolder(const String &sPath);
static bool IsFullPath(const String &sPath);
Expand Down
7 changes: 6 additions & 1 deletion hmailserver/source/Server/hMailServer/hMailServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ ServiceMain(DWORD /*dwArgc*/, LPTSTR* /*lpszArgv*/)
// Then it immediately calls the SetServiceStatus function to notify the
// service control manager that its status is SERVICE_START_PENDING.
// Tell SCM that we have started.
ReportServiceStatus(SERVICE_RUNNING, SERVICE_ACCEPT_STOP);
ReportServiceStatus(SERVICE_RUNNING, SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);

// In Windows 2000/XP, we need to register objects directly
// after service start. Otherwise it won't work. In later
Expand Down Expand Up @@ -409,7 +409,12 @@ ServiceController (DWORD Opcode)
case SERVICE_CONTROL_CONTINUE:
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_SHUTDOWN:
LOG_DEBUG("Received shutdown-request from Windows.");
TerminateServer();
break;
case SERVICE_CONTROL_STOP:
LOG_DEBUG("Received stop-request from Windows.");
TerminateServer();
// --- Send message to the main thread to quit.
return;
Expand Down
2 changes: 1 addition & 1 deletion hmailserver/test/RegressionTests/API/Links.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Links : TestFixtureBase
public void TestAccountLink()
{
Account account =
SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test", "test");
SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "test@test.com", "test");

Assert.AreEqual(account.Address, _links.get_Account(account.ID).Address);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public void TestAccountContainingBackwardSlash()
Assert.Fail("Account containing forward slash was permitted");
}

[Test]
public void TestAccountWithoutAddress()
{
var exception = Assert.Throws<COMException>(() => SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "", "secret2"));
Assert.AreEqual("Failed to save object. The account address is not a valid email address.", exception.Message);
}

[Test]
public void TestAccountContainingForwardSlash()
{
Expand Down Expand Up @@ -282,6 +289,13 @@ public void TestDomainNameDuplicate()
Assert.Fail("Duplicate domain name was permitted.");
}

[Test]
public void TestDomainWithoutName()
{
var exception = Assert.Throws<COMException>(() => SingletonProvider<TestSetup>.Instance.AddDomain(""));

Assert.AreEqual("Failed to save object. The domain name you have entered is not a valid domain name.", exception.Message);
}

[Test]
public void TestDomainNameDuplicateDomainRename()
Expand Down

0 comments on commit 9b60c8b

Please sign in to comment.