From 758e073983f793bb7baca52403fbdaacb92e86a8 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 28 Apr 2012 18:20:38 +0000 Subject: Change BackupQueries List() to use C++ streams for output. --- bin/bbackupquery/BackupQueries.cpp | 85 ++++++++++++++++++++------------------ bin/bbackupquery/BackupQueries.h | 12 +++--- 2 files changed, 51 insertions(+), 46 deletions(-) (limited to 'bin/bbackupquery') diff --git a/bin/bbackupquery/BackupQueries.cpp b/bin/bbackupquery/BackupQueries.cpp index 3a63f8b5..f88a0761 100644 --- a/bin/bbackupquery/BackupQueries.cpp +++ b/bin/bbackupquery/BackupQueries.cpp @@ -50,6 +50,7 @@ #include "SelfFlushingStream.h" #include "Utils.h" #include "autogen_BackupProtocol.h" +#include "autogen_CipherException.h" #include "MemLeakFindOn.h" @@ -355,7 +356,8 @@ static std::string GetTimeString(BackupStoreDirectory::Entry& en, // Created: 2003/10/10 // // -------------------------------------------------------------------------- -void BackupQueries::List(int64_t DirID, const std::string &rListRoot, const bool *opts, bool FirstLevel) +void BackupQueries::List(int64_t DirID, const std::string &rListRoot, + const bool *opts, bool FirstLevel, std::ostream &out) { // Generate exclude flags int16_t excludeFlags = BackupProtocolListDirectory::Flags_EXCLUDE_NOTHING; @@ -366,11 +368,11 @@ void BackupQueries::List(int64_t DirID, const std::string &rListRoot, const bool try { mrConnection.QueryListDirectory( - DirID, - BackupProtocolListDirectory::Flags_INCLUDE_EVERYTHING, - // both files and directories - excludeFlags, - true /* want attributes */); + DirID, + BackupProtocolListDirectory::Flags_INCLUDE_EVERYTHING, + // both files and directories + excludeFlags, + true /* want attributes */); } catch (std::exception &e) { @@ -385,7 +387,6 @@ void BackupQueries::List(int64_t DirID, const std::string &rListRoot, const bool return; } - // Retrieve the directory from the stream following BackupStoreDirectory dir; std::auto_ptr dirstream(mrConnection.ReceiveStream()); @@ -403,11 +404,9 @@ void BackupQueries::List(int64_t DirID, const std::string &rListRoot, const bool if(!opts[LIST_OPTION_NOOBJECTID]) { // add object ID to line -#ifdef _MSC_VER - printf("%08I64x ", (int64_t)en->GetObjectID()); -#else - printf("%08llx ", (long long)en->GetObjectID()); -#endif + out << std::hex << std::internal << std::setw(8) << + std::setfill('0') << en->GetObjectID() << + std::dec << " "; } // Flags? @@ -434,44 +433,40 @@ void BackupQueries::List(int64_t DirID, const std::string &rListRoot, const bool // terminate *(f++) = ' '; *(f++) = '\0'; - printf("%s", displayflags); + out << displayflags; if(en_flags != 0) { - printf("[ERROR: Entry has additional flags set] "); + out << "[ERROR: Entry has additional flags set] "; } } if(opts[LIST_OPTION_TIMES_UTC]) { // Show UTC times... - printf("%s ", GetTimeString(*en, false, - opts[LIST_OPTION_TIMES_ATTRIBS]).c_str()); + out << GetTimeString(*en, false, + opts[LIST_OPTION_TIMES_ATTRIBS]) << " "; } if(opts[LIST_OPTION_TIMES_LOCAL]) { // Show local times... - printf("%s ", GetTimeString(*en, true, - opts[LIST_OPTION_TIMES_ATTRIBS]).c_str()); + out << GetTimeString(*en, true, + opts[LIST_OPTION_TIMES_ATTRIBS]) << " "; } if(opts[LIST_OPTION_DISPLAY_HASH]) { -#ifdef _MSC_VER - printf("%016I64x ", (int64_t)en->GetAttributesHash()); -#else - printf("%016llx ", (long long)en->GetAttributesHash()); -#endif + out << std::hex << std::internal << std::setw(16) << + std::setfill('0') << en->GetAttributesHash() << + std::dec; } if(opts[LIST_OPTION_SIZEINBLOCKS]) { -#ifdef _MSC_VER - printf("%05I64d ", (int64_t)en->GetSizeInBlocks()); -#else - printf("%05lld ", (long long)en->GetSizeInBlocks()); -#endif + out << std::internal << std::setw(5) << + std::setfill('0') << en->GetSizeInBlocks() << + " "; } // add name @@ -481,30 +476,38 @@ void BackupQueries::List(int64_t DirID, const std::string &rListRoot, const bool std::string listRootDecoded; if(!ConvertUtf8ToConsole(rListRoot.c_str(), listRootDecoded)) return; - printf("%s/", listRootDecoded.c_str()); + out << listRootDecoded << "/"; #else - printf("%s/", rListRoot.c_str()); + out << rListRoot << "/"; #endif } + std::string fileName; + try + { + fileName = clear.GetClearFilename(); + } + catch(CipherException &e) + { + fileName = ""; + } + #ifdef WIN32 + std::string fileNameUtf8 = fileName; + if(!ConvertUtf8ToConsole(fileNameUtf8, fileName)) { - std::string fileName; - if(!ConvertUtf8ToConsole( - clear.GetClearFilename().c_str(), fileName)) - return; - printf("%s", fileName.c_str()); + fileName = fileNameUtf8 + " [convert encoding failed]"; } -#else - printf("%s", clear.GetClearFilename().c_str()); #endif + + out << fileName; if(!en->GetName().IsEncrypted()) { - printf("[FILENAME NOT ENCRYPTED]"); + out << " [FILENAME NOT ENCRYPTED]"; } - printf("\n"); + out << std::endl; // Directory? if((en->GetFlags() & BackupStoreDirectory::Entry::Flags_Dir) != 0) @@ -515,7 +518,9 @@ void BackupQueries::List(int64_t DirID, const std::string &rListRoot, const bool std::string subroot(rListRoot); if(!FirstLevel) subroot += '/'; subroot += clear.GetClearFilename(); - List(en->GetObjectID(), subroot, opts, false /* not the first level to list */); + List(en->GetObjectID(), subroot, opts, + false /* not the first level to list */, + out); } } } diff --git a/bin/bbackupquery/BackupQueries.h b/bin/bbackupquery/BackupQueries.h index b7eefa18..74773ba9 100644 --- a/bin/bbackupquery/BackupQueries.h +++ b/bin/bbackupquery/BackupQueries.h @@ -10,8 +10,9 @@ #ifndef BACKUPQUERIES__H #define BACKUPQUERIES__H -#include +#include #include +#include #include "BoxTime.h" #include "BoxBackupCompareParams.h" @@ -85,9 +86,12 @@ public: // Return code? int GetReturnCode() {return mReturnCode;} + void List(int64_t DirID, const std::string &rListRoot, const bool *opts, + bool FirstLevel, std::ostream &out = std::cout); + void CommandList(const std::vector &args, const bool *opts); + private: // Commands - void CommandList(const std::vector &args, const bool *opts); void CommandChangeDir(const std::vector &args, const bool *opts); void CommandChangeLocalDir(const std::vector &args); void CommandGetObject(const std::vector &args, const bool *opts); @@ -102,10 +106,6 @@ private: int64_t HardLimit, int32_t BlockSize, bool MachineReadable); void CommandHelp(const std::vector &args); - // Implementations - void List(int64_t DirID, const std::string &rListRoot, const bool *opts, - bool FirstLevel); - public: class CompareParams : public BoxBackupCompareParams { -- cgit v1.2.3