diff options
author | Chris Wilson <chris+github@qwirx.com> | 2012-11-18 16:29:41 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2012-11-18 16:29:41 +0000 |
commit | 6af3a67ae549913692a4cee69fabb50988a61fe9 (patch) | |
tree | 918fe6350d522acb4c608bb77ee93cade767ad7c /lib | |
parent | 3db7d944f5849db8ea8098de54ad45bbb0bf6b86 (diff) |
Add a helper in BackupStoreAccounts to get a write lock on an account.
Use it in two places to simplify code (not in BackupStoreContext yet,
because that wants to communicate with HK process as well).
Diffstat (limited to 'lib')
-rw-r--r-- | lib/backupstore/BackupStoreAccounts.cpp | 40 | ||||
-rw-r--r-- | lib/backupstore/BackupStoreAccounts.h | 2 | ||||
-rw-r--r-- | lib/backupstore/BackupStoreCheck.cpp | 34 |
3 files changed, 43 insertions, 33 deletions
diff --git a/lib/backupstore/BackupStoreAccounts.cpp b/lib/backupstore/BackupStoreAccounts.cpp index b7ef13ab..18500fc1 100644 --- a/lib/backupstore/BackupStoreAccounts.cpp +++ b/lib/backupstore/BackupStoreAccounts.cpp @@ -11,14 +11,16 @@ #include <stdio.h> -#include "BoxPortsAndFiles.h" #include "BackupStoreAccounts.h" #include "BackupStoreAccountDatabase.h" +#include "BackupStoreConstants.h" +#include "BackupStoreDirectory.h" +#include "BackupStoreException.h" +#include "BackupStoreInfo.h" #include "BackupStoreRefCountDatabase.h" +#include "BoxPortsAndFiles.h" #include "RaidFileWrite.h" -#include "BackupStoreInfo.h" -#include "BackupStoreDirectory.h" -#include "BackupStoreConstants.h" +#include "StoreStructure.h" #include "UnixUser.h" #include "MemLeakFindOn.h" @@ -168,4 +170,34 @@ bool BackupStoreAccounts::AccountExists(int32_t ID) return mrDatabase.EntryExists(ID); } +void BackupStoreAccounts::LockAccount(int32_t ID, NamedLock& rNamedLock) +{ + const BackupStoreAccountDatabase::Entry &en(mrDatabase.GetEntry(ID)); + std::string rootDir = MakeAccountRootDir(ID, en.GetDiscSet()); + int discSet = en.GetDiscSet(); + + std::string writeLockFilename; + StoreStructure::MakeWriteLockFilename(rootDir, discSet, writeLockFilename); + + bool gotLock = false; + int triesLeft = 8; + do + { + gotLock = rNamedLock.TryAndGetLock(writeLockFilename, + 0600 /* restrictive file permissions */); + + if(!gotLock) + { + --triesLeft; + ::sleep(1); + } + } + while (!gotLock && triesLeft > 0); + if (!gotLock) + { + THROW_EXCEPTION_MESSAGE(BackupStoreException, + CouldNotLockStoreAccount, "Failed to get exclusive " + "lock on account " << ID); + } +} diff --git a/lib/backupstore/BackupStoreAccounts.h b/lib/backupstore/BackupStoreAccounts.h index 109de701..3163f15c 100644 --- a/lib/backupstore/BackupStoreAccounts.h +++ b/lib/backupstore/BackupStoreAccounts.h @@ -13,6 +13,7 @@ #include <string> #include "BackupStoreAccountDatabase.h" +#include "NamedLock.h" // -------------------------------------------------------------------------- // @@ -41,6 +42,7 @@ public: { return MakeAccountRootDir(rEntry.GetID(), rEntry.GetDiscSet()); } + void LockAccount(int32_t ID, NamedLock& rNamedLock); private: static std::string MakeAccountRootDir(int32_t ID, int DiscSet); diff --git a/lib/backupstore/BackupStoreCheck.cpp b/lib/backupstore/BackupStoreCheck.cpp index 04f6a41c..4530b9dd 100644 --- a/lib/backupstore/BackupStoreCheck.cpp +++ b/lib/backupstore/BackupStoreCheck.cpp @@ -81,40 +81,16 @@ BackupStoreCheck::~BackupStoreCheck() // // Function // Name: BackupStoreCheck::Check() -// Purpose: Perform the check on the given account +// Purpose: Perform the check on the given account. You need to +// hold a lock on the account before calling this! // Created: 21/4/04 // // -------------------------------------------------------------------------- void BackupStoreCheck::Check() { - // Lock the account - { - std::string writeLockFilename; - StoreStructure::MakeWriteLockFilename(mStoreRoot, mDiscSetNumber, writeLockFilename); - - bool gotLock = false; - int triesLeft = 8; - do - { - gotLock = mAccountLock.TryAndGetLock(writeLockFilename.c_str(), 0600 /* restrictive file permissions */); - - if(!gotLock) - { - --triesLeft; - ::sleep(1); - } - } while(!gotLock && triesLeft > 0); - - if(!gotLock) - { - // Couldn't lock the account -- just stop now - if(!mQuiet) - { - BOX_ERROR("Failed to lock the account -- did not check.\nTry again later after the client has disconnected.\nAlternatively, forcibly kill the server."); - } - THROW_EXCEPTION(BackupStoreException, CouldNotLockStoreAccount) - } - } + std::string writeLockFilename; + StoreStructure::MakeWriteLockFilename(mStoreRoot, mDiscSetNumber, writeLockFilename); + ASSERT(FileExists(writeLockFilename)); if(!mQuiet && mFixErrors) { |