summaryrefslogtreecommitdiff
path: root/bin/bbackupquery/bbackupquery.cpp
diff options
context:
space:
mode:
authorReinhard Tartler <siretart@tauware.de>2008-01-19 15:08:54 +0100
committerReinhard Tartler <siretart@tauware.de>2008-01-19 15:08:54 +0100
commit2733267954e91e394fbb512ea3abb4c497c0752f (patch)
treed6cdebd8776bceba06a2fb5e4ed06a4744bc1b57 /bin/bbackupquery/bbackupquery.cpp
parent1d56581c644c53f1b9a182c6574bc2fc5243d4d1 (diff)
import version 0.11rc1
This commit has been made by 'bzr import'. I used the upstream tarball of Version 0.11rc1 for creating it. It has the md5sum: 75608d8bb72dff9a556850ccd0ae8cb9
Diffstat (limited to 'bin/bbackupquery/bbackupquery.cpp')
-rw-r--r--bin/bbackupquery/bbackupquery.cpp147
1 files changed, 106 insertions, 41 deletions
diff --git a/bin/bbackupquery/bbackupquery.cpp b/bin/bbackupquery/bbackupquery.cpp
index 1bd15f3c..2006f3d3 100644
--- a/bin/bbackupquery/bbackupquery.cpp
+++ b/bin/bbackupquery/bbackupquery.cpp
@@ -1,4 +1,4 @@
-// distribution boxbackup-0.10 (svn version: 494)
+// distribution boxbackup-0.11rc1 (svn version: 2023_2024)
//
// Copyright (c) 2003 - 2006
// Ben Summers and contributors. All rights reserved.
@@ -50,8 +50,14 @@
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
+
+#include <errno.h>
#include <stdio.h>
-#include <sys/types.h>
+
+#ifdef HAVE_SYS_TYPES_H
+ #include <sys/types.h>
+#endif
+
#ifdef HAVE_LIBREADLINE
#ifdef HAVE_READLINE_READLINE_H
#include <readline/readline.h>
@@ -83,6 +89,7 @@
#include "FdGetLine.h"
#include "BackupClientCryptoKeys.h"
#include "BannerText.h"
+#include "Logging.h"
#include "MemLeakFindOn.h"
@@ -101,7 +108,11 @@ void PrintUsageAndExit()
int main(int argc, const char *argv[])
{
- MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbackupquery.memleaks", "bbackupquery")
+ int returnCode = 0;
+
+ MAINHELPER_SETUP_MEMORY_LEAK_EXIT_REPORT("bbackupquery.memleaks",
+ "bbackupquery")
+ MAINHELPER_START
#ifdef WIN32
WSADATA info;
@@ -111,7 +122,7 @@ int main(int argc, const char *argv[])
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
@@ -121,24 +132,34 @@ int main(int argc, const char *argv[])
BoxDebugTraceOn = false;
#endif
- int returnCode = 0;
-
- MAINHELPER_START
-
FILE *logFile = 0;
- // Filename for configuraiton file?
- const char *configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG;
+ // Filename for configuration file?
+ std::string configFilename;
+
+ #ifdef WIN32
+ configFilename = BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE;
+ #else
+ configFilename = BOX_FILE_BBACKUPD_DEFAULT_CONFIG;
+ #endif
// Flags
bool quiet = false;
bool readWrite = false;
+ Logging::SetProgramName("Box Backup (bbackupquery)");
+
+ #ifdef NDEBUG
+ int masterLevel = Log::NOTICE; // need an int to do math with
+ #else
+ int masterLevel = Log::INFO; // need an int to do math with
+ #endif
+
#ifdef WIN32
- const char* validOpts = "qwuc:l:";
+ const char* validOpts = "qvwuc:l:";
bool unicodeConsole = false;
#else
- const char* validOpts = "qwc:l:";
+ const char* validOpts = "qvwc:l:";
#endif
// See if there's another entry on the command line
@@ -147,11 +168,35 @@ int main(int argc, const char *argv[])
{
switch(c)
{
- case 'q':
- // Quiet mode
- quiet = true;
+ case 'q':
+ {
+ // Quiet mode
+ quiet = true;
+
+ if(masterLevel == Log::NOTHING)
+ {
+ BOX_FATAL("Too many '-q': "
+ "Cannot reduce logging "
+ "level any more");
+ return 2;
+ }
+ masterLevel--;
+ }
break;
-
+
+ case 'v':
+ {
+ if(masterLevel == Log::EVERYTHING)
+ {
+ BOX_FATAL("Too many '-v': "
+ "Cannot increase logging "
+ "level any more");
+ return 2;
+ }
+ masterLevel++;
+ }
+ break;
+
case 'w':
// Read/write mode
readWrite = true;
@@ -167,7 +212,8 @@ int main(int argc, const char *argv[])
logFile = ::fopen(optarg, "w");
if(logFile == 0)
{
- printf("Can't open log file '%s'\n", optarg);
+ BOX_ERROR("Failed to open log file '" <<
+ optarg << "': " << strerror(errno));
}
break;
@@ -186,11 +232,13 @@ int main(int argc, const char *argv[])
argc -= optind;
argv += optind;
+ Logging::SetGlobalLevel((Log::Level)masterLevel);
+
// Print banner?
if(!quiet)
{
const char *banner = BANNER_TEXT("Backup Query Tool");
- printf(banner);
+ BOX_NOTICE(banner);
}
#ifdef WIN32
@@ -198,18 +246,19 @@ int main(int argc, const char *argv[])
{
if (!SetConsoleCP(CP_UTF8))
{
- fprintf(stderr, "Failed to set input codepage: "
- "error %d\n", GetLastError());
+ BOX_ERROR("Failed to set input codepage: " <<
+ GetErrorMessage(GetLastError()));
}
if (!SetConsoleOutputCP(CP_UTF8))
{
- fprintf(stderr, "Failed to set output codepage: "
- "error %d\n", GetLastError());
+ BOX_ERROR("Failed to set output codepage: " <<
+ GetErrorMessage(GetLastError()));
}
// enable input of Unicode characters
- if (_setmode(_fileno(stdin), _O_TEXT) == -1)
+ if (_fileno(stdin) != -1 &&
+ _setmode(_fileno(stdin), _O_TEXT) == -1)
{
perror("Failed to set the console input to "
"binary mode");
@@ -218,12 +267,16 @@ int main(int argc, const char *argv[])
#endif // WIN32
// Read in the configuration file
- if(!quiet) printf("Using configuration file %s\n", configFilename);
+ if(!quiet) BOX_INFO("Using configuration file " << configFilename);
+
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());
+ BOX_FATAL("Invalid configuration file: " << errs);
return 1;
}
// Easier coding
@@ -243,12 +296,12 @@ int main(int argc, const char *argv[])
BackupClientCryptoKeys_Setup(conf.GetKeyValue("KeysFile").c_str());
// 2. Connect to server
- if(!quiet) printf("Connecting to store...\n");
+ if(!quiet) BOX_INFO("Connecting to store...");
SocketStreamTLS socket;
socket.Open(tlsContext, Socket::TypeINET, conf.GetKeyValue("StoreHostname").c_str(), BOX_PORT_BBSTORED);
// 3. Make a protocol, and handshake
- if(!quiet) printf("Handshake with store...\n");
+ if(!quiet) BOX_INFO("Handshake with store...");
BackupProtocolClient connection(socket);
connection.Handshake();
@@ -259,7 +312,7 @@ int main(int argc, const char *argv[])
}
// 4. Log in to server
- if(!quiet) printf("Login to store...\n");
+ if(!quiet) BOX_INFO("Login to store...");
// Check the version of the server
{
std::auto_ptr<BackupProtocolClientVersion> serverVersion(connection.QueryVersion(BACKUP_STORE_SERVER_VERSION));
@@ -283,12 +336,21 @@ int main(int argc, const char *argv[])
int c = 0;
while(c < argc && !context.Stop())
{
- context.DoCommand(argv[c++]);
+ context.DoCommand(argv[c++], true);
}
}
// Get commands from input
+
#ifdef HAVE_LIBREADLINE
+ // Must initialise the locale before using editline's readline(),
+ // otherwise cannot enter international characters.
+ if (setlocale(LC_ALL, "") == NULL)
+ {
+ BOX_ERROR("Failed to initialise locale. International "
+ "character support may not work.");
+ }
+
#ifdef HAVE_READLINE_HISTORY
using_history();
#endif
@@ -301,7 +363,7 @@ int main(int argc, const char *argv[])
// Ctrl-D pressed -- terminate now
break;
}
- context.DoCommand(command);
+ context.DoCommand(command, false);
if(last_cmd != 0 && ::strcmp(last_cmd, command) == 0)
{
free(command);
@@ -322,20 +384,23 @@ int main(int argc, const char *argv[])
#endif
#else
// Version for platforms which don't have readline by default
- FdGetLine getLine(fileno(stdin));
- while(!context.Stop())
+ if(fileno(stdin) >= 0)
{
- printf("query > ");
- fflush(stdout);
- std::string command(getLine.GetLine());
- context.DoCommand(command.c_str());
+ FdGetLine getLine(fileno(stdin));
+ while(!context.Stop())
+ {
+ printf("query > ");
+ fflush(stdout);
+ std::string command(getLine.GetLine());
+ context.DoCommand(command.c_str(), false);
+ }
}
#endif
// Done... stop nicely
- if(!quiet) printf("Logging off...\n");
+ if(!quiet) BOX_INFO("Logging off...");
connection.QueryFinished();
- if(!quiet) printf("Session finished.\n");
+ if(!quiet) BOX_INFO("Session finished.");
// Return code
returnCode = context.GetReturnCode();
@@ -348,13 +413,13 @@ int main(int argc, const char *argv[])
// Let everything be cleaned up on exit.
- MAINHELPER_END
-
#ifdef WIN32
// Clean up our sockets
WSACleanup();
#endif
+ MAINHELPER_END
+
return returnCode;
}