[Box Backup-commit] COMMIT r1777 - in box/chris/merge: bin/bbackupctl bin/bbackupd bin/bbackupquery bin/bbstoreaccounts bin/bbstored lib/backupstore lib/common lib/win32 test/bbackupd

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Thu, 26 Jul 2007 23:11:03 +0100


Author: chris
Date: 2007-07-26 23:11:03 +0100 (Thu, 26 Jul 2007)
New Revision: 1777

Modified:
   box/chris/merge/bin/bbackupctl/bbackupctl.cpp
   box/chris/merge/bin/bbackupd/Win32BackupService.cpp
   box/chris/merge/bin/bbackupd/bbackupd.cpp
   box/chris/merge/bin/bbackupquery/bbackupquery.cpp
   box/chris/merge/bin/bbstoreaccounts/bbstoreaccounts.cpp
   box/chris/merge/bin/bbstored/BackupStoreDaemon.cpp
   box/chris/merge/bin/bbstored/bbstored.cpp
   box/chris/merge/lib/backupstore/BackupStoreConfigVerify.cpp
   box/chris/merge/lib/common/BoxPortsAndFiles.h
   box/chris/merge/lib/common/Guards.h
   box/chris/merge/lib/win32/emu.cpp
   box/chris/merge/lib/win32/emu.h
   box/chris/merge/test/bbackupd/testbbackupd.cpp
Log:
Make Configuration take a std::string filename instead of a char array,
in C++ style.

Add a function to get default config file paths at runtime, dependent on
the location of the executable being run.

Pass the config file name directly to Daemon::Main, instead of faking argv.

No default raid file path at compile time on Windows, depends on
executable location when run.

Determine RaidFile path at runtime if not supplied in config file
on Windows.

Don't define default locations for config files at compile time on Windows,
provide macros to determine them at runtime instead.

Make FileHandleGuard take a std::string instead of a char array, C++ style.

Determine config file location at runtime instead of hard-coding on
Windows. Thanks to Paul MacKenzie, Per Thomsen, Pete Jalajas, Stuart
Sanders, Dave Bamford and Gary for pushing me to do this. (fixes #12)

Determine config file path at runtime. Call Daemon::Main with config file
name instead of building fake argv.

(refs #3, merges [1684] [1685] [1686] [1687] [1688] [1689] [1690]
[1691] [1692])


Modified: box/chris/merge/bin/bbackupctl/bbackupctl.cpp
===================================================================
--- box/chris/merge/bin/bbackupctl/bbackupctl.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/bin/bbackupctl/bbackupctl.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -66,7 +66,13 @@
 #endif
 
 	// Filename for configuration file?
-	const char *configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG;
+	std::string configFilename;
+
+	#ifdef WIN32
+		configFilename = BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE;
+	#else
+		configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG;
+	#endif
 	
 	// Quiet?
 	bool quiet = false;
@@ -103,9 +109,14 @@
 	}
 
 	// Read in the configuration file
-	if(!quiet) printf("Using configuration file %s\n", configFilename);
+	if(!quiet) printf("Using configuration file %s\n", 
+		configFilename.c_str());
+
 	std::string errs;
