mirror of
https://github.com/alliedmodders/amxmodx.git
synced 2024-12-24 13:55:36 +03:00
Fixed bug am42307 (nvault_open did not return -1 on failure)
This commit is contained in:
parent
a8b4ebbe70
commit
042b9f7f4b
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
@ -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,8 +305,14 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
20
plugins/testsuite/nvault_test.sma
Normal file
20
plugins/testsuite/nvault_test.sma
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user