Skip to content

Commit

Permalink
Refactor BackupStoreInfo to remove legacy RaidFile support
Browse files Browse the repository at this point in the history
  • Loading branch information
qris committed Jan 8, 2018
1 parent ac6e1ed commit cb25823
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 139 deletions.
97 changes: 0 additions & 97 deletions lib/backupstore/BackupStoreInfo.cpp
Expand Up @@ -14,8 +14,6 @@
#include "Archive.h"
#include "BackupStoreInfo.h"
#include "BackupStoreException.h"
#include "RaidFileWrite.h"
#include "RaidFileRead.h"

#include "MemLeakFindOn.h"

Expand All @@ -35,7 +33,6 @@
// --------------------------------------------------------------------------
BackupStoreInfo::BackupStoreInfo()
: mAccountID(-1),
mDiscSet(-1),
mReadOnly(true),
mIsModified(false),
mClientStoreMarker(0),
Expand Down Expand Up @@ -67,38 +64,9 @@ BackupStoreInfo::~BackupStoreInfo()
{
}

// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreInfo::CreateNew(int32_t, const std::string &, int)
// Purpose: Create a new info file on disc
// Created: 2003/08/28
//
// --------------------------------------------------------------------------
void BackupStoreInfo::CreateNew(int32_t AccountID, const std::string &rRootDir, int DiscSet, int64_t BlockSoftLimit, int64_t BlockHardLimit)
{
BackupStoreInfo info;
info.mAccountID = AccountID;
info.mDiscSet = DiscSet;
info.mReadOnly = false;
info.mLastObjectIDUsed = 0;
info.mBlocksSoftLimit = BlockSoftLimit;
info.mBlocksHardLimit = BlockHardLimit;

// Generate the filename
ASSERT(rRootDir[rRootDir.size() - 1] == '/' ||
rRootDir[rRootDir.size() - 1] == DIRECTORY_SEPARATOR_ASCHAR);
info.mFilename = rRootDir + INFO_FILENAME;
info.mExtraData.SetForReading(); // extra data is empty in this case

info.Save(false);
}

BackupStoreInfo::BackupStoreInfo(int32_t AccountID, int64_t BlockSoftLimit,
int64_t BlockHardLimit)
: mAccountID(AccountID),
mDiscSet(-1),
mFilename("<unused>"),
mReadOnly(false),
mIsModified(false),
mClientStoreMarker(0),
Expand All @@ -119,39 +87,6 @@ BackupStoreInfo::BackupStoreInfo(int32_t AccountID, int64_t BlockSoftLimit,
mExtraData.SetForReading(); // extra data is empty in this case
}


// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreInfo::Load(int32_t, const std::string &,
// int, bool)
// Purpose: Loads the info from disc, given the root
// information. Can be marked as read only.
// Created: 2003/08/28
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupStoreInfo> BackupStoreInfo::Load(int32_t AccountID,
const std::string &rRootDir, int DiscSet, bool ReadOnly,
int64_t *pRevisionID)
{
// Generate the filename
std::string fn(rRootDir + INFO_FILENAME);

// Open the file for reading (passing on optional request for revision ID)
std::auto_ptr<RaidFileRead> rf(RaidFileRead::Open(DiscSet, fn, pRevisionID));
std::auto_ptr<BackupStoreInfo> info = Load(*rf, fn, ReadOnly);

// Check it
if(info->GetAccountID() != AccountID)
{
THROW_FILE_ERROR("Found wrong account ID in store info",
fn, BackupStoreException, BadStoreInfoOnLoad);
}

info->mDiscSet = DiscSet;
return info;
}

