diff --git a/dlls/nvault/IVault.h b/dlls/nvault/IVault.h index 7111b7a2..ec9a5640 100755 --- a/dlls/nvault/IVault.h +++ b/dlls/nvault/IVault.h @@ -20,6 +20,9 @@ public: class IVaultMngr { public: + /** + * Note: Will return NULL if the vault failed to create. + */ virtual IVault *OpenVault(const char *name) =0; }; diff --git a/dlls/nvault/Journal.cpp b/dlls/nvault/Journal.cpp index 2c98e9ad..37cf56d4 100755 --- a/dlls/nvault/Journal.cpp +++ b/dlls/nvault/Journal.cpp @@ -18,7 +18,9 @@ int Journal::Replay(VaultMap *pMap) { m_fp = fopen(m_File.c_str(), "rb"); if (!m_fp) + { return -1; + } BinaryReader br(m_fp); diff --git a/dlls/nvault/NVault.cpp b/dlls/nvault/NVault.cpp index f95e943f..ec61be47 100755 --- a/dlls/nvault/NVault.cpp +++ b/dlls/nvault/NVault.cpp @@ -32,6 +32,18 @@ NVault::NVault(const char *file) m_File.assign(file); m_Journal = NULL; m_Open = false; + + FILE *fp = fopen(m_File.c_str(), "rb"); + if (!fp) + { + fp = fopen(m_File.c_str(), "wb"); + if (!fp) + { + throw Vault_NoFile; + } + } + + fclose(fp); } NVault::~NVault() @@ -44,7 +56,9 @@ VaultError NVault::_ReadFromFile() FILE *fp = fopen(m_File.c_str(), "rb"); if (!fp) + { return Vault_NoFile; + } //this is a little more optimized than the other version in the journal <_< //I could optimize this more by embedding the position in the hash table but... // the hash function can be changed. this could be fixed by storing a string and its @@ -123,7 +137,9 @@ bool NVault::_SaveToFile() FILE *fp = fopen(m_File.c_str(), "wb"); if (!fp) + { return false; + } BinaryWriter bw(fp); @@ -195,7 +211,11 @@ bool NVault::Open() m_Journal->Replay(&m_Hash); m_Journal->Erase(); - m_Journal->Begin(); + if (!m_Journal->Begin()) + { + delete m_Journal; + m_Journal = NULL; + } m_Open = true; @@ -285,8 +305,14 @@ bool NVault::GetValue(const char *key, time_t &stamp, char buffer[], size_t len) IVault *VaultMngr::OpenVault(const char *file) { - NVault *pVault = new NVault(file); - + NVault *pVault; + try + { + pVault = new NVault(file); + } catch (...) { + pVault = NULL; + } + return static_cast(pVault); } diff --git a/dlls/nvault/amxxapi.cpp b/dlls/nvault/amxxapi.cpp index 5ba80cc8..619028d3 100755 --- a/dlls/nvault/amxxapi.cpp +++ b/dlls/nvault/amxxapi.cpp @@ -36,8 +36,8 @@ static cell nvault_open(AMX *amx, cell *params) if (strcmp(g_Vaults.at(i)->GetFilename(), file) == 0) return i; } - NVault *v = new NVault(file); - if (!v->Open()) + NVault *v = (NVault *)g_VaultMngr.OpenVault(file); + if (v == NULL || !v->Open()) { delete v; return -1; diff --git a/plugins/testsuite/nvault_test.sma b/plugins/testsuite/nvault_test.sma new file mode 100644 index 00000000..0a1dda71 --- /dev/null +++ b/plugins/testsuite/nvault_test.sma @@ -0,0 +1,20 @@ +#include +#include + +public plugin_init() +{ + register_plugin("nVault Test", "1.0", "BAILOPAN") + + register_srvcmd("test_nvault", "Command_TestNvault") +} + +public Command_TestNvault() +{ + new v = nvault_open("://:/1/R!?#@41345$%:$") + server_print("Vault value: %d (expected: %d)", v, -1) + + if (v != -1) + { + nvault_close(v) + } +}