-	std::auto_ptr<Configuration> config(Configuration::LoadAndVerify(configFilename, &BackupDaemonConfigVerify, errs));
+	std::auto_ptr<Configuration> config(
+		Configuration::LoadAndVerify
+			(configFilename, &BackupDaemonConfigVerify, errs));
+
 	if(config.get() == 0 || !errs.empty())
 	{
 		printf("Invalid configuration file:\n%s", errs.c_str());

Modified: box/chris/merge/bin/bbackupd/Win32BackupService.cpp
===================================================================
--- box/chris/merge/bin/bbackupd/Win32BackupService.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/bin/bbackupd/Win32BackupService.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -29,31 +29,23 @@
 
 DWORD Win32BackupService::WinService(const char* pConfigFileName)
 {
-	char exepath[MAX_PATH];
-	GetModuleFileName(NULL, exepath, sizeof(exepath));
+	DWORD ret;
 
-	std::string configfile;
-	
+	// keep MAINHELPER_START happy
+	int argc = 0;
+	char* argv[] = {NULL};
+
+	MAINHELPER_START
+
 	if (pConfigFileName != NULL)
 	{
-		configfile = pConfigFileName;
+		ret = this->Main(pConfigFileName);
 	}
 	else
 	{
-		// make the default config file name,
-		// based on the program path
-		configfile = exepath;
-		configfile = configfile.substr(0,
-			configfile.rfind(DIRECTORY_SEPARATOR_ASCHAR));
-		configfile += DIRECTORY_SEPARATOR "bbackupd.conf";
+		ret = this->Main(BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE);
 	}
 
-	const char *argv[] = {exepath, "-c", configfile.c_str()};
-	int argc = sizeof(argv) / sizeof(*argv);
-	DWORD ret;
-
-	MAINHELPER_START
-	ret = this->Main(BOX_FILE_BBACKUPD_DEFAULT_CONFIG, argc, argv);
 	MAINHELPER_END
 
 	return ret;

Modified: box/chris/merge/bin/bbackupd/bbackupd.cpp
===================================================================
--- box/chris/merge/bin/bbackupd/bbackupd.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/bin/bbackupd/bbackupd.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -89,7 +89,7 @@
 	else
 	{
 		ExitCode = gpDaemonService->Main(
-			BOX_FILE_BBACKUPD_DEFAULT_CONFIG, argc, argv);
+			BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE, argc, argv);
 	}
 
 	delete gpDaemonService;

Modified: box/chris/merge/bin/bbackupquery/bbackupquery.cpp
===================================================================
--- box/chris/merge/bin/bbackupquery/bbackupquery.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/bin/bbackupquery/bbackupquery.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -66,7 +66,8 @@
 {
 	int returnCode = 0;
 
-	MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbackupquery.memleaks", "bbackupquery")
+	MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbackupquery.memleaks",
+		"bbackupquery")
 	MAINHELPER_START
 
 #ifdef WIN32
@@ -77,7 +78,7 @@
 	
 	if (WSAStartup(0x0101, &info) == SOCKET_ERROR) 
 	{
-		// throw error?    perhaps give it its own id in the furture
+		// throw error? perhaps give it its own id in the future
 		THROW_EXCEPTION(BackupStoreException, Internal)
 	}
 #endif
@@ -90,7 +91,13 @@
 	FILE *logFile = 0;
 
 	// Filename for configuration file?
-	const char *configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG;
+	std::string configFilename;
+
+	#ifdef WIN32
+		configFilename = BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE;
+	#else
+		configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG;
+	#endif
 	
 	// Flags
 	bool quiet = false;
@@ -215,9 +222,14 @@
 #endif // WIN32
 
 	// Read in the configuration file
-	if(!quiet) printf("Using configuration file %s\n", configFilename);
+	if(!quiet) printf("Using configuration file %s\n", 
+		configFilename.c_str());
+
 	std::string errs;
-	std::auto_ptr<Configuration> config(Configuration::LoadAndVerify(configFilename, &BackupDaemonConfigVerify, errs));
+	std::auto_ptr<Configuration> config(
+		Configuration::LoadAndVerify
+			(configFilename, &BackupDaemonConfigVerify, errs));
+
 	if(config.get() == 0 || !errs.empty())
 	{
 		printf("Invalid configuration file:\n%s", errs.c_str());

Modified: box/chris/merge/bin/bbstoreaccounts/bbstoreaccounts.cpp
===================================================================
--- box/chris/merge/bin/bbstoreaccounts/bbstoreaccounts.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/bin/bbstoreaccounts/bbstoreaccounts.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -402,12 +402,19 @@
 
 int main(int argc, const char *argv[])
 {
-	MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbstoreaccounts.memleaks", "bbstoreaccounts")
+	MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbstoreaccounts.memleaks",
+		"bbstoreaccounts")
 
 	MAINHELPER_START
 
-	// Filename for configuraiton file?
-	const char *configFilename = BOX_FILE_BBSTORED_DEFAULT_CONFIG;
+	// Filename for configuration file?
+	std::string configFilename;
+
+	#ifdef WIN32
+		configFilename = BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE;
+	#else
+		configFilename = BOX_FILE_BBSTORED_DEFAULT_CONFIG;
+	#endif
 	
 	// See if there's another entry on the command line
 	int c;
@@ -431,7 +438,10 @@
 
 	// Read in the configuration file
 	std::string errs;
-	std::auto_ptr<Configuration> config(Configuration::LoadAndVerify(configFilename, &BackupConfigFileVerify, errs));
+	std::auto_ptr<Configuration> config(
+		Configuration::LoadAndVerify
+			(configFilename, &BackupConfigFileVerify, errs));
+
 	if(config.get() == 0 || !errs.empty())
 	{
 		printf("Invalid configuration file:\n%s", errs.c_str());

Modified: box/chris/merge/bin/bbstored/BackupStoreDaemon.cpp
===================================================================
--- box/chris/merge/bin/bbstored/BackupStoreDaemon.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/bin/bbstored/BackupStoreDaemon.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -132,7 +132,23 @@
 	
 	// Initialise the raid files controller
 	RaidFileController &rcontroller = RaidFileController::GetController();
-	rcontroller.Initialise(config.GetKeyValue("RaidFileConf").c_str());
+
+	std::string raidFileConfig;
+
+	#ifdef WIN32
+		if (!config.KeyExists("RaidFileConf"))
+		{
+			raidFileConfig = BOX_GET_DEFAULT_RAIDFILE_CONFIG_FILE;
+		}
+		else
+		{
+			raidFileConfig = config.GetKeyValue("RaidFileConf");
+		}
+	#else
+		raidFileConfig = config.GetKeyValue("RaidFileConf");
+	#endif
+
+	rcontroller.Initialise(raidFileConfig);
 	
 	// Load the account database
 	std::auto_ptr<BackupStoreAccountDatabase> pdb(BackupStoreAccountDatabase::Read(config.GetKeyValue("AccountDatabase").c_str()));

Modified: box/chris/merge/bin/bbstored/bbstored.cpp
===================================================================
--- box/chris/merge/bin/bbstored/bbstored.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/bin/bbstored/bbstored.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -23,7 +23,14 @@
 	Logging::ToSyslog (true);
 
 	BackupStoreDaemon daemon;
-	return daemon.Main(BOX_FILE_BBSTORED_DEFAULT_CONFIG, argc, argv);
+
+	#ifdef WIN32
+		return daemon.Main(BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE,
+			argc, argv);
+	#else
+		return daemon.Main(BOX_FILE_BBSTORED_DEFAULT_CONFIG,
+			argc, argv);
+	#endif
 	
 	MAINHELPER_END
 }

Modified: box/chris/merge/lib/backupstore/BackupStoreConfigVerify.cpp
===================================================================
--- box/chris/merge/lib/backupstore/BackupStoreConfigVerify.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/lib/backupstore/BackupStoreConfigVerify.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -35,7 +35,13 @@
 	{"AccountDatabase",	0, ConfigTest_Exists, 0},
 	{"TimeBetweenHousekeeping",	0, ConfigTest_Exists | ConfigTest_IsInt, 0},
 	{"ExtendedLogging",	"no", ConfigTest_IsBool, 0},			// make value "yes" to enable in config file
-	{"RaidFileConf",	BOX_FILE_RAIDFILE_DEFAULT_CONFIG, ConfigTest_LastEntry, 	0}
+
+	#ifdef WIN32
+		{"RaidFileConf", "", ConfigTest_LastEntry, 0}
+	#else
+		{"RaidFileConf", BOX_FILE_RAIDFILE_DEFAULT_CONFIG, ConfigTest_LastEntry, 0}
+	#endif
+
 };
 
 const ConfigurationVerify BackupConfigFileVerify =

Modified: box/chris/merge/lib/common/BoxPortsAndFiles.h
===================================================================
--- box/chris/merge/lib/common/BoxPortsAndFiles.h	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/lib/common/BoxPortsAndFiles.h	2007-07-26 22:11:03 UTC (rev 1777)
@@ -14,21 +14,28 @@
 
 
 // Backup store daemon
-#define BOX_PORT_BBSTORED					(BOX_PORT_BASE+1)
-#define BOX_FILE_BBSTORED_DEFAULT_CONFIG	"/etc/box/bbstored.conf"
+#define BOX_PORT_BBSTORED			(BOX_PORT_BASE+1)
+
 // directory within the RAIDFILE root for the backup store daemon
-#define BOX_RAIDFILE_ROOT_BBSTORED			"backup"
+#define BOX_RAIDFILE_ROOT_BBSTORED		"backup"
 
-// Backup client daemon
+// configuration file paths
 #ifdef WIN32
-#define BOX_FILE_BBACKUPD_DEFAULT_CONFIG	"C:\\Program Files\\Box Backup\\bbackupd.conf"
+	// no default config file path, use these macros to call
+	// GetDefaultConfigFilePath() instead.
+
+	#define BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE \
+		GetDefaultConfigFilePath("bbackupd.conf").c_str()
+	#define BOX_GET_DEFAULT_RAIDFILE_CONFIG_FILE \
+		GetDefaultConfigFilePath("raidfile.conf").c_str()
+	#define BOX_GET_DEFAULT_BBSTORED_CONFIG_FILE \
+		GetDefaultConfigFilePath("bbstored.conf").c_str()
 #else
 #define BOX_FILE_BBACKUPD_DEFAULT_CONFIG	"/etc/box/bbackupd.conf"
+#define BOX_FILE_RAIDFILE_DEFAULT_CONFIG	"/etc/box/raidfile.conf"
+#define BOX_FILE_BBSTORED_DEFAULT_CONFIG 	"/etc/box/bbstored.conf"
 #endif
 
-// RaidFile conf location default
-#define BOX_FILE_RAIDFILE_DEFAULT_CONFIG	"/etc/box/raidfile.conf"
-
 // Default name of the named pipe
 #define BOX_NAMED_PIPE_NAME L"\\\\.\\pipe\\boxbackup"
 

Modified: box/chris/merge/lib/common/Guards.h
===================================================================
--- box/chris/merge/lib/common/Guards.h	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/lib/common/Guards.h	2007-07-26 22:11:03 UTC (rev 1777)
@@ -32,13 +32,13 @@
 class FileHandleGuard
 {
 public:
-	FileHandleGuard(const char *filename)
-		: mOSFileHandle(::open(filename, flags, mode))
+	FileHandleGuard(const std::string& rFilename)
+		: mOSFileHandle(::open(rFilename.c_str(), flags, mode))
 	{
 		if(mOSFileHandle < 0)
 		{
 			BOX_ERROR("FileHandleGuard: failed to open file '" <<
-				filename << "': " << strerror(errno));
+				rFilename << "': " << strerror(errno));
 			THROW_EXCEPTION(CommonException, OSFileOpenError)
 		}
 	}

Modified: box/chris/merge/lib/win32/emu.cpp
===================================================================
--- box/chris/merge/lib/win32/emu.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/lib/win32/emu.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -218,9 +218,47 @@
 	}
 }
 
