2014-08-04 10:38:42 +00:00
|
|
|
// vim: set ts=4 sw=4 tw=99 noet:
|
|
|
|
//
|
|
|
|
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
|
|
|
// Copyright (C) The AMX Mod X Development Team.
|
|
|
|
//
|
|
|
|
// This software is licensed under the GNU General Public License, version 3 or higher.
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
|
|
|
// https://alliedmods.net/amxmodx-license
|
|
|
|
|
|
|
|
//
|
|
|
|
// NVault Module
|
|
|
|
//
|
|
|
|
|
2006-06-04 07:49:07 +00:00
|
|
|
#include <stdio.h>
|
2006-04-07 10:25:45 +00:00
|
|
|
#include "amxxmodule.h"
|
2005-07-31 06:07:48 +00:00
|
|
|
#include "NVault.h"
|
|
|
|
#include "Binary.h"
|
2014-08-10 14:57:53 +00:00
|
|
|
#include <am-string.h>
|
2005-07-31 06:07:48 +00:00
|
|
|
|
2006-08-20 21:23:38 +00:00
|
|
|
/**
|
|
|
|
* :TODO: This beast calls strcpy()/new() way too much by creating new strings on the stack.
|
|
|
|
* That's easily remedied and it should be fixed?
|
|
|
|
* ---bail
|
|
|
|
*/
|
|
|
|
|
2005-07-31 06:07:48 +00:00
|
|
|
NVault::NVault(const char *file)
|
|
|
|
{
|
2014-08-10 14:57:53 +00:00
|
|
|
m_File = file;
|
2005-07-31 06:07:48 +00:00
|
|
|
m_Journal = NULL;
|
|
|
|
m_Open = false;
|
2006-08-18 22:46:07 +00:00
|
|
|
|
2014-08-10 14:57:53 +00:00
|
|
|
FILE *fp = fopen(m_File.chars(), "rb");
|
2006-08-18 22:46:07 +00:00
|
|
|
if (!fp)
|
|
|
|
{
|
2014-08-10 14:57:53 +00:00
|
|
|
fp = fopen(m_File.chars(), "wb");
|
2006-08-18 22:46:07 +00:00
|
|
|
if (!fp)
|
|
|
|
{
|
2008-03-04 19:06:39 +00:00
|
|
|
this->m_Valid = false;
|
|
|
|
return;
|
2006-08-18 22:46:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-04 19:06:39 +00:00
|
|
|
this->m_Valid = true;
|
2006-08-18 22:46:07 +00:00
|
|
|
fclose(fp);
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NVault::~NVault()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
VaultError NVault::_ReadFromFile()
|
|
|
|
{
|
2014-08-10 14:57:53 +00:00
|
|
|
FILE *fp = fopen(m_File.chars(), "rb");
|
2005-07-31 06:07:48 +00:00
|
|
|
|
|
|
|
if (!fp)
|
2006-08-18 22:46:07 +00:00
|
|
|
{
|
2005-07-31 06:07:48 +00:00
|
|
|
return Vault_NoFile;
|
2006-08-18 22:46:07 +00:00
|
|
|
}
|
2005-07-31 06:07:48 +00:00
|
|
|
//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
|
|
|
|
// resulting hash, and the entries could be ignored therein, but not right now.
|
|
|
|
//Also the string class could have embedded binary reading (like it does for text files)
|
|
|
|
BinaryReader br(fp);
|
|
|
|
|
|
|
|
uint8_t oldkeylen=0;
|
|
|
|
uint16_t oldvallen=0;
|
|
|
|
uint8_t keylen;
|
|
|
|
uint16_t vallen;
|
|
|
|
time_t stamp;
|
|
|
|
char *key = NULL;
|
|
|
|
char *val = NULL;
|
|
|
|
|
2008-03-04 19:06:39 +00:00
|
|
|
// try
|
|
|
|
// {
|
|
|
|
uint32_t magic;
|
|
|
|
if (!br.ReadUInt32(magic)) goto fail;
|
|
|
|
|
2005-07-31 06:07:48 +00:00
|
|
|
if (magic != VAULT_MAGIC)
|
|
|
|
return Vault_BadFile;
|
2008-03-04 19:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
uint16_t version;
|
|
|
|
if (!br.ReadUInt16(version)) goto fail;
|
|
|
|
|
2005-07-31 06:07:48 +00:00
|
|
|
if (version != VAULT_VERSION)
|
|
|
|
return Vault_OldFile;
|
2008-03-04 19:06:39 +00:00
|
|
|
|
|
|
|
int32_t entries;
|
|
|
|
|
|
|
|
if (!br.ReadInt32(entries)) goto fail;
|
|
|
|
|
|
|
|
|
|
|
|
int32_t temp;
|
2005-07-31 06:07:48 +00:00
|
|
|
for (int32_t i=0; i<entries; i++)
|
|
|
|
{
|
2008-03-04 19:06:39 +00:00
|
|
|
if (!br.ReadInt32(temp)) goto fail;
|
|
|
|
|
|
|
|
stamp = static_cast<time_t>(temp);
|
|
|
|
|
|
|
|
if (!br.ReadUInt8(keylen)) goto fail;
|
|
|
|
if (!br.ReadUInt16(vallen)) goto fail;
|
|
|
|
|
2014-08-10 18:13:22 +00:00
|
|
|
if (!keylen || keylen > oldkeylen)
|
2005-07-31 06:07:48 +00:00
|
|
|
{
|
|
|
|
if (key)
|
|
|
|
delete [] key;
|
|
|
|
key = new char[keylen + 1];
|
|
|
|
oldkeylen = keylen;
|
|
|
|
}
|
2014-08-10 18:13:22 +00:00
|
|
|
if (!vallen || vallen > oldvallen)
|
2005-07-31 06:07:48 +00:00
|
|
|
{
|
|
|
|
if (val)
|
|
|
|
delete [] val;
|
|
|
|
val = new char[vallen + 1];
|
|
|
|
oldvallen = vallen;
|
|
|
|
}
|
2008-03-04 19:06:39 +00:00
|
|
|
|
|
|
|
if (!br.ReadChars(key, keylen)) goto fail;
|
|
|
|
if (!br.ReadChars(val, vallen)) goto fail;
|
|
|
|
|
2005-07-31 06:07:48 +00:00
|
|
|
key[keylen] = '\0';
|
|
|
|
val[vallen] = '\0';
|
2014-08-10 14:57:53 +00:00
|
|
|
|
|
|
|
ArrayInfo info; info.value = val; info.stamp = stamp;
|
|
|
|
m_Hash.replace(key, info);
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
2008-03-04 19:06:39 +00:00
|
|
|
|
|
|
|
// } catch (...) {
|
|
|
|
|
|
|
|
goto success;
|
|
|
|
fail:
|
|
|
|
if (key)
|
|
|
|
{
|
|
|
|
delete [] key;
|
|
|
|
key = NULL;
|
|
|
|
}
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
delete [] val;
|
|
|
|
val = NULL;
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
2008-03-04 19:06:39 +00:00
|
|
|
fclose(fp);
|
|
|
|
return Vault_Read;
|
|
|
|
// }
|
2005-07-31 06:07:48 +00:00
|
|
|
|
2008-03-04 19:06:39 +00:00
|
|
|
success:
|
2005-07-31 06:07:48 +00:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return Vault_Ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NVault::_SaveToFile()
|
|
|
|
{
|
2014-08-10 14:57:53 +00:00
|
|
|
FILE *fp = fopen(m_File.chars(), "wb");
|
2005-07-31 06:07:48 +00:00
|
|
|
|
|
|
|
if (!fp)
|
2006-08-18 22:46:07 +00:00
|
|
|
{
|
2005-07-31 06:07:48 +00:00
|
|
|
return false;
|
2006-08-18 22:46:07 +00:00
|
|
|
}
|
2005-07-31 06:07:48 +00:00
|
|
|
|
|
|
|
BinaryWriter bw(fp);
|
|
|
|
|
2008-03-04 19:06:39 +00:00
|
|
|
// try
|
|
|
|
// {
|
|
|
|
uint32_t magic = VAULT_MAGIC;
|
|
|
|
uint16_t version = VAULT_VERSION;
|
2005-07-31 06:07:48 +00:00
|
|
|
|
2008-03-04 19:06:39 +00:00
|
|
|
time_t stamp;
|
2014-08-10 14:57:53 +00:00
|
|
|
ke::AString key;
|
|
|
|
ke::AString val;
|
2005-07-31 06:07:48 +00:00
|
|
|
|
2014-08-10 14:57:53 +00:00
|
|
|
StringHashMap<ArrayInfo>::iterator iter = m_Hash.iter();
|
2008-03-04 19:06:39 +00:00
|
|
|
|
|
|
|
if (!bw.WriteUInt32(magic)) goto fail;
|
|
|
|
if (!bw.WriteUInt16(version)) goto fail;
|
|
|
|
|
2014-08-10 14:57:53 +00:00
|
|
|
if (!bw.WriteUInt32( m_Hash.elements() )) goto fail;
|
2005-07-31 06:07:48 +00:00
|
|
|
|
2014-08-10 14:57:53 +00:00
|
|
|
while (!iter.empty())
|
2008-03-04 19:06:39 +00:00
|
|
|
{
|
|
|
|
key = (*iter).key;
|
2014-08-10 14:57:53 +00:00
|
|
|
val = (*iter).value.value;
|
|
|
|
stamp = (*iter).value.stamp;
|
2008-03-04 19:06:39 +00:00
|
|
|
|
|
|
|
if (!bw.WriteInt32(static_cast<int32_t>(stamp))) goto fail;;
|
2014-08-10 14:57:53 +00:00
|
|
|
if (!bw.WriteUInt8( key.length() )) goto fail;
|
|
|
|
if (!bw.WriteUInt16( val.length() )) goto fail;
|
|
|
|
if (!bw.WriteChars( key.chars(), key.length() )) goto fail;
|
|
|
|
if (!bw.WriteChars( val.chars(), val.length() )) goto fail;
|
|
|
|
|
|
|
|
iter.next();
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
2008-03-04 19:06:39 +00:00
|
|
|
|
|
|
|
goto success;
|
|
|
|
// } catch (...) {
|
|
|
|
|
|
|
|
fail:
|
|
|
|
fclose(fp);
|
|
|
|
return false;
|
|
|
|
// }
|
|
|
|
success:
|
2005-07-31 06:07:48 +00:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
const char *NVault::GetValue(const char *key)
|
|
|
|
{
|
2014-08-10 18:13:22 +00:00
|
|
|
StringHashMap<ArrayInfo>::Result r = m_Hash.find(key);
|
|
|
|
if (!r.found())
|
2005-08-01 19:56:54 +00:00
|
|
|
{
|
2014-08-10 18:13:22 +00:00
|
|
|
return "";
|
2005-08-01 19:56:54 +00:00
|
|
|
}
|
2014-08-10 14:57:53 +00:00
|
|
|
|
2014-08-10 18:13:22 +00:00
|
|
|
return r->value.value.chars();
|
2005-08-01 19:56:54 +00:00
|
|
|
}
|
|
|
|
|
2005-07-31 06:07:48 +00:00
|
|
|
bool NVault::Open()
|
|
|
|
{
|
|
|
|
_ReadFromFile();
|
|
|
|
|
2014-08-10 14:57:53 +00:00
|
|
|
char *journal_name = new char[m_File.length() + 10];
|
|
|
|
strcpy(journal_name, m_File.chars());
|
2005-07-31 06:07:48 +00:00
|
|
|
|
|
|
|
char *pos = strstr(journal_name, ".vault");
|
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
strcpy(pos, ".journal");
|
|
|
|
} else {
|
|
|
|
strcat(journal_name, ".journal");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Journal = new Journal(journal_name);
|
|
|
|
delete [] journal_name;
|
|
|
|
|
|
|
|
m_Journal->Replay(&m_Hash);
|
|
|
|
m_Journal->Erase();
|
2006-08-18 22:46:07 +00:00
|
|
|
if (!m_Journal->Begin())
|
|
|
|
{
|
|
|
|
delete m_Journal;
|
|
|
|
m_Journal = NULL;
|
|
|
|
}
|
2005-07-31 06:07:48 +00:00
|
|
|
|
|
|
|
m_Open = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NVault::Close()
|
|
|
|
{
|
|
|
|
if (!m_Open)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_SaveToFile();
|
2013-07-02 08:20:21 +00:00
|
|
|
|
|
|
|
if (m_Journal)
|
|
|
|
{
|
|
|
|
m_Journal->End();
|
|
|
|
m_Journal->Erase();
|
|
|
|
}
|
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
m_Open = false;
|
2005-07-31 06:07:48 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NVault::SetValue(const char *key, const char *val)
|
|
|
|
{
|
|
|
|
if (m_Journal)
|
|
|
|
m_Journal->Write_Insert(key, val, time(NULL));
|
2014-08-10 14:57:53 +00:00
|
|
|
|
|
|
|
ArrayInfo info; info.value = val; info.stamp = time(NULL);
|
|
|
|
m_Hash.replace(key, info);
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NVault::SetValue(const char *key, const char *val, time_t stamp)
|
|
|
|
{
|
|
|
|
if (m_Journal)
|
|
|
|
m_Journal->Write_Insert(key, val, stamp);
|
2014-08-10 14:57:53 +00:00
|
|
|
|
|
|
|
ArrayInfo info; info.value = val; info.stamp = stamp;
|
|
|
|
m_Hash.replace(key, info);
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NVault::Remove(const char *key)
|
|
|
|
{
|
|
|
|
if (m_Journal)
|
|
|
|
m_Journal->Write_Remove(key);
|
2014-08-10 14:57:53 +00:00
|
|
|
|
|
|
|
m_Hash.remove(ke::AString(key).chars());
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NVault::Clear()
|
|
|
|
{
|
|
|
|
if (m_Journal)
|
|
|
|
m_Journal->Write_Clear();
|
2014-08-10 14:57:53 +00:00
|
|
|
|
|
|
|
m_Hash.clear();
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t NVault::Items()
|
|
|
|
{
|
2014-08-10 14:57:53 +00:00
|
|
|
return m_Hash.elements();
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t NVault::Prune(time_t start, time_t end)
|
|
|
|
{
|
|
|
|
if (m_Journal)
|
|
|
|
m_Journal->Write_Prune(start, end);
|
2014-08-10 14:57:53 +00:00
|
|
|
|
|
|
|
size_t removed = 0;
|
|
|
|
|
|
|
|
for (StringHashMap<ArrayInfo>::iterator iter = m_Hash.iter(); !iter.empty(); iter.next())
|
|
|
|
{
|
|
|
|
time_t stamp = iter->value.stamp;
|
|
|
|
bool remove = false;
|
|
|
|
|
|
|
|
if (stamp != 0)
|
|
|
|
{
|
|
|
|
if (start == 0 && end == 0)
|
|
|
|
remove = true;
|
|
|
|
else if (start == 0 && stamp < end)
|
|
|
|
remove = true;
|
|
|
|
else if (end == 0 && stamp > start)
|
|
|
|
remove = true;
|
|
|
|
else if (stamp > start && stamp < end)
|
|
|
|
remove = true;
|
|
|
|
|
|
|
|
if (remove)
|
|
|
|
{
|
|
|
|
iter.erase();
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed;
|
2005-07-31 06:07:48 +00:00
|
|
|
}
|
|
|
|
|
2006-08-20 21:23:38 +00:00
|
|
|
void NVault::Touch(const char *key, time_t stamp)
|
|
|
|
{
|
2014-08-10 14:57:53 +00:00
|
|
|
StringHashMap<ArrayInfo>::Insert i = m_Hash.findForAdd(key);
|
|
|
|
if (!i.found())
|
2006-08-20 21:23:38 +00:00
|
|
|
{
|
2014-08-10 14:57:53 +00:00
|
|
|
if (!m_Hash.add(i, key))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-10 18:13:22 +00:00
|
|
|
SetValue(key, "", time(NULL));
|
2006-08-20 21:23:38 +00:00
|
|
|
}
|
|
|
|
|
2014-08-10 14:57:53 +00:00
|
|
|
i->value.stamp = stamp;
|
2006-08-20 21:23:38 +00:00
|
|
|
}
|
|
|
|
|
2005-07-31 06:07:48 +00:00
|
|
|
bool NVault::GetValue(const char *key, time_t &stamp, char buffer[], size_t len)
|
|
|
|
{
|
2014-08-10 14:57:53 +00:00
|
|
|
ArrayInfo result;
|
|
|
|
|
|
|
|
if (!m_Hash.retrieve(key, &result))
|
2005-07-31 06:07:48 +00:00
|
|
|
{
|
|
|
|
buffer[0] = '\0';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-10 14:57:53 +00:00
|
|
|
stamp = result.stamp;
|
|
|
|
UTIL_Format(buffer, len, "%s", result.value.chars());
|
2005-07-31 06:07:48 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2005-08-01 19:56:54 +00:00
|
|
|
|
|
|
|
IVault *VaultMngr::OpenVault(const char *file)
|
|
|
|
{
|
2006-08-18 22:46:07 +00:00
|
|
|
NVault *pVault;
|
2008-03-04 19:06:39 +00:00
|
|
|
//try
|
|
|
|
//{
|
|
|
|
pVault = new NVault(file);
|
|
|
|
|
|
|
|
// } catch (...) {
|
|
|
|
if (!pVault->isValid())
|
2006-08-18 22:46:07 +00:00
|
|
|
{
|
2008-03-04 19:06:39 +00:00
|
|
|
delete pVault;
|
2006-08-18 22:46:07 +00:00
|
|
|
pVault = NULL;
|
|
|
|
}
|
|
|
|
|
2005-08-01 19:56:54 +00:00
|
|
|
return static_cast<IVault *>(pVault);
|
|
|
|
}
|
2005-08-01 20:54:41 +00:00
|
|
|
|