std::auto_ptr<BackupStoreInfo> BackupStoreInfo::Load(IOStream& rStream,
const std::string& FileName, bool ReadOnly)
{
Expand Down Expand Up @@ -185,7 +120,6 @@ std::auto_ptr<BackupStoreInfo> BackupStoreInfo::Load(IOStream& rStream,
std::auto_ptr<BackupStoreInfo> info(new BackupStoreInfo);

// Put in basic location info
info->mFilename = FileName;
info->mReadOnly = ReadOnly;
int64_t numDelObj = 0;

Expand Down Expand Up @@ -341,37 +275,6 @@ std::auto_ptr<BackupStoreInfo> BackupStoreInfo::CreateForRegeneration(
}


// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreInfo::Save(bool allowOverwrite)
// Purpose: Save modified info back to disc
// Created: 2003/08/28
//
// --------------------------------------------------------------------------
void BackupStoreInfo::Save(bool allowOverwrite)
{
// Make sure we're initialised (although should never come to this)
if(mFilename.empty() || mAccountID == -1 || mDiscSet == -1)
{
THROW_EXCEPTION(BackupStoreException, Internal)
}

// Can we do this?
if(mReadOnly)
{
THROW_EXCEPTION(BackupStoreException, StoreInfoIsReadOnly)
}

// Then... open a write file
RaidFileWrite rf(mDiscSet, mFilename);
rf.Open(allowOverwrite);
Save(rf);

// Commit it to disc, converting it to RAID now
rf.Commit(true);
}

void BackupStoreInfo::Save(IOStream& rOutStream)
{
// Make header
Expand Down
19 changes: 1 addition & 18 deletions lib/backupstore/BackupStoreInfo.h
Expand Up @@ -74,37 +74,22 @@ class BackupStoreInfo
// No copying allowed
BackupStoreInfo(const BackupStoreInfo &);

public:
// Create a New account, saving a blank info object to the disc
static void CreateNew(int32_t AccountID, const std::string &rRootDir, int DiscSet,
int64_t BlockSoftLimit, int64_t BlockHardLimit);

protected:
BackupStoreInfo();

public:
// Load it from the store
// TODO FIXME: remove this RaidFile version of Load(), let BackupFileSystem
// handle the RaidFile part.
static std::auto_ptr<BackupStoreInfo> Load(int32_t AccountID, const std::string &rRootDir, int DiscSet, bool ReadOnly, int64_t *pRevisionID = 0);

BackupStoreInfo(int32_t AccountID, int64_t BlockSoftLimit,
int64_t BlockHardLimit);
~BackupStoreInfo();

// Load it from a stream (file or RaidFile)
static std::auto_ptr<BackupStoreInfo> Load(IOStream& rStream,
const std::string& FileName, bool ReadOnly);
void Save(IOStream& rOutStream);

// Has info been modified?
bool IsModified() const {return mIsModified;}

// Save modified infomation back to store
// TODO FIXME: remove this RaidFile version of Save(), let BackupFileSystem
// handle the RaidFile part.
void Save(bool allowOverwrite = true);
void Save(IOStream& rOutStream);

// Data access functions
int32_t GetAccountID() const {return mAccountID;}
int64_t GetLastObjectIDUsed() const {return mLastObjectIDUsed;}
Expand Down Expand Up @@ -184,8 +169,6 @@ class BackupStoreInfo
// they now define the sizes of fields on disk (via Archive).
int32_t mAccountID;
std::string mAccountName;
int mDiscSet;
std::string mFilename;
bool mReadOnly;
bool mIsModified;

Expand Down
84 changes: 60 additions & 24 deletions test/backupstore/testbackupstore.cpp
Expand Up @@ -2700,42 +2700,51 @@ bool test_symlinks()
bool test_store_info()
{
SETUP_TEST_BACKUPSTORE();
RaidBackupFileSystem fs(76, "test-info/", 0);

{
RaidFileWrite::CreateDirectory(0, "test-info");
BackupStoreInfo::CreateNew(76, "test-info/", 0, 3461231233455433LL, 2934852487LL);
TEST_CHECK_THROWS(BackupStoreInfo::CreateNew(76, "test-info/", 0, 0, 0), RaidFileException, CannotOverwriteExistingFile);
std::auto_ptr<BackupStoreInfo> info(BackupStoreInfo::Load(76, "test-info/", 0, true));
TEST_CHECK_THROWS(info->Save(), BackupStoreException, StoreInfoIsReadOnly);
BackupStoreInfo info(76, 3461231233455433LL, 2934852487LL);
fs.PutBackupStoreInfo(info);
}

{
std::auto_ptr<BackupStoreInfo> info = fs.GetBackupStoreInfoUncached();
TEST_CHECK_THROWS(fs.PutBackupStoreInfo(*info), BackupStoreException, StoreInfoIsReadOnly);
TEST_CHECK_THROWS(info->ChangeBlocksUsed(1), BackupStoreException, StoreInfoIsReadOnly);
TEST_CHECK_THROWS(info->ChangeBlocksInOldFiles(1), BackupStoreException, StoreInfoIsReadOnly);
TEST_CHECK_THROWS(info->ChangeBlocksInDeletedFiles(1), BackupStoreException, StoreInfoIsReadOnly);
TEST_CHECK_THROWS(info->RemovedDeletedDirectory(2), BackupStoreException, StoreInfoIsReadOnly);
TEST_CHECK_THROWS(info->AddDeletedDirectory(2), BackupStoreException, StoreInfoIsReadOnly);
TEST_CHECK_THROWS(info->SetAccountName("hello"), BackupStoreException, StoreInfoIsReadOnly);
}
{
std::auto_ptr<BackupStoreInfo> info(BackupStoreInfo::Load(76, "test-info/", 0, false));
info->ChangeBlocksUsed(8);
info->ChangeBlocksInOldFiles(9);
info->ChangeBlocksInDeletedFiles(10);
info->ChangeBlocksUsed(-1);
info->ChangeBlocksInOldFiles(-4);
info->ChangeBlocksInDeletedFiles(-9);
TEST_CHECK_THROWS(info->ChangeBlocksUsed(-100), BackupStoreException, StoreInfoBlockDeltaMakesValueNegative);
TEST_CHECK_THROWS(info->ChangeBlocksInOldFiles(-100), BackupStoreException, StoreInfoBlockDeltaMakesValueNegative);
TEST_CHECK_THROWS(info->ChangeBlocksInDeletedFiles(-100), BackupStoreException, StoreInfoBlockDeltaMakesValueNegative);
info->AddDeletedDirectory(2);
info->AddDeletedDirectory(3);
info->AddDeletedDirectory(4);
info->RemovedDeletedDirectory(3);
info->SetAccountName("whee");
TEST_CHECK_THROWS(info->RemovedDeletedDirectory(9), BackupStoreException, StoreInfoDirNotInList);
info->Save();
}

{
std::auto_ptr<BackupStoreInfo> info(BackupStoreInfo::Load(76, "test-info/", 0, true));
BackupStoreInfo& info(fs.GetBackupStoreInfo(false)); // !ReadOnly
info.ChangeBlocksUsed(8);
info.ChangeBlocksInOldFiles(9);
info.ChangeBlocksInDeletedFiles(10);
info.ChangeBlocksUsed(-1);
info.ChangeBlocksInOldFiles(-4);
info.ChangeBlocksInDeletedFiles(-9);
TEST_CHECK_THROWS(info.ChangeBlocksUsed(-100),
BackupStoreException, StoreInfoBlockDeltaMakesValueNegative);
TEST_CHECK_THROWS(info.ChangeBlocksInOldFiles(-100),
BackupStoreException, StoreInfoBlockDeltaMakesValueNegative);
TEST_CHECK_THROWS(info.ChangeBlocksInDeletedFiles(-100),
BackupStoreException, StoreInfoBlockDeltaMakesValueNegative);
info.AddDeletedDirectory(2);
info.AddDeletedDirectory(3);
info.AddDeletedDirectory(4);
info.RemovedDeletedDirectory(3);
info.SetAccountName("whee");
TEST_CHECK_THROWS(info.RemovedDeletedDirectory(9),
BackupStoreException, StoreInfoDirNotInList);
fs.PutBackupStoreInfo(info);
}

{
std::auto_ptr<BackupStoreInfo> info = fs.GetBackupStoreInfoUncached();
TEST_THAT(info->GetBlocksUsed() == 7);
TEST_THAT(info->GetBlocksInOldFiles() == 5);
TEST_THAT(info->GetBlocksInDeletedFiles() == 1);
Expand Down Expand Up @@ -2791,6 +2800,33 @@ bool test_bbstoreaccounts_create()
"10000B 20000B") == 0, FAIL);
TestRemoteProcessMemLeaks("bbstoreaccounts.memleaks");

// This code is almost exactly the same as tests3store.cpp:check_new_account_info()
RaidBackupFileSystem fs(0x01234567, "backup/01234567/", 0);
std::auto_ptr<BackupStoreInfo> info = fs.GetBackupStoreInfoUncached();
TEST_EQUAL(0x01234567, info->GetAccountID());
TEST_EQUAL(1, info->GetLastObjectIDUsed());
TEST_EQUAL(2, info->GetBlocksUsed());
TEST_EQUAL(0, info->GetBlocksInCurrentFiles());
TEST_EQUAL(0, info->GetBlocksInOldFiles());
TEST_EQUAL(0, info->GetBlocksInDeletedFiles());
TEST_EQUAL(2, info->GetBlocksInDirectories());
TEST_EQUAL(0, info->GetDeletedDirectories().size());
TEST_EQUAL(10000, info->GetBlocksSoftLimit());
TEST_EQUAL(20000, info->GetBlocksHardLimit());
TEST_EQUAL(0, info->GetNumCurrentFiles());
TEST_EQUAL(0, info->GetNumOldFiles());
TEST_EQUAL(0, info->GetNumDeletedFiles());
TEST_EQUAL(1, info->GetNumDirectories());
TEST_EQUAL(true, info->IsAccountEnabled());
TEST_EQUAL(true, info->IsReadOnly());
TEST_EQUAL(0, info->GetClientStoreMarker());
TEST_EQUAL("", info->GetAccountName());

std::auto_ptr<RaidFileRead> root_stream =
get_raid_file(BACKUPSTORE_ROOT_DIRECTORY_ID);
BackupStoreDirectory root_dir(*root_stream);
TEST_EQUAL(0, root_dir.GetNumberOfEntries());

TEARDOWN_TEST_BACKUPSTORE();
}

Expand Down

0 comments on commit cb25823

Please sign in to comment.