Fixed bug am42307 (nvault_open did not return -1 on failure)

This commit is contained in:
David Anderson 2006-08-18 22:46:07 +00:00
parent a8b4ebbe70
commit 042b9f7f4b
5 changed files with 56 additions and 5 deletions

View File

@ -20,6 +20,9 @@ public:
class IVaultMngr class IVaultMngr
{ {
public: public:
/**
* Note: Will return NULL if the vault failed to create.
*/
virtual IVault *OpenVault(const char *name) =0; virtual IVault *OpenVault(const char *name) =0;
}; };

View File

@ -18,7 +18,9 @@ int Journal::Replay(VaultMap *pMap)
{ {
m_fp = fopen(m_File.c_str(), "rb"); m_fp = fopen(m_File.c_str(), "rb");
if (!m_fp) if (!m_fp)
{
return -1; return -1;
}
BinaryReader br(m_fp); BinaryReader br(m_fp);

View File

@ -32,6 +32,18 @@ NVault::NVault(const char *file)
m_File.assign(file); m_File.assign(file);
m_Journal = NULL; m_Journal = NULL;
m_Open = false; 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() NVault::~NVault()
@ -44,7 +56,9 @@ VaultError NVault::_ReadFromFile()
FILE *fp = fopen(m_File.c_str(), "rb"); FILE *fp = fopen(m_File.c_str(), "rb");
if (!fp) if (!fp)
{
return Vault_NoFile; return Vault_NoFile;
}
//this is a little more optimized than the other version in the journal <_< //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... //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 // 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"); FILE *fp = fopen(m_File.c_str(), "wb");
if (!fp) if (!fp)
{
return false; return false;
}
BinaryWriter bw(fp); BinaryWriter bw(fp);
@ -195,7 +211,11 @@ bool NVault::Open()
m_Journal->Replay(&m_Hash); m_Journal->Replay(&m_Hash);
m_Journal->Erase(); m_Journal->Erase();
m_Journal->Begin(); if (!m_Journal->Begin())
{
delete m_Journal;
m_Journal = NULL;
}
m_Open = true; m_Open = true;
@ -285,7 +305,13 @@ bool NVault::GetValue(const char *key, time_t &stamp, char buffer[], size_t len)
IVault *VaultMngr::OpenVault(const char *file) IVault *VaultMngr::OpenVault(const char *file)
{ {
NVault *pVault = new NVault(file); NVault *pVault;
try
{
pVault = new NVault(file);
} catch (...) {
pVault = NULL;
}
return static_cast<IVault *>(pVault); return static_cast<IVault *>(pVault);
} }

View File

@ -36,8 +36,8 @@ static cell nvault_open(AMX *amx, cell *params)
if (strcmp(g_Vaults.at(i)->GetFilename(), file) == 0) if (strcmp(g_Vaults.at(i)->GetFilename(), file) == 0)
return i; return i;
} }
NVault *v = new NVault(file); NVault *v = (NVault *)g_VaultMngr.OpenVault(file);
if (!v->Open()) if (v == NULL || !v->Open())
{ {
delete v; delete v;
return -1; return -1;

View File

@ -0,0 +1,20 @@
#include <amxmodx>
#include <nvault>
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)
}
}