[Box Backup-dev] COMMIT r723 - box/chris/general/bin/bbackupctl

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Mon Aug 7 23:42:19 BST 2006


Author: chris
Date: 2006-08-07 22:42:18 +0000 (Mon, 07 Aug 2006)
New Revision: 723

Modified:
   box/chris/general/bin/bbackupctl/bbackupctl.cpp
Log:
* bbackupctl.cpp
- Changed code structure to be more readable and robust, following 
  Martin's advice.


Modified: box/chris/general/bin/bbackupctl/bbackupctl.cpp
===================================================================
--- box/chris/general/bin/bbackupctl/bbackupctl.cpp	2006-08-07 22:01:26 UTC (rev 722)
+++ box/chris/general/bin/bbackupctl/bbackupctl.cpp	2006-08-07 22:42:18 UTC (rev 723)
@@ -28,6 +28,14 @@
 
 #include "MemLeakFindOn.h"
 
+enum Command
+{
+	Default,
+	WaitForSyncStart,
+	WaitForSyncEnd,
+	SyncAndWaitForEnd,
+};
+
 void PrintUsageAndExit()
 {
 	printf("Usage: bbackupctl [-q] [-c config_file] <command>\n"
@@ -221,112 +229,134 @@
 		return 1;
 	}
 
-	// Is the command the "wait for sync to start" command?
-	bool areWaitingForSync = false;
-	bool areWaitingForSyncEnd = false;
+	Command command = Default;
+	std::string commandName(argv[0]);
 
-	if(::strcmp(argv[0], "wait-for-sync") == 0 ||
-	   ::strcmp(argv[0], "wait-for-end") == 0)
+	if (commandName == "wait-for-sync")
 	{
-		// Check that it's not in non-automatic mode, 
-		// because then it'll never start
-		if(!autoBackup)
+		command = WaitForSyncStart;
+	}
+	else if (commandName == "wait-for-end")
+	{
+		command = WaitForSyncEnd;
+	}
+	else if (commandName == "sync-and-wait")
+	{
+		command = SyncAndWaitForEnd;
+	}
+
+	switch (command)
+	{
+		case WaitForSyncStart:
+		case WaitForSyncEnd:
 		{
-			printf("ERROR: Daemon is not in automatic mode -- "
-				"sync will never start!\n");
-			return 1;
+			// Check that it's not in non-automatic mode, 
+			// because then it'll never start
+
+			if(!autoBackup)
+			{
+				printf("ERROR: Daemon is not in automatic mode -- "
+					"sync will never start!\n");
+				return 1;
+			}
+
 		}
-	
-		// Yes... set the flag so we know that 
-		// we're waiting for a sync to start/end
-		if(::strcmp(argv[0], "wait-for-sync") == 0)
+		break;
+
+		case SyncAndWaitForEnd:
 		{
-			areWaitingForSync = true;
+			// send a sync command
+			std::string cmd("force-sync\n");
+			connection.Write(cmd.c_str(), cmd.size());
+			connection.WriteAllBuffered();
+
+			if (currentState != 0)
+			{
+				printf("Waiting for current sync/error state "
+					"to finish...\n");
+			}
 		}
-		else if (::strcmp(argv[0], "wait-for-end") == 0)
-		{
-			areWaitingForSyncEnd = true;
-		}
-	}
-	else if (::strcmp(argv[0], "sync-and-wait") == 0)
-	{
-		// send a sync command
-		std::string cmd("force-sync\n");
-		connection.Write(cmd.c_str(), cmd.size());
-		connection.WriteAllBuffered();
-		areWaitingForSyncEnd = true;
+		break;
 
-		if (currentState != 0)
+		default:
 		{
-			printf("Waiting for current sync/error state "
-				"to finish...\n");
+			// Normal case, just send the command given 
+			// plus a quit command.
+			std::string cmd = commandName;
+			cmd += "\nquit\n";
+			connection.Write(cmd.c_str(), cmd.size());
 		}
 	}
-	else
-	{
-		// No? Just send the command given plus a quit command.
-		std::string cmd(argv[0]);
-		cmd += "\nquit\n";
-		connection.Write(cmd.c_str(), cmd.size());
-	}
 	
 	// Read the response
 	std::string line;
 	bool syncIsRunning = false;
+	bool finished = false;
 
-	while(!getLine.IsEOF() && getLine.GetLine(line))
+	while(!finished && !getLine.IsEOF() && getLine.GetLine(line))
 	{
-		if(areWaitingForSync)
+		switch (command)
 		{
-			// Need to wait for the state change...
-			if(line == "start-sync")
+			case WaitForSyncStart:
 			{
-				// Send a quit command to finish nicely
-				connection.Write("quit\n", 5);
-				
-				// And we're done
-				break;
-			}		
-		}
-		else if(areWaitingForSyncEnd)
-		{
-			if(line == "start-sync")
-			{
-				if (!quiet) printf("Sync started...\n");
-				syncIsRunning = true;
+				// Need to wait for the state change...
+				if(line == "start-sync")
+				{
+					// Send a quit command to finish nicely
+					connection.Write("quit\n", 5);
+					
+					// And we're done
+					finished = true;
+				}
 			}
-			else if(line == "finish-sync" && syncIsRunning)
+			break;
+
+			case WaitForSyncEnd:
+			case SyncAndWaitForEnd:
 			{
-				if (!quiet) printf("Sync finished.\n");
-				// Send a quit command to finish nicely
-				connection.Write("quit\n", 5);
-				
-				// And we're done
-				break;
+				if(line == "start-sync")
+				{
+					if (!quiet) printf("Sync started...\n");
+					syncIsRunning = true;
+				}
+				else if(line == "finish-sync")
+				{
+					if (syncIsRunning)
+					{
+						if (!quiet) printf("Sync finished.\n");
+						// Send a quit command to finish nicely
+						connection.Write("quit\n", 5);
+					
+						// And we're done
+						finished = true;
+					}
+					else
+					{
+						if (!quiet) printf("Previous sync finished.\n");
+					}
+					// daemon must still be busy
+				}
 			}
-			else if(line == "finish-sync")
+			break;
+
+			default:
 			{
-				if (!quiet) printf("Previous sync finished.\n");
-			}
-			// daemon must still be busy
-		}
-		else
-		{
-			// Is this an OK or error line?
-			if(line == "ok")
-			{
-				if(!quiet)
+				// Is this an OK or error line?
+				if(line == "ok")
 				{
-					printf("Succeeded.\n");
+					if(!quiet)
+					{
+						printf("Succeeded.\n");
+					}
+					finished = true;
 				}
-				break;
+				else if(line == "error")
+				{
+					printf("ERROR. (Check command spelling)\n");
+					returnCode = 1;
+					finished = true;
+				}
 			}
-			else if(line == "error")
-			{
-				printf("ERROR. (Check command spelling)\n");
-				returnCode = 1;
-				break;
-			}
 		}
 	}
 




More information about the Boxbackup-dev mailing list