+// forward declaration
+char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage);
+
 // --------------------------------------------------------------------------
 //
 // Function
+//		Name:    GetDefaultConfigFilePath(std::string name)
+//		Purpose: Calculates the default configuration file name,
+//			 by using the directory location of the currently
+//			 executing program, and appending the provided name.
+//			 In case of fire, returns an empty string.
+//		Created: 26th May 2007
+//
+// --------------------------------------------------------------------------
+std::string GetDefaultConfigFilePath(const std::string& rName)
+{
+	WCHAR exePathWide[MAX_PATH];
+	GetModuleFileNameW(NULL, exePathWide, MAX_PATH-1);
+
+	char* exePathUtf8 = ConvertFromWideString(exePathWide, CP_UTF8);
+	if (exePathUtf8 == NULL)
+	{
+		return "";
+	}
+
+	std::string configfile = exePathUtf8;
+	delete [] exePathUtf8;
+	
+	// make the default config file name,
+	// based on the program path
+	configfile = configfile.substr(0,
+		configfile.rfind('\\'));
+	configfile += "\\";
+	configfile += rName;
+
+	return configfile;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
 //		Name:    ConvertToWideString
 //		Purpose: Converts a string from specified codepage to 
 //			 a wide string (WCHAR*). Returns a buffer which 
@@ -1252,15 +1290,15 @@
 	// Work out the executable file name, to register ourselves
 	// as the event source
 
-	char cmd[MAX_PATH];
-	if (GetModuleFileName(NULL, cmd, sizeof(cmd)-1) == 0)
+	WCHAR cmd[MAX_PATH];
+	DWORD len = GetModuleFileNameW(NULL, cmd, MAX_PATH);
+
+	if (len == 0)
 	{
 		::syslog(LOG_ERR, "Failed to get the program file name: %s",
 			GetErrorMessage(GetLastError()).c_str());
 		return FALSE;
 	}
-	cmd[sizeof(cmd)-1] = 0;
- 	std::string exepath(cmd);
 
 	// Create the event source as a subkey of the log. 
 
@@ -1282,12 +1320,12 @@
 
 	// Set the name of the message file. 
  
-	if (RegSetValueEx(hk,                // subkey handle 
-			 "EventMessageFile", // value name 
-			 0,                  // must be zero 
-			 REG_EXPAND_SZ,      // value type 
-			 (LPBYTE) exepath.c_str(),  // pointer to value data 
-			 (DWORD) (exepath.size()))) // data size
+	if (RegSetValueExW(hk,                 // subkey handle 
+			   L"EventMessageFile", // value name 
+			   0,                  // must be zero 
+			   REG_EXPAND_SZ,      // value type 
+			   (LPBYTE)cmd,        // pointer to value data 
+			   len*sizeof(WCHAR))) // data size
 	{
 		::syslog(LOG_ERR, "Failed to set the event message file: %s",
 			GetErrorMessage(GetLastError()).c_str());
@@ -1315,12 +1353,12 @@
  
 	// Set the category message file and number of categories.
 
-	if (RegSetValueEx(hk,                        // subkey handle 
-			  "CategoryMessageFile",     // value name 
-			  0,                         // must be zero 
-			  REG_EXPAND_SZ,             // value type 
-			  (LPBYTE) exepath.c_str(),  // pointer to value data 
-			  (DWORD) (exepath.size()))) // data size
+	if (RegSetValueExW(hk,                    // subkey handle 
+			   L"CategoryMessageFile", // value name 
+			   0,                     // must be zero 
+			   REG_EXPAND_SZ,         // value type 
+			   (LPBYTE)cmd,           // pointer to value data 
+			   len*sizeof(WCHAR)))    // data size
 	{
 		::syslog(LOG_ERR, "Failed to set the category message file: "
 			"%s", GetErrorMessage(GetLastError()).c_str());

Modified: box/chris/merge/lib/win32/emu.h
===================================================================
--- box/chris/merge/lib/win32/emu.h	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/lib/win32/emu.h	2007-07-26 22:11:03 UTC (rev 1777)
@@ -377,6 +377,10 @@
 bool ConvertUtf8ToConsole(const char* pString, std::string& rDest);
 bool ConvertConsoleToUtf8(const char* pString, std::string& rDest);
 
+// Utility function which returns a default config file name,
+// based on the path of the current executable.
+std::string GetDefaultConfigFilePath(const std::string& rName);
+
 // GetErrorMessage() returns a system error message, like strerror() 
 // but for Windows error codes.
 std::string GetErrorMessage(DWORD errorCode);

Modified: box/chris/merge/test/bbackupd/testbbackupd.cpp
===================================================================
--- box/chris/merge/test/bbackupd/testbbackupd.cpp	2007-07-26 22:08:21 UTC (rev 1776)
+++ box/chris/merge/test/bbackupd/testbbackupd.cpp	2007-07-26 22:11:03 UTC (rev 1777)
@@ -612,11 +612,8 @@
 	int own_pid = getpid();
 		
 	BackupDaemon daemon;
-	const char* fake_argv[] = { "bbackupd", "testfiles/bbackupd.conf" };
+	int result = daemon.Main("testfiles/bbackupd.conf");
 	
-	int result = daemon.Main(BOX_FILE_BBACKUPD_DEFAULT_CONFIG, 2, 
-		fake_argv);
-	
 	TEST_THAT(result == 0);
 	if (result != 0)
 	{