| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: BackupStoreAccounts.cpp |
|---|
| 5 | // Purpose: Account management for backup store server |
|---|
| 6 | // Created: 2003/08/21 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | |
|---|
| 12 | #include <stdio.h> |
|---|
| 13 | |
|---|
| 14 | #include "BoxPortsAndFiles.h" |
|---|
| 15 | #include "BackupStoreAccounts.h" |
|---|
| 16 | #include "BackupStoreAccountDatabase.h" |
|---|
| 17 | #include "BackupStoreRefCountDatabase.h" |
|---|
| 18 | #include "RaidFileWrite.h" |
|---|
| 19 | #include "BackupStoreInfo.h" |
|---|
| 20 | #include "BackupStoreDirectory.h" |
|---|
| 21 | #include "BackupStoreConstants.h" |
|---|
| 22 | #include "UnixUser.h" |
|---|
| 23 | |
|---|
| 24 | #include "MemLeakFindOn.h" |
|---|
| 25 | |
|---|
| 26 | // -------------------------------------------------------------------------- |
|---|
| 27 | // |
|---|
| 28 | // Function |
|---|
| 29 | // Name: BackupStoreAccounts::BackupStoreAccounts(BackupStoreAccountDatabase &) |
|---|
| 30 | // Purpose: Constructor |
|---|
| 31 | // Created: 2003/08/21 |
|---|
| 32 | // |
|---|
| 33 | // -------------------------------------------------------------------------- |
|---|
| 34 | BackupStoreAccounts::BackupStoreAccounts(BackupStoreAccountDatabase &rDatabase) |
|---|
| 35 | : mrDatabase(rDatabase) |
|---|
| 36 | { |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | // -------------------------------------------------------------------------- |
|---|
| 40 | // |
|---|
| 41 | // Function |
|---|
| 42 | // Name: BackupStoreAccounts::~BackupStoreAccounts() |
|---|
| 43 | // Purpose: Destructor |
|---|
| 44 | // Created: 2003/08/21 |
|---|
| 45 | // |
|---|
| 46 | // -------------------------------------------------------------------------- |
|---|
| 47 | BackupStoreAccounts::~BackupStoreAccounts() |
|---|
| 48 | { |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | // -------------------------------------------------------------------------- |
|---|
| 54 | // |
|---|
| 55 | // Function |
|---|
| 56 | // Name: BackupStoreAccounts::Create(int32_t, int, int64_t, int64_t, const std::string &) |
|---|
| 57 | // Purpose: Create a new account on the specified disc set. |
|---|
| 58 | // If rAsUsername is not empty, then the account information will be written under the |
|---|
| 59 | // username specified. |
|---|
| 60 | // Created: 2003/08/21 |
|---|
| 61 | // |
|---|
| 62 | // -------------------------------------------------------------------------- |
|---|
| 63 | void BackupStoreAccounts::Create(int32_t ID, int DiscSet, int64_t SizeSoftLimit, int64_t SizeHardLimit, const std::string &rAsUsername) |
|---|
| 64 | { |
|---|
| 65 | // Create the entry in the database |
|---|
| 66 | BackupStoreAccountDatabase::Entry Entry(mrDatabase.AddEntry(ID, |
|---|
| 67 | DiscSet)); |
|---|
| 68 | |
|---|
| 69 | { |
|---|
| 70 | // Become the user specified in the config file? |
|---|
| 71 | std::auto_ptr<UnixUser> user; |
|---|
| 72 | if(!rAsUsername.empty()) |
|---|
| 73 | { |
|---|
| 74 | // Username specified, change... |
|---|
| 75 | user.reset(new UnixUser(rAsUsername.c_str())); |
|---|
| 76 | user->ChangeProcessUser(true /* temporary */); |
|---|
| 77 | // Change will be undone at the end of this function |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | // Get directory name |
|---|
| 81 | std::string dirName(MakeAccountRootDir(ID, DiscSet)); |
|---|
| 82 | |
|---|
| 83 | // Create a directory on disc |
|---|
| 84 | RaidFileWrite::CreateDirectory(DiscSet, dirName, true /* recursive */); |
|---|
| 85 | |
|---|
| 86 | // Create an info file |
|---|
| 87 | BackupStoreInfo::CreateNew(ID, dirName, DiscSet, SizeSoftLimit, SizeHardLimit); |
|---|
| 88 | |
|---|
| 89 | // And an empty directory |
|---|
| 90 | BackupStoreDirectory rootDir(BACKUPSTORE_ROOT_DIRECTORY_ID, BACKUPSTORE_ROOT_DIRECTORY_ID); |
|---|
| 91 | int64_t rootDirSize = 0; |
|---|
| 92 | // Write it, knowing the directory scheme |
|---|
| 93 | { |
|---|
| 94 | RaidFileWrite rf(DiscSet, dirName + "o01"); |
|---|
| 95 | rf.Open(); |
|---|
| 96 | rootDir.WriteToStream(rf); |
|---|
| 97 | rootDirSize = rf.GetDiscUsageInBlocks(); |
|---|
| 98 | rf.Commit(true); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | // Update the store info to reflect the size of the root directory |
|---|
| 102 | std::auto_ptr<BackupStoreInfo> info(BackupStoreInfo::Load(ID, dirName, DiscSet, false /* ReadWrite */)); |
|---|
| 103 | info->ChangeBlocksUsed(rootDirSize); |
|---|
| 104 | info->ChangeBlocksInDirectories(rootDirSize); |
|---|
| 105 | info->AdjustNumDirectories(1); |
|---|
| 106 | |
|---|
| 107 | // Save it back |
|---|
| 108 | info->Save(); |
|---|
| 109 | |
|---|
| 110 | // Create the refcount database |
|---|
| 111 | BackupStoreRefCountDatabase::CreateNew(Entry); |
|---|
| 112 | std::auto_ptr<BackupStoreRefCountDatabase> refcount( |
|---|
| 113 | BackupStoreRefCountDatabase::Load(Entry, false)); |
|---|
| 114 | refcount->AddReference(BACKUPSTORE_ROOT_DIRECTORY_ID); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | // As the original user... |
|---|
| 118 | // Write the database back |
|---|
| 119 | mrDatabase.Write(); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | // -------------------------------------------------------------------------- |
|---|
| 124 | // |
|---|
| 125 | // Function |
|---|
| 126 | // Name: BackupStoreAccounts::GetAccountRoot(int32_t, std::string &, int &) |
|---|
| 127 | // Purpose: Gets the root of an account, returning the info via references |
|---|
| 128 | // Created: 2003/08/21 |
|---|
| 129 | // |
|---|
| 130 | // -------------------------------------------------------------------------- |
|---|
| 131 | void BackupStoreAccounts::GetAccountRoot(int32_t ID, std::string &rRootDirOut, int &rDiscSetOut) const |
|---|
| 132 | { |
|---|
| 133 | // Find the account |
|---|
| 134 | const BackupStoreAccountDatabase::Entry &en(mrDatabase.GetEntry(ID)); |
|---|
| 135 | |
|---|
| 136 | rRootDirOut = MakeAccountRootDir(ID, en.GetDiscSet()); |
|---|
| 137 | rDiscSetOut = en.GetDiscSet(); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | // -------------------------------------------------------------------------- |
|---|
| 142 | // |
|---|
| 143 | // Function |
|---|
| 144 | // Name: BackupStoreAccounts::MakeAccountRootDir(int32_t, int) |
|---|
| 145 | // Purpose: Private. Generates a root directory name for the account |
|---|
| 146 | // Created: 2003/08/21 |
|---|
| 147 | // |
|---|
| 148 | // -------------------------------------------------------------------------- |
|---|
| 149 | std::string BackupStoreAccounts::MakeAccountRootDir(int32_t ID, int DiscSet) |
|---|
| 150 | { |
|---|
| 151 | char accid[64]; // big enough! |
|---|
| 152 | ::sprintf(accid, "%08x" DIRECTORY_SEPARATOR, ID); |
|---|
| 153 | return std::string(std::string(BOX_RAIDFILE_ROOT_BBSTORED |
|---|
| 154 | DIRECTORY_SEPARATOR) + accid); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | // -------------------------------------------------------------------------- |
|---|
| 159 | // |
|---|
| 160 | // Function |
|---|
| 161 | // Name: BackupStoreAccounts::AccountExists(int32_t) |
|---|
| 162 | // Purpose: Does an account exist? |
|---|
| 163 | // Created: 2003/08/21 |
|---|
| 164 | // |
|---|
| 165 | // -------------------------------------------------------------------------- |
|---|
| 166 | bool BackupStoreAccounts::AccountExists(int32_t ID) |
|---|
| 167 | { |
|---|
| 168 | return mrDatabase.EntryExists(ID); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|