summaryrefslogtreecommitdiff
path: root/bin/bbstoreaccounts/bbstoreaccounts.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'bin/bbstoreaccounts/bbstoreaccounts.cpp')
-rw-r--r--bin/bbstoreaccounts/bbstoreaccounts.cpp112
1 files changed, 79 insertions, 33 deletions
diff --git a/bin/bbstoreaccounts/bbstoreaccounts.cpp b/bin/bbstoreaccounts/bbstoreaccounts.cpp
index 6f079d30..71114ef4 100644
--- a/bin/bbstoreaccounts/bbstoreaccounts.cpp
+++ b/bin/bbstoreaccounts/bbstoreaccounts.cpp
@@ -9,12 +9,17 @@
#include "Box.h"
-#include <unistd.h>
+#include <limits.h>
#include <stdio.h>
+#include <unistd.h>
+
#include <sys/types.h>
-#include <limits.h>
-#include <vector>
+
#include <algorithm>
+#include <cstring>
+#include <iostream>
+#include <ostream>
+#include <vector>
#include "BoxPortsAndFiles.h"
#include "BackupStoreConfigVerify.h"
@@ -27,12 +32,15 @@
#include "NamedLock.h"
#include "UnixUser.h"
#include "BackupStoreCheck.h"
+#include "Utils.h"
#include "MemLeakFindOn.h"
// 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)
@@ -62,22 +70,11 @@ int BlockSizeOfDiscSet(int DiscSet)
return controller.GetDiscSet(DiscSet).GetBlockSize();
}
-const char *BlockSizeToString(int64_t Blocks, int DiscSet)
+std::string BlockSizeToString(int64_t Blocks, int64_t MaxBlocks, int DiscSet)
{
- // Not reentrant, nor can be used in the same function call twice, etc.
- static char string[256];
-
- // Work out size in Mb.
- double mb = (Blocks * BlockSizeOfDiscSet(DiscSet)) / (1024.0*1024.0);
-
- // Format string
-#ifdef WIN32
- sprintf(string, "%I64d (%.2fMb)", Blocks, mb);
-#else
- sprintf(string, "%lld (%.2fMb)", Blocks, mb);
-#endif
-
- return string;
+ return FormatUsageBar(Blocks, Blocks * BlockSizeOfDiscSet(DiscSet),
+ MaxBlocks * BlockSizeOfDiscSet(DiscSet),
+ sMachineReadableOutput);
}
int64_t SizeStringToBlocks(const char *string, int DiscSet)
@@ -118,7 +115,7 @@ int64_t SizeStringToBlocks(const char *string, int DiscSet)
default:
BOX_FATAL(string << " has an invalid units specifier "
- "(use B for blocks, M for Mb, G for Gb, eg 2Gb)");
+ "(use B for blocks, M for MB, G for GB, eg 2GB)");
exit(1);
break;
}
@@ -211,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))
@@ -226,18 +225,34 @@ 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
- printf(" Account ID: %08x\n", ID);
- printf(" Last object ID: %lld\n", info->GetLastObjectIDUsed());
- printf(" Blocks used: %s\n", BlockSizeToString(info->GetBlocksUsed(), discSet));
- printf(" Blocks used by old files: %s\n", BlockSizeToString(info->GetBlocksInOldFiles(), discSet));
- printf("Blocks used by deleted files: %s\n", BlockSizeToString(info->GetBlocksInDeletedFiles(), discSet));
- printf(" Blocks used by directories: %s\n", BlockSizeToString(info->GetBlocksInDirectories(), discSet));
- printf(" Block soft limit: %s\n", BlockSizeToString(info->GetBlocksSoftLimit(), discSet));
- printf(" Block hard limit: %s\n", BlockSizeToString(info->GetBlocksHardLimit(), discSet));
- printf(" Client store marker: %lld\n", info->GetClientStoreMarker());
+ std::cout << FormatUsageLineStart("Account ID", sMachineReadableOutput) <<
+ BOX_FORMAT_ACCOUNT(ID) << std::endl;
+ std::cout << FormatUsageLineStart("Last object ID", sMachineReadableOutput) <<
+ BOX_FORMAT_OBJECTID(info->GetLastObjectIDUsed()) << std::endl;
+ std::cout << FormatUsageLineStart("Used", sMachineReadableOutput) <<
+ BlockSizeToString(info->GetBlocksUsed(),
+ info->GetBlocksHardLimit(), discSet) << std::endl;
+ std::cout << FormatUsageLineStart("Old files", sMachineReadableOutput) <<
+ BlockSizeToString(info->GetBlocksInOldFiles(),
+ info->GetBlocksHardLimit(), discSet) << std::endl;
+ std::cout << FormatUsageLineStart("Deleted files", sMachineReadableOutput) <<
+ BlockSizeToString(info->GetBlocksInDeletedFiles(),
+ info->GetBlocksHardLimit(), discSet) << std::endl;
+ std::cout << FormatUsageLineStart("Directories", sMachineReadableOutput) <<
+ BlockSizeToString(info->GetBlocksInDirectories(),
+ info->GetBlocksHardLimit(), discSet) << std::endl;
+ std::cout << FormatUsageLineStart("Soft limit", sMachineReadableOutput) <<
+ BlockSizeToString(info->GetBlocksSoftLimit(),
+ info->GetBlocksHardLimit(), discSet) << std::endl;
+ std::cout << FormatUsageLineStart("Hard limit", sMachineReadableOutput) <<
+ BlockSizeToString(info->GetBlocksHardLimit(),
+ info->GetBlocksHardLimit(), discSet) << std::endl;
+ std::cout << FormatUsageLineStart("Client store marker", sMachineReadableOutput) <<
+ info->GetLastObjectIDUsed() << std::endl;
return 0;
}
@@ -409,8 +424,32 @@ int CreateAccount(Configuration &rConfig, const std::string &rUsername, int32_t
void PrintUsageAndExit()
{
- printf("Usage: bbstoreaccounts [-c config_file] action account_id [args]\nAccount ID is integer specified in hex\n");
- exit(1);
+ printf(
+"Usage: bbstoreaccounts [-c config_file] action account_id [args]\n"
+"Account ID is integer specified in hex\n"
+"\n"
+"Commands (and arguments):\n"
+" create <account> <discnum> <softlimit> <hardlimit>\n"
+" Creates the specified account number (in hex with no 0x) on the\n"
+" 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 [-m] <account>\n"
+" Prints information about the specified account including number\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"
+" delete <account> [yes]\n"
+" Deletes the specified account. Prompts for confirmation unless\n"
+" the optional 'yes' parameter is provided.\n"
+" check <account> [fix] [quiet]\n"
+" Checks the specified account for errors. If the 'fix' option is\n"
+" provided, any errors discovered that can be fixed automatically\n"
+" will be fixed. If the 'quiet' option is provided, less output is\n"
+" produced.\n"
+ );
+ exit(2);
}
int main(int argc, const char *argv[])
@@ -420,6 +459,8 @@ int main(int argc, const char *argv[])
MAINHELPER_START
+ Logging::SetProgramName("bbstoreaccounts");
+
// Filename for configuration file?
std::string configFilename;
@@ -431,7 +472,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)
{
@@ -440,6 +481,11 @@ int main(int argc, const char *argv[])
configFilename = optarg;
break;
+ case 'm':
+ // enable machine readable output
+ sMachineReadableOutput = true;
+ break;
+
case '?':
default:
PrintUsageAndExit();