diff options
-rw-r--r-- | bin/bbackupquery/BackupQueries.cpp | 4 | ||||
-rw-r--r-- | bin/bbstoreaccounts/bbstoreaccounts.cpp | 41 | ||||
-rw-r--r-- | lib/common/Utils.cpp | 58 | ||||
-rw-r--r-- | lib/common/Utils.h | 6 |
4 files changed, 71 insertions, 38 deletions
diff --git a/bin/bbackupquery/BackupQueries.cpp b/bin/bbackupquery/BackupQueries.cpp index 3d4c5b5d..622fa20f 100644 --- a/bin/bbackupquery/BackupQueries.cpp +++ b/bin/bbackupquery/BackupQueries.cpp @@ -2190,8 +2190,8 @@ void BackupQueries::CommandUsage() // -------------------------------------------------------------------------- void BackupQueries::CommandUsageDisplayEntry(const char *Name, int64_t Size, int64_t HardLimit, int32_t BlockSize) { - std::cout << FormatUsageLineStart(Name) << - FormatUsageBar(Size, Size * BlockSize, HardLimit * BlockSize) << + std::cout << FormatUsageLineStart(Name, false) << + FormatUsageBar(Size, Size * BlockSize, HardLimit * BlockSize, false) << std::endl; } diff --git a/bin/bbstoreaccounts/bbstoreaccounts.cpp b/bin/bbstoreaccounts/bbstoreaccounts.cpp index a1af1d70..2546026f 100644 --- a/bin/bbstoreaccounts/bbstoreaccounts.cpp +++ b/bin/bbstoreaccounts/bbstoreaccounts.cpp @@ -39,6 +39,8 @@ // max size of soft limit as percent of hard limit #define MAX_SOFT_LIMIT_SIZE 97 +bool sMachineReadableOutput = false; + void CheckSoftHardLimits(int64_t SoftLimit, int64_t HardLimit) { if(SoftLimit >= HardLimit) @@ -71,7 +73,8 @@ int BlockSizeOfDiscSet(int DiscSet) std::string BlockSizeToString(int64_t Blocks, int64_t MaxBlocks, int DiscSet) { return FormatUsageBar(Blocks, Blocks * BlockSizeOfDiscSet(DiscSet), - MaxBlocks * BlockSizeOfDiscSet(DiscSet)); + MaxBlocks * BlockSizeOfDiscSet(DiscSet), + sMachineReadableOutput); } int64_t SizeStringToBlocks(const char *string, int DiscSet) @@ -205,7 +208,9 @@ int SetLimit(Configuration &rConfig, const std::string &rUsername, int32_t ID, c int AccountInfo(Configuration &rConfig, int32_t ID) { // Load in the account database - std::auto_ptr<BackupStoreAccountDatabase> db(BackupStoreAccountDatabase::Read(rConfig.GetKeyValue("AccountDatabase").c_str())); + std::auto_ptr<BackupStoreAccountDatabase> db( + BackupStoreAccountDatabase::Read( + rConfig.GetKeyValue("AccountDatabase").c_str())); // Exists? if(!db->EntryExists(ID)) @@ -220,32 +225,33 @@ int AccountInfo(Configuration &rConfig, int32_t ID) std::string rootDir; int discSet; acc.GetAccountRoot(ID, rootDir, discSet); - std::auto_ptr<BackupStoreInfo> info(BackupStoreInfo::Load(ID, rootDir, discSet, true /* ReadOnly */)); + std::auto_ptr<BackupStoreInfo> info(BackupStoreInfo::Load(ID, + rootDir, discSet, true /* ReadOnly */)); // Then print out lots of info - std::cout << FormatUsageLineStart("Account ID") << + std::cout << FormatUsageLineStart("Account ID", sMachineReadableOutput) << BOX_FORMAT_ACCOUNT(ID) << std::endl; - std::cout << FormatUsageLineStart("Last object ID") << + std::cout << FormatUsageLineStart("Last object ID", sMachineReadableOutput) << BOX_FORMAT_OBJECTID(info->GetLastObjectIDUsed()) << std::endl; - std::cout << FormatUsageLineStart("Used") << + std::cout << FormatUsageLineStart("Used", sMachineReadableOutput) << BlockSizeToString(info->GetBlocksUsed(), info->GetBlocksHardLimit(), discSet) << std::endl; - std::cout << FormatUsageLineStart("Old files") << + std::cout << FormatUsageLineStart("Old files", sMachineReadableOutput) << BlockSizeToString(info->GetBlocksInOldFiles(), info->GetBlocksHardLimit(), discSet) << std::endl; - std::cout << FormatUsageLineStart("Deleted files") << + std::cout << FormatUsageLineStart("Deleted files", sMachineReadableOutput) << BlockSizeToString(info->GetBlocksInDeletedFiles(), info->GetBlocksHardLimit(), discSet) << std::endl; - std::cout << FormatUsageLineStart("Directories") << + std::cout << FormatUsageLineStart("Directories", sMachineReadableOutput) << BlockSizeToString(info->GetBlocksInDirectories(), info->GetBlocksHardLimit(), discSet) << std::endl; - std::cout << FormatUsageLineStart("Soft limit") << + std::cout << FormatUsageLineStart("Soft limit", sMachineReadableOutput) << BlockSizeToString(info->GetBlocksSoftLimit(), info->GetBlocksHardLimit(), discSet) << std::endl; - std::cout << FormatUsageLineStart("Hard limit") << + std::cout << FormatUsageLineStart("Hard limit", sMachineReadableOutput) << BlockSizeToString(info->GetBlocksHardLimit(), info->GetBlocksHardLimit(), discSet) << std::endl; - std::cout << FormatUsageLineStart("Client store marker") << + std::cout << FormatUsageLineStart("Client store marker", sMachineReadableOutput) << info->GetLastObjectIDUsed() << std::endl; return 0; @@ -428,9 +434,9 @@ void PrintUsageAndExit() " specified raidfile disc set number (see raidfile.conf for valid\n" " set numbers) with the specified soft and hard limits (in blocks\n" " if suffixed with B, MB with M, GB with G)\n" -" info <account>\n" +" info [-m] <account>\n" " Prints information about the specified account including number\n" -" of blocks used.\n" +" of blocks used. The -m option enable machine-readable output.\n" " setlimit <accounts> <softlimit> <hardlimit>\n" " Changes the limits of the account as specified. Numbers are\n" " interpreted as for the 'create' command (suffixed with B, M or G)\n" @@ -464,7 +470,7 @@ int main(int argc, const char *argv[]) // See if there's another entry on the command line int c; - while((c = getopt(argc, (char * const *)argv, "c:")) != -1) + while((c = getopt(argc, (char * const *)argv, "c:m")) != -1) { switch(c) { @@ -473,6 +479,11 @@ int main(int argc, const char *argv[]) configFilename = optarg; break; + case 'm': + // enable machine readable output + sMachineReadableOutput = true; + break; + case '?': default: PrintUsageAndExit(); diff --git a/lib/common/Utils.cpp b/lib/common/Utils.cpp index b2a16d5b..3f06ac48 100644 --- a/lib/common/Utils.cpp +++ b/lib/common/Utils.cpp @@ -252,36 +252,56 @@ std::string HumanReadableSize(int64_t Bytes) return result.str(); } -std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max) +std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max, + bool MachineReadable) { std::ostringstream result; - // Bar graph - char bar[17]; - unsigned int b = (int)((Bytes * (sizeof(bar)-1)) / Max); - if(b > sizeof(bar)-1) {b = sizeof(bar)-1;} - for(unsigned int l = 0; l < b; l++) + + if (MachineReadable) { - bar[l] = '*'; + result << (Bytes >> 10) << " kB, " << + std::setprecision(0) << ((Bytes*100)/Max) << "%"; } - for(unsigned int l = b; l < sizeof(bar) - 1; l++) + else { - bar[l] = ' '; + // Bar graph + char bar[17]; + unsigned int b = (int)((Bytes * (sizeof(bar)-1)) / Max); + if(b > sizeof(bar)-1) {b = sizeof(bar)-1;} + for(unsigned int l = 0; l < b; l++) + { + bar[l] = '*'; + } + for(unsigned int l = b; l < sizeof(bar) - 1; l++) + { + bar[l] = ' '; + } + bar[sizeof(bar)-1] = '\0'; + + result << std::fixed << + std::setw(10) << Blocks << " blocks, " << + std::setw(10) << HumanReadableSize(Bytes) << ", " << + std::setw(3) << std::setprecision(0) << + ((Bytes*100)/Max) << "% |" << bar << "|"; } - bar[sizeof(bar)-1] = '\0'; - - result << std::fixed << - std::setw(10) << Blocks << " blocks, " << - std::setw(10) << HumanReadableSize(Bytes) << ", " << - std::setw(3) << std::setprecision(0) << - ((Bytes*100)/Max) << "% |" << bar << "|"; return result.str(); } -std::string FormatUsageLineStart(const std::string& rName) +std::string FormatUsageLineStart(const std::string& rName, + bool MachineReadable) { - std::ostringstream result; - result << std::setw(20) << std::right << rName << ": "; + std::ostringstream result; + + if (MachineReadable) + { + result << rName << ": "; + } + else + { + result << std::setw(20) << std::right << rName << ": "; + } + return result.str(); } diff --git a/lib/common/Utils.h b/lib/common/Utils.h index 78bcbd6b..d6792077 100644 --- a/lib/common/Utils.h +++ b/lib/common/Utils.h @@ -31,8 +31,10 @@ enum }; int ObjectExists(const std::string& rFilename); std::string HumanReadableSize(int64_t Bytes); -std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max); -std::string FormatUsageLineStart(const std::string& rName); +std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max, + bool MachineReadable); +std::string FormatUsageLineStart(const std::string& rName, + bool MachineReadable); #include "MemLeakFindOff.h" |