diff options
author | Chris Wilson <chris+github@qwirx.com> | 2012-06-29 22:15:36 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2012-06-29 22:15:36 +0000 |
commit | a9e867a2392349a76b3dbac30b3b85b22b478579 (patch) | |
tree | fb915aa3b3e403ceade8b1b851647c6c56fd2867 | |
parent | 9b89929355f6d1522e66a2919af0fb07dc28c508 (diff) |
Add housekeep command to bbstoreaccounts to run housekeeping right now.
-rw-r--r-- | bin/bbstoreaccounts/bbstoreaccounts.cpp | 88 | ||||
-rw-r--r-- | lib/backupstore/HousekeepStoreAccount.cpp (renamed from bin/bbstored/HousekeepStoreAccount.cpp) | 29 | ||||
-rw-r--r-- | lib/backupstore/HousekeepStoreAccount.h (renamed from bin/bbstored/HousekeepStoreAccount.h) | 3 |
3 files changed, 89 insertions, 31 deletions
diff --git a/bin/bbstoreaccounts/bbstoreaccounts.cpp b/bin/bbstoreaccounts/bbstoreaccounts.cpp index 84b27a67..8564376b 100644 --- a/bin/bbstoreaccounts/bbstoreaccounts.cpp +++ b/bin/bbstoreaccounts/bbstoreaccounts.cpp @@ -24,17 +24,18 @@ #include <ostream> #include <vector> -#include "BoxPortsAndFiles.h" -#include "BackupStoreConfigVerify.h" -#include "RaidFileController.h" #include "BackupStoreAccounts.h" #include "BackupStoreAccountDatabase.h" -#include "MainHelper.h" +#include "BackupStoreCheck.h" +#include "BackupStoreConfigVerify.h" #include "BackupStoreInfo.h" -#include "StoreStructure.h" +#include "BoxPortsAndFiles.h" +#include "HousekeepStoreAccount.h" +#include "MainHelper.h" #include "NamedLock.h" +#include "RaidFileController.h" +#include "StoreStructure.h" #include "UnixUser.h" -#include "BackupStoreCheck.h" #include "Utils.h" #include "MemLeakFindOn.h" @@ -264,7 +265,6 @@ int SetAccountName(Configuration &rConfig, const std::string &rUsername, return 0; } - int AccountInfo(Configuration &rConfig, int32_t ID) { // Load in the account database @@ -438,7 +438,9 @@ int DeleteAccount(Configuration &rConfig, const std::string &rUsername, int32_t return retcode; } -int CheckAccount(Configuration &rConfig, const std::string &rUsername, int32_t ID, bool FixErrors, bool Quiet) +bool OpenAccount(Configuration &rConfig, int32_t ID, + const std::string &rUsername, std::string &rRootDirOut, + int &rDiscSetOut, std::auto_ptr<UnixUser> apUser) { // Load in the account database std::auto_ptr<BackupStoreAccountDatabase> db(BackupStoreAccountDatabase::Read(rConfig.GetKeyValue("AccountDatabase").c_str())); @@ -448,23 +450,37 @@ int CheckAccount(Configuration &rConfig, const std::string &rUsername, int32_t I { BOX_ERROR("Account " << BOX_FORMAT_ACCOUNT(ID) << " does not exist."); - return 1; + return false; } // Get info from the database BackupStoreAccounts acc(*db); - std::string rootDir; - int discSetNum; - acc.GetAccountRoot(ID, rootDir, discSetNum); + acc.GetAccountRoot(ID, rRootDirOut, rDiscSetOut); // Become the right user - std::auto_ptr<UnixUser> user; if(!rUsername.empty()) { // Username specified, change... - user.reset(new UnixUser(rUsername.c_str())); - user->ChangeProcessUser(true /* temporary */); - // Change will be undone at the end of this function + apUser.reset(new UnixUser(rUsername)); + apUser->ChangeProcessUser(true /* temporary */); + // Change will be undone when apUser goes out of scope + // in the caller. + } + + return true; +} + +int CheckAccount(Configuration &rConfig, const std::string &rUsername, int32_t ID, bool FixErrors, bool Quiet) +{ + std::string rootDir; + int discSetNum; + std::auto_ptr<UnixUser> user; + + if(!OpenAccount(rConfig, ID, rUsername, rootDir, discSetNum, user)) + { + BOX_ERROR("Failed to open account " << BOX_FORMAT_ACCOUNT(ID) + << " for checking."); + return 1; } // Check it @@ -496,6 +512,38 @@ int CreateAccount(Configuration &rConfig, const std::string &rUsername, int32_t return 0; } +int HousekeepAccountNow(Configuration &rConfig, const std::string &rUsername, + int32_t ID) +{ + std::string rootDir; + int discSetNum; + std::auto_ptr<UnixUser> user; + + if(!OpenAccount(rConfig, ID, rUsername, rootDir, discSetNum, user)) + { + BOX_ERROR("Failed to open account " << BOX_FORMAT_ACCOUNT(ID) + << " for housekeeping."); + return 1; + } + + HousekeepStoreAccount housekeeping(ID, rootDir, discSetNum, NULL); + bool success = housekeeping.DoHousekeeping(); + + if(!success) + { + BOX_ERROR("Failed to lock account " << BOX_FORMAT_ACCOUNT(ID) + << " for housekeeping: perhaps a client is " + "still connected?"); + return 1; + } + else + { + BOX_TRACE("Finished housekeeping on account " << + BOX_FORMAT_ACCOUNT(ID)); + return 0; + } +} + void PrintUsageAndExit() { printf( @@ -526,6 +574,10 @@ void PrintUsageAndExit() " Changes the \"name\" of the account to the specified string.\n" " The name is purely cosmetic and intended to make it easier to\n" " identify your accounts.\n" +" housekeep <account>\n" +" Runs housekeeping immediately on the account. If it cannot be locked,\n" +" bbstoreaccounts returns an error status code (1), otherwise success\n" +" (0) even if any errors were fixed by housekeeping.\n" ); exit(2); } @@ -706,6 +758,10 @@ int main(int argc, const char *argv[]) // Check the account return CheckAccount(*config, username, id, fixErrors, quiet); } + else if(::strcmp(argv[0], "housekeep") == 0) + { + return HousekeepAccountNow(*config, username, id); + } else { BOX_ERROR("Unknown command '" << argv[0] << "'."); diff --git a/bin/bbstored/HousekeepStoreAccount.cpp b/lib/backupstore/HousekeepStoreAccount.cpp index dde813f9..311ae433 100644 --- a/bin/bbstored/HousekeepStoreAccount.cpp +++ b/lib/backupstore/HousekeepStoreAccount.cpp @@ -13,18 +13,20 @@ #include <map> -#include "HousekeepStoreAccount.h" -#include "BackupStoreDaemon.h" -#include "StoreStructure.h" +#include "autogen_BackupStoreException.h" +#include "BackupConstants.h" +#include "BackupStoreAccountDatabase.h" #include "BackupStoreConstants.h" -#include "RaidFileRead.h" -#include "RaidFileWrite.h" #include "BackupStoreDirectory.h" -#include "BackupStoreInfo.h" -#include "NamedLock.h" -#include "autogen_BackupStoreException.h" #include "BackupStoreFile.h" +#include "BackupStoreInfo.h" +#include "BackupStoreRefCountDatabase.h" #include "BufferedStream.h" +#include "HousekeepStoreAccount.h" +#include "NamedLock.h" +#include "RaidFileRead.h" +#include "RaidFileWrite.h" +#include "StoreStructure.h" #include "MemLeakFindOn.h" @@ -85,10 +87,10 @@ HousekeepStoreAccount::~HousekeepStoreAccount() // Created: 11/12/03 // // -------------------------------------------------------------------------- -void HousekeepStoreAccount::DoHousekeeping(bool KeepTryingForever) +bool HousekeepStoreAccount::DoHousekeeping(bool KeepTryingForever) { BOX_TRACE("Starting housekeeping on account " << - BOX_FORMAT_OBJECTID(mAccountID)); + BOX_FORMAT_ACCOUNT(mAccountID)); // Attempt to lock the account std::string writeLockFilename; @@ -111,7 +113,7 @@ void HousekeepStoreAccount::DoHousekeeping(bool KeepTryingForever) else { // Couldn't lock the account -- just stop now - return; + return false; } } @@ -166,7 +168,7 @@ void HousekeepStoreAccount::DoHousekeeping(bool KeepTryingForever) info->Save(); } - return; + return false; } // Log any difference in opinion between the values recorded in @@ -366,7 +368,8 @@ void HousekeepStoreAccount::DoHousekeeping(bool KeepTryingForever) writeLock.ReleaseLock(); BOX_TRACE("Finished housekeeping on account " << - BOX_FORMAT_OBJECTID(mAccountID)); + BOX_FORMAT_ACCOUNT(mAccountID)); + return true; } diff --git a/bin/bbstored/HousekeepStoreAccount.h b/lib/backupstore/HousekeepStoreAccount.h index 5e3d8ffd..61cff868 100644 --- a/bin/bbstored/HousekeepStoreAccount.h +++ b/lib/backupstore/HousekeepStoreAccount.h @@ -14,7 +14,6 @@ #include <set> #include <vector> -class BackupStoreDaemon; class BackupStoreDirectory; class HousekeepingCallback @@ -39,7 +38,7 @@ public: int StoreDiscSet, HousekeepingCallback* pHousekeepingCallback); ~HousekeepStoreAccount(); - void DoHousekeeping(bool KeepTryingForever = false); + bool DoHousekeeping(bool KeepTryingForever = false); int GetRefCountsAdjusted() { return mRefCountsAdjusted; } private: |